4

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?

Tomáš M.
  • 752
  • 7
  • 24

1 Answers1

5

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.

Community
  • 1
  • 1
lefticus
  • 3,346
  • 2
  • 24
  • 28
  • I think that adding easily accessible debugging features to ChaiScript would be immensely beneficial to this awesome scripting system. In fact, it can be argued that any programming tool without debugging ability, be it script or native code, it almost useless for large or even medium purposes. And ChaiScript deserves to not be un-debuggable :) – Marius Myburg May 08 '18 at 13:44