Merge from amd64-branch:
[reactos.git] / reactos / base / applications / 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 Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include <precomp.h>
24
25 /* TODO:
26 * - the dialog box could be non modal
27 * - in that case,
28 * + could refresh channels from time to time
29 * - get a better UI (replace the 'x' by real tick boxes in list view)
30 * - implement a real solution around the get_symbol hack
31 * - enhance visual feedback: the list is large, and it's hard to get the
32 * right line when clicking on rightmost column (trace for example)
33 * - get rid of printfs (error reporting) and use real message boxes
34 * - include the column width settings in the full column management scheme
35 */
36
37 BOOL DebugChannelsAreSupported(void)
38 {
39 #ifdef WINE
40 return TRUE;
41 #endif
42 return FALSE;
43 }
44
45 static int list_channel_CB(HANDLE hProcess, void* addr, WCHAR* buffer, void* user)
46 {
47 int j;
48 WCHAR val[2];
49 LVITEM lvi;
50 int index;
51 HWND hChannelLV = (HWND)user;
52
53 memset(&lvi, 0, sizeof(lvi));
54
55 lvi.mask = LVIF_TEXT;
56 lvi.pszText = buffer + 1;
57
58 index = ListView_InsertItem(hChannelLV, &lvi);
59 if (index == -1) return 0;
60
61 val[1] = L'\0';
62 for (j = 0; j < 4; j++)
63 {
64 val[0] = (buffer[0] & (1 << j)) ? L'x' : L' ';
65 ListView_SetItemText(hChannelLV, index, j + 1, val);
66 }
67 return 1;
68 }
69
70 struct cce_user
71 {
72 LPCWSTR name; /* channel to look for */
73 unsigned value, mask; /* how to change channel */
74 unsigned done; /* number of successful changes */
75 unsigned notdone; /* number of unsuccessful changes */
76 };
77
78 /******************************************************************
79 * change_channel_CB
80 *
81 * Callback used for changing a given channel attributes
82 */
83 static int change_channel_CB(HANDLE hProcess, void* addr, WCHAR* buffer, void* pmt)
84 {
85 struct cce_user* user = (struct cce_user*)pmt;
86
87 if (!user->name || !wcscmp(buffer + 1, user->name))
88 {
89 buffer[0] = (buffer[0] & ~user->mask) | (user->value & user->mask);
90 if (WriteProcessMemory(hProcess, addr, buffer, 1, NULL))
91 user->done++;
92 else
93 user->notdone++;
94 }
95 return 1;
96 }
97
98 #ifdef WINE
99 /******************************************************************
100 * get_symbol
101 *
102 * Here it gets ugly :-(
103 * This is quick hack to get the address of first_dll in a running process
104 * We make the following assumptions:
105 * - libwine (lib) is loaded in all processes at the same address (or
106 * at least at the same address at this process)
107 * - we load the same libwine.so version in this process and in the
108 * examined process
109 * Final address is gotten by: 1/ querying the address of a known exported
110 * symbol out of libwine.so with dlsym, 2/ then querying nm on libwine.so to
111 * get the offset from the data segment of this known symbol and of first_dll,
112 * 3/ computing the actual address of first_dll by adding the result of 1/ and
113 * the delta of 2/.
114 * Ugly, yes, but it somehow works. We should replace that with debughlp
115 * library, that'd be way better. Exporting first_dll from libwine.so would make
116 * this code simpler, but still ugly.
117 */
118 /* FIXME: we only need those includes for the next function */
119 #include <dlfcn.h> /* for RTLD_LAZY */
120 #include <sys/types.h>
121 #include <sys/stat.h>
122 #include <unistd.h>
123 #include <stdio.h>
124 #include "wine/library.h"
125
126 void* get_symbol(HANDLE hProcess, const char* name, const char* lib)
127 {
128 char buffer[1024];
129 void* h;
130 DWORD addr = 0, tmp = 0;
131 FILE* f;
132 char* env;
133
134 if (!(h = wine_dlopen(lib, RTLD_LAZY, buffer, sizeof(buffer))))
135 {
136 printf("Couldn't load %s (%s)\n", lib, buffer);
137 return NULL;
138 }
139
140 env = getenv("LD_LIBRARY_PATH");
141 if (env)
142 {
143 char *next, *ptr;
144 struct stat s;
145
146 for (ptr = env = strdup(env); ptr; ptr = next)
147 {
148 next = strchr(ptr, ':');
149 if (next) *next++ = '\0';
150 sprintf(buffer, "nm %s", ptr);
151 if (buffer[strlen(buffer) - 1] != '/') strcat(buffer, "/");
152 strcat(buffer, lib);
153 if (stat(buffer + 3, &s) == 0) break;
154 }
155 free(env);
156 if (!ptr)
157 {
158 printf("Couldn't find %s in LD_LIBRARY_PATH\n", lib);
159 return NULL;
160 }
161 }
162 if (!(f = popen(buffer, "r")))
163 {
164 printf("Cannot execute '%s'\n", buffer);
165 return NULL;
166 }
167
168 while (fgets(buffer, sizeof(buffer), f))
169 {
170 char *p = buffer + strlen(buffer) - 1;
171 if (p < buffer) continue;
172 if (*p == '\n') *p-- = 0;
173 if (p - buffer < 11) continue;
174 buffer[8] = '\0';
175 if (!strcmp(&buffer[11], name)) addr += strtol(buffer, NULL, 16);
176 if (buffer[9] == 'D' && !tmp && (tmp = (DWORD)wine_dlsym(h, &buffer[11], NULL, 0)) != 0)
177 addr += tmp - strtol(buffer, NULL, 16);
178 }
179 pclose(f);
180 return (char*)addr;
181 }
182 #else
183 void* get_symbol(HANDLE hProcess, const char* name, const char* lib)
184 {
185 printf("get_symbol: not implemented on this platform\n");
186 return NULL;
187 }
188 #endif
189
190 struct dll_option_layout
191 {
192 void* next;
193 void* prev;
194 char* const* channels;
195 unsigned int nb_channels;
196 };
197
198 typedef int (*EnumChannelCB)(HANDLE, void*, WCHAR*, void*);
199
200 /******************************************************************
201 * enum_channel
202 *
203 * Enumerates all known channels on process hProcess through callback
204 * ce.
205 */
206 static int enum_channel(HANDLE hProcess, EnumChannelCB ce, void* user, unsigned unique)
207 {
208 struct dll_option_layout dol;
209 int ret = 1;
210 void* buf_addr;
211 WCHAR buffer[32];
212 void* addr;
213 WCHAR** cache = NULL;
214 unsigned i, j, num_cache, used_cache;
215
216 addr = get_symbol(hProcess, "first_dll", "libwine.so");
217 if (!addr) return -1;
218 if (unique)
219 cache = HeapAlloc(GetProcessHeap(), 0, (num_cache = 32) * sizeof(WCHAR*));
220 else
221 num_cache = 0;
222 used_cache = 0;
223
224 for (;
225 ret && addr && ReadProcessMemory(hProcess, addr, &dol, sizeof(dol), NULL);
226 addr = dol.next)
227 {
228 for (i = 0; i < dol.nb_channels; i++)
229 {
230 if (ReadProcessMemory(hProcess, (void*)(dol.channels + i), &buf_addr, sizeof(buf_addr), NULL) &&
231 ReadProcessMemory(hProcess, buf_addr, buffer, sizeof(buffer), NULL))
232 {
233 if (unique)
234 {
235 /* since some channels are defined in multiple compilation units,
236 * they will appear several times...
237 * so cache the channel's names we already reported and don't report
238 * them again
239 */
240 for (j = 0; j < used_cache; j++)
241 if (!wcscmp(cache[j], buffer + 1)) break;
242 if (j != used_cache) continue;
243 if (used_cache == num_cache)
244 cache = HeapReAlloc(GetProcessHeap(), 0, cache, (num_cache *= 2) * sizeof(WCHAR*));
245 cache[used_cache++] = wcscpy(HeapAlloc(GetProcessHeap(), 0, (wcslen(buffer + 1) + 1) * sizeof(WCHAR)),
246 buffer + 1);
247 }
248 ret = ce(hProcess, buf_addr, buffer, user);
249 }
250 }
251 }
252 if (unique)
253 {
254 for (j = 0; j < used_cache; j++) HeapFree(GetProcessHeap(), 0, (WCHAR*)cache[j]);
255 HeapFree(GetProcessHeap(), 0, cache);
256 }
257 return 0;
258 }
259
260 static void DebugChannels_FillList(HWND hChannelLV)
261 {
262 HANDLE hProcess;
263
264 (void)ListView_DeleteAllItems(hChannelLV);
265
266 hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ, FALSE, GetSelectedProcessId());
267 if (!hProcess) return; /* FIXME messagebox */
268 SendMessageW(hChannelLV, WM_SETREDRAW, FALSE, 0);
269 enum_channel(hProcess, list_channel_CB, (void*)hChannelLV, TRUE);
270 SendMessageW(hChannelLV, WM_SETREDRAW, TRUE, 0);
271 CloseHandle(hProcess);
272 }
273
274 static void DebugChannels_OnCreate(HWND hwndDlg)
275 {
276 HWND hLV = GetDlgItem(hwndDlg, IDC_DEBUG_CHANNELS_LIST);
277 LVCOLUMN lvc;
278
279 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
280 lvc.fmt = LVCFMT_LEFT;
281 lvc.pszText = L"Debug Channel";
282 lvc.cx = 100;
283 (void)ListView_InsertColumn(hLV, 0, &lvc);
284
285 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
286 lvc.fmt = LVCFMT_CENTER;
287 lvc.pszText = L"Fixme";
288 lvc.cx = 55;
289 (void)ListView_InsertColumn(hLV, 1, &lvc);
290
291 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
292 lvc.fmt = LVCFMT_CENTER;
293 lvc.pszText = L"Err";
294 lvc.cx = 55;
295 (void)ListView_InsertColumn(hLV, 2, &lvc);
296
297 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
298 lvc.fmt = LVCFMT_CENTER;
299 lvc.pszText = L"Warn";
300 lvc.cx = 55;
301 (void)ListView_InsertColumn(hLV, 3, &lvc);
302
303 lvc.mask = LVCF_FMT | LVCF_TEXT | LVCF_WIDTH;
304 lvc.fmt = LVCFMT_CENTER;
305 lvc.pszText = L"Trace";
306 lvc.cx = 55;
307 (void)ListView_InsertColumn(hLV, 4, &lvc);
308
309 DebugChannels_FillList(hLV);
310 }
311
312 static void DebugChannels_OnNotify(HWND hDlg, LPARAM lParam)
313 {
314 NMHDR* nmh = (NMHDR*)lParam;
315
316 switch (nmh->code)
317 {
318 case NM_CLICK:
319 if (nmh->idFrom == IDC_DEBUG_CHANNELS_LIST)
320 {
321 LVHITTESTINFO lhti;
322 HWND hChannelLV;
323 HANDLE hProcess;
324 NMITEMACTIVATE* nmia = (NMITEMACTIVATE*)lParam;
325
326 hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, GetSelectedProcessId());
327 if (!hProcess) return; /* FIXME message box */
328 lhti.pt = nmia->ptAction;
329 hChannelLV = GetDlgItem(hDlg, IDC_DEBUG_CHANNELS_LIST);
330 SendMessageW(hChannelLV, LVM_SUBITEMHITTEST, 0, (LPARAM)&lhti);
331 if (nmia->iSubItem >= 1 && nmia->iSubItem <= 4)
332 {
333 WCHAR val[2];
334 WCHAR name[32];
335 unsigned bitmask = 1 << (lhti.iSubItem - 1);
336 struct cce_user user;
337
338 ListView_GetItemText(hChannelLV, lhti.iItem, 0, name, sizeof(name) / sizeof(name[0]));
339 ListView_GetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val, sizeof(val) / sizeof(val[0]));
340 user.name = name;
341 user.value = (val[0] == L'x') ? 0 : bitmask;
342 user.mask = bitmask;
343 user.done = user.notdone = 0;
344 enum_channel(hProcess, change_channel_CB, &user, FALSE);
345 if (user.done)
346 {
347 val[0] ^= (L'x' ^ L' ');
348 ListView_SetItemText(hChannelLV, lhti.iItem, lhti.iSubItem, val);
349 }
350 if (user.notdone)
351 printf("Some channel instance weren't correctly set\n");
352 }
353 CloseHandle(hProcess);
354 }
355 break;
356 }
357 }
358
359 static INT_PTR CALLBACK DebugChannelsDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
360 {
361 switch (message)
362 {
363 case WM_INITDIALOG:
364 DebugChannels_OnCreate(hDlg);
365 return TRUE;
366 case WM_COMMAND:
367 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL) {
368 EndDialog(hDlg, LOWORD(wParam));
369 return TRUE;
370 }
371 break;
372 case WM_NOTIFY:
373 DebugChannels_OnNotify(hDlg, lParam);
374 break;
375 }
376 return FALSE;
377 }
378
379 void ProcessPage_OnDebugChannels(void)
380 {
381 DialogBoxW(hInst, (LPCWSTR)IDD_DEBUG_CHANNELS_DIALOG, hMainWnd, DebugChannelsDlgProc);
382 }