Removed obsolete INT_Int31Handler.
[wine/multimedia.git] / dlls / kernel / debugger.c
blobc1e95ee54870ba9ad7675b88321d032a3d4ad818
1 /*
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
21 #include <stdio.h>
22 #include <string.h>
24 #include "winerror.h"
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.
39 * RETURNS
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. */
47 BOOL ret;
48 DWORD res;
50 for (;;)
52 HANDLE wait = 0;
53 debug_event_t data;
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 */
62 wait = reply->wait;
63 ret = FALSE;
64 goto done;
66 event->dwDebugEventCode = data.code;
67 event->dwProcessId = (DWORD)reply->pid;
68 event->dwThreadId = (DWORD)reply->tid;
69 switch(data.code)
71 case EXCEPTION_DEBUG_EVENT:
72 event->u.Exception.ExceptionRecord = data.info.exception.record;
73 event->u.Exception.dwFirstChance = data.info.exception.first;
74 break;
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;
79 break;
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 break;
92 case EXIT_THREAD_DEBUG_EVENT:
93 event->u.ExitThread.dwExitCode = data.info.exit.exit_code;
94 break;
95 case EXIT_PROCESS_DEBUG_EVENT:
96 event->u.ExitProcess.dwExitCode = data.info.exit.exit_code;
97 break;
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;
105 break;
106 case UNLOAD_DLL_DEBUG_EVENT:
107 event->u.UnloadDll.lpBaseOfDll = data.info.unload_dll.base;
108 break;
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;
113 break;
114 case RIP_EVENT:
115 event->u.RipInfo.dwError = data.info.rip_info.error;
116 event->u.RipInfo.dwType = data.info.rip_info.type;
117 break;
119 done:
120 /* nothing */ ;
122 SERVER_END_REQ;
123 if (ret) return TRUE;
124 if (!wait) break;
125 res = WaitForSingleObject( wait, timeout );
126 CloseHandle( wait );
127 if (res != STATUS_WAIT_0) break;
129 SetLastError( ERROR_SEM_TIMEOUT );
130 return FALSE;
134 /**********************************************************************
135 * ContinueDebugEvent (KERNEL32.@)
137 * Enables a thread that previously produced a debug event to continue.
139 * RETURNS
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. */
149 BOOL ret;
150 SERVER_START_REQ( continue_debug_event )
152 req->pid = pid;
153 req->tid = tid;
154 req->status = status;
155 ret = !wine_server_call_err( req );
157 SERVER_END_REQ;
158 return ret;
162 /**********************************************************************
163 * DebugActiveProcess (KERNEL32.@)
165 * Attempts to attach the debugger to a process.
167 * RETURNS
169 * True if the debugger was attached to process.
171 BOOL WINAPI DebugActiveProcess(
172 DWORD pid) /* [in] The process to be debugged. */
174 BOOL ret;
175 SERVER_START_REQ( debug_process )
177 req->pid = pid;
178 req->attach = 1;
179 ret = !wine_server_call_err( req );
181 SERVER_END_REQ;
182 return ret;
185 /**********************************************************************
186 * DebugActiveProcessStop (KERNEL32.@)
188 * Attempts to detach the debugger from a process.
190 * RETURNS
192 * True if the debugger was detached from the process.
194 BOOL WINAPI DebugActiveProcessStop(
195 DWORD pid) /* [in] The process to be detached. */
197 BOOL ret;
198 SERVER_START_REQ( debug_process )
200 req->pid = pid;
201 req->attach = 0;
202 ret = !wine_server_call_err( req );
204 SERVER_END_REQ;
205 return ret;
209 /***********************************************************************
210 * OutputDebugStringA (KERNEL32.@)
212 * Output by an application of a unicode string to a debugger (if attached)
213 * and program log.
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;
221 req->unicode = 0;
222 req->length = strlen(str) + 1;
223 wine_server_call( req );
225 SERVER_END_REQ;
226 WARN("%s\n", str);
230 /***********************************************************************
231 * OutputDebugStringW (KERNEL32.@)
233 * Output by an appliccation of a unicode string to a debugger (if attached)
234 * and program log.
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;
242 req->unicode = 1;
243 req->length = (lstrlenW(str) + 1) * sizeof(WCHAR);
244 wine_server_call( req );
246 SERVER_END_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)
255 * and program log.
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)
272 DbgBreakPoint();
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)
283 BOOL ret, self;
285 TRACE("(%p)\n", hProc);
287 SERVER_START_REQ( debug_break )
289 req->handle = hProc;
290 ret = !wine_server_call_err( req );
291 self = ret && reply->self;
293 SERVER_END_REQ;
294 if (self) DbgBreakPoint();
295 return ret;
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.
305 * BUGS
307 * Only 386 compatible processors implemented.
309 void WINAPI DebugBreak16(
310 CONTEXT86 *context) /* [in/out] A pointer to the 386 compatible processor state. */
312 #ifdef __i386__
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.
330 * RETURNS
332 * True if there is a debugger attached.
334 BOOL WINAPI IsDebuggerPresent(void)
336 BOOL ret = FALSE;
337 SERVER_START_REQ( get_process_info )
339 req->handle = GetCurrentProcess();
340 if (!wine_server_call_err( req )) ret = reply->debugged;
342 SERVER_END_REQ;
343 return ret;
347 /***********************************************************************
348 * _DebugOutput (KERNEL.328)
350 void WINAPIV _DebugOutput( void )
352 VA_LIST16 valist;
353 WORD flags;
354 SEGPTR spec;
355 char caller[101];
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 ); */
368 /* Output */
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
376 * termination
378 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
380 BOOL ret = FALSE;
382 SERVER_START_REQ( set_debugger_kill_on_exit )
384 req->kill_on_exit = kill;
385 ret = !wine_server_call_err( req );
387 SERVER_END_REQ;
388 return ret;