blob: 8f84e0149ff8e1e8a6c9f6189edbc6a3699c2a3f [file] [log] [blame]
Bruce Beare6cc49232010-10-13 16:11:15 -07001#include <cutils/logd.h>
2#include <sys/ptrace.h>
3#include "../utility.h"
4#include "x86_utility.h"
5
6
7int unwind_backtrace_with_ptrace_x86(int tfd, pid_t pid, mapinfo *map,
8 bool at_fault)
9{
10 struct pt_regs_x86 r;
11 unsigned int stack_level = 0;
12 unsigned int stack_depth = 0;
13 unsigned int rel_pc;
14 unsigned int stack_ptr;
15 unsigned int stack_content;
16
17 if(ptrace(PTRACE_GETREGS, pid, 0, &r)) return 0;
18 unsigned int eip = (unsigned int)r.eip;
19 unsigned int ebp = (unsigned int)r.ebp;
20 unsigned int cur_sp = (unsigned int)r.esp;
21 const mapinfo *mi;
22 const struct symbol* sym = 0;
23
24
25//ebp==0, it indicates that the stack is poped to the bottom or there is no stack at all.
26 while (ebp) {
27 _LOG(tfd, !at_fault, "#0%d ",stack_level);
28 mi = pc_to_mapinfo(map, eip, &rel_pc);
29
30 /* See if we can determine what symbol this stack frame resides in */
31 if (mi != 0 && mi->symbols != 0) {
32 sym = symbol_table_lookup(mi->symbols, rel_pc);
33 }
34 if (sym) {
35 _LOG(tfd, !at_fault, " eip: %08x %s (%s)\n", eip, mi ? mi->name : "", sym->name);
36 } else {
37 _LOG(tfd, !at_fault, " eip: %08x %s\n", eip, mi ? mi->name : "");
38 }
39
40 stack_level++;
41 if (stack_level >= STACK_DEPTH || eip == 0)
42 break;
43 eip = ptrace(PTRACE_PEEKTEXT, pid, (void*)(ebp + 4), NULL);
44 ebp = ptrace(PTRACE_PEEKTEXT, pid, (void*)ebp, NULL);
45 }
46 ebp = (unsigned int)r.ebp;
47 stack_depth = stack_level;
48 stack_level = 0;
49 if (ebp)
50 _LOG(tfd, !at_fault, "stack: \n");
51 while (ebp) {
52 _LOG(tfd, !at_fault, "#0%d \n",stack_level);
53 stack_ptr = cur_sp;
54 while((int)(ebp - stack_ptr) >= 0) {
55 stack_content = ptrace(PTRACE_PEEKTEXT, pid, (void*)stack_ptr, NULL);
56 mi = pc_to_mapinfo(map, stack_content, &rel_pc);
57
58 /* See if we can determine what symbol this stack frame resides in */
59 if (mi != 0 && mi->symbols != 0) {
60 sym = symbol_table_lookup(mi->symbols, rel_pc);
61 }
62 if (sym) {
63 _LOG(tfd, !at_fault, " %08x %08x %s (%s)\n",
64 stack_ptr, stack_content, mi ? mi->name : "", sym->name);
65 } else {
66 _LOG(tfd, !at_fault, " %08x %08x %s\n", stack_ptr, stack_content, mi ? mi->name : "");
67 }
68
69 stack_ptr = stack_ptr + 4;
70 //the stack frame may be very deep.
71 if((int)(stack_ptr - cur_sp) >= STACK_FRAME_DEPTH) {
72 _LOG(tfd, !at_fault, " ...... ...... \n");
73 break;
74 }
75 }
76 cur_sp = ebp + 4;
77 stack_level++;
78 if (stack_level >= STACK_DEPTH || stack_level >= stack_depth)
79 break;
80 ebp = ptrace(PTRACE_PEEKTEXT, pid, (void*)ebp, NULL);
81 }
82
83 return stack_depth;
84}
85