dbgchnl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * ReactOS Task Manager
  3. *
  4. * dbgchnl.c
  5. *
  6. * Copyright (C) 2003 - 2004 Eric Pouech
  7. * Copyright (C) 2008 Vladimir Pankratov
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  22. */
  23. #include <ctype.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <windows.h>
  27. #include <commctrl.h>
  28. #include <winnt.h>
  29. #include <dbghelp.h>
  30. #include "taskmgr.h"
  31. #include "perfdata.h"
  32. #include "column.h"
  33. #include "wine/debug.h"
  34. /* TODO:
  35. * - the dialog box could be non modal
  36. * - in that case,
  37. * + could refresh channels from time to time
  38. * + set the name of exec (and perhaps its pid) in dialog title
  39. * - get a better UI (replace the 'x' by real tick boxes in list view)
  40. * - enhance visual feedback: the list is large, and it's hard to get the
  41. * right line when clicking on rightmost column (trace for example)
  42. * - get rid of printfs (error reporting) and use real message boxes
  43. * - include the column width settings in the full column management scheme
  44. * - use more global settings (like having a temporary on/off
  45. * setting for a fixme:s or err:s
  46. */
  47. static DWORD (WINAPI *pSymSetOptions)(DWORD);
  48. static BOOL (WINAPI *pSymInitialize)(HANDLE, PSTR, BOOL);
  49. static DWORD (WINAPI *pSymLoadModule)(HANDLE, HANDLE, PCSTR, PCSTR, DWORD, DWORD);
  50. static BOOL (WINAPI *pSymCleanup)(HANDLE);
  51. static BOOL (WINAPI *pSymFromName)(HANDLE, PCSTR, PSYMBOL_INFO);
  52. BOOL AreDebugChannelsSupported(void)
  53. {
  54. static HANDLE hDbgHelp /* = NULL */;
  55. static const WCHAR wszDbgHelp[] = {'D','B','G','H','E','L','P','.','D','L','L',0};
  56. if (hDbgHelp) return TRUE;
  57. if (!(hDbgHelp = LoadLibraryW(wszDbgHelp))) return FALSE;
  58. pSymSetOptions = (void*)GetProcAddress(hDbgHelp, "SymSetOptions");
  59. pSymInitialize = (void*)GetProcAddress(hDbgHelp, "SymInitialize");
  60. pSymLoadModule = (void*)GetProcAddress(hDbgHelp, "SymLoadModule");
  61. pSymFromName = (void*)GetProcAddress(hDbgHelp, "SymFromName");
  62. pSymCleanup = (void*)GetProcAddress(hDbgHelp, "SymCleanup");
  63. if (!pSymSetOptions || !pSymInitialize || !pSymLoadModule || !pSymCleanup || !pSymFromName)
  64. {
  65. FreeLibrary(hDbgHelp);
  66. hDbgHelp = NULL;
  67. return FALSE;
  68. }
  69. return TRUE;
  70. }
  71. static DWORD get_selected_pid(void)
  72. {
  73. LVITEMW lvitem;
  74. ULONG Index, Count;
  75. DWORD dwProcessId;
  76. Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0);
  77. for (Index = 0; Index < Count; Index++)
  78. {
  79. lvitem.mask = LVIF_STATE;
  80. lvitem.stateMask = LVIS_SELECTED;
  81. lvitem.iItem = Index;
  82. lvitem.iSubItem = 0;
  83. SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
  84. if (lvitem.state & LVIS_SELECTED)
  85. break;
  86. }
  87. Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0);
  88. dwProcessId = PerfDataGetProcessId(Index);
  89. if ((Count != 1) || (dwProcessId == 0))
  90. return 0;
  91. return dwProcessId;
  92. }
  93. static int list_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel* channel, void* user)
  94. {
  95. int j;
  96. WCHAR nameW[sizeof(channel->name)], val[2];
  97. LVITEMW lvitem;
  98. int index;
  99. HWND hChannelLV = user;
  100. MultiByteToWideChar(CP_ACP, 0, channel->name, sizeof(channel->name), nameW, ARRAY_SIZE(nameW));
  101. lvitem.mask = LVIF_TEXT;
  102. lvitem.pszText = nameW;
  103. lvitem.iItem = 0;
  104. lvitem.iSubItem = 0;
  105. index = ListView_InsertItemW(hChannelLV, &lvitem);
  106. if (index == -1) return 0;
  107. val[1] = '\0';
  108. for (j = 0; j < 4; j++)
  109. {
  110. val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
  111. ListView_SetItemTextW(hChannelLV, index, j + 1, val);
  112. }
  113. return 1;
  114. }
  115. struct cce_user
  116. {
  117. const char* name; /* channel to look for */
  118. unsigned value, mask; /* how to change channel */
  119. unsigned done; /* number of successful changes */
  120. unsigned notdone; /* number of unsuccessful changes */
  121. };
  122. /******************************************************************
  123. * change_channel_CB
  124. *
  125. * Callback used for changing a given channel attributes
  126. */
  127. static int change_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel *channel, void* pmt)
  128. {
  129. struct cce_user* user = (struct cce_user*)pmt;
  130. if (!user->name || !strcmp(channel->name, user->name))
  131. {
  132. channel->flags = (channel->flags & ~user->mask) | (user->value & user->mask);
  133. if (WriteProcessMemory(hProcess, addr, channel, sizeof(*channel), NULL))
  134. user->done++;
  135. else
  136. user->notdone++;
  137. }
  138. return 1;
  139. }
  140. static void* get_symbol(HANDLE hProcess, const char* name)
  141. {
  142. char buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
  143. SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
  144. void* ret = NULL;
  145. /* also ask for wine extensions (loading symbols from ELF files) */
  146. pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_PUBLICS_ONLY | 0x40000000);
  147. /* FIXME: the TRUE option is due to the fact that dbghelp requires it
  148. * when loading an ELF module
  149. */
  150. if (pSymInitialize(hProcess, NULL, TRUE))
  151. {
  152. si->SizeOfStruct = sizeof(*si);
  153. si->MaxNameLen = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL);
  154. if (pSymFromName(hProcess, name, si))
  155. ret = (void*)(ULONG_PTR)si->Address;
  156. pSymCleanup(hProcess);
  157. }
  158. return ret;
  159. }
  160. typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
  161. /******************************************************************
  162. * enum_channel
  163. *
  164. * Enumerates all known channels on process hProcess through callback
  165. * ce.
  166. */
  167. static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
  168. {
  169. struct __wine_debug_channel channel;
  170. int ret = 1;
  171. void* addr;
  172. if (!(addr = get_symbol(hProcess, "libwine.so.1!debug_options"))) return -1;
  173. while (ret && addr && ReadProcessMemory(hProcess, addr, &channel, sizeof(channel), NULL))
  174. {
  175. if (!channel.name[0]) break;
  176. ret = ce(hProcess, addr, &channel, user);
  177. addr = (struct __wine_debug_channel *)addr + 1;
  178. }
  179. return 0;
  180. }
  181. static void DebugChannels_FillList(HWND hChannelLV)
  182. {
  183. HANDLE hProcess;
  184. SendMessageW(hChannelLV, LVM_DELETEALLITEMS, 0, 0);
  185. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, get_selected_pid());
  186. if (!hProcess) return; /* FIXME messagebox */
  187. SendMessageW(hChannelLV, WM_SETREDRAW, FALSE, 0);
  188. enum_channel(hProcess, list_channel_CB, (void*)hChannelLV);
  189. SendMessageW(hChannelLV, WM_SETREDRAW, TRUE, 0);
  190. CloseHandle(hProcess);
  191. }
  192. static void DebugChannels_OnCreate(HWND hwndDlg)
  193. {
  194. static WCHAR fixmeW[] = {'F','i','x','m','e','\0'};
  195. static WCHAR errW[] = {'E','r','r','\0'};
  196. static WCHAR warnW[] = {'W','a','r','n','\0'};
  197. static WCHAR traceW[] = {'T','r','a','c','e','\0'};
  198. HWND hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
  199. LVCOLUMNW lvc;
  200. WCHAR debug_channelW[255];
  201. LoadStringW(hInst, IDS_DEBUG_CHANNEL, debug_channelW, ARRAY_SIZE(debug_channelW));
  202. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  203. lvc.fmt = LVCFMT_LEFT;
  204. lvc.pszText = debug_channelW;
  205. lvc.cx = 100;
  206. SendMessageW(hLV, LVM_INSERTCOLUMNW, 0, (LPARAM) &lvc);
  207. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  208. lvc.fmt = LVCFMT_CENTER;
  209. lvc.pszText = fixmeW;
  210. lvc.cx = 55;
  211. SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
  212. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  213. lvc.fmt = LVCFMT_CENTER;
  214. lvc.pszText = errW;
  215. lvc.cx = 55;
  216. SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
  217. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  218. lvc.fmt = LVCFMT_CENTER;
  219. lvc.pszText = warnW;
  220. lvc.cx = 55;
  221. SendMessageW(hLV, LVM_INSERTCOLUMNW, 3, (LPARAM) &lvc);
  222. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  223. lvc.fmt = LVCFMT_CENTER;
  224. lvc.pszText = traceW;
  225. lvc.cx = 55;
  226. SendMessageW(hLV, LVM_INSERTCOLUMNW, 4, (LPARAM) &lvc);
  227. DebugChannels_FillList(hLV);
  228. }
  229. static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
  230. {
  231. NMHDR* nmh = (NMHDR*)lParam;
  232. switch (nmh->code)
  233. {
  234. case NM_CLICK:
  235. if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
  236. {
  237. LVHITTESTINFO lhti;
  238. HWND hChannelLV;
  239. HANDLE hProcess;
  240. NMITEMACTIVATE* nmia = (NMITEMACTIVATE*)lParam;
  241. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
  242. FALSE, get_selected_pid());
  243. if (!hProcess) return; /* FIXME message box */
  244. lhti.pt = nmia->ptAction;
  245. hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
  246. SendMessageW(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
  247. if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
  248. {
  249. WCHAR val[2];
  250. char name[32];
  251. unsigned bitmask = 1 << (lhti.iSubItem - 1);
  252. struct cce_user user;
  253. ListView_GetItemTextA(hChannelLV, lhti.iItem, 0, name, ARRAY_SIZE(name));
  254. ListView_GetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val, ARRAY_SIZE(val));
  255. user.name = name;
  256. user.value = (val[0] == 'x') ? 0 : bitmask;
  257. user.mask = bitmask;
  258. user.done = user.notdone = 0;
  259. enum_channel(hProcess, change_channel_CB, &user);
  260. if (user.done)
  261. {
  262. val[0] ^= ('x' ^ ' ');
  263. ListView_SetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val);
  264. }
  265. if (user.notdone)
  266. printf("Some channel instances weren't correctly set\n");
  267. }
  268. CloseHandle(hProcess);
  269. }
  270. break;
  271. }
  272. }
  273. static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  274. {
  275. switch (message)
  276. {
  277. case WM_INITDIALOG:
  278. DebugChannels_OnCreate(hDlg);
  279. return TRUE;
  280. case WM_COMMAND:
  281. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  282. {
  283. EndDialog(hDlg, LOWORD(wParam));
  284. return TRUE;
  285. }
  286. break;
  287. case WM_NOTIFY:
  288. DebugChannels_OnNotify(hDlg, lParam);
  289. break;
  290. }
  291. return FALSE;
  292. }
  293. void ProcessPage_OnDebugChannels(void)
  294. {
  295. DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);
  296. }