190b3dd41d54c03be0182d932702ece2ab9a48a9
[reactos.git] / reactos / dll / win32 / dbghelp / cpu_arm.c
1 /*
2 * File cpu_arm.c
3 *
4 * Copyright (C) 2009 Eric Pouech
5 * Copyright (C) 2010, 2011 André Hentschel
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "dbghelp_private.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
25
26 static BOOL arm_get_addr(HANDLE hThread, const CONTEXT* ctx,
27 enum cpu_addr ca, ADDRESS64* addr)
28 {
29 addr->Mode = AddrModeFlat;
30 addr->Segment = 0; /* don't need segment */
31 switch (ca)
32 {
33 #ifdef __arm__
34 case cpu_addr_pc: addr->Offset = ctx->Pc; return TRUE;
35 case cpu_addr_stack: addr->Offset = ctx->Sp; return TRUE;
36 case cpu_addr_frame: addr->Offset = ctx->R11; return TRUE;
37 #endif
38 default: addr->Mode = -1;
39 return FALSE;
40 }
41 }
42
43 #ifdef __arm__
44 enum st_mode {stm_start, stm_arm, stm_done};
45
46 /* indexes in Reserved array */
47 #define __CurrentModeCount 0
48
49 #define curr_mode (frame->Reserved[__CurrentModeCount] & 0x0F)
50 #define curr_count (frame->Reserved[__CurrentModeCount] >> 4)
51
52 #define set_curr_mode(m) {frame->Reserved[__CurrentModeCount] &= ~0x0F; frame->Reserved[__CurrentModeCount] |= (m & 0x0F);}
53 #define inc_curr_count() (frame->Reserved[__CurrentModeCount] += 0x10)
54
55 /* fetch_next_frame()
56 *
57 * modify (at least) context.Pc using unwind information
58 * either out of debug info (dwarf), or simple Lr trace
59 */
60 static BOOL fetch_next_frame(struct cpu_stack_walk* csw,
61 CONTEXT* context, DWORD_PTR curr_pc)
62 {
63 DWORD_PTR xframe;
64 DWORD oldReturn = context->Lr;
65
66 if (dwarf2_virtual_unwind(csw, curr_pc, context, &xframe))
67 {
68 context->Sp = xframe;
69 context->Pc = oldReturn;
70 return TRUE;
71 }
72
73 if (context->Pc == context->Lr) return FALSE;
74 context->Pc = oldReturn;
75
76 return TRUE;
77 }
78
79 static BOOL arm_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame, CONTEXT* context)
80 {
81 unsigned deltapc = curr_count <= 1 ? 0 : 4;
82
83 /* sanity check */
84 if (curr_mode >= stm_done) return FALSE;
85
86 TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s\n",
87 wine_dbgstr_addr(&frame->AddrPC),
88 wine_dbgstr_addr(&frame->AddrFrame),
89 wine_dbgstr_addr(&frame->AddrReturn),
90 wine_dbgstr_addr(&frame->AddrStack),
91 curr_mode == stm_start ? "start" : "ARM",
92 wine_dbgstr_longlong(curr_count));
93
94 if (curr_mode == stm_start)
95 {
96 /* Init done */
97 set_curr_mode(stm_arm);
98 frame->AddrReturn.Mode = frame->AddrStack.Mode = AddrModeFlat;
99 /* don't set up AddrStack on first call. Either the caller has set it up, or
100 * we will get it in the next frame
101 */
102 memset(&frame->AddrBStore, 0, sizeof(frame->AddrBStore));
103 }
104 else
105 {
106 if (context->Sp != frame->AddrStack.Offset) FIXME("inconsistent Stack Pointer\n");
107 if (context->Pc != frame->AddrPC.Offset) FIXME("inconsistent Program Counter\n");
108
109 if (frame->AddrReturn.Offset == 0) goto done_err;
110 if (!fetch_next_frame(csw, context, frame->AddrPC.Offset - deltapc))
111 goto done_err;
112 }
113
114 memset(&frame->Params, 0, sizeof(frame->Params));
115
116 /* set frame information */
117 frame->AddrStack.Offset = context->Sp;
118 frame->AddrReturn.Offset = context->Lr;
119 frame->AddrFrame.Offset = context->R11;
120 frame->AddrPC.Offset = context->Pc;
121
122 frame->Far = TRUE;
123 frame->Virtual = TRUE;
124 inc_curr_count();
125
126 TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s FuncTable=%p\n",
127 wine_dbgstr_addr(&frame->AddrPC),
128 wine_dbgstr_addr(&frame->AddrFrame),
129 wine_dbgstr_addr(&frame->AddrReturn),
130 wine_dbgstr_addr(&frame->AddrStack),
131 curr_mode == stm_start ? "start" : "ARM",
132 wine_dbgstr_longlong(curr_count),
133 frame->FuncTableEntry);
134
135 return TRUE;
136 done_err:
137 set_curr_mode(stm_done);
138 return FALSE;
139 }
140 #else
141 static BOOL arm_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame, CONTEXT* context)
142 {
143 return FALSE;
144 }
145 #endif
146
147 static unsigned arm_map_dwarf_register(unsigned regno, BOOL eh_frame)
148 {
149 if (regno <= 15) return CV_ARM_R0 + regno;
150 if (regno == 128) return CV_ARM_CPSR;
151
152 FIXME("Don't know how to map register %d\n", regno);
153 return CV_ARM_NOREG;
154 }
155
156 static void* arm_fetch_context_reg(CONTEXT* ctx, unsigned regno, unsigned* size)
157 {
158 #ifdef __arm__
159 switch (regno)
160 {
161 case CV_ARM_R0 + 0: *size = sizeof(ctx->R0); return &ctx->R0;
162 case CV_ARM_R0 + 1: *size = sizeof(ctx->R1); return &ctx->R1;
163 case CV_ARM_R0 + 2: *size = sizeof(ctx->R2); return &ctx->R2;
164 case CV_ARM_R0 + 3: *size = sizeof(ctx->R3); return &ctx->R3;
165 case CV_ARM_R0 + 4: *size = sizeof(ctx->R4); return &ctx->R4;
166 case CV_ARM_R0 + 5: *size = sizeof(ctx->R5); return &ctx->R5;
167 case CV_ARM_R0 + 6: *size = sizeof(ctx->R6); return &ctx->R6;
168 case CV_ARM_R0 + 7: *size = sizeof(ctx->R7); return &ctx->R7;
169 case CV_ARM_R0 + 8: *size = sizeof(ctx->R8); return &ctx->R8;
170 case CV_ARM_R0 + 9: *size = sizeof(ctx->R9); return &ctx->R9;
171 case CV_ARM_R0 + 10: *size = sizeof(ctx->R10); return &ctx->R10;
172 case CV_ARM_R0 + 11: *size = sizeof(ctx->R11); return &ctx->R11;
173 case CV_ARM_R0 + 12: *size = sizeof(ctx->R12); return &ctx->R12;
174
175 case CV_ARM_SP: *size = sizeof(ctx->Sp); return &ctx->Sp;
176 case CV_ARM_LR: *size = sizeof(ctx->Lr); return &ctx->Lr;
177 case CV_ARM_PC: *size = sizeof(ctx->Pc); return &ctx->Pc;
178 case CV_ARM_CPSR: *size = sizeof(ctx->Cpsr); return &ctx->Cpsr;
179 }
180 #endif
181 FIXME("Unknown register %x\n", regno);
182 return NULL;
183 }
184
185 static const char* arm_fetch_regname(unsigned regno)
186 {
187 switch (regno)
188 {
189 case CV_ARM_R0 + 0: return "r0";
190 case CV_ARM_R0 + 1: return "r1";
191 case CV_ARM_R0 + 2: return "r2";
192 case CV_ARM_R0 + 3: return "r3";
193 case CV_ARM_R0 + 4: return "r4";
194 case CV_ARM_R0 + 5: return "r5";
195 case CV_ARM_R0 + 6: return "r6";
196 case CV_ARM_R0 + 7: return "r7";
197 case CV_ARM_R0 + 8: return "r8";
198 case CV_ARM_R0 + 9: return "r9";
199 case CV_ARM_R0 + 10: return "r10";
200 case CV_ARM_R0 + 11: return "r11";
201 case CV_ARM_R0 + 12: return "r12";
202
203 case CV_ARM_SP: return "sp";
204 case CV_ARM_LR: return "lr";
205 case CV_ARM_PC: return "pc";
206 case CV_ARM_CPSR: return "cpsr";
207 }
208 FIXME("Unknown register %x\n", regno);
209 return NULL;
210 }
211
212 static BOOL arm_fetch_minidump_thread(struct dump_context* dc, unsigned index, unsigned flags, const CONTEXT* ctx)
213 {
214 if (ctx->ContextFlags && (flags & ThreadWriteInstructionWindow))
215 {
216 /* FIXME: crop values across module boundaries, */
217 #ifdef __arm__
218 ULONG base = ctx->Pc <= 0x80 ? 0 : ctx->Pc - 0x80;
219 minidump_add_memory_block(dc, base, ctx->Pc + 0x80 - base, 0);
220 #endif
221 }
222
223 return TRUE;
224 }
225
226 static BOOL arm_fetch_minidump_module(struct dump_context* dc, unsigned index, unsigned flags)
227 {
228 /* FIXME: actually, we should probably take care of FPO data, unless it's stored in
229 * function table minidump stream
230 */
231 return FALSE;
232 }
233
234 DECLSPEC_HIDDEN struct cpu cpu_arm = {
235 IMAGE_FILE_MACHINE_ARMNT,
236 4,
237 CV_ARM_R0 + 11,
238 arm_get_addr,
239 arm_stack_walk,
240 NULL,
241 arm_map_dwarf_register,
242 arm_fetch_context_reg,
243 arm_fetch_regname,
244 arm_fetch_minidump_thread,
245 arm_fetch_minidump_module,
246 };