13 static char *get_debug_data() {
17 while (!feof(stdin
)) {
18 buf
= realloc(buf
,size
+BUFSIZ
);
19 size
+= fread(buf
+size
,1,BUFSIZ
,stdin
);
25 static char *dd_get_proc(char *debug
) {
26 char *line
= strstr(debug
,"Process: #");
28 char *space
= strchr(line
+10,' ');
30 char *newline
= strchr(space
,'\n');
32 static char name
[256];
34 memcpy(name
,space
,newline
-space
);
35 name
[newline
-space
] = 0;
43 static void *dd_get_eip(char *debug
) {
44 char *pos
= strstr(debug
,"EIP: ");
46 return (void*)strtoul(pos
+5,NULL
,16);
51 static symbol_t
*get_symbols(char *name
) {
53 symbol_t
*symbols
= NULL
;
55 asprintf(&cmd
,"nm -pP --defined-only files/bin/%s",name
);
56 FILE *nm
= popen(cmd
,"r");
65 for (i
=0;fscanf(nm
,"%s %c %x %x",name
,&type
,&addr
,&size
)>0;i
++) {
66 symbols
= realloc(symbols
,(i
+1)*sizeof(symbol_t
));
67 symbols
[i
].addr
= addr
;
68 symbols
[i
].name
= strdup(name
);
69 symbols
[i
].type
= type
;
70 symbols
[i
].size
= size
;
76 symbols
= realloc(symbols
,(i
+1)*sizeof(symbol_t
));
77 memset(symbols
+i
,0,sizeof(symbol_t
));
84 static symbol_t
*match_symbol(symbol_t
*symbols
,void *addr
) {
86 symbol_t
*best
= NULL
;
89 for (i
=0;symbols
[i
].type
!=0;i
++) {
90 if (addr
>symbols
[i
].addr
&& symbols
[i
].addr
>max
&& (symbols
[i
].type
=='t' || symbols
[i
].type
=='T')) {
91 max
= symbols
[i
].addr
;
98 static void match_stack(char *debug
,symbol_t
*symbols
) {
100 char *stack
= strstr(debug
,"Stack:\n");
102 char *line
= stack
+6;
109 sscanf(line
,"0x%x: 0x%x",&esp
,&addr
);
111 symbol_t
*symbol
= match_symbol(symbols
,addr
);
113 printf("0x%08x: 0x%08x %s+0x%x (%c,%d)\n",esp
,addr
,symbol
->name
,addr
-symbol
->addr
,symbol
->type
,symbol
->size
);
115 else printf("0x%08x: 0x%08x\n",esp
,addr
);
117 } while ((line
= strchr(line
,'\n'))!=NULL
);
121 static void match_eip(symbol_t
*symbols
,void *eip
) {
122 symbol_t
*symbol
= match_symbol(symbols
,eip
);
124 printf("(EIP) 0x%08x %s+0x%x (%c,%d)\n",eip
,symbol
->name
,eip
-symbol
->addr
,symbol
->type
,symbol
->size
);
126 else printf("(EIP) 0x%08x\n",eip
);
129 int main(int argc
,char *argv
[]) {
131 char *debug
= get_debug_data();
132 if (debug
==NULL
) return 1;
134 char *proc_name
= dd_get_proc(debug
);
135 if (proc_name
==NULL
) return 1;
136 printf("|======{%s}======[Stacktrace]======|\n",proc_name
);
138 symbol_t
*symbols
= get_symbols(proc_name
);
139 if (symbols
==NULL
) return 1;
141 void *eip
= dd_get_eip(debug
);
142 if (eip
!=NULL
) match_eip(symbols
,eip
);
144 match_stack(debug
,symbols
);
147 for (i
=0;symbols
[i
].type
!=0;i
++) free(symbols
[i
].name
);