Skip to content

Commit

Permalink
wslua: include Lua stack trace in startup error messages
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Lekensteyn committed Jan 28, 2019
1 parent 6cab8c5 commit 6a49c72
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions epan/wslua/init_wslua.c
Expand Up @@ -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)
Expand Down Expand Up @@ -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);

Expand All @@ -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:
Expand Down

0 comments on commit 6a49c72

Please sign in to comment.