blob: 7b7b9b40ea021f7e050e9a2a8c351f494b68bb01 [file] [log] [blame]
Alexandre Julliarde712e071999-05-23 19:53:30 +00001/*
2 * Server-side debugger functions
3 *
4 * Copyright (C) 1999 Alexandre Julliard
5 */
6
7#include <assert.h>
Patrik Stridvall2c684081999-07-31 17:36:48 +00008#include <string.h>
Alexandre Julliard79b1ec82000-01-04 02:24:43 +00009#include <stdio.h>
Patrik Stridvall2c684081999-07-31 17:36:48 +000010
Alexandre Julliarde712e071999-05-23 19:53:30 +000011#include "winbase.h"
Alexandre Julliarde712e071999-05-23 19:53:30 +000012
Alexandre Julliarde712e071999-05-23 19:53:30 +000013#include "handle.h"
14#include "process.h"
15#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000016#include "request.h"
Alexandre Julliarde712e071999-05-23 19:53:30 +000017
Alexandre Julliard79b1ec82000-01-04 02:24:43 +000018enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED };
19
20/* debug event */
Alexandre Julliarde712e071999-05-23 19:53:30 +000021struct debug_event
22{
Alexandre Julliard79b1ec82000-01-04 02:24:43 +000023 struct object obj; /* object header */
24 struct debug_event *next; /* event queue */
Alexandre Julliarde712e071999-05-23 19:53:30 +000025 struct debug_event *prev;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +000026 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 */
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000030 debug_event_t data; /* event data */
Alexandre Julliarde712e071999-05-23 19:53:30 +000031};
32
Alexandre Julliard79b1ec82000-01-04 02:24:43 +000033/* debug context */
Alexandre Julliarde712e071999-05-23 19:53:30 +000034struct debug_ctx
35{
Alexandre Julliard79b1ec82000-01-04 02:24:43 +000036 struct object obj; /* object header */
Alexandre Julliarde712e071999-05-23 19:53:30 +000037 struct debug_event *event_head; /* head of pending events queue */
38 struct debug_event *event_tail; /* tail of pending events queue */
Alexandre Julliard79b1ec82000-01-04 02:24:43 +000039 struct debug_event *to_send; /* next event on the queue to send to debugger */
40};
41
42
43static void debug_event_dump( struct object *obj, int verbose );
44static int debug_event_signaled( struct object *obj, struct thread *thread );
45static void debug_event_destroy( struct object *obj );
46
47static const struct object_ops debug_event_ops =
48{
49 sizeof(struct debug_event), /* size */
50 debug_event_dump, /* dump */
51 add_queue, /* add_queue */
52 remove_queue, /* remove_queue */
53 debug_event_signaled, /* signaled */
54 no_satisfied, /* satisfied */
55 NULL, /* get_poll_events */
56 NULL, /* poll_event */
57 no_read_fd, /* get_read_fd */
58 no_write_fd, /* get_write_fd */
59 no_flush, /* flush */
60 no_get_file_info, /* get_file_info */
61 debug_event_destroy /* destroy */
62};
63
64static void debug_ctx_dump( struct object *obj, int verbose );
65static int debug_ctx_signaled( struct object *obj, struct thread *thread );
66static void debug_ctx_destroy( struct object *obj );
67
68static const struct object_ops debug_ctx_ops =
69{
70 sizeof(struct debug_ctx), /* size */
71 debug_ctx_dump, /* dump */
72 add_queue, /* add_queue */
73 remove_queue, /* remove_queue */
74 debug_ctx_signaled, /* signaled */
75 no_satisfied, /* satisfied */
76 NULL, /* get_poll_events */
77 NULL, /* poll_event */
78 no_read_fd, /* get_read_fd */
79 no_write_fd, /* get_write_fd */
80 no_flush, /* flush */
81 no_get_file_info, /* get_file_info */
82 debug_ctx_destroy /* destroy */
Alexandre Julliarde712e071999-05-23 19:53:30 +000083};
84
Alexandre Julliarde712e071999-05-23 19:53:30 +000085
86/* initialise the fields that do not need to be filled by the client */
Alexandre Julliard05f0b712000-03-09 18:18:41 +000087static int fill_debug_event( struct debug_event *event, void *arg )
Alexandre Julliarde712e071999-05-23 19:53:30 +000088{
Alexandre Julliard05f0b712000-03-09 18:18:41 +000089 struct process *debugger = event->debugger->process;
90 struct process *process;
91 struct thread *thread;
92 struct process_dll *dll;
Alexandre Julliarde712e071999-05-23 19:53:30 +000093 int handle;
94
95 /* some events need special handling */
Alexandre Julliard3e2517c2000-01-20 18:59:03 +000096 switch(event->data.code)
Alexandre Julliarde712e071999-05-23 19:53:30 +000097 {
98 case CREATE_THREAD_DEBUG_EVENT:
Alexandre Julliard05f0b712000-03-09 18:18:41 +000099 thread = arg;
100 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
101 if ((handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )) == -1)
Alexandre Julliarde712e071999-05-23 19:53:30 +0000102 return 0;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000103 event->data.info.create_thread.handle = handle;
104 event->data.info.create_thread.teb = thread->teb;
105 event->data.info.create_thread.start = thread->entry;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000106 break;
107 case CREATE_PROCESS_DEBUG_EVENT:
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000108 process = arg;
109 thread = process->thread_list;
110 /* documented: PROCESS_VM_READ | PROCESS_VM_WRITE */
111 if ((handle = alloc_handle( debugger, process, PROCESS_ALL_ACCESS, FALSE )) == -1)
Alexandre Julliarde712e071999-05-23 19:53:30 +0000112 return 0;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000113 event->data.info.create_process.process = handle;
114
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000115 /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */
116 if ((handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )) == -1)
Alexandre Julliarde712e071999-05-23 19:53:30 +0000117 {
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000118 close_handle( debugger, event->data.info.create_process.process );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000119 return 0;
120 }
Alexandre Julliardff81d782000-03-08 12:01:30 +0000121 event->data.info.create_process.thread = handle;
122
123 handle = -1;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000124 if (process->exe.file &&
125 /* the doc says write access too, but this doesn't seem a good idea */
126 ((handle = alloc_handle( debugger, process->exe.file, GENERIC_READ, FALSE )) == -1))
Alexandre Julliardff81d782000-03-08 12:01:30 +0000127 {
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000128 close_handle( debugger, event->data.info.create_process.process );
129 close_handle( debugger, event->data.info.create_process.thread );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000130 return 0;
131 }
132 event->data.info.create_process.file = handle;
133 event->data.info.create_process.teb = thread->teb;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000134 event->data.info.create_process.base = process->exe.base;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000135 event->data.info.create_process.start = thread->entry;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000136 event->data.info.create_process.dbg_offset = process->exe.dbg_offset;
137 event->data.info.create_process.dbg_size = process->exe.dbg_size;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000138 event->data.info.create_process.name = 0;
139 event->data.info.create_process.unicode = 0;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000140 break;
141 case LOAD_DLL_DEBUG_EVENT:
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000142 dll = arg;
143 handle = -1;
144 if (dll->file &&
145 (handle = alloc_handle( debugger, dll->file, GENERIC_READ, FALSE )) == -1)
146 return 0;
147 event->data.info.load_dll.handle = handle;
148 event->data.info.load_dll.base = dll->base;
149 event->data.info.load_dll.dbg_offset = dll->dbg_offset;
150 event->data.info.load_dll.dbg_size = dll->dbg_size;
151 event->data.info.load_dll.name = dll->name;
152 event->data.info.load_dll.unicode = 0;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000153 break;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000154 case EXIT_PROCESS_DEBUG_EVENT:
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000155 process = arg;
156 event->data.info.exit.exit_code = process->exit_code;
157 break;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000158 case EXIT_THREAD_DEBUG_EVENT:
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000159 thread = arg;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000160 event->data.info.exit.exit_code = thread->exit_code;
161 break;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000162 case UNLOAD_DLL_DEBUG_EVENT:
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000163 event->data.info.unload_dll.base = arg;
164 break;
165 case EXCEPTION_DEBUG_EVENT:
Alexandre Julliardff81d782000-03-08 12:01:30 +0000166 case OUTPUT_DEBUG_STRING_EVENT:
167 case RIP_EVENT:
168 break;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000169 }
170 return 1;
171}
172
Alexandre Julliarde712e071999-05-23 19:53:30 +0000173/* unlink the first event from the queue */
174static void unlink_event( struct debug_ctx *debug_ctx, struct debug_event *event )
175{
176 if (event->prev) event->prev->next = event->next;
177 else debug_ctx->event_head = event->next;
178 if (event->next) event->next->prev = event->prev;
179 else debug_ctx->event_tail = event->prev;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000180 if (debug_ctx->to_send == event) debug_ctx->to_send = event->next;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000181 event->next = event->prev = NULL;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000182 release_object( event );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000183}
184
185/* link an event at the end of the queue */
186static void link_event( struct debug_ctx *debug_ctx, struct debug_event *event )
187{
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000188 grab_object( event );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000189 event->next = NULL;
190 event->prev = debug_ctx->event_tail;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000191 debug_ctx->event_tail = event;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000192 if (event->prev) event->prev->next = event;
193 else debug_ctx->event_head = event;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000194 if (!debug_ctx->to_send)
Alexandre Julliarde712e071999-05-23 19:53:30 +0000195 {
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000196 debug_ctx->to_send = event;
197 wake_up( &debug_ctx->obj, 0 );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000198 }
Alexandre Julliarde712e071999-05-23 19:53:30 +0000199}
200
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000201/* build a reply for the wait_debug_event request */
202static void build_wait_debug_reply( struct thread *thread, struct object *obj, int signaled )
Alexandre Julliarde712e071999-05-23 19:53:30 +0000203{
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000204 struct wait_debug_event_request *req = get_req_ptr( thread );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000205
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000206 if (obj)
207 {
208 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
209 struct debug_event *event = debug_ctx->to_send;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000210
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000211 /* the object that woke us has to be our debug context */
212 assert( obj->ops == &debug_ctx_ops );
213 assert( event );
214
215 event->state = EVENT_SENT;
216 debug_ctx->to_send = event->next;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000217 req->event.code = event->data.code;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000218 req->pid = event->sender->process;
219 req->tid = event->sender;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000220 memcpy( &req->event, &event->data, sizeof(req->event) );
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000221 }
222 else /* timeout or error */
223 {
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000224 req->event.code = 0;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000225 req->pid = 0;
226 req->tid = 0;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000227 }
228}
229
230/* build a reply for the send_event request */
231static void build_send_event_reply( struct thread *thread, struct object *obj, int signaled )
232{
233 struct send_debug_event_request *req = get_req_ptr( thread );
234 struct debug_event *event = (struct debug_event *)obj;
235 assert( obj->ops == &debug_event_ops );
236
237 req->status = event->status;
238 /* copy the context into the reply */
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000239 if (event->data.code == EXCEPTION_DEBUG_EVENT)
240 memcpy( &req->event.info.exception.context,
241 &event->data.info.exception.context,
242 sizeof(req->event.info.exception.context) );
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000243}
244
245static void debug_event_dump( struct object *obj, int verbose )
246{
247 struct debug_event *debug_event = (struct debug_event *)obj;
248 assert( obj->ops == &debug_event_ops );
249 fprintf( stderr, "Debug event sender=%p code=%d state=%d\n",
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000250 debug_event->sender, debug_event->data.code, debug_event->state );
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000251}
252
253static int debug_event_signaled( struct object *obj, struct thread *thread )
254{
255 struct debug_event *debug_event = (struct debug_event *)obj;
256 assert( obj->ops == &debug_event_ops );
257 return debug_event->state == EVENT_CONTINUED;
258}
259
260static void debug_event_destroy( struct object *obj )
261{
262 struct debug_event *event = (struct debug_event *)obj;
263 assert( obj->ops == &debug_event_ops );
264
265 /* cannot still be in the queue */
266 assert( !event->next );
267 assert( !event->prev );
268
269 /* If the event has been sent already, the handles are now under the */
270 /* responsibility of the debugger process, so we don't touch them */
271 if (event->state == EVENT_QUEUED)
272 {
273 struct process *debugger = event->debugger->process;
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000274 switch(event->data.code)
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000275 {
276 case CREATE_THREAD_DEBUG_EVENT:
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000277 close_handle( debugger, event->data.info.create_thread.handle );
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000278 break;
279 case CREATE_PROCESS_DEBUG_EVENT:
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000280 if (event->data.info.create_process.file != -1)
281 close_handle( debugger, event->data.info.create_process.file );
282 close_handle( debugger, event->data.info.create_process.thread );
283 close_handle( debugger, event->data.info.create_process.process );
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000284 break;
285 case LOAD_DLL_DEBUG_EVENT:
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000286 if (event->data.info.load_dll.handle != -1)
287 close_handle( debugger, event->data.info.load_dll.handle );
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000288 break;
289 }
290 }
291 release_object( event->sender );
292 release_object( event->debugger );
293}
294
295static void debug_ctx_dump( struct object *obj, int verbose )
296{
297 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
298 assert( obj->ops == &debug_ctx_ops );
299 fprintf( stderr, "Debug context head=%p tail=%p to_send=%p\n",
300 debug_ctx->event_head, debug_ctx->event_tail, debug_ctx->to_send );
301}
302
303static int debug_ctx_signaled( struct object *obj, struct thread *thread )
304{
305 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
306 assert( obj->ops == &debug_ctx_ops );
307 return debug_ctx->to_send != NULL;
308}
309
310static void debug_ctx_destroy( struct object *obj )
311{
312 struct debug_event *event;
313 struct debug_ctx *debug_ctx = (struct debug_ctx *)obj;
314 assert( obj->ops == &debug_ctx_ops );
315
316 /* free all pending events */
317 while ((event = debug_ctx->event_head) != NULL) unlink_event( debug_ctx, event );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000318}
319
320/* wait for a debug event (or send a reply at once if one is pending) */
321static int wait_for_debug_event( int timeout )
322{
323 struct debug_ctx *debug_ctx = current->debug_ctx;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000324 struct object *obj = &debug_ctx->obj;
325 int flags = 0;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000326
327 if (!debug_ctx) /* current thread is not a debugger */
328 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000329 set_error( STATUS_INVALID_HANDLE );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000330 return 0;
331 }
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000332 if (timeout != -1) flags = SELECT_TIMEOUT;
333 return sleep_on( 1, &obj, flags, timeout, build_wait_debug_reply );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000334}
335
336/* continue a debug event */
337static int continue_debug_event( struct process *process, struct thread *thread, int status )
338{
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000339 struct debug_event *event;
340 struct debug_ctx *debug_ctx = current->debug_ctx;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000341
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000342 if (!debug_ctx || process->debugger != current || thread->process != process) goto error;
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000343
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000344 /* find the event in the queue */
345 for (event = debug_ctx->event_head; event; event = event->next)
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000346 {
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000347 if (event == debug_ctx->to_send) goto error;
348 if (event->sender == thread) break;
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000349 }
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000350 if (!event) goto error;
351
352 event->status = status;
353 event->state = EVENT_CONTINUED;
354 wake_up( &event->obj, 0 );
355
356 unlink_event( debug_ctx, event );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000357 resume_process( process );
358 return 1;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000359 error:
360 /* not debugging this process, or no such event */
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000361 set_error( STATUS_ACCESS_DENIED ); /* FIXME */
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000362 return 0;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000363}
364
365/* queue a debug event for a debugger */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000366static struct debug_event *queue_debug_event( struct thread *thread, int code, void *arg,
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000367 debug_event_t *data )
Alexandre Julliarde712e071999-05-23 19:53:30 +0000368{
Alexandre Julliardff81d782000-03-08 12:01:30 +0000369 struct thread *debugger = thread->process->debugger;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000370 struct debug_ctx *debug_ctx = debugger->debug_ctx;
371 struct debug_event *event;
372
373 assert( debug_ctx );
374 /* cannot queue a debug event for myself */
375 assert( debugger->process != thread->process );
376
377 /* build the event */
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000378 if (!(event = alloc_object( &debug_event_ops, -1 ))) return NULL;
379 event->next = NULL;
380 event->prev = NULL;
381 event->state = EVENT_QUEUED;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000382 event->sender = (struct thread *)grab_object( thread );
383 event->debugger = (struct thread *)grab_object( debugger );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000384 if (data) memcpy( &event->data, data, sizeof(event->data) );
385 event->data.code = code;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000386
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000387 if (!fill_debug_event( event, arg ))
Alexandre Julliarde712e071999-05-23 19:53:30 +0000388 {
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000389 event->data.code = -1; /* make sure we don't attempt to close handles */
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000390 release_object( event );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000391 return NULL;
392 }
393
Alexandre Julliarde712e071999-05-23 19:53:30 +0000394 link_event( debug_ctx, event );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000395 suspend_process( thread->process );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000396 return event;
397}
398
Alexandre Julliardff81d782000-03-08 12:01:30 +0000399/* generate a debug event from inside the server and queue it */
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000400void generate_debug_event( struct thread *thread, int code, void *arg )
Alexandre Julliardff81d782000-03-08 12:01:30 +0000401{
402 if (thread->process->debugger)
403 {
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000404 struct debug_event *event = queue_debug_event( thread, code, arg, NULL );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000405 if (event) release_object( event );
406 }
407}
408
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000409/* generate all startup events of a given process */
410void generate_startup_debug_events( struct process *process )
411{
412 struct process_dll *dll;
413 struct thread *thread = process->thread_list;
414
415 /* generate creation events */
416 generate_debug_event( thread, CREATE_PROCESS_DEBUG_EVENT, process );
417 while ((thread = thread->next))
418 generate_debug_event( thread, CREATE_THREAD_DEBUG_EVENT, thread );
419
420 /* generate dll events (in loading order, i.e. reverse list order) */
421 for (dll = &process->exe; dll->next; dll = dll->next);
422 while (dll != &process->exe)
423 {
424 generate_debug_event( process->thread_list, LOAD_DLL_DEBUG_EVENT, dll );
425 dll = dll->prev;
426 }
427}
428
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000429/* return a pointer to the context in case the thread is inside an exception event */
430CONTEXT *get_debug_context( struct thread *thread )
431{
432 struct debug_event *event;
433 struct thread *debugger = thread->process->debugger;
434
435 if (!debugger) return NULL; /* not being debugged */
436 assert( debugger->debug_ctx );
437
438 /* find the exception event in the debugger's queue */
439 for (event = debugger->debug_ctx->event_head; event; event = event->next)
440 if (event->sender == thread && (event->data.code == EXCEPTION_DEBUG_EVENT))
441 return &event->data.info.exception.context;
442 return NULL;
443}
444
Alexandre Julliarde712e071999-05-23 19:53:30 +0000445/* attach a process to a debugger thread */
446int debugger_attach( struct process *process, struct thread *debugger )
447{
448 struct debug_ctx *debug_ctx;
449 struct thread *thread;
450
451 if (process->debugger) /* already being debugged */
452 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000453 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000454 return 0;
455 }
456 /* make sure we don't create a debugging loop */
457 for (thread = debugger; thread; thread = thread->process->debugger)
458 if (thread->process == process)
459 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +0000460 set_error( STATUS_ACCESS_DENIED );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000461 return 0;
462 }
463
464 if (!debugger->debug_ctx) /* need to allocate a context */
465 {
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000466 if (!(debug_ctx = alloc_object( &debug_ctx_ops, -1 ))) return 0;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000467 debug_ctx->event_head = NULL;
468 debug_ctx->event_tail = NULL;
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000469 debug_ctx->to_send = NULL;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000470 debugger->debug_ctx = debug_ctx;
471 }
Alexandre Julliard17cf8101999-11-24 01:22:14 +0000472 process->debugger = debugger;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000473 return 1;
474}
475
Alexandre Julliarde712e071999-05-23 19:53:30 +0000476/* a thread is exiting */
Alexandre Julliardff81d782000-03-08 12:01:30 +0000477void debug_exit_thread( struct thread *thread )
Alexandre Julliarde712e071999-05-23 19:53:30 +0000478{
Alexandre Julliardff81d782000-03-08 12:01:30 +0000479 if (thread->debug_ctx) /* this thread is a debugger */
Alexandre Julliarde712e071999-05-23 19:53:30 +0000480 {
Alexandre Julliarde712e071999-05-23 19:53:30 +0000481 /* kill all debugged processes */
Alexandre Julliardff81d782000-03-08 12:01:30 +0000482 kill_debugged_processes( thread, thread->exit_code );
483 release_object( thread->debug_ctx );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000484 thread->debug_ctx = NULL;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000485 }
486}
487
488/* Wait for a debug event */
489DECL_HANDLER(wait_debug_event)
490{
Alexandre Julliarde712e071999-05-23 19:53:30 +0000491 if (!wait_for_debug_event( req->timeout ))
492 {
Alexandre Julliard3e2517c2000-01-20 18:59:03 +0000493 req->event.code = 0;
494 req->pid = NULL;
495 req->tid = NULL;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000496 }
497}
498
499/* Continue a debug event */
500DECL_HANDLER(continue_debug_event)
501{
502 struct process *process = get_process_from_id( req->pid );
503 if (process)
504 {
505 struct thread *thread = get_thread_from_id( req->tid );
506 if (thread)
507 {
508 continue_debug_event( process, thread, req->status );
509 release_object( thread );
510 }
511 release_object( process );
512 }
Alexandre Julliarde712e071999-05-23 19:53:30 +0000513}
514
515/* Start debugging an existing process */
516DECL_HANDLER(debug_process)
517{
518 struct process *process = get_process_from_id( req->pid );
Alexandre Julliardff81d782000-03-08 12:01:30 +0000519 if (!process) return;
520 if (debugger_attach( process, current ))
Alexandre Julliarde712e071999-05-23 19:53:30 +0000521 {
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000522 generate_startup_debug_events( process );
523 /* FIXME: breakpoint exception event */
Alexandre Julliarde712e071999-05-23 19:53:30 +0000524 }
Alexandre Julliardff81d782000-03-08 12:01:30 +0000525 release_object( process );
Alexandre Julliarde712e071999-05-23 19:53:30 +0000526}
527
528/* Send a debug event */
529DECL_HANDLER(send_debug_event)
530{
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000531 struct debug_event *event;
Alexandre Julliardff81d782000-03-08 12:01:30 +0000532 int code = req->event.code;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000533
Alexandre Julliardff81d782000-03-08 12:01:30 +0000534 if ((code <= 0) || (code > RIP_EVENT))
Alexandre Julliarde712e071999-05-23 19:53:30 +0000535 {
Alexandre Julliardff81d782000-03-08 12:01:30 +0000536 fatal_protocol_error( current, "send_debug_event: bad code %d\n", code );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000537 return;
Alexandre Julliarde712e071999-05-23 19:53:30 +0000538 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000539 req->status = 0;
Alexandre Julliard05f0b712000-03-09 18:18:41 +0000540 if (current->process->debugger && ((event = queue_debug_event( current, code,
541 NULL, &req->event ))))
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000542 {
543 /* wait for continue_debug_event */
Alexandre Julliard79b1ec82000-01-04 02:24:43 +0000544 struct object *obj = &event->obj;
545 sleep_on( 1, &obj, 0, -1, build_send_event_reply );
546 release_object( event );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000547 }
Alexandre Julliarde712e071999-05-23 19:53:30 +0000548}