| 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 | LIBMALLOCWRAP="./libmallocwrap/.libs/libmallocwrap.so" |
| 9 | |
| 10 | STD_LDLIBRARY_UST="./libust/.libs:../liburcu" |
| 11 | |
| 12 | BASE_TRACE_DIR="$HOME/.usttraces" |
| 13 | |
| 14 | function usage () { |
| 15 | echo "usage: $0 OPTIONS COMMAND" 2>/dev/stderr |
| 16 | echo "" 2>/dev/stderr |
| 17 | echo "Options:" 2>/dev/stderr |
| 18 | echo " -l Runtime link with UST library." 2>/dev/stderr |
| 19 | echo " (Needed only if program was not linked at compile time with libust.)" 2>/dev/stderr |
| 20 | echo " -L Add path to ust libraries to LD_LIBRARY_PATH." 2>/dev/stderr |
| 21 | echo " -m Instrument malloc calls." 2>/dev/stderr |
| 22 | echo " -f Also trace forked processes." 2>/dev/stderr |
| 23 | } |
| 24 | |
| 25 | function error() { |
| 26 | echo "$0: error: $1" 2>/dev/stderr |
| 27 | } |
| 28 | |
| 29 | while getopts ":hlLmf" options; do |
| 30 | case $options in |
| 31 | l) arg_preload_libust=1;; |
| 32 | L) arg_ld_std_ust=1;; |
| 33 | m) arg_preload_malloc=1;; |
| 34 | f) arg_preload_fork=1;; |
| 35 | h) usage; |
| 36 | exit 0;; |
| 37 | \?) echo $usage |
| 38 | exit 1;; |
| 39 | *) echo $usage |
| 40 | exit 1;; |
| 41 | esac |
| 42 | done |
| 43 | shift $(($OPTIND - 1)) |
| 44 | |
| 45 | # Prepare vars |
| 46 | CMD=$1 |
| 47 | |
| 48 | # Validate input |
| 49 | if [ -z "$HOME" ]; |
| 50 | then |
| 51 | error "no home specified" |
| 52 | fi |
| 53 | |
| 54 | if [ -z "$CMD" ]; |
| 55 | then |
| 56 | error "no command specified" |
| 57 | usage; |
| 58 | exit 1 |
| 59 | fi |
| 60 | |
| 61 | # Create directory for trace output |
| 62 | DATESTRING="$(hostname)-$(date +%Y%m%d%H%M%S)" |
| 63 | OUTDIR="$BASE_TRACE_DIR/$DATESTRING" |
| 64 | mkdir -p "$OUTDIR" |
| 65 | |
| 66 | # Choose socket path |
| 67 | SOCKPATH="/tmp/ust-sock-$$" |
| 68 | |
| 69 | # Start daemon |
| 70 | $USTD -s "$SOCKPATH" -o "$OUTDIR" >"$OUTDIR/ustd.log" 2>&1 & |
| 71 | USTDPID=$! |
| 72 | |
| 73 | # Establish the environment for the command |
| 74 | export UST_TRACE=1 |
| 75 | export UST_AUTOPROBE=1 |
| 76 | export UST_DAEMON_SOCKET="$SOCKPATH" |
| 77 | |
| 78 | if [ "$arg_preload_libust" = "1" ]; |
| 79 | then |
| 80 | export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:./libust/.libs" |
| 81 | export LD_PRELOAD="$LD_PRELOAD:./libust/.libs/libust.so" |
| 82 | fi |
| 83 | |
| 84 | if [ "$arg_preload_malloc" = "1" ]; |
| 85 | then |
| 86 | export LD_PRELOAD="$LD_PRELOAD:./libmallocwrap/.libs/libmallocwrap.so" |
| 87 | fi |
| 88 | |
| 89 | if [ "$arg_preload_fork" = "1" ]; |
| 90 | then |
| 91 | export LD_PRELOAD="$LD_PRELOAD:./libinterfork/.libs/libinterfork.so" |
| 92 | fi |
| 93 | |
| 94 | if [ "$arg_ld_std_ust" = "1" ]; |
| 95 | then |
| 96 | export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$STD_LDLIBRARY_UST" |
| 97 | fi |
| 98 | |
| 99 | # Execute the command |
| 100 | bash -c "$CMD" |
| 101 | |
| 102 | ## Because of the keepalive mechanism, we're sure that by the time |
| 103 | ## we get here, the daemon is connected to all the buffers that still exist. |
| 104 | ## Therefore we can politely ask it to die when it's done. |
| 105 | |
| 106 | kill -SIGTERM "$USTDPID" |
| 107 | |
| 108 | # Tell the daemon to die |
| 109 | echo "Waiting for ustd to shutdown..." |
| 110 | wait "$USTDPID" |