| 1 | #!/bin/sh |
| 2 | |
| 3 | # usttrace by Pierre-Marc Fournier 2009 |
| 4 | # Distributed under the GPLv2. |
| 5 | |
| 6 | error() { |
| 7 | echo "$0: error: $1" 1>&2 |
| 8 | } |
| 9 | |
| 10 | sighandler() { |
| 11 | echo "Caught Ctrl-C" |
| 12 | if [ -z "${UST_CONSUMERD_PID}" ]; then |
| 13 | UST_CONSUMERD_PID=`cat $pidfilepath` |
| 14 | fi |
| 15 | # Tell the daemon to die |
| 16 | kill -TERM "${UST_CONSUMERD_PID}" |
| 17 | |
| 18 | echo "Waiting for ust-consumerd to shutdown..." |
| 19 | wait "${UST_CONSUMERD_PID}" |
| 20 | |
| 21 | rm "$pidfilepath" |
| 22 | |
| 23 | exit 0; |
| 24 | } |
| 25 | |
| 26 | USTTRACE_DIR="$(dirname $0)" |
| 27 | if [ -x "${USTTRACE_DIR}/ust-consumerd/ust-consumerd" ] ; then |
| 28 | # Use the not installed libraries instead |
| 29 | UST_CONSUMERD="${USTTRACE_DIR}/ust-consumerd/ust-consumerd" |
| 30 | LIBINTERFORK_PATH="${USTTRACE_DIR}/libustfork/.libs/libustfork.so" |
| 31 | LIBMALLOCWRAP_PATH="${USTTRACE_DIR}/libustinstr-malloc/.libs/libustinstr-malloc.so" |
| 32 | LIBUST_PATH="${USTTRACE_DIR}/libust/.libs/libust.so" |
| 33 | else |
| 34 | # Use the libraries that the dynamic link finds |
| 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" |
| 38 | exit 1 |
| 39 | fi |
| 40 | LIBINTERFORK_PATH="libustfork.so" |
| 41 | LIBMALLOCWRAP_PATH="libustinstr-malloc.so" |
| 42 | LIBUST_PATH="libust.so.0" |
| 43 | fi |
| 44 | |
| 45 | BASE_TRACE_DIR="${HOME}/.usttraces" |
| 46 | |
| 47 | usage() { |
| 48 | echo "usage: $0 OPTIONS COMMAND" 1>&2 |
| 49 | echo "" 1>&2 |
| 50 | echo "Options:" 1>&2 |
| 51 | echo " -l Runtime link with UST library." 1>&2 |
| 52 | echo " (Needed only if program was not linked at compile time with libust.)" 1>&2 |
| 53 | echo " -L Add path to ust libraries to LD_LIBRARY_PATH." 1>&2 |
| 54 | echo " -m Instrument malloc calls." 1>&2 |
| 55 | echo " -f Also trace forked processes." 1>&2 |
| 56 | echo " -s Use system-wide daemon instead of creating one for this session." 1>&2 |
| 57 | echo " -S Specify the subbuffer size." 1>&2 |
| 58 | echo " -N Specify the number of subbuffers." 1>&2 |
| 59 | echo " -o Output directory of the trace." 1>&2 |
| 60 | } |
| 61 | |
| 62 | while getopts ":hlLmfsWS:N:o:" options; do |
| 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;; |
| 68 | s) arg_syswide_daemon=1;; |
| 69 | W) where=1;; |
| 70 | S) export UST_SUBBUF_SIZE=$OPTARG;; |
| 71 | N) export UST_SUBBUF_NUM=$OPTARG;; |
| 72 | o) OUTPUT_DIR=$OPTARG;; |
| 73 | h) usage; |
| 74 | exit 0;; |
| 75 | \?) usage |
| 76 | exit 1;; |
| 77 | *) usage |
| 78 | exit 1;; |
| 79 | esac |
| 80 | done |
| 81 | shift $(($OPTIND - 1)) |
| 82 | |
| 83 | if [ -n "$where" ]; then |
| 84 | echo $BASE_TRACE_DIR/$(ls "$BASE_TRACE_DIR" | tail -n 1) |
| 85 | exit 0 |
| 86 | fi |
| 87 | |
| 88 | # Prepare vars |
| 89 | CMD=$* |
| 90 | |
| 91 | # Validate input |
| 92 | if [ -z "$HOME" ]; |
| 93 | then |
| 94 | error "no home specified" |
| 95 | fi |
| 96 | |
| 97 | if [ -z "$CMD" ]; |
| 98 | then |
| 99 | error "no command specified" |
| 100 | usage; |
| 101 | exit 1 |
| 102 | fi |
| 103 | |
| 104 | # Create directory for trace output |
| 105 | if [ -n "$OUTPUT_DIR" ]; then |
| 106 | OUTDIR=$OUTPUT_DIR |
| 107 | else |
| 108 | DATESTRING="$(hostname)-$(date +%Y%m%d%H%M%S%N)" |
| 109 | OUTDIR="$BASE_TRACE_DIR/$DATESTRING" |
| 110 | fi |
| 111 | |
| 112 | # Check if directory exist |
| 113 | if [ ! -d "$OUTDIR" ]; then |
| 114 | mkdir -p $OUTDIR |
| 115 | if [ $? -eq 1 ]; then |
| 116 | exit 1 |
| 117 | fi |
| 118 | fi |
| 119 | |
| 120 | # Choose ust-consumerd socket path |
| 121 | UST_CONSUMERD_SOCKPATH="/tmp/ust-consumerd-sock-$$" |
| 122 | |
| 123 | if [ "$arg_syswide_daemon" != "1" ]; |
| 124 | then |
| 125 | pidfilepath="/tmp/usttrace-$USER-$(date +%Y%m%d%H%M%S%N)-ust-consumerd-pid" |
| 126 | trap "sighandler $pidfilepath" INT |
| 127 | mkfifo -m 0600 "$pidfilepath" |
| 128 | # Start daemon |
| 129 | ${UST_CONSUMERD} --pidfile "$pidfilepath" -s "${UST_CONSUMERD_SOCKPATH}" -o "$OUTDIR" >"$OUTDIR/ust-consumerd.log" 2>&1 & |
| 130 | # ust-consumerd sets up its server socket |
| 131 | # ust-consumerd opens the pidfile, blocks because no one has opened it |
| 132 | # we open pidfile |
| 133 | # we block reading pidfile |
| 134 | # ust-consumerd writes to pidfile |
| 135 | # ust-consumerd closes pidfile |
| 136 | # we unblock reading pidfile |
| 137 | UST_CONSUMERD_PID=`cat $pidfilepath` |
| 138 | export UST_DAEMON_SOCKET="${UST_CONSUMERD_SOCKPATH}" |
| 139 | fi |
| 140 | |
| 141 | # Establish the environment for the command |
| 142 | ( |
| 143 | export UST_TRACE=1 |
| 144 | export UST_AUTOPROBE=1 |
| 145 | |
| 146 | if [ "$arg_preload_libust" = "1" ]; |
| 147 | then |
| 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 |
| 165 | fi |
| 166 | fi |
| 167 | |
| 168 | if [ "$arg_ld_std_ust" = "1" ] && [ -n "${LIBUST_PATH%libust.so}" ]; |
| 169 | then |
| 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}" |
| 175 | fi |
| 176 | fi |
| 177 | |
| 178 | if [ "$arg_preload_malloc" = "1" ] && [ -n "$LIBMALLOCWRAP_PATH" ]; |
| 179 | then |
| 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 |
| 186 | fi |
| 187 | |
| 188 | if [ "$arg_preload_fork" = "1" ] && [ -n "$LIBINTERFORK_PATH" ]; |
| 189 | then |
| 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 |
| 196 | fi |
| 197 | |
| 198 | # Execute the command |
| 199 | $CMD 2>&1 |
| 200 | ) | tee "$OUTDIR/app.log" |
| 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 | |
| 206 | if [ "$arg_syswide_daemon" != "1" ]; |
| 207 | then |
| 208 | # Tell the daemon to die |
| 209 | kill -TERM "${UST_CONSUMERD_PID}" |
| 210 | |
| 211 | echo "Waiting for ust-consumerd to shutdown..." |
| 212 | wait "${UST_CONSUMERD_PID}" |
| 213 | |
| 214 | rm "$pidfilepath" |
| 215 | fi |
| 216 | |
| 217 | echo "Trace was output in: " $OUTDIR |