-
Notifications
You must be signed in to change notification settings - Fork 0
/
backtrace.c
34 lines (33 loc) · 881 Bytes
/
backtrace.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
#include <csapp.h>
#define STACK_SIZE 32
int main(int argc, char** argv) {
int fd = Open("./backtrace.txt", O_RDONLY, S_IRUSR);
rio_t rio;
Rio_readinitb(&rio, fd);
char* args[STACK_SIZE] = {"/usr/bin/addr2line", "-e", "kernel/kernel"};
char buff[MAXBUF];
char* p = buff;
ssize_t len = 0;
int idx = 3;
for (; idx < STACK_SIZE && (len = Rio_readlineb(&rio, p, MAXBUF)); idx++) {
args[idx] = p;
p += len;
if (*(p - 1) == '\n') *(p - 1) = 0;
else {
*p = 0;
p++;
}
}
args[idx] = NULL;
Close(fd);
for (int i = 3; i < idx; i++) {
fprintf(stdout, "args[%d]:%s\n", i, args[i]);
}
pid_t pid;
if ((pid = Fork()) == 0) {
Execve(args[0], args, environ);
}
while (wait(NULL) != pid) ;
fprintf(stdout, "reap:addr2line\n");
return 0;
}