1 
2 /*
3  * Copyright (C) Xiaozhe Wang (chaoslawful)
4  * Copyright (C) Yichun Zhang (agentzh)
5  */
6 
7 
8 #ifndef DDEBUG
9 #define DDEBUG 0
10 #endif
11 #include "ddebug.h"
12 
13 
14 #include "ngx_http_lua_exception.h"
15 #include "ngx_http_lua_util.h"
16 
17 
18 /*  longjmp mark for restoring nginx execution after Lua VM crashing */
19 jmp_buf ngx_http_lua_exception;
20 
21 /**
22  * Override default Lua panic handler, output VM crash reason to nginx error
23  * log, and restore execution to the nearest jmp-mark.
24  *
25  * @param L Lua state pointer
26  * @retval Long jump to the nearest jmp-mark, never returns.
27  * @note nginx request pointer should be stored in Lua thread's globals table
28  * in order to make logging working.
29  * */
30 int
ngx_http_lua_atpanic(lua_State * L)31 ngx_http_lua_atpanic(lua_State *L)
32 {
33 #ifdef NGX_LUA_ABORT_AT_PANIC
34     abort();
35 #else
36     u_char                  *s = NULL;
37     size_t                   len = 0;
38 
39     if (lua_type(L, -1) == LUA_TSTRING) {
40         s = (u_char *) lua_tolstring(L, -1, &len);
41     }
42 
43     if (s == NULL) {
44         s = (u_char *) "unknown reason";
45         len = sizeof("unknown reason") - 1;
46     }
47 
48     ngx_log_stderr(0, "lua atpanic: Lua VM crashed, reason: %*s", len, s);
49     ngx_quit = 1;
50 
51     /*  restore nginx execution */
52     NGX_LUA_EXCEPTION_THROW(1);
53 
54     /* impossible to reach here */
55 #endif
56 }
57 
58 /* vi:set ft=c ts=4 sw=4 et fdm=marker: */
59