Compile the debugger with STRICT on.
[wine] / debugger / info.c
1 /*
2  * Wine debugger utility routines
3  *
4  * Copyright 1993 Eric Youngdale
5  * Copyright 1995 Alexandre Julliard
6  */
7
8 #include "config.h"
9 #include <stdlib.h>
10 #include <string.h>
11 #include "winbase.h"
12 #include "wingdi.h"
13 #include "winuser.h"
14 #include "toolhelp.h"
15 #include "tlhelp32.h"
16 #include "debugger.h"
17 #include "expr.h"
18
19 /***********************************************************************
20  *           DEBUG_PrintBasic
21  *
22  * Implementation of the 'print' command.
23  */
24 void DEBUG_PrintBasic( const DBG_VALUE* value, int count, char format )
25 {
26   char        * default_format;
27   long long int res;
28
29   assert(value->cookie == DV_TARGET || value->cookie == DV_HOST);
30   if( value->type == NULL ) 
31     {
32       DEBUG_Printf(DBG_CHN_MESG, "Unable to evaluate expression\n");
33       return;
34     }
35   
36   default_format = NULL;
37   res = DEBUG_GetExprValue(value, &default_format);
38
39   switch(format)
40     {
41     case 'x':
42       if (value->addr.seg) 
43         {
44           DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%04lx", (long unsigned int) res );
45         }
46       else 
47         {
48           DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "0x%08lx", (long unsigned int) res );
49         }
50       break;
51       
52     case 'd':
53       DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "%ld\n", (long int) res );
54       break;
55       
56     case 'c':
57       DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, "%d = '%c'",
58                                    (char)(res & 0xff), (char)(res & 0xff) );
59       break;
60       
61     case 'i':
62     case 's':
63     case 'w':
64     case 'b':
65       DEBUG_Printf( DBG_CHN_MESG, "Format specifier '%c' is meaningless in 'print' command\n", format );
66     case 0:
67       if( default_format != NULL )
68         {
69           if (strstr(default_format, "%S") == NULL)
70             {
71                DEBUG_nchar += DEBUG_Printf( DBG_CHN_MESG, default_format, res );
72             } 
73           else
74             {
75                char*    ptr;
76                int      state = 0;
77
78                /* FIXME: simplistic implementation for default_format being
79                 * foo%Sbar => will print foo, then string then bar
80                 */
81                for (ptr = default_format; *ptr; ptr++) 
82                {
83                   if (*ptr == '%') state++;
84                   else if (state == 1) 
85                     {
86                        if (*ptr == 'S') 
87                          {
88                             char        ch;
89                             char*       str = (char*)(long)res;
90
91                             for (; DEBUG_READ_MEM(str, &ch, 1) && ch; str++) {
92                                DEBUG_Output(DBG_CHN_MESG, &ch, 1);
93                                DEBUG_nchar++;
94                             }
95                          }
96                        else 
97                          {
98                             /* shouldn't happen */
99                             DEBUG_Printf(DBG_CHN_MESG, "%%%c", *ptr);
100                             DEBUG_nchar += 2;
101                          }
102                        state = 0;
103                     }
104                   else
105                     {
106                        DEBUG_Output(DBG_CHN_MESG, ptr, 1);
107                        DEBUG_nchar++;
108                     }
109                }
110             } 
111         }
112       break;
113     }
114 }
115
116
117 /***********************************************************************
118  *           DEBUG_PrintAddress
119  *
120  * Print an 16- or 32-bit address, with the nearest symbol if any.
121  */
122 struct symbol_info
123 DEBUG_PrintAddress( const DBG_ADDR *addr, enum dbg_mode mode, int flag )
124 {
125     struct symbol_info rtn;
126
127     const char *name = DEBUG_FindNearestSymbol( addr, flag, &rtn.sym, 0, 
128                                                 &rtn.list );
129
130     if (addr->seg) DEBUG_Printf( DBG_CHN_MESG, "0x%04lx:", addr->seg&0xFFFF );
131     if (mode != MODE_32) DEBUG_Printf( DBG_CHN_MESG, "0x%04lx", addr->off );
132     else DEBUG_Printf( DBG_CHN_MESG, "0x%08lx", addr->off );
133     if (name) DEBUG_Printf( DBG_CHN_MESG, " (%s)", name );
134     return rtn;
135 }
136 /***********************************************************************
137  *           DEBUG_PrintAddressAndArgs
138  *
139  * Print an 16- or 32-bit address, with the nearest symbol if any.
140  * Similar to DEBUG_PrintAddress, but we print the arguments to
141  * each function (if known).  This is useful in a backtrace.
142  */
143 struct symbol_info
144 DEBUG_PrintAddressAndArgs( const DBG_ADDR *addr, enum dbg_mode mode,
145                            unsigned int ebp, int flag )
146 {
147     struct symbol_info rtn;
148
149     const char *name = DEBUG_FindNearestSymbol( addr, flag, &rtn.sym, ebp, 
150                                                 &rtn.list );
151
152     if (addr->seg) DEBUG_Printf( DBG_CHN_MESG, "0x%04lx:", addr->seg );
153     if (mode != MODE_32) DEBUG_Printf( DBG_CHN_MESG, "0x%04lx", addr->off );
154     else DEBUG_Printf( DBG_CHN_MESG, "0x%08lx", addr->off );
155     if (name) DEBUG_Printf( DBG_CHN_MESG, " (%s)", name );
156
157     return rtn;
158 }
159
160
161 /***********************************************************************
162  *           DEBUG_Help
163  *
164  * Implementation of the 'help' command.
165  */
166 void DEBUG_Help(void)
167 {
168     int i = 0;
169     static const char * const helptext[] =
170 {
171 "The commands accepted by the Wine debugger are a reasonable",
172 "subset of the commands that gdb accepts.",
173 "The commands currently are:",
174 "  help                                   quit",
175 "  break [*<addr>]                        delete break bpnum",
176 "  disable bpnum                          enable bpnum",
177 "  condition <bpnum> [<expr>]             pass",
178 "  bt                                     cont [N]",
179 "  step [N]                               next [N]",
180 "  stepi [N]                              nexti [N]",
181 "  x <addr>                               print <expr>",
182 "  set <reg> = <expr>                     set *<addr> = <expr>",
183 "  up                                     down",
184 "  list <lines>                           disassemble [<addr>][,<addr>]",
185 "  frame <n>                              finish",
186 "  show dir                               dir <path>",
187 "  display <expr>                         undisplay <disnum>",
188 "  delete display <disnum>                debugmsg <class>[-+]<type>\n",
189 "  mode [16,32,vm86]                      walk [wnd,class,queue,module,",
190 "  whatis                                       process,modref <pid>]",
191 "  info (see 'help info' for options)\n",
192
193 "The 'x' command accepts repeat counts and formats (including 'i') in the",
194 "same way that gdb does.\n",
195
196 " The following are examples of legal expressions:",
197 " $eax     $eax+0x3   0x1000   ($eip + 256)  *$eax   *($esp + 3)",
198 " Also, a nm format symbol table can be read from a file using the",
199 " symbolfile command.  Symbols can also be defined individually with",
200 " the define command.",
201 "",
202 NULL
203 };
204
205     while(helptext[i]) DEBUG_Printf(DBG_CHN_MESG,"%s\n", helptext[i++]);
206 }
207
208
209 /***********************************************************************
210  *           DEBUG_HelpInfo
211  *
212  * Implementation of the 'help info' command.
213  */
214 void DEBUG_HelpInfo(void)
215 {
216     int i = 0;
217     static const char * const infotext[] =
218 {
219 "The info commands allow you to get assorted bits of interesting stuff",
220 "to be displayed.  The options are:",
221 "  info break           Dumps information about breakpoints",
222 "  info display         Shows auto-display expressions in use",
223 "  info locals          Displays values of all local vars for current frame",
224 "  info maps            Dumps all virtual memory mappings",
225 "  info module <handle> Displays internal module state",
226 "  info queue <handle>  Displays internal queue state",
227 "  info reg             Displays values in all registers at top of stack",
228 "  info segments        Dumps information about all known segments",
229 "  info share           Dumps information about shared libraries",
230 "  info stack           Dumps information about top of stack",
231 "  info wnd <handle>    Displays internal window state",
232 "",
233 NULL
234 };
235
236     while(infotext[i]) DEBUG_Printf(DBG_CHN_MESG,"%s\n", infotext[i++]);
237 }
238
239 /* FIXME: merge InfoClass and InfoClass2 */
240 void DEBUG_InfoClass(const char* name)
241 {
242    WNDCLASSEXA  wca;
243
244    if (!GetClassInfoEx(0, name, &wca)) {
245       DEBUG_Printf(DBG_CHN_MESG, "Cannot find class '%s'\n", name);
246       return;
247    }
248
249    DEBUG_Printf(DBG_CHN_MESG,  "Class '%s':\n", name);
250    DEBUG_Printf(DBG_CHN_MESG,  
251                 "style=%08x  wndProc=%08lx\n"
252                 "inst=%p  icon=%p  cursor=%p  bkgnd=%p\n"
253                 "clsExtra=%d  winExtra=%d\n",
254                 wca.style, (DWORD)wca.lpfnWndProc, wca.hInstance,
255                 wca.hIcon, wca.hCursor, wca.hbrBackground,
256                 wca.cbClsExtra, wca.cbWndExtra);
257
258    /* FIXME: 
259     * + print #windows (or even list of windows...)
260     * + print extra bytes => this requires a window handle on this very class...
261     */
262 }
263
264 static  void DEBUG_InfoClass2(HWND hWnd, const char* name)
265 {
266    WNDCLASSEXA  wca;
267
268    if (!GetClassInfoEx((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), name, &wca)) {
269       DEBUG_Printf(DBG_CHN_MESG, "Cannot find class '%s'\n", name);
270       return;
271    }
272
273    DEBUG_Printf(DBG_CHN_MESG,  "Class '%s':\n", name);
274    DEBUG_Printf(DBG_CHN_MESG,  
275                 "style=%08x  wndProc=%08lx\n"
276                 "inst=%p  icon=%p  cursor=%p  bkgnd=%p\n"
277                 "clsExtra=%d  winExtra=%d\n",
278                 wca.style, (DWORD)wca.lpfnWndProc, wca.hInstance,
279                 wca.hIcon, wca.hCursor, wca.hbrBackground,
280                 wca.cbClsExtra, wca.cbWndExtra);
281
282    if (wca.cbClsExtra) {
283       int               i;
284       WORD              w;
285
286       DEBUG_Printf(DBG_CHN_MESG,  "Extra bytes:" );
287       for (i = 0; i < wca.cbClsExtra / 2; i++) {
288          w = GetClassWord(hWnd, i * 2);
289          /* FIXME: depends on i386 endian-ity */
290          DEBUG_Printf(DBG_CHN_MESG,  " %02x", HIBYTE(w));
291          DEBUG_Printf(DBG_CHN_MESG,  " %02x", LOBYTE(w));
292       }
293       DEBUG_Printf(DBG_CHN_MESG,  "\n" );
294     }
295     DEBUG_Printf(DBG_CHN_MESG,  "\n" );
296 }
297
298 struct class_walker {
299    ATOM*        table;
300    int          used;
301    int          alloc;
302 };
303
304 static  void DEBUG_WalkClassesHelper(HWND hWnd, struct class_walker* cw)
305 {
306    char clsName[128];
307    int  i;
308    ATOM atom;
309    HWND child;
310
311    if (!GetClassName(hWnd, clsName, sizeof(clsName)))
312       return;
313    if ((atom = FindAtom(clsName)) == 0)
314       return;
315
316    for (i = 0; i < cw->used; i++) {
317       if (cw->table[i] == atom)
318          break;
319    }
320    if (i == cw->used) {
321       if (cw->used >= cw->alloc) {
322          cw->alloc += 16;
323          cw->table = DBG_realloc(cw->table, cw->alloc * sizeof(ATOM));
324       }
325       cw->table[cw->used++] = atom;
326       DEBUG_InfoClass2(hWnd, clsName);
327    }
328    do {
329       if ((child = GetWindow(hWnd, GW_CHILD)) != 0)
330          DEBUG_WalkClassesHelper(child, cw);
331    } while ((hWnd = GetWindow(hWnd, GW_HWNDNEXT)) != 0);
332 }
333
334 void DEBUG_WalkClasses(void)
335 {
336    struct class_walker cw;
337
338    cw.table = NULL;
339    cw.used = cw.alloc = 0;
340    DEBUG_WalkClassesHelper(GetDesktopWindow(), &cw);
341    DBG_free(cw.table);
342 }
343
344 void DEBUG_DumpQueue(DWORD q)
345 {
346    DEBUG_Printf(DBG_CHN_MESG, "No longer doing info queue '0x%08lx'\n", q);
347 }
348
349 void DEBUG_WalkQueues(void)
350 {
351    DEBUG_Printf(DBG_CHN_MESG, "No longer walking queues list\n");
352 }
353
354 void DEBUG_InfoWindow(HWND hWnd)
355 {
356    char clsName[128];
357    char wndName[128];
358    RECT clientRect;
359    RECT windowRect;
360    int  i;
361    WORD w;
362
363    if (!GetClassName(hWnd, clsName, sizeof(clsName)))
364        strcpy(clsName, "-- Unknown --");
365    if (!GetWindowText(hWnd, wndName, sizeof(wndName)))
366       strcpy(wndName, "-- Empty --");
367    if (!GetClientRect(hWnd, &clientRect))
368       SetRectEmpty(&clientRect);
369    if (!GetWindowRect(hWnd, &windowRect))
370       SetRectEmpty(&windowRect);
371
372    /* FIXME missing fields: hmemTaskQ, hrgnUpdate, dce, flags, pProp, scroll */
373    DEBUG_Printf(DBG_CHN_MESG,
374                 "next=%p  child=%p  parent=%p  owner=%p  class='%s'\n"
375                 "inst=%p  active=%p  idmenu=%08lx\n"
376                 "style=%08lx  exstyle=%08lx  wndproc=%08lx  text='%s'\n"
377                 "client=%d,%d-%d,%d  window=%d,%d-%d,%d sysmenu=%p\n",
378                 GetWindow(hWnd, GW_HWNDNEXT), 
379                 GetWindow(hWnd, GW_CHILD),
380                 GetParent(hWnd), 
381                 GetWindow(hWnd, GW_OWNER),
382                 clsName,
383                 (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE), 
384                 GetLastActivePopup(hWnd),
385                 GetWindowLong(hWnd, GWL_ID),
386                 GetWindowLong(hWnd, GWL_STYLE),
387                 GetWindowLong(hWnd, GWL_EXSTYLE),
388                 GetWindowLong(hWnd, GWL_WNDPROC),
389                 wndName, 
390                 clientRect.left, clientRect.top, clientRect.right, clientRect.bottom, 
391                 windowRect.left, windowRect.top, windowRect.right, windowRect.bottom, 
392                 GetSystemMenu(hWnd, FALSE));
393
394     if (GetClassLong(hWnd, GCL_CBWNDEXTRA)) {
395         DEBUG_Printf(DBG_CHN_MESG,  "Extra bytes:" );
396         for (i = 0; i < GetClassLong(hWnd, GCL_CBWNDEXTRA) / 2; i++) {
397            w = GetWindowWord(hWnd, i * 2);
398            /* FIXME: depends on i386 endian-ity */
399            DEBUG_Printf(DBG_CHN_MESG,  " %02x", HIBYTE(w));
400            DEBUG_Printf(DBG_CHN_MESG,  " %02x", LOBYTE(w));
401         }
402         DEBUG_Printf(DBG_CHN_MESG, "\n");
403     }
404     DEBUG_Printf(DBG_CHN_MESG, "\n");
405 }
406
407 void DEBUG_WalkWindows(HWND hWnd, int indent)
408 {
409    char clsName[128];
410    char wndName[128];
411    HWND child;
412
413    if (!IsWindow(hWnd))
414       hWnd = GetDesktopWindow();
415
416     if (!indent)  /* first time around */
417        DEBUG_Printf(DBG_CHN_MESG,  
418                     "%-16.16s %-17.17s %-8.8s %s\n",
419                     "hwnd", "Class Name", " Style", " WndProc Text");
420
421     do {
422        if (!GetClassName(hWnd, clsName, sizeof(clsName)))
423           strcpy(clsName, "-- Unknown --");
424        if (!GetWindowText(hWnd, wndName, sizeof(wndName)))
425           strcpy(wndName, "-- Empty --");
426        
427        /* FIXME: missing hmemTaskQ */
428        DEBUG_Printf(DBG_CHN_MESG, "%*s%04x%*s", indent, "", (UINT)hWnd, 13-indent,"");
429        DEBUG_Printf(DBG_CHN_MESG, "%-17.17s %08lx %08lx %.14s\n",
430                     clsName, GetWindowLong(hWnd, GWL_STYLE),
431                     GetWindowLong(hWnd, GWL_WNDPROC), wndName);
432
433        if ((child = GetWindow(hWnd, GW_CHILD)) != 0)
434           DEBUG_WalkWindows(child, indent + 1 );
435     } while ((hWnd = GetWindow(hWnd, GW_HWNDNEXT)) != 0);
436 }
437
438 void DEBUG_WalkProcess(void)
439 {
440     HANDLE snap = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
441     if (snap != INVALID_HANDLE_VALUE)
442     {
443         PROCESSENTRY32 entry;
444         DWORD current = DEBUG_CurrProcess ? DEBUG_CurrProcess->pid : 0;
445         BOOL ok;
446
447                   entry.dwSize = sizeof(entry);
448                   ok = Process32First( snap, &entry );
449
450         DEBUG_Printf(DBG_CHN_MESG, "%-8.8s %-8.8s %-8.8s %s\n",
451                      "pid", "threads", "parent", "exe" );
452         while (ok)
453         {
454             if (entry.th32ProcessID != GetCurrentProcessId())
455                 DEBUG_Printf(DBG_CHN_MESG, "%08lx %8ld %08lx '%s'%s\n",
456                              entry.th32ProcessID, entry.cntThreads,
457                              entry.th32ParentProcessID, entry.szExeFile,
458                              (entry.th32ProcessID == current) ? " <==" : "" );
459             ok = Process32Next( snap, &entry );
460         }
461         CloseHandle( snap );
462     }
463 }
464
465 void DEBUG_WalkThreads(void)
466 {
467     HANDLE snap = CreateToolhelp32Snapshot( TH32CS_SNAPTHREAD, 0 );
468     if (snap != INVALID_HANDLE_VALUE)
469     {
470         THREADENTRY32 entry;
471         DWORD current = DEBUG_CurrThread ? DEBUG_CurrThread->tid : 0;
472         BOOL ok;
473
474                   entry.dwSize = sizeof(entry);
475                   ok = Thread32First( snap, &entry );
476
477         DEBUG_Printf(DBG_CHN_MESG, "%-8.8s %-8.8s %s\n",
478                      "tid", "process", "prio" );
479         while (ok)
480         {
481             if (entry.th32OwnerProcessID != GetCurrentProcessId())
482                 DEBUG_Printf(DBG_CHN_MESG, "%08lx %08lx %4ld%s\n",
483                              entry.th32ThreadID, entry.th32OwnerProcessID,
484                              entry.tpBasePri, (entry.th32ThreadID == current) ? " <==" : "" );
485             ok = Thread32Next( snap, &entry );
486         }
487         CloseHandle( snap );
488     }
489 }
490
491 void DEBUG_WalkModref(DWORD p)
492 {
493    DEBUG_Printf(DBG_CHN_MESG, "No longer walking module references list\n");
494 }
495
496 void DEBUG_InfoSegments(DWORD start, int length)
497 {
498     char        flags[3];
499     DWORD       i;
500     LDT_ENTRY   le;
501
502     if (length == -1) length = (8192 - start);
503
504     for (i = start; i < start + length; i++)
505     {
506        if (!GetThreadSelectorEntry(DEBUG_CurrThread->handle, (i << 3)|7, &le))
507           continue;
508
509         if (le.HighWord.Bits.Type & 0x08) 
510         {
511             flags[0] = (le.HighWord.Bits.Type & 0x2) ? 'r' : '-';
512             flags[1] = '-';
513             flags[2] = 'x';
514         }
515         else
516         {
517             flags[0] = 'r';
518             flags[1] = (le.HighWord.Bits.Type & 0x2) ? 'w' : '-';
519             flags[2] = '-';
520         }
521         DEBUG_Printf(DBG_CHN_MESG, 
522                      "%04lx: sel=%04lx base=%08x limit=%08x %d-bit %c%c%c\n",
523                      i, (i<<3)|7, 
524                      (le.HighWord.Bits.BaseHi << 24) + 
525                      (le.HighWord.Bits.BaseMid << 16) + le.BaseLow,
526                      ((le.HighWord.Bits.LimitHi << 8) + le.LimitLow) << 
527                      (le.HighWord.Bits.Granularity ? 12 : 0),
528                      le.HighWord.Bits.Default_Big ? 32 : 16,
529                      flags[0], flags[1], flags[2] );
530     }
531 }
532
533 void DEBUG_InfoVirtual(void)
534 {
535    DEBUG_Printf(DBG_CHN_MESG, "No longer providing virtual mapping information\n");
536 }