services: Save service dependencies.
[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 <memory.h>
30 #include <stdio.h>
31 #include <winnt.h>
32 #include <dbghelp.h>
33     
34 #include "taskmgr.h"
35 #include "perfdata.h"
36 #include "column.h"
37 #include "wine/debug.h"
38
39 /* TODO:
40  *      - the dialog box could be non modal
41  *      - in that case,
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
51  */
52
53 static DWORD (WINAPI *pSymSetOptions)(DWORD);
54 static BOOL  (WINAPI *pSymInitialize)(HANDLE, PSTR, BOOL);
55 static DWORD (WINAPI *pSymLoadModule)(HANDLE, HANDLE, PCSTR, PCSTR, DWORD, DWORD);
56 static BOOL  (WINAPI *pSymCleanup)(HANDLE);
57 static BOOL  (WINAPI *pSymFromName)(HANDLE, PCSTR, PSYMBOL_INFO);
58
59 BOOL AreDebugChannelsSupported(void)
60 {
61     static HANDLE   hDbgHelp /* = NULL */;
62
63     static const WCHAR    wszDbgHelp[] = {'D','B','G','H','E','L','P','.','D','L','L',0};
64
65     if (hDbgHelp) return TRUE;
66
67     if (!(hDbgHelp = LoadLibraryW(wszDbgHelp))) return FALSE;
68     pSymSetOptions = (void*)GetProcAddress(hDbgHelp, "SymSetOptions");
69     pSymInitialize = (void*)GetProcAddress(hDbgHelp, "SymInitialize");
70     pSymLoadModule = (void*)GetProcAddress(hDbgHelp, "SymLoadModule");
71     pSymFromName   = (void*)GetProcAddress(hDbgHelp, "SymFromName");
72     pSymCleanup    = (void*)GetProcAddress(hDbgHelp, "SymCleanup");
73     if (!pSymSetOptions || !pSymInitialize || !pSymLoadModule || !pSymCleanup || !pSymFromName)
74     {
75         FreeLibrary(hDbgHelp);
76         hDbgHelp = NULL;
77         return FALSE;
78     }
79     return TRUE;
80 }
81
82 static DWORD    get_selected_pid(void)
83 {
84     LVITEM      lvitem;
85     ULONG       Index;
86     DWORD       dwProcessId;
87
88     for (Index = 0; Index < (ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
89     {
90         lvitem.mask = LVIF_STATE;
91         lvitem.stateMask = LVIS_SELECTED;
92         lvitem.iItem = Index;
93         lvitem.iSubItem = 0;
94
95         SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem);
96
97         if (lvitem.state & LVIS_SELECTED)
98             break;
99     }
100
101     dwProcessId = PerfDataGetProcessId(Index);
102
103     if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 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       val[2];
112     LVITEMA     lvitem;
113     int         index;
114     HWND        hChannelLV = user;
115
116     lvitem.mask = LVIF_TEXT;
117     lvitem.pszText = channel->name;
118     lvitem.iItem = 0;
119     lvitem.iSubItem = 0;
120
121     index = ListView_InsertItem(hChannelLV, &lvitem);
122     if (index == -1) return 0;
123
124     val[1] = '\0';
125     for (j = 0; j < 4; j++)
126     {
127         val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
128         ListView_SetItemTextW(hChannelLV, index, j + 1, val);
129     }
130     return 1;
131 }
132
133 struct cce_user
134 {
135     const char* name;           /* channel to look for */
136     unsigned    value, mask;    /* how to change channel */
137     unsigned    done;           /* number of successful changes */
138     unsigned    notdone;        /* number of unsuccessful changes */
139 };
140
141 /******************************************************************
142  *              change_channel_CB
143  *
144  * Callback used for changing a given channel attributes
145  */
146 static int change_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel *channel, void* pmt)
147 {
148     struct cce_user* user = (struct cce_user*)pmt;
149
150     if (!user->name || !strcmp(channel->name, user->name))
151     {
152         channel->flags = (channel->flags & ~user->mask) | (user->value & user->mask);
153         if (WriteProcessMemory(hProcess, addr, channel, sizeof(*channel), NULL))
154             user->done++;
155         else
156             user->notdone++;
157     }
158     return 1;
159 }
160
161 static void* get_symbol(HANDLE hProcess, const char* name)
162 {
163     char                buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
164     SYMBOL_INFO*        si = (SYMBOL_INFO*)buffer;
165     void*               ret = NULL;
166
167     /* also ask for wine extensions (loading symbols from ELF files) */
168     pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_PUBLICS_ONLY | 0x40000000);
169     /* FIXME: the TRUE option is due to the fact that dbghelp requires it
170      * when loading an ELF module
171      */
172     if (pSymInitialize(hProcess, NULL, TRUE))
173     {
174         si->SizeOfStruct = sizeof(*si);
175         si->MaxNameLen = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL);
176         if (pSymFromName(hProcess, name, si))
177             ret = (void*)(ULONG_PTR)si->Address;
178         pSymCleanup(hProcess);
179     }
180     return ret;
181 }
182
183 typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
184
185 /******************************************************************
186  *              enum_channel
187  *
188  * Enumerates all known channels on process hProcess through callback
189  * ce.
190  */
191 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
192 {
193     struct __wine_debug_channel channel;
194     int                         ret = 1;
195     void*                       addr;
196
197     if (!(addr = get_symbol(hProcess, "libwine.so.1!debug_options"))) return -1;
198
199     while (ret && addr && ReadProcessMemory(hProcess, addr, &channel, sizeof(channel), NULL))
200     {
201         if (!channel.name[0]) break;
202         ret = ce(hProcess, addr, &channel, user);
203         addr = (struct __wine_debug_channel *)addr + 1;
204     }
205     return 0;
206 }
207
208 static void DebugChannels_FillList(HWND hChannelLV)
209 {
210     HANDLE      hProcess;
211
212     SendMessage(hChannelLV, LVM_DELETEALLITEMS, 0, 0);
213
214     hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, get_selected_pid());
215     if (!hProcess) return; /* FIXME messagebox */
216     SendMessage(hChannelLV, WM_SETREDRAW, FALSE, 0);
217     enum_channel(hProcess, list_channel_CB, (void*)hChannelLV);
218     SendMessage(hChannelLV, WM_SETREDRAW, TRUE, 0);
219     CloseHandle(hProcess);
220 }
221
222 static void DebugChannels_OnCreate(HWND hwndDlg)
223 {
224     HWND        hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
225     LVCOLUMNW   lvc;
226
227     WCHAR debug_channelW[255];
228     WCHAR fixmeW[255];
229     WCHAR errW[255];
230     WCHAR warnW[255];
231     WCHAR traceW[255];
232
233     LoadStringW(hInst, IDS_DEBUG_CHANNEL, debug_channelW, sizeof(debug_channelW)/sizeof(WCHAR));
234     LoadStringW(hInst, IDS_DEBUG_CHANNEL_FIXME, fixmeW, sizeof(fixmeW)/sizeof(WCHAR));
235     LoadStringW(hInst, IDS_DEBUG_CHANNEL_ERR, errW, sizeof(errW)/sizeof(WCHAR));
236     LoadStringW(hInst, IDS_DEBUG_CHANNEL_WARN, warnW, sizeof(warnW)/sizeof(WCHAR));
237     LoadStringW(hInst, IDS_DEBUG_CHANNEL_TRACE, traceW, sizeof(traceW)/sizeof(WCHAR));
238
239     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
240     lvc.fmt = LVCFMT_LEFT;
241     lvc.pszText = debug_channelW;
242     lvc.cx = 100;
243     SendMessageW(hLV, LVM_INSERTCOLUMNW, 0, (LPARAM) &lvc);
244
245     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
246     lvc.fmt = LVCFMT_CENTER;
247     lvc.pszText = fixmeW;
248     lvc.cx = 55;
249     SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
250
251     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
252     lvc.fmt = LVCFMT_CENTER;
253     lvc.pszText = errW;
254     lvc.cx = 55;
255     SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
256
257     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
258     lvc.fmt = LVCFMT_CENTER;
259     lvc.pszText = warnW;
260     lvc.cx = 55;
261     SendMessageW(hLV, LVM_INSERTCOLUMNW, 3, (LPARAM) &lvc);
262
263     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
264     lvc.fmt = LVCFMT_CENTER;
265     lvc.pszText = traceW;
266     lvc.cx = 55;
267     SendMessageW(hLV, LVM_INSERTCOLUMNW, 4, (LPARAM) &lvc);
268
269     DebugChannels_FillList(hLV);
270 }
271
272 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
273 {
274     NMHDR*      nmh = (NMHDR*)lParam;
275
276     switch (nmh->code)
277     {
278     case NM_CLICK:
279         if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
280         {
281             LVHITTESTINFO       lhti;
282             HWND                hChannelLV;
283             HANDLE              hProcess;
284             NMITEMACTIVATE*     nmia = (NMITEMACTIVATE*)lParam;
285
286             hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, get_selected_pid());
287             if (!hProcess) return; /* FIXME message box */
288             lhti.pt = nmia->ptAction;
289             hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
290             SendMessage(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
291             if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
292             {
293                 WCHAR           val[2];
294                 char            name[32];
295                 unsigned        bitmask = 1 << (lhti.iSubItem - 1);
296                 struct cce_user user;
297
298                 ListView_GetItemTextA(hChannelLV, lhti.iItem, 0, name, sizeof(name) / sizeof(name[0]));
299                 ListView_GetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val, sizeof(val) / sizeof(val[0]));
300                 user.name = name;
301                 user.value = (val[0] == 'x') ? 0 : bitmask;
302                 user.mask = bitmask;
303                 user.done = user.notdone = 0;
304                 enum_channel(hProcess, change_channel_CB, &user);
305                 if (user.done)
306                 {
307                     val[0] ^= ('x' ^ ' ');
308                     ListView_SetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val);
309                 }
310                 if (user.notdone)
311                     printf("Some channel instances weren't correctly set\n");
312             }
313             CloseHandle(hProcess);
314         }
315         break;
316     }
317 }
318
319 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
320 {
321     switch (message)
322     {
323     case WM_INITDIALOG:
324         DebugChannels_OnCreate(hDlg);
325         return TRUE;
326     case WM_COMMAND:
327         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
328         {
329             EndDialog(hDlg, LOWORD(wParam));
330             return TRUE;
331         }
332         break;
333     case WM_NOTIFY:
334         DebugChannels_OnNotify(hDlg, lParam);
335         break;
336     }
337     return FALSE;
338 }
339
340 void ProcessPage_OnDebugChannels(void)
341 {
342     DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);
343 }