Search code examples
c++debuggingchaiscript

Is it possible to add breakpoints to a ChaiScript execution?


Does ChaiScript support debugger-like behavior? For instance, can I set breakpoints at which should the execution pause, and allow me to inspect the stack before resuming? If so, how?


Solution

  • It is not possible to break into ChaiScript currently.

    You have two options. You could cause an error to occur (say eval('**');) which would cause an eval error exception and could generate a stack error to show you were you are.

    See here: https://github.com/ChaiScript/ChaiScript/blob/develop/src/main.cpp#L344 for how you might display the stack and call information for what went wrong.

    Another option would be to cause the debugger to break inside your code. It might go something like: (see: Is there a portable equivalent to DebugBreak()/__debugbreak?)

    Function definition

    void debugbreak()
    {
    #ifdef _MSC_VER
      __debugbreak()
    #else
      raise(SIGTRAP);
    #endif
    }
    

    Adding it to ChaiScript

    chai.add(fun(&debugbreak), "debugbreak");
    

    Triggering it

    //inside chaicript code
    for (var i = 0; i < 1000; ++i)
    {
      if (i == 980) {
        // should cause your C++ debugger to break
        debugbreak();
      }
    }
    

    The problem at this point would be actually understanding the C++ stack that you see. It will take some getting used to, but the AST node names should be fairly descriptive.