Various cosmetic changes.
[wine/wine64.git] / server / debugger.c
blob60e406bc956de0f83635ba316c1112aaa3c54545
1 /*
2 * Server-side debugger functions
4 * Copyright (C) 1999 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <signal.h>
9 #include <string.h>
10 #include <stdio.h>
12 #include "winbase.h"
14 #include "handle.h"
15 #include "process.h"
16 #include "thread.h"
17 #include "request.h"
18 #include "console.h"
20 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
22 /* debug event */
23 struct debug_event
25 struct object obj; /* object header */
26 struct debug_event *next; /* event queue */
27 struct debug_event *prev;
28 struct thread *sender; /* thread which sent this event */
29 struct thread *debugger; /* debugger thread receiving the event */
30 enum debug_event_state state; /* event state */
31 int status; /* continuation status */
32 debug_event_t data; /* event data */
33 CONTEXT context; /* register context */
36 /* debug context */
37 struct debug_ctx
39 struct object obj; /* object header */
40 struct debug_event *event_head; /* head of pending events queue */
41 struct debug_event *event_tail; /* tail of pending events queue */
42 int kill_on_exit;/* kill debuggees on debugger exit ? */
46 static void debug_event_dump( struct object *obj, int verbose );
47 static int debug_event_signaled( struct object *obj, struct thread *thread );
48 static void debug_event_destroy( struct object *obj );
50 static const struct object_ops debug_event_ops =
52 sizeof(struct debug_event), /* size */
53 debug_event_dump, /* dump */
54 add_queue, /* add_queue */
55 remove_queue, /* remove_queue */
56 debug_event_signaled, /* signaled */
57 no_satisfied, /* satisfied */
58 NULL, /* get_poll_events */
59 NULL, /* poll_event */
60 no_get_fd, /* get_fd */
61 no_flush, /* flush */
62 no_get_file_info, /* get_file_info */
63 NULL, /* queue_async */
64 debug_event_destroy /* destroy */
67 static void debug_ctx_dump( struct object *obj, int verbose );
68 static int debug_ctx_signaled( struct object *obj, struct thread *thread );
69 static void debug_ctx_destroy( struct object *obj );
71 static const struct object_ops debug_ctx_ops =
73 sizeof(struct debug_ctx), /* size */
74 debug_ctx_dump, /* dump */
75 add_queue, /* add_queue */
76 remove_queue, /* remove_queue */
77 debug_ctx_signaled, /* signaled */
78 no_satisfied, /* satisfied */
79 NULL, /* get_poll_events */
80 NULL, /* poll_event */
81 no_get_fd, /* get_fd */
82 no_flush, /* flush */
83 no_get_file_info, /* get_file_info */
84 NULL, /* queue_async */
85 debug_ctx_destroy /* destroy */
89 /* routines to build an event according to its type */
91 static int fill_exception_event( struct debug_event *event, void *arg )
93 memcpy( &event->data.info.exception, arg, sizeof(event->data.info.exception) );
94 return 1;
97 static int fill_create_thread_event( struct debug_event *event, void *arg )
99 struct process *debugger = event->debugger->process;
100 struct thread *thread = event->sender;
101 handle_t handle;
103 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
104 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE ))) return 0;
105 event->data.info.create_thread.handle = handle;
106 event->data.info.create_thread.teb = thread->teb;
107 event->data.info.create_thread.start = arg;
108 return 1;
111 static int fill_create_process_event( struct debug_event *event, void *arg )
113 struct process *debugger = event->debugger->process;
114 struct thread *thread = event->sender;
115 struct process *process = thread->process;
116 handle_t handle;
118 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
119 if (!(handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, FALSE ))) return 0;
120 event->data.info.create_process.process = handle;
122 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
123 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )))
125 close_handle( debugger, event->data.info.create_process.process, NULL );
126 return 0;
128 event->data.info.create_process.thread = handle;
130 handle = 0;
131 if (process->exe.file &&
132 /* the doc says write access too, but this doesn't seem a good idea */
133 !(handle = alloc_handle( debugger, process->exe.file, GENERIC_READ, FALSE )))
135 close_handle( debugger, event->data.info.create_process.process, NULL );
136 close_handle( debugger, event->data.info.create_process.thread, NULL );
137 return 0;
139 event->data.info.create_process.file = handle;
140 event->data.info.create_process.teb = thread->teb;
141 event->data.info.create_process.base = process->exe.base;
142 event->data.info.create_process.start = arg;
143 event->data.info.create_process.dbg_offset = process->exe.dbg_offset;
144 event->data.info.create_process.dbg_size = process->exe.dbg_size;
145 event->data.info.create_process.name = process->exe.name;
146 event->data.info.create_process.unicode = 0;
147 return 1;
150 static int fill_exit_thread_event( struct debug_event *event, void *arg )
152 struct thread *thread = arg;
153 event->data.info.exit.exit_code = thread->exit_code;
154 return 1;
157 static int fill_exit_process_event( struct debug_event *event, void *arg )
159 struct process *process = arg;
160 event->data.info.exit.exit_code = process->exit_code;
161 return 1;
164 static int fill_load_dll_event( struct debug_event *event, void *arg )
166 struct process *debugger = event->debugger->process;
167 struct process_dll *dll = arg;
168 handle_t handle = 0;
170 if (dll->file && !(handle = alloc_handle( debugger, dll->file, GENERIC_READ, FALSE )))
171 return 0;
172 event->data.info.load_dll.handle = handle;
173 event->data.info.load_dll.base = dll->base;
174 event->data.info.load_dll.dbg_offset = dll->dbg_offset;
175 event->data.info.load_dll.dbg_size = dll->dbg_size;
176 event->data.info.load_dll.name = dll->name;
177 event->data.info.load_dll.unicode = 0;
178 return 1;
181 static int fill_unload_dll_event( struct debug_event *event, void *arg )
183 event->data.info.unload_dll.base = arg;
184 return 1;
187 static int fill_output_debug_string_event( struct debug_event *event, void *arg )
189 struct debug_event_output_string *data = arg;
190 event->data.info.output_string = *data;
191 return 1;
194 typedef int (*fill_event_func)( struct debug_event *event, void *arg );
196 #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT /* RIP_EVENT not supported */
198 static const fill_event_func fill_debug_event[NB_DEBUG_EVENTS] =
200 fill_exception_event, /* EXCEPTION_DEBUG_EVENT */
201 fill_create_thread_event, /* CREATE_THREAD_DEBUG_EVENT */
202 fill_create_process_event, /* CREATE_PROCESS_DEBUG_EVENT */
203 fill_exit_thread_event, /* EXIT_THREAD_DEBUG_EVENT */
204 fill_exit_process_event, /* EXIT_PROCESS_DEBUG_EVENT */
205 fill_load_dll_event, /* LOAD_DLL_DEBUG_EVENT */
206 fill_unload_dll_event, /* UNLOAD_DLL_DEBUG_EVENT */
207 fill_output_debug_string_event /* OUTPUT_DEBUG_STRING_EVENT */
211 /* unlink the first event from the queue */
212 static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
214 if (event->prev) event->prev->next = event->next;
215 else debug_ctx->event_head = event->next;
216 if (event->next) event->next->prev = event->prev;
217 else debug_ctx->event_tail = event->prev;
218 event->next = event->prev = NULL;
219 if (event->sender->debug_event == event) event->sender->debug_event = NULL;
220 release_object( event );
223 /* link an event at the end of the queue */
224 static void link_event( struct debug_event *event )
226 struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
228 assert( debug_ctx );
229 grab_object( event );
230 event->next = NULL;
231 event->prev = debug_ctx->event_tail;
232 debug_ctx->event_tail = event;
233 if (event->prev) event->prev->next = event;
234 else debug_ctx->event_head = event;
235 if (!event->sender->debug_event) wake_up( &debug_ctx->obj, 0 );
238 /* find the next event that we can send to the debugger */
239 static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
241 struct debug_event *event;
242 for (event = debug_ctx->event_head; event; event = event->next)
244 if (event->state == EVENT_SENT) continue; /* already sent */
245 if (event->sender->debug_event) continue; /* thread busy with another one */
246 break;
248 return event;
251 static void debug_event_dump( struct object *obj, int verbose )
253 struct debug_event *debug_event = (struct debug_event *)obj;
254 assert( obj->ops == &debug_event_ops );
255 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
256 debug_event->sender, debug_event->data.code, debug_event->state );
259 static int debug_event_signaled( struct object *obj, struct thread *thread )
261 struct debug_event *debug_event = (struct debug_event *)obj;
262 assert( obj->ops == &debug_event_ops );
263 return debug_event->state == EVENT_CONTINUED;
266 static void debug_event_destroy( struct object *obj )
268 struct debug_event *event = (struct debug_event *)obj;
269 assert( obj->ops == &debug_event_ops );
271 /* cannot still be in the queue */
272 assert( !event->next );
273 assert( !event->prev );
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 );
284 break;
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 );
290 break;
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 );
294 break;
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_head, debug_ctx->event_tail );
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 )
319 struct debug_event *event;
320 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
321 assert( obj->ops == &debug_ctx_ops );
323 /* free all pending events */
324 while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
327 /* continue a debug event */
328 static int continue_debug_event( struct process *process, struct thread *thread, int status )
330 struct debug_event *event;
331 struct debug_ctx *debug_ctx = current->debug_ctx;
333 if (!debug_ctx || process->debugger != current || thread->process != process) goto error;
335 /* find the event in the queue */
336 for (event = debug_ctx->event_head; event; event = event->next)
338 if (event->state != EVENT_SENT) continue;
339 if (event->sender == thread) break;
341 if (!event) goto error;
343 assert( event->sender->debug_event == event );
345 event->status = status;
346 event->state = EVENT_CONTINUED;
347 wake_up( &event->obj, 0 );
349 unlink_event( debug_ctx, event );
350 resume_process( process );
351 return 1;
352 error:
353 /* not debugging this process, or no such event */
354 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
355 return 0;
358 /* alloc a debug event for a debugger */
359 static struct debug_event *alloc_debug_event( struct thread *thread, int code,
360 void *arg, const CONTEXT *context )
362 struct thread *debugger = thread->process->debugger;
363 struct debug_event *event;
365 assert( code > 0 && code <= NB_DEBUG_EVENTS );
366 /* cannot queue a debug event for myself */
367 assert( debugger->process != thread->process );
369 /* build the event */
370 if (!(event = alloc_object( &debug_event_ops, -1 ))) return NULL;
371 event->next = NULL;
372 event->prev = NULL;
373 event->state = EVENT_QUEUED;
374 event->sender = (struct thread *)grab_object( thread );
375 event->debugger = (struct thread *)grab_object( debugger );
376 event->data.code = code;
378 if (!fill_debug_event[code-1]( event, arg ))
380 event->data.code = -1; /* make sure we don't attempt to close handles */
381 release_object( event );
382 return NULL;
384 if (context)
386 memcpy( &event->context, context, sizeof(event->context) );
387 thread->context = &event->context;
389 return event;
392 /* generate a debug event from inside the server and queue it */
393 void generate_debug_event( struct thread *thread, int code, void *arg )
395 if (thread->process->debugger)
397 struct debug_event *event = alloc_debug_event( thread, code, arg, NULL );
398 if (event)
400 link_event( event );
401 suspend_process( thread->process );
402 release_object( event );
407 /* attach a process to a debugger thread and suspend it */
408 static int debugger_attach( struct process *process, struct thread *debugger )
410 struct thread *thread;
412 if (process->debugger) goto error; /* already being debugged */
413 if (process->init_event) goto error; /* still starting up */
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)
421 goto error;
423 suspend_process( process );
425 /* we must have been able to attach all threads */
426 for (thread = process->thread_list; thread; thread = thread->proc_next)
428 if (!thread->attached)
430 resume_process( process );
431 goto error;
435 if (set_process_debugger( process, debugger )) return 1;
436 resume_process( process );
437 return 0;
439 error:
440 set_error( STATUS_ACCESS_DENIED );
441 return 0;
445 /* detach a process from a debugger thread (and resume it ?) */
446 int debugger_detach( struct process *process, struct thread *debugger )
448 struct thread *thread;
449 struct debug_event *event;
450 struct debug_ctx *debug_ctx;
452 if (!process->debugger || process->debugger != debugger)
453 goto error; /* not currently debugged, or debugged by another debugger */
454 if (!debugger->debug_ctx ) goto error; /* should be a debugger */
455 /* init should be done, otherwise wouldn't be attached */
456 assert(!process->init_event);
458 suspend_process( process );
459 /* send continue indication for all events */
460 debug_ctx = debugger->debug_ctx;
462 /* find the event in the queue
463 * FIXME: could loop on process' threads and look the debug_event field */
464 for (event = debug_ctx->event_head; event; event = event->next)
466 if (event->state != EVENT_QUEUED) continue;
468 if (event->sender->process == process)
470 assert( event->sender->debug_event == event );
471 event->status = DBG_CONTINUE;
472 event->state = EVENT_CONTINUED;
473 wake_up( &event->obj, 0 );
474 unlink_event( debug_ctx, event );
475 /* from queued debug event */
476 resume_process( process );
480 /* remove relationships between process and its debugger */
481 process->debugger = NULL;
482 release_object( debugger->debug_ctx );
483 debugger->debug_ctx = NULL;
485 /* now detach all the threads */
486 for (thread = process->thread_list; thread; thread = thread->proc_next)
488 if (thread->attached)
490 detach_thread( thread, 0 );
494 /* from this function */
495 resume_process( process );
496 return 0;
498 error:
499 set_error( STATUS_ACCESS_DENIED );
500 return 0;
503 /* generate all startup events of a given process */
504 void generate_startup_debug_events( struct process *process, void *entry )
506 struct process_dll *dll;
507 struct thread *thread = process->thread_list;
509 /* generate creation events */
510 generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT, entry );
511 while ((thread = thread->proc_next))
512 generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
514 /* generate dll events (in loading order, i.e. reverse list order) */
515 dll = &process->exe;
516 while (dll->next) dll = dll->next;
517 while (dll != &process->exe)
519 generate_debug_event( process->thread_list, LOAD_DLL_DEBUG_EVENT, dll );
520 dll = dll->prev;
524 /* set the debugger of a given process */
525 int set_process_debugger( struct process *process, struct thread *debugger )
527 struct debug_ctx *debug_ctx;
529 assert( !process->debugger );
531 if (!debugger->debug_ctx) /* need to allocate a context */
533 if (!(debug_ctx = alloc_object( &debug_ctx_ops, -1 ))) return 0;
534 debug_ctx->event_head = NULL;
535 debug_ctx->event_tail = NULL;
536 debug_ctx->kill_on_exit = 1;
537 debugger->debug_ctx = debug_ctx;
539 process->debugger = debugger;
540 return 1;
543 /* a thread is exiting */
544 void debug_exit_thread( struct thread *thread )
546 if (thread->debug_ctx) /* this thread is a debugger */
548 if (thread->debug_ctx->kill_on_exit)
550 /* kill all debugged processes */
551 kill_debugged_processes( thread, thread->exit_code );
553 else
555 detach_debugged_processes( thread );
557 release_object( thread->debug_ctx );
558 thread->debug_ctx = NULL;
562 /* Wait for a debug event */
563 DECL_HANDLER(wait_debug_event)
565 struct debug_ctx *debug_ctx = current->debug_ctx;
566 struct debug_event *event;
568 if (!debug_ctx) /* current thread is not a debugger */
570 set_error( STATUS_INVALID_HANDLE );
571 return;
573 reply->wait = 0;
574 if ((event = find_event_to_send( debug_ctx )))
576 size_t size = get_reply_max_size();
577 event->state = EVENT_SENT;
578 event->sender->debug_event = event;
579 reply->pid = event->sender->process;
580 reply->tid = event->sender;
581 if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
582 set_reply_data( &event->data, size );
584 else /* no event ready */
586 reply->pid = 0;
587 reply->tid = 0;
588 if (req->get_handle)
589 reply->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, FALSE );
593 /* Continue a debug event */
594 DECL_HANDLER(continue_debug_event)
596 struct process *process = get_process_from_id( req->pid );
597 if (process)
599 struct thread *thread = get_thread_from_id( req->tid );
600 if (thread)
602 continue_debug_event( process, thread, req->status );
603 release_object( thread );
605 release_object( process );
609 /* Start debugging an existing process */
610 DECL_HANDLER(debug_process)
612 struct process *process = get_process_from_id( req->pid );
613 if (!process) return;
615 if (!req->attach)
617 debugger_detach( process, current );
619 else if (debugger_attach( process, current ))
621 struct debug_event_exception data;
623 generate_startup_debug_events( process, NULL );
624 resume_process( process );
626 data.record.ExceptionCode = EXCEPTION_BREAKPOINT;
627 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
628 data.record.ExceptionRecord = NULL;
629 data.record.ExceptionAddress = get_thread_ip( process->thread_list );
630 data.record.NumberParameters = 0;
631 data.first = 1;
632 generate_debug_event( process->thread_list, EXCEPTION_DEBUG_EVENT, &data );
634 release_object( process );
637 /* queue an exception event */
638 DECL_HANDLER(queue_exception_event)
640 reply->handle = 0;
641 if (current->process->debugger)
643 struct debug_event_exception data;
644 struct debug_event *event;
645 const CONTEXT *context = get_req_data();
646 EXCEPTION_RECORD *rec = (EXCEPTION_RECORD *)(context + 1);
648 if (get_req_data_size() < sizeof(*rec) + sizeof(*context))
650 set_error( STATUS_INVALID_PARAMETER );
651 return;
653 data.record = *rec;
654 data.first = req->first;
655 if ((event = alloc_debug_event( current, EXCEPTION_DEBUG_EVENT, &data, context )))
657 if ((reply->handle = alloc_handle( current->process, event, SYNCHRONIZE, FALSE )))
659 link_event( event );
660 suspend_process( current->process );
662 release_object( event );
667 /* retrieve the status of an exception event */
668 DECL_HANDLER(get_exception_status)
670 struct debug_event *event;
672 reply->status = 0;
673 if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
674 0, &debug_event_ops )))
676 if (event->state == EVENT_CONTINUED)
678 reply->status = event->status;
679 if (current->context == &event->context)
681 size_t size = min( sizeof(CONTEXT), get_reply_max_size() );
682 set_reply_data( &event->context, size );
683 current->context = NULL;
686 else set_error( STATUS_PENDING );
687 release_object( event );
691 /* send an output string to the debugger */
692 DECL_HANDLER(output_debug_string)
694 struct debug_event_output_string data;
696 data.string = req->string;
697 data.unicode = req->unicode;
698 data.length = req->length;
699 generate_debug_event( current, OUTPUT_DEBUG_STRING_EVENT, &data );
702 /* simulate a breakpoint in a process */
703 DECL_HANDLER(debug_break)
705 struct process *process;
707 reply->self = 0;
708 if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION /*FIXME*/ )))
709 return;
710 if (process != current->process)
712 /* find a suitable thread to signal */
713 struct thread *thread;
714 for (thread = process->thread_list; thread; thread = thread->proc_next)
716 if (thread->unix_pid)
718 kill( thread->unix_pid, SIGTRAP );
719 break;
722 if (!thread) set_error( STATUS_ACCESS_DENIED );
724 else reply->self = 1;
725 release_object( process );
728 /* set debugger kill on exit flag */
729 DECL_HANDLER(set_debugger_kill_on_exit)
731 if (!current->debug_ctx)
733 set_error( STATUS_ACCESS_DENIED );
734 return;
736 current->debug_ctx->kill_on_exit = req->kill_on_exit;