dbghelp: In x86-64 support code, fix RUNTIME_FUNCTION dumping.
[wine/multimedia.git] / dlls / dbghelp / cpu_x86_64.c
blob8b101d299502d9feaf2d3a22cfc9ecf4606c480e
1 /*
2 * File cpu_x86_64.c
4 * Copyright (C) 1999, 2005 Alexandre Julliard
5 * Copyright (C) 2009, 2011 Eric Pouech.
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.
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.
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
22 #include <assert.h>
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
26 #include "ntstatus.h"
27 #define WIN32_NO_STATUS
28 #include "dbghelp_private.h"
29 #include "winternl.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
34 /* x86-64 unwind information, for PE modules, as described on MSDN */
36 typedef enum _UNWIND_OP_CODES
38 UWOP_PUSH_NONVOL = 0,
39 UWOP_ALLOC_LARGE,
40 UWOP_ALLOC_SMALL,
41 UWOP_SET_FPREG,
42 UWOP_SAVE_NONVOL,
43 UWOP_SAVE_NONVOL_FAR,
44 UWOP_SAVE_XMM128,
45 UWOP_SAVE_XMM128_FAR,
46 UWOP_PUSH_MACHFRAME
47 } UNWIND_CODE_OPS;
49 typedef union _UNWIND_CODE
51 struct
53 BYTE CodeOffset;
54 BYTE UnwindOp : 4;
55 BYTE OpInfo : 4;
57 USHORT FrameOffset;
58 } UNWIND_CODE, *PUNWIND_CODE;
60 typedef struct _UNWIND_INFO
62 BYTE Version : 3;
63 BYTE Flags : 5;
64 BYTE SizeOfProlog;
65 BYTE CountOfCodes;
66 BYTE FrameRegister : 4;
67 BYTE FrameOffset : 4;
68 UNWIND_CODE UnwindCode[1]; /* actually CountOfCodes (aligned) */
70 * union
71 * {
72 * OPTIONAL ULONG ExceptionHandler;
73 * OPTIONAL ULONG FunctionEntry;
74 * };
75 * OPTIONAL ULONG ExceptionData[];
77 } UNWIND_INFO, *PUNWIND_INFO;
79 #define GetUnwindCodeEntry(info, index) \
80 ((info)->UnwindCode[index])
82 #define GetLanguageSpecificDataPtr(info) \
83 ((PVOID)&GetUnwindCodeEntry((info),((info)->CountOfCodes + 1) & ~1))
85 #define GetExceptionHandler(base, info) \
86 ((PEXCEPTION_HANDLER)((base) + *(PULONG)GetLanguageSpecificDataPtr(info)))
88 #define GetChainedFunctionEntry(base, info) \
89 ((PRUNTIME_FUNCTION)((base) + *(PULONG)GetLanguageSpecificDataPtr(info)))
91 #define GetExceptionDataPtr(info) \
92 ((PVOID)((PULONG)GetLanguageSpecificData(info) + 1)
94 static unsigned x86_64_get_addr(HANDLE hThread, const CONTEXT* ctx,
95 enum cpu_addr ca, ADDRESS64* addr)
97 addr->Mode = AddrModeFlat;
98 switch (ca)
100 #ifdef __x86_64__
101 case cpu_addr_pc: addr->Segment = ctx->SegCs; addr->Offset = ctx->Rip; return TRUE;
102 case cpu_addr_stack: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rsp; return TRUE;
103 case cpu_addr_frame: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rbp; return TRUE;
104 #endif
105 default: addr->Mode = -1;
106 return FALSE;
110 enum st_mode {stm_start, stm_64bit, stm_done};
112 /* indexes in Reserved array */
113 #define __CurrentMode 0
114 #define __CurrentCount 1
115 /* #define __ 2 (unused) */
117 #define curr_mode (frame->Reserved[__CurrentMode])
118 #define curr_count (frame->Reserved[__CurrentCount])
119 /* #define ??? (frame->Reserved[__]) (unused) */
121 #ifdef __x86_64__
122 union handler_data
124 RUNTIME_FUNCTION chain;
125 ULONG handler;
128 static void dump_unwind_info(struct cpu_stack_walk* csw, ULONG64 base, RUNTIME_FUNCTION *function)
130 static const char * const reg_names[16] =
131 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
132 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
134 union handler_data handler_data;
135 char buffer[sizeof(UNWIND_INFO) + 256 * sizeof(UNWIND_CODE)];
136 UNWIND_INFO* info = (UNWIND_INFO*)buffer;
137 unsigned int i, count;
138 RUNTIME_FUNCTION snext;
139 ULONG64 addr;
141 TRACE("**** func %x-%x\n", function->BeginAddress, function->EndAddress);
142 for (;;)
144 if (function->UnwindData & 1)
146 if (!sw_read_mem(csw, base + function->UnwindData, &snext, sizeof(snext)))
148 TRACE("Couldn't unwind RUNTIME_INFO\n");
149 return;
151 TRACE("unwind info for function %p-%p chained to function %p-%p\n",
152 (char*)base + function->BeginAddress, (char*)base + function->EndAddress,
153 (char*)base + snext.BeginAddress, (char*)base + snext.EndAddress);
154 function = &snext;
155 continue;
157 addr = base + function->UnwindData;
158 if (!sw_read_mem(csw, addr, info, FIELD_OFFSET(UNWIND_INFO, UnwindCode)) ||
159 !sw_read_mem(csw, addr + FIELD_OFFSET(UNWIND_INFO, UnwindCode),
160 info->UnwindCode, info->CountOfCodes * sizeof(UNWIND_CODE)))
162 FIXME("couldn't read memory for UNWIND_INFO\n");
163 return;
165 TRACE("unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
166 (char*)addr, info->Flags, info->SizeOfProlog,
167 (char*)base + function->BeginAddress, (char*)base + function->EndAddress);
169 if (info->FrameRegister)
170 TRACE(" frame register %s offset 0x%x(%%rsp)\n",
171 reg_names[info->FrameRegister], info->FrameOffset * 16);
173 for (i = 0; i < info->CountOfCodes; i++)
175 TRACE(" 0x%x: ", info->UnwindCode[i].CodeOffset);
176 switch (info->UnwindCode[i].UnwindOp)
178 case UWOP_PUSH_NONVOL:
179 TRACE("pushq %%%s\n", reg_names[info->UnwindCode[i].OpInfo]);
180 break;
181 case UWOP_ALLOC_LARGE:
182 if (info->UnwindCode[i].OpInfo)
184 count = *(DWORD*)&info->UnwindCode[i+1];
185 i += 2;
187 else
189 count = *(USHORT*)&info->UnwindCode[i+1] * 8;
190 i++;
192 TRACE("subq $0x%x,%%rsp\n", count);
193 break;
194 case UWOP_ALLOC_SMALL:
195 count = (info->UnwindCode[i].OpInfo + 1) * 8;
196 TRACE("subq $0x%x,%%rsp\n", count);
197 break;
198 case UWOP_SET_FPREG:
199 TRACE("leaq 0x%x(%%rsp),%s\n",
200 info->FrameOffset * 16, reg_names[info->FrameRegister]);
201 break;
202 case UWOP_SAVE_NONVOL:
203 count = *(USHORT*)&info->UnwindCode[i+1] * 8;
204 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names[info->UnwindCode[i].OpInfo], count);
205 i++;
206 break;
207 case UWOP_SAVE_NONVOL_FAR:
208 count = *(DWORD*)&info->UnwindCode[i+1];
209 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names[info->UnwindCode[i].OpInfo], count);
210 i += 2;
211 break;
212 case UWOP_SAVE_XMM128:
213 count = *(USHORT*)&info->UnwindCode[i+1] * 16;
214 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info->UnwindCode[i].OpInfo, count);
215 i++;
216 break;
217 case UWOP_SAVE_XMM128_FAR:
218 count = *(DWORD*)&info->UnwindCode[i+1];
219 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info->UnwindCode[i].OpInfo, count);
220 i += 2;
221 break;
222 case UWOP_PUSH_MACHFRAME:
223 TRACE("PUSH_MACHFRAME %u\n", info->UnwindCode[i].OpInfo);
224 break;
225 default:
226 FIXME("unknown code %u\n", info->UnwindCode[i].UnwindOp);
227 break;
231 addr += FIELD_OFFSET(UNWIND_INFO, UnwindCode) +
232 ((info->CountOfCodes + 1) & ~1) * sizeof(UNWIND_CODE);
233 if (info->Flags & UNW_FLAG_CHAININFO)
235 if (!sw_read_mem(csw, addr, &handler_data, sizeof(handler_data.chain)))
237 FIXME("couldn't read memory for handler_data.chain\n");
238 return;
240 TRACE(" chained to function %p-%p\n",
241 (char*)base + handler_data.chain.BeginAddress,
242 (char*)base + handler_data.chain.EndAddress);
243 function = &handler_data.chain;
244 continue;
246 if (info->Flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
248 if (!sw_read_mem(csw, addr, &handler_data, sizeof(handler_data.handler)))
250 FIXME("couldn't read memory for handler_data.handler\n");
251 return;
253 TRACE(" handler %p data at %p\n",
254 (char*)base + handler_data.handler, (char*)addr + sizeof(handler_data.handler));
256 break;
260 /* highly derived from dlls/ntdll/signal_x86_64.c */
261 static ULONG64 get_int_reg(CONTEXT *context, int reg)
263 return *(&context->Rax + reg);
266 static void set_int_reg(CONTEXT *context, int reg, ULONG64 val)
268 *(&context->Rax + reg) = val;
271 static void set_float_reg(CONTEXT *context, int reg, M128A val)
273 *(&context->u.s.Xmm0 + reg) = val;
276 static int get_opcode_size(UNWIND_CODE op)
278 switch (op.UnwindOp)
280 case UWOP_ALLOC_LARGE:
281 return 2 + (op.OpInfo != 0);
282 case UWOP_SAVE_NONVOL:
283 case UWOP_SAVE_XMM128:
284 return 2;
285 case UWOP_SAVE_NONVOL_FAR:
286 case UWOP_SAVE_XMM128_FAR:
287 return 3;
288 default:
289 return 1;
293 static BOOL is_inside_epilog(struct cpu_stack_walk* csw, DWORD64 pc)
295 BYTE op0, op1, op2;
297 if (!sw_read_mem(csw, pc, &op0, 1)) return FALSE;
299 /* add or lea must be the first instruction, and it must have a rex.W prefix */
300 if ((op0 & 0xf8) == 0x48)
302 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
303 switch (op1)
305 case 0x81: /* add $nnnn,%rsp */
306 if (!sw_read_mem(csw, pc + 2, &op2, 1)) return FALSE;
307 if (op0 == 0x48 && op2 == 0xc4)
309 pc += 7;
310 break;
312 return FALSE;
313 case 0x83: /* add $n,%rsp */
314 if (op0 == 0x48 && op2 == 0xc4)
316 pc += 4;
317 break;
319 return FALSE;
320 case 0x8d: /* lea n(reg),%rsp */
321 if (op0 & 0x06) return FALSE; /* rex.RX must be cleared */
322 if (((op2 >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
323 if ((op2 & 7) == 4) return FALSE; /* no SIB byte allowed */
324 if ((op2 >> 6) == 1) /* 8-bit offset */
326 pc += 4;
327 break;
329 if ((op2 >> 6) == 2) /* 32-bit offset */
331 pc += 7;
332 break;
334 return FALSE;
338 /* now check for various pop instructions */
339 for (;;)
341 BYTE rex = 0;
343 if (!sw_read_mem(csw, pc, &op0, 1)) return FALSE;
344 if ((op0 & 0xf0) == 0x40)
346 rex = op0 & 0x0f; /* rex prefix */
347 if (!sw_read_mem(csw, ++pc, &op0, 1)) return FALSE;
350 switch (op0)
352 case 0x58: /* pop %rax/%r8 */
353 case 0x59: /* pop %rcx/%r9 */
354 case 0x5a: /* pop %rdx/%r10 */
355 case 0x5b: /* pop %rbx/%r11 */
356 case 0x5c: /* pop %rsp/%r12 */
357 case 0x5d: /* pop %rbp/%r13 */
358 case 0x5e: /* pop %rsi/%r14 */
359 case 0x5f: /* pop %rdi/%r15 */
360 pc++;
361 continue;
362 case 0xc2: /* ret $nn */
363 case 0xc3: /* ret */
364 return TRUE;
365 /* FIXME: add various jump instructions */
367 return FALSE;
371 static BOOL interpret_epilog(struct cpu_stack_walk* csw, ULONG64 pc, CONTEXT *context )
373 BYTE insn, val8;
374 WORD val16;
375 LONG val32;
376 DWORD64 val64;
378 for (;;)
380 BYTE rex = 0;
382 if (!sw_read_mem(csw, pc, &insn, 1)) return FALSE;
383 if ((insn & 0xf0) == 0x40)
385 rex = insn & 0x0f; /* rex prefix */
386 if (!sw_read_mem(csw, ++pc, &insn, 1)) return FALSE;
389 switch (insn)
391 case 0x58: /* pop %rax/r8 */
392 case 0x59: /* pop %rcx/r9 */
393 case 0x5a: /* pop %rdx/r10 */
394 case 0x5b: /* pop %rbx/r11 */
395 case 0x5c: /* pop %rsp/r12 */
396 case 0x5d: /* pop %rbp/r13 */
397 case 0x5e: /* pop %rsi/r14 */
398 case 0x5f: /* pop %rdi/r15 */
399 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
400 set_int_reg(context, insn - 0x58 + (rex & 1) * 8, val64);
401 context->Rsp += sizeof(ULONG64);
402 pc++;
403 continue;
404 case 0x81: /* add $nnnn,%rsp */
405 if (!sw_read_mem(csw, pc + 2, &val32, sizeof(ULONG))) return FALSE;
406 context->Rsp += (LONG)val32;
407 pc += 2 + sizeof(LONG);
408 continue;
409 case 0x83: /* add $n,%rsp */
410 if (!sw_read_mem(csw, pc + 2, &val8, sizeof(BYTE))) return FALSE;
411 context->Rsp += (signed char)val8;
412 pc += 3;
413 continue;
414 case 0x8d:
415 if (!sw_read_mem(csw, pc + 1, &insn, sizeof(BYTE))) return FALSE;
416 if ((insn >> 6) == 1) /* lea n(reg),%rsp */
418 if (!sw_read_mem(csw, pc + 2, &val8, sizeof(BYTE))) return FALSE;
419 context->Rsp = get_int_reg( context, (insn & 7) + (rex & 1) * 8 ) + (signed char)val8;
420 pc += 3;
422 else /* lea nnnn(reg),%rsp */
424 if (!sw_read_mem(csw, pc + 2, &val32, sizeof(ULONG))) return FALSE;
425 context->Rsp = get_int_reg( context, (insn & 7) + (rex & 1) * 8 ) + (LONG)val32;
426 pc += 2 + sizeof(LONG);
428 continue;
429 case 0xc2: /* ret $nn */
430 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
431 if (!sw_read_mem(csw, pc + 1, &val16, sizeof(WORD))) return FALSE;
432 context->Rip = val64;
433 context->Rsp += sizeof(ULONG64) + val16;
434 return TRUE;
435 case 0xc3: /* ret */
436 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
437 context->Rip = val64;
438 context->Rsp += sizeof(ULONG64);
439 return TRUE;
440 /* FIXME: add various jump instructions */
442 FIXME("unsupported insn %x\n", insn);
443 return FALSE;
447 static BOOL default_unwind(struct cpu_stack_walk* csw, CONTEXT* context)
449 if (!sw_read_mem(csw, context->Rsp, &context->Rip, sizeof(DWORD64)))
451 WARN("Cannot read new frame offset %s\n", wine_dbgstr_longlong(context->Rsp));
452 return FALSE;
454 context->Rsp += sizeof(DWORD64);
455 return TRUE;
458 static BOOL interpret_function_table_entry(struct cpu_stack_walk* csw,
459 CONTEXT* context, RUNTIME_FUNCTION* function, DWORD64 base)
461 char buffer[sizeof(UNWIND_INFO) + 256 * sizeof(UNWIND_CODE)];
462 UNWIND_INFO* info = (UNWIND_INFO*)buffer;
463 unsigned i;
464 DWORD64 newframe, prolog_offset, off, value;
465 M128A floatvalue;
466 union handler_data handler_data;
468 /* FIXME: we have some assumptions here */
469 assert(context);
470 dump_unwind_info(csw, sw_module_base(csw, context->Rip), function);
471 newframe = context->Rsp;
472 for (;;)
474 if (!sw_read_mem(csw, base + function->UnwindData, info, sizeof(*info)) ||
475 !sw_read_mem(csw, base + function->UnwindData + FIELD_OFFSET(UNWIND_INFO, UnwindCode),
476 info->UnwindCode, info->CountOfCodes * sizeof(UNWIND_CODE)))
478 WARN("Couldn't read unwind_code at %lx\n", base + function->UnwindData);
479 return FALSE;
482 if (info->Version != 1)
484 WARN("unknown unwind info version %u at %lx\n", info->Version, base + function->UnwindData);
485 return FALSE;
488 if (info->FrameRegister)
489 newframe = get_int_reg(context, info->FrameRegister) - info->FrameOffset * 16;
491 /* check if in prolog */
492 if (context->Rip >= base + function->BeginAddress &&
493 context->Rip < base + function->BeginAddress + info->SizeOfProlog)
495 prolog_offset = context->Rip - base - function->BeginAddress;
497 else
499 prolog_offset = ~0;
500 if (is_inside_epilog(csw, context->Rip))
502 interpret_epilog(csw, context->Rip, context);
503 return TRUE;
507 for (i = 0; i < info->CountOfCodes; i += get_opcode_size(info->UnwindCode[i]))
509 if (prolog_offset < info->UnwindCode[i].CodeOffset) continue; /* skip it */
511 switch (info->UnwindCode[i].UnwindOp)
513 case UWOP_PUSH_NONVOL: /* pushq %reg */
514 if (!sw_read_mem(csw, context->Rsp, &value, sizeof(DWORD64))) return FALSE;
515 set_int_reg(context, info->UnwindCode[i].OpInfo, value);
516 context->Rsp += sizeof(ULONG64);
517 break;
518 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
519 if (info->UnwindCode[i].OpInfo) context->Rsp += *(DWORD*)&info->UnwindCode[i+1];
520 else context->Rsp += *(USHORT*)&info->UnwindCode[i+1] * 8;
521 break;
522 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
523 context->Rsp += (info->UnwindCode[i].OpInfo + 1) * 8;
524 break;
525 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
526 context->Rsp = newframe;
527 break;
528 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
529 off = newframe + *(USHORT*)&info->UnwindCode[i+1] * 8;
530 if (!sw_read_mem(csw, off, &value, sizeof(DWORD64))) return FALSE;
531 set_int_reg(context, info->UnwindCode[i].OpInfo, value);
532 break;
533 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
534 off = newframe + *(DWORD*)&info->UnwindCode[i+1];
535 if (!sw_read_mem(csw, off, &value, sizeof(DWORD64))) return FALSE;
536 set_int_reg(context, info->UnwindCode[i].OpInfo, value);
537 break;
538 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
539 off = newframe + *(USHORT*)&info->UnwindCode[i+1] * 16;
540 if (!sw_read_mem(csw, off, &floatvalue, sizeof(M128A))) return FALSE;
541 set_float_reg(context, info->UnwindCode[i].OpInfo, floatvalue);
542 break;
543 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
544 off = newframe + *(DWORD*)&info->UnwindCode[i+1];
545 if (!sw_read_mem(csw, off, &floatvalue, sizeof(M128A))) return FALSE;
546 set_float_reg(context, info->UnwindCode[i].OpInfo, floatvalue);
547 break;
548 case UWOP_PUSH_MACHFRAME:
549 FIXME("PUSH_MACHFRAME %u\n", info->UnwindCode[i].OpInfo);
550 break;
551 default:
552 FIXME("unknown code %u\n", info->UnwindCode[i].UnwindOp);
553 break;
556 if (!(info->Flags & UNW_FLAG_CHAININFO)) break;
557 if (!sw_read_mem(csw, base + function->UnwindData + FIELD_OFFSET(UNWIND_INFO, UnwindCode) +
558 ((info->CountOfCodes + 1) & ~1) * sizeof(UNWIND_CODE),
559 &handler_data, sizeof(handler_data))) return FALSE;
560 function = &handler_data.chain; /* restart with the chained info */
562 return default_unwind(csw, context);
565 /* fetch_next_frame()
567 * modify (at least) context.{rip, rsp, rbp} using unwind information
568 * either out of PE exception handlers, debug info (dwarf), or simple stack unwind
570 static BOOL fetch_next_frame(struct cpu_stack_walk* csw, CONTEXT* context,
571 DWORD_PTR curr_pc, void** prtf)
573 DWORD_PTR cfa;
574 RUNTIME_FUNCTION* rtf;
575 DWORD64 base;
577 if (!curr_pc || !(base = sw_module_base(csw, curr_pc))) return FALSE;
578 rtf = sw_table_access(csw, curr_pc);
579 if (prtf) *prtf = rtf;
580 if (rtf)
582 return interpret_function_table_entry(csw, context, rtf, base);
584 else if (dwarf2_virtual_unwind(csw, curr_pc, context, &cfa))
586 context->Rsp = cfa;
587 TRACE("next function rip=%016lx\n", context->Rip);
588 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
589 context->Rax, context->Rbx, context->Rcx, context->Rdx);
590 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
591 context->Rsi, context->Rdi, context->Rbp, context->Rsp);
592 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
593 context->R8, context->R9, context->R10, context->R11);
594 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
595 context->R12, context->R13, context->R14, context->R15);
596 return TRUE;
598 else
599 return default_unwind(csw, context);
602 static BOOL x86_64_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame, CONTEXT* context)
604 unsigned deltapc = curr_count <= 1 ? 0 : 1;
606 /* sanity check */
607 if (curr_mode >= stm_done) return FALSE;
608 assert(!csw->is32);
610 TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s\n",
611 wine_dbgstr_addr(&frame->AddrPC),
612 wine_dbgstr_addr(&frame->AddrFrame),
613 wine_dbgstr_addr(&frame->AddrReturn),
614 wine_dbgstr_addr(&frame->AddrStack),
615 curr_mode == stm_start ? "start" : "64bit",
616 wine_dbgstr_longlong(curr_count));
618 if (curr_mode == stm_start)
620 if ((frame->AddrPC.Mode == AddrModeFlat) &&
621 (frame->AddrFrame.Mode != AddrModeFlat))
623 WARN("Bad AddrPC.Mode / AddrFrame.Mode combination\n");
624 goto done_err;
627 /* Init done */
628 curr_mode = stm_64bit;
629 frame->AddrReturn.Mode = frame->AddrStack.Mode = AddrModeFlat;
630 /* don't set up AddrStack on first call. Either the caller has set it up, or
631 * we will get it in the next frame
633 memset(&frame->AddrBStore, 0, sizeof(frame->AddrBStore));
635 else
637 if (context->Rsp != frame->AddrStack.Offset) FIXME("inconsistent Stack Pointer\n");
638 if (context->Rip != frame->AddrPC.Offset) FIXME("inconsistent Instruction Pointer\n");
640 if (frame->AddrReturn.Offset == 0) goto done_err;
641 if (!fetch_next_frame(csw, context, frame->AddrPC.Offset - deltapc, &frame->FuncTableEntry))
642 goto done_err;
643 deltapc = 1;
646 memset(&frame->Params, 0, sizeof(frame->Params));
648 /* set frame information */
649 frame->AddrStack.Offset = context->Rsp;
650 frame->AddrFrame.Offset = context->Rbp;
651 frame->AddrPC.Offset = context->Rip;
652 if (1)
654 CONTEXT newctx = *context;
656 if (!fetch_next_frame(csw, &newctx, frame->AddrPC.Offset - deltapc, NULL))
657 goto done_err;
658 frame->AddrReturn.Mode = AddrModeFlat;
659 frame->AddrReturn.Offset = newctx.Rip;
662 frame->Far = TRUE;
663 frame->Virtual = TRUE;
664 curr_count++;
666 TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s FuncTable=%p\n",
667 wine_dbgstr_addr(&frame->AddrPC),
668 wine_dbgstr_addr(&frame->AddrFrame),
669 wine_dbgstr_addr(&frame->AddrReturn),
670 wine_dbgstr_addr(&frame->AddrStack),
671 curr_mode == stm_start ? "start" : "64bit",
672 wine_dbgstr_longlong(curr_count),
673 frame->FuncTableEntry);
675 return TRUE;
676 done_err:
677 curr_mode = stm_done;
678 return FALSE;
680 #else
681 static BOOL x86_64_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame, CONTEXT* context)
683 return FALSE;
685 #endif
687 static void* x86_64_find_runtime_function(struct module* module, DWORD64 addr)
689 #ifdef __x86_64__
690 RUNTIME_FUNCTION* rtf;
691 ULONG size;
692 int min, max;
694 rtf = (RUNTIME_FUNCTION*)pe_map_directory(module, IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size);
695 if (rtf) for (min = 0, max = size / sizeof(*rtf); min <= max; )
697 int pos = (min + max) / 2;
698 if (addr < module->module.BaseOfImage + rtf[pos].BeginAddress) max = pos - 1;
699 else if (addr >= module->module.BaseOfImage + rtf[pos].EndAddress) min = pos + 1;
700 else
702 rtf += pos;
703 while (rtf->UnwindData & 1) /* follow chained entry */
705 FIXME("RunTime_Function outside IMAGE_DIRECTORY_ENTRY_EXCEPTION unimplemented yet!\n");
706 /* we need to read into the other process */
707 /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
709 return rtf;
712 #endif
713 return NULL;
716 static unsigned x86_64_map_dwarf_register(unsigned regno)
718 unsigned reg;
720 if (regno >= 17 && regno <= 24)
721 reg = CV_AMD64_XMM0 + regno - 17;
722 else if (regno >= 25 && regno <= 32)
723 reg = CV_AMD64_XMM8 + regno - 25;
724 else if (regno >= 33 && regno <= 40)
725 reg = CV_AMD64_ST0 + regno - 33;
726 else switch (regno)
728 case 0: reg = CV_AMD64_RAX; break;
729 case 1: reg = CV_AMD64_RDX; break;
730 case 2: reg = CV_AMD64_RCX; break;
731 case 3: reg = CV_AMD64_RBX; break;
732 case 4: reg = CV_AMD64_RSI; break;
733 case 5: reg = CV_AMD64_RDI; break;
734 case 6: reg = CV_AMD64_RBP; break;
735 case 7: reg = CV_AMD64_RSP; break;
736 case 8: reg = CV_AMD64_R8; break;
737 case 9: reg = CV_AMD64_R9; break;
738 case 10: reg = CV_AMD64_R10; break;
739 case 11: reg = CV_AMD64_R11; break;
740 case 12: reg = CV_AMD64_R12; break;
741 case 13: reg = CV_AMD64_R13; break;
742 case 14: reg = CV_AMD64_R14; break;
743 case 15: reg = CV_AMD64_R15; break;
744 case 16: reg = CV_AMD64_RIP; break;
745 case 49: reg = CV_AMD64_EFLAGS; break;
746 case 50: reg = CV_AMD64_ES; break;
747 case 51: reg = CV_AMD64_CS; break;
748 case 52: reg = CV_AMD64_SS; break;
749 case 53: reg = CV_AMD64_DS; break;
750 case 54: reg = CV_AMD64_FS; break;
751 case 55: reg = CV_AMD64_GS; break;
752 case 62: reg = CV_AMD64_TR; break;
753 case 63: reg = CV_AMD64_LDTR; break;
754 case 64: reg = CV_AMD64_MXCSR; break;
755 case 65: reg = CV_AMD64_CTRL; break;
756 case 66: reg = CV_AMD64_STAT; break;
758 * 56-57 reserved
759 * 58 %fs.base
760 * 59 %gs.base
761 * 60-61 reserved
763 default:
764 FIXME("Don't know how to map register %d\n", regno);
765 return 0;
767 return reg;
770 static void* x86_64_fetch_context_reg(CONTEXT* ctx, unsigned regno, unsigned* size)
772 #ifdef __x86_64__
773 switch (regno)
775 case CV_AMD64_RAX: *size = sizeof(ctx->Rax); return &ctx->Rax;
776 case CV_AMD64_RDX: *size = sizeof(ctx->Rdx); return &ctx->Rdx;
777 case CV_AMD64_RCX: *size = sizeof(ctx->Rcx); return &ctx->Rcx;
778 case CV_AMD64_RBX: *size = sizeof(ctx->Rbx); return &ctx->Rbx;
779 case CV_AMD64_RSI: *size = sizeof(ctx->Rsi); return &ctx->Rsi;
780 case CV_AMD64_RDI: *size = sizeof(ctx->Rdi); return &ctx->Rdi;
781 case CV_AMD64_RBP: *size = sizeof(ctx->Rbp); return &ctx->Rbp;
782 case CV_AMD64_RSP: *size = sizeof(ctx->Rsp); return &ctx->Rsp;
783 case CV_AMD64_R8: *size = sizeof(ctx->R8); return &ctx->R8;
784 case CV_AMD64_R9: *size = sizeof(ctx->R9); return &ctx->R9;
785 case CV_AMD64_R10: *size = sizeof(ctx->R10); return &ctx->R10;
786 case CV_AMD64_R11: *size = sizeof(ctx->R11); return &ctx->R11;
787 case CV_AMD64_R12: *size = sizeof(ctx->R12); return &ctx->R12;
788 case CV_AMD64_R13: *size = sizeof(ctx->R13); return &ctx->R13;
789 case CV_AMD64_R14: *size = sizeof(ctx->R14); return &ctx->R14;
790 case CV_AMD64_R15: *size = sizeof(ctx->R15); return &ctx->R15;
791 case CV_AMD64_RIP: *size = sizeof(ctx->Rip); return &ctx->Rip;
793 case CV_AMD64_XMM0 + 0: *size = sizeof(ctx->u.s.Xmm0 ); return &ctx->u.s.Xmm0;
794 case CV_AMD64_XMM0 + 1: *size = sizeof(ctx->u.s.Xmm1 ); return &ctx->u.s.Xmm1;
795 case CV_AMD64_XMM0 + 2: *size = sizeof(ctx->u.s.Xmm2 ); return &ctx->u.s.Xmm2;
796 case CV_AMD64_XMM0 + 3: *size = sizeof(ctx->u.s.Xmm3 ); return &ctx->u.s.Xmm3;
797 case CV_AMD64_XMM0 + 4: *size = sizeof(ctx->u.s.Xmm4 ); return &ctx->u.s.Xmm4;
798 case CV_AMD64_XMM0 + 5: *size = sizeof(ctx->u.s.Xmm5 ); return &ctx->u.s.Xmm5;
799 case CV_AMD64_XMM0 + 6: *size = sizeof(ctx->u.s.Xmm6 ); return &ctx->u.s.Xmm6;
800 case CV_AMD64_XMM0 + 7: *size = sizeof(ctx->u.s.Xmm7 ); return &ctx->u.s.Xmm7;
801 case CV_AMD64_XMM8 + 0: *size = sizeof(ctx->u.s.Xmm8 ); return &ctx->u.s.Xmm8;
802 case CV_AMD64_XMM8 + 1: *size = sizeof(ctx->u.s.Xmm9 ); return &ctx->u.s.Xmm9;
803 case CV_AMD64_XMM8 + 2: *size = sizeof(ctx->u.s.Xmm10); return &ctx->u.s.Xmm10;
804 case CV_AMD64_XMM8 + 3: *size = sizeof(ctx->u.s.Xmm11); return &ctx->u.s.Xmm11;
805 case CV_AMD64_XMM8 + 4: *size = sizeof(ctx->u.s.Xmm12); return &ctx->u.s.Xmm12;
806 case CV_AMD64_XMM8 + 5: *size = sizeof(ctx->u.s.Xmm13); return &ctx->u.s.Xmm13;
807 case CV_AMD64_XMM8 + 6: *size = sizeof(ctx->u.s.Xmm14); return &ctx->u.s.Xmm14;
808 case CV_AMD64_XMM8 + 7: *size = sizeof(ctx->u.s.Xmm15); return &ctx->u.s.Xmm15;
810 case CV_AMD64_ST0 + 0: *size = sizeof(ctx->u.s.Legacy[0]); return &ctx->u.s.Legacy[0];
811 case CV_AMD64_ST0 + 1: *size = sizeof(ctx->u.s.Legacy[1]); return &ctx->u.s.Legacy[1];
812 case CV_AMD64_ST0 + 2: *size = sizeof(ctx->u.s.Legacy[2]); return &ctx->u.s.Legacy[2];
813 case CV_AMD64_ST0 + 3: *size = sizeof(ctx->u.s.Legacy[3]); return &ctx->u.s.Legacy[3];
814 case CV_AMD64_ST0 + 4: *size = sizeof(ctx->u.s.Legacy[4]); return &ctx->u.s.Legacy[4];
815 case CV_AMD64_ST0 + 5: *size = sizeof(ctx->u.s.Legacy[5]); return &ctx->u.s.Legacy[5];
816 case CV_AMD64_ST0 + 6: *size = sizeof(ctx->u.s.Legacy[6]); return &ctx->u.s.Legacy[6];
817 case CV_AMD64_ST0 + 7: *size = sizeof(ctx->u.s.Legacy[7]); return &ctx->u.s.Legacy[7];
819 case CV_AMD64_EFLAGS: *size = sizeof(ctx->EFlags); return &ctx->EFlags;
820 case CV_AMD64_ES: *size = sizeof(ctx->SegEs); return &ctx->SegEs;
821 case CV_AMD64_CS: *size = sizeof(ctx->SegCs); return &ctx->SegCs;
822 case CV_AMD64_SS: *size = sizeof(ctx->SegSs); return &ctx->SegSs;
823 case CV_AMD64_DS: *size = sizeof(ctx->SegDs); return &ctx->SegDs;
824 case CV_AMD64_FS: *size = sizeof(ctx->SegFs); return &ctx->SegFs;
825 case CV_AMD64_GS: *size = sizeof(ctx->SegGs); return &ctx->SegGs;
828 #endif
829 FIXME("Unknown register %x\n", regno);
830 return NULL;
833 static const char* x86_64_fetch_regname(unsigned regno)
835 switch (regno)
837 case CV_AMD64_RAX: return "rax";
838 case CV_AMD64_RDX: return "rdx";
839 case CV_AMD64_RCX: return "rcx";
840 case CV_AMD64_RBX: return "rbx";
841 case CV_AMD64_RSI: return "rsi";
842 case CV_AMD64_RDI: return "rdi";
843 case CV_AMD64_RBP: return "rbp";
844 case CV_AMD64_RSP: return "rsp";
845 case CV_AMD64_R8: return "r8";
846 case CV_AMD64_R9: return "r9";
847 case CV_AMD64_R10: return "r10";
848 case CV_AMD64_R11: return "r11";
849 case CV_AMD64_R12: return "r12";
850 case CV_AMD64_R13: return "r13";
851 case CV_AMD64_R14: return "r14";
852 case CV_AMD64_R15: return "r15";
853 case CV_AMD64_RIP: return "rip";
855 case CV_AMD64_XMM0 + 0: return "xmm0";
856 case CV_AMD64_XMM0 + 1: return "xmm1";
857 case CV_AMD64_XMM0 + 2: return "xmm2";
858 case CV_AMD64_XMM0 + 3: return "xmm3";
859 case CV_AMD64_XMM0 + 4: return "xmm4";
860 case CV_AMD64_XMM0 + 5: return "xmm5";
861 case CV_AMD64_XMM0 + 6: return "xmm6";
862 case CV_AMD64_XMM0 + 7: return "xmm7";
863 case CV_AMD64_XMM8 + 0: return "xmm8";
864 case CV_AMD64_XMM8 + 1: return "xmm9";
865 case CV_AMD64_XMM8 + 2: return "xmm10";
866 case CV_AMD64_XMM8 + 3: return "xmm11";
867 case CV_AMD64_XMM8 + 4: return "xmm12";
868 case CV_AMD64_XMM8 + 5: return "xmm13";
869 case CV_AMD64_XMM8 + 6: return "xmm14";
870 case CV_AMD64_XMM8 + 7: return "xmm15";
872 case CV_AMD64_ST0 + 0: return "st0";
873 case CV_AMD64_ST0 + 1: return "st1";
874 case CV_AMD64_ST0 + 2: return "st2";
875 case CV_AMD64_ST0 + 3: return "st3";
876 case CV_AMD64_ST0 + 4: return "st4";
877 case CV_AMD64_ST0 + 5: return "st5";
878 case CV_AMD64_ST0 + 6: return "st6";
879 case CV_AMD64_ST0 + 7: return "st7";
881 case CV_AMD64_EFLAGS: return "eflags";
882 case CV_AMD64_ES: return "es";
883 case CV_AMD64_CS: return "cs";
884 case CV_AMD64_SS: return "ss";
885 case CV_AMD64_DS: return "ds";
886 case CV_AMD64_FS: return "fs";
887 case CV_AMD64_GS: return "gs";
889 FIXME("Unknown register %x\n", regno);
890 return NULL;
893 DECLSPEC_HIDDEN struct cpu cpu_x86_64 = {
894 IMAGE_FILE_MACHINE_AMD64,
896 CV_AMD64_RSP,
897 x86_64_get_addr,
898 x86_64_stack_walk,
899 x86_64_find_runtime_function,
900 x86_64_map_dwarf_register,
901 x86_64_fetch_context_reg,
902 x86_64_fetch_regname,