Documentation updates.
[wine] / documentation / debugging
1 This file describes where to start debugging Wine and how to write
2 useful bug reports.
3
4 To avoid debugging
5 ==================
6
7 Sometimes you can bring applications to run by using some of the
8 native Windows DLL's, together with Wine. Here are some Tips by
9 Juergen Schmied on how to proceed. This assumes that your C:\windows
10 directory in the configuration file does not point to a native Windows
11 installation but is in a separate Unix file system. (For instance,
12 C:\windows is really /home/ego/wine/drives/c).
13
14 - Create empty C:\windows and C:\windows\system directories.
15   Do not point Wine to a Windows directory full of old installations
16   and a messy registry. (Wine creates a special registry in your home
17   directory, in $HOME/.wine/*.reg. Perhaps you have to remove these
18   files).
19 - Run the application with   -debugmsg +module,+file   to find out
20   which files are needed. Copy the required DLL's one by one to the
21   C:\windows\system directory.
22 - Note that some network DLL's are not needed even though Wine is
23   looking for them. Do not copy the MPR.DLL into the directory,
24   use the internal implementation.
25 - Copy SHELL/SHELL32 and COMDLG/COMDLG32 COMMCTRL/COMCTL32
26   only as pairs to your Wine directory (these DLL's are
27   "clean" to use)
28 - Be consistent: Use only DLLS from the same Windows version
29   together.
30 - Put regedit.exe in the C:\windows directory (office95 imports
31   a *.reg file when it runs with a empty registry, don't know
32   about office97).
33
34
35 Crashes
36 =======
37
38   These usually show up like this:
39
40 |Unexpected Windows program segfault - opcode = 8b
41 |Segmentation fault in Windows program 1b7:c41.
42 |Loading symbols from ELF file /root/wine/wine...
43 |....more Loading symbols from ...
44 |In 16 bit mode.
45 |Register dump:
46 | CS:01b7 SS:016f DS:0287 ES:0000
47 | IP:0c41 SP:878a BP:8796 FLAGS:0246
48 | AX:811e BX:0000 CX:0000 DX:0000 SI:0001 DI:ffff
49 |Stack dump:
50 |0x016f:0x878a:  0001 016f ffed 0000 0000 0287 890b 1e5b
51 |0x016f:0x879a:  01b7 0001 000d 1050 08b7 016f 0001 000d
52 |0x016f:0x87aa:  000a 0003 0004 0000 0007 0007 0190 0000
53 |0x016f:0x87ba:
54 |
55 |0050: sel=0287 base=40211d30 limit=0b93f (bytes) 16-bit rw-
56 |Backtrace:
57 |0 0x01b7:0x0c41 (PXSRV_FONGETFACENAME+0x7c)
58 |1 0x01b7:0x1e5b (PXSRV_FONPUTCATFONT+0x2cd)
59 |2 0x01a7:0x05aa
60 |3 0x01b7:0x0768 (PXSRV_FONINITFONTS+0x81)
61 |4 0x014f:0x03ed (PDOXWIN_@SQLCURCB$Q6CBTYPEULN8CBSCTYPE+0x1b1)
62 |5 0x013f:0x00ac
63 |
64 |0x01b7:0x0c41 (PXSRV_FONGETFACENAME+0x7c):  movw        %es:0x38(%bx),%dx
65
66 Steps to debug a crash. You may stop at any step, but please report the bug
67 and provide as much of the information gathered to the newsgroup or the
68 relevant developer as feasonable.
69   
70  1. Get the reason for the crash. This is usually an access to an invalid
71     selector, an access to an out of range address in a valid selector,
72     popping a segmentregister from the stack or the like. When reporting a
73     crash, report this WHOLE crashdump even if it doesn't make sense to you.
74
75     (In this case it is access to an invalid selector, for %es is 0000, as
76     seen in the register dump).
77     
78  2. Determine where the reason came from.
79     Since this is usually a primary/secondary reaction to a failed or
80     misbehaving Wine function, rerun Wine with "-debugmsg +relay" (without ")
81     added to the commandline. This will get rather much output, but usually
82     the reason is located in the last call(s).  Those lines usually look like
83     this:
84
85 |Call KERNEL.90: LSTRLEN(0227:0692 "text") ret=01e7:2ce7 ds=0227
86       ^^^^^^^^^  ^       ^^^^^^^^^ ^^^^^^      ^^^^^^^^^    ^^^^
87       |          |       |         |           |            |Datasegment
88       |          |       |         |           |Return address
89       |          |       |         |textual parameter
90       |          |       |
91       |          |       |Argument(s). This one is a win16 segmented pointer.
92       |          |Function called.
93       |The module, the function is called in. In this case it is KERNEL.
94                         
95 |Ret  KERNEL.90: LSTRLEN() retval=0x0004 ret=01e7:2ce7 ds=0227
96                                   ^^^^^^
97                                   |Returnvalue is 16 bit and has the value 4.
98
99
100  3. If you have found a misbehaving function, try to find out why it
101     misbehaves. Find the function in the source code. Try to make sense of
102     the arguments passed. Usually there is a 'TRACE(<channel>,"(...)\n");' 
103     at the beginning of the function. Rerun wine with 
104     "-debugmsg +xyz,+relay" added to the commandline.
105
106  4. Additional information on how to debug using the internal debugger can be 
107     found in debugger/README.
108
109  5. If those information isn't clear enough or if you want to know more about
110     what's happening in the function itself, try running wine with "-debugmsg
111     +all", which dumps ALL included debug information in wine.
112
113  6. If that isn't enough add more debug output for yourself into the
114     functions you find relevant.  See documentation/debug-msgs.
115     You might also try to run the program in gdb instead of using the
116     WINE-debugger. If you do that, use "handle SIGSEGV nostop noprint"
117     to disable the handling of seg faults inside gdb (needed for Win16).
118     If you don't use the "-desktop" or "-managed" option,
119     start the WINE process with "-sync", or chances are good to get X into
120     an unusable state.
121   
122  7. You can also set a breakpoint for that function. Start wine with the
123     "-debug" option added to the commandline. After loading the executable
124     wine will enter the internal debugger. Use "break KERNEL_LSTRLEN"
125     (replace by function you want to debug, CASE IS RELEVANT.) to set a
126     breakpoint.  Then use "continue" to start normal program-execution. Wine
127     will stop if it reaches the breakpoint. If the program isn't yet at the
128     crashing call of that function, use "continue" again until you are about
129     to enter that function. You may now proceed with single-stepping the
130     function until you reach the point of crash. Use the other debugger
131     commands to print registers and the like.
132
133
134 Program hangs, nothing happens
135 ==============================
136
137   Switch to UNIX shell, get the process-ID using "ps -a|grep wine", and do a
138   "kill -HUP <pid>" (without " and <>). Wine will then enter its internal
139   debugger and you can proceed as explained above. Also, you can use -debug
140   switch and then you can get into internal debugger by pressing Ctrl-C in 
141   the terminal where you run Wine.
142
143 Program reports an error with a Messagebox
144 ==========================================
145
146   Sometimes programs are reporting failure using a more or less nondescript
147   messageboxes. We can debug this using the same method as Crashes, but there
148   is one problem... For setting up a message box the program also calls Wine
149   producing huge chunks of debug code.
150
151   Since the failure happens usually directly before setting up the Messagebox
152   you can start wine with "-debug" added to the commandline, set a breakpoint
153   at "MessageBox32A" (called by win16 and win32 programs) and proceed with
154   "continue". With "-debugmsg +all" Wine will now stop directly before 
155   setting up the Messagebox.  Proceed as explained above.
156
157   You can also run wine using "wine -debugmsg +relay program.exe 2>&1|less -i"
158   and in less search for messagebox.
159
160 Disassembling programs:
161 =======================
162   You may also try to disassemble the offending program to check for 
163   undocumented features and/or use of them.
164
165   The best, freely available, disassembler for Win16 programs is
166   Windows Codeback, archivename wcbxxx.zip, which usually can be found
167   in the Cica-Mirror subdirectory on the WINE ftpsites. (See ANNOUNCE).
168   Disassembling win32 programs is possible using the Windows Disassembler 32,
169   archivename something like w32dasm.zip on ftp.winsite.com and mirrors.
170   The shareware version does not allow saving of disassembly listings.
171
172   [It also has a bug, it disassembles the dll and immediately after that
173    crashes, leaving a very large file caled 'winsys' in the directory of the
174    disassembled file. This file contains nothing of value (just the disassembly)
175    and can be safely deleted.]
176
177   Understanding disassembled code is just a question of exercise.
178
179   Most code out there uses standard C function entries (for it is usually 
180   written in C). Win16 function entries usually look like that:
181 |       push bp
182 |       mov bp, sp
183 |       ... function code ..
184 |       retf XXXX       <--------- XXXX is number of bytes of arguments
185
186   This is a FAR function with no local storage. The arguments usually start
187   at [bp+6] with increasing offsets. Note, that [bp+6] belongs to the RIGHTMOST 
188   argument, for exported win16 functions use the PASCAL calling convention.
189   So, if we use strcmp(a,b) with a and b both 32 bit variables b would be at
190   [bp+6] and a at [bp+10].
191   Most functions make also use of local storage in the stackframe:
192 |       enter 0086, 00
193 |       ... function code ...
194 |       leave
195 |       retf XXXX
196   This does mostly the same as above, but also adds 0x86 bytes of
197   stackstorage, which is accessed using [bp-xx].
198   Before calling a function, arguments are pushed on the stack using something
199   like this:
200 |       push word ptr [bp-02]   <- will be at [bp+8]
201 |       push di                 <- will be at [bp+6]
202 |       call KERNEL.LSTRLEN
203   Here first the selector and then the offset to the passed string are pushed.
204
205 Sample debugging session:
206 =========================
207
208   Let's debug the infamous Word SHARE.EXE messagebox: 
209
210 |marcus@jet $ wine winword.exe
211 |            +---------------------------------------------+
212 |            | !  You must leave Windows and load SHARE.EXE|
213 |            |    before starting Word.                    |
214 |            +---------------------------------------------+
215
216
217 |marcus@jet $ wine winword.exe -debugmsg +relay -debug
218 |CallTo32(wndproc=0x40065bc0,hwnd=000001ac,msg=00000081,wp=00000000,lp=00000000)
219 |Win16 task 'winword': Breakpoint 1 at 0x01d7:0x001a
220 |CallTo16(func=0127:0070,ds=0927)
221 |Call WPROCS.24: TASK_RESCHEDULE() ret=00b7:1456 ds=0927
222 |Ret  WPROCS.24: TASK_RESCHEDULE() retval=0x8672 ret=00b7:1456 ds=0927
223 |CallTo16(func=01d7:001a,ds=0927)
224 |     AX=0000 BX=3cb4 CX=1f40 DX=0000 SI=0000 DI=0927 BP=0000 ES=11f7
225 |Loading symbols: /home/marcus/wine/wine...
226 |Stopped on breakpoint 1 at 0x01d7:0x001a
227 |In 16 bit mode.
228 |Wine-dbg>break MessageBox32A                          <---- Set Breakpoint
229 |Breakpoint 2 at 0x40189100 (MessageBox32A [msgbox.c:190])
230 |Wine-dbg>c                                            <---- Continue
231 |Call KERNEL.91: INITTASK() ret=0157:0022 ds=08a7
232 |     AX=0000 BX=3cb4 CX=1f40 DX=0000 SI=0000 DI=08a7 ES=11d7 EFL=00000286
233 |CallTo16(func=090f:085c,ds=0dcf,0x0000,0x0000,0x0000,0x0000,0x0800,0x0000,0x0000,0x0dcf)
234 |...                                                   <----- Much debugoutput
235 |Call KERNEL.136: GETDRIVETYPE(0x0000) ret=060f:097b ds=0927
236                                ^^^^^^ Drive 0 (A:)
237 |Ret  KERNEL.136: GETDRIVETYPE() retval=0x0002 ret=060f:097b ds=0927
238                                         ^^^^^^  DRIVE_REMOVEABLE
239                                                 (It is a floppy diskdrive.)
240
241 |Call KERNEL.136: GETDRIVETYPE(0x0001) ret=060f:097b ds=0927
242                                ^^^^^^ Drive 1 (B:)
243 |Ret  KERNEL.136: GETDRIVETYPE() retval=0x0000 ret=060f:097b ds=0927
244                                         ^^^^^^  DRIVE_CANNOTDETERMINE
245                                                 (I don't have drive B: assigned)
246
247 |Call KERNEL.136: GETDRIVETYPE(0x0002) ret=060f:097b ds=0927
248                                ^^^^^^^ Drive 2 (C:)
249 |Ret  KERNEL.136: GETDRIVETYPE() retval=0x0003 ret=060f:097b ds=0927
250                                         ^^^^^^ DRIVE_FIXED
251                                                (specified as a harddisk)
252
253 |Call KERNEL.97: GETTEMPFILENAME(0x00c3,0x09278364"doc",0x0000,0927:8248) ret=060f:09b1 ds=0927
254                                  ^^^^^^           ^^^^^        ^^^^^^^^^
255                                  |                |            |buffer for fname
256                                  |                |temporary name ~docXXXX.tmp
257                                  |Force use of Drive C:.
258
259 |Warning: GetTempFileName returns 'C:~doc9281.tmp', which doesn't seem to be writeable.
260 |Please check your configuration file if this generates a failure.
261
262 Whoops, it even detects that something is wrong!
263
264 |Ret  KERNEL.97: GETTEMPFILENAME() retval=0x9281 ret=060f:09b1 ds=0927
265                                           ^^^^^^ Temporary storage ID
266
267 |Call KERNEL.74: OPENFILE(0x09278248"C:~doc9281.tmp",0927:82da,0x1012) ret=060f:09d8 ds=0927
268                                     ^^^^^^^^^^^^^^^^ ^^^^^^^^^ ^^^^^^^
269                                     |filename        |OFSTRUCT |open mode:
270
271                                        OF_CREATE|OF_SHARE_EXCLUSIVE|OF_READWRITE
272
273 This fails, since my C: drive is in this case mounted readonly.
274
275 |Ret  KERNEL.74: OPENFILE() retval=0xffff ret=060f:09d8 ds=0927
276                                    ^^^^^^ HFILE_ERROR16, yes, it failed.
277
278 |Call USER.1: MESSAGEBOX(0x0000,0x09278376"Sie müssen Windows verlassen und SHARE.EXE laden bevor Sie Word starten.",0x00000000,0x1030) ret=060f:084f ds=0927
279            
280 And MessageBox'ed.
281
282 |Stopped on breakpoint 2 at 0x40189100 (MessageBox32A [msgbox.c:190])
283 |190     {              <- the sourceline
284 In 32 bit mode.
285 Wine-dbg>
286
287         The code seems to find a writeable harddisk and tries to create a file
288         there. To work around this bug, you can define C: as a networkdrive,
289         which is ignored by the code above.
290
291 Written by Marcus Meissner <msmeissn@cip.informatik.uni-erlangen.de>,
292 additions welcome.
293 -------
294
295 Here are some useful debugging tips, added by Andreas Mohr:
296
297
298 a) If you have a program crashing at such an early loader phase that you can't
299 use the Wine debugger normally, but Wine already executes the program's
300 start code, then you may use a special trick:
301 You should do a
302 wine -debugmsg +relay program
303 to get a listing of the functions the program calls in its start function.
304 Now you do a
305 wine -debug winfile.exe
306 This way, you get into Wine-dbg. Now you can set a breakpoint on any
307 function the program calls in the start function and just type "c" to bypass
308 the eventual calls of Winfile to this function until you are finally at the
309 place where this function gets called by the crashing start function.
310 Now you can proceed with your debugging as usual.
311
312
313 b) If you try to run a program and it quits after showing an error messagebox,
314 the problem can usually be identified in the return value of one of the
315 functions executed before MessageBox().
316 That's why you should re-run the program with e.g.
317 wine -debugmsg +relay <program name> &>relmsg
318 Then do a "more relmsg" and search for the last occurrence of a call to the string "MESSAGEBOX".
319 This is a line like
320 Call USER.1: MESSAGEBOX(0x0000,0x01ff1246 "Runtime error 219 at 0004:1056.",0x00000000,0x1010) ret=01f7:2160 ds=01ff
321
322 In my example the lines before the call to MessageBox() look like that:
323
324 Call KERNEL.96: FREELIBRARY(0x0347) ret=01cf:1033 ds=01ff
325 CallTo16(func=033f:0072,ds=01ff,0x0000)
326 Ret  KERNEL.96: FREELIBRARY() retval=0x0001 ret=01cf:1033 ds=01ff
327 Call KERNEL.96: FREELIBRARY(0x036f) ret=01cf:1043 ds=01ff
328 CallTo16(func=0367:0072,ds=01ff,0x0000)
329 Ret  KERNEL.96: FREELIBRARY() retval=0x0001 ret=01cf:1043 ds=01ff
330 Call KERNEL.96: FREELIBRARY(0x031f) ret=01cf:105c ds=01ff
331 CallTo16(func=0317:0072,ds=01ff,0x0000)
332 Ret  KERNEL.96: FREELIBRARY() retval=0x0001 ret=01cf:105c ds=01ff
333 Call USER.171: WINHELP(0x02ac,0x01ff05b4 "COMET.HLP",0x0002,0x00000000) ret=01cf:1070 ds=01ff
334 CallTo16(func=0117:0080,ds=01ff)
335 Call WPROCS.24: TASK_RESCHEDULE() ret=00a7:0a2d ds=002b
336 Ret  WPROCS.24: TASK_RESCHEDULE() retval=0x0000 ret=00a7:0a2d ds=002b
337 Ret  USER.171: WINHELP() retval=0x0001 ret=01cf:1070 ds=01ff
338 Call KERNEL.96: FREELIBRARY(0x01be) ret=01df:3e29 ds=01ff
339 Ret  KERNEL.96: FREELIBRARY() retval=0x0000 ret=01df:3e29 ds=01ff
340 Call KERNEL.52: FREEPROCINSTANCE(0x02cf00ba) ret=01f7:1460 ds=01ff
341 Ret  KERNEL.52: FREEPROCINSTANCE() retval=0x0001 ret=01f7:1460 ds=01ff
342 Call USER.1: MESSAGEBOX(0x0000,0x01ff1246 "Runtime error 219 at 0004:1056.",0x00000000,0x1010) ret=01f7:2160 ds=01ff
343
344 I think that the call to MessageBox() in this example is _not_ caused by a wrong result value of some previously executed function (it's happening quite often like that), but instead the messagebox complains about a runtime error at 0x0004:0x1056.
345 As the segment value of the address is only "4", I think that that is only an internal program value. But the offset address reveals something quite interesting:
346 Offset 1056 is _very_ close to the return address of FREELIBRARY():
347
348 Call KERNEL.96: FREELIBRARY(0x031f) ret=01cf:105c ds=01ff
349                                              ^^^^
350 Provided that segment 0x0004 is indeed segment 0x1cf, we now we can use IDA (available at ftp://ftp.uni-koeln.de/pc/msdos/programming/assembler/ida35bx.zip) to
351 disassemble the part that caused the error. We just have to find the address of
352 the call to FreeLibrary(). Some lines before that the runtime error occurred.
353 But be careful ! In some cases you don't have to disassemble the main program, but instead some DLL called by it in order to find the correct place where the runtime error occurred. That can be determined by finding the origin of the segment value (in this case 0x1cf).
354
355 c) If you have created a relay file of some crashing program and want to set a
356 breakpoint at a certain location which is not yet available as the
357 program loads the breakpoint's segment during execution,
358 you may set a breakpoint to GetVersion16/32 as those functions are called
359 very often.
360 Then do a "c" until you are able to set this breakpoint without error message.
361
362 d) Some useful programs:
363 IDA: ftp://ftp.uni-koeln.de/pc/msdos/programming/assembler/ida35bx.zip
364 *Very* good DOS disassembler ! It's badly needed for debugging Wine sometimes.
365
366 XRAY: ftp://ftp.th-darmstadt.de/pub/machines/ms-dos/SimTel/msdos/asmutil/xray15.zip
367 Traces DOS calls (Int 21h, DPMI, ...). Use it with Windows to correct
368 file management problems etc.
369
370 pedump: http://oak.oakland.edu/pub/simtelnet/win95/prog/pedump.zip
371 Dumps the imports and exports of a PE (Portable Executable) DLL.
372
373
374 Some basic debugger usages:
375 ===========================
376
377 After starting you program with
378   wine -debug myprog.exe
379 the program loads and you get a prompt at the program starting point.
380 Then you can set breakpoints:
381   b RoutineName      (by outine name) OR
382   b *0x812575        (by address)
383 Then you hit 'c' (continue) to run the program. It stops at
384 the breakpoint. You can type
385   step               (to step one line) OR
386   stepi              (to step one machine instruction at a time;
387                       here, it helps to know the basic 386
388                       instruction set)
389   info reg           (to see registers)
390   info stack         (to see hex values in the stack)
391   info local         (to see local variables)
392   list <line number> (to list source code)
393   x <variable name>  (to examine a variable; only works if code
394                       is not compiled with optimization)
395   x 0x4269978        (to examine a memory location)
396   ?                  (help)
397   q                  (quit)
398 By hitting Enter, you repeat the last command.