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