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