user32/tests: Set last error if the menu item cannot be found.
[wine/multimedia.git] / programs / taskmgr / dbgchnl.c
blob83d42816ba48285b94a4a201fe43149c2ada1b86
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 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
25 #include <windows.h>
26 #include <ctype.h>
27 #include <commctrl.h>
28 #include <stdlib.h>
29 #include <malloc.h>
30 #include <memory.h>
31 #include <stdio.h>
32 #include <winnt.h>
33 #include <dbghelp.h>
35 #include "taskmgr.h"
36 #include "perfdata.h"
37 #include "column.h"
38 #include "wine/debug.h"
40 /* TODO:
41 * - the dialog box could be non modal
42 * - in that case,
43 * + could refresh channels from time to time
44 * + set the name of exec (and perhaps its pid) in dialog title
45 * - get a better UI (replace the 'x' by real tick boxes in list view)
46 * - enhance visual feedback: the list is large, and it's hard to get the
47 * right line when clicking on rightmost column (trace for example)
48 * - get rid of printfs (error reporting) and use real message boxes
49 * - include the column width settings in the full column management scheme
50 * - use more global settings (like having a temporary on/off
51 * setting for a fixme:s or err:s
54 static DWORD (WINAPI *pSymSetOptions)(DWORD);
55 static BOOL (WINAPI *pSymInitialize)(HANDLE, PSTR, BOOL);
56 static DWORD (WINAPI *pSymLoadModule)(HANDLE, HANDLE, PCSTR, PCSTR, DWORD, DWORD);
57 static BOOL (WINAPI *pSymCleanup)(HANDLE);
58 static BOOL (WINAPI *pSymFromName)(HANDLE, PCSTR, PSYMBOL_INFO);
60 BOOL AreDebugChannelsSupported(void)
62 static HANDLE hDbgHelp /* = NULL */;
64 static const WCHAR wszDbgHelp[] = {'D','B','G','H','E','L','P','.','D','L','L',0};
66 if (hDbgHelp) return TRUE;
68 if (!(hDbgHelp = LoadLibraryW(wszDbgHelp))) return FALSE;
69 pSymSetOptions = (void*)GetProcAddress(hDbgHelp, "SymSetOptions");
70 pSymInitialize = (void*)GetProcAddress(hDbgHelp, "SymInitialize");
71 pSymLoadModule = (void*)GetProcAddress(hDbgHelp, "SymLoadModule");
72 pSymFromName = (void*)GetProcAddress(hDbgHelp, "SymFromName");
73 pSymCleanup = (void*)GetProcAddress(hDbgHelp, "SymCleanup");
74 if (!pSymSetOptions || !pSymInitialize || !pSymLoadModule || !pSymCleanup || !pSymFromName)
76 FreeLibrary(hDbgHelp);
77 hDbgHelp = NULL;
78 return FALSE;
80 return TRUE;
83 static DWORD get_selected_pid(void)
85 LVITEM lvitem;
86 ULONG Index;
87 DWORD dwProcessId;
89 for (Index = 0; Index < (ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
91 lvitem.mask = LVIF_STATE;
92 lvitem.stateMask = LVIS_SELECTED;
93 lvitem.iItem = Index;
94 lvitem.iSubItem = 0;
96 SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem);
98 if (lvitem.state & LVIS_SELECTED)
99 break;
102 dwProcessId = PerfDataGetProcessId(Index);
104 if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
105 return 0;
106 return dwProcessId;
109 static int list_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel* channel, void* user)
111 int j;
112 char val[2];
113 LVITEMA lvitem;
114 int index;
115 HWND hChannelLV = (HWND)user;
117 lvitem.mask = LVIF_TEXT;
118 lvitem.pszText = channel->name;
119 lvitem.iItem = 0;
120 lvitem.iSubItem = 0;
122 index = ListView_InsertItem(hChannelLV, &lvitem);
123 if (index == -1) return 0;
125 val[1] = '\0';
126 for (j = 0; j < 4; j++)
128 val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
129 ListView_SetItemText(hChannelLV, index, j + 1, val);
131 return 1;
134 struct cce_user
136 const char* name; /* channel to look for */
137 unsigned value, mask; /* how to change channel */
138 unsigned done; /* number of successful changes */
139 unsigned notdone; /* number of unsuccessful changes */
142 /******************************************************************
143 * change_channel_CB
145 * Callback used for changing a given channel attributes
147 static int change_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel *channel, void* pmt)
149 struct cce_user* user = (struct cce_user*)pmt;
151 if (!user->name || !strcmp(channel->name, user->name))
153 channel->flags = (channel->flags & ~user->mask) | (user->value & user->mask);
154 if (WriteProcessMemory(hProcess, addr, channel, sizeof(*channel), NULL))
155 user->done++;
156 else
157 user->notdone++;
159 return 1;
162 static void* get_symbol(HANDLE hProcess, const char* name)
164 char buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
165 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
166 void* ret = NULL;
168 /* also ask for wine extensions (loading symbols from ELF files) */
169 pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_PUBLICS_ONLY | 0x40000000);
170 /* FIXME: the TRUE option is due to the fact that dbghelp requires it
171 * when loading an ELF module
173 if (pSymInitialize(hProcess, NULL, TRUE))
175 si->SizeOfStruct = sizeof(*si);
176 si->MaxNameLen = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL);
177 if (pSymFromName(hProcess, name, si))
178 ret = (void*)(ULONG_PTR)si->Address;
179 pSymCleanup(hProcess);
181 return ret;
184 typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
186 /******************************************************************
187 * enum_channel
189 * Enumerates all known channels on process hProcess through callback
190 * ce.
192 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
194 struct __wine_debug_channel channel;
195 int ret = 1;
196 void* addr;
198 if (!(addr = get_symbol(hProcess, "libwine.so.1!debug_options"))) return -1;
200 while (ret && addr && ReadProcessMemory(hProcess, addr, &channel, sizeof(channel), NULL))
202 if (!channel.name[0]) break;
203 ret = ce(hProcess, addr, &channel, user);
204 addr = (struct __wine_debug_channel *)addr + 1;
206 return 0;
209 static void DebugChannels_FillList(HWND hChannelLV)
211 HANDLE hProcess;
213 SendMessage(hChannelLV, LVM_DELETEALLITEMS, 0, 0);
215 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, get_selected_pid());
216 if (!hProcess) return; /* FIXME messagebox */
217 SendMessage(hChannelLV, WM_SETREDRAW, FALSE, 0);
218 enum_channel(hProcess, list_channel_CB, (void*)hChannelLV);
219 SendMessage(hChannelLV, WM_SETREDRAW, TRUE, 0);
220 CloseHandle(hProcess);
223 static void DebugChannels_OnCreate(HWND hwndDlg)
225 HWND hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
226 LVCOLUMNW lvc;
228 static WCHAR debug_channelW[] = {'D','e','b','u','g',' ','C','h','a','n','n','e','l',0},
229 fixmeW[] = {'F','i','x','m','e',0},
230 errW[] = {'E','r','r',0},
231 warnW[] = {'W','a','r','n',0},
232 traceW[] = {'T','r','a','c','e',0};
234 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
235 lvc.fmt = LVCFMT_LEFT;
236 lvc.pszText = debug_channelW;
237 lvc.cx = 100;
238 SendMessageW(hLV, LVM_INSERTCOLUMNW, 0, (LPARAM) &lvc);
240 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
241 lvc.fmt = LVCFMT_CENTER;
242 lvc.pszText = fixmeW;
243 lvc.cx = 55;
244 SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
246 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
247 lvc.fmt = LVCFMT_CENTER;
248 lvc.pszText = errW;
249 lvc.cx = 55;
250 SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
252 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
253 lvc.fmt = LVCFMT_CENTER;
254 lvc.pszText = warnW;
255 lvc.cx = 55;
256 SendMessageW(hLV, LVM_INSERTCOLUMNW, 3, (LPARAM) &lvc);
258 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
259 lvc.fmt = LVCFMT_CENTER;
260 lvc.pszText = traceW;
261 lvc.cx = 55;
262 SendMessageW(hLV, LVM_INSERTCOLUMNW, 4, (LPARAM) &lvc);
264 DebugChannels_FillList(hLV);
267 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
269 NMHDR* nmh = (NMHDR*)lParam;
271 switch (nmh->code)
273 case NM_CLICK:
274 if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
276 LVHITTESTINFO lhti;
277 HWND hChannelLV;
278 HANDLE hProcess;
279 NMITEMACTIVATE* nmia = (NMITEMACTIVATE*)lParam;
281 hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, get_selected_pid());
282 if (!hProcess) return; /* FIXME message box */
283 lhti.pt = nmia->ptAction;
284 hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
285 SendMessage(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
286 if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
288 TCHAR val[2];
289 TCHAR name[32];
290 unsigned bitmask = 1 << (lhti.iSubItem - 1);
291 struct cce_user user;
293 ListView_GetItemText(hChannelLV, lhti.iItem, 0, name, sizeof(name) / sizeof(name[0]));
294 ListView_GetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val, sizeof(val) / sizeof(val[0]));
295 user.name = name;
296 user.value = (val[0] == 'x') ? 0 : bitmask;
297 user.mask = bitmask;
298 user.done = user.notdone = 0;
299 enum_channel(hProcess, change_channel_CB, &user);
300 if (user.done)
302 val[0] ^= ('x' ^ ' ');
303 ListView_SetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val);
305 if (user.notdone)
306 printf("Some channel instances weren't correctly set\n");
308 CloseHandle(hProcess);
310 break;
314 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
316 switch (message)
318 case WM_INITDIALOG:
319 DebugChannels_OnCreate(hDlg);
320 return TRUE;
321 case WM_COMMAND:
322 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
324 EndDialog(hDlg, LOWORD(wParam));
325 return TRUE;
327 break;
328 case WM_NOTIFY:
329 DebugChannels_OnNotify(hDlg, lParam);
330 break;
332 return FALSE;
335 void ProcessPage_OnDebugChannels(void)
337 DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);