Added support for the QS_ALLPOSTMESSAGE flag.
[wine/multimedia.git] / server / debugger.c
blob8940c0bdb5c2ed8d89250ea75393b06d6c93ff38
1 /*
2 * Server-side debugger functions
4 * Copyright (C) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
30 #include "windef.h"
32 #include "handle.h"
33 #include "process.h"
34 #include "thread.h"
35 #include "request.h"
36 #include "console.h"
38 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
40 /* debug event */
41 struct debug_event
43 struct object obj; /* object header */
44 struct list entry; /* entry in event queue */
45 struct thread *sender; /* thread which sent this event */
46 struct thread *debugger; /* debugger thread receiving the event */
47 enum debug_event_state state; /* event state */
48 int status; /* continuation status */
49 debug_event_t data; /* event data */
50 CONTEXT context; /* register context */
53 /* debug context */
54 struct debug_ctx
56 struct object obj; /* object header */
57 struct list event_queue; /* pending events queue */
58 int kill_on_exit;/* kill debuggees on debugger exit ? */
62 static void debug_event_dump( struct object *obj, int verbose );
63 static int debug_event_signaled( struct object *obj, struct thread *thread );
64 static void debug_event_destroy( struct object *obj );
66 static const struct object_ops debug_event_ops =
68 sizeof(struct debug_event), /* size */
69 debug_event_dump, /* dump */
70 add_queue, /* add_queue */
71 remove_queue, /* remove_queue */
72 debug_event_signaled, /* signaled */
73 no_satisfied, /* satisfied */
74 no_signal, /* signal */
75 no_get_fd, /* get_fd */
76 no_close_handle, /* close_handle */
77 debug_event_destroy /* destroy */
80 static void debug_ctx_dump( struct object *obj, int verbose );
81 static int debug_ctx_signaled( struct object *obj, struct thread *thread );
82 static void debug_ctx_destroy( struct object *obj );
84 static const struct object_ops debug_ctx_ops =
86 sizeof(struct debug_ctx), /* size */
87 debug_ctx_dump, /* dump */
88 add_queue, /* add_queue */
89 remove_queue, /* remove_queue */
90 debug_ctx_signaled, /* signaled */
91 no_satisfied, /* satisfied */
92 no_signal, /* signal */
93 no_get_fd, /* get_fd */
94 no_close_handle, /* close_handle */
95 debug_ctx_destroy /* destroy */
99 /* routines to build an event according to its type */
101 static int fill_exception_event( struct debug_event *event, void *arg )
103 memcpy( &event->data.info.exception, arg, sizeof(event->data.info.exception) );
104 return 1;
107 static int fill_create_thread_event( struct debug_event *event, void *arg )
109 struct process *debugger = event->debugger->process;
110 struct thread *thread = event->sender;
111 obj_handle_t handle;
113 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
114 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE ))) return 0;
115 event->data.info.create_thread.handle = handle;
116 event->data.info.create_thread.teb = thread->teb;
117 event->data.info.create_thread.start = arg;
118 return 1;
121 static int fill_create_process_event( struct debug_event *event, void *arg )
123 struct process *debugger = event->debugger->process;
124 struct thread *thread = event->sender;
125 struct process *process = thread->process;
126 obj_handle_t handle;
128 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
129 if (!(handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, FALSE ))) return 0;
130 event->data.info.create_process.process = handle;
132 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
133 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )))
135 close_handle( debugger, event->data.info.create_process.process, NULL );
136 return 0;
138 event->data.info.create_process.thread = handle;
140 handle = 0;
141 if (process->exe.file &&
142 /* the doc says write access too, but this doesn't seem a good idea */
143 !(handle = alloc_handle( debugger, process->exe.file, GENERIC_READ, FALSE )))
145 close_handle( debugger, event->data.info.create_process.process, NULL );
146 close_handle( debugger, event->data.info.create_process.thread, NULL );
147 return 0;
149 event->data.info.create_process.file = handle;
150 event->data.info.create_process.teb = thread->teb;
151 event->data.info.create_process.base = process->exe.base;
152 event->data.info.create_process.start = arg;
153 event->data.info.create_process.dbg_offset = process->exe.dbg_offset;
154 event->data.info.create_process.dbg_size = process->exe.dbg_size;
155 event->data.info.create_process.name = process->exe.name;
156 event->data.info.create_process.unicode = 1;
157 return 1;
160 static int fill_exit_thread_event( struct debug_event *event, void *arg )
162 struct thread *thread = arg;
163 event->data.info.exit.exit_code = thread->exit_code;
164 return 1;
167 static int fill_exit_process_event( struct debug_event *event, void *arg )
169 struct process *process = arg;
170 event->data.info.exit.exit_code = process->exit_code;
171 return 1;
174 static int fill_load_dll_event( struct debug_event *event, void *arg )
176 struct process *debugger = event->debugger->process;
177 struct process_dll *dll = arg;
178 obj_handle_t handle = 0;
180 if (dll->file && !(handle = alloc_handle( debugger, dll->file, GENERIC_READ, FALSE )))
181 return 0;
182 event->data.info.load_dll.handle = handle;
183 event->data.info.load_dll.base = dll->base;
184 event->data.info.load_dll.dbg_offset = dll->dbg_offset;
185 event->data.info.load_dll.dbg_size = dll->dbg_size;
186 event->data.info.load_dll.name = dll->name;
187 event->data.info.load_dll.unicode = 1;
188 return 1;
191 static int fill_unload_dll_event( struct debug_event *event, void *arg )
193 event->data.info.unload_dll.base = arg;
194 return 1;
197 static int fill_output_debug_string_event( struct debug_event *event, void *arg )
199 struct debug_event_output_string *data = arg;
200 event->data.info.output_string = *data;
201 return 1;
204 typedef int (*fill_event_func)( struct debug_event *event, void *arg );
206 #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT /* RIP_EVENT not supported */
208 static const fill_event_func fill_debug_event[NB_DEBUG_EVENTS] =
210 fill_exception_event, /* EXCEPTION_DEBUG_EVENT */
211 fill_create_thread_event, /* CREATE_THREAD_DEBUG_EVENT */
212 fill_create_process_event, /* CREATE_PROCESS_DEBUG_EVENT */
213 fill_exit_thread_event, /* EXIT_THREAD_DEBUG_EVENT */
214 fill_exit_process_event, /* EXIT_PROCESS_DEBUG_EVENT */
215 fill_load_dll_event, /* LOAD_DLL_DEBUG_EVENT */
216 fill_unload_dll_event, /* UNLOAD_DLL_DEBUG_EVENT */
217 fill_output_debug_string_event /* OUTPUT_DEBUG_STRING_EVENT */
221 /* unlink the first event from the queue */
222 static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
224 list_remove( &event->entry );
225 if (event->sender->debug_event == event) event->sender->debug_event = NULL;
226 release_object( event );
229 /* link an event at the end of the queue */
230 static void link_event( struct debug_event *event )
232 struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
234 assert( debug_ctx );
235 grab_object( event );
236 list_add_tail( &debug_ctx->event_queue, &event->entry );
237 if (!event->sender->debug_event) wake_up( &debug_ctx->obj, 0 );
240 /* find the next event that we can send to the debugger */
241 static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
243 struct debug_event *event;
245 LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
247 if (event->state == EVENT_SENT) continue; /* already sent */
248 if (event->sender->debug_event) continue; /* thread busy with another one */
249 return event;
251 return NULL;
254 static void debug_event_dump( struct object *obj, int verbose )
256 struct debug_event *debug_event = (struct debug_event *)obj;
257 assert( obj->ops == &debug_event_ops );
258 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
259 debug_event->sender, debug_event->data.code, debug_event->state );
262 static int debug_event_signaled( struct object *obj, struct thread *thread )
264 struct debug_event *debug_event = (struct debug_event *)obj;
265 assert( obj->ops == &debug_event_ops );
266 return debug_event->state == EVENT_CONTINUED;
269 static void debug_event_destroy( struct object *obj )
271 struct debug_event *event = (struct debug_event *)obj;
272 assert( obj->ops == &debug_event_ops );
274 /* If the event has been sent already, the handles are now under the */
275 /* responsibility of the debugger process, so we don't touch them */
276 if (event->state == EVENT_QUEUED)
278 struct process *debugger = event->debugger->process;
279 switch(event->data.code)
281 case CREATE_THREAD_DEBUG_EVENT:
282 close_handle( debugger, event->data.info.create_thread.handle, NULL );
283 break;
284 case CREATE_PROCESS_DEBUG_EVENT:
285 if (event->data.info.create_process.file)
286 close_handle( debugger, event->data.info.create_process.file, NULL );
287 close_handle( debugger, event->data.info.create_process.thread, NULL );
288 close_handle( debugger, event->data.info.create_process.process, NULL );
289 break;
290 case LOAD_DLL_DEBUG_EVENT:
291 if (event->data.info.load_dll.handle)
292 close_handle( debugger, event->data.info.load_dll.handle, NULL );
293 break;
296 if (event->sender->context == &event->context) event->sender->context = NULL;
297 release_object( event->sender );
298 release_object( event->debugger );
301 static void debug_ctx_dump( struct object *obj, int verbose )
303 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
304 assert( obj->ops == &debug_ctx_ops );
305 fprintf( stderr, "Debug context head=%p tail=%p\n",
306 debug_ctx->event_queue.next, debug_ctx->event_queue.prev );
309 static int debug_ctx_signaled( struct object *obj, struct thread *thread )
311 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
312 assert( obj->ops == &debug_ctx_ops );
313 return find_event_to_send( debug_ctx ) != NULL;
316 static void debug_ctx_destroy( struct object *obj )
318 struct list *ptr;
319 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
320 assert( obj->ops == &debug_ctx_ops );
322 /* free all pending events */
323 while ((ptr = list_head( &debug_ctx->event_queue )))
324 unlink_event( debug_ctx, LIST_ENTRY( ptr, struct debug_event, entry ));
327 /* continue a debug event */
328 static int continue_debug_event( struct process *process, struct thread *thread, int status )
330 struct debug_ctx *debug_ctx = current->debug_ctx;
332 if (debug_ctx && process->debugger == current && thread->process == process)
334 struct debug_event *event;
336 /* find the event in the queue */
337 LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
339 if (event->state != EVENT_SENT) continue;
340 if (event->sender == thread)
342 assert( event->sender->debug_event == event );
344 event->status = status;
345 event->state = EVENT_CONTINUED;
346 wake_up( &event->obj, 0 );
347 unlink_event( debug_ctx, event );
348 resume_process( process );
349 return 1;
353 /* not debugging this process, or no such event */
354 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
355 return 0;
358 /* alloc a debug event for a debugger */
359 static struct debug_event *alloc_debug_event( struct thread *thread, int code,
360 void *arg, const CONTEXT *context )
362 struct thread *debugger = thread->process->debugger;
363 struct debug_event *event;
365 assert( code > 0 && code <= NB_DEBUG_EVENTS );
366 /* cannot queue a debug event for myself */
367 assert( debugger->process != thread->process );
369 /* build the event */
370 if (!(event = alloc_object( &debug_event_ops ))) return 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;
376 if (!fill_debug_event[code-1]( event, arg ))
378 event->data.code = -1; /* make sure we don't attempt to close handles */
379 release_object( event );
380 return NULL;
382 if (context)
384 memcpy( &event->context, context, sizeof(event->context) );
385 thread->context = &event->context;
387 return event;
390 /* generate a debug event from inside the server and queue it */
391 void generate_debug_event( struct thread *thread, int code, void *arg )
393 if (thread->process->debugger)
395 struct debug_event *event = alloc_debug_event( thread, code, arg, NULL );
396 if (event)
398 link_event( event );
399 suspend_process( thread->process );
400 release_object( event );
405 /* attach a process to a debugger thread and suspend it */
406 static int debugger_attach( struct process *process, struct thread *debugger )
408 struct thread *thread;
410 if (process->debugger) goto error; /* already being debugged */
411 if (!is_process_init_done( process )) goto error; /* still starting up */
412 if (list_empty( &process->thread_list )) goto error; /* no thread running in the process */
414 /* make sure we don't create a debugging loop */
415 for (thread = debugger; thread; thread = thread->process->debugger)
416 if (thread->process == process) goto error;
418 /* don't let a debugger debug its console... won't work */
419 if (debugger->process->console && debugger->process->console->renderer->process == process)
420 goto error;
422 suspend_process( process );
423 if (!attach_process( process ) || !set_process_debugger( process, debugger ))
425 resume_process( process );
426 return 0;
428 if (!set_process_debug_flag( process, 1 ))
430 process->debugger = NULL;
431 resume_process( process );
432 return 0;
434 return 1;
436 error:
437 set_error( STATUS_ACCESS_DENIED );
438 return 0;
442 /* detach a process from a debugger thread (and resume it ?) */
443 int debugger_detach( struct process *process, struct thread *debugger )
445 struct debug_event *event;
446 struct debug_ctx *debug_ctx;
448 if (!process->debugger || process->debugger != debugger)
449 goto error; /* not currently debugged, or debugged by another debugger */
450 if (!debugger->debug_ctx ) goto error; /* should be a debugger */
451 /* init should be done, otherwise wouldn't be attached */
452 assert(is_process_init_done(process));
454 suspend_process( process );
455 /* send continue indication for all events */
456 debug_ctx = debugger->debug_ctx;
458 /* find the event in the queue
459 * FIXME: could loop on process' threads and look the debug_event field */
460 LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
462 if (event->state != EVENT_QUEUED) continue;
464 if (event->sender->process == process)
466 assert( event->sender->debug_event == event );
467 event->status = DBG_CONTINUE;
468 event->state = EVENT_CONTINUED;
469 wake_up( &event->obj, 0 );
470 unlink_event( debug_ctx, event );
471 /* from queued debug event */
472 resume_process( process );
473 break;
477 /* remove relationships between process and its debugger */
478 process->debugger = NULL;
479 if (!set_process_debug_flag( process, 0 )) clear_error(); /* ignore error */
480 detach_process( process );
482 /* from this function */
483 resume_process( process );
484 return 0;
486 error:
487 set_error( STATUS_ACCESS_DENIED );
488 return 0;
491 /* generate all startup events of a given process */
492 void generate_startup_debug_events( struct process *process, void *entry )
494 struct list *ptr;
495 struct thread *thread, *first_thread = get_process_first_thread( process );
497 /* generate creation events */
498 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
500 if (thread == first_thread)
501 generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT, entry );
502 else
503 generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
506 /* generate dll events (in loading order, i.e. reverse list order) */
507 ptr = list_tail( &process->dlls );
508 while (ptr)
510 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
511 generate_debug_event( first_thread, LOAD_DLL_DEBUG_EVENT, dll );
512 ptr = list_prev( &process->dlls, ptr );
516 /* set the debugger of a given process */
517 int set_process_debugger( struct process *process, struct thread *debugger )
519 struct debug_ctx *debug_ctx;
521 assert( !process->debugger );
523 if (!debugger->debug_ctx) /* need to allocate a context */
525 if (!(debug_ctx = alloc_object( &debug_ctx_ops ))) return 0;
526 debug_ctx->kill_on_exit = 1;
527 list_init( &debug_ctx->event_queue );
528 debugger->debug_ctx = debug_ctx;
530 process->debugger = debugger;
531 return 1;
534 /* a thread is exiting */
535 void debug_exit_thread( struct thread *thread )
537 if (thread->debug_ctx) /* this thread is a debugger */
539 if (thread->debug_ctx->kill_on_exit)
541 /* kill all debugged processes */
542 kill_debugged_processes( thread, thread->exit_code );
544 else
546 detach_debugged_processes( thread );
548 release_object( thread->debug_ctx );
549 thread->debug_ctx = NULL;
553 /* Wait for a debug event */
554 DECL_HANDLER(wait_debug_event)
556 struct debug_ctx *debug_ctx = current->debug_ctx;
557 struct debug_event *event;
559 if (!debug_ctx) /* current thread is not a debugger */
561 set_error( STATUS_INVALID_HANDLE );
562 return;
564 reply->wait = 0;
565 if ((event = find_event_to_send( debug_ctx )))
567 size_t size = get_reply_max_size();
568 event->state = EVENT_SENT;
569 event->sender->debug_event = event;
570 reply->pid = get_process_id( event->sender->process );
571 reply->tid = get_thread_id( event->sender );
572 if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
573 set_reply_data( &event->data, size );
575 else /* no event ready */
577 reply->pid = 0;
578 reply->tid = 0;
579 if (req->get_handle)
580 reply->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, FALSE );
584 /* Continue a debug event */
585 DECL_HANDLER(continue_debug_event)
587 struct process *process = get_process_from_id( req->pid );
588 if (process)
590 struct thread *thread = get_thread_from_id( req->tid );
591 if (thread)
593 continue_debug_event( process, thread, req->status );
594 release_object( thread );
596 release_object( process );
600 /* Start debugging an existing process */
601 DECL_HANDLER(debug_process)
603 struct process *process = get_process_from_id( req->pid );
604 if (!process) return;
606 if (!req->attach)
608 debugger_detach( process, current );
610 else if (debugger_attach( process, current ))
612 struct debug_event_exception data;
613 struct thread *thread = get_process_first_thread( process );
615 generate_startup_debug_events( process, NULL );
616 resume_process( process );
618 data.record.ExceptionCode = EXCEPTION_BREAKPOINT;
619 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
620 data.record.ExceptionRecord = NULL;
621 data.record.ExceptionAddress = get_thread_ip( thread );
622 data.record.NumberParameters = 0;
623 data.first = 1;
624 generate_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data );
626 release_object( process );
629 /* queue an exception event */
630 DECL_HANDLER(queue_exception_event)
632 reply->handle = 0;
633 if (current->process->debugger)
635 struct debug_event_exception data;
636 struct debug_event *event;
637 const CONTEXT *context = get_req_data();
638 const EXCEPTION_RECORD *rec = (const EXCEPTION_RECORD *)(context + 1);
640 if (get_req_data_size() < sizeof(*rec) + sizeof(*context))
642 set_error( STATUS_INVALID_PARAMETER );
643 return;
645 data.record = *rec;
646 data.first = req->first;
647 if ((event = alloc_debug_event( current, EXCEPTION_DEBUG_EVENT, &data, context )))
649 if ((reply->handle = alloc_handle( current->process, event, SYNCHRONIZE, FALSE )))
651 link_event( event );
652 suspend_process( current->process );
654 release_object( event );
659 /* retrieve the status of an exception event */
660 DECL_HANDLER(get_exception_status)
662 struct debug_event *event;
664 if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
665 0, &debug_event_ops )))
667 close_handle( current->process, req->handle, NULL );
668 if (event->state == EVENT_CONTINUED)
670 if (current->context == &event->context)
672 size_t size = min( sizeof(CONTEXT), get_reply_max_size() );
673 set_reply_data( &event->context, size );
674 current->context = NULL;
676 set_error( event->status );
678 else set_error( STATUS_PENDING );
679 release_object( event );
683 /* send an output string to the debugger */
684 DECL_HANDLER(output_debug_string)
686 struct debug_event_output_string data;
688 data.string = req->string;
689 data.unicode = req->unicode;
690 data.length = req->length;
691 generate_debug_event( current, OUTPUT_DEBUG_STRING_EVENT, &data );
694 /* simulate a breakpoint in a process */
695 DECL_HANDLER(debug_break)
697 struct process *process;
699 reply->self = 0;
700 if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION /*FIXME*/ )))
701 return;
702 if (process != current->process)
704 /* find a suitable thread to signal */
705 struct thread *thread;
706 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
708 if (send_thread_signal( thread, SIGTRAP )) goto done;
710 set_error( STATUS_ACCESS_DENIED );
712 else reply->self = 1;
714 done:
715 release_object( process );
718 /* set debugger kill on exit flag */
719 DECL_HANDLER(set_debugger_kill_on_exit)
721 if (!current->debug_ctx)
723 set_error( STATUS_ACCESS_DENIED );
724 return;
726 current->debug_ctx->kill_on_exit = req->kill_on_exit;