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
, CONTEXT
* context
)
103 STACK32FRAME frame32
;
104 STACK16FRAME frame16
;
110 unsigned deltapc
= 1;
113 if (curr_mode
>= stm_done
) return FALSE
;
115 TRACE("Enter: PC=%s Frame=%s Return=%s Stack=%s Mode=%s cSwitch=%p nSwitch=%p\n",
116 wine_dbgstr_addr(&frame
->AddrPC
),
117 wine_dbgstr_addr(&frame
->AddrFrame
),
118 wine_dbgstr_addr(&frame
->AddrReturn
),
119 wine_dbgstr_addr(&frame
->AddrStack
),
120 curr_mode
== stm_start
? "start" : (curr_mode
== stm_16bit
? "16bit" : "32bit"),
121 (void*)(DWORD_PTR
)curr_switch
, (void*)(DWORD_PTR
)next_switch
);
123 if (curr_mode
== stm_start
)
125 THREAD_BASIC_INFORMATION info
;
127 if ((frame
->AddrPC
.Mode
== AddrModeFlat
) &&
128 (frame
->AddrFrame
.Mode
!= AddrModeFlat
))
130 WARN("Bad AddrPC.Mode / AddrFrame.Mode combination\n");
135 curr_mode
= (frame
->AddrPC
.Mode
== AddrModeFlat
) ? stm_32bit
: stm_16bit
;
138 /* cur_switch holds address of WOW32Reserved field in TEB in debuggee
141 if (NtQueryInformationThread(csw
->hThread
, ThreadBasicInformation
, &info
,
142 sizeof(info
), NULL
) == STATUS_SUCCESS
)
144 curr_switch
= (unsigned long)info
.TebBaseAddress
+ FIELD_OFFSET(TEB
, WOW32Reserved
);
145 if (!sw_read_mem(csw
, curr_switch
, &p
, sizeof(p
)))
147 WARN("Can't read TEB:WOW32Reserved\n");
151 if (!next_switch
) /* no 16-bit stack */
155 else if (curr_mode
== stm_16bit
)
157 if (!sw_read_mem(csw
, next_switch
, &frame32
, sizeof(frame32
)))
159 WARN("Bad stack frame %p\n", (void*)(DWORD_PTR
)next_switch
);
162 curr_switch
= (DWORD
)frame32
.frame16
;
163 tmp
.Mode
= AddrMode1616
;
164 tmp
.Segment
= SELECTOROF(curr_switch
);
165 tmp
.Offset
= OFFSETOF(curr_switch
);
166 if (!sw_read_mem(csw
, sw_xlat_addr(csw
, &tmp
), &ch
, sizeof(ch
)))
167 curr_switch
= 0xFFFFFFFF;
171 tmp
.Mode
= AddrMode1616
;
172 tmp
.Segment
= SELECTOROF(next_switch
);
173 tmp
.Offset
= OFFSETOF(next_switch
);
174 p
= sw_xlat_addr(csw
, &tmp
);
175 if (!sw_read_mem(csw
, p
, &frame16
, sizeof(frame16
)))
177 WARN("Bad stack frame 0x%08x\n", p
);
180 curr_switch
= (DWORD_PTR
)frame16
.frame32
;
182 if (!sw_read_mem(csw
, curr_switch
, &ch
, sizeof(ch
)))
183 curr_switch
= 0xFFFFFFFF;
187 /* FIXME: this will allow to work when we're not attached to a live target,
188 * but the 16 <=> 32 switch facility won't be available.
191 frame
->AddrReturn
.Mode
= frame
->AddrStack
.Mode
= (curr_mode
== stm_16bit
) ? AddrMode1616
: AddrModeFlat
;
192 /* don't set up AddrStack on first call. Either the caller has set it up, or
193 * we will get it in the next frame
195 memset(&frame
->AddrBStore
, 0, sizeof(frame
->AddrBStore
));
197 if (curr_mode
== stm_32bit
)
201 if (dwarf2_virtual_unwind(csw
, frame
->AddrPC
.Offset
- deltapc
, context
, &xframe
))
203 frame
->AddrStack
.Mode
= frame
->AddrFrame
.Mode
= frame
->AddrReturn
.Mode
= AddrModeFlat
;
204 frame
->AddrStack
.Offset
= context
->Esp
= xframe
;
205 frame
->AddrFrame
.Offset
= context
->Ebp
;
206 frame
->AddrReturn
.Offset
= context
->Eip
;
214 if (frame
->AddrFrame
.Offset
== 0) goto done_err
;
215 if (frame
->AddrFrame
.Mode
== AddrModeFlat
)
217 assert(curr_mode
== stm_32bit
);
218 do_switch
= curr_switch
&& frame
->AddrFrame
.Offset
>= curr_switch
;
222 assert(curr_mode
== stm_16bit
);
223 do_switch
= curr_switch
&&
224 frame
->AddrFrame
.Segment
== SELECTOROF(curr_switch
) &&
225 frame
->AddrFrame
.Offset
>= OFFSETOF(curr_switch
);
230 if (curr_mode
== stm_16bit
)
232 if (!sw_read_mem(csw
, next_switch
, &frame32
, sizeof(frame32
)))
234 WARN("Bad stack frame %p\n", (void*)(DWORD_PTR
)next_switch
);
238 frame
->AddrPC
.Mode
= AddrModeFlat
;
239 frame
->AddrPC
.Segment
= 0;
240 frame
->AddrPC
.Offset
= frame32
.retaddr
;
241 frame
->AddrFrame
.Mode
= AddrModeFlat
;
242 frame
->AddrFrame
.Segment
= 0;
243 frame
->AddrFrame
.Offset
= frame32
.ebp
;
245 frame
->AddrStack
.Mode
= AddrModeFlat
;
246 frame
->AddrStack
.Segment
= 0;
247 frame
->AddrReturn
.Mode
= AddrModeFlat
;
248 frame
->AddrReturn
.Segment
= 0;
250 next_switch
= curr_switch
;
251 tmp
.Mode
= AddrMode1616
;
252 tmp
.Segment
= SELECTOROF(next_switch
);
253 tmp
.Offset
= OFFSETOF(next_switch
);
254 p
= sw_xlat_addr(csw
, &tmp
);
256 if (!sw_read_mem(csw
, p
, &frame16
, sizeof(frame16
)))
258 WARN("Bad stack frame 0x%08x\n", p
);
261 curr_switch
= (DWORD_PTR
)frame16
.frame32
;
262 curr_mode
= stm_32bit
;
263 if (!sw_read_mem(csw
, curr_switch
, &ch
, sizeof(ch
)))
268 tmp
.Mode
= AddrMode1616
;
269 tmp
.Segment
= SELECTOROF(next_switch
);
270 tmp
.Offset
= OFFSETOF(next_switch
);
271 p
= sw_xlat_addr(csw
, &tmp
);
273 if (!sw_read_mem(csw
, p
, &frame16
, sizeof(frame16
)))
275 WARN("Bad stack frame 0x%08x\n", p
);
279 TRACE("Got a 16 bit stack switch:"
281 "\n\tedx:%08x ecx:%08x ebp:%08x"
282 "\n\tds:%04x es:%04x fs:%04x gs:%04x"
283 "\n\tcall_from_ip:%08x module_cs:%04x relay=%08x"
284 "\n\tentry_ip:%04x entry_point:%08x"
285 "\n\tbp:%04x ip:%04x cs:%04x\n",
286 (unsigned long)frame16
.frame32
,
287 frame16
.edx
, frame16
.ecx
, frame16
.ebp
,
288 frame16
.ds
, frame16
.es
, frame16
.fs
, frame16
.gs
,
289 frame16
.callfrom_ip
, frame16
.module_cs
, frame16
.relay
,
290 frame16
.entry_ip
, frame16
.entry_point
,
291 frame16
.bp
, frame16
.ip
, frame16
.cs
);
293 frame
->AddrPC
.Mode
= AddrMode1616
;
294 frame
->AddrPC
.Segment
= frame16
.cs
;
295 frame
->AddrPC
.Offset
= frame16
.ip
;
297 frame
->AddrFrame
.Mode
= AddrMode1616
;
298 frame
->AddrFrame
.Segment
= SELECTOROF(next_switch
);
299 frame
->AddrFrame
.Offset
= frame16
.bp
;
301 frame
->AddrStack
.Mode
= AddrMode1616
;
302 frame
->AddrStack
.Segment
= SELECTOROF(next_switch
);
304 frame
->AddrReturn
.Mode
= AddrMode1616
;
305 frame
->AddrReturn
.Segment
= frame16
.cs
;
307 next_switch
= curr_switch
;
308 if (!sw_read_mem(csw
, next_switch
, &frame32
, sizeof(frame32
)))
310 WARN("Bad stack frame %p\n", (void*)(DWORD_PTR
)next_switch
);
313 curr_switch
= (DWORD
)frame32
.frame16
;
314 tmp
.Mode
= AddrMode1616
;
315 tmp
.Segment
= SELECTOROF(curr_switch
);
316 tmp
.Offset
= OFFSETOF(curr_switch
);
318 if (!sw_read_mem(csw
, sw_xlat_addr(csw
, &tmp
), &ch
, sizeof(ch
)))
320 curr_mode
= stm_16bit
;
325 frame
->AddrPC
= frame
->AddrReturn
;
326 if (curr_mode
== stm_16bit
)
328 frame
->AddrStack
.Offset
= frame
->AddrFrame
.Offset
+ 2 * sizeof(WORD
);
329 /* "pop up" previous BP value */
330 if (!sw_read_mem(csw
, sw_xlat_addr(csw
, &frame
->AddrFrame
),
333 frame
->AddrFrame
.Offset
= val
;
340 if (dwarf2_virtual_unwind(csw
, frame
->AddrPC
.Offset
- deltapc
, context
, &xframe
))
342 frame
->AddrStack
.Mode
= frame
->AddrFrame
.Mode
= frame
->AddrReturn
.Mode
= AddrModeFlat
;
343 frame
->AddrStack
.Offset
= context
->Esp
= xframe
;
344 frame
->AddrFrame
.Offset
= context
->Ebp
;
345 frame
->AddrReturn
.Offset
= context
->Eip
;
349 frame
->AddrStack
.Offset
= frame
->AddrFrame
.Offset
+ 2 * sizeof(DWORD
);
350 /* "pop up" previous EBP value */
351 if (!sw_read_mem(csw
, frame
->AddrFrame
.Offset
,
352 &frame
->AddrFrame
.Offset
, sizeof(DWORD
)))
358 if (curr_mode
== stm_16bit
)
362 p
= sw_xlat_addr(csw
, &frame
->AddrFrame
);
363 if (!sw_read_mem(csw
, p
+ sizeof(WORD
), &val
, sizeof(WORD
)))
365 frame
->AddrReturn
.Offset
= val
;
366 /* get potential cs if a far call was used */
367 if (!sw_read_mem(csw
, p
+ 2 * sizeof(WORD
), &val
, sizeof(WORD
)))
369 if (frame
->AddrFrame
.Offset
& 1)
370 frame
->AddrReturn
.Segment
= val
; /* far call assumed */
373 /* not explicitly marked as far call,
374 * but check whether it could be anyway
376 if ((val
& 7) == 7 && val
!= frame
->AddrReturn
.Segment
)
380 if (GetThreadSelectorEntry(csw
->hThread
, val
, &le
) &&
381 (le
.HighWord
.Bits
.Type
& 0x08)) /* code segment */
383 /* it is very uncommon to push a code segment cs as
384 * a parameter, so this should work in most cases
386 frame
->AddrReturn
.Segment
= val
;
390 frame
->AddrFrame
.Offset
&= ~1;
391 /* we "pop" parameters as 16 bit entities... of course, this won't
392 * work if the parameter is in fact bigger than 16bit, but
393 * there's no way to know that here
395 for (i
= 0; i
< sizeof(frame
->Params
) / sizeof(frame
->Params
[0]); i
++)
397 sw_read_mem(csw
, p
+ (2 + i
) * sizeof(WORD
), &val
, sizeof(val
));
398 frame
->Params
[i
] = val
;
403 if (!sw_read_mem(csw
, frame
->AddrFrame
.Offset
+ sizeof(DWORD
),
404 &frame
->AddrReturn
.Offset
, sizeof(DWORD
)))
406 WARN("Cannot read new frame offset %p\n",
407 (void*)(DWORD_PTR
)(frame
->AddrFrame
.Offset
+ (int)sizeof(DWORD
)));
410 sw_read_mem(csw
, frame
->AddrFrame
.Offset
+ 2 * sizeof(DWORD
),
411 frame
->Params
, sizeof(frame
->Params
));
413 goto done_pep
; /* just to ensure done_pep label is referenced */
417 frame
->Virtual
= TRUE
;
418 p
= sw_xlat_addr(csw
, &frame
->AddrPC
);
419 if (p
&& sw_module_base(csw
, p
))
420 frame
->FuncTableEntry
= sw_table_access(csw
, p
);
422 frame
->FuncTableEntry
= NULL
;
424 TRACE("Leave: PC=%s Frame=%s Return=%s Stack=%s Mode=%s cSwitch=%p nSwitch=%p FuncTable=%p\n",
425 wine_dbgstr_addr(&frame
->AddrPC
),
426 wine_dbgstr_addr(&frame
->AddrFrame
),
427 wine_dbgstr_addr(&frame
->AddrReturn
),
428 wine_dbgstr_addr(&frame
->AddrStack
),
429 curr_mode
== stm_start
? "start" : (curr_mode
== stm_16bit
? "16bit" : "32bit"),
430 (void*)(DWORD_PTR
)curr_switch
, (void*)(DWORD_PTR
)next_switch
, frame
->FuncTableEntry
);
434 curr_mode
= stm_done
;
438 static unsigned i386_map_dwarf_register(unsigned regno
)
444 case 0: reg
= CV_REG_EAX
; break;
445 case 1: reg
= CV_REG_ECX
; break;
446 case 2: reg
= CV_REG_EDX
; break;
447 case 3: reg
= CV_REG_EBX
; break;
448 case 4: reg
= CV_REG_ESP
; break;
449 case 5: reg
= CV_REG_EBP
; break;
450 case 6: reg
= CV_REG_ESI
; break;
451 case 7: reg
= CV_REG_EDI
; break;
452 case 8: reg
= CV_REG_EIP
; break;
453 case 9: reg
= CV_REG_EFLAGS
; break;
454 case 10: reg
= CV_REG_CS
; break;
455 case 11: reg
= CV_REG_SS
; break;
456 case 12: reg
= CV_REG_DS
; break;
457 case 13: reg
= CV_REG_ES
; break;
458 case 14: reg
= CV_REG_FS
; break;
459 case 15: reg
= CV_REG_GS
; break;
460 case 16: case 17: case 18: case 19:
461 case 20: case 21: case 22: case 23:
462 reg
= CV_REG_ST0
+ regno
- 16; break;
463 case 24: reg
= CV_REG_CTRL
; break;
464 case 25: reg
= CV_REG_STAT
; break;
465 case 26: reg
= CV_REG_TAG
; break;
473 case 32: case 33: case 34: case 35:
474 case 36: case 37: case 38: case 39:
475 reg
= CV_REG_XMM0
+ regno
- 32; break;
476 case 40: reg
= CV_REG_MXCSR
; break;
478 FIXME("Don't know how to map register %d\n", regno
);
484 static void* i386_fetch_context_reg(CONTEXT
* ctx
, unsigned regno
, unsigned* size
)
489 case CV_REG_EAX
: *size
= sizeof(ctx
->Eax
); return &ctx
->Eax
;
490 case CV_REG_EDX
: *size
= sizeof(ctx
->Edx
); return &ctx
->Edx
;
491 case CV_REG_ECX
: *size
= sizeof(ctx
->Ecx
); return &ctx
->Ecx
;
492 case CV_REG_EBX
: *size
= sizeof(ctx
->Ebx
); return &ctx
->Ebx
;
493 case CV_REG_ESI
: *size
= sizeof(ctx
->Esi
); return &ctx
->Esi
;
494 case CV_REG_EDI
: *size
= sizeof(ctx
->Edi
); return &ctx
->Edi
;
495 case CV_REG_EBP
: *size
= sizeof(ctx
->Ebp
); return &ctx
->Ebp
;
496 case CV_REG_ESP
: *size
= sizeof(ctx
->Esp
); return &ctx
->Esp
;
497 case CV_REG_EIP
: *size
= sizeof(ctx
->Eip
); return &ctx
->Eip
;
499 case CV_REG_ST0
+ 0: *size
= sizeof(long double); return &ctx
->FloatSave
.RegisterArea
[0*sizeof(long double)];
500 case CV_REG_ST0
+ 1: *size
= sizeof(long double); return &ctx
->FloatSave
.RegisterArea
[1*sizeof(long double)];
501 case CV_REG_ST0
+ 2: *size
= sizeof(long double); return &ctx
->FloatSave
.RegisterArea
[2*sizeof(long double)];
502 case CV_REG_ST0
+ 3: *size
= sizeof(long double); return &ctx
->FloatSave
.RegisterArea
[3*sizeof(long double)];
503 case CV_REG_ST0
+ 4: *size
= sizeof(long double); return &ctx
->FloatSave
.RegisterArea
[4*sizeof(long double)];
504 case CV_REG_ST0
+ 5: *size
= sizeof(long double); return &ctx
->FloatSave
.RegisterArea
[5*sizeof(long double)];
505 case CV_REG_ST0
+ 6: *size
= sizeof(long double); return &ctx
->FloatSave
.RegisterArea
[6*sizeof(long double)];
506 case CV_REG_ST0
+ 7: *size
= sizeof(long double); return &ctx
->FloatSave
.RegisterArea
[7*sizeof(long double)];
508 case CV_REG_EFLAGS
: *size
= sizeof(ctx
->EFlags
); return &ctx
->EFlags
;
509 case CV_REG_ES
: *size
= sizeof(ctx
->SegEs
); return &ctx
->SegEs
;
510 case CV_REG_CS
: *size
= sizeof(ctx
->SegCs
); return &ctx
->SegCs
;
511 case CV_REG_SS
: *size
= sizeof(ctx
->SegSs
); return &ctx
->SegSs
;
512 case CV_REG_DS
: *size
= sizeof(ctx
->SegDs
); return &ctx
->SegDs
;
513 case CV_REG_FS
: *size
= sizeof(ctx
->SegFs
); return &ctx
->SegFs
;
514 case CV_REG_GS
: *size
= sizeof(ctx
->SegGs
); return &ctx
->SegGs
;
518 FIXME("Unknown register %x\n", regno
);
522 static const char* i386_fetch_regname(unsigned regno
)
526 case CV_REG_EAX
: return "eax";
527 case CV_REG_EDX
: return "edx";
528 case CV_REG_ECX
: return "ecx";
529 case CV_REG_EBX
: return "ebx";
530 case CV_REG_ESI
: return "esi";
531 case CV_REG_EDI
: return "edi";
532 case CV_REG_EBP
: return "ebp";
533 case CV_REG_ESP
: return "esp";
534 case CV_REG_EIP
: return "eip";
536 case CV_REG_ST0
+ 0: return "st0";
537 case CV_REG_ST0
+ 1: return "st1";
538 case CV_REG_ST0
+ 2: return "st2";
539 case CV_REG_ST0
+ 3: return "st3";
540 case CV_REG_ST0
+ 4: return "st4";
541 case CV_REG_ST0
+ 5: return "st5";
542 case CV_REG_ST0
+ 6: return "st6";
543 case CV_REG_ST0
+ 7: return "st7";
545 case CV_REG_EFLAGS
: return "eflags";
546 case CV_REG_ES
: return "es";
547 case CV_REG_CS
: return "cs";
548 case CV_REG_SS
: return "ss";
549 case CV_REG_DS
: return "ds";
550 case CV_REG_FS
: return "fs";
551 case CV_REG_GS
: return "gs";
553 FIXME("Unknown register %x\n", regno
);
557 struct cpu cpu_i386
= {
558 IMAGE_FILE_MACHINE_I386
,
563 i386_map_dwarf_register
,
564 i386_fetch_context_reg
,