x = foo();
invoke_debugger(); // An interactive console appears here.
y = bar(x) x = foo();
invoke_debugger(); // An interactive console appears here.
y = bar(x)Well, the trouble is knowing if you need to spawn a debugger. On Windows, there's a nice API for checking if you're being debugged (IsDebuggerPresent). On Linux, you have to read /proc/self/status to see if there is somebody running ptrace on you. On other Unixes, well, the only trick is trying to spawn another process to ptrace you and hope it works.
It is possible, just tricky.
I'd expect something like this: https://github.com/krixano/Lydrige to have it easy in this regard - it's a statically-typed but interpreted language.
Mostly, if I want a break somewhere I do this:
*(int*)0 = 1;
Which will result in a crash, I attach my debugger, run the code and if it reaches that line it will break and I can inspect everything I want.It's easier than adding a __asm(int3) because it's cross platform. On windows for example it's not possible to perform inline assembly in x64 builds, and compiler intrinsics are not portable.
If you have JIT debug enabled, you will be prompted to attach your debugger to the process when hitting such a breakpoint.