6 * Copyright (C) 2003 - 2004 Eric Pouech
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #define WIN32_LEAN_AND_MEAN /* Exclude rarely-used stuff from Windows headers */
40 * - the dialog box could be non modal
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 BOOL (WINAPI *pSymInitialize)(HANDLE, PSTR, BOOL);
54 static DWORD (WINAPI *pSymLoadModule)(HANDLE, HANDLE, PSTR, PSTR, DWORD, DWORD);
55 static BOOL (WINAPI *pSymCleanup)(HANDLE);
56 static BOOL (WINAPI *pSymFromName)(HANDLE, LPSTR, PSYMBOL_INFO);
58 BOOL AreDebugChannelsSupported(void)
60 static HANDLE hDbgHelp /* = NULL */;
62 if (hDbgHelp) return TRUE;
64 if (!(hDbgHelp = LoadLibrary("dbghelp.dll"))) return FALSE;
65 pSymInitialize = (void*)GetProcAddress(hDbgHelp, "SymInitialize");
66 pSymLoadModule = (void*)GetProcAddress(hDbgHelp, "SymLoadModule");
67 pSymFromName = (void*)GetProcAddress(hDbgHelp, "SymFromName");
68 pSymCleanup = (void*)GetProcAddress(hDbgHelp, "SymCleanup");
69 if (!pSymInitialize || !pSymLoadModule || !pSymCleanup || !pSymFromName)
71 FreeLibrary(hDbgHelp);
78 static DWORD get_selected_pid(void)
84 for (Index = 0; Index < (ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
86 memset(&lvitem, 0, sizeof(LVITEM));
88 lvitem.mask = LVIF_STATE;
89 lvitem.stateMask = LVIS_SELECTED;
92 ListView_GetItem(hProcessPageListCtrl, &lvitem);
94 if (lvitem.state & LVIS_SELECTED)
98 dwProcessId = PerfDataGetProcessId(Index);
100 if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
105 static int list_channel_CB(HANDLE hProcess, void* addr, char* buffer, void* user)
111 HWND hChannelLV = (HWND)user;
113 memset(&lvi, 0, sizeof(lvi));
115 lvi.mask = LVIF_TEXT;
116 lvi.pszText = buffer + 1;
118 index = ListView_InsertItem(hChannelLV, &lvi);
119 if (index == -1) return 0;
122 for (j = 0; j < 4; j++)
124 val[0] = (buffer[0] & (1 << j)) ? 'x' : ' ';
125 ListView_SetItemText(hChannelLV, index, j + 1, val);
132 const char* name; /* channel to look for */
133 unsigned value, mask; /* how to change channel */
134 unsigned done; /* number of successful changes */
135 unsigned notdone; /* number of unsuccessful changes */
138 /******************************************************************
141 * Callback used for changing a given channel attributes
143 static int change_channel_CB(HANDLE hProcess, void* addr, char* buffer, void* pmt)
145 struct cce_user* user = (struct cce_user*)pmt;
147 if (!user->name || !strcmp(buffer + 1, user->name))
149 buffer[0] = (buffer[0] & ~user->mask) | (user->value & user->mask);
150 if (WriteProcessMemory(hProcess, addr, buffer, 1, NULL))
158 void* get_symbol(HANDLE hProcess, char* name, char* lib)
160 char buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
161 SYMBOL_INFO* si = (SYMBOL_INFO*)buffer;
164 if (pSymInitialize(hProcess, NULL, FALSE))
166 si->SizeOfStruct = sizeof(*si);
167 si->MaxNameLen = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL);
168 if (pSymLoadModule(hProcess, NULL, lib, NULL, 0, 0) &&
169 pSymFromName(hProcess, name, si))
170 ret = (void*)si->Address;
171 pSymCleanup(hProcess);
176 struct dll_option_layout
180 char* const* channels;
184 typedef int (*EnumChannelCB)(HANDLE, void*, char*, void*);
186 /******************************************************************
189 * Enumerates all known channels on process hProcess through callback
192 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user, unsigned unique)
194 struct dll_option_layout dol;
197 unsigned char buffer[32];
199 const char** cache = NULL;
200 unsigned num_cache, used_cache;
202 if (!(addr = get_symbol(hProcess, "first_dll", "libwine.so"))) return -1;
204 cache = HeapAlloc(GetProcessHeap(), 0, (num_cache = 32) * sizeof(char*));
210 ret && addr && ReadProcessMemory(hProcess, addr, &dol, sizeof(dol), NULL);
213 for (i = 0; i < dol.nb_channels; i++)
215 if (ReadProcessMemory(hProcess, (void*)(dol.channels + i), &buf_addr, sizeof(buf_addr), NULL) &&
216 ReadProcessMemory(hProcess, buf_addr, buffer, sizeof(buffer), NULL))
220 /* since some channels are defined in multiple compilation units,
221 * they will appear several times...
222 * so cache the channel's names we already reported and don't report
225 for (j = 0; j < used_cache; j++)
226 if (!strcmp(cache[j], buffer + 1)) break;
227 if (j != used_cache) continue;
228 if (used_cache == num_cache)
229 cache = HeapReAlloc(GetProcessHeap(), 0, cache, (num_cache *= 2) * sizeof(char*));
230 cache[used_cache++] = strcpy(HeapAlloc(GetProcessHeap(), 0, strlen(buffer + 1) + 1),
233 ret = ce(hProcess, buf_addr, buffer, user);
239 for (j = 0; j < used_cache; j++) HeapFree(GetProcessHeap(), 0, (char*)cache[j]);
240 HeapFree(GetProcessHeap(), 0, cache);
245 static void DebugChannels_FillList(HWND hChannelLV)
249 ListView_DeleteAllItems(hChannelLV);
251 hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, get_selected_pid());
252 if (!hProcess) return; /* FIXME messagebox */
253 SendMessage(hChannelLV, WM_SETREDRAW, FALSE, 0);
254 enum_channel(hProcess, list_channel_CB, (void*)hChannelLV, TRUE);
255 SendMessage(hChannelLV, WM_SETREDRAW, TRUE, 0);
256 CloseHandle(hProcess);
259 static void DebugChannels_OnCreate(HWND hwndDlg)
261 HWND hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
264 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
265 lvc.fmt = LVCFMT_LEFT;
266 lvc.pszText = _T("Debug Channel");
268 ListView_InsertColumn(hLV, 0, &lvc);
270 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
271 lvc.fmt = LVCFMT_CENTER;
272 lvc.pszText = _T("Fixme");
274 ListView_InsertColumn(hLV, 1, &lvc);
276 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
277 lvc.fmt = LVCFMT_CENTER;
278 lvc.pszText = _T("Err");
280 ListView_InsertColumn(hLV, 2, &lvc);
282 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
283 lvc.fmt = LVCFMT_CENTER;
284 lvc.pszText = _T("Warn");
286 ListView_InsertColumn(hLV, 3, &lvc);
288 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
289 lvc.fmt = LVCFMT_CENTER;
290 lvc.pszText = _T("Trace");
292 ListView_InsertColumn(hLV, 4, &lvc);
294 DebugChannels_FillList(hLV);
297 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
299 NMHDR* nmh = (NMHDR*)lParam;
304 if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
309 NMITEMACTIVATE* nmia = (NMITEMACTIVATE*)lParam;
311 hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, get_selected_pid());
312 if (!hProcess) return; /* FIXME message box */
313 lhti.pt = nmia->ptAction;
314 hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
315 SendMessage(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
316 if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
320 unsigned bitmask = 1 << (lhti.iSubItem - 1);
321 struct cce_user user;
323 ListView_GetItemText(hChannelLV, lhti.iItem, 0, name, sizeof(name) / sizeof(name[0]));
324 ListView_GetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val, sizeof(val) / sizeof(val[0]));
326 user.value = (val[0] == 'x') ? 0 : bitmask;
328 user.done = user.notdone = 0;
329 enum_channel(hProcess, change_channel_CB, &user, FALSE);
332 val[0] ^= ('x' ^ ' ');
333 ListView_SetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val);
336 printf("Some channel instance weren't correctly set\n");
338 CloseHandle(hProcess);
344 static LRESULT CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
349 DebugChannels_OnCreate(hDlg);
352 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
354 EndDialog(hDlg, LOWORD(wParam));
359 DebugChannels_OnNotify(hDlg, lParam);
365 void ProcessPage_OnDebugChannels(void)
367 DialogBox(hInst, (LPCTSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, (DLGPROC)DebugChannelsDlgProc);