Sync to trunk (r44789)
[reactos.git] / rostests / winetests / kernel32 / debugger.c
1 /*
2 * Unit tests for the debugger facility
3 *
4 * Copyright (c) 2007 Francois Gouget for CodeWeavers
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdio.h>
22 #include <assert.h>
23
24 #define WIN32_NO_STATUS
25 #include <windows.h>
26 #include <winreg.h>
27 #include <ntndk.h>
28 #include "wine/test.h"
29
30 #ifndef STATUS_DEBUGGER_INACTIVE
31 #define STATUS_DEBUGGER_INACTIVE ((NTSTATUS) 0xC0000354)
32 #endif
33
34 static int myARGC;
35 static char** myARGV;
36
37 static BOOL (WINAPI *pCheckRemoteDebuggerPresent)(HANDLE,PBOOL);
38 static BOOL (WINAPI *pDebugActiveProcessStop)(DWORD);
39 static BOOL (WINAPI *pDebugSetProcessKillOnExit)(BOOL);
40
41 /* Copied from the process test */
42 static void get_file_name(char* buf)
43 {
44 char path[MAX_PATH];
45
46 buf[0] = '\0';
47 GetTempPathA(sizeof(path), path);
48 GetTempFileNameA(path, "wt", 0, buf);
49 }
50
51 typedef struct tag_reg_save_value
52 {
53 const char *name;
54 DWORD type;
55 BYTE *data;
56 DWORD size;
57 } reg_save_value;
58
59 static DWORD save_value(HKEY hkey, const char *value, reg_save_value *saved)
60 {
61 DWORD ret;
62 saved->name=value;
63 saved->data=0;
64 saved->size=0;
65 ret=RegQueryValueExA(hkey, value, NULL, &saved->type, NULL, &saved->size);
66 if (ret == ERROR_SUCCESS)
67 {
68 saved->data=HeapAlloc(GetProcessHeap(), 0, saved->size);
69 RegQueryValueExA(hkey, value, NULL, &saved->type, saved->data, &saved->size);
70 }
71 return ret;
72 }
73
74 static void restore_value(HKEY hkey, reg_save_value *saved)
75 {
76 if (saved->data)
77 {
78 RegSetValueExA(hkey, saved->name, 0, saved->type, saved->data, saved->size);
79 HeapFree(GetProcessHeap(), 0, saved->data);
80 }
81 else
82 RegDeleteValueA(hkey, saved->name);
83 }
84
85 static void get_events(const char* name, HANDLE *start_event, HANDLE *done_event)
86 {
87 const char* basename;
88 char* event_name;
89
90 basename=strrchr(name, '\\');
91 basename=(basename ? basename+1 : name);
92 event_name=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename)+1);
93
94 sprintf(event_name, "start_%s", basename);
95 *start_event=CreateEvent(NULL, 0,0, event_name);
96 sprintf(event_name, "done_%s", basename);
97 *done_event=CreateEvent(NULL, 0,0, event_name);
98 HeapFree(GetProcessHeap(), 0, event_name);
99 }
100
101 static void save_blackbox(const char* logfile, void* blackbox, int size)
102 {
103 HANDLE hFile;
104 DWORD written;
105
106 hFile=CreateFileA(logfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
107 if (hFile == INVALID_HANDLE_VALUE)
108 return;
109 WriteFile(hFile, blackbox, size, &written, NULL);
110 CloseHandle(hFile);
111 }
112
113 static int load_blackbox(const char* logfile, void* blackbox, int size)
114 {
115 HANDLE hFile;
116 DWORD read;
117 BOOL ret;
118
119 hFile=CreateFileA(logfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
120 if (hFile == INVALID_HANDLE_VALUE)
121 {
122 ok(0, "unable to open '%s'\n", logfile);
123 return 0;
124 }
125 ret=ReadFile(hFile, blackbox, size, &read, NULL);
126 ok(read == size, "wrong size for '%s': read=%d\n", logfile, read);
127 CloseHandle(hFile);
128 return 1;
129 }
130
131 typedef struct
132 {
133 DWORD pid;
134 } crash_blackbox_t;
135
136 static void doCrash(int argc, char** argv)
137 {
138 char* p;
139
140 if (argc >= 4)
141 {
142 crash_blackbox_t blackbox;
143 blackbox.pid=GetCurrentProcessId();
144 save_blackbox(argv[3], &blackbox, sizeof(blackbox));
145 }
146
147 /* Just crash */
148 trace("child: crashing...\n");
149 p=NULL;
150 *p=0;
151 }
152
153 typedef struct
154 {
155 int argc;
156 DWORD pid;
157 BOOL debug_rc;
158 DWORD debug_err;
159 BOOL attach_rc;
160 DWORD attach_err;
161 BOOL nokill_rc;
162 DWORD nokill_err;
163 BOOL detach_rc;
164 DWORD detach_err;
165 } debugger_blackbox_t;
166
167 static void doDebugger(int argc, char** argv)
168 {
169 const char* logfile;
170 debugger_blackbox_t blackbox;
171 HANDLE start_event = 0, done_event = 0, debug_event;
172
173 blackbox.argc=argc;
174 logfile=(argc >= 4 ? argv[3] : NULL);
175 blackbox.pid=(argc >= 5 ? atol(argv[4]) : 0);
176
177 blackbox.attach_err=0;
178 if (strstr(myARGV[2], "attach"))
179 {
180 blackbox.attach_rc=DebugActiveProcess(blackbox.pid);
181 if (!blackbox.attach_rc)
182 blackbox.attach_err=GetLastError();
183 }
184 else
185 blackbox.attach_rc=TRUE;
186
187 debug_event=(argc >= 6 ? (HANDLE)(INT_PTR)atol(argv[5]) : NULL);
188 blackbox.debug_err=0;
189 if (debug_event && strstr(myARGV[2], "event"))
190 {
191 blackbox.debug_rc=SetEvent(debug_event);
192 if (!blackbox.debug_rc)
193 blackbox.debug_err=GetLastError();
194 }
195 else
196 blackbox.debug_rc=TRUE;
197
198 if (logfile)
199 {
200 get_events(logfile, &start_event, &done_event);
201 }
202
203 if (strstr(myARGV[2], "order"))
204 {
205 trace("debugger: waiting for the start signal...\n");
206 WaitForSingleObject(start_event, INFINITE);
207 }
208
209 blackbox.nokill_err=0;
210 if (strstr(myARGV[2], "nokill"))
211 {
212 blackbox.nokill_rc=pDebugSetProcessKillOnExit(FALSE);
213 if (!blackbox.nokill_rc)
214 blackbox.nokill_err=GetLastError();
215 }
216 else
217 blackbox.nokill_rc=TRUE;
218
219 blackbox.detach_err=0;
220 if (strstr(myARGV[2], "detach"))
221 {
222 blackbox.detach_rc=pDebugActiveProcessStop(blackbox.pid);
223 if (!blackbox.detach_rc)
224 blackbox.detach_err=GetLastError();
225 }
226 else
227 blackbox.detach_rc=TRUE;
228
229 if (logfile)
230 {
231 save_blackbox(logfile, &blackbox, sizeof(blackbox));
232 }
233 trace("debugger: done debugging...\n");
234 SetEvent(done_event);
235
236 /* Just exit with a known value */
237 ExitProcess(0xdeadbeef);
238 }
239
240 static void crash_and_debug(HKEY hkey, const char* argv0, const char* dbgtasks)
241 {
242 DWORD ret;
243 HANDLE start_event, done_event;
244 char* cmd;
245 char dbglog[MAX_PATH];
246 char childlog[MAX_PATH];
247 PROCESS_INFORMATION info;
248 STARTUPINFOA startup;
249 DWORD exit_code;
250 crash_blackbox_t crash_blackbox;
251 debugger_blackbox_t dbg_blackbox;
252
253 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
254 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
255
256 get_file_name(dbglog);
257 get_events(dbglog, &start_event, &done_event);
258 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(dbgtasks)+1+strlen(dbglog)+34+1);
259 sprintf(cmd, "%s debugger %s %s %%ld %%ld", argv0, dbgtasks, dbglog);
260 ret=RegSetValueExA(hkey, "debugger", 0, REG_SZ, (BYTE*)cmd, strlen(cmd)+1);
261 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/debugger: ret=%d\n", ret);
262 HeapFree(GetProcessHeap(), 0, cmd);
263
264 get_file_name(childlog);
265 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+16+strlen(dbglog)+1);
266 sprintf(cmd, "%s debugger crash %s", argv0, childlog);
267
268 memset(&startup, 0, sizeof(startup));
269 startup.cb = sizeof(startup);
270 startup.dwFlags = STARTF_USESHOWWINDOW;
271 startup.wShowWindow = SW_SHOWNORMAL;
272 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
273 ok(ret, "CreateProcess: err=%d\n", GetLastError());
274 HeapFree(GetProcessHeap(), 0, cmd);
275 CloseHandle(info.hThread);
276
277 /* The process exits... */
278 trace("waiting for child exit...\n");
279 ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
280 ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
281 if (strstr(dbgtasks, "code2"))
282 {
283 /* If, after attaching to the debuggee, the debugger exits without
284 * detaching, then the debuggee gets a special exit code.
285 */
286 ok(exit_code == STATUS_DEBUGGER_INACTIVE ||
287 broken(exit_code == STATUS_ACCESS_VIOLATION) || /* Intermittent Vista+ */
288 broken(exit_code == 0xffffffff) || /* Win9x */
289 broken(exit_code == WAIT_ABANDONED), /* NT4, W2K */
290 "wrong exit code : %08x\n", exit_code);
291 }
292 else
293 ok(exit_code == STATUS_ACCESS_VIOLATION ||
294 broken(exit_code == WAIT_ABANDONED) || /* NT4, W2K, W2K3 */
295 broken(exit_code == 0xffffffff), /* Win9x, WinME */
296 "wrong exit code : %08x\n", exit_code);
297 CloseHandle(info.hProcess);
298
299 /* ...before the debugger */
300 if (strstr(dbgtasks, "order"))
301 ok(SetEvent(start_event), "SetEvent(start_event) failed\n");
302
303 trace("waiting for the debugger...\n");
304 ok(WaitForSingleObject(done_event, 60000) == WAIT_OBJECT_0, "Timed out waiting for the debugger\n");
305
306 assert(load_blackbox(childlog, &crash_blackbox, sizeof(crash_blackbox)));
307 assert(load_blackbox(dbglog, &dbg_blackbox, sizeof(dbg_blackbox)));
308
309 ok(dbg_blackbox.argc == 6, "wrong debugger argument count: %d\n", dbg_blackbox.argc);
310 ok(dbg_blackbox.pid == crash_blackbox.pid, "the child and debugged pids don't match: %d != %d\n", crash_blackbox.pid, dbg_blackbox.pid);
311 ok(dbg_blackbox.debug_rc, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox.debug_err);
312 ok(dbg_blackbox.attach_rc, "DebugActiveProcess(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.attach_err);
313 ok(dbg_blackbox.nokill_rc, "DebugSetProcessKillOnExit(FALSE) failed err=%d\n", dbg_blackbox.nokill_err);
314 ok(dbg_blackbox.detach_rc, "DebugActiveProcessStop(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.detach_err);
315
316 assert(DeleteFileA(dbglog) != 0);
317 assert(DeleteFileA(childlog) != 0);
318 }
319
320 static void crash_and_winedbg(HKEY hkey, const char* argv0)
321 {
322 DWORD ret;
323 char* cmd;
324 PROCESS_INFORMATION info;
325 STARTUPINFOA startup;
326 DWORD exit_code;
327
328 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
329 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
330
331 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+15+1);
332 sprintf(cmd, "%s debugger crash", argv0);
333
334 memset(&startup, 0, sizeof(startup));
335 startup.cb = sizeof(startup);
336 startup.dwFlags = STARTF_USESHOWWINDOW;
337 startup.wShowWindow = SW_SHOWNORMAL;
338 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
339 ok(ret, "CreateProcess: err=%d\n", GetLastError());
340 HeapFree(GetProcessHeap(), 0, cmd);
341 CloseHandle(info.hThread);
342
343 trace("waiting for child exit...\n");
344 ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
345 ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
346 ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x\n", exit_code);
347 CloseHandle(info.hProcess);
348 }
349
350 static void test_ExitCode(void)
351 {
352 static const char* AeDebug="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
353 static const char* WineDbg="Software\\Wine\\WineDbg";
354 char test_exe[MAX_PATH];
355 DWORD ret;
356 HKEY hkey;
357 DWORD disposition;
358 reg_save_value auto_value;
359 reg_save_value debugger_value;
360
361 GetModuleFileNameA(GetModuleHandle(NULL), test_exe, sizeof(test_exe));
362 if (GetFileAttributes(test_exe) == INVALID_FILE_ATTRIBUTES)
363 strcat(test_exe, ".so");
364 if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
365 {
366 ok(0, "could not find the test executable '%s'\n", test_exe);
367 return;
368 }
369
370 ret=RegCreateKeyExA(HKEY_LOCAL_MACHINE, AeDebug, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition);
371 if (ret == ERROR_SUCCESS)
372 {
373 save_value(hkey, "auto", &auto_value);
374 save_value(hkey, "debugger", &debugger_value);
375 trace("HKLM\\%s\\debugger is set to '%s'\n", AeDebug, debugger_value.data);
376 }
377 else if (ret == ERROR_ACCESS_DENIED)
378 {
379 skip("not enough privileges to change the debugger\n");
380 return;
381 }
382 else if (ret != ERROR_FILE_NOT_FOUND)
383 {
384 ok(0, "could not open the AeDebug key: %d\n", ret);
385 return;
386 }
387
388 if (debugger_value.data && debugger_value.type == REG_SZ &&
389 strstr((char*)debugger_value.data, "winedbg --auto"))
390 {
391 HKEY hkeyWinedbg;
392 ret=RegCreateKeyA(HKEY_CURRENT_USER, WineDbg, &hkeyWinedbg);
393 if (ret == ERROR_SUCCESS)
394 {
395 static DWORD zero;
396 reg_save_value crash_dlg_value;
397 save_value(hkeyWinedbg, "ShowCrashDialog", &crash_dlg_value);
398 RegSetValueExA(hkeyWinedbg, "ShowCrashDialog", 0, REG_DWORD, (BYTE *)&zero, sizeof(DWORD));
399 crash_and_winedbg(hkey, test_exe);
400 restore_value(hkeyWinedbg, &crash_dlg_value);
401 RegCloseKey(hkeyWinedbg);
402 }
403 else
404 ok(0, "Couldn't access WineDbg Key - error %u\n", ret);
405 }
406
407 if (winetest_interactive)
408 /* Since the debugging process never sets the debug event, it isn't recognized
409 as a valid debugger and, after the debugger exits, Windows will show a dialog box
410 asking the user what to do */
411 crash_and_debug(hkey, test_exe, "dbg,none");
412 else
413 skip("\"none\" debugger test needs user interaction\n");
414 crash_and_debug(hkey, test_exe, "dbg,event,order");
415 crash_and_debug(hkey, test_exe, "dbg,attach,event,code2");
416 if (pDebugSetProcessKillOnExit)
417 crash_and_debug(hkey, test_exe, "dbg,attach,event,nokill");
418 else
419 win_skip("DebugSetProcessKillOnExit is not available\n");
420 if (pDebugActiveProcessStop)
421 crash_and_debug(hkey, test_exe, "dbg,attach,event,detach");
422 else
423 win_skip("DebugActiveProcessStop is not available\n");
424
425 if (disposition == REG_CREATED_NEW_KEY)
426 {
427 RegCloseKey(hkey);
428 RegDeleteKeyA(HKEY_LOCAL_MACHINE, AeDebug);
429 }
430 else
431 {
432 restore_value(hkey, &auto_value);
433 restore_value(hkey, &debugger_value);
434 RegCloseKey(hkey);
435 }
436 }
437
438 static void test_RemoteDebugger(void)
439 {
440 BOOL bret, present;
441 if(!pCheckRemoteDebuggerPresent)
442 {
443 win_skip("CheckRemoteDebuggerPresent is not available\n");
444 return;
445 }
446 present = TRUE;
447 SetLastError(0xdeadbeef);
448 bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),&present);
449 ok(bret , "expected CheckRemoteDebuggerPresent to succeed\n");
450 ok(0xdeadbeef == GetLastError(),
451 "expected error to be unchanged, got %d/%x\n",GetLastError(), GetLastError());
452
453 present = TRUE;
454 SetLastError(0xdeadbeef);
455 bret = pCheckRemoteDebuggerPresent(NULL,&present);
456 ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
457 ok(present, "expected parameter to be unchanged\n");
458 ok(ERROR_INVALID_PARAMETER == GetLastError(),
459 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
460
461 SetLastError(0xdeadbeef);
462 bret = pCheckRemoteDebuggerPresent(GetCurrentProcess(),NULL);
463 ok(!bret , "expected CheckRemoteDebuggerPresent to fail\n");
464 ok(ERROR_INVALID_PARAMETER == GetLastError(),
465 "expected error ERROR_INVALID_PARAMETER, got %d/%x\n",GetLastError(), GetLastError());
466 }
467
468 START_TEST(debugger)
469 {
470 HMODULE hdll;
471
472 hdll=GetModuleHandle("kernel32.dll");
473 pCheckRemoteDebuggerPresent=(void*)GetProcAddress(hdll, "CheckRemoteDebuggerPresent");
474 pDebugActiveProcessStop=(void*)GetProcAddress(hdll, "DebugActiveProcessStop");
475 pDebugSetProcessKillOnExit=(void*)GetProcAddress(hdll, "DebugSetProcessKillOnExit");
476
477 myARGC=winetest_get_mainargs(&myARGV);
478 if (myARGC >= 3 && strcmp(myARGV[2], "crash") == 0)
479 {
480 doCrash(myARGC, myARGV);
481 }
482 else if (myARGC >= 3 && strncmp(myARGV[2], "dbg,", 4) == 0)
483 {
484 doDebugger(myARGC, myARGV);
485 }
486 else
487 {
488 test_ExitCode();
489 test_RemoteDebugger();
490 }
491 }