wininet: Handle the cache being full better in CommitUrlCacheEntryInternal.
[wine] / programs / taskmgr / dbgchnl.c
1 /*
2  *  ReactOS Task Manager
3  *
4  *  dbgchnl.c
5  *
6  *  Copyright (C) 2003 - 2004 Eric Pouech
7  *
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.
12  *
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.
17  *
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
21  */
22     
23 #define WIN32_LEAN_AND_MEAN    /* Exclude rarely-used stuff from Windows headers */
24 #include <windows.h>
25 #include <ctype.h>
26 #include <commctrl.h>
27 #include <stdlib.h>
28 #include <malloc.h>
29 #include <memory.h>
30 #include <tchar.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, PSTR, PSTR, DWORD, DWORD);
57 static BOOL  (WINAPI *pSymCleanup)(HANDLE);
58 static BOOL  (WINAPI *pSymFromName)(HANDLE, LPSTR, PSYMBOL_INFO);
59
60 BOOL AreDebugChannelsSupported(void)
61 {
62     static HANDLE   hDbgHelp /* = NULL */;
63
64     if (hDbgHelp) return TRUE;
65
66     if (!(hDbgHelp = LoadLibrary("dbghelp.dll"))) 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     LVITEM      lvitem;
84     ULONG       Index;
85     DWORD       dwProcessId;
86
87     for (Index = 0; Index < (ULONG)ListView_GetItemCount(hProcessPageListCtrl); Index++)
88     {
89         memset(&lvitem, 0, sizeof(LVITEM));
90
91         lvitem.mask = LVIF_STATE;
92         lvitem.stateMask = LVIS_SELECTED;
93         lvitem.iItem = Index;
94
95         ListView_GetItem(hProcessPageListCtrl, &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     char        val[2];
112     LVITEMA     lvi;
113     int         index;
114     HWND        hChannelLV = (HWND)user;
115
116     memset(&lvi, 0, sizeof(lvi));
117
118     lvi.mask = LVIF_TEXT;
119     lvi.pszText = channel->name;
120
121     index = ListView_InsertItem(hChannelLV, &lvi);
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_SetItemText(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, char* name, char* lib)
162 {
163     char                buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
164     SYMBOL_INFO*        si = (SYMBOL_INFO*)buffer;
165     void*               ret = NULL;
166
167     pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_PUBLICS_ONLY);
168     /* FIXME: the TRUE option is due to the face that dbghelp requires it
169      * when loading an ELF module
170      */
171     if (pSymInitialize(hProcess, NULL, TRUE))
172     {
173         si->SizeOfStruct = sizeof(*si);
174         si->MaxNameLen = sizeof(buffer) - sizeof(IMAGEHLP_SYMBOL);
175         if (pSymLoadModule(hProcess, NULL, lib, NULL, 0, 0) &&
176             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, "debug_options", "libwine.so"))) 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     ListView_DeleteAllItems(hChannelLV);
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     LVCOLUMN    lvc;
226
227     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
228     lvc.fmt = LVCFMT_LEFT;
229     lvc.pszText = _T("Debug Channel");
230     lvc.cx = 100;
231     ListView_InsertColumn(hLV, 0, &lvc);
232
233     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
234     lvc.fmt = LVCFMT_CENTER;
235     lvc.pszText = _T("Fixme");
236     lvc.cx = 55;
237     ListView_InsertColumn(hLV, 1, &lvc);
238
239     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
240     lvc.fmt = LVCFMT_CENTER;
241     lvc.pszText = _T("Err");
242     lvc.cx = 55;
243     ListView_InsertColumn(hLV, 2, &lvc);
244
245     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
246     lvc.fmt = LVCFMT_CENTER;
247     lvc.pszText = _T("Warn");
248     lvc.cx = 55;
249     ListView_InsertColumn(hLV, 3, &lvc);
250
251     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
252     lvc.fmt = LVCFMT_CENTER;
253     lvc.pszText = _T("Trace");
254     lvc.cx = 55;
255     ListView_InsertColumn(hLV, 4, &lvc);
256
257     DebugChannels_FillList(hLV);
258 }
259
260 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
261 {
262     NMHDR*      nmh = (NMHDR*)lParam;
263
264     switch (nmh->code)
265     {
266     case NM_CLICK:
267         if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
268         {
269             LVHITTESTINFO       lhti;
270             HWND                hChannelLV;
271             HANDLE              hProcess;
272             NMITEMACTIVATE*     nmia = (NMITEMACTIVATE*)lParam;
273
274             hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, get_selected_pid());
275             if (!hProcess) return; /* FIXME message box */
276             lhti.pt = nmia->ptAction;
277             hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
278             SendMessage(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
279             if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
280             {
281                 TCHAR           val[2];
282                 TCHAR           name[32];
283                 unsigned        bitmask = 1 << (lhti.iSubItem - 1);
284                 struct cce_user user;
285
286                 ListView_GetItemText(hChannelLV, lhti.iItem, 0, name, sizeof(name) / sizeof(name[0]));
287                 ListView_GetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val, sizeof(val) / sizeof(val[0]));
288                 user.name = name;
289                 user.value = (val[0] == 'x') ? 0 : bitmask;
290                 user.mask = bitmask;
291                 user.done = user.notdone = 0;
292                 enum_channel(hProcess, change_channel_CB, &user);
293                 if (user.done)
294                 {
295                     val[0] ^= ('x' ^ ' ');
296                     ListView_SetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val);
297                 }
298                 if (user.notdone)
299                     printf("Some channel instances weren't correctly set\n");
300             }
301             CloseHandle(hProcess);
302         }
303         break;
304     }
305 }
306
307 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
308 {
309     switch (message)
310     {
311     case WM_INITDIALOG:
312         DebugChannels_OnCreate(hDlg);
313         return TRUE;
314     case WM_COMMAND:
315         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
316         {
317             EndDialog(hDlg, LOWORD(wParam));
318             return TRUE;
319         }
320         break;
321     case WM_NOTIFY:
322         DebugChannels_OnNotify(hDlg, lParam);
323         break;
324     }
325     return FALSE;
326 }
327
328 void ProcessPage_OnDebugChannels(void)
329 {
330     DialogBox(hInst, (LPCTSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);
331 }