From e93f19fb66c491f36e9579ccb83517c1e77a9a29 Mon Sep 17 00:00:00 2001 From: Quentin Young Date: Sun, 11 Aug 2019 19:44:04 +0000 Subject: lib: add Lua stack dumper Signed-off-by: Quentin Young --- lib/frrlua.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'lib/frrlua.c') 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 -- cgit v1.2.3