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