【JDI】at com.sun.tools.jdi.ConcreteMethodImpl.getVariables1(ConcreteMethodImpl.java:495)

梦想的初衷 提交于 2019-12-27 04:29:18


import com.sun.jdi.*;
import com.sun.jdi.connect.Connector;
import com.sun.jdi.connect.LaunchingConnector;
import com.sun.jdi.event.*;
import com.sun.jdi.request.BreakpointRequest;
import com.sun.jdi.request.ClassPrepareRequest;
import com.sun.jdi.request.EventRequest;
import com.sun.jdi.request.EventRequestManager;

import java.util.List;
import java.util.Map;

public class SimpleDebugger {
    static VirtualMachine vm;
    static Process process;
    static EventRequestManager eventRequestManager;
    static EventQueue eventQueue;
    static EventSet eventSet;
    static boolean vmExit = false;
    static Integer lineNo = 6;
    public static void main(String[] args) throws Exception {
        LaunchingConnector launchingConnector
                = Bootstrap.virtualMachineManager().defaultConnector();

        // Get arguments of the launching connector
        Map<String, Connector.Argument> defaultArguments
                = launchingConnector.defaultArguments();
        Connector.Argument mainArg = defaultArguments.get("main");
        Connector.Argument suspendArg = defaultArguments.get("suspend");
        // Set class of main method
        mainArg.setValue("HelloWorld");
        suspendArg.setValue("true");
        defaultArguments.put("main",mainArg);
        vm = launchingConnector.launch(defaultArguments);

        process = vm.process();

        // Register ClassPrepareRequest
        eventRequestManager = vm.eventRequestManager();
        ClassPrepareRequest classPrepareRequest
                = eventRequestManager.createClassPrepareRequest();
        classPrepareRequest.addClassFilter("HelloWorld");
        classPrepareRequest.addCountFilter(1);
        classPrepareRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
        classPrepareRequest.enable();

        // Enter event loop
        eventLoop();

        process.destroy();
    }

    private static void eventLoop() throws Exception {
        eventQueue = vm.eventQueue();
        while (true) {
            if (vmExit == true) {
                break;
            }
            eventSet = eventQueue.remove();
            EventIterator eventIterator = eventSet.eventIterator();
            while (eventIterator.hasNext()) {
                Event event = (Event) eventIterator.next();
                System.out.println("zw---"+((event!=null && event.request() != null)?event.request().getClass().getName():""));
                execute(event);
            }
        }
    }

    private static void execute(Event event) throws Exception {
        if (event instanceof VMStartEvent) {
            System.out.println("VM started");
            // 特别地,随着一次事件集的发送,目标虚拟机上
            // 可能会有一部分的线程因此而被挂起。如果一直
            // 不恢复这些线程,有可能会导致目标虚拟机挂机。
            // 因此,在处理好一个事件集中的事件后,建议
            // 调用事件集的 resume() 方法,恢复所有可能被挂起的线程。
            eventSet.resume();
        } else if (event instanceof ClassPrepareEvent) {
            ClassPrepareEvent classPrepareEvent = (ClassPrepareEvent) event;
            String mainClassName = classPrepareEvent.referenceType().name();
            if (mainClassName.contains("HelloWorld")) {
                System.out.println("Class " + mainClassName
                        + " is already prepared");
            }
            if (true) {
                // Get location
                ReferenceType referenceType = classPrepareEvent.referenceType();
                for(int i = 0; i<10;i++) {
                    List locations = referenceType.locationsOfLine(lineNo);
                    if(locations!=null && locations.size()>0) {
                        Location location = (Location) locations.get(0);

                        // Create BreakpointEvent
                        BreakpointRequest breakpointRequest = eventRequestManager
                                .createBreakpointRequest(location);
                        breakpointRequest.setSuspendPolicy(EventRequest.SUSPEND_ALL);
                        breakpointRequest.enable();
                    }
                }
            }
            eventSet.resume();
        } else if (event instanceof BreakpointEvent) {
            System.out.println("Reach line 10 of com.sharedaka.venus.business.aop.JDI.HelloWorld");
            BreakpointEvent breakpointEvent = (BreakpointEvent) event;
            ThreadReference threadReference = breakpointEvent.thread();
            StackFrame stackFrame = threadReference.frame(0);
            if(stackFrame == null){
                System.out.println("stackFrame == null ");
            }
            Location location = stackFrame.location();
            System.out.println("sourcePath = "+location.sourcePath());
            System.out.println("lineNumber = "+location.lineNumber());
            System.out.println("sourceName = "+location.sourceName());
            List<LocalVariable> localVariables = stackFrame.visibleVariables();
            System.out.println("localVariables size = "+localVariables.size());
            for (LocalVariable localVariable : localVariables) {
                System.out.println("localVariable.name() = " + localVariable.name());
                System.out.println("localVariable.signature() = " + localVariable.signature());
                System.out.println("localVariable.genericSignature() = " + localVariable.genericSignature());
                System.out.println("localVariable.typeName() = " + localVariable.typeName());
            }
            LocalVariable localVariable = stackFrame
                    .visibleVariableByName("str");
            Value value = stackFrame.getValue(localVariable);
            String str = ((StringReference) value).value();
            System.out.println("The local variable str at line 10 is " + str
                    + " of " + value.type().name());
            eventSet.resume();
        } else if (event instanceof VMDisconnectEvent) {
            vmExit = true;
        } else {
            eventSet.resume();
        }
    }
}

public class HelloWorld {
      public static void main(String[] args) {
           System.out.println("----->>>>>>");
           String str = "Hello world1!";
           str = "Hello world2!";
           str = "Hello world3!";
           str = "Hello world4!";
           str = "Hello world5!";
           str = "Hello world6!";
           str = "Hello world7!";
           str = "Hello world8!";

      }
      static{
              System.out.println("除暴安良");
      }

}

 

 

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