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
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.
39 * event [O] Address of structure for event information.
40 * timeout [I] Number of milliseconds to wait for event.
44 * Returns true if a debug event occurred and false if the call timed out.
46 BOOL WINAPI
WaitForDebugEvent(
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
);
69 event
->dwDebugEventCode
= data
.code
;
70 event
->dwProcessId
= (DWORD
)reply
->pid
;
71 event
->dwThreadId
= (DWORD
)reply
->tid
;
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
];
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
);
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
;
101 case EXIT_THREAD_DEBUG_EVENT
:
102 event
->u
.ExitThread
.dwExitCode
= data
.exit
.exit_code
;
104 case EXIT_PROCESS_DEBUG_EVENT
:
105 event
->u
.ExitProcess
.dwExitCode
= data
.exit
.exit_code
;
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
;
115 case UNLOAD_DLL_DEBUG_EVENT
:
116 event
->u
.UnloadDll
.lpBaseOfDll
= wine_server_get_ptr( data
.unload_dll
.base
);
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
;
124 event
->u
.RipInfo
.dwError
= data
.rip_info
.error
;
125 event
->u
.RipInfo
.dwType
= data
.rip_info
.type
;
132 if (ret
) return TRUE
;
134 res
= WaitForSingleObject( wait
, timeout
);
136 if (res
!= STATUS_WAIT_0
) break;
138 SetLastError( ERROR_SEM_TIMEOUT
);
143 /**********************************************************************
144 * ContinueDebugEvent (KERNEL32.@)
146 * Enables a thread that previously produced a debug event to continue.
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.
155 * True if the debugger is listed as the processes owner and the process
156 * and thread are valid.
158 BOOL WINAPI
ContinueDebugEvent(
164 SERVER_START_REQ( continue_debug_event
)
168 req
->status
= status
;
169 ret
= !wine_server_call_err( req
);
176 /**********************************************************************
177 * DebugActiveProcess (KERNEL32.@)
179 * Attempts to attach the debugger to a process.
182 * pid [I] The process to be debugged.
186 * True if the debugger was attached to process.
188 BOOL WINAPI
DebugActiveProcess( DWORD pid
)
191 SERVER_START_REQ( debug_process
)
195 ret
= !wine_server_call_err( req
);
201 /**********************************************************************
202 * DebugActiveProcessStop (KERNEL32.@)
204 * Attempts to detach the debugger from a process.
207 * pid [I] The process to be detached.
211 * True if the debugger was detached from the process.
213 BOOL WINAPI
DebugActiveProcessStop( DWORD pid
)
216 SERVER_START_REQ( debug_process
)
220 ret
= !wine_server_call_err( req
);
227 /***********************************************************************
228 * OutputDebugStringA (KERNEL32.@)
230 * Output by an application of an ascii string to a debugger (if attached)
234 * str [I] The message to be logged and given to the debugger.
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
);
256 /* send string to a system-wide monitor */
257 /* FIXME should only send to monitor if no debuggers are attached */
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
);
266 if (InterlockedCompareExchangePointer( &DBWinMutex
, mutex
, 0 ) != 0)
268 /* someone beat us here... */
269 CloseHandle( mutex
);
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};
282 mapping
= OpenFileMappingW( FILE_MAP_WRITE
, FALSE
, shmname
);
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
)
301 struct _mon_buffer_t
{
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
);
321 UnmapViewOfFile( buffer
);
323 CloseHandle( eventbuffer
);
325 CloseHandle( eventdata
);
326 CloseHandle( mapping
);
332 /***********************************************************************
333 * OutputDebugStringW (KERNEL32.@)
335 * Output by an application of a unicode string to a debugger (if attached)
339 * str [I] The message to be logged and given to the debugger.
345 void WINAPI
OutputDebugStringW( LPCWSTR str
)
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.
369 void WINAPI
DebugBreak(void)
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.
381 * hProc [I] Process to break into.
385 * True if successful.
387 BOOL WINAPI
DebugBreakProcess(HANDLE hProc
)
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
;
400 if (self
) DbgBreakPoint();
405 /***********************************************************************
406 * IsDebuggerPresent (KERNEL32.@)
408 * Allows a process to determine if there is a debugger attached.
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
431 * TRUE because it is a stub.
433 BOOL WINAPI
CheckRemoteDebuggerPresent(HANDLE process
, PBOOL DebuggerPresent
)
435 if(!process
|| !DebuggerPresent
)
437 SetLastError(ERROR_INVALID_PARAMETER
);
440 FIXME("(%p)->(%p): Stub!\n", process
, DebuggerPresent
);
441 *DebuggerPresent
= FALSE
;
445 /***********************************************************************
446 * DebugSetProcessKillOnExit (KERNEL32.@)
448 * Let a debugger decide whether a debuggee will be killed upon debugger
452 * kill [I] If set to true then kill the process on exit.
455 * True if successful, false otherwise.
457 BOOL WINAPI
DebugSetProcessKillOnExit(BOOL kill
)
461 SERVER_START_REQ( set_debugger_kill_on_exit
)
463 req
->kill_on_exit
= kill
;
464 ret
= !wine_server_call_err( req
);