How to set a breakpoint before execution

I would like to set a breakpoint in the application before it starts, so that I can make sure that the application does not pass a breakpoint at startup.

To set a breakpoint, you need to do something like:

EventRequestManager reqMan = vm.eventRequestManager(); BreakpointRequest bpReq = reqMan.createBreakpointRequest(locationForBreakpoint); bpReq.enable(); 

To get the location of the breakpoint, you can do something like:

 Method method = location.method(); List<Location> locations = method.locationsOfLine(55); Location locationForBreakpoint = locations.get(0); 

To get the method, you can do something like:

 classType.concreteMethodByName(methodNname, String signature) 

However, in order to get this class, it seems to you that you need an ObjectReference, which seems to require a working JVM.

Is there a way to set a breakpoint before starting the application JVM, to be sure that the breakpoint will not be passed during the launch of the application?

+1
source share
1 answer

First of all, run the target program using the LaunchingConnector to return the target virtual machine.

 VirtualMachineManager vmm = Bootstrap.virtualMachineManager(); LaunchingConnector lc = vmm.launchingConnectors().get(0); // Equivalently, can call: // LaunchingConnector lc = vmm.defaultConnector(); Map<String, Connector.Argument> env = lc.defaultArguments(); env.get("main").setValue("p.DebugDummy"); env.get("suspend").setValue("true"); env.get("home").setValue("C:/Program Files/Java/jdk1.7.0_51"); VirtualMachine vm = lc.launch(env); 

(change the environment values ​​according to your needs, but do not forget to start the target virtual machine with suspended=true ).
With this virtual machine, you intercept ClassPrepareEvent with ClassPrepareRequest .

 ClassPrepareRequest r = reqMan.createClassPrepareRequest(); r.addClassFilter("myclasses.SampleClass"); r.enable(); 

Create a ClassPrepareEvent

  executor.execute(()-> { try { while(true) { EventQueue eventQueue = vm.eventQueue(); EventSet eventSet = eventQueue.remove(); EventIterator eventIterator = eventSet.eventIterator(); if (eventIterator.hasNext()) { Event event = eventIterator.next(); if(event instanceof ClassPrepareEvent) { ClassPrepareEvent evt = (ClassPrepareEvent) event; ClassType classType = (ClassType) evt.referenceType(); List<Location> locations = referenceType.locationsOfLine(55); Location locationForBreakpoint = locations.get(0); vm.resume(); } } } } catch (InterruptedException | AbsentInformationException | IncompatibleThreadStateException e) { e.printStackTrace(); } } 

then resume the target virtual machine with vm.resume () call to start the program. I hope this solves your problem.

+1
source

Source: https://habr.com/ru/post/1315423/


All Articles