riched20: Enable and show the scrollbar before updating its position and range.
[wine.git] / dlls / dbghelp / cpu_x86_64.c
blob8e8ac85dd2f696a2a92f398246658dc040227fc5
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_EPILOG,
45 UWOP_SAVE_XMM128 = 8,
46 UWOP_SAVE_XMM128_FAR,
47 UWOP_PUSH_MACHFRAME
48 } UNWIND_CODE_OPS;
50 typedef union _UNWIND_CODE
52 struct
54 BYTE CodeOffset;
55 BYTE UnwindOp : 4;
56 BYTE OpInfo : 4;
57 } u;
58 USHORT FrameOffset;
59 } UNWIND_CODE, *PUNWIND_CODE;
61 typedef struct _UNWIND_INFO
63 BYTE Version : 3;
64 BYTE Flags : 5;
65 BYTE SizeOfProlog;
66 BYTE CountOfCodes;
67 BYTE FrameRegister : 4;
68 BYTE FrameOffset : 4;
69 UNWIND_CODE UnwindCode[1]; /* actually CountOfCodes (aligned) */
71 * union
72 * {
73 * OPTIONAL ULONG ExceptionHandler;
74 * OPTIONAL ULONG FunctionEntry;
75 * };
76 * OPTIONAL ULONG ExceptionData[];
78 } UNWIND_INFO, *PUNWIND_INFO;
80 static BOOL x86_64_get_addr(HANDLE hThread, const CONTEXT* ctx,
81 enum cpu_addr ca, ADDRESS64* addr)
83 addr->Mode = AddrModeFlat;
84 switch (ca)
86 #ifdef __x86_64__
87 case cpu_addr_pc: addr->Segment = ctx->SegCs; addr->Offset = ctx->Rip; return TRUE;
88 case cpu_addr_stack: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rsp; return TRUE;
89 case cpu_addr_frame: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rbp; return TRUE;
90 #endif
91 default: addr->Mode = -1;
92 return FALSE;
96 #ifdef __x86_64__
98 enum st_mode {stm_start, stm_64bit, stm_done};
100 /* indexes in Reserved array */
101 #define __CurrentMode 0
102 #define __CurrentCount 1
103 /* #define __ 2 (unused) */
105 #define curr_mode (frame->Reserved[__CurrentMode])
106 #define curr_count (frame->Reserved[__CurrentCount])
107 /* #define ??? (frame->Reserved[__]) (unused) */
109 union handler_data
111 RUNTIME_FUNCTION chain;
112 ULONG handler;
115 static void dump_unwind_info(struct cpu_stack_walk* csw, ULONG64 base, RUNTIME_FUNCTION *function)
117 static const char * const reg_names[16] =
118 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
119 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
121 union handler_data handler_data;
122 char buffer[sizeof(UNWIND_INFO) + 256 * sizeof(UNWIND_CODE)];
123 UNWIND_INFO* info = (UNWIND_INFO*)buffer;
124 unsigned int i, count;
125 RUNTIME_FUNCTION snext;
126 ULONG64 addr;
128 TRACE("**** func %x-%x\n", function->BeginAddress, function->EndAddress);
129 for (;;)
131 if (function->UnwindData & 1)
133 if (!sw_read_mem(csw, base + function->UnwindData, &snext, sizeof(snext)))
135 TRACE("Couldn't unwind RUNTIME_INFO at %lx\n", base + function->UnwindData);
136 return;
138 TRACE("unwind info for function %p-%p chained to function %p-%p\n",
139 (char*)base + function->BeginAddress, (char*)base + function->EndAddress,
140 (char*)base + snext.BeginAddress, (char*)base + snext.EndAddress);
141 function = &snext;
142 continue;
144 addr = base + function->UnwindData;
145 if (!sw_read_mem(csw, addr, info, FIELD_OFFSET(UNWIND_INFO, UnwindCode)) ||
146 !sw_read_mem(csw, addr + FIELD_OFFSET(UNWIND_INFO, UnwindCode),
147 info->UnwindCode, info->CountOfCodes * sizeof(UNWIND_CODE)))
149 FIXME("couldn't read memory for UNWIND_INFO at %lx\n", addr);
150 return;
152 TRACE("unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
153 (char*)addr, info->Flags, info->SizeOfProlog,
154 (char*)base + function->BeginAddress, (char*)base + function->EndAddress);
156 if (info->FrameRegister)
157 TRACE(" frame register %s offset 0x%x(%%rsp)\n",
158 reg_names[info->FrameRegister], info->FrameOffset * 16);
160 for (i = 0; i < info->CountOfCodes; i++)
162 TRACE(" 0x%x: ", info->UnwindCode[i].u.CodeOffset);
163 switch (info->UnwindCode[i].u.UnwindOp)
165 case UWOP_PUSH_NONVOL:
166 TRACE("pushq %%%s\n", reg_names[info->UnwindCode[i].u.OpInfo]);
167 break;
168 case UWOP_ALLOC_LARGE:
169 if (info->UnwindCode[i].u.OpInfo)
171 count = *(DWORD*)&info->UnwindCode[i+1];
172 i += 2;
174 else
176 count = *(USHORT*)&info->UnwindCode[i+1] * 8;
177 i++;
179 TRACE("subq $0x%x,%%rsp\n", count);
180 break;
181 case UWOP_ALLOC_SMALL:
182 count = (info->UnwindCode[i].u.OpInfo + 1) * 8;
183 TRACE("subq $0x%x,%%rsp\n", count);
184 break;
185 case UWOP_SET_FPREG:
186 TRACE("leaq 0x%x(%%rsp),%s\n",
187 info->FrameOffset * 16, reg_names[info->FrameRegister]);
188 break;
189 case UWOP_SAVE_NONVOL:
190 count = *(USHORT*)&info->UnwindCode[i+1] * 8;
191 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names[info->UnwindCode[i].u.OpInfo], count);
192 i++;
193 break;
194 case UWOP_SAVE_NONVOL_FAR:
195 count = *(DWORD*)&info->UnwindCode[i+1];
196 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names[info->UnwindCode[i].u.OpInfo], count);
197 i += 2;
198 break;
199 case UWOP_SAVE_XMM128:
200 count = *(USHORT*)&info->UnwindCode[i+1] * 16;
201 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info->UnwindCode[i].u.OpInfo, count);
202 i++;
203 break;
204 case UWOP_SAVE_XMM128_FAR:
205 count = *(DWORD*)&info->UnwindCode[i+1];
206 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info->UnwindCode[i].u.OpInfo, count);
207 i += 2;
208 break;
209 case UWOP_PUSH_MACHFRAME:
210 TRACE("PUSH_MACHFRAME %u\n", info->UnwindCode[i].u.OpInfo);
211 break;
212 default:
213 FIXME("unknown code %u\n", info->UnwindCode[i].u.UnwindOp);
214 break;
218 addr += FIELD_OFFSET(UNWIND_INFO, UnwindCode) +
219 ((info->CountOfCodes + 1) & ~1) * sizeof(UNWIND_CODE);
220 if (info->Flags & UNW_FLAG_CHAININFO)
222 if (!sw_read_mem(csw, addr, &handler_data, sizeof(handler_data.chain)))
224 FIXME("couldn't read memory for handler_data.chain\n");
225 return;
227 TRACE(" chained to function %p-%p\n",
228 (char*)base + handler_data.chain.BeginAddress,
229 (char*)base + handler_data.chain.EndAddress);
230 function = &handler_data.chain;
231 continue;
233 if (info->Flags & (UNW_FLAG_EHANDLER | UNW_FLAG_UHANDLER))
235 if (!sw_read_mem(csw, addr, &handler_data, sizeof(handler_data.handler)))
237 FIXME("couldn't read memory for handler_data.handler\n");
238 return;
240 TRACE(" handler %p data at %p\n",
241 (char*)base + handler_data.handler, (char*)addr + sizeof(handler_data.handler));
243 break;
247 /* highly derived from dlls/ntdll/signal_x86_64.c */
248 static ULONG64 get_int_reg(CONTEXT *context, int reg)
250 return *(&context->Rax + reg);
253 static void set_int_reg(CONTEXT *context, int reg, ULONG64 val)
255 *(&context->Rax + reg) = val;
258 static void set_float_reg(CONTEXT *context, int reg, M128A val)
260 *(&context->u.s.Xmm0 + reg) = val;
263 static int get_opcode_size(UNWIND_CODE op)
265 switch (op.u.UnwindOp)
267 case UWOP_ALLOC_LARGE:
268 return 2 + (op.u.OpInfo != 0);
269 case UWOP_SAVE_NONVOL:
270 case UWOP_SAVE_XMM128:
271 return 2;
272 case UWOP_SAVE_NONVOL_FAR:
273 case UWOP_SAVE_XMM128_FAR:
274 return 3;
275 default:
276 return 1;
280 static BOOL is_inside_epilog(struct cpu_stack_walk* csw, DWORD64 pc,
281 DWORD64 base, const RUNTIME_FUNCTION *function )
283 BYTE op0, op1, op2;
284 LONG val32;
286 if (!sw_read_mem(csw, pc, &op0, 1)) return FALSE;
288 /* add or lea must be the first instruction, and it must have a rex.W prefix */
289 if ((op0 & 0xf8) == 0x48)
291 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
292 switch (op1)
294 case 0x81: /* add $nnnn,%rsp */
295 if (!sw_read_mem(csw, pc + 2, &op2, 1)) return FALSE;
296 if (op0 == 0x48 && op2 == 0xc4)
298 pc += 7;
299 break;
301 return FALSE;
302 case 0x83: /* add $n,%rsp */
303 if (!sw_read_mem(csw, pc + 2, &op2, 1)) return FALSE;
304 if (op0 == 0x48 && op2 == 0xc4)
306 pc += 4;
307 break;
309 return FALSE;
310 case 0x8d: /* lea n(reg),%rsp */
311 if (!sw_read_mem(csw, pc + 2, &op2, 1)) return FALSE;
312 if (op0 & 0x06) return FALSE; /* rex.RX must be cleared */
313 if (((op2 >> 3) & 7) != 4) return FALSE; /* dest reg mus be %rsp */
314 if ((op2 & 7) == 4) return FALSE; /* no SIB byte allowed */
315 if ((op2 >> 6) == 1) /* 8-bit offset */
317 pc += 4;
318 break;
320 if ((op2 >> 6) == 2) /* 32-bit offset */
322 pc += 7;
323 break;
325 return FALSE;
329 /* now check for various pop instructions */
330 for (;;)
332 if (!sw_read_mem(csw, pc, &op0, 1)) return FALSE;
333 if ((op0 & 0xf0) == 0x40) /* rex prefix */
335 if (!sw_read_mem(csw, ++pc, &op0, 1)) return FALSE;
338 switch (op0)
340 case 0x58: /* pop %rax/%r8 */
341 case 0x59: /* pop %rcx/%r9 */
342 case 0x5a: /* pop %rdx/%r10 */
343 case 0x5b: /* pop %rbx/%r11 */
344 case 0x5c: /* pop %rsp/%r12 */
345 case 0x5d: /* pop %rbp/%r13 */
346 case 0x5e: /* pop %rsi/%r14 */
347 case 0x5f: /* pop %rdi/%r15 */
348 pc++;
349 continue;
350 case 0xc2: /* ret $nn */
351 case 0xc3: /* ret */
352 return TRUE;
353 case 0xe9: /* jmp nnnn */
354 if (!sw_read_mem(csw, pc + 1, &val32, sizeof(LONG))) return FALSE;
355 pc += 5 + val32;
356 if (pc - base >= function->BeginAddress && pc - base < function->EndAddress)
357 continue;
358 break;
359 case 0xeb: /* jmp n */
360 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
361 pc += 2 + (signed char)op1;
362 if (pc - base >= function->BeginAddress && pc - base < function->EndAddress)
363 continue;
364 break;
365 case 0xf3: /* rep; ret (for amd64 prediction bug) */
366 if (!sw_read_mem(csw, pc + 1, &op1, 1)) return FALSE;
367 return op1 == 0xc3;
369 return FALSE;
373 static BOOL interpret_epilog(struct cpu_stack_walk* csw, ULONG64 pc, CONTEXT *context )
375 BYTE insn, val8;
376 WORD val16;
377 LONG val32;
378 DWORD64 val64;
380 for (;;)
382 BYTE rex = 0;
384 if (!sw_read_mem(csw, pc, &insn, 1)) return FALSE;
385 if ((insn & 0xf0) == 0x40)
387 rex = insn & 0x0f; /* rex prefix */
388 if (!sw_read_mem(csw, ++pc, &insn, 1)) return FALSE;
391 switch (insn)
393 case 0x58: /* pop %rax/r8 */
394 case 0x59: /* pop %rcx/r9 */
395 case 0x5a: /* pop %rdx/r10 */
396 case 0x5b: /* pop %rbx/r11 */
397 case 0x5c: /* pop %rsp/r12 */
398 case 0x5d: /* pop %rbp/r13 */
399 case 0x5e: /* pop %rsi/r14 */
400 case 0x5f: /* pop %rdi/r15 */
401 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
402 set_int_reg(context, insn - 0x58 + (rex & 1) * 8, val64);
403 context->Rsp += sizeof(ULONG64);
404 pc++;
405 continue;
406 case 0x81: /* add $nnnn,%rsp */
407 if (!sw_read_mem(csw, pc + 2, &val32, sizeof(LONG))) return FALSE;
408 context->Rsp += val32;
409 pc += 2 + sizeof(LONG);
410 continue;
411 case 0x83: /* add $n,%rsp */
412 if (!sw_read_mem(csw, pc + 2, &val8, sizeof(BYTE))) return FALSE;
413 context->Rsp += (signed char)val8;
414 pc += 3;
415 continue;
416 case 0x8d:
417 if (!sw_read_mem(csw, pc + 1, &insn, sizeof(BYTE))) return FALSE;
418 if ((insn >> 6) == 1) /* lea n(reg),%rsp */
420 if (!sw_read_mem(csw, pc + 2, &val8, sizeof(BYTE))) return FALSE;
421 context->Rsp = get_int_reg( context, (insn & 7) + (rex & 1) * 8 ) + (signed char)val8;
422 pc += 3;
424 else /* lea nnnn(reg),%rsp */
426 if (!sw_read_mem(csw, pc + 2, &val32, sizeof(LONG))) return FALSE;
427 context->Rsp = get_int_reg( context, (insn & 7) + (rex & 1) * 8 ) + val32;
428 pc += 2 + sizeof(LONG);
430 continue;
431 case 0xc2: /* ret $nn */
432 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
433 if (!sw_read_mem(csw, pc + 1, &val16, sizeof(WORD))) return FALSE;
434 context->Rip = val64;
435 context->Rsp += sizeof(ULONG64) + val16;
436 return TRUE;
437 case 0xc3: /* ret */
438 case 0xf3: /* rep; ret */
439 if (!sw_read_mem(csw, context->Rsp, &val64, sizeof(DWORD64))) return FALSE;
440 context->Rip = val64;
441 context->Rsp += sizeof(ULONG64);
442 return TRUE;
443 case 0xe9: /* jmp nnnn */
444 if (!sw_read_mem(csw, pc + 1, &val32, sizeof(LONG))) return FALSE;
445 pc += 5 + val32;
446 continue;
447 case 0xeb: /* jmp n */
448 if (!sw_read_mem(csw, pc + 1, &val8, sizeof(BYTE))) return FALSE;
449 pc += 2 + (signed char)val8;
450 continue;
452 FIXME("unsupported insn %x\n", insn);
453 return FALSE;
457 static BOOL default_unwind(struct cpu_stack_walk* csw, CONTEXT* context)
459 if (!sw_read_mem(csw, context->Rsp, &context->Rip, sizeof(DWORD64)))
461 WARN("Cannot read new frame offset %s\n", wine_dbgstr_longlong(context->Rsp));
462 return FALSE;
464 context->Rsp += sizeof(DWORD64);
465 return TRUE;
468 static BOOL interpret_function_table_entry(struct cpu_stack_walk* csw,
469 CONTEXT* context, RUNTIME_FUNCTION* function, DWORD64 base)
471 char buffer[sizeof(UNWIND_INFO) + 256 * sizeof(UNWIND_CODE)];
472 UNWIND_INFO* info = (UNWIND_INFO*)buffer;
473 unsigned i;
474 DWORD64 newframe, prolog_offset, off, value;
475 M128A floatvalue;
476 union handler_data handler_data;
477 BOOL mach_frame = FALSE;
479 /* FIXME: we have some assumptions here */
480 assert(context);
481 dump_unwind_info(csw, sw_module_base(csw, context->Rip), function);
482 newframe = context->Rsp;
483 for (;;)
485 if (!sw_read_mem(csw, base + function->UnwindData, info, sizeof(*info)) ||
486 !sw_read_mem(csw, base + function->UnwindData + FIELD_OFFSET(UNWIND_INFO, UnwindCode),
487 info->UnwindCode, info->CountOfCodes * sizeof(UNWIND_CODE)))
489 WARN("Couldn't read unwind_code at %lx\n", base + function->UnwindData);
490 return FALSE;
493 if (info->Version != 1)
495 WARN("unknown unwind info version %u at %lx\n", info->Version, base + function->UnwindData);
496 return FALSE;
499 if (info->FrameRegister)
500 newframe = get_int_reg(context, info->FrameRegister) - info->FrameOffset * 16;
502 /* check if in prolog */
503 if (context->Rip >= base + function->BeginAddress &&
504 context->Rip < base + function->BeginAddress + info->SizeOfProlog)
506 prolog_offset = context->Rip - base - function->BeginAddress;
508 else
510 prolog_offset = ~0;
511 if (is_inside_epilog(csw, context->Rip, base, function))
513 interpret_epilog(csw, context->Rip, context);
514 return TRUE;
518 for (i = 0; i < info->CountOfCodes; i += get_opcode_size(info->UnwindCode[i]))
520 if (prolog_offset < info->UnwindCode[i].u.CodeOffset) continue; /* skip it */
522 switch (info->UnwindCode[i].u.UnwindOp)
524 case UWOP_PUSH_NONVOL: /* pushq %reg */
525 if (!sw_read_mem(csw, context->Rsp, &value, sizeof(DWORD64))) return FALSE;
526 set_int_reg(context, info->UnwindCode[i].u.OpInfo, value);
527 context->Rsp += sizeof(ULONG64);
528 break;
529 case UWOP_ALLOC_LARGE: /* subq $nn,%rsp */
530 if (info->UnwindCode[i].u.OpInfo) context->Rsp += *(DWORD*)&info->UnwindCode[i+1];
531 else context->Rsp += *(USHORT*)&info->UnwindCode[i+1] * 8;
532 break;
533 case UWOP_ALLOC_SMALL: /* subq $n,%rsp */
534 context->Rsp += (info->UnwindCode[i].u.OpInfo + 1) * 8;
535 break;
536 case UWOP_SET_FPREG: /* leaq nn(%rsp),%framereg */
537 context->Rsp = newframe;
538 break;
539 case UWOP_SAVE_NONVOL: /* movq %reg,n(%rsp) */
540 off = newframe + *(USHORT*)&info->UnwindCode[i+1] * 8;
541 if (!sw_read_mem(csw, off, &value, sizeof(DWORD64))) return FALSE;
542 set_int_reg(context, info->UnwindCode[i].u.OpInfo, value);
543 break;
544 case UWOP_SAVE_NONVOL_FAR: /* movq %reg,nn(%rsp) */
545 off = newframe + *(DWORD*)&info->UnwindCode[i+1];
546 if (!sw_read_mem(csw, off, &value, sizeof(DWORD64))) return FALSE;
547 set_int_reg(context, info->UnwindCode[i].u.OpInfo, value);
548 break;
549 case UWOP_SAVE_XMM128: /* movaps %xmmreg,n(%rsp) */
550 off = newframe + *(USHORT*)&info->UnwindCode[i+1] * 16;
551 if (!sw_read_mem(csw, off, &floatvalue, sizeof(M128A))) return FALSE;
552 set_float_reg(context, info->UnwindCode[i].u.OpInfo, floatvalue);
553 break;
554 case UWOP_SAVE_XMM128_FAR: /* movaps %xmmreg,nn(%rsp) */
555 off = newframe + *(DWORD*)&info->UnwindCode[i+1];
556 if (!sw_read_mem(csw, off, &floatvalue, sizeof(M128A))) return FALSE;
557 set_float_reg(context, info->UnwindCode[i].u.OpInfo, floatvalue);
558 break;
559 case UWOP_PUSH_MACHFRAME:
560 if (info->Flags & UNW_FLAG_CHAININFO)
562 FIXME("PUSH_MACHFRAME with chained unwind info.\n");
563 break;
565 if (i + get_opcode_size(info->UnwindCode[i]) < info->CountOfCodes)
567 FIXME("PUSH_MACHFRAME is not the last opcode.\n");
568 break;
571 if (info->UnwindCode[i].u.OpInfo)
572 context->Rsp += 0x8;
574 if (!sw_read_mem(csw, context->Rsp, &context->Rip, sizeof(DWORD64))) return FALSE;
575 if (!sw_read_mem(csw, context->Rsp + 24, &context->Rsp, sizeof(DWORD64))) return FALSE;
576 mach_frame = TRUE;
577 break;
578 default:
579 FIXME("unknown code %u\n", info->UnwindCode[i].u.UnwindOp);
580 break;
583 if (!(info->Flags & UNW_FLAG_CHAININFO)) break;
584 if (!sw_read_mem(csw, base + function->UnwindData + FIELD_OFFSET(UNWIND_INFO, UnwindCode) +
585 ((info->CountOfCodes + 1) & ~1) * sizeof(UNWIND_CODE),
586 &handler_data, sizeof(handler_data))) return FALSE;
587 function = &handler_data.chain; /* restart with the chained info */
589 return mach_frame ? TRUE : default_unwind(csw, context);
592 /* fetch_next_frame()
594 * modify (at least) context.{rip, rsp, rbp} using unwind information
595 * either out of PE exception handlers, debug info (dwarf), or simple stack unwind
597 static BOOL fetch_next_frame(struct cpu_stack_walk *csw, union ctx *pcontext,
598 DWORD_PTR curr_pc, void** prtf)
600 DWORD64 cfa;
601 RUNTIME_FUNCTION* rtf;
602 DWORD64 base;
603 CONTEXT *context = &pcontext->ctx;
605 if (!curr_pc || !(base = sw_module_base(csw, curr_pc))) return FALSE;
606 rtf = sw_table_access(csw, curr_pc);
607 if (prtf) *prtf = rtf;
608 if (rtf)
610 return interpret_function_table_entry(csw, context, rtf, base);
612 else if (dwarf2_virtual_unwind(csw, curr_pc, pcontext, &cfa))
614 context->Rsp = cfa;
615 TRACE("next function rip=%016lx\n", context->Rip);
616 TRACE(" rax=%016lx rbx=%016lx rcx=%016lx rdx=%016lx\n",
617 context->Rax, context->Rbx, context->Rcx, context->Rdx);
618 TRACE(" rsi=%016lx rdi=%016lx rbp=%016lx rsp=%016lx\n",
619 context->Rsi, context->Rdi, context->Rbp, context->Rsp);
620 TRACE(" r8=%016lx r9=%016lx r10=%016lx r11=%016lx\n",
621 context->R8, context->R9, context->R10, context->R11);
622 TRACE(" r12=%016lx r13=%016lx r14=%016lx r15=%016lx\n",
623 context->R12, context->R13, context->R14, context->R15);
624 return TRUE;
626 else
627 return default_unwind(csw, context);
630 static BOOL x86_64_stack_walk(struct cpu_stack_walk *csw, STACKFRAME64 *frame,
631 union ctx *context)
633 unsigned deltapc = curr_count <= 1 ? 0 : 1;
635 /* sanity check */
636 if (curr_mode >= stm_done) return FALSE;
637 assert(!csw->is32);
639 TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s\n",
640 wine_dbgstr_addr(&frame->AddrPC),
641 wine_dbgstr_addr(&frame->AddrFrame),
642 wine_dbgstr_addr(&frame->AddrReturn),
643 wine_dbgstr_addr(&frame->AddrStack),
644 curr_mode == stm_start ? "start" : "64bit",
645 wine_dbgstr_longlong(curr_count));
647 if (curr_mode == stm_start)
649 if ((frame->AddrPC.Mode == AddrModeFlat) &&
650 (frame->AddrFrame.Mode != AddrModeFlat))
652 WARN("Bad AddrPC.Mode / AddrFrame.Mode combination\n");
653 goto done_err;
656 /* Init done */
657 curr_mode = stm_64bit;
658 frame->AddrReturn.Mode = frame->AddrStack.Mode = AddrModeFlat;
659 /* don't set up AddrStack on first call. Either the caller has set it up, or
660 * we will get it in the next frame
662 memset(&frame->AddrBStore, 0, sizeof(frame->AddrBStore));
664 else
666 if (context->ctx.Rsp != frame->AddrStack.Offset) FIXME("inconsistent Stack Pointer\n");
667 if (context->ctx.Rip != frame->AddrPC.Offset) FIXME("inconsistent Instruction Pointer\n");
669 if (frame->AddrReturn.Offset == 0) goto done_err;
670 if (!fetch_next_frame(csw, context, frame->AddrPC.Offset - deltapc, &frame->FuncTableEntry))
671 goto done_err;
672 deltapc = 1;
675 memset(&frame->Params, 0, sizeof(frame->Params));
677 /* set frame information */
678 frame->AddrStack.Offset = context->ctx.Rsp;
679 frame->AddrFrame.Offset = context->ctx.Rbp;
680 frame->AddrPC.Offset = context->ctx.Rip;
681 if (1)
683 union ctx newctx = *context;
685 if (!fetch_next_frame(csw, &newctx, frame->AddrPC.Offset - deltapc, NULL))
686 goto done_err;
687 frame->AddrReturn.Mode = AddrModeFlat;
688 frame->AddrReturn.Offset = newctx.ctx.Rip;
691 frame->Far = TRUE;
692 frame->Virtual = TRUE;
693 curr_count++;
695 TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%s FuncTable=%p\n",
696 wine_dbgstr_addr(&frame->AddrPC),
697 wine_dbgstr_addr(&frame->AddrFrame),
698 wine_dbgstr_addr(&frame->AddrReturn),
699 wine_dbgstr_addr(&frame->AddrStack),
700 curr_mode == stm_start ? "start" : "64bit",
701 wine_dbgstr_longlong(curr_count),
702 frame->FuncTableEntry);
704 return TRUE;
705 done_err:
706 curr_mode = stm_done;
707 return FALSE;
709 #else
710 static BOOL x86_64_stack_walk(struct cpu_stack_walk *csw, STACKFRAME64 *frame,
711 union ctx *ctx)
713 return FALSE;
715 #endif
717 static void* x86_64_find_runtime_function(struct module* module, DWORD64 addr)
719 #ifdef __x86_64__
720 RUNTIME_FUNCTION* rtf;
721 ULONG size;
722 int min, max;
724 rtf = (RUNTIME_FUNCTION*)pe_map_directory(module, IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size);
725 if (rtf) for (min = 0, max = size / sizeof(*rtf); min <= max; )
727 int pos = (min + max) / 2;
728 if (addr < module->module.BaseOfImage + rtf[pos].BeginAddress) max = pos - 1;
729 else if (addr >= module->module.BaseOfImage + rtf[pos].EndAddress) min = pos + 1;
730 else
732 rtf += pos;
733 while (rtf->UnwindData & 1) /* follow chained entry */
735 FIXME("RunTime_Function outside IMAGE_DIRECTORY_ENTRY_EXCEPTION unimplemented yet!\n");
736 return NULL;
737 /* we need to read into the other process */
738 /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
740 return rtf;
743 #endif
744 return NULL;
747 static unsigned x86_64_map_dwarf_register(unsigned regno, const struct module* module, BOOL eh_frame)
749 unsigned reg;
751 if (regno >= 17 && regno <= 24)
752 reg = CV_AMD64_XMM0 + regno - 17;
753 else if (regno >= 25 && regno <= 32)
754 reg = CV_AMD64_XMM8 + regno - 25;
755 else if (regno >= 33 && regno <= 40)
756 reg = CV_AMD64_ST0 + regno - 33;
757 else switch (regno)
759 case 0: reg = CV_AMD64_RAX; break;
760 case 1: reg = CV_AMD64_RDX; break;
761 case 2: reg = CV_AMD64_RCX; break;
762 case 3: reg = CV_AMD64_RBX; break;
763 case 4: reg = CV_AMD64_RSI; break;
764 case 5: reg = CV_AMD64_RDI; break;
765 case 6: reg = CV_AMD64_RBP; break;
766 case 7: reg = CV_AMD64_RSP; break;
767 case 8: reg = CV_AMD64_R8; break;
768 case 9: reg = CV_AMD64_R9; break;
769 case 10: reg = CV_AMD64_R10; break;
770 case 11: reg = CV_AMD64_R11; break;
771 case 12: reg = CV_AMD64_R12; break;
772 case 13: reg = CV_AMD64_R13; break;
773 case 14: reg = CV_AMD64_R14; break;
774 case 15: reg = CV_AMD64_R15; break;
775 case 16: reg = CV_AMD64_RIP; break;
776 case 49: reg = CV_AMD64_EFLAGS; break;
777 case 50: reg = CV_AMD64_ES; break;
778 case 51: reg = CV_AMD64_CS; break;
779 case 52: reg = CV_AMD64_SS; break;
780 case 53: reg = CV_AMD64_DS; break;
781 case 54: reg = CV_AMD64_FS; break;
782 case 55: reg = CV_AMD64_GS; break;
783 case 62: reg = CV_AMD64_TR; break;
784 case 63: reg = CV_AMD64_LDTR; break;
785 case 64: reg = CV_AMD64_MXCSR; break;
786 case 65: reg = CV_AMD64_CTRL; break;
787 case 66: reg = CV_AMD64_STAT; break;
789 * 56-57 reserved
790 * 58 %fs.base
791 * 59 %gs.base
792 * 60-61 reserved
794 default:
795 FIXME("Don't know how to map register %d\n", regno);
796 return 0;
798 return reg;
801 static void *x86_64_fetch_context_reg(union ctx *pctx, unsigned regno, unsigned *size)
803 #ifdef __x86_64__
804 CONTEXT *ctx = &pctx->ctx;
806 switch (regno)
808 case CV_AMD64_RAX: *size = sizeof(ctx->Rax); return &ctx->Rax;
809 case CV_AMD64_RDX: *size = sizeof(ctx->Rdx); return &ctx->Rdx;
810 case CV_AMD64_RCX: *size = sizeof(ctx->Rcx); return &ctx->Rcx;
811 case CV_AMD64_RBX: *size = sizeof(ctx->Rbx); return &ctx->Rbx;
812 case CV_AMD64_RSI: *size = sizeof(ctx->Rsi); return &ctx->Rsi;
813 case CV_AMD64_RDI: *size = sizeof(ctx->Rdi); return &ctx->Rdi;
814 case CV_AMD64_RBP: *size = sizeof(ctx->Rbp); return &ctx->Rbp;
815 case CV_AMD64_RSP: *size = sizeof(ctx->Rsp); return &ctx->Rsp;
816 case CV_AMD64_R8: *size = sizeof(ctx->R8); return &ctx->R8;
817 case CV_AMD64_R9: *size = sizeof(ctx->R9); return &ctx->R9;
818 case CV_AMD64_R10: *size = sizeof(ctx->R10); return &ctx->R10;
819 case CV_AMD64_R11: *size = sizeof(ctx->R11); return &ctx->R11;
820 case CV_AMD64_R12: *size = sizeof(ctx->R12); return &ctx->R12;
821 case CV_AMD64_R13: *size = sizeof(ctx->R13); return &ctx->R13;
822 case CV_AMD64_R14: *size = sizeof(ctx->R14); return &ctx->R14;
823 case CV_AMD64_R15: *size = sizeof(ctx->R15); return &ctx->R15;
824 case CV_AMD64_RIP: *size = sizeof(ctx->Rip); return &ctx->Rip;
826 case CV_AMD64_XMM0 + 0: *size = sizeof(ctx->u.s.Xmm0 ); return &ctx->u.s.Xmm0;
827 case CV_AMD64_XMM0 + 1: *size = sizeof(ctx->u.s.Xmm1 ); return &ctx->u.s.Xmm1;
828 case CV_AMD64_XMM0 + 2: *size = sizeof(ctx->u.s.Xmm2 ); return &ctx->u.s.Xmm2;
829 case CV_AMD64_XMM0 + 3: *size = sizeof(ctx->u.s.Xmm3 ); return &ctx->u.s.Xmm3;
830 case CV_AMD64_XMM0 + 4: *size = sizeof(ctx->u.s.Xmm4 ); return &ctx->u.s.Xmm4;
831 case CV_AMD64_XMM0 + 5: *size = sizeof(ctx->u.s.Xmm5 ); return &ctx->u.s.Xmm5;
832 case CV_AMD64_XMM0 + 6: *size = sizeof(ctx->u.s.Xmm6 ); return &ctx->u.s.Xmm6;
833 case CV_AMD64_XMM0 + 7: *size = sizeof(ctx->u.s.Xmm7 ); return &ctx->u.s.Xmm7;
834 case CV_AMD64_XMM8 + 0: *size = sizeof(ctx->u.s.Xmm8 ); return &ctx->u.s.Xmm8;
835 case CV_AMD64_XMM8 + 1: *size = sizeof(ctx->u.s.Xmm9 ); return &ctx->u.s.Xmm9;
836 case CV_AMD64_XMM8 + 2: *size = sizeof(ctx->u.s.Xmm10); return &ctx->u.s.Xmm10;
837 case CV_AMD64_XMM8 + 3: *size = sizeof(ctx->u.s.Xmm11); return &ctx->u.s.Xmm11;
838 case CV_AMD64_XMM8 + 4: *size = sizeof(ctx->u.s.Xmm12); return &ctx->u.s.Xmm12;
839 case CV_AMD64_XMM8 + 5: *size = sizeof(ctx->u.s.Xmm13); return &ctx->u.s.Xmm13;
840 case CV_AMD64_XMM8 + 6: *size = sizeof(ctx->u.s.Xmm14); return &ctx->u.s.Xmm14;
841 case CV_AMD64_XMM8 + 7: *size = sizeof(ctx->u.s.Xmm15); return &ctx->u.s.Xmm15;
843 case CV_AMD64_ST0 + 0: *size = sizeof(ctx->u.s.Legacy[0]); return &ctx->u.s.Legacy[0];
844 case CV_AMD64_ST0 + 1: *size = sizeof(ctx->u.s.Legacy[1]); return &ctx->u.s.Legacy[1];
845 case CV_AMD64_ST0 + 2: *size = sizeof(ctx->u.s.Legacy[2]); return &ctx->u.s.Legacy[2];
846 case CV_AMD64_ST0 + 3: *size = sizeof(ctx->u.s.Legacy[3]); return &ctx->u.s.Legacy[3];
847 case CV_AMD64_ST0 + 4: *size = sizeof(ctx->u.s.Legacy[4]); return &ctx->u.s.Legacy[4];
848 case CV_AMD64_ST0 + 5: *size = sizeof(ctx->u.s.Legacy[5]); return &ctx->u.s.Legacy[5];
849 case CV_AMD64_ST0 + 6: *size = sizeof(ctx->u.s.Legacy[6]); return &ctx->u.s.Legacy[6];
850 case CV_AMD64_ST0 + 7: *size = sizeof(ctx->u.s.Legacy[7]); return &ctx->u.s.Legacy[7];
852 case CV_AMD64_EFLAGS: *size = sizeof(ctx->EFlags); return &ctx->EFlags;
853 case CV_AMD64_ES: *size = sizeof(ctx->SegEs); return &ctx->SegEs;
854 case CV_AMD64_CS: *size = sizeof(ctx->SegCs); return &ctx->SegCs;
855 case CV_AMD64_SS: *size = sizeof(ctx->SegSs); return &ctx->SegSs;
856 case CV_AMD64_DS: *size = sizeof(ctx->SegDs); return &ctx->SegDs;
857 case CV_AMD64_FS: *size = sizeof(ctx->SegFs); return &ctx->SegFs;
858 case CV_AMD64_GS: *size = sizeof(ctx->SegGs); return &ctx->SegGs;
861 #endif
862 FIXME("Unknown register %x\n", regno);
863 return NULL;
866 static const char* x86_64_fetch_regname(unsigned regno)
868 switch (regno)
870 case CV_AMD64_RAX: return "rax";
871 case CV_AMD64_RDX: return "rdx";
872 case CV_AMD64_RCX: return "rcx";
873 case CV_AMD64_RBX: return "rbx";
874 case CV_AMD64_RSI: return "rsi";
875 case CV_AMD64_RDI: return "rdi";
876 case CV_AMD64_RBP: return "rbp";
877 case CV_AMD64_RSP: return "rsp";
878 case CV_AMD64_R8: return "r8";
879 case CV_AMD64_R9: return "r9";
880 case CV_AMD64_R10: return "r10";
881 case CV_AMD64_R11: return "r11";
882 case CV_AMD64_R12: return "r12";
883 case CV_AMD64_R13: return "r13";
884 case CV_AMD64_R14: return "r14";
885 case CV_AMD64_R15: return "r15";
886 case CV_AMD64_RIP: return "rip";
888 case CV_AMD64_XMM0 + 0: return "xmm0";
889 case CV_AMD64_XMM0 + 1: return "xmm1";
890 case CV_AMD64_XMM0 + 2: return "xmm2";
891 case CV_AMD64_XMM0 + 3: return "xmm3";
892 case CV_AMD64_XMM0 + 4: return "xmm4";
893 case CV_AMD64_XMM0 + 5: return "xmm5";
894 case CV_AMD64_XMM0 + 6: return "xmm6";
895 case CV_AMD64_XMM0 + 7: return "xmm7";
896 case CV_AMD64_XMM8 + 0: return "xmm8";
897 case CV_AMD64_XMM8 + 1: return "xmm9";
898 case CV_AMD64_XMM8 + 2: return "xmm10";
899 case CV_AMD64_XMM8 + 3: return "xmm11";
900 case CV_AMD64_XMM8 + 4: return "xmm12";
901 case CV_AMD64_XMM8 + 5: return "xmm13";
902 case CV_AMD64_XMM8 + 6: return "xmm14";
903 case CV_AMD64_XMM8 + 7: return "xmm15";
905 case CV_AMD64_ST0 + 0: return "st0";
906 case CV_AMD64_ST0 + 1: return "st1";
907 case CV_AMD64_ST0 + 2: return "st2";
908 case CV_AMD64_ST0 + 3: return "st3";
909 case CV_AMD64_ST0 + 4: return "st4";
910 case CV_AMD64_ST0 + 5: return "st5";
911 case CV_AMD64_ST0 + 6: return "st6";
912 case CV_AMD64_ST0 + 7: return "st7";
914 case CV_AMD64_EFLAGS: return "eflags";
915 case CV_AMD64_ES: return "es";
916 case CV_AMD64_CS: return "cs";
917 case CV_AMD64_SS: return "ss";
918 case CV_AMD64_DS: return "ds";
919 case CV_AMD64_FS: return "fs";
920 case CV_AMD64_GS: return "gs";
922 FIXME("Unknown register %x\n", regno);
923 return NULL;
926 static BOOL x86_64_fetch_minidump_thread(struct dump_context* dc, unsigned index, unsigned flags, const CONTEXT* ctx)
928 if (ctx->ContextFlags && (flags & ThreadWriteInstructionWindow))
930 /* FIXME: crop values across module boundaries, */
931 #ifdef __x86_64__
932 ULONG64 base = ctx->Rip <= 0x80 ? 0 : ctx->Rip - 0x80;
933 minidump_add_memory_block(dc, base, ctx->Rip + 0x80 - base, 0);
934 #endif
937 return TRUE;
940 static BOOL x86_64_fetch_minidump_module(struct dump_context* dc, unsigned index, unsigned flags)
942 /* FIXME: not sure about the flags... */
943 if (1)
945 /* FIXME: crop values across module boundaries, */
946 #ifdef __x86_64__
947 struct process* pcs;
948 struct module* module;
949 const RUNTIME_FUNCTION* rtf;
950 ULONG size;
952 if (!(pcs = process_find_by_handle(dc->process->handle)) ||
953 !(module = module_find_by_addr(pcs, dc->modules[index].base, DMT_UNKNOWN)))
954 return FALSE;
955 rtf = (const RUNTIME_FUNCTION*)pe_map_directory(module, IMAGE_DIRECTORY_ENTRY_EXCEPTION, &size);
956 if (rtf)
958 const RUNTIME_FUNCTION* end = (const RUNTIME_FUNCTION*)((const char*)rtf + size);
959 UNWIND_INFO ui;
961 while (rtf + 1 < end)
963 while (rtf->UnwindData & 1) /* follow chained entry */
965 FIXME("RunTime_Function outside IMAGE_DIRECTORY_ENTRY_EXCEPTION unimplemented yet!\n");
966 return FALSE;
967 /* we need to read into the other process */
968 /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
970 if (read_process_memory(dc->process, dc->modules[index].base + rtf->UnwindData, &ui, sizeof(ui)))
971 minidump_add_memory_block(dc, dc->modules[index].base + rtf->UnwindData,
972 FIELD_OFFSET(UNWIND_INFO, UnwindCode) + ui.CountOfCodes * sizeof(UNWIND_CODE), 0);
973 rtf++;
976 #endif
979 return TRUE;
982 DECLSPEC_HIDDEN struct cpu cpu_x86_64 = {
983 IMAGE_FILE_MACHINE_AMD64,
985 CV_AMD64_RSP,
986 x86_64_get_addr,
987 x86_64_stack_walk,
988 x86_64_find_runtime_function,
989 x86_64_map_dwarf_register,
990 x86_64_fetch_context_reg,
991 x86_64_fetch_regname,
992 x86_64_fetch_minidump_thread,
993 x86_64_fetch_minidump_module,