push 212f1dad91f15aefd8e676124180e0b86d7c9ee6
[wine/hacks.git] / dlls / kernel32 / debugger.c
blob6e7b4b1a4db826b80a96866cc7db1c514cba5852
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 "kernel16_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(debugstr);
34 /******************************************************************************
35 * WaitForDebugEvent (KERNEL32.@)
37 * Waits for a debugging event to occur in a process being debugged before
38 * filling out the debug event structure.
40 * PARAMS
41 * event [O] Address of structure for event information.
42 * timeout [I] Number of milliseconds to wait for event.
44 * RETURNS
46 * Returns true if a debug event occurred and false if the call timed out.
48 BOOL WINAPI WaitForDebugEvent(
49 LPDEBUG_EVENT event,
50 DWORD timeout)
52 BOOL ret;
53 DWORD i, res;
55 for (;;)
57 HANDLE wait = 0;
58 debug_event_t data;
59 SERVER_START_REQ( wait_debug_event )
61 req->get_handle = (timeout != 0);
62 wine_server_set_reply( req, &data, sizeof(data) );
63 if (!(ret = !wine_server_call_err( req ))) goto done;
65 if (!wine_server_reply_size(reply)) /* timeout */
67 wait = wine_server_ptr_handle( reply->wait );
68 ret = FALSE;
69 goto done;
71 event->dwDebugEventCode = data.code;
72 event->dwProcessId = (DWORD)reply->pid;
73 event->dwThreadId = (DWORD)reply->tid;
74 switch(data.code)
76 case EXCEPTION_DEBUG_EVENT:
77 event->u.Exception.dwFirstChance = data.exception.first;
78 event->u.Exception.ExceptionRecord.ExceptionCode = data.exception.exc_code;
79 event->u.Exception.ExceptionRecord.ExceptionFlags = data.exception.flags;
80 event->u.Exception.ExceptionRecord.ExceptionRecord = wine_server_get_ptr( data.exception.record );
81 event->u.Exception.ExceptionRecord.ExceptionAddress = wine_server_get_ptr( data.exception.address );
82 event->u.Exception.ExceptionRecord.NumberParameters = data.exception.nb_params;
83 for (i = 0; i < data.exception.nb_params; i++)
84 event->u.Exception.ExceptionRecord.ExceptionInformation[i] = data.exception.params[i];
85 break;
86 case CREATE_THREAD_DEBUG_EVENT:
87 event->u.CreateThread.hThread = wine_server_ptr_handle( data.create_thread.handle );
88 event->u.CreateThread.lpThreadLocalBase = wine_server_get_ptr( data.create_thread.teb );
89 event->u.CreateThread.lpStartAddress = wine_server_get_ptr( data.create_thread.start );
90 break;
91 case CREATE_PROCESS_DEBUG_EVENT:
92 event->u.CreateProcessInfo.hFile = wine_server_ptr_handle( data.create_process.file );
93 event->u.CreateProcessInfo.hProcess = wine_server_ptr_handle( data.create_process.process );
94 event->u.CreateProcessInfo.hThread = wine_server_ptr_handle( data.create_process.thread );
95 event->u.CreateProcessInfo.lpBaseOfImage = wine_server_get_ptr( data.create_process.base );
96 event->u.CreateProcessInfo.dwDebugInfoFileOffset = data.create_process.dbg_offset;
97 event->u.CreateProcessInfo.nDebugInfoSize = data.create_process.dbg_size;
98 event->u.CreateProcessInfo.lpThreadLocalBase = wine_server_get_ptr( data.create_process.teb );
99 event->u.CreateProcessInfo.lpStartAddress = wine_server_get_ptr( data.create_process.start );
100 event->u.CreateProcessInfo.lpImageName = wine_server_get_ptr( data.create_process.name );
101 event->u.CreateProcessInfo.fUnicode = data.create_process.unicode;
102 break;
103 case EXIT_THREAD_DEBUG_EVENT:
104 event->u.ExitThread.dwExitCode = data.exit.exit_code;
105 break;
106 case EXIT_PROCESS_DEBUG_EVENT:
107 event->u.ExitProcess.dwExitCode = data.exit.exit_code;
108 break;
109 case LOAD_DLL_DEBUG_EVENT:
110 event->u.LoadDll.hFile = wine_server_ptr_handle( data.load_dll.handle );
111 event->u.LoadDll.lpBaseOfDll = wine_server_get_ptr( data.load_dll.base );
112 event->u.LoadDll.dwDebugInfoFileOffset = data.load_dll.dbg_offset;
113 event->u.LoadDll.nDebugInfoSize = data.load_dll.dbg_size;
114 event->u.LoadDll.lpImageName = wine_server_get_ptr( data.load_dll.name );
115 event->u.LoadDll.fUnicode = data.load_dll.unicode;
116 break;
117 case UNLOAD_DLL_DEBUG_EVENT:
118 event->u.UnloadDll.lpBaseOfDll = wine_server_get_ptr( data.unload_dll.base );
119 break;
120 case OUTPUT_DEBUG_STRING_EVENT:
121 event->u.DebugString.lpDebugStringData = wine_server_get_ptr( data.output_string.string );
122 event->u.DebugString.fUnicode = data.output_string.unicode;
123 event->u.DebugString.nDebugStringLength = data.output_string.length;
124 break;
125 case RIP_EVENT:
126 event->u.RipInfo.dwError = data.rip_info.error;
127 event->u.RipInfo.dwType = data.rip_info.type;
128 break;
130 done:
131 /* nothing */ ;
133 SERVER_END_REQ;
134 if (ret) return TRUE;
135 if (!wait) break;
136 res = WaitForSingleObject( wait, timeout );
137 CloseHandle( wait );
138 if (res != STATUS_WAIT_0) break;
140 SetLastError( ERROR_SEM_TIMEOUT );
141 return FALSE;
145 /**********************************************************************
146 * ContinueDebugEvent (KERNEL32.@)
148 * Enables a thread that previously produced a debug event to continue.
150 * PARAMS
151 * pid [I] The id of the process to continue.
152 * tid [I] The id of the thread to continue.
153 * status [I] The rule to apply to unhandled exeptions.
155 * RETURNS
157 * True if the debugger is listed as the processes owner and the process
158 * and thread are valid.
160 BOOL WINAPI ContinueDebugEvent(
161 DWORD pid,
162 DWORD tid,
163 DWORD status)
165 BOOL ret;
166 SERVER_START_REQ( continue_debug_event )
168 req->pid = pid;
169 req->tid = tid;
170 req->status = status;
171 ret = !wine_server_call_err( req );
173 SERVER_END_REQ;
174 return ret;
178 /**********************************************************************
179 * DebugActiveProcess (KERNEL32.@)
181 * Attempts to attach the debugger to a process.
183 * PARAMS
184 * pid [I] The process to be debugged.
186 * RETURNS
188 * True if the debugger was attached to process.
190 BOOL WINAPI DebugActiveProcess( DWORD pid )
192 BOOL ret;
193 SERVER_START_REQ( debug_process )
195 req->pid = pid;
196 req->attach = 1;
197 ret = !wine_server_call_err( req );
199 SERVER_END_REQ;
200 return ret;
203 /**********************************************************************
204 * DebugActiveProcessStop (KERNEL32.@)
206 * Attempts to detach the debugger from a process.
208 * PARAMS
209 * pid [I] The process to be detached.
211 * RETURNS
213 * True if the debugger was detached from the process.
215 BOOL WINAPI DebugActiveProcessStop( DWORD pid )
217 BOOL ret;
218 SERVER_START_REQ( debug_process )
220 req->pid = pid;
221 req->attach = 0;
222 ret = !wine_server_call_err( req );
224 SERVER_END_REQ;
225 return ret;
229 /***********************************************************************
230 * OutputDebugStringA (KERNEL32.@)
232 * Output by an application of an ascii string to a debugger (if attached)
233 * and program log.
235 * PARAMS
236 * str [I] The message to be logged and given to the debugger.
238 * RETURNS
240 * Nothing.
242 void WINAPI OutputDebugStringA( LPCSTR str )
244 SERVER_START_REQ( output_debug_string )
246 req->string = wine_server_client_ptr( str );
247 req->unicode = 0;
248 req->length = strlen(str) + 1;
249 wine_server_call( req );
251 SERVER_END_REQ;
252 WARN("%s\n", str);
256 /***********************************************************************
257 * OutputDebugStringW (KERNEL32.@)
259 * Output by an application of a unicode string to a debugger (if attached)
260 * and program log.
262 * PARAMS
263 * str [I] The message to be logged and given to the debugger.
265 * RETURNS
267 * Nothing.
269 void WINAPI OutputDebugStringW( LPCWSTR str )
271 SERVER_START_REQ( output_debug_string )
273 req->string = wine_server_client_ptr( str );
274 req->unicode = 1;
275 req->length = (lstrlenW(str) + 1) * sizeof(WCHAR);
276 wine_server_call( req );
278 SERVER_END_REQ;
279 WARN("%s\n", debugstr_w(str));
283 /***********************************************************************
284 * OutputDebugString (KERNEL.115)
286 * Output by a 16 bit application of an ascii string to a debugger (if attached)
287 * and program log.
289 * PARAMS
290 * str [I] The message to be logged and given to the debugger.
292 * RETURNS
294 void WINAPI OutputDebugString16( LPCSTR str )
296 OutputDebugStringA( str );
300 /***********************************************************************
301 * DebugBreak (KERNEL32.@)
303 * Raises an exception so that a debugger (if attached)
304 * can take some action.
306 * PARAMS
308 * RETURNS
310 void WINAPI DebugBreak(void)
312 DbgBreakPoint();
315 /***********************************************************************
316 * DebugBreakProcess (KERNEL32.@)
318 * Raises an exception so that a debugger (if attached)
319 * can take some action. Same as DebugBreak, but applies to any process.
321 * PARAMS
322 * hProc [I] Process to break into.
324 * RETURNS
326 * True if successful.
328 BOOL WINAPI DebugBreakProcess(HANDLE hProc)
330 BOOL ret, self;
332 TRACE("(%p)\n", hProc);
334 SERVER_START_REQ( debug_break )
336 req->handle = wine_server_obj_handle( hProc );
337 ret = !wine_server_call_err( req );
338 self = ret && reply->self;
340 SERVER_END_REQ;
341 if (self) DbgBreakPoint();
342 return ret;
346 /***********************************************************************
347 * DebugBreak (KERNEL.203)
349 * Raises an exception in a 16 bit application so that a debugger (if attached)
350 * can take some action.
352 * PARAMS
354 * RETURNS
356 * BUGS
358 * Only 386 compatible processors implemented.
360 void WINAPI DebugBreak16(
361 CONTEXT86 *context) /* [in/out] A pointer to the 386 compatible processor state. */
363 #ifdef __i386__
364 EXCEPTION_RECORD rec;
366 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
367 rec.ExceptionFlags = 0;
368 rec.ExceptionRecord = NULL;
369 rec.ExceptionAddress = (LPVOID)context->Eip;
370 rec.NumberParameters = 0;
371 NtRaiseException( &rec, context, TRUE );
372 #endif /* defined(__i386__) */
376 /***********************************************************************
377 * IsDebuggerPresent (KERNEL32.@)
379 * Allows a process to determine if there is a debugger attached.
381 * PARAMS
383 * RETURNS
385 * True if there is a debugger attached.
387 BOOL WINAPI IsDebuggerPresent(void)
389 return NtCurrentTeb()->Peb->BeingDebugged;
392 /***********************************************************************
393 * CheckRemoteDebuggerPresent (KERNEL32.@)
395 * Allows a process to determine if there is a remote debugger
396 * attached.
398 * PARAMS
400 * RETURNS
402 * TRUE because it is a stub.
404 BOOL WINAPI CheckRemoteDebuggerPresent(HANDLE process, PBOOL DebuggerPresent)
406 FIXME("(%p)->(%p): Stub!\n", process, DebuggerPresent);
407 *DebuggerPresent = FALSE;
408 return TRUE;
411 /***********************************************************************
412 * _DebugOutput (KERNEL.328)
414 void WINAPIV _DebugOutput( WORD flags, LPCSTR spec, VA_LIST16 valist )
416 char caller[101];
418 /* Decode caller address */
419 if (!GetModuleName16( GetExePtr(CURRENT_STACK16->cs), caller, sizeof(caller) ))
420 sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
422 /* FIXME: cannot use wvsnprintf16 from kernel */
423 /* wvsnprintf16( temp, sizeof(temp), spec, valist ); */
425 /* Output */
426 FIXME("%s %04x %s\n", caller, flags, debugstr_a(spec) );
429 /***********************************************************************
430 * DebugSetProcessKillOnExit (KERNEL32.@)
432 * Let a debugger decide whether a debuggee will be killed upon debugger
433 * termination.
435 * PARAMS
436 * kill [I] If set to true then kill the process on exit.
438 * RETURNS
439 * True if successful, false otherwise.
441 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
443 BOOL ret = FALSE;
445 SERVER_START_REQ( set_debugger_kill_on_exit )
447 req->kill_on_exit = kill;
448 ret = !wine_server_call_err( req );
450 SERVER_END_REQ;
451 return ret;