wineoss: Fix missing break statement.
[wine.git] / programs / taskmgr / dbgchnl.c
blob2d160bf4b5092d68e5a11b2f8141b031e0d48e85
1 /*
2 * ReactOS Task Manager
4 * dbgchnl.c
6 * Copyright (C) 2003 - 2004 Eric Pouech
7 * Copyright (C) 2008 Vladimir Pankratov
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.
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.
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
24 #include <ctype.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include <windows.h>
29 #include <commctrl.h>
30 #include <winnt.h>
32 #include "taskmgr.h"
33 #include "perfdata.h"
34 #include "column.h"
35 #include "wine/debug.h"
37 /* TODO:
38 * - the dialog box could be non modal
39 * - in that case,
40 * + could refresh channels from time to time
41 * + set the name of exec (and perhaps its pid) in dialog title
42 * - get a better UI (replace the 'x' by real tick boxes in list view)
43 * - enhance visual feedback: the list is large, and it's hard to get the
44 * right line when clicking on rightmost column (trace for example)
45 * - get rid of printfs (error reporting) and use real message boxes
46 * - include the column width settings in the full column management scheme
47 * - use more global settings (like having a temporary on/off
48 * setting for a fixme:s or err:s
51 static DWORD get_selected_pid(void)
53 LVITEMW lvitem;
54 ULONG Index, Count;
55 DWORD dwProcessId;
57 Count = SendMessageW(hProcessPageListCtrl, LVM_GETITEMCOUNT, 0, 0);
58 for (Index = 0; Index < Count; Index++)
60 lvitem.mask = LVIF_STATE;
61 lvitem.stateMask = LVIS_SELECTED;
62 lvitem.iItem = Index;
63 lvitem.iSubItem = 0;
65 SendMessageW(hProcessPageListCtrl, LVM_GETITEMW, 0, (LPARAM) &lvitem);
67 if (lvitem.state & LVIS_SELECTED)
68 break;
71 Count = SendMessageW(hProcessPageListCtrl, LVM_GETSELECTEDCOUNT, 0, 0);
72 dwProcessId = PerfDataGetProcessId(Index);
73 if ((Count != 1) || (dwProcessId == 0))
74 return 0;
75 return dwProcessId;
78 static int list_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel* channel, void* user)
80 int j;
81 WCHAR nameW[sizeof(channel->name)], val[2];
82 LVITEMW lvitem;
83 int index;
84 HWND hChannelLV = user;
86 MultiByteToWideChar(CP_ACP, 0, channel->name, sizeof(channel->name), nameW, ARRAY_SIZE(nameW));
88 lvitem.mask = LVIF_TEXT;
89 lvitem.pszText = nameW;
90 lvitem.iItem = 0;
91 lvitem.iSubItem = 0;
93 index = ListView_InsertItemW(hChannelLV, &lvitem);
94 if (index == -1) return 0;
96 val[1] = '\0';
97 for (j = 0; j < 4; j++)
99 val[0] = (channel->flags & (1 << j)) ? 'x' : ' ';
100 ListView_SetItemTextW(hChannelLV, index, j + 1, val);
102 return 1;
105 struct cce_user
107 const char* name; /* channel to look for */
108 unsigned value, mask; /* how to change channel */
109 unsigned done; /* number of successful changes */
110 unsigned notdone; /* number of unsuccessful changes */
113 /******************************************************************
114 * change_channel_CB
116 * Callback used for changing a given channel attributes
118 static int change_channel_CB(HANDLE hProcess, void* addr, struct __wine_debug_channel *channel, void* pmt)
120 struct cce_user* user = (struct cce_user*)pmt;
122 if (!user->name || !strcmp(channel->name, user->name))
124 channel->flags = (channel->flags & ~user->mask) | (user->value & user->mask);
125 if (WriteProcessMemory(hProcess, addr, channel, sizeof(*channel), NULL))
126 user->done++;
127 else
128 user->notdone++;
130 return 1;
134 typedef int (*EnumChannelCB)(HANDLE, void*, struct __wine_debug_channel*, void*);
136 /******************************************************************
137 * enum_channel
139 * Enumerates all known channels on process hProcess through callback
140 * ce.
142 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user)
144 struct __wine_debug_channel channel;
145 PROCESS_BASIC_INFORMATION info;
146 int ret = 1;
147 void* addr;
149 NtQueryInformationProcess( hProcess, ProcessBasicInformation, &info, sizeof(info), NULL );
150 #ifdef _WIN64
151 addr = (char *)info.PebBaseAddress + 0x2000;
152 #else
153 addr = (char *)info.PebBaseAddress + 0x1000;
154 #endif
155 while (ret && addr && ReadProcessMemory(hProcess, addr, &channel, sizeof(channel), NULL))
157 if (!channel.name[0]) break;
158 ret = ce(hProcess, addr, &channel, user);
159 addr = (struct __wine_debug_channel *)addr + 1;
161 return 0;
164 static void DebugChannels_FillList(HWND hChannelLV)
166 HANDLE hProcess;
168 SendMessageW(hChannelLV, LVM_DELETEALLITEMS, 0, 0);
170 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, get_selected_pid());
171 if (!hProcess) return; /* FIXME messagebox */
172 SendMessageW(hChannelLV, WM_SETREDRAW, FALSE, 0);
173 enum_channel(hProcess, list_channel_CB, (void*)hChannelLV);
174 SendMessageW(hChannelLV, WM_SETREDRAW, TRUE, 0);
175 CloseHandle(hProcess);
178 static void DebugChannels_OnCreate(HWND hwndDlg)
180 static WCHAR fixmeW[] = {'F','i','x','m','e','\0'};
181 static WCHAR errW[] = {'E','r','r','\0'};
182 static WCHAR warnW[] = {'W','a','r','n','\0'};
183 static WCHAR traceW[] = {'T','r','a','c','e','\0'};
184 HWND hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
185 LVCOLUMNW lvc;
186 WCHAR debug_channelW[255];
188 LoadStringW(hInst, IDS_DEBUG_CHANNEL, debug_channelW, ARRAY_SIZE(debug_channelW));
190 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
191 lvc.fmt = LVCFMT_LEFT;
192 lvc.pszText = debug_channelW;
193 lvc.cx = 100;
194 SendMessageW(hLV, LVM_INSERTCOLUMNW, 0, (LPARAM) &lvc);
196 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
197 lvc.fmt = LVCFMT_CENTER;
198 lvc.pszText = fixmeW;
199 lvc.cx = 55;
200 SendMessageW(hLV, LVM_INSERTCOLUMNW, 1, (LPARAM) &lvc);
202 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
203 lvc.fmt = LVCFMT_CENTER;
204 lvc.pszText = errW;
205 lvc.cx = 55;
206 SendMessageW(hLV, LVM_INSERTCOLUMNW, 2, (LPARAM) &lvc);
208 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
209 lvc.fmt = LVCFMT_CENTER;
210 lvc.pszText = warnW;
211 lvc.cx = 55;
212 SendMessageW(hLV, LVM_INSERTCOLUMNW, 3, (LPARAM) &lvc);
214 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
215 lvc.fmt = LVCFMT_CENTER;
216 lvc.pszText = traceW;
217 lvc.cx = 55;
218 SendMessageW(hLV, LVM_INSERTCOLUMNW, 4, (LPARAM) &lvc);
220 DebugChannels_FillList(hLV);
223 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
225 NMHDR* nmh = (NMHDR*)lParam;
227 switch (nmh->code)
229 case NM_CLICK:
230 if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
232 LVHITTESTINFO lhti;
233 HWND hChannelLV;
234 HANDLE hProcess;
235 NMITEMACTIVATE* nmia = (NMITEMACTIVATE*)lParam;
237 hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
238 FALSE, get_selected_pid());
239 if (!hProcess) return; /* FIXME message box */
240 lhti.pt = nmia->ptAction;
241 hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
242 SendMessageW(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
243 if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
245 WCHAR val[2];
246 char name[32];
247 unsigned bitmask = 1 << (lhti.iSubItem - 1);
248 struct cce_user user;
250 ListView_GetItemTextA(hChannelLV, lhti.iItem, 0, name, ARRAY_SIZE(name));
251 ListView_GetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val, ARRAY_SIZE(val));
252 user.name = name;
253 user.value = (val[0] == 'x') ? 0 : bitmask;
254 user.mask = bitmask;
255 user.done = user.notdone = 0;
256 enum_channel(hProcess, change_channel_CB, &user);
257 if (user.done)
259 val[0] ^= ('x' ^ ' ');
260 ListView_SetItemTextW(hChannelLV, lhti.iItem, lhti.iSubItem, val);
262 if (user.notdone)
263 printf("Some channel instances weren't correctly set\n");
265 CloseHandle(hProcess);
267 break;
271 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
273 switch (message)
275 case WM_INITDIALOG:
276 DebugChannels_OnCreate(hDlg);
277 return TRUE;
278 case WM_COMMAND:
279 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
281 EndDialog(hDlg, LOWORD(wParam));
282 return TRUE;
284 break;
285 case WM_NOTIFY:
286 DebugChannels_OnNotify(hDlg, lParam);
287 break;
289 return FALSE;
292 void ProcessPage_OnDebugChannels(void)
294 DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);