Lua: More detailed error information

Started by
10 comments, last by jdarling 16 years, 11 months ago
G'day! I was wondering whether it is possible to get more detailed error information from Lua. I am embedding Lua in my C++ application, and when something goes wrong with Lua I print the Lua error. However, if (hypothetically) my error is that I call an object's method using a dot instead of a colon (Object.Method() instead of Object:Method()), Lua says that the method is trying to access the local 'self', but can't. I would like a more detailed error message, such as from where that function was called, and if possible, from where the function that called the erroneous function was called as well (is this a call-stack? Something tells me it is). So, essentially, what I would like to know is how to get more debug information from Lua when it is embedded in an application. Thank you!
Advertisement
After much Googleing, I think what i need to do is use debug.traceback in lua_pcall. But how do I do that?
Well, if this can't be done, could someone tell me what kind of debugging methods they used for their program (involving Lua)?
You really must have looked very hard for this information :)
http://www.lua.org/pil/8.5.html
Quote:When pcall returns its error message, it destroys part of the stack (the part that went from it to the error point). Consequently, if we want a traceback, we must build it before pcall returns. To do that, Lua provides the xpcall function. Besides the function to be called, it receives a second argument, an error handler function.

http://www.lua.org/manual/5.1/manual.html#pdf-xpcall
Thank you for that!

I am sorry, this was not clear. I was referring to the C API end of Lua, not the Lua scripts themselves (so, the C/C++ functions: lua_pcall(), etc.).

For lua_pcall (http://www.lua.org/manual/5.1/manual.html#lua_pcall), the manual says:
Quote:
int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc);

Calls a function in protected mode.
[...]
If errfunc is 0, then the error message returned on the stack is exactly the original error message. Otherwise, errfunc is the stack index of an error handler function.
[...]
Typically, the error handler function is used to add more debug information to the error message, such as a stack traceback. Such information cannot be gathered after the return of lua_pcall, since by then the stack has unwound.


So, it refers to "indexing" the debug function to use (I am assuming that this would be debug.traceback()?), how can I specify this function? i.e. how do I make debug.traceback get onto the stack and find the index for it? ("errfunc is the stack index of an error handler function")

Thank you!
Nobody knows?
lua_pcall's standard behavior when no error function is given is to call debug.traceback and leave that string on the stack. So If you are simply trying to get the stack traceback if a lua_pcall fails its as simple as this:

lua_pushcfunction(L, foo);lua_pushnumber(L, 5.3);if (lua_pcall(L, 1, 0, 0) != 0) lua_error(L);

As far as indexing is concerned it means you pass the stack position of where you pushed an error function.

/* stack the debug.traceback function */lua_getglobal(L, "debug");lua_getfield(L, -1, "traceback");lua_remove(L, -2);lua_pushcfunction(L, foo);lua_pushnumber(L, 5.3);if (lua_pcall(L, 1, 0, -3) != 0) {  /* do something */}

If the pcall fails debug.traceback (at stack pos -3) will get called. Hope that helps.
Thank you so much!

I understand what you have said (or at least I think I do), but I am unable to put it into practice.

This is the relevant code:
	luaL_loadfile(lua, name);	lua_getglobal(lua, "debug");	lua_getfield(lua, -1, "traceback");	lua_remove(lua, -2);	if (lua_pcall(lua, 0, 0, -1) != 0) {		lua_error(lua);	}

"lua" is what is normally "L" - the lua instance thingo.
"name" is the filename to execute.
What I would like it to do is to load the file, put the traceback function onto the stack and run the file. When an error occurs, it should run the traceback function. I have made some Lua code that has errors, but there is no message generated at all - not even just a simple Lua PANIC thing.

Could you please tell me what is wrong? Thank you.
The only real problem I can see is that you're screwing up your stack. The called function and arguments need to be at the top of the stack. The error function, if there is one, needs to be somewhere under them.
But what will the stack index be if I do this:
	lua_getglobal(lua, "debug");	lua_getfield(lua, -1, "traceback");	lua_remove(lua, -2);	luaL_loadfile(lua, name);	if (lua_pcall(lua, 0, 0, ???????) != 0) {		lua_error(lua);	}

This topic is closed to new replies.

Advertisement