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