SlideShare a Scribd company logo
1 of 49
Download to read offline
Taming	
  Java	
  Agents	
  


Anton	
  Arhipov	
  
ZeroTurnaround,	
  JRebel	
  
@antonarhipov	
  
-­‐javaagent	
  
             	
  
java.lang.instrument	
  
java.lang.instrument	
  
import	
  java.lang.instrument.ClassFileTransformer;	
  
import	
  java.lang.instrument.InstrumentaBon;	
                           META-INF/MANIFEST.MF
	
                                                                         Premain-Class: Agent
public	
  class	
  Agent	
  {	
                                            Agent-Class: Agent

	
  	
  	
  public	
  staBc	
  void	
  premain(String	
  args,	
  InstrumentaBon	
  inst)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  inst.addTransformer(new	
  ClassFileTransformer	
  {	
  …	
  });	
  
	
  	
  	
  }	
  
	
  	
  	
  public	
  staBc	
  void	
  agentmain(String	
  args,	
  InstrumentaBon	
  inst)	
  {	
  
	
  	
  	
  	
  	
  	
  premain(args,	
  inst);	
  
	
  	
  	
  }	
  
}	
  
                          java	
  –javaagent:agent.jar	
  …	
  
META-­‐INF/MANIFEST.MF	
  
•    Premain-­‐Class	
  
•    Agent-­‐Main	
  
•    Boot-­‐Class-­‐Path	
  
•    Can-­‐Redefine-­‐Classes	
  
•    Can-­‐Retransform-­‐Classes	
  
j.l.instrument.ClassFileTransformer	
  	
  
 	
  
 	
  
 	
  	
  	
  	
  	
  	
  byte[]	
  transform(ClassLoader	
  loader,	
  	
  
 	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  String	
  className,	
  
 	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Class<?>	
  classBeingRedefined,	
  
 	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ProtecBonDomain	
  protecBonDomain,	
  
 	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  byte[]	
  classfileBuffer);	
  
public	
  class	
  MyTransformer	
  implements	
  ClassFileTransformer	
  {	
  
	
  	
  	
  	
  	
  	
  	
  public	
  void	
  byte[]	
  transform(ClassLoader	
  loader,	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  String	
  className,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Class<?>	
  classBeingRedefined,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ProtecBonDomain	
  protecBonDomain,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  byte[]	
  classfileBuffer){	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ClassPool	
  cp	
  =	
  ClassPool.getDefault();	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  CtClass	
  ct	
  =	
  cp.makeClass(	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  new	
  ByteArrayInputStream(classfileBuffer));	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  …	
  …	
  …	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  return	
  ct.toBytecode();	
  
public	
  class	
  MyTransformer	
  implements	
  ClassFileTransformer	
  {	
  
	
  	
  	
  	
  	
  	
  	
  public	
  void	
  byte[]	
  transform(ClassLoader	
  loader,	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  String	
  className,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Class<?>	
  classBeingRedefined,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ProtecBonDomain	
  protecBonDomain,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  byte[]	
  classfileBuffer){	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ClassPool	
  cp	
  =	
  ClassPool.getDefault();	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  CtClass	
  ct	
  =	
  cp.makeClass(	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  new	
  ByteArrayInputStream(classfileBuffer));	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  …	
  …	
  …	
  
	
  
                                                                                                                                                                                  Enter	
  Javassist!	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  return	
  ct.toBytecode();	
  
	
  	
  	
  	
  	
  	
  
public	
  class	
  MyTransformer	
  implements	
  ClassFileTransformer	
  {	
  
	
  	
  	
  	
  	
  	
  	
  public	
  void	
  byte[]	
  transform(ClassLoader	
  loader,	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  String	
  className,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Class<?>	
  classBeingRedefined,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ProtecBonDomain	
  protecBonDomain,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  byte[]	
  classfileBuffer){	
  
	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ct.addMethod(CtNewMethod.make(	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  "public	
  void	
  foo()	
  {}	
  ",	
  ct));	
  
	
  
	
  
	
  
	
  	
  	
  	
  	
  	
  
public	
  class	
  MyTransformer	
  implements	
  ClassFileTransformer	
  {	
  
	
  	
  	
  	
  	
  	
  	
  public	
  void	
  byte[]	
  transform(ClassLoader	
  loader,	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  String	
  className,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  Class<?>	
  classBeingRedefined,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  ProtecBonDomain	
  protecBonDomain,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  byte[]	
  classfileBuffer){	
  
	
  
	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  CtMethod	
  m	
  =	
  ct.getMethod("foo",	
  "()V");	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  m.insertA_er("System.out.println("..")");	
  
	
  
	
  
	
  
	
  	
  	
  	
  	
  	
  
j.l.instrument.InstrumentaBon	
  
	
  
	
  	
  	
  	
  	
  void	
  addTransformer(ClassFileTransformer	
  transformer,	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  boolean	
  canRetransform);	
  
	
  
	
  	
  	
  	
  	
  void	
  appendToBootstrapClassLoaderSearch(JarFile	
  jarfile);	
  
	
  	
  	
  	
  	
  void	
  appendToSystemClassLoaderSearch(JarFile	
  jarfile);	
  
	
  
	
  	
  	
  	
  	
  Class[]	
  getAllLoadedClasses();	
  
	
  	
  	
  	
  	
  Class[]	
  getIniBatedClasses(ClassLoader	
  loader);	
  
	
  	
  	
  	
  	
  
	
  	
  	
  	
  	
  void	
  redefineClasses(ClassDefiniBon...	
  classes);	
  
	
  	
  	
  	
  	
  void	
  retransformClasses(Class<?>...	
  classes);	
  
	
  	
  	
  	
  	
  .	
  .	
  .	
  
Example	
  1:	
  Simple	
  Trace	
  
public	
  class	
  Main	
  {	
  	
  
	
  	
  	
  	
  	
  public	
  staBc	
  void	
  main(String[]	
  args)	
  {	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  for	
  (int	
  i	
  =	
  0;	
  i	
  <	
  args.length;	
  i++)	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  System.out.println(args[i]);	
  	
  
	
  	
  	
  	
  	
  }	
  	
  
}	
  	
  
	
  
$	
  java	
  Main	
  1	
  2	
  3	
  	
  
1	
  
2	
  
3	
  
Example	
  1:	
  Simple	
  Trace	
  
RULE	
  trace	
  main	
  entry	
        RULE	
  trace	
  main	
  exit	
  
CLASS	
  Main	
                         CLASS	
  Main	
  
METHOD	
  main	
                        METHOD	
  main	
  
AT	
  ENTRY	
                           AT	
  EXIT	
  
IF	
  true	
                            IF	
  true	
  
DO	
  traceln("entering	
  main")	
     DO	
  traceln("exiBng	
  main")	
  
ENDRULE	
                               ENDRULE	
  

 $	
  java	
  –javaagent:byteman.jar=script:trace.btm	
  Main	
  
 entering	
  main	
  
 1	
  
 2	
  
 3	
  
 exiBng	
  main	
  
Example	
  2:	
  Tracing	
  Threads	
  
for	
  (int	
  i	
  =	
  0;	
  i	
  <	
  args.length;	
  i++)	
  {	
  	
  
	
  	
  	
  	
  	
  final	
  String	
  arg	
  =	
  args[i];	
  
	
  	
  	
  	
  	
  Thread	
  thread	
  =	
  new	
  Thread(arg)	
  {	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  public	
  void	
  run()	
  {	
  	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  System.out.println(arg);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  }	
  
	
  	
  	
  	
  	
  };	
  	
  
	
  	
  	
  	
  	
  thread.start();	
  	
  
	
  	
  	
  	
  	
  thread.join();	
  
}	
  
Example	
  2:	
  Tracing	
  Threads	
  
RULE	
  trace	
  thread	
  start	
  	
  
CLASS	
  java.lang.Thread	
  	
  
METHOD	
  start()	
  	
  
IF	
  true	
  	
  
DO	
  traceln("***	
  start	
  for	
  thread	
  "	
  +	
  $0.getName())	
  	
  
ENDRULE	
  	
  
Example	
  2:	
  Tracing	
  Threads	
  
$	
  	
  java	
  -­‐Dorg.jboss.byteman.transform.all	
  	
  
	
  	
  	
  -­‐javaagent:byteman.jar=script:thread.btm,boot:byteman.jar	
  	
  
	
  	
  	
  Main	
  foo	
  bar	
  baz	
  
	
  
***	
  start	
  for	
  thread	
  foo	
  
foo	
  
***	
  start	
  for	
  thread	
  bar	
  
bar	
  
***	
  start	
  for	
  thread	
  baz	
  
baz	
  
Example	
  3:	
  Side	
  Effects	
  
RULE	
  skip	
  loop	
  iteraBon	
  
CLASS	
  Main	
  
METHOD	
  main	
  
AFTER	
  CALL	
  join	
  
IF	
  ($args[$i]).contains("foo")	
  
DO	
  $i	
  =	
  $i	
  +	
  1	
  ;	
  
	
  	
  	
  traceln("skipping	
  iteraBon	
  "	
  +	
  $i)	
  
ENDRULE	
  
Example	
  4:	
  Arach	
  to	
  Process	
  
BufferedReader	
  in	
  =	
  new	
  BufferedReader(new	
  InputStreamReader(System.in));	
  
String	
  next	
  =	
  in.readLine();	
  
	
  
while	
  (next	
  !=	
  null	
  &&	
  next.length()	
  >	
  0	
  &&	
  !next.contains("end"))	
  {	
  
	
  	
  	
  	
  final	
  String	
  arg	
  =	
  next;	
  
	
  	
  	
  	
  Thread	
  thread	
  =	
  new	
  Thread(arg)	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  public	
  void	
  run()	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  System.out.println(arg);	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  }	
  	
  
	
  	
  	
  	
  	
  };	
  
	
  	
  	
  	
  	
  thread.start();	
  
	
  	
  	
  	
  	
  thread.join();	
  
	
  	
  	
  	
  	
  next	
  =	
  in.readLine();	
  
}	
  
Example	
  4:	
  Arach	
  to	
  Process	
  
 $	
  java	
  Main	
  
 Hello	
  
 Hello	
  
 	
  
                                             $	
  bminstall.sh	
  -­‐b	
  –Dorg…	
  <PID>	
  
                                             $	
  bmsubmit.sh	
  script/ex2.btm	
  	
  
                                             install	
  rule	
  trace	
  thread	
  start	
  
 Test	
  
 ***	
  start	
  for	
  thread	
  Test	
  
 Test	
  
 	
  
                                              $	
  bmsubmit.sh	
  -­‐u	
  script/ex2.btm	
  	
  
                                              uninstall	
  RULE	
  trace	
  thread	
  start	
  
 Test	
  
 Test	
  
 end	
  
 $	
  
Arach	
  API	
  

VirtualMachine	
  vm	
  =	
  VirtualMachine.arach("2177");	
  
	
  
vm.loadAgent("agent.jar",	
  "arg1=x,arg2=y");	
  
	
  
vm.detach();	
  
	
  
	
  

     LimitaBon:	
  can’t	
  change	
  class	
  schema	
  
Fault	
  InjecBon	
  
          VS	
  
  Mocking	
  
Example	
  5:	
  Fault	
  InjecBon	
  
@RunWith(BMUnitRunner.class)	
  
public	
  class	
  BytemanJUnitTests	
  {	
  
	
  
	
  	
  	
  	
  	
  @Test(expected=MyServiceUnavailableExcepBon.class)	
  
	
  	
  	
  	
  	
  @BMRule(name="throw	
  Bmeout	
  at	
  1st	
  call",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetClass	
  =	
  "Socket",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetMethod	
  =	
  "connect",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  acBon	
  =	
  "throw	
  new	
  java.io.IOExcepBon()")	
  
	
  
	
  	
  	
  	
  	
  	
  public	
  void	
  testErrorInPipeline()	
  throws	
  ExcepBon	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Invokes	
  internally	
  Socket.connect(..):	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  new	
  MyHrpClient("hrp://example.com/data").read();	
  
	
  	
  	
  	
  	
  	
  }	
  
}	
  
Example	
  5:	
  Fault	
  InjecBon	
  
@RunWith(BMUnitRunner.class)	
  
public	
  class	
  BytemanJUnitTests	
  {	
  
	
  
	
  	
  	
  	
  	
  @Test(expected=MyServiceUnavailableExcepBon.class)	
  
	
  	
  	
  	
  	
  @BMRule(name="throw	
  Bmeout	
  at	
  1st	
  call",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetClass	
  =	
  "Socket",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetMethod	
  =	
  "connect",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  acBon	
  =	
  "throw	
  new	
  java.io.IOExcepBon()")	
  
	
  
	
  	
  	
  	
  	
  	
  public	
  void	
  testErrorInPipeline()	
  throws	
  ExcepBon	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Invokes	
  internally	
  Socket.connect(..):	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  new	
  MyHrpClient("hrp://example.com/data").read();	
  
	
  	
  	
  	
  	
  	
  }	
  
}	
  
Example	
  5:	
  Fault	
  InjecBon	
  
@RunWith(BMUnitRunner.class)	
  
public	
  class	
  BytemanJUnitTests	
  {	
  
	
  
	
  	
  	
  	
  	
  @Test(expected=MyServiceUnavailableExcepBon.class)	
  
	
  	
  	
  	
  	
  @BMRule(name="throw	
  Bmeout	
  at	
  1st	
  call",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetClass	
  =	
  "Socket",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetMethod	
  =	
  "connect",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  acBon	
  =	
  "throw	
  new	
  java.io.IOExcepBon()")	
  
	
  
	
  	
  	
  	
  	
  	
  public	
  void	
  testErrorInPipeline()	
  throws	
  ExcepBon	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Invokes	
  internally	
  Socket.connect(..):	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  new	
  MyHrpClient("hrp://example.com/data").read();	
  
	
  	
  	
  	
  	
  	
  }	
  
}	
  
Example	
  5:	
  Fault	
  InjecBon	
  
@RunWith(BMUnitRunner.class)	
  
public	
  class	
  BytemanJUnitTests	
  {	
  
	
  
	
  	
  	
  	
  	
  @Test(expected=MyServiceUnavailableExcepBon.class)	
  
	
  	
  	
  	
  	
  @BMRule(name="throw	
  Bmeout	
  at	
  1st	
  call",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetClass	
  =	
  "Socket",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetMethod	
  =	
  "connect",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  acBon	
  =	
  "throw	
  new	
  java.io.IOExcepBon()")	
  
	
  
	
  	
  	
  	
  	
  	
  public	
  void	
  testErrorInPipeline()	
  throws	
  ExcepBon	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Invokes	
  internally	
  Socket.connect(..):	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  new	
  MyHrpClient("hrp://example.com/data").read();	
  
	
  	
  	
  	
  	
  	
  }	
  
}	
  
Example	
  5:	
  Fault	
  InjecBon	
  
@RunWith(BMUnitRunner.class)	
  
public	
  class	
  BytemanJUnitTests	
  {	
  
	
  
	
  	
  	
  	
  	
  @Test(expected=MyServiceUnavailableExcepBon.class)	
  
	
  	
  	
  	
  	
  @BMRule(name="throw	
  Bmeout	
  at	
  1st	
  call",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetClass	
  =	
  "Socket",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  targetMethod	
  =	
  "connect",	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  acBon	
  =	
  "throw	
  new	
  java.io.IOExcepBon()")	
  
	
  
	
  	
  	
  	
  	
  	
  public	
  void	
  testErrorInPipeline()	
  throws	
  ExcepBon	
  {	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  //	
  Invokes	
  internally	
  Socket.connect(..):	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  new	
  MyHrpClient("hrp://example.com/data").read();	
  
	
  	
  	
  	
  	
  	
  }	
  
}	
  
@Path("/")	
  
public	
  String	
  foo()	
  {	
  
	
  	
  return	
  "FooBar";	
  
}	
  
@Path("/")	
  
public	
  String	
  foo()	
  {	
  
	
  	
  return	
  "FooBar";	
  
}	
  

                      @Path("/")	
  
                      public	
  FooBar	
  foo()	
  {	
  
                      	
  	
  return	
  new	
  FooBar();	
  
                      }	
  
@Path("/")	
  
public	
  String	
  foo()	
  {	
  
	
  	
  return	
  "FooBar";	
  
}	
  

                      @Path("/foobar")	
  
                      public	
  FooBar	
  foo()	
  {	
  
                      	
  	
  return	
  new	
  FooBar();	
  
                      }	
  
JRebel	
  
                                                                 Make	
  changes	
  in	
  IDE	
  
                        OldClassLoader
Framework	
  



                  MyObject.class	
  
                   Code	
  
                   101000101	
  
                   100010010	
                 New	
  code	
  
                                                                        JRebel	
  
                                               111000100	
  
                                               101010010	
  



                MyObject
                                                  ConfiguraDon	
  
                                               (XML,	
  annotaDons,..)	
  
java	
  –javaagent:jrebel.jar	
  App	
  
Chronon	
  
•  A	
  back-­‐in-­‐Bme	
  debugger	
  for	
  Java	
  
    –  Recording	
  a	
  program	
  execuBon	
  
    –  Debugging	
  the	
  recording	
  
•  What	
  is	
  recorded?	
  
    –  Variable	
  history	
  
    –  Method	
  calls	
  
    –  ExcepBons	
  
    –  Console	
  output	
  
    –  Threads	
  
Memory	
  buffer	
  

                            Recorded	
  data	
  

                                                   Flusher	
  threads	
  




ApplicaBon	
  threads	
  

                                                        Recording	
  
java	
  -­‐javaagent:recorder.jar=config.txt	
  
Recording	
  Local	
  Variable	
  Changes	
  
•  Just	
  a	
  brute-­‐force	
  idea:	
  
    –  Instrument	
  *store	
  (astore,	
  istore,	
  etc)	
  instrucBons	
  
       to	
  write	
  the	
  value	
  to	
  a	
  store	
  

    int	
  i	
  =	
  2;	
      iconst_2	
                iconst_2	
  
                               istore_1	
                istore_1	
  
                                                         iload_1	
  
                                                         invokestaBc	
  …	
  
Plumbr:	
  Memory	
  leak	
  detecBon	
  
•  RunBme	
  applicaBon	
  analysis	
  
   –  vs	
  post-­‐mortem	
  heap	
  dump	
  analysis	
  
   –  vs	
  profilers	
  –	
  human	
  operated,	
  rarely	
  usable	
  in	
  
      producBon	
  
•  AdapBve	
  introspecBon	
  
   –  Monitor	
  high	
  level	
  metrics,	
  cheap	
  to	
  obtain	
  
   –  Collect	
  expensive	
  detailed	
  informaBon	
  when	
  
      suspects	
  are	
  found	
  
Plumbr	
  
•  Memory	
  leak	
  in	
  Java	
  –	
  some	
  objects	
  are	
  
     created	
  Bme	
  a_er	
  Bme	
  but	
  a	
  reference	
  is	
  
     forgoren	
  
•  Look	
  at	
  when	
  object	
  was	
  created	
  and	
  how	
  
     long	
  it	
  lives	
  
	
  
What	
  is	
  leaking?	
  

Time (garbage collection cycles)




Instances of a class that belong to base application framework:


Class instances that handle user interaction:


Common JDK class instances:


Instances of a leaking class:
-­‐javaagent	
  
             	
  
java.lang.instrument	
  
@antonarhipov	
  
anton@zeroturnaround.com	
  

More Related Content

What's hot

The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31Mahmoud Samir Fayed
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introductioncaswenson
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP Zaenal Arifin
 
ConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with GroovyConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with GroovyIván López Martín
 
Java căn bản - Chapter2
Java căn bản - Chapter2Java căn bản - Chapter2
Java căn bản - Chapter2Vince Vo
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to KotlinMagda Miu
 
모던자바의 역습
모던자바의 역습모던자바의 역습
모던자바의 역습DoHyun Jung
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersBartosz Kosarzycki
 
The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agentsRafael Winterhalter
 
Logic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with EclipseLogic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with EclipseCoen De Roover
 

What's hot (20)

The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31The Ring programming language version 1.4.1 book - Part 9 of 31
The Ring programming language version 1.4.1 book - Part 9 of 31
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
NIO and NIO2
NIO and NIO2NIO and NIO2
NIO and NIO2
 
JavaYDL11
JavaYDL11JavaYDL11
JavaYDL11
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
Modul Praktek Java OOP
Modul Praktek Java OOP Modul Praktek Java OOP
Modul Praktek Java OOP
 
Project Coin
Project CoinProject Coin
Project Coin
 
ConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with GroovyConFess Vienna 2015 - Metaprogramming with Groovy
ConFess Vienna 2015 - Metaprogramming with Groovy
 
Java căn bản - Chapter2
Java căn bản - Chapter2Java căn bản - Chapter2
Java căn bản - Chapter2
 
Intro to Kotlin
Intro to KotlinIntro to Kotlin
Intro to Kotlin
 
oops-1
oops-1oops-1
oops-1
 
Unit i
Unit iUnit i
Unit i
 
모던자바의 역습
모던자바의 역습모던자바의 역습
모던자바의 역습
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
Kotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developersKotlin advanced - language reference for android developers
Kotlin advanced - language reference for android developers
 
Java 10, Java 11 and beyond
Java 10, Java 11 and beyondJava 10, Java 11 and beyond
Java 10, Java 11 and beyond
 
Revisão OCPJP7 - Class Design (parte 01)
Revisão OCPJP7 - Class Design (parte 01)Revisão OCPJP7 - Class Design (parte 01)
Revisão OCPJP7 - Class Design (parte 01)
 
The definitive guide to java agents
The definitive guide to java agentsThe definitive guide to java agents
The definitive guide to java agents
 
Logic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with EclipseLogic-based program transformation in symbiosis with Eclipse
Logic-based program transformation in symbiosis with Eclipse
 

Viewers also liked

Con-FESS 2015 - Is your profiler speaking to you?
Con-FESS 2015 - Is your profiler speaking to you?Con-FESS 2015 - Is your profiler speaking to you?
Con-FESS 2015 - Is your profiler speaking to you?Anton Arhipov
 
import continuous.delivery.*
import continuous.delivery.*import continuous.delivery.*
import continuous.delivery.*Anton Arhipov
 
Improve your Developer Experiece using the WAS Liberty Profile with JRebel
Improve your Developer Experiece using the WAS Liberty Profile with JRebel Improve your Developer Experiece using the WAS Liberty Profile with JRebel
Improve your Developer Experiece using the WAS Liberty Profile with JRebel Anton Arhipov
 
JPoint 2015 - Javassist на службе Java-разработчика
JPoint 2015 - Javassist на службе Java-разработчикаJPoint 2015 - Javassist на службе Java-разработчика
JPoint 2015 - Javassist на службе Java-разработчикаAnton Arhipov
 
NetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportNetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportAnton Arhipov
 
Загрузчики классов в Java - коллекция граблей
Загрузчики классов в Java - коллекция граблейЗагрузчики классов в Java - коллекция граблей
Загрузчики классов в Java - коллекция граблейAnton Arhipov
 
JPoint 2016 - Bytecode
JPoint 2016 - BytecodeJPoint 2016 - Bytecode
JPoint 2016 - BytecodeAnton Arhipov
 
Con-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With JavassistCon-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With JavassistAnton Arhipov
 
Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101Anton Arhipov
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to GroovyAnton Arhipov
 
JavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistJavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistAnton Arhipov
 
Something about Golang
Something about GolangSomething about Golang
Something about GolangAnton Arhipov
 
JPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profilerJPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profilerAnton Arhipov
 
Devclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервьюDevclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервьюAnton Arhipov
 
Oredev 2015 - Taming Java Agents
Oredev 2015 - Taming Java AgentsOredev 2015 - Taming Java Agents
Oredev 2015 - Taming Java AgentsAnton Arhipov
 
Jenkins Evolutions - JEEConf 2012
Jenkins Evolutions - JEEConf 2012Jenkins Evolutions - JEEConf 2012
Jenkins Evolutions - JEEConf 2012Anton Arhipov
 
Riga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistRiga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistAnton Arhipov
 
Voxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with JavassistVoxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with JavassistAnton Arhipov
 
Something about Golang
Something about GolangSomething about Golang
Something about GolangAnton Arhipov
 

Viewers also liked (19)

Con-FESS 2015 - Is your profiler speaking to you?
Con-FESS 2015 - Is your profiler speaking to you?Con-FESS 2015 - Is your profiler speaking to you?
Con-FESS 2015 - Is your profiler speaking to you?
 
import continuous.delivery.*
import continuous.delivery.*import continuous.delivery.*
import continuous.delivery.*
 
Improve your Developer Experiece using the WAS Liberty Profile with JRebel
Improve your Developer Experiece using the WAS Liberty Profile with JRebel Improve your Developer Experiece using the WAS Liberty Profile with JRebel
Improve your Developer Experiece using the WAS Liberty Profile with JRebel
 
JPoint 2015 - Javassist на службе Java-разработчика
JPoint 2015 - Javassist на службе Java-разработчикаJPoint 2015 - Javassist на службе Java-разработчика
JPoint 2015 - Javassist на службе Java-разработчика
 
NetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience ReportNetBeans Plugin Development: JRebel Experience Report
NetBeans Plugin Development: JRebel Experience Report
 
Загрузчики классов в Java - коллекция граблей
Загрузчики классов в Java - коллекция граблейЗагрузчики классов в Java - коллекция граблей
Загрузчики классов в Java - коллекция граблей
 
JPoint 2016 - Bytecode
JPoint 2016 - BytecodeJPoint 2016 - Bytecode
JPoint 2016 - Bytecode
 
Con-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With JavassistCon-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With Javassist
 
Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101Joker 2016 - Bytecode 101
Joker 2016 - Bytecode 101
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 
JavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with JavassistJavaOne 2015 - Having fun with Javassist
JavaOne 2015 - Having fun with Javassist
 
Something about Golang
Something about GolangSomething about Golang
Something about Golang
 
JPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profilerJPoint 2016 - Etudes of DIY Java profiler
JPoint 2016 - Etudes of DIY Java profiler
 
Devclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервьюDevclub 01/2017 - (Не)адекватное Java-интервью
Devclub 01/2017 - (Не)адекватное Java-интервью
 
Oredev 2015 - Taming Java Agents
Oredev 2015 - Taming Java AgentsOredev 2015 - Taming Java Agents
Oredev 2015 - Taming Java Agents
 
Jenkins Evolutions - JEEConf 2012
Jenkins Evolutions - JEEConf 2012Jenkins Evolutions - JEEConf 2012
Jenkins Evolutions - JEEConf 2012
 
Riga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with JavassistRiga Dev Day 2016 - Having fun with Javassist
Riga Dev Day 2016 - Having fun with Javassist
 
Voxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with JavassistVoxxed Days Vilnius 2015 - Having fun with Javassist
Voxxed Days Vilnius 2015 - Having fun with Javassist
 
Something about Golang
Something about GolangSomething about Golang
Something about Golang
 

Similar to Taming Java Agents

Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012Anton Arhipov
 
Java agents are watching your ByteCode
Java agents are watching your ByteCodeJava agents are watching your ByteCode
Java agents are watching your ByteCodeRoman Tsypuk
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistAnton Arhipov
 
Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]Palak Sanghani
 
from java to c
from java to cfrom java to c
from java to cVõ Hòa
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นFinian Nian
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarROHIT JAISWAR
 
Hotspot & hotswap, who and who are best freinds
Hotspot & hotswap, who and who are best freindsHotspot & hotswap, who and who are best freinds
Hotspot & hotswap, who and who are best freinds亚军 汪
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical FileFahad Shaikh
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMRafael Winterhalter
 
Class loader basic
Class loader basicClass loader basic
Class loader basic명철 강
 

Similar to Taming Java Agents (20)

Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012Binary patching for fun and profit @ JUG.ru, 25.02.2012
Binary patching for fun and profit @ JUG.ru, 25.02.2012
 
Java agents are watching your ByteCode
Java agents are watching your ByteCodeJava agents are watching your ByteCode
Java agents are watching your ByteCode
 
Java ppt
Java pptJava ppt
Java ppt
 
Java byte code in practice
Java byte code in practiceJava byte code in practice
Java byte code in practice
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
 
Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]Lec 5 13_aug [compatibility mode]
Lec 5 13_aug [compatibility mode]
 
from java to c
from java to cfrom java to c
from java to c
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
Nabil code
Nabil  codeNabil  code
Nabil code
 
Nabil code
Nabil  codeNabil  code
Nabil code
 
Nabil code
Nabil  codeNabil  code
Nabil code
 
Java programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswarJava programming lab_manual_by_rohit_jaiswar
Java programming lab_manual_by_rohit_jaiswar
 
Java Reflection
Java ReflectionJava Reflection
Java Reflection
 
Hotspot & hotswap, who and who are best freinds
Hotspot & hotswap, who and who are best freindsHotspot & hotswap, who and who are best freinds
Hotspot & hotswap, who and who are best freinds
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
Advanced Java - Practical File
Advanced Java - Practical FileAdvanced Java - Practical File
Advanced Java - Practical File
 
Making Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVMMaking Java more dynamic: runtime code generation for the JVM
Making Java more dynamic: runtime code generation for the JVM
 
Class loader basic
Class loader basicClass loader basic
Class loader basic
 
Python tour
Python tourPython tour
Python tour
 
Java interface
Java interfaceJava interface
Java interface
 

More from Anton Arhipov

JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfAnton Arhipov
 
TechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервьюTechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервьюAnton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCityAnton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCityAnton Arhipov
 
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hourDevoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hourAnton Arhipov
 
GeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hourGeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hourAnton Arhipov
 
Build pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSLBuild pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSLAnton Arhipov
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCityAnton Arhipov
 
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainersJavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainersAnton Arhipov
 
GeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersGeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersAnton Arhipov
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingAnton Arhipov
 
JavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassleJavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassleAnton Arhipov
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingAnton Arhipov
 
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloadingJavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloadingAnton Arhipov
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentationJUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentationAnton Arhipov
 
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloadingRiga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloadingAnton Arhipov
 
GeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassleGeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassleAnton Arhipov
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingAnton Arhipov
 

More from Anton Arhipov (19)

JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
 
Idiomatic kotlin
Idiomatic kotlinIdiomatic kotlin
Idiomatic kotlin
 
TechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервьюTechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервью
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hourDevoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
 
GeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hourGeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hour
 
Build pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSLBuild pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSL
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainersJavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
 
GeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersGeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainers
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
 
JavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassleJavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassle
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
 
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloadingJavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentationJUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentation
 
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloadingRiga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
 
GeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassleGeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassle
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
 

Recently uploaded

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 

Recently uploaded (20)

Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 

Taming Java Agents

  • 1. Taming  Java  Agents   Anton  Arhipov   ZeroTurnaround,  JRebel   @antonarhipov  
  • 2. -­‐javaagent     java.lang.instrument  
  • 3. java.lang.instrument   import  java.lang.instrument.ClassFileTransformer;   import  java.lang.instrument.InstrumentaBon;   META-INF/MANIFEST.MF   Premain-Class: Agent public  class  Agent  {   Agent-Class: Agent      public  staBc  void  premain(String  args,  InstrumentaBon  inst)  {                inst.addTransformer(new  ClassFileTransformer  {  …  });        }        public  staBc  void  agentmain(String  args,  InstrumentaBon  inst)  {              premain(args,  inst);        }   }   java  –javaagent:agent.jar  …  
  • 4. META-­‐INF/MANIFEST.MF   •  Premain-­‐Class   •  Agent-­‐Main   •  Boot-­‐Class-­‐Path   •  Can-­‐Redefine-­‐Classes   •  Can-­‐Retransform-­‐Classes  
  • 5. j.l.instrument.ClassFileTransformer                    byte[]  transform(ClassLoader  loader,                                                                              String  className,                                                                            Class<?>  classBeingRedefined,                                                                            ProtecBonDomain  protecBonDomain,                                                                            byte[]  classfileBuffer);  
  • 6. public  class  MyTransformer  implements  ClassFileTransformer  {                public  void  byte[]  transform(ClassLoader  loader,                                                                              String  className,                                                                            Class<?>  classBeingRedefined,                                                                            ProtecBonDomain  protecBonDomain,                                                                            byte[]  classfileBuffer){                                ClassPool  cp  =  ClassPool.getDefault();                              CtClass  ct  =  cp.makeClass(                                                                            new  ByteArrayInputStream(classfileBuffer));                                  …  …  …                                return  ct.toBytecode();  
  • 7. public  class  MyTransformer  implements  ClassFileTransformer  {                public  void  byte[]  transform(ClassLoader  loader,                                                                              String  className,                                                                            Class<?>  classBeingRedefined,                                                                            ProtecBonDomain  protecBonDomain,                                                                            byte[]  classfileBuffer){                                ClassPool  cp  =  ClassPool.getDefault();                              CtClass  ct  =  cp.makeClass(                                                                            new  ByteArrayInputStream(classfileBuffer));                                  …  …  …     Enter  Javassist!                              return  ct.toBytecode();              
  • 8. public  class  MyTransformer  implements  ClassFileTransformer  {                public  void  byte[]  transform(ClassLoader  loader,                                                                              String  className,                                                                            Class<?>  classBeingRedefined,                                                                            ProtecBonDomain  protecBonDomain,                                                                            byte[]  classfileBuffer){                                ct.addMethod(CtNewMethod.make(                                  "public  void  foo()  {}  ",  ct));                    
  • 9. public  class  MyTransformer  implements  ClassFileTransformer  {                public  void  byte[]  transform(ClassLoader  loader,                                                                              String  className,                                                                            Class<?>  classBeingRedefined,                                                                            ProtecBonDomain  protecBonDomain,                                                                            byte[]  classfileBuffer){                                CtMethod  m  =  ct.getMethod("foo",  "()V");                            m.insertA_er("System.out.println("..")");                    
  • 10. j.l.instrument.InstrumentaBon              void  addTransformer(ClassFileTransformer  transformer,                                                                                          boolean  canRetransform);              void  appendToBootstrapClassLoaderSearch(JarFile  jarfile);            void  appendToSystemClassLoaderSearch(JarFile  jarfile);              Class[]  getAllLoadedClasses();            Class[]  getIniBatedClasses(ClassLoader  loader);                      void  redefineClasses(ClassDefiniBon...  classes);            void  retransformClasses(Class<?>...  classes);            .  .  .  
  • 11.
  • 12.
  • 13.
  • 14.
  • 15. Example  1:  Simple  Trace   public  class  Main  {              public  staBc  void  main(String[]  args)  {                        for  (int  i  =  0;  i  <  args.length;  i++)                                System.out.println(args[i]);              }     }       $  java  Main  1  2  3     1   2   3  
  • 16. Example  1:  Simple  Trace   RULE  trace  main  entry   RULE  trace  main  exit   CLASS  Main   CLASS  Main   METHOD  main   METHOD  main   AT  ENTRY   AT  EXIT   IF  true   IF  true   DO  traceln("entering  main")   DO  traceln("exiBng  main")   ENDRULE   ENDRULE   $  java  –javaagent:byteman.jar=script:trace.btm  Main   entering  main   1   2   3   exiBng  main  
  • 17. Example  2:  Tracing  Threads   for  (int  i  =  0;  i  <  args.length;  i++)  {              final  String  arg  =  args[i];            Thread  thread  =  new  Thread(arg)  {                          public  void  run()  {                                      System.out.println(arg);                        }            };              thread.start();              thread.join();   }  
  • 18. Example  2:  Tracing  Threads   RULE  trace  thread  start     CLASS  java.lang.Thread     METHOD  start()     IF  true     DO  traceln("***  start  for  thread  "  +  $0.getName())     ENDRULE    
  • 19. Example  2:  Tracing  Threads   $    java  -­‐Dorg.jboss.byteman.transform.all          -­‐javaagent:byteman.jar=script:thread.btm,boot:byteman.jar          Main  foo  bar  baz     ***  start  for  thread  foo   foo   ***  start  for  thread  bar   bar   ***  start  for  thread  baz   baz  
  • 20. Example  3:  Side  Effects   RULE  skip  loop  iteraBon   CLASS  Main   METHOD  main   AFTER  CALL  join   IF  ($args[$i]).contains("foo")   DO  $i  =  $i  +  1  ;        traceln("skipping  iteraBon  "  +  $i)   ENDRULE  
  • 21. Example  4:  Arach  to  Process   BufferedReader  in  =  new  BufferedReader(new  InputStreamReader(System.in));   String  next  =  in.readLine();     while  (next  !=  null  &&  next.length()  >  0  &&  !next.contains("end"))  {          final  String  arg  =  next;          Thread  thread  =  new  Thread(arg)  {                    public  void  run()  {                              System.out.println(arg);                    }              };            thread.start();            thread.join();            next  =  in.readLine();   }  
  • 22. Example  4:  Arach  to  Process   $  java  Main   Hello   Hello     $  bminstall.sh  -­‐b  –Dorg…  <PID>   $  bmsubmit.sh  script/ex2.btm     install  rule  trace  thread  start   Test   ***  start  for  thread  Test   Test     $  bmsubmit.sh  -­‐u  script/ex2.btm     uninstall  RULE  trace  thread  start   Test   Test   end   $  
  • 23. Arach  API   VirtualMachine  vm  =  VirtualMachine.arach("2177");     vm.loadAgent("agent.jar",  "arg1=x,arg2=y");     vm.detach();       LimitaBon:  can’t  change  class  schema  
  • 24. Fault  InjecBon   VS   Mocking  
  • 25. Example  5:  Fault  InjecBon   @RunWith(BMUnitRunner.class)   public  class  BytemanJUnitTests  {              @Test(expected=MyServiceUnavailableExcepBon.class)            @BMRule(name="throw  Bmeout  at  1st  call",                                                  targetClass  =  "Socket",                                                  targetMethod  =  "connect",                                                  acBon  =  "throw  new  java.io.IOExcepBon()")                public  void  testErrorInPipeline()  throws  ExcepBon  {                        //  Invokes  internally  Socket.connect(..):                        new  MyHrpClient("hrp://example.com/data").read();              }   }  
  • 26. Example  5:  Fault  InjecBon   @RunWith(BMUnitRunner.class)   public  class  BytemanJUnitTests  {              @Test(expected=MyServiceUnavailableExcepBon.class)            @BMRule(name="throw  Bmeout  at  1st  call",                                                  targetClass  =  "Socket",                                                  targetMethod  =  "connect",                                                  acBon  =  "throw  new  java.io.IOExcepBon()")                public  void  testErrorInPipeline()  throws  ExcepBon  {                        //  Invokes  internally  Socket.connect(..):                        new  MyHrpClient("hrp://example.com/data").read();              }   }  
  • 27. Example  5:  Fault  InjecBon   @RunWith(BMUnitRunner.class)   public  class  BytemanJUnitTests  {              @Test(expected=MyServiceUnavailableExcepBon.class)            @BMRule(name="throw  Bmeout  at  1st  call",                                                  targetClass  =  "Socket",                                                  targetMethod  =  "connect",                                                  acBon  =  "throw  new  java.io.IOExcepBon()")                public  void  testErrorInPipeline()  throws  ExcepBon  {                        //  Invokes  internally  Socket.connect(..):                        new  MyHrpClient("hrp://example.com/data").read();              }   }  
  • 28. Example  5:  Fault  InjecBon   @RunWith(BMUnitRunner.class)   public  class  BytemanJUnitTests  {              @Test(expected=MyServiceUnavailableExcepBon.class)            @BMRule(name="throw  Bmeout  at  1st  call",                                                  targetClass  =  "Socket",                                                  targetMethod  =  "connect",                                                  acBon  =  "throw  new  java.io.IOExcepBon()")                public  void  testErrorInPipeline()  throws  ExcepBon  {                        //  Invokes  internally  Socket.connect(..):                        new  MyHrpClient("hrp://example.com/data").read();              }   }  
  • 29. Example  5:  Fault  InjecBon   @RunWith(BMUnitRunner.class)   public  class  BytemanJUnitTests  {              @Test(expected=MyServiceUnavailableExcepBon.class)            @BMRule(name="throw  Bmeout  at  1st  call",                                                  targetClass  =  "Socket",                                                  targetMethod  =  "connect",                                                  acBon  =  "throw  new  java.io.IOExcepBon()")                public  void  testErrorInPipeline()  throws  ExcepBon  {                        //  Invokes  internally  Socket.connect(..):                        new  MyHrpClient("hrp://example.com/data").read();              }   }  
  • 30.
  • 31. @Path("/")   public  String  foo()  {      return  "FooBar";   }  
  • 32. @Path("/")   public  String  foo()  {      return  "FooBar";   }   @Path("/")   public  FooBar  foo()  {      return  new  FooBar();   }  
  • 33. @Path("/")   public  String  foo()  {      return  "FooBar";   }   @Path("/foobar")   public  FooBar  foo()  {      return  new  FooBar();   }  
  • 34. JRebel   Make  changes  in  IDE   OldClassLoader Framework   MyObject.class   Code   101000101   100010010   New  code   JRebel   111000100   101010010   MyObject ConfiguraDon   (XML,  annotaDons,..)  
  • 36.
  • 37. Chronon   •  A  back-­‐in-­‐Bme  debugger  for  Java   –  Recording  a  program  execuBon   –  Debugging  the  recording   •  What  is  recorded?   –  Variable  history   –  Method  calls   –  ExcepBons   –  Console  output   –  Threads  
  • 38. Memory  buffer   Recorded  data   Flusher  threads   ApplicaBon  threads   Recording  
  • 40.
  • 41. Recording  Local  Variable  Changes   •  Just  a  brute-­‐force  idea:   –  Instrument  *store  (astore,  istore,  etc)  instrucBons   to  write  the  value  to  a  store   int  i  =  2;   iconst_2   iconst_2   istore_1   istore_1   iload_1   invokestaBc  …  
  • 42.
  • 43. Plumbr:  Memory  leak  detecBon   •  RunBme  applicaBon  analysis   –  vs  post-­‐mortem  heap  dump  analysis   –  vs  profilers  –  human  operated,  rarely  usable  in   producBon   •  AdapBve  introspecBon   –  Monitor  high  level  metrics,  cheap  to  obtain   –  Collect  expensive  detailed  informaBon  when   suspects  are  found  
  • 44. Plumbr   •  Memory  leak  in  Java  –  some  objects  are   created  Bme  a_er  Bme  but  a  reference  is   forgoren   •  Look  at  when  object  was  created  and  how   long  it  lives    
  • 45. What  is  leaking?   Time (garbage collection cycles) Instances of a class that belong to base application framework: Class instances that handle user interaction: Common JDK class instances: Instances of a leaking class:
  • 46.
  • 47.
  • 48. -­‐javaagent     java.lang.instrument