Reputation: 2679
I am writing a big project using c++. In this project, some lua scripts will be called to implement functions. Now I want to set breakpoints in lua scripts but I don't know how to do that. I would prefer something like "pdb.set_trace()" as for python. Any idea would be appreciated. Thanks in advance.
Upvotes: 3
Views: 5535
Reputation: 1521
Unfortunately, Lua has no built-in debugger, and many of the debugging options available to you in the Lua standalone are not available in an embedded Lua scenario.
One way to deal with this would be to "script in" debugging - simply use print(whatever) and print(debug.traceback()) liberally throughout the code, possibly switched on or off by a DEBUG global (perhaps set by a DEBUG #define in the C++ code) so that the messages wouldn't be emitted in production executables.
Also, when using lua_pcall(), if a function has an error, it calls debug.traceback() and puts the resulting string on the stack. You can get it with:
lua_pushcfunction(L, c_function_name);
lua_pushnumber(L, 5.3);
if (lua_pcall(L, 1, 0, 0) != 0) lua_error(L);
A note: none of this works unless you open the debug library first, using luaopen_debug(L);
where L is your lua_State*
.
If you really do need interactive debugging, as @Colonel Thirty Two said, you should find an interactive debugging library; I'm sure one is available, but that is outside the scope of a StackOverflow question.
Upvotes: 2