From 6a49c72974f657371986d355e8c372ae70b0185e Mon Sep 17 00:00:00 2001 From: Peter Wu Date: Sun, 27 Jan 2019 19:03:21 +0100 Subject: wslua: include Lua stack trace in startup error messages MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Tested-by: Petri Dish Buildbot Reviewed-by: Stig Bjørlykke Reviewed-by: Peter Wu --- epan/wslua/init_wslua.c | 34 ++++++++++++++++++++++++++++------ 1 file 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: -- cgit v1.2.3