| 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 | # Validate input |
| 89 | if [ -z "$HOME" ]; |
| 90 | then |
| 91 | error "no home specified" |
| 92 | fi |
| 93 | |
| 94 | if [ -z "$*" ]; |
| 95 | then |
| 96 | error "no command specified" |
| 97 | usage; |
| 98 | exit 1 |
| 99 | fi |
| 100 | |
| 101 | # Create directory for trace output |
| 102 | if [ -n "$OUTPUT_DIR" ]; then |
| 103 | OUTDIR=$OUTPUT_DIR |
| 104 | else |
| 105 | DATESTRING="$(hostname)-$(date +%Y%m%d%H%M%S%N)" |
| 106 | OUTDIR="$BASE_TRACE_DIR/$DATESTRING" |
| 107 | fi |
| 108 | |
| 109 | # Check if directory exist |
| 110 | if [ ! -d "$OUTDIR" ]; then |
| 111 | mkdir -p $OUTDIR |
| 112 | if [ $? -eq 1 ]; then |
| 113 | exit 1 |
| 114 | fi |
| 115 | fi |
| 116 | |
| 117 | # Choose ust-consumerd socket path |
| 118 | UST_CONSUMERD_SOCKPATH="/tmp/ust-consumerd-sock-$$" |
| 119 | |
| 120 | if [ "$arg_syswide_daemon" != "1" ]; |
| 121 | then |
| 122 | pidfilepath="/tmp/usttrace-$USER-$(date +%Y%m%d%H%M%S%N)-ust-consumerd-pid" |
| 123 | trap "sighandler $pidfilepath" INT |
| 124 | mkfifo -m 0600 "$pidfilepath" |
| 125 | # Start daemon |
| 126 | ${UST_CONSUMERD} --pidfile "$pidfilepath" -s "${UST_CONSUMERD_SOCKPATH}" -o "$OUTDIR" >"$OUTDIR/ust-consumerd.log" 2>&1 & |
| 127 | # ust-consumerd sets up its server socket |
| 128 | # ust-consumerd opens the pidfile, blocks because no one has opened it |
| 129 | # we open pidfile |
| 130 | # we block reading pidfile |
| 131 | # ust-consumerd writes to pidfile |
| 132 | # ust-consumerd closes pidfile |
| 133 | # we unblock reading pidfile |
| 134 | UST_CONSUMERD_PID=`cat $pidfilepath` |
| 135 | export UST_DAEMON_SOCKET="${UST_CONSUMERD_SOCKPATH}" |
| 136 | fi |
| 137 | |
| 138 | # Establish the environment for the command |
| 139 | ( |
| 140 | export UST_TRACE=1 |
| 141 | export UST_AUTOPROBE=1 |
| 142 | |
| 143 | if [ "$arg_preload_libust" = "1" ]; |
| 144 | then |
| 145 | if [ -n "${LIBUST_PATH%libust.so}" ]; |
| 146 | then |
| 147 | if [ -n "$LD_LIBRARY_PATH" ]; |
| 148 | then |
| 149 | export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${LIBUST_PATH%libust.so}" |
| 150 | else |
| 151 | export LD_LIBRARY_PATH="${LIBUST_PATH%libust.so}" |
| 152 | fi |
| 153 | fi |
| 154 | if [ -n "$LIBUST_PATH" ]; |
| 155 | then |
| 156 | if [ -n "$LD_PRELOAD" ]; |
| 157 | then |
| 158 | export LD_PRELOAD="$LD_PRELOAD:$LIBUST_PATH" |
| 159 | else |
| 160 | export LD_PRELOAD="$LIBUST_PATH" |
| 161 | fi |
| 162 | fi |
| 163 | fi |
| 164 | |
| 165 | if [ "$arg_ld_std_ust" = "1" ] && [ -n "${LIBUST_PATH%libust.so}" ]; |
| 166 | then |
| 167 | if [ -n "$LD_LIBRARY_PATH" ]; |
| 168 | then |
| 169 | export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:${LIBUST_PATH%libust.so}" |
| 170 | else |
| 171 | export LD_LIBRARY_PATH="${LIBUST_PATH%libust.so}" |
| 172 | fi |
| 173 | fi |
| 174 | |
| 175 | if [ "$arg_preload_malloc" = "1" ] && [ -n "$LIBMALLOCWRAP_PATH" ]; |
| 176 | then |
| 177 | if [ -n "$LD_PRELOAD" ]; |
| 178 | then |
| 179 | export LD_PRELOAD="$LD_PRELOAD:$LIBMALLOCWRAP_PATH" |
| 180 | else |
| 181 | export LD_PRELOAD="$LIBMALLOCWRAP_PATH" |
| 182 | fi |
| 183 | fi |
| 184 | |
| 185 | if [ "$arg_preload_fork" = "1" ] && [ -n "$LIBINTERFORK_PATH" ]; |
| 186 | then |
| 187 | if [ -n "$LD_PRELOAD" ]; |
| 188 | then |
| 189 | export LD_PRELOAD="$LD_PRELOAD:$LIBINTERFORK_PATH" |
| 190 | else |
| 191 | export LD_PRELOAD="$LIBINTERFORK_PATH" |
| 192 | fi |
| 193 | fi |
| 194 | |
| 195 | # Execute the command |
| 196 | "$@" 2>&1 |
| 197 | ) | tee "$OUTDIR/app.log" |
| 198 | |
| 199 | ## Because of the keepalive mechanism, we're sure that by the time |
| 200 | ## we get here, the daemon is connected to all the buffers that still exist. |
| 201 | ## Therefore we can politely ask it to die when it's done. |
| 202 | |
| 203 | if [ "$arg_syswide_daemon" != "1" ]; |
| 204 | then |
| 205 | # Tell the daemon to die |
| 206 | kill -TERM "${UST_CONSUMERD_PID}" |
| 207 | |
| 208 | echo "Waiting for ust-consumerd to shutdown..." |
| 209 | wait "${UST_CONSUMERD_PID}" |
| 210 | |
| 211 | rm "$pidfilepath" |
| 212 | fi |
| 213 | |
| 214 | echo "Trace was output in: " $OUTDIR |