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"
39 enum debug_event_state
{ EVENT_QUEUED
, EVENT_SENT
, EVENT_CONTINUED
};
44 struct object obj
; /* object header */
45 struct list entry
; /* entry in event queue */
46 struct thread
*sender
; /* thread which sent this event */
47 struct thread
*debugger
; /* debugger thread receiving the event */
48 enum debug_event_state state
; /* event state */
49 int status
; /* continuation status */
50 debug_event_t data
; /* event data */
51 CONTEXT context
; /* register context */
57 struct object obj
; /* object header */
58 struct list event_queue
; /* pending events queue */
59 int kill_on_exit
;/* kill debuggees on debugger exit ? */
63 static void debug_event_dump( struct object
*obj
, int verbose
);
64 static int debug_event_signaled( struct object
*obj
, struct thread
*thread
);
65 static void debug_event_destroy( struct object
*obj
);
67 static const struct object_ops debug_event_ops
=
69 sizeof(struct debug_event
), /* size */
70 debug_event_dump
, /* dump */
71 add_queue
, /* add_queue */
72 remove_queue
, /* remove_queue */
73 debug_event_signaled
, /* signaled */
74 no_satisfied
, /* satisfied */
75 no_signal
, /* signal */
76 no_get_fd
, /* get_fd */
77 no_close_handle
, /* close_handle */
78 debug_event_destroy
/* destroy */
81 static void debug_ctx_dump( struct object
*obj
, int verbose
);
82 static int debug_ctx_signaled( struct object
*obj
, struct thread
*thread
);
83 static void debug_ctx_destroy( struct object
*obj
);
85 static const struct object_ops debug_ctx_ops
=
87 sizeof(struct debug_ctx
), /* size */
88 debug_ctx_dump
, /* dump */
89 add_queue
, /* add_queue */
90 remove_queue
, /* remove_queue */
91 debug_ctx_signaled
, /* signaled */
92 no_satisfied
, /* satisfied */
93 no_signal
, /* signal */
94 no_get_fd
, /* get_fd */
95 no_close_handle
, /* close_handle */
96 debug_ctx_destroy
/* destroy */
100 /* routines to build an event according to its type */
102 static int fill_exception_event( struct debug_event
*event
, void *arg
)
104 memcpy( &event
->data
.info
.exception
, arg
, sizeof(event
->data
.info
.exception
) );
108 static int fill_create_thread_event( struct debug_event
*event
, void *arg
)
110 struct process
*debugger
= event
->debugger
->process
;
111 struct thread
*thread
= event
->sender
;
114 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
115 if (!(handle
= alloc_handle( debugger
, thread
, THREAD_ALL_ACCESS
, FALSE
))) return 0;
116 event
->data
.info
.create_thread
.handle
= handle
;
117 event
->data
.info
.create_thread
.teb
= thread
->teb
;
118 event
->data
.info
.create_thread
.start
= arg
;
122 static int fill_create_process_event( struct debug_event
*event
, void *arg
)
124 struct process
*debugger
= event
->debugger
->process
;
125 struct thread
*thread
= event
->sender
;
126 struct process
*process
= thread
->process
;
129 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
130 if (!(handle
= alloc_handle( debugger
, process
, PROCESS_ALL_ACCESS
, FALSE
))) return 0;
131 event
->data
.info
.create_process
.process
= handle
;
133 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
134 if (!(handle
= alloc_handle( debugger
, thread
, THREAD_ALL_ACCESS
, FALSE
)))
136 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
139 event
->data
.info
.create_process
.thread
= handle
;
142 if (process
->exe
.file
&&
143 /* the doc says write access too, but this doesn't seem a good idea */
144 !(handle
= alloc_handle( debugger
, process
->exe
.file
, GENERIC_READ
, FALSE
)))
146 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
147 close_handle( debugger
, event
->data
.info
.create_process
.thread
, NULL
);
150 event
->data
.info
.create_process
.file
= handle
;
151 event
->data
.info
.create_process
.teb
= thread
->teb
;
152 event
->data
.info
.create_process
.base
= process
->exe
.base
;
153 event
->data
.info
.create_process
.start
= arg
;
154 event
->data
.info
.create_process
.dbg_offset
= process
->exe
.dbg_offset
;
155 event
->data
.info
.create_process
.dbg_size
= process
->exe
.dbg_size
;
156 event
->data
.info
.create_process
.name
= process
->exe
.name
;
157 event
->data
.info
.create_process
.unicode
= 1;
161 static int fill_exit_thread_event( struct debug_event
*event
, void *arg
)
163 struct thread
*thread
= arg
;
164 event
->data
.info
.exit
.exit_code
= thread
->exit_code
;
168 static int fill_exit_process_event( struct debug_event
*event
, void *arg
)
170 struct process
*process
= arg
;
171 event
->data
.info
.exit
.exit_code
= process
->exit_code
;
175 static int fill_load_dll_event( struct debug_event
*event
, void *arg
)
177 struct process
*debugger
= event
->debugger
->process
;
178 struct process_dll
*dll
= arg
;
179 obj_handle_t handle
= 0;
181 if (dll
->file
&& !(handle
= alloc_handle( debugger
, dll
->file
, GENERIC_READ
, FALSE
)))
183 event
->data
.info
.load_dll
.handle
= handle
;
184 event
->data
.info
.load_dll
.base
= dll
->base
;
185 event
->data
.info
.load_dll
.dbg_offset
= dll
->dbg_offset
;
186 event
->data
.info
.load_dll
.dbg_size
= dll
->dbg_size
;
187 event
->data
.info
.load_dll
.name
= dll
->name
;
188 event
->data
.info
.load_dll
.unicode
= 1;
192 static int fill_unload_dll_event( struct debug_event
*event
, void *arg
)
194 event
->data
.info
.unload_dll
.base
= arg
;
198 static int fill_output_debug_string_event( struct debug_event
*event
, void *arg
)
200 struct debug_event_output_string
*data
= arg
;
201 event
->data
.info
.output_string
= *data
;
205 typedef int (*fill_event_func
)( struct debug_event
*event
, void *arg
);
207 #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT /* RIP_EVENT not supported */
209 static const fill_event_func fill_debug_event
[NB_DEBUG_EVENTS
] =
211 fill_exception_event
, /* EXCEPTION_DEBUG_EVENT */
212 fill_create_thread_event
, /* CREATE_THREAD_DEBUG_EVENT */
213 fill_create_process_event
, /* CREATE_PROCESS_DEBUG_EVENT */
214 fill_exit_thread_event
, /* EXIT_THREAD_DEBUG_EVENT */
215 fill_exit_process_event
, /* EXIT_PROCESS_DEBUG_EVENT */
216 fill_load_dll_event
, /* LOAD_DLL_DEBUG_EVENT */
217 fill_unload_dll_event
, /* UNLOAD_DLL_DEBUG_EVENT */
218 fill_output_debug_string_event
/* OUTPUT_DEBUG_STRING_EVENT */
222 /* unlink the first event from the queue */
223 static void unlink_event( struct debug_ctx
*debug_ctx
, struct debug_event
*event
)
225 list_remove( &event
->entry
);
226 if (event
->sender
->debug_event
== event
) event
->sender
->debug_event
= NULL
;
227 release_object( event
);
230 /* link an event at the end of the queue */
231 static void link_event( struct debug_event
*event
)
233 struct debug_ctx
*debug_ctx
= event
->debugger
->debug_ctx
;
236 grab_object( event
);
237 list_add_tail( &debug_ctx
->event_queue
, &event
->entry
);
238 if (!event
->sender
->debug_event
) wake_up( &debug_ctx
->obj
, 0 );
241 /* find the next event that we can send to the debugger */
242 static struct debug_event
*find_event_to_send( struct debug_ctx
*debug_ctx
)
244 struct debug_event
*event
;
246 LIST_FOR_EACH_ENTRY( event
, &debug_ctx
->event_queue
, struct debug_event
, entry
)
248 if (event
->state
== EVENT_SENT
) continue; /* already sent */
249 if (event
->sender
->debug_event
) continue; /* thread busy with another one */
255 static void debug_event_dump( struct object
*obj
, int verbose
)
257 struct debug_event
*debug_event
= (struct debug_event
*)obj
;
258 assert( obj
->ops
== &debug_event_ops
);
259 fprintf( stderr
, "Debug event sender=%p code=%d state=%d\n",
260 debug_event
->sender
, debug_event
->data
.code
, debug_event
->state
);
263 static int debug_event_signaled( struct object
*obj
, struct thread
*thread
)
265 struct debug_event
*debug_event
= (struct debug_event
*)obj
;
266 assert( obj
->ops
== &debug_event_ops
);
267 return debug_event
->state
== EVENT_CONTINUED
;
270 static void debug_event_destroy( struct object
*obj
)
272 struct debug_event
*event
= (struct debug_event
*)obj
;
273 assert( obj
->ops
== &debug_event_ops
);
275 /* If the event has been sent already, the handles are now under the */
276 /* responsibility of the debugger process, so we don't touch them */
277 if (event
->state
== EVENT_QUEUED
)
279 struct process
*debugger
= event
->debugger
->process
;
280 switch(event
->data
.code
)
282 case CREATE_THREAD_DEBUG_EVENT
:
283 close_handle( debugger
, event
->data
.info
.create_thread
.handle
, NULL
);
285 case CREATE_PROCESS_DEBUG_EVENT
:
286 if (event
->data
.info
.create_process
.file
)
287 close_handle( debugger
, event
->data
.info
.create_process
.file
, NULL
);
288 close_handle( debugger
, event
->data
.info
.create_process
.thread
, NULL
);
289 close_handle( debugger
, event
->data
.info
.create_process
.process
, NULL
);
291 case LOAD_DLL_DEBUG_EVENT
:
292 if (event
->data
.info
.load_dll
.handle
)
293 close_handle( debugger
, event
->data
.info
.load_dll
.handle
, NULL
);
297 if (event
->sender
->context
== &event
->context
) event
->sender
->context
= NULL
;
298 release_object( event
->sender
);
299 release_object( event
->debugger
);
302 static void debug_ctx_dump( struct object
*obj
, int verbose
)
304 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
305 assert( obj
->ops
== &debug_ctx_ops
);
306 fprintf( stderr
, "Debug context head=%p tail=%p\n",
307 debug_ctx
->event_queue
.next
, debug_ctx
->event_queue
.prev
);
310 static int debug_ctx_signaled( struct object
*obj
, struct thread
*thread
)
312 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
313 assert( obj
->ops
== &debug_ctx_ops
);
314 return find_event_to_send( debug_ctx
) != NULL
;
317 static void debug_ctx_destroy( struct object
*obj
)
320 struct debug_ctx
*debug_ctx
= (struct debug_ctx
*)obj
;
321 assert( obj
->ops
== &debug_ctx_ops
);
323 /* free all pending events */
324 while ((ptr
= list_head( &debug_ctx
->event_queue
)))
325 unlink_event( debug_ctx
, LIST_ENTRY( ptr
, struct debug_event
, entry
));
328 /* continue a debug event */
329 static int continue_debug_event( struct process
*process
, struct thread
*thread
, int status
)
331 struct debug_ctx
*debug_ctx
= current
->debug_ctx
;
333 if (debug_ctx
&& process
->debugger
== current
&& thread
->process
== process
)
335 struct debug_event
*event
;
337 /* find the event in the queue */
338 LIST_FOR_EACH_ENTRY( event
, &debug_ctx
->event_queue
, struct debug_event
, entry
)
340 if (event
->state
!= EVENT_SENT
) continue;
341 if (event
->sender
== thread
)
343 assert( event
->sender
->debug_event
== event
);
345 event
->status
= status
;
346 event
->state
= EVENT_CONTINUED
;
347 wake_up( &event
->obj
, 0 );
348 unlink_event( debug_ctx
, event
);
349 resume_process( process
);
354 /* not debugging this process, or no such event */
355 set_error( STATUS_ACCESS_DENIED
); /* FIXME */
359 /* alloc a debug event for a debugger */
360 static struct debug_event
*alloc_debug_event( struct thread
*thread
, int code
,
361 void *arg
, const CONTEXT
*context
)
363 struct thread
*debugger
= thread
->process
->debugger
;
364 struct debug_event
*event
;
366 assert( code
> 0 && code
<= NB_DEBUG_EVENTS
);
367 /* cannot queue a debug event for myself */
368 assert( debugger
->process
!= thread
->process
);
370 /* build the event */
371 if (!(event
= alloc_object( &debug_event_ops
))) return NULL
;
372 event
->state
= EVENT_QUEUED
;
373 event
->sender
= (struct thread
*)grab_object( thread
);
374 event
->debugger
= (struct thread
*)grab_object( debugger
);
375 event
->data
.code
= code
;
377 if (!fill_debug_event
[code
-1]( event
, arg
))
379 event
->data
.code
= -1; /* make sure we don't attempt to close handles */
380 release_object( event
);
385 memcpy( &event
->context
, context
, sizeof(event
->context
) );
386 thread
->context
= &event
->context
;
391 /* generate a debug event from inside the server and queue it */
392 void generate_debug_event( struct thread
*thread
, int code
, void *arg
)
394 if (thread
->process
->debugger
)
396 struct debug_event
*event
= alloc_debug_event( thread
, code
, arg
, NULL
);
400 suspend_process( thread
->process
);
401 release_object( event
);
406 /* attach a process to a debugger thread and suspend it */
407 static int debugger_attach( struct process
*process
, struct thread
*debugger
)
409 struct thread
*thread
;
411 if (process
->debugger
) goto error
; /* already being debugged */
412 if (!is_process_init_done( process
)) goto error
; /* still starting up */
413 if (list_empty( &process
->thread_list
)) goto error
; /* no thread running in the process */
415 /* make sure we don't create a debugging loop */
416 for (thread
= debugger
; thread
; thread
= thread
->process
->debugger
)
417 if (thread
->process
== process
) goto error
;
419 /* don't let a debugger debug its console... won't work */
420 if (debugger
->process
->console
&& debugger
->process
->console
->renderer
->process
== process
)
423 suspend_process( process
);
424 if (!attach_process( process
) || !set_process_debugger( process
, debugger
))
426 resume_process( process
);
429 if (!set_process_debug_flag( process
, 1 ))
431 process
->debugger
= NULL
;
432 resume_process( process
);
438 set_error( STATUS_ACCESS_DENIED
);
443 /* detach a process from a debugger thread (and resume it ?) */
444 int debugger_detach( struct process
*process
, struct thread
*debugger
)
446 struct debug_event
*event
;
447 struct debug_ctx
*debug_ctx
;
449 if (!process
->debugger
|| process
->debugger
!= debugger
)
450 goto error
; /* not currently debugged, or debugged by another debugger */
451 if (!debugger
->debug_ctx
) goto error
; /* should be a debugger */
452 /* init should be done, otherwise wouldn't be attached */
453 assert(is_process_init_done(process
));
455 suspend_process( process
);
456 /* send continue indication for all events */
457 debug_ctx
= debugger
->debug_ctx
;
459 /* find the event in the queue
460 * FIXME: could loop on process' threads and look the debug_event field */
461 LIST_FOR_EACH_ENTRY( event
, &debug_ctx
->event_queue
, struct debug_event
, entry
)
463 if (event
->state
!= EVENT_QUEUED
) continue;
465 if (event
->sender
->process
== process
)
467 assert( event
->sender
->debug_event
== event
);
468 event
->status
= DBG_CONTINUE
;
469 event
->state
= EVENT_CONTINUED
;
470 wake_up( &event
->obj
, 0 );
471 unlink_event( debug_ctx
, event
);
472 /* from queued debug event */
473 resume_process( process
);
478 /* remove relationships between process and its debugger */
479 process
->debugger
= NULL
;
480 if (!set_process_debug_flag( process
, 0 )) clear_error(); /* ignore error */
481 detach_process( process
);
483 /* from this function */
484 resume_process( process
);
488 set_error( STATUS_ACCESS_DENIED
);
492 /* generate all startup events of a given process */
493 void generate_startup_debug_events( struct process
*process
, void *entry
)
496 struct thread
*thread
, *first_thread
= get_process_first_thread( process
);
498 /* generate creation events */
499 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
501 if (thread
== first_thread
)
502 generate_debug_event( thread
, CREATE_PROCESS_DEBUG_EVENT
, entry
);
504 generate_debug_event( thread
, CREATE_THREAD_DEBUG_EVENT
, NULL
);
507 /* generate dll events (in loading order, i.e. reverse list order) */
508 ptr
= list_tail( &process
->dlls
);
511 struct process_dll
*dll
= LIST_ENTRY( ptr
, struct process_dll
, entry
);
512 generate_debug_event( first_thread
, LOAD_DLL_DEBUG_EVENT
, dll
);
513 ptr
= list_prev( &process
->dlls
, ptr
);
517 /* set the debugger of a given process */
518 int set_process_debugger( struct process
*process
, struct thread
*debugger
)
520 struct debug_ctx
*debug_ctx
;
522 assert( !process
->debugger
);
524 if (!debugger
->debug_ctx
) /* need to allocate a context */
526 if (!(debug_ctx
= alloc_object( &debug_ctx_ops
))) return 0;
527 debug_ctx
->kill_on_exit
= 1;
528 list_init( &debug_ctx
->event_queue
);
529 debugger
->debug_ctx
= debug_ctx
;
531 process
->debugger
= debugger
;
535 /* a thread is exiting */
536 void debug_exit_thread( struct thread
*thread
)
538 if (thread
->debug_ctx
) /* this thread is a debugger */
540 if (thread
->debug_ctx
->kill_on_exit
)
542 /* kill all debugged processes */
543 kill_debugged_processes( thread
, thread
->exit_code
);
547 detach_debugged_processes( thread
);
549 release_object( thread
->debug_ctx
);
550 thread
->debug_ctx
= NULL
;
554 /* Wait for a debug event */
555 DECL_HANDLER(wait_debug_event
)
557 struct debug_ctx
*debug_ctx
= current
->debug_ctx
;
558 struct debug_event
*event
;
560 if (!debug_ctx
) /* current thread is not a debugger */
562 set_error( STATUS_INVALID_HANDLE
);
566 if ((event
= find_event_to_send( debug_ctx
)))
568 size_t size
= get_reply_max_size();
569 event
->state
= EVENT_SENT
;
570 event
->sender
->debug_event
= event
;
571 reply
->pid
= get_process_id( event
->sender
->process
);
572 reply
->tid
= get_thread_id( event
->sender
);
573 if (size
> sizeof(debug_event_t
)) size
= sizeof(debug_event_t
);
574 set_reply_data( &event
->data
, size
);
576 else /* no event ready */
581 reply
->wait
= alloc_handle( current
->process
, debug_ctx
, SYNCHRONIZE
, FALSE
);
585 /* Continue a debug event */
586 DECL_HANDLER(continue_debug_event
)
588 struct process
*process
= get_process_from_id( req
->pid
);
591 struct thread
*thread
= get_thread_from_id( req
->tid
);
594 continue_debug_event( process
, thread
, req
->status
);
595 release_object( thread
);
597 release_object( process
);
601 /* Start debugging an existing process */
602 DECL_HANDLER(debug_process
)
604 struct process
*process
= get_process_from_id( req
->pid
);
605 if (!process
) return;
609 debugger_detach( process
, current
);
611 else if (debugger_attach( process
, current
))
613 struct debug_event_exception data
;
614 struct thread
*thread
= get_process_first_thread( process
);
616 generate_startup_debug_events( process
, NULL
);
617 resume_process( process
);
619 data
.record
.ExceptionCode
= EXCEPTION_BREAKPOINT
;
620 data
.record
.ExceptionFlags
= EXCEPTION_CONTINUABLE
;
621 data
.record
.ExceptionRecord
= NULL
;
622 data
.record
.ExceptionAddress
= get_thread_ip( thread
);
623 data
.record
.NumberParameters
= 0;
625 generate_debug_event( thread
, EXCEPTION_DEBUG_EVENT
, &data
);
627 release_object( process
);
630 /* queue an exception event */
631 DECL_HANDLER(queue_exception_event
)
634 if (current
->process
->debugger
)
636 struct debug_event_exception data
;
637 struct debug_event
*event
;
638 const CONTEXT
*context
= get_req_data();
639 const EXCEPTION_RECORD
*rec
= (const EXCEPTION_RECORD
*)(context
+ 1);
641 if (get_req_data_size() < sizeof(*rec
) + sizeof(*context
))
643 set_error( STATUS_INVALID_PARAMETER
);
647 data
.first
= req
->first
;
648 if ((event
= alloc_debug_event( current
, EXCEPTION_DEBUG_EVENT
, &data
, context
)))
650 if ((reply
->handle
= alloc_handle( current
->process
, event
, SYNCHRONIZE
, FALSE
)))
653 suspend_process( current
->process
);
655 release_object( event
);
660 /* retrieve the status of an exception event */
661 DECL_HANDLER(get_exception_status
)
663 struct debug_event
*event
;
666 if ((event
= (struct debug_event
*)get_handle_obj( current
->process
, req
->handle
,
667 0, &debug_event_ops
)))
669 if (event
->state
== EVENT_CONTINUED
)
671 reply
->status
= event
->status
;
672 if (current
->context
== &event
->context
)
674 size_t size
= min( sizeof(CONTEXT
), get_reply_max_size() );
675 set_reply_data( &event
->context
, size
);
676 current
->context
= NULL
;
679 else set_error( STATUS_PENDING
);
680 release_object( event
);
684 /* send an output string to the debugger */
685 DECL_HANDLER(output_debug_string
)
687 struct debug_event_output_string data
;
689 data
.string
= req
->string
;
690 data
.unicode
= req
->unicode
;
691 data
.length
= req
->length
;
692 generate_debug_event( current
, OUTPUT_DEBUG_STRING_EVENT
, &data
);
695 /* simulate a breakpoint in a process */
696 DECL_HANDLER(debug_break
)
698 struct process
*process
;
701 if (!(process
= get_process_from_handle( req
->handle
, PROCESS_SET_INFORMATION
/*FIXME*/ )))
703 if (process
!= current
->process
)
705 /* find a suitable thread to signal */
706 struct thread
*thread
;
707 LIST_FOR_EACH_ENTRY( thread
, &process
->thread_list
, struct thread
, proc_entry
)
709 if (send_thread_signal( thread
, SIGTRAP
)) goto done
;
711 set_error( STATUS_ACCESS_DENIED
);
713 else reply
->self
= 1;
716 release_object( process
);
719 /* set debugger kill on exit flag */
720 DECL_HANDLER(set_debugger_kill_on_exit
)
722 if (!current
->debug_ctx
)
724 set_error( STATUS_ACCESS_DENIED
);
727 current
->debug_ctx
->kill_on_exit
= req
->kill_on_exit
;