Skip to content

Writing a debugger

Wang Renxin edited this page Jul 8, 2022 · 6 revisions

MY-BASIC offer four simple debugging API:

  • MBAPI int mb_debug_get(struct mb_interpreter_t* s, const char* n, mb_value_t* val); Retrieves the value of a variable with a specific identifier.
  • MBAPI int mb_debug_set(struct mb_interpreter_t* s, const char* n, mb_value_t val); Sets the value of a variable with a specific identifier.
  • MBAPI int mb_debug_get_stack_trace(struct mb_interpreter_t* s, char** fs, unsigned fc); Gets current stack frame trace. It requires the MB_ENABLE_STACK_TRACE macro enabled to use this function.
  • MBAPI int mb_debug_set_stepped_handler(struct mb_interpreter_t* s, mb_debug_stepped_handler_t prev, mb_debug_stepped_handler_t post); Sets a pair of step by step handlers to an interpreter instance.

The int (* mb_debug_stepped_handler_t)(struct mb_interpreter_t*, void**, const char*, int, unsigned short, unsigned short); signatured stepping handlers, prev and post, will be called each time before and after execution of a statement respectively. The get/set function pair would be useful for variable watching and modification. Note identifiers must be uppercase at the native side, assuming there is a variable A in BASIC:

a = 0
A = 1 ' The same variable as `a`

Accessing from the native side for example:

mb_value_t val;
val.type = MB_DT_REAL;
val.value.float_point = 3.14f;
mb_debug_set(bas, "A", val);
mb_debug_get(bas, "A", &val);

You can simply write a BASIC function to implement the break point functionality, for more information about writing your own BASIC functions, see the "Customizing MY-BASIC/Writing scripting API" section in the MY-BASIC Quick Reference.

It's possible to use mb_debug_get_stack_trace to get call stack. Typically pass a string array to char** fs, and the size of that array to unsigned fc for example:

char* frames[8];
mb_check(mb_debug_get_stack_trace(s, frames, _countof(frames)));

MY-BASIC fills the frame names of latest call stack to frames and fills with NULL if no more frames. It fills from inner out.

Clone this wiki locally