push 8b07bf1f08b23b9893a622b47d2be359556765b1
[wine/hacks.git] / dlls / kernel32 / debugger.c
blobd732d8bf07cca24d46bed63b162aea8811a12298
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/server.h"
26 #include "kernel_private.h"
27 #include "wine/debug.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(debugstr);
32 /******************************************************************************
33 * WaitForDebugEvent (KERNEL32.@)
35 * Waits for a debugging event to occur in a process being debugged before
36 * filling out the debug event structure.
38 * PARAMS
39 * event [O] Address of structure for event information.
40 * timeout [I] Number of milliseconds to wait for event.
42 * RETURNS
44 * Returns true if a debug event occurred and false if the call timed out.
46 BOOL WINAPI WaitForDebugEvent(
47 LPDEBUG_EVENT event,
48 DWORD timeout)
50 BOOL ret;
51 DWORD i, res;
53 for (;;)
55 HANDLE wait = 0;
56 debug_event_t data;
57 SERVER_START_REQ( wait_debug_event )
59 req->get_handle = (timeout != 0);
60 wine_server_set_reply( req, &data, sizeof(data) );
61 if (!(ret = !wine_server_call_err( req ))) goto done;
63 if (!wine_server_reply_size(reply)) /* timeout */
65 wait = wine_server_ptr_handle( reply->wait );
66 ret = FALSE;
67 goto done;
69 event->dwDebugEventCode = data.code;
70 event->dwProcessId = (DWORD)reply->pid;
71 event->dwThreadId = (DWORD)reply->tid;
72 switch(data.code)
74 case EXCEPTION_DEBUG_EVENT:
75 event->u.Exception.dwFirstChance = data.exception.first;
76 event->u.Exception.ExceptionRecord.ExceptionCode = data.exception.exc_code;
77 event->u.Exception.ExceptionRecord.ExceptionFlags = data.exception.flags;
78 event->u.Exception.ExceptionRecord.ExceptionRecord = wine_server_get_ptr( data.exception.record );
79 event->u.Exception.ExceptionRecord.ExceptionAddress = wine_server_get_ptr( data.exception.address );
80 event->u.Exception.ExceptionRecord.NumberParameters = data.exception.nb_params;
81 for (i = 0; i < data.exception.nb_params; i++)
82 event->u.Exception.ExceptionRecord.ExceptionInformation[i] = data.exception.params[i];
83 break;
84 case CREATE_THREAD_DEBUG_EVENT:
85 event->u.CreateThread.hThread = wine_server_ptr_handle( data.create_thread.handle );
86 event->u.CreateThread.lpThreadLocalBase = wine_server_get_ptr( data.create_thread.teb );
87 event->u.CreateThread.lpStartAddress = wine_server_get_ptr( data.create_thread.start );
88 break;
89 case CREATE_PROCESS_DEBUG_EVENT:
90 event->u.CreateProcessInfo.hFile = wine_server_ptr_handle( data.create_process.file );
91 event->u.CreateProcessInfo.hProcess = wine_server_ptr_handle( data.create_process.process );
92 event->u.CreateProcessInfo.hThread = wine_server_ptr_handle( data.create_process.thread );
93 event->u.CreateProcessInfo.lpBaseOfImage = wine_server_get_ptr( data.create_process.base );
94 event->u.CreateProcessInfo.dwDebugInfoFileOffset = data.create_process.dbg_offset;
95 event->u.CreateProcessInfo.nDebugInfoSize = data.create_process.dbg_size;
96 event->u.CreateProcessInfo.lpThreadLocalBase = wine_server_get_ptr( data.create_process.teb );
97 event->u.CreateProcessInfo.lpStartAddress = wine_server_get_ptr( data.create_process.start );
98 event->u.CreateProcessInfo.lpImageName = wine_server_get_ptr( data.create_process.name );
99 event->u.CreateProcessInfo.fUnicode = data.create_process.unicode;
100 break;
101 case EXIT_THREAD_DEBUG_EVENT:
102 event->u.ExitThread.dwExitCode = data.exit.exit_code;
103 break;
104 case EXIT_PROCESS_DEBUG_EVENT:
105 event->u.ExitProcess.dwExitCode = data.exit.exit_code;
106 break;
107 case LOAD_DLL_DEBUG_EVENT:
108 event->u.LoadDll.hFile = wine_server_ptr_handle( data.load_dll.handle );
109 event->u.LoadDll.lpBaseOfDll = wine_server_get_ptr( data.load_dll.base );
110 event->u.LoadDll.dwDebugInfoFileOffset = data.load_dll.dbg_offset;
111 event->u.LoadDll.nDebugInfoSize = data.load_dll.dbg_size;
112 event->u.LoadDll.lpImageName = wine_server_get_ptr( data.load_dll.name );
113 event->u.LoadDll.fUnicode = data.load_dll.unicode;
114 break;
115 case UNLOAD_DLL_DEBUG_EVENT:
116 event->u.UnloadDll.lpBaseOfDll = wine_server_get_ptr( data.unload_dll.base );
117 break;
118 case OUTPUT_DEBUG_STRING_EVENT:
119 event->u.DebugString.lpDebugStringData = wine_server_get_ptr( data.output_string.string );
120 event->u.DebugString.fUnicode = FALSE;
121 event->u.DebugString.nDebugStringLength = data.output_string.length;
122 break;
123 case RIP_EVENT:
124 event->u.RipInfo.dwError = data.rip_info.error;
125 event->u.RipInfo.dwType = data.rip_info.type;
126 break;
128 done:
129 /* nothing */ ;
131 SERVER_END_REQ;
132 if (ret) return TRUE;
133 if (!wait) break;
134 res = WaitForSingleObject( wait, timeout );
135 CloseHandle( wait );
136 if (res != STATUS_WAIT_0) break;
138 SetLastError( ERROR_SEM_TIMEOUT );
139 return FALSE;
143 /**********************************************************************
144 * ContinueDebugEvent (KERNEL32.@)
146 * Enables a thread that previously produced a debug event to continue.
148 * PARAMS
149 * pid [I] The id of the process to continue.
150 * tid [I] The id of the thread to continue.
151 * status [I] The rule to apply to unhandled exeptions.
153 * RETURNS
155 * True if the debugger is listed as the processes owner and the process
156 * and thread are valid.
158 BOOL WINAPI ContinueDebugEvent(
159 DWORD pid,
160 DWORD tid,
161 DWORD status)
163 BOOL ret;
164 SERVER_START_REQ( continue_debug_event )
166 req->pid = pid;
167 req->tid = tid;
168 req->status = status;
169 ret = !wine_server_call_err( req );
171 SERVER_END_REQ;
172 return ret;
176 /**********************************************************************
177 * DebugActiveProcess (KERNEL32.@)
179 * Attempts to attach the debugger to a process.
181 * PARAMS
182 * pid [I] The process to be debugged.
184 * RETURNS
186 * True if the debugger was attached to process.
188 BOOL WINAPI DebugActiveProcess( DWORD pid )
190 BOOL ret;
191 SERVER_START_REQ( debug_process )
193 req->pid = pid;
194 req->attach = 1;
195 ret = !wine_server_call_err( req );
197 SERVER_END_REQ;
198 return ret;
201 /**********************************************************************
202 * DebugActiveProcessStop (KERNEL32.@)
204 * Attempts to detach the debugger from a process.
206 * PARAMS
207 * pid [I] The process to be detached.
209 * RETURNS
211 * True if the debugger was detached from the process.
213 BOOL WINAPI DebugActiveProcessStop( DWORD pid )
215 BOOL ret;
216 SERVER_START_REQ( debug_process )
218 req->pid = pid;
219 req->attach = 0;
220 ret = !wine_server_call_err( req );
222 SERVER_END_REQ;
223 return ret;
227 /***********************************************************************
228 * OutputDebugStringA (KERNEL32.@)
230 * Output by an application of an ascii string to a debugger (if attached)
231 * and program log.
233 * PARAMS
234 * str [I] The message to be logged and given to the debugger.
236 * RETURNS
238 * Nothing.
240 void WINAPI OutputDebugStringA( LPCSTR str )
242 static HANDLE DBWinMutex = NULL;
243 static BOOL mutex_inited = FALSE;
245 /* send string to attached debugger */
246 SERVER_START_REQ( output_debug_string )
248 req->string = wine_server_client_ptr( str );
249 req->length = strlen(str) + 1;
250 wine_server_call( req );
252 SERVER_END_REQ;
254 WARN("%s\n", str);
256 /* send string to a system-wide monitor */
257 /* FIXME should only send to monitor if no debuggers are attached */
259 if (!mutex_inited)
261 /* first call to OutputDebugString, initialize mutex handle */
262 static const WCHAR mutexname[] = {'D','B','W','i','n','M','u','t','e','x',0};
263 HANDLE mutex = CreateMutexExW( NULL, mutexname, 0, SYNCHRONIZE );
264 if (mutex)
266 if (InterlockedCompareExchangePointer( &DBWinMutex, mutex, 0 ) != 0)
268 /* someone beat us here... */
269 CloseHandle( mutex );
272 mutex_inited = TRUE;
275 if (DBWinMutex)
277 static const WCHAR shmname[] = {'D','B','W','I','N','_','B','U','F','F','E','R',0};
278 static const WCHAR eventbuffername[] = {'D','B','W','I','N','_','B','U','F','F','E','R','_','R','E','A','D','Y',0};
279 static const WCHAR eventdataname[] = {'D','B','W','I','N','_','D','A','T','A','_','R','E','A','D','Y',0};
280 HANDLE mapping;
282 mapping = OpenFileMappingW( FILE_MAP_WRITE, FALSE, shmname );
283 if (mapping)
285 LPVOID buffer;
286 HANDLE eventbuffer, eventdata;
288 buffer = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
289 eventbuffer = OpenEventW( SYNCHRONIZE, FALSE, eventbuffername );
290 eventdata = OpenEventW( EVENT_MODIFY_STATE, FALSE, eventdataname );
292 if (buffer && eventbuffer && eventdata)
294 /* monitor is present, synchronize with other OutputDebugString invokations */
295 WaitForSingleObject( DBWinMutex, INFINITE );
297 /* acquire control over the buffer */
298 if (WaitForSingleObject( eventbuffer, 10000 ) == WAIT_OBJECT_0)
300 int str_len;
301 struct _mon_buffer_t {
302 DWORD pid;
303 char buffer[1];
304 } *mon_buffer = (struct _mon_buffer_t*) buffer;
306 str_len = strlen( str );
307 if (str_len > (4096 - sizeof(DWORD) - 1))
308 str_len = 4096 - sizeof(DWORD) - 1;
310 mon_buffer->pid = GetCurrentProcessId();
311 memcpy( mon_buffer->buffer, str, str_len );
312 mon_buffer->buffer[str_len] = 0;
314 /* signal data ready */
315 SetEvent( eventdata );
317 ReleaseMutex( DBWinMutex );
320 if (buffer)
321 UnmapViewOfFile( buffer );
322 if (eventbuffer)
323 CloseHandle( eventbuffer );
324 if (eventdata)
325 CloseHandle( eventdata );
326 CloseHandle( mapping );
332 /***********************************************************************
333 * OutputDebugStringW (KERNEL32.@)
335 * Output by an application of a unicode string to a debugger (if attached)
336 * and program log.
338 * PARAMS
339 * str [I] The message to be logged and given to the debugger.
341 * RETURNS
343 * Nothing.
345 void WINAPI OutputDebugStringW( LPCWSTR str )
347 UNICODE_STRING strW;
348 STRING strA;
350 RtlInitUnicodeString( &strW, str );
351 if (!RtlUnicodeStringToAnsiString( &strA, &strW, TRUE ))
353 OutputDebugStringA( strA.Buffer );
354 RtlFreeAnsiString( &strA );
359 /***********************************************************************
360 * DebugBreak (KERNEL32.@)
362 * Raises an exception so that a debugger (if attached)
363 * can take some action.
365 * PARAMS
367 * RETURNS
369 void WINAPI DebugBreak(void)
371 DbgBreakPoint();
374 /***********************************************************************
375 * DebugBreakProcess (KERNEL32.@)
377 * Raises an exception so that a debugger (if attached)
378 * can take some action. Same as DebugBreak, but applies to any process.
380 * PARAMS
381 * hProc [I] Process to break into.
383 * RETURNS
385 * True if successful.
387 BOOL WINAPI DebugBreakProcess(HANDLE hProc)
389 BOOL ret, self;
391 TRACE("(%p)\n", hProc);
393 SERVER_START_REQ( debug_break )
395 req->handle = wine_server_obj_handle( hProc );
396 ret = !wine_server_call_err( req );
397 self = ret && reply->self;
399 SERVER_END_REQ;
400 if (self) DbgBreakPoint();
401 return ret;
405 /***********************************************************************
406 * IsDebuggerPresent (KERNEL32.@)
408 * Allows a process to determine if there is a debugger attached.
410 * PARAMS
412 * RETURNS
414 * True if there is a debugger attached.
416 BOOL WINAPI IsDebuggerPresent(void)
418 return NtCurrentTeb()->Peb->BeingDebugged;
421 /***********************************************************************
422 * CheckRemoteDebuggerPresent (KERNEL32.@)
424 * Allows a process to determine if there is a remote debugger
425 * attached.
427 * PARAMS
429 * RETURNS
431 * TRUE because it is a stub.
433 BOOL WINAPI CheckRemoteDebuggerPresent(HANDLE process, PBOOL DebuggerPresent)
435 if(!process || !DebuggerPresent)
437 SetLastError(ERROR_INVALID_PARAMETER);
438 return FALSE;
440 FIXME("(%p)->(%p): Stub!\n", process, DebuggerPresent);
441 *DebuggerPresent = FALSE;
442 return TRUE;
445 /***********************************************************************
446 * DebugSetProcessKillOnExit (KERNEL32.@)
448 * Let a debugger decide whether a debuggee will be killed upon debugger
449 * termination.
451 * PARAMS
452 * kill [I] If set to true then kill the process on exit.
454 * RETURNS
455 * True if successful, false otherwise.
457 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
459 BOOL ret = FALSE;
461 SERVER_START_REQ( set_debugger_kill_on_exit )
463 req->kill_on_exit = kill;
464 ret = !wine_server_call_err( req );
466 SERVER_END_REQ;
467 return ret;