A debugger in the editor
ztcl debug --dap serves a debugger on stdin and stdout with the Debug Adapter Protocol, the protocol of the debuggers of VS Code and other editors. The runner options after --dap set the capabilities and the limits of the run. The program is the script after the options, or the argument program of the request launch.
ztcl debug --dap --allow clock job.tcl
The debugger has these parts:
| Part | Requests |
|---|---|
| Breakpoints on lines of the program file, with an optional condition (a Tcl expression in the frame of the line). | setBreakpoints |
| Breakpoints on procs: the run stops at the entry of the proc. | setFunctionBreakpoints |
Steps: step in, step over, and step out. stopOnEntry in launch stops at the first command. |
next, stepIn, stepOut, continue |
| The stack, with the line of each frame. | stackTrace |
| The locals of each frame, and the globals. | scopes, variables |
| Tcl code in a frame: a watch, a hover, or a command of the console. | evaluate |
Error breakpoints: with the exception filter error, the run stops at the start of each error, also an error that catch or try catches. The stop has the message and the command. The error goes on after continue. With the filter uncaught, the run stops only at an error with no open body of catch around it, and no open body of a try with a trap or an on error handler. A trap with a pattern that does not match counts as a catch. A body that a coroutine leaves with yield stays open. |
setExceptionBreakpoints |
Pause: the thread stops at its next line. A reader thread of the debugger reads each request at once, so setBreakpoints works in a run too. A long native command, such as after 5000 or vwait, ends first. With no threadId, each thread stops. |
pause |
Threads: each thread of thread::create stops on its own, also an isolated thread. The other threads run on. |
threads, the event thread |
Hot reload: the debugger reads the program file again and runs each proc command of its top level. A frame that runs keeps the old body until it returns. |
ztclReload (a custom request, with an optional path) |
A stop happens at each line of a proc, at each command of the top level of the program file, and at each line of a body of the top level, such as the body of a while or a foreach. Such a body runs in the bytecode interpreter while the debugger runs.
The threads of thread::create have the line hook too. The DAP thread id is the number of the thread id of thread::id, and the main thread is 1. The script of a thread that is a part of the program file, such as a braced body of thread::create, has its lines in the file, so a breakpoint in it works. A thread at the level process or vm runs a debugger of its own in the child, with the requests, the answers, and the events in DEBUG frames of the isolation protocol (docs/dev/isolation.md). A child with no module ztcl::probe, such as ztcl.wasm at the level wasm, drops the frames and runs with no stops. A stop stops only its thread, so the event stopped has allThreadsStopped false when the program has more than one thread. The output of puts goes to the event output, because stdout has the protocol. An editor gets the debugger with a launch entry that starts ztcl debug --dap. In VS Code, an extension of the type ztcl with this entry in package.json is enough:
{"debuggers": [{"type": "ztcl", "label": "ztcl", "program": "ztcl", "args": ["debug", "--dap"]}]}
From Zig
src/debug.zig has the functions of ztcl debug for an embedder: debug.run(it, text, options) gives a Report, and debug.writeText and debug.writeJson write it. A report owns its strings, so it stays valid after reset. Call report.deinit() to free it.