dmusic: Implement IDirectMusicPortDownload_AllocateBuffer.
[wine.git] / server / debugger.c
blob48adb244b09ded3f8a5c8ccda0828a61cd504a8e
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"
23 #include <assert.h>
24 #include <signal.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <stdio.h>
29 #include "ntstatus.h"
30 #define WIN32_NO_STATUS
31 #include "windef.h"
32 #include "winternl.h"
34 #include "handle.h"
35 #include "file.h"
36 #include "process.h"
37 #include "thread.h"
38 #include "request.h"
40 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_DELAYED, EVENT_CONTINUED };
42 /* debug event */
43 struct debug_event
45 struct object obj; /* object header */
46 struct list entry; /* entry in event queue */
47 struct thread *sender; /* thread which sent this event */
48 struct file *file; /* file object for events that need one */
49 enum debug_event_state state; /* event state */
50 int status; /* continuation status */
51 debug_event_t data; /* event data */
54 static const WCHAR debug_obj_name[] = {'D','e','b','u','g','O','b','j','e','c','t'};
56 struct type_descr debug_obj_type =
58 { debug_obj_name, sizeof(debug_obj_name) }, /* name */
59 DEBUG_ALL_ACCESS | SYNCHRONIZE, /* valid_access */
60 { /* mapping */
61 STANDARD_RIGHTS_READ | DEBUG_READ_EVENT,
62 STANDARD_RIGHTS_WRITE | DEBUG_PROCESS_ASSIGN,
63 STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
64 DEBUG_ALL_ACCESS
68 /* debug object */
69 struct debug_obj
71 struct object obj; /* object header */
72 struct list event_queue; /* pending events queue */
73 unsigned int flags; /* debug flags */
77 static void debug_event_dump( struct object *obj, int verbose );
78 static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry );
79 static void debug_event_destroy( struct object *obj );
81 static const struct object_ops debug_event_ops =
83 sizeof(struct debug_event), /* size */
84 &no_type, /* type */
85 debug_event_dump, /* dump */
86 add_queue, /* add_queue */
87 remove_queue, /* remove_queue */
88 debug_event_signaled, /* signaled */
89 no_satisfied, /* satisfied */
90 no_signal, /* signal */
91 no_get_fd, /* get_fd */
92 default_map_access, /* map_access */
93 default_get_sd, /* get_sd */
94 default_set_sd, /* set_sd */
95 no_get_full_name, /* get_full_name */
96 no_lookup_name, /* lookup_name */
97 no_link_name, /* link_name */
98 NULL, /* unlink_name */
99 no_open_file, /* open_file */
100 no_kernel_obj_list, /* get_kernel_obj_list */
101 no_close_handle, /* close_handle */
102 debug_event_destroy /* destroy */
105 static void debug_obj_dump( struct object *obj, int verbose );
106 static int debug_obj_signaled( struct object *obj, struct wait_queue_entry *entry );
107 static void debug_obj_destroy( struct object *obj );
109 static const struct object_ops debug_obj_ops =
111 sizeof(struct debug_obj), /* size */
112 &debug_obj_type, /* type */
113 debug_obj_dump, /* dump */
114 add_queue, /* add_queue */
115 remove_queue, /* remove_queue */
116 debug_obj_signaled, /* signaled */
117 no_satisfied, /* satisfied */
118 no_signal, /* signal */
119 no_get_fd, /* get_fd */
120 default_map_access, /* map_access */
121 default_get_sd, /* get_sd */
122 default_set_sd, /* set_sd */
123 default_get_full_name, /* get_full_name */
124 no_lookup_name, /* lookup_name */
125 directory_link_name, /* link_name */
126 default_unlink_name, /* unlink_name */
127 no_open_file, /* open_file */
128 no_kernel_obj_list, /* get_kernel_obj_list */
129 no_close_handle, /* close_handle */
130 debug_obj_destroy /* destroy */
133 /* get a pointer to TEB->ArbitraryUserPointer in the client address space */
134 static client_ptr_t get_teb_user_ptr( struct thread *thread )
136 unsigned int ptr_size = is_machine_64bit( native_machine ) ? 8 : 4;
137 return thread->teb + 5 * ptr_size;
141 /* routines to build an event according to its type */
143 static void fill_exception_event( struct debug_event *event, const void *arg )
145 const debug_event_t *data = arg;
146 event->data.exception = data->exception;
147 event->data.exception.nb_params = min( event->data.exception.nb_params, EXCEPTION_MAXIMUM_PARAMETERS );
150 static void fill_create_thread_event( struct debug_event *event, const void *arg )
152 const client_ptr_t *entry = arg;
154 if (entry) event->data.create_thread.start = *entry;
157 static void fill_create_process_event( struct debug_event *event, const void *arg )
159 const struct memory_view *view = arg;
160 const pe_image_info_t *image_info = get_view_image_info( view, &event->data.create_process.base );
162 event->data.create_process.start = event->data.create_process.base + image_info->entry_point;
163 event->data.create_process.dbg_offset = image_info->dbg_offset;
164 event->data.create_process.dbg_size = image_info->dbg_size;
165 /* the doc says write access too, but this doesn't seem a good idea */
166 event->file = get_view_file( view, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE );
169 static void fill_exit_thread_event( struct debug_event *event, const void *arg )
171 const struct thread *thread = arg;
172 event->data.exit.exit_code = thread->exit_code;
175 static void fill_exit_process_event( struct debug_event *event, const void *arg )
177 const struct process *process = arg;
178 event->data.exit.exit_code = process->exit_code;
181 static void fill_load_dll_event( struct debug_event *event, const void *arg )
183 const struct memory_view *view = arg;
184 const pe_image_info_t *image_info = get_view_image_info( view, &event->data.load_dll.base );
186 event->data.load_dll.dbg_offset = image_info->dbg_offset;
187 event->data.load_dll.dbg_size = image_info->dbg_size;
188 event->data.load_dll.name = get_teb_user_ptr( event->sender );
189 event->file = get_view_file( view, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE );
192 static void fill_unload_dll_event( struct debug_event *event, const void *arg )
194 const struct memory_view *view = arg;
195 get_view_image_info( view, &event->data.unload_dll.base );
198 typedef void (*fill_event_func)( struct debug_event *event, const void *arg );
200 static const fill_event_func fill_debug_event[] =
202 fill_create_thread_event, /* DbgCreateThreadStateChange */
203 fill_create_process_event, /* DbgCreateProcessStateChange */
204 fill_exit_thread_event, /* DbgExitThreadStateChange */
205 fill_exit_process_event, /* DbgExitProcessStateChange */
206 fill_exception_event, /* DbgExceptionStateChange */
207 fill_exception_event, /* DbgBreakpointStateChange */
208 fill_exception_event, /* DbgSingleStepStateChange */
209 fill_load_dll_event, /* DbgLoadDllStateChange */
210 fill_unload_dll_event /* DbgUnloadDllStateChange */
213 /* allocate the necessary handles in the event data */
214 static void alloc_event_handles( struct debug_event *event, struct process *process )
216 switch (event->data.code)
218 case DbgCreateThreadStateChange:
219 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
220 event->data.create_thread.handle = alloc_handle( process, event->sender, THREAD_ALL_ACCESS, 0 );
221 break;
222 case DbgCreateProcessStateChange:
223 event->data.create_process.thread = alloc_handle( process, event->sender, THREAD_ALL_ACCESS, 0 );
224 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
225 event->data.create_process.process = alloc_handle( process, event->sender->process,
226 PROCESS_ALL_ACCESS, 0 );
227 if (event->file)
228 event->data.create_process.file = alloc_handle( process, event->file, GENERIC_READ, 0 );
229 break;
230 case DbgLoadDllStateChange:
231 if (event->file)
232 event->data.load_dll.handle = alloc_handle( process, event->file, GENERIC_READ, 0 );
233 break;
235 clear_error(); /* ignore errors, simply set handles to 0 */
238 /* unlink the first event from the queue */
239 static void unlink_event( struct debug_obj *debug_obj, struct debug_event *event )
241 list_remove( &event->entry );
242 if (event->sender->process->debug_event == event) event->sender->process->debug_event = NULL;
243 release_object( event );
246 /* link an event at the end of the queue */
247 static void link_event( struct debug_obj *debug_obj, struct debug_event *event )
249 grab_object( event );
250 list_add_tail( &debug_obj->event_queue, &event->entry );
251 if (!event->sender->process->debug_event)
253 /* grab reference since debugger could be killed while trying to wake up */
254 grab_object( debug_obj );
255 wake_up( &debug_obj->obj, 0 );
256 release_object( debug_obj );
260 /* resume a delayed debug event already in the queue */
261 static void resume_event( struct debug_obj *debug_obj, struct debug_event *event )
263 event->state = EVENT_QUEUED;
264 if (!event->sender->process->debug_event)
266 grab_object( debug_obj );
267 wake_up( &debug_obj->obj, 0 );
268 release_object( debug_obj );
272 /* delay a debug event already in the queue to be replayed when thread wakes up */
273 static void delay_event( struct debug_obj *debug_obj, struct debug_event *event )
275 event->state = EVENT_DELAYED;
276 if (event->sender->process->debug_event == event) event->sender->process->debug_event = NULL;
279 /* find the next event that we can send to the debugger */
280 static struct debug_event *find_event_to_send( struct debug_obj *debug_obj )
282 struct debug_event *event;
284 LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
286 if (event->state == EVENT_SENT) continue; /* already sent */
287 if (event->state == EVENT_DELAYED) continue; /* delayed until thread resumes */
288 if (event->sender->process->debug_event) continue; /* process busy with another one */
289 return event;
291 return NULL;
294 static void debug_event_dump( struct object *obj, int verbose )
296 struct debug_event *debug_event = (struct debug_event *)obj;
297 assert( obj->ops == &debug_event_ops );
298 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
299 debug_event->sender, debug_event->data.code, debug_event->state );
302 static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry )
304 struct debug_event *debug_event = (struct debug_event *)obj;
305 assert( obj->ops == &debug_event_ops );
306 return debug_event->state == EVENT_CONTINUED;
309 static void debug_event_destroy( struct object *obj )
311 struct debug_event *event = (struct debug_event *)obj;
312 assert( obj->ops == &debug_event_ops );
314 if (event->file) release_object( event->file );
315 release_object( event->sender );
318 static void debug_obj_dump( struct object *obj, int verbose )
320 struct debug_obj *debug_obj = (struct debug_obj *)obj;
321 assert( obj->ops == &debug_obj_ops );
322 fprintf( stderr, "Debug context head=%p tail=%p\n",
323 debug_obj->event_queue.next, debug_obj->event_queue.prev );
326 static int debug_obj_signaled( struct object *obj, struct wait_queue_entry *entry )
328 struct debug_obj *debug_obj = (struct debug_obj *)obj;
329 assert( obj->ops == &debug_obj_ops );
330 return find_event_to_send( debug_obj ) != NULL;
333 static void debug_obj_destroy( struct object *obj )
335 struct list *ptr;
336 struct debug_obj *debug_obj = (struct debug_obj *)obj;
337 assert( obj->ops == &debug_obj_ops );
339 detach_debugged_processes( debug_obj,
340 (debug_obj->flags & DEBUG_KILL_ON_CLOSE) ? STATUS_DEBUGGER_INACTIVE : 0 );
342 /* free all pending events */
343 while ((ptr = list_head( &debug_obj->event_queue )))
344 unlink_event( debug_obj, LIST_ENTRY( ptr, struct debug_event, entry ));
347 struct debug_obj *get_debug_obj( struct process *process, obj_handle_t handle, unsigned int access )
349 return (struct debug_obj *)get_handle_obj( process, handle, access, &debug_obj_ops );
352 static struct debug_obj *create_debug_obj( struct object *root, const struct unicode_str *name,
353 unsigned int attr, unsigned int flags,
354 const struct security_descriptor *sd )
356 struct debug_obj *debug_obj;
358 if ((debug_obj = create_named_object( root, &debug_obj_ops, name, attr, sd )))
360 if (get_error() != STATUS_OBJECT_NAME_EXISTS)
362 debug_obj->flags = flags;
363 list_init( &debug_obj->event_queue );
366 return debug_obj;
369 /* continue a debug event */
370 static int continue_debug_event( struct debug_obj *debug_obj, struct process *process,
371 struct thread *thread, int status )
373 if (process->debug_obj == debug_obj && thread->process == process)
375 struct debug_event *event;
377 if (status == DBG_REPLY_LATER)
379 /* if thread is suspended, delay all its events and resume process
380 * if not, reset the event for immediate replay */
381 LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
383 if (event->sender != thread) continue;
384 if (thread->suspend)
386 delay_event( debug_obj, event );
387 resume_process( process );
389 else if (event->state == EVENT_SENT)
391 assert( event->sender->process->debug_event == event );
392 event->sender->process->debug_event = NULL;
393 resume_event( debug_obj, event );
394 return 1;
397 return 1;
400 /* find the event in the queue */
401 LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
403 if (event->state != EVENT_SENT) continue;
404 if (event->sender == thread)
406 assert( event->sender->process->debug_event == event );
407 event->status = status;
408 event->state = EVENT_CONTINUED;
409 wake_up( &event->obj, 0 );
410 unlink_event( debug_obj, event );
411 resume_process( process );
412 return 1;
416 /* not debugging this process, or no such event */
417 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
418 return 0;
421 /* alloc a debug event for a debugger */
422 static struct debug_event *alloc_debug_event( struct thread *thread, int code, const void *arg )
424 struct debug_event *event;
426 assert( code >= DbgCreateThreadStateChange && code <= DbgUnloadDllStateChange );
428 /* build the event */
429 if (!(event = alloc_object( &debug_event_ops ))) return NULL;
430 event->state = EVENT_QUEUED;
431 event->sender = (struct thread *)grab_object( thread );
432 event->file = NULL;
433 memset( &event->data, 0, sizeof(event->data) );
434 fill_debug_event[code - DbgCreateThreadStateChange]( event, arg );
435 event->data.code = code;
436 return event;
439 /* generate a debug event from inside the server and queue it */
440 void generate_debug_event( struct thread *thread, int code, const void *arg )
442 struct debug_obj *debug_obj = thread->process->debug_obj;
444 if (debug_obj)
446 struct debug_event *event = alloc_debug_event( thread, code, arg );
447 if (event)
449 link_event( debug_obj, event );
450 suspend_process( thread->process );
451 release_object( event );
453 clear_error(); /* ignore errors */
457 void resume_delayed_debug_events( struct thread *thread )
459 struct debug_obj *debug_obj = thread->process->debug_obj;
460 struct debug_event *event;
462 if (debug_obj)
464 LIST_FOR_EACH_ENTRY( event, &debug_obj->event_queue, struct debug_event, entry )
466 if (event->sender != thread) continue;
467 if (event->state != EVENT_DELAYED) continue;
468 resume_event( debug_obj, event );
469 suspend_process( thread->process );
474 /* attach a process to a debugger thread and suspend it */
475 static int debugger_attach( struct process *process, struct thread *debugger, struct debug_obj *debug_obj )
477 if (debugger->process == process) goto error;
478 if (!is_process_init_done( process )) goto error; /* still starting up */
479 if (list_empty( &process->thread_list )) goto error; /* no thread running in the process */
480 /* don't let a debugger debug its console... won't work */
481 if (debugger->process->console)
483 struct thread *renderer = console_get_renderer(debugger->process->console);
484 if (renderer && renderer->process == process) goto error;
487 suspend_process( process );
489 if (!set_process_debug_flag( process, 1 ))
491 resume_process( process );
492 return 0;
494 process->debug_obj = debug_obj;
495 process->debug_children = 0;
496 generate_startup_debug_events( process );
497 resume_process( process );
498 return 1;
500 error:
501 set_error( STATUS_ACCESS_DENIED );
502 return 0;
506 /* detach a process from a debugger thread (and resume it) */
507 void debugger_detach( struct process *process, struct debug_obj *debug_obj )
509 struct debug_event *event, *next;
511 suspend_process( process );
513 /* send continue indication for all events */
514 LIST_FOR_EACH_ENTRY_SAFE( event, next, &debug_obj->event_queue, struct debug_event, entry )
516 if (event->sender->process != process) continue;
518 assert( event->state != EVENT_CONTINUED );
519 event->status = DBG_CONTINUE;
520 event->state = EVENT_CONTINUED;
521 wake_up( &event->obj, 0 );
522 unlink_event( debug_obj, event );
523 /* from queued debug event */
524 resume_process( process );
527 /* remove relationships between process and its debugger */
528 process->debug_obj = NULL;
529 if (!set_process_debug_flag( process, 0 )) clear_error(); /* ignore error */
531 resume_process( process );
534 /* create a debug object */
535 DECL_HANDLER(create_debug_obj)
537 struct debug_obj *debug_obj;
538 struct unicode_str name;
539 struct object *root;
540 const struct security_descriptor *sd;
541 const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
543 if (!objattr) return;
544 if ((debug_obj = create_debug_obj( root, &name, objattr->attributes, req->flags, sd )))
546 if (get_error() == STATUS_OBJECT_NAME_EXISTS)
547 reply->handle = alloc_handle( current->process, debug_obj, req->access, objattr->attributes );
548 else
549 reply->handle = alloc_handle_no_access_check( current->process, debug_obj,
550 req->access, objattr->attributes );
551 release_object( debug_obj );
553 if (root) release_object( root );
556 /* Wait for a debug event */
557 DECL_HANDLER(wait_debug_event)
559 struct debug_obj *debug_obj;
560 struct debug_event *event;
562 if (!(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_READ_EVENT ))) return;
564 if ((event = find_event_to_send( debug_obj )))
566 event->state = EVENT_SENT;
567 event->sender->process->debug_event = event;
568 reply->pid = get_process_id( event->sender->process );
569 reply->tid = get_thread_id( event->sender );
570 alloc_event_handles( event, current->process );
571 set_reply_data( &event->data, min( get_reply_max_size(), sizeof(event->data) ));
573 else
575 int state = list_empty( &debug_obj->event_queue ) ? DbgIdle : DbgReplyPending;
576 set_reply_data( &state, min( get_reply_max_size(), sizeof(state) ));
578 release_object( debug_obj );
581 /* Continue a debug event */
582 DECL_HANDLER(continue_debug_event)
584 struct debug_obj *debug_obj;
585 struct process *process;
587 if (req->status != DBG_EXCEPTION_NOT_HANDLED &&
588 req->status != DBG_EXCEPTION_HANDLED &&
589 req->status != DBG_CONTINUE &&
590 req->status != DBG_REPLY_LATER)
592 set_error( STATUS_INVALID_PARAMETER );
593 return;
596 if (!(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_READ_EVENT ))) return;
598 if ((process = get_process_from_id( req->pid )))
600 struct thread *thread = get_thread_from_id( req->tid );
601 if (thread)
603 continue_debug_event( debug_obj, process, thread, req->status );
604 release_object( thread );
606 release_object( process );
608 release_object( debug_obj );
611 /* start or stop debugging an existing process */
612 DECL_HANDLER(debug_process)
614 struct debug_obj *debug_obj;
615 struct process *process = get_process_from_handle( req->handle,
616 PROCESS_VM_READ | PROCESS_VM_WRITE | PROCESS_SUSPEND_RESUME );
618 if (!process) return;
619 if ((debug_obj = get_debug_obj( current->process, req->debug, DEBUG_PROCESS_ASSIGN )))
621 if (req->attach)
623 if (!process->debug_obj) debugger_attach( process, current, debug_obj );
624 else set_error( STATUS_ACCESS_DENIED );
626 else
628 if (process->debug_obj == debug_obj) debugger_detach( process, debug_obj );
629 else set_error( STATUS_ACCESS_DENIED );
631 release_object( debug_obj );
633 release_object( process );
636 /* queue an exception event */
637 DECL_HANDLER(queue_exception_event)
639 struct debug_obj *debug_obj = current->process->debug_obj;
641 reply->handle = 0;
642 if (debug_obj)
644 debug_event_t data;
645 struct debug_event *event;
646 struct thread *thread = current;
648 if ((req->len % sizeof(client_ptr_t)) != 0 ||
649 req->len > get_req_data_size() ||
650 req->len > EXCEPTION_MAXIMUM_PARAMETERS * sizeof(client_ptr_t))
652 set_error( STATUS_INVALID_PARAMETER );
653 return;
655 memset( &data, 0, sizeof(data) );
656 data.exception.first = req->first;
657 data.exception.exc_code = req->code;
658 data.exception.flags = req->flags;
659 data.exception.record = req->record;
660 data.exception.address = req->address;
661 data.exception.nb_params = req->len / sizeof(client_ptr_t);
662 memcpy( data.exception.params, get_req_data(), req->len );
664 if ((event = alloc_debug_event( thread, DbgExceptionStateChange, &data )))
666 if ((reply->handle = alloc_handle( thread->process, event, SYNCHRONIZE, 0 )))
668 link_event( debug_obj, event );
669 suspend_process( thread->process );
671 release_object( event );
676 /* retrieve the status of an exception event */
677 DECL_HANDLER(get_exception_status)
679 struct debug_event *event;
681 if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
682 0, &debug_event_ops )))
684 close_handle( current->process, req->handle );
685 set_error( event->state == EVENT_CONTINUED ? event->status : STATUS_PENDING );
686 release_object( event );
690 /* set debug object information */
691 DECL_HANDLER(set_debug_obj_info)
693 struct debug_obj *debug_obj;
695 if (!(debug_obj = get_debug_obj( current->process, req->debug, DEBUG_SET_INFORMATION ))) return;
696 debug_obj->flags = req->flags;
697 release_object( debug_obj );