No longer directly accessing debuggee memory.
[wine] / server / debugger.c
1 /*
2  * Server-side debugger functions
3  *
4  * Copyright (C) 1999 Alexandre Julliard
5  */
6
7 #include <assert.h>
8 #include <string.h>
9 #include <stdio.h>
10
11 #include "winbase.h"
12
13 #include "handle.h"
14 #include "process.h"
15 #include "thread.h"
16 #include "request.h"
17
18 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
19
20 /* debug event */
21 struct debug_event
22 {
23     struct object          obj;       /* object header */
24     struct debug_event    *next;      /* event queue */
25     struct debug_event    *prev;
26     struct thread         *sender;    /* thread which sent this event */
27     struct thread         *debugger;  /* debugger thread receiving the event */
28     enum debug_event_state state;     /* event state */
29     int                    status;    /* continuation status */
30     debug_event_t          data;      /* event data */
31 };
32
33 /* debug context */
34 struct debug_ctx
35 {
36     struct object        obj;         /* object header */
37     struct debug_event  *event_head;  /* head of pending events queue */
38     struct debug_event  *event_tail;  /* tail of pending events queue */
39     struct debug_event  *to_send;     /* next event on the queue to send to debugger */
40 };
41
42
43 static void debug_event_dump( struct object *obj, int verbose );
44 static int debug_event_signaled( struct object *obj, struct thread *thread );
45 static void debug_event_destroy( struct object *obj );
46
47 static const struct object_ops debug_event_ops =
48 {
49     sizeof(struct debug_event),    /* size */
50     debug_event_dump,              /* dump */
51     add_queue,                     /* add_queue */
52     remove_queue,                  /* remove_queue */
53     debug_event_signaled,          /* signaled */
54     no_satisfied,                  /* satisfied */
55     NULL,                          /* get_poll_events */
56     NULL,                          /* poll_event */
57     no_read_fd,                    /* get_read_fd */
58     no_write_fd,                   /* get_write_fd */
59     no_flush,                      /* flush */
60     no_get_file_info,              /* get_file_info */
61     debug_event_destroy            /* destroy */
62 };
63
64 static void debug_ctx_dump( struct object *obj, int verbose );
65 static int debug_ctx_signaled( struct object *obj, struct thread *thread );
66 static void debug_ctx_destroy( struct object *obj );
67
68 static const struct object_ops debug_ctx_ops =
69 {
70     sizeof(struct debug_ctx),      /* size */
71     debug_ctx_dump,                /* dump */
72     add_queue,                     /* add_queue */
73     remove_queue,                  /* remove_queue */
74     debug_ctx_signaled,            /* signaled */
75     no_satisfied,                  /* satisfied */
76     NULL,                          /* get_poll_events */
77     NULL,                          /* poll_event */
78     no_read_fd,                    /* get_read_fd */
79     no_write_fd,                   /* get_write_fd */
80     no_flush,                      /* flush */
81     no_get_file_info,              /* get_file_info */
82     debug_ctx_destroy              /* destroy */
83 };
84
85
86 /* initialise the fields that do not need to be filled by the client */
87 static int fill_debug_event( struct thread *debugger, struct thread *thread,
88                              struct debug_event *event )
89 {
90     int handle;
91
92     /* some events need special handling */
93     switch(event->data.code)
94     {
95     case CREATE_THREAD_DEBUG_EVENT:
96         if ((event->data.info.create_thread.handle = alloc_handle( debugger->process, thread,
97                /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
98                                                               THREAD_ALL_ACCESS, FALSE )) == -1)
99             return 0;
100         event->data.info.create_thread.teb   = thread->teb;
101         event->data.info.create_thread.start = thread->entry;
102         break;
103     case CREATE_PROCESS_DEBUG_EVENT:
104         if ((handle = alloc_handle( debugger->process, thread->process,
105                                     /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
106                                     PROCESS_ALL_ACCESS, FALSE )) == -1)
107             return 0;
108         event->data.info.create_process.process = handle;
109
110         if ((handle = alloc_handle( debugger->process, thread,
111                /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
112                                     THREAD_ALL_ACCESS, FALSE )) == -1)
113         {
114             close_handle( debugger->process, event->data.info.create_process.process );
115             return 0;
116         }
117         event->data.info.create_process.thread = handle;
118
119         handle = -1;
120         if (thread->process->exe_file &&
121             ((handle = alloc_handle( debugger->process, thread->process->exe_file,
122                         /* the doc says write access too, but this doesn't seem a good idea */
123                                      GENERIC_READ, FALSE )) == -1))
124         {
125             close_handle( debugger->process, event->data.info.create_process.process );
126             close_handle( debugger->process, event->data.info.create_process.thread );
127             return 0;
128         }
129         event->data.info.create_process.file       = handle;
130         event->data.info.create_process.teb        = thread->teb;
131         event->data.info.create_process.base       = thread->process->module;
132         event->data.info.create_process.start      = thread->entry;
133         event->data.info.create_process.dbg_offset = 0;
134         event->data.info.create_process.dbg_size   = 0;
135         event->data.info.create_process.name       = 0;
136         event->data.info.create_process.unicode    = 0;
137         break;
138     case LOAD_DLL_DEBUG_EVENT:
139         if ((handle = event->data.info.load_dll.handle) != -1)
140         {
141             if ((handle = duplicate_handle( thread->process, handle, debugger->process,
142                                             GENERIC_READ, FALSE, 0 )) == -1)
143                 return 0;
144             event->data.info.load_dll.handle = handle;
145         }
146         break;
147     case EXIT_PROCESS_DEBUG_EVENT:
148     case EXIT_THREAD_DEBUG_EVENT:
149         event->data.info.exit.exit_code = thread->exit_code;
150         break;
151     case EXCEPTION_DEBUG_EVENT:
152     case UNLOAD_DLL_DEBUG_EVENT:
153     case OUTPUT_DEBUG_STRING_EVENT:
154     case RIP_EVENT:
155         break;
156     }
157     return 1;
158 }
159
160 /* unlink the first event from the queue */
161 static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
162 {
163     if (event->prev) event->prev->next = event->next;
164     else debug_ctx->event_head = event->next;
165     if (event->next) event->next->prev = event->prev;
166     else debug_ctx->event_tail = event->prev;
167     if (debug_ctx->to_send == event) debug_ctx->to_send = event->next;
168     event->next = event->prev = NULL;
169     release_object( event );
170 }
171
172 /* link an event at the end of the queue */
173 static void link_event( struct debug_ctx *debug_ctx, struct debug_event *event )
174 {
175     grab_object( event );
176     event->next = NULL;
177     event->prev = debug_ctx->event_tail;
178     debug_ctx->event_tail = event;
179     if (event->prev) event->prev->next = event;
180     else debug_ctx->event_head = event;
181     if (!debug_ctx->to_send)
182     {
183         debug_ctx->to_send = event;
184         wake_up( &debug_ctx->obj, 0 );
185     }
186 }
187
188 /* build a reply for the wait_debug_event request */
189 static void build_wait_debug_reply( struct thread *thread, struct object *obj, int signaled )
190 {
191     struct wait_debug_event_request *req = get_req_ptr( thread );
192
193     if (obj)
194     {
195         struct debug_ctx *debug_ctx = (struct debug_ctx *)obj; 
196         struct debug_event *event = debug_ctx->to_send;
197
198         /* the object that woke us has to be our debug context */
199         assert( obj->ops == &debug_ctx_ops );
200         assert( event );
201
202         event->state = EVENT_SENT;
203         debug_ctx->to_send = event->next;
204         req->event.code = event->data.code;
205         req->pid  = event->sender->process;
206         req->tid  = event->sender;
207         memcpy( &req->event, &event->data, sizeof(req->event) );
208     }
209     else  /* timeout or error */
210     {
211         req->event.code = 0;
212         req->pid  = 0;
213         req->tid  = 0;
214     }
215 }
216
217 /* build a reply for the send_event request */
218 static void build_send_event_reply( struct thread *thread, struct object *obj, int signaled )
219 {
220     struct send_debug_event_request *req = get_req_ptr( thread );
221     struct debug_event *event = (struct debug_event *)obj;
222     assert( obj->ops == &debug_event_ops );
223
224     req->status = event->status;
225     /* copy the context into the reply */
226     if (event->data.code == EXCEPTION_DEBUG_EVENT)
227         memcpy( &req->event.info.exception.context,
228                 &event->data.info.exception.context,
229                 sizeof(req->event.info.exception.context) );
230 }
231
232 static void debug_event_dump( struct object *obj, int verbose )
233 {
234     struct debug_event *debug_event = (struct debug_event *)obj;
235     assert( obj->ops == &debug_event_ops );
236     fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
237              debug_event->sender, debug_event->data.code, debug_event->state );
238 }
239
240 static int debug_event_signaled( struct object *obj, struct thread *thread )
241 {
242     struct debug_event *debug_event = (struct debug_event *)obj;
243     assert( obj->ops == &debug_event_ops );
244     return debug_event->state == EVENT_CONTINUED;
245 }
246
247 static void debug_event_destroy( struct object *obj )
248 {
249     struct debug_event *event = (struct debug_event *)obj;
250     assert( obj->ops == &debug_event_ops );
251
252     /* cannot still be in the queue */
253     assert( !event->next );
254     assert( !event->prev );
255
256     /* If the event has been sent already, the handles are now under the */
257     /* responsibility of the debugger process, so we don't touch them    */
258     if (event->state == EVENT_QUEUED)
259     {
260         struct process *debugger = event->debugger->process;
261         switch(event->data.code)
262         {
263         case CREATE_THREAD_DEBUG_EVENT:
264             close_handle( debugger, event->data.info.create_thread.handle );
265             break;
266         case CREATE_PROCESS_DEBUG_EVENT:
267             if (event->data.info.create_process.file != -1)
268                 close_handle( debugger, event->data.info.create_process.file );
269             close_handle( debugger, event->data.info.create_process.thread );
270             close_handle( debugger, event->data.info.create_process.process );
271             break;
272         case LOAD_DLL_DEBUG_EVENT:
273             if (event->data.info.load_dll.handle != -1)
274                 close_handle( debugger, event->data.info.load_dll.handle );
275             break;
276         }
277     }
278     release_object( event->sender );
279     release_object( event->debugger );
280 }
281
282 static void debug_ctx_dump( struct object *obj, int verbose )
283 {
284     struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
285     assert( obj->ops == &debug_ctx_ops );
286     fprintf( stderr, "Debug context head=%p tail=%p to_send=%p\n",
287              debug_ctx->event_head, debug_ctx->event_tail, debug_ctx->to_send );
288 }
289
290 static int debug_ctx_signaled( struct object *obj, struct thread *thread )
291 {
292     struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
293     assert( obj->ops == &debug_ctx_ops );
294     return debug_ctx->to_send != NULL;
295 }
296
297 static void debug_ctx_destroy( struct object *obj )
298 {
299     struct debug_event *event;
300     struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
301     assert( obj->ops == &debug_ctx_ops );
302
303     /* free all pending events */
304     while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
305 }
306
307 /* wait for a debug event (or send a reply at once if one is pending) */
308 static int wait_for_debug_event( int timeout )
309 {
310     struct debug_ctx *debug_ctx = current->debug_ctx;
311     struct object *obj = &debug_ctx->obj;
312     int flags = 0;
313
314     if (!debug_ctx)  /* current thread is not a debugger */
315     {
316         set_error( STATUS_INVALID_HANDLE );
317         return 0;
318     }
319     if (timeout != -1) flags = SELECT_TIMEOUT;
320     return sleep_on( 1, &obj, flags, timeout, build_wait_debug_reply );
321 }
322
323 /* continue a debug event */
324 static int continue_debug_event( struct process *process, struct thread *thread, int status )
325 {
326     struct debug_event *event;
327     struct debug_ctx *debug_ctx = current->debug_ctx;
328
329     if (!debug_ctx || process->debugger != current || thread->process != process) goto error;
330
331     /* find the event in the queue */
332     for (event = debug_ctx->event_head; event; event = event->next)
333     {
334         if (event == debug_ctx->to_send) goto error;
335         if (event->sender == thread) break;
336         event = event->next;
337     }
338     if (!event) goto error;
339
340     event->status = status;
341     event->state  = EVENT_CONTINUED;
342     wake_up( &event->obj, 0 );
343
344     unlink_event( debug_ctx, event );
345     resume_process( process );
346     return 1;
347  error:
348     /* not debugging this process, or no such event */
349     set_error( STATUS_ACCESS_DENIED );  /* FIXME */
350     return 0;
351 }
352
353 /* queue a debug event for a debugger */
354 static struct debug_event *queue_debug_event( struct thread *thread, int code,
355                                               debug_event_t *data )
356 {
357     struct thread *debugger = thread->process->debugger;
358     struct debug_ctx *debug_ctx = debugger->debug_ctx;
359     struct debug_event *event;
360
361     assert( debug_ctx );
362     /* cannot queue a debug event for myself */
363     assert( debugger->process != thread->process );
364
365     /* build the event */
366     if (!(event = alloc_object( &debug_event_ops, -1 ))) return NULL;
367     event->next     = NULL;
368     event->prev     = NULL;
369     event->state    = EVENT_QUEUED;
370     event->sender   = (struct thread *)grab_object( thread );
371     event->debugger = (struct thread *)grab_object( debugger );
372     if (data) memcpy( &event->data, data, sizeof(event->data) );
373     event->data.code = code;
374
375     if (!fill_debug_event( debugger, thread, event ))
376     {
377         event->data.code = -1;  /* make sure we don't attempt to close handles */
378         release_object( event );
379         return NULL;
380     }
381
382     link_event( debug_ctx, event );
383     suspend_process( thread->process );
384     return event;
385 }
386
387 /* generate a debug event from inside the server and queue it */
388 void generate_debug_event( struct thread *thread, int code )
389 {
390     if (thread->process->debugger)
391     {
392         struct debug_event *event = queue_debug_event( thread, code, NULL );
393         if (event) release_object( event );
394     }
395 }
396
397 /* return a pointer to the context in case the thread is inside an exception event */
398 CONTEXT *get_debug_context( struct thread *thread )
399 {
400     struct debug_event *event;
401     struct thread *debugger = thread->process->debugger;
402
403     if (!debugger) return NULL;  /* not being debugged */
404     assert( debugger->debug_ctx );
405
406     /* find the exception event in the debugger's queue */
407     for (event = debugger->debug_ctx->event_head; event; event = event->next)
408         if (event->sender == thread && (event->data.code == EXCEPTION_DEBUG_EVENT))
409             return &event->data.info.exception.context;
410     return NULL;
411 }
412
413 /* attach a process to a debugger thread */
414 int debugger_attach( struct process *process, struct thread *debugger )
415 {
416     struct debug_ctx *debug_ctx;
417     struct thread *thread;
418
419     if (process->debugger)  /* already being debugged */
420     {
421         set_error( STATUS_ACCESS_DENIED );
422         return 0;
423     }
424     /* make sure we don't create a debugging loop */
425     for (thread = debugger; thread; thread = thread->process->debugger)
426         if (thread->process == process)
427         {
428             set_error( STATUS_ACCESS_DENIED );
429             return 0;
430         }
431
432     if (!debugger->debug_ctx)  /* need to allocate a context */
433     {
434         if (!(debug_ctx = alloc_object( &debug_ctx_ops, -1 ))) return 0;
435         debug_ctx->event_head = NULL;
436         debug_ctx->event_tail = NULL;
437         debug_ctx->to_send    = NULL;
438         debugger->debug_ctx = debug_ctx;
439     }
440     process->debugger = debugger;
441     return 1;
442 }
443
444 /* a thread is exiting */
445 void debug_exit_thread( struct thread *thread )
446 {
447     if (thread->debug_ctx)  /* this thread is a debugger */
448     {
449         /* kill all debugged processes */
450         kill_debugged_processes( thread, thread->exit_code );
451         release_object( thread->debug_ctx );
452         thread->debug_ctx = NULL;
453     }
454 }
455
456 /* Wait for a debug event */
457 DECL_HANDLER(wait_debug_event)
458 {
459     if (!wait_for_debug_event( req->timeout ))
460     {
461         req->event.code = 0;
462         req->pid = NULL;
463         req->tid = NULL;
464     }
465 }
466
467 /* Continue a debug event */
468 DECL_HANDLER(continue_debug_event)
469 {
470     struct process *process = get_process_from_id( req->pid );
471     if (process)
472     {
473         struct thread *thread = get_thread_from_id( req->tid );
474         if (thread)
475         {
476             continue_debug_event( process, thread, req->status );
477             release_object( thread );
478         }
479         release_object( process );
480     }
481 }
482
483 /* Start debugging an existing process */
484 DECL_HANDLER(debug_process)
485 {
486     struct process *process = get_process_from_id( req->pid );
487     if (!process) return;
488     if (debugger_attach( process, current ))
489     {
490         struct thread *thread = process->thread_list;
491         generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT );
492         while ((thread = thread->next)) generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT );
493         /* FIXME: load dll + breakpoint exception events */
494     }
495     release_object( process );
496 }
497
498 /* Send a debug event */
499 DECL_HANDLER(send_debug_event)
500 {
501     struct debug_event *event;
502     int code = req->event.code;
503
504     if ((code <= 0) || (code > RIP_EVENT))
505     {
506         fatal_protocol_error( current, "send_debug_event: bad code %d\n", code );
507         return;
508     }
509     req->status = 0;
510     if (current->process->debugger && ((event = queue_debug_event( current, code, &req->event ))))
511     {
512         /* wait for continue_debug_event */
513         struct object *obj = &event->obj;
514         sleep_on( 1, &obj, 0, -1, build_send_event_reply );
515         release_object( event );
516     }
517 }