mfreadwrite/reader: Enable DXGI device manager path.
[wine/zf.git] / programs / taskmgr / dbgchnl.c
blob7200eb476c720753baac03575bd40d259a75e8e9
1 /*
2 * ReactOS Task Manager
4 * dbgchnl.c
6 * Copyright (C) 2003 - 2004 Eric Pouech
7 * Copyright (C) 2008 Vladimir Pankratov
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.
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.
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
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include <windows.h>
29 #include <commctrl.h>
30 #include <winnt.h>
31 #include <dbghelp.h>
33 #include "taskmgr.h"
34 #include "perfdata.h"
35 #include "column.h"
36 #include "wine/debug.h"
38 /* TODO:
39 * - the dialog box could be non modal
40 * - in that case,
41 * + could refresh channels from time to time
42 * + set the name of exec (and perhaps its pid) in dialog title
43 * - get a better UI (replace the 'x' by real tick boxes in list view)
44 * - enhance visual feedback: the list is large, and it's hard to get the
45 * right line when clicking on rightmost column (trace for example)
46 * - get rid of printfs (error reporting) and use real message boxes
47 * - include the column width settings in the full column management scheme
48 * - use more global settings (like having a temporary on/off
49 * setting for a fixme:s or err:s
52 static DWORD (WINAPI *pSymSetOptions)(DWORD);
53 static BOOL (WINAPI *pSymInitialize)(HANDLE, PSTR, BOOL);
54 static DWORD (WINAPI *pSymLoadModule)(HANDLE, HANDLE, PCSTR, PCSTR, DWORD, DWORD);
55 static BOOL (WINAPI *pSymCleanup)(HANDLE);
56 static BOOL (WINAPI *pSymFromName)(HANDLE, PCSTR, PSYMBOL_INFO);
58 BOOL AreDebugChannelsSupported(void)
60 static HANDLE hDbgHelp /* = NULL */;
62 static const WCHAR wszDbgHelp[] = {'D','B','G','H','E','L','P','.','D','L','L',0};
64 if (hDbgHelp) return TRUE;
66 if (!(hDbgHelp = LoadLibraryW(wszDbgHelp))) return FALSE;
67 pSymSetOptions = (void*)GetProcAddress(hDbgHelp, "SymSetOptions");
68 pSymInitialize = (void*)GetProcAddress(hDbgHelp, "SymInitialize");
69 pSymLoadModule = (void*)GetProcAddress(hDbgHelp, "SymLoadModule");
70 pSymFromName = (void*)GetProcAddress(hDbgHelp, "SymFromName");
71 pSymCleanup = (void*)GetProcAddress(hDbgHelp, "SymCleanup");
72 if (!pSymSetOptions || !pSymInitialize || !pSymLoadModule || !pSymCleanup || !pSymFromName)
74 FreeLibrary(hDbgHelp);
75 hDbgHelp = NULL;
76 return FALSE;
78 return TRUE;
81 static DWORD get_selected_pid(void)
83 LVITEMW lvitem;
84 ULONG Index, Count;
85 DWORD dwProcessId;
87 Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0);
88 for (Index = 0; Index < Count; Index++)
90 lvitem.mask = LVIF_STATE;
91 lvitem.stateMask = LVIS_SELECTED;
92 lvitem.iItem = Index;
93 lvitem.iSubItem = 0;
95 SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
97 if (lvitem.state & LVIS_SELECTED)
98 break;
101 Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0);
102 dwProcessId = PerfDataGetProcessId(Index);
103 if ((Count != 1) || (dwProcessId == 0))
104 return 0;
105 return dwProcessId;
108 static int list_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel* channel, void* user)
110 int j;
111 WCHAR nameW[sizeof(channel->name)], val[2];
112 LVITEMW lvitem;
113 int index;
114 HWND hChannelLV = user;
116 MultiByteToWideChar(CP_ACP, 0, channel->name, sizeof(channel->name), nameW, ARRAY_SIZE(nameW));
118 lvitem.mask = LVIF_TEXT;
119 lvitem.pszText = nameW;
120 lvitem.iItem = 0;
121 lvitem.iSubItem = 0;
123 index = ListView_InsertItemW(hChannelLV, &lvitem);
124 if (index == -1) return 0;
126 val[1] = '\0';
127 for (j = 0; j < 4; j++)
129 val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
130 ListView_SetItemTextW(hChannelLV, index, j + 1, val);
132 return 1;
135 struct cce_user
137 const char* name; /* channel to look for */
138 unsigned value, mask; /* how to change channel */
139 unsigned done; /* number of successful changes */
140 unsigned notdone; /* number of unsuccessful changes */
143 /******************************************************************
144 * change_channel_CB
146 * Callback used for changing a given channel attributes
148 static int change_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel *channel, void* pmt)
150 struct cce_user* user = (struct cce_user*)pmt;
152 if (!user->name || !strcmp(channel->name, user->name))
154 channel->flags = (channel->flags & ~user->mask) | (user->value & user->mask);
155 if (WriteProcessMemory(hProcess, addr, channel, sizeof(*channel), NULL))
156 user->done++;
157 else
158 user->notdone++;
160 return 1;
163 static void* get_symbol(HANDLE hProcess, const char* name)
165 char buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
166 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
167 void* ret = NULL;
169 /* also ask for wine extensions (loading symbols from ELF files) */
170 pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_PUBLICS_ONLY | 0x40000000);
171 /* FIXME: the TRUE option is due to the fact that dbghelp requires it
172 * when loading an ELF module
174 if (pSymInitialize(hProcess, NULL, TRUE))
176 si->SizeOfStruct = sizeof(*si);
177 si->MaxNameLen = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL);
178 if (pSymFromName(hProcess, name, si))
179 ret = (void*)(ULONG_PTR)si->Address;
180 pSymCleanup(hProcess);
182 return ret;
185 typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
187 /******************************************************************
188 * enum_channel
190 * Enumerates all known channels on process hProcess through callback
191 * ce.
193 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
195 struct __wine_debug_channel channel;
196 int ret = 1;
197 void* addr;
199 if (!(addr = get_symbol(hProcess, "libwine.so.1!debug_options"))) return -1;
201 while (ret && addr && ReadProcessMemory(hProcess, addr, &channel, sizeof(channel), NULL))
203 if (!channel.name[0]) break;
204 ret = ce(hProcess, addr, &channel, user);
205 addr = (struct __wine_debug_channel *)addr + 1;
207 return 0;
210 static void DebugChannels_FillList(HWND hChannelLV)
212 HANDLE hProcess;
214 SendMessageW(hChannelLV, LVM_DELETEALLITEMS, 0, 0);
216 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, get_selected_pid());
217 if (!hProcess) return; /* FIXME messagebox */
218 SendMessageW(hChannelLV, WM_SETREDRAW, FALSE, 0);
219 enum_channel(hProcess, list_channel_CB, (void*)hChannelLV);
220 SendMessageW(hChannelLV, WM_SETREDRAW, TRUE, 0);
221 CloseHandle(hProcess);
224 static void DebugChannels_OnCreate(HWND hwndDlg)
226 static WCHAR fixmeW[] = {'F','i','x','m','e','\0'};
227 static WCHAR errW[] = {'E','r','r','\0'};
228 static WCHAR warnW[] = {'W','a','r','n','\0'};
229 static WCHAR traceW[] = {'T','r','a','c','e','\0'};
230 HWND hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
231 LVCOLUMNW lvc;
232 WCHAR debug_channelW[255];
234 LoadStringW(hInst, IDS_DEBUG_CHANNEL, debug_channelW, ARRAY_SIZE(debug_channelW));
236 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
237 lvc.fmt = LVCFMT_LEFT;
238 lvc.pszText = debug_channelW;
239 lvc.cx = 100;
240 SendMessageW(hLV, LVM_INSERTCOLUMNW, 0, (LPARAM) &lvc);
242 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
243 lvc.fmt = LVCFMT_CENTER;
244 lvc.pszText = fixmeW;
245 lvc.cx = 55;
246 SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
248 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
249 lvc.fmt = LVCFMT_CENTER;
250 lvc.pszText = errW;
251 lvc.cx = 55;
252 SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
254 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
255 lvc.fmt = LVCFMT_CENTER;
256 lvc.pszText = warnW;
257 lvc.cx = 55;
258 SendMessageW(hLV, LVM_INSERTCOLUMNW, 3, (LPARAM) &lvc);
260 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
261 lvc.fmt = LVCFMT_CENTER;
262 lvc.pszText = traceW;
263 lvc.cx = 55;
264 SendMessageW(hLV, LVM_INSERTCOLUMNW, 4, (LPARAM) &lvc);
266 DebugChannels_FillList(hLV);
269 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
271 NMHDR* nmh = (NMHDR*)lParam;
273 switch (nmh->code)
275 case NM_CLICK:
276 if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
278 LVHITTESTINFO lhti;
279 HWND hChannelLV;
280 HANDLE hProcess;
281 NMITEMACTIVATE* nmia = (NMITEMACTIVATE*)lParam;
283 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
284 FALSE, get_selected_pid());
285 if (!hProcess) return; /* FIXME message box */
286 lhti.pt = nmia->ptAction;
287 hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
288 SendMessageW(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
289 if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
291 WCHAR val[2];
292 char name[32];
293 unsigned bitmask = 1 << (lhti.iSubItem - 1);
294 struct cce_user user;
296 ListView_GetItemTextA(hChannelLV, lhti.iItem, 0, name, ARRAY_SIZE(name));
297 ListView_GetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val, ARRAY_SIZE(val));
298 user.name = name;
299 user.value = (val[0] == 'x') ? 0 : bitmask;
300 user.mask = bitmask;
301 user.done = user.notdone = 0;
302 enum_channel(hProcess, change_channel_CB, &user);
303 if (user.done)
305 val[0] ^= ('x' ^ ' ');
306 ListView_SetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val);
308 if (user.notdone)
309 printf("Some channel instances weren't correctly set\n");
311 CloseHandle(hProcess);
313 break;
317 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
319 switch (message)
321 case WM_INITDIALOG:
322 DebugChannels_OnCreate(hDlg);
323 return TRUE;
324 case WM_COMMAND:
325 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
327 EndDialog(hDlg, LOWORD(wParam));
328 return TRUE;
330 break;
331 case WM_NOTIFY:
332 DebugChannels_OnNotify(hDlg, lParam);
333 break;
335 return FALSE;
338 void ProcessPage_OnDebugChannels(void)
340 DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);