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
26 #define WIN32_NO_STATUS
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.
43 * event [O] Address of structure for event information.
44 * timeout [I] Number of milliseconds to wait for event.
48 * Returns true if a debug event occurred and false if the call timed out.
50 BOOL WINAPI
WaitForDebugEvent(
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
);
74 event
->dwDebugEventCode
= data
.code
;
75 event
->dwProcessId
= (DWORD
)reply
->pid
;
76 event
->dwThreadId
= (DWORD
)reply
->tid
;
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];
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];
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
];
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
);
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
;
121 case EXIT_THREAD_DEBUG_EVENT
:
122 event
->u
.ExitThread
.dwExitCode
= data
.exit
.exit_code
;
124 case EXIT_PROCESS_DEBUG_EVENT
:
125 event
->u
.ExitProcess
.dwExitCode
= data
.exit
.exit_code
;
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
;
135 case UNLOAD_DLL_DEBUG_EVENT
:
136 event
->u
.UnloadDll
.lpBaseOfDll
= wine_server_get_ptr( data
.unload_dll
.base
);
143 if (ret
) return TRUE
;
145 res
= WaitForSingleObject( wait
, timeout
);
147 if (res
!= STATUS_WAIT_0
) break;
149 SetLastError( ERROR_SEM_TIMEOUT
);
154 /**********************************************************************
155 * ContinueDebugEvent (KERNEL32.@)
157 * Enables a thread that previously produced a debug event to continue.
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.
166 * True if the debugger is listed as the processes owner and the process
167 * and thread are valid.
169 BOOL WINAPI
ContinueDebugEvent(
175 SERVER_START_REQ( continue_debug_event
)
179 req
->status
= status
;
180 ret
= !wine_server_call_err( req
);
187 /**********************************************************************
188 * DebugActiveProcess (KERNEL32.@)
190 * Attempts to attach the debugger to a process.
193 * pid [I] The process to be debugged.
197 * True if the debugger was attached to process.
199 BOOL WINAPI
DebugActiveProcess( DWORD pid
)
202 SERVER_START_REQ( debug_process
)
206 ret
= !wine_server_call_err( req
);
212 /**********************************************************************
213 * DebugActiveProcessStop (KERNEL32.@)
215 * Attempts to detach the debugger from a process.
218 * pid [I] The process to be detached.
222 * True if the debugger was detached from the process.
224 BOOL WINAPI
DebugActiveProcessStop( DWORD pid
)
227 SERVER_START_REQ( debug_process
)
231 ret
= !wine_server_call_err( req
);
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)
250 * str [I] The message to be logged and given to the debugger.
256 void WINAPI
OutputDebugStringA( LPCSTR str
)
258 static HANDLE DBWinMutex
= NULL
;
259 static BOOL mutex_inited
= FALSE
;
260 BOOL caught_by_dbg
= TRUE
;
263 WARN("%s\n", debugstr_a(str
));
265 /* raise exception, WaitForDebugEvent() will generate a corresponding debug event */
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
;
278 if (caught_by_dbg
) return;
280 /* send string to a system-wide monitor */
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
);
288 if (InterlockedCompareExchangePointer( &DBWinMutex
, mutex
, 0 ) != 0)
290 /* someone beat us here... */
291 CloseHandle( mutex
);
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};
304 mapping
= OpenFileMappingW( FILE_MAP_WRITE
, FALSE
, shmname
);
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
)
323 struct _mon_buffer_t
{
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
);
343 UnmapViewOfFile( buffer
);
345 CloseHandle( eventbuffer
);
347 CloseHandle( eventdata
);
348 CloseHandle( mapping
);
354 /***********************************************************************
355 * OutputDebugStringW (KERNEL32.@)
357 * Output by an application of a unicode string to a debugger (if attached)
361 * str [I] The message to be logged and given to the debugger.
367 void WINAPI
OutputDebugStringW( LPCWSTR str
)
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") )
390 void WINAPI
DebugBreak(void)
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.
403 * hProc [I] Process to break into.
407 * True if successful.
409 BOOL WINAPI
DebugBreakProcess(HANDLE hProc
)
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
;
422 if (self
) DbgBreakPoint();
427 /***********************************************************************
428 * IsDebuggerPresent (KERNEL32.@)
430 * Allows a process to determine if there is a debugger attached.
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
453 * TRUE because it is a stub.
455 BOOL WINAPI
CheckRemoteDebuggerPresent(HANDLE process
, PBOOL DebuggerPresent
)
460 if(!process
|| !DebuggerPresent
)
462 SetLastError(ERROR_INVALID_PARAMETER
);
466 status
= NtQueryInformationProcess(process
, ProcessDebugPort
, &port
, sizeof(port
), NULL
);
467 if (status
!= STATUS_SUCCESS
)
469 SetLastError(RtlNtStatusToDosError(status
));
473 *DebuggerPresent
= !!port
;
477 /***********************************************************************
478 * DebugSetProcessKillOnExit (KERNEL32.@)
480 * Let a debugger decide whether a debuggee will be killed upon debugger
484 * kill [I] If set to true then kill the process on exit.
487 * True if successful, false otherwise.
489 BOOL WINAPI
DebugSetProcessKillOnExit(BOOL kill
)
493 SERVER_START_REQ( set_debugger_kill_on_exit
)
495 req
->kill_on_exit
= kill
;
496 ret
= !wine_server_call_err( req
);