ole32: Remove superfluous pointer casts.
[wine] / dlls / kernel32 / debugger.c
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/winbase16.h"
26 #include "wine/server.h"
27 #include "kernel_private.h"
28 #include "wine/debug.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(debugstr);
31
32
33 /******************************************************************************
34  *           WaitForDebugEvent   (KERNEL32.@)
35  *
36  *  Waits for a debugging event to occur in a process being debugged before
37  *  filling out the debug event structure.
38  *
39  * PARAMS
40  *  event   [O] Address of structure for event information.
41  *  timeout [I] Number of milliseconds to wait for event.
42  *
43  * RETURNS
44  *
45  *  Returns true if a debug event occurred and false if the call timed out.
46  */
47 BOOL WINAPI WaitForDebugEvent(
48     LPDEBUG_EVENT event,
49     DWORD         timeout)
50 {
51     BOOL ret;
52     DWORD i, res;
53
54     for (;;)
55     {
56         HANDLE wait = 0;
57         debug_event_t data;
58         SERVER_START_REQ( wait_debug_event )
59         {
60             req->get_handle = (timeout != 0);
61             wine_server_set_reply( req, &data, sizeof(data) );
62             if (!(ret = !wine_server_call_err( req ))) goto done;
63
64             if (!wine_server_reply_size(reply))  /* timeout */
65             {
66                 wait = wine_server_ptr_handle( reply->wait );
67                 ret = FALSE;
68                 goto done;
69             }
70             event->dwDebugEventCode = data.code;
71             event->dwProcessId      = (DWORD)reply->pid;
72             event->dwThreadId       = (DWORD)reply->tid;
73             switch(data.code)
74             {
75             case EXCEPTION_DEBUG_EVENT:
76                 event->u.Exception.dwFirstChance = data.exception.first;
77                 event->u.Exception.ExceptionRecord.ExceptionCode    = data.exception.exc_code;
78                 event->u.Exception.ExceptionRecord.ExceptionFlags   = data.exception.flags;
79                 event->u.Exception.ExceptionRecord.ExceptionRecord  = wine_server_get_ptr( data.exception.record );
80                 event->u.Exception.ExceptionRecord.ExceptionAddress = wine_server_get_ptr( data.exception.address );
81                 event->u.Exception.ExceptionRecord.NumberParameters = data.exception.nb_params;
82                 for (i = 0; i < data.exception.nb_params; i++)
83                     event->u.Exception.ExceptionRecord.ExceptionInformation[i] = data.exception.params[i];
84                 break;
85             case CREATE_THREAD_DEBUG_EVENT:
86                 event->u.CreateThread.hThread           = wine_server_ptr_handle( data.create_thread.handle );
87                 event->u.CreateThread.lpThreadLocalBase = wine_server_get_ptr( data.create_thread.teb );
88                 event->u.CreateThread.lpStartAddress    = wine_server_get_ptr( data.create_thread.start );
89                 break;
90             case CREATE_PROCESS_DEBUG_EVENT:
91                 event->u.CreateProcessInfo.hFile                 = wine_server_ptr_handle( data.create_process.file );
92                 event->u.CreateProcessInfo.hProcess              = wine_server_ptr_handle( data.create_process.process );
93                 event->u.CreateProcessInfo.hThread               = wine_server_ptr_handle( data.create_process.thread );
94                 event->u.CreateProcessInfo.lpBaseOfImage         = wine_server_get_ptr( data.create_process.base );
95                 event->u.CreateProcessInfo.dwDebugInfoFileOffset = data.create_process.dbg_offset;
96                 event->u.CreateProcessInfo.nDebugInfoSize        = data.create_process.dbg_size;
97                 event->u.CreateProcessInfo.lpThreadLocalBase     = wine_server_get_ptr( data.create_process.teb );
98                 event->u.CreateProcessInfo.lpStartAddress        = wine_server_get_ptr( data.create_process.start );
99                 event->u.CreateProcessInfo.lpImageName           = wine_server_get_ptr( data.create_process.name );
100                 event->u.CreateProcessInfo.fUnicode              = data.create_process.unicode;
101                 break;
102             case EXIT_THREAD_DEBUG_EVENT:
103                 event->u.ExitThread.dwExitCode = data.exit.exit_code;
104                 break;
105             case EXIT_PROCESS_DEBUG_EVENT:
106                 event->u.ExitProcess.dwExitCode = data.exit.exit_code;
107                 break;
108             case LOAD_DLL_DEBUG_EVENT:
109                 event->u.LoadDll.hFile                 = wine_server_ptr_handle( data.load_dll.handle );
110                 event->u.LoadDll.lpBaseOfDll           = wine_server_get_ptr( data.load_dll.base );
111                 event->u.LoadDll.dwDebugInfoFileOffset = data.load_dll.dbg_offset;
112                 event->u.LoadDll.nDebugInfoSize        = data.load_dll.dbg_size;
113                 event->u.LoadDll.lpImageName           = wine_server_get_ptr( data.load_dll.name );
114                 event->u.LoadDll.fUnicode              = data.load_dll.unicode;
115                 break;
116             case UNLOAD_DLL_DEBUG_EVENT:
117                 event->u.UnloadDll.lpBaseOfDll = wine_server_get_ptr( data.unload_dll.base );
118                 break;
119             case OUTPUT_DEBUG_STRING_EVENT:
120                 event->u.DebugString.lpDebugStringData  = wine_server_get_ptr( data.output_string.string );
121                 event->u.DebugString.fUnicode           = data.output_string.unicode;
122                 event->u.DebugString.nDebugStringLength = data.output_string.length;
123                 break;
124             case RIP_EVENT:
125                 event->u.RipInfo.dwError = data.rip_info.error;
126                 event->u.RipInfo.dwType  = data.rip_info.type;
127                 break;
128             }
129         done:
130             /* nothing */ ;
131         }
132         SERVER_END_REQ;
133         if (ret) return TRUE;
134         if (!wait) break;
135         res = WaitForSingleObject( wait, timeout );
136         CloseHandle( wait );
137         if (res != STATUS_WAIT_0) break;
138     }
139     SetLastError( ERROR_SEM_TIMEOUT );
140     return FALSE;
141 }
142
143
144 /**********************************************************************
145  *           ContinueDebugEvent   (KERNEL32.@)
146  *
147  *  Enables a thread that previously produced a debug event to continue.
148  *
149  * PARAMS
150  *  pid    [I] The id of the process to continue.
151  *  tid    [I] The id of the thread to continue.
152  *  status [I] The rule to apply to unhandled exeptions.
153  *
154  * RETURNS
155  *
156  *  True if the debugger is listed as the processes owner and the process
157  *  and thread are valid.
158  */
159 BOOL WINAPI ContinueDebugEvent(
160     DWORD pid,
161     DWORD tid,
162     DWORD status)
163 {
164     BOOL ret;
165     SERVER_START_REQ( continue_debug_event )
166     {
167         req->pid    = pid;
168         req->tid    = tid;
169         req->status = status;
170         ret = !wine_server_call_err( req );
171     }
172     SERVER_END_REQ;
173     return ret;
174 }
175
176
177 /**********************************************************************
178  *           DebugActiveProcess   (KERNEL32.@)
179  *
180  *  Attempts to attach the debugger to a process.
181  *
182  * PARAMS
183  *  pid [I] The process to be debugged.
184  *
185  * RETURNS
186  *
187  *  True if the debugger was attached to process.
188  */
189 BOOL WINAPI DebugActiveProcess( DWORD pid )
190 {
191     BOOL ret;
192     SERVER_START_REQ( debug_process )
193     {
194         req->pid = pid;
195         req->attach = 1;
196         ret = !wine_server_call_err( req );
197     }
198     SERVER_END_REQ;
199     return ret;
200 }
201
202 /**********************************************************************
203  *           DebugActiveProcessStop   (KERNEL32.@)
204  *
205  *  Attempts to detach the debugger from a process.
206  *
207  * PARAMS
208  *  pid [I] The process to be detached.
209  *
210  * RETURNS
211  *
212  *  True if the debugger was detached from the process.
213  */
214 BOOL WINAPI DebugActiveProcessStop( DWORD pid )
215 {
216     BOOL ret;
217     SERVER_START_REQ( debug_process )
218     {
219         req->pid = pid;
220         req->attach = 0;
221         ret = !wine_server_call_err( req );
222     }
223     SERVER_END_REQ;
224     return ret;
225 }
226
227
228 /***********************************************************************
229  *           OutputDebugStringA   (KERNEL32.@)
230  *
231  *  Output by an application of an ascii string to a debugger (if attached)
232  *  and program log.
233  *
234  * PARAMS
235  *  str [I] The message to be logged and given to the debugger.
236  *
237  * RETURNS
238  *
239  *  Nothing.
240  */
241 void WINAPI OutputDebugStringA( LPCSTR str )
242 {
243     SERVER_START_REQ( output_debug_string )
244     {
245         req->string  = wine_server_client_ptr( str );
246         req->unicode = 0;
247         req->length  = strlen(str) + 1;
248         wine_server_call( req );
249     }
250     SERVER_END_REQ;
251     WARN("%s\n", str);
252 }
253
254
255 /***********************************************************************
256  *           OutputDebugStringW   (KERNEL32.@)
257  *
258  *  Output by an application of a unicode string to a debugger (if attached)
259  *  and program log.
260  *
261  * PARAMS
262  *  str [I] The message to be logged and given to the debugger.
263  *
264  * RETURNS
265  *
266  *  Nothing.
267  */
268 void WINAPI OutputDebugStringW( LPCWSTR str )
269 {
270     SERVER_START_REQ( output_debug_string )
271     {
272         req->string  = wine_server_client_ptr( str );
273         req->unicode = 1;
274         req->length  = (lstrlenW(str) + 1) * sizeof(WCHAR);
275         wine_server_call( req );
276     }
277     SERVER_END_REQ;
278     WARN("%s\n", debugstr_w(str));
279 }
280
281
282 /***********************************************************************
283  *           OutputDebugString   (KERNEL.115)
284  *
285  *  Output by a 16 bit application of an ascii string to a debugger (if attached)
286  *  and program log.
287  *
288  * PARAMS
289  *  str [I] The message to be logged and given to the debugger.
290  *
291  * RETURNS
292  */
293 void WINAPI OutputDebugString16( LPCSTR str )
294 {
295     OutputDebugStringA( str );
296 }
297
298
299 /***********************************************************************
300  *           DebugBreak   (KERNEL32.@)
301  *
302  *  Raises an exception so that a debugger (if attached)
303  *  can take some action.
304  *
305  * PARAMS
306  *
307  * RETURNS
308  */
309 void WINAPI DebugBreak(void)
310 {
311     DbgBreakPoint();
312 }
313
314 /***********************************************************************
315  *           DebugBreakProcess   (KERNEL32.@)
316  *
317  *  Raises an exception so that a debugger (if attached)
318  *  can take some action. Same as DebugBreak, but applies to any process.
319  *
320  * PARAMS
321  *  hProc [I] Process to break into.
322  *
323  * RETURNS
324  *
325  *  True if successful.
326  */
327 BOOL WINAPI DebugBreakProcess(HANDLE hProc)
328 {
329     BOOL ret, self;
330
331     TRACE("(%p)\n", hProc);
332
333     SERVER_START_REQ( debug_break )
334     {
335         req->handle = wine_server_obj_handle( hProc );
336         ret = !wine_server_call_err( req );
337         self = ret && reply->self;
338     }
339     SERVER_END_REQ;
340     if (self) DbgBreakPoint();
341     return ret;
342 }
343
344
345 /***********************************************************************
346  *           DebugBreak   (KERNEL.203)
347  *
348  *  Raises an exception in a 16 bit application so that a debugger (if attached)
349  *  can take some action.
350  *
351  * PARAMS
352  *
353  * RETURNS
354  *
355  * BUGS
356  *
357  *  Only 386 compatible processors implemented.
358  */
359 void WINAPI DebugBreak16(
360     CONTEXT86 *context) /* [in/out] A pointer to the 386 compatible processor state. */
361 {
362 #ifdef __i386__
363     EXCEPTION_RECORD rec;
364
365     rec.ExceptionCode    = EXCEPTION_BREAKPOINT;
366     rec.ExceptionFlags   = 0;
367     rec.ExceptionRecord  = NULL;
368     rec.ExceptionAddress = (LPVOID)context->Eip;
369     rec.NumberParameters = 0;
370     NtRaiseException( &rec, context, TRUE );
371 #endif  /* defined(__i386__) */
372 }
373
374
375 /***********************************************************************
376  *           IsDebuggerPresent   (KERNEL32.@)
377  *
378  *  Allows a process to determine if there is a debugger attached.
379  *
380  * PARAMS
381  *
382  * RETURNS
383  *
384  *  True if there is a debugger attached.
385  */
386 BOOL WINAPI IsDebuggerPresent(void)
387 {
388     return NtCurrentTeb()->Peb->BeingDebugged;
389 }
390
391 /***********************************************************************
392  *           CheckRemoteDebuggerPresent   (KERNEL32.@)
393  *
394  *  Allows a process to determine if there is a remote debugger
395  *  attached.
396  *
397  * PARAMS
398  *
399  * RETURNS
400  *
401  *  TRUE because it is a stub.
402  */
403 BOOL WINAPI CheckRemoteDebuggerPresent(HANDLE process, PBOOL DebuggerPresent)
404 {
405     FIXME("(%p)->(%p): Stub!\n", process, DebuggerPresent);
406     *DebuggerPresent = FALSE;
407     return TRUE;
408 }
409
410 /***********************************************************************
411  *           DebugSetProcessKillOnExit                    (KERNEL32.@)
412  *
413  * Let a debugger decide whether a debuggee will be killed upon debugger
414  * termination.
415  *
416  * PARAMS
417  *  kill [I] If set to true then kill the process on exit.
418  *
419  * RETURNS
420  *  True if successful, false otherwise.
421  */
422 BOOL WINAPI DebugSetProcessKillOnExit(BOOL kill)
423 {
424     BOOL ret = FALSE;
425
426     SERVER_START_REQ( set_debugger_kill_on_exit )
427     {
428         req->kill_on_exit = kill;
429         ret = !wine_server_call_err( req );
430     }
431     SERVER_END_REQ;
432     return ret;
433 }