dbgchnl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 "taskmgr.h"
  30. #include "perfdata.h"
  31. #include "column.h"
  32. #include "wine/debug.h"
  33. /* TODO:
  34. * - the dialog box could be non modal
  35. * - in that case,
  36. * + could refresh channels from time to time
  37. * + set the name of exec (and perhaps its pid) in dialog title
  38. * - get a better UI (replace the 'x' by real tick boxes in list view)
  39. * - enhance visual feedback: the list is large, and it's hard to get the
  40. * right line when clicking on rightmost column (trace for example)
  41. * - get rid of printfs (error reporting) and use real message boxes
  42. * - include the column width settings in the full column management scheme
  43. * - use more global settings (like having a temporary on/off
  44. * setting for a fixme:s or err:s
  45. */
  46. static DWORD get_selected_pid(void)
  47. {
  48. LVITEMW lvitem;
  49. ULONG Index, Count;
  50. DWORD dwProcessId;
  51. Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0);
  52. for (Index = 0; Index < Count; Index++)
  53. {
  54. lvitem.mask = LVIF_STATE;
  55. lvitem.stateMask = LVIS_SELECTED;
  56. lvitem.iItem = Index;
  57. lvitem.iSubItem = 0;
  58. SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
  59. if (lvitem.state & LVIS_SELECTED)
  60. break;
  61. }
  62. Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0);
  63. dwProcessId = PerfDataGetProcessId(Index);
  64. if ((Count != 1) || (dwProcessId == 0))
  65. return 0;
  66. return dwProcessId;
  67. }
  68. static int list_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel* channel, void* user)
  69. {
  70. int j;
  71. WCHAR nameW[sizeof(channel->name)], val[2];
  72. LVITEMW lvitem;
  73. int index;
  74. HWND hChannelLV = user;
  75. MultiByteToWideChar(CP_ACP, 0, channel->name, sizeof(channel->name), nameW, ARRAY_SIZE(nameW));
  76. lvitem.mask = LVIF_TEXT;
  77. lvitem.pszText = nameW;
  78. lvitem.iItem = 0;
  79. lvitem.iSubItem = 0;
  80. index = ListView_InsertItemW(hChannelLV, &lvitem);
  81. if (index == -1) return 0;
  82. val[1] = '\0';
  83. for (j = 0; j < 4; j++)
  84. {
  85. val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
  86. ListView_SetItemTextW(hChannelLV, index, j + 1, val);
  87. }
  88. return 1;
  89. }
  90. struct cce_user
  91. {
  92. const char* name; /* channel to look for */
  93. unsigned value, mask; /* how to change channel */
  94. unsigned done; /* number of successful changes */
  95. unsigned notdone; /* number of unsuccessful changes */
  96. };
  97. /******************************************************************
  98. * change_channel_CB
  99. *
  100. * Callback used for changing a given channel attributes
  101. */
  102. static int change_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel *channel, void* pmt)
  103. {
  104. struct cce_user* user = (struct cce_user*)pmt;
  105. if (!user->name || !strcmp(channel->name, user->name))
  106. {
  107. channel->flags = (channel->flags & ~user->mask) | (user->value & user->mask);
  108. if (WriteProcessMemory(hProcess, addr, channel, sizeof(*channel), NULL))
  109. user->done++;
  110. else
  111. user->notdone++;
  112. }
  113. return 1;
  114. }
  115. typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
  116. /******************************************************************
  117. * enum_channel
  118. *
  119. * Enumerates all known channels on process hProcess through callback
  120. * ce.
  121. */
  122. static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
  123. {
  124. struct __wine_debug_channel channel;
  125. PROCESS_BASIC_INFORMATION info;
  126. int ret = 1;
  127. void* addr;
  128. NtQueryInformationProcess( hProcess, ProcessBasicInformation, &info, sizeof(info), NULL );
  129. #ifdef _WIN64
  130. addr = (char *)info.PebBaseAddress + 0x2000;
  131. #else
  132. addr = (char *)info.PebBaseAddress + 0x1000;
  133. #endif
  134. while (ret && addr && ReadProcessMemory(hProcess, addr, &channel, sizeof(channel), NULL))
  135. {
  136. if (!channel.name[0]) break;
  137. ret = ce(hProcess, addr, &channel, user);
  138. addr = (struct __wine_debug_channel *)addr + 1;
  139. }
  140. return 0;
  141. }
  142. static void DebugChannels_FillList(HWND hChannelLV)
  143. {
  144. HANDLE hProcess;
  145. SendMessageW(hChannelLV, LVM_DELETEALLITEMS, 0, 0);
  146. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, get_selected_pid());
  147. if (!hProcess) return; /* FIXME messagebox */
  148. SendMessageW(hChannelLV, WM_SETREDRAW, FALSE, 0);
  149. enum_channel(hProcess, list_channel_CB, (void*)hChannelLV);
  150. SendMessageW(hChannelLV, WM_SETREDRAW, TRUE, 0);
  151. CloseHandle(hProcess);
  152. }
  153. static void DebugChannels_OnCreate(HWND hwndDlg)
  154. {
  155. static WCHAR fixmeW[] = {'F','i','x','m','e','\0'};
  156. static WCHAR errW[] = {'E','r','r','\0'};
  157. static WCHAR warnW[] = {'W','a','r','n','\0'};
  158. static WCHAR traceW[] = {'T','r','a','c','e','\0'};
  159. HWND hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
  160. LVCOLUMNW lvc;
  161. WCHAR debug_channelW[255];
  162. LoadStringW(hInst, IDS_DEBUG_CHANNEL, debug_channelW, ARRAY_SIZE(debug_channelW));
  163. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  164. lvc.fmt = LVCFMT_LEFT;
  165. lvc.pszText = debug_channelW;
  166. lvc.cx = 100;
  167. SendMessageW(hLV, LVM_INSERTCOLUMNW, 0, (LPARAM) &lvc);
  168. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  169. lvc.fmt = LVCFMT_CENTER;
  170. lvc.pszText = fixmeW;
  171. lvc.cx = 55;
  172. SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
  173. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  174. lvc.fmt = LVCFMT_CENTER;
  175. lvc.pszText = errW;
  176. lvc.cx = 55;
  177. SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
  178. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  179. lvc.fmt = LVCFMT_CENTER;
  180. lvc.pszText = warnW;
  181. lvc.cx = 55;
  182. SendMessageW(hLV, LVM_INSERTCOLUMNW, 3, (LPARAM) &lvc);
  183. lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
  184. lvc.fmt = LVCFMT_CENTER;
  185. lvc.pszText = traceW;
  186. lvc.cx = 55;
  187. SendMessageW(hLV, LVM_INSERTCOLUMNW, 4, (LPARAM) &lvc);
  188. DebugChannels_FillList(hLV);
  189. }
  190. static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
  191. {
  192. NMHDR* nmh = (NMHDR*)lParam;
  193. switch (nmh->code)
  194. {
  195. case NM_CLICK:
  196. if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
  197. {
  198. LVHITTESTINFO lhti;
  199. HWND hChannelLV;
  200. HANDLE hProcess;
  201. NMITEMACTIVATE* nmia = (NMITEMACTIVATE*)lParam;
  202. hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
  203. FALSE, get_selected_pid());
  204. if (!hProcess) return; /* FIXME message box */
  205. lhti.pt = nmia->ptAction;
  206. hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
  207. SendMessageW(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
  208. if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
  209. {
  210. WCHAR val[2];
  211. char name[32];
  212. unsigned bitmask = 1 << (lhti.iSubItem - 1);
  213. struct cce_user user;
  214. ListView_GetItemTextA(hChannelLV, lhti.iItem, 0, name, ARRAY_SIZE(name));
  215. ListView_GetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val, ARRAY_SIZE(val));
  216. user.name = name;
  217. user.value = (val[0] == 'x') ? 0 : bitmask;
  218. user.mask = bitmask;
  219. user.done = user.notdone = 0;
  220. enum_channel(hProcess, change_channel_CB, &user);
  221. if (user.done)
  222. {
  223. val[0] ^= ('x' ^ ' ');
  224. ListView_SetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val);
  225. }
  226. if (user.notdone)
  227. printf("Some channel instances weren't correctly set\n");
  228. }
  229. CloseHandle(hProcess);
  230. }
  231. break;
  232. }
  233. }
  234. static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  235. {
  236. switch (message)
  237. {
  238. case WM_INITDIALOG:
  239. DebugChannels_OnCreate(hDlg);
  240. return TRUE;
  241. case WM_COMMAND:
  242. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  243. {
  244. EndDialog(hDlg, LOWORD(wParam));
  245. return TRUE;
  246. }
  247. break;
  248. case WM_NOTIFY:
  249. DebugChannels_OnNotify(hDlg, lParam);
  250. break;
  251. }
  252. return FALSE;
  253. }
  254. void ProcessPage_OnDebugChannels(void)
  255. {
  256. DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);
  257. }