rename libinterfork -> libustfork
[lttng-ust.git] / usttrace
1 #!/bin/bash
2
3 # usttrace by Pierre-Marc Fournier 2009
4 # Distributed under the GPLv2.
5
6 function error() {
7 echo "$0: error: $1" 2>/dev/stderr
8 }
9
10 function sighandler() {
11 echo "Caught Ctrl-C"
12 if [ -z "$USTDPID" ]; then
13 USTDPID="$(<$pidfilepath)"
14 fi
15 # Tell the daemon to die
16 kill -SIGTERM "$USTDPID"
17
18 echo "Waiting for ustd to shutdown..."
19 wait "$USTDPID"
20
21 rm "$pidfilepath"
22
23 exit 0;
24 }
25
26 USTTRACE_DIR="$(dirname $0)"
27 if [ -x "${USTTRACE_DIR}/ustd/ustd" ] ; then
28 # Use the not installed libraries instead
29 USTD="${USTTRACE_DIR}/ustd/ustd"
30 LIBINTERFORK_PATH="${USTTRACE_DIR}/libustfork/.libs/libustfork.so"
31 LIBMALLOCWRAP_PATH="${USTTRACE_DIR}/libmallocwrap/.libs/libmallocwrap.so"
32 LIBUST_PATH="${USTTRACE_DIR}/libust/.libs/libust.so"
33 else
34 # Use the libraries that the dynamic link finds
35 USTD="ustd"
36 if [ ! -x "$(which ustd 2>/dev/null)" ]; then
37 error "cannot find an executable ustd; make sure its location is in the PATH"
38 exit 1
39 fi
40 LIBINTERFORK_PATH="libustfork.so"
41 LIBMALLOCWRAP_PATH="libmallocwrap.so"
42 LIBUST_PATH="libust.so.0"
43 fi
44
45 BASE_TRACE_DIR="${HOME}/.usttraces"
46
47 function usage () {
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
56 echo " -s Use system-wide daemon instead of creating one for this session." 2>/dev/stderr
57 }
58
59 while getopts ":hlLmfsW" options; do
60 case $options in
61 l) arg_preload_libust=1;;
62 L) arg_ld_std_ust=1;;
63 m) arg_preload_malloc=1;;
64 f) arg_preload_fork=1;;
65 s) arg_syswide_daemon=1;;
66 W) where=1;;
67 h) usage;
68 exit 0;;
69 \?) usage
70 exit 1;;
71 *) usage
72 exit 1;;
73 esac
74 done
75 shift $(($OPTIND - 1))
76
77 if [ -n "$where" ]; then
78 echo $BASE_TRACE_DIR/$(ls "$BASE_TRACE_DIR" | tail -n 1)
79 exit 0
80 fi
81
82 # Prepare vars
83 CMD=$*
84
85 # Validate input
86 if [ -z "$HOME" ];
87 then
88 error "no home specified"
89 fi
90
91 if [ -z "$CMD" ];
92 then
93 error "no command specified"
94 usage;
95 exit 1
96 fi
97
98 # Create directory for trace output
99 DATESTRING="$(hostname)-$(date +%Y%m%d%H%M%S%N)"
100 OUTDIR="$BASE_TRACE_DIR/$DATESTRING"
101 mkdir -p "$OUTDIR"
102
103 # Choose ustd socket path
104 USTDSOCKPATH="/tmp/ustd-sock-$$"
105
106 if [ "$arg_syswide_daemon" != "1" ];
107 then
108 pidfilepath="/tmp/usttrace-$USER-$(date +%Y%m%d%H%M%S%N)-ustd-pid"
109 trap "sighandler $pidfilepath" SIGINT
110 mkfifo -m 0600 "$pidfilepath"
111 # Start daemon
112 $USTD --pidfile "$pidfilepath" -s "$USTDSOCKPATH" -o "$OUTDIR" >"$OUTDIR/ustd.log" 2>&1 &
113 # ustd sets up its server socket
114 # ustd opens the pidfile, blocks because no one has opened it
115 # we open pidfile
116 # we block reading pidfile
117 # ustd writes to pidfile
118 # ustd closes pidfile
119 # we unblock reading pidfile
120 USTDPID="$(<$pidfilepath)"
121 export UST_DAEMON_SOCKET="$USTDSOCKPATH"
122 fi
123
124 # Establish the environment for the command
125 (
126 export UST_TRACE=1
127 export UST_AUTOPROBE=1
128
129 if [ "$arg_preload_libust" = "1" ];
130 then
131 if [ -n "${LIBUST_PATH%libust.so}" ] ; then
132 export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${LIBUST_PATH%libust.so}"
133 fi
134 export LD_PRELOAD="$LD_PRELOAD:$LIBUST_PATH"
135 fi
136
137 if [ "$arg_ld_std_ust" = "1" ];
138 then
139 if [ -n "$${LIBUST_PATH%libust.so}" ] ; then
140 export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${LIBUST_PATH%libust.so}"
141 fi
142 fi
143
144 if [ "$arg_preload_malloc" = "1" ];
145 then
146 export LD_PRELOAD="$LD_PRELOAD:$LIBMALLOCWRAP_PATH"
147 fi
148
149 if [ "$arg_preload_fork" = "1" ];
150 then
151 export LD_PRELOAD="$LD_PRELOAD:$LIBINTERFORK_PATH"
152 fi
153
154 # Execute the command
155 $CMD 2>&1
156 ) | tee "$OUTDIR/app.log"
157
158 ## Because of the keepalive mechanism, we're sure that by the time
159 ## we get here, the daemon is connected to all the buffers that still exist.
160 ## Therefore we can politely ask it to die when it's done.
161
162 if [ "$arg_syswide_daemon" != "1" ];
163 then
164 # Tell the daemon to die
165 kill -SIGTERM "$USTDPID"
166
167 echo "Waiting for ustd to shutdown..."
168 wait "$USTDPID"
169
170 rm "$pidfilepath"
171 fi
172
173 echo "Trace was output in: " $OUTDIR
This page took 0.035388 seconds and 4 git commands to generate.