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