|
| 1 | +package com.baeldung.jdi; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStreamReader; |
| 5 | +import java.io.OutputStreamWriter; |
| 6 | +import java.util.Map; |
| 7 | + |
| 8 | +import com.sun.jdi.AbsentInformationException; |
| 9 | +import com.sun.jdi.Bootstrap; |
| 10 | +import com.sun.jdi.ClassType; |
| 11 | +import com.sun.jdi.IncompatibleThreadStateException; |
| 12 | +import com.sun.jdi.LocalVariable; |
| 13 | +import com.sun.jdi.Location; |
| 14 | +import com.sun.jdi.StackFrame; |
| 15 | +import com.sun.jdi.VMDisconnectedException; |
| 16 | +import com.sun.jdi.Value; |
| 17 | +import com.sun.jdi.VirtualMachine; |
| 18 | +import com.sun.jdi.connect.Connector; |
| 19 | +import com.sun.jdi.connect.IllegalConnectorArgumentsException; |
| 20 | +import com.sun.jdi.connect.LaunchingConnector; |
| 21 | +import com.sun.jdi.connect.VMStartException; |
| 22 | +import com.sun.jdi.event.BreakpointEvent; |
| 23 | +import com.sun.jdi.event.ClassPrepareEvent; |
| 24 | +import com.sun.jdi.event.Event; |
| 25 | +import com.sun.jdi.event.EventSet; |
| 26 | +import com.sun.jdi.event.LocatableEvent; |
| 27 | +import com.sun.jdi.event.StepEvent; |
| 28 | +import com.sun.jdi.request.BreakpointRequest; |
| 29 | +import com.sun.jdi.request.ClassPrepareRequest; |
| 30 | +import com.sun.jdi.request.StepRequest; |
| 31 | + |
| 32 | +public class JDIExampleDebugger { |
| 33 | + |
| 34 | + private Class debugClass; |
| 35 | + private int[] breakPointLines; |
| 36 | + |
| 37 | + public Class getDebugClass() { |
| 38 | + return debugClass; |
| 39 | + } |
| 40 | + |
| 41 | + public void setDebugClass(Class debugClass) { |
| 42 | + this.debugClass = debugClass; |
| 43 | + } |
| 44 | + |
| 45 | + public int[] getBreakPointLines() { |
| 46 | + return breakPointLines; |
| 47 | + } |
| 48 | + |
| 49 | + public void setBreakPointLines(int[] breakPointLines) { |
| 50 | + this.breakPointLines = breakPointLines; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Sets the debug class as the main argument in the connector and launches the VM |
| 55 | + * @return VirtualMachine |
| 56 | + * @throws IOException |
| 57 | + * @throws IllegalConnectorArgumentsException |
| 58 | + * @throws VMStartException |
| 59 | + */ |
| 60 | + public VirtualMachine connectAndLaunchVM() throws IOException, IllegalConnectorArgumentsException, VMStartException { |
| 61 | + LaunchingConnector launchingConnector = Bootstrap.virtualMachineManager().defaultConnector(); |
| 62 | + Map<String, Connector.Argument> arguments = launchingConnector.defaultArguments(); |
| 63 | + arguments.get("main").setValue(debugClass.getName()); |
| 64 | + VirtualMachine vm = launchingConnector.launch(arguments); |
| 65 | + return vm; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates a request to prepare the debug class, add filter as the debug class and enables it |
| 70 | + * @param vm |
| 71 | + */ |
| 72 | + public void enableClassPrepareRequest(VirtualMachine vm) { |
| 73 | + ClassPrepareRequest classPrepareRequest = vm.eventRequestManager().createClassPrepareRequest(); |
| 74 | + classPrepareRequest.addClassFilter(debugClass.getName()); |
| 75 | + classPrepareRequest.enable(); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Sets the break points at the line numbers mentioned in breakPointLines array |
| 80 | + * @param vm |
| 81 | + * @param event |
| 82 | + * @throws AbsentInformationException |
| 83 | + */ |
| 84 | + public void setBreakPoints(VirtualMachine vm, ClassPrepareEvent event) throws AbsentInformationException { |
| 85 | + ClassType classType = (ClassType) event.referenceType(); |
| 86 | + for(int lineNumber: breakPointLines) { |
| 87 | + Location location = classType.locationsOfLine(lineNumber).get(0); |
| 88 | + BreakpointRequest bpReq = vm.eventRequestManager().createBreakpointRequest(location); |
| 89 | + bpReq.enable(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Displays the visible variables |
| 95 | + * @param event |
| 96 | + * @throws IncompatibleThreadStateException |
| 97 | + * @throws AbsentInformationException |
| 98 | + */ |
| 99 | + public void displayVariables(LocatableEvent event) throws IncompatibleThreadStateException, AbsentInformationException { |
| 100 | + StackFrame stackFrame = event.thread().frame(0); |
| 101 | + if(stackFrame.location().toString().contains(debugClass.getName())) { |
| 102 | + Map<LocalVariable, Value> visibleVariables = stackFrame.getValues(stackFrame.visibleVariables()); |
| 103 | + System.out.println("Variables at " +stackFrame.location().toString() + " > "); |
| 104 | + for (Map.Entry<LocalVariable, Value> entry : visibleVariables.entrySet()) { |
| 105 | + System.out.println(entry.getKey().name() + " = " + entry.getValue()); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Enables step request for a break point |
| 112 | + * @param vm |
| 113 | + * @param event |
| 114 | + */ |
| 115 | + public void enableStepRequest(VirtualMachine vm, BreakpointEvent event) { |
| 116 | + //enable step request for last break point |
| 117 | + if(event.location().toString().contains(debugClass.getName()+":"+breakPointLines[breakPointLines.length-1])) { |
| 118 | + StepRequest stepRequest = vm.eventRequestManager().createStepRequest(event.thread(), StepRequest.STEP_LINE, StepRequest.STEP_OVER); |
| 119 | + stepRequest.enable(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public static void main(String[] args) throws Exception { |
| 124 | + |
| 125 | + JDIExampleDebugger debuggerInstance = new JDIExampleDebugger(); |
| 126 | + debuggerInstance.setDebugClass(JDIExample.class); |
| 127 | + int[] breakPoints = {6, 9}; |
| 128 | + debuggerInstance.setBreakPointLines(breakPoints); |
| 129 | + VirtualMachine vm = null; |
| 130 | + |
| 131 | + try { |
| 132 | + vm = debuggerInstance.connectAndLaunchVM(); |
| 133 | + debuggerInstance.enableClassPrepareRequest(vm); |
| 134 | + |
| 135 | + EventSet eventSet = null; |
| 136 | + while ((eventSet = vm.eventQueue().remove()) != null) { |
| 137 | + for (Event event : eventSet) { |
| 138 | + if (event instanceof ClassPrepareEvent) { |
| 139 | + debuggerInstance.setBreakPoints(vm, (ClassPrepareEvent)event); |
| 140 | + } |
| 141 | + |
| 142 | + if (event instanceof BreakpointEvent) { |
| 143 | + event.request().disable(); |
| 144 | + debuggerInstance.displayVariables((BreakpointEvent) event); |
| 145 | + debuggerInstance.enableStepRequest(vm, (BreakpointEvent)event); |
| 146 | + } |
| 147 | + |
| 148 | + if (event instanceof StepEvent) { |
| 149 | + debuggerInstance.displayVariables((StepEvent) event); |
| 150 | + } |
| 151 | + vm.resume(); |
| 152 | + } |
| 153 | + } |
| 154 | + } catch (VMDisconnectedException e) { |
| 155 | + System.out.println("Virtual Machine is disconnected."); |
| 156 | + } catch (Exception e) { |
| 157 | + e.printStackTrace(); |
| 158 | + } |
| 159 | + finally { |
| 160 | + InputStreamReader reader = new InputStreamReader(vm.process().getInputStream()); |
| 161 | + OutputStreamWriter writer = new OutputStreamWriter(System.out); |
| 162 | + char[] buf = new char[512]; |
| 163 | + |
| 164 | + reader.read(buf); |
| 165 | + writer.write(buf); |
| 166 | + writer.flush(); |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments