[CMAKE]
[reactos.git] / dll / win32 / dbghelp / cpu_x86_64.c
1 /*
2 * File cpu_x86_64.c
3 *
4 * Copyright (C) 2009-2009, Eric Pouech.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <assert.h>
22
23 #include "ntstatus.h"
24 #define WIN32_NO_STATUS
25 #include "dbghelp_private.h"
26 #include "winternl.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp);
30
31 static unsigned x86_64_get_addr(HANDLE hThread, const CONTEXT* ctx,
32 enum cpu_addr ca, ADDRESS64* addr)
33 {
34 addr->Mode = AddrModeFlat;
35 switch (ca)
36 {
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;
44 }
45 }
46
47 enum st_mode {stm_start, stm_64bit, stm_done};
48
49 /* indexes in Reserved array */
50 #define __CurrentMode 0
51 #define __CurrentSwitch 1
52 #define __NextSwitch 2
53
54 #define curr_mode (frame->Reserved[__CurrentMode])
55 #define curr_switch (frame->Reserved[__CurrentSwitch])
56 #define next_switch (frame->Reserved[__NextSwitch])
57
58 static BOOL x86_64_stack_walk(struct cpu_stack_walk* csw, LPSTACKFRAME64 frame)
59 {
60 /* sanity check */
61 if (curr_mode >= stm_done) return FALSE;
62 assert(!csw->is32);
63
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");
70
71 if (curr_mode == stm_start)
72 {
73 if ((frame->AddrPC.Mode == AddrModeFlat) &&
74 (frame->AddrFrame.Mode != AddrModeFlat))
75 {
76 WARN("Bad AddrPC.Mode / AddrFrame.Mode combination\n");
77 goto done_err;
78 }
79
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
86 */
87 memset(&frame->AddrBStore, 0, sizeof(frame->AddrBStore));
88 }
89 else
90 {
91 if (frame->AddrReturn.Offset == 0) goto done_err;
92 frame->AddrPC = frame->AddrReturn;
93 }
94
95 if (!sw_read_mem(csw, frame->AddrStack.Offset,
96 &frame->AddrReturn.Offset, sizeof(DWORD64)))
97 {
98 WARN("Cannot read new frame offset %s\n",
99 wine_dbgstr_longlong(frame->AddrFrame.Offset + sizeof(DWORD64)));
100 goto done_err;
101 }
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));
105
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;
112
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);
120
121 return TRUE;
122 done_err:
123 curr_mode = stm_done;
124 return FALSE;
125 }
126
127 struct cpu cpu_x86_64 = {
128 IMAGE_FILE_MACHINE_AMD64,
129 8,
130 x86_64_get_addr,
131 x86_64_stack_walk,
132 };