|
| 1 | +package processing.mode.android; |
| 2 | + |
| 3 | +import com.sun.jdi.*; |
| 4 | +import com.sun.jdi.event.*; |
| 5 | +import com.sun.jdi.request.ClassPrepareRequest; |
| 6 | +import com.sun.jdi.request.EventRequestManager; |
| 7 | +import com.sun.jdi.request.ModificationWatchpointRequest; |
| 8 | +import processing.mode.java.Debugger; |
| 9 | +import processing.mode.java.debug.LineBreakpoint; |
| 10 | +import processing.mode.java.debug.LineID; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | +import java.util.List; |
| 14 | + |
| 15 | +public class AndroidDebugger extends Debugger { |
| 16 | + /// editor window, acting as main view |
| 17 | + protected AndroidEditor editor; |
| 18 | + protected AndroidRunner runtime; |
| 19 | + protected AndroidMode androidMode; |
| 20 | + |
| 21 | + protected boolean isEnabled; |
| 22 | + |
| 23 | + private static final int TCP_PORT = 7777; |
| 24 | + |
| 25 | + private String pkgName = ""; |
| 26 | + private String sketchClassName = ""; |
| 27 | + |
| 28 | + public static final String FIELD_NAME = "mouseX"; |
| 29 | + |
| 30 | + public AndroidDebugger(AndroidEditor editor, AndroidMode androidMode) { |
| 31 | + super(editor); |
| 32 | + this.editor = editor; |
| 33 | + this.androidMode = androidMode; |
| 34 | + } |
| 35 | + |
| 36 | + public boolean isEnabled() { |
| 37 | + return isEnabled; |
| 38 | + } |
| 39 | + |
| 40 | + public void toggleDebug() { |
| 41 | + isEnabled = !isEnabled; |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public AndroidEditor getEditor() { |
| 46 | + return editor; |
| 47 | + } |
| 48 | + |
| 49 | + public synchronized void startDebug(AndroidRunner runner, Device device) { |
| 50 | + //stopDebug(); // stop any running sessions |
| 51 | + if (isStarted()) { |
| 52 | + return; // do nothing |
| 53 | + } |
| 54 | + |
| 55 | + runtime = runner; |
| 56 | + pkgName = runner.build.getPackageName(); |
| 57 | + sketchClassName = runner.build.getSketchClassName(); |
| 58 | + |
| 59 | + try { |
| 60 | + device.forwardPort(TCP_PORT); |
| 61 | + |
| 62 | + // connect |
| 63 | + System.out.println("\n\n\n:debugger:Attaching Debugger"); |
| 64 | + VirtualMachine vm = runner.connectVirtualMachine(TCP_PORT); |
| 65 | + |
| 66 | + // set watch field on already loaded classes |
| 67 | + List<ReferenceType> referenceTypes = vm.classesByName(pkgName + "." + sketchClassName); |
| 68 | + |
| 69 | + for (ReferenceType refType : referenceTypes) { |
| 70 | + addFieldWatch(vm, refType); |
| 71 | + |
| 72 | + // Adding breakpoint at line 27 |
| 73 | +// try { |
| 74 | +// List<Location> locations = refType.locationsOfLine(28); |
| 75 | +// if (locations.isEmpty()){ |
| 76 | +// System.out.println("no location found for line 27"); |
| 77 | +// } else { |
| 78 | +// BreakpointRequest bpr = vm.eventRequestManager().createBreakpointRequest(locations.get(0)); |
| 79 | +// bpr.enable(); |
| 80 | +// } |
| 81 | +// } catch (AbsentInformationException e) { |
| 82 | +// e.printStackTrace(); |
| 83 | +// } |
| 84 | + } |
| 85 | + // watch for loaded classes |
| 86 | + addClassWatch(vm); |
| 87 | + |
| 88 | + // resume the vm |
| 89 | + vm.resume(); |
| 90 | + |
| 91 | + // start receiving vm events |
| 92 | + VMEventReader eventThread = new VMEventReader(vm.eventQueue(), vmEventListener); |
| 93 | + eventThread.start(); |
| 94 | + } catch (IOException e) { |
| 95 | + e.printStackTrace(); |
| 96 | + } catch (InterruptedException e) { |
| 97 | + e.printStackTrace(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Watch all classes ({@value sketchClassName}) variable |
| 103 | + */ |
| 104 | + private void addClassWatch(VirtualMachine vm) { |
| 105 | + EventRequestManager erm = vm.eventRequestManager(); |
| 106 | + ClassPrepareRequest classPrepareRequest = erm.createClassPrepareRequest(); |
| 107 | + classPrepareRequest.addClassFilter(sketchClassName); |
| 108 | + classPrepareRequest.setEnabled(true); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Watch field ({@value FIELD_NAME}) |
| 113 | + */ |
| 114 | + private void addFieldWatch(VirtualMachine vm, |
| 115 | + ReferenceType refType) { |
| 116 | + EventRequestManager erm = vm.eventRequestManager(); |
| 117 | + Field field = refType.fieldByName(FIELD_NAME); |
| 118 | + ModificationWatchpointRequest modificationWatchpointRequest = erm.createModificationWatchpointRequest(field); |
| 119 | + modificationWatchpointRequest.setEnabled(true); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public VirtualMachine vm() { |
| 124 | + if (runtime != null) { |
| 125 | + return runtime.vm(); |
| 126 | + } |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Get the breakpoint on a certain line, if set. |
| 132 | + * |
| 133 | + * @param line the line to get the breakpoint from |
| 134 | + * @return the breakpoint, or null if no breakpoint is set on the specified |
| 135 | + * line. |
| 136 | + */ |
| 137 | + LineBreakpoint breakpointOnLine(LineID line) { |
| 138 | + for (LineBreakpoint bp : breakpoints) { |
| 139 | + if (bp.isOnLine(line)) { |
| 140 | + return bp; |
| 141 | + } |
| 142 | + } |
| 143 | + return null; |
| 144 | + } |
| 145 | + |
| 146 | + synchronized void toggleBreakpoint(int lineIdx) { |
| 147 | + LineID line = editor.getLineIDInCurrentTab(lineIdx); |
| 148 | + int index = line.lineIdx(); |
| 149 | + if (hasBreakpoint(line)) { |
| 150 | + removeBreakpoint(index); |
| 151 | + } else { |
| 152 | + // Make sure the line contains actual code before setting the break |
| 153 | + // https://github.com/processing/processing/issues/3765 |
| 154 | + if (editor.getLineText(index).trim().length() != 0) { |
| 155 | + setBreakpoint(index); |
| 156 | + } |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Set a breakpoint on a line in the current tab. |
| 162 | + * |
| 163 | + * @param lineIdx the line index (0-based) of the current tab to set the |
| 164 | + * breakpoint on |
| 165 | + */ |
| 166 | + synchronized void setBreakpoint(int lineIdx) { |
| 167 | + setBreakpoint(editor.getLineIDInCurrentTab(lineIdx)); |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + synchronized void setBreakpoint(LineID line) { |
| 172 | + // do nothing if we are kinda busy |
| 173 | + if (isStarted() && !isPaused()) { |
| 174 | + return; |
| 175 | + } |
| 176 | + // do nothing if there already is a breakpoint on this line |
| 177 | + if (hasBreakpoint(line)) { |
| 178 | + return; |
| 179 | + } |
| 180 | + breakpoints.add(new LineBreakpoint(line, this)); |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + /** |
| 185 | + * Remove a breakpoint from the current line (if set). |
| 186 | + */ |
| 187 | + synchronized void removeBreakpoint() { |
| 188 | + removeBreakpoint(editor.getCurrentLineID().lineIdx()); |
| 189 | + } |
| 190 | + |
| 191 | + |
| 192 | + /** |
| 193 | + * Remove a breakpoint from a line in the current tab. |
| 194 | + * |
| 195 | + * @param lineIdx the line index (0-based) in the current tab to remove the |
| 196 | + * breakpoint from |
| 197 | + */ |
| 198 | + void removeBreakpoint(int lineIdx) { |
| 199 | + // do nothing if we are kinda busy |
| 200 | + if (isBusy()) { |
| 201 | + return; |
| 202 | + } |
| 203 | + |
| 204 | + LineBreakpoint bp = breakpointOnLine(editor.getLineIDInCurrentTab(lineIdx)); |
| 205 | + if (bp != null) { |
| 206 | + bp.remove(); |
| 207 | + breakpoints.remove(bp); |
| 208 | + } |
| 209 | + } |
| 210 | +} |
0 commit comments