Forward CBEM_SETEXSTYLE to CBEM_SETEXTENDEDSTYLE, pass WM_SETTEXT and
[wine.git] / server / debugger.c
blob946d659b171e530b2c797164f2553a6386d2d334
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"
18 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
20 /* debug event */
21 struct debug_event
23 struct object obj; /* object header */
24 struct debug_event *next; /* event queue */
25 struct debug_event *prev;
26 struct thread *sender; /* thread which sent this event */
27 struct thread *debugger; /* debugger thread receiving the event */
28 enum debug_event_state state; /* event state */
29 int status; /* continuation status */
30 debug_event_t data; /* event data */
33 /* debug context */
34 struct debug_ctx
36 struct object obj; /* object header */
37 struct debug_event *event_head; /* head of pending events queue */
38 struct debug_event *event_tail; /* tail of pending events queue */
42 static void debug_event_dump( struct object *obj, int verbose );
43 static int debug_event_signaled( struct object *obj, struct thread *thread );
44 static void debug_event_destroy( struct object *obj );
46 static const struct object_ops debug_event_ops =
48 sizeof(struct debug_event), /* size */
49 debug_event_dump, /* dump */
50 add_queue, /* add_queue */
51 remove_queue, /* remove_queue */
52 debug_event_signaled, /* signaled */
53 no_satisfied, /* satisfied */
54 NULL, /* get_poll_events */
55 NULL, /* poll_event */
56 no_read_fd, /* get_read_fd */
57 no_write_fd, /* get_write_fd */
58 no_flush, /* flush */
59 no_get_file_info, /* get_file_info */
60 debug_event_destroy /* destroy */
63 static void debug_ctx_dump( struct object *obj, int verbose );
64 static int debug_ctx_signaled( struct object *obj, struct thread *thread );
65 static void debug_ctx_destroy( struct object *obj );
67 static const struct object_ops debug_ctx_ops =
69 sizeof(struct debug_ctx), /* size */
70 debug_ctx_dump, /* dump */
71 add_queue, /* add_queue */
72 remove_queue, /* remove_queue */
73 debug_ctx_signaled, /* signaled */
74 no_satisfied, /* satisfied */
75 NULL, /* get_poll_events */
76 NULL, /* poll_event */
77 no_read_fd, /* get_read_fd */
78 no_write_fd, /* get_write_fd */
79 no_flush, /* flush */
80 no_get_file_info, /* get_file_info */
81 debug_ctx_destroy /* destroy */
85 /* routines to build an event according to its type */
87 static int fill_exception_event( struct debug_event *event, void *arg )
89 memcpy( &event->data.info.exception, arg, sizeof(event->data.info.exception) );
90 return 1;
93 static int fill_create_thread_event( struct debug_event *event, void *arg )
95 struct process *debugger = event->debugger->process;
96 struct thread *thread = event->sender;
97 int handle;
99 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
100 if ((handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )) == -1)
101 return 0;
102 event->data.info.create_thread.handle = handle;
103 event->data.info.create_thread.teb = thread->teb;
104 event->data.info.create_thread.start = arg;
105 return 1;
108 static int fill_create_process_event( struct debug_event *event, void *arg )
110 struct process *debugger = event->debugger->process;
111 struct thread *thread = event->sender;
112 struct process *process = thread->process;
113 int handle;
115 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
116 if ((handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, FALSE )) == -1)
117 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 )) == -1)
123 close_handle( debugger, event->data.info.create_process.process );
124 return 0;
126 event->data.info.create_process.thread = handle;
128 handle = -1;
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 )) == -1))
133 close_handle( debugger, event->data.info.create_process.process );
134 close_handle( debugger, event->data.info.create_process.thread );
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 int handle = -1;
168 if (dll->file && (handle = alloc_handle( debugger, dll->file, GENERIC_READ, FALSE )) == -1)
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 output_debug_string_request *req = arg;
188 event->data.info.output_string.string = req->string;
189 event->data.info.output_string.unicode = req->unicode;
190 event->data.info.output_string.length = req->length;
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_ctx *debug_ctx, struct debug_event *event )
226 grab_object( event );
227 event->next = NULL;
228 event->prev = debug_ctx->event_tail;
229 debug_ctx->event_tail = event;
230 if (event->prev) event->prev->next = event;
231 else debug_ctx->event_head = event;
232 if (!event->sender->debug_event) wake_up( &debug_ctx->obj, 0 );
235 /* find the next event that we can send to the debugger */
236 static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
238 struct debug_event *event;
239 for (event = debug_ctx->event_head; event; event = event->next)
241 if (event->state == EVENT_SENT) continue; /* already sent */
242 if (event->sender->debug_event) continue; /* thread busy with another one */
243 break;
245 return event;
248 /* build a reply for the wait_debug_event request */
249 static void build_wait_debug_reply( struct thread *thread, struct object *obj, int signaled )
251 struct wait_debug_event_request *req = get_req_ptr( thread );
253 if (obj)
255 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
256 struct debug_event *event = find_event_to_send( debug_ctx );
257 size_t size = get_req_data_size(req);
259 /* the object that woke us has to be our debug context */
260 assert( obj->ops == &debug_ctx_ops );
261 assert( event );
263 event->state = EVENT_SENT;
264 event->sender->debug_event = event;
265 req->pid = event->sender->process;
266 req->tid = event->sender;
267 if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
268 memcpy( get_req_data(req), &event->data, size );
269 set_req_data_size( req, size );
271 else /* timeout or error */
273 set_req_data_size( req, 0 );
274 req->pid = 0;
275 req->tid = 0;
279 /* build a reply for the send_event request */
280 static void build_exception_event_reply( struct thread *thread, struct object *obj, int signaled )
282 struct exception_event_request *req = get_req_ptr( thread );
283 struct debug_event *event = (struct debug_event *)obj;
284 assert( obj->ops == &debug_event_ops );
285 req->status = event->status;
286 thread->context = NULL;
289 static void debug_event_dump( struct object *obj, int verbose )
291 struct debug_event *debug_event = (struct debug_event *)obj;
292 assert( obj->ops == &debug_event_ops );
293 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
294 debug_event->sender, debug_event->data.code, debug_event->state );
297 static int debug_event_signaled( struct object *obj, struct thread *thread )
299 struct debug_event *debug_event = (struct debug_event *)obj;
300 assert( obj->ops == &debug_event_ops );
301 return debug_event->state == EVENT_CONTINUED;
304 static void debug_event_destroy( struct object *obj )
306 struct debug_event *event = (struct debug_event *)obj;
307 assert( obj->ops == &debug_event_ops );
309 /* cannot still be in the queue */
310 assert( !event->next );
311 assert( !event->prev );
313 /* If the event has been sent already, the handles are now under the */
314 /* responsibility of the debugger process, so we don't touch them */
315 if (event->state == EVENT_QUEUED)
317 struct process *debugger = event->debugger->process;
318 switch(event->data.code)
320 case CREATE_THREAD_DEBUG_EVENT:
321 close_handle( debugger, event->data.info.create_thread.handle );
322 break;
323 case CREATE_PROCESS_DEBUG_EVENT:
324 if (event->data.info.create_process.file != -1)
325 close_handle( debugger, event->data.info.create_process.file );
326 close_handle( debugger, event->data.info.create_process.thread );
327 close_handle( debugger, event->data.info.create_process.process );
328 break;
329 case LOAD_DLL_DEBUG_EVENT:
330 if (event->data.info.load_dll.handle != -1)
331 close_handle( debugger, event->data.info.load_dll.handle );
332 break;
335 release_object( event->sender );
336 release_object( event->debugger );
339 static void debug_ctx_dump( struct object *obj, int verbose )
341 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
342 assert( obj->ops == &debug_ctx_ops );
343 fprintf( stderr, "Debug context head=%p tail=%p\n",
344 debug_ctx->event_head, debug_ctx->event_tail );
347 static int debug_ctx_signaled( struct object *obj, struct thread *thread )
349 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
350 assert( obj->ops == &debug_ctx_ops );
351 return find_event_to_send( debug_ctx ) != NULL;
354 static void debug_ctx_destroy( struct object *obj )
356 struct debug_event *event;
357 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
358 assert( obj->ops == &debug_ctx_ops );
360 /* free all pending events */
361 while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
364 /* wait for a debug event (or send a reply at once if one is pending) */
365 static int wait_for_debug_event( int timeout )
367 struct debug_ctx *debug_ctx = current->debug_ctx;
368 struct object *obj = &debug_ctx->obj;
369 int flags = 0;
371 if (!debug_ctx) /* current thread is not a debugger */
373 set_error( STATUS_INVALID_HANDLE );
374 return 0;
376 if (timeout != -1) flags = SELECT_TIMEOUT;
377 return sleep_on( 1, &obj, flags, timeout, build_wait_debug_reply );
380 /* continue a debug event */
381 static int continue_debug_event( struct process *process, struct thread *thread, int status )
383 struct debug_event *event;
384 struct debug_ctx *debug_ctx = current->debug_ctx;
386 if (!debug_ctx || process->debugger != current || thread->process != process) goto error;
388 /* find the event in the queue */
389 for (event = debug_ctx->event_head; event; event = event->next)
391 if (event->state != EVENT_SENT) continue;
392 if (event->sender == thread) break;
394 if (!event) goto error;
396 assert( event->sender->debug_event == event );
398 event->status = status;
399 event->state = EVENT_CONTINUED;
400 wake_up( &event->obj, 0 );
402 unlink_event( debug_ctx, event );
403 resume_process( process );
404 return 1;
405 error:
406 /* not debugging this process, or no such event */
407 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
408 return 0;
411 /* queue a debug event for a debugger */
412 static struct debug_event *queue_debug_event( struct thread *thread, int code, void *arg )
414 struct thread *debugger = thread->process->debugger;
415 struct debug_ctx *debug_ctx = debugger->debug_ctx;
416 struct debug_event *event;
418 assert( code > 0 && code <= NB_DEBUG_EVENTS );
419 assert( debug_ctx );
420 /* cannot queue a debug event for myself */
421 assert( debugger->process != thread->process );
423 /* build the event */
424 if (!(event = alloc_object( &debug_event_ops, -1 ))) return NULL;
425 event->next = NULL;
426 event->prev = NULL;
427 event->state = EVENT_QUEUED;
428 event->sender = (struct thread *)grab_object( thread );
429 event->debugger = (struct thread *)grab_object( debugger );
430 event->data.code = code;
432 if (!fill_debug_event[code-1]( event, arg ))
434 event->data.code = -1; /* make sure we don't attempt to close handles */
435 release_object( event );
436 return NULL;
439 link_event( debug_ctx, event );
440 suspend_process( thread->process );
441 return event;
444 /* generate a debug event from inside the server and queue it */
445 void generate_debug_event( struct thread *thread, int code, void *arg )
447 if (thread->process->debugger)
449 struct debug_event *event = queue_debug_event( thread, code, arg );
450 if (event) release_object( event );
454 /* attach a process to a debugger thread and suspend it */
455 static int debugger_attach( struct process *process, struct thread *debugger )
457 struct thread *thread;
459 if (process->debugger) goto error; /* already being debugged */
460 if (process->init_event) goto error; /* still starting up */
462 /* make sure we don't create a debugging loop */
463 for (thread = debugger; thread; thread = thread->process->debugger)
464 if (thread->process == process) goto error;
466 suspend_process( process );
468 /* we must have been able to attach all threads */
469 for (thread = process->thread_list; thread; thread = thread->proc_next)
470 if (!thread->attached)
472 fprintf( stderr, "%p not attached\n", thread );
473 resume_process( process );
474 goto error;
477 if (set_process_debugger( process, debugger )) return 1;
478 resume_process( process );
479 return 0;
481 error:
482 set_error( STATUS_ACCESS_DENIED );
483 return 0;
486 /* generate all startup events of a given process */
487 void generate_startup_debug_events( struct process *process, void *entry )
489 struct process_dll *dll;
490 struct thread *thread = process->thread_list;
492 /* generate creation events */
493 generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT, entry );
494 while ((thread = thread->proc_next))
495 generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
497 /* generate dll events (in loading order, i.e. reverse list order) */
498 for (dll = &process->exe; dll->next; dll = dll->next);
499 while (dll != &process->exe)
501 generate_debug_event( process->thread_list, LOAD_DLL_DEBUG_EVENT, dll );
502 dll = dll->prev;
506 /* set the debugger of a given process */
507 int set_process_debugger( struct process *process, struct thread *debugger )
509 struct debug_ctx *debug_ctx;
511 assert( !process->debugger );
513 if (!debugger->debug_ctx) /* need to allocate a context */
515 if (!(debug_ctx = alloc_object( &debug_ctx_ops, -1 ))) return 0;
516 debug_ctx->event_head = NULL;
517 debug_ctx->event_tail = NULL;
518 debugger->debug_ctx = debug_ctx;
520 process->debugger = debugger;
521 return 1;
524 /* a thread is exiting */
525 void debug_exit_thread( struct thread *thread )
527 if (thread->debug_ctx) /* this thread is a debugger */
529 /* kill all debugged processes */
530 kill_debugged_processes( thread, thread->exit_code );
531 release_object( thread->debug_ctx );
532 thread->debug_ctx = NULL;
536 /* Wait for a debug event */
537 DECL_HANDLER(wait_debug_event)
539 if (!wait_for_debug_event( req->timeout ))
541 req->pid = NULL;
542 req->tid = NULL;
543 set_req_data_size( req, 0 );
547 /* Continue a debug event */
548 DECL_HANDLER(continue_debug_event)
550 struct process *process = get_process_from_id( req->pid );
551 if (process)
553 struct thread *thread = get_thread_from_id( req->tid );
554 if (thread)
556 continue_debug_event( process, thread, req->status );
557 release_object( thread );
559 release_object( process );
563 /* Start debugging an existing process */
564 DECL_HANDLER(debug_process)
566 struct debug_event_exception data;
567 struct debug_event *event;
568 struct process *process = get_process_from_id( req->pid );
569 if (!process) return;
571 if (debugger_attach( process, current ))
573 generate_startup_debug_events( process, NULL );
574 resume_process( process );
576 data.record.ExceptionCode = EXCEPTION_BREAKPOINT;
577 data.record.ExceptionFlags = EXCEPTION_CONTINUABLE;
578 data.record.ExceptionRecord = NULL;
579 data.record.ExceptionAddress = get_thread_ip( process->thread_list );
580 data.record.NumberParameters = 0;
581 data.first = 1;
582 if ((event = queue_debug_event( process->thread_list, EXCEPTION_DEBUG_EVENT, &data )))
583 release_object( event );
585 release_object( process );
588 /* send an exception event */
589 DECL_HANDLER(exception_event)
591 req->status = 0;
592 if (current->process->debugger)
594 struct debug_event_exception data;
595 struct debug_event *event;
596 CONTEXT *context = get_req_data( req );
597 EXCEPTION_RECORD *rec = (EXCEPTION_RECORD *)(context + 1);
599 if (get_req_data_size( req ) < sizeof(*rec) + sizeof(*context))
601 set_error( STATUS_INVALID_PARAMETER );
602 return;
604 data.record = *rec;
605 data.first = req->first;
606 if ((event = queue_debug_event( current, EXCEPTION_DEBUG_EVENT, &data )))
608 struct object *obj = &event->obj;
609 current->context = context;
610 sleep_on( 1, &obj, 0, -1, build_exception_event_reply );
611 release_object( event );
616 /* send an output string to the debugger */
617 DECL_HANDLER(output_debug_string)
619 if (current->process->debugger)
621 struct debug_event *event = queue_debug_event( current, OUTPUT_DEBUG_STRING_EVENT, req );
622 if (event) release_object( event );