Don't return from overlapped ReadFile on EAGAIN and other non-fatal
[wine/gsoc_dplay.git] / server / debugger.c
blobe487eb0480c0c4df43ece4473e147896eb9684d9
1 /*
2 * Server-side debugger functions
4 * Copyright (C) 1999 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <string.h>
9 #include <stdio.h>
11 #include "winbase.h"
13 #include "handle.h"
14 #include "process.h"
15 #include "thread.h"
16 #include "request.h"
17 #include "console.h"
19 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
21 /* debug event */
22 struct debug_event
24 struct object obj; /* object header */
25 struct debug_event *next; /* event queue */
26 struct debug_event *prev;
27 struct thread *sender; /* thread which sent this event */
28 struct thread *debugger; /* debugger thread receiving the event */
29 enum debug_event_state state; /* event state */
30 int status; /* continuation status */
31 debug_event_t data; /* event data */
32 CONTEXT context; /* register context */
35 /* debug context */
36 struct debug_ctx
38 struct object obj; /* object header */
39 struct debug_event *event_head; /* head of pending events queue */
40 struct debug_event *event_tail; /* tail of pending events queue */
44 static void debug_event_dump( struct object *obj, int verbose );
45 static int debug_event_signaled( struct object *obj, struct thread *thread );
46 static void debug_event_destroy( struct object *obj );
48 static const struct object_ops debug_event_ops =
50 sizeof(struct debug_event), /* size */
51 debug_event_dump, /* dump */
52 add_queue, /* add_queue */
53 remove_queue, /* remove_queue */
54 debug_event_signaled, /* signaled */
55 no_satisfied, /* satisfied */
56 NULL, /* get_poll_events */
57 NULL, /* poll_event */
58 no_get_fd, /* get_fd */
59 no_flush, /* flush */
60 no_get_file_info, /* get_file_info */
61 NULL, /* queue_async */
62 debug_event_destroy /* destroy */
65 static void debug_ctx_dump( struct object *obj, int verbose );
66 static int debug_ctx_signaled( struct object *obj, struct thread *thread );
67 static void debug_ctx_destroy( struct object *obj );
69 static const struct object_ops debug_ctx_ops =
71 sizeof(struct debug_ctx), /* size */
72 debug_ctx_dump, /* dump */
73 add_queue, /* add_queue */
74 remove_queue, /* remove_queue */
75 debug_ctx_signaled, /* signaled */
76 no_satisfied, /* satisfied */
77 NULL, /* get_poll_events */
78 NULL, /* poll_event */
79 no_get_fd, /* get_fd */
80 no_flush, /* flush */
81 no_get_file_info, /* get_file_info */
82 NULL, /* queue_async */
83 debug_ctx_destroy /* destroy */
87 /* routines to build an event according to its type */
89 static int fill_exception_event( struct debug_event *event, void *arg )
91 memcpy( &event->data.info.exception, arg, sizeof(event->data.info.exception) );
92 return 1;
95 static int fill_create_thread_event( struct debug_event *event, void *arg )
97 struct process *debugger = event->debugger->process;
98 struct thread *thread = event->sender;
99 handle_t handle;
101 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
102 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE ))) return 0;
103 event->data.info.create_thread.handle = handle;
104 event->data.info.create_thread.teb = thread->teb;
105 event->data.info.create_thread.start = arg;
106 return 1;
109 static int fill_create_process_event( struct debug_event *event, void *arg )
111 struct process *debugger = event->debugger->process;
112 struct thread *thread = event->sender;
113 struct process *process = thread->process;
114 handle_t handle;
116 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
117 if (!(handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, FALSE ))) return 0;
118 event->data.info.create_process.process = handle;
120 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
121 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )))
123 close_handle( debugger, event->data.info.create_process.process, NULL );
124 return 0;
126 event->data.info.create_process.thread = handle;
128 handle = 0;
129 if (process->exe.file &&
130 /* the doc says write access too, but this doesn't seem a good idea */
131 !(handle = alloc_handle( debugger, process->exe.file, GENERIC_READ, FALSE )))
133 close_handle( debugger, event->data.info.create_process.process, NULL );
134 close_handle( debugger, event->data.info.create_process.thread, NULL );
135 return 0;
137 event->data.info.create_process.file = handle;
138 event->data.info.create_process.teb = thread->teb;
139 event->data.info.create_process.base = process->exe.base;
140 event->data.info.create_process.start = arg;
141 event->data.info.create_process.dbg_offset = process->exe.dbg_offset;
142 event->data.info.create_process.dbg_size = process->exe.dbg_size;
143 event->data.info.create_process.name = process->exe.name;
144 event->data.info.create_process.unicode = 0;
145 return 1;
148 static int fill_exit_thread_event( struct debug_event *event, void *arg )
150 struct thread *thread = arg;
151 event->data.info.exit.exit_code = thread->exit_code;
152 return 1;
155 static int fill_exit_process_event( struct debug_event *event, void *arg )
157 struct process *process = arg;
158 event->data.info.exit.exit_code = process->exit_code;
159 return 1;
162 static int fill_load_dll_event( struct debug_event *event, void *arg )
164 struct process *debugger = event->debugger->process;
165 struct process_dll *dll = arg;
166 handle_t handle = 0;
168 if (dll->file && !(handle = alloc_handle( debugger, dll->file, GENERIC_READ, FALSE )))
169 return 0;
170 event->data.info.load_dll.handle = handle;
171 event->data.info.load_dll.base = dll->base;
172 event->data.info.load_dll.dbg_offset = dll->dbg_offset;
173 event->data.info.load_dll.dbg_size = dll->dbg_size;
174 event->data.info.load_dll.name = dll->name;
175 event->data.info.load_dll.unicode = 0;
176 return 1;
179 static int fill_unload_dll_event( struct debug_event *event, void *arg )
181 event->data.info.unload_dll.base = arg;
182 return 1;
185 static int fill_output_debug_string_event( struct debug_event *event, void *arg )
187 struct debug_event_output_string *data = arg;
188 event->data.info.output_string = *data;
189 return 1;
192 typedef int (*fill_event_func)( struct debug_event *event, void *arg );
194 #define NB_DEBUG_EVENTS OUTPUT_DEBUG_STRING_EVENT /* RIP_EVENT not supported */
196 static const fill_event_func fill_debug_event[NB_DEBUG_EVENTS] =
198 fill_exception_event, /* EXCEPTION_DEBUG_EVENT */
199 fill_create_thread_event, /* CREATE_THREAD_DEBUG_EVENT */
200 fill_create_process_event, /* CREATE_PROCESS_DEBUG_EVENT */
201 fill_exit_thread_event, /* EXIT_THREAD_DEBUG_EVENT */
202 fill_exit_process_event, /* EXIT_PROCESS_DEBUG_EVENT */
203 fill_load_dll_event, /* LOAD_DLL_DEBUG_EVENT */
204 fill_unload_dll_event, /* UNLOAD_DLL_DEBUG_EVENT */
205 fill_output_debug_string_event /* OUTPUT_DEBUG_STRING_EVENT */
209 /* unlink the first event from the queue */
210 static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
212 if (event->prev) event->prev->next = event->next;
213 else debug_ctx->event_head = event->next;
214 if (event->next) event->next->prev = event->prev;
215 else debug_ctx->event_tail = event->prev;
216 event->next = event->prev = NULL;
217 if (event->sender->debug_event == event) event->sender->debug_event = NULL;
218 release_object( event );
221 /* link an event at the end of the queue */
222 static void link_event( struct debug_event *event )
224 struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
226 assert( debug_ctx );
227 grab_object( event );
228 event->next = NULL;
229 event->prev = debug_ctx->event_tail;
230 debug_ctx->event_tail = event;
231 if (event->prev) event->prev->next = event;
232 else debug_ctx->event_head = event;
233 if (!event->sender->debug_event) wake_up( &debug_ctx->obj, 0 );
236 /* find the next event that we can send to the debugger */
237 static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
239 struct debug_event *event;
240 for (event = debug_ctx->event_head; event; event = event->next)
242 if (event->state == EVENT_SENT) continue; /* already sent */
243 if (event->sender->debug_event) continue; /* thread busy with another one */
244 break;
246 return event;
249 static void debug_event_dump( struct object *obj, int verbose )
251 struct debug_event *debug_event = (struct debug_event *)obj;
252 assert( obj->ops == &debug_event_ops );
253 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
254 debug_event->sender, debug_event->data.code, debug_event->state );
257 static int debug_event_signaled( struct object *obj, struct thread *thread )
259 struct debug_event *debug_event = (struct debug_event *)obj;
260 assert( obj->ops == &debug_event_ops );
261 return debug_event->state == EVENT_CONTINUED;
264 static void debug_event_destroy( struct object *obj )
266 struct debug_event *event = (struct debug_event *)obj;
267 assert( obj->ops == &debug_event_ops );
269 /* cannot still be in the queue */
270 assert( !event->next );
271 assert( !event->prev );
273 /* If the event has been sent already, the handles are now under the */
274 /* responsibility of the debugger process, so we don't touch them */
275 if (event->state == EVENT_QUEUED)
277 struct process *debugger = event->debugger->process;
278 switch(event->data.code)
280 case CREATE_THREAD_DEBUG_EVENT:
281 close_handle( debugger, event->data.info.create_thread.handle, NULL );
282 break;
283 case CREATE_PROCESS_DEBUG_EVENT:
284 if (event->data.info.create_process.file)
285 close_handle( debugger, event->data.info.create_process.file, NULL );
286 close_handle( debugger, event->data.info.create_process.thread, NULL );
287 close_handle( debugger, event->data.info.create_process.process, NULL );
288 break;
289 case LOAD_DLL_DEBUG_EVENT:
290 if (event->data.info.load_dll.handle)
291 close_handle( debugger, event->data.info.load_dll.handle, NULL );
292 break;
295 if (event->sender->context == &event->context) event->sender->context = NULL;
296 release_object( event->sender );
297 release_object( event->debugger );
300 static void debug_ctx_dump( struct object *obj, int verbose )
302 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
303 assert( obj->ops == &debug_ctx_ops );
304 fprintf( stderr, "Debug context head=%p tail=%p\n",
305 debug_ctx->event_head, debug_ctx->event_tail );
308 static int debug_ctx_signaled( struct object *obj, struct thread *thread )
310 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
311 assert( obj->ops == &debug_ctx_ops );
312 return find_event_to_send( debug_ctx ) != NULL;
315 static void debug_ctx_destroy( struct object *obj )
317 struct debug_event *event;
318 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
319 assert( obj->ops == &debug_ctx_ops );
321 /* free all pending events */
322 while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
325 /* continue a debug event */
326 static int continue_debug_event( struct process *process, struct thread *thread, int status )
328 struct debug_event *event;
329 struct debug_ctx *debug_ctx = current->debug_ctx;
331 if (!debug_ctx || process->debugger != current || thread->process != process) goto error;
333 /* find the event in the queue */
334 for (event = debug_ctx->event_head; event; event = event->next)
336 if (event->state != EVENT_SENT) continue;
337 if (event->sender == thread) break;
339 if (!event) goto error;
341 assert( event->sender->debug_event == event );
343 event->status = status;
344 event->state = EVENT_CONTINUED;
345 wake_up( &event->obj, 0 );
347 unlink_event( debug_ctx, event );
348 resume_process( process );
349 return 1;
350 error:
351 /* not debugging this process, or no such event */
352 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
353 return 0;
356 /* alloc a debug event for a debugger */
357 static struct debug_event *alloc_debug_event( struct thread *thread, int code,
358 void *arg, const CONTEXT *context )
360 struct thread *debugger = thread->process->debugger;
361 struct debug_event *event;
363 assert( code > 0 && code <= NB_DEBUG_EVENTS );
364 /* cannot queue a debug event for myself */
365 assert( debugger->process != thread->process );
367 /* build the event */
368 if (!(event = alloc_object( &debug_event_ops, -1 ))) return NULL;
369 event->next = NULL;
370 event->prev = NULL;
371 event->state = EVENT_QUEUED;
372 event->sender = (struct thread *)grab_object( thread );
373 event->debugger = (struct thread *)grab_object( debugger );
374 event->data.code = code;
376 if (!fill_debug_event[code-1]( event, arg ))
378 event->data.code = -1; /* make sure we don't attempt to close handles */
379 release_object( event );
380 return NULL;
382 if (context)
384 memcpy( &event->context, context, sizeof(event->context) );
385 thread->context = &event->context;
387 return event;
390 /* generate a debug event from inside the server and queue it */
391 void generate_debug_event( struct thread *thread, int code, void *arg )
393 if (thread->process->debugger)
395 struct debug_event *event = alloc_debug_event( thread, code, arg, NULL );
396 if (event)
398 link_event( event );
399 suspend_process( thread->process );
400 release_object( event );
405 /* attach a process to a debugger thread and suspend it */
406 static int debugger_attach( struct process *process, struct thread *debugger )
408 struct thread *thread;
410 if (process->debugger) goto error; /* already being debugged */
411 if (process->init_event) goto error; /* still starting up */
413 /* make sure we don't create a debugging loop */
414 for (thread = debugger; thread; thread = thread->process->debugger)
415 if (thread->process == process) goto error;
417 /* don't let a debugger debug its console... won't work */
418 if (debugger->process->console && debugger->process->console->renderer->process == process)
419 goto error;
421 suspend_process( process );
423 /* we must have been able to attach all threads */
424 for (thread = process->thread_list; thread; thread = thread->proc_next)
425 if (!thread->attached)
427 resume_process( process );
428 goto error;
431 if (set_process_debugger( process, debugger )) return 1;
432 resume_process( process );
433 return 0;
435 error:
436 set_error( STATUS_ACCESS_DENIED );
437 return 0;
440 /* generate all startup events of a given process */
441 void generate_startup_debug_events( struct process *process, void *entry )
443 struct process_dll *dll;
444 struct thread *thread = process->thread_list;
446 /* generate creation events */
447 generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT, entry );
448 while ((thread = thread->proc_next))
449 generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
451 /* generate dll events (in loading order, i.e. reverse list order) */
452 dll = &process->exe;
453 while (dll->next) dll = dll->next;
454 while (dll != &process->exe)
456 generate_debug_event( process->thread_list, LOAD_DLL_DEBUG_EVENT, dll );
457 dll = dll->prev;
461 /* set the debugger of a given process */
462 int set_process_debugger( struct process *process, struct thread *debugger )
464 struct debug_ctx *debug_ctx;
466 assert( !process->debugger );
468 if (!debugger->debug_ctx) /* need to allocate a context */
470 if (!(debug_ctx = alloc_object( &debug_ctx_ops, -1 ))) return 0;
471 debug_ctx->event_head = NULL;
472 debug_ctx->event_tail = NULL;
473 debugger->debug_ctx = debug_ctx;
475 process->debugger = debugger;
476 return 1;
479 /* a thread is exiting */
480 void debug_exit_thread( struct thread *thread )
482 if (thread->debug_ctx) /* this thread is a debugger */
484 /* kill all debugged processes */
485 kill_debugged_processes( thread, thread->exit_code );
486 release_object( thread->debug_ctx );
487 thread->debug_ctx = NULL;
491 /* Wait for a debug event */
492 DECL_HANDLER(wait_debug_event)
494 struct debug_ctx *debug_ctx = current->debug_ctx;
495 struct debug_event *event;
497 if (!debug_ctx) /* current thread is not a debugger */
499 set_error( STATUS_INVALID_HANDLE );
500 return;
502 reply->wait = 0;
503 if ((event = find_event_to_send( debug_ctx )))
505 size_t size = get_reply_max_size();
506 event->state = EVENT_SENT;
507 event->sender->debug_event = event;
508 reply->pid = event->sender->process;
509 reply->tid = event->sender;
510 if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
511 set_reply_data( &event->data, size );
513 else /* no event ready */
515 reply->pid = 0;
516 reply->tid = 0;
517 if (req->get_handle)
518 reply->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, FALSE );
522 /* Continue a debug event */
523 DECL_HANDLER(continue_debug_event)
525 struct process *process = get_process_from_id( req->pid );
526 if (process)
528 struct thread *thread = get_thread_from_id( req->tid );
529 if (thread)
531 continue_debug_event( process, thread, req->status );
532 release_object( thread );
534 release_object( process );
538 /* Start debugging an existing process */
539 DECL_HANDLER(debug_process)
541 struct debug_event_exception data;
542 struct process *process = get_process_from_id( req->pid );
543 if (!process) return;
545 if (debugger_attach( process, current ))
547 generate_startup_debug_events( process, NULL );
548 resume_process( process );
550 data.record.ExceptionCode = EXCEPTION_BREAKPOINT;
551 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
552 data.record.ExceptionRecord = NULL;
553 data.record.ExceptionAddress = get_thread_ip( process->thread_list );
554 data.record.NumberParameters = 0;
555 data.first = 1;
556 generate_debug_event( process->thread_list, EXCEPTION_DEBUG_EVENT, &data );
558 release_object( process );
561 /* queue an exception event */
562 DECL_HANDLER(queue_exception_event)
564 reply->handle = 0;
565 if (current->process->debugger)
567 struct debug_event_exception data;
568 struct debug_event *event;
569 const CONTEXT *context = get_req_data();
570 EXCEPTION_RECORD *rec = (EXCEPTION_RECORD *)(context + 1);
572 if (get_req_data_size() < sizeof(*rec) + sizeof(*context))
574 set_error( STATUS_INVALID_PARAMETER );
575 return;
577 data.record = *rec;
578 data.first = req->first;
579 if ((event = alloc_debug_event( current, EXCEPTION_DEBUG_EVENT, &data, context )))
581 if ((reply->handle = alloc_handle( current->process, event, SYNCHRONIZE, FALSE )))
583 link_event( event );
584 suspend_process( current->process );
586 release_object( event );
591 /* retrieve the status of an exception event */
592 DECL_HANDLER(get_exception_status)
594 struct debug_event *event;
596 reply->status = 0;
597 if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
598 0, &debug_event_ops )))
600 if (event->state == EVENT_CONTINUED)
602 reply->status = event->status;
603 if (current->context == &event->context)
605 size_t size = min( sizeof(CONTEXT), get_reply_max_size() );
606 set_reply_data( &event->context, size );
607 current->context = NULL;
610 else set_error( STATUS_PENDING );
611 release_object( event );
615 /* send an output string to the debugger */
616 DECL_HANDLER(output_debug_string)
618 struct debug_event_output_string data;
620 data.string = req->string;
621 data.unicode = req->unicode;
622 data.length = req->length;
623 generate_debug_event( current, OUTPUT_DEBUG_STRING_EVENT, &data );