]> sourceware.org Git - systemtap.git/blob - java/stapbm.in
Adjust stapbm.in and docs to avoid recurssion
[systemtap.git] / java / stapbm.in
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
10
11 exec 1>&2 # redirect byteman/etc. tracing output to stderr, for easier filtering
12
13 if [ $# -ne 7 ]; then
14 echo "need exactly seven arguments"
15 exit 1
16 fi
17
18 arg_command=$1
19 arg_jvmpid=$2
20 arg_rulename=$3
21 arg_class=$4
22 arg_method=$5
23 arg_argcount=$6
24 arg_probetype=$7
25
26 SYSTEMTAP_DIR=${SYSTEMTAP_DIR-$HOME/.systemtap}
27 BYTEMAN_HOME=${BYTEMAN_HOME-/usr/share/java/byteman}
28 JAVA_HOME=${JAVA_HOME-/usr/lib/jvm/java}
29 BYTEMAN_INSTALL_OPTS=${BYTEMAN_INSTALL_OPTS--Dorg.jboss.byteman.transform.all=true}
30 STAPBM_VERBOSE=${STAPBM_VERBOSE-no}
31
32
33 if [ "$STAPBM_VERBOSE" != "no" ]; then
34 BYTEMAN_INSTALL_OPTS="$BYTEMAN_INSTALL_OPTS -Dorg.jboss.byteman.verbose"
35 else
36 exec >/dev/null 2>&1
37 fi
38
39
40 # the byteman and byteman-submit jars should be in ${BYTEMAN_HOME}/lib
41 BYTEMAN_JAR=${BYTEMAN_HOME}/byteman.jar
42 if [ ! -r ${BYTEMAN_JAR} ]; then
43 echo "Missing $BYTEMAN_JAR"
44 exit 1
45 fi
46
47 BYTEMAN_SUBMIT_JAR=${BYTEMAN_HOME}/byteman-submit.jar
48 if [ ! -r ${BYTEMAN_SUBMIT_JAR} ]; then
49 echo "Missing $BYTEMAN_SUBMIT_JAR"
50 exit 1
51 fi
52
53 BYTEMAN_INSTALL_JAR=${BYTEMAN_HOME}/byteman-install.jar
54 if [ ! -r ${BYTEMAN_INSTALL_JAR} ]; then
55 echo "Missing $BYTEMAN_INSTALL_JAR"
56 exit 1
57 fi
58
59
60 TOOLS_JAR=${JAVA_HOME}/lib/tools.jar
61 if [ ! -f ${TOOLS_JAR} ]; then
62 echo "Missing $TOOLS_JAR"
63 exit 1
64 fi
65
66 # resolve $*prefix fully
67 prefix=@prefix@
68 exec_prefix=@exec_prefix@
69 pkglibexecdir=@libexecdir@/@PACKAGE@
70 pkglibexecdir=`eval echo $pkglibexecdir`
71 pkglibexecdir=`eval echo $pkglibexecdir`
72
73 num=`ls -1 ${JAVA_HOME}/jre/lib/ext/HelperSDT.jar ${JAVA_HOME}/jre/lib/*/libHelperSDT_*.so 2>/dev/null | wc -l`
74 if [ $num -lt 2 ]; then
75 echo "Missing HelperSDT JNI class/shared library"
76 echo "Install them like this, as root:"
77 echo ""
78 echo "for so in ${pkglibexecdir}/libHelperSDT_*.so; do"
79 echo ' arch=`basename $so | cut -f2 -d_ | cut -f1 -d.`'
80 echo " ln -sf ${pkglibexecdir}/libHelperSDT_"'${arch}'".so ${JAVA_HOME}/jre/lib/"'${arch}'"/"
81 echo "done"
82 echo "ln -sf ${pkglibexecdir}/HelperSDT.jar ${JAVA_HOME}/jre/lib/ext/"
83 exit 1
84 fi
85
86 flagdir="$SYSTEMTAP_DIR/java"
87 mkdir -p $flagdir
88
89 # Find our target jvm pid. Due to the possibility of our
90 # target jvm pid being passed as a string, we need to allow
91 # for the possiblity that more than one pid may match the
92 # target jvm pid. If this is the case, we need to have a
93 # nested call to stapbm with the actual pid of the jvm pid
94
95 if [[ $arg_jvmpid != *[[:digit:]]* ]]; then
96 target_pid=`jps -l | grep $arg_jvmpid | cut -f1 -d" "`
97 for target in $target_pid; do
98 $0 $arg_command $target $arg_rulename $arg_class $arg_method $arg_argcount $arg_probetype
99 done;
100 exit 0
101 else
102 target_pid=$arg_jvmpid
103 fi
104
105 # Our target jvm may not have the byteman agent installed yet. Let's do
106 # that first. We use a signal file in $flagdir to show that the
107 # JVM is ready for further bytemanning without a prior setup step,
108 # and include in it the designated byteman agent listening-port number.
109 #
110 byteman_installed_portfile=$flagdir/`hostname`-${target_pid}-bm
111
112 exec 200>>$byteman_installed_portfile # open/create lock file
113 flock -x 200 # exclusive-lock it
114
115 if [ -s $byteman_installed_portfile ]; then
116 bmport=`cat $byteman_installed_portfile`
117
118 if [ "$STAPBM_VERBOSE" != "no" ]; then
119 echo "Byteman agent reused for java pid $target_pid, port $bmport"
120 fi
121
122 # XXX: liveness-check the port; bmsubmit with no argument just lists current rules
123 # if fails, delete the _portfile and retry everything
124 else
125 # bmport=9091
126 bmport=`expr 9090 + $RANDOM % 10000`
127 existing=`netstat -atn | awk '{print $4}' | grep ':'$bmport'$'`
128 if [ "x$existing" != "x" ]; then
129 echo "Byteman port $bmport already in use, retrying."
130 exec "$@"
131 fi
132 if [ "$STAPBM_VERBOSE" != "no" ]; then
133 echo java -classpath ${BYTEMAN_INSTALL_JAR}:${BYTEMAN_JAR}:${TOOLS_JAR} org.jboss.byteman.agent.install.Install -b -p $bmport -Dorg.jboss.byteman.listener-port=$bmport $BYTEMAN_INSTALL_OPTS $target_pid
134 fi
135 java -classpath ${BYTEMAN_INSTALL_JAR}:${BYTEMAN_JAR}:${TOOLS_JAR} org.jboss.byteman.agent.install.Install -b -p $bmport -Dorg.jboss.byteman.listener-port=$bmport $BYTEMAN_INSTALL_OPTS $target_pid
136 if [ $? -ne 0 ]; then
137 echo "Byteman agent failed to install for java pid $target_pid, port $bmport"
138 exit 1
139 fi
140 echo $bmport > $byteman_installed_portfile
141
142 if [ "$STAPBM_VERBOSE" != "no" ]; then
143 echo "Byteman agent installed for java pid $target_pid, port $bmport"
144 fi
145 # XXX: Erase file to keep it from sticking around indefinitely,
146 # in case process ends, machine reboots, pid gets reused
147 # XXX: consider explicit notification to stapbm via process("java").begin/end ?
148 # ... or else: liveness-check below
149 fi
150 exec 200>&- # close file & release flock
151
152
153 function echo_bytemanrule()
154 {
155 echo "RULE $arg_rulename"
156 echo "CLASS $arg_class"
157 echo "METHOD $arg_method"
158 echo "HELPER HelperSDT"
159 case "$arg_probetype" in
160 entry)
161 echo "AT ENTRY"
162 ;;
163 exi*)
164 echo "AT RETURN"
165 ;;
166 *)
167 echo "AT LINE $arg_probetype"
168 ;;
169 esac
170 echo "IF TRUE"
171 case "$arg_argcount" in
172 0) echo 'DO METHOD_STAP_PROBE0("'$arg_rulename'")' ;;
173 1) echo 'DO METHOD_STAP_PROBE1("'$arg_rulename'", $1)' ;;
174 2) echo 'DO METHOD_STAP_PROBE2("'$arg_rulename'", $1, $2)' ;;
175 3) echo 'DO METHOD_STAP_PROBE3("'$arg_rulename'", $1, $2, $3)' ;;
176 4) echo 'DO METHOD_STAP_PROBE4("'$arg_rulename'", $1, $2, $3, $4)' ;;
177 5) echo 'DO METHOD_STAP_PROBE5("'$arg_rulename'", $1, $2, $3, $4, $5)' ;;
178 6) echo 'DO METHOD_STAP_PROBE6("'$arg_rulename'", $1, $2, $3, $4, $5, $6)' ;;
179 7) echo 'DO METHOD_STAP_PROBE7("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7)' ;;
180 8) echo 'DO METHOD_STAP_PROBE8("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7, $8)' ;;
181 9) echo 'DO METHOD_STAP_PROBE9("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7, $8, $9)' ;;
182 10) echo 'DO METHOD_STAP_PROBE10("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)' ;;
183 *) echo 'bad arg-count'; exit 1 ;;
184 esac
185 echo "ENDRULE"
186 }
187
188
189 # Generate the byteman rule file on-the-fly
190 btmfile=$flagdir/`hostname`-$$.btm
191 echo_bytemanrule > $btmfile
192 trap 'rm -f $btmfile' 0 1 2 3 4 5 9 15
193
194 if [ "$STAPBM_VERBOSE" != "no" ]; then
195 echo "Byteman rule file:"
196 cat $btmfile
197 fi
198
199 if [ $arg_command = "uninstall" ]; then
200 bmcmd=-u
201 else
202 bmcmd=-l
203 fi
204
205 if [ "$STAPBM_VERBOSE" != "no" ]; then
206 echo java -classpath ${BYTEMAN_SUBMIT_JAR}:${BYTEMAN_JAR} org.jboss.byteman.agent.submit.Submit -p $bmport $bmcmd $btmfile
207 fi
208
209 exec java -classpath ${BYTEMAN_SUBMIT_JAR}:${BYTEMAN_JAR} org.jboss.byteman.agent.submit.Submit -p $bmport $bmcmd $btmfile
This page took 0.051536 seconds and 6 git commands to generate.