Rev 2947 | Rev 3093 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 2947 | Rev 3005 | ||
|---|---|---|---|
| Line 43... | Line 43... | ||
| 43 | #include "../../../main.h" |
43 | #include "../../../main.h" |
| 44 | #include "../../../include/arch.h" |
44 | #include "../../../include/arch.h" |
| 45 | 45 | ||
| 46 | #define OPCODE_INT3 0xCC |
46 | #define OPCODE_INT3 0xCC |
| 47 | 47 | ||
| 48 | static breakpoint_t *lifted_brkpt; |
- | |
| 49 | - | ||
| 50 | void arch_breakpoint_add(uintptr_t addr) |
48 | int arch_breakpoint_set(breakpoint_t *b) |
| 51 | { |
49 | { |
| 52 | char brkp[1]; |
50 | char brkp[1]; |
| 53 | int rc; |
51 | int rc; |
| 54 | breakpoint_t *brk; |
- | |
| 55 | int i; |
- | |
| 56 | - | ||
| 57 | brk = NULL; |
- | |
| 58 | for (i = 1; i < MAX_BRKPTS; i++) |
- | |
| 59 | if (brk_list[i].set == 0) { brk = brk_list+i; break; } |
- | |
| 60 | 52 | ||
| 61 | if (!brk) { |
- | |
| 62 | cons_printf("too many breakpoints\n"); |
- | |
| 63 | return; |
- | |
| 64 | } |
- | |
| 65 | - | ||
| 66 | rc = udebug_mem_read(app_phone, &brk->arch.back, addr, 1); |
53 | rc = udebug_mem_read(app_phone, &b->arch.back, b->addr, 1); |
| 67 | cons_printf("udebug_mem_read() -> %d\n", rc); |
54 | cons_printf("udebug_mem_read() -> %d\n", rc); |
| 68 | brkp[0] = OPCODE_INT3; |
55 | if (rc < 0) return rc; |
| 69 | rc = udebug_mem_write(app_phone, brkp, addr, 1); |
- | |
| 70 | cons_printf("udebug_mem_write() -> %d\n", rc); |
- | |
| 71 | 56 | ||
| 72 | brk->addr = addr; |
57 | brkp[0] = OPCODE_INT3; |
| - | 58 | rc = udebug_mem_write(app_phone, brkp, b->addr, 1); |
|
| 73 | brk->set = 1; |
59 | if (rc < 0) return rc; |
| 74 | 60 | ||
| 75 | cons_printf("Added breakpoint %d\n", i); |
61 | cons_printf("udebug_mem_write() -> %d\n", rc); |
| - | 62 | return 0; |
|
| 76 | } |
63 | } |
| 77 | 64 | ||
| 78 | void arch_breakpoint_remove(int id) |
65 | int arch_breakpoint_remove(breakpoint_t *b) |
| 79 | { |
66 | { |
| 80 | int rc; |
67 | int rc; |
| 81 | 68 | ||
| 82 | if (id < 1 || id >= MAX_BRKPTS || brk_list[id].set == 0) { |
- | |
| 83 | cons_printf("No such breakpoint\n"); |
- | |
| 84 | return; |
- | |
| 85 | } |
- | |
| 86 | - | ||
| 87 | if (lifted_brkpt == &brk_list[id]) { |
69 | if (b->active) { |
| 88 | lifted_brkpt = NULL; |
70 | active_bkpt = NULL; |
| 89 | } else { |
71 | } else { |
| 90 | rc = udebug_mem_write(app_phone, &brk_list[id].arch.back, brk_list[id].addr, 1); |
72 | rc = udebug_mem_write(app_phone, &b->arch.back, b->addr, 1); |
| 91 | if (rc < 0) { printf("error writing mem\n"); return; } |
- | |
| 92 | } |
- | |
| 93 | - | ||
| 94 | brk_list[id].set = 0; |
- | |
| 95 | - | ||
| 96 | cons_printf("Breakpoint removed\n"); |
- | |
| 97 | } |
- | |
| 98 | - | ||
| 99 | void arch_breakpoint_list(void) |
- | |
| 100 | { |
- | |
| 101 | int i, cnt; |
- | |
| 102 | - | ||
| 103 | cnt = 0; |
- | |
| 104 | for (i = 0; i < MAX_BRKPTS; ++i) { |
- | |
| 105 | if (brk_list[i].set != 0) { |
73 | if (rc < 0) { |
| 106 | cons_printf("Breakpoint %d at 0x%lx\n", i, |
74 | cons_printf("error writing mem\n"); |
| 107 | brk_list[i].addr); |
- | |
| 108 | ++cnt; |
75 | return rc; |
| 109 | } |
76 | } |
| 110 | } |
77 | } |
| - | 78 | ||
| 111 | if (cnt == 0) cons_printf("No breakpoints set\n"); |
79 | return 0; |
| 112 | } |
80 | } |
| 113 | 81 | ||
| 114 | void arch_event_breakpoint(thash_t thread_hash) |
82 | void arch_event_breakpoint(thash_t thread_hash) |
| 115 | { |
83 | { |
| 116 | static istate_t istate; |
84 | static istate_t istate; |
| - | 85 | breakpoint_t *b; |
|
| 117 | int rc; |
86 | int rc; |
| 118 | 87 | ||
| 119 | rc = udebug_regs_read(app_phone, thread_hash, &istate); |
88 | rc = udebug_regs_read(app_phone, thread_hash, &istate); |
| 120 | // cons_printf("udebug_regs_read -> %d\n", rc); |
89 | // cons_printf("udebug_regs_read -> %d\n", rc); |
| 121 | // cons_printf("EIP was 0x%08x\n", istate.eip); |
90 | // cons_printf("EIP was 0x%08x\n", istate.eip); |
| 122 | int brk_addr = istate.eip - 1; |
91 | int brk_addr = istate.eip - 1; |
| 123 | int bi; |
92 | |
| 124 | for (bi = 0; bi < MAX_BRKPTS; bi++) { |
93 | b = breakpoint_find_by_addr(brk_addr); |
| - | 94 | if (!b) { |
|
| 125 | if (brk_list[bi].set && brk_list[bi].addr == brk_addr) |
95 | cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr); |
| 126 | break; |
96 | return; |
| 127 | } |
97 | } |
| 128 | 98 | ||
| 129 | if (bi < MAX_BRKPTS) { |
99 | istate.eip = brk_addr; |
| 130 | cons_printf("breakpoint %d hit\n", bi); |
100 | istate.eflags |= 0x0100; /* trap flag */ |
| 131 | 101 | ||
| 132 | istate.eip = brk_addr; |
- | |
| 133 | istate.eflags |= 0x0100; /* trap flag */ |
- | |
| 134 | // cons_printf("setting EIP to 0x%08x\n", istate.eip); |
- | |
| 135 | rc = udebug_regs_write(app_phone, thread_hash, &istate); |
102 | rc = udebug_regs_write(app_phone, thread_hash, &istate); |
| 136 | if (rc < 0) { printf("error writing regs\n"); return; } |
103 | if (rc < 0) { cons_printf("error writing regs\n"); return; } |
| 137 | rc = udebug_mem_write(app_phone, &brk_list[bi].arch.back, brk_addr, 1); |
104 | rc = udebug_mem_write(app_phone, &b->arch.back, brk_addr, 1); |
| 138 | if (rc < 0) { printf("error writing mem\n"); return; } |
105 | if (rc < 0) { cons_printf("error writing mem\n"); return; } |
| 139 | // cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc); |
106 | // cons_printf("udebug_mem_write(phone, 0x%x, 0x%02x, 1) -> %d\n", brk_addr, brk_list[bi].arch.back, rc); |
| 140 | lifted_brkpt = &brk_list[bi]; |
- | |
| 141 | 107 | ||
| 142 | breakpoint_hit(); |
108 | b->active = true; |
| 143 | } else { |
109 | active_bkpt = b; |
| 144 | cons_printf("unrecognized breakpoint at 0x%x\n", brk_addr); |
- | |
| 145 | } |
110 | |
| - | 111 | breakpoint_hit(b); |
|
| 146 | } |
112 | } |
| 147 | 113 | ||
| 148 | void arch_event_trap(dthread_t *dt) |
114 | void arch_event_trap(dthread_t *dt) |
| 149 | { |
115 | { |
| - | 116 | breakpoint_t *b; |
|
| 150 | static istate_t istate; |
117 | static istate_t istate; |
| 151 | unsigned char brkinstr[1]; |
118 | unsigned char brkinstr[1]; |
| 152 | int rc; |
119 | int rc; |
| 153 | 120 | ||
| 154 | // cons_printf("trap event\n"); |
121 | // cons_printf("trap event\n"); |
| - | 122 | b = active_bkpt; |
|
| 155 | 123 | ||
| 156 | breakpoint_t *lb = lifted_brkpt; |
- | |
| 157 | if (lb) { |
124 | if (b) { |
| 158 | brkinstr[0] = OPCODE_INT3; |
125 | brkinstr[0] = OPCODE_INT3; |
| 159 | rc = udebug_mem_write(app_phone, brkinstr, lb->addr, 1); |
126 | rc = udebug_mem_write(app_phone, brkinstr, b->addr, 1); |
| 160 | // cons_printf("restore breakpoint -> %d\n", rc); |
127 | // cons_printf("restore breakpoint -> %d\n", rc); |
| 161 | lifted_brkpt = NULL; |
128 | active_bkpt = NULL; |
| 162 | } |
129 | } |
| 163 | 130 | ||
| 164 | if (!dt->arch.singlestep) { |
131 | if (!dt->arch.singlestep) { |
| 165 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
132 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
| 166 | // cons_printf("udebug_regs_read -> %d\n", rc); |
133 | // cons_printf("udebug_regs_read -> %d\n", rc); |
| Line 195... | Line 162... | ||
| 195 | 162 | ||
| 196 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
163 | rc = udebug_regs_read(app_phone, dt->hash, &istate); |
| 197 | if (rc < 0) { printf("regs read failed\n"); return; } |
164 | if (rc < 0) { printf("regs read failed\n"); return; } |
| 198 | 165 | ||
| 199 | if (enable) istate.eflags |= 0x0100; /* trap flag */ |
166 | if (enable) istate.eflags |= 0x0100; /* trap flag */ |
| 200 | else if (!lifted_brkpt) istate.eflags &= ~0x0100; /* trap flag */ |
167 | else if (!active_bkpt) istate.eflags &= ~0x0100; /* trap flag */ |
| 201 | 168 | ||
| 202 | rc = udebug_regs_write(app_phone, dt->hash, &istate); |
169 | rc = udebug_regs_write(app_phone, dt->hash, &istate); |
| 203 | if (rc < 0) { printf("regs write failed\n"); return; } |
170 | if (rc < 0) { printf("regs write failed\n"); return; } |
| 204 | 171 | ||
| 205 | dt->arch.singlestep = enable; |
172 | dt->arch.singlestep = enable; |