minor refactor of ustd to avoid deadlock
[ust.git] / usttrace
... / ...
CommitLineData
1#!/bin/bash
2
3# usttrace by Pierre-Marc Fournier 2009
4# Distributed under the GPLv2.
5
6USTD="./ustd/ustd"
7LIBINTERFORK="./libinterfork/.libs/libinterfork.so"
8LIBMALLOCWRAP="./libmallocwrap/.libs/libmallocwrap.so"
9
10STD_LDLIBRARY_UST="./libust/.libs:../liburcu"
11
12BASE_TRACE_DIR="$HOME/.usttraces"
13
14function 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
25function error() {
26 echo "$0: error: $1" 2>/dev/stderr
27}
28
29while 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
42done
43shift $(($OPTIND - 1))
44
45# Prepare vars
46CMD=$1
47
48# Validate input
49if [ -z "$HOME" ];
50then
51 error "no home specified"
52fi
53
54if [ -z "$CMD" ];
55then
56 error "no command specified"
57 usage;
58 exit 1
59fi
60
61# Create directory for trace output
62DATESTRING="$(hostname)-$(date +%Y%m%d%H%M%S)"
63OUTDIR="$BASE_TRACE_DIR/$DATESTRING"
64mkdir -p "$OUTDIR"
65
66# Choose socket path
67SOCKPATH="/tmp/ust-sock-$$"
68
69# Start daemon
70$USTD -s "$SOCKPATH" -o "$OUTDIR" >"$OUTDIR/ustd.log" 2>&1 &
71USTDPID=$!
72
73# Establish the environment for the command
74export UST_TRACE=1
75export UST_AUTOPROBE=1
76export UST_DAEMON_SOCKET="$SOCKPATH"
77
78if [ "$arg_preload_libust" = "1" ];
79then
80 export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:./libust/.libs"
81 export LD_PRELOAD="$LD_PRELOAD:./libust/.libs/libust.so"
82fi
83
84if [ "$arg_preload_malloc" = "1" ];
85then
86 export LD_PRELOAD="$LD_PRELOAD:./libmallocwrap/.libs/libmallocwrap.so"
87fi
88
89if [ "$arg_preload_fork" = "1" ];
90then
91 export LD_PRELOAD="$LD_PRELOAD:./libinterfork/.libs/libinterfork.so"
92fi
93
94if [ "$arg_ld_std_ust" = "1" ];
95then
96 export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$STD_LDLIBRARY_UST"
97fi
98
99# Execute the command
100bash -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
106kill -SIGTERM "$USTDPID"
107
108# Tell the daemon to die
109echo "Waiting for ustd to shutdown..."
110wait "$USTDPID"
This page took 0.022309 seconds and 4 git commands to generate.