From e93f19fb66c491f36e9579ccb83517c1e77a9a29 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 11 Aug 2019 19:44:04 +0000 Subject: [PATCH] lib: add Lua stack dumper Signed-off-by: Quentin Young --- lib/frrlua.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ lib/frrlua.h | 7 +++++++ 2 files changed, 53 insertions(+) diff --git a/lib/frrlua.c b/lib/frrlua.c index 252b667961..15fda79201 100644 --- a/lib/frrlua.c +++ b/lib/frrlua.c @@ -25,6 +25,7 @@ #include "prefix.h" #include "frrlua.h" #include "log.h" +#include "buffer.h" /* * FRR convenience functions. @@ -170,4 +171,49 @@ void frrlua_export_logging(lua_State *L) lua_setfield(L, -2, "log"); } +/* + * Debugging. + */ + +char *frrlua_stackdump(lua_State *L) +{ + int top = lua_gettop(L); + + char tmpbuf[64]; + struct buffer *buf = buffer_new(4098); + + for (int i = 1; i <= top; i++) { + int t = lua_type(L, i); + + switch (t) { + case LUA_TSTRING: /* strings */ + snprintf(tmpbuf, sizeof(tmpbuf), "\"%s\"\n", + lua_tostring(L, i)); + buffer_putstr(buf, tmpbuf); + break; + case LUA_TBOOLEAN: /* booleans */ + snprintf(tmpbuf, sizeof(tmpbuf), "%s\n", + lua_toboolean(L, i) ? "true" : "false"); + buffer_putstr(buf, tmpbuf); + break; + case LUA_TNUMBER: /* numbers */ + snprintf(tmpbuf, sizeof(tmpbuf), "%g\n", + lua_tonumber(L, i)); + buffer_putstr(buf, tmpbuf); + break; + default: /* other values */ + snprintf(tmpbuf, sizeof(tmpbuf), "%s\n", + lua_typename(L, t)); + buffer_putstr(buf, tmpbuf); + break; + } + } + + char *result = XSTRDUP(MTYPE_TMP, buffer_getstr(buf)); + + buffer_free(buf); + + return result; +} + #endif diff --git a/lib/frrlua.h b/lib/frrlua.h index f77842b445..bc017b6441 100644 --- a/lib/frrlua.h +++ b/lib/frrlua.h @@ -74,6 +74,13 @@ int frrlua_table_get_integer(lua_State *L, const char *key); */ void frrlua_export_logging(lua_State *L); +/* + * Dump Lua stack to a string. + * + * Return value must be freed with XFREE(MTYPE_TMP, ...); + */ +char *frrlua_stackdump(lua_State *L); + #ifdef __cplusplus } #endif -- 2.39.5