3 # usttrace by Pierre-Marc Fournier 2009
4 # Distributed under the GPLv2.
7 echo "$0: error: $1" 2>/dev
/stderr
10 USTTRACE_DIR
="$(dirname $0)"
11 if [ -x "${USTTRACE_DIR}/ustd/ustd" ] ; then
12 # Use the not installed libraries instead
13 USTD
="${USTTRACE_DIR}/ustd/ustd"
14 LIBINTERFORK_PATH
="${USTTRACE_DIR}/libinterfork/.libs/libinterfork.so"
15 LIBMALLOCWRAP_PATH
="${USTTRACE_DIR}/libmallocwrap/.libs/libmallocwrap.so"
16 LIBUST_PATH
="${USTTRACE_DIR}/libust/.libs/libust.so"
18 # Use the libraries that the dynamic link finds
20 if [ ! -x "$(which ustd 2>/dev/null)" ]; then
21 error
"cannot find an executable ustd; make sure its location is in the PATH"
24 LIBINTERFORK_PATH
="libinterfork.so"
25 LIBMALLOCWRAP_PATH
="libmallocwrap.so"
26 LIBUST_PATH
="libust.so"
29 BASE_TRACE_DIR
="${HOME}/.usttraces"
32 echo "usage: $0 OPTIONS COMMAND" 2>/dev
/stderr
34 echo "Options:" 2>/dev
/stderr
35 echo " -l Runtime link with UST library." 2>/dev
/stderr
36 echo " (Needed only if program was not linked at compile time with libust.)" 2>/dev
/stderr
37 echo " -L Add path to ust libraries to LD_LIBRARY_PATH." 2>/dev
/stderr
38 echo " -m Instrument malloc calls." 2>/dev
/stderr
39 echo " -f Also trace forked processes." 2>/dev
/stderr
40 echo " -s Use system-wide daemon instead of creating one for this session." 2>/dev
/stderr
43 while getopts ":hlLmfs" options
; do
45 l
) arg_preload_libust
=1;;
47 m
) arg_preload_malloc
=1;;
48 f
) arg_preload_fork
=1;;
49 s
) arg_syswide_daemon
=1;;
58 shift $
(($OPTIND - 1))
66 error
"no home specified"
71 error
"no command specified"
76 # Create directory for trace output
77 DATESTRING
="$(hostname)-$(date +%Y%m%d%H%M%S)"
78 OUTDIR
="$BASE_TRACE_DIR/$DATESTRING"
82 SOCKPATH
="/tmp/ust-sock-$$"
84 if [ "$arg_syswide_daemon" != "1" ];
86 pidfilepath
="/tmp/usttrace-$USER-$(date +%Y%m%d%H%M%S%N)-ustd-pid"
87 mkfifo -m 0600 "$pidfilepath"
89 $USTD --pidfile "$pidfilepath" -s "$SOCKPATH" -o "$OUTDIR" >"$OUTDIR/ustd.log" 2>&1 &
90 # ustd sets up its server socket
91 # ustd opens the pidfile, blocks because no one has opened it
93 # we block reading pidfile
94 # ustd writes to pidfile
96 # we unblock reading pidfile
97 USTDPID
="$(<$pidfilepath)"
98 export UST_DAEMON_SOCKET
="$SOCKPATH"
101 # Establish the environment for the command
104 export UST_AUTOPROBE
=1
106 if [ "$arg_preload_libust" = "1" ];
108 if [ -n "${LIBUST_PATH%libust.so}" ] ; then
109 export LD_LIBRARY_PATH
="$LD_LIBRARY_PATH:${LIBUST_PATH%libust.so}"
111 export LD_PRELOAD
="$LD_PRELOAD:$LIBUST_PATH"
114 if [ "$arg_ld_std_ust" = "1" ];
116 if [ -n "$${LIBUST_PATH%libust.so}" ] ; then
117 export LD_LIBRARY_PATH
="$LD_LIBRARY_PATH:${LIBUST_PATH%libust.so}"
121 if [ "$arg_preload_malloc" = "1" ];
123 export LD_PRELOAD
="$LD_PRELOAD:$LIBMALLOCWRAP_PATH"
126 if [ "$arg_preload_fork" = "1" ];
128 export LD_PRELOAD
="$LD_PRELOAD:$LIBINTERFORK_PATH"
131 # Install a handler for SIGIO. This is the signal that will be sent by ustd to
132 # the traced program to trigger the creation of its listener thread. However,
133 # it is possible that the SIGIO will be sent after the shell fork, but before
134 # the exec of the command. If this handler isn't there, bash might terminate
135 # because of a unhandled signal.
137 # Execute the command
139 ) |
tee "$OUTDIR/app.log"
141 ## Because of the keepalive mechanism, we're sure that by the time
142 ## we get here, the daemon is connected to all the buffers that still exist.
143 ## Therefore we can politely ask it to die when it's done.
145 if [ "$arg_syswide_daemon" != "1" ];
147 # Tell the daemon to die
148 kill -SIGTERM "$USTDPID"
150 echo "Waiting for ustd to shutdown..."
156 echo "Trace was output in: " $OUTDIR
This page took 0.033464 seconds and 4 git commands to generate.