Merged display.dll into USER.
[wine.git] / documentation / winedbg
blob04835ed2587de18fc0643643a0d8630804987ac4
1 I Introduction
2 ==============
4 I.1 Processes and threads: in underlying OS and in Windows
5 ----------------------------------------------------------
6 Before going into the depths of debugging in Wine, here's a small
7 overview of process and thread handling in Wine. It has to be clear
8 that there are two different beasts: processes/threads from the Unix
9 point of view and processes/threads from a Windows point of view.
11 Each Windows' thread is implemented as a Unix process (under Linux
12 using the clone syscall), meaning that all threads of a same Windows'
13 process share the same (unix) address space (currently, one of wine
14 limitation is that several windows processes run in the same (unix)
15 address space. it's being worked on).
17 In the following:
18 + W-process means a process in Windows' terminology
19 + U-process means a process in Unix' terminology
20 + W-thread means a thread in Windows' terminology
22 A W-process is made of one or several W-threads.
23 Each W-thread is mapped to one and only one U-process. All U-processes 
24 of a same W-process share the same address space.
26 Each Unix process can be identified by two values:
27 - the Unix process id (upid in the following)
28 - the Windows's thread id (tid)
29 Each Windows' process has also a Windows' process (wpid in the
30 following). It must be clear that upid and wpid are different and
31 shall not be used instead of the other.
33 Wpid and tid are defined (Windows) system wide. They must not be
34 confused with process or thread handles which, as any handle, is an
35 indirection to a system object (in this case process or thread). A
36 same process can have several different handles on the same kernel
37 object. The handles can be defined as local (the values is only valid
38 in a process), or system wide (the same handle can be used by any
39 W-process).
42 I.2 Wine, debugging and WineDbg
43 -------------------------------
44 When talking of debugging in Wine, there are at least two levels to
45 think of:
46 + the Windows' debugging API.
47 + the Wine integrated debugger, dubbed WineDbg.
49 Wine implements most the the Windows' debugging API (the part in
50 KERNEL32, not the one in IMAGEHLP.DLL), and allows any program
51 (emulated or WineLib) using that API to debug a W-process.
53 WineDbg is a WineLib application making use of this API to allow
54 debugging both any Wine or WineLib applications as well as Wine itself 
55 (kernel and all DLLs).
57 II WineDbg's modes of invocation
58 ================================
60 II.1 Starting a process
61 -----------------------
62 Any application (either a Windows' native executable, or a WineLib
63 application) can be run through WineDbg. Command line options and
64 tricks are the same than for wine:
66 winedbg telnet.exe
67 winedbg "hl.exe -windowed"
69 II.2 Attaching
70 --------------
71 WineDbg can also launched without any command line argument:
72 - if a wineserver is running, WineDbg lists the running W-processes
73 (and their wpid:s), and let you pick up the wpid of the W-process you
74 want to debug.
76 This is (for now) a neat feature for the following reasons: 
77 * debug an already started application
78 + launching WineDbg this way let WineDbg and the debugged process run
79 in a *separate address space* (launching with 'winedbg myprog.exe' 
80 doesn't), and most of the deadlocks seen when running the debugger
81 disappear (because there is no crit sect shared by both
82 processes). That's the best (but far from being acceptable) current
83 way to debug an application
85 This last advantage shall disappear when address space separation is
86 in place. At that time, only the ability to debug an already started
87 process will remain.
89 II.3 On exception
90 -----------------
91 When something goes wrong, Windows track this as an
92 exception. Exceptions exist for segmentation violation, stack
93 overflow, division by zero...
95 When an exception occurs, Wine checks if the W-process is debugged. If
96 so, the exception event is sent to the debugger, which takes care of
97 it: end of the story. This mechanism is part of the standard Windows'
98 debugging API. 
100 If the W-process is not debugged, Wine tries to launch a
101 debugger. This debugger (normally WineDbg, see III Configuration for
102 more details), at startup, attaches to the W-process which generated
103 the exception event. In this case, you are able to look at the causes
104 of the exception, and either fix the causes (and continue further the
105 execution) or dig deeper to understand what went wrong.
107 If WineDbg is the standard debugger, the 'pass' and 'cont' commands
108 are the two ways to let the process go further for the handling of the 
109 exception event.
111 To be more precise on the way Wine (and Windows) generates exception
112 events, when a fault occurs (segmentation violation, stack
113 overflow...), the event is first sent to the debugger (this is know as 
114 a first chance exception). The debugger can give two answers:
115 - continue: the debugger had the ability to correct what's generated
116 the exception, and is now able to continue process execution.
117 - pass: the debugger couldn't correct the cause of the (first chance
118 exception). Wine will now try to walk the list of exception handlers
119 to see if one of them can handle the exception. If no exception
120 handler is found, the exception is sent once again to the debugger to
121 indicate the failure of the exception handling.
123 Note: since some of Wine's code uses exceptions and try/catch blocks
124 to provide some functionality, WineDbg can be entered in such cases
125 with segv exceptions. This happens, for example, with IsBadReadPtr
126 function. In that case, the pass command shall be used, to let the
127 handling of the exception to be done by the catch block in
128 IsBadReadPtr.
130 II.4 Quitting
131 -------------
132 Unfortunately, Windows' don't provide a detach kind of API, meaning
133 that once you started debugging a process, you must do so until the
134 process dies. Killing (or stopping/aborting) the debugger will also
135 kill the debugged process.
136 This will be true for any Windows' debugging API compliant debugger,
137 starting with WineDbg.
139 III Configuration
140 =================
142 III.1 Registry configuration
143 ----------------------------
144 The Windows' debugging API uses a registry entry to know with debugger 
145 to invoke when an unhandled exception occurs (see II.3 for some
146 details).
147 Two values in key 
148 "MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"
149 determine the behavior:
150 + Debugger: this is the command line used to launch the debugger (it
151 uses two printf formats (%ld) to pass context dependent information to 
152 the debugger). You should put here a complete path to your debugger
153 (WineDbg can of course be used, but any other Windows' debugging API
154 aware debugger will do).
155 + Auto: if this value is zero, a message box will ask the user if
156 he/she wishes to launch the debugger when an unhandled exception
157 occurs. Otherwise, the debugger is automatically started.
159 A regular Wine registry looks like:
160 [MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug] 957636538
161 "Auto"=dword:00000001
162 "Debugger"="/usr/local/bin/winedbg %ld %ld"
164 Note 1: creating this key is mandatory. Not doing so will not fire the
165 debugger when an exception occurs.
167 Note 2: wineinstall sets up this correctly. However, due to some
168 limitation of the registry installed, if a previous Wine installation
169 exists, it's safer to remove the whole 
170 [MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug]
171 key before running again wineinstall to regenerate this key.
173 III.2 WineDbg configuration
174 ---------------------------
175 WineDbg can be configured thru a number of options. Those options are
176 stored in the registry, on a per user basis. The key is (in *my*
177 registry) [eric\\Software\\Wine\\WineDbg]
178 Those options can be read/written while inside WineDbg, as part of the 
179 debugger expressions. To refer to one of this option, its name must be 
180 prefixed by a $ sign.
181 For example, 
182 set $BreakAllThreadsStartup = 1
183 sets the option 'BreakAllThreadsStartup' to TRUE.
184 All the options are read from the registry when WineDbg starts (if no
185 corresponding value is found, a default value is used), and are
186 written back to the registry when WineDbg exits (hence, all
187 modifications to those options are automatically saved when WineDbg
188 terminates).
190 Here's the list of all options:
191 BreakAllThreadsStartup  set to TRUE if at all threads start-up the
192                         debugger stops 
193                         set to FALSE if only at the first thread
194                         startup of a given process the debugger stops.
195                         FALSE by default.
196 BreakOnCritSectTimeOut  set to TRUE if the debugger stops when a
197                         critical section times out (5 minutes);
198                         TRUE by default.
199 BreakOnAttach,          set to TRUE if when WineDbg attaches to an
200                         existing process after an unhandled exception,
201                         WineDbg shall be entered on the first attach
202                         event.
203                         Since the attach event is meaningless in the 
204                         context of an exception event (the next event 
205                         which is the exception event is of course
206                         relevant), that option is likely to be FALSE.
208 ConChannelMask          mask of active debugger output channels on
209                         console 
210 StdChannelMask          mask of active debugger output channels on
211                         stderr 
212 UseXTerm                set to TRUE if the debugger uses its own xterm
213                         window for console input/output 
214                         set to FALSE is the debugger uses the current
215                         Unix console for input/output
217 Those last 3 variables are jointly used in two generic ways:
218 1/ default
219         ConChannelMask = DBG_CHN_MESG (1)
220         StdChannelMask = 0
221         UseXTerm = 1
222 In this case, all input/output goes into a specific xterm window (but
223 all debug messages TRACE/WARN... still goes to tty where wine is run
224 from).
226 2/ to have all input/output go into the tty where Wine was started
227 from (to be used in a X11-free environment)
228         ConChannelMask = 0
229         StdChannelMask = DBG_CHN_MESG (1)
230         UseXTerm = 1
232 Those variables also allow, for example for debugging purposes, to
233 use: 
234         ConChannelMask = 0xfff
235         StdChannelMask = 0xfff
236         UseXTerm = 1
237 This allows to redirect all WineDbg output to both tty Wine was
238 started from, and xterm debugging window. If Wine (or WineDbg) was
239 started with a redirection of stdout and/or stderr to a file (with for 
240 example >& shell redirect command), you'll get in that file both
241 outputs. It may be interesting to look in the relay trace for specific
242 values which the process segv:ed on.
244 IV WineDbg commands
245 ===================
247 IV.1 Misc
248 ---------
249 abort           aborts the debugger
250 quit            exits the debugger
252 help            prints some help on the commands
253 help info       prints some help on info commands
255 mode 16         switch to 16 bit mode
256 mode 32         switch to 32 bit mode
258 IV.2 Flow control
259 -----------------
260 cont            continue execution until next breakpoint or exception.
261 pass            pass the exception event up to the filter chain. 
262 step            continue execution until next C line of code (enters
263                 function call)
264 next            continue execution until next C line of code (doesn't
265                 enter function call) 
266 stepi           execute next assembly instruction (enters function
267                 call)
268 nexti           execute next assembly instruction (doesn't enter
269                 function call)
270 finish          do nexti commands until current function is exited
272 cont, step, next, stepi, nexti can be postfixed by a number (N),
273 meaning that the command must be executed N times.
275 IV.3 Breakpoints, watch points
276 ------------------------------
277 enable N        enables (break|watch)point #N
278 disable N       disables (break|watch)point #N
279 delete N        deletes  (break|watch)point #N
280 cond N          removes any a existing condition to (break|watch)point N
281 cond N <expr>   adds condition <expr> to (break|watch)point N. <expr>
282                 will be evaluated each time the breakpoint is hit. If
283                 the result is a zero value, the breakpoint isn't
284                 triggered 
285 break * N       adds a breakpoint at address N
286 break <id>      adds a breakpoint at the address of symbol <id>
287 break <id> N    adds a breakpoint at the address of symbol <id> (N ?)
288 break N         adds a breakpoint at line N of current source file
289 break           adds a breakpoint at current $pc address
290 watch * N       adds a watch command (on write) at address N (on 4 bytes)
291 watch <id>      adds a watch command (on write) at the address of
292                 symbol <id>
293 info break      lists all (break|watch)points (with state)
295 IV.4 Stack manipulation
296 -----------------------
297 bt              print calling stack of current thread
298 up              goes up one frame in current thread's stack
299 up N            goes up N frames in current thread's stack
300 dn              goes down one frame in current thread's stack
301 dn N            goes down N frames in current thread's stack
302 frame N         set N as the current frame
303 info local      prints information on local variables for current
304                 function 
306 IV.5 Directory & source file manipulation
307 -----------------------------------------
308 show dir
309 dir <pathname>
311 symbolfile <pathname>
313 list            lists 10 source lines from current position
314 list -          lists 10 source lines before current position
315 list N          lists 10 source lines from line N in current file
316 list <path>:N   lists 10 source lines from line N in file <path>
317 list <id>       lists 10 source lines of function <id>
318 list * N        lists 10 source lines from address N
320 You can specify the end target (to change the 10 lines value) using
321 the ','. For example:
322 list 123, 234   lists source lines from line 123 up to line 234 in
323                 current file
324 list foo.c:1,56 lists source lines from line 1 up to 56 in file foo.c
326 IV.6 Displaying
327 ---------------
328 a display is an expression that's evaluated and printed after the
329 execution of any WineDbg command
331 display         lists the active displays
332 info display    (same as above command)
333 display <expr>  adds a display for expression <expr>
334 display /fmt <expr>     adds a display for expression <expr>. Printing 
335                 evaluated <expr> is done using the given format (see
336                 print command for more on formats)
337 del display N   deletes display #N
338 undisplay N     (same as del display)
340 IV.7 Disassembly
341 ----------------
342 disas           disassemble from current position
343 disas <expr>    disassemble from address <expr>
344 disas <expr>,<expr>disassembles code between addresses specified by
345                 the two <expr>
347 IV.8 Information on Wine's internals
348 ------------------------------------
349 info class <id> prints information on Windows's class <id>
350 walk class      lists all Windows' class registered in Wine
351 info share      lists all the dynamic libraries loaded the debugged
352                 program (including .so files, NE and PE DLLs)
353 info module N   prints information on module of handle N
354 walk module     lists all modules loaded by debugged program
355 info queue N    prints information on Wine's queue N
356 walk queue      lists all queues allocated in Wine
357 info regs       prints the value of CPU register
358 info segment N  prints information on segment N
359 info segment    lists all allocated segments
360 info stack      prints the values on top of the stack
361 info map        lists all virtual mappings used by the debugged
362                 program 
363 info wnd N      prints information of Window of handle N
364 walk wnd        lists all the window hierarchy starting from the
365                 desktop window
366 walk wnd N      lists all the window hierarchy starting from the
367                 window of handle N
368 walk process    lists all w-processes in Wine session
369 walk thread     lists all w-threads in Wine session
370 walk modref     (no longer avail)
372 IV.9 Memory (reading, writing, typing)
374 x <expr>        examines memory at <expr> address
375 x /fmt <expr>   examines memory at <expr> address using format /fmt
376 print <expr>    prints the value of <expr> (possibly using its type)
377 print /fmt <expr>       prints the value of <expr> (possibly using its
378                 type) 
379 set <lval>=<expr>       writes the value of <expr> in <lval> 
380 whatis <expr>   prints the C type of expression <expr>
382 /fmt is either /<letter> or /<count><letter>
383 letter can be   
384                 s => an ASCII string
385                 u => an Unicode UTF16 string
386                 i => instructions (disassemble)
387                 x => 32 bit unsigned hexadecimal integer
388                 d => 32 bit signed decimal integer
389                 w => 16 bit unsigned hexadecimal integer
390                 c => character (only printable 0x20-0x7f are actually
391                      printed) 
392                 b => 8 bit unsigned hexadecimal integer
394 V Other debuggers
395 =================
397 V.1 Using other Unix debuggers
398 ------------------------------
399 You can also use other debuggers (like gdb), but you must be aware of
400 a few items:
401 - you need to attach the unix debugger to the correct unix process
402 (representing the correct windows thread) (you can "guess" it from a
403 'ps fax' for example: When running the emulator, usually the first
404 two upids are for the Windows' application running the desktop, the
405 first thread of the application is generally the third upid; when
406 running a WineLib program, the first thread of the application is
407 generally the first upid)
409 Note: even if latest gdb implements the notion of threads, it won't
410 work with Wine because the thread abstraction used for implementing
411 Windows' thread is not 100% mapped onto the linux posix threads
412 implementation. It means that you'll have to spawn a different gdb
413 session for each Windows' thread you wish to debug.
415 V.2 Using other Windows debuggers
416 ---------------------------------
417 You can use any Windows' debugging API compliant debugger with
418 Wine. Some reports have been made of success with VisualStudio
419 debugger (in remote mode, only the hub runs in Wine).
420 GoVest fully runs in Wine, but is plagued with the same address space
421 issues as WineDbg as stated in II.2
423 V.3 Main differences between winedbg and regular Unix debuggers
424 ---------------------------------------------------------------
426 +----------------------------------+---------------------------------+
427 |             WineDbg              |                 gdb             |
428 +----------------------------------+---------------------------------+
429 |WineDbg debugs a Windows' process:|gdb debugs a Windows' thread:    |
430 |+ the various threads will be     |+ a separate gdb session is      |
431 |  handled by the same WineDbg     |  needed for each thread of      |
432 | session                          |  Windows' process               |
433 |+ a breakpoint will be triggered  |+ a breakpoint will be triggered |
434 |  for any thread of the w-process |  only for the w-thread debugged |
435 +----------------------------------+---------------------------------+
436 |WineDbg supports debug information|gdb supports debug information   |
437 |from:                             |from:                            |
438 |+ stabs (standard Unix format)    |+ stabs (standard Unix format)   |
439 |+ Microsoft's C, CodeView, .DBG   |                                 |
440 +----------------------------------+---------------------------------+
442 VI Limitations
443 ==============
445 16 bit processes are not supported (but calls to 16 bit code in 32 bit 
446 applications is).
447 Lack of address space separation exhibits some deadlocks.
449 Last updated: 5/21/2000 by ericP