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
36 #include "wine/debug.h"
39 * - the dialog box could be non modal
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);
81 static DWORD get_selected_pid(void)
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;
95 SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
97 if (lvitem.state & LVIS_SELECTED)
101 Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0);
102 dwProcessId = PerfDataGetProcessId(Index);
103 if ((Count != 1) || (dwProcessId == 0))
108 static int list_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel* channel, void* user)
111 WCHAR nameW[sizeof(channel->name)], val[2];
114 HWND hChannelLV = user;
116 MultiByteToWideChar(CP_ACP, 0, channel->name, sizeof(channel->name), nameW, sizeof(nameW)/sizeof(WCHAR));
118 lvitem.mask = LVIF_TEXT;
119 lvitem.pszText = nameW;
123 index = ListView_InsertItemW(hChannelLV, &lvitem);
124 if (index == -1) return 0;
127 for (j = 0; j < 4; j++)
129 val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
130 ListView_SetItemTextW(hChannelLV, index, j + 1, val);
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 /******************************************************************
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))
163 static void* get_symbol(HANDLE hProcess, const char* name)
165 char buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
166 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
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);
185 typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
187 /******************************************************************
190 * Enumerates all known channels on process hProcess through callback
193 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
195 struct __wine_debug_channel channel;
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;
210 static void DebugChannels_FillList(HWND hChannelLV)
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);
232 WCHAR debug_channelW[255];
234 LoadStringW(hInst, IDS_DEBUG_CHANNEL, debug_channelW, sizeof(debug_channelW)/sizeof(WCHAR));
236 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
237 lvc.fmt = LVCFMT_LEFT;
238 lvc.pszText = debug_channelW;
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;
246 SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
248 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
249 lvc.fmt = LVCFMT_CENTER;
252 SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
254 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
255 lvc.fmt = LVCFMT_CENTER;
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;
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;
276 if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
281 NMITEMACTIVATE* nmia = (NMITEMACTIVATE*)lParam;
283 hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, get_selected_pid());
284 if (!hProcess) return; /* FIXME message box */
285 lhti.pt = nmia->ptAction;
286 hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
287 SendMessageW(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
288 if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
292 unsigned bitmask = 1 << (lhti.iSubItem - 1);
293 struct cce_user user;
295 ListView_GetItemTextA(hChannelLV, lhti.iItem, 0, name, sizeof(name) / sizeof(name[0]));
296 ListView_GetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val, sizeof(val) / sizeof(val[0]));
298 user.value = (val[0] == 'x') ? 0 : bitmask;
300 user.done = user.notdone = 0;
301 enum_channel(hProcess, change_channel_CB, &user);
304 val[0] ^= ('x' ^ ' ');
305 ListView_SetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val);
308 printf("Some channel instances weren't correctly set\n");
310 CloseHandle(hProcess);
316 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
321 DebugChannels_OnCreate(hDlg);
324 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
326 EndDialog(hDlg, LOWORD(wParam));
331 DebugChannels_OnNotify(hDlg, lParam);
337 void ProcessPage_OnDebugChannels(void)
339 DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);