ntdll: Fix the Mac build with SDKs older than 10.14.
[wine.git] / server / debugger.c
bloba5c39aa33e75e61bd6e1f7078322682c4335bc77
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"
22 #include "wine/port.h"
24 #include <assert.h>
25 #include <signal.h>
26 #include <string.h>
27 #include <stdarg.h>
28 #include <stdio.h>
30 #include "ntstatus.h"
31 #define WIN32_NO_STATUS
32 #include "windef.h"
33 #include "winternl.h"
35 #include "handle.h"
36 #include "file.h"
37 #include "process.h"
38 #include "thread.h"
39 #include "request.h"
41 enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
43 /* debug event */
44 struct debug_event
46 struct object obj; /* object header */
47 struct list entry; /* entry in event queue */
48 struct thread *sender; /* thread which sent this event */
49 struct thread *debugger; /* debugger thread receiving the event */
50 enum debug_event_state state; /* event state */
51 int status; /* continuation status */
52 debug_event_t data; /* event data */
53 context_t context; /* register context */
56 /* debug context */
57 struct debug_ctx
59 struct object obj; /* object header */
60 struct list event_queue; /* pending events queue */
61 int kill_on_exit;/* kill debuggees on debugger exit ? */
65 static void debug_event_dump( struct object *obj, int verbose );
66 static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry );
67 static void debug_event_destroy( struct object *obj );
69 static const struct object_ops debug_event_ops =
71 sizeof(struct debug_event), /* size */
72 debug_event_dump, /* dump */
73 no_get_type, /* get_type */
74 add_queue, /* add_queue */
75 remove_queue, /* remove_queue */
76 debug_event_signaled, /* signaled */
77 no_satisfied, /* satisfied */
78 no_signal, /* signal */
79 no_get_fd, /* get_fd */
80 no_map_access, /* map_access */
81 default_get_sd, /* get_sd */
82 default_set_sd, /* set_sd */
83 no_lookup_name, /* lookup_name */
84 no_link_name, /* link_name */
85 NULL, /* unlink_name */
86 no_open_file, /* open_file */
87 no_kernel_obj_list, /* get_kernel_obj_list */
88 no_close_handle, /* close_handle */
89 debug_event_destroy /* destroy */
92 static void debug_ctx_dump( struct object *obj, int verbose );
93 static int debug_ctx_signaled( struct object *obj, struct wait_queue_entry *entry );
94 static void debug_ctx_destroy( struct object *obj );
96 static const struct object_ops debug_ctx_ops =
98 sizeof(struct debug_ctx), /* size */
99 debug_ctx_dump, /* dump */
100 no_get_type, /* get_type */
101 add_queue, /* add_queue */
102 remove_queue, /* remove_queue */
103 debug_ctx_signaled, /* signaled */
104 no_satisfied, /* satisfied */
105 no_signal, /* signal */
106 no_get_fd, /* get_fd */
107 no_map_access, /* map_access */
108 default_get_sd, /* get_sd */
109 default_set_sd, /* set_sd */
110 no_lookup_name, /* lookup_name */
111 no_link_name, /* link_name */
112 NULL, /* unlink_name */
113 no_open_file, /* open_file */
114 no_kernel_obj_list, /* get_kernel_obj_list */
115 no_close_handle, /* close_handle */
116 debug_ctx_destroy /* destroy */
120 /* routines to build an event according to its type */
122 static int fill_exception_event( struct debug_event *event, const void *arg )
124 const debug_event_t *data = arg;
125 event->data.exception = data->exception;
126 event->data.exception.nb_params = min( event->data.exception.nb_params, EXCEPTION_MAXIMUM_PARAMETERS );
127 return 1;
130 static int fill_create_thread_event( struct debug_event *event, const void *arg )
132 struct process *debugger = event->debugger->process;
133 struct thread *thread = event->sender;
134 const client_ptr_t *entry = arg;
135 obj_handle_t handle;
137 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
138 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, 0 ))) return 0;
139 event->data.create_thread.handle = handle;
140 event->data.create_thread.teb = thread->teb;
141 if (entry) event->data.create_thread.start = *entry;
142 return 1;
145 static int fill_create_process_event( struct debug_event *event, const void *arg )
147 struct process *debugger = event->debugger->process;
148 struct thread *thread = event->sender;
149 struct process *process = thread->process;
150 struct process_dll *exe_module = get_process_exe_module( process );
151 const client_ptr_t *entry = arg;
152 struct file *file;
153 obj_handle_t handle;
155 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
156 if (!(handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, 0 ))) return 0;
157 event->data.create_process.process = handle;
159 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
160 if (!(handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, 0 )))
162 close_handle( debugger, event->data.create_process.process );
163 return 0;
165 event->data.create_process.thread = handle;
166 event->data.create_process.file = 0;
167 event->data.create_process.teb = thread->teb;
168 event->data.create_process.base = exe_module->base;
169 event->data.create_process.start = *entry;
170 event->data.create_process.dbg_offset = exe_module->dbg_offset;
171 event->data.create_process.dbg_size = exe_module->dbg_size;
172 event->data.create_process.name = exe_module->name;
173 event->data.create_process.unicode = 1;
175 /* the doc says write access too, but this doesn't seem a good idea */
176 if ((file = get_mapping_file( process, exe_module->base, GENERIC_READ,
177 FILE_SHARE_READ | FILE_SHARE_WRITE )))
179 event->data.create_process.file = alloc_handle( debugger, file, GENERIC_READ, 0 );
180 release_object( file );
182 return 1;
185 static int fill_exit_thread_event( struct debug_event *event, const void *arg )
187 const struct thread *thread = arg;
188 event->data.exit.exit_code = thread->exit_code;
189 return 1;
192 static int fill_exit_process_event( struct debug_event *event, const void *arg )
194 const struct process *process = arg;
195 event->data.exit.exit_code = process->exit_code;
196 return 1;
199 static int fill_load_dll_event( struct debug_event *event, const void *arg )
201 struct process *process = event->sender->process;
202 struct process *debugger = event->debugger->process;
203 const struct process_dll *dll = arg;
204 struct file *file;
206 event->data.load_dll.handle = 0;
207 event->data.load_dll.base = dll->base;
208 event->data.load_dll.dbg_offset = dll->dbg_offset;
209 event->data.load_dll.dbg_size = dll->dbg_size;
210 event->data.load_dll.name = dll->name;
211 event->data.load_dll.unicode = 1;
212 if ((file = get_mapping_file( process, dll->base, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE )))
214 event->data.load_dll.handle = alloc_handle( debugger, file, GENERIC_READ, 0 );
215 release_object( file );
217 return 1;
220 static int fill_unload_dll_event( struct debug_event *event, const void *arg )
222 const mod_handle_t *base = arg;
223 event->data.unload_dll.base = *base;
224 return 1;
227 typedef int (*fill_event_func)( struct debug_event *event, const void *arg );
229 #define NB_DEBUG_EVENTS UNLOAD_DLL_DEBUG_EVENT
231 static const fill_event_func fill_debug_event[NB_DEBUG_EVENTS] =
233 fill_exception_event, /* EXCEPTION_DEBUG_EVENT */
234 fill_create_thread_event, /* CREATE_THREAD_DEBUG_EVENT */
235 fill_create_process_event, /* CREATE_PROCESS_DEBUG_EVENT */
236 fill_exit_thread_event, /* EXIT_THREAD_DEBUG_EVENT */
237 fill_exit_process_event, /* EXIT_PROCESS_DEBUG_EVENT */
238 fill_load_dll_event, /* LOAD_DLL_DEBUG_EVENT */
239 fill_unload_dll_event /* UNLOAD_DLL_DEBUG_EVENT */
243 /* unlink the first event from the queue */
244 static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
246 list_remove( &event->entry );
247 if (event->sender->process->debug_event == event) event->sender->process->debug_event = NULL;
248 release_object( event );
251 /* link an event at the end of the queue */
252 static void link_event( struct debug_event *event )
254 struct debug_ctx *debug_ctx = event->debugger->debug_ctx;
256 assert( debug_ctx );
257 grab_object( event );
258 list_add_tail( &debug_ctx->event_queue, &event->entry );
259 if (!event->sender->process->debug_event)
261 /* grab reference since debugger could be killed while trying to wake up */
262 grab_object( debug_ctx );
263 wake_up( &debug_ctx->obj, 0 );
264 release_object( debug_ctx );
268 /* find the next event that we can send to the debugger */
269 static struct debug_event *find_event_to_send( struct debug_ctx *debug_ctx )
271 struct debug_event *event;
273 LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
275 if (event->state == EVENT_SENT) continue; /* already sent */
276 if (event->sender->process->debug_event) continue; /* process busy with another one */
277 return event;
279 return NULL;
282 static void debug_event_dump( struct object *obj, int verbose )
284 struct debug_event *debug_event = (struct debug_event *)obj;
285 assert( obj->ops == &debug_event_ops );
286 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
287 debug_event->sender, debug_event->data.code, debug_event->state );
290 static int debug_event_signaled( struct object *obj, struct wait_queue_entry *entry )
292 struct debug_event *debug_event = (struct debug_event *)obj;
293 assert( obj->ops == &debug_event_ops );
294 return debug_event->state == EVENT_CONTINUED;
297 static void debug_event_destroy( struct object *obj )
299 struct debug_event *event = (struct debug_event *)obj;
300 assert( obj->ops == &debug_event_ops );
302 /* If the event has been sent already, the handles are now under the */
303 /* responsibility of the debugger process, so we don't touch them */
304 if (event->state == EVENT_QUEUED)
306 struct process *debugger = event->debugger->process;
307 switch(event->data.code)
309 case CREATE_THREAD_DEBUG_EVENT:
310 close_handle( debugger, event->data.create_thread.handle );
311 break;
312 case CREATE_PROCESS_DEBUG_EVENT:
313 if (event->data.create_process.file)
314 close_handle( debugger, event->data.create_process.file );
315 close_handle( debugger, event->data.create_process.thread );
316 close_handle( debugger, event->data.create_process.process );
317 break;
318 case LOAD_DLL_DEBUG_EVENT:
319 if (event->data.load_dll.handle)
320 close_handle( debugger, event->data.load_dll.handle );
321 break;
324 if (event->sender->context == &event->context)
326 event->sender->context = NULL;
327 stop_thread_if_suspended( event->sender );
329 release_object( event->sender );
330 release_object( event->debugger );
333 static void debug_ctx_dump( struct object *obj, int verbose )
335 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
336 assert( obj->ops == &debug_ctx_ops );
337 fprintf( stderr, "Debug context head=%p tail=%p\n",
338 debug_ctx->event_queue.next, debug_ctx->event_queue.prev );
341 static int debug_ctx_signaled( struct object *obj, struct wait_queue_entry *entry )
343 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
344 assert( obj->ops == &debug_ctx_ops );
345 return find_event_to_send( debug_ctx ) != NULL;
348 static void debug_ctx_destroy( struct object *obj )
350 struct list *ptr;
351 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
352 assert( obj->ops == &debug_ctx_ops );
354 /* free all pending events */
355 while ((ptr = list_head( &debug_ctx->event_queue )))
356 unlink_event( debug_ctx, LIST_ENTRY( ptr, struct debug_event, entry ));
359 /* continue a debug event */
360 static int continue_debug_event( struct process *process, struct thread *thread, int status )
362 struct debug_ctx *debug_ctx = current->debug_ctx;
364 if (debug_ctx && process->debugger == current && thread->process == process)
366 struct debug_event *event;
368 /* find the event in the queue */
369 LIST_FOR_EACH_ENTRY( event, &debug_ctx->event_queue, struct debug_event, entry )
371 if (event->state != EVENT_SENT) continue;
372 if (event->sender == thread)
374 assert( event->sender->process->debug_event == event );
376 event->status = status;
377 event->state = EVENT_CONTINUED;
378 wake_up( &event->obj, 0 );
379 unlink_event( debug_ctx, event );
380 resume_process( process );
381 return 1;
385 /* not debugging this process, or no such event */
386 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
387 return 0;
390 /* alloc a debug event for a debugger */
391 static struct debug_event *alloc_debug_event( struct thread *thread, int code, const void *arg )
393 struct thread *debugger = thread->process->debugger;
394 struct debug_event *event;
396 assert( code > 0 && code <= NB_DEBUG_EVENTS );
397 /* cannot queue a debug event for myself */
398 assert( debugger->process != thread->process );
400 /* build the event */
401 if (!(event = alloc_object( &debug_event_ops ))) return NULL;
402 event->state = EVENT_QUEUED;
403 event->sender = (struct thread *)grab_object( thread );
404 event->debugger = (struct thread *)grab_object( debugger );
405 memset( &event->data, 0, sizeof(event->data) );
407 if (!fill_debug_event[code-1]( event, arg ))
409 event->data.code = -1; /* make sure we don't attempt to close handles */
410 release_object( event );
411 return NULL;
413 event->data.code = code;
414 return event;
417 /* generate a debug event from inside the server and queue it */
418 void generate_debug_event( struct thread *thread, int code, const void *arg )
420 if (thread->process->debugger)
422 struct debug_event *event = alloc_debug_event( thread, code, arg );
423 if (event)
425 link_event( event );
426 suspend_process( thread->process );
427 release_object( event );
429 clear_error(); /* ignore errors */
433 /* attach a process to a debugger thread and suspend it */
434 static int debugger_attach( struct process *process, struct thread *debugger )
436 if (process->debugger) goto error; /* already being debugged */
437 if (debugger->process == process) goto error;
438 if (!is_process_init_done( process )) goto error; /* still starting up */
439 if (list_empty( &process->thread_list )) goto error; /* no thread running in the process */
441 /* don't let a debugger debug its console... won't work */
442 if (debugger->process->console)
444 struct thread *renderer = console_get_renderer(debugger->process->console);
445 if (renderer && renderer->process == process)
446 goto error;
449 suspend_process( process );
450 if (!set_process_debugger( process, debugger ))
452 resume_process( process );
453 return 0;
455 if (!set_process_debug_flag( process, 1 ))
457 process->debugger = NULL;
458 resume_process( process );
459 return 0;
461 process->debug_children = 0;
462 return 1;
464 error:
465 set_error( STATUS_ACCESS_DENIED );
466 return 0;
470 /* detach a process from a debugger thread (and resume it ?) */
471 int debugger_detach( struct process *process, struct thread *debugger )
473 struct debug_event *event, *next;
474 struct debug_ctx *debug_ctx;
476 if (!process->debugger || process->debugger != debugger)
477 goto error; /* not currently debugged, or debugged by another debugger */
478 if (!debugger->debug_ctx ) goto error; /* should be a debugger */
479 /* init should be done, otherwise wouldn't be attached */
480 assert(is_process_init_done(process));
482 suspend_process( process );
483 /* send continue indication for all events */
484 debug_ctx = debugger->debug_ctx;
486 /* free all events from this process */
487 LIST_FOR_EACH_ENTRY_SAFE( event, next, &debug_ctx->event_queue, struct debug_event, entry )
489 if (event->sender->process != process) continue;
491 assert( event->state != EVENT_CONTINUED );
492 event->status = DBG_CONTINUE;
493 event->state = EVENT_CONTINUED;
494 wake_up( &event->obj, 0 );
495 unlink_event( debug_ctx, event );
496 /* from queued debug event */
497 resume_process( process );
500 /* remove relationships between process and its debugger */
501 process->debugger = NULL;
502 if (!set_process_debug_flag( process, 0 )) clear_error(); /* ignore error */
504 /* from this function */
505 resume_process( process );
506 return 0;
508 error:
509 set_error( STATUS_ACCESS_DENIED );
510 return 0;
513 /* generate all startup events of a given process */
514 void generate_startup_debug_events( struct process *process, client_ptr_t entry )
516 struct list *ptr;
517 struct thread *thread, *first_thread = get_process_first_thread( process );
519 generate_debug_event( first_thread, CREATE_PROCESS_DEBUG_EVENT, &entry );
520 ptr = list_head( &process->dlls ); /* skip main module reported in create process event */
522 /* generate ntdll.dll load event */
523 if (ptr && (ptr = list_next( &process->dlls, ptr )))
525 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
526 generate_debug_event( first_thread, LOAD_DLL_DEBUG_EVENT, dll );
529 /* generate creation events */
530 LIST_FOR_EACH_ENTRY( thread, &process->thread_list, struct thread, proc_entry )
532 if (thread != first_thread)
533 generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, NULL );
536 /* generate dll events (in loading order) */
537 while (ptr && (ptr = list_next( &process->dlls, ptr )))
539 struct process_dll *dll = LIST_ENTRY( ptr, struct process_dll, entry );
540 generate_debug_event( first_thread, LOAD_DLL_DEBUG_EVENT, dll );
544 /* set the debugger of a given process */
545 int set_process_debugger( struct process *process, struct thread *debugger )
547 struct debug_ctx *debug_ctx;
549 assert( !process->debugger );
551 if (!debugger->debug_ctx) /* need to allocate a context */
553 if (!(debug_ctx = alloc_object( &debug_ctx_ops ))) return 0;
554 debug_ctx->kill_on_exit = 1;
555 list_init( &debug_ctx->event_queue );
556 debugger->debug_ctx = debug_ctx;
558 process->debugger = debugger;
559 return 1;
562 /* a thread is exiting */
563 void debug_exit_thread( struct thread *thread )
565 if (thread->debug_ctx) /* this thread is a debugger */
567 if (thread->debug_ctx->kill_on_exit)
569 /* kill all debugged processes */
570 kill_debugged_processes( thread, STATUS_DEBUGGER_INACTIVE );
572 else
574 detach_debugged_processes( thread );
576 release_object( thread->debug_ctx );
577 thread->debug_ctx = NULL;
581 /* Wait for a debug event */
582 DECL_HANDLER(wait_debug_event)
584 struct debug_ctx *debug_ctx = current->debug_ctx;
585 struct debug_event *event;
587 if (!debug_ctx) /* current thread is not a debugger */
589 set_error( STATUS_INVALID_HANDLE );
590 return;
592 reply->wait = 0;
593 if ((event = find_event_to_send( debug_ctx )))
595 data_size_t size = get_reply_max_size();
596 event->state = EVENT_SENT;
597 event->sender->process->debug_event = event;
598 reply->pid = get_process_id( event->sender->process );
599 reply->tid = get_thread_id( event->sender );
600 if (size > sizeof(debug_event_t)) size = sizeof(debug_event_t);
601 set_reply_data( &event->data, size );
603 else /* no event ready */
605 reply->pid = 0;
606 reply->tid = 0;
607 if (req->get_handle)
608 reply->wait = alloc_handle( current->process, debug_ctx, SYNCHRONIZE, 0 );
612 /* Continue a debug event */
613 DECL_HANDLER(continue_debug_event)
615 struct process *process = get_process_from_id( req->pid );
616 if (process)
618 struct thread *thread = get_thread_from_id( req->tid );
619 if (thread)
621 continue_debug_event( process, thread, req->status );
622 release_object( thread );
624 release_object( process );
628 /* Start debugging an existing process */
629 DECL_HANDLER(debug_process)
631 struct process *process = get_process_from_id( req->pid );
632 if (!process) return;
634 if (!req->attach)
636 debugger_detach( process, current );
638 else if (debugger_attach( process, current ))
640 generate_startup_debug_events( process, 0 );
641 resume_process( process );
643 release_object( process );
646 /* queue an exception event */
647 DECL_HANDLER(queue_exception_event)
649 reply->handle = 0;
650 if (current->process->debugger)
652 debug_event_t data;
653 struct debug_event *event;
654 struct thread *thread = current;
656 if ((req->len % sizeof(client_ptr_t)) != 0 ||
657 req->len > get_req_data_size() ||
658 req->len > EXCEPTION_MAXIMUM_PARAMETERS * sizeof(client_ptr_t))
660 set_error( STATUS_INVALID_PARAMETER );
661 return;
663 memset( &data, 0, sizeof(data) );
664 data.exception.first = req->first;
665 data.exception.exc_code = req->code;
666 data.exception.flags = req->flags;
667 data.exception.record = req->record;
668 data.exception.address = req->address;
669 data.exception.nb_params = req->len / sizeof(client_ptr_t);
670 memcpy( data.exception.params, get_req_data(), req->len );
672 if ((event = alloc_debug_event( thread, EXCEPTION_DEBUG_EVENT, &data )))
674 const context_t *context = (const context_t *)((const char *)get_req_data() + req->len);
675 data_size_t size = get_req_data_size() - req->len;
677 memset( &event->context, 0, sizeof(event->context) );
678 memcpy( &event->context, context, min( sizeof(event->context), size ) );
679 thread->context = &event->context;
681 if ((reply->handle = alloc_handle( thread->process, event, SYNCHRONIZE, 0 )))
683 link_event( event );
684 suspend_process( thread->process );
686 release_object( event );
691 /* retrieve the status of an exception event */
692 DECL_HANDLER(get_exception_status)
694 struct debug_event *event;
696 if ((event = (struct debug_event *)get_handle_obj( current->process, req->handle,
697 0, &debug_event_ops )))
699 close_handle( current->process, req->handle );
700 if (event->state == EVENT_CONTINUED)
702 if (current->context == &event->context)
704 data_size_t size = min( sizeof(context_t), get_reply_max_size() );
705 set_reply_data( &event->context, size );
706 current->context = NULL;
707 stop_thread_if_suspended( current );
709 set_error( event->status );
711 else set_error( STATUS_PENDING );
712 release_object( event );
716 /* set debugger kill on exit flag */
717 DECL_HANDLER(set_debugger_kill_on_exit)
719 if (!current->debug_ctx)
721 set_error( STATUS_ACCESS_DENIED );
722 return;
724 current->debug_ctx->kill_on_exit = req->kill_on_exit;