Moved hardware message queue handling to the server.
[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     CONTEXT                context;   /* register context */
32 };
33
34 /* debug context */
35 struct debug_ctx
36 {
37     struct object        obj;         /* object header */
38     struct debug_event  *event_head;  /* head of pending events queue */
39     struct debug_event  *event_tail;  /* tail of pending events queue */
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_get_fd,                     /* get_fd */
58     no_flush,                      /* flush */
59     no_get_file_info,              /* get_file_info */
60     debug_event_destroy            /* destroy */
61 };
62
63 static void debug_ctx_dump( struct object *obj, int verbose );
64 static int debug_ctx_signaled( struct object *obj, struct thread *thread );
65 static void debug_ctx_destroy( struct object *obj );
66
67 static const struct object_ops debug_ctx_ops =
68 {
69     sizeof(struct debug_ctx),      /* size */
70     debug_ctx_dump,                /* dump */
71     add_queue,                     /* add_queue */
72     remove_queue,                  /* remove_queue */
73     debug_ctx_signaled,            /* signaled */
74     no_satisfied,                  /* satisfied */
75     NULL,                          /* get_poll_events */
76     NULL,                          /* poll_event */
77     no_get_fd,                     /* get_fd */
78     no_flush,                      /* flush */
79     no_get_file_info,              /* get_file_info */
80     debug_ctx_destroy              /* destroy */
81 };
82
83
84 /* routines to build an event according to its type */
85
86 static int fill_exception_event( struct debug_event *event, void *arg )
87 {
88     memcpy( &event->data.info.exception, arg, sizeof(event->data.info.exception) );
89     return 1;
90 }
91
92 static int fill_create_thread_event( struct debug_event *event, void *arg )
93 {
94     struct process *debugger = event->debugger->process;
95     struct thread *thread = event->sender;
96     handle_t handle;
97
98     /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
99     if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE ))) return 0;
100     event->data.info.create_thread.handle = handle;
101     event->data.info.create_thread.teb    = thread->teb;
102     event->data.info.create_thread.start  = arg;
103     return 1;
104 }
105
106 static int fill_create_process_event( struct debug_event *event, void *arg )
107 {
108     struct process *debugger = event->debugger->process;
109     struct thread *thread = event->sender;
110     struct process *process = thread->process;
111     handle_t handle;
112
113     /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
114     if (!(handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, FALSE ))) return 0;
115     event->data.info.create_process.process = handle;
116
117     /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
118     if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )))
119     {
120         close_handle( debugger, event->data.info.create_process.process, NULL );
121         return 0;
122     }
123     event->data.info.create_process.thread = handle;
124
125     handle = 0;
126     if (process->exe.file &&
127         /* the doc says write access too, but this doesn't seem a good idea */
128         !(handle = alloc_handle( debugger, process->exe.file, GENERIC_READ, FALSE )))
129     {
130         close_handle( debugger, event->data.info.create_process.process, NULL );
131         close_handle( debugger, event->data.info.create_process.thread, NULL );
132         return 0;
133     }
134     event->data.info.create_process.file       = handle;
135     event->data.info.create_process.teb        = thread->teb;
136     event->data.info.create_process.base       = process->exe.base;
137     event->data.info.create_process.start      = arg;
138     event->data.info.create_process.dbg_offset = process->exe.dbg_offset;
139     event->data.info.create_process.dbg_size   = process->exe.dbg_size;
140     event->data.info.create_process.name       = process->exe.name;
141     event->data.info.create_process.unicode    = 0;
142     return 1;
143 }
144
145 static int fill_exit_thread_event( struct debug_event *event, void *arg )
146 {
147     struct thread *thread = arg;
148     event->data.info.exit.exit_code = thread->exit_code;
149     return 1;
150 }
151
152 static int fill_exit_process_event( struct debug_event *event, void *arg )
153 {
154     struct process *process = arg;
155     event->data.info.exit.exit_code = process->exit_code;
156     return 1;
157 }
158
159 static int fill_load_dll_event( struct debug_event *event, void *arg )
160 {
161     struct process *debugger = event->debugger->process;
162     struct process_dll *dll = arg;
163     handle_t handle = 0;
164
165     if (dll->file && !(handle = alloc_handle( debugger, dll->file, GENERIC_READ, FALSE )))
166         return 0;
167     event->data.info.load_dll.handle     = handle;
168     event->data.info.load_dll.base       = dll->base;
169     event->data.info.load_dll.dbg_offset = dll->dbg_offset;
170     event->data.info.load_dll.dbg_size   = dll->dbg_size;
171     event->data.info.load_dll.name       = dll->name;
172     event->data.info.load_dll.unicode    = 0;
173     return 1;
174 }
175
176 static int fill_unload_dll_event( struct debug_event *event, void *arg )
177 {
178     event->data.info.unload_dll.base = arg;
179     return 1;
180 }
181
182 static int fill_output_debug_string_event( struct debug_event *event, void *arg )
183 {
184     struct debug_event_output_string *data = arg;
185     event->data.info.output_string = *data;
186     return 1;
187 }
188
189 typedef int (*fill_event_func)( struct debug_event *event, void *arg );
190
191 #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT  /* RIP_EVENT not supported */
192
193 static const fill_event_func fill_debug_event[NB_DEBUG_EVENTS] =
194 {
195     fill_exception_event,            /* EXCEPTION_DEBUG_EVENT */
196     fill_create_thread_event,        /* CREATE_THREAD_DEBUG_EVENT */
197     fill_create_process_event,       /* CREATE_PROCESS_DEBUG_EVENT */
198     fill_exit_thread_event,          /* EXIT_THREAD_DEBUG_EVENT */
199     fill_exit_process_event,         /* EXIT_PROCESS_DEBUG_EVENT */
200     fill_load_dll_event,             /* LOAD_DLL_DEBUG_EVENT */
201     fill_unload_dll_event,           /* UNLOAD_DLL_DEBUG_EVENT */
202     fill_output_debug_string_event   /* OUTPUT_DEBUG_STRING_EVENT */
203 };
204
205
206 /* unlink the first event from the queue */
207 static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
208 {
209     if (event->prev) event->prev->next = event->next;
210     else debug_ctx->event_head = event->next;
211     if (event->next) event->next->prev = event->prev;
212     else debug_ctx->event_tail = event->prev;
213     event->next = event->prev = NULL;
214     if (event->sender->debug_event == event) event->sender->debug_event = NULL;
215     release_object( event );
216 }
217
218 /* link an event at the end of the queue */
219 static void link_event( struct debug_event *event )
220 {
221     struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
222
223     assert( debug_ctx );
224     grab_object( event );
225     event->next = NULL;
226     event->prev = debug_ctx->event_tail;
227     debug_ctx->event_tail = event;
228     if (event->prev) event->prev->next = event;
229     else debug_ctx->event_head = event;
230     if (!event->sender->debug_event) wake_up( &debug_ctx->obj, 0 );
231 }
232
233 /* find the next event that we can send to the debugger */
234 static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
235 {
236     struct debug_event *event;
237     for (event = debug_ctx->event_head; event; event = event->next)
238     {
239         if (event->state == EVENT_SENT) continue;  /* already sent */
240         if (event->sender->debug_event) continue;  /* thread busy with another one */
241         break;
242     }
243     return event;
244 }
245
246 static void debug_event_dump( struct object *obj, int verbose )
247 {
248     struct debug_event *debug_event = (struct debug_event *)obj;
249     assert( obj->ops == &debug_event_ops );
250     fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
251              debug_event->sender, debug_event->data.code, debug_event->state );
252 }
253
254 static int debug_event_signaled( struct object *obj, struct thread *thread )
255 {
256     struct debug_event *debug_event = (struct debug_event *)obj;
257     assert( obj->ops == &debug_event_ops );
258     return debug_event->state == EVENT_CONTINUED;
259 }
260
261 static void debug_event_destroy( struct object *obj )
262 {
263     struct debug_event *event = (struct debug_event *)obj;
264     assert( obj->ops == &debug_event_ops );
265
266     /* cannot still be in the queue */
267     assert( !event->next );
268     assert( !event->prev );
269
270     /* If the event has been sent already, the handles are now under the */
271     /* responsibility of the debugger process, so we don't touch them    */
272     if (event->state == EVENT_QUEUED)
273     {
274         struct process *debugger = event->debugger->process;
275         switch(event->data.code)
276         {
277         case CREATE_THREAD_DEBUG_EVENT:
278             close_handle( debugger, event->data.info.create_thread.handle, NULL );
279             break;
280         case CREATE_PROCESS_DEBUG_EVENT:
281             if (event->data.info.create_process.file)
282                 close_handle( debugger, event->data.info.create_process.file, NULL );
283             close_handle( debugger, event->data.info.create_process.thread, NULL );
284             close_handle( debugger, event->data.info.create_process.process, NULL );
285             break;
286         case LOAD_DLL_DEBUG_EVENT:
287             if (event->data.info.load_dll.handle)
288                 close_handle( debugger, event->data.info.load_dll.handle, NULL );
289             break;
290         }
291     }
292     if (event->sender->context == &event->context) event->sender->context = NULL;
293     release_object( event->sender );
294     release_object( event->debugger );
295 }
296
297 static void debug_ctx_dump( struct object *obj, int verbose )
298 {
299     struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
300     assert( obj->ops == &debug_ctx_ops );
301     fprintf( stderr, "Debug context head=%p tail=%p\n",
302              debug_ctx->event_head, debug_ctx->event_tail );
303 }
304
305 static int debug_ctx_signaled( struct object *obj, struct thread *thread )
306 {
307     struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
308     assert( obj->ops == &debug_ctx_ops );
309     return find_event_to_send( debug_ctx ) != NULL;
310 }
311
312 static void debug_ctx_destroy( struct object *obj )
313 {
314     struct debug_event *event;
315     struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
316     assert( obj->ops == &debug_ctx_ops );
317
318     /* free all pending events */
319     while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
320 }
321
322 /* continue a debug event */
323 static int continue_debug_event( struct process *process, struct thread *thread, int status )
324 {
325     struct debug_event *event;
326     struct debug_ctx *debug_ctx = current->debug_ctx;
327
328     if (!debug_ctx || process->debugger != current || thread->process != process) goto error;
329
330     /* find the event in the queue */
331     for (event = debug_ctx->event_head; event; event = event->next)
332     {
333         if (event->state != EVENT_SENT) continue;
334         if (event->sender == thread) break;
335     }
336     if (!event) goto error;
337
338     assert( event->sender->debug_event == event );
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 /* alloc a debug event for a debugger */
354 static struct debug_event *alloc_debug_event( struct thread *thread, int code,
355                                               void *arg, CONTEXT *context )
356 {
357     struct thread *debugger = thread->process->debugger;
358     struct debug_event *event;
359
360     assert( code > 0 && code <= NB_DEBUG_EVENTS );
361     /* cannot queue a debug event for myself */
362     assert( debugger->process != thread->process );
363
364     /* build the event */
365     if (!(event = alloc_object( &debug_event_ops, -1 ))) return NULL;
366     event->next      = NULL;
367     event->prev      = NULL;
368     event->state     = EVENT_QUEUED;
369     event->sender    = (struct thread *)grab_object( thread );
370     event->debugger  = (struct thread *)grab_object( debugger );
371     event->data.code = code;
372
373     if (!fill_debug_event[code-1]( event, arg ))
374     {
375         event->data.code = -1;  /* make sure we don't attempt to close handles */
376         release_object( event );
377         return NULL;
378     }
379     if (context)
380     {
381         memcpy( &event->context, context, sizeof(event->context) );
382         thread->context = &event->context;
383     }
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, void *arg )
389 {
390     if (thread->process->debugger)
391     {
392         struct debug_event *event = alloc_debug_event( thread, code, arg, NULL );
393         if (event)
394         {
395             link_event( event );
396             suspend_process( thread->process );
397             release_object( event );
398         }
399     }
400 }
401
402 /* attach a process to a debugger thread and suspend it */
403 static int debugger_attach( struct process *process, struct thread *debugger )
404 {
405     struct thread *thread;
406
407     if (process->debugger) goto error;  /* already being debugged */
408     if (process->init_event) goto error;  /* still starting up */
409
410     /* make sure we don't create a debugging loop */
411     for (thread = debugger; thread; thread = thread->process->debugger)
412         if (thread->process == process) goto error;
413
414     suspend_process( process );
415
416     /* we must have been able to attach all threads */
417     for (thread = process->thread_list; thread; thread = thread->proc_next)
418         if (!thread->attached)
419         {
420             fprintf( stderr, "%p not attached\n", thread );
421             resume_process( process );
422             goto error;
423         }
424
425     if (set_process_debugger( process, debugger )) return 1;
426     resume_process( process );
427     return 0;
428
429  error:
430     set_error( STATUS_ACCESS_DENIED );
431     return 0;
432 }
433
434 /* generate all startup events of a given process */
435 void generate_startup_debug_events( struct process *process, void *entry )
436 {
437     struct process_dll *dll;
438     struct thread *thread = process->thread_list;
439
440     /* generate creation events */
441     generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT, entry );
442     while ((thread = thread->proc_next))
443         generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
444
445     /* generate dll events (in loading order, i.e. reverse list order) */
446     dll = &process->exe;
447     while (dll->next) dll = dll->next;
448     while (dll != &process->exe)
449     {
450         generate_debug_event( process->thread_list, LOAD_DLL_DEBUG_EVENT, dll );
451         dll = dll->prev;
452     }
453 }
454
455 /* set the debugger of a given process */
456 int set_process_debugger( struct process *process, struct thread *debugger )
457 {
458     struct debug_ctx *debug_ctx;
459
460     assert( !process->debugger );
461
462     if (!debugger->debug_ctx)  /* need to allocate a context */
463     {
464         if (!(debug_ctx = alloc_object( &debug_ctx_ops, -1 ))) return 0;
465         debug_ctx->event_head = NULL;
466         debug_ctx->event_tail = NULL;
467         debugger->debug_ctx = debug_ctx;
468     }
469     process->debugger = debugger;
470     return 1;
471 }
472
473 /* a thread is exiting */
474 void debug_exit_thread( struct thread *thread )
475 {
476     if (thread->debug_ctx)  /* this thread is a debugger */
477     {
478         /* kill all debugged processes */
479         kill_debugged_processes( thread, thread->exit_code );
480         release_object( thread->debug_ctx );
481         thread->debug_ctx = NULL;
482     }
483 }
484
485 /* Wait for a debug event */
486 DECL_HANDLER(wait_debug_event)
487 {
488     struct debug_ctx *debug_ctx = current->debug_ctx;
489     struct debug_event *event;
490
491     if (!debug_ctx)  /* current thread is not a debugger */
492     {
493         set_error( STATUS_INVALID_HANDLE );
494         return;
495     }
496     req->wait = 0;
497     if ((event = find_event_to_send( debug_ctx )))
498     {
499         size_t size = get_req_data_size(req);
500         event->state = EVENT_SENT;
501         event->sender->debug_event = event;
502         req->pid = event->sender->process;
503         req->tid = event->sender;
504         if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
505         memcpy( get_req_data(req), &event->data, size );
506         set_req_data_size( req, size );
507     }
508     else  /* no event ready */
509     {
510         req->pid  = 0;
511         req->tid  = 0;
512         set_req_data_size( req, 0 );
513         if (req->get_handle)
514             req->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, FALSE );
515     }
516 }
517
518 /* Continue a debug event */
519 DECL_HANDLER(continue_debug_event)
520 {
521     struct process *process = get_process_from_id( req->pid );
522     if (process)
523     {
524         struct thread *thread = get_thread_from_id( req->tid );
525         if (thread)
526         {
527             continue_debug_event( process, thread, req->status );
528             release_object( thread );
529         }
530         release_object( process );
531     }
532 }
533
534 /* Start debugging an existing process */
535 DECL_HANDLER(debug_process)
536 {
537     struct debug_event_exception data;
538     struct process *process = get_process_from_id( req->pid );
539     if (!process) return;
540
541     if (debugger_attach( process, current ))
542     {
543         generate_startup_debug_events( process, NULL );
544         resume_process( process );
545
546         data.record.ExceptionCode    = EXCEPTION_BREAKPOINT;
547         data.record.ExceptionFlags   = EXCEPTION_CONTINUABLE;
548         data.record.ExceptionRecord  = NULL;
549         data.record.ExceptionAddress = get_thread_ip( process->thread_list );
550         data.record.NumberParameters = 0;
551         data.first = 1;
552         generate_debug_event( process->thread_list, EXCEPTION_DEBUG_EVENT, &data );
553     }
554     release_object( process );
555 }
556
557 /* queue an exception event */
558 DECL_HANDLER(queue_exception_event)
559 {
560     req->handle = 0;
561     if (current->process->debugger)
562     {
563         struct debug_event_exception data;
564         struct debug_event *event;
565         CONTEXT *context = get_req_data( req );
566         EXCEPTION_RECORD *rec = (EXCEPTION_RECORD *)(context + 1);
567
568         if (get_req_data_size( req ) < sizeof(*rec) + sizeof(*context))
569         {
570             set_error( STATUS_INVALID_PARAMETER );
571             return;
572         }
573         data.record = *rec;
574         data.first  = req->first;
575         if ((event = alloc_debug_event( current, EXCEPTION_DEBUG_EVENT, &data, context )))
576         {
577             if ((req->handle = alloc_handle( current->process, event, SYNCHRONIZE, FALSE )))
578             {
579                 link_event( event );
580                 suspend_process( current->process );
581             }
582             release_object( event );
583         }
584     }
585 }
586
587 /* retrieve the status of an exception event */
588 DECL_HANDLER(get_exception_status)
589 {
590     struct debug_event *event;
591     size_t size = 0;
592
593     req->status = 0;
594     if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
595                                                        0, &debug_event_ops )))
596     {
597         if (event->state == EVENT_CONTINUED)
598         {
599             req->status = event->status;
600             if (current->context == &event->context)
601             {
602                 size = min( sizeof(CONTEXT), get_req_data_size(req) );
603                 memcpy( get_req_data(req), &event->context, size );
604                 current->context = NULL;
605             }
606         }
607         else set_error( STATUS_PENDING );
608         release_object( event );
609     }
610     set_req_data_size( req, size );
611 }
612
613 /* send an output string to the debugger */
614 DECL_HANDLER(output_debug_string)
615 {
616     struct debug_event_output_string data;
617
618     data.string  = req->string;
619     data.unicode = req->unicode;
620     data.length  = req->length;
621     generate_debug_event( current, OUTPUT_DEBUG_STRING_EVENT, &data );
622 }