dsound/tests: Mark some capture status as broken.
[wine] / programs / taskmgr / dbgchnl.c
1 /*
2  *  ReactOS Task Manager
3  *
4  *  dbgchnl.c
5  *
6  *  Copyright (C) 2003 - 2004 Eric Pouech
7  *  Copyright (C) 2008  Vladimir Pankratov
8  *
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.
13  *
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.
18  *
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
22  */
23
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include <windows.h>
29 #include <commctrl.h>
30 #include <winnt.h>
31 #include <dbghelp.h>
32
33 #include "taskmgr.h"
34 #include "perfdata.h"
35 #include "column.h"
36 #include "wine/debug.h"
37
38 /* TODO:
39  *      - the dialog box could be non modal
40  *      - in that case,
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
50  */
51
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);
57
58 BOOL AreDebugChannelsSupported(void)
59 {
60     static HANDLE   hDbgHelp /* = NULL */;
61
62     static const WCHAR    wszDbgHelp[] = {'D','B','G','H','E','L','P','.','D','L','L',0};
63
64     if (hDbgHelp) return TRUE;
65
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)
73     {
74         FreeLibrary(hDbgHelp);
75         hDbgHelp = NULL;
76         return FALSE;
77     }
78     return TRUE;
79 }
80
81 static DWORD    get_selected_pid(void)
82 {
83     LVITEMW     lvitem;
84     ULONG       Index, Count;
85     DWORD       dwProcessId;
86
87     Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0);
88     for (Index = 0; Index < Count; Index++)
89     {
90         lvitem.mask = LVIF_STATE;
91         lvitem.stateMask = LVIS_SELECTED;
92         lvitem.iItem = Index;
93         lvitem.iSubItem = 0;
94
95         SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
96
97         if (lvitem.state & LVIS_SELECTED)
98             break;
99     }
100
101     Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0);
102     dwProcessId = PerfDataGetProcessId(Index);
103     if ((Count != 1) || (dwProcessId == 0))
104         return 0;
105     return dwProcessId;
106 }
107
108 static int     list_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel* channel, void* user)
109 {
110     int         j;
111     WCHAR       nameW[sizeof(channel->name)], val[2];
112     LVITEMW     lvitem;
113     int         index;
114     HWND        hChannelLV = user;
115
116     MultiByteToWideChar(CP_ACP, 0, channel->name, sizeof(channel->name), nameW, sizeof(nameW)/sizeof(WCHAR));
117
118     lvitem.mask = LVIF_TEXT;
119     lvitem.pszText = nameW;
120     lvitem.iItem = 0;
121     lvitem.iSubItem = 0;
122
123     index = ListView_InsertItemW(hChannelLV, &lvitem);
124     if (index == -1) return 0;
125
126     val[1] = '\0';
127     for (j = 0; j < 4; j++)
128     {
129         val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
130         ListView_SetItemTextW(hChannelLV, index, j + 1, val);
131     }
132     return 1;
133 }
134
135 struct cce_user
136 {
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 */
141 };
142
143 /******************************************************************
144  *              change_channel_CB
145  *
146  * Callback used for changing a given channel attributes
147  */
148 static int change_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel *channel, void* pmt)
149 {
150     struct cce_user* user = (struct cce_user*)pmt;
151
152     if (!user->name || !strcmp(channel->name, user->name))
153     {
154         channel->flags = (channel->flags & ~user->mask) | (user->value & user->mask);
155         if (WriteProcessMemory(hProcess, addr, channel, sizeof(*channel), NULL))
156             user->done++;
157         else
158             user->notdone++;
159     }
160     return 1;
161 }
162
163 static void* get_symbol(HANDLE hProcess, const char* name)
164 {
165     char                buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
166     SYMBOL_INFO*        si = (SYMBOL_INFO*)buffer;
167     void*               ret = NULL;
168
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
173      */
174     if (pSymInitialize(hProcess, NULL, TRUE))
175     {
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);
181     }
182     return ret;
183 }
184
185 typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
186
187 /******************************************************************
188  *              enum_channel
189  *
190  * Enumerates all known channels on process hProcess through callback
191  * ce.
192  */
193 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
194 {
195     struct __wine_debug_channel channel;
196     int                         ret = 1;
197     void*                       addr;
198
199     if (!(addr = get_symbol(hProcess, "libwine.so.1!debug_options"))) return -1;
200
201     while (ret && addr && ReadProcessMemory(hProcess, addr, &channel, sizeof(channel), NULL))
202     {
203         if (!channel.name[0]) break;
204         ret = ce(hProcess, addr, &channel, user);
205         addr = (struct __wine_debug_channel *)addr + 1;
206     }
207     return 0;
208 }
209
210 static void DebugChannels_FillList(HWND hChannelLV)
211 {
212     HANDLE      hProcess;
213
214     SendMessageW(hChannelLV, LVM_DELETEALLITEMS, 0, 0);
215
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);
222 }
223
224 static void DebugChannels_OnCreate(HWND hwndDlg)
225 {
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);
231     LVCOLUMNW   lvc;
232     WCHAR debug_channelW[255];
233
234     LoadStringW(hInst, IDS_DEBUG_CHANNEL, debug_channelW, sizeof(debug_channelW)/sizeof(WCHAR));
235
236     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
237     lvc.fmt = LVCFMT_LEFT;
238     lvc.pszText = debug_channelW;
239     lvc.cx = 100;
240     SendMessageW(hLV, LVM_INSERTCOLUMNW, 0, (LPARAM) &lvc);
241
242     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
243     lvc.fmt = LVCFMT_CENTER;
244     lvc.pszText = fixmeW;
245     lvc.cx = 55;
246     SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
247
248     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
249     lvc.fmt = LVCFMT_CENTER;
250     lvc.pszText = errW;
251     lvc.cx = 55;
252     SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
253
254     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
255     lvc.fmt = LVCFMT_CENTER;
256     lvc.pszText = warnW;
257     lvc.cx = 55;
258     SendMessageW(hLV, LVM_INSERTCOLUMNW, 3, (LPARAM) &lvc);
259
260     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
261     lvc.fmt = LVCFMT_CENTER;
262     lvc.pszText = traceW;
263     lvc.cx = 55;
264     SendMessageW(hLV, LVM_INSERTCOLUMNW, 4, (LPARAM) &lvc);
265
266     DebugChannels_FillList(hLV);
267 }
268
269 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
270 {
271     NMHDR*      nmh = (NMHDR*)lParam;
272
273     switch (nmh->code)
274     {
275     case NM_CLICK:
276         if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
277         {
278             LVHITTESTINFO       lhti;
279             HWND                hChannelLV;
280             HANDLE              hProcess;
281             NMITEMACTIVATE*     nmia = (NMITEMACTIVATE*)lParam;
282
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)
289             {
290                 WCHAR           val[2];
291                 char            name[32];
292                 unsigned        bitmask = 1 << (lhti.iSubItem - 1);
293                 struct cce_user user;
294
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]));
297                 user.name = name;
298                 user.value = (val[0] == 'x') ? 0 : bitmask;
299                 user.mask = bitmask;
300                 user.done = user.notdone = 0;
301                 enum_channel(hProcess, change_channel_CB, &user);
302                 if (user.done)
303                 {
304                     val[0] ^= ('x' ^ ' ');
305                     ListView_SetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val);
306                 }
307                 if (user.notdone)
308                     printf("Some channel instances weren't correctly set\n");
309             }
310             CloseHandle(hProcess);
311         }
312         break;
313     }
314 }
315
316 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
317 {
318     switch (message)
319     {
320     case WM_INITDIALOG:
321         DebugChannels_OnCreate(hDlg);
322         return TRUE;
323     case WM_COMMAND:
324         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
325         {
326             EndDialog(hDlg, LOWORD(wParam));
327             return TRUE;
328         }
329         break;
330     case WM_NOTIFY:
331         DebugChannels_OnNotify(hDlg, lParam);
332         break;
333     }
334     return FALSE;
335 }
336
337 void ProcessPage_OnDebugChannels(void)
338 {
339     DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);
340 }