Replaced PTR_SEG_TO_LIN macro by exported MapSL function.
[wine] / dlls / kernel / debugger.c
1 /*
2  * Win32 debugger functions
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include <stdio.h>
8 #include <string.h>
9
10 #include "winerror.h"
11 #include "wine/winbase16.h"
12 #include "server.h"
13 #include "stackframe.h"
14 #include "debugtools.h"
15
16 DEFAULT_DEBUG_CHANNEL(debugstr);
17
18
19 /******************************************************************************
20  *           WaitForDebugEvent   (KERNEL32.720)
21  *
22  * Waits for a debugging event to occur in a process being debugged
23  *
24  * PARAMS
25  *    event   [I] Address of structure for event information
26  *    timeout [I] Number of milliseconds to wait for event
27  *
28  * RETURNS STD
29  */
30 BOOL WINAPI WaitForDebugEvent( LPDEBUG_EVENT event, DWORD timeout )
31 {
32     BOOL ret;
33     SERVER_START_REQ
34     {
35         debug_event_t *data;
36         struct wait_debug_event_request *req = server_alloc_req( sizeof(*req), sizeof(*data) );
37
38         req->timeout = timeout;
39         if (!(ret = !server_call( REQ_WAIT_DEBUG_EVENT ))) goto done;
40
41         if (!server_data_size(req))  /* timeout */
42         {
43             SetLastError( ERROR_SEM_TIMEOUT );
44             ret = FALSE;
45             goto done;
46         }
47         data = server_data_ptr(req);
48         event->dwDebugEventCode = data->code;
49         event->dwProcessId      = (DWORD)req->pid;
50         event->dwThreadId       = (DWORD)req->tid;
51         switch(data->code)
52         {
53         case EXCEPTION_DEBUG_EVENT:
54             event->u.Exception.ExceptionRecord = data->info.exception.record;
55             event->u.Exception.dwFirstChance   = data->info.exception.first;
56             break;
57         case CREATE_THREAD_DEBUG_EVENT:
58             event->u.CreateThread.hThread           = data->info.create_thread.handle;
59             event->u.CreateThread.lpThreadLocalBase = data->info.create_thread.teb;
60             event->u.CreateThread.lpStartAddress    = data->info.create_thread.start;
61             break;
62         case CREATE_PROCESS_DEBUG_EVENT:
63             event->u.CreateProcessInfo.hFile                 = data->info.create_process.file;
64             event->u.CreateProcessInfo.hProcess              = data->info.create_process.process;
65             event->u.CreateProcessInfo.hThread               = data->info.create_process.thread;
66             event->u.CreateProcessInfo.lpBaseOfImage         = data->info.create_process.base;
67             event->u.CreateProcessInfo.dwDebugInfoFileOffset = data->info.create_process.dbg_offset;
68             event->u.CreateProcessInfo.nDebugInfoSize        = data->info.create_process.dbg_size;
69             event->u.CreateProcessInfo.lpThreadLocalBase     = data->info.create_process.teb;
70             event->u.CreateProcessInfo.lpStartAddress        = data->info.create_process.start;
71             event->u.CreateProcessInfo.lpImageName           = data->info.create_process.name;
72             event->u.CreateProcessInfo.fUnicode              = data->info.create_process.unicode;
73             if (data->info.create_process.file == -1) event->u.CreateProcessInfo.hFile = 0;
74             break;
75         case EXIT_THREAD_DEBUG_EVENT:
76             event->u.ExitThread.dwExitCode = data->info.exit.exit_code;
77             break;
78         case EXIT_PROCESS_DEBUG_EVENT:
79             event->u.ExitProcess.dwExitCode = data->info.exit.exit_code;
80             break;
81         case LOAD_DLL_DEBUG_EVENT:
82             event->u.LoadDll.hFile                 = data->info.load_dll.handle;
83             event->u.LoadDll.lpBaseOfDll           = data->info.load_dll.base;
84             event->u.LoadDll.dwDebugInfoFileOffset = data->info.load_dll.dbg_offset;
85             event->u.LoadDll.nDebugInfoSize        = data->info.load_dll.dbg_size;
86             event->u.LoadDll.lpImageName           = data->info.load_dll.name;
87             event->u.LoadDll.fUnicode              = data->info.load_dll.unicode;
88             if (data->info.load_dll.handle == -1) event->u.LoadDll.hFile = 0;
89             break;
90         case UNLOAD_DLL_DEBUG_EVENT:
91             event->u.UnloadDll.lpBaseOfDll = data->info.unload_dll.base;
92             break;
93         case OUTPUT_DEBUG_STRING_EVENT:
94             event->u.DebugString.lpDebugStringData  = data->info.output_string.string;
95             event->u.DebugString.fUnicode           = data->info.output_string.unicode;
96             event->u.DebugString.nDebugStringLength = data->info.output_string.length;
97             break;
98         case RIP_EVENT:
99             event->u.RipInfo.dwError = data->info.rip_info.error;
100             event->u.RipInfo.dwType  = data->info.rip_info.type;
101             break;
102         default:
103             server_protocol_error( "WaitForDebugEvent: bad code %d\n", data->code );
104         }
105     done:
106     }
107     SERVER_END_REQ;
108     return ret;
109 }
110
111
112 /**********************************************************************
113  *           ContinueDebugEvent   (KERNEL32.146)
114  */
115 BOOL WINAPI ContinueDebugEvent( DWORD pid, DWORD tid, DWORD status )
116 {
117     BOOL ret;
118     SERVER_START_REQ
119     {
120         struct continue_debug_event_request *req = server_alloc_req( sizeof(*req), 0 );
121         req->pid    = (void *)pid;
122         req->tid    = (void *)tid;
123         req->status = status;
124         ret = !server_call( REQ_CONTINUE_DEBUG_EVENT );
125     }
126     SERVER_END_REQ;
127     return ret;
128 }
129
130
131 /**********************************************************************
132  *           DebugActiveProcess   (KERNEL32.180)
133  */
134 BOOL WINAPI DebugActiveProcess( DWORD pid )
135 {
136     BOOL ret;
137     SERVER_START_REQ
138     {
139         struct debug_process_request *req = server_alloc_req( sizeof(*req), 0 );
140         req->pid = (void *)pid;
141         ret = !server_call( REQ_DEBUG_PROCESS );
142     }
143     SERVER_END_REQ;
144     return ret;
145 }
146
147
148 /***********************************************************************
149  *           OutputDebugStringA   (KERNEL32.548)
150  */
151 void WINAPI OutputDebugStringA( LPCSTR str )
152 {
153     SERVER_START_REQ
154     {
155         struct output_debug_string_request *req = server_alloc_req( sizeof(*req), 0 );
156         req->string  = (void *)str;
157         req->unicode = 0;
158         req->length  = strlen(str) + 1;
159         server_call_noerr( REQ_OUTPUT_DEBUG_STRING );
160     }
161     SERVER_END_REQ;
162     WARN("%s\n", str);
163 }
164
165
166 /***********************************************************************
167  *           OutputDebugStringW   (KERNEL32.549)
168  */
169 void WINAPI OutputDebugStringW( LPCWSTR str )
170 {
171     SERVER_START_REQ
172     {
173         struct output_debug_string_request *req = server_alloc_req( sizeof(*req), 0 );
174         req->string  = (void *)str;
175         req->unicode = 1;
176         req->length  = (lstrlenW(str) + 1) * sizeof(WCHAR);
177         server_call_noerr( REQ_OUTPUT_DEBUG_STRING );
178     }
179     SERVER_END_REQ;
180     WARN("%s\n", debugstr_w(str));
181 }
182
183
184 /***********************************************************************
185  *           OutputDebugString16   (KERNEL.115)
186  */
187 void WINAPI OutputDebugString16( LPCSTR str )
188 {
189     OutputDebugStringA( str );
190 }
191
192
193 /***********************************************************************
194  *           DebugBreak   (KERNEL32.181)
195  */
196 void WINAPI DebugBreak(void)
197 {
198     DbgBreakPoint();
199 }
200
201
202 /***********************************************************************
203  *           DebugBreak16   (KERNEL.203)
204  */
205 void WINAPI DebugBreak16( CONTEXT86 *context )
206 {
207 #ifdef __i386__
208     EXCEPTION_RECORD rec;
209
210     rec.ExceptionCode    = EXCEPTION_BREAKPOINT;
211     rec.ExceptionFlags   = 0;
212     rec.ExceptionRecord  = NULL;
213     rec.ExceptionAddress = (LPVOID)context->Eip;
214     rec.NumberParameters = 0;
215     NtRaiseException( &rec, context, TRUE );
216 #endif  /* defined(__i386__) */
217 }
218
219
220 /***********************************************************************
221  *           IsDebuggerPresent   (KERNEL32)
222  */
223 BOOL WINAPI IsDebuggerPresent(void)
224 {
225     BOOL ret = FALSE;
226     SERVER_START_REQ
227     {
228         struct get_process_info_request *req = server_alloc_req( sizeof(*req), 0 );
229         req->handle = GetCurrentProcess();
230         if (!server_call( REQ_GET_PROCESS_INFO )) ret = req->debugged;
231     }
232     SERVER_END_REQ;
233     return ret;
234 }
235
236
237 /***********************************************************************
238  *           _DebugOutput                    (KERNEL.328)
239  */
240 void WINAPIV _DebugOutput( void )
241 {
242     VA_LIST16 valist;
243     WORD flags;
244     SEGPTR spec;
245     char caller[101];
246
247     /* Decode caller address */
248     if (!GetModuleName16( GetExePtr(CURRENT_STACK16->cs), caller, sizeof(caller) ))
249         sprintf( caller, "%04X:%04X", CURRENT_STACK16->cs, CURRENT_STACK16->ip );
250
251     /* Build debug message string */
252     VA_START16( valist );
253     flags = VA_ARG16( valist, WORD );
254     spec  = VA_ARG16( valist, SEGPTR );
255     /* FIXME: cannot use wvsnprintf16 from kernel */
256     /* wvsnprintf16( temp, sizeof(temp), MapSL(spec), valist ); */
257
258     /* Output */
259     FIXME("%s %04x %s\n", caller, flags, debugstr_a(MapSL(spec)) );
260 }