How do you set a breakpoint before executing

依然范特西╮ 提交于 2019-12-06 17:28:08
Luca Basso Ricci

First of all start you target program using a LaunchingConnector to get back 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 environment values according to your needs,but remember to start target VM with suspended=true).
With this VM in you hand intercept a ClassPrepareEvent using a ClassPrepareRequest.

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

Create a ClassPrepareEvent handler

 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 target VM with a call to vm.resume() to run program. I hope this solve your problem.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!