-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlcoredump.c
More file actions
187 lines (168 loc) · 4.2 KB
/
lcoredump.c
File metadata and controls
187 lines (168 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <lua.h>
#include <lauxlib.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#if LUA_VERSION_NUM < 502
# ifndef luaL_newlib
# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
# endif
#endif
#if (!(defined(WIN32) || defined(_WIN32)))
# define ENABLE_C_TRACE
#endif
#define pushfstring(buf, e, fmt, ...) do{if(sz-e > 0) e+=snprintf(buf+e, sz-e, fmt, ##__VA_ARGS__);}while(0)
#define LEVELS1 12
#define LEVELS2 10
#define MAX_BUF_SZ (1024 * 1024)
#define MAX_FRAME_SZ 128
/*for dump_c_traceback, *nix only */
#ifdef ENABLE_C_TRACE
#include <execinfo.h>
#include <unistd.h>
/* *nix only */
void dump_c_traceback(int fd)
{
void *frames[MAX_FRAME_SZ];
size_t frame_size;
frame_size = backtrace(frames, sizeof(frames) / sizeof(frames[0]));
/*you can also use backtrace_symbols here */
backtrace_symbols_fd(frames, frame_size, fd);
}
#endif /* endif for defined windows */
/**
* signal(SIGSEGV, crash_signal_handler);
*/
lua_State *GlobalL = NULL;
static int is_full_stack = 0;
static size_t dump_lua_traceback(lua_State *L, char *buf, size_t sz, int is_show_var)
{
size_t e = 0;
int level = 1;
int firstpart = 1;
lua_Debug ar;
int i = 0;
pushfstring(buf, e, "%s traceback:", is_show_var ? "full" : "");
while (lua_getstack(L, level++, &ar)) {
if (level > LEVELS1 && firstpart) {
if (!lua_getstack(L, level+LEVELS2, &ar))
level--;
else {
pushfstring(buf, e, "%s", "\n...");
while (lua_getstack(L, level+LEVELS2, &ar))
level++;
}
firstpart = 0;
continue;
}
pushfstring(buf, e, "\n");
lua_getinfo(L, "Snl", &ar);
pushfstring(buf, e, "[%d]%s:", level - 2, ar.short_src);
if (ar.currentline > 0)
pushfstring(buf, e, "%d:", ar.currentline);
if (*ar.namewhat != '\0')
pushfstring(buf, e, " in function %s", ar.name);
else {
if (*ar.what == 'm')
pushfstring(buf, e, " in main chunk");
else if (*ar.what == 'C' || *ar.what == 't')
pushfstring(buf, e, " ?");
else
pushfstring(buf, e, " in function <%s:%d>",
ar.short_src, ar.linedefined);
}
if (!lua_checkstack(L, 1)) /* to call lua_getlocal*/
continue;
i = 1;
while(is_show_var) {
const void *pointer;
int type;
lua_Debug arf;
const char *name = lua_getlocal(L, &ar, i++);
const char *typename;
if (name == NULL)
break;
type = lua_type(L, -1);
typename = lua_typename(L, type);
pushfstring(buf, e, "\n\t%s(%s) : ", name, typename);
switch(type) {
case LUA_TFUNCTION:
pointer = lua_topointer(L, -1);
lua_getinfo(L, ">Snl", &arf);
if (*arf.what == 'C' || *arf.what == 't')
pushfstring(buf, e, "%p %s@C",
pointer,
(arf.name != NULL ? arf.name : "defined"));
else
pushfstring(buf, e, "%p %s@%s:%d",
pointer,
(arf.name != NULL ? arf.name : "defined"),
arf.short_src, arf.linedefined);
break;
case LUA_TBOOLEAN:
pushfstring(buf, e, "%s", lua_toboolean(L, 1) ? "true" : "false");
lua_pop(L, 1);
break;
case LUA_TNIL:
pushfstring(buf, e, "nil");
lua_pop(L, 1);
break;
case LUA_TNUMBER:
case LUA_TSTRING:
pushfstring(buf, e, "%s", lua_tostring(L, -1));
lua_pop(L, 1);
break;
default:
pushfstring(buf, e, "%p", lua_topointer(L, -1));
lua_pop(L, 1);
break;
}
}
}
pushfstring(buf, e, "\n");
return e;
}
static void signal_handler(int signum)
{
lua_State *L = GlobalL;
char *buf;
signal(signum, SIG_DFL);
buf = malloc(MAX_BUF_SZ);
dump_lua_traceback(L, buf, MAX_BUF_SZ, is_full_stack);
fprintf(stderr, "%s", buf);
fflush(stderr);
free(buf);
#ifdef ENABLE_C_TRACE
/* *nix only */
dump_c_traceback(STDERR_FILENO);
#endif
}
static int lua__register(lua_State *L)
{
int signum = luaL_checkinteger(L, 1);
GlobalL = L;
signal(signum, signal_handler);
return 0;
}
static int lua__freopen(lua_State *L)
{
const char * filepath = luaL_checkstring(L, 1);
freopen(filepath, "a", stderr);
return 0;
}
static int lua__set_show_var(lua_State *L)
{
is_full_stack = lua_toboolean(L, 1);
return 0;
}
int luaopen_lcoredump(lua_State* L)
{
luaL_Reg lfuncs[] = {
{"register", lua__register},
{"freopen", lua__freopen},
{"set_show_var", lua__set_show_var},
{NULL, NULL},
};
luaL_newlib(L, lfuncs);
return 1;
}