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
24 #define WIN32_NO_STATUS
25 #include "dbghelp_private.h"
26 #include "wine/winbase16.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp
);
32 #define STEP_FLAG 0x00000100 /* single step flag */
33 #define V86_FLAG 0x00020000
35 #define IS_VM86_MODE(ctx) (ctx->EFlags & V86_FLAG)
38 static ADDRESS_MODE
get_selector_type(HANDLE hThread
, const CONTEXT
* ctx
, WORD sel
)
42 if (IS_VM86_MODE(ctx
)) return AddrModeReal
;
43 /* null or system selector */
44 if (!(sel
& 4) || ((sel
>> 3) < 17)) return AddrModeFlat
;
45 if (hThread
&& GetThreadSelectorEntry(hThread
, sel
, &le
))
46 return le
.HighWord
.Bits
.Default_Big
? AddrMode1632
: AddrMode1616
;
47 /* selector doesn't exist */
51 static unsigned i386_build_addr(HANDLE hThread
, const CONTEXT
* ctx
, ADDRESS64
* addr
,
52 unsigned seg
, unsigned long offset
)
54 addr
->Mode
= AddrModeFlat
;
56 addr
->Offset
= offset
;
59 switch (addr
->Mode
= get_selector_type(hThread
, ctx
, seg
))
63 addr
->Offset
&= 0xffff;
76 static unsigned i386_get_addr(HANDLE hThread
, const CONTEXT
* ctx
,
77 enum cpu_addr ca
, ADDRESS64
* addr
)
82 case cpu_addr_pc
: return i386_build_addr(hThread
, ctx
, addr
, ctx
->SegCs
, ctx
->Eip
);
83 case cpu_addr_stack
: return i386_build_addr(hThread
, ctx
, addr
, ctx
->SegSs
, ctx
->Esp
);
84 case cpu_addr_frame
: return i386_build_addr(hThread
, ctx
, addr
, ctx
->SegSs
, ctx
->Ebp
);
90 enum st_mode
{stm_start
, stm_32bit
, stm_16bit
, stm_done
};
92 /* indexes in Reserved array */
93 #define __CurrentMode 0
94 #define __CurrentSwitch 1
95 #define __NextSwitch 2
97 #define curr_mode (frame->Reserved[__CurrentMode])
98 #define curr_switch (frame->Reserved[__CurrentSwitch])
99 #define next_switch (frame->Reserved[__NextSwitch])
101 static BOOL
i386_stack_walk(struct cpu_stack_walk
* csw
, LPSTACKFRAME64 frame
)
103 STACK32FRAME frame32
;
104 STACK16FRAME frame16
;
112 if (curr_mode
>= stm_done
) return FALSE
;
114 TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s cSwitch=%p nSwitch=%p\n",
115 wine_dbgstr_addr(&frame
->AddrPC
),
116 wine_dbgstr_addr(&frame
->AddrFrame
),
117 wine_dbgstr_addr(&frame
->AddrReturn
),
118 wine_dbgstr_addr(&frame
->AddrStack
),
119 curr_mode
== stm_start
? "start" : (curr_mode
== stm_16bit
? "16bit" : "32bit"),
120 (void*)(DWORD_PTR
)curr_switch
, (void*)(DWORD_PTR
)next_switch
);
122 if (curr_mode
== stm_start
)
124 THREAD_BASIC_INFORMATION info
;
126 if ((frame
->AddrPC
.Mode
== AddrModeFlat
) &&
127 (frame
->AddrFrame
.Mode
!= AddrModeFlat
))
129 WARN("Bad AddrPC.Mode / AddrFrame.Mode combination\n");
134 curr_mode
= (frame
->AddrPC
.Mode
== AddrModeFlat
) ? stm_32bit
: stm_16bit
;
136 /* cur_switch holds address of WOW32Reserved field in TEB in debuggee
139 if (NtQueryInformationThread(csw
->hThread
, ThreadBasicInformation
, &info
,
140 sizeof(info
), NULL
) == STATUS_SUCCESS
)
142 curr_switch
= (unsigned long)info
.TebBaseAddress
+ FIELD_OFFSET(TEB
, WOW32Reserved
);
143 if (!sw_read_mem(csw
, curr_switch
, &p
, sizeof(p
)))
145 WARN("Can't read TEB:WOW32Reserved\n");
149 if (curr_mode
== stm_16bit
)
151 if (!sw_read_mem(csw
, next_switch
, &frame32
, sizeof(frame32
)))
153 WARN("Bad stack frame %p\n", (void*)(DWORD_PTR
)next_switch
);
156 curr_switch
= (DWORD
)frame32
.frame16
;
157 tmp
.Mode
= AddrMode1616
;
158 tmp
.Segment
= SELECTOROF(curr_switch
);
159 tmp
.Offset
= OFFSETOF(curr_switch
);
160 if (!sw_read_mem(csw
, sw_xlat_addr(csw
, &tmp
), &ch
, sizeof(ch
)))
161 curr_switch
= 0xFFFFFFFF;
165 tmp
.Mode
= AddrMode1616
;
166 tmp
.Segment
= SELECTOROF(next_switch
);
167 tmp
.Offset
= OFFSETOF(next_switch
);
168 p
= sw_xlat_addr(csw
, &tmp
);
169 if (!sw_read_mem(csw
, p
, &frame16
, sizeof(frame16
)))
171 WARN("Bad stack frame 0x%08x\n", p
);
174 curr_switch
= (DWORD_PTR
)frame16
.frame32
;
176 if (!sw_read_mem(csw
, curr_switch
, &ch
, sizeof(ch
)))
177 curr_switch
= 0xFFFFFFFF;
181 /* FIXME: this will allow to work when we're not attached to a live target,
182 * but the 16 <=> 32 switch facility won't be available.
185 frame
->AddrReturn
.Mode
= frame
->AddrStack
.Mode
= (curr_mode
== stm_16bit
) ? AddrMode1616
: AddrModeFlat
;
186 /* don't set up AddrStack on first call. Either the caller has set it up, or
187 * we will get it in the next frame
189 memset(&frame
->AddrBStore
, 0, sizeof(frame
->AddrBStore
));
193 if (frame
->AddrFrame
.Offset
== 0) goto done_err
;
194 if (frame
->AddrFrame
.Mode
== AddrModeFlat
)
196 assert(curr_mode
== stm_32bit
);
197 do_switch
= curr_switch
&& frame
->AddrFrame
.Offset
>= curr_switch
;
201 assert(curr_mode
== stm_16bit
);
202 do_switch
= curr_switch
&&
203 frame
->AddrFrame
.Segment
== SELECTOROF(curr_switch
) &&
204 frame
->AddrFrame
.Offset
>= OFFSETOF(curr_switch
);
209 if (curr_mode
== stm_16bit
)
211 if (!sw_read_mem(csw
, next_switch
, &frame32
, sizeof(frame32
)))
213 WARN("Bad stack frame %p\n", (void*)(DWORD_PTR
)next_switch
);
217 frame
->AddrPC
.Mode
= AddrModeFlat
;
218 frame
->AddrPC
.Segment
= 0;
219 frame
->AddrPC
.Offset
= frame32
.retaddr
;
220 frame
->AddrFrame
.Mode
= AddrModeFlat
;
221 frame
->AddrFrame
.Segment
= 0;
222 frame
->AddrFrame
.Offset
= frame32
.ebp
;
224 frame
->AddrStack
.Mode
= AddrModeFlat
;
225 frame
->AddrStack
.Segment
= 0;
226 frame
->AddrReturn
.Mode
= AddrModeFlat
;
227 frame
->AddrReturn
.Segment
= 0;
229 next_switch
= curr_switch
;
230 tmp
.Mode
= AddrMode1616
;
231 tmp
.Segment
= SELECTOROF(next_switch
);
232 tmp
.Offset
= OFFSETOF(next_switch
);
233 p
= sw_xlat_addr(csw
, &tmp
);
235 if (!sw_read_mem(csw
->hProcess
, p
, &frame16
, sizeof(frame16
)))
237 WARN("Bad stack frame 0x%08x\n", p
);
240 curr_switch
= (DWORD_PTR
)frame16
.frame32
;
241 curr_mode
= stm_32bit
;
242 if (!sw_read_mem(csw
, curr_switch
, &ch
, sizeof(ch
)))
247 tmp
.Mode
= AddrMode1616
;
248 tmp
.Segment
= SELECTOROF(next_switch
);
249 tmp
.Offset
= OFFSETOF(next_switch
);
250 p
= sw_xlat_addr(csw
, &tmp
);
252 if (!sw_read_mem(csw
, p
, &frame16
, sizeof(frame16
)))
254 WARN("Bad stack frame 0x%08x\n", p
);
258 TRACE("Got a 16 bit stack switch:"
260 "\n\tedx:%08x ecx:%08x ebp:%08x"
261 "\n\tds:%04x es:%04x fs:%04x gs:%04x"
262 "\n\tcall_from_ip:%08x module_cs:%04x relay=%08x"
263 "\n\tentry_ip:%04x entry_point:%08x"
264 "\n\tbp:%04x ip:%04x cs:%04x\n",
265 (unsigned long)frame16
.frame32
,
266 frame16
.edx
, frame16
.ecx
, frame16
.ebp
,
267 frame16
.ds
, frame16
.es
, frame16
.fs
, frame16
.gs
,
268 frame16
.callfrom_ip
, frame16
.module_cs
, frame16
.relay
,
269 frame16
.entry_ip
, frame16
.entry_point
,
270 frame16
.bp
, frame16
.ip
, frame16
.cs
);
272 frame
->AddrPC
.Mode
= AddrMode1616
;
273 frame
->AddrPC
.Segment
= frame16
.cs
;
274 frame
->AddrPC
.Offset
= frame16
.ip
;
276 frame
->AddrFrame
.Mode
= AddrMode1616
;
277 frame
->AddrFrame
.Segment
= SELECTOROF(next_switch
);
278 frame
->AddrFrame
.Offset
= frame16
.bp
;
280 frame
->AddrStack
.Mode
= AddrMode1616
;
281 frame
->AddrStack
.Segment
= SELECTOROF(next_switch
);
283 frame
->AddrReturn
.Mode
= AddrMode1616
;
284 frame
->AddrReturn
.Segment
= frame16
.cs
;
286 next_switch
= curr_switch
;
287 if (!sw_read_mem(csw
, next_switch
, &frame32
, sizeof(frame32
)))
289 WARN("Bad stack frame %p\n", (void*)(DWORD_PTR
)next_switch
);
292 curr_switch
= (DWORD
)frame32
.frame16
;
293 tmp
.Mode
= AddrMode1616
;
294 tmp
.Segment
= SELECTOROF(curr_switch
);
295 tmp
.Offset
= OFFSETOF(curr_switch
);
297 if (!sw_read_mem(csw
, sw_xlat_addr(csw
, &tmp
), &ch
, sizeof(ch
)))
299 curr_mode
= stm_16bit
;
304 frame
->AddrPC
= frame
->AddrReturn
;
305 if (curr_mode
== stm_16bit
)
307 frame
->AddrStack
.Offset
= frame
->AddrFrame
.Offset
+ 2 * sizeof(WORD
);
308 /* "pop up" previous BP value */
309 if (!sw_read_mem(csw
, sw_xlat_addr(csw
, &frame
->AddrFrame
),
312 frame
->AddrFrame
.Offset
= val
;
316 frame
->AddrStack
.Offset
= frame
->AddrFrame
.Offset
+ 2 * sizeof(DWORD
);
317 /* "pop up" previous EBP value */
318 if (!sw_read_mem(csw
, frame
->AddrFrame
.Offset
,
319 &frame
->AddrFrame
.Offset
, sizeof(DWORD
)))
325 if (curr_mode
== stm_16bit
)
329 p
= sw_xlat_addr(csw
, &frame
->AddrFrame
);
330 if (!sw_read_mem(csw
, p
+ sizeof(WORD
), &val
, sizeof(WORD
)))
332 frame
->AddrReturn
.Offset
= val
;
333 /* get potential cs if a far call was used */
334 if (!sw_read_mem(csw
, p
+ 2 * sizeof(WORD
), &val
, sizeof(WORD
)))
336 if (frame
->AddrFrame
.Offset
& 1)
337 frame
->AddrReturn
.Segment
= val
; /* far call assumed */
340 /* not explicitly marked as far call,
341 * but check whether it could be anyway
343 if ((val
& 7) == 7 && val
!= frame
->AddrReturn
.Segment
)
347 if (GetThreadSelectorEntry(csw
->hThread
, val
, &le
) &&
348 (le
.HighWord
.Bits
.Type
& 0x08)) /* code segment */
350 /* it is very uncommon to push a code segment cs as
351 * a parameter, so this should work in most cases
353 frame
->AddrReturn
.Segment
= val
;
357 frame
->AddrFrame
.Offset
&= ~1;
358 /* we "pop" parameters as 16 bit entities... of course, this won't
359 * work if the parameter is in fact bigger than 16bit, but
360 * there's no way to know that here
362 for (i
= 0; i
< sizeof(frame
->Params
) / sizeof(frame
->Params
[0]); i
++)
364 sw_read_mem(csw
, p
+ (2 + i
) * sizeof(WORD
), &val
, sizeof(val
));
365 frame
->Params
[i
] = val
;
370 if (!sw_read_mem(csw
, frame
->AddrFrame
.Offset
+ sizeof(DWORD
),
371 &frame
->AddrReturn
.Offset
, sizeof(DWORD
)))
373 WARN("Cannot read new frame offset %p\n",
374 (void*)(DWORD_PTR
)(frame
->AddrFrame
.Offset
+ (int)sizeof(DWORD
)));
377 sw_read_mem(csw
, frame
->AddrFrame
.Offset
+ 2 * sizeof(DWORD
),
378 frame
->Params
, sizeof(frame
->Params
));
382 frame
->Virtual
= TRUE
;
383 p
= sw_xlat_addr(csw
, &frame
->AddrPC
);
384 if (p
&& sw_module_base(csw
, p
))
385 frame
->FuncTableEntry
= sw_table_access(csw
, p
);
387 frame
->FuncTableEntry
= NULL
;
389 TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s cSwitch=%p nSwitch=%p FuncTable=%p\n",
390 wine_dbgstr_addr(&frame
->AddrPC
),
391 wine_dbgstr_addr(&frame
->AddrFrame
),
392 wine_dbgstr_addr(&frame
->AddrReturn
),
393 wine_dbgstr_addr(&frame
->AddrStack
),
394 curr_mode
== stm_start
? "start" : (curr_mode
== stm_16bit
? "16bit" : "32bit"),
395 (void*)(DWORD_PTR
)curr_switch
, (void*)(DWORD_PTR
)next_switch
, frame
->FuncTableEntry
);
399 curr_mode
= stm_done
;
403 struct cpu cpu_i386
= {
404 IMAGE_FILE_MACHINE_I386
,