user32/tests: Fix a couple of dimension checks on W2k3 and Vista.
[wine/multimedia.git] / dlls / kernel32 / debugger.c
blobb83647caf069278f3f5c5fcd61570bbb0b708766
1 /*
2 * Win32 debugger functions
4 * Copyright (C) 1999 Alexandre Julliard
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
21 #include <stdio.h>
22 #include <string.h>
24 #include "winerror.h"
25 #include "wine/winbase16.h"
26 #include "wine/server.h"
27 #include "kernel_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(debugstr);
33 /******************************************************************************
34 * WaitForDebugEvent (KERNEL32.@)
36 * Waits for a debugging event to occur in a process being debugged before
37 * filling out the debug event structure.
39 * PARAMS
40 * event [O] Address of structure for event information.
41 * timeout [I] Number of milliseconds to wait for event.
43 * RETURNS
45 * Returns true if a debug event occurred and false if the call timed out.
47 BOOL WINAPI WaitForDebugEvent(
48 LPDEBUG_EVENT event,
49 DWORD timeout)
51 BOOL ret;
52 DWORD i, res;
54 for (;;)
56 HANDLE wait = 0;
57 debug_event_t data;
58 SERVER_START_REQ( wait_debug_event )
60 req->get_handle = (timeout != 0);
61 wine_server_set_reply( req, &data, sizeof(data) );
62 if (!(ret = !wine_server_call_err( req ))) goto done;
64 if (!wine_server_reply_size(reply)) /* timeout */
66 wait = wine_server_ptr_handle( reply->wait );
67 ret = FALSE;
68 goto done;
70 event->dwDebugEventCode = data.code;
71 event->dwProcessId = (DWORD)reply->pid;
72 event->dwThreadId = (DWORD)reply->tid;
73 switch(data.code)
75 case EXCEPTION_DEBUG_EVENT:
76 event->u.Exception.dwFirstChance = data.exception.first;
77 event->u.Exception.ExceptionRecord.ExceptionCode = data.exception.exc_code;
78 event->u.Exception.ExceptionRecord.ExceptionFlags = data.exception.flags;
79 event->u.Exception.ExceptionRecord.ExceptionRecord = wine_server_get_ptr( data.exception.record );
80 event->u.Exception.ExceptionRecord.ExceptionAddress = wine_server_get_ptr( data.exception.address );
81 event->u.Exception.ExceptionRecord.NumberParameters = data.exception.nb_params;
82 for (i = 0; i < data.exception.nb_params; i++)
83 event->u.Exception.ExceptionRecord.ExceptionInformation[i] = data.exception.params[i];
84 break;
85 case CREATE_THREAD_DEBUG_EVENT:
86 event->u.CreateThread.hThread = wine_server_ptr_handle( data.create_thread.handle );
87 event->u.CreateThread.lpThreadLocalBase = wine_server_get_ptr( data.create_thread.teb );
88 event->u.CreateThread.lpStartAddress = wine_server_get_ptr( data.create_thread.start );
89 break;
90 case CREATE_PROCESS_DEBUG_EVENT:
91 event->u.CreateProcessInfo.hFile = wine_server_ptr_handle( data.create_process.file );
92 event->u.CreateProcessInfo.hProcess = wine_server_ptr_handle( data.create_process.process );
93 event->u.CreateProcessInfo.hThread = wine_server_ptr_handle( data.create_process.thread );
94 event->u.CreateProcessInfo.lpBaseOfImage = wine_server_get_ptr( data.create_process.base );
95 event->u.CreateProcessInfo.dwDebugInfoFileOffset = data.create_process.dbg_offset;
96 event->u.CreateProcessInfo.nDebugInfoSize = data.create_process.dbg_size;
97 event->u.CreateProcessInfo.lpThreadLocalBase = wine_server_get_ptr( data.create_process.teb );
98 event->u.CreateProcessInfo.lpStartAddress = wine_server_get_ptr( data.create_process.start );
99 event->u.CreateProcessInfo.lpImageName = wine_server_get_ptr( data.create_process.name );
100 event->u.CreateProcessInfo.fUnicode = data.create_process.unicode;
101 break;
102 case EXIT_THREAD_DEBUG_EVENT:
103 event->u.ExitThread.dwExitCode = data.exit.exit_code;
104 break;
105 case EXIT_PROCESS_DEBUG_EVENT:
106 event->u.ExitProcess.dwExitCode = data.exit.exit_code;
107 break;
108 case LOAD_DLL_DEBUG_EVENT:
109 event->u.LoadDll.hFile = wine_server_ptr_handle( data.load_dll.handle );
110 event->u.LoadDll.lpBaseOfDll = wine_server_get_ptr( data.load_dll.base );
111 event->u.LoadDll.dwDebugInfoFileOffset = data.load_dll.dbg_offset;
112 event->u.LoadDll.nDebugInfoSize = data.load_dll.dbg_size;
113 event->u.LoadDll.lpImageName = wine_server_get_ptr( data.load_dll.name );
114 event->u.LoadDll.fUnicode = data.load_dll.unicode;
115 break;
116 case UNLOAD_DLL_DEBUG_EVENT:
117 event->u.UnloadDll.lpBaseOfDll = wine_server_get_ptr( data.unload_dll.base );
118 break;
119 case OUTPUT_DEBUG_STRING_EVENT:
120 event->u.DebugString.lpDebugStringData = wine_server_get_ptr( data.output_string.string );
121 event->u.DebugString.fUnicode = data.output_string.unicode;
122 event->u.DebugString.nDebugStringLength = data.output_string.length;
123 break;
124 case RIP_EVENT:
125 event->u.RipInfo.dwError = data.rip_info.error;
126 event->u.RipInfo.dwType = data.rip_info.type;
127 break;
129 done:
130 /* nothing */ ;
132 SERVER_END_REQ;
133 if (ret) return TRUE;
134 if (!wait) break;
135 res = WaitForSingleObject( wait, timeout );
136 CloseHandle( wait );
137 if (res != STATUS_WAIT_0) break;
139 SetLastError( ERROR_SEM_TIMEOUT );
140 return FALSE;
144 /**********************************************************************
145 * ContinueDebugEvent (KERNEL32.@)
147 * Enables a thread that previously produced a debug event to continue.
149 * PARAMS
150 * pid [I] The id of the process to continue.
151 * tid [I] The id of the thread to continue.
152 * status [I] The rule to apply to unhandled exeptions.
154 * RETURNS
156 * True if the debugger is listed as the processes owner and the process
157 * and thread are valid.
159 BOOL WINAPI ContinueDebugEvent(
160 DWORD pid,
161 DWORD tid,
162 DWORD status)
164 BOOL ret;
165 SERVER_START_REQ( continue_debug_event )
167 req->pid = pid;
168 req->tid = tid;
169 req->status = status;
170 ret = !wine_server_call_err( req );
172 SERVER_END_REQ;
173 return ret;
177 /**********************************************************************
178 * DebugActiveProcess (KERNEL32.@)
180 * Attempts to attach the debugger to a process.
182 * PARAMS
183 * pid [I] The process to be debugged.
185 * RETURNS
187 * True if the debugger was attached to process.
189 BOOL WINAPI DebugActiveProcess( DWORD pid )
191 BOOL ret;
192 SERVER_START_REQ( debug_process )
194 req->pid = pid;
195 req->attach = 1;
196 ret = !wine_server_call_err( req );
198 SERVER_END_REQ;
199 return ret;
202 /**********************************************************************
203 * DebugActiveProcessStop (KERNEL32.@)
205 * Attempts to detach the debugger from a process.
207 * PARAMS
208 * pid [I] The process to be detached.
210 * RETURNS
212 * True if the debugger was detached from the process.
214 BOOL WINAPI DebugActiveProcessStop( DWORD pid )
216 BOOL ret;
217 SERVER_START_REQ( debug_process )
219 req->pid = pid;
220 req->attach = 0;
221 ret = !wine_server_call_err( req );
223 SERVER_END_REQ;
224 return ret;
228 /***********************************************************************
229 * OutputDebugStringA (KERNEL32.@)
231 * Output by an application of an ascii string to a debugger (if attached)
232 * and program log.
234 * PARAMS
235 * str [I] The message to be logged and given to the debugger.
237 * RETURNS
239 * Nothing.
241 void WINAPI OutputDebugStringA( LPCSTR str )
243 SERVER_START_REQ( output_debug_string )
245 req->string = wine_server_client_ptr( str );
246 req->unicode = 0;
247 req->length = strlen(str) + 1;
248 wine_server_call( req );
250 SERVER_END_REQ;
251 WARN("%s\n", str);
255 /***********************************************************************
256 * OutputDebugStringW (KERNEL32.@)
258 * Output by an application of a unicode string to a debugger (if attached)
259 * and program log.
261 * PARAMS
262 * str [I] The message to be logged and given to the debugger.
264 * RETURNS
266 * Nothing.
268 void WINAPI OutputDebugStringW( LPCWSTR str )
270 SERVER_START_REQ( output_debug_string )
272 req->string = wine_server_client_ptr( str );
273 req->unicode = 1;
274 req->length = (lstrlenW(str) + 1) * sizeof(WCHAR);
275 wine_server_call( req );
277 SERVER_END_REQ;
278 WARN("%s\n", debugstr_w(str));
282 /***********************************************************************
283 * OutputDebugString (KERNEL.115)
285 * Output by a 16 bit application of an ascii string to a debugger (if attached)
286 * and program log.
288 * PARAMS
289 * str [I] The message to be logged and given to the debugger.
291 * RETURNS
293 void WINAPI OutputDebugString16( LPCSTR str )
295 OutputDebugStringA( str );
299 /***********************************************************************
300 * DebugBreak (KERNEL32.@)
302 * Raises an exception so that a debugger (if attached)
303 * can take some action.
305 * PARAMS
307 * RETURNS
309 void WINAPI DebugBreak(void)
311 DbgBreakPoint();
314 /***********************************************************************
315 * DebugBreakProcess (KERNEL32.@)
317 * Raises an exception so that a debugger (if attached)
318 * can take some action. Same as DebugBreak, but applies to any process.
320 * PARAMS
321 * hProc [I] Process to break into.
323 * RETURNS
325 * True if successful.
327 BOOL WINAPI DebugBreakProcess(HANDLE hProc)
329 BOOL ret, self;
331 TRACE("(%p)\n", hProc);
333 SERVER_START_REQ( debug_break )
335 req->handle = wine_server_obj_handle( hProc );
336 ret = !wine_server_call_err( req );
337 self = ret && reply->self;
339 SERVER_END_REQ;
340 if (self) DbgBreakPoint();
341 return ret;
345 /***********************************************************************
346 * DebugBreak (KERNEL.203)
348 * Raises an exception in a 16 bit application so that a debugger (if attached)
349 * can take some action.
351 * PARAMS
353 * RETURNS
355 * BUGS
357 * Only 386 compatible processors implemented.
359 void WINAPI DebugBreak16(
360 CONTEXT86 *context) /* [in/out] A pointer to the 386 compatible processor state. */
362 #ifdef __i386__
363 EXCEPTION_RECORD rec;
365 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
366 rec.ExceptionFlags = 0;
367 rec.ExceptionRecord = NULL;
368 rec.ExceptionAddress = (LPVOID)context->Eip;
369 rec.NumberParameters = 0;
370 NtRaiseException( &rec, context, TRUE );
371 #endif /* defined(__i386__) */
375 /***********************************************************************
376 * IsDebuggerPresent (KERNEL32.@)
378 * Allows a process to determine if there is a debugger attached.
380 * PARAMS
382 * RETURNS
384 * True if there is a debugger attached.
386 BOOL WINAPI IsDebuggerPresent(void)
388 return NtCurrentTeb()->Peb->BeingDebugged;
391 /***********************************************************************
392 * CheckRemoteDebuggerPresent (KERNEL32.@)
394 * Allows a process to determine if there is a remote debugger
395 * attached.
397 * PARAMS
399 * RETURNS
401 * TRUE because it is a stub.
403 BOOL WINAPI CheckRemoteDebuggerPresent(HANDLE process, PBOOL DebuggerPresent)
405 FIXME("(%p)->(%p): Stub!\n", process, DebuggerPresent);
406 *DebuggerPresent = FALSE;
407 return TRUE;
410 /***********************************************************************
411 * DebugSetProcessKillOnExit (KERNEL32.@)
413 * Let a debugger decide whether a debuggee will be killed upon debugger
414 * termination.
416 * PARAMS
417 * kill [I] If set to true then kill the process on exit.
419 * RETURNS
420 * True if successful, false otherwise.
422 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
424 BOOL ret = FALSE;
426 SERVER_START_REQ( set_debugger_kill_on_exit )
428 req->kill_on_exit = kill;
429 ret = !wine_server_call_err( req );
431 SERVER_END_REQ;
432 return ret;