1 /*
2 * Win32 debugger functions
3 *
4 * Copyright (C) 1999 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23
24 #include "winerror.h"
25 #include "wine/server.h"
26 #include "kernel_private.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(debugstr);
30
31
32 /******************************************************************************
33 * WaitForDebugEvent (KERNEL32.@)
34 *
35 * Waits for a debugging event to occur in a process being debugged before
36 * filling out the debug event structure.
37 *
38 * PARAMS
39 * event [O] Address of structure for event information.
40 * timeout [I] Number of milliseconds to wait for event.
41 *
42 * RETURNS
43 *
44 * Returns true if a debug event occurred and false if the call timed out.
45 */
46 BOOL WINAPI WaitForDebugEvent(
47 LPDEBUG_EVENT event,
48 DWORD timeout)
49 {
50 BOOL ret;
51 DWORD i, res;
52
53 for (;;)
54 {
55 HANDLE wait = 0;
56 debug_event_t data;
57 SERVER_START_REQ( wait_debug_event )
58 {
59 req->get_handle = (timeout != 0);
60 wine_server_set_reply( req, &data, sizeof(data) );
61 if (!(ret = !wine_server_call_err( req ))) goto done;
62
63 if (!wine_server_reply_size(reply)) /* timeout */
64 {
65 wait = wine_server_ptr_handle( reply->wait );
66 ret = FALSE;
67 goto done;
68 }
69 event->dwDebugEventCode = data.code;
70 event->dwProcessId = (DWORD)reply->pid;
71 event->dwThreadId = (DWORD)reply->tid;
72 switch(data.code)
73 {
74 case EXCEPTION_DEBUG_EVENT:
75 event->u.Exception.dwFirstChance = data.exception.first;
76 event->u.Exception.ExceptionRecord.ExceptionCode = data.exception.exc_code;
77 event->u.Exception.ExceptionRecord.ExceptionFlags = data.exception.flags;
78 event->u.Exception.ExceptionRecord.ExceptionRecord = wine_server_get_ptr( data.exception.record );
79 event->u.Exception.ExceptionRecord.ExceptionAddress = wine_server_get_ptr( data.exception.address );
80 event->u.Exception.ExceptionRecord.NumberParameters = data.exception.nb_params;
81 for (i = 0; i < data.exception.nb_params; i++)
82 event->u.Exception.ExceptionRecord.ExceptionInformation[i] = data.exception.params[i];
83 break;
84 case CREATE_THREAD_DEBUG_EVENT:
85 event->u.CreateThread.hThread = wine_server_ptr_handle( data.create_thread.handle );
86 event->u.CreateThread.lpThreadLocalBase = wine_server_get_ptr( data.create_thread.teb );
87 event->u.CreateThread.lpStartAddress = wine_server_get_ptr( data.create_thread.start );
88 break;
89 case CREATE_PROCESS_DEBUG_EVENT:
90 event->u.CreateProcessInfo.hFile = wine_server_ptr_handle( data.create_process.file );
91 event->u.CreateProcessInfo.hProcess = wine_server_ptr_handle( data.create_process.process );
92 event->u.CreateProcessInfo.hThread = wine_server_ptr_handle( data.create_process.thread );
93 event->u.CreateProcessInfo.lpBaseOfImage = wine_server_get_ptr( data.create_process.base );
94 event->u.CreateProcessInfo.dwDebugInfoFileOffset = data.create_process.dbg_offset;
95 event->u.CreateProcessInfo.nDebugInfoSize = data.create_process.dbg_size;
96 event->u.CreateProcessInfo.lpThreadLocalBase = wine_server_get_ptr( data.create_process.teb );
97 event->u.CreateProcessInfo.lpStartAddress = wine_server_get_ptr( data.create_process.start );
98 event->u.CreateProcessInfo.lpImageName = wine_server_get_ptr( data.create_process.name );
99 event->u.CreateProcessInfo.fUnicode = data.create_process.unicode;
100 break;
101 case EXIT_THREAD_DEBUG_EVENT:
102 event->u.ExitThread.dwExitCode = data.exit.exit_code;
103 break;
104 case EXIT_PROCESS_DEBUG_EVENT:
105 event->u.ExitProcess.dwExitCode = data.exit.exit_code;
106 break;
107 case LOAD_DLL_DEBUG_EVENT:
108 event->u.LoadDll.hFile = wine_server_ptr_handle( data.load_dll.handle );
109 event->u.LoadDll.lpBaseOfDll = wine_server_get_ptr( data.load_dll.base );
110 event->u.LoadDll.dwDebugInfoFileOffset = data.load_dll.dbg_offset;
111 event->u.LoadDll.nDebugInfoSize = data.load_dll.dbg_size;
112 event->u.LoadDll.lpImageName = wine_server_get_ptr( data.load_dll.name );
113 event->u.LoadDll.fUnicode = data.load_dll.unicode;
114 break;
115 case UNLOAD_DLL_DEBUG_EVENT:
116 event->u.UnloadDll.lpBaseOfDll = wine_server_get_ptr( data.unload_dll.base );
117 break;
118 case OUTPUT_DEBUG_STRING_EVENT:
119 event->u.DebugString.lpDebugStringData = wine_server_get_ptr( data.output_string.string );
120 event->u.DebugString.fUnicode = FALSE;
121 event->u.DebugString.nDebugStringLength = data.output_string.length;
122 break;
123 case RIP_EVENT:
124 event->u.RipInfo.dwError = data.rip_info.error;
125 event->u.RipInfo.dwType = data.rip_info.type;
126 break;
127 }
128 done:
129 /* nothing */ ;
130 }
131 SERVER_END_REQ;
132 if (ret) return TRUE;
133 if (!wait) break;
134 res = WaitForSingleObject( wait, timeout );
135 CloseHandle( wait );
136 if (res != STATUS_WAIT_0) break;
137 }
138 SetLastError( ERROR_SEM_TIMEOUT );
139 return FALSE;
140 }
141
142
143 /**********************************************************************
144 * ContinueDebugEvent (KERNEL32.@)
145 *
146 * Enables a thread that previously produced a debug event to continue.
147 *
148 * PARAMS
149 * pid [I] The id of the process to continue.
150 * tid [I] The id of the thread to continue.
151 * status [I] The rule to apply to unhandled exeptions.
152 *
153 * RETURNS
154 *
155 * True if the debugger is listed as the processes owner and the process
156 * and thread are valid.
157 */
158 BOOL WINAPI ContinueDebugEvent(
159 DWORD pid,
160 DWORD tid,
161 DWORD status)
162 {
163 BOOL ret;
164 SERVER_START_REQ( continue_debug_event )
165 {
166 req->pid = pid;
167 req->tid = tid;
168 req->status = status;
169 ret = !wine_server_call_err( req );
170 }
171 SERVER_END_REQ;
172 return ret;
173 }
174
175
176 /**********************************************************************
177 * DebugActiveProcess (KERNEL32.@)
178 *
179 * Attempts to attach the debugger to a process.
180 *
181 * PARAMS
182 * pid [I] The process to be debugged.
183 *
184 * RETURNS
185 *
186 * True if the debugger was attached to process.
187 */
188 BOOL WINAPI DebugActiveProcess( DWORD pid )
189 {
190 BOOL ret;
191 SERVER_START_REQ( debug_process )
192 {
193 req->pid = pid;
194 req->attach = 1;
195 ret = !wine_server_call_err( req );
196 }
197 SERVER_END_REQ;
198 return ret;
199 }
200
201 /**********************************************************************
202 * DebugActiveProcessStop (KERNEL32.@)
203 *
204 * Attempts to detach the debugger from a process.
205 *
206 * PARAMS
207 * pid [I] The process to be detached.
208 *
209 * RETURNS
210 *
211 * True if the debugger was detached from the process.
212 */
213 BOOL WINAPI DebugActiveProcessStop( DWORD pid )
214 {
215 BOOL ret;
216 SERVER_START_REQ( debug_process )
217 {
218 req->pid = pid;
219 req->attach = 0;
220 ret = !wine_server_call_err( req );
221 }
222 SERVER_END_REQ;
223 return ret;
224 }
225
226
227 /***********************************************************************
228 * OutputDebugStringA (KERNEL32.@)
229 *
230 * Output by an application of an ascii string to a debugger (if attached)
231 * and program log.
232 *
233 * PARAMS
234 * str [I] The message to be logged and given to the debugger.
235 *
236 * RETURNS
237 *
238 * Nothing.
239 */
240 void WINAPI OutputDebugStringA( LPCSTR str )
241 {
242 static HANDLE DBWinMutex = NULL;
243 static BOOL mutex_inited = FALSE;
244
245 /* send string to attached debugger */
246 SERVER_START_REQ( output_debug_string )
247 {
248 req->string = wine_server_client_ptr( str );
249 req->length = strlen(str) + 1;
250 wine_server_call( req );
251 }
252 SERVER_END_REQ;
253
254 WARN("%s\n", str);
255
256 /* send string to a system-wide monitor */
257 /* FIXME should only send to monitor if no debuggers are attached */
258
259 if (!mutex_inited)
260 {
261 /* first call to OutputDebugString, initialize mutex handle */
262 static const WCHAR mutexname[] = {'D','B','W','i','n','M','u','t','e','x',0};
263 HANDLE mutex = CreateMutexExW( NULL, mutexname, 0, SYNCHRONIZE );
264 if (mutex)
265 {
266 if (InterlockedCompareExchangePointer( &DBWinMutex, mutex, 0 ) != 0)
267 {
268 /* someone beat us here... */
269 CloseHandle( mutex );
270 }
271 }
272 mutex_inited = TRUE;
273 }
274
275 if (DBWinMutex)
276 {
277 static const WCHAR shmname[] = {'D','B','W','I','N','_','B','U','F','F','E','R',0};
278 static const WCHAR eventbuffername[] = {'D','B','W','I','N','_','B','U','F','F','E','R','_','R','E','A','D','Y',0};
279 static const WCHAR eventdataname[] = {'D','B','W','I','N','_','D','A','T','A','_','R','E','A','D','Y',0};
280 HANDLE mapping;
281
282 mapping = OpenFileMappingW( FILE_MAP_WRITE, FALSE, shmname );
283 if (mapping)
284 {
285 LPVOID buffer;
286 HANDLE eventbuffer, eventdata;
287
288 buffer = MapViewOfFile( mapping, FILE_MAP_WRITE, 0, 0, 0 );
289 eventbuffer = OpenEventW( SYNCHRONIZE, FALSE, eventbuffername );
290 eventdata = OpenEventW( EVENT_MODIFY_STATE, FALSE, eventdataname );
291
292 if (buffer && eventbuffer && eventdata)
293 {
294 /* monitor is present, synchronize with other OutputDebugString invokations */
295 WaitForSingleObject( DBWinMutex, INFINITE );
296
297 /* acquire control over the buffer */
298 if (WaitForSingleObject( eventbuffer, 10000 ) == WAIT_OBJECT_0)
299 {
300 int str_len;
301 struct _mon_buffer_t {
302 DWORD pid;
303 char buffer[1];
304 } *mon_buffer = (struct _mon_buffer_t*) buffer;
305
306 str_len = strlen( str );
307 if (str_len > (4096 - sizeof(DWORD) - 1))
308 str_len = 4096 - sizeof(DWORD) - 1;
309
310 mon_buffer->pid = GetCurrentProcessId();
311 memcpy( mon_buffer->buffer, str, str_len );
312 mon_buffer->buffer[str_len] = 0;
313
314 /* signal data ready */
315 SetEvent( eventdata );
316 }
317 ReleaseMutex( DBWinMutex );
318 }
319
320 if (buffer)
321 UnmapViewOfFile( buffer );
322 if (eventbuffer)
323 CloseHandle( eventbuffer );
324 if (eventdata)
325 CloseHandle( eventdata );
326 CloseHandle( mapping );
327 }
328 }
329 }
330
331
332 /***********************************************************************
333 * OutputDebugStringW (KERNEL32.@)
334 *
335 * Output by an application of a unicode string to a debugger (if attached)
336 * and program log.
337 *
338 * PARAMS
339 * str [I] The message to be logged and given to the debugger.
340 *
341 * RETURNS
342 *
343 * Nothing.
344 */
345 void WINAPI OutputDebugStringW( LPCWSTR str )
346 {
347 UNICODE_STRING strW;
348 STRING strA;
349
350 RtlInitUnicodeString( &strW, str );
351 if (!RtlUnicodeStringToAnsiString( &strA, &strW, TRUE ))
352 {
353 OutputDebugStringA( strA.Buffer );
354 RtlFreeAnsiString( &strA );
355 }
356 }
357
358
359 /***********************************************************************
360 * DebugBreak (KERNEL32.@)
361 *
362 * Raises an exception so that a debugger (if attached)
363 * can take some action.
364 *
365 * PARAMS
366 *
367 * RETURNS
368 */
369 void WINAPI DebugBreak(void)
370 {
371 DbgBreakPoint();
372 }
373
374 /***********************************************************************
375 * DebugBreakProcess (KERNEL32.@)
376 *
377 * Raises an exception so that a debugger (if attached)
378 * can take some action. Same as DebugBreak, but applies to any process.
379 *
380 * PARAMS
381 * hProc [I] Process to break into.
382 *
383 * RETURNS
384 *
385 * True if successful.
386 */
387 BOOL WINAPI DebugBreakProcess(HANDLE hProc)
388 {
389 BOOL ret, self;
390
391 TRACE("(%p)\n", hProc);
392
393 SERVER_START_REQ( debug_break )
394 {
395 req->handle = wine_server_obj_handle( hProc );
396 ret = !wine_server_call_err( req );
397 self = ret && reply->self;
398 }
399 SERVER_END_REQ;
400 if (self) DbgBreakPoint();
401 return ret;
402 }
403
404
405 /***********************************************************************
406 * IsDebuggerPresent (KERNEL32.@)
407 *
408 * Allows a process to determine if there is a debugger attached.
409 *
410 * PARAMS
411 *
412 * RETURNS
413 *
414 * True if there is a debugger attached.
415 */
416 BOOL WINAPI IsDebuggerPresent(void)
417 {
418 return NtCurrentTeb()->Peb->BeingDebugged;
419 }
420
421 /***********************************************************************
422 * CheckRemoteDebuggerPresent (KERNEL32.@)
423 *
424 * Allows a process to determine if there is a remote debugger
425 * attached.
426 *
427 * PARAMS
428 *
429 * RETURNS
430 *
431 * TRUE because it is a stub.
432 */
433 BOOL WINAPI CheckRemoteDebuggerPresent(HANDLE process, PBOOL DebuggerPresent)
434 {
435 if(!process || !DebuggerPresent)
436 {
437 SetLastError(ERROR_INVALID_PARAMETER);
438 return FALSE;
439 }
440 FIXME("(%p)->(%p): Stub!\n", process, DebuggerPresent);
441 *DebuggerPresent = FALSE;
442 return TRUE;
443 }
444
445 /***********************************************************************
446 * DebugSetProcessKillOnExit (KERNEL32.@)
447 *
448 * Let a debugger decide whether a debuggee will be killed upon debugger
449 * termination.
450 *
451 * PARAMS
452 * kill [I] If set to true then kill the process on exit.
453 *
454 * RETURNS
455 * True if successful, false otherwise.
456 */
457 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
458 {
459 BOOL ret = FALSE;
460
461 SERVER_START_REQ( set_debugger_kill_on_exit )
462 {
463 req->kill_on_exit = kill;
464 ret = !wine_server_call_err( req );
465 }
466 SERVER_END_REQ;
467 return ret;
468 }
469
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.