2 * Win32 debugger functions
4 * Copyright (C) 1999 Alexandre Julliard
11 #include "wine/winbase16.h"
13 #include "stackframe.h"
14 #include "debugtools.h"
16 DEFAULT_DEBUG_CHANNEL(debugstr);
19 /******************************************************************************
20 * WaitForDebugEvent (KERNEL32.720)
22 * Waits for a debugging event to occur in a process being debugged before
23 * filling out the debug event structure.
27 * Returns true if a debug event occurred and false if the call timed out.
29 BOOL WINAPI WaitForDebugEvent(
30 LPDEBUG_EVENT event, /* [out] Address of structure for event information. */
31 DWORD timeout) /* [in] Number of milliseconds to wait for event. */
40 SERVER_START_VAR_REQ( wait_debug_event, sizeof(*data) )
42 req->get_handle = (timeout != 0);
43 if (!(ret = !SERVER_CALL_ERR())) goto done;
45 if (!server_data_size(req)) /* timeout */
51 data = server_data_ptr(req);
52 event->dwDebugEventCode = data->code;
53 event->dwProcessId = (DWORD)req->pid;
54 event->dwThreadId = (DWORD)req->tid;
57 case EXCEPTION_DEBUG_EVENT:
58 event->u.Exception.ExceptionRecord = data->info.exception.record;
59 event->u.Exception.dwFirstChance = data->info.exception.first;
61 case CREATE_THREAD_DEBUG_EVENT:
62 event->u.CreateThread.hThread = data->info.create_thread.handle;
63 event->u.CreateThread.lpThreadLocalBase = data->info.create_thread.teb;
64 event->u.CreateThread.lpStartAddress = data->info.create_thread.start;
66 case CREATE_PROCESS_DEBUG_EVENT:
67 event->u.CreateProcessInfo.hFile = data->info.create_process.file;
68 event->u.CreateProcessInfo.hProcess = data->info.create_process.process;
69 event->u.CreateProcessInfo.hThread = data->info.create_process.thread;
70 event->u.CreateProcessInfo.lpBaseOfImage = data->info.create_process.base;
71 event->u.CreateProcessInfo.dwDebugInfoFileOffset = data->info.create_process.dbg_offset;
72 event->u.CreateProcessInfo.nDebugInfoSize = data->info.create_process.dbg_size;
73 event->u.CreateProcessInfo.lpThreadLocalBase = data->info.create_process.teb;
74 event->u.CreateProcessInfo.lpStartAddress = data->info.create_process.start;
75 event->u.CreateProcessInfo.lpImageName = data->info.create_process.name;
76 event->u.CreateProcessInfo.fUnicode = data->info.create_process.unicode;
77 if (data->info.create_process.file == -1) event->u.CreateProcessInfo.hFile = 0;
79 case EXIT_THREAD_DEBUG_EVENT:
80 event->u.ExitThread.dwExitCode = data->info.exit.exit_code;
82 case EXIT_PROCESS_DEBUG_EVENT:
83 event->u.ExitProcess.dwExitCode = data->info.exit.exit_code;
85 case LOAD_DLL_DEBUG_EVENT:
86 event->u.LoadDll.hFile = data->info.load_dll.handle;
87 event->u.LoadDll.lpBaseOfDll = data->info.load_dll.base;
88 event->u.LoadDll.dwDebugInfoFileOffset = data->info.load_dll.dbg_offset;
89 event->u.LoadDll.nDebugInfoSize = data->info.load_dll.dbg_size;
90 event->u.LoadDll.lpImageName = data->info.load_dll.name;
91 event->u.LoadDll.fUnicode = data->info.load_dll.unicode;
92 if (data->info.load_dll.handle == -1) event->u.LoadDll.hFile = 0;
94 case UNLOAD_DLL_DEBUG_EVENT:
95 event->u.UnloadDll.lpBaseOfDll = data->info.unload_dll.base;
97 case OUTPUT_DEBUG_STRING_EVENT:
98 event->u.DebugString.lpDebugStringData = data->info.output_string.string;
99 event->u.DebugString.fUnicode = data->info.output_string.unicode;
100 event->u.DebugString.nDebugStringLength = data->info.output_string.length;
103 event->u.RipInfo.dwError = data->info.rip_info.error;
104 event->u.RipInfo.dwType = data->info.rip_info.type;
110 if (ret) return TRUE;
112 res = WaitForSingleObject( wait, timeout );
114 if (res != STATUS_WAIT_0) break;
116 SetLastError( ERROR_SEM_TIMEOUT );
121 /**********************************************************************
122 * ContinueDebugEvent (KERNEL32.146)
123 * ContinueDebugEvent (WIN32S16.5)
125 * Enables a thread that previously produced a debug event to continue.
129 * True if the debugger is listed as the processes owner and the process
130 * and thread are valid.
132 BOOL WINAPI ContinueDebugEvent(
133 DWORD pid, /* [in] The id of the process to continue. */
134 DWORD tid, /* [in] The id of the thread to continue. */
135 DWORD status) /* [in] The rule to apply to unhandled exeptions. */
138 SERVER_START_REQ( continue_debug_event )
140 req->pid = (void *)pid;
141 req->tid = (void *)tid;
142 req->status = status;
143 ret = !SERVER_CALL_ERR();
150 /**********************************************************************
151 * DebugActiveProcess (KERNEL32.180)
153 * Attempts to attach the dugger to a process.
157 * True if the debugger was attached to process.
159 BOOL WINAPI DebugActiveProcess(
160 DWORD pid) /* [in] The process to be debugged. */
163 SERVER_START_REQ( debug_process )
165 req->pid = (void *)pid;
166 ret = !SERVER_CALL_ERR();
173 /***********************************************************************
174 * OutputDebugStringA (KERNEL.115)
175 * OutputDebugStringA (KERNEL32.548)
177 * Output by an application of a unicode string to a debugger (if attached)
180 void WINAPI OutputDebugStringA(
181 LPCSTR str) /* [in] The message to be logged and given to the debugger. */
183 SERVER_START_REQ( output_debug_string )
185 req->string = (void *)str;
187 req->length = strlen(str) + 1;
195 /***********************************************************************
196 * OutputDebugStringW (KERNEL32.549)
198 * Output by an appliccation of a unicode string to a debugger (if attached)
201 void WINAPI OutputDebugStringW(
202 LPCWSTR str) /* [in] The message to be logged and given to the debugger. */
204 SERVER_START_REQ( output_debug_string )
206 req->string = (void *)str;
208 req->length = (lstrlenW(str) + 1) * sizeof(WCHAR);
212 WARN("%s\n", debugstr_w(str));
216 /***********************************************************************
217 * OutputDebugString16 (KERNEL.115)
219 * Output by a 16 bit application of an ascii string to a debugger (if attached)
222 void WINAPI OutputDebugString16(
223 LPCSTR str) /* [in] The message to be logged and given to the debugger. */
225 OutputDebugStringA( str );
229 /***********************************************************************
230 * DebugBreak (KERNEL32.181)
232 * Raises an exception so that a debugger (if attached)
233 * can take some action.
235 void WINAPI DebugBreak(void)
241 /***********************************************************************
242 * DebugBreak16 (KERNEL.203)
244 * Raises an expection in a 16 bit application so that a debugger (if attached)
245 * can take some action.
249 * Only 386 compatible processors implemented.
251 void WINAPI DebugBreak16(
252 CONTEXT86 *context) /* [in/out] A pointer to the 386 compatible processor state. */
255 EXCEPTION_RECORD rec;
257 rec.ExceptionCode = EXCEPTION_BREAKPOINT;
258 rec.ExceptionFlags = 0;
259 rec.ExceptionRecord = NULL;
260 rec.ExceptionAddress = (LPVOID)context->Eip;
261 rec.NumberParameters = 0;
262 NtRaiseException( &rec, context, TRUE );
263 #endif /* defined(__i386__) */
267 /***********************************************************************
268 * IsDebuggerPresent (KERNEL32.827)
270 * Allows a process to determine if there is a debugger attached.
274 * True if there is a debugger attached.
276 BOOL WINAPI IsDebuggerPresent(void)
279 SERVER_START_REQ( get_process_info )
281 req->handle = GetCurrentProcess();
282 if (!SERVER_CALL_ERR()) ret = req->debugged;
289 /***********************************************************************
290 * _DebugOutput (KERNEL.328)
292 void WINAPIV _DebugOutput( void )
299 /* Decode caller address */
300 if (!GetModuleName16( GetExePtr(CURRENT_STACK16->cs), caller, sizeof(caller) ))
301 sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
303 /* Build debug message string */
304 VA_START16( valist );
305 flags = VA_ARG16( valist, WORD );
306 spec = VA_ARG16( valist, SEGPTR );
307 /* FIXME: cannot use wvsnprintf16 from kernel */
308 /* wvsnprintf16( temp, sizeof(temp), MapSL(spec), valist ); */
311 FIXME("%s %04x %s\n", caller, flags, debugstr_a(MapSL(spec)) );