Debugging in Windows Debugging in Windows: Crash Dump Analysis 2014/2015
Debugging in Windows Debugging in Windows: Crash Dump Analysis 2014/2015
http://d3s.mff.cuni.cz
Debugging in Windows
Calling conventions
cdecl (C calling convention)
Almost identical to System V ABI on IA-32
Arguments passed on stack in reverse order
Support for variadic functions
Caller cleans the stack (pops the arguments)
leave
ret
Debugging in Windows
Different prologue
enter $imm, 0
Debugging in Windows
thiscall
For C++
Almost identical to stdcall
Implicit object argument (*this) passed in ECX
Debugging in Windows
Debugging in Windows
Debugging facilities
User space debuggers
Common debugging API (dbghelp.dll)
Standard debuggers
Visual Studio Debugger
CDB, NTSD
Kernel debugger
Part of Windows NT kernel
KD
Remote debugging (serial line, FireWire, USB 2.0, VMware extension)
Debugging in Windows
Debugging in Windows
Resources
Debugging tools for Windows
WinDbg and related tools
http://www.microsoft.com/whdc/devtools/debugging/
Documentation in MSDN
https://msdn.microsoft.com/en-us/library/ff551063.aspx
Tutorials
http://www.codeproject.com/Articles/6084/Windows-Debuggers-Part-A-WinDbg-Tutorial
WinDbg from A to Z
http://windbg.info/
Debugging in Windows
Debugging API
Common methods for writing debuggers
Parsing binaries (ImageNtHeader)
Dumping core (MiniDumpWriteDump)
Generating stack trace (StackWalk)
Symbol handling (SymFromAddr)
Original symbol information format: COFF
Current symbol information format: PDB file
Debugging in Windows
10
Symbols
Symbols location
_NT_SYMBOL_PATH environment variable
Binaries and symbols matched according to compilation
timestamp and/or GUID
Symbols for Windows components (all public builds)
Available from Microsoft public symbol server
Can be also downloaded by hand (hundreds of MBs)
Debugging in Windows
11
CDB
Command line user space debugger
NTSD is almost identical, but it is not a console
application
Debugging modes
Invasive debugging
A break-in thread in target process
Full-featured debugging (but only one debugging session)
Non-invasive debugging
Debugging in Windows
12
KD
Command line kernel debugger
Local kernel debugging very limited
Remote debugging
Serial line
Limited to 115 kbaud
VMware virtual serial line can be much faster
USB 2.0
Debugging in Windows
13
WinDbg
Universal GUI front-end
Both for CDB and KB
Running processes
Attaching to existing processes
Opening core and crash dumps
Remote debugging
Debugging in Windows
14
Remote debugging
For user space applications
Debugging target
dbgsrv.exe -t tcp:port=1025
Debugging client
windbg.exe -premote tcp:server=hostname,port=1025
Useful commands
.tlist
List processes running on the target
Debugging in Windows
15
WinDbg commands
Regular commands
No prefix, but
possible suffixes
(variants)
Controlling the
debugging session
? <cmd>
t
Step into
pt
Step over until next
return
tt
Help on cmd
g
Continue execution
Debugging in Windows
16
r
Step over until next
call (if the current
instruction is a call,
then it is ignored)
tc
Step into until next
call
pa <addr>
u [addr]
Disassemble
lm
List loaded modules
(DLLs)
k
Print the stack trace
Debugging in Windows
17
kP
Get information from
all threads
~.
Get the current
thread information
~[tid]
Get information from
the thread tid
kv
Print the stack trace
with the information
about calling
conventions
~* k
Print the stack trace
of all threads
Debugging in Windows
18
bp <addr>
Set execution
breakpoint at addr
Display doubleword,
ASCII, Unicode at
addr
f <addr> <value>
...
Fill the memory at
addr with the values
bl
List breakpoints
Debugging in Windows
ba <addr>
Set memory access
breakpoint at addr
bc <addr>
Clear breakpoint at addr
be <addr>
bd <addr>
Enable/disable
breakpoint at addr
19
WinDbg expressions
?? <expr>
@@c++(<expr>)
Return the value of any C++ expression which does not have any
side effects (i.e. no function calls)
Compound types, arrays, pointer arithmetics, etc.
Debugging in Windows
20
Advanced breakpoints
bp module!my_func_*
Breakpoints on multiple functions (wildcards)
bp @@c++(MyClass:MyMethod)
Breakpoint on a member function of all instances of a
class
~1 bu kernel32!LoadLibraryExW
Breakpoint on a function which hits only in a given
thread
Lazy symbol resolving
Debugging in Windows
21
.attach <pid>
Attach to a process
pid
Slightly more
advanved
.detach
.help <cmd>
Help on dot-cmd
.lastevent
Information about
last event/exception
.restart
Restart the attached
process
.dump
Create a core dump
Debugging in Windows
22
Debugging in Windows
.for ...
.while <expr>
.Break
.continue
Advanced scripting
.foreach <cmd>
The output of cmd is fed
to a other commands
Usually line-by-line
The semantics
differs for each cmd
23
!locks
Display information about locked
critical sections
!address <addr>
Display information (protection
status, owner) of the given page
Debugging in Windows
!analyze
!analyze -hang
Various heuristics for
analyzing the root cause
of the previous
event/exception
Runs various consistency
checks on kernel
structures
Stack analysis, heap
analysis
Corrupted code stream
analysis (bad RAM)
Invalid call sequences
(bad CPU)
24
WinDbg pseudoregisters
Various values useful for
debugging
Can be used in expressions or
directly as command
arguments
$ra
Current stack frame return
address
$csp
Current stack pointer
(ESP, RSP)
$tpid
Current process ID
$tid
Current thread ID
$ip
Current instruction address
(EIP, RIP)
$retreg
Current value of the return
register (EAX, RAX)
Debugging in Windows
25