3 # $1 - install/uninstall
10 # $8 - backtrace trigger
12 exec 1>&2 # redirect byteman/etc. tracing output to stderr, for easier filtering
14 if [[ $# -gt 8 ||
$# -lt 7 ]]; then
15 echo "need seven or eight arguments"
20 # resolve the overloaded parameter; PR21020
21 if [ $arg_command = "install31" ]; then
24 elif [ $arg_command = "uninstall31" ]; then
27 elif [ $arg_command = "install" ]; then
30 elif [ $arg_command = "uninstall" ]; then
48 SYSTEMTAP_DIR
=${SYSTEMTAP_DIR-$HOME/.systemtap}
49 BYTEMAN_HOME
=${BYTEMAN_HOME-/usr/share/java/byteman}
50 JAVA_HOME
=${JAVA_HOME-/usr/lib/jvm/java}
51 BYTEMAN_INSTALL_OPTS
=${BYTEMAN_INSTALL_OPTS--Dorg.jboss.byteman.transform.all=true}
52 SYSTEMTAP_VERBOSE
=${SYSTEMTAP_VERBOSE-0}
54 if [ "$SYSTEMTAP_VERBOSE" != "0" ]; then
55 BYTEMAN_INSTALL_OPTS
="$BYTEMAN_INSTALL_OPTS -Dorg.jboss.byteman.verbose"
63 exec_prefix
=@exec_prefix@
64 pkglibexecdir
=@pkglibexecdir@
66 HELPERSDT_JAR
=${pkglibexecdir}/HelperSDT.jar
67 if [ ! -f ${HELPERSDT_JAR} ]; then
69 echo "Missing $HELPERSDT_JAR"
73 flagdir
="$SYSTEMTAP_DIR/java"
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
82 if ! [[ $arg_jvmpid =~ ^
[0-9]+$
]]; then
83 target_pid
=`jps -l | grep $arg_jvmpid | cut -f1 -d" "`
84 for target
in $target_pid; do
85 $0 "$arg_command" "$target" "$arg_rulename" "$arg_class" "$arg_method" "$arg_argcount" "$arg_probetype" "$arg_backtrace"
89 target_pid
=$arg_jvmpid
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.
97 byteman_installed_portfile
=$flagdir/`hostname`-${target_pid}-bm
99 exec 200>>$byteman_installed_portfile # open/create lock file
100 flock
-x 200 # exclusive-lock it
102 if [ -s $byteman_installed_portfile ]; then
103 bmport
=`cat $byteman_installed_portfile`
105 if [ "$SYSTEMTAP_VERBOSE" != "0" ]; then
106 echo "Byteman agent reused for java pid $target_pid, port $bmport"
109 # XXX: liveness-check the port; bmsubmit with no argument just lists current rules
110 # if fails, delete the _portfile and retry everything
113 bmport
=`expr 9090 + $RANDOM % 10000`
114 existing
=`ss -atn | awk '{print $4}' | grep ':'$bmport'$'`
115 if [ "x$existing" != "x" ]; then
116 echo "Byteman port $bmport already in use, retrying."
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
122 # byteman and its use of VMAttach libraries, for our case it always makes sense to use
123 # byteman classes directly and avoid -javaagent
125 bminstall
-b -p $bmport $BYTEMAN_INSTALL_OPTS $target_pid
126 if [ $?
-ne 0 ]; then
127 echo "Byteman agent failed to install for java pid $target_pid, port $bmport"
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"
136 echo $bmport > $byteman_installed_portfile
138 if [ "$SYSTEMTAP_VERBOSE" != "0" ]; then
139 echo "Byteman agent installed for java pid $target_pid, port $bmport"
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
146 exec 200>&- # close file & release flock
149 function echo_bytemanrule
()
151 echo "RULE $arg_rulename"
152 echo "CLASS $arg_class"
153 echo "METHOD $arg_method"
154 echo "HELPER org.systemtap.byteman.helper.HelperSDT"
155 case "$arg_probetype" in
163 echo "AT LINE $arg_probetype"
167 if [ "$arg_backtrace" == "1" ]; then
168 echo 'DO STAP_BACKTRACE("'$arg_rulename'");'
172 case "$arg_argcount" in
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)' ;;
185 *) echo 'bad arg-count'; exit 1 ;;
187 if [ "$arg_backtrace" == "1" ]; then
189 echo 'METHOD_BT_DELETE("'$arg_rulename'")'
197 # Generate the byteman rule file on-the-fly
198 btmfile
=$flagdir/`hostname`-$$.btm
199 echo_bytemanrule
> $btmfile
200 trap 'rm -f $btmfile' 0 1 2 3 4 5 9 15
202 if [ "$SYSTEMTAP_VERBOSE" != "0" ]; then
203 echo "Byteman rule file:"
207 if [ $mode = "uninstall" ]; then
213 bmsubmit
-p $bmport $bmcmd $btmfile