3 # $1 - install/uninstall
11 exec 1>&2 # redirect byteman/etc. tracing output to stderr, for easier filtering
14 echo "need exactly seven arguments"
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}
33 if [ "$STAPBM_VERBOSE" != "no" ]; then
34 BYTEMAN_INSTALL_OPTS
="$BYTEMAN_INSTALL_OPTS -Dorg.jboss.byteman.verbose"
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"
47 BYTEMAN_SUBMIT_JAR
=${BYTEMAN_HOME}/byteman-submit.jar
48 if [ ! -r ${BYTEMAN_SUBMIT_JAR} ]; then
49 echo "Missing $BYTEMAN_SUBMIT_JAR"
53 BYTEMAN_INSTALL_JAR
=${BYTEMAN_HOME}/byteman-install.jar
54 if [ ! -r ${BYTEMAN_INSTALL_JAR} ]; then
55 echo "Missing $BYTEMAN_INSTALL_JAR"
60 TOOLS_JAR
=${JAVA_HOME}/lib
/tools.jar
61 if [ ! -f ${TOOLS_JAR} ]; then
62 echo "Missing $TOOLS_JAR"
66 # resolve $*prefix fully
68 exec_prefix
=@exec_prefix@
69 pkglibexecdir
=@libexecdir@
/@PACKAGE@
70 pkglibexecdir
=`eval echo $pkglibexecdir`
71 pkglibexecdir
=`eval echo $pkglibexecdir`
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:"
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}'"/"
82 echo "ln -sf ${pkglibexecdir}/HelperSDT.jar
${JAVA_HOME}/jre
/lib
/ext
/"
86 flagdir="$SYSTEMTAP_DIR/java
"
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
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
102 target_pid=$arg_jvmpid
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.
110 byteman_installed_portfile=$flagdir/`hostname`-${target_pid}-bm
112 exec 200>>$byteman_installed_portfile # open/create lock file
113 flock -x 200 # exclusive-lock it
115 if [ -s $byteman_installed_portfile ]; then
116 bmport=`cat $byteman_installed_portfile`
118 if [ "$STAPBM_VERBOSE" != "no
" ]; then
119 echo "Byteman agent reused
for java pid
$target_pid, port
$bmport"
122 # XXX: liveness-check the port; bmsubmit with no argument just lists current rules
123 # if fails, delete the _portfile and retry everything
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.
"
133 # We use -javaagent parameters to make it possible to probe jboss-as
134 # applications utilizing the -Djboss.modules.system.pkgs= parameter
136 if [ "$STAPBM_VERBOSE" != "no
" ]; then
137 echo java -classpath ${BYTEMAN_INSTALL_JAR}:${BYTEMAN_JAR}:${TOOLS_JAR} -javaagent:${BYTEMAN_HOME}/lib/byteman.jar=sys:${JAVA_HOME}/jre/lib/ext/HelperSDT.jar,listener:true,port:$bmport org.jboss.byteman.agent.install.Install -b -Dorg.jboss.byteman.listener-port=$bmport $BYTEMAN_INSTALL_OPTS $target_pid
139 java -classpath ${BYTEMAN_INSTALL_JAR}:${BYTEMAN_JAR}:${TOOLS_JAR} -javaagent:${BYTEMAN_JAR}=sys:${JAVA_HOME}/jre/lib/ext/HelperSDT.jar,listener:true,port:$bmport org.jboss.byteman.agent.install.Install -b -Dorg.jboss.byteman.listener-port=$bmport $BYTEMAN_INSTALL_OPTS $target_pid
140 if [ $? -ne 0 ]; then
141 echo "Byteman agent failed to
install for java pid
$target_pid, port
$bmport"
144 echo $bmport > $byteman_installed_portfile
146 if [ "$STAPBM_VERBOSE" != "no
" ]; then
147 echo "Byteman agent installed
for java pid
$target_pid, port
$bmport"
149 # XXX: Erase file to keep it from sticking around indefinitely,
150 # in case process ends, machine reboots, pid gets reused
151 # XXX: consider explicit notification to stapbm via process("java
").begin/end ?
152 # ... or else: liveness-check below
154 exec 200>&- # close file & release flock
157 function echo_bytemanrule()
159 echo "RULE
$arg_rulename"
160 echo "CLASS
$arg_class"
161 echo "METHOD
$arg_method"
162 echo "HELPER org.systemtap.byteman.helper.HelperSDT
"
163 case "$arg_probetype" in
171 echo "AT LINE
$arg_probetype"
175 case "$arg_argcount" in
176 0) echo 'DO METHOD_STAP_PROBE0("'$arg_rulename'")' ;;
177 1) echo 'DO METHOD_STAP_PROBE1("'$arg_rulename'", $1)' ;;
178 2) echo 'DO METHOD_STAP_PROBE2("'$arg_rulename'", $1, $2)' ;;
179 3) echo 'DO METHOD_STAP_PROBE3("'$arg_rulename'", $1, $2, $3)' ;;
180 4) echo 'DO METHOD_STAP_PROBE4("'$arg_rulename'", $1, $2, $3, $4)' ;;
181 5) echo 'DO METHOD_STAP_PROBE5("'$arg_rulename'", $1, $2, $3, $4, $5)' ;;
182 6) echo 'DO METHOD_STAP_PROBE6("'$arg_rulename'", $1, $2, $3, $4, $5, $6)' ;;
183 7) echo 'DO METHOD_STAP_PROBE7("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7)' ;;
184 8) echo 'DO METHOD_STAP_PROBE8("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7, $8)' ;;
185 9) echo 'DO METHOD_STAP_PROBE9("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7, $8, $9)' ;;
186 10) echo 'DO METHOD_STAP_PROBE10("'$arg_rulename'", $1, $2, $3, $4, $5, $6, $7, $8, $9, $10)' ;;
187 *) echo 'bad arg-count'; exit 1 ;;
193 # Generate the byteman rule file on-the-fly
194 btmfile=$flagdir/`hostname`-$$.btm
195 echo_bytemanrule > $btmfile
196 trap 'rm -f $btmfile' 0 1 2 3 4 5 9 15
198 if [ "$STAPBM_VERBOSE" != "no
" ]; then
199 echo "Byteman rule
file:"
203 if [ $arg_command = "uninstall
" ]; then
209 if [ "$STAPBM_VERBOSE" != "no
" ]; then
210 echo java -classpath ${BYTEMAN_SUBMIT_JAR}:${BYTEMAN_JAR} -javaagent:${BYTEMAN_JAR}=sys:${JAVA_HOME}/jre/lib/ext/HelperSDT.jar,script:$btmfile,port:$bmport,listener:true org.jboss.byteman.agent.submit.Submit $bmcmd $btmfile
213 exec java -classpath ${BYTEMAN_SUBMIT_JAR}:${BYTEMAN_JAR} -javaagent:${BYTEMAN_JAR}=sys:${JAVA_HOME}/jre/lib/ext/HelperSDT.jar,script:$btmfile,port:$bmport,listener:true org.jboss.byteman.agent.submit.Submit $bmcmd $btmfile