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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/winbase16.h"
26 #include "wine/server.h"
27 #include "stackframe.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(debugstr);
33 /******************************************************************************
34 * WaitForDebugEvent (KERNEL32.@)
36 * Waits for a debugging event to occur in a process being debugged before
37 * filling out the debug event structure.
41 * Returns true if a debug event occurred and false if the call timed out.
43 BOOL WINAPI WaitForDebugEvent(
44 LPDEBUG_EVENT event, /* [out] Address of structure for event information. */
45 DWORD timeout) /* [in] Number of milliseconds to wait for event. */
54 SERVER_START_REQ( wait_debug_event )
56 req->get_handle = (timeout != 0);
57 wine_server_set_reply( req, &data, sizeof(data) );
58 if (!(ret = !wine_server_call_err( req ))) goto done;
60 if (!wine_server_reply_size(reply)) /* timeout */
66 event->dwDebugEventCode = data.code;
67 event->dwProcessId = (DWORD)reply->pid;
68 event->dwThreadId = (DWORD)reply->tid;
71 case EXCEPTION_DEBUG_EVENT:
72 event->u.Exception.ExceptionRecord = data.info.exception.record;
73 event->u.Exception.dwFirstChance = data.info.exception.first;
75 case CREATE_THREAD_DEBUG_EVENT:
76 event->u.CreateThread.hThread = data.info.create_thread.handle;
77 event->u.CreateThread.lpThreadLocalBase = data.info.create_thread.teb;
78 event->u.CreateThread.lpStartAddress = data.info.create_thread.start;
80 case CREATE_PROCESS_DEBUG_EVENT:
81 event->u.CreateProcessInfo.hFile = data.info.create_process.file;
82 event->u.CreateProcessInfo.hProcess = data.info.create_process.process;
83 event->u.CreateProcessInfo.hThread = data.info.create_process.thread;
84 event->u.CreateProcessInfo.lpBaseOfImage = data.info.create_process.base;
85 event->u.CreateProcessInfo.dwDebugInfoFileOffset = data.info.create_process.dbg_offset;
86 event->u.CreateProcessInfo.nDebugInfoSize = data.info.create_process.dbg_size;
87 event->u.CreateProcessInfo.lpThreadLocalBase = data.info.create_process.teb;
88 event->u.CreateProcessInfo.lpStartAddress = data.info.create_process.start;
89 event->u.CreateProcessInfo.lpImageName = data.info.create_process.name;
90 event->u.CreateProcessInfo.fUnicode = data.info.create_process.unicode;
92 case EXIT_THREAD_DEBUG_EVENT:
93 event->u.ExitThread.dwExitCode = data.info.exit.exit_code;
95 case EXIT_PROCESS_DEBUG_EVENT:
96 event->u.ExitProcess.dwExitCode = data.info.exit.exit_code;
98 case LOAD_DLL_DEBUG_EVENT:
99 event->u.LoadDll.hFile = data.info.load_dll.handle;
100 event->u.LoadDll.lpBaseOfDll = data.info.load_dll.base;
101 event->u.LoadDll.dwDebugInfoFileOffset = data.info.load_dll.dbg_offset;
102 event->u.LoadDll.nDebugInfoSize = data.info.load_dll.dbg_size;
103 event->u.LoadDll.lpImageName = data.info.load_dll.name;
104 event->u.LoadDll.fUnicode = data.info.load_dll.unicode;
106 case UNLOAD_DLL_DEBUG_EVENT:
107 event->u.UnloadDll.lpBaseOfDll = data.info.unload_dll.base;
109 case OUTPUT_DEBUG_STRING_EVENT:
110 event->u.DebugString.lpDebugStringData = data.info.output_string.string;
111 event->u.DebugString.fUnicode = data.info.output_string.unicode;
112 event->u.DebugString.nDebugStringLength = data.info.output_string.length;
115 event->u.RipInfo.dwError = data.info.rip_info.error;
116 event->u.RipInfo.dwType = data.info.rip_info.type;
123 if (ret) return TRUE;
125 res = WaitForSingleObject( wait, timeout );
127 if (res != STATUS_WAIT_0) break;
129 SetLastError( ERROR_SEM_TIMEOUT );
134 /**********************************************************************
135 * ContinueDebugEvent (KERNEL32.@)
137 * Enables a thread that previously produced a debug event to continue.
141 * True if the debugger is listed as the processes owner and the process
142 * and thread are valid.
144 BOOL WINAPI ContinueDebugEvent(
145 DWORD pid, /* [in] The id of the process to continue. */
146 DWORD tid, /* [in] The id of the thread to continue. */
147 DWORD status) /* [in] The rule to apply to unhandled exeptions. */
150 SERVER_START_REQ( continue_debug_event )
154 req->status = status;
155 ret = !wine_server_call_err( req );
162 /**********************************************************************
163 * DebugActiveProcess (KERNEL32.@)
165 * Attempts to attach the debugger to a process.
169 * True if the debugger was attached to process.
171 BOOL WINAPI DebugActiveProcess(
172 DWORD pid) /* [in] The process to be debugged. */
175 SERVER_START_REQ( debug_process )
179 ret = !wine_server_call_err( req );
185 /**********************************************************************
186 * DebugActiveProcessStop (KERNEL32.@)
188 * Attempts to detach the debugger from a process.
192 * True if the debugger was detached from the process.
194 BOOL WINAPI DebugActiveProcessStop(
195 DWORD pid) /* [in] The process to be detached. */
198 SERVER_START_REQ( debug_process )
202 ret = !wine_server_call_err( req );
209 /***********************************************************************
210 * OutputDebugStringA (KERNEL32.@)
212 * Output by an application of a unicode string to a debugger (if attached)
215 void WINAPI OutputDebugStringA(
216 LPCSTR str) /* [in] The message to be logged and given to the debugger. */
218 SERVER_START_REQ( output_debug_string )
220 req->string = (void *)str;
222 req->length = strlen(str) + 1;
223 wine_server_call( req );
230 /***********************************************************************
231 * OutputDebugStringW (KERNEL32.@)
233 * Output by an appliccation of a unicode string to a debugger (if attached)
236 void WINAPI OutputDebugStringW(
237 LPCWSTR str) /* [in] The message to be logged and given to the debugger. */
239 SERVER_START_REQ( output_debug_string )
241 req->string = (void *)str;
243 req->length = (lstrlenW(str) + 1) * sizeof(WCHAR);
244 wine_server_call( req );
247 WARN("%s\n", debugstr_w(str));
251 /***********************************************************************
252 * OutputDebugString (KERNEL.115)
254 * Output by a 16 bit application of an ascii string to a debugger (if attached)
257 void WINAPI OutputDebugString16(
258 LPCSTR str) /* [in] The message to be logged and given to the debugger. */
260 OutputDebugStringA( str );
264 /***********************************************************************
265 * DebugBreak (KERNEL32.@)
267 * Raises an exception so that a debugger (if attached)
268 * can take some action.
270 void WINAPI DebugBreak(void)
275 /***********************************************************************
276 * DebugBreakProcess (KERNEL32.@)
278 * Raises an exception so that a debugger (if attached)
279 * can take some action. Same as DebugBreak, but applies to any process.
281 BOOL WINAPI DebugBreakProcess(HANDLE hProc)
285 TRACE("(%p)\n", hProc);
287 SERVER_START_REQ( debug_break )
290 ret = !wine_server_call_err( req );
291 self = ret && reply->self;
294 if (self) DbgBreakPoint();
299 /***********************************************************************
300 * DebugBreak (KERNEL.203)
302 * Raises an expection in a 16 bit application so that a debugger (if attached)
303 * can take some action.
307 * Only 386 compatible processors implemented.
309 void WINAPI DebugBreak16(
310 CONTEXT86 *context) /* [in/out] A pointer to the 386 compatible processor state. */
313 EXCEPTION_RECORD rec;
315 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
316 rec.ExceptionFlags = 0;
317 rec.ExceptionRecord = NULL;
318 rec.ExceptionAddress = (LPVOID)context->Eip;
319 rec.NumberParameters = 0;
320 NtRaiseException( &rec, context, TRUE );
321 #endif /* defined(__i386__) */
325 /***********************************************************************
326 * IsDebuggerPresent (KERNEL32.@)
328 * Allows a process to determine if there is a debugger attached.
332 * True if there is a debugger attached.
334 BOOL WINAPI IsDebuggerPresent(void)
337 SERVER_START_REQ( get_process_info )
339 req->handle = GetCurrentProcess();
340 if (!wine_server_call_err( req )) ret = reply->debugged;
347 /***********************************************************************
348 * _DebugOutput (KERNEL.328)
350 void WINAPIV _DebugOutput( void )
357 /* Decode caller address */
358 if (!GetModuleName16( GetExePtr(CURRENT_STACK16->cs), caller, sizeof(caller) ))
359 sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
361 /* Build debug message string */
362 VA_START16( valist );
363 flags = VA_ARG16( valist, WORD );
364 spec = VA_ARG16( valist, SEGPTR );
365 /* FIXME: cannot use wvsnprintf16 from kernel */
366 /* wvsnprintf16( temp, sizeof(temp), MapSL(spec), valist ); */
369 FIXME("%s %04x %s\n", caller, flags, debugstr_a(MapSL(spec)) );
372 /***********************************************************************
373 * DebugSetProcessKillOnExit (KERNEL32.@)
375 * Let a debugger decide wether a debuggee will be killed upon debugger
378 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
382 SERVER_START_REQ( set_debugger_kill_on_exit )
384 req->kill_on_exit = kill;
385 ret = !wine_server_call_err( req );