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
25 #define WIN32_NO_STATUS
26 #include "dbghelp_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
32 /* x86-64 unwind information, for PE modules, as described on MSDN */
34 typedef enum _UNWIND_OP_CODES
48 typedef union _UNWIND_CODE
57 } UNWIND_CODE
, *PUNWIND_CODE
;
59 typedef struct _UNWIND_INFO
65 BYTE FrameRegister
: 4;
67 UNWIND_CODE UnwindCode
[1]; /* actually CountOfCodes (aligned) */
71 * OPTIONAL ULONG ExceptionHandler;
72 * OPTIONAL ULONG FunctionEntry;
74 * OPTIONAL ULONG ExceptionData[];
76 } UNWIND_INFO
, *PUNWIND_INFO
;
78 static BOOL
x86_64_get_addr(HANDLE hThread
, const CONTEXT
* ctx
,
79 enum cpu_addr ca
, ADDRESS64
* addr
)
81 addr
->Mode
= AddrModeFlat
;
85 case cpu_addr_pc
: addr
->Segment
= ctx
->SegCs
; addr
->Offset
= ctx
->Rip
; return TRUE
;
86 case cpu_addr_stack
: addr
->Segment
= ctx
->SegSs
; addr
->Offset
= ctx
->Rsp
; return TRUE
;
87 case cpu_addr_frame
: addr
->Segment
= ctx
->SegSs
; addr
->Offset
= ctx
->Rbp
; return TRUE
;
89 default: addr
->Mode
= -1;
96 enum st_mode
{stm_start
, stm_64bit
, stm_done
};
98 /* indexes in Reserved array */
99 #define __CurrentMode 0
100 #define __CurrentCount 1
101 /* #define __ 2 (unused) */
103 #define curr_mode (frame->Reserved[__CurrentMode])
104 #define curr_count (frame->Reserved[__CurrentCount])
105 /* #define ??? (frame->Reserved[__]) (unused) */
109 RUNTIME_FUNCTION chain
;
113 static void dump_unwind_info(struct cpu_stack_walk
* csw
, ULONG64 base
, RUNTIME_FUNCTION
*function
)
115 static const char * const reg_names
[16] =
116 { "rax", "rcx", "rdx", "rbx", "rsp", "rbp", "rsi", "rdi",
117 "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" };
119 union handler_data handler_data
;
120 char buffer
[sizeof(UNWIND_INFO
) + 256 * sizeof(UNWIND_CODE
)];
121 UNWIND_INFO
* info
= (UNWIND_INFO
*)buffer
;
122 unsigned int i
, count
;
123 RUNTIME_FUNCTION snext
;
126 TRACE("**** func %lx-%lx\n", function
->BeginAddress
, function
->EndAddress
);
129 if (function
->UnwindData
& 1)
131 if (!sw_read_mem(csw
, base
+ function
->UnwindData
, &snext
, sizeof(snext
)))
133 TRACE("Couldn't unwind RUNTIME_INFO at %Ix\n", base
+ function
->UnwindData
);
136 TRACE("unwind info for function %p-%p chained to function %p-%p\n",
137 (char*)base
+ function
->BeginAddress
, (char*)base
+ function
->EndAddress
,
138 (char*)base
+ snext
.BeginAddress
, (char*)base
+ snext
.EndAddress
);
142 addr
= base
+ function
->UnwindData
;
143 if (!sw_read_mem(csw
, addr
, info
, FIELD_OFFSET(UNWIND_INFO
, UnwindCode
)) ||
144 !sw_read_mem(csw
, addr
+ FIELD_OFFSET(UNWIND_INFO
, UnwindCode
),
145 info
->UnwindCode
, info
->CountOfCodes
* sizeof(UNWIND_CODE
)))
147 FIXME("couldn't read memory for UNWIND_INFO at %Ix\n", addr
);
150 TRACE("unwind info at %p flags %x prolog 0x%x bytes function %p-%p\n",
151 (char*)addr
, info
->Flags
, info
->SizeOfProlog
,
152 (char*)base
+ function
->BeginAddress
, (char*)base
+ function
->EndAddress
);
154 if (info
->FrameRegister
)
155 TRACE(" frame register %s offset 0x%x(%%rsp)\n",
156 reg_names
[info
->FrameRegister
], info
->FrameOffset
* 16);
158 for (i
= 0; i
< info
->CountOfCodes
; i
++)
160 TRACE(" 0x%x: ", info
->UnwindCode
[i
].CodeOffset
);
161 switch (info
->UnwindCode
[i
].UnwindOp
)
163 case UWOP_PUSH_NONVOL
:
164 TRACE("pushq %%%s\n", reg_names
[info
->UnwindCode
[i
].OpInfo
]);
166 case UWOP_ALLOC_LARGE
:
167 if (info
->UnwindCode
[i
].OpInfo
)
169 count
= *(DWORD
*)&info
->UnwindCode
[i
+1];
174 count
= *(USHORT
*)&info
->UnwindCode
[i
+1] * 8;
177 TRACE("subq $0x%x,%%rsp\n", count
);
179 case UWOP_ALLOC_SMALL
:
180 count
= (info
->UnwindCode
[i
].OpInfo
+ 1) * 8;
181 TRACE("subq $0x%x,%%rsp\n", count
);
184 TRACE("leaq 0x%x(%%rsp),%s\n",
185 info
->FrameOffset
* 16, reg_names
[info
->FrameRegister
]);
187 case UWOP_SAVE_NONVOL
:
188 count
= *(USHORT
*)&info
->UnwindCode
[i
+1] * 8;
189 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->UnwindCode
[i
].OpInfo
], count
);
192 case UWOP_SAVE_NONVOL_FAR
:
193 count
= *(DWORD
*)&info
->UnwindCode
[i
+1];
194 TRACE("movq %%%s,0x%x(%%rsp)\n", reg_names
[info
->UnwindCode
[i
].OpInfo
], count
);
197 case UWOP_SAVE_XMM128
:
198 count
= *(USHORT
*)&info
->UnwindCode
[i
+1] * 16;
199 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info
->UnwindCode
[i
].OpInfo
, count
);
202 case UWOP_SAVE_XMM128_FAR
:
203 count
= *(DWORD
*)&info
->UnwindCode
[i
+1];
204 TRACE("movaps %%xmm%u,0x%x(%%rsp)\n", info
->UnwindCode
[i
].OpInfo
, count
);
207 case UWOP_PUSH_MACHFRAME
:
208 TRACE("PUSH_MACHFRAME %u\n", info
->UnwindCode
[i
].OpInfo
);
211 if (info
->Version
== 2)
214 if (info
->UnwindCode
[i
].OpInfo
)
215 offset
= info
->UnwindCode
[i
].CodeOffset
;
217 offset
= (info
->UnwindCode
[i
+1].OpInfo
<< 8) + info
->UnwindCode
[i
+1].CodeOffset
;
218 TRACE("UWOP_EPILOG %u offset %u\n", info
->UnwindCode
[i
].OpInfo
, offset
);
224 FIXME("unknown code %u\n", info
->UnwindCode
[i
].UnwindOp
);
229 addr
+= FIELD_OFFSET(UNWIND_INFO
, UnwindCode
) +
230 ((info
->CountOfCodes
+ 1) & ~1) * sizeof(UNWIND_CODE
);
231 if (info
->Flags
& UNW_FLAG_CHAININFO
)
233 if (!sw_read_mem(csw
, addr
, &handler_data
, sizeof(handler_data
.chain
)))
235 FIXME("couldn't read memory for handler_data.chain\n");
238 TRACE(" chained to function %p-%p\n",
239 (char*)base
+ handler_data
.chain
.BeginAddress
,
240 (char*)base
+ handler_data
.chain
.EndAddress
);
241 function
= &handler_data
.chain
;
244 if (info
->Flags
& (UNW_FLAG_EHANDLER
| UNW_FLAG_UHANDLER
))
246 if (!sw_read_mem(csw
, addr
, &handler_data
, sizeof(handler_data
.handler
)))
248 FIXME("couldn't read memory for handler_data.handler\n");
251 TRACE(" handler %p data at %p\n",
252 (char*)base
+ handler_data
.handler
, (char*)addr
+ sizeof(handler_data
.handler
));
258 /* highly derived from dlls/ntdll/signal_x86_64.c */
259 static ULONG64
get_int_reg(CONTEXT
*context
, int reg
)
261 return *(&context
->Rax
+ reg
);
264 static void set_int_reg(CONTEXT
*context
, int reg
, ULONG64 val
)
266 *(&context
->Rax
+ reg
) = val
;
269 static void set_float_reg(CONTEXT
*context
, int reg
, M128A val
)
271 *(&context
->Xmm0
+ reg
) = val
;
274 static int get_opcode_size(UNWIND_CODE op
)
278 case UWOP_ALLOC_LARGE
:
279 return 2 + (op
.OpInfo
!= 0);
280 case UWOP_SAVE_NONVOL
:
281 case UWOP_SAVE_XMM128
:
283 case UWOP_SAVE_NONVOL_FAR
:
284 case UWOP_SAVE_XMM128_FAR
:
291 static BOOL
is_inside_epilog(struct cpu_stack_walk
* csw
, DWORD64 pc
,
292 DWORD64 base
, const RUNTIME_FUNCTION
*function
)
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
;
305 case 0x81: /* add $nnnn,%rsp */
306 if (!sw_read_mem(csw
, pc
+ 2, &op2
, 1)) return FALSE
;
307 if (op0
== 0x48 && op2
== 0xc4)
313 case 0x83: /* add $n,%rsp */
314 if (!sw_read_mem(csw
, pc
+ 2, &op2
, 1)) return FALSE
;
315 if (op0
== 0x48 && op2
== 0xc4)
321 case 0x8d: /* lea n(reg),%rsp */
322 if (!sw_read_mem(csw
, pc
+ 2, &op2
, 1)) return FALSE
;
323 if (op0
& 0x06) return FALSE
; /* rex.RX must be cleared */
324 if (((op2
>> 3) & 7) != 4) return FALSE
; /* dest reg mus be %rsp */
325 if ((op2
& 7) == 4) return FALSE
; /* no SIB byte allowed */
326 if ((op2
>> 6) == 1) /* 8-bit offset */
331 if ((op2
>> 6) == 2) /* 32-bit offset */
340 /* now check for various pop instructions */
343 if (!sw_read_mem(csw
, pc
, &op0
, 1)) return FALSE
;
344 if ((op0
& 0xf0) == 0x40) /* rex prefix */
346 if (!sw_read_mem(csw
, ++pc
, &op0
, 1)) return FALSE
;
351 case 0x58: /* pop %rax/%r8 */
352 case 0x59: /* pop %rcx/%r9 */
353 case 0x5a: /* pop %rdx/%r10 */
354 case 0x5b: /* pop %rbx/%r11 */
355 case 0x5c: /* pop %rsp/%r12 */
356 case 0x5d: /* pop %rbp/%r13 */
357 case 0x5e: /* pop %rsi/%r14 */
358 case 0x5f: /* pop %rdi/%r15 */
361 case 0xc2: /* ret $nn */
364 case 0xe9: /* jmp nnnn */
365 if (!sw_read_mem(csw
, pc
+ 1, &val32
, sizeof(LONG
))) return FALSE
;
367 if (pc
- base
>= function
->BeginAddress
&& pc
- base
< function
->EndAddress
)
370 case 0xeb: /* jmp n */
371 if (!sw_read_mem(csw
, pc
+ 1, &op1
, 1)) return FALSE
;
372 pc
+= 2 + (signed char)op1
;
373 if (pc
- base
>= function
->BeginAddress
&& pc
- base
< function
->EndAddress
)
376 case 0xf3: /* rep; ret (for amd64 prediction bug) */
377 if (!sw_read_mem(csw
, pc
+ 1, &op1
, 1)) return FALSE
;
384 static BOOL
interpret_epilog(struct cpu_stack_walk
* csw
, ULONG64 pc
, CONTEXT
*context
)
395 if (!sw_read_mem(csw
, pc
, &insn
, 1)) return FALSE
;
396 if ((insn
& 0xf0) == 0x40)
398 rex
= insn
& 0x0f; /* rex prefix */
399 if (!sw_read_mem(csw
, ++pc
, &insn
, 1)) return FALSE
;
404 case 0x58: /* pop %rax/r8 */
405 case 0x59: /* pop %rcx/r9 */
406 case 0x5a: /* pop %rdx/r10 */
407 case 0x5b: /* pop %rbx/r11 */
408 case 0x5c: /* pop %rsp/r12 */
409 case 0x5d: /* pop %rbp/r13 */
410 case 0x5e: /* pop %rsi/r14 */
411 case 0x5f: /* pop %rdi/r15 */
412 if (!sw_read_mem(csw
, context
->Rsp
, &val64
, sizeof(DWORD64
))) return FALSE
;
413 set_int_reg(context
, insn
- 0x58 + (rex
& 1) * 8, val64
);
414 context
->Rsp
+= sizeof(ULONG64
);
417 case 0x81: /* add $nnnn,%rsp */
418 if (!sw_read_mem(csw
, pc
+ 2, &val32
, sizeof(LONG
))) return FALSE
;
419 context
->Rsp
+= val32
;
420 pc
+= 2 + sizeof(LONG
);
422 case 0x83: /* add $n,%rsp */
423 if (!sw_read_mem(csw
, pc
+ 2, &val8
, sizeof(BYTE
))) return FALSE
;
424 context
->Rsp
+= (signed char)val8
;
428 if (!sw_read_mem(csw
, pc
+ 1, &insn
, sizeof(BYTE
))) return FALSE
;
429 if ((insn
>> 6) == 1) /* lea n(reg),%rsp */
431 if (!sw_read_mem(csw
, pc
+ 2, &val8
, sizeof(BYTE
))) return FALSE
;
432 context
->Rsp
= get_int_reg( context
, (insn
& 7) + (rex
& 1) * 8 ) + (signed char)val8
;
435 else /* lea nnnn(reg),%rsp */
437 if (!sw_read_mem(csw
, pc
+ 2, &val32
, sizeof(LONG
))) return FALSE
;
438 context
->Rsp
= get_int_reg( context
, (insn
& 7) + (rex
& 1) * 8 ) + val32
;
439 pc
+= 2 + sizeof(LONG
);
442 case 0xc2: /* ret $nn */
443 if (!sw_read_mem(csw
, context
->Rsp
, &val64
, sizeof(DWORD64
))) return FALSE
;
444 if (!sw_read_mem(csw
, pc
+ 1, &val16
, sizeof(WORD
))) return FALSE
;
445 context
->Rip
= val64
;
446 context
->Rsp
+= sizeof(ULONG64
) + val16
;
449 case 0xf3: /* rep; ret */
450 if (!sw_read_mem(csw
, context
->Rsp
, &val64
, sizeof(DWORD64
))) return FALSE
;
451 context
->Rip
= val64
;
452 context
->Rsp
+= sizeof(ULONG64
);
454 case 0xe9: /* jmp nnnn */
455 if (!sw_read_mem(csw
, pc
+ 1, &val32
, sizeof(LONG
))) return FALSE
;
458 case 0xeb: /* jmp n */
459 if (!sw_read_mem(csw
, pc
+ 1, &val8
, sizeof(BYTE
))) return FALSE
;
460 pc
+= 2 + (signed char)val8
;
463 FIXME("unsupported insn %x\n", insn
);
468 static BOOL
default_unwind(struct cpu_stack_walk
* csw
, CONTEXT
* context
)
470 if (!sw_read_mem(csw
, context
->Rsp
, &context
->Rip
, sizeof(DWORD64
)))
472 WARN("Cannot read new frame offset %I64x\n", context
->Rsp
);
475 context
->Rsp
+= sizeof(DWORD64
);
479 static BOOL
interpret_function_table_entry(struct cpu_stack_walk
* csw
,
480 CONTEXT
* context
, RUNTIME_FUNCTION
* function
, DWORD64 base
)
482 char buffer
[sizeof(UNWIND_INFO
) + 256 * sizeof(UNWIND_CODE
)];
483 UNWIND_INFO
* info
= (UNWIND_INFO
*)buffer
;
485 DWORD64 newframe
, prolog_offset
, off
, value
;
487 union handler_data handler_data
;
488 BOOL mach_frame
= FALSE
;
490 /* FIXME: we have some assumptions here */
492 dump_unwind_info(csw
, sw_module_base(csw
, context
->Rip
), function
);
493 newframe
= context
->Rsp
;
496 if (!sw_read_mem(csw
, base
+ function
->UnwindData
, info
, sizeof(*info
)) ||
497 !sw_read_mem(csw
, base
+ function
->UnwindData
+ FIELD_OFFSET(UNWIND_INFO
, UnwindCode
),
498 info
->UnwindCode
, info
->CountOfCodes
* sizeof(UNWIND_CODE
)))
500 WARN("Couldn't read unwind_code at %Ix\n", base
+ function
->UnwindData
);
504 if (info
->Version
!= 1 && info
->Version
!= 2)
506 WARN("unknown unwind info version %u at %Ix\n", info
->Version
, base
+ function
->UnwindData
);
510 if (info
->FrameRegister
)
511 newframe
= get_int_reg(context
, info
->FrameRegister
) - info
->FrameOffset
* 16;
513 /* check if in prolog */
514 if (context
->Rip
>= base
+ function
->BeginAddress
&&
515 context
->Rip
< base
+ function
->BeginAddress
+ info
->SizeOfProlog
)
517 prolog_offset
= context
->Rip
- base
- function
->BeginAddress
;
522 if (is_inside_epilog(csw
, context
->Rip
, base
, function
))
524 interpret_epilog(csw
, context
->Rip
, context
);
529 for (i
= 0; i
< info
->CountOfCodes
; i
+= get_opcode_size(info
->UnwindCode
[i
]))
531 if (prolog_offset
< info
->UnwindCode
[i
].CodeOffset
) continue; /* skip it */
533 switch (info
->UnwindCode
[i
].UnwindOp
)
535 case UWOP_PUSH_NONVOL
: /* pushq %reg */
536 if (!sw_read_mem(csw
, context
->Rsp
, &value
, sizeof(DWORD64
))) return FALSE
;
537 set_int_reg(context
, info
->UnwindCode
[i
].OpInfo
, value
);
538 context
->Rsp
+= sizeof(ULONG64
);
540 case UWOP_ALLOC_LARGE
: /* subq $nn,%rsp */
541 if (info
->UnwindCode
[i
].OpInfo
) context
->Rsp
+= *(DWORD
*)&info
->UnwindCode
[i
+1];
542 else context
->Rsp
+= *(USHORT
*)&info
->UnwindCode
[i
+1] * 8;
544 case UWOP_ALLOC_SMALL
: /* subq $n,%rsp */
545 context
->Rsp
+= (info
->UnwindCode
[i
].OpInfo
+ 1) * 8;
547 case UWOP_SET_FPREG
: /* leaq nn(%rsp),%framereg */
548 context
->Rsp
= newframe
;
550 case UWOP_SAVE_NONVOL
: /* movq %reg,n(%rsp) */
551 off
= newframe
+ *(USHORT
*)&info
->UnwindCode
[i
+1] * 8;
552 if (!sw_read_mem(csw
, off
, &value
, sizeof(DWORD64
))) return FALSE
;
553 set_int_reg(context
, info
->UnwindCode
[i
].OpInfo
, value
);
555 case UWOP_SAVE_NONVOL_FAR
: /* movq %reg,nn(%rsp) */
556 off
= newframe
+ *(DWORD
*)&info
->UnwindCode
[i
+1];
557 if (!sw_read_mem(csw
, off
, &value
, sizeof(DWORD64
))) return FALSE
;
558 set_int_reg(context
, info
->UnwindCode
[i
].OpInfo
, value
);
560 case UWOP_SAVE_XMM128
: /* movaps %xmmreg,n(%rsp) */
561 off
= newframe
+ *(USHORT
*)&info
->UnwindCode
[i
+1] * 16;
562 if (!sw_read_mem(csw
, off
, &floatvalue
, sizeof(M128A
))) return FALSE
;
563 set_float_reg(context
, info
->UnwindCode
[i
].OpInfo
, floatvalue
);
565 case UWOP_SAVE_XMM128_FAR
: /* movaps %xmmreg,nn(%rsp) */
566 off
= newframe
+ *(DWORD
*)&info
->UnwindCode
[i
+1];
567 if (!sw_read_mem(csw
, off
, &floatvalue
, sizeof(M128A
))) return FALSE
;
568 set_float_reg(context
, info
->UnwindCode
[i
].OpInfo
, floatvalue
);
570 case UWOP_PUSH_MACHFRAME
:
571 if (info
->Flags
& UNW_FLAG_CHAININFO
)
573 FIXME("PUSH_MACHFRAME with chained unwind info.\n");
576 if (i
+ get_opcode_size(info
->UnwindCode
[i
]) < info
->CountOfCodes
)
578 FIXME("PUSH_MACHFRAME is not the last opcode.\n");
582 if (info
->UnwindCode
[i
].OpInfo
)
585 if (!sw_read_mem(csw
, context
->Rsp
, &context
->Rip
, sizeof(DWORD64
))) return FALSE
;
586 if (!sw_read_mem(csw
, context
->Rsp
+ 24, &context
->Rsp
, sizeof(DWORD64
))) return FALSE
;
590 FIXME("unknown code %u\n", info
->UnwindCode
[i
].UnwindOp
);
594 if (!(info
->Flags
& UNW_FLAG_CHAININFO
)) break;
595 if (!sw_read_mem(csw
, base
+ function
->UnwindData
+ FIELD_OFFSET(UNWIND_INFO
, UnwindCode
) +
596 ((info
->CountOfCodes
+ 1) & ~1) * sizeof(UNWIND_CODE
),
597 &handler_data
, sizeof(handler_data
))) return FALSE
;
598 function
= &handler_data
.chain
; /* restart with the chained info */
600 return mach_frame
? TRUE
: default_unwind(csw
, context
);
603 /* fetch_next_frame()
605 * modify (at least) context.{rip, rsp, rbp} using unwind information
606 * either out of PE exception handlers, debug info (dwarf), or simple stack unwind
608 static BOOL
fetch_next_frame(struct cpu_stack_walk
*csw
, union ctx
*pcontext
,
609 DWORD_PTR curr_pc
, void** prtf
)
612 RUNTIME_FUNCTION
* rtf
;
614 CONTEXT
*context
= &pcontext
->ctx
;
615 DWORD64 input_Rip
= context
->Rip
;
617 if (!curr_pc
|| !(base
= sw_module_base(csw
, curr_pc
))) return FALSE
;
618 rtf
= sw_table_access(csw
, curr_pc
);
619 if (prtf
) *prtf
= rtf
;
622 return interpret_function_table_entry(csw
, context
, rtf
, base
);
624 else if (dwarf2_virtual_unwind(csw
, curr_pc
, pcontext
, &cfa
) && input_Rip
!= context
->Rip
)
627 TRACE("next function rip=%016Ix\n", context
->Rip
);
628 TRACE(" rax=%016Ix rbx=%016Ix rcx=%016Ix rdx=%016Ix\n",
629 context
->Rax
, context
->Rbx
, context
->Rcx
, context
->Rdx
);
630 TRACE(" rsi=%016Ix rdi=%016Ix rbp=%016Ix rsp=%016Ix\n",
631 context
->Rsi
, context
->Rdi
, context
->Rbp
, context
->Rsp
);
632 TRACE(" r8=%016Ix r9=%016Ix r10=%016Ix r11=%016Ix\n",
633 context
->R8
, context
->R9
, context
->R10
, context
->R11
);
634 TRACE(" r12=%016Ix r13=%016Ix r14=%016Ix r15=%016Ix\n",
635 context
->R12
, context
->R13
, context
->R14
, context
->R15
);
639 return default_unwind(csw
, context
);
642 static BOOL
x86_64_stack_walk(struct cpu_stack_walk
*csw
, STACKFRAME64
*frame
,
645 unsigned deltapc
= curr_count
<= 1 ? 0 : 1;
648 if (curr_mode
>= stm_done
) return FALSE
;
651 TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%I64u\n",
652 wine_dbgstr_addr(&frame
->AddrPC
),
653 wine_dbgstr_addr(&frame
->AddrFrame
),
654 wine_dbgstr_addr(&frame
->AddrReturn
),
655 wine_dbgstr_addr(&frame
->AddrStack
),
656 curr_mode
== stm_start
? "start" : "64bit",
659 if (curr_mode
== stm_start
)
661 if ((frame
->AddrPC
.Mode
== AddrModeFlat
) &&
662 (frame
->AddrFrame
.Mode
!= AddrModeFlat
))
664 WARN("Bad AddrPC.Mode / AddrFrame.Mode combination\n");
669 curr_mode
= stm_64bit
;
670 frame
->AddrReturn
.Mode
= frame
->AddrStack
.Mode
= AddrModeFlat
;
671 /* don't set up AddrStack on first call. Either the caller has set it up, or
672 * we will get it in the next frame
674 memset(&frame
->AddrBStore
, 0, sizeof(frame
->AddrBStore
));
678 if (context
->ctx
.Rsp
!= frame
->AddrStack
.Offset
) FIXME("inconsistent Stack Pointer\n");
679 if (context
->ctx
.Rip
!= frame
->AddrPC
.Offset
) FIXME("inconsistent Instruction Pointer\n");
681 if (frame
->AddrReturn
.Offset
== 0) goto done_err
;
682 if (!fetch_next_frame(csw
, context
, frame
->AddrPC
.Offset
- deltapc
, &frame
->FuncTableEntry
))
687 memset(&frame
->Params
, 0, sizeof(frame
->Params
));
689 /* set frame information */
690 frame
->AddrStack
.Offset
= context
->ctx
.Rsp
;
691 frame
->AddrFrame
.Offset
= context
->ctx
.Rbp
;
692 frame
->AddrPC
.Offset
= context
->ctx
.Rip
;
695 union ctx newctx
= *context
;
697 if (!fetch_next_frame(csw
, &newctx
, frame
->AddrPC
.Offset
- deltapc
, NULL
))
699 frame
->AddrReturn
.Mode
= AddrModeFlat
;
700 frame
->AddrReturn
.Offset
= newctx
.ctx
.Rip
;
704 frame
->Virtual
= TRUE
;
707 TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s Count=%I64u FuncTable=%p\n",
708 wine_dbgstr_addr(&frame
->AddrPC
),
709 wine_dbgstr_addr(&frame
->AddrFrame
),
710 wine_dbgstr_addr(&frame
->AddrReturn
),
711 wine_dbgstr_addr(&frame
->AddrStack
),
712 curr_mode
== stm_start
? "start" : "64bit",
714 frame
->FuncTableEntry
);
718 curr_mode
= stm_done
;
722 static BOOL
x86_64_stack_walk(struct cpu_stack_walk
*csw
, STACKFRAME64
*frame
,
729 static void* x86_64_find_runtime_function(struct module
* module
, DWORD64 addr
)
732 RUNTIME_FUNCTION
* rtf
;
736 rtf
= (RUNTIME_FUNCTION
*)pe_map_directory(module
, IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
);
737 if (rtf
) for (min
= 0, max
= size
/ sizeof(*rtf
); min
<= max
; )
739 int pos
= (min
+ max
) / 2;
740 if (addr
< module
->module
.BaseOfImage
+ rtf
[pos
].BeginAddress
) max
= pos
- 1;
741 else if (addr
>= module
->module
.BaseOfImage
+ rtf
[pos
].EndAddress
) min
= pos
+ 1;
745 while (rtf
->UnwindData
& 1) /* follow chained entry */
747 FIXME("RunTime_Function outside IMAGE_DIRECTORY_ENTRY_EXCEPTION unimplemented yet!\n");
749 /* we need to read into the other process */
750 /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
759 static unsigned x86_64_map_dwarf_register(unsigned regno
, const struct module
* module
, BOOL eh_frame
)
763 if (regno
>= 17 && regno
<= 24)
764 reg
= CV_AMD64_XMM0
+ regno
- 17;
765 else if (regno
>= 25 && regno
<= 32)
766 reg
= CV_AMD64_XMM8
+ regno
- 25;
767 else if (regno
>= 33 && regno
<= 40)
768 reg
= CV_AMD64_ST0
+ regno
- 33;
771 case 0: reg
= CV_AMD64_RAX
; break;
772 case 1: reg
= CV_AMD64_RDX
; break;
773 case 2: reg
= CV_AMD64_RCX
; break;
774 case 3: reg
= CV_AMD64_RBX
; break;
775 case 4: reg
= CV_AMD64_RSI
; break;
776 case 5: reg
= CV_AMD64_RDI
; break;
777 case 6: reg
= CV_AMD64_RBP
; break;
778 case 7: reg
= CV_AMD64_RSP
; break;
779 case 8: reg
= CV_AMD64_R8
; break;
780 case 9: reg
= CV_AMD64_R9
; break;
781 case 10: reg
= CV_AMD64_R10
; break;
782 case 11: reg
= CV_AMD64_R11
; break;
783 case 12: reg
= CV_AMD64_R12
; break;
784 case 13: reg
= CV_AMD64_R13
; break;
785 case 14: reg
= CV_AMD64_R14
; break;
786 case 15: reg
= CV_AMD64_R15
; break;
787 case 16: reg
= CV_AMD64_RIP
; break;
788 case 49: reg
= CV_AMD64_EFLAGS
; break;
789 case 50: reg
= CV_AMD64_ES
; break;
790 case 51: reg
= CV_AMD64_CS
; break;
791 case 52: reg
= CV_AMD64_SS
; break;
792 case 53: reg
= CV_AMD64_DS
; break;
793 case 54: reg
= CV_AMD64_FS
; break;
794 case 55: reg
= CV_AMD64_GS
; break;
795 case 62: reg
= CV_AMD64_TR
; break;
796 case 63: reg
= CV_AMD64_LDTR
; break;
797 case 64: reg
= CV_AMD64_MXCSR
; break;
798 case 65: reg
= CV_AMD64_CTRL
; break;
799 case 66: reg
= CV_AMD64_STAT
; break;
807 FIXME("Don't know how to map register %d\n", regno
);
813 static void *x86_64_fetch_context_reg(union ctx
*pctx
, unsigned regno
, unsigned *size
)
816 CONTEXT
*ctx
= &pctx
->ctx
;
820 case CV_AMD64_RAX
: *size
= sizeof(ctx
->Rax
); return &ctx
->Rax
;
821 case CV_AMD64_RDX
: *size
= sizeof(ctx
->Rdx
); return &ctx
->Rdx
;
822 case CV_AMD64_RCX
: *size
= sizeof(ctx
->Rcx
); return &ctx
->Rcx
;
823 case CV_AMD64_RBX
: *size
= sizeof(ctx
->Rbx
); return &ctx
->Rbx
;
824 case CV_AMD64_RSI
: *size
= sizeof(ctx
->Rsi
); return &ctx
->Rsi
;
825 case CV_AMD64_RDI
: *size
= sizeof(ctx
->Rdi
); return &ctx
->Rdi
;
826 case CV_AMD64_RBP
: *size
= sizeof(ctx
->Rbp
); return &ctx
->Rbp
;
827 case CV_AMD64_RSP
: *size
= sizeof(ctx
->Rsp
); return &ctx
->Rsp
;
828 case CV_AMD64_R8
: *size
= sizeof(ctx
->R8
); return &ctx
->R8
;
829 case CV_AMD64_R9
: *size
= sizeof(ctx
->R9
); return &ctx
->R9
;
830 case CV_AMD64_R10
: *size
= sizeof(ctx
->R10
); return &ctx
->R10
;
831 case CV_AMD64_R11
: *size
= sizeof(ctx
->R11
); return &ctx
->R11
;
832 case CV_AMD64_R12
: *size
= sizeof(ctx
->R12
); return &ctx
->R12
;
833 case CV_AMD64_R13
: *size
= sizeof(ctx
->R13
); return &ctx
->R13
;
834 case CV_AMD64_R14
: *size
= sizeof(ctx
->R14
); return &ctx
->R14
;
835 case CV_AMD64_R15
: *size
= sizeof(ctx
->R15
); return &ctx
->R15
;
836 case CV_AMD64_RIP
: *size
= sizeof(ctx
->Rip
); return &ctx
->Rip
;
838 case CV_AMD64_XMM0
+ 0: *size
= sizeof(ctx
->Xmm0
); return &ctx
->Xmm0
;
839 case CV_AMD64_XMM0
+ 1: *size
= sizeof(ctx
->Xmm1
); return &ctx
->Xmm1
;
840 case CV_AMD64_XMM0
+ 2: *size
= sizeof(ctx
->Xmm2
); return &ctx
->Xmm2
;
841 case CV_AMD64_XMM0
+ 3: *size
= sizeof(ctx
->Xmm3
); return &ctx
->Xmm3
;
842 case CV_AMD64_XMM0
+ 4: *size
= sizeof(ctx
->Xmm4
); return &ctx
->Xmm4
;
843 case CV_AMD64_XMM0
+ 5: *size
= sizeof(ctx
->Xmm5
); return &ctx
->Xmm5
;
844 case CV_AMD64_XMM0
+ 6: *size
= sizeof(ctx
->Xmm6
); return &ctx
->Xmm6
;
845 case CV_AMD64_XMM0
+ 7: *size
= sizeof(ctx
->Xmm7
); return &ctx
->Xmm7
;
846 case CV_AMD64_XMM8
+ 0: *size
= sizeof(ctx
->Xmm8
); return &ctx
->Xmm8
;
847 case CV_AMD64_XMM8
+ 1: *size
= sizeof(ctx
->Xmm9
); return &ctx
->Xmm9
;
848 case CV_AMD64_XMM8
+ 2: *size
= sizeof(ctx
->Xmm10
); return &ctx
->Xmm10
;
849 case CV_AMD64_XMM8
+ 3: *size
= sizeof(ctx
->Xmm11
); return &ctx
->Xmm11
;
850 case CV_AMD64_XMM8
+ 4: *size
= sizeof(ctx
->Xmm12
); return &ctx
->Xmm12
;
851 case CV_AMD64_XMM8
+ 5: *size
= sizeof(ctx
->Xmm13
); return &ctx
->Xmm13
;
852 case CV_AMD64_XMM8
+ 6: *size
= sizeof(ctx
->Xmm14
); return &ctx
->Xmm14
;
853 case CV_AMD64_XMM8
+ 7: *size
= sizeof(ctx
->Xmm15
); return &ctx
->Xmm15
;
855 case CV_AMD64_ST0
+ 0: *size
= sizeof(ctx
->Legacy
[0]); return &ctx
->Legacy
[0];
856 case CV_AMD64_ST0
+ 1: *size
= sizeof(ctx
->Legacy
[1]); return &ctx
->Legacy
[1];
857 case CV_AMD64_ST0
+ 2: *size
= sizeof(ctx
->Legacy
[2]); return &ctx
->Legacy
[2];
858 case CV_AMD64_ST0
+ 3: *size
= sizeof(ctx
->Legacy
[3]); return &ctx
->Legacy
[3];
859 case CV_AMD64_ST0
+ 4: *size
= sizeof(ctx
->Legacy
[4]); return &ctx
->Legacy
[4];
860 case CV_AMD64_ST0
+ 5: *size
= sizeof(ctx
->Legacy
[5]); return &ctx
->Legacy
[5];
861 case CV_AMD64_ST0
+ 6: *size
= sizeof(ctx
->Legacy
[6]); return &ctx
->Legacy
[6];
862 case CV_AMD64_ST0
+ 7: *size
= sizeof(ctx
->Legacy
[7]); return &ctx
->Legacy
[7];
864 case CV_AMD64_EFLAGS
: *size
= sizeof(ctx
->EFlags
); return &ctx
->EFlags
;
865 case CV_AMD64_ES
: *size
= sizeof(ctx
->SegEs
); return &ctx
->SegEs
;
866 case CV_AMD64_CS
: *size
= sizeof(ctx
->SegCs
); return &ctx
->SegCs
;
867 case CV_AMD64_SS
: *size
= sizeof(ctx
->SegSs
); return &ctx
->SegSs
;
868 case CV_AMD64_DS
: *size
= sizeof(ctx
->SegDs
); return &ctx
->SegDs
;
869 case CV_AMD64_FS
: *size
= sizeof(ctx
->SegFs
); return &ctx
->SegFs
;
870 case CV_AMD64_GS
: *size
= sizeof(ctx
->SegGs
); return &ctx
->SegGs
;
874 FIXME("Unknown register %x\n", regno
);
878 static const char* x86_64_fetch_regname(unsigned regno
)
882 case CV_AMD64_RAX
: return "rax";
883 case CV_AMD64_RDX
: return "rdx";
884 case CV_AMD64_RCX
: return "rcx";
885 case CV_AMD64_RBX
: return "rbx";
886 case CV_AMD64_RSI
: return "rsi";
887 case CV_AMD64_RDI
: return "rdi";
888 case CV_AMD64_RBP
: return "rbp";
889 case CV_AMD64_RSP
: return "rsp";
890 case CV_AMD64_R8
: return "r8";
891 case CV_AMD64_R9
: return "r9";
892 case CV_AMD64_R10
: return "r10";
893 case CV_AMD64_R11
: return "r11";
894 case CV_AMD64_R12
: return "r12";
895 case CV_AMD64_R13
: return "r13";
896 case CV_AMD64_R14
: return "r14";
897 case CV_AMD64_R15
: return "r15";
898 case CV_AMD64_RIP
: return "rip";
900 case CV_AMD64_XMM0
+ 0: return "xmm0";
901 case CV_AMD64_XMM0
+ 1: return "xmm1";
902 case CV_AMD64_XMM0
+ 2: return "xmm2";
903 case CV_AMD64_XMM0
+ 3: return "xmm3";
904 case CV_AMD64_XMM0
+ 4: return "xmm4";
905 case CV_AMD64_XMM0
+ 5: return "xmm5";
906 case CV_AMD64_XMM0
+ 6: return "xmm6";
907 case CV_AMD64_XMM0
+ 7: return "xmm7";
908 case CV_AMD64_XMM8
+ 0: return "xmm8";
909 case CV_AMD64_XMM8
+ 1: return "xmm9";
910 case CV_AMD64_XMM8
+ 2: return "xmm10";
911 case CV_AMD64_XMM8
+ 3: return "xmm11";
912 case CV_AMD64_XMM8
+ 4: return "xmm12";
913 case CV_AMD64_XMM8
+ 5: return "xmm13";
914 case CV_AMD64_XMM8
+ 6: return "xmm14";
915 case CV_AMD64_XMM8
+ 7: return "xmm15";
917 case CV_AMD64_ST0
+ 0: return "st0";
918 case CV_AMD64_ST0
+ 1: return "st1";
919 case CV_AMD64_ST0
+ 2: return "st2";
920 case CV_AMD64_ST0
+ 3: return "st3";
921 case CV_AMD64_ST0
+ 4: return "st4";
922 case CV_AMD64_ST0
+ 5: return "st5";
923 case CV_AMD64_ST0
+ 6: return "st6";
924 case CV_AMD64_ST0
+ 7: return "st7";
926 case CV_AMD64_EFLAGS
: return "eflags";
927 case CV_AMD64_ES
: return "es";
928 case CV_AMD64_CS
: return "cs";
929 case CV_AMD64_SS
: return "ss";
930 case CV_AMD64_DS
: return "ds";
931 case CV_AMD64_FS
: return "fs";
932 case CV_AMD64_GS
: return "gs";
934 FIXME("Unknown register %x\n", regno
);
938 static BOOL
x86_64_fetch_minidump_thread(struct dump_context
* dc
, unsigned index
, unsigned flags
, const CONTEXT
* ctx
)
940 if (ctx
->ContextFlags
&& (flags
& ThreadWriteInstructionWindow
))
942 /* FIXME: crop values across module boundaries, */
944 ULONG64 base
= ctx
->Rip
<= 0x80 ? 0 : ctx
->Rip
- 0x80;
945 minidump_add_memory_block(dc
, base
, ctx
->Rip
+ 0x80 - base
, 0);
952 static BOOL
x86_64_fetch_minidump_module(struct dump_context
* dc
, unsigned index
, unsigned flags
)
954 /* FIXME: not sure about the flags... */
957 /* FIXME: crop values across module boundaries, */
960 struct module
* module
;
961 const RUNTIME_FUNCTION
* rtf
;
964 if (!(pcs
= process_find_by_handle(dc
->process
->handle
)) ||
965 !(module
= module_find_by_addr(pcs
, dc
->modules
[index
].base
, DMT_UNKNOWN
)))
967 rtf
= (const RUNTIME_FUNCTION
*)pe_map_directory(module
, IMAGE_DIRECTORY_ENTRY_EXCEPTION
, &size
);
970 const RUNTIME_FUNCTION
* end
= (const RUNTIME_FUNCTION
*)((const char*)rtf
+ size
);
973 while (rtf
+ 1 < end
)
975 while (rtf
->UnwindData
& 1) /* follow chained entry */
977 FIXME("RunTime_Function outside IMAGE_DIRECTORY_ENTRY_EXCEPTION unimplemented yet!\n");
979 /* we need to read into the other process */
980 /* rtf = (RUNTIME_FUNCTION*)(module->module.BaseOfImage + (rtf->UnwindData & ~1)); */
982 if (read_process_memory(dc
->process
, dc
->modules
[index
].base
+ rtf
->UnwindData
, &ui
, sizeof(ui
)))
983 minidump_add_memory_block(dc
, dc
->modules
[index
].base
+ rtf
->UnwindData
,
984 FIELD_OFFSET(UNWIND_INFO
, UnwindCode
) + ui
.CountOfCodes
* sizeof(UNWIND_CODE
), 0);
994 DECLSPEC_HIDDEN
struct cpu cpu_x86_64
= {
995 IMAGE_FILE_MACHINE_AMD64
,
1000 x86_64_find_runtime_function
,
1001 x86_64_map_dwarf_register
,
1002 x86_64_fetch_context_reg
,
1003 x86_64_fetch_regname
,
1004 x86_64_fetch_minidump_thread
,
1005 x86_64_fetch_minidump_module
,