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"
31 #define WIN32_NO_STATUS
41 enum debug_event_state
{ EVENT_QUEUED
, EVENT_SENT
, EVENT_CONTINUED
};
46 struct object obj
; /* object header */
47 struct list entry
; /* entry in event queue */
48 struct thread
*sender
; /* thread which sent this event */
49 struct thread
*debugger
; /* debugger thread receiving the event */
50 enum debug_event_state state
; /* event state */
51 int status
; /* continuation status */
52 debug_event_t data
; /* event data */
53 CONTEXT context
; /* register context */
59 struct object obj
; /* object header */
60 struct list event_queue
; /* pending events queue */
61 int kill_on_exit
;/* kill debuggees on debugger exit ? */
65 static void debug_event_dump( struct object
*obj
, int verbose
);
66 static int debug_event_signaled( struct object
*obj
, struct thread
*thread
);
67 static void debug_event_destroy( struct object
*obj
);
69 static const struct object_ops debug_event_ops
=
71 sizeof(struct debug_event
), /* size */
72 debug_event_dump
, /* dump */
73 add_queue
, /* add_queue */
74 remove_queue
, /* remove_queue */
75 debug_event_signaled
, /* signaled */
76 no_satisfied
, /* satisfied */
77 no_signal
, /* signal */
78 no_get_fd
, /* get_fd */
79 no_map_access
, /* map_access */
80 no_lookup_name
, /* lookup_name */
81 no_close_handle
, /* close_handle */
82 debug_event_destroy
/* destroy */
85 static void debug_ctx_dump( struct object
*obj
, int verbose
);
86 static int debug_ctx_signaled( struct object
*obj
, struct thread
*thread
);
87 static void debug_ctx_destroy( struct object
*obj
);
89 static const struct object_ops debug_ctx_ops
=
91 sizeof(struct debug_ctx
), /* size */
92 debug_ctx_dump
, /* dump */
93 add_queue
, /* add_queue */
94 remove_queue
, /* remove_queue */
95 debug_ctx_signaled
, /* signaled */
96 no_satisfied
, /* satisfied */
97 no_signal
, /* signal */
98 no_get_fd
, /* get_fd */
99 no_map_access
, /* map_access */
100 no_lookup_name
, /* lookup_name */
101 no_close_handle
, /* close_handle */
102 debug_ctx_destroy
/* destroy */
106 /* routines to build an event according to its type */
108 static int fill_exception_event( struct debug_event
*event
, void *arg
)
110 memcpy( &event
->data
.info
.exception
, arg
, sizeof(event
->data
.info
.exception
) );
114 static int fill_create_thread_event( struct debug_event
*event
, void *arg
)
116 struct process
*debugger
= event
->debugger
->process
;
117 struct thread
*thread
= event
->sender
;
120 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
121 if (!(handle
= alloc_handle( debugger
, thread
, THREAD_ALL_ACCESS
, 0 ))) return 0;
122 event
->data
.info
.create_thread
.handle
= handle
;
123 event
->data
.info
.create_thread
.teb
= thread
->teb
;
124 event
->data
.info
.create_thread
.start
= arg
;
128 static int fill_create_process_event( struct debug_event
*event
, void *arg
)
130 struct process
*debugger
= event
->debugger
->process
;
131 struct thread
*thread
= event
->sender
;
132 struct process
*process
= thread
->process
;
135 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
136 if (!(handle
= alloc_handle( debugger
, process
, PROCESS_ALL_ACCESS
, 0 ))) return 0;
137 event
->data
.info
.create_process
.process
= handle
;
139 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
140 if (!(handle
= alloc_handle( debugger
, thread
, THREAD_ALL_ACCESS
, 0 )))
142 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
145 event
->data
.info
.create_process
.thread
= handle
;
148 if (process
->exe
.file
&&
149 /* the doc says write access too, but this doesn't seem a good idea */
150 !(handle
= alloc_handle( debugger
, process
->exe
.file
, GENERIC_READ
, 0 )))
152 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
153 close_handle( debugger
, event
->data
.info
.create_process
.thread
, NULL
);
156 event
->data
.info
.create_process
.file
= handle
;
157 event
->data
.info
.create_process
.teb
= thread
->teb
;
158 event
->data
.info
.create_process
.base
= process
->exe
.base
;
159 event
->data
.info
.create_process
.start
= arg
;
160 event
->data
.info
.create_process
.dbg_offset
= process
->exe
.dbg_offset
;
161 event
->data
.info
.create_process
.dbg_size
= process
->exe
.dbg_size
;
162 event
->data
.info
.create_process
.name
= process
->exe
.name
;
163 event
->data
.info
.create_process
.unicode
= 1;
167 static int fill_exit_thread_event( struct debug_event
*event
, void *arg
)
169 struct thread
*thread
= arg
;
170 event
->data
.info
.exit
.exit_code
= thread
->exit_code
;
174 static int fill_exit_process_event( struct debug_event
*event
, void *arg
)
176 struct process
*process
= arg
;
177 event
->data
.info
.exit
.exit_code
= process
->exit_code
;
181 static int fill_load_dll_event( struct debug_event
*event
, void *arg
)
183 struct process
*debugger
= event
->debugger
->process
;
184 struct process_dll
*dll
= arg
;
185 obj_handle_t handle
= 0;
187 if (dll
->file
&& !(handle
= alloc_handle( debugger
, dll
->file
, GENERIC_READ
, 0 )))
189 event
->data
.info
.load_dll
.handle
= handle
;
190 event
->data
.info
.load_dll
.base
= dll
->base
;
191 event
->data
.info
.load_dll
.dbg_offset
= dll
->dbg_offset
;
192 event
->data
.info
.load_dll
.dbg_size
= dll
->dbg_size
;
193 event
->data
.info
.load_dll
.name
= dll
->name
;
194 event
->data
.info
.load_dll
.unicode
= 1;
198 static int fill_unload_dll_event( struct debug_event
*event
, void *arg
)
200 event
->data
.info
.unload_dll
.base
= arg
;
204 static int fill_output_debug_string_event( struct debug_event
*event
, void *arg
)
206 struct debug_event_output_string
*data
= arg
;
207 event
->data
.info
.output_string
= *data
;
211 typedef int (*fill_event_func
)( struct debug_event
*event
, void *arg
);
213 #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT /* RIP_EVENT not supported */
215 static const fill_event_func fill_debug_event
[NB_DEBUG_EVENTS
] =
217 fill_exception_event
, /* EXCEPTION_DEBUG_EVENT */
218 fill_create_thread_event
, /* CREATE_THREAD_DEBUG_EVENT */
219 fill_create_process_event
, /* CREATE_PROCESS_DEBUG_EVENT */
220 fill_exit_thread_event
, /* EXIT_THREAD_DEBUG_EVENT */
221 fill_exit_process_event
, /* EXIT_PROCESS_DEBUG_EVENT */
222 fill_load_dll_event
, /* LOAD_DLL_DEBUG_EVENT */
223 fill_unload_dll_event
, /* UNLOAD_DLL_DEBUG_EVENT */
224 fill_output_debug_string_event
/* OUTPUT_DEBUG_STRING_EVENT */
228 /* unlink the first event from the queue */
229 static void unlink_event( struct debug_ctx
*debug_ctx
, struct debug_event
*event
)
231 list_remove( &event
->entry
);
232 if (event
->sender
->debug_event
== event
) event
->sender
->debug_event
= NULL
;
233 release_object( event
);
236 /* link an event at the end of the queue */
237 static void link_event( struct debug_event
*event
)
239 struct debug_ctx
*debug_ctx
= event
->debugger
->debug_ctx
;
242 grab_object( event
);
243 list_add_tail( &debug_ctx
->event_queue
, &event
->entry
);
244 if (!event
->sender
->debug_event
) wake_up( &debug_ctx
->obj
, 0 );
247 /* find the next event that we can send to the debugger */
248 static struct debug_event
*find_event_to_send( struct debug_ctx
*debug_ctx
)
250 struct debug_event
*event
;
252 LIST_FOR_EACH_ENTRY( event
, &debug_ctx
->event_queue
, struct debug_event
, entry
)
254 if (event
->state
== EVENT_SENT
) continue; /* already sent */
255 if (event
->sender
->debug_event
) continue; /* thread busy with another one */
261 static void debug_event_dump( struct object
*obj
, int verbose
)
263 struct debug_event
*debug_event
= (struct debug_event
*)obj
;
264 assert( obj
->ops
== &debug_event_ops
);
265 fprintf( stderr
, "Debug event sender=%p code=%d state=%d\n",
266 debug_event
->sender
, debug_event
->data
.code
, debug_event
->state
);
269 static int debug_event_signaled( struct object
*obj
, struct thread
*thread
)
271 struct debug_event
*debug_event
= (struct debug_event
*)obj
;
272 assert( obj
->ops
== &debug_event_ops
);
273 return debug_event
->state
== EVENT_CONTINUED
;
276 static void debug_event_destroy( struct object
*obj
)
278 struct debug_event
*event
= (struct debug_event
*)obj
;
279 assert( obj
->ops
== &debug_event_ops
);
281 /* If the event has been sent already, the handles are now under the */
282 /* responsibility of the debugger process, so we don't touch them */
283 if (event
->state
== EVENT_QUEUED
)
285 struct process
*debugger
= event
->debugger
->process
;
286 switch(event
->data
.code
)
288 case CREATE_THREAD_DEBUG_EVENT
:
289 close_handle( debugger
, event
->data
.info
.create_thread
.handle
, NULL
);
291 case CREATE_PROCESS_DEBUG_EVENT
:
292 if (event
->data
.info
.create_process
.file
)
293 close_handle( debugger
, event
->data
.info
.create_process
.file
, NULL
);
294 close_handle( debugger
, event
->data
.info
.create_process
.thread
, NULL
);
295 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
297 case LOAD_DLL_DEBUG_EVENT
:
298 if (event
->data
.info
.load_dll
.handle
)
299 close_handle( debugger
, event
->data
.info
.load_dll
.handle
, NULL
);
303 if (event
->sender
->context
== &event
->context
) event
->sender
->context
= NULL
;
304 release_object( event
->sender
);
305 release_object( event
->debugger
);
308 static void debug_ctx_dump( struct object
*obj
, int verbose
)
310 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
311 assert( obj
->ops
== &debug_ctx_ops
);
312 fprintf( stderr
, "Debug context head=%p tail=%p\n",
313 debug_ctx
->event_queue
.next
, debug_ctx
->event_queue
.prev
);
316 static int debug_ctx_signaled( struct object
*obj
, struct thread
*thread
)
318 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
319 assert( obj
->ops
== &debug_ctx_ops
);
320 return find_event_to_send( debug_ctx
) != NULL
;
323 static void debug_ctx_destroy( struct object
*obj
)
326 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
327 assert( obj
->ops
== &debug_ctx_ops
);
329 /* free all pending events */
330 while ((ptr
= list_head( &debug_ctx
->event_queue
)))
331 unlink_event( debug_ctx
, LIST_ENTRY( ptr
, struct debug_event
, entry
));
334 /* continue a debug event */
335 static int continue_debug_event( struct process
*process
, struct thread
*thread
, int status
)
337 struct debug_ctx
*debug_ctx
= current
->debug_ctx
;
339 if (debug_ctx
&& process
->debugger
== current
&& thread
->process
== process
)
341 struct debug_event
*event
;
343 /* find the event in the queue */
344 LIST_FOR_EACH_ENTRY( event
, &debug_ctx
->event_queue
, struct debug_event
, entry
)
346 if (event
->state
!= EVENT_SENT
) continue;
347 if (event
->sender
== thread
)
349 assert( event
->sender
->debug_event
== event
);
351 event
->status
= status
;
352 event
->state
= EVENT_CONTINUED
;
353 wake_up( &event
->obj
, 0 );
354 unlink_event( debug_ctx
, event
);
355 resume_process( process
);
360 /* not debugging this process, or no such event */
361 set_error( STATUS_ACCESS_DENIED
); /* FIXME */
365 /* alloc a debug event for a debugger */
366 static struct debug_event
*alloc_debug_event( struct thread
*thread
, int code
,
367 void *arg
, const CONTEXT
*context
)
369 struct thread
*debugger
= thread
->process
->debugger
;
370 struct debug_event
*event
;
372 assert( code
> 0 && code
<= NB_DEBUG_EVENTS
);
373 /* cannot queue a debug event for myself */
374 assert( debugger
->process
!= thread
->process
);
376 /* build the event */
377 if (!(event
= alloc_object( &debug_event_ops
))) return NULL
;
378 event
->state
= EVENT_QUEUED
;
379 event
->sender
= (struct thread
*)grab_object( thread
);
380 event
->debugger
= (struct thread
*)grab_object( debugger
);
381 event
->data
.code
= code
;
383 if (!fill_debug_event
[code
-1]( event
, arg
))
385 event
->data
.code
= -1; /* make sure we don't attempt to close handles */
386 release_object( event
);
391 memcpy( &event
->context
, context
, sizeof(event
->context
) );
392 thread
->context
= &event
->context
;
397 /* generate a debug event from inside the server and queue it */
398 void generate_debug_event( struct thread
*thread
, int code
, void *arg
)
400 if (thread
->process
->debugger
)
402 struct debug_event
*event
= alloc_debug_event( thread
, code
, arg
, NULL
);
406 suspend_process( thread
->process
);
407 release_object( event
);
412 /* attach a process to a debugger thread and suspend it */
413 static int debugger_attach( struct process
*process
, struct thread
*debugger
)
415 struct thread
*thread
;
417 if (process
->debugger
) goto error
; /* already being debugged */
418 if (!is_process_init_done( process
)) goto error
; /* still starting up */
419 if (list_empty( &process
->thread_list
)) goto error
; /* no thread running in the process */
421 /* make sure we don't create a debugging loop */
422 for (thread
= debugger
; thread
; thread
= thread
->process
->debugger
)
423 if (thread
->process
== process
) goto error
;
425 /* don't let a debugger debug its console... won't work */
426 if (debugger
->process
->console
&& debugger
->process
->console
->renderer
->process
== process
)
429 suspend_process( process
);
430 if (!attach_process( process
) || !set_process_debugger( process
, debugger
))
432 resume_process( process
);
435 if (!set_process_debug_flag( process
, 1 ))
437 process
->debugger
= NULL
;
438 resume_process( process
);
444 set_error( STATUS_ACCESS_DENIED
);
449 /* detach a process from a debugger thread (and resume it ?) */
450 int debugger_detach( struct process
*process
, struct thread
*debugger
)
452 struct debug_event
*event
;
453 struct debug_ctx
*debug_ctx
;
455 if (!process
->debugger
|| process
->debugger
!= debugger
)
456 goto error
; /* not currently debugged, or debugged by another debugger */
457 if (!debugger
->debug_ctx
) goto error
; /* should be a debugger */
458 /* init should be done, otherwise wouldn't be attached */
459 assert(is_process_init_done(process
));
461 suspend_process( process
);
462 /* send continue indication for all events */
463 debug_ctx
= debugger
->debug_ctx
;
465 /* find the event in the queue
466 * FIXME: could loop on process' threads and look the debug_event field */
467 LIST_FOR_EACH_ENTRY( event
, &debug_ctx
->event_queue
, struct debug_event
, entry
)
469 if (event
->state
!= EVENT_QUEUED
) continue;
471 if (event
->sender
->process
== process
)
473 assert( event
->sender
->debug_event
== event
);
474 event
->status
= DBG_CONTINUE
;
475 event
->state
= EVENT_CONTINUED
;
476 wake_up( &event
->obj
, 0 );
477 unlink_event( debug_ctx
, event
);
478 /* from queued debug event */
479 resume_process( process
);
484 /* remove relationships between process and its debugger */
485 process
->debugger
= NULL
;
486 if (!set_process_debug_flag( process
, 0 )) clear_error(); /* ignore error */
487 detach_process( process
);
489 /* from this function */
490 resume_process( process
);
494 set_error( STATUS_ACCESS_DENIED
);
498 /* generate all startup events of a given process */
499 void generate_startup_debug_events( struct process
*process
, void *entry
)
502 struct thread
*thread
, *first_thread
= get_process_first_thread( process
);
504 /* generate creation events */
505 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
507 if (thread
== first_thread
)
508 generate_debug_event( thread
, CREATE_PROCESS_DEBUG_EVENT
, entry
);
510 generate_debug_event( thread
, CREATE_THREAD_DEBUG_EVENT
, NULL
);
513 /* generate dll events (in loading order, i.e. reverse list order) */
514 ptr
= list_tail( &process
->dlls
);
517 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
518 generate_debug_event( first_thread
, LOAD_DLL_DEBUG_EVENT
, dll
);
519 ptr
= list_prev( &process
->dlls
, ptr
);
523 /* set the debugger of a given process */
524 int set_process_debugger( struct process
*process
, struct thread
*debugger
)
526 struct debug_ctx
*debug_ctx
;
528 assert( !process
->debugger
);
530 if (!debugger
->debug_ctx
) /* need to allocate a context */
532 if (!(debug_ctx
= alloc_object( &debug_ctx_ops
))) return 0;
533 debug_ctx
->kill_on_exit
= 1;
534 list_init( &debug_ctx
->event_queue
);
535 debugger
->debug_ctx
= debug_ctx
;
537 process
->debugger
= debugger
;
541 /* a thread is exiting */
542 void debug_exit_thread( struct thread
*thread
)
544 if (thread
->debug_ctx
) /* this thread is a debugger */
546 if (thread
->debug_ctx
->kill_on_exit
)
548 /* kill all debugged processes */
549 kill_debugged_processes( thread
, thread
->exit_code
);
553 detach_debugged_processes( thread
);
555 release_object( thread
->debug_ctx
);
556 thread
->debug_ctx
= NULL
;
560 /* Wait for a debug event */
561 DECL_HANDLER(wait_debug_event
)
563 struct debug_ctx
*debug_ctx
= current
->debug_ctx
;
564 struct debug_event
*event
;
566 if (!debug_ctx
) /* current thread is not a debugger */
568 set_error( STATUS_INVALID_HANDLE
);
572 if ((event
= find_event_to_send( debug_ctx
)))
574 size_t size
= get_reply_max_size();
575 event
->state
= EVENT_SENT
;
576 event
->sender
->debug_event
= event
;
577 reply
->pid
= get_process_id( event
->sender
->process
);
578 reply
->tid
= get_thread_id( event
->sender
);
579 if (size
> sizeof(debug_event_t
)) size
= sizeof(debug_event_t
);
580 set_reply_data( &event
->data
, size
);
582 else /* no event ready */
587 reply
->wait
= alloc_handle( current
->process
, debug_ctx
, SYNCHRONIZE
, 0 );
591 /* Continue a debug event */
592 DECL_HANDLER(continue_debug_event
)
594 struct process
*process
= get_process_from_id( req
->pid
);
597 struct thread
*thread
= get_thread_from_id( req
->tid
);
600 continue_debug_event( process
, thread
, req
->status
);
601 release_object( thread
);
603 release_object( process
);
607 /* Start debugging an existing process */
608 DECL_HANDLER(debug_process
)
610 struct process
*process
= get_process_from_id( req
->pid
);
611 if (!process
) return;
615 debugger_detach( process
, current
);
617 else if (debugger_attach( process
, current
))
619 struct debug_event_exception data
;
620 struct thread
*thread
= get_process_first_thread( process
);
622 generate_startup_debug_events( process
, NULL
);
623 resume_process( process
);
625 data
.record
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
626 data
.record
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
627 data
.record
.ExceptionRecord
= NULL
;
628 data
.record
.ExceptionAddress
= get_thread_ip( thread
);
629 data
.record
.NumberParameters
= 0;
631 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
633 release_object( process
);
636 /* queue an exception event */
637 DECL_HANDLER(queue_exception_event
)
640 if (current
->process
->debugger
)
642 struct debug_event_exception data
;
643 struct debug_event
*event
;
644 const CONTEXT
*context
= get_req_data();
645 const EXCEPTION_RECORD
*rec
= (const EXCEPTION_RECORD
*)(context
+ 1);
647 if (get_req_data_size() < sizeof(*rec
) + sizeof(*context
))
649 set_error( STATUS_INVALID_PARAMETER
);
653 data
.first
= req
->first
;
654 if ((event
= alloc_debug_event( current
, EXCEPTION_DEBUG_EVENT
, &data
, context
)))
656 if ((reply
->handle
= alloc_handle( current
->process
, event
, SYNCHRONIZE
, 0 )))
659 suspend_process( current
->process
);
661 release_object( event
);
666 /* retrieve the status of an exception event */
667 DECL_HANDLER(get_exception_status
)
669 struct debug_event
*event
;
671 if ((event
= (struct debug_event
*)get_handle_obj( current
->process
, req
->handle
,
672 0, &debug_event_ops
)))
674 close_handle( current
->process
, req
->handle
, NULL
);
675 if (event
->state
== EVENT_CONTINUED
)
677 if (current
->context
== &event
->context
)
679 size_t size
= min( sizeof(CONTEXT
), get_reply_max_size() );
680 set_reply_data( &event
->context
, size
);
681 current
->context
= NULL
;
683 set_error( event
->status
);
685 else set_error( STATUS_PENDING
);
686 release_object( event
);
690 /* send an output string to the debugger */
691 DECL_HANDLER(output_debug_string
)
693 struct debug_event_output_string data
;
695 data
.string
= req
->string
;
696 data
.unicode
= req
->unicode
;
697 data
.length
= req
->length
;
698 generate_debug_event( current
, OUTPUT_DEBUG_STRING_EVENT
, &data
);
701 /* simulate a breakpoint in a process */
702 DECL_HANDLER(debug_break
)
704 struct process
*process
;
707 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
/*FIXME*/ )))
709 if (process
!= current
->process
)
711 /* find a suitable thread to signal */
712 struct thread
*thread
;
713 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
715 if (send_thread_signal( thread
, SIGTRAP
)) goto done
;
717 set_error( STATUS_ACCESS_DENIED
);
719 else reply
->self
= 1;
722 release_object( process
);
725 /* set debugger kill on exit flag */
726 DECL_HANDLER(set_debugger_kill_on_exit
)
728 if (!current
->debug_ctx
)
730 set_error( STATUS_ACCESS_DENIED
);
733 current
->debug_ctx
->kill_on_exit
= req
->kill_on_exit
;