Fixed some issues found by winapi_check.
[wine] / documentation / debugger.sgml
1   <chapter id="debugger">
2     <title>Debugging Wine</title>
3
4     <sect1 id="dbg-intro">
5       <title>Introduction</title>
6
7       <para>
8         Written by &name-eric-pouech; <email>&email-eric-pouech;</email>
9         (Last updated: 9/15/2002)
10       </para>
11       <para>
12         (Extracted from <filename>wine/documentation/winedbg</filename>)
13       </para>
14
15       <sect2>
16         <title>Processes and threads: in underlying OS and in Windows</title>
17
18         <para>
19           Before going into the depths of debugging in Wine, here's
20           a small overview of process and thread handling in Wine.
21           It has to be clear that there are two different beasts:
22           processes/threads from the Unix point of view and
23           processes/threads from a Windows point of view.
24         </para>
25         <para>
26           Each Windows' thread is implemented as a Unix process (under
27           Linux using the <function>clone</function> syscall), meaning
28           that all threads of a same Windows' process share the same
29           (unix) address space.
30         </para>
31         <para>
32           In the following:
33         </para>
34         <itemizedlist>
35           <listitem>
36             <para><varname>W-process</varname> means a process in Windows' terminology</para>
37           </listitem>
38           <listitem>
39             <para><varname>U-process</varname> means a process in Unix' terminology</para>
40           </listitem>
41           <listitem>
42             <para><varname>W-thread</varname> means a thread in Windows' terminology</para>
43           </listitem>
44         </itemizedlist>
45         <para>
46           A <varname>W-process</varname> is made of one or several
47           <varname>W-threads</varname>. Each
48           <varname>W-thread</varname> is mapped to one and only one
49           <varname>U-process</varname>. All
50           <varname>U-processes</varname> of a same
51           <varname>W-process</varname> share the same address space.
52         </para>
53         <para>
54           Each Unix process can be identified by two values:
55         </para>
56         <itemizedlist>
57           <listitem>
58             <para>the Unix process id (<varname>upid</varname> in the following)</para>
59           </listitem>
60           <listitem>
61             <para>the Windows's thread id (<varname>tid</varname>)</para>
62           </listitem>
63         </itemizedlist>
64         <para>
65           Each Windows' process has also a Windows' process id
66           (<varname>wpid</varname> in the following). It must be clear
67           that <varname>upid</varname> and <varname>wpid</varname> are
68           different and shall not be used instead of the other.
69         </para>
70         <para>
71           <varname>Wpid</varname> and <varname>tid</varname> are
72           defined (Windows) system wide. They must not be confused
73           with process or thread handles which, as any handle, is an
74           indirection to a system object (in this case process or
75           thread). A same process can have several different handles
76           on the same kernel object. The handles can be defined as
77           local (the values is only valid in a process), or system
78           wide (the same handle can be used by any
79           <varname>W-process</varname>).
80         </para>
81       </sect2>
82
83       <sect2>
84         <title>Wine, debugging and WineDbg</title>
85
86         <para>
87           When talking of debugging in Wine, there are at least two
88           levels to think of:
89         </para>
90         <itemizedlist>
91           <listitem>
92             <para>the Windows' debugging API.</para>
93           </listitem>
94           <listitem>
95             <para>the Wine integrated debugger, dubbed
96               <command>WineDbg</command>.</para>
97           </listitem>
98         </itemizedlist>
99         <para>
100           Wine implements most of the Windows' debugging API (the
101           part in <filename>KERNEL32.DLL</filename>, not the one in
102           <filename>IMAGEHLP.DLL</filename>), and allows any program
103           (emulated or Winelib) using that API to debug a
104           <varname>W-process</varname>.
105         </para>
106         <para>
107           <command>WineDbg</command> is a Winelib application making
108           use of this API to allow debugging both any Wine or Winelib
109           applications as well as Wine itself (kernel and all DLLs).
110         </para>
111       </sect2>
112     </sect1>
113
114
115     <sect1 id="dbg-modes">
116       <title>WineDbg's modes of invocation</title>
117
118       <sect2>
119         <title>Starting a process</title>
120
121         <para>
122           Any application (either a Windows' native executable, or a
123           Winelib application) can be run through
124           <command>WineDbg</command>. Command line options and tricks
125           are the same as for wine:
126         </para>
127         <screen>
128 winedbg telnet.exe
129 winedbg "hl.exe -windowed"
130         </screen>
131       </sect2>
132
133       <sect2>
134         <title>Attaching</title>
135
136         <para>
137           <command>WineDbg</command> can also be launched without any
138           command line argument: <command>WineDbg</command> is started
139           without any attached process. You can get a list of running
140           <varname>W-processes</varname> (and their
141           <varname>wpid</varname>'s) using the <command>walk
142             process</command> command, and then, with the
143           <command>attach</command> command, pick up the
144           <varname>wpid</varname> of the <varname>W-process</varname>
145           you want to debug. This is (for now) a neat feature for the
146           following reasons:
147         </para>
148         <itemizedlist>
149           <listitem>
150             <para>you can debug an already started application</para>
151           </listitem>
152         </itemizedlist>
153       </sect2>
154
155       <sect2 id="dbg-on-exception">
156         <title id="dbg-exception-title">On exceptions</title>
157
158         <para>
159           When something goes wrong, Windows tracks this as an
160           exception. Exceptions exist for segmentation violation,
161           stack overflow, division by zero...
162         </para>
163         <para>
164           When an exception occurs, Wine checks if the <varname>W-process</varname> is
165           debugged. If so, the exception event is sent to the
166           debugger, which takes care of it: end of the story. This
167           mechanism is part of the standard Windows' debugging API.
168         </para>
169         <para>
170           If the <varname>W-process</varname> is not debugged, Wine
171           tries to launch a debugger. This debugger (normally
172           <command>WineDbg</command>, see III Configuration for more
173           details), at startup, attaches to the
174           <varname>W-process</varname> which generated the exception
175           event. In this case, you are able to look at the causes of
176           the exception, and either fix the causes (and continue
177           further the execution) or dig deeper to understand what went
178           wrong.
179         </para>
180         <para>
181           If <command>WineDbg</command> is the standard debugger, the
182           <command>pass</command> and <command>cont</command> commands
183           are the two ways to let the process go further for the
184           handling of the  exception event.
185         </para>
186         <para>
187           To be more precise on the way Wine (and Windows) generates
188           exception events, when a fault occurs (segmentation
189           violation, stack overflow...), the event is first sent to
190           the debugger (this is known as a first chance exception).
191           The debugger can give two answers:
192         </para>
193
194         <variablelist>
195           <varlistentry>
196             <term>continue:</term>
197             <listitem>
198               <para>
199                 the debugger had the ability to correct what's
200                 generated the exception, and is now able to continue
201                 process execution.
202               </para>
203             </listitem>
204           </varlistentry>
205           <varlistentry>
206             <term>pass:</term>
207             <listitem>
208               <para>
209                 the debugger couldn't correct the cause of the
210                 first chance exception. Wine will now try to walk
211                 the list of exception handlers to see if one of them
212                 can handle the exception. If no exception handler is
213                 found, the exception is sent once again to the
214                 debugger to indicate the failure of the exception
215                 handling.
216               </para>
217             </listitem>
218           </varlistentry>
219         </variablelist>
220         <note>
221           <para>
222             since some of Wine's code uses exceptions and
223             <function>try/catch</function> blocks to provide some
224             functionality, <command>WineDbg</command> can be entered
225             in such cases with segv exceptions. This happens, for
226             example, with <function>IsBadReadPtr</function> function.
227             In that case, the <command>pass</command> command shall be
228             used, to let the handling of the exception to be done by
229             the <function>catch</function> block in
230             <function>IsBadReadPtr</function>.
231           </para>
232         </note>
233       </sect2>
234
235       <sect2 id="interrupt">
236         <title>Interrupting</title>
237
238         <para>
239           You can stop the debugger while it's running by hitting
240           Ctrl-C in its window. This will stop the debugged process,
241           and let you manipulate the current context
242         </para>
243       </sect2>
244
245       <sect2>
246         <title>Quitting</title>
247
248         <para>
249           Wine supports the new XP APIs, allowing for a debugger to
250           detach from a program being debugged (see
251           <command>detach</command> command). Unfortunately, as the
252           debugger cannot, for now, neither clear its internal
253           information, nor restart a new process, the debugger, after
254           detaching itself, cannot do much except being quited.  
255         </para>
256       </sect2>
257     </sect1>
258
259
260     <sect1 id="wine-debugger">
261       <title>Using the Wine Debugger</title>
262
263       <para>
264         Written by &name-marcus-meissner; <email>&email-marcus-meissner;</email>,
265         additions welcome.
266       </para>
267       <para>
268         (Extracted from <filename>wine/documentation/debugging</filename>)
269       </para>
270
271       <para>
272         This file describes where to start debugging Wine. If at any
273         point you get stuck and want to ask for help, please read the
274         file <filename>documentation/bugreports</filename> for
275         information on how to write useful bug reports.
276       </para>
277
278       <sect2>
279         <title>Crashes</title>
280
281         <para>
282           These usually show up like this:
283         </para>
284         <screen>
285 |Unexpected Windows program segfault - opcode = 8b
286 |Segmentation fault in Windows program 1b7:c41.
287 |Loading symbols from ELF file /root/wine/wine...
288 |....more Loading symbols from ...
289 |In 16 bit mode.
290 |Register dump:
291 | CS:01b7 SS:016f DS:0287 ES:0000
292 | IP:0c41 SP:878a BP:8796 FLAGS:0246
293 | AX:811e BX:0000 CX:0000 DX:0000 SI:0001 DI:ffff
294 |Stack dump:
295 |0x016f:0x878a:  0001 016f ffed 0000 0000 0287 890b 1e5b
296 |0x016f:0x879a:  01b7 0001 000d 1050 08b7 016f 0001 000d
297 |0x016f:0x87aa:  000a 0003 0004 0000 0007 0007 0190 0000
298 |0x016f:0x87ba:
299 |
300 |0050: sel=0287 base=40211d30 limit=0b93f (bytes) 16-bit rw-
301 |Backtrace:
302 |0 0x01b7:0x0c41 (PXSRV_FONGETFACENAME+0x7c)
303 |1 0x01b7:0x1e5b (PXSRV_FONPUTCATFONT+0x2cd)
304 |2 0x01a7:0x05aa
305 |3 0x01b7:0x0768 (PXSRV_FONINITFONTS+0x81)
306 |4 0x014f:0x03ed (PDOXWIN_@SQLCURCB$Q6CBTYPEULN8CBSCTYPE+0x1b1)
307 |5 0x013f:0x00ac
308 |
309 |0x01b7:0x0c41 (PXSRV_FONGETFACENAME+0x7c):  movw        %es:0x38(%bx),%dx
310         </screen>
311         <para>
312           Steps to debug a crash. You may stop at any step, but please
313           report the bug and provide as much of the information
314           gathered to the newsgroup or the relevant developer as
315           feasible.
316         </para>
317
318         <orderedlist>
319           <listitem>
320             <para>
321               Get the reason for the crash. This is usually an access to
322               an invalid selector, an access to an out of range address
323               in a valid selector, popping a segmentregister from the
324               stack or the like. When reporting a crash, report this
325               <emphasis>whole</emphasis> crashdump even if it doesn't
326               make sense to you.
327             </para>
328             <para>
329               (In this case it is access to an invalid selector, for
330               <systemitem>%es</systemitem> is <literal>0000</literal>, as
331               seen in the register dump).
332             </para>
333           </listitem>
334           <listitem>
335             <para>
336               Determine the cause of the crash. Since this is usually
337               a primary/secondary reaction to a failed or misbehaving
338               Wine function, rerun Wine with <parameter>-debugmsg
339                 +relay</parameter> added to the commandline. This will
340               generate quite a lot of output, but usually the reason is
341               located in the last call(s).  Those lines usually look like
342               this:
343             </para>
344             <screen>
345 |Call KERNEL.90: LSTRLEN(0227:0692 "text") ret=01e7:2ce7 ds=0227
346       ^^^^^^^^^  ^       ^^^^^^^^^ ^^^^^^      ^^^^^^^^^    ^^^^
347       |          |       |         |           |            |Datasegment
348       |          |       |         |           |Return address
349       |          |       |         |textual parameter
350       |          |       |
351       |          |       |Argument(s). This one is a win16 segmented pointer.
352       |          |Function called.
353       |The module, the function is called in. In this case it is KERNEL.
354
355 |Ret  KERNEL.90: LSTRLEN() retval=0x0004 ret=01e7:2ce7 ds=0227
356                                   ^^^^^^
357                                   |Returnvalue is 16 bit and has the value 4.
358             </screen>
359           </listitem>
360           <listitem>
361             <para>
362               If you have found a misbehaving function, try to find out
363               why it misbehaves. Find the function in the source code.
364               Try to make sense of the arguments passed. Usually there is
365               a <function>TRACE(&lt;channel>,"(...)\n");</function> at
366               the beginning of the function. Rerun wine with
367               <parameter>-debugmsg +xyz,+relay</parameter> added to the
368               commandline.
369             </para>
370           </listitem>
371           <listitem>
372             <para>
373               Additional information on how to debug using the internal
374               debugger can be  found in
375               <filename>debugger/README</filename>.
376             </para>
377           </listitem>
378           <listitem>
379             <para>
380               If this information isn't clear enough or if you want to
381               know more about what's happening in the function itself,
382               try running wine with <parameter>-debugmsg
383                 +all</parameter>, which dumps ALL included debug
384               information in wine.
385             </para>
386           </listitem>
387           <listitem>
388             <para>
389               If even that isn't enough, add more debug output for
390               yourself into the functions you find relevant.  See
391               <filename>documentation/debug-msgs</filename>. You might
392               also try to run the program in <command>gdb</command>
393               instead of using the WINE-debugger. If you do that, use
394               <parameter>handle SIGSEGV nostop noprint</parameter> to
395               disable the handling of seg faults inside
396               <command>gdb</command> (needed for Win16). If you don't use
397               the <parameter>--desktop</parameter> or
398               <parameter>--managed</parameter> option, start the WINE
399               process with <parameter>--sync</parameter>, or chances are
400               good to get X into an unusable state.
401             </para>
402           </listitem>
403           <listitem>
404             <para>
405               You can also set a breakpoint for that function. Start wine
406               with the <parameter>--debug</parameter> option added to the
407               commandline. After loading the executable wine will enter
408               the internal debugger. Use <parameter>break
409                 KERNEL_LSTRLEN</parameter> (replace by function you want
410               to debug, CASE IS RELEVANT) to set a breakpoint.  Then use
411               <command>continue</command> to start normal
412               program-execution. Wine will stop if it reaches the
413               breakpoint. If the program isn't yet at the crashing call
414               of that function, use <command>continue</command> again
415               until you are about to enter that function. You may now
416               proceed with single-stepping the function until you reach
417               the point of crash. Use the other debugger commands to
418               print registers and the like.
419             </para>
420           </listitem>
421         </orderedlist>
422       </sect2>
423
424       <sect2>
425         <title>Program hangs, nothing happens</title>
426
427         <para>
428           Switch to UNIX shell, get the process-ID using <command>ps -a |
429             grep wine</command>, and do a <command>kill -HUP
430             &lt;pid></command> (without the &lt; and >). Wine will
431           then enter its internal debugger and you can proceed as
432           explained above. Also, you can use
433           <parameter>--debug</parameter> switch and then you can get into
434           internal debugger by pressing
435           <keycombo><keycap>Ctrl</keycap><keycap>C</keycap></keycombo> in
436           the terminal where you run Wine.
437         </para>
438       </sect2>
439
440       <sect2>
441         <title>Program reports an error with a Messagebox</title>
442
443         <para>
444           Sometimes programs are reporting failure using more or
445           less nondescript messageboxes. We can debug this using the
446           same method as Crashes, but there is one problem... For
447           setting up a message box the program also calls Wine
448           producing huge chunks of debug code.
449         </para>
450         <para>
451           Since the failure happens usually directly before setting up
452           the Messagebox you can start wine with
453           <parameter>--debug</parameter> added to the commandline, set a
454           breakpoint at <function>MessageBoxA</function> (called by win16
455           and win32 programs) and proceed with
456           <command>continue</command>. With <parameter>--debugmsg
457             +all</parameter> Wine will now stop directly before setting
458           up the Messagebox. Proceed as explained above.
459         </para>
460         <para>
461           You can also run wine using <command>wine -debugmsg +relay
462             program.exe 2>&1 | less -i</command> and in
463           <command>less</command> search for <quote>MessageBox</quote>.
464         </para>
465       </sect2>
466
467       <sect2>
468         <title>Disassembling programs:</title>
469
470         <para>
471           You may also try to disassemble the offending program to
472           check for undocumented features and/or use of them.
473         </para>
474         <para>
475           The best, freely available, disassembler for Win16 programs is
476           <application>Windows Codeback</application>, archivename
477           <filename>wcbxxx.zip</filename>, which usually can be found in
478           the <filename>Cica-Mirror</filename> subdirectory on the WINE
479           ftpsites. (See <filename>ANNOUNCE</filename>).
480         </para>
481         <para>
482           Disassembling win32 programs is possible using
483           <application>Windows Disassembler 32</application>, archivename
484           something like <filename>w32dsm87.zip</filename> (or similar)
485           on <systemitem class="systemname">ftp.winsite.com</systemitem>
486           and mirrors.  The shareware version does not allow saving of
487           disassembly listings. You can also use the newer (and in the
488           full version better) <application>Interactive
489             Disassembler</application> (IDA) from the ftp sites mentioned
490           at the end of the document. Understanding disassembled code is
491           mostly a question of exercise.
492         </para>
493         <para>
494           Most code out there uses standard C function entries (for it
495           is usually  written in C). Win16 function entries usually
496           look like that:
497         </para>
498         <programlisting>
499 push bp
500 mov bp, sp
501 ... function code ..
502 retf XXXX       &lt;--------- XXXX is number of bytes of arguments
503         </programlisting>
504         <para>
505           This is a <function>FAR</function> function with no local
506           storage. The arguments usually start at
507           <literal>[bp+6]</literal> with increasing offsets. Note, that
508           <literal>[bp+6]</literal> belongs to the
509           <emphasis>rightmost</emphasis> argument, for exported win16
510           functions use the PASCAL calling convention. So, if we use
511           <function>strcmp(a,b)</function> with <parameter>a</parameter>
512           and <parameter>b</parameter> both 32 bit variables
513           <parameter>b</parameter> would be at <literal>[bp+6]</literal>
514           and <parameter>a</parameter> at <literal>[bp+10]</literal>.
515         </para>
516         <para>
517           Most functions make also use of local storage in the stackframe:
518         </para>
519         <programlisting>
520 enter 0086, 00
521 ... function code ...
522 leave
523 retf XXXX
524         </programlisting>
525         <para>
526           This does mostly the same as above, but also adds
527           <literal>0x86</literal> bytes of stackstorage, which is
528           accessed using <literal>[bp-xx]</literal>. Before calling a
529           function, arguments are pushed on the stack using something
530           like this:
531         </para>
532         <programlisting>
533 push word ptr [bp-02]   &lt;- will be at [bp+8]
534 push di                 &lt;- will be at [bp+6]
535 call KERNEL.LSTRLEN
536         </programlisting>
537         <para>
538           Here first the selector and then the offset to the passed
539           string are pushed.
540         </para>
541       </sect2>
542
543       <sect2>
544         <title>Sample debugging session:</title>
545
546         <para>
547           Let's debug the infamous Word <filename>SHARE.EXE</filename>
548           messagebox:
549         </para>
550         <screen>
551 |marcus@jet $ wine winword.exe
552 |            +---------------------------------------------+
553 |            | !  You must leave Windows and load SHARE.EXE|
554 |            |    before starting Word.                    |
555 |            +---------------------------------------------+
556         </screen>
557         <screen>
558 |marcus@jet $ wine winword.exe -debugmsg +relay -debug
559 |CallTo32(wndproc=0x40065bc0,hwnd=000001ac,msg=00000081,wp=00000000,lp=00000000)
560 |Win16 task 'winword': Breakpoint 1 at 0x01d7:0x001a
561 |CallTo16(func=0127:0070,ds=0927)
562 |Call WPROCS.24: TASK_RESCHEDULE() ret=00b7:1456 ds=0927
563 |Ret  WPROCS.24: TASK_RESCHEDULE() retval=0x8672 ret=00b7:1456 ds=0927
564 |CallTo16(func=01d7:001a,ds=0927)
565 |     AX=0000 BX=3cb4 CX=1f40 DX=0000 SI=0000 DI=0927 BP=0000 ES=11f7
566 |Loading symbols: /home/marcus/wine/wine...
567 |Stopped on breakpoint 1 at 0x01d7:0x001a
568 |In 16 bit mode.
569 |Wine-dbg>break MessageBoxA                          &lt;---- Set Breakpoint
570 |Breakpoint 2 at 0x40189100 (MessageBoxA [msgbox.c:190])
571 |Wine-dbg>c                                            &lt;---- Continue
572 |Call KERNEL.91: INITTASK() ret=0157:0022 ds=08a7
573 |     AX=0000 BX=3cb4 CX=1f40 DX=0000 SI=0000 DI=08a7 ES=11d7 EFL=00000286
574 |CallTo16(func=090f:085c,ds=0dcf,0x0000,0x0000,0x0000,0x0000,0x0800,0x0000,0x0000,0x0dcf)
575 |...                                                   &lt;----- Much debugoutput
576 |Call KERNEL.136: GETDRIVETYPE(0x0000) ret=060f:097b ds=0927
577                                ^^^^^^ Drive 0 (A:)
578 |Ret  KERNEL.136: GETDRIVETYPE() retval=0x0002 ret=060f:097b ds=0927
579                                         ^^^^^^  DRIVE_REMOVEABLE
580                                                 (It is a floppy diskdrive.)
581
582 |Call KERNEL.136: GETDRIVETYPE(0x0001) ret=060f:097b ds=0927
583                                ^^^^^^ Drive 1 (B:)
584 |Ret  KERNEL.136: GETDRIVETYPE() retval=0x0000 ret=060f:097b ds=0927
585                                         ^^^^^^  DRIVE_CANNOTDETERMINE
586                                                 (I don't have drive B: assigned)
587
588 |Call KERNEL.136: GETDRIVETYPE(0x0002) ret=060f:097b ds=0927
589                                ^^^^^^^ Drive 2 (C:)
590 |Ret  KERNEL.136: GETDRIVETYPE() retval=0x0003 ret=060f:097b ds=0927
591                                         ^^^^^^ DRIVE_FIXED
592                                                (specified as a harddisk)
593
594 |Call KERNEL.97: GETTEMPFILENAME(0x00c3,0x09278364"doc",0x0000,0927:8248) ret=060f:09b1 ds=0927
595                                  ^^^^^^           ^^^^^        ^^^^^^^^^
596                                  |                |            |buffer for fname
597                                  |                |temporary name ~docXXXX.tmp
598                                  |Force use of Drive C:.
599
600 |Warning: GetTempFileName returns 'C:~doc9281.tmp', which doesn't seem to be writeable.
601 |Please check your configuration file if this generates a failure.
602         </screen>
603         <para>
604           Whoops, it even detects that something is wrong!
605         </para>
606         <screen>
607 |Ret  KERNEL.97: GETTEMPFILENAME() retval=0x9281 ret=060f:09b1 ds=0927
608                                           ^^^^^^ Temporary storage ID
609
610 |Call KERNEL.74: OPENFILE(0x09278248"C:~doc9281.tmp",0927:82da,0x1012) ret=060f:09d8 ds=0927
611                                     ^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^
612                                     |filename        |OFSTRUCT |open mode:
613
614                                        OF_CREATE|OF_SHARE_EXCLUSIVE|OF_READWRITE
615         </screen>
616         <para>
617           This fails, since my <medialabel>C:</medialabel> drive is in
618           this case mounted readonly.
619         </para>
620         <screen>
621 |Ret  KERNEL.74: OPENFILE() retval=0xffff ret=060f:09d8 ds=0927
622                                    ^^^^^^ HFILE_ERROR16, yes, it failed.
623
624 |Call USER.1: MESSAGEBOX(0x0000,0x09278376"Sie mussen Windows verlassen und SHARE.EXE laden bevor Sie Word starten.",0x00000000,0x1030) ret=060f:084f ds=0927
625         </screen>
626         <para>
627           And MessageBox'ed.
628         </para>
629         <screen>
630 |Stopped on breakpoint 2 at 0x40189100 (MessageBoxA [msgbox.c:190])
631 |190     {              &lt;- the sourceline
632 In 32 bit mode.
633 Wine-dbg>
634         </screen>
635         <para>
636           The code seems to find a writeable harddisk and tries to create
637           a file there. To work around this bug, you can define
638           <medialabel>C:</medialabel> as a networkdrive, which is ignored
639           by the code above.
640         </para>
641       </sect2>
642
643       <sect2>
644         <title>Debugging Tips</title>
645
646         <para>
647           Here are some useful debugging tips, added by Andreas Mohr:
648         </para>
649
650         <itemizedlist>
651           <listitem>
652             <para>
653               If you have a program crashing at such an early loader phase that you can't
654               use the Wine debugger normally, but Wine already executes the program's
655               start code, then you may use a special trick. You should do a
656               <programlisting>
657 wine --debugmsg +relay program
658               </programlisting>
659               to get a listing of the functions the program calls in its start function.
660               Now you do a
661               <programlisting>
662 wine --debug winfile.exe
663               </programlisting>
664             </para>
665             <para>
666               This way, you get into <command>Wine-dbg</command>. Now you
667               can set a breakpoint on any function the program calls in
668               the start function and just type <userinput>c</userinput>
669               to bypass the eventual calls of Winfile to this function
670               until you are finally at the place where this function gets
671               called by the crashing start function. Now you can proceed
672               with your debugging as usual.
673             </para>
674           </listitem>
675           <listitem>
676             <para>
677               If you try to run a program and it quits after showing an error messagebox,
678               the problem can usually be identified in the return value of one of the
679               functions executed before <function>MessageBox()</function>.
680               That's why you should re-run the program with e.g.
681               <programlisting>
682 wine --debugmsg +relay &lt;program name> &>relmsg
683               </programlisting>
684               Then do a <command>more relmsg</command> and search for the
685               last occurrence of a call to the string "MESSAGEBOX". This is a line like
686               <programlisting>
687 Call USER.1: MESSAGEBOX(0x0000,0x01ff1246 "Runtime error 219 at 0004:1056.",0x00000000,0x1010) ret=01f7:2160 ds=01ff
688               </programlisting>
689               In my example the lines before the call to
690               <function>MessageBox()</function> look like that:
691               <programlisting>
692 Call KERNEL.96: FREELIBRARY(0x0347) ret=01cf:1033 ds=01ff
693 CallTo16(func=033f:0072,ds=01ff,0x0000)
694 Ret  KERNEL.96: FREELIBRARY() retval=0x0001 ret=01cf:1033 ds=01ff
695 Call KERNEL.96: FREELIBRARY(0x036f) ret=01cf:1043 ds=01ff
696 CallTo16(func=0367:0072,ds=01ff,0x0000)
697 Ret  KERNEL.96: FREELIBRARY() retval=0x0001 ret=01cf:1043 ds=01ff
698 Call KERNEL.96: FREELIBRARY(0x031f) ret=01cf:105c ds=01ff
699 CallTo16(func=0317:0072,ds=01ff,0x0000)
700 Ret  KERNEL.96: FREELIBRARY() retval=0x0001 ret=01cf:105c ds=01ff
701 Call USER.171: WINHELP(0x02ac,0x01ff05b4 "COMET.HLP",0x0002,0x00000000) ret=01cf:1070 ds=01ff
702 CallTo16(func=0117:0080,ds=01ff)
703 Call WPROCS.24: TASK_RESCHEDULE() ret=00a7:0a2d ds=002b
704 Ret  WPROCS.24: TASK_RESCHEDULE() retval=0x0000 ret=00a7:0a2d ds=002b
705 Ret  USER.171: WINHELP() retval=0x0001 ret=01cf:1070 ds=01ff
706 Call KERNEL.96: FREELIBRARY(0x01be) ret=01df:3e29 ds=01ff
707 Ret  KERNEL.96: FREELIBRARY() retval=0x0000 ret=01df:3e29 ds=01ff
708 Call KERNEL.52: FREEPROCINSTANCE(0x02cf00ba) ret=01f7:1460 ds=01ff
709 Ret  KERNEL.52: FREEPROCINSTANCE() retval=0x0001 ret=01f7:1460 ds=01ff
710 Call USER.1: MESSAGEBOX(0x0000,0x01ff1246 "Runtime error 219 at 0004:1056.",0x00000000,0x1010) ret=01f7:2160 ds=01ff
711               </programlisting>
712             </para>
713             <para>
714               I think that the call to <function>MessageBox()</function>
715               in this example is <emphasis>not</emphasis> caused by a
716               wrong result value of some previously executed function
717               (it's happening quite often like that), but instead the
718               messagebox complains about a runtime error at
719               <literal>0x0004:0x1056</literal>.
720             </para>
721             <para>
722               As the segment value of the address is only
723               <literal>4</literal>, I think that that is only an internal
724               program value. But the offset address reveals something
725               quite interesting: Offset <literal>1056</literal> is
726               <emphasis>very</emphasis> close to the return address of
727               <function>FREELIBRARY()</function>:
728               <programlisting>
729 Call KERNEL.96: FREELIBRARY(0x031f) ret=01cf:105c ds=01ff
730                                              ^^^^
731               </programlisting>
732             </para>
733             <para>
734               Provided that segment <literal>0x0004</literal> is indeed segment
735               <literal>0x1cf</literal>, we now we can use IDA                 (available at
736               <ulink url="ftp://ftp.uni-koeln.de/pc/msdos/programming/assembler/ida35bx.zip">
737                 ftp://ftp.uni-koeln.de/pc/msdos/programming/assembler/ida35bx.zip</ulink>) to
738               disassemble the part that caused the error. We just have to find the address of
739               the call to <function>FreeLibrary()</function>. Some lines before that the
740               runtime error occurred. But be careful! In some cases you don't have to
741               disassemble the main program, but instead some DLL called by it in order to find
742               the correct place where the runtime error occurred. That can be determined by
743               finding the origin of the segment value (in this case <literal>0x1cf</literal>).
744             </para>
745           </listitem>
746           <listitem>
747             <para>
748               If you have created a relay file of some crashing
749               program and want to set a breakpoint at a certain
750               location which is not yet available as the program loads
751               the breakpoint's segment during execution, you may set a
752               breakpoint to <function>GetVersion16/32</function> as
753               those functions are called very often.
754             </para>
755             <para>
756               Then do a <userinput>c</userinput> until you are able to
757               set this breakpoint without error message.
758             </para>
759           </listitem>
760           <listitem>
761             <para>
762               Some useful programs:
763             </para>
764             <variablelist>
765               <varlistentry>
766                 <term>
767                   <application>IDA</application>:
768                   <filename>
769                     <ulink url="ftp://ftp.uni-koeln.de/pc/msdos/programming/assembler/ida35bx.zip">
770                       ftp://ftp.uni-koeln.de/pc/msdos/programming/assembler/ida35bx.zip</ulink>
771                   </filename>
772                 </term>
773                 <listitem>
774                   <para>
775                     <emphasis>Very</emphasis> good DOS disassembler ! It's badly needed
776                     for debugging Wine sometimes.
777                   </para>
778                 </listitem>
779               </varlistentry>
780               <varlistentry>
781                 <term>
782                   <application>XRAY</application>:
783                   <filename>
784                     <ulink url="ftp://ftp.th-darmstadt.de/pub/machines/ms-dos/SimTel/msdos/asmutil/xray15.zip">
785                       ftp://ftp.th-darmstadt.de/pub/machines/ms-dos/SimTel/msdos/asmutil/xray15.zip</ulink>
786                   </filename>
787                 </term>
788                 <listitem>
789                   <para>
790                     Traces DOS calls (Int 21h, DPMI, ...). Use it with
791                     Windows to correct file management problems etc.
792                   </para>
793                 </listitem>
794               </varlistentry>
795               <varlistentry>
796                 <term>
797                   <application>pedump</application>:
798                   <filename>
799                     <ulink url="http://oak.oakland.edu/pub/simtelnet/win95/prog/pedump.zip">
800                       http://oak.oakland.edu/pub/simtelnet/win95/prog/pedump.zip</ulink>
801                   </filename>
802                 </term>
803                 <listitem>
804                   <para>
805                     Dumps the imports and exports of a PE (Portable
806                     Executable) DLL.
807                   </para>
808                 </listitem>
809               </varlistentry>
810               <varlistentry>
811                 <term>
812                   <application>winedump</application>:
813                 </term>
814                 <listitem>
815                   <para>
816                     Dumps the imports and exports of a PE (Portable
817                     Executable) DLL (included in wine tree).
818                   </para>
819                 </listitem>
820               </varlistentry>
821             </variablelist>
822           </listitem>
823         </itemizedlist>
824       </sect2>
825
826       <sect2>
827         <title>Some basic debugger usages:</title>
828
829         <para>
830           After starting your program with
831         </para>
832         <screen>
833 wine -debug myprog.exe
834         </screen>
835         <para>
836           the program loads and you get a prompt at the program
837           starting point. Then you can set breakpoints:
838         </para>
839         <screen>
840   b RoutineName      (by outine name) OR
841   b *0x812575        (by address)
842         </screen>
843         <para>
844           Then you hit <command>c</command> (continue) to run the
845           program. It stops at the breakpoint. You can type
846         </para>
847         <screen>
848   step               (to step one line) OR
849   stepi              (to step one machine instruction at a time;
850                       here, it helps to know the basic 386
851                       instruction set)
852   info reg           (to see registers)
853   info stack         (to see hex values in the stack)
854   info local         (to see local variables)
855   list &lt;line number&gt; (to list source code)
856   x &lt;variable name&gt;  (to examine a variable; only works if code
857                       is not compiled with optimization)
858   x 0x4269978        (to examine a memory location)
859   ?                  (help)
860   q                  (quit)
861         </screen>
862         <para>
863           By hitting <keycap>Enter</keycap>, you repeat the last
864           command.
865         </para>
866       </sect2>
867     </sect1>
868
869
870     <sect1 id="memory-addresses">
871       <title>Useful memory addresses</title>
872       <para>
873         Written by &name-andreas-mohr; <email>&email-andreas-mohr;</email>
874       </para>
875       <para>
876         Wine uses several different kinds of memory addresses.
877       </para>
878       <variablelist>
879         <varlistentry>
880           <term>
881             Win32/"normal" Wine addresses/Linux: linear addresses.
882           </term>
883           <listitem>
884             <para>
885               Linear addresses can be everything from 0x0 up to
886               0xffffffff.  In Wine on Linux they are often around
887               e.g. 0x08000000, 0x00400000 (std. Win32 program load
888               address), 0x40000000.  Every Win32 process has its own
889               private 4GB address space (that is, from 0x0 up to
890               0xffffffff).
891             </para>
892           </listitem>
893         </varlistentry>
894         <varlistentry>
895           <term>
896             Win16 "enhanced mode": segmented addresses.
897           </term>
898           <listitem>
899             <para>
900               These are the "normal" Win16 addresses, called SEGPTR.
901               They have a segment:offset notation, e.g. 0x01d7:0x0012.
902               The segment part usually is a "selector", which *always*
903               has the lowest 3 bits set.  Some sample selectors are
904               0x1f7, 0x16f, 0x8f.  If these bits are set except for
905               the lowest bit, as e.g. with 0x1f6,xi then it might be a
906               handle to global memory. Just set the lowest bit to get
907               the selector in these cases.  A selector kind of
908               "points" to a certain linear (see above) base address.
909               It has more or less three important attributes: segment
910               base address, segment limit, segment access rights.
911             </para>
912             <para>
913               Example:
914             </para>
915             <para>
916               Selector 0x1f7 (0x40320000, 0x0000ffff, r-x) So 0x1f7
917               has a base address of 0x40320000, the segment's last
918               address is 0x4032ffff (limit 0xffff), and it's readable
919               and executable.  So an address of 0x1f7:0x2300 would be
920               the linear address of 0x40322300.
921             </para>
922           </listitem>
923         </varlistentry>
924         <varlistentry>
925           <term>
926             DOS/Win16 "standard mode"
927           </term>
928           <listitem>
929             <para>
930               They, too, have a segment:offset notation.  But they are
931               completely different from "normal" Win16 addresses, as
932               they just represent at most 1MB of memory: The segment
933               part can be anything from 0 to 0xffff, and it's the same
934               with the offset part.
935             </para>
936             <para>
937               Now the strange thing is the calculation that's behind
938               these addresses: Just calculate segment*16 + offset in
939               order to get a "linear DOS" address.  So
940               e.g. 0x0f04:0x3628 results in 0xf040 + 0x3628 = 0x12668.
941               And the highest address you can get is 0xfffff (1MB), of
942               course.  In Wine, this "linear DOS" address of 0x12668
943               has to be added to the linear base address of the
944               corresponding DOS memory allocated for dosmod in order
945               to get the true linear address of a DOS seg:offs
946               address.  And make sure that you're doing this in the
947               correct process with the correct linear address space,
948               of course ;-)
949             </para>
950           </listitem>
951         </varlistentry>
952       </variablelist>
953     </sect1>
954
955     <sect1 id="dbg-config">
956       <title>Configuration</title>
957
958       <sect2>
959         <title>Registry configuration</title>
960
961         <para>
962           The Windows' debugging API uses a registry entry to know
963           which debugger to invoke when an unhandled exception occurs
964           (see <link endterm="dbg-exception-title"
965           linkend="dbg-on-exception"></link> for some details). Two
966           values in key
967         </para>
968         <programlisting>
969 "MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug"
970         </programlisting>
971         <para>
972           Determine the behavior:
973         </para>
974         <variablelist>
975           <varlistentry>
976             <term>Debugger:</term>
977             <listitem>
978               <para>
979                 this is the command line used to launch the debugger
980                 (it uses two <function>printf</function> formats
981                 (<literal>%ld</literal>) to pass context dependent
982                 information to  the debugger). You should put here a
983                 complete path to your debugger
984                 (<command>WineDbg</command> can of course be used, but
985                 any other Windows' debugging API aware debugger will
986                 do).
987                 The path to the debugger you chose to use must be reachable
988                 via a DOS drive in the Wine config file !
989               </para>
990               <para>
991                 You can also set a shell script to launch the debugger. In
992                 this case, you need to be sure that the invocation in
993                 this shell script is of the form:
994                 <programlisting>
995                   WINEPRELOAD=&lt;path_to_winedbg.so&gt; exec wine $*
996                 </programlisting>
997                 (Shell script must use exec, and the debugger .so file must
998                 be preloaded to override the shell script information).
999               </para>
1000             </listitem>
1001           </varlistentry>
1002           <varlistentry>
1003             <term>Auto:</term>
1004             <listitem>
1005               <para>
1006                 if this value is zero, a message box will ask the
1007                 user if he/she wishes to launch the debugger when an
1008                 unhandled exception occurs. Otherwise, the debugger
1009                 is automatically started.
1010               </para>
1011             </listitem>
1012           </varlistentry>
1013         </variablelist>
1014
1015         <para>
1016           A regular Wine registry looks like:
1017         </para>
1018         <programlisting>
1019 [MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug] 957636538
1020 "Auto"=dword:00000001
1021 "Debugger"="/usr/local/bin/winedbg %ld %ld"
1022         </programlisting>
1023
1024         <note>
1025           <title>Note 1</title>
1026           <para>
1027             creating this key is mandatory. Not doing so will not
1028             fire the debugger when an exception occurs.
1029           </para>
1030         </note>
1031         <note>
1032           <title>Note 2</title>
1033           <para>
1034             <command>wineinstall</command> (available in Wine source)
1035             sets up this correctly.
1036             However, due to some limitation of the registry installed,
1037             if a previous Wine installation exists, it's safer to
1038             remove the whole
1039           </para>
1040           <programlisting>
1041 [MACHINE\\Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug]
1042           </programlisting>
1043           <para>
1044             key before running again <command>wineinstall</command> to
1045             regenerate this key.
1046           </para>
1047         </note>
1048       </sect2>
1049
1050       <sect2>
1051         <title>WineDbg configuration</title>
1052
1053         <para>
1054           <command>WineDbg</command> can be configured through a number
1055           of options. Those options are stored in the registry, on a
1056           per user basis. The key is (in <emphasis>my</emphasis> registry)
1057         </para>
1058         <programlisting>
1059 [eric\\Software\\Wine\\WineDbg]
1060         </programlisting>
1061         <para>
1062           Those options can be read/written while inside
1063           <command>WineDbg</command>, as part of the debugger
1064           expressions. To refer to one of these options, its name must
1065           be  prefixed by a <literal>$</literal> sign. For example,
1066         </para>
1067         <programlisting>
1068 set $BreakAllThreadsStartup = 1
1069         </programlisting>
1070         <para>
1071           sets the option <varname>BreakAllThreadsStartup</varname> to
1072           <literal>TRUE</literal>.
1073         </para>
1074         <para>
1075           All the options are read from the registry when
1076           <command>WineDbg</command> starts (if no corresponding value
1077           is found, a default value is used), and are written back to
1078           the registry when <command>WineDbg</command> exits (hence,
1079           all modifications to those options are automatically saved
1080           when <command>WineDbg</command> terminates).
1081         </para>
1082         <para>
1083           Here's the list of all options:
1084         </para>
1085
1086         <sect3>
1087           <title>Controlling when the debugger is entered</title>
1088
1089           <variablelist>
1090             <varlistentry>
1091               <term><varname>BreakAllThreadsStartup</varname></term>
1092               <listitem>
1093                 <para>
1094                   Set to <literal>TRUE</literal> if at all threads
1095                   start-up the debugger stops  set to
1096                   <literal>FALSE</literal> if only at the first thread
1097                   startup of a given process the debugger stops.
1098                   <literal>FALSE</literal> by default.
1099                 </para>
1100               </listitem>
1101             </varlistentry>
1102             <varlistentry>
1103               <term><varname>BreakOnCritSectTimeOut</varname></term>
1104               <listitem>
1105                 <para>
1106                   Set to <literal>TRUE</literal> if the debugger stops
1107                   when a critical section times out (5 minutes);
1108                   <literal>TRUE</literal> by default.
1109                 </para>
1110               </listitem>
1111             </varlistentry>
1112             <varlistentry>
1113               <term><varname>BreakOnAttach</varname></term>
1114               <listitem>
1115                 <para>
1116                   Set to <literal>TRUE</literal> if when
1117                   <command>WineDbg</command> attaches to an existing
1118                   process after an unhandled exception,
1119                   <command>WineDbg</command> shall be entered on the
1120                   first attach event. Since the attach event is
1121                   meaningless in the context of an exception event
1122                   (the next event  which is the exception event is of
1123                   course relevant), that option is likely to be
1124                   <literal>FALSE</literal>.
1125                 </para>
1126               </listitem>
1127             </varlistentry>
1128             <varlistentry>
1129               <term><varname>BreakOnFirstChance</varname></term>
1130               <listitem>
1131                 <para>
1132                   An exception can generate two debug events. The
1133                   first one is passed to the debugger (known as a
1134                   first chance) just after the exception. The debugger
1135                   can then decides either to resume execution (see
1136                   <command>WineDbg</command>'s <command>cont</command>
1137                   command) or pass the exception up to the exception
1138                   handler chain in the program (if it exists)
1139                   (<command>WineDbg</command> implements this through the
1140                   <command>pass</command> command). If none of the
1141                   exception handlers takes care of the exception, the
1142                   exception event is sent again to the debugger (known
1143                   as last chance exception). You cannot pass on a last
1144                   exception. When the
1145                   <varname>BreakOnFirstChance</varname> exception is
1146                   <literal>TRUE</literal>, then winedbg is entered for
1147                   both first and last chance execptions (to
1148                   <literal>FALSE</literal>, it's only entered for last
1149                   chance exceptions).
1150                 </para>
1151               </listitem>
1152             </varlistentry>
1153             <varlistentry>
1154               <term><varname>BreakOnDllLoad</varname></term>
1155               <listitem>
1156                 <para>
1157                   Set to <literal>TRUE</literal> if the debugger stops
1158                   when a DLL is loaded into memory; when the debugger
1159                   is invoked after a crash, the DLLs already mapped in
1160                   memory will not trigger this break.
1161                   <literal>FALSE</literal> by default.
1162                 </para>
1163               </listitem>
1164             </varlistentry>
1165           </variablelist>
1166         </sect3>
1167
1168         <sect3>
1169           <title>Output handling</title>
1170
1171           <variablelist>
1172             <varlistentry>
1173               <term><varname>ConChannelMask</varname></term>
1174               <listitem>
1175                 <para>
1176                   Mask of active debugger output channels on console
1177                 </para>
1178               </listitem>
1179             </varlistentry>
1180             <varlistentry>
1181               <term><varname>StdChannelMask</varname></term>
1182               <listitem>
1183                 <para>
1184                   Mask of active debugger output channels on <filename>stderr</filename>
1185                 </para>
1186               </listitem>
1187             </varlistentry>
1188             <varlistentry>
1189               <term><varname>UseXTerm</varname></term>
1190               <listitem>
1191                 <para>
1192                   Set to <literal>TRUE</literal> if the debugger uses
1193                   its own <command>xterm</command> window for console
1194                   input/output.  Set to <literal>FALSE</literal> if
1195                   the debugger uses the current Unix console for
1196                   input/output
1197                 </para>
1198               </listitem>
1199             </varlistentry>
1200           </variablelist>
1201
1202           <para>
1203             Those last 3 variables are jointly used in two generic ways:
1204           </para>
1205
1206           <orderedlist>
1207             <listitem>
1208               <para>default</para>
1209               <programlisting>
1210 ConChannelMask = DBG_CHN_MESG (1)
1211 StdChannelMask = 0
1212 UseXTerm = 1
1213               </programlisting>
1214               <para>
1215                 In this case, all input/output goes into a specific
1216                 <command>xterm</command> window (but all debug
1217                 messages <function>TRACE</function>,
1218                 <function>WARN</function>... still goes to tty where
1219                 wine is run from).
1220               </para>
1221             </listitem>
1222             <listitem>
1223               <para>
1224                 to have all input/output go into the tty where Wine
1225                 was started from (to be used in a X11-free
1226                 environment)
1227               </para>
1228               <screen>
1229 ConChannelMask = 0
1230 StdChannelMask = DBG_CHN_MESG (1)
1231 UseXTerm = 1
1232               </screen>
1233             </listitem>
1234           </orderedlist>
1235           <para>
1236             Those variables also allow, for example for debugging
1237             purposes, to use:
1238           </para>
1239           <screen>
1240 ConChannelMask = 0xfff
1241 StdChannelMask = 0xfff
1242 UseXTerm = 1
1243           </screen>
1244           <para>
1245             This allows to redirect all <function>WineDbg</function>
1246             output to both tty Wine was started from, and
1247             <command>xterm</command> debugging window. If Wine (or
1248             <command>WineDbg</command>) was started with a redirection
1249             of <filename>stdout</filename> and/or
1250             <filename>stderr</filename> to a file (with for
1251             example &gt;& shell redirect command), you'll get in that
1252             file both outputs. It may be interesting to look in the
1253             relay trace for specific values which the process segv'ed
1254             on.
1255           </para>
1256         </sect3>
1257
1258         <sect3>
1259           <title>Context information</title>
1260
1261           <variablelist>
1262             <varlistentry>
1263               <term><varname>ThreadId</varname></term>
1264               <listitem>
1265                 <para>ID of the <varname>W-thread</varname> currently
1266                   examined by the debugger</para>
1267               </listitem>
1268             </varlistentry>
1269             <varlistentry>
1270               <term><varname>ProcessId</varname></term>
1271               <listitem>
1272                 <para>ID of the <varname>W-thread</varname> currently
1273                   examined by the debugger</para>
1274               </listitem>
1275             </varlistentry>
1276             <varlistentry>
1277               <term>&lt;registers></term>
1278               <listitem>
1279                 <para>All CPU registers are also available</para>
1280               </listitem>
1281             </varlistentry>
1282           </variablelist>
1283
1284           <para>
1285             The <varname>ThreadId</varname> and
1286             <varname>ProcessId</varname> variables can be handy to set
1287             conditional breakpoints on a given thread or process.
1288           </para>
1289         </sect3>
1290       </sect2>
1291     </sect1>
1292
1293
1294     <sect1 id="dbg-commands">
1295       <title>WineDbg Command Reference</title>
1296
1297       <sect2>
1298         <title>Misc</title>
1299
1300         <screen>
1301 abort           aborts the debugger
1302 quit            exits the debugger
1303
1304 attach N        attach to a W-process (N is its ID). IDs can be
1305                 obtained using the walk process command
1306 detach          detach from a W-process. WineDbg will exit (this may
1307                 be changed later on)
1308         </screen>
1309         <screen>
1310 help            prints some help on the commands
1311 help info       prints some help on info commands
1312         </screen>
1313         <screen>
1314 mode 16         switch to 16 bit mode
1315 mode 32         switch to 32 bit mode
1316         </screen>
1317       </sect2>
1318
1319       <sect2>
1320         <title>Flow control</title>
1321
1322         <screen>
1323 cont            continue execution until next breakpoint or exception.
1324 pass            pass the exception event up to the filter chain.
1325 step            continue execution until next C line of code (enters
1326                 function call)
1327 next            continue execution until next C line of code (doesn't
1328                 enter function call)
1329 stepi           execute next assembly instruction (enters function
1330                 call)
1331 nexti           execute next assembly instruction (doesn't enter
1332                 function call)
1333 finish          do nexti commands until current function is exited
1334         </screen>
1335         <para>
1336           cont, step, next, stepi, nexti can be postfixed by a
1337           number (N), meaning that the command must be executed N
1338           times.
1339         </para>
1340       </sect2>
1341
1342       <sect2>
1343         <title>Breakpoints, watch points</title>
1344
1345         <screen>
1346 enable N        enables (break|watch)point #N
1347 disable N       disables (break|watch)point #N
1348 delete N        deletes  (break|watch)point #N
1349 cond N          removes any a existing condition to (break|watch)point N
1350 cond N &lt;expr&gt;     adds condition &lt;expr&gt; to (break|watch)point N. &lt;expr&gt;
1351                 will be evaluated each time the breakpoint is hit. If
1352                 the result is a zero value, the breakpoint isn't
1353                 triggered
1354 break * N       adds a breakpoint at address N
1355 break &lt;id&gt;        adds a breakpoint at the address of symbol &lt;id&gt;
1356 break &lt;id&gt; N      adds a breakpoint at the address of symbol &lt;id&gt; (N ?)
1357 break N         adds a breakpoint at line N of current source file
1358 break           adds a breakpoint at current $pc address
1359 watch * N       adds a watch command (on write) at address N (on 4 bytes)
1360 watch &lt;id&gt;        adds a watch command (on write) at the address of
1361                 symbol &lt;id&gt;
1362 info break      lists all (break|watch)points (with state)
1363         </screen>
1364         <para>
1365           When setting a breakpoint on an &lt;id&gt;, if several symbols with this
1366           &lt;id&gt; exist, the debugger will prompt for the symbol you want to use.
1367           Pick up the one you want from its number.
1368         </para>
1369         <para>
1370           Alternatively you can specify a DLL in the &lt;id&gt; (for example
1371           MYDLL.DLL.myFunc for function myFunc of
1372           <filename>G:\AnyPath\MyDll.dll)</filename>.
1373         </para>
1374         <para>
1375           You can use the symbol <emphasis>EntryPoint</emphasis> to stand for
1376           the entry point of the Dll.
1377         </para>
1378         <para>
1379           When setting a break/watch-point by &lt;id&gt;, if the symbol cannot be found (for example, the symbol is contained in a not yet loaded module), winedbg will
1380           recall the name of the symbol and will try to set the breakpoint each time a new module is loaded (until it succeeds).
1381         </para>
1382       </sect2>
1383
1384       <sect2>
1385         <title>Stack manipulation</title>
1386
1387         <screen>
1388 bt              print calling stack of current thread
1389 bt N            print calling stack of thread of ID N (note: this
1390                 doesn't change the position of the current frame as
1391                 manipulated by the up & dn commands)
1392 up              goes up one frame in current thread's stack
1393 up N            goes up N frames in current thread's stack
1394 dn              goes down one frame in current thread's stack
1395 dn N            goes down N frames in current thread's stack
1396 frame N         set N as the current frame for current thread's stack
1397 info local      prints information on local variables for current
1398                 function
1399         </screen>
1400       </sect2>
1401
1402       <sect2>
1403         <title>Directory & source file manipulation</title>
1404
1405         <screen>
1406 show dir
1407 dir &lt;pathname&gt;
1408 dir
1409 symbolfile &lt;pathname&gt;   loads external symbol definition
1410 symbolfile &lt;pathname&gt; N loads external symbol definition
1411                            (applying an offset of N to addresses)
1412         </screen>
1413         <screen>
1414 list            lists 10 source lines from current position
1415 list -          lists 10 source lines before current position
1416 list N          lists 10 source lines from line N in current file
1417 list &lt;path&gt;:N     lists 10 source lines from line N in file &lt;path&gt;
1418 list &lt;id&gt; lists 10 source lines of function &lt;id&gt;
1419 list * N        lists 10 source lines from address N
1420         </screen>
1421         <para>
1422           You can specify the end target (to change the 10 lines
1423           value) using the ','. For example:
1424         </para>
1425         <screen>
1426 list 123, 234   lists source lines from line 123 up to line 234 in
1427                 current file
1428 list foo.c:1,56 lists source lines from line 1 up to 56 in file foo.c
1429         </screen>
1430       </sect2>
1431
1432       <sect2>
1433         <title>Displaying</title>
1434
1435         <para>
1436           A display is an expression that's evaluated and printed
1437           after the execution of any <command>WineDbg</command>
1438           command.
1439         </para>
1440         <screen>
1441 display         lists the active displays
1442 info display    (same as above command)
1443 display &lt;expr&gt;    adds a display for expression &lt;expr&gt;
1444 display /fmt &lt;expr&gt;       adds a display for expression &lt;expr&gt;. Printing
1445                 evaluated &lt;expr&gt; is done using the given format (see
1446                 print command for more on formats)
1447 del display N   deletes display #N
1448 undisplay N     (same as del display)
1449         </screen>
1450       </sect2>
1451
1452       <sect2>
1453         <title>Disassembly</title>
1454
1455         <screen>
1456 disas           disassemble from current position
1457 disas &lt;expr&gt;      disassemble from address &lt;expr&gt;
1458 disas &lt;expr&gt;,&lt;expr&gt;disassembles code between addresses specified by
1459                 the two &lt;expr&gt;
1460         </screen>
1461       </sect2>
1462
1463       <sect2>
1464         <title>Information on Wine's internals</title>
1465
1466         <screen>
1467 info class &lt;id&gt;   prints information on Windows's class &lt;id&gt;
1468 walk class      lists all Windows' class registered in Wine
1469 info share      lists all the dynamic libraries loaded the debugged
1470                 program (including .so files, NE and PE DLLs)
1471 info module N   prints information on module of handle N
1472 walk module     lists all modules loaded by debugged program
1473 info queue N    prints information on Wine's queue N
1474 walk queue      lists all queues allocated in Wine
1475 info regs       prints the value of CPU register
1476 info segment N  prints information on segment N
1477 info segment    lists all allocated segments
1478 info stack      prints the values on top of the stack
1479 info map        lists all virtual mappings used by the debugged
1480                 program
1481 info wnd N      prints information of Window of handle N
1482 walk wnd        lists all the window hierarchy starting from the
1483                 desktop window
1484 walk wnd N      lists all the window hierarchy starting from the
1485                 window of handle N
1486 walk process    lists all w-processes in Wine session
1487 walk thread     lists all w-threads in Wine session
1488 walk modref     (no longer avail)
1489 walk exception  lists the exception frames (starting from current
1490                 stack frame)
1491         </screen>
1492       </sect2>
1493
1494       <sect2>
1495         <title>Memory (reading, writing, typing)</title>
1496
1497         <screen>
1498 x &lt;expr&gt;  examines memory at &lt;expr&gt; address
1499 x /fmt &lt;expr&gt;     examines memory at &lt;expr&gt; address using format /fmt
1500 print &lt;expr&gt;      prints the value of &lt;expr&gt; (possibly using its type)
1501 print /fmt &lt;expr&gt; prints the value of &lt;expr&gt; (possibly using its
1502                 type)
1503 set &lt;lval&gt;=&lt;expr&gt;   writes the value of &lt;expr&gt; in &lt;lval&gt;
1504 whatis &lt;expr&gt;     prints the C type of expression &lt;expr&gt;
1505         </screen>
1506         <para>
1507           <filename>/fmt</filename> is either <filename>/&lt;letter&gt;</filename> or
1508           <filename>/&lt;count&gt;&lt;letter&gt;</filename> letter can be
1509         </para>
1510         <screen>
1511 s =&gt; an ASCII string
1512 u =&gt; an Unicode UTF16 string
1513 i =&gt; instructions (disassemble)
1514 x =&gt; 32 bit unsigned hexadecimal integer
1515 d =&gt; 32 bit signed decimal integer
1516 w =&gt; 16 bit unsigned hexadecimal integer
1517 c =&gt; character (only printable 0x20-0x7f are actuallyprinted)
1518 b =&gt; 8 bit unsigned hexadecimal integer
1519 g =&gt; GUID
1520         </screen>
1521       </sect2>
1522
1523       <sect2>
1524         <title>Expressions</title>
1525
1526         <para>
1527           Expressions in Wine Debugger are mostly written in a C form. However, there
1528           are a few discrepancies:
1529           <itemizedlist>
1530             <listitem>
1531               <para>
1532                 Identifiers can take a '.' in their names. This allow
1533                 mainly to access symbols from different DLLs like
1534                 <function>USER32.DLL.CreateWindowA</function>.
1535               </para>
1536             </listitem>
1537             <listitem>
1538               <para>
1539                 The debugger will try to distinguish this writing with structure operations.
1540                 Therefore, you can only use the previous writing in operations manipulating
1541                 symbols ({break|watch}points, type information command...).
1542               </para>
1543             </listitem>
1544           </itemizedlist>
1545         </para>
1546       </sect2>
1547       <sect2>
1548           <title>Debug channels</title>
1549               <para>
1550                It is possible to turn on and off debug messages as you are debuging using
1551                the set command.
1552               </para>
1553             <screen>
1554 set + warn win  =&gt; turn on warn on 'win' channel
1555 set + win       =&gt; turn on warn/fixme/err/trace on 'win' channel
1556 set - win       =&gt; turn off warn/fixme/err/trace on 'win' channel
1557 set - fixme     =&gt; turn off the 'fixme' class
1558             </screen>
1559       </sect2>
1560
1561     </sect1>
1562
1563     <sect1 id="dbg-others">
1564       <title>Other debuggers</title>
1565
1566       <sect2>
1567         <title>GDB mode</title>
1568
1569         <para>
1570           WineDbg can act as a remote monitor for GDB. This allows to
1571           use all the power of GDB, but while debugging wine and/or
1572           any Win32 application. To enable this mode, just add
1573           <parameter>--gdb</parameter> to winedbg command line. You'll
1574           end up on a GDB prompt. You'll have to use the GDB commands
1575           (not the wine nes).
1576         </para>
1577
1578         <para>
1579           However, some limitation in GDB while debugging wine (see
1580           below) don't ppear in this mode:
1581           <itemizedlist>
1582             <listitem>
1583               <para>
1584                 GDB will correctly present Win32 thread
1585                 information and breakpoint behavior
1586               </para> 
1587             </listitem>
1588             <listitem>
1589               <para>
1590                 Moreover, it also provides support for the Dwarf II
1591                 debug format (which became the default format (instead
1592                 of stabs) in gcc 3.1). 
1593               </para>
1594             </listitem>
1595           </itemizedlist>
1596         </para>
1597
1598         <para>
1599           A few wine extensions available through the monitor command.
1600 <screen>
1601 monitor wnd           lists all window in the Wine session
1602 monitor proc          lists all processes in the Wine session
1603 monitor mem           displays memory mapping of debugged process
1604                       (doesn't work)
1605 </screen>
1606         </para>
1607       </sect2>
1608
1609       <sect2>
1610         <title>Using other Unix debuggers</title>
1611
1612         <para>
1613           You can also use other debuggers (like
1614           <command>gdb</command>), but you must be aware of a few
1615           items:
1616         </para>
1617         <para>
1618           You need to attach the unix debugger to the correct unix
1619           process (representing the correct windows thread) (you can
1620           "guess" it from a <command>ps fax</command> for example:
1621           When running the emulator, usually the first two
1622           <varname>upids</varname> are for the Windows' application
1623           running the desktop, the first thread of the application is
1624           generally the third <varname>upid</varname>; when running a
1625           Winelib program, the first thread of the application is
1626           generally the first <varname>upid</varname>)
1627         </para>
1628         <note>
1629           <para>
1630             Even if latest <command>gdb</command> implements the
1631             notion of threads, it won't work with Wine because the
1632             thread abstraction used for implementing Windows' thread
1633             is not 100% mapped onto the linux posix threads
1634             implementation. It means that you'll have to spawn a
1635             different <command>gdb</command> session for each Windows'
1636             thread you wish to debug.
1637           </para>
1638         </note>
1639
1640         <!-- *** Extra content spliced in from article by Andreas Mohr *** -->
1641         <para>
1642           Following text written by &name-andreas-mohr; <email>&email-andreas-mohr;</email>
1643         </para>
1644         <para>
1645           Here's how to get info about the current execution status of a
1646           certain Wine process:
1647         </para>
1648         <para>
1649           Change into your Wine source dir and enter:
1650         </para>
1651         <screen>
1652 $ gdb wine
1653         </screen>
1654         <para>
1655           Switch to another console and enter <command>ps ax | grep
1656             wine</command> to find all wine processes. Inside
1657           <command>gdb</command>, repeat for all Wine processes:
1658         </para>
1659         <screen>
1660 (gdb) attach <userinput>PID</userinput>
1661         </screen>
1662         <para>
1663           with <userinput>PID</userinput> being the process ID of one of
1664           the Wine processes.  Use
1665         </para>
1666         <screen>
1667 (gdb) bt
1668         </screen>
1669         <para>
1670           to get the backtrace of the current Wine process, i.e. the
1671           function call history.  That way you can find out what the
1672           current process is doing right now.  And then you can use
1673           several times:
1674         </para>
1675         <screen>
1676 (gdb) n
1677         </screen>
1678         <para>
1679           or maybe even
1680         </para>
1681         <screen>
1682 (gdb) b <userinput>SomeFunction</userinput>
1683         </screen>
1684         <para>
1685           and
1686         </para>
1687         <screen>
1688 (gdb) c
1689         </screen>
1690         <para>
1691           to set a breakpoint at a certain function and continue up to
1692           that function.  Finally you can enter
1693         </para>
1694         <screen>
1695 (gdb) detach
1696         </screen>
1697         <para>
1698           to detach from the Wine process.
1699         </para>
1700         <!-- *** End of xtra content *** -->
1701       </sect2>
1702
1703       <sect2>
1704         <title>Using other Windows debuggers</title>
1705
1706         <para>
1707           You can use any Windows' debugging API compliant debugger
1708           with Wine. Some reports have been made of success with
1709           VisualStudio debugger (in remote mode, only the hub runs
1710           in Wine). GoVest fully runs in Wine.
1711         </para>
1712       </sect2>
1713
1714       <sect2>
1715         <title>Main differences between winedbg and regular Unix debuggers</title>
1716         <table><title>Debuggers comparison</title>
1717           <tgroup cols=2 align="left">
1718             <tbody>
1719               <row>
1720                 <entry>WineDbg</entry><entry>gdb</entry>
1721               </row>
1722               <row>
1723                 <entry>
1724                   WineDbg debugs a Windows' process: the various
1725                   threads will be handled by the same WineDbg session,
1726                   and a breakpoint will be triggered for any thread of
1727                   the W-process 
1728                 </entry>
1729                 <entry>
1730                   gdb debugs a Windows' thread: a separate gdb session
1731                   is needed for each thread of a Windows' process and
1732                   a breakpoint will be triggered only for the w-thread
1733                   debugged 
1734                 </entry>
1735               </row>
1736               <row>
1737                 <entry>
1738                   WineDbg supports debug information from stabs
1739                   (standard Unix format) and Microsoft's C, CodeView,
1740                   .DBG
1741                 </entry>
1742                 <entry>
1743                   GDB supports debug information from stabs (standard
1744                   Unix format) and Dwarf II.
1745                 </entry>
1746               </row>
1747             </tbody>
1748           </tgroup>
1749         </table>
1750       </sect2>
1751     </sect1>
1752
1753
1754     <sect1 id="dbg-limits">
1755       <title>Limitations</title>
1756
1757       <itemizedlist>
1758         <listitem>
1759           <para>
1760             16 bit processes are not supported (but calls to 16 bit
1761             code in 32 bit  applications are).
1762           </para>
1763         </listitem>
1764         <listitem>
1765           <para>
1766             Function call in expression is no longer supported
1767           </para>
1768         </listitem>
1769       </itemizedlist>
1770     </sect1>
1771   </chapter>
1772
1773 <!-- Keep this comment at the end of the file
1774 Local variables:
1775 mode: sgml
1776 sgml-parent-document:("wine-doc.sgml" "set" "book" "part" "chapter" "")
1777 End:
1778 -->