java-web serves the Java Debug Wire Protocol, so jdb, IDEs and any JDI client
attach to a program as they do to HotSpot. The agent is in src/jdwp.ts.
node src/node.ts -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005 -cp classes Main
jdb -attach 5005The same option works on the java-web command, and as jdwp: "transport=..." in
the options of runNode. The program prints Listening for transport dt_socket at address: 5005
as HotSpot does.
In a page, cheerpjInit({ jdwp: "transport=dt_socket,server=y,suspend=n,address=5005" })
starts the agent in each cheerpjRunMain and cheerpjRunJar run (not in library mode). The
agent listens on the VM's loopback, so a debugger reaches it from another VM on the page with
sharedLoopback, or from page code with javaWebConnect (browser.md).
The debugger can run in java-web too. The image has jdk.jdi, the Java Debug Interface, so a
JDI client in one VM attaches to the agent in another, with the JDK's own connectors or a JDI
implementation such as Eclipse JDT's. jdk.jdi is in the boot layer, so OSGi frameworks export
com.sun.jdi through their system bundle. test/jdi.test.ts runs such a session.
transport=dt_socketis the only transport.server=ylistens for a debugger.server=n(connect to one) is not supported.suspend=y(the default) holds the main thread before it loads the main class until a debugger attaches, and the VM starts suspended.suspend=nruns at once. As on HotSpot, only a debugger the VM waited for gets the VM start event.address=[host:]port:*listens on every interface, no host on loopback only, port 0 picks a port.
The VM accepts one debugger at a time. After it leaves, the VM resumes all threads, drops the debugger's breakpoints and requests, and listens again.
Breakpoints, single steps (into, over, out, by line or instruction, with the class filters
jdb sets), exceptions caught and uncaught, thread start and death, class prepare, VM start
and death, method entry and exit with the returned value, field watchpoints (reads and writes of
a field by bytecode, with the value to be written), thread suspension, stack frames and their
locals, fields, arrays, strings, calls of methods and constructors in a suspended thread
(jdb's eval), popping frames (an IDE's restart frame), and class redefinition (an IDE's hot
code replace).
Not supported, and reported as such in the capabilities: monitor events and monitor
information, forcing a return, instance counts and server=n. Method entry and exit events cover methods with bytecode, and a method left by an
exception reports no exit.
Popping frames follows HotSpot: the caller of the last popped frame stands before its invoke
again with the arguments as the popped frame left them, the locks the popped frames took are
released, and finally blocks do not run. A native frame, a frame whose caller is native or VM
code, and a thread blocked on a monitor cannot be popped (OPAQUE_FRAME); a frame with no Java
caller answers NO_MORE_FRAMES.
Class redefinition has HotSpot's default limits: only method bodies may change. A new version that adds or removes a field or a method, or changes the superclass, the interfaces, a modifier, or the nest, record or permitted subclasses attributes, is refused with HotSpot's error. New calls run the new bytecode, and compiled code for the class's methods is dropped. A frame that was running a method of the class finishes in the bytecode it started with. When that bytecode differs from the new version only in constant pool indices (EMCP), the debugger sees the frame in the current method, and breakpoints set there reach it. Otherwise the frame's method is obsolete, and the agent reports its locations with method id 0, as HotSpot's agent does.
Stepping follows HotSpot's agent: a step ends on a new line in the frame it started in, in a
called method that has line numbers (into), or in the caller when the frame returns.
A thread that steps or that a method entry or exit request covers runs in the interpreter, and
so does any method with a breakpoint or with an instruction that may read or write a watched
field. The JIT stops inlining those methods. Without -agentlib:jdwp the VM does no extra work.