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 */
38 #include "wine/debug.h"
41 * - the dialog box could be non modal
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);
83 static DWORD get_selected_pid(void)
89 for (Index = 0; Index < (ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
91 lvitem.mask = LVIF_STATE;
92 lvitem.stateMask = LVIS_SELECTED;
96 SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem);
98 if (lvitem.state & LVIS_SELECTED)
102 dwProcessId = PerfDataGetProcessId(Index);
104 if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
109 static int list_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel* channel, void* user)
115 HWND hChannelLV = (HWND)user;
117 lvitem.mask = LVIF_TEXT;
118 lvitem.pszText = channel->name;
122 index = ListView_InsertItem(hChannelLV, &lvitem);
123 if (index == -1) return 0;
126 for (j = 0; j < 4; j++)
128 val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
129 ListView_SetItemText(hChannelLV, index, j + 1, val);
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 /******************************************************************
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))
162 static void* get_symbol(HANDLE hProcess, const char* name)
164 char buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
165 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
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);
184 typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
186 /******************************************************************
189 * Enumerates all known channels on process hProcess through callback
192 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
194 struct __wine_debug_channel channel;
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;
209 static void DebugChannels_FillList(HWND hChannelLV)
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);
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;
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;
244 SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
246 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
247 lvc.fmt = LVCFMT_CENTER;
250 SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
252 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
253 lvc.fmt = LVCFMT_CENTER;
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;
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;
274 if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
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)
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]));
296 user.value = (val[0] == 'x') ? 0 : bitmask;
298 user.done = user.notdone = 0;
299 enum_channel(hProcess, change_channel_CB, &user);
302 val[0] ^= ('x' ^ ' ');
303 ListView_SetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val);
306 printf("Some channel instances weren't correctly set\n");
308 CloseHandle(hProcess);
314 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
319 DebugChannels_OnCreate(hDlg);
322 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
324 EndDialog(hDlg, LOWORD(wParam));
329 DebugChannels_OnNotify(hDlg, lParam);
335 void ProcessPage_OnDebugChannels(void)
337 DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);