| 1 | #!/bin/bash |
| 2 | |
| 3 | # usttrace by Pierre-Marc Fournier 2009 |
| 4 | # Distributed under the GPLv2. |
| 5 | |
| 6 | USTD="./ustd/ustd" |
| 7 | LIBINTERFORK="./libinterfork/.libs/libinterfork.so" |
| 8 | |
| 9 | BASE_TRACE_DIR="$HOME/.usttraces" |
| 10 | |
| 11 | function usage () { |
| 12 | echo "usage: $0 COMMAND" 2>/dev/stderr |
| 13 | } |
| 14 | |
| 15 | function error() { |
| 16 | echo "$0: error: $1" 2>/dev/stderr |
| 17 | } |
| 18 | |
| 19 | # Prepare vars |
| 20 | CMD=$1 |
| 21 | |
| 22 | # Validate input |
| 23 | if [ -z "$HOME" ]; |
| 24 | then |
| 25 | error "no home specified" |
| 26 | fi |
| 27 | |
| 28 | if [ -z "$CMD" ]; |
| 29 | then |
| 30 | error "no command specified" |
| 31 | usage; |
| 32 | exit 1 |
| 33 | fi |
| 34 | |
| 35 | # Create directory for trace output |
| 36 | DATESTRING="$(hostname)-$(date +%Y%m%d%H%M%S)" |
| 37 | OUTDIR="$BASE_TRACE_DIR/$DATESTRING" |
| 38 | mkdir -p "$OUTDIR" |
| 39 | |
| 40 | # Choose socket path |
| 41 | SOCKPATH="/tmp/ust-sock-$$" |
| 42 | |
| 43 | # Start daemon |
| 44 | $USTD -s "$SOCKPATH" -o "$OUTDIR" >"$OUTDIR/ustd.log" 2>&1 & |
| 45 | USTDPID=$! |
| 46 | |
| 47 | # Establish the environment for the command |
| 48 | export UST_TRACE=1 |
| 49 | export UST_AUTOPROBE=1 |
| 50 | export UST_DAEMON_SOCKET="$SOCKPATH" |
| 51 | |
| 52 | # Execute the command |
| 53 | bash -c "$CMD" |
| 54 | |
| 55 | ## Because of the keepalive mechanism, we're sure that by the time |
| 56 | ## we get here, the daemon is connected to all the buffers that still exist. |
| 57 | ## Therefore we can politely ask it to die when it's done. |
| 58 | |
| 59 | kill -SIGTERM "$USTDPID" |
| 60 | |
| 61 | # Tell the daemon to die |
| 62 | echo "Waiting for ustd to shutdown..." |
| 63 | wait "$USTDPID" |