Initialize _res_header variable to zero before use in process_client_cmd
[ust.git] / usttrace
CommitLineData
ec064713 1#!/bin/sh
eb6adb25 2
4a7c7161
PMF
3# usttrace by Pierre-Marc Fournier 2009
4# Distributed under the GPLv2.
5
ec064713 6error() {
63f16f2e
PMF
7 echo "$0: error: $1" 2>/dev/stderr
8}
9
ec064713 10sighandler() {
87d40fcf 11 echo "Caught Ctrl-C"
37b70e91 12 if [ -z "${UST_CONSUMERD_PID}" ]; then
ec064713 13 UST_CONSUMERD_PID=`cat $pidfilepath`
87d40fcf
PMF
14 fi
15 # Tell the daemon to die
a1bece55 16 kill -TERM "${UST_CONSUMERD_PID}"
87d40fcf 17
9dc7b7ff 18 echo "Waiting for ust-consumerd to shutdown..."
37b70e91 19 wait "${UST_CONSUMERD_PID}"
87d40fcf
PMF
20
21 rm "$pidfilepath"
22
23 exit 0;
24}
25
cd814711 26USTTRACE_DIR="$(dirname $0)"
9dc7b7ff 27if [ -x "${USTTRACE_DIR}/ust-consumerd/ust-consumerd" ] ; then
6b22d5c1 28 # Use the not installed libraries instead
9dc7b7ff 29 UST_CONSUMERD="${USTTRACE_DIR}/ust-consumerd/ust-consumerd"
e8300fb7 30 LIBINTERFORK_PATH="${USTTRACE_DIR}/libustfork/.libs/libustfork.so"
d169a27d 31 LIBMALLOCWRAP_PATH="${USTTRACE_DIR}/libustinstr-malloc/.libs/libustinstr-malloc.so"
6b22d5c1
JB
32 LIBUST_PATH="${USTTRACE_DIR}/libust/.libs/libust.so"
33else
34 # Use the libraries that the dynamic link finds
9dc7b7ff
NC
35 UST_CONSUMERD="ust-consumerd"
36 if [ ! -x "$(which ust-consumerd 2>/dev/null)" ]; then
37 error "cannot find an executable ust-consumerd; make sure its location is in the PATH"
63f16f2e
PMF
38 exit 1
39 fi
e8300fb7 40 LIBINTERFORK_PATH="libustfork.so"
d169a27d 41 LIBMALLOCWRAP_PATH="libustinstr-malloc.so"
9ccc0699 42 LIBUST_PATH="libust.so.0"
6b22d5c1 43fi
eb6adb25 44
cd814711 45BASE_TRACE_DIR="${HOME}/.usttraces"
eb6adb25 46
ec064713 47usage() {
b13ba584
PMF
48 echo "usage: $0 OPTIONS COMMAND" 2>/dev/stderr
49 echo "" 2>/dev/stderr
50 echo "Options:" 2>/dev/stderr
51 echo " -l Runtime link with UST library." 2>/dev/stderr
52 echo " (Needed only if program was not linked at compile time with libust.)" 2>/dev/stderr
53 echo " -L Add path to ust libraries to LD_LIBRARY_PATH." 2>/dev/stderr
54 echo " -m Instrument malloc calls." 2>/dev/stderr
55 echo " -f Also trace forked processes." 2>/dev/stderr
89a10f62 56 echo " -s Use system-wide daemon instead of creating one for this session." 2>/dev/stderr
223f2e7c
AH
57 echo " -S Specify the subbuffer size." 2>/dev/stderr
58 echo " -N Specify the number of subbuffers." 2>/dev/stderr
d1fa5f35 59 echo " -o Output directory of the trace." 2>/dev/stderr
eb6adb25
PMF
60}
61
d1fa5f35 62while getopts ":hlLmfsWS:N:o:" options; do
b13ba584
PMF
63 case $options in
64 l) arg_preload_libust=1;;
65 L) arg_ld_std_ust=1;;
66 m) arg_preload_malloc=1;;
67 f) arg_preload_fork=1;;
89a10f62 68 s) arg_syswide_daemon=1;;
c1a44c29 69 W) where=1;;
223f2e7c
AH
70 S) export UST_SUBBUF_SIZE=$OPTARG;;
71 N) export UST_SUBBUF_NUM=$OPTARG;;
d1fa5f35 72 o) OUTPUT_DIR=$OPTARG;;
b13ba584
PMF
73 h) usage;
74 exit 0;;
2a3fba49 75 \?) usage
b13ba584 76 exit 1;;
2a3fba49 77 *) usage
b13ba584
PMF
78 exit 1;;
79 esac
80done
81shift $(($OPTIND - 1))
82
c1a44c29
PMF
83if [ -n "$where" ]; then
84 echo $BASE_TRACE_DIR/$(ls "$BASE_TRACE_DIR" | tail -n 1)
85 exit 0
86fi
87
eb6adb25 88# Prepare vars
6b22d5c1 89CMD=$*
eb6adb25
PMF
90
91# Validate input
92if [ -z "$HOME" ];
93then
94 error "no home specified"
95fi
96
97if [ -z "$CMD" ];
98then
99 error "no command specified"
100 usage;
101 exit 1
102fi
103
104# Create directory for trace output
d1fa5f35
DG
105if [ -n "$OUTPUT_DIR" ]; then
106 OUTDIR=$OUTPUT_DIR
107else
108 DATESTRING="$(hostname)-$(date +%Y%m%d%H%M%S%N)"
109 OUTDIR="$BASE_TRACE_DIR/$DATESTRING"
110fi
111
112# Check if directory exist
113if [ ! -d "$OUTDIR" ]; then
114 mkdir -p $OUTDIR
115 if [ $? -eq 1 ]; then
116 exit 1
117 fi
118fi
eb6adb25 119
9dc7b7ff 120# Choose ust-consumerd socket path
37b70e91 121UST_CONSUMERD_SOCKPATH="/tmp/ust-consumerd-sock-$$"
eb6adb25 122
89a10f62
PMF
123if [ "$arg_syswide_daemon" != "1" ];
124then
9dc7b7ff 125 pidfilepath="/tmp/usttrace-$USER-$(date +%Y%m%d%H%M%S%N)-ust-consumerd-pid"
87d40fcf 126 trap "sighandler $pidfilepath" SIGINT
8232f1db 127 mkfifo -m 0600 "$pidfilepath"
89a10f62 128 # Start daemon
37b70e91 129 ${UST_CONSUMERD} --pidfile "$pidfilepath" -s "${UST_CONSUMERD_SOCKPATH}" -o "$OUTDIR" >"$OUTDIR/ust-consumerd.log" 2>&1 &
9dc7b7ff
NC
130 # ust-consumerd sets up its server socket
131 # ust-consumerd opens the pidfile, blocks because no one has opened it
0f3af9f0
PMF
132 # we open pidfile
133 # we block reading pidfile
9dc7b7ff
NC
134 # ust-consumerd writes to pidfile
135 # ust-consumerd closes pidfile
0f3af9f0 136 # we unblock reading pidfile
ec064713 137 UST_CONSUMERD_PID=`cat $pidfilepath`
37b70e91 138 export UST_DAEMON_SOCKET="${UST_CONSUMERD_SOCKPATH}"
89a10f62
PMF
139fi
140
eb6adb25 141# Establish the environment for the command
6b22d5c1
JB
142(
143 export UST_TRACE=1
144 export UST_AUTOPROBE=1
145
146 if [ "$arg_preload_libust" = "1" ];
147 then
cdf50e9e
MD
148 if [ -n "${LIBUST_PATH%libust.so}" ];
149 then
150 if [ -n "$LD_LIBRARY_PATH" ];
151 then
152 export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${LIBUST_PATH%libust.so}"
153 else
154 export LD_LIBRARY_PATH="${LIBUST_PATH%libust.so}"
155 fi
156 fi
157 if [ -n "$LIBUST_PATH" ];
158 then
159 if [ -n "$LD_PRELOAD" ];
160 then
161 export LD_PRELOAD="$LD_PRELOAD:$LIBUST_PATH"
162 else
163 export LD_PRELOAD="$LIBUST_PATH"
164 fi
6b22d5c1 165 fi
6b22d5c1
JB
166 fi
167
cdf50e9e 168 if [ "$arg_ld_std_ust" = "1" ] && [ -n "${LIBUST_PATH%libust.so}" ];
6b22d5c1 169 then
cdf50e9e
MD
170 if [ -n "$LD_LIBRARY_PATH" ];
171 then
172 export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${LIBUST_PATH%libust.so}"
173 else
174 export LD_LIBRARY_PATH="${LIBUST_PATH%libust.so}"
6b22d5c1
JB
175 fi
176 fi
177
cdf50e9e 178 if [ "$arg_preload_malloc" = "1" ] && [ -n "$LIBMALLOCWRAP_PATH" ];
6b22d5c1 179 then
cdf50e9e
MD
180 if [ -n "$LD_PRELOAD" ];
181 then
182 export LD_PRELOAD="$LD_PRELOAD:$LIBMALLOCWRAP_PATH"
183 else
184 export LD_PRELOAD="$LIBMALLOCWRAP_PATH"
185 fi
6b22d5c1
JB
186 fi
187
cdf50e9e 188 if [ "$arg_preload_fork" = "1" ] && [ -n "$LIBINTERFORK_PATH" ];
6b22d5c1 189 then
cdf50e9e
MD
190 if [ -n "$LD_PRELOAD" ];
191 then
192 export LD_PRELOAD="$LD_PRELOAD:$LIBINTERFORK_PATH"
193 else
194 export LD_PRELOAD="$LIBINTERFORK_PATH"
195 fi
6b22d5c1 196 fi
b13ba584 197
eb6adb25 198# Execute the command
6b22d5c1
JB
199 $CMD 2>&1
200) | tee "$OUTDIR/app.log"
eb6adb25
PMF
201
202## Because of the keepalive mechanism, we're sure that by the time
203## we get here, the daemon is connected to all the buffers that still exist.
204## Therefore we can politely ask it to die when it's done.
205
89a10f62
PMF
206if [ "$arg_syswide_daemon" != "1" ];
207then
208 # Tell the daemon to die
a1bece55 209 kill -TERM "${UST_CONSUMERD_PID}"
89a10f62 210
9dc7b7ff 211 echo "Waiting for ust-consumerd to shutdown..."
37b70e91 212 wait "${UST_CONSUMERD_PID}"
b924c127
PMF
213
214 rm "$pidfilepath"
89a10f62 215fi
eb6adb25 216
89a10f62 217echo "Trace was output in: " $OUTDIR
This page took 0.040843 seconds and 4 git commands to generate.