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
))) return 0;
99 event
->data
.info
.create_thread
.handle
= handle
;
100 event
->data
.info
.create_thread
.teb
= thread
->teb
;
101 event
->data
.info
.create_thread
.start
= arg
;
105 static int fill_create_process_event( struct debug_event
*event
, void *arg
)
107 struct process
*debugger
= event
->debugger
->process
;
108 struct thread
*thread
= event
->sender
;
109 struct process
*process
= thread
->process
;
112 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
113 if (!(handle
= alloc_handle( debugger
, process
, PROCESS_ALL_ACCESS
, FALSE
))) return 0;
114 event
->data
.info
.create_process
.process
= handle
;
116 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
117 if (!(handle
= alloc_handle( debugger
, thread
, THREAD_ALL_ACCESS
, FALSE
)))
119 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
122 event
->data
.info
.create_process
.thread
= handle
;
125 if (process
->exe
.file
&&
126 /* the doc says write access too, but this doesn't seem a good idea */
127 !(handle
= alloc_handle( debugger
, process
->exe
.file
, GENERIC_READ
, FALSE
)))
129 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
130 close_handle( debugger
, event
->data
.info
.create_process
.thread
, NULL
);
133 event
->data
.info
.create_process
.file
= handle
;
134 event
->data
.info
.create_process
.teb
= thread
->teb
;
135 event
->data
.info
.create_process
.base
= process
->exe
.base
;
136 event
->data
.info
.create_process
.start
= arg
;
137 event
->data
.info
.create_process
.dbg_offset
= process
->exe
.dbg_offset
;
138 event
->data
.info
.create_process
.dbg_size
= process
->exe
.dbg_size
;
139 event
->data
.info
.create_process
.name
= process
->exe
.name
;
140 event
->data
.info
.create_process
.unicode
= 0;
144 static int fill_exit_thread_event( struct debug_event
*event
, void *arg
)
146 struct thread
*thread
= arg
;
147 event
->data
.info
.exit
.exit_code
= thread
->exit_code
;
151 static int fill_exit_process_event( struct debug_event
*event
, void *arg
)
153 struct process
*process
= arg
;
154 event
->data
.info
.exit
.exit_code
= process
->exit_code
;
158 static int fill_load_dll_event( struct debug_event
*event
, void *arg
)
160 struct process
*debugger
= event
->debugger
->process
;
161 struct process_dll
*dll
= arg
;
164 if (dll
->file
&& !(handle
= alloc_handle( debugger
, dll
->file
, GENERIC_READ
, FALSE
)))
166 event
->data
.info
.load_dll
.handle
= handle
;
167 event
->data
.info
.load_dll
.base
= dll
->base
;
168 event
->data
.info
.load_dll
.dbg_offset
= dll
->dbg_offset
;
169 event
->data
.info
.load_dll
.dbg_size
= dll
->dbg_size
;
170 event
->data
.info
.load_dll
.name
= dll
->name
;
171 event
->data
.info
.load_dll
.unicode
= 0;
175 static int fill_unload_dll_event( struct debug_event
*event
, void *arg
)
177 event
->data
.info
.unload_dll
.base
= arg
;
181 static int fill_output_debug_string_event( struct debug_event
*event
, void *arg
)
183 struct output_debug_string_request
*req
= arg
;
184 event
->data
.info
.output_string
.string
= req
->string
;
185 event
->data
.info
.output_string
.unicode
= req
->unicode
;
186 event
->data
.info
.output_string
.length
= req
->length
;
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_ctx
*debug_ctx
, struct debug_event
*event
)
222 grab_object( event
);
224 event
->prev
= debug_ctx
->event_tail
;
225 debug_ctx
->event_tail
= event
;
226 if (event
->prev
) event
->prev
->next
= event
;
227 else debug_ctx
->event_head
= event
;
228 if (!event
->sender
->debug_event
) wake_up( &debug_ctx
->obj
, 0 );
231 /* find the next event that we can send to the debugger */
232 static struct debug_event
*find_event_to_send( struct debug_ctx
*debug_ctx
)
234 struct debug_event
*event
;
235 for (event
= debug_ctx
->event_head
; event
; event
= event
->next
)
237 if (event
->state
== EVENT_SENT
) continue; /* already sent */
238 if (event
->sender
->debug_event
) continue; /* thread busy with another one */
244 /* build a reply for the send_event request */
245 static void build_exception_event_reply( struct thread
*thread
, struct object
*obj
, int signaled
)
247 struct exception_event_request
*req
= get_req_ptr( thread
);
248 struct debug_event
*event
= (struct debug_event
*)obj
;
249 assert( obj
->ops
== &debug_event_ops
);
250 req
->status
= event
->status
;
251 thread
->context
= 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 /* cannot still be in the queue */
275 assert( !event
->next
);
276 assert( !event
->prev
);
278 /* If the event has been sent already, the handles are now under the */
279 /* responsibility of the debugger process, so we don't touch them */
280 if (event
->state
== EVENT_QUEUED
)
282 struct process
*debugger
= event
->debugger
->process
;
283 switch(event
->data
.code
)
285 case CREATE_THREAD_DEBUG_EVENT
:
286 close_handle( debugger
, event
->data
.info
.create_thread
.handle
, NULL
);
288 case CREATE_PROCESS_DEBUG_EVENT
:
289 if (event
->data
.info
.create_process
.file
)
290 close_handle( debugger
, event
->data
.info
.create_process
.file
, NULL
);
291 close_handle( debugger
, event
->data
.info
.create_process
.thread
, NULL
);
292 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
294 case LOAD_DLL_DEBUG_EVENT
:
295 if (event
->data
.info
.load_dll
.handle
)
296 close_handle( debugger
, event
->data
.info
.load_dll
.handle
, NULL
);
300 release_object( event
->sender
);
301 release_object( event
->debugger
);
304 static void debug_ctx_dump( struct object
*obj
, int verbose
)
306 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
307 assert( obj
->ops
== &debug_ctx_ops
);
308 fprintf( stderr
, "Debug context head=%p tail=%p\n",
309 debug_ctx
->event_head
, debug_ctx
->event_tail
);
312 static int debug_ctx_signaled( struct object
*obj
, struct thread
*thread
)
314 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
315 assert( obj
->ops
== &debug_ctx_ops
);
316 return find_event_to_send( debug_ctx
) != NULL
;
319 static void debug_ctx_destroy( struct object
*obj
)
321 struct debug_event
*event
;
322 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
323 assert( obj
->ops
== &debug_ctx_ops
);
325 /* free all pending events */
326 while ((event
= debug_ctx
->event_head
) != NULL
) unlink_event( debug_ctx
, event
);
329 /* continue a debug event */
330 static int continue_debug_event( struct process
*process
, struct thread
*thread
, int status
)
332 struct debug_event
*event
;
333 struct debug_ctx
*debug_ctx
= current
->debug_ctx
;
335 if (!debug_ctx
|| process
->debugger
!= current
|| thread
->process
!= process
) goto error
;
337 /* find the event in the queue */
338 for (event
= debug_ctx
->event_head
; event
; event
= event
->next
)
340 if (event
->state
!= EVENT_SENT
) continue;
341 if (event
->sender
== thread
) break;
343 if (!event
) goto error
;
345 assert( event
->sender
->debug_event
== event
);
347 event
->status
= status
;
348 event
->state
= EVENT_CONTINUED
;
349 wake_up( &event
->obj
, 0 );
351 unlink_event( debug_ctx
, event
);
352 resume_process( process
);
355 /* not debugging this process, or no such event */
356 set_error( STATUS_ACCESS_DENIED
); /* FIXME */
360 /* queue a debug event for a debugger */
361 static struct debug_event
*queue_debug_event( struct thread
*thread
, int code
, void *arg
)
363 struct thread
*debugger
= thread
->process
->debugger
;
364 struct debug_ctx
*debug_ctx
= debugger
->debug_ctx
;
365 struct debug_event
*event
;
367 assert( code
> 0 && code
<= NB_DEBUG_EVENTS
);
369 /* cannot queue a debug event for myself */
370 assert( debugger
->process
!= thread
->process
);
372 /* build the event */
373 if (!(event
= alloc_object( &debug_event_ops
, -1 ))) return NULL
;
376 event
->state
= EVENT_QUEUED
;
377 event
->sender
= (struct thread
*)grab_object( thread
);
378 event
->debugger
= (struct thread
*)grab_object( debugger
);
379 event
->data
.code
= code
;
381 if (!fill_debug_event
[code
-1]( event
, arg
))
383 event
->data
.code
= -1; /* make sure we don't attempt to close handles */
384 release_object( event
);
388 link_event( debug_ctx
, event
);
389 suspend_process( thread
->process
);
393 /* generate a debug event from inside the server and queue it */
394 void generate_debug_event( struct thread
*thread
, int code
, void *arg
)
396 if (thread
->process
->debugger
)
398 struct debug_event
*event
= queue_debug_event( thread
, code
, arg
);
399 if (event
) 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 suspend_process( process
);
417 /* we must have been able to attach all threads */
418 for (thread
= process
->thread_list
; thread
; thread
= thread
->proc_next
)
419 if (!thread
->attached
)
421 fprintf( stderr
, "%p not attached\n", thread
);
422 resume_process( process
);
426 if (set_process_debugger( process
, debugger
)) return 1;
427 resume_process( process
);
431 set_error( STATUS_ACCESS_DENIED
);
435 /* generate all startup events of a given process */
436 void generate_startup_debug_events( struct process
*process
, void *entry
)
438 struct process_dll
*dll
;
439 struct thread
*thread
= process
->thread_list
;
441 /* generate creation events */
442 generate_debug_event( thread
, CREATE_PROCESS_DEBUG_EVENT
, entry
);
443 while ((thread
= thread
->proc_next
))
444 generate_debug_event( thread
, CREATE_THREAD_DEBUG_EVENT
, NULL
);
446 /* generate dll events (in loading order, i.e. reverse list order) */
447 for (dll
= &process
->exe
; dll
->next
; dll
= dll
->next
);
448 while (dll
!= &process
->exe
)
450 generate_debug_event( process
->thread_list
, LOAD_DLL_DEBUG_EVENT
, dll
);
455 /* set the debugger of a given process */
456 int set_process_debugger( struct process
*process
, struct thread
*debugger
)
458 struct debug_ctx
*debug_ctx
;
460 assert( !process
->debugger
);
462 if (!debugger
->debug_ctx
) /* need to allocate a context */
464 if (!(debug_ctx
= alloc_object( &debug_ctx_ops
, -1 ))) return 0;
465 debug_ctx
->event_head
= NULL
;
466 debug_ctx
->event_tail
= NULL
;
467 debugger
->debug_ctx
= debug_ctx
;
469 process
->debugger
= debugger
;
473 /* a thread is exiting */
474 void debug_exit_thread( struct thread
*thread
)
476 if (thread
->debug_ctx
) /* this thread is a debugger */
478 /* kill all debugged processes */
479 kill_debugged_processes( thread
, thread
->exit_code
);
480 release_object( thread
->debug_ctx
);
481 thread
->debug_ctx
= NULL
;
485 /* Wait for a debug event */
486 DECL_HANDLER(wait_debug_event
)
488 struct debug_ctx
*debug_ctx
= current
->debug_ctx
;
489 struct debug_event
*event
;
491 if (!debug_ctx
) /* current thread is not a debugger */
493 set_error( STATUS_INVALID_HANDLE
);
497 if ((event
= find_event_to_send( debug_ctx
)))
499 size_t size
= get_req_data_size(req
);
500 event
->state
= EVENT_SENT
;
501 event
->sender
->debug_event
= event
;
502 req
->pid
= event
->sender
->process
;
503 req
->tid
= event
->sender
;
504 if (size
> sizeof(debug_event_t
)) size
= sizeof(debug_event_t
);
505 memcpy( get_req_data(req
), &event
->data
, size
);
506 set_req_data_size( req
, size
);
508 else /* no event ready */
512 set_req_data_size( req
, 0 );
514 req
->wait
= alloc_handle( current
->process
, debug_ctx
, SYNCHRONIZE
, FALSE
);
518 /* Continue a debug event */
519 DECL_HANDLER(continue_debug_event
)
521 struct process
*process
= get_process_from_id( req
->pid
);
524 struct thread
*thread
= get_thread_from_id( req
->tid
);
527 continue_debug_event( process
, thread
, req
->status
);
528 release_object( thread
);
530 release_object( process
);
534 /* Start debugging an existing process */
535 DECL_HANDLER(debug_process
)
537 struct debug_event_exception data
;
538 struct debug_event
*event
;
539 struct process
*process
= get_process_from_id( req
->pid
);
540 if (!process
) return;
542 if (debugger_attach( process
, current
))
544 generate_startup_debug_events( process
, NULL
);
545 resume_process( process
);
547 data
.record
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
548 data
.record
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
549 data
.record
.ExceptionRecord
= NULL
;
550 data
.record
.ExceptionAddress
= get_thread_ip( process
->thread_list
);
551 data
.record
.NumberParameters
= 0;
553 if ((event
= queue_debug_event( process
->thread_list
, EXCEPTION_DEBUG_EVENT
, &data
)))
554 release_object( event
);
556 release_object( process
);
559 /* send an exception event */
560 DECL_HANDLER(exception_event
)
563 if (current
->process
->debugger
)
565 struct debug_event_exception data
;
566 struct debug_event
*event
;
567 CONTEXT
*context
= get_req_data( req
);
568 EXCEPTION_RECORD
*rec
= (EXCEPTION_RECORD
*)(context
+ 1);
570 if (get_req_data_size( req
) < sizeof(*rec
) + sizeof(*context
))
572 set_error( STATUS_INVALID_PARAMETER
);
576 data
.first
= req
->first
;
577 if ((event
= queue_debug_event( current
, EXCEPTION_DEBUG_EVENT
, &data
)))
579 struct object
*obj
= &event
->obj
;
580 current
->context
= context
;
581 sleep_on( 1, &obj
, 0, 0, 0, build_exception_event_reply
);
582 release_object( event
);
587 /* send an output string to the debugger */
588 DECL_HANDLER(output_debug_string
)
590 if (current
->process
->debugger
)
592 struct debug_event
*event
= queue_debug_event( current
, OUTPUT_DEBUG_STRING_EVENT
, req
);
593 if (event
) release_object( event
);