New channel 'debugstr' used to print OutputDebugString API calls.
[wine] / scheduler / debugger.c
1 /*
2  * Win32 debugger functions
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include <string.h>
8
9 #include "process.h"
10 #include "thread.h"
11 #include "server.h"
12 #include "debugtools.h"
13
14 DECLARE_DEBUG_CHANNEL(debugstr)
15
16 /**********************************************************************
17  *           DEBUG_SendEvent
18  *
19  * Internal helper to send a debug event request to the server.
20  */
21 static DWORD DEBUG_SendEvent( int code, void *data, int size )
22 {
23     DWORD ret = 0;
24     struct send_debug_event_request *req = get_req_buffer();
25     req->code = code;
26     memcpy( req + 1, data, size );
27     if (!server_call( REQ_SEND_DEBUG_EVENT )) ret = req->status;
28     return ret;
29 }
30
31
32 /**********************************************************************
33  *           DEBUG_SendExceptionEvent
34  *
35  * Send an EXCEPTION_DEBUG_EVENT event to the current process debugger.
36  */
37 DWORD DEBUG_SendExceptionEvent( EXCEPTION_RECORD *rec, BOOL first_chance )
38 {
39     struct debug_event_exception event;
40     int i;
41
42     event.code      = rec->ExceptionCode;
43     event.flags     = rec->ExceptionFlags;
44     event.record    = rec->ExceptionRecord;
45     event.addr      = rec->ExceptionAddress;
46     event.nb_params = rec->NumberParameters;
47     for (i = 0; i < event.nb_params; i++) event.params[i] = rec->ExceptionInformation[i];
48     event.first_chance = first_chance;
49     return DEBUG_SendEvent( EXCEPTION_DEBUG_EVENT, &event, sizeof(event) );
50 }
51
52
53 /**********************************************************************
54  *           DEBUG_SendCreateProcessEvent
55  *
56  * Send an CREATE_PROCESS_DEBUG_EVENT event to the current process debugger.
57  * Must be called from the context of the new process.
58  */
59 DWORD DEBUG_SendCreateProcessEvent( HFILE file, HMODULE module, void *entry )
60 {
61     struct debug_event_create_process event;
62
63     event.file       = file;
64     event.process    = 0; /* will be filled by server */
65     event.thread     = 0; /* will be filled by server */
66     event.base       = (void *)module;
67     event.dbg_offset = 0; /* FIXME */
68     event.dbg_size   = 0; /* FIXME */
69     event.teb        = NtCurrentTeb();
70     event.start      = entry;
71     event.name       = 0; /* FIXME */
72     event.unicode    = 0; /* FIXME */
73     return DEBUG_SendEvent( CREATE_PROCESS_DEBUG_EVENT, &event, sizeof(event) );
74 }
75
76
77 /**********************************************************************
78  *           DEBUG_SendCreateThreadEvent
79  *
80  * Send an CREATE_THREAD_DEBUG_EVENT event to the current process debugger.
81  * Must be called from the context of the new thread.
82  */
83 DWORD DEBUG_SendCreateThreadEvent( void *entry )
84 {
85     struct debug_event_create_thread event;
86
87     event.handle = 0; /* will be filled by server */
88     event.teb    = NtCurrentTeb();
89     event.start  = entry;
90     return DEBUG_SendEvent( CREATE_THREAD_DEBUG_EVENT, &event, sizeof(event) );
91 }
92
93
94 /**********************************************************************
95  *           DEBUG_SendLoadDLLEvent
96  *
97  * Send an LOAD_DLL_DEBUG_EVENT event to the current process debugger.
98  */
99 DWORD DEBUG_SendLoadDLLEvent( HFILE file, HMODULE module, LPSTR name )
100 {
101     struct debug_event_load_dll event;
102
103     event.handle     = file;
104     event.base       = (void *)module;
105     event.dbg_offset = 0;  /* FIXME */
106     event.dbg_size   = 0;  /* FIXME */
107     event.name       = name;
108     event.unicode    = 0;
109     return DEBUG_SendEvent( LOAD_DLL_DEBUG_EVENT, &event, sizeof(event) );
110 }
111
112
113 /**********************************************************************
114  *           DEBUG_SendUnloadDLLEvent
115  *
116  * Send an UNLOAD_DLL_DEBUG_EVENT event to the current process debugger.
117  */
118 DWORD DEBUG_SendUnloadDLLEvent( HMODULE module )
119 {
120     struct debug_event_unload_dll event;
121
122     event.base = (void *)module;
123     return DEBUG_SendEvent( UNLOAD_DLL_DEBUG_EVENT, &event, sizeof(event) );
124 }
125
126
127 /******************************************************************************
128  *           WaitForDebugEvent   (KERNEL32.720)
129  *
130  * Waits for a debugging event to occur in a process being debugged
131  *
132  * PARAMS
133  *    event   [I] Address of structure for event information
134  *    timeout [I] Number of milliseconds to wait for event
135  *
136  * RETURNS STD
137  */
138 BOOL WINAPI WaitForDebugEvent( LPDEBUG_EVENT event, DWORD timeout )
139 {
140     struct wait_debug_event_request *req = get_req_buffer();
141     union debug_event_data *data = (union debug_event_data *)(req + 1);
142     int i;
143
144     req->timeout = timeout;
145     if (server_call( REQ_WAIT_DEBUG_EVENT )) return FALSE;
146     if ((req->code < 0) || (req->code > RIP_EVENT))
147         server_protocol_error( "WaitForDebugEvent: bad code %d\n", req->code );
148
149     event->dwDebugEventCode = req->code;
150     event->dwProcessId      = (DWORD)req->pid;
151     event->dwThreadId       = (DWORD)req->tid;
152     switch(req->code)
153     {
154     case EXCEPTION_DEBUG_EVENT:
155         event->u.Exception.ExceptionRecord.ExceptionCode    = data->exception.code;
156         event->u.Exception.ExceptionRecord.ExceptionFlags   = data->exception.flags;
157         event->u.Exception.ExceptionRecord.ExceptionRecord  = data->exception.record;
158         event->u.Exception.ExceptionRecord.ExceptionAddress = data->exception.addr;
159         event->u.Exception.ExceptionRecord.NumberParameters = data->exception.nb_params;
160         for (i = 0; i < data->exception.nb_params; i++)
161             event->u.Exception.ExceptionRecord.ExceptionInformation[i] = data->exception.params[i];
162         event->u.Exception.dwFirstChance = data->exception.first_chance;
163         break;
164     case CREATE_THREAD_DEBUG_EVENT:
165         event->u.CreateThread.hThread           = data->create_thread.handle;
166         event->u.CreateThread.lpThreadLocalBase = data->create_thread.teb;
167         event->u.CreateThread.lpStartAddress    = data->create_thread.start;
168         break;
169     case CREATE_PROCESS_DEBUG_EVENT:
170         event->u.CreateProcessInfo.hFile                 = data->create_process.file;
171         event->u.CreateProcessInfo.hProcess              = data->create_process.process;
172         event->u.CreateProcessInfo.hThread               = data->create_process.thread;
173         event->u.CreateProcessInfo.lpBaseOfImage         = data->create_process.base;
174         event->u.CreateProcessInfo.dwDebugInfoFileOffset = data->create_process.dbg_offset;
175         event->u.CreateProcessInfo.nDebugInfoSize        = data->create_process.dbg_size;
176         event->u.CreateProcessInfo.lpThreadLocalBase     = data->create_process.teb;
177         event->u.CreateProcessInfo.lpStartAddress        = data->create_process.start;
178         event->u.CreateProcessInfo.lpImageName           = data->create_process.name;
179         event->u.CreateProcessInfo.fUnicode              = data->create_process.unicode;
180         if (data->create_process.file == -1) event->u.CreateProcessInfo.hFile = 0;
181         break;
182     case EXIT_THREAD_DEBUG_EVENT:
183         event->u.ExitThread.dwExitCode = data->exit.exit_code;
184         break;
185     case EXIT_PROCESS_DEBUG_EVENT:
186         event->u.ExitProcess.dwExitCode = data->exit.exit_code;
187         break;
188     case LOAD_DLL_DEBUG_EVENT:
189         event->u.LoadDll.hFile                 = data->load_dll.handle;
190         event->u.LoadDll.lpBaseOfDll           = data->load_dll.base;
191         event->u.LoadDll.dwDebugInfoFileOffset = data->load_dll.dbg_offset;
192         event->u.LoadDll.nDebugInfoSize        = data->load_dll.dbg_size;
193         event->u.LoadDll.lpImageName           = data->load_dll.name;
194         event->u.LoadDll.fUnicode              = data->load_dll.unicode;
195         if (data->load_dll.handle == -1) event->u.LoadDll.hFile = 0;
196         break;
197     case UNLOAD_DLL_DEBUG_EVENT:
198         event->u.UnloadDll.lpBaseOfDll = data->unload_dll.base;
199         break;
200     case OUTPUT_DEBUG_STRING_EVENT:
201         event->u.DebugString.lpDebugStringData  = data->output_string.string;
202         event->u.DebugString.fUnicode           = data->output_string.unicode;
203         event->u.DebugString.nDebugStringLength = data->output_string.length;
204         break;
205     case RIP_EVENT:
206         event->u.RipInfo.dwError = data->rip_info.error;
207         event->u.RipInfo.dwType  = data->rip_info.type;
208         break;
209     }
210     return TRUE;
211 }
212
213
214 /**********************************************************************
215  *           ContinueDebugEvent   (KERNEL32.146)
216  */
217 BOOL WINAPI ContinueDebugEvent( DWORD pid, DWORD tid, DWORD status )
218 {
219     struct continue_debug_event_request *req = get_req_buffer();
220     req->pid    = (void *)pid;
221     req->tid    = (void *)tid;
222     req->status = status;
223     return !server_call( REQ_CONTINUE_DEBUG_EVENT );
224 }
225
226
227 /**********************************************************************
228  *           DebugActiveProcess   (KERNEL32.180)
229  */
230 BOOL WINAPI DebugActiveProcess( DWORD pid )
231 {
232     struct debug_process_request *req = get_req_buffer();
233     req->pid = (void *)pid;
234     return !server_call( REQ_DEBUG_PROCESS );
235 }
236
237
238 /***********************************************************************
239  *           OutputDebugStringA   (KERNEL32.548)
240  */
241 void WINAPI OutputDebugStringA( LPCSTR str )
242 {
243     if (PROCESS_Current()->flags & PDB32_DEBUGGED)
244     {
245         struct debug_event_output_string event;
246         event.string  = (void *)str;
247         event.unicode = 0;
248         event.length  = strlen(str) + 1;
249         DEBUG_SendEvent( OUTPUT_DEBUG_STRING_EVENT, &event, sizeof(event) );
250     }
251
252     TRACE_(debugstr)("%s\n", str);
253 }
254
255
256 /***********************************************************************
257  *           OutputDebugStringW   (KERNEL32.549)
258  */
259 void WINAPI OutputDebugStringW( LPCWSTR str )
260 {
261     if (PROCESS_Current()->flags & PDB32_DEBUGGED)
262     {
263         struct debug_event_output_string event;
264         event.string  = (void *)str;
265         event.unicode = 1;
266         event.length  = (lstrlenW(str) + 1) * sizeof(WCHAR);
267         DEBUG_SendEvent( OUTPUT_DEBUG_STRING_EVENT, &event, sizeof(event) );
268     }
269
270     TRACE_(debugstr)("%s\n", debugstr_w(str));
271 }
272
273
274 /***********************************************************************
275  *           OutputDebugString16   (KERNEL.115)
276  */
277 void WINAPI OutputDebugString16( LPCSTR str )
278 {
279     OutputDebugStringA( str );
280 }