2 * Unit tests for the debugger facility
4 * Copyright (c) 2007 Francois Gouget for CodeWeavers
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.
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.
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
26 #include "wine/test.h"
32 /* Copied from the process test */
33 static void get_file_name(char* buf
)
38 GetTempPathA(sizeof(path
), path
);
39 GetTempFileNameA(path
, "wt", 0, buf
);
42 static void get_events(const char* name
, HANDLE
*start_event
, HANDLE
*done_event
)
47 basename
=strrchr(name
, '\\');
48 basename
=(basename
? basename
+1 : name
);
49 event_name
=HeapAlloc(GetProcessHeap(), 0, 6+strlen(basename
)+1);
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
);
58 static void save_blackbox(const char* logfile
, void* blackbox
, int size
)
63 hFile
=CreateFileA(logfile
, GENERIC_WRITE
, 0, NULL
, CREATE_ALWAYS
, 0, 0);
64 if (hFile
== INVALID_HANDLE_VALUE
)
66 WriteFile(hFile
, blackbox
, size
, &written
, NULL
);
70 static int load_blackbox(const char* logfile
, void* blackbox
, int size
)
76 hFile
=CreateFileA(logfile
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, 0, 0);
77 if (hFile
== INVALID_HANDLE_VALUE
)
79 ok(0, "unable to open '%s'\n", logfile
);
82 ret
=ReadFile(hFile
, blackbox
, size
, &read
, NULL
);
83 ok(read
== size
, "wrong size for '%s': read=%d\n", logfile
, read
);
93 static void doCrash(int argc
, char** argv
)
99 crash_blackbox_t blackbox
;
100 blackbox
.pid
=GetCurrentProcessId();
101 save_blackbox(argv
[3], &blackbox
, sizeof(blackbox
));
105 trace("child: crashing...\n");
116 } debugger_blackbox_t
;
118 static void doDebugger(int argc
, char** argv
)
121 debugger_blackbox_t blackbox
;
122 HANDLE start_event
, done_event
, debug_event
;
125 logfile
=(argc
>= 4 ? argv
[3] : NULL
);
126 blackbox
.pid
=(argc
>= 5 ? atol(argv
[4]) : 0);
127 debug_event
=(argc
>= 6 ? (HANDLE
)atol(argv
[5]) : NULL
);
128 if (debug_event
&& strcmp(myARGV
[2], "dbgnoevent") != 0)
130 blackbox
.debug_rc
=SetEvent(debug_event
);
131 if (!blackbox
.debug_rc
)
132 blackbox
.debug_err
=GetLastError();
135 blackbox
.debug_rc
=TRUE
;
137 get_events(logfile
, &start_event
, &done_event
);
138 if (strcmp(myARGV
[2], "dbgnoevent") != 0)
140 trace("debugger: waiting for the start signal...\n");
141 WaitForSingleObject(start_event
, INFINITE
);
144 save_blackbox(logfile
, &blackbox
, sizeof(blackbox
));
145 trace("debugger: done debugging...\n");
146 SetEvent(done_event
);
148 /* Just exit with a known value */
149 ExitProcess(0xdeadbeef);
152 static void crash_and_debug(HKEY hkey
, const char* argv0
, const char* debugger
)
155 HANDLE start_event
, done_event
;
157 char dbglog
[MAX_PATH
];
158 char childlog
[MAX_PATH
];
159 PROCESS_INFORMATION info
;
160 STARTUPINFOA startup
;
162 crash_blackbox_t crash_blackbox
;
163 debugger_blackbox_t dbg_blackbox
;
165 ret
=RegSetValueExA(hkey
, "auto", 0, REG_SZ
, (BYTE
*)"1", 2);
166 ok(ret
== ERROR_SUCCESS
, "unable to set AeDebug/auto: ret=%d\n", ret
);
168 get_file_name(dbglog
);
169 get_events(dbglog
, &start_event
, &done_event
);
170 cmd
=HeapAlloc(GetProcessHeap(), 0, strlen(argv0
)+10+strlen(debugger
)+1+strlen(dbglog
)+34+1);
171 sprintf(cmd
, "%s debugger %s %s %%ld %%ld", argv0
, debugger
, dbglog
);
172 ret
=RegSetValueExA(hkey
, "debugger", 0, REG_SZ
, (BYTE
*)cmd
, strlen(cmd
)+1);
173 ok(ret
== ERROR_SUCCESS
, "unable to set AeDebug/debugger: ret=%d\n", ret
);
174 HeapFree(GetProcessHeap(), 0, cmd
);
176 get_file_name(childlog
);
177 cmd
=HeapAlloc(GetProcessHeap(), 0, strlen(argv0
)+16+strlen(dbglog
)+1);
178 sprintf(cmd
, "%s debugger crash %s", argv0
, childlog
);
180 memset(&startup
, 0, sizeof(startup
));
181 startup
.cb
= sizeof(startup
);
182 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
183 startup
.wShowWindow
= SW_SHOWNORMAL
;
184 ret
=CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
185 ok(ret
, "CreateProcess: err=%d\n", GetLastError());
186 HeapFree(GetProcessHeap(), 0, cmd
);
187 CloseHandle(info
.hThread
);
189 /* The process exits... */
190 trace("waiting for child exit...\n");
191 ok(WaitForSingleObject(info
.hProcess
, 60000) == WAIT_OBJECT_0
, "Timed out waiting for the child to crash\n");
192 ok(GetExitCodeProcess(info
.hProcess
, &exit_code
), "GetExitCodeProcess failed: err=%d\n", GetLastError());
193 ok(exit_code
== STATUS_ACCESS_VIOLATION
, "exit code = %08x\n", exit_code
);
194 CloseHandle(info
.hProcess
);
196 /* ...before the debugger */
197 if (strcmp(debugger
, "dbgnoevent") != 0)
198 ok(SetEvent(start_event
), "SetEvent(start_event) failed\n");
200 trace("waiting for the debugger...\n");
201 ok(WaitForSingleObject(done_event
, 60000) == WAIT_OBJECT_0
, "Timed out waiting for the debugger\n");
203 assert(load_blackbox(childlog
, &crash_blackbox
, sizeof(crash_blackbox
)));
204 assert(load_blackbox(dbglog
, &dbg_blackbox
, sizeof(dbg_blackbox
)));
206 ok(dbg_blackbox
.argc
== 6, "wrong debugger argument count: %d\n", dbg_blackbox
.argc
);
207 ok(dbg_blackbox
.pid
== crash_blackbox
.pid
, "the child and debugged pids don't match: %d != %d\n", crash_blackbox
.pid
, dbg_blackbox
.pid
);
208 ok(dbg_blackbox
.debug_rc
, "debugger: SetEvent(debug_event) failed err=%d\n", dbg_blackbox
.debug_err
);
210 assert(DeleteFileA(dbglog
) != 0);
211 assert(DeleteFileA(childlog
) != 0);
214 static void crash_and_winedbg(HKEY hkey
, const char* argv0
)
218 PROCESS_INFORMATION info
;
219 STARTUPINFOA startup
;
222 ret
=RegSetValueExA(hkey
, "auto", 0, REG_SZ
, (BYTE
*)"1", 2);
223 ok(ret
== ERROR_SUCCESS
, "unable to set AeDebug/auto: ret=%d\n", ret
);
225 cmd
=HeapAlloc(GetProcessHeap(), 0, strlen(argv0
)+15+1);
226 sprintf(cmd
, "%s debugger crash", argv0
);
228 memset(&startup
, 0, sizeof(startup
));
229 startup
.cb
= sizeof(startup
);
230 startup
.dwFlags
= STARTF_USESHOWWINDOW
;
231 startup
.wShowWindow
= SW_SHOWNORMAL
;
232 ret
=CreateProcessA(NULL
, cmd
, NULL
, NULL
, FALSE
, 0, NULL
, NULL
, &startup
, &info
);
233 ok(ret
, "CreateProcess: err=%d\n", GetLastError());
234 HeapFree(GetProcessHeap(), 0, cmd
);
235 CloseHandle(info
.hThread
);
237 trace("waiting for child exit...\n");
238 ok(WaitForSingleObject(info
.hProcess
, 60000) == WAIT_OBJECT_0
, "Timed out waiting for the child to crash\n");
239 ok(GetExitCodeProcess(info
.hProcess
, &exit_code
), "GetExitCodeProcess failed: err=%d\n", GetLastError());
240 ok(exit_code
== STATUS_ACCESS_VIOLATION
, "exit code = %08x\n", exit_code
);
241 CloseHandle(info
.hProcess
);
244 static void test_ExitCode(void)
246 static const char* AeDebug
="Software\\Microsoft\\Windows NT\\CurrentVersion\\AeDebug";
247 char test_exe
[MAX_PATH
];
251 LPBYTE auto_val
=NULL
;
252 DWORD auto_size
, auto_type
;
253 LPBYTE debugger_val
=NULL
;
254 DWORD debugger_size
, debugger_type
;
256 GetModuleFileNameA(GetModuleHandle(NULL
), test_exe
, sizeof(test_exe
));
257 if (GetFileAttributes(test_exe
) == INVALID_FILE_ATTRIBUTES
)
258 strcat(test_exe
, ".so");
259 if (GetFileAttributesA(test_exe
) == INVALID_FILE_ATTRIBUTES
)
261 ok(0, "could not find the test executable '%s'\n", test_exe
);
265 ret
=RegCreateKeyExA(HKEY_LOCAL_MACHINE
, AeDebug
, 0, NULL
, REG_OPTION_NON_VOLATILE
, KEY_ALL_ACCESS
, NULL
, &hkey
, &disposition
);
266 if (ret
== ERROR_SUCCESS
)
269 ret
=RegQueryValueExA(hkey
, "auto", NULL
, &auto_type
, NULL
, &auto_size
);
270 if (ret
== ERROR_SUCCESS
)
272 auto_val
=HeapAlloc(GetProcessHeap(), 0, auto_size
);
273 RegQueryValueExA(hkey
, "auto", NULL
, &auto_type
, auto_val
, &auto_size
);
277 ret
=RegQueryValueExA(hkey
, "debugger", NULL
, &debugger_type
, NULL
, &debugger_size
);
278 if (ret
== ERROR_SUCCESS
)
280 debugger_val
=HeapAlloc(GetProcessHeap(), 0, debugger_size
);
281 RegQueryValueExA(hkey
, "debugger", NULL
, &debugger_type
, debugger_val
, &debugger_size
);
284 else if (ret
== ERROR_ACCESS_DENIED
)
286 skip("not enough privileges to change the debugger\n");
289 else if (ret
!= ERROR_FILE_NOT_FOUND
)
291 ok(0, "could not open the AeDebug key: %d\n", ret
);
295 if (debugger_val
&& debugger_type
== REG_SZ
&&
296 strstr((char*)debugger_val
, "winedbg --auto"))
297 crash_and_winedbg(hkey
, test_exe
);
299 crash_and_debug(hkey
, test_exe
, "dbgevent");
300 crash_and_debug(hkey
, test_exe
, "dbgnoevent");
302 if (disposition
== REG_CREATED_NEW_KEY
)
305 RegDeleteKeyA(HKEY_LOCAL_MACHINE
, AeDebug
);
311 RegSetValueExA(hkey
, "auto", 0, auto_type
, auto_val
, auto_size
);
312 HeapFree(GetProcessHeap(), 0, auto_val
);
315 RegDeleteValueA(hkey
, "auto");
318 RegSetValueExA(hkey
, "debugger", 0, debugger_type
, debugger_val
, debugger_size
);
319 HeapFree(GetProcessHeap(), 0, debugger_val
);
322 RegDeleteValueA(hkey
, "debugger");
330 myARGC
=winetest_get_mainargs(&myARGV
);
332 if (myARGC
>= 3 && strcmp(myARGV
[2], "crash") == 0)
334 doCrash(myARGC
, myARGV
);
336 else if (myARGC
>= 3 &&
337 (strcmp(myARGV
[2], "dbgevent") == 0 ||
338 strcmp(myARGV
[2], "dbgnoevent") == 0))
340 doDebugger(myARGC
, myARGV
);