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
22 #include "wine/port.h"
39 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
44 struct object obj; /* object header */
45 struct list entry; /* entry in event queue */
46 struct thread *sender; /* thread which sent this event */
47 struct thread *debugger; /* debugger thread receiving the event */
48 enum debug_event_state state; /* event state */
49 int status; /* continuation status */
50 debug_event_t data; /* event data */
51 CONTEXT context; /* register context */
57 struct object obj; /* object header */
58 struct list event_queue; /* pending events queue */
59 int kill_on_exit;/* kill debuggees on debugger exit ? */
63 static void debug_event_dump( struct object *obj, int verbose );
64 static int debug_event_signaled( struct object *obj, struct thread *thread );
65 static void debug_event_destroy( struct object *obj );
67 static const struct object_ops debug_event_ops =
69 sizeof(struct debug_event), /* size */
70 debug_event_dump, /* dump */
71 add_queue, /* add_queue */
72 remove_queue, /* remove_queue */
73 debug_event_signaled, /* signaled */
74 no_satisfied, /* satisfied */
75 no_get_fd, /* get_fd */
76 debug_event_destroy /* destroy */
79 static void debug_ctx_dump( struct object *obj, int verbose );
80 static int debug_ctx_signaled( struct object *obj, struct thread *thread );
81 static void debug_ctx_destroy( struct object *obj );
83 static const struct object_ops debug_ctx_ops =
85 sizeof(struct debug_ctx), /* size */
86 debug_ctx_dump, /* dump */
87 add_queue, /* add_queue */
88 remove_queue, /* remove_queue */
89 debug_ctx_signaled, /* signaled */
90 no_satisfied, /* satisfied */
91 no_get_fd, /* get_fd */
92 debug_ctx_destroy /* destroy */
96 /* routines to build an event according to its type */
98 static int fill_exception_event( struct debug_event *event, void *arg )
100 memcpy( &event->data.info.exception, arg, sizeof(event->data.info.exception) );
104 static int fill_create_thread_event( struct debug_event *event, void *arg )
106 struct process *debugger = event->debugger->process;
107 struct thread *thread = event->sender;
110 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
111 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE ))) return 0;
112 event->data.info.create_thread.handle = handle;
113 event->data.info.create_thread.teb = thread->teb;
114 event->data.info.create_thread.start = arg;
118 static int fill_create_process_event( struct debug_event *event, void *arg )
120 struct process *debugger = event->debugger->process;
121 struct thread *thread = event->sender;
122 struct process *process = thread->process;
125 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
126 if (!(handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, FALSE ))) return 0;
127 event->data.info.create_process.process = handle;
129 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
130 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )))
132 close_handle( debugger, event->data.info.create_process.process, NULL );
135 event->data.info.create_process.thread = handle;
138 if (process->exe.file &&
139 /* the doc says write access too, but this doesn't seem a good idea */
140 !(handle = alloc_handle( debugger, process->exe.file, GENERIC_READ, FALSE )))
142 close_handle( debugger, event->data.info.create_process.process, NULL );
143 close_handle( debugger, event->data.info.create_process.thread, NULL );
146 event->data.info.create_process.file = handle;
147 event->data.info.create_process.teb = thread->teb;
148 event->data.info.create_process.base = process->exe.base;
149 event->data.info.create_process.start = arg;
150 event->data.info.create_process.dbg_offset = process->exe.dbg_offset;
151 event->data.info.create_process.dbg_size = process->exe.dbg_size;
152 event->data.info.create_process.name = process->exe.name;
153 event->data.info.create_process.unicode = 1;
157 static int fill_exit_thread_event( struct debug_event *event, void *arg )
159 struct thread *thread = arg;
160 event->data.info.exit.exit_code = thread->exit_code;
164 static int fill_exit_process_event( struct debug_event *event, void *arg )
166 struct process *process = arg;
167 event->data.info.exit.exit_code = process->exit_code;
171 static int fill_load_dll_event( struct debug_event *event, void *arg )
173 struct process *debugger = event->debugger->process;
174 struct process_dll *dll = arg;
175 obj_handle_t handle = 0;
177 if (dll->file && !(handle = alloc_handle( debugger, dll->file, GENERIC_READ, FALSE )))
179 event->data.info.load_dll.handle = handle;
180 event->data.info.load_dll.base = dll->base;
181 event->data.info.load_dll.dbg_offset = dll->dbg_offset;
182 event->data.info.load_dll.dbg_size = dll->dbg_size;
183 event->data.info.load_dll.name = dll->name;
184 event->data.info.load_dll.unicode = 1;
188 static int fill_unload_dll_event( struct debug_event *event, void *arg )
190 event->data.info.unload_dll.base = arg;
194 static int fill_output_debug_string_event( struct debug_event *event, void *arg )
196 struct debug_event_output_string *data = arg;
197 event->data.info.output_string = *data;
201 typedef int (*fill_event_func)( struct debug_event *event, void *arg );
203 #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT /* RIP_EVENT not supported */
205 static const fill_event_func fill_debug_event[NB_DEBUG_EVENTS] =
207 fill_exception_event, /* EXCEPTION_DEBUG_EVENT */
208 fill_create_thread_event, /* CREATE_THREAD_DEBUG_EVENT */
209 fill_create_process_event, /* CREATE_PROCESS_DEBUG_EVENT */
210 fill_exit_thread_event, /* EXIT_THREAD_DEBUG_EVENT */
211 fill_exit_process_event, /* EXIT_PROCESS_DEBUG_EVENT */
212 fill_load_dll_event, /* LOAD_DLL_DEBUG_EVENT */
213 fill_unload_dll_event, /* UNLOAD_DLL_DEBUG_EVENT */
214 fill_output_debug_string_event /* OUTPUT_DEBUG_STRING_EVENT */
218 /* unlink the first event from the queue */
219 static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
221 list_remove( &event->entry );
222 if (event->sender->debug_event == event) event->sender->debug_event = NULL;
223 release_object( event );
226 /* link an event at the end of the queue */
227 static void link_event( struct debug_event *event )
229 struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
232 grab_object( event );
233 list_add_tail( &debug_ctx->event_queue, &event->entry );
234 if (!event->sender->debug_event) wake_up( &debug_ctx->obj, 0 );
237 /* find the next event that we can send to the debugger */
238 static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
240 struct debug_event *event;
242 LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
244 if (event->state == EVENT_SENT) continue; /* already sent */
245 if (event->sender->debug_event) continue; /* thread busy with another one */
251 static void debug_event_dump( struct object *obj, int verbose )
253 struct debug_event *debug_event = (struct debug_event *)obj;
254 assert( obj->ops == &debug_event_ops );
255 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
256 debug_event->sender, debug_event->data.code, debug_event->state );
259 static int debug_event_signaled( struct object *obj, struct thread *thread )
261 struct debug_event *debug_event = (struct debug_event *)obj;
262 assert( obj->ops == &debug_event_ops );
263 return debug_event->state == EVENT_CONTINUED;
266 static void debug_event_destroy( struct object *obj )
268 struct debug_event *event = (struct debug_event *)obj;
269 assert( obj->ops == &debug_event_ops );
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)
275 struct process *debugger = event->debugger->process;
276 switch(event->data.code)
278 case CREATE_THREAD_DEBUG_EVENT:
279 close_handle( debugger, event->data.info.create_thread.handle, NULL );
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 );
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 );
293 if (event->sender->context == &event->context) event->sender->context = NULL;
294 release_object( event->sender );
295 release_object( event->debugger );
298 static void debug_ctx_dump( struct object *obj, int verbose )
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_queue.next, debug_ctx->event_queue.prev );
306 static int debug_ctx_signaled( struct object *obj, struct thread *thread )
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;
313 static void debug_ctx_destroy( struct object *obj )
316 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
317 assert( obj->ops == &debug_ctx_ops );
319 /* free all pending events */
320 while ((ptr = list_head( &debug_ctx->event_queue )))
321 unlink_event( debug_ctx, LIST_ENTRY( ptr, struct debug_event, entry ));
324 /* continue a debug event */
325 static int continue_debug_event( struct process *process, struct thread *thread, int status )
327 struct debug_ctx *debug_ctx = current->debug_ctx;
329 if (debug_ctx && process->debugger == current && thread->process == process)
331 struct debug_event *event;
333 /* find the event in the queue */
334 LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
336 if (event->state != EVENT_SENT) continue;
337 if (event->sender == thread)
339 assert( event->sender->debug_event == event );
341 event->status = status;
342 event->state = EVENT_CONTINUED;
343 wake_up( &event->obj, 0 );
344 unlink_event( debug_ctx, event );
345 resume_process( process );
350 /* not debugging this process, or no such event */
351 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
355 /* alloc a debug event for a debugger */
356 static struct debug_event *alloc_debug_event( struct thread *thread, int code,
357 void *arg, const CONTEXT *context )
359 struct thread *debugger = thread->process->debugger;
360 struct debug_event *event;
362 assert( code > 0 && code <= NB_DEBUG_EVENTS );
363 /* cannot queue a debug event for myself */
364 assert( debugger->process != thread->process );
366 /* build the event */
367 if (!(event = alloc_object( &debug_event_ops ))) return 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;
373 if (!fill_debug_event[code-1]( event, arg ))
375 event->data.code = -1; /* make sure we don't attempt to close handles */
376 release_object( event );
381 memcpy( &event->context, context, sizeof(event->context) );
382 thread->context = &event->context;
387 /* generate a debug event from inside the server and queue it */
388 void generate_debug_event( struct thread *thread, int code, void *arg )
390 if (thread->process->debugger)
392 struct debug_event *event = alloc_debug_event( thread, code, arg, NULL );
396 suspend_process( thread->process );
397 release_object( event );
402 /* attach a process to a debugger thread and suspend it */
403 static int debugger_attach( struct process *process, struct thread *debugger )
405 struct thread *thread;
407 if (process->debugger) goto error; /* already being debugged */
408 if (!is_process_init_done( process )) goto error; /* still starting up */
409 if (!process->thread_list) goto error; /* no thread running in the process */
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;
415 /* don't let a debugger debug its console... won't work */
416 if (debugger->process->console && debugger->process->console->renderer->process == process)
419 suspend_process( process );
420 if (!attach_process( process ) || !set_process_debugger( process, debugger ))
422 resume_process( process );
425 if (!set_process_debug_flag( process, 1 ))
427 process->debugger = NULL;
428 resume_process( process );
434 set_error( STATUS_ACCESS_DENIED );
439 /* detach a process from a debugger thread (and resume it ?) */
440 int debugger_detach( struct process *process, struct thread *debugger )
442 struct debug_event *event;
443 struct debug_ctx *debug_ctx;
445 if (!process->debugger || process->debugger != debugger)
446 goto error; /* not currently debugged, or debugged by another debugger */
447 if (!debugger->debug_ctx ) goto error; /* should be a debugger */
448 /* init should be done, otherwise wouldn't be attached */
449 assert(is_process_init_done(process));
451 suspend_process( process );
452 /* send continue indication for all events */
453 debug_ctx = debugger->debug_ctx;
455 /* find the event in the queue
456 * FIXME: could loop on process' threads and look the debug_event field */
457 LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
459 if (event->state != EVENT_QUEUED) continue;
461 if (event->sender->process == process)
463 assert( event->sender->debug_event == event );
464 event->status = DBG_CONTINUE;
465 event->state = EVENT_CONTINUED;
466 wake_up( &event->obj, 0 );
467 unlink_event( debug_ctx, event );
468 /* from queued debug event */
469 resume_process( process );
474 /* remove relationships between process and its debugger */
475 process->debugger = NULL;
476 if (!set_process_debug_flag( process, 0 )) clear_error(); /* ignore error */
477 detach_process( process );
479 /* from this function */
480 resume_process( process );
484 set_error( STATUS_ACCESS_DENIED );
488 /* generate all startup events of a given process */
489 void generate_startup_debug_events( struct process *process, void *entry )
491 struct process_dll *dll;
492 struct thread *thread = process->thread_list;
494 /* generate creation events */
495 generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT, entry );
496 while ((thread = thread->proc_next))
497 generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
499 /* generate dll events (in loading order, i.e. reverse list order) */
501 while (dll->next) dll = dll->next;
502 while (dll != &process->exe)
504 generate_debug_event( process->thread_list, LOAD_DLL_DEBUG_EVENT, dll );
509 /* set the debugger of a given process */
510 int set_process_debugger( struct process *process, struct thread *debugger )
512 struct debug_ctx *debug_ctx;
514 assert( !process->debugger );
516 if (!debugger->debug_ctx) /* need to allocate a context */
518 if (!(debug_ctx = alloc_object( &debug_ctx_ops ))) return 0;
519 debug_ctx->kill_on_exit = 1;
520 list_init( &debug_ctx->event_queue );
521 debugger->debug_ctx = debug_ctx;
523 process->debugger = debugger;
527 /* a thread is exiting */
528 void debug_exit_thread( struct thread *thread )
530 if (thread->debug_ctx) /* this thread is a debugger */
532 if (thread->debug_ctx->kill_on_exit)
534 /* kill all debugged processes */
535 kill_debugged_processes( thread, thread->exit_code );
539 detach_debugged_processes( thread );
541 release_object( thread->debug_ctx );
542 thread->debug_ctx = NULL;
546 /* Wait for a debug event */
547 DECL_HANDLER(wait_debug_event)
549 struct debug_ctx *debug_ctx = current->debug_ctx;
550 struct debug_event *event;
552 if (!debug_ctx) /* current thread is not a debugger */
554 set_error( STATUS_INVALID_HANDLE );
558 if ((event = find_event_to_send( debug_ctx )))
560 size_t size = get_reply_max_size();
561 event->state = EVENT_SENT;
562 event->sender->debug_event = event;
563 reply->pid = get_process_id( event->sender->process );
564 reply->tid = get_thread_id( event->sender );
565 if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
566 set_reply_data( &event->data, size );
568 else /* no event ready */
573 reply->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, FALSE );
577 /* Continue a debug event */
578 DECL_HANDLER(continue_debug_event)
580 struct process *process = get_process_from_id( req->pid );
583 struct thread *thread = get_thread_from_id( req->tid );
586 continue_debug_event( process, thread, req->status );
587 release_object( thread );
589 release_object( process );
593 /* Start debugging an existing process */
594 DECL_HANDLER(debug_process)
596 struct process *process = get_process_from_id( req->pid );
597 if (!process) return;
601 debugger_detach( process, current );
603 else if (debugger_attach( process, current ))
605 struct debug_event_exception data;
607 generate_startup_debug_events( process, NULL );
608 resume_process( process );
610 data.record.ExceptionCode = EXCEPTION_BREAKPOINT;
611 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
612 data.record.ExceptionRecord = NULL;
613 data.record.ExceptionAddress = get_thread_ip( process->thread_list );
614 data.record.NumberParameters = 0;
616 generate_debug_event( process->thread_list, EXCEPTION_DEBUG_EVENT, &data );
618 release_object( process );
621 /* queue an exception event */
622 DECL_HANDLER(queue_exception_event)
625 if (current->process->debugger)
627 struct debug_event_exception data;
628 struct debug_event *event;
629 const CONTEXT *context = get_req_data();
630 const EXCEPTION_RECORD *rec = (const EXCEPTION_RECORD *)(context + 1);
632 if (get_req_data_size() < sizeof(*rec) + sizeof(*context))
634 set_error( STATUS_INVALID_PARAMETER );
638 data.first = req->first;
639 if ((event = alloc_debug_event( current, EXCEPTION_DEBUG_EVENT, &data, context )))
641 if ((reply->handle = alloc_handle( current->process, event, SYNCHRONIZE, FALSE )))
644 suspend_process( current->process );
646 release_object( event );
651 /* retrieve the status of an exception event */
652 DECL_HANDLER(get_exception_status)
654 struct debug_event *event;
657 if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
658 0, &debug_event_ops )))
660 if (event->state == EVENT_CONTINUED)
662 reply->status = event->status;
663 if (current->context == &event->context)
665 size_t size = min( sizeof(CONTEXT), get_reply_max_size() );
666 set_reply_data( &event->context, size );
667 current->context = NULL;
670 else set_error( STATUS_PENDING );
671 release_object( event );
675 /* send an output string to the debugger */
676 DECL_HANDLER(output_debug_string)
678 struct debug_event_output_string data;
680 data.string = req->string;
681 data.unicode = req->unicode;
682 data.length = req->length;
683 generate_debug_event( current, OUTPUT_DEBUG_STRING_EVENT, &data );
686 /* simulate a breakpoint in a process */
687 DECL_HANDLER(debug_break)
689 struct process *process;
692 if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION /*FIXME*/ )))
694 if (process != current->process)
696 /* find a suitable thread to signal */
697 struct thread *thread;
698 for (thread = process->thread_list; thread; thread = thread->proc_next)
700 if (send_thread_signal( thread, SIGTRAP )) break;
702 if (!thread) set_error( STATUS_ACCESS_DENIED );
704 else reply->self = 1;
705 release_object( process );
708 /* set debugger kill on exit flag */
709 DECL_HANDLER(set_debugger_kill_on_exit)
711 if (!current->debug_ctx)
713 set_error( STATUS_ACCESS_DENIED );
716 current->debug_ctx->kill_on_exit = req->kill_on_exit;