forked from cloudwu/lmonitor
/
lmonitor.c
190 lines (176 loc) · 4.09 KB
/
lmonitor.c
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
188
189
190
#include <lua.h>
#include <lauxlib.h>
#include <string.h>
#include <stdlib.h>
#define REPORT_MAX (1024*1024)
#define FNAME_MAX 1024
struct status {
int max_depth;
int depth;
int calls;
int ptr;
int from;
int count;
char buffer[REPORT_MAX];
};
static struct status * G = NULL;
static void
monitor_init(struct status * st) {
st->max_depth = 0;
st->depth = 0;
st->calls = 0;
st->ptr = 0;
st->count = 0;
st->from = 0;
}
static void
monitor_cat(struct status * st, const char * str, size_t sz) {
if (sz + st->ptr > REPORT_MAX) {
sz = REPORT_MAX - st->ptr;
}
memcpy(st->buffer + st->ptr, str, sz);
st->ptr += sz;
}
static void
recordcall(struct status *s, int event) {
switch (event) {
case LUA_HOOKCALL:
case LUA_HOOKTAILCALL:
if (++s->depth > s->max_depth) {
s->max_depth = s->depth;
}
break;
case LUA_HOOKRET:
--s->depth;
++s->calls;
break;
case LUA_HOOKLINE:
s->count ++;
break;
}
}
static void
monitor_depth(lua_State *L, lua_Debug *ar) {
recordcall(G, ar->event);
}
static void
monitor_detailreport(lua_State *L, lua_Debug *ar) {
char info[FNAME_MAX];
struct status * s = G;
int n;
lua_getinfo(L, "nS", ar);
switch (ar->event) {
case LUA_HOOKCALL:
case LUA_HOOKTAILCALL:
if (ar->name != NULL) {
n = snprintf(info, FNAME_MAX, "%d\n%*s%s ", s->count, s->depth, "", ar->name);
} else if (ar->linedefined < 0) {
n = snprintf(info, FNAME_MAX, "%d\n%*s= ", s->count, s->depth, "");
} else {
n = snprintf(info, FNAME_MAX, "%d\n%*s%s:%d ", s->count, s->depth, "", ar->short_src, ar->linedefined);
}
monitor_cat(s, info, n);
s->count = 0;
break;
case LUA_HOOKRET:
if (s->count > 0) {
n = snprintf(info, FNAME_MAX, "%d\n%*s", s->count, s->depth,"");
monitor_cat(s, info, n);
s->count = 0;
}
break;
}
recordcall(s, ar->event);
}
static void
monitor_depthfrom(lua_State *L, lua_Debug *ar) {
struct status * s = G;
switch (ar->event) {
case LUA_HOOKRET:
if (s->calls > s->from) {
lua_sethook(L, monitor_detailreport, LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 0);
}
break;
}
recordcall(s, ar->event);
}
static void
monitor_report(lua_State *L, lua_Debug *ar) {
char info[FNAME_MAX];
struct status * s = G;
int n;
lua_getinfo(L, "nS", ar);
switch (ar->event) {
case LUA_HOOKCALL:
case LUA_HOOKTAILCALL:
if (ar->name != NULL) {
n = snprintf(info, FNAME_MAX, "%*s%s\n", s->depth, "", ar->name);
} else if (ar->linedefined < 0) {
n = snprintf(info, FNAME_MAX, "%*s=\n", s->depth, "");
} else {
n = snprintf(info, FNAME_MAX, "%*s%s:%d\n", s->depth, "", ar->short_src, ar->linedefined);
}
monitor_cat(s, info, n);
break;
}
recordcall(s, ar->event);
}
static int
lreport(lua_State *L) {
monitor_init(G);
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_sethook(L, monitor_report, LUA_MASKCALL | LUA_MASKRET, 0);
int args = lua_gettop(L) - 1;
lua_call(L, args, 0);
lua_sethook(L, NULL, 0 , 0);
lua_pushinteger(L, G->max_depth);
lua_pushinteger(L, G->calls);
lua_pushlstring(L, G->buffer, G->ptr);
return 3;
}
static int
ldepth(lua_State *L) {
monitor_init(G);
int args = lua_gettop(L) - 1;
if (!lua_isfunction(L, 1)) {
int n = luaL_checkinteger(L, 1);
G->from = n;
--args;
lua_sethook(L, monitor_depthfrom, LUA_MASKCALL | LUA_MASKRET, 0);
} else {
lua_sethook(L, monitor_depth, LUA_MASKCALL | LUA_MASKRET, 0);
}
lua_call(L, args, 0);
lua_sethook(L, NULL, 0 , 0);
lua_pushinteger(L, G->max_depth);
lua_pushinteger(L, G->calls);
lua_pushlstring(L, G->buffer, G->ptr);
return 3;
}
static int
ldetailreport(lua_State *L) {
monitor_init(G);
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_sethook(L, monitor_detailreport, LUA_MASKCALL | LUA_MASKRET | LUA_MASKLINE, 0);
int args = lua_gettop(L) - 1;
lua_call(L, args, 0);
lua_sethook(L, NULL, 0 , 0);
lua_pushinteger(L, G->max_depth);
lua_pushinteger(L, G->calls);
lua_pushlstring(L, G->buffer, G->ptr);
return 3;
}
int
luaopen_monitor(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{ "depth", ldepth },
{ "report", lreport },
{ "detailreport", ldetailreport },
{ NULL, NULL },
};
luaL_newlib(L,l);
struct status * s = malloc(sizeof(*G));
G = s;
return 1;
}