get all threads stuck in a certain place and let them continue at the same time (hello race condition)

throw Exceptions at unexpected locations

tracing through your code during execution

change return values

public class BrokenSingleton { private static volatile BrokenSingleton instance; private BrokenSingleton() { } public static BrokenSingleton get() { if (instance == null) { instance = new BrokenSingleton(); } return instance; } }

public class BrokenSingletonMain { public static void main(String[] args) throws Exception { Thread thread1 = new Thread(new SingletonAccessRunnable()); Thread thread2 = new Thread(new SingletonAccessRunnable()); thread1.start(); thread2.start(); thread1.join(); thread2.join(); } public static class SingletonAccessRunnable implements Runnable { @Override public void run() { System.out.println(BrokenSingleton.get()); } } }

RULE trace entering CLASS de.codepitbull.byteman.BrokenSingleton METHOD get AT ENTRY IF true DO traceln("entered get-Method") ENDRULE RULE trace read stacks CLASS de.codepitbull.byteman.BrokenSingleton METHOD get AFTER READ BrokenSingleton.instance IF true DO traceln("READ:

" + formatStack()) ENDRULE

# rule skeleton RULE <rule name> CLASS <class name> METHOD <method name> BIND <bindings> IF <condition> DO <actions> ENDRULE

__-javaagent:<BYTEMAN_HOME>/lib/byteman.jar=script:check.btm__

__-javaagent:<BYTEMAN_HOME>/lib/byteman.jar=script:appmain.btm,boot:<BYTEMAN_HOME>/lib/byteman.jar__

__<bytemanhome>/bin/bminstall.sh <pid>__

__<bytemanhome>/bin/bmsubmit.sh check.btm__

entered get-Method entered get-Method READ: Stack trace for thread Thread-0 de.codepitbull.byteman.BrokenSingleton.get(BrokenSingleton.java:14) de.codepitbull.byteman.BrokenSingletonMain$SingletonAccessRunnable.run(BrokenSingletonMain.java:20) java.lang.Thread.run(Thread.java:745) READ: Stack trace for thread Thread-1 de.codepitbull.byteman.BrokenSingleton.get(BrokenSingleton.java:14) de.codepitbull.byteman.BrokenSingletonMain$SingletonAccessRunnable.run(BrokenSingletonMain.java:20) java.lang.Thread.run(Thread.java:745)

RULE define rendezvous CLASS de.codepitbull.byteman.BrokenSingleton METHOD get AT ENTRY IF NOT isRendezvous("rendezvous", 2) DO createRendezvous("rendezvous", 2, true); traceln("rendezvous created"); ENDRULE

RULE catch threads CLASS de.codepitbull.byteman.BrokenSingleton METHOD get AFTER READ BrokenSingleton.instance IF isRendezvous("rendezvous", 2) DO rendezvous("rendezvous"); ENDRULE

<dependency> <groupId>org.jboss.byteman</groupId> <artifactId>byteman-submit</artifactId> <scope>test</scope> <version>${byteman.version}</version> </dependency>

@RunWith(BMUnitRunner.class) public class BrokenSingletonTest { @Test @BMScript("check.btm") public void testForRaceCondition() { ... } }