2 * Server-side debugger functions
4 * Copyright (C) 1999 Alexandre Julliard
18 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
23 struct object obj; /* object header */
24 struct debug_event *next; /* event queue */
25 struct debug_event *prev;
26 struct thread *sender; /* thread which sent this event */
27 struct thread *debugger; /* debugger thread receiving the event */
28 enum debug_event_state state; /* event state */
29 int status; /* continuation status */
30 debug_event_t data; /* event data */
36 struct object obj; /* object header */
37 struct debug_event *event_head; /* head of pending events queue */
38 struct debug_event *event_tail; /* tail of pending events queue */
39 struct debug_event *to_send; /* next event on the queue to send to debugger */
43 static void debug_event_dump( struct object *obj, int verbose );
44 static int debug_event_signaled( struct object *obj, struct thread *thread );
45 static void debug_event_destroy( struct object *obj );
47 static const struct object_ops debug_event_ops =
49 sizeof(struct debug_event), /* size */
50 debug_event_dump, /* dump */
51 add_queue, /* add_queue */
52 remove_queue, /* remove_queue */
53 debug_event_signaled, /* signaled */
54 no_satisfied, /* satisfied */
55 NULL, /* get_poll_events */
56 NULL, /* poll_event */
57 no_read_fd, /* get_read_fd */
58 no_write_fd, /* get_write_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_read_fd, /* get_read_fd */
79 no_write_fd, /* get_write_fd */
81 no_get_file_info, /* get_file_info */
82 debug_ctx_destroy /* destroy */
86 /* initialise the fields that do not need to be filled by the client */
87 static int fill_debug_event( struct thread *debugger, struct thread *thread,
88 struct debug_event *event )
92 /* some events need special handling */
93 switch(event->data.code)
95 case CREATE_THREAD_DEBUG_EVENT:
96 if ((event->data.info.create_thread.handle = alloc_handle( debugger->process, thread,
97 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
98 THREAD_ALL_ACCESS, FALSE )) == -1)
100 event->data.info.create_thread.teb = thread->teb;
101 event->data.info.create_thread.start = thread->entry;
103 case CREATE_PROCESS_DEBUG_EVENT:
104 if ((handle = alloc_handle( debugger->process, thread->process,
105 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
106 PROCESS_ALL_ACCESS, FALSE )) == -1)
108 event->data.info.create_process.process = handle;
110 if ((handle = alloc_handle( debugger->process, thread,
111 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
112 THREAD_ALL_ACCESS, FALSE )) == -1)
114 close_handle( debugger->process, event->data.info.create_process.process );
117 event->data.info.create_process.thread = handle;
120 if (thread->process->exe_file &&
121 ((handle = alloc_handle( debugger->process, thread->process->exe_file,
122 /* the doc says write access too, but this doesn't seem a good idea */
123 GENERIC_READ, FALSE )) == -1))
125 close_handle( debugger->process, event->data.info.create_process.process );
126 close_handle( debugger->process, event->data.info.create_process.thread );
129 event->data.info.create_process.file = handle;
130 event->data.info.create_process.teb = thread->teb;
131 event->data.info.create_process.base = thread->process->module;
132 event->data.info.create_process.start = thread->entry;
133 event->data.info.create_process.dbg_offset = 0;
134 event->data.info.create_process.dbg_size = 0;
135 event->data.info.create_process.name = 0;
136 event->data.info.create_process.unicode = 0;
138 case LOAD_DLL_DEBUG_EVENT:
139 if ((handle = event->data.info.load_dll.handle) != -1)
141 if ((handle = duplicate_handle( thread->process, handle, debugger->process,
142 GENERIC_READ, FALSE, 0 )) == -1)
144 event->data.info.load_dll.handle = handle;
147 case EXIT_PROCESS_DEBUG_EVENT:
148 case EXIT_THREAD_DEBUG_EVENT:
149 event->data.info.exit.exit_code = thread->exit_code;
151 case EXCEPTION_DEBUG_EVENT:
152 case UNLOAD_DLL_DEBUG_EVENT:
153 case OUTPUT_DEBUG_STRING_EVENT:
160 /* unlink the first event from the queue */
161 static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
163 if (event->prev) event->prev->next = event->next;
164 else debug_ctx->event_head = event->next;
165 if (event->next) event->next->prev = event->prev;
166 else debug_ctx->event_tail = event->prev;
167 if (debug_ctx->to_send == event) debug_ctx->to_send = event->next;
168 event->next = event->prev = NULL;
169 release_object( event );
172 /* link an event at the end of the queue */
173 static void link_event( struct debug_ctx *debug_ctx, struct debug_event *event )
175 grab_object( event );
177 event->prev = debug_ctx->event_tail;
178 debug_ctx->event_tail = event;
179 if (event->prev) event->prev->next = event;
180 else debug_ctx->event_head = event;
181 if (!debug_ctx->to_send)
183 debug_ctx->to_send = event;
184 wake_up( &debug_ctx->obj, 0 );
188 /* build a reply for the wait_debug_event request */
189 static void build_wait_debug_reply( struct thread *thread, struct object *obj, int signaled )
191 struct wait_debug_event_request *req = get_req_ptr( thread );
195 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
196 struct debug_event *event = debug_ctx->to_send;
198 /* the object that woke us has to be our debug context */
199 assert( obj->ops == &debug_ctx_ops );
202 event->state = EVENT_SENT;
203 debug_ctx->to_send = event->next;
204 req->event.code = event->data.code;
205 req->pid = event->sender->process;
206 req->tid = event->sender;
207 memcpy( &req->event, &event->data, sizeof(req->event) );
209 else /* timeout or error */
217 /* build a reply for the send_event request */
218 static void build_send_event_reply( struct thread *thread, struct object *obj, int signaled )
220 struct send_debug_event_request *req = get_req_ptr( thread );
221 struct debug_event *event = (struct debug_event *)obj;
222 assert( obj->ops == &debug_event_ops );
224 req->status = event->status;
225 /* copy the context into the reply */
226 if (event->data.code == EXCEPTION_DEBUG_EVENT)
227 memcpy( &req->event.info.exception.context,
228 &event->data.info.exception.context,
229 sizeof(req->event.info.exception.context) );
232 static void debug_event_dump( struct object *obj, int verbose )
234 struct debug_event *debug_event = (struct debug_event *)obj;
235 assert( obj->ops == &debug_event_ops );
236 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
237 debug_event->sender, debug_event->data.code, debug_event->state );
240 static int debug_event_signaled( struct object *obj, struct thread *thread )
242 struct debug_event *debug_event = (struct debug_event *)obj;
243 assert( obj->ops == &debug_event_ops );
244 return debug_event->state == EVENT_CONTINUED;
247 static void debug_event_destroy( struct object *obj )
249 struct debug_event *event = (struct debug_event *)obj;
250 assert( obj->ops == &debug_event_ops );
252 /* cannot still be in the queue */
253 assert( !event->next );
254 assert( !event->prev );
256 /* If the event has been sent already, the handles are now under the */
257 /* responsibility of the debugger process, so we don't touch them */
258 if (event->state == EVENT_QUEUED)
260 struct process *debugger = event->debugger->process;
261 switch(event->data.code)
263 case CREATE_THREAD_DEBUG_EVENT:
264 close_handle( debugger, event->data.info.create_thread.handle );
266 case CREATE_PROCESS_DEBUG_EVENT:
267 if (event->data.info.create_process.file != -1)
268 close_handle( debugger, event->data.info.create_process.file );
269 close_handle( debugger, event->data.info.create_process.thread );
270 close_handle( debugger, event->data.info.create_process.process );
272 case LOAD_DLL_DEBUG_EVENT:
273 if (event->data.info.load_dll.handle != -1)
274 close_handle( debugger, event->data.info.load_dll.handle );
278 release_object( event->sender );
279 release_object( event->debugger );
282 static void debug_ctx_dump( struct object *obj, int verbose )
284 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
285 assert( obj->ops == &debug_ctx_ops );
286 fprintf( stderr, "Debug context head=%p tail=%p to_send=%p\n",
287 debug_ctx->event_head, debug_ctx->event_tail, debug_ctx->to_send );
290 static int debug_ctx_signaled( struct object *obj, struct thread *thread )
292 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
293 assert( obj->ops == &debug_ctx_ops );
294 return debug_ctx->to_send != NULL;
297 static void debug_ctx_destroy( struct object *obj )
299 struct debug_event *event;
300 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
301 assert( obj->ops == &debug_ctx_ops );
303 /* free all pending events */
304 while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
307 /* wait for a debug event (or send a reply at once if one is pending) */
308 static int wait_for_debug_event( int timeout )
310 struct debug_ctx *debug_ctx = current->debug_ctx;
311 struct object *obj = &debug_ctx->obj;
314 if (!debug_ctx) /* current thread is not a debugger */
316 set_error( STATUS_INVALID_HANDLE );
319 if (timeout != -1) flags = SELECT_TIMEOUT;
320 return sleep_on( 1, &obj, flags, timeout, build_wait_debug_reply );
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 == debug_ctx->to_send) goto error;
335 if (event->sender == thread) break;
338 if (!event) goto error;
340 event->status = status;
341 event->state = EVENT_CONTINUED;
342 wake_up( &event->obj, 0 );
344 unlink_event( debug_ctx, event );
345 resume_process( process );
348 /* not debugging this process, or no such event */
349 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
353 /* queue a debug event for a debugger */
354 static struct debug_event *queue_debug_event( struct thread *thread, int code,
355 debug_event_t *data )
357 struct thread *debugger = thread->process->debugger;
358 struct debug_ctx *debug_ctx = debugger->debug_ctx;
359 struct debug_event *event;
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 if (data) memcpy( &event->data, data, sizeof(event->data) );
373 event->data.code = code;
375 if (!fill_debug_event( debugger, thread, event ))
377 event->data.code = -1; /* make sure we don't attempt to close handles */
378 release_object( event );
382 link_event( debug_ctx, event );
383 suspend_process( thread->process );
387 /* generate a debug event from inside the server and queue it */
388 void generate_debug_event( struct thread *thread, int code )
390 if (thread->process->debugger)
392 struct debug_event *event = queue_debug_event( thread, code, NULL );
393 if (event) release_object( event );
397 /* return a pointer to the context in case the thread is inside an exception event */
398 CONTEXT *get_debug_context( struct thread *thread )
400 struct debug_event *event;
401 struct thread *debugger = thread->process->debugger;
403 if (!debugger) return NULL; /* not being debugged */
404 assert( debugger->debug_ctx );
406 /* find the exception event in the debugger's queue */
407 for (event = debugger->debug_ctx->event_head; event; event = event->next)
408 if (event->sender == thread && (event->data.code == EXCEPTION_DEBUG_EVENT))
409 return &event->data.info.exception.context;
413 /* attach a process to a debugger thread */
414 int debugger_attach( struct process *process, struct thread *debugger )
416 struct debug_ctx *debug_ctx;
417 struct thread *thread;
419 if (process->debugger) /* already being debugged */
421 set_error( STATUS_ACCESS_DENIED );
424 /* make sure we don't create a debugging loop */
425 for (thread = debugger; thread; thread = thread->process->debugger)
426 if (thread->process == process)
428 set_error( STATUS_ACCESS_DENIED );
432 if (!debugger->debug_ctx) /* need to allocate a context */
434 if (!(debug_ctx = alloc_object( &debug_ctx_ops, -1 ))) return 0;
435 debug_ctx->event_head = NULL;
436 debug_ctx->event_tail = NULL;
437 debug_ctx->to_send = NULL;
438 debugger->debug_ctx = debug_ctx;
440 process->debugger = debugger;
444 /* a thread is exiting */
445 void debug_exit_thread( struct thread *thread )
447 if (thread->debug_ctx) /* this thread is a debugger */
449 /* kill all debugged processes */
450 kill_debugged_processes( thread, thread->exit_code );
451 release_object( thread->debug_ctx );
452 thread->debug_ctx = NULL;
456 /* Wait for a debug event */
457 DECL_HANDLER(wait_debug_event)
459 if (!wait_for_debug_event( req->timeout ))
467 /* Continue a debug event */
468 DECL_HANDLER(continue_debug_event)
470 struct process *process = get_process_from_id( req->pid );
473 struct thread *thread = get_thread_from_id( req->tid );
476 continue_debug_event( process, thread, req->status );
477 release_object( thread );
479 release_object( process );
483 /* Start debugging an existing process */
484 DECL_HANDLER(debug_process)
486 struct process *process = get_process_from_id( req->pid );
487 if (!process) return;
488 if (debugger_attach( process, current ))
490 struct thread *thread = process->thread_list;
491 generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT );
492 while ((thread = thread->next)) generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT );
493 /* FIXME: load dll + breakpoint exception events */
495 release_object( process );
498 /* Send a debug event */
499 DECL_HANDLER(send_debug_event)
501 struct debug_event *event;
502 int code = req->event.code;
504 if ((code <= 0) || (code > RIP_EVENT))
506 fatal_protocol_error( current, "send_debug_event: bad code %d\n", code );
510 if (current->process->debugger && ((event = queue_debug_event( current, code, &req->event ))))
512 /* wait for continue_debug_event */
513 struct object *obj = &event->obj;
514 sleep_on( 1, &obj, 0, -1, build_send_event_reply );
515 release_object( event );