diff options
author | Peter Wu <peter@lekensteyn.nl> | 2019-01-27 19:03:21 +0100 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2019-01-28 11:04:23 +0000 |
commit | 6a49c72974f657371986d355e8c372ae70b0185e (patch) | |
tree | ed898832edd984d987493d4420272fe5f4c8d5e6 /epan/wslua | |
parent | 6cab8c57bd0174ffbba203d456d8b10bd3fa02a5 (diff) |
wslua: include Lua stack trace in startup error messages
Error messages without a stack trace are rather hard to debug for more
complex Lua dissectors. Be sure to append one, it will look like this:
tshark: Lua: Error during loading:
/tmp/kdnet/kdnet.lua:13: bad argument #3 to 'proto_field_constructor' (Display must be either base.NONE, base.DOT, base.DASH, base.COLON or base.SPACE)
stack traceback:
[C]: in function 'proto_field_constructor'
/tmp/kdnet/kdnet.lua:13: in function 'add_field'
/tmp/kdnet/kdnet.lua:35: in function 'add_fields'
/tmp/kdnet/kdnet.lua:242: in main chunk
It would be nice to reuse the error handler for dissector calls as well,
but I am not sure whether this works with absolute indices which are
used almost everywhere in wslua.
Change-Id: I89b2dcd360fce3865e1bf052b9fe03e888aae167
Reviewed-on: https://code.wireshark.org/review/31763
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot
Reviewed-by: Stig Bjørlykke <stig@bjorlykke.org>
Reviewed-by: Peter Wu <peter@lekensteyn.nl>
Diffstat (limited to 'epan/wslua')
-rw-r--r-- | epan/wslua/init_wslua.c | 34 |
1 files changed, 28 insertions, 6 deletions
diff --git a/epan/wslua/init_wslua.c b/epan/wslua/init_wslua.c index eded6eead4..c6ed24e6e3 100644 --- a/epan/wslua/init_wslua.c +++ b/epan/wslua/init_wslua.c @@ -402,10 +402,16 @@ static const char *getF(lua_State *LS _U_, void *ud, size_t *size) return (*size>0) ? buff : NULL; } -static int lua_main_error_handler(lua_State* LS) { - const gchar* error = lua_tostring(LS,1); - report_failure("Lua: Error during loading:\n %s",error); - return 0; +static int error_handler_with_callback(lua_State *LS) { +#if LUA_VERSION_NUM >= 502 + const char *msg = lua_tostring(LS, 1); + luaL_traceback(LS, LS, msg, 1); /* push message with traceback. */ + lua_remove(LS, -2); /* remove original msg */ +#else + /* Return error message, unmodified */ + (void)LS; +#endif + return 1; } static void wslua_add_plugin(const gchar *name, const gchar *version, const gchar *filename) @@ -523,7 +529,7 @@ static gboolean lua_load_script(const gchar* filename, const gchar* dirname, con lua_settop(L,0); - lua_pushcfunction(L,lua_main_error_handler); + lua_pushcfunction(L, error_handler_with_callback); /* The source argument should start with with '@' to indicate a file. */ lua_pushfstring(L, "@%s", filename); @@ -541,7 +547,23 @@ static gboolean lua_load_script(const gchar* filename, const gchar* dirname, con if (file_count > 0) { numargs = lua_script_push_args(file_count); } - error = lua_pcall(L,numargs,0,1); + error = lua_pcall(L, numargs, 0, 1); + if (error != LUA_OK) { + switch (error) { + case LUA_ERRRUN: + report_failure("Lua: Error during loading:\n%s", lua_tostring(L, -1)); + break; + case LUA_ERRMEM: + report_failure("Lua: Error during loading: out of memory"); + break; + case LUA_ERRERR: + report_failure("Lua: Error during loading: error while retrieving error message"); + break; + default: + report_failure("Lua: Error during loading: unknown error %d", error); + break; + } + } break; case LUA_ERRSYNTAX: |