From c603cba2c23c7a7ef2afba6dabed3c049827c905 Mon Sep 17 00:00:00 2001 From: stbuehler Date: Mon, 26 Oct 2009 15:12:18 +0000 Subject: mod_magnet: add traceback for printing lua errors git-svn-id: svn://svn.lighttpd.net/lighttpd/trunk@2682 152afb58-edef-0310-8abb-c4023f1b3aa9 --- NEWS | 1 + src/mod_magnet.c | 32 +++++++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 06d25926..afc46534 100644 --- a/NEWS +++ b/NEWS @@ -152,6 +152,7 @@ NEWS * Fix mod_cgi hang on "crash-before-header-sent" bug * Set content-length in mod_compress (fixes #2089, thx liming) * Mark recv-queue closed if backend connection got closed in mod_proxy_core (fixes #2090, thx liming) + * mod_magnet: add traceback for printing lua errors - 1.5.0-r19.. - * -F option added for spawn-fcgi diff --git a/src/mod_magnet.c b/src/mod_magnet.c index 48b33f00..d1260396 100644 --- a/src/mod_magnet.c +++ b/src/mod_magnet.c @@ -695,9 +695,36 @@ static int magnet_attach_content(server *srv, connection *con, plugin_data *p, l return 0; } +static int traceback (lua_State *L) { + if (!lua_isstring(L, 1)) /* 'message' not a string? */ + return 1; /* keep it intact */ + lua_getfield(L, LUA_GLOBALSINDEX, "debug"); + if (!lua_istable(L, -1)) { + lua_pop(L, 1); + return 1; + } + lua_getfield(L, -1, "traceback"); + if (!lua_isfunction(L, -1)) { + lua_pop(L, 2); + return 1; + } + lua_pushvalue(L, 1); /* pass error message */ + lua_pushinteger(L, 2); /* skip this function and traceback */ + lua_call(L, 2, 1); /* call debug.traceback */ + return 1; +} + +static int push_traceback(lua_State *L, int narg) { + int base = lua_gettop(L) - narg; /* function index */ + lua_pushcfunction(L, traceback); + lua_insert(L, base); + return base; +} + static handler_t magnet_attract(server *srv, connection *con, plugin_data *p, buffer *name) { lua_State *L; int lua_return_value = -1; + int errfunc; /* get the script-context */ @@ -812,7 +839,9 @@ static handler_t magnet_attract(server *srv, connection *con, plugin_data *p, bu lua_setfenv(L, -2); /* on the stack should be a modified env (sp -= 1) */ - if (lua_pcall(L, 0, 1, 0)) { + errfunc = push_traceback(L, 0); + if (lua_pcall(L, 0, 1, errfunc)) { + lua_remove(L, errfunc); log_error_write(srv, __FILE__, __LINE__, "ss", "lua_pcall():", @@ -825,6 +854,7 @@ static handler_t magnet_attract(server *srv, connection *con, plugin_data *p, bu return HANDLER_FINISHED; } + lua_remove(L, errfunc); /* we should have the function-copy and the return value on the stack */ assert(lua_gettop(L) == 2); -- cgit v1.2.1