ReactOS  0.4.13-dev-39-g8b6696f
stack.c
Go to the documentation of this file.
1 /*
2  * Stack walking
3  *
4  * Copyright 1995 Alexandre Julliard
5  * Copyright 1996 Eric Youngdale
6  * Copyright 1999 Ove K√•ven
7  * Copyright 2004 Eric Pouech
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23 
24 #include "config.h"
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <assert.h>
29 
30 #include "dbghelp_private.h"
31 #include "wine/debug.h"
32 
34 
36 {
37  LDT_ENTRY le;
38 
39  switch (addr->Mode)
40  {
41  case AddrMode1616:
42  if (GetThreadSelectorEntry(hThread, addr->Segment, &le))
43  return (le.HighWord.Bits.BaseHi << 24) +
44  (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + LOWORD(addr->Offset);
45  break;
46  case AddrMode1632:
47  if (GetThreadSelectorEntry(hThread, addr->Segment, &le))
48  return (le.HighWord.Bits.BaseHi << 24) +
49  (le.HighWord.Bits.BaseMid << 16) + le.BaseLow + addr->Offset;
50  break;
51  case AddrModeReal:
52  return (DWORD)(LOWORD(addr->Segment) << 4) + addr->Offset;
53  case AddrModeFlat:
54  return addr->Offset;
55  default:
56  FIXME("Unsupported (yet) mode (%x)\n", addr->Mode);
57  return 0;
58  }
59  FIXME("Failed to linearize address %04x:%s (mode %x)\n",
60  addr->Segment, wine_dbgstr_longlong(addr->Offset), addr->Mode);
61  return 0;
62 }
63 
65  DWORD size, LPDWORD nread)
66 {
67  SIZE_T r;
68  if (!ReadProcessMemory(hProcess, (void*)(DWORD_PTR)addr, buffer, size, &r)) return FALSE;
69  if (nread) *nread = r;
70  return TRUE;
71 }
72 
74  DWORD size, LPDWORD nread)
75 {
76  SIZE_T r;
77  if (!ReadProcessMemory(hProcess, (void*)(DWORD_PTR)addr, buffer, size, &r)) return FALSE;
78  if (nread) *nread = r;
79  return TRUE;
80 }
81 
82 static inline void addr_32to64(const ADDRESS* addr32, ADDRESS64* addr64)
83 {
84  addr64->Offset = (ULONG64)addr32->Offset;
85  addr64->Segment = addr32->Segment;
86  addr64->Mode = addr32->Mode;
87 }
88 
89 static inline void addr_64to32(const ADDRESS64* addr64, ADDRESS* addr32)
90 {
91  addr32->Offset = (ULONG)addr64->Offset;
92  addr32->Segment = addr64->Segment;
93  addr32->Mode = addr64->Mode;
94 }
95 
97 {
98  DWORD bytes_read = 0;
99  if (csw->is32)
100  return csw->u.s32.f_read_mem(csw->hProcess, addr, ptr, sz, &bytes_read);
101  else
102  return csw->u.s64.f_read_mem(csw->hProcess, addr, ptr, sz, &bytes_read);
103 }
104 
106 {
107  if (addr->Mode == AddrModeFlat) return addr->Offset;
108  if (csw->is32)
109  {
110  ADDRESS addr32;
111 
112  addr_64to32(addr, &addr32);
113  return csw->u.s32.f_xlat_adr(csw->hProcess, csw->hThread, &addr32);
114  }
115  else if (csw->u.s64.f_xlat_adr)
116  return csw->u.s64.f_xlat_adr(csw->hProcess, csw->hThread, addr);
117  return addr_to_linear(csw->hProcess, csw->hThread, addr);
118 }
119 
121 {
122  if (csw->is32)
123  return csw->u.s32.f_tabl_acs(csw->hProcess, addr);
124  else
125  return csw->u.s64.f_tabl_acs(csw->hProcess, addr);
126 }
127 
129 {
130  if (csw->is32)
131  return csw->u.s32.f_modl_bas(csw->hProcess, addr);
132  else
133  return csw->u.s64.f_modl_bas(csw->hProcess, addr);
134 }
135 
136 /***********************************************************************
137  * StackWalk (DBGHELP.@)
138  */
140  LPSTACKFRAME frame32, PVOID ctx,
141  PREAD_PROCESS_MEMORY_ROUTINE f_read_mem,
142  PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine,
143  PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine,
144  PTRANSLATE_ADDRESS_ROUTINE f_xlat_adr)
145 {
146  struct cpu_stack_walk csw;
147  STACKFRAME64 frame64;
148  BOOL ret;
149  struct cpu* cpu;
150 
151  TRACE("(%d, %p, %p, %p, %p, %p, %p, %p, %p)\n",
152  MachineType, hProcess, hThread, frame32, ctx,
153  f_read_mem, FunctionTableAccessRoutine,
154  GetModuleBaseRoutine, f_xlat_adr);
155 
156  if (!(cpu = cpu_find(MachineType)))
157  {
159  return FALSE;
160  }
161 
162  addr_32to64(&frame32->AddrPC, &frame64.AddrPC);
163  addr_32to64(&frame32->AddrReturn, &frame64.AddrReturn);
164  addr_32to64(&frame32->AddrFrame, &frame64.AddrFrame);
165  addr_32to64(&frame32->AddrStack, &frame64.AddrStack);
166  addr_32to64(&frame32->AddrBStore, &frame64.AddrBStore);
167  frame64.FuncTableEntry = frame32->FuncTableEntry; /* FIXME */
168  frame64.Far = frame32->Far;
169  frame64.Virtual = frame32->Virtual;
170  frame64.Reserved[0] = frame32->Reserved[0];
171  frame64.Reserved[1] = frame32->Reserved[1];
172  frame64.Reserved[2] = frame32->Reserved[2];
173  /* we don't handle KdHelp */
174 
175  csw.hProcess = hProcess;
176  csw.hThread = hThread;
177  csw.is32 = TRUE;
178  /* sigh... MS isn't even consistent in the func prototypes */
179  csw.u.s32.f_read_mem = (f_read_mem) ? f_read_mem : read_mem;
180  csw.u.s32.f_xlat_adr = f_xlat_adr;
181  csw.u.s32.f_tabl_acs = (FunctionTableAccessRoutine) ? FunctionTableAccessRoutine : SymFunctionTableAccess;
182  csw.u.s32.f_modl_bas = (GetModuleBaseRoutine) ? GetModuleBaseRoutine : SymGetModuleBase;
183 
184  if ((ret = cpu->stack_walk(&csw, &frame64, ctx)))
185  {
186  addr_64to32(&frame64.AddrPC, &frame32->AddrPC);
187  addr_64to32(&frame64.AddrReturn, &frame32->AddrReturn);
188  addr_64to32(&frame64.AddrFrame, &frame32->AddrFrame);
189  addr_64to32(&frame64.AddrStack, &frame32->AddrStack);
190  addr_64to32(&frame64.AddrBStore, &frame32->AddrBStore);
191  frame32->FuncTableEntry = frame64.FuncTableEntry; /* FIXME */
192  frame32->Params[0] = frame64.Params[0];
193  frame32->Params[1] = frame64.Params[1];
194  frame32->Params[2] = frame64.Params[2];
195  frame32->Params[3] = frame64.Params[3];
196  frame32->Far = frame64.Far;
197  frame32->Virtual = frame64.Virtual;
198  frame32->Reserved[0] = frame64.Reserved[0];
199  frame32->Reserved[1] = frame64.Reserved[1];
200  frame32->Reserved[2] = frame64.Reserved[2];
201  }
202 
203  return ret;
204 }
205 
206 
207 /***********************************************************************
208  * StackWalk64 (DBGHELP.@)
209  */
211  LPSTACKFRAME64 frame, PVOID ctx,
213  PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine,
214  PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine,
215  PTRANSLATE_ADDRESS_ROUTINE64 f_xlat_adr)
216 {
217  struct cpu_stack_walk csw;
218  struct cpu* cpu;
219 
220  TRACE("(%d, %p, %p, %p, %p, %p, %p, %p, %p)\n",
221  MachineType, hProcess, hThread, frame, ctx,
222  f_read_mem, FunctionTableAccessRoutine,
223  GetModuleBaseRoutine, f_xlat_adr);
224 
225  if (!(cpu = cpu_find(MachineType)))
226  {
228  return FALSE;
229  }
230 
231  csw.hProcess = hProcess;
232  csw.hThread = hThread;
233  csw.is32 = FALSE;
234  /* sigh... MS isn't even consistent in the func prototypes */
235  csw.u.s64.f_read_mem = (f_read_mem) ? f_read_mem : read_mem64;
236  csw.u.s64.f_xlat_adr = (f_xlat_adr) ? f_xlat_adr : addr_to_linear;
237  csw.u.s64.f_tabl_acs = (FunctionTableAccessRoutine) ? FunctionTableAccessRoutine : SymFunctionTableAccess64;
238  csw.u.s64.f_modl_bas = (GetModuleBaseRoutine) ? GetModuleBaseRoutine : SymGetModuleBase64;
239 
240  if (!cpu->stack_walk(&csw, frame, ctx)) return FALSE;
241 
242  /* we don't handle KdHelp */
243 
244  return TRUE;
245 }
246 
247 /******************************************************************
248  * SymRegisterFunctionEntryCallback (DBGHELP.@)
249  *
250  *
251  */
254 {
255  FIXME("(%p %p %p): stub!\n", hProc, cb, user);
257  return FALSE;
258 }
259 
260 /******************************************************************
261  * SymRegisterFunctionEntryCallback64 (DBGHELP.@)
262  *
263  *
264  */
267  ULONG64 user)
268 {
269  FIXME("(%p %p %s): stub!\n", hProc, cb, wine_dbgstr_longlong(user));
271  return FALSE;
272 }
#define ERROR_INVALID_PARAMETER
Definition: compat.h:91
return
Definition: dirsup.c:529
ADDRESS_MODE Mode
Definition: compat.h:831
BOOL WINAPI StackWalk(DWORD MachineType, HANDLE hProcess, HANDLE hThread, LPSTACKFRAME frame32, PVOID ctx, PREAD_PROCESS_MEMORY_ROUTINE f_read_mem, PFUNCTION_TABLE_ACCESS_ROUTINE FunctionTableAccessRoutine, PGET_MODULE_BASE_ROUTINE GetModuleBaseRoutine, PTRANSLATE_ADDRESS_ROUTINE f_xlat_adr)
Definition: stack.c:139
#define TRUE
Definition: types.h:120
static DWORD64 WINAPI addr_to_linear(HANDLE hProcess, HANDLE hThread, ADDRESS64 *addr)
Definition: stack.c:35
DWORD Params[4]
Definition: dbghelp.h:2161
PVOID FuncTableEntry
Definition: dbghelp.h:2160
PVOID WINAPI SymFunctionTableAccess(HANDLE, DWORD)
Definition: module.c:1161
static BOOL CALLBACK read_mem64(HANDLE hProcess, DWORD64 addr, void *buffer, DWORD size, LPDWORD nread)
Definition: stack.c:73
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
PVOID(CALLBACK * PSYMBOL_FUNCENTRY_CALLBACK)(_In_ HANDLE, _In_ DWORD, _In_opt_ PVOID)
Definition: dbghelp.h:2451
#define CALLBACK
Definition: compat.h:27
PVOID WINAPI SymFunctionTableAccess64(HANDLE, DWORD64)
Definition: module.c:1169
BOOL WINAPI StackWalk64(DWORD MachineType, HANDLE hProcess, HANDLE hThread, LPSTACKFRAME64 frame, PVOID ctx, PREAD_PROCESS_MEMORY_ROUTINE64 f_read_mem, PFUNCTION_TABLE_ACCESS_ROUTINE64 FunctionTableAccessRoutine, PGET_MODULE_BASE_ROUTINE64 GetModuleBaseRoutine, PTRANSLATE_ADDRESS_ROUTINE64 f_xlat_adr)
Definition: stack.c:210
BOOL(* stack_walk)(struct cpu_stack_walk *csw, LPSTACKFRAME64 frame, CONTEXT *context)
GLuint buffer
Definition: glext.h:5915
_In_ BOOL _In_ HANDLE hProcess
Definition: mapping.h:70
PVOID(CALLBACK * PFUNCTION_TABLE_ACCESS_ROUTINE64)(HANDLE, DWORD64)
Definition: compat.h:813
DWORD64(CALLBACK * PGET_MODULE_BASE_ROUTINE64)(HANDLE, DWORD64)
Definition: compat.h:814
#define ReadProcessMemory(a, b, c, d, e)
Definition: compat.h:415
ADDRESS AddrPC
Definition: dbghelp.h:2156
union cpu_stack_walk::@358 u
static void addr_64to32(const ADDRESS64 *addr64, ADDRESS *addr32)
Definition: stack.c:89
PVOID(CALLBACK * PSYMBOL_FUNCENTRY_CALLBACK64)(_In_ HANDLE, _In_ ULONG64, _In_ ULONG64)
Definition: dbghelp.h:2225
BOOL WINAPI SymRegisterFunctionEntryCallback64(HANDLE hProc, PSYMBOL_FUNCENTRY_CALLBACK64 cb, ULONG64 user)
Definition: stack.c:265
WORD BaseLow
Definition: compat.h:430
WORD Segment
Definition: compat.h:809
BOOL sw_read_mem(struct cpu_stack_walk *csw, DWORD64 addr, void *ptr, DWORD sz)
Definition: stack.c:96
unsigned int BOOL
Definition: ntddk_ex.h:94
#define FIXME(fmt,...)
Definition: debug.h:110
static PVOID ptr
Definition: dispmode.c:27
DWORD64 sw_xlat_addr(struct cpu_stack_walk *csw, ADDRESS64 *addr)
Definition: stack.c:105
DWORD(CALLBACK * PTRANSLATE_ADDRESS_ROUTINE)(HANDLE, HANDLE, LPADDRESS)
Definition: compat.h:863
BOOL WINAPI SymRegisterFunctionEntryCallback(HANDLE hProc, PSYMBOL_FUNCENTRY_CALLBACK cb, PVOID user)
Definition: stack.c:252
struct cpu * cpu_find(DWORD machine)
Definition: dbghelp.c:166
WINE_DEFAULT_DEBUG_CHANNEL(dbghelp)
BOOL(CALLBACK * PREAD_PROCESS_MEMORY_ROUTINE64)(HANDLE, DWORD64, PVOID, DWORD, PDWORD)
Definition: compat.h:817
DWORD64(CALLBACK * PTRANSLATE_ADDRESS_ROUTINE64)(HANDLE, HANDLE, LPADDRESS64)
Definition: compat.h:815
#define TRACE(s)
Definition: solgame.cpp:4
struct cpu_stack_walk::@358::@359 s32
GLsizeiptr size
Definition: glext.h:5919
ADDRESS AddrBStore
Definition: dbghelp.h:2166
DWORD64 Offset
Definition: compat.h:808
#define WINAPI
Definition: msvc.h:8
unsigned long DWORD
Definition: ntddk_ex.h:95
BOOL(CALLBACK * PREAD_PROCESS_MEMORY_ROUTINE)(HANDLE, DWORD, PVOID, DWORD, PDWORD)
Definition: compat.h:862
#define SetLastError(x)
Definition: compat.h:409
static DWORD cb
Definition: integrity.c:41
DWORD Offset
Definition: compat.h:829
unsigned __int64 ULONG64
Definition: imports.h:198
int ret
GLenum const GLvoid * addr
Definition: glext.h:9621
static void addr_32to64(const ADDRESS *addr32, ADDRESS64 *addr64)
Definition: stack.c:82
ADDRESS AddrReturn
Definition: dbghelp.h:2157
DWORD64 WINAPI SymGetModuleBase64(HANDLE, DWORD64)
Definition: module.c:1107
void * sw_table_access(struct cpu_stack_walk *csw, DWORD64 addr)
Definition: stack.c:120
struct cpu_stack_walk::@358::@360 s64
static BOOL CALLBACK read_mem(HANDLE hProcess, DWORD addr, void *buffer, DWORD size, LPDWORD nread)
Definition: stack.c:64
uint32_t DWORD_PTR
Definition: typedefs.h:63
DWORD Reserved[3]
Definition: dbghelp.h:2164
DWORD64 sw_module_base(struct cpu_stack_walk *csw, DWORD64 addr)
Definition: stack.c:128
Definition: compat.h:428
WORD Segment
Definition: compat.h:830
ULONG_PTR SIZE_T
Definition: typedefs.h:78
uint64_t DWORD64
Definition: typedefs.h:65
DWORD(CALLBACK * PGET_MODULE_BASE_ROUTINE)(HANDLE, DWORD)
Definition: compat.h:865
BOOL WINAPI GetThreadSelectorEntry(IN HANDLE hThread, IN DWORD dwSelector, OUT LPLDT_ENTRY lpSelectorEntry)
Definition: thread.c:792
HANDLE hThread
Definition: wizard.c:27
ADDRESS AddrFrame
Definition: dbghelp.h:2158
PVOID(CALLBACK * PFUNCTION_TABLE_ACCESS_ROUTINE)(HANDLE, DWORD)
Definition: compat.h:864
union _LDT_ENTRY::@340 HighWord
uint32_t * LPDWORD
Definition: typedefs.h:57
BOOL Virtual
Definition: dbghelp.h:2163
unsigned int ULONG
Definition: retypes.h:1
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:41
#define ERROR_CALL_NOT_IMPLEMENTED
Definition: compat.h:92
DWORD WINAPI SymGetModuleBase(HANDLE hProcess, DWORD dwAddr)
Definition: module.c:1096
struct _LDT_ENTRY::@340::@342 Bits
ADDRESS AddrStack
Definition: dbghelp.h:2159
BOOL Far
Definition: dbghelp.h:2162
ADDRESS_MODE Mode
Definition: compat.h:810
void user(int argc, const char *argv[])
Definition: cmds.c:1350
#define LOWORD(l)
Definition: pedump.c:82