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"
28 #include "stackframe.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(debugstr);
34 /******************************************************************************
35 * WaitForDebugEvent (KERNEL32.@)
37 * Waits for a debugging event to occur in a process being debugged before
38 * filling out the debug event structure.
42 * Returns true if a debug event occurred and false if the call timed out.
44 BOOL WINAPI WaitForDebugEvent(
45 LPDEBUG_EVENT event, /* [out] Address of structure for event information. */
46 DWORD timeout) /* [in] Number of milliseconds to wait for event. */
55 SERVER_START_REQ( wait_debug_event )
57 req->get_handle = (timeout != 0);
58 wine_server_set_reply( req, &data, sizeof(data) );
59 if (!(ret = !wine_server_call_err( req ))) goto done;
61 if (!wine_server_reply_size(reply)) /* timeout */
67 event->dwDebugEventCode = data.code;
68 event->dwProcessId = (DWORD)reply->pid;
69 event->dwThreadId = (DWORD)reply->tid;
72 case EXCEPTION_DEBUG_EVENT:
73 event->u.Exception.ExceptionRecord = data.info.exception.record;
74 event->u.Exception.dwFirstChance = data.info.exception.first;
76 case CREATE_THREAD_DEBUG_EVENT:
77 event->u.CreateThread.hThread = data.info.create_thread.handle;
78 event->u.CreateThread.lpThreadLocalBase = data.info.create_thread.teb;
79 event->u.CreateThread.lpStartAddress = data.info.create_thread.start;
81 case CREATE_PROCESS_DEBUG_EVENT:
82 event->u.CreateProcessInfo.hFile = data.info.create_process.file;
83 event->u.CreateProcessInfo.hProcess = data.info.create_process.process;
84 event->u.CreateProcessInfo.hThread = data.info.create_process.thread;
85 event->u.CreateProcessInfo.lpBaseOfImage = data.info.create_process.base;
86 event->u.CreateProcessInfo.dwDebugInfoFileOffset = data.info.create_process.dbg_offset;
87 event->u.CreateProcessInfo.nDebugInfoSize = data.info.create_process.dbg_size;
88 event->u.CreateProcessInfo.lpThreadLocalBase = data.info.create_process.teb;
89 event->u.CreateProcessInfo.lpStartAddress = data.info.create_process.start;
90 event->u.CreateProcessInfo.lpImageName = data.info.create_process.name;
91 event->u.CreateProcessInfo.fUnicode = data.info.create_process.unicode;
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;
107 case UNLOAD_DLL_DEBUG_EVENT:
108 event->u.UnloadDll.lpBaseOfDll = data.info.unload_dll.base;
110 case OUTPUT_DEBUG_STRING_EVENT:
111 event->u.DebugString.lpDebugStringData = data.info.output_string.string;
112 event->u.DebugString.fUnicode = data.info.output_string.unicode;
113 event->u.DebugString.nDebugStringLength = data.info.output_string.length;
116 event->u.RipInfo.dwError = data.info.rip_info.error;
117 event->u.RipInfo.dwType = data.info.rip_info.type;
124 if (ret) return TRUE;
126 res = WaitForSingleObject( wait, timeout );
128 if (res != STATUS_WAIT_0) break;
130 SetLastError( ERROR_SEM_TIMEOUT );
135 /**********************************************************************
136 * ContinueDebugEvent (KERNEL32.@)
138 * Enables a thread that previously produced a debug event to continue.
142 * True if the debugger is listed as the processes owner and the process
143 * and thread are valid.
145 BOOL WINAPI ContinueDebugEvent(
146 DWORD pid, /* [in] The id of the process to continue. */
147 DWORD tid, /* [in] The id of the thread to continue. */
148 DWORD status) /* [in] The rule to apply to unhandled exeptions. */
151 SERVER_START_REQ( continue_debug_event )
155 req->status = status;
156 ret = !wine_server_call_err( req );
163 /**********************************************************************
164 * DebugActiveProcess (KERNEL32.@)
166 * Attempts to attach the debugger to a process.
170 * True if the debugger was attached to process.
172 BOOL WINAPI DebugActiveProcess(
173 DWORD pid) /* [in] The process to be debugged. */
176 SERVER_START_REQ( debug_process )
180 ret = !wine_server_call_err( req );
186 /**********************************************************************
187 * DebugActiveProcessStop (KERNEL32.@)
189 * Attempts to detach the debugger from a process.
193 * True if the debugger was detached from the process.
195 BOOL WINAPI DebugActiveProcessStop(
196 DWORD pid) /* [in] The process to be detached. */
199 SERVER_START_REQ( debug_process )
203 ret = !wine_server_call_err( req );
210 /***********************************************************************
211 * OutputDebugStringA (KERNEL32.@)
213 * Output by an application of an ascii string to a debugger (if attached)
216 void WINAPI OutputDebugStringA(
217 LPCSTR str) /* [in] The message to be logged and given to the debugger. */
219 SERVER_START_REQ( output_debug_string )
221 req->string = (void *)str;
223 req->length = strlen(str) + 1;
224 wine_server_call( req );
231 /***********************************************************************
232 * OutputDebugStringW (KERNEL32.@)
234 * Output by an application of a unicode string to a debugger (if attached)
237 void WINAPI OutputDebugStringW(
238 LPCWSTR str) /* [in] The message to be logged and given to the debugger. */
240 SERVER_START_REQ( output_debug_string )
242 req->string = (void *)str;
244 req->length = (lstrlenW(str) + 1) * sizeof(WCHAR);
245 wine_server_call( req );
248 WARN("%s\n", debugstr_w(str));
252 /***********************************************************************
253 * OutputDebugString (KERNEL.115)
255 * Output by a 16 bit application of an ascii string to a debugger (if attached)
258 void WINAPI OutputDebugString16(
259 LPCSTR str) /* [in] The message to be logged and given to the debugger. */
261 OutputDebugStringA( str );
265 /***********************************************************************
266 * DebugBreak (KERNEL32.@)
268 * Raises an exception so that a debugger (if attached)
269 * can take some action.
271 void WINAPI DebugBreak(void)
276 /***********************************************************************
277 * DebugBreakProcess (KERNEL32.@)
279 * Raises an exception so that a debugger (if attached)
280 * can take some action. Same as DebugBreak, but applies to any process.
282 BOOL WINAPI DebugBreakProcess(HANDLE hProc)
286 TRACE("(%p)\n", hProc);
288 SERVER_START_REQ( debug_break )
291 ret = !wine_server_call_err( req );
292 self = ret && reply->self;
295 if (self) DbgBreakPoint();
300 /***********************************************************************
301 * DebugBreak (KERNEL.203)
303 * Raises an expection in a 16 bit application so that a debugger (if attached)
304 * can take some action.
308 * Only 386 compatible processors implemented.
310 void WINAPI DebugBreak16(
311 CONTEXT86 *context) /* [in/out] A pointer to the 386 compatible processor state. */
314 EXCEPTION_RECORD rec;
316 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
317 rec.ExceptionFlags = 0;
318 rec.ExceptionRecord = NULL;
319 rec.ExceptionAddress = (LPVOID)context->Eip;
320 rec.NumberParameters = 0;
321 NtRaiseException( &rec, context, TRUE );
322 #endif /* defined(__i386__) */
326 /***********************************************************************
327 * IsDebuggerPresent (KERNEL32.@)
329 * Allows a process to determine if there is a debugger attached.
333 * True if there is a debugger attached.
335 BOOL WINAPI IsDebuggerPresent(void)
337 return NtCurrentTeb()->Peb->BeingDebugged;
341 /***********************************************************************
342 * _DebugOutput (KERNEL.328)
344 void WINAPIV _DebugOutput( WORD flags, LPCSTR spec, VA_LIST16 valist )
348 /* Decode caller address */
349 if (!GetModuleName16( GetExePtr(CURRENT_STACK16->cs), caller, sizeof(caller) ))
350 sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
352 /* FIXME: cannot use wvsnprintf16 from kernel */
353 /* wvsnprintf16( temp, sizeof(temp), spec, valist ); */
356 FIXME("%s %04x %s\n", caller, flags, debugstr_a(spec) );
359 /***********************************************************************
360 * DebugSetProcessKillOnExit (KERNEL32.@)
362 * Let a debugger decide whether a debuggee will be killed upon debugger
365 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
369 SERVER_START_REQ( set_debugger_kill_on_exit )
371 req->kill_on_exit = kill;
372 ret = !wine_server_call_err( req );