debug.h File Reference

Functions for debugging bytecode execution. More...

#include <stdio.h>
#include "vm.h"
#include "chunk.h"
#include "object.h"
Include dependency graph for debug.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define KRK_BREAKPOINT_NORMAL   0
 
#define KRK_BREAKPOINT_ONCE   1
 
#define KRK_BREAKPOINT_REPEAT   2
 
#define KRK_DEBUGGER_CONTINUE   0
 
#define KRK_DEBUGGER_ABORT   1
 
#define KRK_DEBUGGER_STEP   2
 
#define KRK_DEBUGGER_RAISE   3
 
#define KRK_DEBUGGER_QUIT   4
 

Typedefs

typedef int(* KrkDebugCallback) (KrkCallFrame *frame)
 Function pointer for a debugger hook. krk_debug_registerCallback()
 

Functions

void krk_disassembleCodeObject (FILE *f, KrkCodeObject *func, const char *name)
 Print a disassembly of 'func' to the stream 'f'. More...
 
size_t krk_disassembleInstruction (FILE *f, KrkCodeObject *func, size_t offset)
 Print a disassembly of a single opcode instruction. More...
 
int krk_debugBreakpointHandler (void)
 Called by the VM when a breakpoint is encountered. More...
 
int krk_debuggerHook (KrkCallFrame *frame)
 Called by the VM on single step. More...
 
int krk_debug_registerCallback (KrkDebugCallback hook)
 Register a debugger callback. More...
 
int krk_debug_addBreakpointFileLine (KrkString *filename, size_t line, int flags)
 Add a breakpoint to the given line of a file. More...
 
int krk_debug_addBreakpointCodeOffset (KrkCodeObject *codeObject, size_t offset, int flags)
 Add a breakpoint to the given code object. More...
 
int krk_debug_removeBreakpoint (int breakpointId)
 Remove a breakpoint from the breakpoint table. More...
 
int krk_debug_enableBreakpoint (int breakpointId)
 Enable a breakpoint. More...
 
int krk_debug_disableBreakpoint (int breakpointId)
 Disable a breakpoint. More...
 
void krk_debug_enableSingleStep (void)
 Enable single stepping in the current thread.
 
void krk_debug_disableSingleStep (void)
 Disable single stepping in the current thread.
 
void krk_debug_dumpTraceback (void)
 Safely dump a traceback to stderr. More...
 
int krk_debug_examineBreakpoint (int breakIndex, KrkCodeObject **funcOut, size_t *offsetOut, int *flagsOut, int *enabledOut)
 Retreive information on a breakpoint. More...
 
void krk_debug_dumpStack (FILE *f, KrkCallFrame *frame)
 Print the elements on the stack. More...
 
void krk_debug_init (void)
 Initialize debugger state. Call exactly once per VM.
 

Detailed Description

Functions for debugging bytecode execution.

This header provides functions for disassembly bytecode to readable instruction traces, mapping bytecode offsets to source code lines, and handling breakpoint instructions.

Several of these functions are also exported to user code in the dis module.

Note that these functions are not related to manage code exception handling, but instead inteaded to provide a low level interface to the VM's execution process and allow for the implementation of debuggers and debugger extensions.

Definition in file debug.h.

Macro Definition Documentation

◆ KRK_BREAKPOINT_NORMAL

#define KRK_BREAKPOINT_NORMAL   0

This breakpoint should fire once and then remain in the table to be re-enabled later.

Definition at line 231 of file debug.h.

◆ KRK_BREAKPOINT_ONCE

#define KRK_BREAKPOINT_ONCE   1

This breakpoint should fire once and then be removed from the breakpoint table.

Definition at line 232 of file debug.h.

◆ KRK_BREAKPOINT_REPEAT

#define KRK_BREAKPOINT_REPEAT   2

After this breakpoint has fired and execution resumes, the interpreter should re-enable it to fire again until it is removed by a call to krk_debug_removeBreakpoint()

Definition at line 233 of file debug.h.

Function Documentation

◆ krk_debug_addBreakpointCodeOffset()

int krk_debug_addBreakpointCodeOffset ( KrkCodeObject codeObject,
size_t  offset,
int  flags 
)

Add a breakpoint to the given code object.

A new breakpoint is added to the breakpoint table and the code object's bytecode is overwritten to insert the OP_BREAKPOINT instruction.

Callers must ensure that offset points to the opcode portion of an instruction, and not an operand, or the attempt to add a breakpoint can corrupt the bytecode.

Parameters
codeObjectKrkCodeObject* for the code object to break on.
offsetBytecode offset to insert the breakpoint at.
flagsAllows configuring the disposition of the breakpoint.
Returns
A breakpoint identifier handle on success, or -1 on failure.

Definition at line 469 of file debug.c.

◆ krk_debug_addBreakpointFileLine()

int krk_debug_addBreakpointFileLine ( KrkString filename,
size_t  line,
int  flags 
)

Add a breakpoint to the given line of a file.

The interpreter will scan all code objects and attach a breakpoint instruction to the first such object that has a match filename and contains an instruction with a matching line mapping. Breakpoints can only be added to code which has already been compiled.

Parameters
filenameKrkString * representation of a source code filename to look for.
lineThe line to set the breakpoint at.
flagsAllows configuring the disposition of the breakpoint.
Returns
A breakpoint identifier handle on success, or -1 on failure.

Definition at line 495 of file debug.c.

◆ krk_debug_disableBreakpoint()

int krk_debug_disableBreakpoint ( int  breakpointId)

Disable a breakpoint.

Restores the bytecode instructions for the given breakpoint if it is currently enabled.

No error is returned if the breakpoint was already disabled.

Parameters
breakpointIdThe breakpoint to disable.
Returns
0 on success, 1 if the breakpoint identifier is invalid.

Definition at line 542 of file debug.c.

◆ krk_debug_dumpStack()

void krk_debug_dumpStack ( FILE *  file,
KrkCallFrame frame 
)

Print the elements on the stack.

Prints the elements on the stack for the current thread to file, highlighting frame as the activate call point and indicating where its arguments start.

When tracing is enabled, we will present the elements on the stack with a safe printer; the format of values printed by krk_printValueSafe will look different from those printed by printValue, but they guarantee that the VM will never be called to produce a string, which would result in a nasty infinite recursion if we did it while trying to trace the VM!

Definition at line 125 of file debug.c.

◆ krk_debug_dumpTraceback()

void krk_debug_dumpTraceback ( void  )

Safely dump a traceback to stderr.

Wraps krk_dumpTraceback() so it can be safely called from a debugger.

Safely dump a traceback to stderr.

Since traceback printing may involve calling into user code, we clear the debugging bits. Then we make a new exception to attach a traceback to.

Definition at line 577 of file debug.c.

◆ krk_debug_enableBreakpoint()

int krk_debug_enableBreakpoint ( int  breakpointId)

Enable a breakpoint.

Writes the OP_BREAKPOINT instruction into the function bytecode chunk for the given breakpoint.

Parameters
breakpointIdThe breakpoint to enable.
Returns
0 on success, 1 if the breakpoint identifier is invalid.

Definition at line 535 of file debug.c.

◆ krk_debug_examineBreakpoint()

int krk_debug_examineBreakpoint ( int  breakIndex,
KrkCodeObject **  funcOut,
size_t *  offsetOut,
int *  flagsOut,
int *  enabledOut 
)

Retreive information on a breakpoint.

Can be called by debuggers to examine the details of a breakpoint by its handle. Information is returned through the pointers provided as parameters.

Parameters
breakIndexBreakpoint handle to examine.
funcOut(Out) The code object this breakpoint is in.
offsetOut(Out) The bytecode offset within the code object where the breakpoint is located.
flagsOut(Out) The configuration flags for the breakpoint.
enabledOut(Out) Whether the breakpoint is enabled or not.
Returns
0 on success, -1 on out of range, -2 if the selected slot was removed.

Definition at line 649 of file debug.c.

◆ krk_debug_registerCallback()

int krk_debug_registerCallback ( KrkDebugCallback  hook)

Register a debugger callback.

The registered function hook will be called when an OP_BREAKPOINT instruction is encountered. The debugger is provided a pointer to the active frame and can use functions from the krk_debug_* suite to examine the thread state, execute more code, and resume execution.

The debugger hook will be called from the thread that encountered the breakpoint, and should return one of the KRK_DEBUGGER_ status values to inform the VM of what action to take.

Parameters
hookThe hook function to attach.
Returns
0 if the hook was registered; 1 if a hook was already registered, in which case the new hook has not been registered.

Definition at line 643 of file debug.c.

◆ krk_debug_removeBreakpoint()

int krk_debug_removeBreakpoint ( int  breakpointId)

Remove a breakpoint from the breakpoint table.

Removes the breakpoint breakpointId from the breakpoint table, restoring the bytecode instruction for it if it was enabled.

Parameters
breakpointIdThe breakpoint to remove.
Returns
0 on success, 1 if the breakpoint identifier is invalid.

Definition at line 553 of file debug.c.

◆ krk_debugBreakpointHandler()

int krk_debugBreakpointHandler ( void  )

Called by the VM when a breakpoint is encountered.

Internal method, should not generally be called.

Definition at line 663 of file debug.c.

◆ krk_debuggerHook()

int krk_debuggerHook ( KrkCallFrame frame)

Called by the VM on single step.

Handles calling the registered debugger hook, if one has been set, and performing the requested response action. Also takes care of re-enabling REPEAT breakpoints.

Internal method, should not generally be called.

Definition at line 597 of file debug.c.

◆ krk_disassembleCodeObject()

void krk_disassembleCodeObject ( FILE *  f,
KrkCodeObject func,
const char *  name 
)

Print a disassembly of 'func' to the stream 'f'.

Generates and prints a bytecode disassembly of the code object 'func', writing it to the requested stream.

Parameters
fStream to write to.
funcCode object to disassemble.
nameFunction name to display in disassembly output.

Definition at line 163 of file debug.c.

◆ krk_disassembleInstruction()

size_t krk_disassembleInstruction ( FILE *  f,
KrkCodeObject func,
size_t  offset 
)

Print a disassembly of a single opcode instruction.

Generates and prints a bytecode disassembly for one instruction from the code object 'func' at byte offset 'offset', printing the result to the requested stream and returning the size of the instruction.

Parameters
fStream to write to.
funcCode object to disassemble.
offsetByte offset of the instruction to disassemble.
Returns
The size of the instruction in bytes.

Definition at line 415 of file debug.c.