push ac4b6ebdd3fd886d6a18959265755579ed195b88
[wine/hacks.git] / dlls / dbghelp / cpu_x86_64.c
blobcd0e32c50810e7ba39284205fb6f48d8e93e0747
1 /*
2 * File cpu_x86_64.c
4 * Copyright (C) 2009-2009, Eric Pouech.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <assert.h>
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "dbghelp_private.h"
26 #include "winternl.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
31 static unsigned x86_64_get_addr(HANDLE hThread, const CONTEXT* ctx,
32 enum cpu_addr ca, ADDRESS64* addr)
34 addr->Mode = AddrModeFlat;
35 switch (ca)
37 #ifdef __x86_64__
38 case cpu_addr_pc: addr->Segment = ctx->SegCs; addr->Offset = ctx->Rip; return TRUE;
39 case cpu_addr_stack: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rsp; return TRUE;
40 case cpu_addr_frame: addr->Segment = ctx->SegSs; addr->Offset = ctx->Rbp; return TRUE;
41 #endif
42 default: addr->Mode = -1;
43 return FALSE;
47 enum st_mode {stm_start, stm_64bit, stm_done};
49 /* indexes in Reserved array */
50 #define __CurrentMode 0
51 #define __CurrentSwitch 1
52 #define __NextSwitch 2
54 #define curr_mode (frame->Reserved[__CurrentMode])
55 #define curr_switch (frame->Reserved[__CurrentSwitch])
56 #define next_switch (frame->Reserved[__NextSwitch])
58 static BOOL x86_64_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame)
60 /* sanity check */
61 if (curr_mode >= stm_done) return FALSE;
62 assert(!csw->is32);
64 TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s\n",
65 wine_dbgstr_addr(&frame->AddrPC),
66 wine_dbgstr_addr(&frame->AddrFrame),
67 wine_dbgstr_addr(&frame->AddrReturn),
68 wine_dbgstr_addr(&frame->AddrStack),
69 curr_mode == stm_start ? "start" : "64bit");
71 if (curr_mode == stm_start)
73 if ((frame->AddrPC.Mode == AddrModeFlat) &&
74 (frame->AddrFrame.Mode != AddrModeFlat))
76 WARN("Bad AddrPC.Mode / AddrFrame.Mode combination\n");
77 goto done_err;
80 /* Init done */
81 curr_mode = stm_64bit;
82 curr_switch = 0;
83 frame->AddrReturn.Mode = frame->AddrStack.Mode = AddrModeFlat;
84 /* don't set up AddrStack on first call. Either the caller has set it up, or
85 * we will get it in the next frame
87 memset(&frame->AddrBStore, 0, sizeof(frame->AddrBStore));
89 else
91 if (frame->AddrReturn.Offset == 0) goto done_err;
92 frame->AddrPC = frame->AddrReturn;
95 if (!sw_read_mem(csw, frame->AddrStack.Offset,
96 &frame->AddrReturn.Offset, sizeof(DWORD64)))
98 WARN("Cannot read new frame offset %s\n",
99 wine_dbgstr_longlong(frame->AddrFrame.Offset + sizeof(DWORD64)));
100 goto done_err;
102 /* FIXME: simplistic stuff... need to handle both dwarf & PE stack information */
103 frame->AddrStack.Offset += sizeof(DWORD64);
104 memset(&frame->Params, 0, sizeof(frame->Params));
106 frame->Far = TRUE;
107 frame->Virtual = TRUE;
108 if (frame->AddrPC.Offset && sw_module_base(csw, frame->AddrPC.Offset))
109 frame->FuncTableEntry = sw_table_access(csw, frame->AddrPC.Offset);
110 else
111 frame->FuncTableEntry = NULL;
113 TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s FuncTable=%p\n",
114 wine_dbgstr_addr(&frame->AddrPC),
115 wine_dbgstr_addr(&frame->AddrFrame),
116 wine_dbgstr_addr(&frame->AddrReturn),
117 wine_dbgstr_addr(&frame->AddrStack),
118 curr_mode == stm_start ? "start" : "64bit",
119 frame->FuncTableEntry);
121 return TRUE;
122 done_err:
123 curr_mode = stm_done;
124 return FALSE;
127 struct cpu cpu_x86_64 = {
128 IMAGE_FILE_MACHINE_AMD64,
130 x86_64_get_addr,
131 x86_64_stack_walk,