info.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /*
  2. * Wine debugger utility routines
  3. *
  4. * Copyright 1993 Eric Youngdale
  5. * Copyright 1995 Alexandre Julliard
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdarg.h>
  25. #include "debugger.h"
  26. #include "wingdi.h"
  27. #include "winuser.h"
  28. #include "tlhelp32.h"
  29. #include "wine/debug.h"
  30. WINE_DEFAULT_DEBUG_CHANNEL(winedbg);
  31. /***********************************************************************
  32. * print_help
  33. *
  34. * Implementation of the 'help' command.
  35. */
  36. void print_help(void)
  37. {
  38. int i = 0;
  39. static const char * const helptext[] =
  40. {
  41. "The commands accepted by the Wine debugger are a reasonable",
  42. "subset of the commands that gdb accepts.",
  43. "The commands currently are:",
  44. " help quit",
  45. " attach <wpid> detach",
  46. " break [*<addr>] watch | rwatch *<addr>",
  47. " delete break bpnum disable bpnum",
  48. " enable bpnum condition <bpnum> [<expr>]",
  49. " finish cont [N]",
  50. " step [N] next [N]",
  51. " stepi [N] nexti [N]",
  52. " x <addr> print <expr>",
  53. " display <expr> undisplay <disnum>",
  54. " local display <expr> delete display <disnum>",
  55. " enable display <disnum> disable display <disnum>",
  56. " bt [<tid>|all] frame <n>",
  57. " up down",
  58. " list <lines> disassemble [<addr>][,<addr>]",
  59. " show dir dir <path>",
  60. " set <reg> = <expr> set *<addr> = <expr>",
  61. " pass whatis",
  62. " info (see 'help info' for options) thread <tid>",
  63. "The 'x' command accepts repeat counts and formats (including 'i') in the",
  64. "same way that gdb does.\n",
  65. "The following are examples of legal expressions:",
  66. " $eax $eax+0x3 0x1000 ($eip + 256) *$eax *($esp + 3)",
  67. " Also, a nm format symbol table can be read from a file using the",
  68. " symbolfile command.", /* Symbols can also be defined individually with",
  69. " the define command.", */
  70. "",
  71. NULL
  72. };
  73. while (helptext[i]) dbg_printf("%s\n", helptext[i++]);
  74. }
  75. /***********************************************************************
  76. * info_help
  77. *
  78. * Implementation of the 'help info' command.
  79. */
  80. void info_help(void)
  81. {
  82. int i = 0;
  83. static const char * const infotext[] =
  84. {
  85. "The info commands allow you to get assorted bits of interesting stuff",
  86. "to be displayed. The options are:",
  87. " info break Displays information about breakpoints",
  88. " info class <name> Displays information about window class <name>",
  89. " info display Shows auto-display expressions in use",
  90. " info except <pid> Shows exception handler chain (in a given process)",
  91. " info locals Displays values of all local vars for current frame",
  92. " info maps <pid> Shows virtual mappings (in a given process)",
  93. " info process Shows all running processes",
  94. " info reg Displays values of the general registers at top of stack",
  95. " info all-reg Displays the general and floating point registers",
  96. " info segments <pid> Displays information about all known segments",
  97. " info share Displays all loaded modules",
  98. " info share <addr> Displays internal module state",
  99. " info stack [<len>] Dumps information about top of stack, up to len words",
  100. " info symbol <sym> Displays information about a given symbol",
  101. " info thread Shows all running threads",
  102. " info wnd <handle> Displays internal window state",
  103. "",
  104. NULL
  105. };
  106. while (infotext[i]) dbg_printf("%s\n", infotext[i++]);
  107. }
  108. static const char* get_symtype_str(const IMAGEHLP_MODULE64* mi)
  109. {
  110. switch (mi->SymType)
  111. {
  112. default:
  113. case SymNone: return "--none--";
  114. case SymCoff: return "COFF";
  115. case SymCv: return "CodeView";
  116. case SymPdb: return "PDB";
  117. case SymExport: return "Export";
  118. case SymDeferred: return "Deferred";
  119. case SymSym: return "Sym";
  120. case SymDia:
  121. switch (mi->CVSig)
  122. {
  123. case 'S' | ('T' << 8) | ('A' << 16) | ('B' << 24):
  124. return "Stabs";
  125. case 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24):
  126. /* previous versions of dbghelp used to report this... */
  127. return "Dwarf";
  128. default:
  129. if ((mi->CVSig & 0x00FFFFFF) == ('D' | ('W' << 8) | ('F' << 16)))
  130. {
  131. static char tmp[64];
  132. DWORD versbit = mi->CVSig >> 24;
  133. strcpy(tmp, "Dwarf");
  134. if (versbit & 1) strcat(tmp, "-2");
  135. if (versbit & 2) strcat(tmp, "-3");
  136. if (versbit & 4) strcat(tmp, "-4");
  137. if (versbit & 8) strcat(tmp, "-5");
  138. return tmp;
  139. }
  140. return "DIA";
  141. }
  142. }
  143. }
  144. struct info_module
  145. {
  146. IMAGEHLP_MODULE64 mi;
  147. char name[64];
  148. };
  149. struct info_modules
  150. {
  151. struct info_module *modules;
  152. unsigned num_alloc;
  153. unsigned num_used;
  154. };
  155. static void module_print_info(const struct info_module *module, BOOL is_embedded)
  156. {
  157. dbg_printf("%*.*I64x-%*.*I64x\t%-16s%s\n",
  158. ADDRWIDTH, ADDRWIDTH, module->mi.BaseOfImage,
  159. ADDRWIDTH, ADDRWIDTH, module->mi.BaseOfImage + module->mi.ImageSize,
  160. is_embedded ? "\\" : get_symtype_str(&module->mi), module->name);
  161. }
  162. static int __cdecl module_compare(const void* p1, const void* p2)
  163. {
  164. struct info_module *left = (struct info_module *)p1;
  165. struct info_module *right = (struct info_module *)p2;
  166. LONGLONG val = left->mi.BaseOfImage - right->mi.BaseOfImage;
  167. if (val < 0) return -1;
  168. else if (val > 0) return 1;
  169. else return 0;
  170. }
  171. static inline BOOL module_is_container(const struct info_module *wmod_cntnr,
  172. const struct info_module *wmod_child)
  173. {
  174. return wmod_cntnr->mi.BaseOfImage <= wmod_child->mi.BaseOfImage &&
  175. wmod_cntnr->mi.BaseOfImage + wmod_cntnr->mi.ImageSize >=
  176. wmod_child->mi.BaseOfImage + wmod_child->mi.ImageSize;
  177. }
  178. static BOOL CALLBACK info_mod_cb(PCSTR mod_name, DWORD64 base, PVOID ctx)
  179. {
  180. struct info_modules *im = ctx;
  181. if (im->num_used + 1 > im->num_alloc)
  182. {
  183. im->num_alloc += 16;
  184. im->modules = dbg_heap_realloc(im->modules, im->num_alloc * sizeof(*im->modules));
  185. }
  186. im->modules[im->num_used].mi.SizeOfStruct = sizeof(im->modules[im->num_used].mi);
  187. if (SymGetModuleInfo64(dbg_curr_process->handle, base, &im->modules[im->num_used].mi))
  188. {
  189. const int dst_len = sizeof(im->modules[im->num_used].name);
  190. lstrcpynA(im->modules[im->num_used].name, mod_name, dst_len - 1);
  191. im->modules[im->num_used].name[dst_len - 1] = 0;
  192. im->num_used++;
  193. }
  194. return TRUE;
  195. }
  196. /***********************************************************************
  197. * info_win32_module
  198. *
  199. * Display information about a given module (DLL or EXE), or about all modules
  200. */
  201. void info_win32_module(DWORD64 base)
  202. {
  203. struct info_modules im;
  204. UINT i, j, num_printed = 0;
  205. BOOL opt;
  206. if (!dbg_curr_process)
  207. {
  208. dbg_printf("Cannot get info on module while no process is loaded\n");
  209. return;
  210. }
  211. im.modules = NULL;
  212. im.num_alloc = im.num_used = 0;
  213. /* this is a wine specific options to return also ELF modules in the
  214. * enumeration
  215. */
  216. opt = SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, TRUE);
  217. SymEnumerateModules64(dbg_curr_process->handle, info_mod_cb, &im);
  218. SymSetExtendedOption(SYMOPT_EX_WINE_NATIVE_MODULES, opt);
  219. qsort(im.modules, im.num_used, sizeof(im.modules[0]), module_compare);
  220. dbg_printf("Module\tAddress\t\t\t%sDebug info\tName (%d modules)\n",
  221. ADDRWIDTH == 16 ? "\t\t" : "", im.num_used);
  222. for (i = 0; i < im.num_used; i++)
  223. {
  224. if (base &&
  225. (base < im.modules[i].mi.BaseOfImage || base >= im.modules[i].mi.BaseOfImage + im.modules[i].mi.ImageSize))
  226. continue;
  227. if (strstr(im.modules[i].name, "<elf>"))
  228. {
  229. dbg_printf("ELF\t");
  230. module_print_info(&im.modules[i], FALSE);
  231. /* print all modules embedded in this one */
  232. for (j = 0; j < im.num_used; j++)
  233. {
  234. if (!strstr(im.modules[j].name, "<elf>") && module_is_container(&im.modules[i], &im.modules[j]))
  235. {
  236. dbg_printf(" \\-PE\t");
  237. module_print_info(&im.modules[j], TRUE);
  238. }
  239. }
  240. }
  241. else
  242. {
  243. /* check module is not embedded in another module */
  244. for (j = 0; j < im.num_used; j++)
  245. {
  246. if (strstr(im.modules[j].name, "<elf>") && module_is_container(&im.modules[j], &im.modules[i]))
  247. break;
  248. }
  249. if (j < im.num_used) continue;
  250. if (strstr(im.modules[i].name, ".so") || strchr(im.modules[i].name, '<'))
  251. dbg_printf("ELF\t");
  252. else
  253. dbg_printf("PE\t");
  254. module_print_info(&im.modules[i], FALSE);
  255. }
  256. num_printed++;
  257. }
  258. HeapFree(GetProcessHeap(), 0, im.modules);
  259. if (base && !num_printed)
  260. dbg_printf("'0x%0*I64x' is not a valid module address\n", ADDRWIDTH, base);
  261. }
  262. struct class_walker
  263. {
  264. ATOM* table;
  265. int used;
  266. int alloc;
  267. };
  268. static void class_walker(HWND hWnd, struct class_walker* cw)
  269. {
  270. char clsName[128];
  271. int i;
  272. ATOM atom;
  273. HWND child;
  274. if (!GetClassNameA(hWnd, clsName, sizeof(clsName)))
  275. return;
  276. if ((atom = FindAtomA(clsName)) == 0)
  277. return;
  278. for (i = 0; i < cw->used; i++)
  279. {
  280. if (cw->table[i] == atom)
  281. break;
  282. }
  283. if (i == cw->used)
  284. {
  285. if (cw->used >= cw->alloc)
  286. {
  287. cw->alloc += 16;
  288. cw->table = dbg_heap_realloc(cw->table, cw->alloc * sizeof(ATOM));
  289. }
  290. cw->table[cw->used++] = atom;
  291. info_win32_class(hWnd, clsName);
  292. }
  293. do
  294. {
  295. if ((child = GetWindow(hWnd, GW_CHILD)) != 0)
  296. class_walker(child, cw);
  297. } while ((hWnd = GetWindow(hWnd, GW_HWNDNEXT)) != 0);
  298. }
  299. void info_win32_class(HWND hWnd, const char* name)
  300. {
  301. WNDCLASSEXA wca;
  302. HINSTANCE hInst = hWnd ? (HINSTANCE)GetWindowLongPtrW(hWnd, GWLP_HINSTANCE) : 0;
  303. if (!name)
  304. {
  305. struct class_walker cw;
  306. cw.table = NULL;
  307. cw.used = cw.alloc = 0;
  308. class_walker(GetDesktopWindow(), &cw);
  309. HeapFree(GetProcessHeap(), 0, cw.table);
  310. return;
  311. }
  312. if (!GetClassInfoExA(hInst, name, &wca))
  313. {
  314. dbg_printf("Cannot find class '%s'\n", name);
  315. return;
  316. }
  317. dbg_printf("Class '%s':\n", name);
  318. dbg_printf("style=0x%08x wndProc=%p\n"
  319. "inst=%p icon=%p cursor=%p bkgnd=%p\n"
  320. "clsExtra=%d winExtra=%d\n",
  321. wca.style, wca.lpfnWndProc, wca.hInstance,
  322. wca.hIcon, wca.hCursor, wca.hbrBackground,
  323. wca.cbClsExtra, wca.cbWndExtra);
  324. if (hWnd && wca.cbClsExtra)
  325. {
  326. int i;
  327. WORD w;
  328. dbg_printf("Extra bytes:");
  329. for (i = 0; i < wca.cbClsExtra / 2; i++)
  330. {
  331. w = GetClassWord(hWnd, i * 2);
  332. /* FIXME: depends on i386 endian-ity */
  333. dbg_printf(" %02x %02x", HIBYTE(w), LOBYTE(w));
  334. }
  335. dbg_printf("\n");
  336. }
  337. dbg_printf("\n");
  338. /* FIXME:
  339. * + print #windows (or even list of windows...)
  340. * + print extra bytes => this requires a window handle on this very class...
  341. */
  342. }
  343. static void info_window(HWND hWnd, int indent)
  344. {
  345. char clsName[128];
  346. char wndName[128];
  347. HWND child;
  348. do
  349. {
  350. if (!GetClassNameA(hWnd, clsName, sizeof(clsName)))
  351. strcpy(clsName, "-- Unknown --");
  352. if (!GetWindowTextA(hWnd, wndName, sizeof(wndName)))
  353. strcpy(wndName, "-- Empty --");
  354. dbg_printf("%*s%08Ix%*s %-17.17s %08lx %0*Ix %08lx %.14s\n",
  355. indent, "", (DWORD_PTR)hWnd, 12 - indent, "",
  356. clsName, GetWindowLongW(hWnd, GWL_STYLE),
  357. ADDRWIDTH, (ULONG_PTR)GetWindowLongPtrW(hWnd, GWLP_WNDPROC),
  358. GetWindowThreadProcessId(hWnd, NULL), wndName);
  359. if ((child = GetWindow(hWnd, GW_CHILD)) != 0)
  360. info_window(child, indent + 1);
  361. } while ((hWnd = GetWindow(hWnd, GW_HWNDNEXT)) != 0);
  362. }
  363. void info_win32_window(HWND hWnd, BOOL detailed)
  364. {
  365. char clsName[128];
  366. char wndName[128];
  367. RECT clientRect;
  368. RECT windowRect;
  369. WORD w;
  370. if (!IsWindow(hWnd)) hWnd = GetDesktopWindow();
  371. if (!detailed)
  372. {
  373. dbg_printf("%-20.20s %-17.17s %-8.8s %-*.*s %-8.8s %s\n",
  374. "Window handle", "Class Name", "Style",
  375. ADDRWIDTH, ADDRWIDTH, "WndProc", "Thread", "Text");
  376. info_window(hWnd, 0);
  377. return;
  378. }
  379. if (!GetClassNameA(hWnd, clsName, sizeof(clsName)))
  380. strcpy(clsName, "-- Unknown --");
  381. if (!GetWindowTextA(hWnd, wndName, sizeof(wndName)))
  382. strcpy(wndName, "-- Empty --");
  383. if (!GetClientRect(hWnd, &clientRect) ||
  384. !MapWindowPoints(hWnd, 0, (LPPOINT) &clientRect, 2))
  385. SetRectEmpty(&clientRect);
  386. if (!GetWindowRect(hWnd, &windowRect))
  387. SetRectEmpty(&windowRect);
  388. /* FIXME missing fields: hmemTaskQ, hrgnUpdate, dce, flags, pProp, scroll */
  389. dbg_printf("next=%p child=%p parent=%p owner=%p class='%s'\n"
  390. "inst=%p active=%p idmenu=%08Ix\n"
  391. "style=0x%08lx exstyle=0x%08lx wndproc=%p text='%s'\n"
  392. "client=%ld,%ld-%ld,%ld window=%ld,%ld-%ld,%ld sysmenu=%p\n",
  393. GetWindow(hWnd, GW_HWNDNEXT),
  394. GetWindow(hWnd, GW_CHILD),
  395. GetParent(hWnd),
  396. GetWindow(hWnd, GW_OWNER),
  397. clsName,
  398. (HINSTANCE)GetWindowLongPtrW(hWnd, GWLP_HINSTANCE),
  399. GetLastActivePopup(hWnd),
  400. (ULONG_PTR)GetWindowLongPtrW(hWnd, GWLP_ID),
  401. GetWindowLongW(hWnd, GWL_STYLE),
  402. GetWindowLongW(hWnd, GWL_EXSTYLE),
  403. (void*)GetWindowLongPtrW(hWnd, GWLP_WNDPROC),
  404. wndName,
  405. clientRect.left, clientRect.top, clientRect.right, clientRect.bottom,
  406. windowRect.left, windowRect.top, windowRect.right, windowRect.bottom,
  407. GetSystemMenu(hWnd, FALSE));
  408. if (GetClassLongW(hWnd, GCL_CBWNDEXTRA))
  409. {
  410. UINT i;
  411. dbg_printf("Extra bytes:");
  412. for (i = 0; i < GetClassLongW(hWnd, GCL_CBWNDEXTRA) / 2; i++)
  413. {
  414. w = GetWindowWord(hWnd, i * 2);
  415. /* FIXME: depends on i386 endian-ity */
  416. dbg_printf(" %02x %02x", HIBYTE(w), LOBYTE(w));
  417. }
  418. dbg_printf("\n");
  419. }
  420. dbg_printf("\n");
  421. }
  422. struct dump_proc_entry
  423. {
  424. PROCESSENTRY32 proc;
  425. unsigned children; /* index in dump_proc.entries of first child */
  426. unsigned sibling; /* index in dump_proc.entries of next sibling */
  427. };
  428. struct dump_proc
  429. {
  430. struct dump_proc_entry*entries;
  431. unsigned count;
  432. unsigned alloc;
  433. };
  434. static unsigned get_parent(const struct dump_proc* dp, unsigned idx)
  435. {
  436. unsigned i;
  437. for (i = 0; i < dp->count; i++)
  438. {
  439. if (i != idx && dp->entries[i].proc.th32ProcessID == dp->entries[idx].proc.th32ParentProcessID)
  440. return i;
  441. }
  442. return -1;
  443. }
  444. static void dump_proc_info(const struct dump_proc* dp, unsigned idx, unsigned depth)
  445. {
  446. struct dump_proc_entry* dpe;
  447. for ( ; idx != -1; idx = dp->entries[idx].sibling)
  448. {
  449. assert(idx < dp->count);
  450. dpe = &dp->entries[idx];
  451. dbg_printf("%c%08lx %-8ld ",
  452. (dpe->proc.th32ProcessID == (dbg_curr_process ?
  453. dbg_curr_process->pid : 0)) ? '>' : ' ',
  454. dpe->proc.th32ProcessID, dpe->proc.cntThreads);
  455. if (depth)
  456. {
  457. unsigned i;
  458. for (i = 3 * (depth - 1); i > 0; i--) dbg_printf(" ");
  459. dbg_printf("\\_ ");
  460. }
  461. dbg_printf("'%s'\n", dpe->proc.szExeFile);
  462. dump_proc_info(dp, dpe->children, depth + 1);
  463. }
  464. }
  465. void info_win32_processes(void)
  466. {
  467. HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  468. if (snap != INVALID_HANDLE_VALUE)
  469. {
  470. struct dump_proc dp;
  471. unsigned i, first = -1;
  472. BOOL ok;
  473. dp.count = 0;
  474. dp.alloc = 16;
  475. dp.entries = HeapAlloc(GetProcessHeap(), 0, sizeof(*dp.entries) * dp.alloc);
  476. if (!dp.entries)
  477. {
  478. CloseHandle(snap);
  479. return;
  480. }
  481. dp.entries[dp.count].proc.dwSize = sizeof(dp.entries[dp.count].proc);
  482. ok = Process32First(snap, &dp.entries[dp.count].proc);
  483. /* fetch all process information into dp (skipping this debugger) */
  484. while (ok)
  485. {
  486. if (dp.entries[dp.count].proc.th32ProcessID != GetCurrentProcessId())
  487. dp.entries[dp.count++].children = -1;
  488. if (dp.count >= dp.alloc)
  489. {
  490. dp.entries = HeapReAlloc(GetProcessHeap(), 0, dp.entries, sizeof(*dp.entries) * (dp.alloc *= 2));
  491. if (!dp.entries) return;
  492. }
  493. dp.entries[dp.count].proc.dwSize = sizeof(dp.entries[dp.count].proc);
  494. ok = Process32Next(snap, &dp.entries[dp.count].proc);
  495. }
  496. CloseHandle(snap);
  497. /* chain the siblings wrt. their parent */
  498. for (i = 0; i < dp.count; i++)
  499. {
  500. unsigned parent = get_parent(&dp, i);
  501. unsigned *chain = parent == -1 ? &first : &dp.entries[parent].children;
  502. dp.entries[i].sibling = *chain;
  503. *chain = i;
  504. }
  505. dbg_printf(" %-8.8s %-8.8s %s (all id:s are in hex)\n", "pid", "threads", "executable");
  506. dump_proc_info(&dp, first, 0);
  507. HeapFree(GetProcessHeap(), 0, dp.entries);
  508. }
  509. }
  510. static BOOL get_process_name(DWORD pid, PROCESSENTRY32W* entry)
  511. {
  512. BOOL ret = FALSE;
  513. HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  514. if (snap != INVALID_HANDLE_VALUE)
  515. {
  516. entry->dwSize = sizeof(*entry);
  517. if (Process32FirstW(snap, entry))
  518. while (!(ret = (entry->th32ProcessID == pid)) &&
  519. Process32NextW(snap, entry));
  520. CloseHandle(snap);
  521. }
  522. return ret;
  523. }
  524. void info_win32_threads(void)
  525. {
  526. HANDLE snap = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, 0);
  527. if (snap != INVALID_HANDLE_VALUE)
  528. {
  529. THREADENTRY32 entry;
  530. BOOL ok;
  531. DWORD lastProcessId = 0;
  532. struct dbg_process* p = NULL;
  533. struct dbg_thread* t = NULL;
  534. entry.dwSize = sizeof(entry);
  535. ok = Thread32First(snap, &entry);
  536. dbg_printf("%-8.8s %-8.8s %s %s (all IDs are in hex)\n",
  537. "process", "tid", "prio", "name");
  538. while (ok)
  539. {
  540. if (entry.th32OwnerProcessID != GetCurrentProcessId())
  541. {
  542. /* FIXME: this assumes that, in the snapshot, all threads of a same process are
  543. * listed sequentially, which is not specified in the doc (Wine's implementation
  544. * does it)
  545. */
  546. if (entry.th32OwnerProcessID != lastProcessId)
  547. {
  548. PROCESSENTRY32W pcs_entry;
  549. const WCHAR* exename;
  550. p = dbg_get_process(entry.th32OwnerProcessID);
  551. if (p)
  552. exename = p->imageName;
  553. else if (get_process_name(entry.th32OwnerProcessID, &pcs_entry))
  554. exename = pcs_entry.szExeFile;
  555. else
  556. exename = L"";
  557. dbg_printf("%08lx%s %ls\n",
  558. entry.th32OwnerProcessID, p ? " (D)" : "", exename);
  559. lastProcessId = entry.th32OwnerProcessID;
  560. }
  561. t = dbg_get_thread(p, entry.th32ThreadID);
  562. dbg_printf("\t%08lx %4ld%s %s\n",
  563. entry.th32ThreadID, entry.tpBasePri,
  564. (entry.th32ThreadID == dbg_curr_tid) ? " <==" : " ",
  565. t ? t->name : "");
  566. }
  567. ok = Thread32Next(snap, &entry);
  568. }
  569. CloseHandle(snap);
  570. }
  571. }
  572. /***********************************************************************
  573. * info_win32_frame_exceptions
  574. *
  575. * Get info on the exception frames of a given thread.
  576. */
  577. void info_win32_frame_exceptions(DWORD tid)
  578. {
  579. struct dbg_thread* thread;
  580. void* next_frame;
  581. if (!dbg_curr_process || !dbg_curr_thread)
  582. {
  583. dbg_printf("Cannot get info on exceptions while no process is loaded\n");
  584. return;
  585. }
  586. dbg_printf("Exception frames:\n");
  587. if (tid == dbg_curr_tid) thread = dbg_curr_thread;
  588. else
  589. {
  590. thread = dbg_get_thread(dbg_curr_process, tid);
  591. if (!thread)
  592. {
  593. dbg_printf("Unknown thread id (%04lx) in current process\n", tid);
  594. return;
  595. }
  596. if (SuspendThread(thread->handle) == -1)
  597. {
  598. dbg_printf("Can't suspend thread id (%04lx)\n", tid);
  599. return;
  600. }
  601. }
  602. if (!dbg_read_memory(thread->teb, &next_frame, sizeof(next_frame)))
  603. {
  604. dbg_printf("Can't read TEB:except_frame\n");
  605. return;
  606. }
  607. while (next_frame != (void*)-1)
  608. {
  609. EXCEPTION_REGISTRATION_RECORD frame;
  610. dbg_printf("%p: ", next_frame);
  611. if (!dbg_read_memory(next_frame, &frame, sizeof(frame)))
  612. {
  613. dbg_printf("Invalid frame address\n");
  614. break;
  615. }
  616. dbg_printf("prev=%p handler=%p\n", frame.Prev, frame.Handler);
  617. next_frame = frame.Prev;
  618. }
  619. if (tid != dbg_curr_tid) ResumeThread(thread->handle);
  620. }
  621. void info_win32_segments(DWORD start, int length)
  622. {
  623. char flags[3];
  624. DWORD i;
  625. LDT_ENTRY le;
  626. if (length == -1) length = (8192 - start);
  627. for (i = start; i < start + length; i++)
  628. {
  629. if (!dbg_curr_process->process_io->get_selector(dbg_curr_thread->handle, (i << 3) | 7, &le))
  630. continue;
  631. if (le.HighWord.Bits.Type & 0x08)
  632. {
  633. flags[0] = (le.HighWord.Bits.Type & 0x2) ? 'r' : '-';
  634. flags[1] = '-';
  635. flags[2] = 'x';
  636. }
  637. else
  638. {
  639. flags[0] = 'r';
  640. flags[1] = (le.HighWord.Bits.Type & 0x2) ? 'w' : '-';
  641. flags[2] = '-';
  642. }
  643. dbg_printf("%04lx: sel=%04lx base=%08x limit=%08x %d-bit %c%c%c\n",
  644. i, (i << 3) | 7,
  645. (le.HighWord.Bits.BaseHi << 24) +
  646. (le.HighWord.Bits.BaseMid << 16) + le.BaseLow,
  647. ((le.HighWord.Bits.LimitHi << 8) + le.LimitLow) <<
  648. (le.HighWord.Bits.Granularity ? 12 : 0),
  649. le.HighWord.Bits.Default_Big ? 32 : 16,
  650. flags[0], flags[1], flags[2]);
  651. }
  652. }
  653. void info_win32_virtual(DWORD pid)
  654. {
  655. MEMORY_BASIC_INFORMATION mbi;
  656. char* addr = 0;
  657. const char* state;
  658. const char* type;
  659. char prot[3+1];
  660. HANDLE hProc;
  661. if (pid == dbg_curr_pid)
  662. {
  663. if (dbg_curr_process == NULL)
  664. {
  665. dbg_printf("Cannot look at mapping of current process, while no process is loaded\n");
  666. return;
  667. }
  668. hProc = dbg_curr_process->handle;
  669. }
  670. else
  671. {
  672. hProc = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
  673. if (hProc == NULL)
  674. {
  675. dbg_printf("Cannot open process <%04lx>\n", pid);
  676. return;
  677. }
  678. }
  679. dbg_printf("Address End State Type RWX\n");
  680. while (VirtualQueryEx(hProc, addr, &mbi, sizeof(mbi)) >= sizeof(mbi))
  681. {
  682. switch (mbi.State)
  683. {
  684. case MEM_COMMIT: state = "commit "; break;
  685. case MEM_FREE: state = "free "; break;
  686. case MEM_RESERVE: state = "reserve"; break;
  687. default: state = "??? "; break;
  688. }
  689. if (mbi.State != MEM_FREE)
  690. {
  691. switch (mbi.Type)
  692. {
  693. case MEM_IMAGE: type = "image "; break;
  694. case MEM_MAPPED: type = "mapped "; break;
  695. case MEM_PRIVATE: type = "private"; break;
  696. case 0: type = " "; break;
  697. default: type = "??? "; break;
  698. }
  699. memset(prot, ' ' , sizeof(prot) - 1);
  700. prot[sizeof(prot) - 1] = '\0';
  701. if (mbi.AllocationProtect & (PAGE_READONLY|PAGE_READWRITE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_WRITECOPY|PAGE_EXECUTE_WRITECOPY))
  702. prot[0] = 'R';
  703. if (mbi.AllocationProtect & (PAGE_READWRITE|PAGE_EXECUTE_READWRITE))
  704. prot[1] = 'W';
  705. if (mbi.AllocationProtect & (PAGE_WRITECOPY|PAGE_EXECUTE_WRITECOPY))
  706. prot[1] = 'C';
  707. if (mbi.AllocationProtect & (PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY))
  708. prot[2] = 'X';
  709. }
  710. else
  711. {
  712. type = "";
  713. prot[0] = '\0';
  714. }
  715. dbg_printf("%0*Ix %0*Ix %s %s %s\n",
  716. ADDRWIDTH, (DWORD_PTR)addr, ADDRWIDTH, (DWORD_PTR)addr + mbi.RegionSize - 1, state, type, prot);
  717. if (addr + mbi.RegionSize < addr) /* wrap around ? */
  718. break;
  719. addr += mbi.RegionSize;
  720. }
  721. if (pid != dbg_curr_pid) CloseHandle(hProc);
  722. }
  723. void info_wine_dbg_channel(BOOL turn_on, const char* cls, const char* name)
  724. {
  725. struct dbg_lvalue lvalue;
  726. struct __wine_debug_channel channel;
  727. unsigned char mask;
  728. int done = 0;
  729. BOOL bAll;
  730. void* addr;
  731. if (!dbg_curr_process || !dbg_curr_thread)
  732. {
  733. dbg_printf("Cannot set/get debug channels while no process is loaded\n");
  734. return;
  735. }
  736. if (symbol_get_lvalue("debug_options", -1, &lvalue, FALSE) != sglv_found)
  737. {
  738. return;
  739. }
  740. addr = memory_to_linear_addr(&lvalue.addr);
  741. if (!cls) mask = ~0;
  742. else if (!strcmp(cls, "fixme")) mask = (1 << __WINE_DBCL_FIXME);
  743. else if (!strcmp(cls, "err")) mask = (1 << __WINE_DBCL_ERR);
  744. else if (!strcmp(cls, "warn")) mask = (1 << __WINE_DBCL_WARN);
  745. else if (!strcmp(cls, "trace")) mask = (1 << __WINE_DBCL_TRACE);
  746. else
  747. {
  748. dbg_printf("Unknown debug class %s\n", cls);
  749. return;
  750. }
  751. bAll = !strcmp("all", name);
  752. while (addr && dbg_read_memory(addr, &channel, sizeof(channel)))
  753. {
  754. if (!channel.name[0]) break;
  755. if (bAll || !strcmp( channel.name, name ))
  756. {
  757. if (turn_on) channel.flags |= mask;
  758. else channel.flags &= ~mask;
  759. if (dbg_write_memory(addr, &channel, sizeof(channel))) done++;
  760. }
  761. addr = (struct __wine_debug_channel *)addr + 1;
  762. }
  763. if (!done) dbg_printf("Unable to find debug channel %s\n", name);
  764. else WINE_TRACE("Changed %d channel instances\n", done);
  765. }
  766. void info_win32_exception(void)
  767. {
  768. const EXCEPTION_RECORD* rec;
  769. ADDRESS64 addr;
  770. char hexbuf[MAX_OFFSET_TO_STR_LEN];
  771. if (!dbg_curr_thread->in_exception)
  772. {
  773. dbg_printf("Thread isn't in an exception\n");
  774. return;
  775. }
  776. rec = &dbg_curr_thread->excpt_record;
  777. memory_get_current_pc(&addr);
  778. /* print some infos */
  779. dbg_printf("%s: ",
  780. dbg_curr_thread->first_chance ? "First chance exception" : "Unhandled exception");
  781. switch (rec->ExceptionCode)
  782. {
  783. case EXCEPTION_BREAKPOINT:
  784. dbg_printf("breakpoint");
  785. break;
  786. case EXCEPTION_SINGLE_STEP:
  787. dbg_printf("single step");
  788. break;
  789. case EXCEPTION_INT_DIVIDE_BY_ZERO:
  790. dbg_printf("divide by zero");
  791. break;
  792. case EXCEPTION_INT_OVERFLOW:
  793. dbg_printf("overflow");
  794. break;
  795. case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  796. dbg_printf("array bounds");
  797. break;
  798. case EXCEPTION_ILLEGAL_INSTRUCTION:
  799. dbg_printf("illegal instruction");
  800. break;
  801. case EXCEPTION_STACK_OVERFLOW:
  802. dbg_printf("stack overflow");
  803. break;
  804. case EXCEPTION_PRIV_INSTRUCTION:
  805. dbg_printf("privileged instruction");
  806. break;
  807. case EXCEPTION_ACCESS_VIOLATION:
  808. if (rec->NumberParameters == 2)
  809. dbg_printf("page fault on %s access to 0x%0*Ix",
  810. rec->ExceptionInformation[0] == EXCEPTION_WRITE_FAULT ? "write" :
  811. rec->ExceptionInformation[0] == EXCEPTION_EXECUTE_FAULT ? "execute" : "read",
  812. ADDRWIDTH, rec->ExceptionInformation[1]);
  813. else
  814. dbg_printf("page fault");
  815. break;
  816. case EXCEPTION_DATATYPE_MISALIGNMENT:
  817. dbg_printf("Alignment");
  818. break;
  819. case DBG_CONTROL_C:
  820. dbg_printf("^C");
  821. break;
  822. case CONTROL_C_EXIT:
  823. dbg_printf("^C");
  824. break;
  825. case STATUS_POSSIBLE_DEADLOCK:
  826. {
  827. ADDRESS64 recaddr;
  828. recaddr.Mode = AddrModeFlat;
  829. recaddr.Offset = rec->ExceptionInformation[0];
  830. dbg_printf("wait failed on critical section ");
  831. print_address(&recaddr, FALSE);
  832. }
  833. break;
  834. case EXCEPTION_WINE_STUB:
  835. {
  836. char dll[64], name[256];
  837. memory_get_string(dbg_curr_process,
  838. (void*)rec->ExceptionInformation[0], TRUE, FALSE,
  839. dll, sizeof(dll));
  840. if (HIWORD(rec->ExceptionInformation[1]))
  841. memory_get_string(dbg_curr_process,
  842. (void*)rec->ExceptionInformation[1], TRUE, FALSE,
  843. name, sizeof(name));
  844. else
  845. sprintf( name, "%Id", rec->ExceptionInformation[1] );
  846. dbg_printf("unimplemented function %s.%s called", dll, name);
  847. }
  848. break;
  849. case EXCEPTION_WINE_ASSERTION:
  850. dbg_printf("assertion failed");
  851. break;
  852. case EXCEPTION_FLT_DENORMAL_OPERAND:
  853. dbg_printf("denormal float operand");
  854. break;
  855. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  856. dbg_printf("divide by zero");
  857. break;
  858. case EXCEPTION_FLT_INEXACT_RESULT:
  859. dbg_printf("inexact float result");
  860. break;
  861. case EXCEPTION_FLT_INVALID_OPERATION:
  862. dbg_printf("invalid float operation");
  863. break;
  864. case EXCEPTION_FLT_OVERFLOW:
  865. dbg_printf("floating point overflow");
  866. break;
  867. case EXCEPTION_FLT_UNDERFLOW:
  868. dbg_printf("floating point underflow");
  869. break;
  870. case EXCEPTION_FLT_STACK_CHECK:
  871. dbg_printf("floating point stack check");
  872. break;
  873. case EXCEPTION_WINE_CXX_EXCEPTION:
  874. if(rec->NumberParameters == 3 && rec->ExceptionInformation[0] == EXCEPTION_WINE_CXX_FRAME_MAGIC)
  875. dbg_printf("C++ exception(object = 0x%0*Ix, type = 0x%0*Ix)",
  876. ADDRWIDTH, rec->ExceptionInformation[1], ADDRWIDTH, rec->ExceptionInformation[2]);
  877. else if(rec->NumberParameters == 4 && rec->ExceptionInformation[0] == EXCEPTION_WINE_CXX_FRAME_MAGIC)
  878. dbg_printf("C++ exception(object = %p, type = %p, base = %p)",
  879. (void*)rec->ExceptionInformation[1], (void*)rec->ExceptionInformation[2],
  880. (void*)rec->ExceptionInformation[3]);
  881. else
  882. dbg_printf("C++ exception with strange parameter count %ld or magic 0x%0*Ix",
  883. rec->NumberParameters, ADDRWIDTH, rec->ExceptionInformation[0]);
  884. break;
  885. default:
  886. dbg_printf("0x%08lx", rec->ExceptionCode);
  887. break;
  888. }
  889. if (rec->ExceptionFlags & EH_STACK_INVALID)
  890. dbg_printf(", invalid program stack");
  891. switch (addr.Mode)
  892. {
  893. case AddrModeFlat:
  894. dbg_printf(" in %ld-bit code (%s)",
  895. dbg_curr_process->be_cpu->pointer_size * 8,
  896. memory_offset_to_string(hexbuf, addr.Offset, 0));
  897. break;
  898. case AddrModeReal:
  899. dbg_printf(" in vm86 code (%04x:%04x)", addr.Segment, (unsigned) addr.Offset);
  900. break;
  901. case AddrMode1616:
  902. dbg_printf(" in 16-bit code (%04x:%04x)", addr.Segment, (unsigned) addr.Offset);
  903. break;
  904. case AddrMode1632:
  905. dbg_printf(" in segmented 32-bit code (%04x:%08x)", addr.Segment, (unsigned) addr.Offset);
  906. break;
  907. default: dbg_printf(" bad address");
  908. }
  909. dbg_printf(".\n");
  910. }