Skip to content

Commit

Permalink
added new bin script bminstall.sh and corresponding Java client class…
Browse files Browse the repository at this point in the history
… whcih enables the agent to be uploaded to an already running program -- fixes BYTEMAN-125
  • Loading branch information
adinn committed Oct 22, 2010
1 parent 445b5bf commit 97c5573
Show file tree
Hide file tree
Showing 9 changed files with 389 additions and 6 deletions.
71 changes: 71 additions & 0 deletions bin/bminstall.sh
@@ -0,0 +1,71 @@
#!/bin/bash
#
# JBoss, Home of Professional Open Source
# Copyright 2010, Red Hat Middleware, and individual contributors
# by the @authors tag. See the copyright.txt in the distribution for a
# full listing of individual contributors.
#
# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation; either version 2.1 of
# the License, or (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public
# License along with this software; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA, or see the FSF site: http://www.fsf.org.
#
# @authors Andrew Dinn
#
# shell script which can be used to install the Byteman agent into
# a JVM which was started without the agent. This provides an
# alternative to using the -javaagent java command line flag
#
# usage: bminstall [-p port] [-h host] [-b] [-Dname[=value]]* pid
# pid is the process id of the target JVM
# -h host selects the host name or address the agent listener binds to
# -p port selects the port the agent listener binds to
# -b adds the byteman jar to the bootstrap classpath
# -Dname=value can be used to set system properties whose name starts with "org.jboss.byteman."
# expects to find a byteman agent jar in BYTEMAN_HOME
#
# use BYTEMAN_HOME to locate installed byteman release
if [ -z "$BYTEMAN_HOME" ]; then
# use the root of the path to this file to locate the byteman jar
BYTEMAN_HOME=${0%*/bin/bminstall.sh}
# allow for rename to plain bminstall
if [ "$BYTEMAN_HOME" == "$0" ]; then
BYTEMAN_HOME=${0%*/bin/bminstall}
fi
if [ "$BYTEMAN_HOME" == "$0" ]; then
echo "Unable to find byteman home"
exit
fi
fi

# the Install class is in the byteman-install jar
if [ -r ${BYTEMAN_HOME}/lib/byteman.jar ]; then
BYTEMAN_JAR=${BYTEMAN_HOME}/lib/byteman.jar
else
echo "Cannot locate byteman install jar"
exit
fi
# we also need a tools jar from JAVA_HOME
if [ -z "$JAVA_HOME" ]; then
echo "please set JAVA_HOME"
exit
fi
if [ -r ${JAVA_HOME}/lib/tools.jar ]; then
TOOLS_JAR=${JAVA_HOME}/lib/tools.jar
else
echo "Cannot locate byteman install jar"
exit
fi
# allow for extra java opts via setting BYTEMAN_JAVA_OPTS
# attach class will validate arguments

java ${BYTEMAN_JAVA_OPTS} -classpath ${BYTEMAN_JAR}:${TOOLS_JAR} org.jboss.byteman.agent.install.Install $*
2 changes: 1 addition & 1 deletion bin/bmjava.sh
@@ -1,7 +1,7 @@
#!/bin/bash
#
# JBoss, Home of Professional Open Source
# Copyright 2009, Red Hat Middleware LLC, and individual contributors
# Copyright 2009, Red Hat Middleware, and individual contributors
# by the @authors tag. See the copyright.txt in the distribution for a
# full listing of individual contributors.
#
Expand Down
2 changes: 1 addition & 1 deletion bin/bytemancheck.sh
@@ -1,7 +1,7 @@
#!/bin/bash
#
# JBoss, Home of Professional Open Source
# Copyright 2009, Red Hat Middleware LLC, and individual contributors
# Copyright 2009, Red Hat Middleware, and individual contributors
# by the @authors tag. See the copyright.txt in the distribution for a
# full listing of individual contributors.
#
Expand Down
8 changes: 4 additions & 4 deletions bin/submit.sh
@@ -1,7 +1,7 @@
#!/bin/bash
#
# JBoss, Home of Professional Open Source
# Copyright 2009, Red Hat Middleware LLC, and individual contributors
# Copyright 2009, Red Hat Middleware, and individual contributors
# by the @authors tag. See the copyright.txt in the distribution for a
# full listing of individual contributors.
#
Expand Down Expand Up @@ -53,10 +53,10 @@
# use BYTEMAN_HOME to locate installed byteman release
if [ -z "$BYTEMAN_HOME" ]; then
# use the root of the path to this file to locate the byteman jar
BYTEMAN_HOME=${0%*/bin/bmjava.sh}
# allow for rename to plain bmjava
BYTEMAN_HOME=${0%*/bin/submit.sh}
# allow for rename to plain submit
if [ "$BYTEMAN_HOME" == "$0" ]; then
BYTEMAN_HOME=${0%*/bin/bmjava}
BYTEMAN_HOME=${0%*/bin/submit}
fi
if [ "$BYTEMAN_HOME" == "$0" ]; then
echo "Unable to find byteman home"
Expand Down
Binary file modified docs/ProgrammersGuide.odt
Binary file not shown.
Binary file modified docs/ProgrammersGuide.pdf
Binary file not shown.
43 changes: 43 additions & 0 deletions src/org/jboss/byteman/agent/Main.java
Expand Up @@ -40,9 +40,20 @@
* agent class supplied at JVM startup to install byteman package bytecode transformer
*/
public class Main {
public static boolean firstTime = true;
public final static String BYTEMAN_PREFIX = "org.jboss.byteman.";

public static void premain(String args, Instrumentation inst)
throws Exception
{
// guard against the agent being loaded twice
synchronized (Main.class) {
if (firstTime) {
firstTime = false;
} else {
throw new Exception("Main : attempting to load Byteman agent more than once");
}
}
boolean allowRedefine = false;

if (args != null) {
Expand Down Expand Up @@ -73,6 +84,28 @@ public static void premain(String args, Instrumentation inst)
// this is only for backwards compatibility -- it is the same as listener
String value = arg.substring(REDEFINE_PREFIX.length(), arg.length());
allowRedefine = Boolean.parseBoolean(value);
} else if (arg.startsWith(PROP_PREFIX)) {
// this can be used to set byteman properties
String prop = arg.substring(PROP_PREFIX.length(), arg.length());
String value="";
if (prop.startsWith(BYTEMAN_PREFIX)) {
int index = prop.indexOf('=');
if (index > 0) {
// need to split off the value
if (index == prop.length() - 1)
{
// value is empty so just drop the =
prop = prop.substring(0, index);
} else {
value = prop.substring(index + 1);
prop = prop.substring(0, index);
}
}
System.out.println("Setting " + prop + "=" + value);
System.setProperty(prop, value);
} else {
System.err.println("Invalid property : " + prop);
}
} else {
System.err.println("org.jboss.byteman.agent.Main:\n" +
" illegal agent argument : " + arg + "\n" +
Expand Down Expand Up @@ -175,6 +208,10 @@ public static void premain(String args, Instrumentation inst)
}
}

public static void agentmain(String args, Instrumentation inst) throws Exception
{
premain(args, inst);
}
/**
* prefix used to specify port argument for agent
*/
Expand Down Expand Up @@ -213,6 +250,12 @@ public static void premain(String args, Instrumentation inst)

private static final String REDEFINE_PREFIX = "redefine:";

/**
* prefix used to specify system properties to be set before starting the agent
*/

private static final String PROP_PREFIX = "prop:";

/**
* list of paths to extra bootstrap jars supplied on command line
*/
Expand Down
2 changes: 2 additions & 0 deletions src/org/jboss/byteman/agent/Transformer.java
Expand Up @@ -218,11 +218,13 @@ public byte[] transform(ClassLoader originalLoader,
return null;
}

/*
if (checker.hasOuterClass()) {
// we don't transform inner classes for now
// TODO -- see if we can match and transform inner classes via the outer class
return null;
}
*/

// TODO-- reconsider this as it is a bit dodgy as far as security is concerned

Expand Down

0 comments on commit 97c5573

Please sign in to comment.