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 */
42 static void debug_event_dump( struct object
*obj
, int verbose
);
43 static int debug_event_signaled( struct object
*obj
, struct thread
*thread
);
44 static void debug_event_destroy( struct object
*obj
);
46 static const struct object_ops debug_event_ops
=
48 sizeof(struct debug_event
), /* size */
49 debug_event_dump
, /* dump */
50 add_queue
, /* add_queue */
51 remove_queue
, /* remove_queue */
52 debug_event_signaled
, /* signaled */
53 no_satisfied
, /* satisfied */
54 NULL
, /* get_poll_events */
55 NULL
, /* poll_event */
56 no_get_fd
, /* get_fd */
58 no_get_file_info
, /* get_file_info */
59 debug_event_destroy
/* destroy */
62 static void debug_ctx_dump( struct object
*obj
, int verbose
);
63 static int debug_ctx_signaled( struct object
*obj
, struct thread
*thread
);
64 static void debug_ctx_destroy( struct object
*obj
);
66 static const struct object_ops debug_ctx_ops
=
68 sizeof(struct debug_ctx
), /* size */
69 debug_ctx_dump
, /* dump */
70 add_queue
, /* add_queue */
71 remove_queue
, /* remove_queue */
72 debug_ctx_signaled
, /* signaled */
73 no_satisfied
, /* satisfied */
74 NULL
, /* get_poll_events */
75 NULL
, /* poll_event */
76 no_get_fd
, /* get_fd */
78 no_get_file_info
, /* get_file_info */
79 debug_ctx_destroy
/* destroy */
83 /* routines to build an event according to its type */
85 static int fill_exception_event( struct debug_event
*event
, void *arg
)
87 memcpy( &event
->data
.info
.exception
, arg
, sizeof(event
->data
.info
.exception
) );
91 static int fill_create_thread_event( struct debug_event
*event
, void *arg
)
93 struct process
*debugger
= event
->debugger
->process
;
94 struct thread
*thread
= event
->sender
;
97 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
98 if ((handle
= alloc_handle( debugger
, thread
, THREAD_ALL_ACCESS
, FALSE
)) == -1)
100 event
->data
.info
.create_thread
.handle
= handle
;
101 event
->data
.info
.create_thread
.teb
= thread
->teb
;
102 event
->data
.info
.create_thread
.start
= arg
;
106 static int fill_create_process_event( struct debug_event
*event
, void *arg
)
108 struct process
*debugger
= event
->debugger
->process
;
109 struct thread
*thread
= event
->sender
;
110 struct process
*process
= thread
->process
;
113 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
114 if ((handle
= alloc_handle( debugger
, process
, PROCESS_ALL_ACCESS
, FALSE
)) == -1)
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
)) == -1)
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
)) == -1))
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
)) == -1)
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 output_debug_string_request
*req
= arg
;
186 event
->data
.info
.output_string
.string
= req
->string
;
187 event
->data
.info
.output_string
.unicode
= req
->unicode
;
188 event
->data
.info
.output_string
.length
= req
->length
;
192 typedef int (*fill_event_func
)( struct debug_event
*event
, void *arg
);
194 #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT /* RIP_EVENT not supported */
196 static const fill_event_func fill_debug_event
[NB_DEBUG_EVENTS
] =
198 fill_exception_event
, /* EXCEPTION_DEBUG_EVENT */
199 fill_create_thread_event
, /* CREATE_THREAD_DEBUG_EVENT */
200 fill_create_process_event
, /* CREATE_PROCESS_DEBUG_EVENT */
201 fill_exit_thread_event
, /* EXIT_THREAD_DEBUG_EVENT */
202 fill_exit_process_event
, /* EXIT_PROCESS_DEBUG_EVENT */
203 fill_load_dll_event
, /* LOAD_DLL_DEBUG_EVENT */
204 fill_unload_dll_event
, /* UNLOAD_DLL_DEBUG_EVENT */
205 fill_output_debug_string_event
/* OUTPUT_DEBUG_STRING_EVENT */
209 /* unlink the first event from the queue */
210 static void unlink_event( struct debug_ctx
*debug_ctx
, struct debug_event
*event
)
212 if (event
->prev
) event
->prev
->next
= event
->next
;
213 else debug_ctx
->event_head
= event
->next
;
214 if (event
->next
) event
->next
->prev
= event
->prev
;
215 else debug_ctx
->event_tail
= event
->prev
;
216 event
->next
= event
->prev
= NULL
;
217 if (event
->sender
->debug_event
== event
) event
->sender
->debug_event
= NULL
;
218 release_object( event
);
221 /* link an event at the end of the queue */
222 static void link_event( struct debug_ctx
*debug_ctx
, struct debug_event
*event
)
224 grab_object( event
);
226 event
->prev
= debug_ctx
->event_tail
;
227 debug_ctx
->event_tail
= event
;
228 if (event
->prev
) event
->prev
->next
= event
;
229 else debug_ctx
->event_head
= event
;
230 if (!event
->sender
->debug_event
) wake_up( &debug_ctx
->obj
, 0 );
233 /* find the next event that we can send to the debugger */
234 static struct debug_event
*find_event_to_send( struct debug_ctx
*debug_ctx
)
236 struct debug_event
*event
;
237 for (event
= debug_ctx
->event_head
; event
; event
= event
->next
)
239 if (event
->state
== EVENT_SENT
) continue; /* already sent */
240 if (event
->sender
->debug_event
) continue; /* thread busy with another one */
246 /* build a reply for the wait_debug_event request */
247 static void build_wait_debug_reply( struct thread
*thread
, struct object
*obj
, int signaled
)
249 struct wait_debug_event_request
*req
= get_req_ptr( thread
);
253 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
254 struct debug_event
*event
= find_event_to_send( debug_ctx
);
255 size_t size
= get_req_data_size(req
);
257 /* the object that woke us has to be our debug context */
258 assert( obj
->ops
== &debug_ctx_ops
);
261 event
->state
= EVENT_SENT
;
262 event
->sender
->debug_event
= event
;
263 req
->pid
= event
->sender
->process
;
264 req
->tid
= event
->sender
;
265 if (size
> sizeof(debug_event_t
)) size
= sizeof(debug_event_t
);
266 memcpy( get_req_data(req
), &event
->data
, size
);
267 set_req_data_size( req
, size
);
269 else /* timeout or error */
271 set_req_data_size( req
, 0 );
277 /* build a reply for the send_event request */
278 static void build_exception_event_reply( struct thread
*thread
, struct object
*obj
, int signaled
)
280 struct exception_event_request
*req
= get_req_ptr( thread
);
281 struct debug_event
*event
= (struct debug_event
*)obj
;
282 assert( obj
->ops
== &debug_event_ops
);
283 req
->status
= event
->status
;
284 thread
->context
= NULL
;
287 static void debug_event_dump( struct object
*obj
, int verbose
)
289 struct debug_event
*debug_event
= (struct debug_event
*)obj
;
290 assert( obj
->ops
== &debug_event_ops
);
291 fprintf( stderr
, "Debug event sender=%p code=%d state=%d\n",
292 debug_event
->sender
, debug_event
->data
.code
, debug_event
->state
);
295 static int debug_event_signaled( struct object
*obj
, struct thread
*thread
)
297 struct debug_event
*debug_event
= (struct debug_event
*)obj
;
298 assert( obj
->ops
== &debug_event_ops
);
299 return debug_event
->state
== EVENT_CONTINUED
;
302 static void debug_event_destroy( struct object
*obj
)
304 struct debug_event
*event
= (struct debug_event
*)obj
;
305 assert( obj
->ops
== &debug_event_ops
);
307 /* cannot still be in the queue */
308 assert( !event
->next
);
309 assert( !event
->prev
);
311 /* If the event has been sent already, the handles are now under the */
312 /* responsibility of the debugger process, so we don't touch them */
313 if (event
->state
== EVENT_QUEUED
)
315 struct process
*debugger
= event
->debugger
->process
;
316 switch(event
->data
.code
)
318 case CREATE_THREAD_DEBUG_EVENT
:
319 close_handle( debugger
, event
->data
.info
.create_thread
.handle
, NULL
);
321 case CREATE_PROCESS_DEBUG_EVENT
:
322 if (event
->data
.info
.create_process
.file
!= -1)
323 close_handle( debugger
, event
->data
.info
.create_process
.file
, NULL
);
324 close_handle( debugger
, event
->data
.info
.create_process
.thread
, NULL
);
325 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
327 case LOAD_DLL_DEBUG_EVENT
:
328 if (event
->data
.info
.load_dll
.handle
!= -1)
329 close_handle( debugger
, event
->data
.info
.load_dll
.handle
, NULL
);
333 release_object( event
->sender
);
334 release_object( event
->debugger
);
337 static void debug_ctx_dump( struct object
*obj
, int verbose
)
339 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
340 assert( obj
->ops
== &debug_ctx_ops
);
341 fprintf( stderr
, "Debug context head=%p tail=%p\n",
342 debug_ctx
->event_head
, debug_ctx
->event_tail
);
345 static int debug_ctx_signaled( struct object
*obj
, struct thread
*thread
)
347 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
348 assert( obj
->ops
== &debug_ctx_ops
);
349 return find_event_to_send( debug_ctx
) != NULL
;
352 static void debug_ctx_destroy( struct object
*obj
)
354 struct debug_event
*event
;
355 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
356 assert( obj
->ops
== &debug_ctx_ops
);
358 /* free all pending events */
359 while ((event
= debug_ctx
->event_head
) != NULL
) unlink_event( debug_ctx
, event
);
362 /* wait for a debug event (or send a reply at once if one is pending) */
363 static int wait_for_debug_event( int timeout
)
365 struct debug_ctx
*debug_ctx
= current
->debug_ctx
;
366 struct object
*obj
= &debug_ctx
->obj
;
369 if (!debug_ctx
) /* current thread is not a debugger */
371 set_error( STATUS_INVALID_HANDLE
);
374 if (timeout
!= -1) flags
= SELECT_TIMEOUT
;
375 return sleep_on( 1, &obj
, flags
, timeout
, build_wait_debug_reply
);
378 /* continue a debug event */
379 static int continue_debug_event( struct process
*process
, struct thread
*thread
, int status
)
381 struct debug_event
*event
;
382 struct debug_ctx
*debug_ctx
= current
->debug_ctx
;
384 if (!debug_ctx
|| process
->debugger
!= current
|| thread
->process
!= process
) goto error
;
386 /* find the event in the queue */
387 for (event
= debug_ctx
->event_head
; event
; event
= event
->next
)
389 if (event
->state
!= EVENT_SENT
) continue;
390 if (event
->sender
== thread
) break;
392 if (!event
) goto error
;
394 assert( event
->sender
->debug_event
== event
);
396 event
->status
= status
;
397 event
->state
= EVENT_CONTINUED
;
398 wake_up( &event
->obj
, 0 );
400 unlink_event( debug_ctx
, event
);
401 resume_process( process
);
404 /* not debugging this process, or no such event */
405 set_error( STATUS_ACCESS_DENIED
); /* FIXME */
409 /* queue a debug event for a debugger */
410 static struct debug_event
*queue_debug_event( struct thread
*thread
, int code
, void *arg
)
412 struct thread
*debugger
= thread
->process
->debugger
;
413 struct debug_ctx
*debug_ctx
= debugger
->debug_ctx
;
414 struct debug_event
*event
;
416 assert( code
> 0 && code
<= NB_DEBUG_EVENTS
);
418 /* cannot queue a debug event for myself */
419 assert( debugger
->process
!= thread
->process
);
421 /* build the event */
422 if (!(event
= alloc_object( &debug_event_ops
, -1 ))) return NULL
;
425 event
->state
= EVENT_QUEUED
;
426 event
->sender
= (struct thread
*)grab_object( thread
);
427 event
->debugger
= (struct thread
*)grab_object( debugger
);
428 event
->data
.code
= code
;
430 if (!fill_debug_event
[code
-1]( event
, arg
))
432 event
->data
.code
= -1; /* make sure we don't attempt to close handles */
433 release_object( event
);
437 link_event( debug_ctx
, event
);
438 suspend_process( thread
->process
);
442 /* generate a debug event from inside the server and queue it */
443 void generate_debug_event( struct thread
*thread
, int code
, void *arg
)
445 if (thread
->process
->debugger
)
447 struct debug_event
*event
= queue_debug_event( thread
, code
, arg
);
448 if (event
) release_object( event
);
452 /* attach a process to a debugger thread and suspend it */
453 static int debugger_attach( struct process
*process
, struct thread
*debugger
)
455 struct thread
*thread
;
457 if (process
->debugger
) goto error
; /* already being debugged */
458 if (process
->init_event
) goto error
; /* still starting up */
460 /* make sure we don't create a debugging loop */
461 for (thread
= debugger
; thread
; thread
= thread
->process
->debugger
)
462 if (thread
->process
== process
) goto error
;
464 suspend_process( process
);
466 /* we must have been able to attach all threads */
467 for (thread
= process
->thread_list
; thread
; thread
= thread
->proc_next
)
468 if (!thread
->attached
)
470 fprintf( stderr
, "%p not attached\n", thread
);
471 resume_process( process
);
475 if (set_process_debugger( process
, debugger
)) return 1;
476 resume_process( process
);
480 set_error( STATUS_ACCESS_DENIED
);
484 /* generate all startup events of a given process */
485 void generate_startup_debug_events( struct process
*process
, void *entry
)
487 struct process_dll
*dll
;
488 struct thread
*thread
= process
->thread_list
;
490 /* generate creation events */
491 generate_debug_event( thread
, CREATE_PROCESS_DEBUG_EVENT
, entry
);
492 while ((thread
= thread
->proc_next
))
493 generate_debug_event( thread
, CREATE_THREAD_DEBUG_EVENT
, NULL
);
495 /* generate dll events (in loading order, i.e. reverse list order) */
496 for (dll
= &process
->exe
; dll
->next
; dll
= dll
->next
);
497 while (dll
!= &process
->exe
)
499 generate_debug_event( process
->thread_list
, LOAD_DLL_DEBUG_EVENT
, dll
);
504 /* set the debugger of a given process */
505 int set_process_debugger( struct process
*process
, struct thread
*debugger
)
507 struct debug_ctx
*debug_ctx
;
509 assert( !process
->debugger
);
511 if (!debugger
->debug_ctx
) /* need to allocate a context */
513 if (!(debug_ctx
= alloc_object( &debug_ctx_ops
, -1 ))) return 0;
514 debug_ctx
->event_head
= NULL
;
515 debug_ctx
->event_tail
= NULL
;
516 debugger
->debug_ctx
= debug_ctx
;
518 process
->debugger
= debugger
;
522 /* a thread is exiting */
523 void debug_exit_thread( struct thread
*thread
)
525 if (thread
->debug_ctx
) /* this thread is a debugger */
527 /* kill all debugged processes */
528 kill_debugged_processes( thread
, thread
->exit_code
);
529 release_object( thread
->debug_ctx
);
530 thread
->debug_ctx
= NULL
;
534 /* Wait for a debug event */
535 DECL_HANDLER(wait_debug_event
)
537 if (!wait_for_debug_event( req
->timeout
))
541 set_req_data_size( req
, 0 );
545 /* Continue a debug event */
546 DECL_HANDLER(continue_debug_event
)
548 struct process
*process
= get_process_from_id( req
->pid
);
551 struct thread
*thread
= get_thread_from_id( req
->tid
);
554 continue_debug_event( process
, thread
, req
->status
);
555 release_object( thread
);
557 release_object( process
);
561 /* Start debugging an existing process */
562 DECL_HANDLER(debug_process
)
564 struct debug_event_exception data
;
565 struct debug_event
*event
;
566 struct process
*process
= get_process_from_id( req
->pid
);
567 if (!process
) return;
569 if (debugger_attach( process
, current
))
571 generate_startup_debug_events( process
, NULL
);
572 resume_process( process
);
574 data
.record
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
575 data
.record
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
576 data
.record
.ExceptionRecord
= NULL
;
577 data
.record
.ExceptionAddress
= get_thread_ip( process
->thread_list
);
578 data
.record
.NumberParameters
= 0;
580 if ((event
= queue_debug_event( process
->thread_list
, EXCEPTION_DEBUG_EVENT
, &data
)))
581 release_object( event
);
583 release_object( process
);
586 /* send an exception event */
587 DECL_HANDLER(exception_event
)
590 if (current
->process
->debugger
)
592 struct debug_event_exception data
;
593 struct debug_event
*event
;
594 CONTEXT
*context
= get_req_data( req
);
595 EXCEPTION_RECORD
*rec
= (EXCEPTION_RECORD
*)(context
+ 1);
597 if (get_req_data_size( req
) < sizeof(*rec
) + sizeof(*context
))
599 set_error( STATUS_INVALID_PARAMETER
);
603 data
.first
= req
->first
;
604 if ((event
= queue_debug_event( current
, EXCEPTION_DEBUG_EVENT
, &data
)))
606 struct object
*obj
= &event
->obj
;
607 current
->context
= context
;
608 sleep_on( 1, &obj
, 0, -1, build_exception_event_reply
);
609 release_object( event
);
614 /* send an output string to the debugger */
615 DECL_HANDLER(output_debug_string
)
617 if (current
->process
->debugger
)
619 struct debug_event
*event
= queue_debug_event( current
, OUTPUT_DEBUG_STRING_EVENT
, req
);
620 if (event
) release_object( event
);