info.c 33 KB

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