Repository navigation
Debug C# and Python cells with breakpoints and stepping - #137
Open
TorreyBetts wants to merge 2 commits into
Open
TorreyBetts wants to merge 2 commits into
TorreyBetts wants to merge 2 commits into
Conversation
A cell could only be inspected after it finished. A kernel can now stop a cell at a line, report where it is and what is in scope, and carry on one step at a time. This is the engine and the C# kernel; the front ends and Python follow. IDebuggableKernel is an optional interface beside ILanguageKernel, so no existing kernel changes. While debugging is on, a cell the user runs goes to DebugAsync in place of ExecuteAsync and is given an IDebugSession, which answers which lines carry a breakpoint and is where the kernel reports a stop. StopAsync returns the user's answer: continue, step over, step into or step out. Cancelling the cell ends a stop. A stop is a DebugStop: a reason and the chain of frames, each with a cell, a line and the variables in scope as text. How a kernel pauses is its own business. Lines in the contract count from 1 in the code the kernel was given. The engine keeps breakpoints in the cell's own numbering, the one the editor shows, and translates in both directions, so a magic command above the code does not move a breakpoint and no kernel has to know. NotebookDebugger holds one notebook's state (on or off, breakpoints, the current stop) on Scaffold. Breakpoints belong to the session and are not written into the notebook. Only cells the user starts are debugged: code run on the notebook's behalf is never stopped, because nothing on screen would say what was waiting. The C# kernel pauses by rewriting. Before a cell is compiled, a check is added ahead of each statement, on that statement's own line so line numbers stay the cell's own, and a statement that is the whole body of a branch or loop gets braces to hold its check. The check asks whether to stop and, when it should, hands over the variables that are certain to hold a value there. Nothing attaches to the process, so the host keeps answering while a cell is stopped, which a managed debugger stopping every thread would not allow. Measured in a prototype, a check costs under two nanoseconds when it does not stop. A function can be stepped into when the cell that defined it ran with debugging on, since that is when its checks were compiled in. A lambda or member written as a single expression has no statement to stop at, so a step passes over it. A value that cannot be boxed is left out. A sequence that has not run yet is named rather than counted, because counting it would run it. If a cell stops compiling once its checks are in, it is tried without variables and then run as written, with a line saying so. With debugging off nothing is rewritten and cells run exactly as before. FakeDebugSession in Verso.Testing scripts a session for testing a kernel's DebugAsync. Signed-off-by: Torrey Betts <torrey.betts@gmail.com>
The engine could pause a C# cell at a line, but nothing let a user ask it to. Both front ends now have a Debug panel that turns debugging on, a margin beside the line numbers for breakpoints, and controls for a stopped cell, and the Python kernel can pause itself as the C# kernel does. Asked for by malte-beck in #136. The Debug panel is offered whenever a registered language can be debugged, and its switch turns the mode on for the notebook. The switch lives in the panel rather than on the toolbar because the mode needs a sentence of explanation the first time and a set of controls every time after; the panel's toolbar toggle carries a dot while the mode is on, amber while a cell is stopped. With the mode on, cells in a debuggable language gain a margin, where a click or F9 sets a breakpoint. The mark is anchored to the line's code, so text typed above it carries it down, and the new line is reported back. A run stops before the line, the panel opens and the cell is brought into view. The panel shows where the cell is, the call stack and the variables in scope, with Continue, Step over, Step into, Step out and Stop. Breakpoints can be changed while a cell is stopped and are listed in the panel. They belong to the session, are not written into the notebook file, and survive a kernel restart. Restarting the kernel while a cell is stopped ends that cell first. With the mode off nothing is different. The Python host stops itself rather than being attached to. On Python 3.12 and newer it is told about lines by sys.monitoring, where a line not worth stopping at is switched off after its first report and costs nothing from then on. Python 3.8 to 3.11 use sys.settrace, which reports every line of cell code for as long as a debugged cell runs, so a tight loop in a cell is noticeably slower there. Library code is never reported under either, and only the cell's own thread stops. Every run names its cell, debugged or not, so a function can be stepped into from another cell without its defining cell having been run again. A breakpoint on a line that begins no statement stops at the next one that does, and only the latest run of a cell carries that cell's breakpoints. A loop written on one line stops on every pass under both. Every call in the stack has its variables; the cell's own namespace is listed without its modules, functions and classes. Values are reduced to short text, a very large collection from its first few items so that a step does not pay for a whole repr. An interrupt cancels a stopped cell, including one that arrives while the stop is still being described. A stop that the interpreter dies under is given up, so it cannot go on holding the notebook's one stop. INotebookService gains the debugging state as one snapshot, an event for when it changes, and three requests: turn the mode on or off, set a cell's breakpoints, and answer a stop. All have defaults, so an existing implementation keeps compiling. In verso serve the service reads the engine's debugger directly. In VS Code the host process answers debug/setEnabled, debug/setBreakpoints and debug/resume, and sends debug/stateChanged carrying the whole state each time, in the order the state was read. A stopped cell is a running cell and the host handles requests one at a time behind the run, so those three are answered on the reader thread, beside cancel and the answers to prompts; HostSession.IsAnsweredWhileRunning names them all. The client takes its state from the notifications only, since a request's own answer can be older than a notification written just before it. A restart in VS Code replaces the host process, so the client puts the mode and the breakpoints back afterwards, and the extension cancels the running cell before it asks the old host for its snapshot. A host whose input closes lets go of a stopped cell, so it cannot be left behind as a process nobody owns. Two limits worth knowing. C# reports variables for the call the cell is stopped in, so an outer entry in the call stack shows where it is waiting but not what it holds. And in VS Code anything that waits its turn behind a running cell, saving the notebook included, waits behind a stopped one too. The guide is docs/guides/debugging-cells.md, the extension interface reference covers IDebuggableKernel, and the architecture notes cover how the state travels. The new strings are translated into German, Spanish, Japanese and Simplified Chinese. Signed-off-by: Torrey Betts <torrey.betts@gmail.com>
SummarySummary
CoverageVerso - 83.3%
Verso.Abstractions - 84.7%
Verso.Ado - 81.4%
Verso.Blazor - 31.2%
Verso.Blazor.Shared - 65.5%
Verso.Cli - 41.4%
Verso.DataFrame - 82.3%
Verso.FSharp - 83.5%
Verso.Host - 60.7%
Verso.Http - 76.2%
Verso.JavaScript - 59.8%
Verso.PowerShell - 64.7%
Verso.Python - 78.5%
Verso.Sample.Diagram - 80.1%
Verso.Sample.Dice - 82.2%
Verso.Sample.Slides - 58.8%
Verso.Sample.Sparkline - 67.3%
Verso.Testing - 80.4%
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds breakpoints and step-through debugging for C# and Python cells, in both
verso serveand VS Code. It was asked for by malte-beck in #136. A cell could only be looked at after it finished; with debugging turned on, a run stops before a marked line, shows where it is and what is in scope, and carries on a step at a time. With debugging off nothing is different.What a user sees
Public interface
IDebuggableKernel, an optional interface besideILanguageKernel, withDebugAsync(code, context, IDebugSession).IDebugSessionanswers which lines carry a breakpoint and is where the kernel reports a stop;StopAsyncreturns the user's answer as aDebugResumeAction. A stop is aDebugStopofDebugFrames, each with itsDebugVariables as text. Everything is an addition, so no existing kernel or extension changes.FakeDebugSessionscripts a session for testing a kernel'sDebugAsync.DebugState,OnDebugStateChanged,SetDebuggingAsync,SetBreakpointsAsyncandResumeDebugAsync, all with defaults, so an existing implementation keeps compiling.debug/setEnabled,debug/setBreakpointsanddebug/resume, and adebug/stateChangednotification that carries the whole state each time.How it works
Kernels pause themselves; nothing attaches to a process. A managed debugger stops every thread, and the host has to keep answering while a cell is stopped, so that was never an option.
sys.monitoringon 3.12 and newer, where a line not worth stopping at is switched off after its first report. Python 3.8 to 3.11 usesys.settrace, so a tight loop in a debugged cell is noticeably slower there. Library code is never reported under either.Limits worth knowing
The guide is
docs/guides/debugging-cells.md. The new strings are translated into German, Spanish, Japanese and Simplified Chinese.Verified locally:
python build/i18n/check.py: 1553 strings, 5 languages, no problemsverso serve: C# and Python cells stopped at breakpoints, stepped over, into and out, and stoppedNot verified locally: Python debugging by hand in VS Code (the host and the panel are shared with what was checked, and the Python kernel is covered by its own tests), Python 3.8 and 3.13, and anything on Windows or Linux. CI runs the Python versions and the other platforms.