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