include: Introduce combaseapi.h.
[wine.git] / server / debugger.c
blobb718c7c283f93e44016b625ef78fb403e540863c
1 /*
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
35 #include "handle.h"
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "request.h"
41 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_DELAYED, EVENT_CONTINUED };
43 /* debug event */
44 struct debug_event
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 file *file; /* file object for events that need one */
50 enum debug_event_state state; /* event state */
51 int status; /* continuation status */
52 debug_event_t data; /* event data */
55 static const WCHAR debug_obj_name[] = {'D','e','b','u','g','O','b','j','e','c','t'};
57 struct type_descr debug_obj_type =
59 { debug_obj_name, sizeof(debug_obj_name) }, /* name */
60 DEBUG_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
61 { /* mapping */
62 STANDARD_RIGHTS_READ | DEBUG_READ_EVENT,
63 STANDARD_RIGHTS_WRITE | DEBUG_PROCESS_ASSIGN,
64 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
65 DEBUG_ALL_ACCESS
69 /* debug object */
70 struct debug_obj
72 struct object obj; /* object header */
73 struct list event_queue; /* pending events queue */
74 unsigned int flags; /* debug flags */
78 static void debug_event_dump( struct object *obj, int verbose );
79 static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry );
80 static void debug_event_destroy( struct object *obj );
82 static const struct object_ops debug_event_ops =
84 sizeof(struct debug_event), /* size */
85 &no_type, /* type */
86 debug_event_dump, /* dump */
87 add_queue, /* add_queue */
88 remove_queue, /* remove_queue */
89 debug_event_signaled, /* signaled */
90 no_satisfied, /* satisfied */
91 no_signal, /* signal */
92 no_get_fd, /* get_fd */
93 default_map_access, /* map_access */
94 default_get_sd, /* get_sd */
95 default_set_sd, /* set_sd */
96 no_get_full_name, /* get_full_name */
97 no_lookup_name, /* lookup_name */
98 no_link_name, /* link_name */
99 NULL, /* unlink_name */
100 no_open_file, /* open_file */
101 no_kernel_obj_list, /* get_kernel_obj_list */
102 no_close_handle, /* close_handle */
103 debug_event_destroy /* destroy */
106 static void debug_obj_dump( struct object *obj, int verbose );
107 static int debug_obj_signaled( struct object *obj, struct wait_queue_entry *entry );
108 static void debug_obj_destroy( struct object *obj );
110 static const struct object_ops debug_obj_ops =
112 sizeof(struct debug_obj), /* size */
113 &debug_obj_type, /* type */
114 debug_obj_dump, /* dump */
115 add_queue, /* add_queue */
116 remove_queue, /* remove_queue */
117 debug_obj_signaled, /* signaled */
118 no_satisfied, /* satisfied */
119 no_signal, /* signal */
120 no_get_fd, /* get_fd */
121 default_map_access, /* map_access */
122 default_get_sd, /* get_sd */
123 default_set_sd, /* set_sd */
124 default_get_full_name, /* get_full_name */
125 no_lookup_name, /* lookup_name */
126 directory_link_name, /* link_name */
127 default_unlink_name, /* unlink_name */
128 no_open_file, /* open_file */
129 no_kernel_obj_list, /* get_kernel_obj_list */
130 no_close_handle, /* close_handle */
131 debug_obj_destroy /* destroy */
134 /* get a pointer to TEB->ArbitraryUserPointer in the client address space */
135 static client_ptr_t get_teb_user_ptr( struct thread *thread )
137 unsigned int ptr_size = is_machine_64bit( native_machine ) ? 8 : 4;
138 return thread->teb + 5 * ptr_size;
142 /* routines to build an event according to its type */
144 static void fill_exception_event( struct debug_event *event, const void *arg )
146 const debug_event_t *data = arg;
147 event->data.exception = data->exception;
148 event->data.exception.nb_params = min( event->data.exception.nb_params, EXCEPTION_MAXIMUM_PARAMETERS );
151 static void fill_create_thread_event( struct debug_event *event, const void *arg )
153 const client_ptr_t *entry = arg;
155 if (entry) event->data.create_thread.start = *entry;
158 static void fill_create_process_event( struct debug_event *event, const void *arg )
160 const struct memory_view *view = arg;
161 const pe_image_info_t *image_info = get_view_image_info( view, &event->data.create_process.base );
163 event->data.create_process.start = image_info->entry_point;
164 event->data.create_process.dbg_offset = image_info->dbg_offset;
165 event->data.create_process.dbg_size = image_info->dbg_size;
166 /* the doc says write access too, but this doesn't seem a good idea */
167 event->file = get_view_file( view, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE );
170 static void fill_exit_thread_event( struct debug_event *event, const void *arg )
172 const struct thread *thread = arg;
173 event->data.exit.exit_code = thread->exit_code;
176 static void fill_exit_process_event( struct debug_event *event, const void *arg )
178 const struct process *process = arg;
179 event->data.exit.exit_code = process->exit_code;
182 static void fill_load_dll_event( struct debug_event *event, const void *arg )
184 const struct memory_view *view = arg;
185 const pe_image_info_t *image_info = get_view_image_info( view, &event->data.load_dll.base );
187 event->data.load_dll.dbg_offset = image_info->dbg_offset;
188 event->data.load_dll.dbg_size = image_info->dbg_size;
189 event->data.load_dll.name = get_teb_user_ptr( event->sender );
190 event->file = get_view_file( view, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE );
193 static void fill_unload_dll_event( struct debug_event *event, const void *arg )
195 const struct memory_view *view = arg;
196 get_view_image_info( view, &event->data.unload_dll.base );
199 typedef void (*fill_event_func)( struct debug_event *event, const void *arg );
201 static const fill_event_func fill_debug_event[] =
203 fill_create_thread_event, /* DbgCreateThreadStateChange */
204 fill_create_process_event, /* DbgCreateProcessStateChange */
205 fill_exit_thread_event, /* DbgExitThreadStateChange */
206 fill_exit_process_event, /* DbgExitProcessStateChange */
207 fill_exception_event, /* DbgExceptionStateChange */
208 fill_exception_event, /* DbgBreakpointStateChange */
209 fill_exception_event, /* DbgSingleStepStateChange */
210 fill_load_dll_event, /* DbgLoadDllStateChange */
211 fill_unload_dll_event /* DbgUnloadDllStateChange */
214 /* allocate the necessary handles in the event data */
215 static void alloc_event_handles( struct debug_event *event, struct process *process )
217 switch (event->data.code)
219 case DbgCreateThreadStateChange:
220 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
221 event->data.create_thread.handle = alloc_handle( process, event->sender, THREAD_ALL_ACCESS, 0 );
222 break;
223 case DbgCreateProcessStateChange:
224 event->data.create_process.thread = alloc_handle( process, event->sender, THREAD_ALL_ACCESS, 0 );
225 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
226 event->data.create_process.process = alloc_handle( process, event->sender->process,
227 PROCESS_ALL_ACCESS, 0 );
228 if (event->file)
229 event->data.create_process.file = alloc_handle( process, event->file, GENERIC_READ, 0 );
230 break;
231 case DbgLoadDllStateChange:
232 if (event->file)
233 event->data.load_dll.handle = alloc_handle( process, event->file, GENERIC_READ, 0 );
234 break;
236 clear_error(); /* ignore errors, simply set handles to 0 */
239 /* unlink the first event from the queue */
240 static void unlink_event( struct debug_obj *debug_obj, struct debug_event *event )
242 list_remove( &event->entry );
243 if (event->sender->process->debug_event == event) event->sender->process->debug_event = NULL;
244 release_object( event );
247 /* link an event at the end of the queue */
248 static void link_event( struct debug_obj *debug_obj, struct debug_event *event )
250 grab_object( event );
251 list_add_tail( &debug_obj->event_queue, &event->entry );
252 if (!event->sender->process->debug_event)
254 /* grab reference since debugger could be killed while trying to wake up */
255 grab_object( debug_obj );
256 wake_up( &debug_obj->obj, 0 );
257 release_object( debug_obj );
261 /* resume a delayed debug event already in the queue */
262 static void resume_event( struct debug_obj *debug_obj, struct debug_event *event )
264 event->state = EVENT_QUEUED;
265 if (!event->sender->process->debug_event)
267 grab_object( debug_obj );
268 wake_up( &debug_obj->obj, 0 );
269 release_object( debug_obj );
273 /* delay a debug event already in the queue to be replayed when thread wakes up */
274 static void delay_event( struct debug_obj *debug_obj, struct debug_event *event )
276 event->state = EVENT_DELAYED;
277 if (event->sender->process->debug_event == event) event->sender->process->debug_event = NULL;
280 /* find the next event that we can send to the debugger */
281 static struct debug_event *find_event_to_send( struct debug_obj *debug_obj )
283 struct debug_event *event;
285 LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
287 if (event->state == EVENT_SENT) continue; /* already sent */
288 if (event->state == EVENT_DELAYED) continue; /* delayed until thread resumes */
289 if (event->sender->process->debug_event) continue; /* process busy with another one */
290 return event;
292 return NULL;
295 static void debug_event_dump( struct object *obj, int verbose )
297 struct debug_event *debug_event = (struct debug_event *)obj;
298 assert( obj->ops == &debug_event_ops );
299 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
300 debug_event->sender, debug_event->data.code, debug_event->state );
303 static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry )
305 struct debug_event *debug_event = (struct debug_event *)obj;
306 assert( obj->ops == &debug_event_ops );
307 return debug_event->state == EVENT_CONTINUED;
310 static void debug_event_destroy( struct object *obj )
312 struct debug_event *event = (struct debug_event *)obj;
313 assert( obj->ops == &debug_event_ops );
315 if (event->file) release_object( event->file );
316 release_object( event->sender );
319 static void debug_obj_dump( struct object *obj, int verbose )
321 struct debug_obj *debug_obj = (struct debug_obj *)obj;
322 assert( obj->ops == &debug_obj_ops );
323 fprintf( stderr, "Debug context head=%p tail=%p\n",
324 debug_obj->event_queue.next, debug_obj->event_queue.prev );
327 static int debug_obj_signaled( struct object *obj, struct wait_queue_entry *entry )
329 struct debug_obj *debug_obj = (struct debug_obj *)obj;
330 assert( obj->ops == &debug_obj_ops );
331 return find_event_to_send( debug_obj ) != NULL;
334 static void debug_obj_destroy( struct object *obj )
336 struct list *ptr;
337 struct debug_obj *debug_obj = (struct debug_obj *)obj;
338 assert( obj->ops == &debug_obj_ops );
340 detach_debugged_processes( debug_obj,
341 (debug_obj->flags & DEBUG_KILL_ON_CLOSE) ? STATUS_DEBUGGER_INACTIVE : 0 );
343 /* free all pending events */
344 while ((ptr = list_head( &debug_obj->event_queue )))
345 unlink_event( debug_obj, LIST_ENTRY( ptr, struct debug_event, entry ));
348 struct debug_obj *get_debug_obj( struct process *process, obj_handle_t handle, unsigned int access )
350 return (struct debug_obj *)get_handle_obj( process, handle, access, &debug_obj_ops );
353 static struct debug_obj *create_debug_obj( struct object *root, const struct unicode_str *name,
354 unsigned int attr, unsigned int flags,
355 const struct security_descriptor *sd )
357 struct debug_obj *debug_obj;
359 if ((debug_obj = create_named_object( root, &debug_obj_ops, name, attr, sd )))
361 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
363 debug_obj->flags = flags;
364 list_init( &debug_obj->event_queue );
367 return debug_obj;
370 /* continue a debug event */
371 static int continue_debug_event( struct debug_obj *debug_obj, struct process *process,
372 struct thread *thread, int status )
374 if (process->debug_obj == debug_obj && thread->process == process)
376 struct debug_event *event;
378 if (status == DBG_REPLY_LATER)
380 /* if thread is suspended, delay all its events and resume process
381 * if not, reset the event for immediate replay */
382 LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
384 if (event->sender != thread) continue;
385 if (thread->suspend)
387 delay_event( debug_obj, event );
388 resume_process( process );
390 else if (event->state == EVENT_SENT)
392 assert( event->sender->process->debug_event == event );
393 event->sender->process->debug_event = NULL;
394 resume_event( debug_obj, event );
395 return 1;
398 return 1;
401 /* find the event in the queue */
402 LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
404 if (event->state != EVENT_SENT) continue;
405 if (event->sender == thread)
407 assert( event->sender->process->debug_event == event );
408 event->status = status;
409 event->state = EVENT_CONTINUED;
410 wake_up( &event->obj, 0 );
411 unlink_event( debug_obj, event );
412 resume_process( process );
413 return 1;
417 /* not debugging this process, or no such event */
418 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
419 return 0;
422 /* alloc a debug event for a debugger */
423 static struct debug_event *alloc_debug_event( struct thread *thread, int code, const void *arg )
425 struct debug_event *event;
427 assert( code >= DbgCreateThreadStateChange && code <= DbgUnloadDllStateChange );
429 /* build the event */
430 if (!(event = alloc_object( &debug_event_ops ))) return NULL;
431 event->state = EVENT_QUEUED;
432 event->sender = (struct thread *)grab_object( thread );
433 event->file = NULL;
434 memset( &event->data, 0, sizeof(event->data) );
435 fill_debug_event[code - DbgCreateThreadStateChange]( event, arg );
436 event->data.code = code;
437 return event;
440 /* generate a debug event from inside the server and queue it */
441 void generate_debug_event( struct thread *thread, int code, const void *arg )
443 struct debug_obj *debug_obj = thread->process->debug_obj;
445 if (debug_obj)
447 struct debug_event *event = alloc_debug_event( thread, code, arg );
448 if (event)
450 link_event( debug_obj, event );
451 suspend_process( thread->process );
452 release_object( event );
454 clear_error(); /* ignore errors */
458 void resume_delayed_debug_events( struct thread *thread )
460 struct debug_obj *debug_obj = thread->process->debug_obj;
461 struct debug_event *event;
463 if (debug_obj)
465 LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
467 if (event->sender != thread) continue;
468 if (event->state != EVENT_DELAYED) continue;
469 resume_event( debug_obj, event );
470 suspend_process( thread->process );
475 /* attach a process to a debugger thread and suspend it */
476 static int debugger_attach( struct process *process, struct thread *debugger, struct debug_obj *debug_obj )
478 if (debugger->process == process) goto error;
479 if (!is_process_init_done( process )) goto error; /* still starting up */
480 if (list_empty( &process->thread_list )) goto error; /* no thread running in the process */
481 /* don't let a debugger debug its console... won't work */
482 if (debugger->process->console)
484 struct thread *renderer = console_get_renderer(debugger->process->console);
485 if (renderer && renderer->process == process) goto error;
488 suspend_process( process );
490 if (!set_process_debug_flag( process, 1 ))
492 resume_process( process );
493 return 0;
495 process->debug_obj = debug_obj;
496 process->debug_children = 0;
497 generate_startup_debug_events( process );
498 resume_process( process );
499 return 1;
501 error:
502 set_error( STATUS_ACCESS_DENIED );
503 return 0;
507 /* detach a process from a debugger thread (and resume it) */
508 void debugger_detach( struct process *process, struct debug_obj *debug_obj )
510 struct debug_event *event, *next;
512 suspend_process( process );
514 /* send continue indication for all events */
515 LIST_FOR_EACH_ENTRY_SAFE( event, next, &debug_obj->event_queue, struct debug_event, entry )
517 if (event->sender->process != process) continue;
519 assert( event->state != EVENT_CONTINUED );
520 event->status = DBG_CONTINUE;
521 event->state = EVENT_CONTINUED;
522 wake_up( &event->obj, 0 );
523 unlink_event( debug_obj, event );
524 /* from queued debug event */
525 resume_process( process );
528 /* remove relationships between process and its debugger */
529 process->debug_obj = NULL;
530 if (!set_process_debug_flag( process, 0 )) clear_error(); /* ignore error */
532 resume_process( process );
535 /* create a debug object */
536 DECL_HANDLER(create_debug_obj)
538 struct debug_obj *debug_obj;
539 struct unicode_str name;
540 struct object *root;
541 const struct security_descriptor *sd;
542 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
544 if (!objattr) return;
545 if ((debug_obj = create_debug_obj( root, &name, objattr->attributes, req->flags, sd )))
547 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
548 reply->handle = alloc_handle( current->process, debug_obj, req->access, objattr->attributes );
549 else
550 reply->handle = alloc_handle_no_access_check( current->process, debug_obj,
551 req->access, objattr->attributes );
552 release_object( debug_obj );
554 if (root) release_object( root );
557 /* Wait for a debug event */
558 DECL_HANDLER(wait_debug_event)
560 struct debug_obj *debug_obj;
561 struct debug_event *event;
563 if (!(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_READ_EVENT ))) return;
565 if ((event = find_event_to_send( debug_obj )))
567 event->state = EVENT_SENT;
568 event->sender->process->debug_event = event;
569 reply->pid = get_process_id( event->sender->process );
570 reply->tid = get_thread_id( event->sender );
571 alloc_event_handles( event, current->process );
572 set_reply_data( &event->data, min( get_reply_max_size(), sizeof(event->data) ));
574 else
576 int state = list_empty( &debug_obj->event_queue ) ? DbgIdle : DbgReplyPending;
577 set_reply_data( &state, min( get_reply_max_size(), sizeof(state) ));
579 release_object( debug_obj );
582 /* Continue a debug event */
583 DECL_HANDLER(continue_debug_event)
585 struct debug_obj *debug_obj;
586 struct process *process;
588 if (req->status != DBG_EXCEPTION_NOT_HANDLED &&
589 req->status != DBG_EXCEPTION_HANDLED &&
590 req->status != DBG_CONTINUE &&
591 req->status != DBG_REPLY_LATER)
593 set_error( STATUS_INVALID_PARAMETER );
594 return;
597 if (!(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_READ_EVENT ))) return;
599 if ((process = get_process_from_id( req->pid )))
601 struct thread *thread = get_thread_from_id( req->tid );
602 if (thread)
604 continue_debug_event( debug_obj, process, thread, req->status );
605 release_object( thread );
607 release_object( process );
609 release_object( debug_obj );
612 /* start or stop debugging an existing process */
613 DECL_HANDLER(debug_process)
615 struct debug_obj *debug_obj;
616 struct process *process = get_process_from_handle( req->handle,
617 PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_SUSPEND_RESUME );
619 if (!process) return;
620 if ((debug_obj = get_debug_obj( current->process, req->debug, DEBUG_PROCESS_ASSIGN )))
622 if (req->attach)
624 if (!process->debug_obj) debugger_attach( process, current, debug_obj );
625 else set_error( STATUS_ACCESS_DENIED );
627 else
629 if (process->debug_obj == debug_obj) debugger_detach( process, debug_obj );
630 else set_error( STATUS_ACCESS_DENIED );
632 release_object( debug_obj );
634 release_object( process );
637 /* queue an exception event */
638 DECL_HANDLER(queue_exception_event)
640 struct debug_obj *debug_obj = current->process->debug_obj;
642 reply->handle = 0;
643 if (debug_obj)
645 debug_event_t data;
646 struct debug_event *event;
647 struct thread *thread = current;
649 if ((req->len % sizeof(client_ptr_t)) != 0 ||
650 req->len > get_req_data_size() ||
651 req->len > EXCEPTION_MAXIMUM_PARAMETERS * sizeof(client_ptr_t))
653 set_error( STATUS_INVALID_PARAMETER );
654 return;
656 memset( &data, 0, sizeof(data) );
657 data.exception.first = req->first;
658 data.exception.exc_code = req->code;
659 data.exception.flags = req->flags;
660 data.exception.record = req->record;
661 data.exception.address = req->address;
662 data.exception.nb_params = req->len / sizeof(client_ptr_t);
663 memcpy( data.exception.params, get_req_data(), req->len );
665 if ((event = alloc_debug_event( thread, DbgExceptionStateChange, &data )))
667 if ((reply->handle = alloc_handle( thread->process, event, SYNCHRONIZE, 0 )))
669 link_event( debug_obj, event );
670 suspend_process( thread->process );
672 release_object( event );
677 /* retrieve the status of an exception event */
678 DECL_HANDLER(get_exception_status)
680 struct debug_event *event;
682 if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
683 0, &debug_event_ops )))
685 close_handle( current->process, req->handle );
686 set_error( event->state == EVENT_CONTINUED ? event->status : STATUS_PENDING );
687 release_object( event );
691 /* set debug object information */
692 DECL_HANDLER(set_debug_obj_info)
694 struct debug_obj *debug_obj;
696 if (!(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_SET_INFORMATION ))) return;
697 debug_obj->flags = req->flags;
698 release_object( debug_obj );