kernel32/tests: Test the debugger startup and crashed processes exit code.
[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 static int    myARGC;
29 static char** myARGV;
30
31
32 /* Copied from the process test */
33 static void get_file_name(char* buf)
34 {
35     char path[MAX_PATH];
36
37     buf[0] = '\0';
38     GetTempPathA(sizeof(path), path);
39     GetTempFileNameA(path, "wt", 0, buf);
40 }
41
42 static void get_events(const char* name, HANDLE *start_event, HANDLE *done_event)
43 {
44     const char* basename;
45     char* event_name;
46
47     basename=strrchr(name, '\\');
48     basename=(basename ? basename+1 : name);
49     event_name=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename)+1);
50
51     sprintf(event_name, "start_%s", basename);
52     *start_event=CreateEvent(NULL, 0,0, event_name);
53     sprintf(event_name, "done_%s", basename);
54     *done_event=CreateEvent(NULL, 0,0, event_name);
55     HeapFree(GetProcessHeap(), 0, event_name);
56 }
57
58 static void log_pid(const char* logfile, DWORD pid)
59 {
60     HANDLE hFile;
61     DWORD written;
62
63     hFile=CreateFileA(logfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
64     if (hFile == INVALID_HANDLE_VALUE)
65         return;
66     WriteFile(hFile, &pid, sizeof(pid), &written, NULL);
67     CloseHandle(hFile);
68 }
69
70 static DWORD get_logged_pid(const char* logfile)
71 {
72     HANDLE hFile;
73     DWORD pid, read;
74     BOOL ret;
75
76     hFile=CreateFileA(logfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
77     if (hFile == INVALID_HANDLE_VALUE)
78     {
79         ok(0, "unable to open '%s'\n", logfile);
80         return 0;
81     }
82     pid=0;
83     read=sizeof(pid);
84     ret=ReadFile(hFile, &pid, sizeof(pid), &read, NULL);
85     ok(read == sizeof(pid), "wrong size for '%s': read=%d\n", logfile, read);
86     CloseHandle(hFile);
87     return pid;
88 }
89
90 static void doCrash(int argc,  char** argv)
91 {
92     char* p;
93     const char* logfile;
94
95     logfile=(argc >= 4 ? argv[3] : NULL);
96     log_pid(logfile, GetCurrentProcessId());
97
98     /* Just crash */
99     trace("child: crashing...\n");
100     p=NULL;
101     *p=0;
102 }
103
104 static void doDebugger(int argc, char** argv)
105 {
106     const char* logfile;
107     HANDLE start_event, done_event, debug_event;
108     DWORD pid;
109
110     ok(argc == 6, "wrong debugger argument count: %d\n", argc);
111     logfile=(argc >= 4 ? argv[3] : NULL);
112     pid=(argc >= 5 ? atol(argv[4]) : 0);
113     debug_event=(argc >= 6 ? (HANDLE)atol(argv[5]) : NULL);
114     if (debug_event && strcmp(myARGV[2], "dbgnoevent") != 0)
115     {
116         ok(SetEvent(debug_event), "debugger: SetEvent(debug_event) failed\n");
117     }
118
119     log_pid(logfile, pid);
120     get_events(logfile, &start_event, &done_event);
121     if (strcmp(myARGV[2], "dbgnoevent") != 0)
122     {
123         trace("debugger: waiting for the start signal...\n");
124         WaitForSingleObject(start_event, INFINITE);
125     }
126
127     ok(SetEvent(done_event), "debugger: SetEvent(done_event) failed\n");
128     trace("debugger: done debugging...\n");
129
130     /* Just exit with a known value */
131     ExitProcess(0xdeadbeef);
132 }
133
134 static void crash_and_debug(HKEY hkey, const char* argv0, const char* debugger)
135 {
136     DWORD ret;
137     HANDLE start_event, done_event;
138     char* cmd;
139     char dbglog[MAX_PATH];
140     char childlog[MAX_PATH];
141     PROCESS_INFORMATION info;
142     STARTUPINFOA startup;
143     DWORD exit_code;
144     DWORD pid1, pid2;
145
146     ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
147     ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
148
149     get_file_name(dbglog);
150     get_events(dbglog, &start_event, &done_event);
151     cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(debugger)+1+strlen(dbglog)+34+1);
152     sprintf(cmd, "%s debugger %s %s %%ld %%ld", argv0, debugger, dbglog);
153     ret=RegSetValueExA(hkey, "debugger", 0, REG_SZ, (BYTE*)cmd, strlen(cmd)+1);
154     ok(ret == ERROR_SUCCESS, "unable to set AeDebug/debugger: ret=%d\n", ret);
155     HeapFree(GetProcessHeap(), 0, cmd);
156
157     get_file_name(childlog);
158     cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+16+strlen(dbglog)+1);
159     sprintf(cmd, "%s debugger crash %s", argv0, childlog);
160
161     memset(&startup, 0, sizeof(startup));
162     startup.cb = sizeof(startup);
163     startup.dwFlags = STARTF_USESHOWWINDOW;
164     startup.wShowWindow = SW_SHOWNORMAL;
165     ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
166     ok(ret, "CreateProcess: err=%d\n", GetLastError());
167     HeapFree(GetProcessHeap(), 0, cmd);
168     CloseHandle(info.hThread);
169
170     /* The process exits... */
171     trace("waiting for child exit...\n");
172     ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
173     ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
174     ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x\n", exit_code);
175     CloseHandle(info.hProcess);
176
177     /* ...before the debugger */
178     if (strcmp(debugger, "dbgnoevent") != 0)
179         ok(SetEvent(start_event), "SetEvent(start_event) failed\n");
180
181     trace("waiting for the debugger...\n");
182     ok(WaitForSingleObject(done_event, 60000) == WAIT_OBJECT_0, "Timed out waiting for the debugger\n");
183
184     pid1=get_logged_pid(dbglog);
185     pid2=get_logged_pid(childlog);
186     ok(pid1 == pid2, "the child and debugged pids don't match: %d != %d\n", pid1, pid2);
187     assert(DeleteFileA(dbglog) != 0);
188     assert(DeleteFileA(childlog) != 0);
189 }
190
191 static void test_ExitCode(void)
192 {
193     static const char* AeDebug="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
194     char test_exe[MAX_PATH];
195     DWORD ret;
196     HKEY hkey;
197     DWORD disposition;
198     LPBYTE auto_val=NULL;
199     DWORD auto_size, auto_type;
200     LPBYTE debugger_val=NULL;
201     DWORD debugger_size, debugger_type;
202
203     GetModuleFileNameA(GetModuleHandle(NULL), test_exe, sizeof(test_exe));
204     if (GetFileAttributes(test_exe) == INVALID_FILE_ATTRIBUTES)
205         strcat(test_exe, ".so");
206     if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
207     {
208         ok(0, "could not find the test executable '%s'\n", test_exe);
209         return;
210     }
211
212     ret=RegCreateKeyExA(HKEY_LOCAL_MACHINE, AeDebug, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition);
213     if (ret == ERROR_SUCCESS)
214     {
215         auto_size=0;
216         ret=RegQueryValueExA(hkey, "auto", NULL, &auto_type, NULL, &auto_size);
217         if (ret == ERROR_SUCCESS)
218         {
219             auto_val=HeapAlloc(GetProcessHeap(), 0, auto_size);
220             RegQueryValueExA(hkey, "auto", NULL, &auto_type, auto_val, &auto_size);
221         }
222
223         debugger_size=0;
224         ret=RegQueryValueExA(hkey, "debugger", NULL, &debugger_type, NULL, &debugger_size);
225         if (ret == ERROR_SUCCESS)
226         {
227             debugger_val=HeapAlloc(GetProcessHeap(), 0, debugger_size);
228             RegQueryValueExA(hkey, "debugger", NULL, &debugger_type, debugger_val, &debugger_size);
229         }
230     }
231     else if (ret == ERROR_ACCESS_DENIED)
232     {
233         skip("not enough privileges to change the debugger\n");
234         return;
235     }
236     else if (ret != ERROR_FILE_NOT_FOUND)
237     {
238         ok(0, "could not open the AeDebug key: %d\n", ret);
239         return;
240     }
241
242     ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
243     ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
244     crash_and_debug(hkey, test_exe, "dbgevent");
245     crash_and_debug(hkey, test_exe, "dbgnoevent");
246
247     if (disposition == REG_CREATED_NEW_KEY)
248     {
249         RegCloseKey(hkey);
250         RegDeleteKeyA(HKEY_LOCAL_MACHINE, AeDebug);
251     }
252     else
253     {
254         if (auto_val)
255         {
256             RegSetValueExA(hkey, "auto", 0, auto_type, auto_val, auto_size);
257             HeapFree(GetProcessHeap(), 0, auto_val);
258         }
259         else
260             RegDeleteValueA(hkey, "auto");
261         if (debugger_val)
262         {
263             RegSetValueExA(hkey, "debugger", 0, debugger_type, debugger_val, debugger_size);
264             HeapFree(GetProcessHeap(), 0, debugger_val);
265         }
266         else
267             RegDeleteValueA(hkey, "debugger");
268         RegCloseKey(hkey);
269     }
270 }
271
272 START_TEST(debugger)
273 {
274
275     myARGC=winetest_get_mainargs(&myARGV);
276
277     if (myARGC >= 3 && strcmp(myARGV[2], "crash") == 0)
278     {
279         doCrash(myARGC, myARGV);
280     }
281     else if (myARGC >= 3 &&
282              (strcmp(myARGV[2], "dbgevent") == 0 ||
283               strcmp(myARGV[2], "dbgnoevent") == 0))
284     {
285         doDebugger(myARGC, myARGV);
286     }
287     else
288     {
289         test_ExitCode();
290     }
291 }