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