winemine: Adding flag before the game starts shouldn't start the timer.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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, 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     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         lvitem.mask = LVIF_STATE;
90         lvitem.stateMask = LVIS_SELECTED;
91         lvitem.iItem = Index;
92         lvitem.iSubItem = 0;
93
94         SendMessage(hProcessPageListCtrl, LVM_GETITEM, 0, (LPARAM) &lvitem);
95
96         if (lvitem.state & LVIS_SELECTED)
97             break;
98     }
99
100     dwProcessId = PerfDataGetProcessId(Index);
101
102     if ((ListView_GetSelectedCount(hProcessPageListCtrl) != 1) || (dwProcessId == 0))
103         return 0;
104     return dwProcessId;
105 }
106
107 static int     list_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel* channel, void* user)
108 {
109     int         j;
110     char        val[2];
111     LVITEMA     lvitem;
112     int         index;
113     HWND        hChannelLV = (HWND)user;
114
115     lvitem.mask = LVIF_TEXT;
116     lvitem.pszText = channel->name;
117     lvitem.iItem = 0;
118     lvitem.iSubItem = 0;
119
120     index = ListView_InsertItem(hChannelLV, &lvitem);
121     if (index == -1) return 0;
122
123     val[1] = '\0';
124     for (j = 0; j < 4; j++)
125     {
126         val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
127         ListView_SetItemText(hChannelLV, index, j + 1, val);
128     }
129     return 1;
130 }
131
132 struct cce_user
133 {
134     const char* name;           /* channel to look for */
135     unsigned    value, mask;    /* how to change channel */
136     unsigned    done;           /* number of successful changes */
137     unsigned    notdone;        /* number of unsuccessful changes */
138 };
139
140 /******************************************************************
141  *              change_channel_CB
142  *
143  * Callback used for changing a given channel attributes
144  */
145 static int change_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel *channel, void* pmt)
146 {
147     struct cce_user* user = (struct cce_user*)pmt;
148
149     if (!user->name || !strcmp(channel->name, user->name))
150     {
151         channel->flags = (channel->flags & ~user->mask) | (user->value & user->mask);
152         if (WriteProcessMemory(hProcess, addr, channel, sizeof(*channel), NULL))
153             user->done++;
154         else
155             user->notdone++;
156     }
157     return 1;
158 }
159
160 static void* get_symbol(HANDLE hProcess, const char* name)
161 {
162     char                buffer[sizeof(IMAGEHLP_SYMBOL) + 256];
163     SYMBOL_INFO*        si = (SYMBOL_INFO*)buffer;
164     void*               ret = NULL;
165
166     /* also ask for wine extensions (loading symbols from ELF files) */
167     pSymSetOptions(SYMOPT_DEFERRED_LOADS | SYMOPT_PUBLICS_ONLY | 0x40000000);
168     /* FIXME: the TRUE option is due to the fact 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 (pSymFromName(hProcess, name, si))
176             ret = (void*)(ULONG_PTR)si->Address;
177         pSymCleanup(hProcess);
178     }
179     return ret;
180 }
181
182 typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
183
184 /******************************************************************
185  *              enum_channel
186  *
187  * Enumerates all known channels on process hProcess through callback
188  * ce.
189  */
190 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
191 {
192     struct __wine_debug_channel channel;
193     int                         ret = 1;
194     void*                       addr;
195
196     if (!(addr = get_symbol(hProcess, "libwine.so.1!debug_options"))) return -1;
197
198     while (ret && addr && ReadProcessMemory(hProcess, addr, &channel, sizeof(channel), NULL))
199     {
200         if (!channel.name[0]) break;
201         ret = ce(hProcess, addr, &channel, user);
202         addr = (struct __wine_debug_channel *)addr + 1;
203     }
204     return 0;
205 }
206
207 static void DebugChannels_FillList(HWND hChannelLV)
208 {
209     HANDLE      hProcess;
210
211     SendMessage(hChannelLV, LVM_DELETEALLITEMS, 0, 0);
212
213     hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, get_selected_pid());
214     if (!hProcess) return; /* FIXME messagebox */
215     SendMessage(hChannelLV, WM_SETREDRAW, FALSE, 0);
216     enum_channel(hProcess, list_channel_CB, (void*)hChannelLV);
217     SendMessage(hChannelLV, WM_SETREDRAW, TRUE, 0);
218     CloseHandle(hProcess);
219 }
220
221 static void DebugChannels_OnCreate(HWND hwndDlg)
222 {
223     HWND        hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
224     LVCOLUMN    lvc;
225     static TCHAR debug_channelT[] = {'D','e','b','u','g',' ','C','h','a','n','n','e','l',0},
226                  fixmeT[]         = {'F','i','x','m','e',0},
227                  errT[]           = {'E','r','r',0},
228                  warnT[]          = {'W','a','r','n',0},
229                  traceT[]         = {'T','r','a','c','e',0};
230
231     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
232     lvc.fmt = LVCFMT_LEFT;
233     lvc.pszText = debug_channelT;
234     lvc.cx = 100;
235     SendMessage(hLV, LVM_INSERTCOLUMN, 0, (LPARAM) &lvc);
236
237     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
238     lvc.fmt = LVCFMT_CENTER;
239     lvc.pszText = fixmeT;
240     lvc.cx = 55;
241     SendMessage(hLV, LVM_INSERTCOLUMN, 1, (LPARAM) &lvc);
242
243     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
244     lvc.fmt = LVCFMT_CENTER;
245     lvc.pszText = errT;
246     lvc.cx = 55;
247     SendMessage(hLV, LVM_INSERTCOLUMN, 2, (LPARAM) &lvc);
248
249     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
250     lvc.fmt = LVCFMT_CENTER;
251     lvc.pszText = warnT;
252     lvc.cx = 55;
253     SendMessage(hLV, LVM_INSERTCOLUMN, 3, (LPARAM) &lvc);
254
255     lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
256     lvc.fmt = LVCFMT_CENTER;
257     lvc.pszText = traceT;
258     lvc.cx = 55;
259     SendMessage(hLV, LVM_INSERTCOLUMN, 4, (LPARAM) &lvc);
260
261     DebugChannels_FillList(hLV);
262 }
263
264 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
265 {
266     NMHDR*      nmh = (NMHDR*)lParam;
267
268     switch (nmh->code)
269     {
270     case NM_CLICK:
271         if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
272         {
273             LVHITTESTINFO       lhti;
274             HWND                hChannelLV;
275             HANDLE              hProcess;
276             NMITEMACTIVATE*     nmia = (NMITEMACTIVATE*)lParam;
277
278             hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, get_selected_pid());
279             if (!hProcess) return; /* FIXME message box */
280             lhti.pt = nmia->ptAction;
281             hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
282             SendMessage(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
283             if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
284             {
285                 TCHAR           val[2];
286                 TCHAR           name[32];
287                 unsigned        bitmask = 1 << (lhti.iSubItem - 1);
288                 struct cce_user user;
289
290                 ListView_GetItemText(hChannelLV, lhti.iItem, 0, name, sizeof(name) / sizeof(name[0]));
291                 ListView_GetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val, sizeof(val) / sizeof(val[0]));
292                 user.name = name;
293                 user.value = (val[0] == 'x') ? 0 : bitmask;
294                 user.mask = bitmask;
295                 user.done = user.notdone = 0;
296                 enum_channel(hProcess, change_channel_CB, &user);
297                 if (user.done)
298                 {
299                     val[0] ^= ('x' ^ ' ');
300                     ListView_SetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val);
301                 }
302                 if (user.notdone)
303                     printf("Some channel instances weren't correctly set\n");
304             }
305             CloseHandle(hProcess);
306         }
307         break;
308     }
309 }
310
311 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
312 {
313     switch (message)
314     {
315     case WM_INITDIALOG:
316         DebugChannels_OnCreate(hDlg);
317         return TRUE;
318     case WM_COMMAND:
319         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
320         {
321             EndDialog(hDlg, LOWORD(wParam));
322             return TRUE;
323         }
324         break;
325     case WM_NOTIFY:
326         DebugChannels_OnNotify(hDlg, lParam);
327         break;
328     }
329     return FALSE;
330 }
331
332 void ProcessPage_OnDebugChannels(void)
333 {
334     DialogBox(hInst, (LPCTSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);
335 }