Skip to content

Writing a debugger

Wang Renxin edited this page Nov 19, 2017 · 6 revisions

MY-BASIC supplies four simple debug 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 variable 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 variable identifier.
  • MBAPI int mb_debug_get_stack_trace(struct mb_interpreter_t* s, void** l, 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 h); Sets a step by step handler to an interpreter interface.

The int (* mb_debug_stepped_handler_t)(struct mb_interpreter_t*, void**, char*, int, unsigned short, unsigned short); signatured step by step handler will be called each time after executed a statement. The get/set function pair would be useful for variable watching and modification. Note identifiers must be uppercase at the host side, assuming there is a variable A in script:

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

At the host 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 script interface to implement the break point functionality, for more information about write your own script interface, see the "Customizing MY-BASIC/Write scripting API" section in the MY-BASIC Quick Reference.

It's able to use mb_debug_get_stack_trace to get stack frame trace. 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, l, frames, _countof(frames)));

MY-BASIC fills latest stack frame names to frames and fills with NULL if current stack depth is shallower than size of frames, or abandon deeper ones. It fills from inner out.

Clone this wiki locally