d2d1: Use d2d_point_set() in d2d_rectangle_geometry_init().
[wine.git] / dlls / kernel32 / debugger.c
bloba277f937898abfa5ec7713200eb57cc8356e1ec4
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 "config.h"
22 #include <stdio.h>
23 #include <string.h>
25 #include "ntstatus.h"
26 #define WIN32_NO_STATUS
27 #include "winerror.h"
28 #include "wine/server.h"
29 #include "kernel_private.h"
30 #include "wine/debug.h"
31 #include "wine/exception.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(debugstr);
36 /******************************************************************************
37 * WaitForDebugEvent (KERNEL32.@)
39 * Waits for a debugging event to occur in a process being debugged before
40 * filling out the debug event structure.
42 * PARAMS
43 * event [O] Address of structure for event information.
44 * timeout [I] Number of milliseconds to wait for event.
46 * RETURNS
48 * Returns true if a debug event occurred and false if the call timed out.
50 BOOL WINAPI WaitForDebugEvent(
51 LPDEBUG_EVENT event,
52 DWORD timeout)
54 BOOL ret;
55 DWORD res;
56 int i;
58 for (;;)
60 HANDLE wait = 0;
61 debug_event_t data;
62 SERVER_START_REQ( wait_debug_event )
64 req->get_handle = (timeout != 0);
65 wine_server_set_reply( req, &data, sizeof(data) );
66 if (!(ret = !wine_server_call_err( req ))) goto done;
68 if (!wine_server_reply_size(reply)) /* timeout */
70 wait = wine_server_ptr_handle( reply->wait );
71 ret = FALSE;
72 goto done;
74 event->dwDebugEventCode = data.code;
75 event->dwProcessId = (DWORD)reply->pid;
76 event->dwThreadId = (DWORD)reply->tid;
77 switch(data.code)
79 case EXCEPTION_DEBUG_EVENT:
80 if (data.exception.exc_code == DBG_PRINTEXCEPTION_C && data.exception.nb_params >= 2)
82 event->dwDebugEventCode = OUTPUT_DEBUG_STRING_EVENT;
83 event->u.DebugString.lpDebugStringData = wine_server_get_ptr( data.exception.params[1] );
84 event->u.DebugString.fUnicode = FALSE;
85 event->u.DebugString.nDebugStringLength = data.exception.params[0];
86 break;
88 else if (data.exception.exc_code == DBG_RIPEXCEPTION && data.exception.nb_params >= 2)
90 event->dwDebugEventCode = RIP_EVENT;
91 event->u.RipInfo.dwError = data.exception.params[0];
92 event->u.RipInfo.dwType = data.exception.params[1];
93 break;
95 event->u.Exception.dwFirstChance = data.exception.first;
96 event->u.Exception.ExceptionRecord.ExceptionCode = data.exception.exc_code;
97 event->u.Exception.ExceptionRecord.ExceptionFlags = data.exception.flags;
98 event->u.Exception.ExceptionRecord.ExceptionRecord = wine_server_get_ptr( data.exception.record );
99 event->u.Exception.ExceptionRecord.ExceptionAddress = wine_server_get_ptr( data.exception.address );
100 event->u.Exception.ExceptionRecord.NumberParameters = data.exception.nb_params;
101 for (i = 0; i < data.exception.nb_params; i++)
102 event->u.Exception.ExceptionRecord.ExceptionInformation[i] = data.exception.params[i];
103 break;
104 case CREATE_THREAD_DEBUG_EVENT:
105 event->u.CreateThread.hThread = wine_server_ptr_handle( data.create_thread.handle );
106 event->u.CreateThread.lpThreadLocalBase = wine_server_get_ptr( data.create_thread.teb );
107 event->u.CreateThread.lpStartAddress = wine_server_get_ptr( data.create_thread.start );
108 break;
109 case CREATE_PROCESS_DEBUG_EVENT:
110 event->u.CreateProcessInfo.hFile = wine_server_ptr_handle( data.create_process.file );
111 event->u.CreateProcessInfo.hProcess = wine_server_ptr_handle( data.create_process.process );
112 event->u.CreateProcessInfo.hThread = wine_server_ptr_handle( data.create_process.thread );
113 event->u.CreateProcessInfo.lpBaseOfImage = wine_server_get_ptr( data.create_process.base );
114 event->u.CreateProcessInfo.dwDebugInfoFileOffset = data.create_process.dbg_offset;
115 event->u.CreateProcessInfo.nDebugInfoSize = data.create_process.dbg_size;
116 event->u.CreateProcessInfo.lpThreadLocalBase = wine_server_get_ptr( data.create_process.teb );
117 event->u.CreateProcessInfo.lpStartAddress = wine_server_get_ptr( data.create_process.start );
118 event->u.CreateProcessInfo.lpImageName = wine_server_get_ptr( data.create_process.name );
119 event->u.CreateProcessInfo.fUnicode = data.create_process.unicode;
120 break;
121 case EXIT_THREAD_DEBUG_EVENT:
122 event->u.ExitThread.dwExitCode = data.exit.exit_code;
123 break;
124 case EXIT_PROCESS_DEBUG_EVENT:
125 event->u.ExitProcess.dwExitCode = data.exit.exit_code;
126 break;
127 case LOAD_DLL_DEBUG_EVENT:
128 event->u.LoadDll.hFile = wine_server_ptr_handle( data.load_dll.handle );
129 event->u.LoadDll.lpBaseOfDll = wine_server_get_ptr( data.load_dll.base );
130 event->u.LoadDll.dwDebugInfoFileOffset = data.load_dll.dbg_offset;
131 event->u.LoadDll.nDebugInfoSize = data.load_dll.dbg_size;
132 event->u.LoadDll.lpImageName = wine_server_get_ptr( data.load_dll.name );
133 event->u.LoadDll.fUnicode = data.load_dll.unicode;
134 break;
135 case UNLOAD_DLL_DEBUG_EVENT:
136 event->u.UnloadDll.lpBaseOfDll = wine_server_get_ptr( data.unload_dll.base );
137 break;
139 done:
140 /* nothing */ ;
142 SERVER_END_REQ;
143 if (ret) return TRUE;
144 if (!wait) break;
145 res = WaitForSingleObject( wait, timeout );
146 CloseHandle( wait );
147 if (res != STATUS_WAIT_0) break;
149 SetLastError( ERROR_SEM_TIMEOUT );
150 return FALSE;
154 /**********************************************************************
155 * ContinueDebugEvent (KERNEL32.@)
157 * Enables a thread that previously produced a debug event to continue.
159 * PARAMS
160 * pid [I] The id of the process to continue.
161 * tid [I] The id of the thread to continue.
162 * status [I] The rule to apply to unhandled exceptions.
164 * RETURNS
166 * True if the debugger is listed as the processes owner and the process
167 * and thread are valid.
169 BOOL WINAPI ContinueDebugEvent(
170 DWORD pid,
171 DWORD tid,
172 DWORD status)
174 BOOL ret;
175 SERVER_START_REQ( continue_debug_event )
177 req->pid = pid;
178 req->tid = tid;
179 req->status = status;
180 ret = !wine_server_call_err( req );
182 SERVER_END_REQ;
183 return ret;
187 /**********************************************************************
188 * DebugActiveProcess (KERNEL32.@)
190 * Attempts to attach the debugger to a process.
192 * PARAMS
193 * pid [I] The process to be debugged.
195 * RETURNS
197 * True if the debugger was attached to process.
199 BOOL WINAPI DebugActiveProcess( DWORD pid )
201 BOOL ret;
202 SERVER_START_REQ( debug_process )
204 req->pid = pid;
205 req->attach = 1;
206 ret = !wine_server_call_err( req );
208 SERVER_END_REQ;
209 return ret;
212 /**********************************************************************
213 * DebugActiveProcessStop (KERNEL32.@)
215 * Attempts to detach the debugger from a process.
217 * PARAMS
218 * pid [I] The process to be detached.
220 * RETURNS
222 * True if the debugger was detached from the process.
224 BOOL WINAPI DebugActiveProcessStop( DWORD pid )
226 BOOL ret;
227 SERVER_START_REQ( debug_process )
229 req->pid = pid;
230 req->attach = 0;
231 ret = !wine_server_call_err( req );
233 SERVER_END_REQ;
234 return ret;
237 static LONG WINAPI debug_exception_handler( EXCEPTION_POINTERS *eptr )
239 EXCEPTION_RECORD *rec = eptr->ExceptionRecord;
240 return (rec->ExceptionCode == DBG_PRINTEXCEPTION_C) ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
243 /***********************************************************************
244 * OutputDebugStringA (KERNEL32.@)
246 * Output by an application of an ascii string to a debugger (if attached)
247 * and program log.
249 * PARAMS
250 * str [I] The message to be logged and given to the debugger.
252 * RETURNS
254 * Nothing.
256 void WINAPI OutputDebugStringA( LPCSTR str )
258 static HANDLE DBWinMutex = NULL;
259 static BOOL mutex_inited = FALSE;
260 BOOL caught_by_dbg = TRUE;
262 if (!str) str = "";
263 WARN("%s\n", debugstr_a(str));
265 /* raise exception, WaitForDebugEvent() will generate a corresponding debug event */
266 __TRY
268 ULONG_PTR args[2];
269 args[0] = strlen(str) + 1;
270 args[1] = (ULONG_PTR)str;
271 RaiseException( DBG_PRINTEXCEPTION_C, 0, 2, args );
273 __EXCEPT(debug_exception_handler)
275 caught_by_dbg = FALSE;
277 __ENDTRY
278 if (caught_by_dbg) return;
280 /* send string to a system-wide monitor */
281 if (!mutex_inited)
283 /* first call to OutputDebugString, initialize mutex handle */
284 static const WCHAR mutexname[] = {'D','B','W','i','n','M','u','t','e','x',0};
285 HANDLE mutex = CreateMutexExW( NULL, mutexname, 0, SYNCHRONIZE );
286 if (mutex)
288 if (InterlockedCompareExchangePointer( &DBWinMutex, mutex, 0 ) != 0)
290 /* someone beat us here... */
291 CloseHandle( mutex );
294 mutex_inited = TRUE;
297 if (DBWinMutex)
299 static const WCHAR shmname[] = {'D','B','W','I','N','_','B','U','F','F','E','R',0};
300 static const WCHAR eventbuffername[] = {'D','B','W','I','N','_','B','U','F','F','E','R','_','R','E','A','D','Y',0};
301 static const WCHAR eventdataname[] = {'D','B','W','I','N','_','D','A','T','A','_','R','E','A','D','Y',0};
302 HANDLE mapping;
304 mapping = OpenFileMappingW( FILE_MAP_WRITE, FALSE, shmname );
305 if (mapping)
307 LPVOID buffer;
308 HANDLE eventbuffer, eventdata;
310 buffer = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
311 eventbuffer = OpenEventW( SYNCHRONIZE, FALSE, eventbuffername );
312 eventdata = OpenEventW( EVENT_MODIFY_STATE, FALSE, eventdataname );
314 if (buffer && eventbuffer && eventdata)
316 /* monitor is present, synchronize with other OutputDebugString invocations */
317 WaitForSingleObject( DBWinMutex, INFINITE );
319 /* acquire control over the buffer */
320 if (WaitForSingleObject( eventbuffer, 10000 ) == WAIT_OBJECT_0)
322 int str_len;
323 struct _mon_buffer_t {
324 DWORD pid;
325 char buffer[1];
326 } *mon_buffer = (struct _mon_buffer_t*) buffer;
328 str_len = strlen( str );
329 if (str_len > (4096 - sizeof(DWORD) - 1))
330 str_len = 4096 - sizeof(DWORD) - 1;
332 mon_buffer->pid = GetCurrentProcessId();
333 memcpy( mon_buffer->buffer, str, str_len );
334 mon_buffer->buffer[str_len] = 0;
336 /* signal data ready */
337 SetEvent( eventdata );
339 ReleaseMutex( DBWinMutex );
342 if (buffer)
343 UnmapViewOfFile( buffer );
344 if (eventbuffer)
345 CloseHandle( eventbuffer );
346 if (eventdata)
347 CloseHandle( eventdata );
348 CloseHandle( mapping );
354 /***********************************************************************
355 * OutputDebugStringW (KERNEL32.@)
357 * Output by an application of a unicode string to a debugger (if attached)
358 * and program log.
360 * PARAMS
361 * str [I] The message to be logged and given to the debugger.
363 * RETURNS
365 * Nothing.
367 void WINAPI OutputDebugStringW( LPCWSTR str )
369 UNICODE_STRING strW;
370 STRING strA;
372 RtlInitUnicodeString( &strW, str );
373 if (!RtlUnicodeStringToAnsiString( &strA, &strW, TRUE ))
375 OutputDebugStringA( strA.Buffer );
376 RtlFreeAnsiString( &strA );
381 /***********************************************************************
382 * DebugBreak (KERNEL32.@)
384 * Raises an exception so that a debugger (if attached)
385 * can take some action.
387 #if defined(__i386__) || defined(__x86_64__)
388 __ASM_STDCALL_FUNC( DebugBreak, 0, "jmp " __ASM_NAME("DbgBreakPoint") )
389 #else
390 void WINAPI DebugBreak(void)
392 DbgBreakPoint();
394 #endif
396 /***********************************************************************
397 * DebugBreakProcess (KERNEL32.@)
399 * Raises an exception so that a debugger (if attached)
400 * can take some action. Same as DebugBreak, but applies to any process.
402 * PARAMS
403 * hProc [I] Process to break into.
405 * RETURNS
407 * True if successful.
409 BOOL WINAPI DebugBreakProcess(HANDLE hProc)
411 BOOL ret, self;
413 TRACE("(%p)\n", hProc);
415 SERVER_START_REQ( debug_break )
417 req->handle = wine_server_obj_handle( hProc );
418 ret = !wine_server_call_err( req );
419 self = ret && reply->self;
421 SERVER_END_REQ;
422 if (self) DbgBreakPoint();
423 return ret;
427 /***********************************************************************
428 * IsDebuggerPresent (KERNEL32.@)
430 * Allows a process to determine if there is a debugger attached.
432 * PARAMS
434 * RETURNS
436 * True if there is a debugger attached.
438 BOOL WINAPI IsDebuggerPresent(void)
440 return NtCurrentTeb()->Peb->BeingDebugged;
443 /***********************************************************************
444 * CheckRemoteDebuggerPresent (KERNEL32.@)
446 * Allows a process to determine if there is a remote debugger
447 * attached.
449 * PARAMS
451 * RETURNS
453 * TRUE because it is a stub.
455 BOOL WINAPI CheckRemoteDebuggerPresent(HANDLE process, PBOOL DebuggerPresent)
457 NTSTATUS status;
458 DWORD_PTR port;
460 if(!process || !DebuggerPresent)
462 SetLastError(ERROR_INVALID_PARAMETER);
463 return FALSE;
466 status = NtQueryInformationProcess(process, ProcessDebugPort, &port, sizeof(port), NULL);
467 if (status != STATUS_SUCCESS)
469 SetLastError(RtlNtStatusToDosError(status));
470 return FALSE;
473 *DebuggerPresent = !!port;
474 return TRUE;
477 /***********************************************************************
478 * DebugSetProcessKillOnExit (KERNEL32.@)
480 * Let a debugger decide whether a debuggee will be killed upon debugger
481 * termination.
483 * PARAMS
484 * kill [I] If set to true then kill the process on exit.
486 * RETURNS
487 * True if successful, false otherwise.
489 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
491 BOOL ret = FALSE;
493 SERVER_START_REQ( set_debugger_kill_on_exit )
495 req->kill_on_exit = kill;
496 ret = !wine_server_call_err( req );
498 SERVER_END_REQ;
499 return ret;