Debugging Klyn Applications
Stop at a source line, follow execution, and inspect the actual values of a running Klyn application. VS Code and KlynEditor use the same native debugger through the Debug Adapter Protocol (DAP), with separate Normal and Debug compilation profiles.
Normal Execution and Debug Inspection
| Profile | Use | Compilation and execution |
|---|---|---|
| Normal | Run applications and measure performance. | Optimized code and the ordinary cache; no debugger library loading or statement-by-statement inspection hooks. |
| Debug (inspection) | Breakpoints, stepping, variables, and watches. | Preserved source locations and variable storage, reduced optimization, and a separate debugger process. |
Debug is intentionally slower than Normal: preserving inspectable variables limits optimizations. Use Normal for benchmarks. Starting an IDE, editing a file, or using completion does not start the debugger.
The Linux and Windows distributions include their native debugging backend. You do not need
to install GDB, LLDB, Python, or a separate debug adapter. Keep the installed
bin and lib directories together, and use the matching VS Code
extension shipped with Klyn.
Your First Debug Session
Create a file named debug_demo.kn containing:
def subtotal(count as Int) as Int:
total = 0
for index = 1 to count:
total += index
return total
counter = 4
result = subtotal(counter)
print(result)
Normal execution prints 10:
klyn debug_demo.kn
- Open the file in VS Code or KlynEditor and save it.
- Place the caret on
result = subtotal(counter)and pressF9to add a breakpoint. - Press
F10to start Debug. Execution pauses at the breakpoint. - Inspect
counter: its value is4. In KlynEditor, this top-level binding belongs to Globals, not Locals. Do not expect the assignment on the current line to have completed yet. - Press
F11to entersubtotal. Its parametercountand initialized local variables appear in the selected function frame. - Use
F10to execute statements without entering their calls. Inspecttotalandindexas the loop progresses. - Use
Shift+F11to finish the current function and return to its caller, orF6to continue to the next breakpoint or program exit.
Step Over executes a called function without stopping inside it, unless another breakpoint interrupts execution. Step Into follows a call; Step Out runs until the current function returns. Variables become unavailable when their frame or lexical scope is no longer active.
Execution Controls
| Action | Shortcut or control |
|---|---|
| Run the current Klyn file in Normal | Ctrl+F10 |
| Start Debug when no session is active | F10 |
| Step Over while paused | F10 |
| Toggle a source breakpoint | F9 |
| Continue | F6 |
| Step Into / Step Out | F11 / Shift+F11 |
| Pause a running application | Pause action in the Debug toolbar |
| Stop or detach in KlynEditor | Shift+F10, or the toolbar action |
| Stop or disconnect in VS Code | Stop / Disconnect action in the debug toolbar |
| Refresh | F5; Klyn does not bind it to start debugging |
These bindings apply to the Klyn editor context; custom IDE keybindings or desktop shortcuts can override them. A launched application is terminated by Stop. An attached application is normally detached and continues running instead.
Debugging in KlynEditor
Open Run > Debug / Step Over, press F10, or use Debug in the
Klyn editor's context menu. The Debug dock has a bug icon and is available with
Alt+8; Run uses a separate play icon.
Click anywhere in a row's line-number gutter to toggle its breakpoint. The red breakpoint marker is to the right of the number. A dim marker is pending until an executable location is resolved; a verified marker is bright red. Blank lines and comments may resolve to a nearby executable line. While paused, the yellow execution arrow appears to the right of the breakpoint, temporarily replacing the diagnostic badge on that row.
| Tab | What to use it for |
|---|---|
| Inspection | Select a stack frame, inspect Locals and Globals, expand native fields, and add Watches. |
| Output | Read program output and diagnostics. Use the program-input field to send a line to a launched application waiting for input. |
| Breakpoints | Review source locations, select a breakpoint, and edit its Boolean condition. Press Enter or Apply to save; leave the field empty to always stop. |
The thread selector and refresh button come first in the shared toolbar. Execution controls are compact icon buttons; hover them to read their action and shortcut. Hover an identifier or member path in the source while paused to inspect its native type, value, and available immediate fields without calling application code.
The Add watch... field is inside the Watches node. Enter an expression and press Enter or its add button. Collapsing Watches also hides this field. To remove a watch, hover its row and click the small trash action at its right edge. KlynEditor supports up to 64 watches. Breakpoint positions and conditions are saved in editor settings.
Moving breakpoints or changing conditions does not require recompiling the program. Changing its source does: stop or detach, save the changes, and launch a new Debug session. There is no implicit hot reload. Editor edits and undo/redo preserve breakpoint anchors and their conditions for the next session.
Debugging in VS Code
Install the Klyn Language Support extension from the distribution's
tools/klyn-language-support.vsix and trust the workspace before debugging.
The debugger uses the Klyn executable resolved by the extension; if necessary, set
klyn.languageServer.path to the executable from that same distribution.
Open a .kn file and use Klyn: Debug Current File from the Command
Palette, or press F10. VS Code's Run and Debug view provides breakpoints, call
stacks, threads, variables, watches, and debug hovers. To retain arguments and a working
directory, create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "klyn",
"request": "launch",
"name": "Klyn: Debug current file",
"program": "${file}",
"args": [],
"cwd": "${fileDirname}"
}
]
}
Here 0.2.0 is VS Code's launch-configuration schema version, not the Klyn release
number. Use a source file for program; the adapter compiles it in Debug itself.
Add a condition through VS Code's breakpoint controls, and use Watch or the debug console
for supported read-only expressions.
Use Klyn: Send Program Input to send a line to a launched program waiting for input. The debug console evaluates expressions; it is not the program's stdin or a Klyn REPL.
Conditions and Watches
In the example, add a breakpoint on total += index inside subtotal
and give it this condition:
index == 3
Continue with F6. On the third loop iteration, index is
3 and total is still 3, from the previous additions.
In that function frame, add this watch:
total + index
It evaluates to 6 without modifying the program. After stepping over the
addition, total becomes 6. Watches are reevaluated from the selected
paused frame; they do not retain a value from a previous stop or a different frame.
index == 3 and index > 1 and total > 0 are Boolean conditions.
index alone is not: Klyn does not treat a nonzero integer as a Boolean.
An empty condition always stops; a false condition continues automatically. Invalid syntax
is reported, and an evaluation or type error stops visibly with a diagnostic rather than
silently ignoring the breakpoint.
Inspection supports primitive literals, native variables and stored fields, arithmetic,
comparisons, bitwise operations, parentheses, and not, and, and
or. Boolean operators and comparison chains short-circuit, but all operand types
must still be valid. Expressions are limited to 4096 bytes.
This is a read-only inspection evaluator, not arbitrary Klyn execution: assignments, function calls, user-defined operators and indexers, and computed getters are not executed. Indexing requires actual native array debug metadata. Opaque String and collection payloads may expose references or native metadata rather than their complete contents. An unavailable expression produces an explicit error, not a guessed value.
Threads and Stack Frames
Pause before inspecting a running program. In KlynEditor, select a thread in the toolbar, expand its stack in Threads and frames, and select the desired frame. Refresh reloads the thread list. In VS Code, select the thread and frame in Call Stack. Variables, watches, and stepping then use that frame and thread.
The debugger uses an all-stop model: Pause and Continue apply to the process. A step targets the selected thread, but other threads may run during that step so that synchronization can complete. Individual thread freeze/resume is not exposed.
A top-level script frame is shown as <module>. Top-level bindings are Globals;
parameters and function/block variables are Locals. A variable's scope is not determined by
whether the compiler happens to store it on a native stack. Select the frame where an
expression's names are available before evaluating it.
Attaching to an Existing Debug Application
For an application that stays running, start it from a terminal with the Debug profile:
klyn -d long_running_app.kn
Klyn prints the application's PID and a local DAP endpoint line containing a private session token. The service already controls that process; connect to it instead of starting a second native debugger against the same PID.
- In VS Code, choose Klyn: Connect to Running Debugger, paste the endpoint line, and enter the PID.
- In KlynEditor, choose Run > Attach Debugger... and provide the PID and endpoint line.
- The application pauses for inspection. Continue when ready; detach to leave it running.
For an unmanaged Debug process, use Klyn: Attach Debugger to Process in VS Code, or leave the endpoint empty in KlynEditor's attach form. A standalone Linux inspection executable is one example. A Normal process is refused: attaching cannot recover variables and source locations that optimization already removed. Fast scripts should be launched from the IDE rather than attached after they have already finished.
The TCP service accepts authenticated loopback connections only. Do not publish its token,
include it in screenshots, or commit it in project settings or launch.json.
Attaching does not redirect an arbitrary existing process's stdin; keep using its original
input mechanism.
Command-Line Modes
| Command | Behavior |
|---|---|
klyn debug_demo.kn | Run the source in Normal. |
klyn -d debug_demo.kn | Run in Debug and expose its local debugger service for a later IDE connection. |
klyn -d | Start a debugger service; wait for a DAP client to launch or attach. This is not the REPL. |
klyn -d --stdio | Use editor-owned DAP input/output pipes. Do not type program input into this protocol stream. |
klyn -d --wait-for-debugger debug_demo.kn | Preselect a source file and wait for the DAP client's launch/configuration instead of running it immediately. |
klyn -d -c -o debug_demo.o debug_demo.kn | Compile a Debug inspection object without executing it. |
klyn -d -x debug_demo.kn | Export a standalone inspection executable on Linux. |
Debugging source files is available on Linux and Windows. Windows inspection executable export
with -d -x is not supported and fails explicitly; ordinary Normal
klyn -x export remains available. KlynOS does not currently provide the native
process-debugging services required by this backend.
Compilation Cache and Performance
Debug uses a branch of the existing user cache, not a new project-local cache:
<cache>/debug/v3/<version>/{app,lib}. Normal retains
<cache>/<version>/{app,lib}, with target-specific namespaces where needed.
For Klyn 0.2.4 on Linux, the default branches are ~/.cache/klyn/0.2.4 and
~/.cache/klyn/debug/v3/0.2.4.
Switching profiles does not destroy the other profile's objects. An up-to-date object is reused rather than compiled again. Temporary native symbol images needed by a live session belong to the OS temporary directory and are cleaned up when their resources retire; they are not another persistent cache.
Clean a profile only when you intentionally want to rebuild it:
klyn --clean debug_demo.kn
klyn -d --clean debug_demo.kn
The first command cleans Normal; the second cleans Debug. Avoid cleaning between ordinary sessions if startup speed matters, and never compare Debug timings with a Normal benchmark.
Troubleshooting and Current Limits
| Symptom | What to check |
|---|---|
| F10 does not start Debug | Focus a saved Klyn source file, check IDE keybindings, and try the explicit Debug command. VS Code also requires workspace trust. |
| A breakpoint stays pending | Use an executable source line, check that the launched file is the open file, and allow its native object to load. Inspect the reported breakpoint location. |
| A watch is unavailable | Pause, select the correct frame, and check scope and supported expression forms. Names from another frame are not automatically local variables. |
| A condition reports a type error | Return a Boolean, not an integer or reference. Invalid conditions are reported rather than treated as false. |
| The program waits for input | For an IDE-launched process, use Output's input field in KlynEditor or Klyn: Send Program Input in VS Code, not the expression console. |
| Source and execution no longer match | Stop or detach and start a new session after saving source edits. There is no hot reload. |
| Direct attachment is refused | Start the program in Debug. If it already has a Klyn DAP service, connect to that original service with its endpoint and token. |
| Debug support is missing | Restore the complete matching distribution, including lib/debug, and check which Klyn executable the IDE uses. |
Inspection is read-only. Editing live values, executing arbitrary application expressions, logpoints, and exception-break filters are not supported. An exception is not automatically an exception breakpoint: consult Output for its diagnostic when execution fails. These limits do not change ordinary Klyn language semantics.