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;
91 if (data.info.create_process.file == -1) event->u.CreateProcessInfo.hFile = 0;
93 case EXIT_THREAD_DEBUG_EVENT:
94 event->u.ExitThread.dwExitCode = data.info.exit.exit_code;
96 case EXIT_PROCESS_DEBUG_EVENT:
97 event->u.ExitProcess.dwExitCode = data.info.exit.exit_code;
99 case LOAD_DLL_DEBUG_EVENT:
100 event->u.LoadDll.hFile = data.info.load_dll.handle;
101 event->u.LoadDll.lpBaseOfDll = data.info.load_dll.base;
102 event->u.LoadDll.dwDebugInfoFileOffset = data.info.load_dll.dbg_offset;
103 event->u.LoadDll.nDebugInfoSize = data.info.load_dll.dbg_size;
104 event->u.LoadDll.lpImageName = data.info.load_dll.name;
105 event->u.LoadDll.fUnicode = data.info.load_dll.unicode;
106 if (data.info.load_dll.handle == -1) event->u.LoadDll.hFile = 0;
108 case UNLOAD_DLL_DEBUG_EVENT:
109 event->u.UnloadDll.lpBaseOfDll = data.info.unload_dll.base;
111 case OUTPUT_DEBUG_STRING_EVENT:
112 event->u.DebugString.lpDebugStringData = data.info.output_string.string;
113 event->u.DebugString.fUnicode = data.info.output_string.unicode;
114 event->u.DebugString.nDebugStringLength = data.info.output_string.length;
117 event->u.RipInfo.dwError = data.info.rip_info.error;
118 event->u.RipInfo.dwType = data.info.rip_info.type;
125 if (ret) return TRUE;
127 res = WaitForSingleObject( wait, timeout );
129 if (res != STATUS_WAIT_0) break;
131 SetLastError( ERROR_SEM_TIMEOUT );
136 /**********************************************************************
137 * ContinueDebugEvent (KERNEL32.@)
139 * Enables a thread that previously produced a debug event to continue.
143 * True if the debugger is listed as the processes owner and the process
144 * and thread are valid.
146 BOOL WINAPI ContinueDebugEvent(
147 DWORD pid, /* [in] The id of the process to continue. */
148 DWORD tid, /* [in] The id of the thread to continue. */
149 DWORD status) /* [in] The rule to apply to unhandled exeptions. */
152 SERVER_START_REQ( continue_debug_event )
154 req->pid = (void *)pid;
155 req->tid = (void *)tid;
156 req->status = status;
157 ret = !wine_server_call_err( req );
164 /**********************************************************************
165 * DebugActiveProcess (KERNEL32.@)
167 * Attempts to attach the debugger to a process.
171 * True if the debugger was attached to process.
173 BOOL WINAPI DebugActiveProcess(
174 DWORD pid) /* [in] The process to be debugged. */
177 SERVER_START_REQ( debug_process )
179 req->pid = (void *)pid;
181 ret = !wine_server_call_err( req );
187 /**********************************************************************
188 * DebugActiveProcessStop (KERNEL32.@)
190 * Attempts to detach the debugger from a process.
194 * True if the debugger was detached from the process.
196 BOOL WINAPI DebugActiveProcessStop(
197 DWORD pid) /* [in] The process to be detached. */
200 SERVER_START_REQ( debug_process )
202 req->pid = (void *)pid;
204 ret = !wine_server_call_err( req );
211 /***********************************************************************
212 * OutputDebugStringA (KERNEL32.@)
214 * Output by an application of a unicode string to a debugger (if attached)
217 void WINAPI OutputDebugStringA(
218 LPCSTR str) /* [in] The message to be logged and given to the debugger. */
220 SERVER_START_REQ( output_debug_string )
222 req->string = (void *)str;
224 req->length = strlen(str) + 1;
225 wine_server_call( req );
232 /***********************************************************************
233 * OutputDebugStringW (KERNEL32.@)
235 * Output by an appliccation of a unicode string to a debugger (if attached)
238 void WINAPI OutputDebugStringW(
239 LPCWSTR str) /* [in] The message to be logged and given to the debugger. */
241 SERVER_START_REQ( output_debug_string )
243 req->string = (void *)str;
245 req->length = (lstrlenW(str) + 1) * sizeof(WCHAR);
246 wine_server_call( req );
249 WARN("%s\n", debugstr_w(str));
253 /***********************************************************************
254 * OutputDebugString (KERNEL.115)
256 * Output by a 16 bit application of an ascii string to a debugger (if attached)
259 void WINAPI OutputDebugString16(
260 LPCSTR str) /* [in] The message to be logged and given to the debugger. */
262 OutputDebugStringA( str );
266 /***********************************************************************
267 * DebugBreak (KERNEL32.@)
269 * Raises an exception so that a debugger (if attached)
270 * can take some action.
272 void WINAPI DebugBreak(void)
277 /***********************************************************************
278 * DebugBreakProcess (KERNEL32.@)
280 * Raises an exception so that a debugger (if attached)
281 * can take some action. Same as DebugBreak, but applies to any process.
283 BOOL WINAPI DebugBreakProcess(HANDLE hProc)
287 TRACE("(%08x)\n", hProc);
289 SERVER_START_REQ( debug_break )
292 ret = !wine_server_call_err( req );
293 self = ret && reply->self;
296 if (self) DbgBreakPoint();
301 /***********************************************************************
302 * DebugBreak (KERNEL.203)
304 * Raises an expection in a 16 bit application so that a debugger (if attached)
305 * can take some action.
309 * Only 386 compatible processors implemented.
311 void WINAPI DebugBreak16(
312 CONTEXT86 *context) /* [in/out] A pointer to the 386 compatible processor state. */
315 EXCEPTION_RECORD rec;
317 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
318 rec.ExceptionFlags = 0;
319 rec.ExceptionRecord = NULL;
320 rec.ExceptionAddress = (LPVOID)context->Eip;
321 rec.NumberParameters = 0;
322 NtRaiseException( &rec, context, TRUE );
323 #endif /* defined(__i386__) */
327 /***********************************************************************
328 * IsDebuggerPresent (KERNEL32.@)
330 * Allows a process to determine if there is a debugger attached.
334 * True if there is a debugger attached.
336 BOOL WINAPI IsDebuggerPresent(void)
339 SERVER_START_REQ( get_process_info )
341 req->handle = GetCurrentProcess();
342 if (!wine_server_call_err( req )) ret = reply->debugged;
349 /***********************************************************************
350 * _DebugOutput (KERNEL.328)
352 void WINAPIV _DebugOutput( void )
359 /* Decode caller address */
360 if (!GetModuleName16( GetExePtr(CURRENT_STACK16->cs), caller, sizeof(caller) ))
361 sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
363 /* Build debug message string */
364 VA_START16( valist );
365 flags = VA_ARG16( valist, WORD );
366 spec = VA_ARG16( valist, SEGPTR );
367 /* FIXME: cannot use wvsnprintf16 from kernel */
368 /* wvsnprintf16( temp, sizeof(temp), MapSL(spec), valist ); */
371 FIXME("%s %04x %s\n", caller, flags, debugstr_a(MapSL(spec)) );
374 /***********************************************************************
375 * DebugSetProcessKillOnExit (KERNEL32.@)
377 * Let a debugger decide wether a debuggee will be killed upon debugger
380 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
384 SERVER_START_REQ( set_debugger_kill_on_exit )
386 req->kill_on_exit = kill;
387 ret = !wine_server_call_err( req );