2 * Server-side debugger functions
4 * Copyright (C) 1999 Alexandre Julliard
19 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
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 */
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 */
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 );
48 static const struct object_ops debug_event_ops =
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 */
60 no_get_file_info, /* get_file_info */
61 debug_event_destroy /* destroy */
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 );
68 static const struct object_ops debug_ctx_ops =
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 */
80 no_get_file_info, /* get_file_info */
81 debug_ctx_destroy /* destroy */
85 /* routines to build an event according to its type */
87 static int fill_exception_event( struct debug_event *event, void *arg )
89 memcpy( &event->data.info.exception, arg, sizeof(event->data.info.exception) );
93 static int fill_create_thread_event( struct debug_event *event, void *arg )
95 struct process *debugger = event->debugger->process;
96 struct thread *thread = event->sender;
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;
107 static int fill_create_process_event( struct debug_event *event, void *arg )
109 struct process *debugger = event->debugger->process;
110 struct thread *thread = event->sender;
111 struct process *process = thread->process;
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;
118 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
119 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )))
121 close_handle( debugger, event->data.info.create_process.process, NULL );
124 event->data.info.create_process.thread = handle;
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 )))
131 close_handle( debugger, event->data.info.create_process.process, NULL );
132 close_handle( debugger, event->data.info.create_process.thread, NULL );
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;
146 static int fill_exit_thread_event( struct debug_event *event, void *arg )
148 struct thread *thread = arg;
149 event->data.info.exit.exit_code = thread->exit_code;
153 static int fill_exit_process_event( struct debug_event *event, void *arg )
155 struct process *process = arg;
156 event->data.info.exit.exit_code = process->exit_code;
160 static int fill_load_dll_event( struct debug_event *event, void *arg )
162 struct process *debugger = event->debugger->process;
163 struct process_dll *dll = arg;
166 if (dll->file && !(handle = alloc_handle( debugger, dll->file, GENERIC_READ, FALSE )))
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;
177 static int fill_unload_dll_event( struct debug_event *event, void *arg )
179 event->data.info.unload_dll.base = arg;
183 static int fill_output_debug_string_event( struct debug_event *event, void *arg )
185 struct debug_event_output_string *data = arg;
186 event->data.info.output_string = *data;
190 typedef int (*fill_event_func)( struct debug_event *event, void *arg );
192 #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT /* RIP_EVENT not supported */
194 static const fill_event_func fill_debug_event[NB_DEBUG_EVENTS] =
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 */
207 /* unlink the first event from the queue */
208 static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
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 );
219 /* link an event at the end of the queue */
220 static void link_event( struct debug_event *event )
222 struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
225 grab_object( event );
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 );
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 )
237 struct debug_event *event;
238 for (event = debug_ctx->event_head; event; event = event->next)
240 if (event->state == EVENT_SENT) continue; /* already sent */
241 if (event->sender->debug_event) continue; /* thread busy with another one */
247 static void debug_event_dump( struct object *obj, int verbose )
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 );
255 static int debug_event_signaled( struct object *obj, struct thread *thread )
257 struct debug_event *debug_event = (struct debug_event *)obj;
258 assert( obj->ops == &debug_event_ops );
259 return debug_event->state == EVENT_CONTINUED;
262 static void debug_event_destroy( struct object *obj )
264 struct debug_event *event = (struct debug_event *)obj;
265 assert( obj->ops == &debug_event_ops );
267 /* cannot still be in the queue */
268 assert( !event->next );
269 assert( !event->prev );
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_head, debug_ctx->event_tail );
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 )
315 struct debug_event *event;
316 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
317 assert( obj->ops == &debug_ctx_ops );
319 /* free all pending events */
320 while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
323 /* continue a debug event */
324 static int continue_debug_event( struct process *process, struct thread *thread, int status )
326 struct debug_event *event;
327 struct debug_ctx *debug_ctx = current->debug_ctx;
329 if (!debug_ctx || process->debugger != current || thread->process != process) goto error;
331 /* find the event in the queue */
332 for (event = debug_ctx->event_head; event; event = event->next)
334 if (event->state != EVENT_SENT) continue;
335 if (event->sender == thread) break;
337 if (!event) goto error;
339 assert( event->sender->debug_event == event );
341 event->status = status;
342 event->state = EVENT_CONTINUED;
343 wake_up( &event->obj, 0 );
345 unlink_event( debug_ctx, event );
346 resume_process( process );
349 /* not debugging this process, or no such event */
350 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
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 )
358 struct thread *debugger = thread->process->debugger;
359 struct debug_event *event;
361 assert( code > 0 && code <= NB_DEBUG_EVENTS );
362 /* cannot queue a debug event for myself */
363 assert( debugger->process != thread->process );
365 /* build the event */
366 if (!(event = alloc_object( &debug_event_ops, -1 ))) return 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;
374 if (!fill_debug_event[code-1]( event, arg ))
376 event->data.code = -1; /* make sure we don't attempt to close handles */
377 release_object( event );
382 memcpy( &event->context, context, sizeof(event->context) );
383 thread->context = &event->context;
388 /* generate a debug event from inside the server and queue it */
389 void generate_debug_event( struct thread *thread, int code, void *arg )
391 if (thread->process->debugger)
393 struct debug_event *event = alloc_debug_event( thread, code, arg, NULL );
397 suspend_process( thread->process );
398 release_object( event );
403 /* attach a process to a debugger thread and suspend it */
404 static int debugger_attach( struct process *process, struct thread *debugger )
406 struct thread *thread;
408 if (process->debugger) goto error; /* already being debugged */
409 if (process->init_event) goto error; /* still starting up */
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 &&
417 debugger->process->console->renderer == process &&
418 process->console) goto error;
420 suspend_process( process );
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)
426 resume_process( process );
430 if (set_process_debugger( process, debugger )) return 1;
431 resume_process( process );
435 set_error( STATUS_ACCESS_DENIED );
439 /* generate all startup events of a given process */
440 void generate_startup_debug_events( struct process *process, void *entry )
442 struct process_dll *dll;
443 struct thread *thread = process->thread_list;
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 );
450 /* generate dll events (in loading order, i.e. reverse list order) */
452 while (dll->next) dll = dll->next;
453 while (dll != &process->exe)
455 generate_debug_event( process->thread_list, LOAD_DLL_DEBUG_EVENT, dll );
460 /* set the debugger of a given process */
461 int set_process_debugger( struct process *process, struct thread *debugger )
463 struct debug_ctx *debug_ctx;
465 assert( !process->debugger );
467 if (!debugger->debug_ctx) /* need to allocate a context */
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;
474 process->debugger = debugger;
478 /* a thread is exiting */
479 void debug_exit_thread( struct thread *thread )
481 if (thread->debug_ctx) /* this thread is a debugger */
483 /* kill all debugged processes */
484 kill_debugged_processes( thread, thread->exit_code );
485 release_object( thread->debug_ctx );
486 thread->debug_ctx = NULL;
490 /* Wait for a debug event */
491 DECL_HANDLER(wait_debug_event)
493 struct debug_ctx *debug_ctx = current->debug_ctx;
494 struct debug_event *event;
496 if (!debug_ctx) /* current thread is not a debugger */
498 set_error( STATUS_INVALID_HANDLE );
502 if ((event = find_event_to_send( debug_ctx )))
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 );
513 else /* no event ready */
517 set_req_data_size( req, 0 );
519 req->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, FALSE );
523 /* Continue a debug event */
524 DECL_HANDLER(continue_debug_event)
526 struct process *process = get_process_from_id( req->pid );
529 struct thread *thread = get_thread_from_id( req->tid );
532 continue_debug_event( process, thread, req->status );
533 release_object( thread );
535 release_object( process );
539 /* Start debugging an existing process */
540 DECL_HANDLER(debug_process)
542 struct debug_event_exception data;
543 struct process *process = get_process_from_id( req->pid );
544 if (!process) return;
546 if (debugger_attach( process, current ))
548 generate_startup_debug_events( process, NULL );
549 resume_process( process );
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;
557 generate_debug_event( process->thread_list, EXCEPTION_DEBUG_EVENT, &data );
559 release_object( process );
562 /* queue an exception event */
563 DECL_HANDLER(queue_exception_event)
566 if (current->process->debugger)
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);
573 if (get_req_data_size( req ) < sizeof(*rec) + sizeof(*context))
575 set_error( STATUS_INVALID_PARAMETER );
579 data.first = req->first;
580 if ((event = alloc_debug_event( current, EXCEPTION_DEBUG_EVENT, &data, context )))
582 if ((req->handle = alloc_handle( current->process, event, SYNCHRONIZE, FALSE )))
585 suspend_process( current->process );
587 release_object( event );
592 /* retrieve the status of an exception event */
593 DECL_HANDLER(get_exception_status)
595 struct debug_event *event;
599 if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
600 0, &debug_event_ops )))
602 if (event->state == EVENT_CONTINUED)
604 req->status = event->status;
605 if (current->context == &event->context)
607 size = min( sizeof(CONTEXT), get_req_data_size(req) );
608 memcpy( get_req_data(req), &event->context, size );
609 current->context = NULL;
612 else set_error( STATUS_PENDING );
613 release_object( event );
615 set_req_data_size( req, size );
618 /* send an output string to the debugger */
619 DECL_HANDLER(output_debug_string)
621 struct debug_event_output_string data;
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 );