Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua: get stack trace without global methods

Tags:

c#

lua

In a game that uses Lua for extensibility, when an error is thrown in a script, I'd like to show the user an error log, i.e. an error message and a stack trace, somewhat like this:

if (lua_pcall(L, nargs, nresults, 0) != 0) log.printline(lua_tostring(L, -1));

This only prints the error message, though, not the call stack.

I am aware I could use debug.traceback, i.e. grabbing the function from the debug table through the API and then calling it, but I don't want to load the debug table at all because of security concerns, i.e. allowing a malicious user to write scripts that screw with the system and other global tables. (For the same reason I don't load io and instead expose my own functions.)

I'm not using a bridge of any sort, I use the Lua API directly, through P/Invoke.

How would I go about printing a stack trace following a failed lua_pcall call, without having to expose the debug table to the end user?

like image 940
BinarySpark Avatar asked Sep 24 '26 20:09

BinarySpark


1 Answers

Call luaL_traceback:

void luaL_traceback (lua_State *L, lua_State *L1, const char *msg, int level);

Creates and pushes a traceback of the stack L1. If msg is not NULL it is appended at the beginning of the traceback. The level parameter tells at which level to start the traceback.

You probably want to use L1=L. See the source of lua.c for an example of use.

like image 126
lhf Avatar answered Sep 27 '26 09:09

lhf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!