On Win9x "hi\n" gives us "hi\r\n".
[wine/wine-kai.git] / server / debugger.c
blobba08bd2fde9090ed8cdfe58bc0061fa8a961d2e1
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <stdio.h>
29 #include "winbase.h"
31 #include "handle.h"
32 #include "process.h"
33 #include "thread.h"
34 #include "request.h"
35 #include "console.h"
37 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
39 /* debug event */
40 struct debug_event
42 struct object obj; /* object header */
43 struct debug_event *next; /* event queue */
44 struct debug_event *prev;
45 struct thread *sender; /* thread which sent this event */
46 struct thread *debugger; /* debugger thread receiving the event */
47 enum debug_event_state state; /* event state */
48 int status; /* continuation status */
49 debug_event_t data; /* event data */
50 CONTEXT context; /* register context */
53 /* debug context */
54 struct debug_ctx
56 struct object obj; /* object header */
57 struct debug_event *event_head; /* head of pending events queue */
58 struct debug_event *event_tail; /* tail of 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 NULL, /* get_poll_events */
76 NULL, /* poll_event */
77 no_get_fd, /* get_fd */
78 no_flush, /* flush */
79 no_get_file_info, /* get_file_info */
80 NULL, /* queue_async */
81 debug_event_destroy /* destroy */
84 static void debug_ctx_dump( struct object *obj, int verbose );
85 static int debug_ctx_signaled( struct object *obj, struct thread *thread );
86 static void debug_ctx_destroy( struct object *obj );
88 static const struct object_ops debug_ctx_ops =
90 sizeof(struct debug_ctx), /* size */
91 debug_ctx_dump, /* dump */
92 add_queue, /* add_queue */
93 remove_queue, /* remove_queue */
94 debug_ctx_signaled, /* signaled */
95 no_satisfied, /* satisfied */
96 NULL, /* get_poll_events */
97 NULL, /* poll_event */
98 no_get_fd, /* get_fd */
99 no_flush, /* flush */
100 no_get_file_info, /* get_file_info */
101 NULL, /* queue_async */
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) );
111 return 1;
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;
118 obj_handle_t handle;
120 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
121 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE ))) 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;
125 return 1;
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;
133 obj_handle_t handle;
135 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
136 if (!(handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, FALSE ))) 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, FALSE )))
142 close_handle( debugger, event->data.info.create_process.process, NULL );
143 return 0;
145 event->data.info.create_process.thread = handle;
147 handle = 0;
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, FALSE )))
152 close_handle( debugger, event->data.info.create_process.process, NULL );
153 close_handle( debugger, event->data.info.create_process.thread, NULL );
154 return 0;
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 = 0;
164 return 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;
171 return 1;
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;
178 return 1;
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, FALSE )))
188 return 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 = 0;
195 return 1;
198 static int fill_unload_dll_event( struct debug_event *event, void *arg )
200 event->data.info.unload_dll.base = arg;
201 return 1;
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;
208 return 1;
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 if (event->prev) event->prev->next = event->next;
232 else debug_ctx->event_head = event->next;
233 if (event->next) event->next->prev = event->prev;
234 else debug_ctx->event_tail = event->prev;
235 event->next = event->prev = NULL;
236 if (event->sender->debug_event == event) event->sender->debug_event = NULL;
237 release_object( event );
240 /* link an event at the end of the queue */
241 static void link_event( struct debug_event *event )
243 struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
245 assert( debug_ctx );
246 grab_object( event );
247 event->next = NULL;
248 event->prev = debug_ctx->event_tail;
249 debug_ctx->event_tail = event;
250 if (event->prev) event->prev->next = event;
251 else debug_ctx->event_head = event;
252 if (!event->sender->debug_event) wake_up( &debug_ctx->obj, 0 );
255 /* find the next event that we can send to the debugger */
256 static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
258 struct debug_event *event;
259 for (event = debug_ctx->event_head; event; event = event->next)
261 if (event->state == EVENT_SENT) continue; /* already sent */
262 if (event->sender->debug_event) continue; /* thread busy with another one */
263 break;
265 return event;
268 static void debug_event_dump( struct object *obj, int verbose )
270 struct debug_event *debug_event = (struct debug_event *)obj;
271 assert( obj->ops == &debug_event_ops );
272 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
273 debug_event->sender, debug_event->data.code, debug_event->state );
276 static int debug_event_signaled( struct object *obj, struct thread *thread )
278 struct debug_event *debug_event = (struct debug_event *)obj;
279 assert( obj->ops == &debug_event_ops );
280 return debug_event->state == EVENT_CONTINUED;
283 static void debug_event_destroy( struct object *obj )
285 struct debug_event *event = (struct debug_event *)obj;
286 assert( obj->ops == &debug_event_ops );
288 /* cannot still be in the queue */
289 assert( !event->next );
290 assert( !event->prev );
292 /* If the event has been sent already, the handles are now under the */
293 /* responsibility of the debugger process, so we don't touch them */
294 if (event->state == EVENT_QUEUED)
296 struct process *debugger = event->debugger->process;
297 switch(event->data.code)
299 case CREATE_THREAD_DEBUG_EVENT:
300 close_handle( debugger, event->data.info.create_thread.handle, NULL );
301 break;
302 case CREATE_PROCESS_DEBUG_EVENT:
303 if (event->data.info.create_process.file)
304 close_handle( debugger, event->data.info.create_process.file, NULL );
305 close_handle( debugger, event->data.info.create_process.thread, NULL );
306 close_handle( debugger, event->data.info.create_process.process, NULL );
307 break;
308 case LOAD_DLL_DEBUG_EVENT:
309 if (event->data.info.load_dll.handle)
310 close_handle( debugger, event->data.info.load_dll.handle, NULL );
311 break;
314 if (event->sender->context == &event->context) event->sender->context = NULL;
315 release_object( event->sender );
316 release_object( event->debugger );
319 static void debug_ctx_dump( struct object *obj, int verbose )
321 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
322 assert( obj->ops == &debug_ctx_ops );
323 fprintf( stderr, "Debug context head=%p tail=%p\n",
324 debug_ctx->event_head, debug_ctx->event_tail );
327 static int debug_ctx_signaled( struct object *obj, struct thread *thread )
329 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
330 assert( obj->ops == &debug_ctx_ops );
331 return find_event_to_send( debug_ctx ) != NULL;
334 static void debug_ctx_destroy( struct object *obj )
336 struct debug_event *event;
337 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
338 assert( obj->ops == &debug_ctx_ops );
340 /* free all pending events */
341 while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
344 /* continue a debug event */
345 static int continue_debug_event( struct process *process, struct thread *thread, int status )
347 struct debug_event *event;
348 struct debug_ctx *debug_ctx = current->debug_ctx;
350 if (!debug_ctx || process->debugger != current || thread->process != process) goto error;
352 /* find the event in the queue */
353 for (event = debug_ctx->event_head; event; event = event->next)
355 if (event->state != EVENT_SENT) continue;
356 if (event->sender == thread) break;
358 if (!event) goto error;
360 assert( event->sender->debug_event == event );
362 event->status = status;
363 event->state = EVENT_CONTINUED;
364 wake_up( &event->obj, 0 );
366 unlink_event( debug_ctx, event );
367 resume_process( process );
368 return 1;
369 error:
370 /* not debugging this process, or no such event */
371 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
372 return 0;
375 /* alloc a debug event for a debugger */
376 static struct debug_event *alloc_debug_event( struct thread *thread, int code,
377 void *arg, const CONTEXT *context )
379 struct thread *debugger = thread->process->debugger;
380 struct debug_event *event;
382 assert( code > 0 && code <= NB_DEBUG_EVENTS );
383 /* cannot queue a debug event for myself */
384 assert( debugger->process != thread->process );
386 /* build the event */
387 if (!(event = alloc_object( &debug_event_ops, -1 ))) return NULL;
388 event->next = NULL;
389 event->prev = NULL;
390 event->state = EVENT_QUEUED;
391 event->sender = (struct thread *)grab_object( thread );
392 event->debugger = (struct thread *)grab_object( debugger );
393 event->data.code = code;
395 if (!fill_debug_event[code-1]( event, arg ))
397 event->data.code = -1; /* make sure we don't attempt to close handles */
398 release_object( event );
399 return NULL;
401 if (context)
403 memcpy( &event->context, context, sizeof(event->context) );
404 thread->context = &event->context;
406 return event;
409 /* generate a debug event from inside the server and queue it */
410 void generate_debug_event( struct thread *thread, int code, void *arg )
412 if (thread->process->debugger)
414 struct debug_event *event = alloc_debug_event( thread, code, arg, NULL );
415 if (event)
417 link_event( event );
418 suspend_process( thread->process );
419 release_object( event );
424 /* attach a process to a debugger thread and suspend it */
425 static int debugger_attach( struct process *process, struct thread *debugger )
427 struct thread *thread;
429 if (process->debugger) goto error; /* already being debugged */
430 if (!is_process_init_done( process )) goto error; /* still starting up */
431 if (!process->thread_list) goto error; /* no thread running in the process */
433 /* make sure we don't create a debugging loop */
434 for (thread = debugger; thread; thread = thread->process->debugger)
435 if (thread->process == process) goto error;
437 /* don't let a debugger debug its console... won't work */
438 if (debugger->process->console && debugger->process->console->renderer->process == process)
439 goto error;
441 suspend_process( process );
443 /* we must have been able to attach all threads */
444 if (!process->thread_list) goto error2;
445 for (thread = process->thread_list; thread; thread = thread->proc_next)
447 if (!thread->attached) goto error2;
450 if (set_process_debugger( process, debugger )) return 1;
451 resume_process( process );
452 return 0;
454 error2:
455 resume_process( process );
456 error:
457 set_error( STATUS_ACCESS_DENIED );
458 return 0;
462 /* detach a process from a debugger thread (and resume it ?) */
463 int debugger_detach( struct process *process, struct thread *debugger )
465 struct thread *thread;
466 struct debug_event *event;
467 struct debug_ctx *debug_ctx;
469 if (!process->debugger || process->debugger != debugger)
470 goto error; /* not currently debugged, or debugged by another debugger */
471 if (!debugger->debug_ctx ) goto error; /* should be a debugger */
472 /* init should be done, otherwise wouldn't be attached */
473 assert(is_process_init_done(process));
475 suspend_process( process );
476 /* send continue indication for all events */
477 debug_ctx = debugger->debug_ctx;
479 /* find the event in the queue
480 * FIXME: could loop on process' threads and look the debug_event field */
481 for (event = debug_ctx->event_head; event; event = event->next)
483 if (event->state != EVENT_QUEUED) continue;
485 if (event->sender->process == process)
487 assert( event->sender->debug_event == event );
488 event->status = DBG_CONTINUE;
489 event->state = EVENT_CONTINUED;
490 wake_up( &event->obj, 0 );
491 unlink_event( debug_ctx, event );
492 /* from queued debug event */
493 resume_process( process );
497 /* remove relationships between process and its debugger */
498 process->debugger = NULL;
499 release_object( debugger->debug_ctx );
500 debugger->debug_ctx = NULL;
502 /* now detach all the threads */
503 for (thread = process->thread_list; thread; thread = thread->proc_next)
505 if (thread->attached)
507 detach_thread( thread, 0 );
511 /* from this function */
512 resume_process( process );
513 return 0;
515 error:
516 set_error( STATUS_ACCESS_DENIED );
517 return 0;
520 /* generate all startup events of a given process */
521 void generate_startup_debug_events( struct process *process, void *entry )
523 struct process_dll *dll;
524 struct thread *thread = process->thread_list;
526 /* generate creation events */
527 generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT, entry );
528 while ((thread = thread->proc_next))
529 generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
531 /* generate dll events (in loading order, i.e. reverse list order) */
532 dll = &process->exe;
533 while (dll->next) dll = dll->next;
534 while (dll != &process->exe)
536 generate_debug_event( process->thread_list, LOAD_DLL_DEBUG_EVENT, dll );
537 dll = dll->prev;
541 /* set the debugger of a given process */
542 int set_process_debugger( struct process *process, struct thread *debugger )
544 struct debug_ctx *debug_ctx;
546 assert( !process->debugger );
548 if (!debugger->debug_ctx) /* need to allocate a context */
550 if (!(debug_ctx = alloc_object( &debug_ctx_ops, -1 ))) return 0;
551 debug_ctx->event_head = NULL;
552 debug_ctx->event_tail = NULL;
553 debug_ctx->kill_on_exit = 1;
554 debugger->debug_ctx = debug_ctx;
556 process->debugger = debugger;
557 return 1;
560 /* a thread is exiting */
561 void debug_exit_thread( struct thread *thread )
563 if (thread->debug_ctx) /* this thread is a debugger */
565 if (thread->debug_ctx->kill_on_exit)
567 /* kill all debugged processes */
568 kill_debugged_processes( thread, thread->exit_code );
570 else
572 detach_debugged_processes( thread );
574 release_object( thread->debug_ctx );
575 thread->debug_ctx = NULL;
579 /* Wait for a debug event */
580 DECL_HANDLER(wait_debug_event)
582 struct debug_ctx *debug_ctx = current->debug_ctx;
583 struct debug_event *event;
585 if (!debug_ctx) /* current thread is not a debugger */
587 set_error( STATUS_INVALID_HANDLE );
588 return;
590 reply->wait = 0;
591 if ((event = find_event_to_send( debug_ctx )))
593 size_t size = get_reply_max_size();
594 event->state = EVENT_SENT;
595 event->sender->debug_event = event;
596 reply->pid = get_process_id( event->sender->process );
597 reply->tid = get_thread_id( event->sender );
598 if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
599 set_reply_data( &event->data, size );
601 else /* no event ready */
603 reply->pid = 0;
604 reply->tid = 0;
605 if (req->get_handle)
606 reply->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, FALSE );
610 /* Continue a debug event */
611 DECL_HANDLER(continue_debug_event)
613 struct process *process = get_process_from_id( req->pid );
614 if (process)
616 struct thread *thread = get_thread_from_id( req->tid );
617 if (thread)
619 continue_debug_event( process, thread, req->status );
620 release_object( thread );
622 release_object( process );
626 /* Start debugging an existing process */
627 DECL_HANDLER(debug_process)
629 struct process *process = get_process_from_id( req->pid );
630 if (!process) return;
632 if (!req->attach)
634 debugger_detach( process, current );
636 else if (debugger_attach( process, current ))
638 struct debug_event_exception data;
640 generate_startup_debug_events( process, NULL );
641 resume_process( process );
643 data.record.ExceptionCode = EXCEPTION_BREAKPOINT;
644 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
645 data.record.ExceptionRecord = NULL;
646 data.record.ExceptionAddress = get_thread_ip( process->thread_list );
647 data.record.NumberParameters = 0;
648 data.first = 1;
649 generate_debug_event( process->thread_list, EXCEPTION_DEBUG_EVENT, &data );
651 release_object( process );
654 /* queue an exception event */
655 DECL_HANDLER(queue_exception_event)
657 reply->handle = 0;
658 if (current->process->debugger)
660 struct debug_event_exception data;
661 struct debug_event *event;
662 const CONTEXT *context = get_req_data();
663 EXCEPTION_RECORD *rec = (EXCEPTION_RECORD *)(context + 1);
665 if (get_req_data_size() < sizeof(*rec) + sizeof(*context))
667 set_error( STATUS_INVALID_PARAMETER );
668 return;
670 data.record = *rec;
671 data.first = req->first;
672 if ((event = alloc_debug_event( current, EXCEPTION_DEBUG_EVENT, &data, context )))
674 if ((reply->handle = alloc_handle( current->process, event, SYNCHRONIZE, FALSE )))
676 link_event( event );
677 suspend_process( current->process );
679 release_object( event );
684 /* retrieve the status of an exception event */
685 DECL_HANDLER(get_exception_status)
687 struct debug_event *event;
689 reply->status = 0;
690 if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
691 0, &debug_event_ops )))
693 if (event->state == EVENT_CONTINUED)
695 reply->status = event->status;
696 if (current->context == &event->context)
698 size_t size = min( sizeof(CONTEXT), get_reply_max_size() );
699 set_reply_data( &event->context, size );
700 current->context = NULL;
703 else set_error( STATUS_PENDING );
704 release_object( event );
708 /* send an output string to the debugger */
709 DECL_HANDLER(output_debug_string)
711 struct debug_event_output_string data;
713 data.string = req->string;
714 data.unicode = req->unicode;
715 data.length = req->length;
716 generate_debug_event( current, OUTPUT_DEBUG_STRING_EVENT, &data );
719 /* simulate a breakpoint in a process */
720 DECL_HANDLER(debug_break)
722 struct process *process;
724 reply->self = 0;
725 if (!(process = get_process_from_handle( req->handle, PROCESS_SET_INFORMATION /*FIXME*/ )))
726 return;
727 if (process != current->process)
729 /* find a suitable thread to signal */
730 struct thread *thread;
731 for (thread = process->thread_list; thread; thread = thread->proc_next)
733 if (thread->unix_pid)
735 kill( thread->unix_pid, SIGTRAP );
736 break;
739 if (!thread) set_error( STATUS_ACCESS_DENIED );
741 else reply->self = 1;
742 release_object( process );
745 /* set debugger kill on exit flag */
746 DECL_HANDLER(set_debugger_kill_on_exit)
748 if (!current->debug_ctx)
750 set_error( STATUS_ACCESS_DENIED );
751 return;
753 current->debug_ctx->kill_on_exit = req->kill_on_exit;