Merging r37048, r37051, r37052, r37055 from the-real-msvc branch
[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 #include <ntdef.h>
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 *pDebugActiveProcessStop)(DWORD);
36 static BOOL (WINAPI *pDebugSetProcessKillOnExit)(BOOL);
37
38 /* Copied from the process test */
39 static void get_file_name(char* buf)
40 {
41 char path[MAX_PATH];
42
43 buf[0] = '\0';
44 GetTempPathA(sizeof(path), path);
45 GetTempFileNameA(path, "wt", 0, buf);
46 }
47
48 static void get_events(const char* name, HANDLE *start_event, HANDLE *done_event)
49 {
50 const char* basename;
51 char* event_name;
52
53 basename=strrchr(name, '\\');
54 basename=(basename ? basename+1 : name);
55 event_name=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename)+1);
56
57 sprintf(event_name, "start_%s", basename);
58 *start_event=CreateEvent(NULL, 0,0, event_name);
59 sprintf(event_name, "done_%s", basename);
60 *done_event=CreateEvent(NULL, 0,0, event_name);
61 HeapFree(GetProcessHeap(), 0, event_name);
62 }
63
64 static void save_blackbox(const char* logfile, void* blackbox, int size)
65 {
66 HANDLE hFile;
67 DWORD written;
68
69 hFile=CreateFileA(logfile, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, 0);
70 if (hFile == INVALID_HANDLE_VALUE)
71 return;
72 WriteFile(hFile, blackbox, size, &written, NULL);
73 CloseHandle(hFile);
74 }
75
76 static int load_blackbox(const char* logfile, void* blackbox, int size)
77 {
78 HANDLE hFile;
79 DWORD read;
80 BOOL ret;
81
82 hFile=CreateFileA(logfile, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, 0);
83 if (hFile == INVALID_HANDLE_VALUE)
84 {
85 ok(0, "unable to open '%s'\n", logfile);
86 return 0;
87 }
88 ret=ReadFile(hFile, blackbox, size, &read, NULL);
89 ok(read == size, "wrong size for '%s': read=%d\n", logfile, read);
90 CloseHandle(hFile);
91 return 1;
92 }
93
94 typedef struct
95 {
96 DWORD pid;
97 } crash_blackbox_t;
98
99 static void doCrash(int argc, char** argv)
100 {
101 char* p;
102
103 if (argc >= 4)
104 {
105 crash_blackbox_t blackbox;
106 blackbox.pid=GetCurrentProcessId();
107 save_blackbox(argv[3], &blackbox, sizeof(blackbox));
108 }
109
110 /* Just crash */
111 trace("child: crashing...\n");
112 p=NULL;
113 *p=0;
114 }
115
116 typedef struct
117 {
118 int argc;
119 DWORD pid;
120 BOOL debug_rc;
121 DWORD debug_err;
122 BOOL attach_rc;
123 DWORD attach_err;
124 BOOL nokill_rc;
125 DWORD nokill_err;
126 BOOL detach_rc;
127 DWORD detach_err;
128 } debugger_blackbox_t;
129
130 static void doDebugger(int argc, char** argv)
131 {
132 const char* logfile;
133 debugger_blackbox_t blackbox;
134 HANDLE start_event, done_event, debug_event;
135
136 blackbox.argc=argc;
137 logfile=(argc >= 4 ? argv[3] : NULL);
138 blackbox.pid=(argc >= 5 ? atol(argv[4]) : 0);
139
140 if (strstr(myARGV[2], "attach"))
141 {
142 blackbox.attach_rc=DebugActiveProcess(blackbox.pid);
143 if (!blackbox.attach_rc)
144 blackbox.attach_err=GetLastError();
145 }
146 else
147 blackbox.attach_rc=TRUE;
148
149 debug_event=(argc >= 6 ? (HANDLE)atol(argv[5]) : NULL);
150 if (debug_event && strstr(myARGV[2], "event"))
151 {
152 blackbox.debug_rc=SetEvent(debug_event);
153 if (!blackbox.debug_rc)
154 blackbox.debug_err=GetLastError();
155 }
156 else
157 blackbox.debug_rc=TRUE;
158
159 get_events(logfile, &start_event, &done_event);
160 if (strstr(myARGV[2], "order"))
161 {
162 trace("debugger: waiting for the start signal...\n");
163 WaitForSingleObject(start_event, INFINITE);
164 }
165
166 if (strstr(myARGV[2], "nokill"))
167 {
168 blackbox.nokill_rc=pDebugSetProcessKillOnExit(FALSE);
169 if (!blackbox.nokill_rc)
170 blackbox.nokill_err=GetLastError();
171 }
172 else
173 blackbox.nokill_rc=TRUE;
174
175 if (strstr(myARGV[2], "detach"))
176 {
177 blackbox.detach_rc=pDebugActiveProcessStop(blackbox.pid);
178 if (!blackbox.detach_rc)
179 blackbox.detach_err=GetLastError();
180 }
181 else
182 blackbox.detach_rc=TRUE;
183
184 save_blackbox(logfile, &blackbox, sizeof(blackbox));
185 trace("debugger: done debugging...\n");
186 SetEvent(done_event);
187
188 /* Just exit with a known value */
189 ExitProcess(0xdeadbeef);
190 }
191
192 static void crash_and_debug(HKEY hkey, const char* argv0, const char* dbgtasks)
193 {
194 DWORD ret;
195 HANDLE start_event, done_event;
196 char* cmd;
197 char dbglog[MAX_PATH];
198 char childlog[MAX_PATH];
199 PROCESS_INFORMATION info;
200 STARTUPINFOA startup;
201 DWORD exit_code;
202 crash_blackbox_t crash_blackbox;
203 debugger_blackbox_t dbg_blackbox;
204
205 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
206 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
207
208 get_file_name(dbglog);
209 get_events(dbglog, &start_event, &done_event);
210 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+10+strlen(dbgtasks)+1+strlen(dbglog)+34+1);
211 sprintf(cmd, "%s debugger %s %s %%ld %%ld", argv0, dbgtasks, dbglog);
212 ret=RegSetValueExA(hkey, "debugger", 0, REG_SZ, (BYTE*)cmd, strlen(cmd)+1);
213 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/debugger: ret=%d\n", ret);
214 HeapFree(GetProcessHeap(), 0, cmd);
215
216 get_file_name(childlog);
217 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+16+strlen(dbglog)+1);
218 sprintf(cmd, "%s debugger crash %s", argv0, childlog);
219
220 memset(&startup, 0, sizeof(startup));
221 startup.cb = sizeof(startup);
222 startup.dwFlags = STARTF_USESHOWWINDOW;
223 startup.wShowWindow = SW_SHOWNORMAL;
224 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
225 ok(ret, "CreateProcess: err=%d\n", GetLastError());
226 HeapFree(GetProcessHeap(), 0, cmd);
227 CloseHandle(info.hThread);
228
229 /* The process exits... */
230 trace("waiting for child exit...\n");
231 ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
232 ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
233 if (strstr(dbgtasks, "code2"))
234 {
235 /* If, after attaching to the debuggee, the debugger exits without
236 * detaching, then the debuggee gets a special exit code.
237 */
238 ok(exit_code == 0xffffffff || /* Win 9x */
239 exit_code == 0x80 || /* NT4 */
240 exit_code == STATUS_DEBUGGER_INACTIVE, /* Win >= XP */
241 "wrong exit code : %08x\n", exit_code);
242 }
243 else
244 ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x instead of STATUS_ACCESS_VIOLATION\n", exit_code);
245 CloseHandle(info.hProcess);
246
247 /* ...before the debugger */
248 if (strstr(dbgtasks, "order"))
249 ok(SetEvent(start_event), "SetEvent(start_event) failed\n");
250
251 trace("waiting for the debugger...\n");
252 ok(WaitForSingleObject(done_event, 60000) == WAIT_OBJECT_0, "Timed out waiting for the debugger\n");
253
254 assert(load_blackbox(childlog, &crash_blackbox, sizeof(crash_blackbox)));
255 assert(load_blackbox(dbglog, &dbg_blackbox, sizeof(dbg_blackbox)));
256
257 ok(dbg_blackbox.argc == 6, "wrong debugger argument count: %d\n", dbg_blackbox.argc);
258 ok(dbg_blackbox.pid == crash_blackbox.pid, "the child and debugged pids don't match: %d != %d\n", crash_blackbox.pid, dbg_blackbox.pid);
259 ok(dbg_blackbox.debug_rc, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox.debug_err);
260 ok(dbg_blackbox.attach_rc, "DebugActiveProcess(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.attach_err);
261 ok(dbg_blackbox.nokill_rc, "DebugSetProcessKillOnExit(FALSE) failed err=%d\n", dbg_blackbox.nokill_err);
262 ok(dbg_blackbox.detach_rc, "DebugActiveProcessStop(%d) failed err=%d\n", dbg_blackbox.pid, dbg_blackbox.detach_err);
263
264 assert(DeleteFileA(dbglog) != 0);
265 assert(DeleteFileA(childlog) != 0);
266 }
267
268 static void crash_and_winedbg(HKEY hkey, const char* argv0)
269 {
270 DWORD ret;
271 char* cmd;
272 PROCESS_INFORMATION info;
273 STARTUPINFOA startup;
274 DWORD exit_code;
275
276 ret=RegSetValueExA(hkey, "auto", 0, REG_SZ, (BYTE*)"1", 2);
277 ok(ret == ERROR_SUCCESS, "unable to set AeDebug/auto: ret=%d\n", ret);
278
279 cmd=HeapAlloc(GetProcessHeap(), 0, strlen(argv0)+15+1);
280 sprintf(cmd, "%s debugger crash", argv0);
281
282 memset(&startup, 0, sizeof(startup));
283 startup.cb = sizeof(startup);
284 startup.dwFlags = STARTF_USESHOWWINDOW;
285 startup.wShowWindow = SW_SHOWNORMAL;
286 ret=CreateProcessA(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &startup, &info);
287 ok(ret, "CreateProcess: err=%d\n", GetLastError());
288 HeapFree(GetProcessHeap(), 0, cmd);
289 CloseHandle(info.hThread);
290
291 trace("waiting for child exit...\n");
292 ok(WaitForSingleObject(info.hProcess, 60000) == WAIT_OBJECT_0, "Timed out waiting for the child to crash\n");
293 ok(GetExitCodeProcess(info.hProcess, &exit_code), "GetExitCodeProcess failed: err=%d\n", GetLastError());
294 ok(exit_code == STATUS_ACCESS_VIOLATION, "exit code = %08x\n", exit_code);
295 CloseHandle(info.hProcess);
296 }
297
298 static void test_ExitCode(void)
299 {
300 static const char* AeDebug="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
301 char test_exe[MAX_PATH];
302 DWORD ret;
303 HKEY hkey;
304 DWORD disposition;
305 LPBYTE auto_val=NULL;
306 DWORD auto_size, auto_type;
307 LPBYTE debugger_val=NULL;
308 DWORD debugger_size, debugger_type;
309
310 GetModuleFileNameA(GetModuleHandle(NULL), test_exe, sizeof(test_exe));
311 if (GetFileAttributes(test_exe) == INVALID_FILE_ATTRIBUTES)
312 strcat(test_exe, ".so");
313 if (GetFileAttributesA(test_exe) == INVALID_FILE_ATTRIBUTES)
314 {
315 ok(0, "could not find the test executable '%s'\n", test_exe);
316 return;
317 }
318
319 ret=RegCreateKeyExA(HKEY_LOCAL_MACHINE, AeDebug, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hkey, &disposition);
320 if (ret == ERROR_SUCCESS)
321 {
322 auto_size=0;
323 ret=RegQueryValueExA(hkey, "auto", NULL, &auto_type, NULL, &auto_size);
324 if (ret == ERROR_SUCCESS)
325 {
326 auto_val=HeapAlloc(GetProcessHeap(), 0, auto_size);
327 RegQueryValueExA(hkey, "auto", NULL, &auto_type, auto_val, &auto_size);
328 }
329
330 debugger_size=0;
331 ret=RegQueryValueExA(hkey, "debugger", NULL, &debugger_type, NULL, &debugger_size);
332 if (ret == ERROR_SUCCESS)
333 {
334 debugger_val=HeapAlloc(GetProcessHeap(), 0, debugger_size);
335 RegQueryValueExA(hkey, "debugger", NULL, &debugger_type, debugger_val, &debugger_size);
336 }
337 }
338 else if (ret == ERROR_ACCESS_DENIED)
339 {
340 skip("not enough privileges to change the debugger\n");
341 return;
342 }
343 else if (ret != ERROR_FILE_NOT_FOUND)
344 {
345 ok(0, "could not open the AeDebug key: %d\n", ret);
346 return;
347 }
348
349 if (debugger_val && debugger_type == REG_SZ &&
350 strstr((char*)debugger_val, "winedbg --auto"))
351 crash_and_winedbg(hkey, test_exe);
352
353 crash_and_debug(hkey, test_exe, "dbg,none");
354 crash_and_debug(hkey, test_exe, "dbg,event,order");
355 crash_and_debug(hkey, test_exe, "dbg,attach,event,code2");
356 if (pDebugSetProcessKillOnExit)
357 crash_and_debug(hkey, test_exe, "dbg,attach,event,nokill");
358 if (pDebugActiveProcessStop)
359 crash_and_debug(hkey, test_exe, "dbg,attach,event,detach");
360
361 if (disposition == REG_CREATED_NEW_KEY)
362 {
363 RegCloseKey(hkey);
364 RegDeleteKeyA(HKEY_LOCAL_MACHINE, AeDebug);
365 }
366 else
367 {
368 if (auto_val)
369 {
370 RegSetValueExA(hkey, "auto", 0, auto_type, auto_val, auto_size);
371 HeapFree(GetProcessHeap(), 0, auto_val);
372 }
373 else
374 RegDeleteValueA(hkey, "auto");
375 if (debugger_val)
376 {
377 RegSetValueExA(hkey, "debugger", 0, debugger_type, debugger_val, debugger_size);
378 HeapFree(GetProcessHeap(), 0, debugger_val);
379 }
380 else
381 RegDeleteValueA(hkey, "debugger");
382 RegCloseKey(hkey);
383 }
384 }
385
386 START_TEST(debugger)
387 {
388 HMODULE hdll;
389
390 hdll=GetModuleHandle("kernel32.dll");
391 pDebugActiveProcessStop=(void*)GetProcAddress(hdll, "DebugActiveProcessStop");
392 pDebugSetProcessKillOnExit=(void*)GetProcAddress(hdll, "DebugSetProcessKillOnExit");
393
394 myARGC=winetest_get_mainargs(&myARGV);
395 if (myARGC >= 3 && strcmp(myARGV[2], "crash") == 0)
396 {
397 doCrash(myARGC, myARGV);
398 }
399 else if (myARGC >= 3 && strncmp(myARGV[2], "dbg,", 4) == 0)
400 {
401 doDebugger(myARGC, myARGV);
402 }
403 else
404 {
405 test_ExitCode();
406 }
407 }