|
1 | 1 | package dotty.tools.debug
|
2 | 2 |
|
3 | 3 | import com.sun.jdi.*
|
4 |
| -import scala.jdk.CollectionConverters.* |
| 4 | +import com.sun.jdi.event.* |
| 5 | +import com.sun.jdi.request.* |
| 6 | + |
| 7 | +import java.lang.ref.Reference |
| 8 | +import java.util.concurrent.LinkedBlockingQueue |
| 9 | +import java.util.concurrent.TimeUnit |
| 10 | +import java.util.concurrent.atomic.AtomicReference |
5 | 11 | import scala.concurrent.duration.Duration
|
| 12 | +import scala.jdk.CollectionConverters.* |
| 13 | + |
| 14 | +class Debugger(vm: VirtualMachine, maxDuration: Duration, verbose: Boolean = false): |
| 15 | + // For some JDI events that we receive, we wait for client actions. |
| 16 | + // Example: On a BreakpointEvent, the client may want to inspect frames and variables, before it |
| 17 | + // decides to step in or continue. |
| 18 | + private val pendingEvents = new LinkedBlockingQueue[Event]() |
| 19 | + |
| 20 | + // Internal event subscriptions, to react to JDI events |
| 21 | + // Example: add a Breakpoint on a ClassPrepareEvent |
| 22 | + private val eventSubs = new AtomicReference(List.empty[PartialFunction[Event, Unit]]) |
| 23 | + private val eventListener = startListeningVM() |
| 24 | + |
| 25 | + def configureBreakpoint(className: String, line: Int): Unit = |
| 26 | + vm.classesByName(className).asScala.foreach(addBreakpoint(_, line)) |
| 27 | + // watch class preparation and add breakpoint when the class is prepared |
| 28 | + val request = vm.eventRequestManager.createClassPrepareRequest |
| 29 | + request.addClassFilter(className) |
| 30 | + subscribe: |
| 31 | + case e: ClassPrepareEvent if e.request == request => addBreakpoint(e.referenceType, line) |
| 32 | + request.enable() |
| 33 | + |
| 34 | + def break(): ThreadReference = receiveEvent { case e: BreakpointEvent => e.thread } |
| 35 | + |
| 36 | + def continue(thread: ThreadReference): Unit = thread.resume() |
| 37 | + |
| 38 | + def next(thread: ThreadReference): ThreadReference = |
| 39 | + stepAndWait(thread, StepRequest.STEP_LINE, StepRequest.STEP_OVER) |
| 40 | + |
| 41 | + def step(thread: ThreadReference): ThreadReference = |
| 42 | + stepAndWait(thread, StepRequest.STEP_LINE, StepRequest.STEP_INTO) |
| 43 | + |
| 44 | + /** stop listening and disconnect debugger */ |
| 45 | + def dispose(): Unit = |
| 46 | + eventListener.interrupt() |
| 47 | + vm.dispose() |
| 48 | + |
| 49 | + private def addBreakpoint(refType: ReferenceType, line: Int): Unit = |
| 50 | + for location <- refType.locationsOfLine(line).asScala do |
| 51 | + if verbose then println(s"Adding breakpoint in $location") |
| 52 | + val breakpoint = vm.eventRequestManager.createBreakpointRequest(location) |
| 53 | + // suspend only the thread which triggered the event |
| 54 | + breakpoint.setSuspendPolicy(EventRequest.SUSPEND_EVENT_THREAD) |
| 55 | + // let's enable the breakpoint and forget about it |
| 56 | + // we don't need to store it because we never remove any breakpoint |
| 57 | + breakpoint.enable() |
| 58 | + |
| 59 | + private def stepAndWait(thread: ThreadReference, size: Int, depth: Int): ThreadReference = |
| 60 | + val request = vm.eventRequestManager.createStepRequest(thread, size, depth) |
| 61 | + request.enable() |
| 62 | + thread.resume() |
| 63 | + // Because our debuggee is mono-threaded, we don't check that `e.request` is our step request. |
| 64 | + // Indeed there can be only one step request per thread at a time. |
| 65 | + val newThreadRef = receiveEvent { case e: StepEvent => e.thread } |
| 66 | + request.disable() |
| 67 | + newThreadRef |
| 68 | + |
| 69 | + private def subscribe(f: PartialFunction[Event, Unit]): Unit = |
| 70 | + eventSubs.updateAndGet(f :: _) |
| 71 | + |
| 72 | + private def startListeningVM(): Thread = |
| 73 | + val thread = Thread: () => |
| 74 | + var isAlive = true |
| 75 | + try while isAlive do |
| 76 | + val eventSet = vm.eventQueue.remove() |
| 77 | + val subscriptions = eventSubs.get |
| 78 | + var shouldResume = true |
| 79 | + for event <- eventSet.iterator.asScala.toSeq do |
| 80 | + if verbose then println(formatEvent(event)) |
| 81 | + for f <- subscriptions if f.isDefinedAt(event) do f(event) |
| 82 | + event match |
| 83 | + case e: (BreakpointEvent | StepEvent) => |
| 84 | + shouldResume = false |
| 85 | + pendingEvents.put(e) |
| 86 | + case _: VMDisconnectEvent => isAlive = false |
| 87 | + case _ => () |
| 88 | + if shouldResume then eventSet.resume() |
| 89 | + catch case _: InterruptedException => () |
| 90 | + thread.start() |
| 91 | + thread |
| 92 | + end startListeningVM |
| 93 | + |
| 94 | + private def receiveEvent[T](f: PartialFunction[Event, T]): T = |
| 95 | + // poll repeatedly until we get an event that matches the partial function or a timeout |
| 96 | + Iterator.continually(pendingEvents.poll(maxDuration.toMillis, TimeUnit.MILLISECONDS)) |
| 97 | + .collect(f) |
| 98 | + .next() |
6 | 99 |
|
7 |
| -class Debugger(vm: VirtualMachine, maxDuration: Duration): |
8 |
| - export vm.dispose |
| 100 | + private def formatEvent(event: Event): String = |
| 101 | + event match |
| 102 | + case e: ClassPrepareEvent => s"$e ${e.referenceType}" |
| 103 | + case e => e.toString |
9 | 104 |
|
10 | 105 | object Debugger:
|
11 | 106 | // The socket JDI connector
|
|
0 commit comments