forked from WuBingzheng/memleax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memleax.c
344 lines (303 loc) · 8.41 KB
/
memleax.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
* memleax, detects memory leak of running process.
*
* Author: Wu Bingzheng
* Date: 2016-5
*/
#include <stdlib.h>
#include <stdint.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include "breakpoint.h"
#include "ptrace_utils.h"
#include "memblock.h"
#include "symtab.h"
#include "debug_line.h"
#include "proc_info.h"
#include "addr_maps.h"
#include "memleax.h"
uintptr_t g_current_entry;
pid_t g_current_thread;
int opt_backtrace_limit = BACKTRACE_MAX;
const char *opt_debug_info_file;
static pid_t g_target_pid;
static int g_signo = 0;
static void signal_handler(int signo)
{
g_signo = signo;
kill(g_target_pid, SIGSTOP);
}
int main(int argc, char * const *argv)
{
time_t memory_expire = 10;
int memblock_limit = 1000;
int callstack_limit = 1000;
const char *help_info = "Usage: memleax [options] target-pid\n"
"\n"
"You should always set `-e` according to your scenarios.\n"
"While other options are good in most instances with default values.\n"
"\n"
"Options:\n"
" -e <expire>\n"
" Specifies memory free expire threshold in seconds.\n"
" Default is 10. An allocated memory block is reported as\n"
" memory leak if it lives longer than this.\n"
" -d <debug-info-file>\n"
" Specifies separate debug information file.\n"
" -l <backtrace-limit>\n"
" Specifies the limit of backtrace levels. Less level,\n"
" better performance. Default is 50, which is also the max.\n"
" -m <memory-block-max>\n"
" Stop monitoring if there are so many expired memory\n"
" block at a same CallStack. Default is 1000.\n"
" -c <call-stack-max>\n"
" Stop monitoring if there are so many CallStack with\n"
" memory leak. Default is 1000.\n"
" -h Print help and quit.\n"
" -v Print version and quit.\n";
/* parse options */
int ch;
while((ch = getopt(argc, argv, "hve:d:l:m:c:")) != -1) {
switch(ch) {
case 'e':
memory_expire = atoi(optarg);
if (memory_expire == 0) {
printf("invalid expire: %s\n", optarg);
return 1;
}
break;
case 'd':
opt_debug_info_file = optarg;
break;
case 'l':
opt_backtrace_limit = atoi(optarg);
if (opt_backtrace_limit == 0) {
printf("invalid backtrace limit: %s\n", optarg);
return 1;
}
if (opt_backtrace_limit > BACKTRACE_MAX) {
printf("too big backtrace limit: %s\n", optarg);
return 1;
}
break;
case 'm':
memblock_limit = atoi(optarg);
if (memblock_limit == 0) {
printf("invalid memblock_limit: %s\n", optarg);
return 1;
}
break;
case 'c':
callstack_limit = atoi(optarg);
if (callstack_limit == 0) {
printf("invalid callstack_limit: %s\n", optarg);
return 1;
}
break;
case 'h':
printf("%s", help_info);
return 0;
case 'v':
printf("Version: 1.0.3\n");
printf("Author: Wu Bingzheng\n");
return 0;
default:
printf("Try `-h` for help information.\n");
return 1;
}
}
if (optind + 1 != argc) {
printf("invalid argument\n");
printf("Try `-h` for help information.\n");
return 1;
}
g_target_pid = atoi(argv[optind]);
if (g_target_pid == 0) {
fprintf(stderr, "invalid target pid: %s\n", argv[optind]);
return 2;
}
/* prepare */
addr_maps_build(g_target_pid);
ptr_maps_build(g_target_pid);
symtab_build(g_target_pid);
debug_line_build(g_target_pid);
#ifdef MLX_LINUX
/* Linux: attach to all other existing threads */
pid_t task_id;
while ((task_id = proc_tasks(g_target_pid)) > 0) {
if (task_id == g_target_pid) {
continue;
}
ptrace_attach(task_id);
waitpid(task_id, NULL, __WALL);
ptrace_trace_child(task_id);
ptrace_continue(task_id, 0);
}
#endif
/* attach to target and set breakpoint */
ptrace_attach(g_target_pid);
waitpid(g_target_pid, NULL, 0);
ptrace_trace_child(g_target_pid);
breakpoint_init(g_target_pid);
ptrace_continue(g_target_pid, 0);
/* signals */
signal(SIGHUP, signal_handler);
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGALRM, signal_handler);
/* begin work */
printf("== Begin monitoring process %d...\n", g_target_pid);
time_t next, begin = time(NULL);
struct breakpoint_s *bp = NULL;
uintptr_t return_address = 0, return_code = 0;
uintptr_t arg1 = 0, arg2 = 0;
pid_t last_thread = 0;
pid_t hold_threads[1000];
int hold_thread_num = 0;
while (1) {
/* wait for breakpoint */
int status;
#ifdef MLX_LINUX
pid_t pid = waitpid(-1, &status, __WALL);
#else /* FreeBSD */
pid_t pid = waitpid(-1, &status, 0);
#endif
log_debug("wait: pid=%d status=%x\n", pid, status);
if (pid < 0) {
if (errno == EINTR) {
continue;
}
perror("\n== Error on waitpid()");
break;
}
if (!WIFSTOPPED(status)) {
if (pid != g_target_pid) {
continue;
}
printf("\n== Target process exit.\n");
break;
}
int signum = WSTOPSIG(status);
if (signum == SIGSTOP) {
/* by signal_handler() */
if (g_signo == SIGALRM) {
g_signo = 0;
goto lexpire;
} else if (g_signo != 0) { /* SIGINT SIGHUP SIGTERM */
printf("\n== Terminate monitoring.\n");
break;
}
/* trap at child's first instruction */
#ifdef MLX_LINUX
else if (proc_task_check(g_target_pid, pid)) { /* thread in Linux */
log_debug("new thread id=%d\n", pid);
ptrace_continue(pid, 0);
continue;
}
#endif
else { /* child process, detach it */
log_debug("new process id=%d\n", pid);
breakpoint_cleanup(pid);
ptrace_detach(pid, 0);
continue;
}
}
if (signum != SIGTRAP) { /* forward signals */
ptrace_continue(pid, signum);
continue;
}
/* new child */
if (ptrace_new_child(pid, status)) {
ptrace_continue(pid, 0);
continue;
}
/* get registers */
registers_info_t regs;
ptrace_get_regs(pid, ®s);
/* unwind the RIP back by 1 to let the CPU execute the
* original instruction that was there. */
REG_RIP(regs) -= 1;
ptrace_set_regs(pid, ®s);
uintptr_t rip = REG_RIP(regs);
log_debug("\nbreak at: %lx\n", rip);
/* another thread is in a function, so hold this one */
if (return_address != 0 && last_thread != pid) {
log_debug("hold thread: %d\n", pid);
hold_threads[hold_thread_num++] = pid;
continue;
}
/* breaking after a function-entry-breakpoint, which means
* we are at the function-return-breakpoint, or at another
* function-entry-breakpoint. In the latter case, we ignore
* the formor function-entry-breakpoint. */
if (return_address != 0) {
/* recover return code */
ptrace_set_data(pid, return_address, return_code);
/* re-set breakpoint at entry address */
ptrace_set_int3(pid, bp->entry_address, bp->entry_code);
}
if (rip == return_address) {
/* -- at function return */
/* set these for building backtrace far away */
g_current_entry = bp->entry_address;
g_current_thread = pid;
return_address = 0;
if (bp->handler(REG_RAX(regs), arg1, arg2) != 0) {
printf("\n== Not enough memory.\n");
break;
}
/* wakeup one hold-thread */
if (hold_thread_num != 0) {
ptrace_continue(hold_threads[--hold_thread_num], 0);
log_debug("wakeup thread: %d\n", hold_threads[hold_thread_num]);
}
} else if ((bp = breakpoint_by_entry(rip)) != NULL) {
/* -- at function entry */
/* recover entry code */
ptrace_set_data(pid, bp->entry_address, bp->entry_code);
/* set breakpoint at return address */
return_address = ptrace_get_data(pid, REG_RSP(regs));
return_code = ptrace_get_data(pid, return_address);
ptrace_set_int3(pid, return_address, return_code);
/* save arguments */
arg1 = REG_RDI(regs);
arg2 = REG_RSI(regs);
last_thread = pid;
} else {
/* maybe other thread met a return-breakpoint which
* is recoverd already */
uintptr_t code = ptrace_get_data(pid, rip);
if ((code & 0xFF) == 0xCC) {
printf("unknown breakpoint %lx\n", rip);
break;
}
}
lexpire:
next = memblock_expire(memory_expire, memblock_limit, callstack_limit);
if (next < 0) { /* too many memory-blocks or callstacks */
break;
}
alarm(next);
ptrace_continue(pid, 0);
}
/* clean up, entry and return point */
breakpoint_cleanup(g_target_pid);
if (return_address != 0) {
ptrace_set_data(g_target_pid, return_address, return_code);
}
ptrace_detach(g_target_pid, 0);
if (time(NULL) - begin < memory_expire) {
printf("== Your monitoring time is too short. "
"%ld seconds is need.\n", memory_expire);
return 2;
}
callstack_report();
return 0;
}