]> sourceware.org Git - systemtap.git/blame - java/stapbm.in
PR32209: Define the __COUNT_ARGS macro conditionally
[systemtap.git] / java / stapbm.in
CommitLineData
d885563b
FCE
1#!/bin/bash
2
3# $1 - install/uninstall
4# $2 - PID/unique name
5# $3 - RULE name
6# $4 - class
7# $5 - method
8# $6 - number of args
9# $7 - entry/exit/line
6ded984a 10# $8 - backtrace trigger
d885563b
FCE
11
12exec 1>&2 # redirect byteman/etc. tracing output to stderr, for easier filtering
13
6ded984a
LB
14if [[ $# -gt 8 || $# -lt 7 ]]; then
15 echo "need seven or eight arguments"
d885563b
FCE
16 exit 1
17fi
18
19arg_command=$1
f8bc2a5e
FCE
20# resolve the overloaded parameter; PR21020
21if [ $arg_command = "install31" ]; then
22 mode=install
23 stap="31"
24elif [ $arg_command = "uninstall31" ]; then
25 mode=uninstall
26 stap="31"
27elif [ $arg_command = "install" ]; then
28 mode=install
29 stap=""
30elif [ $arg_command = "uninstall" ]; then
31 mode=uninstall
32else
33 exit 1
34fi
d885563b
FCE
35arg_jvmpid=$2
36arg_rulename=$3
37arg_class=$4
38arg_method=$5
39arg_argcount=$6
40arg_probetype=$7
41
6ded984a
LB
42if [ $# -eq 7 ]; then
43 arg_backtrace=0
44else
45 arg_backtrace=$8
46fi
47
d885563b
FCE
48SYSTEMTAP_DIR=${SYSTEMTAP_DIR-$HOME/.systemtap}
49BYTEMAN_HOME=${BYTEMAN_HOME-/usr/share/java/byteman}
50JAVA_HOME=${JAVA_HOME-/usr/lib/jvm/java}
296dbeb1 51BYTEMAN_INSTALL_OPTS=${BYTEMAN_INSTALL_OPTS--Dorg.jboss.byteman.transform.all=true}
c31d198c 52SYSTEMTAP_VERBOSE=${SYSTEMTAP_VERBOSE-0}
296dbeb1 53
c31d198c 54if [ "$SYSTEMTAP_VERBOSE" != "0" ]; then
296dbeb1 55 BYTEMAN_INSTALL_OPTS="$BYTEMAN_INSTALL_OPTS -Dorg.jboss.byteman.verbose"
0680bad5 56 set -x
296dbeb1 57else
bad68a14
FCE
58 exec >/dev/null
59 # NB: preserve stderr
296dbeb1 60fi
d885563b 61
d885563b
FCE
62prefix=@prefix@
63exec_prefix=@exec_prefix@
0680bad5 64pkglibexecdir=@pkglibexecdir@
d885563b 65
f8bc2a5e
FCE
66HELPERSDT_JAR=${pkglibexecdir}/HelperSDT.jar
67if [ ! -f ${HELPERSDT_JAR} ]; then
bad68a14 68 exec 1>&2
f8bc2a5e
FCE
69 echo "Missing $HELPERSDT_JAR"
70 exit 1
71fi
72
d885563b
FCE
73flagdir="$SYSTEMTAP_DIR/java"
74mkdir -p $flagdir
75
269cd0ae
LB
76# Find our target jvm pid. Due to the possibility of our
77# target jvm pid being passed as a string, we need to allow
78# for the possiblity that more than one pid may match the
79# target jvm pid. If this is the case, we need to have a
80# nested call to stapbm with the actual pid of the jvm pid
81
c226c5cc 82if ! [[ $arg_jvmpid =~ ^[0-9]+$ ]]; then
269cd0ae
LB
83 target_pid=`jps -l | grep $arg_jvmpid | cut -f1 -d" "`
84 for target in $target_pid; do
0680bad5
FCE
85 $0 "$arg_command" "$target" "$arg_rulename" "$arg_class" "$arg_method" "$arg_argcount" "$arg_probetype" "$arg_backtrace"
86 done
269cd0ae
LB
87 exit 0
88else
89 target_pid=$arg_jvmpid
d885563b
FCE
90fi
91
92# Our target jvm may not have the byteman agent installed yet. Let's do
93# that first. We use a signal file in $flagdir to show that the
94# JVM is ready for further bytemanning without a prior setup step,
95# and include in it the designated byteman agent listening-port number.
96#
97byteman_installed_portfile=$flagdir/`hostname`-${target_pid}-bm
98
99exec 200>>$byteman_installed_portfile # open/create lock file
100flock -x 200 # exclusive-lock it
101
102if [ -s $byteman_installed_portfile ]; then
103 bmport=`cat $byteman_installed_portfile`
296dbeb1 104
c31d198c 105 if [ "$SYSTEMTAP_VERBOSE" != "0" ]; then
296dbeb1
FCE
106 echo "Byteman agent reused for java pid $target_pid, port $bmport"
107 fi
108
d885563b
FCE
109 # XXX: liveness-check the port; bmsubmit with no argument just lists current rules
110 # if fails, delete the _portfile and retry everything
111else
112 # bmport=9091
113 bmport=`expr 9090 + $RANDOM % 10000`
436ddc6f 114 existing=`ss -atn | awk '{print $4}' | grep ':'$bmport'$'`
d885563b
FCE
115 if [ "x$existing" != "x" ]; then
116 echo "Byteman port $bmport already in use, retrying."
117 exec "$@"
118 fi
8eef2738 119
70ff58b7
LB
120# There are two ways to invoke and run byteman operations with the jvm's we're interested
121# in, we can alter the startup arguments to include a -javaagent parameter, or use
6ded984a 122# byteman and its use of VMAttach libraries, for our case it always makes sense to use
70ff58b7 123# byteman classes directly and avoid -javaagent
8eef2738 124
0680bad5 125 bminstall -b -p $bmport $BYTEMAN_INSTALL_OPTS $target_pid
d885563b
FCE
126 if [ $? -ne 0 ]; then
127 echo "Byteman agent failed to install for java pid $target_pid, port $bmport"
128 exit 1
129 fi
0680bad5
FCE
130 bmsubmit -p $bmport -s $HELPERSDT_JAR
131 if [ $? -ne 0 ]; then
132 echo "Byteman agent failed to load HelperSDT.jar java pid $target_pid, port $bmport"
133 exit 1
134 fi
d885563b 135
0680bad5
FCE
136 echo $bmport > $byteman_installed_portfile
137
c31d198c 138 if [ "$SYSTEMTAP_VERBOSE" != "0" ]; then
296dbeb1
FCE
139 echo "Byteman agent installed for java pid $target_pid, port $bmport"
140 fi
d885563b
FCE
141 # XXX: Erase file to keep it from sticking around indefinitely,
142 # in case process ends, machine reboots, pid gets reused
143 # XXX: consider explicit notification to stapbm via process("java").begin/end ?
144 # ... or else: liveness-check below
145fi
146exec 200>&- # close file & release flock
147
148
149function echo_bytemanrule()
150{
151 echo "RULE $arg_rulename"
152 echo "CLASS $arg_class"
153 echo "METHOD $arg_method"
8eef2738 154 echo "HELPER org.systemtap.byteman.helper.HelperSDT"
d885563b
FCE
155 case "$arg_probetype" in
156 entry)
157 echo "AT ENTRY"
158 ;;
159 exi*)
160 echo "AT RETURN"
161 ;;
162 *)
163 echo "AT LINE $arg_probetype"
164 ;;
165 esac
166 echo "IF TRUE"
6ded984a
LB
167 if [ "$arg_backtrace" == "1" ]; then
168 echo 'DO STAP_BACKTRACE("'$arg_rulename'");'
169 else
170 echo -n 'DO '
171 fi
d885563b 172 case "$arg_argcount" in
f8bc2a5e
FCE
173 # For PR21010, we invoke another java<->stap ABI
174 0) echo -n 'METHOD_STAP'$stap'_PROBE0("'$arg_rulename'")' ;;
175 1) echo -n 'METHOD_STAP'$stap'_PROBE1("'$arg_rulename'", $1)' ;;
176 2) echo -n 'METHOD_STAP'$stap'_PROBE2("'$arg_rulename'", $1, $2)' ;;
177 3) echo -n 'METHOD_STAP'$stap'_PROBE3("'$arg_rulename'", $1, $2, $3)' ;;
178 4) echo -n 'METHOD_STAP'$stap'_PROBE4("'$arg_rulename'", $1, $2, $3, $4)' ;;
179 5) echo -n 'METHOD_STAP'$stap'_PROBE5("'$arg_rulename'", $1, $2, $3, $4, $5)' ;;
180 6) echo -n 'METHOD_STAP'$stap'_PROBE6("'$arg_rulename'", $1, $2, $3, $4, $5, $6)' ;;
181 7) echo -n 'METHOD_STAP'$stap'_PROBE7("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7)' ;;
182 8) echo -n 'METHOD_STAP'$stap'_PROBE8("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7, $8)' ;;
183 9) echo -n 'METHOD_STAP'$stap'_PROBE9("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7, $8, $9)' ;;
184 10) echo -n 'METHOD_STAP'$stap'_PROBE10("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)' ;;
d885563b
FCE
185 *) echo 'bad arg-count'; exit 1 ;;
186 esac
6ded984a
LB
187 if [ "$arg_backtrace" == "1" ]; then
188 echo ';'
189 echo 'METHOD_BT_DELETE("'$arg_rulename'")'
190 else
191 echo ''
192 fi
d885563b
FCE
193 echo "ENDRULE"
194}
195
196
197# Generate the byteman rule file on-the-fly
198btmfile=$flagdir/`hostname`-$$.btm
199echo_bytemanrule > $btmfile
200trap 'rm -f $btmfile' 0 1 2 3 4 5 9 15
201
c31d198c 202if [ "$SYSTEMTAP_VERBOSE" != "0" ]; then
296dbeb1
FCE
203 echo "Byteman rule file:"
204 cat $btmfile
205fi
206
f8bc2a5e 207if [ $mode = "uninstall" ]; then
d885563b
FCE
208 bmcmd=-u
209else
210 bmcmd=-l
211fi
212
0680bad5 213bmsubmit -p $bmport $bmcmd $btmfile
This page took 0.119861 seconds and 6 git commands to generate.