]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: hlua: add a new hlua_show_current_location() function
authorWilly Tarreau <w@1wt.eu>
Sun, 19 Jun 2022 15:39:33 +0000 (17:39 +0200)
committerWilly Tarreau <w@1wt.eu>
Sun, 19 Jun 2022 15:58:32 +0000 (17:58 +0200)
This function may be used to try to show where some Lua code is currently
being executed. It tries hard to detect the initialization phase, both for
the global and the per-thread states, and for runtime states. This intends
to be used by error handlers to provide the users with indications about
what Lua code was being executed when the error triggered.

include/haproxy/hlua.h
src/hlua.c

index 21e4534affce8116da868c0e46e888b349da4640..fb535f2820f158e5d5f7d6b5f00a7da48f1be1b9 100644 (file)
@@ -54,6 +54,7 @@ int hlua_post_init();
 void hlua_applet_tcp_fct(struct appctx *ctx);
 void hlua_applet_http_fct(struct appctx *ctx);
 struct task *hlua_process_task(struct task *task, void *context, unsigned int state);
+const char *hlua_show_current_location(const char *pfx);
 
 #else /* USE_LUA */
 
@@ -67,6 +68,7 @@ struct task *hlua_process_task(struct task *task, void *context, unsigned int st
 static inline void hlua_init() { }
 static inline int hlua_post_init() { return 1; }
 static inline void hlua_ctx_destroy(struct hlua *lua) { }
+static inline const char *hlua_show_current_location(const char *pfx) { return NULL; }
 
 #endif /* USE_LUA */
 
index 0959be51cc14e166ab62125ed0b477a44c146875..dd39397de1e6d73dc27120ab8c7ecd0bd91ac529 100644 (file)
@@ -1124,6 +1124,31 @@ static inline void hlua_sethlua(struct hlua *hlua)
        *hlua_store = hlua;
 }
 
+/* Will return a non-NULL string indicating the Lua call trace if the caller
+ * currently is executing from within a Lua function. One line per entry will
+ * be emitted, and each extra line will be prefixed with <pfx>. If a current
+ * Lua function is not detected, NULL is returned.
+ */
+const char *hlua_show_current_location(const char *pfx)
+{
+       lua_State *L;
+       lua_Debug ar;
+
+       /* global or per-thread stack initializing ? */
+       if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar))
+               return hlua_traceback(L, pfx);
+
+       /* per-thread stack running ? */
+       if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar))
+               return hlua_traceback(L, pfx);
+
+       /* global stack running ? */
+       if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar))
+               return hlua_traceback(L, pfx);
+
+       return NULL;
+}
+
 /* This function is used to send logs. It try to send on screen (stderr)
  * and on the default syslog server.
  */