1#!/bin/bash
2# Copyright (C) 1999-2022 Free Software Foundation, Inc.
3# This file is part of the GNU C Library.
4
5# The GNU C Library is free software; you can redistribute it and/or
6# modify it under the terms of the GNU Lesser General Public
7# License as published by the Free Software Foundation; either
8# version 2.1 of the License, or (at your option) any later version.
9
10# The GNU C Library is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13# Lesser General Public License for more details.
14
15# You should have received a copy of the GNU Lesser General Public
16# License along with the GNU C Library; if not, see
17# <https://www.gnu.org/licenses/>.
18
19memusageso='@SLIBDIR@/libmemusage.so'
20memusagestat='@BINDIR@/memusagestat'
21TEXTDOMAIN=libc
22
23# Print usage message.
24do_usage() {
25  printf >&2 $"Try \`%s --help' or \`%s --usage' for more information.\n" memusage memusage
26  exit 1
27}
28
29# Message for missing argument.
30do_missing_arg() {
31  printf >&2 $"%s: option '%s' requires an argument\n" memusage "$1"
32  do_usage
33}
34
35# Print help message
36do_help() {
37  echo $"Usage: memusage [OPTION]... PROGRAM [PROGRAMOPTION]...
38Profile memory usage of PROGRAM.
39
40   -n,--progname=NAME     Name of the program file to profile
41   -p,--png=FILE          Generate PNG graphic and store it in FILE
42   -d,--data=FILE         Generate binary data file and store it in FILE
43   -u,--unbuffered        Don't buffer output
44   -b,--buffer=SIZE       Collect SIZE entries before writing them out
45      --no-timer          Don't collect additional information through timer
46   -m,--mmap              Also trace mmap & friends
47
48   -?,--help              Print this help and exit
49      --usage             Give a short usage message
50   -V,--version           Print version information and exit
51
52 The following options only apply when generating graphical output:
53   -t,--time-based        Make graph linear in time
54   -T,--total             Also draw graph of total memory use
55      --title=STRING      Use STRING as title of the graph
56   -x,--x-size=SIZE       Make graphic SIZE pixels wide
57   -y,--y-size=SIZE       Make graphic SIZE pixels high
58
59Mandatory arguments to long options are also mandatory for any corresponding
60short options.
61
62"
63  printf $"For bug reporting instructions, please see:\\n%s.\\n" \
64    "@REPORT_BUGS_TO@"
65  exit 0
66}
67
68do_version() {
69  echo 'memusage @PKGVERSION@@VERSION@'
70  printf $"Copyright (C) %s Free Software Foundation, Inc.
71This is free software; see the source for copying conditions.  There is NO
72warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
73" "2022"
74  printf $"Written by %s.
75" "Ulrich Drepper"
76  exit 0
77}
78
79# These variables are local
80buffer=
81data=
82memusagestat_args=
83notimer=
84png=
85progname=
86tracemmap=
87
88# Process arguments.  But stop as soon as the program name is found.
89while test $# -gt 0; do
90  case "$1" in
91  -V | --v | --ve | --ver | --vers | --versi | --versio | --version)
92    do_version
93    ;;
94  -\? | --h | --he | --hel | --help)
95    do_help
96    ;;
97  --us | --usa | --usag | --usage)
98    echo $"Syntax: memusage [--data=FILE] [--progname=NAME] [--png=FILE] [--unbuffered]
99	    [--buffer=SIZE] [--no-timer] [--time-based] [--total]
100	    [--title=STRING] [--x-size=SIZE] [--y-size=SIZE]
101	    PROGRAM [PROGRAMOPTION]..."
102    exit 0
103    ;;
104  -n | --pr | --pro | --prog | --progn | --progna | --prognam | --progname)
105    if test $# -eq 1; then
106      do_missing_arg $1
107    fi
108    shift
109    progname="$1"
110    ;;
111  --pr=* | --pro=* | --prog=* | --progn=* | --progna=* | --prognam=* | --progname=*)
112    progname=${1##*=}
113    ;;
114  -p | --pn | --png)
115    if test $# -eq 1; then
116      do_missing_arg $1
117    fi
118    shift
119    png="$1"
120    ;;
121  --pn=* | --png=*)
122    png=${1##*=}
123    ;;
124  -d | --d | --da | --dat | --data)
125    if test $# -eq 1; then
126      do_missing_arg $1
127    fi
128    shift
129    data="$1"
130    ;;
131  --d=* | --da=* | --dat=* | --data=*)
132    data=${1##*=}
133    ;;
134  -u | --un | --unb | --unbu | --unbuf | --unbuff | --unbuffe | --unbuffer | --unbuffere | --unbuffered)
135    buffer=1
136    ;;
137  -b | --b | --bu | --buf | --buff | --buffe | --buffer)
138    if test $# -eq 1; then
139      do_missing_arg $1
140    fi
141    shift
142    buffer="$1"
143    ;;
144  --b=* | --bu=* | --buf=* | --buff=* | --buffe=* | --buffer=*)
145    buffer=${1##*=}
146    ;;
147  --n | --no | --no- | --no-t | --no-ti | --no-tim | --no-time | --no-timer)
148    notimer=yes
149    ;;
150  -m | --m | --mm | --mma | --mmap)
151    tracemmap=yes
152    ;;
153  -t | --tim | --time | --time- | --time-b | --time-ba | --time-bas | --time-base | --time-based)
154    memusagestat_args="$memusagestat_args -t"
155    ;;
156  -T | --to | --tot | --tota | --total)
157    memusagestat_args="$memusagestat_args -T"
158    ;;
159  --tit | --titl | --title)
160    if test $# -eq 1; then
161      do_missing_arg $1
162    fi
163    shift
164    memusagestat_args="$memusagestat_args -s $1"
165    ;;
166  --tit=* | --titl=* | --title=*)
167    memusagestat_args="$memusagestat_args -s ${1##*=}"
168    ;;
169  -x | --x | --x- | --x-s | --x-si | --x-siz | --x-size)
170    if test $# -eq 1; then
171      do_missing_arg $1
172    fi
173    shift
174    memusagestat_args="$memusagestat_args -x $1"
175    ;;
176  --x=* | --x-=* | --x-s=* | --x-si=* | --x-siz=* | --x-size=*)
177    memusagestat_args="$memusagestat_args -x ${1##*=}"
178    ;;
179  -y | --y | --y- | --y-s | --y-si | --y-siz | --y-size)
180    if test $# -eq 1; then
181      do_missing_arg $1
182    fi
183    shift
184    memusagestat_args="$memusagestat_args -y $1"
185    ;;
186  --y=* | --y-=* | --y-s=* | --y-si=* | --y-siz=* | --y-size=*)
187    memusagestat_args="$memusagestat_args -y ${1##*=}"
188    ;;
189  --p | --p=* | --t | --t=* | --ti | --ti=* | --u)
190    echo >&2 $"memusage: option \`${1##*=}' is ambiguous"
191    do_usage
192    ;;
193  --)
194    # Stop processing arguments.
195    shift
196    break
197    ;;
198  --*)
199    echo >&2 $"memusage: unrecognized option \`$1'"
200    do_usage
201    ;;
202  *)
203    # Unknown option.  This means the rest is the program name and parameters.
204    break
205    ;;
206  esac
207  shift
208done
209
210# See whether any arguments are left.
211if test $# -eq 0; then
212  echo >&2 $"No program name given"
213  do_usage
214fi
215
216# This will be in the environment.
217add_env="LD_PRELOAD=$memusageso"
218
219# Generate data file name.
220datafile=
221if test -n "$data"; then
222  datafile="$data"
223elif test -n "$png"; then
224  datafile=$(mktemp -t memusage.XXXXXX) || exit
225  trap 'rm -f "$datafile"; exit 1' HUP INT QUIT TERM PIPE
226fi
227if test -n "$datafile"; then
228  add_env="$add_env MEMUSAGE_OUTPUT=$datafile"
229fi
230
231# Set program name.
232if test -n "$progname"; then
233  add_env="$add_env MEMUSAGE_PROG_NAME=$progname"
234fi
235
236# Set buffer size.
237if test -n "$buffer"; then
238  add_env="$add_env MEMUSAGE_BUFFER_SIZE=$buffer"
239fi
240
241# Disable timers.
242if test -n "$notimer"; then
243  add_env="$add_env MEMUSAGE_NO_TIMER=yes"
244fi
245
246# Trace mmap.
247if test -n "$tracemmap"; then
248  add_env="$add_env MEMUSAGE_TRACE_MMAP=yes"
249fi
250
251# Execute the program itself.
252eval $add_env '"$@"'
253result=$?
254
255# Generate the PNG data file if wanted and there is something to generate
256# it from.
257if test -n "$png" -a -n "$datafile" -a -s "$datafile"; then
258  # Append extension .png if it isn't already there.
259  case $png in
260  *.png) ;;
261  *) png="$png.png" ;;
262  esac
263  $memusagestat $memusagestat_args "$datafile" "$png"
264fi
265
266if test -z "$data" -a -n "$datafile"; then
267  rm -f "$datafile"
268fi
269
270exit $result
271# Local Variables:
272#  mode:ksh
273# End:
274