ieframe: Moved dialog resources to ieframe.
[wine/testsucceed.git] / programs / taskmgr / dbgchnl.c
blob6b7b3256d47ea0059f26ea35ac149fb723c0e27a
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 <memory.h>
30 #include <stdio.h>
31 #include <winnt.h>
32 #include <dbghelp.h>
34 #include "taskmgr.h"
35 #include "perfdata.h"
36 #include "column.h"
37 #include "wine/debug.h"
39 /* TODO:
40 * - the dialog box could be non modal
41 * - in that case,
42 * + could refresh channels from time to time
43 * + set the name of exec (and perhaps its pid) in dialog title
44 * - get a better UI (replace the 'x' by real tick boxes in list view)
45 * - enhance visual feedback: the list is large, and it's hard to get the
46 * right line when clicking on rightmost column (trace for example)
47 * - get rid of printfs (error reporting) and use real message boxes
48 * - include the column width settings in the full column management scheme
49 * - use more global settings (like having a temporary on/off
50 * setting for a fixme:s or err:s
53 static DWORD (WINAPI *pSymSetOptions)(DWORD);
54 static BOOL (WINAPI *pSymInitialize)(HANDLE, PSTR, BOOL);
55 static DWORD (WINAPI *pSymLoadModule)(HANDLE, HANDLE, PCSTR, PCSTR, DWORD, DWORD);
56 static BOOL (WINAPI *pSymCleanup)(HANDLE);
57 static BOOL (WINAPI *pSymFromName)(HANDLE, PCSTR, PSYMBOL_INFO);
59 BOOL AreDebugChannelsSupported(void)
61 static HANDLE hDbgHelp /* = NULL */;
63 static const WCHAR wszDbgHelp[] = {'D','B','G','H','E','L','P','.','D','L','L',0};
65 if (hDbgHelp) return TRUE;
67 if (!(hDbgHelp = LoadLibraryW(wszDbgHelp))) return FALSE;
68 pSymSetOptions = (void*)GetProcAddress(hDbgHelp, "SymSetOptions");
69 pSymInitialize = (void*)GetProcAddress(hDbgHelp, "SymInitialize");
70 pSymLoadModule = (void*)GetProcAddress(hDbgHelp, "SymLoadModule");
71 pSymFromName = (void*)GetProcAddress(hDbgHelp, "SymFromName");
72 pSymCleanup = (void*)GetProcAddress(hDbgHelp, "SymCleanup");
73 if (!pSymSetOptions || !pSymInitialize || !pSymLoadModule || !pSymCleanup || !pSymFromName)
75 FreeLibrary(hDbgHelp);
76 hDbgHelp = NULL;
77 return FALSE;
79 return TRUE;
82 static DWORD get_selected_pid(void)
84 LVITEMW lvitem;
85 ULONG Index, Count;
86 DWORD dwProcessId;
88 Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0);
89 for (Index = 0; Index < Count; Index++)
91 lvitem.mask = LVIF_STATE;
92 lvitem.stateMask = LVIS_SELECTED;
93 lvitem.iItem = Index;
94 lvitem.iSubItem = 0;
96 SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
98 if (lvitem.state & LVIS_SELECTED)
99 break;
102 Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0);
103 dwProcessId = PerfDataGetProcessId(Index);
104 if ((Count != 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 WCHAR nameW[sizeof(channel->name)], val[2];
113 LVITEMW lvitem;
114 int index;
115 HWND hChannelLV = user;
117 MultiByteToWideChar(CP_ACP, 0, channel->name, sizeof(channel->name), nameW, sizeof(nameW)/sizeof(WCHAR));
119 lvitem.mask = LVIF_TEXT;
120 lvitem.pszText = nameW;
121 lvitem.iItem = 0;
122 lvitem.iSubItem = 0;
124 index = ListView_InsertItemW(hChannelLV, &lvitem);
125 if (index == -1) return 0;
127 val[1] = '\0';
128 for (j = 0; j < 4; j++)
130 val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
131 ListView_SetItemTextW(hChannelLV, index, j + 1, val);
133 return 1;
136 struct cce_user
138 const char* name; /* channel to look for */
139 unsigned value, mask; /* how to change channel */
140 unsigned done; /* number of successful changes */
141 unsigned notdone; /* number of unsuccessful changes */
144 /******************************************************************
145 * change_channel_CB
147 * Callback used for changing a given channel attributes
149 static int change_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel *channel, void* pmt)
151 struct cce_user* user = (struct cce_user*)pmt;
153 if (!user->name || !strcmp(channel->name, user->name))
155 channel->flags = (channel->flags & ~user->mask) | (user->value & user->mask);
156 if (WriteProcessMemory(hProcess, addr, channel, sizeof(*channel), NULL))
157 user->done++;
158 else
159 user->notdone++;
161 return 1;
164 static void* get_symbol(HANDLE hProcess, const char* name)
166 char buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
167 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
168 void* ret = NULL;
170 /* also ask for wine extensions (loading symbols from ELF files) */
171 pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_PUBLICS_ONLY | 0x40000000);
172 /* FIXME: the TRUE option is due to the fact that dbghelp requires it
173 * when loading an ELF module
175 if (pSymInitialize(hProcess, NULL, TRUE))
177 si->SizeOfStruct = sizeof(*si);
178 si->MaxNameLen = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL);
179 if (pSymFromName(hProcess, name, si))
180 ret = (void*)(ULONG_PTR)si->Address;
181 pSymCleanup(hProcess);
183 return ret;
186 typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
188 /******************************************************************
189 * enum_channel
191 * Enumerates all known channels on process hProcess through callback
192 * ce.
194 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
196 struct __wine_debug_channel channel;
197 int ret = 1;
198 void* addr;
200 if (!(addr = get_symbol(hProcess, "libwine.so.1!debug_options"))) return -1;
202 while (ret && addr && ReadProcessMemory(hProcess, addr, &channel, sizeof(channel), NULL))
204 if (!channel.name[0]) break;
205 ret = ce(hProcess, addr, &channel, user);
206 addr = (struct __wine_debug_channel *)addr + 1;
208 return 0;
211 static void DebugChannels_FillList(HWND hChannelLV)
213 HANDLE hProcess;
215 SendMessageW(hChannelLV, LVM_DELETEALLITEMS, 0, 0);
217 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, get_selected_pid());
218 if (!hProcess) return; /* FIXME messagebox */
219 SendMessageW(hChannelLV, WM_SETREDRAW, FALSE, 0);
220 enum_channel(hProcess, list_channel_CB, (void*)hChannelLV);
221 SendMessageW(hChannelLV, WM_SETREDRAW, TRUE, 0);
222 CloseHandle(hProcess);
225 static void DebugChannels_OnCreate(HWND hwndDlg)
227 HWND hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
228 LVCOLUMNW lvc;
230 WCHAR debug_channelW[255];
231 WCHAR fixmeW[255];
232 WCHAR errW[255];
233 WCHAR warnW[255];
234 WCHAR traceW[255];
236 LoadStringW(hInst, IDS_DEBUG_CHANNEL, debug_channelW, sizeof(debug_channelW)/sizeof(WCHAR));
237 LoadStringW(hInst, IDS_DEBUG_CHANNEL_FIXME, fixmeW, sizeof(fixmeW)/sizeof(WCHAR));
238 LoadStringW(hInst, IDS_DEBUG_CHANNEL_ERR, errW, sizeof(errW)/sizeof(WCHAR));
239 LoadStringW(hInst, IDS_DEBUG_CHANNEL_WARN, warnW, sizeof(warnW)/sizeof(WCHAR));
240 LoadStringW(hInst, IDS_DEBUG_CHANNEL_TRACE, traceW, sizeof(traceW)/sizeof(WCHAR));
242 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
243 lvc.fmt = LVCFMT_LEFT;
244 lvc.pszText = debug_channelW;
245 lvc.cx = 100;
246 SendMessageW(hLV, LVM_INSERTCOLUMNW, 0, (LPARAM) &lvc);
248 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
249 lvc.fmt = LVCFMT_CENTER;
250 lvc.pszText = fixmeW;
251 lvc.cx = 55;
252 SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
254 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
255 lvc.fmt = LVCFMT_CENTER;
256 lvc.pszText = errW;
257 lvc.cx = 55;
258 SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
260 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
261 lvc.fmt = LVCFMT_CENTER;
262 lvc.pszText = warnW;
263 lvc.cx = 55;
264 SendMessageW(hLV, LVM_INSERTCOLUMNW, 3, (LPARAM) &lvc);
266 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
267 lvc.fmt = LVCFMT_CENTER;
268 lvc.pszText = traceW;
269 lvc.cx = 55;
270 SendMessageW(hLV, LVM_INSERTCOLUMNW, 4, (LPARAM) &lvc);
272 DebugChannels_FillList(hLV);
275 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
277 NMHDR* nmh = (NMHDR*)lParam;
279 switch (nmh->code)
281 case NM_CLICK:
282 if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
284 LVHITTESTINFO lhti;
285 HWND hChannelLV;
286 HANDLE hProcess;
287 NMITEMACTIVATE* nmia = (NMITEMACTIVATE*)lParam;
289 hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, get_selected_pid());
290 if (!hProcess) return; /* FIXME message box */
291 lhti.pt = nmia->ptAction;
292 hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
293 SendMessageW(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
294 if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
296 WCHAR val[2];
297 char name[32];
298 unsigned bitmask = 1 << (lhti.iSubItem - 1);
299 struct cce_user user;
301 ListView_GetItemTextA(hChannelLV, lhti.iItem, 0, name, sizeof(name) / sizeof(name[0]));
302 ListView_GetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val, sizeof(val) / sizeof(val[0]));
303 user.name = name;
304 user.value = (val[0] == 'x') ? 0 : bitmask;
305 user.mask = bitmask;
306 user.done = user.notdone = 0;
307 enum_channel(hProcess, change_channel_CB, &user);
308 if (user.done)
310 val[0] ^= ('x' ^ ' ');
311 ListView_SetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val);
313 if (user.notdone)
314 printf("Some channel instances weren't correctly set\n");
316 CloseHandle(hProcess);
318 break;
322 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
324 switch (message)
326 case WM_INITDIALOG:
327 DebugChannels_OnCreate(hDlg);
328 return TRUE;
329 case WM_COMMAND:
330 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
332 EndDialog(hDlg, LOWORD(wParam));
333 return TRUE;
335 break;
336 case WM_NOTIFY:
337 DebugChannels_OnNotify(hDlg, lParam);
338 break;
340 return FALSE;
343 void ProcessPage_OnDebugChannels(void)
345 DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);