Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side debugger functions |
| 3 | * |
| 4 | * Copyright (C) 1999 Alexandre Julliard |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Patrik Stridvall | 2c68408 | 1999-07-31 17:36:48 +0000 | [diff] [blame] | 8 | #include <string.h> |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 9 | #include <stdio.h> |
Patrik Stridvall | 2c68408 | 1999-07-31 17:36:48 +0000 | [diff] [blame] | 10 | |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 11 | #include "winbase.h" |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 12 | |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 13 | #include "handle.h" |
| 14 | #include "process.h" |
| 15 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 16 | #include "request.h" |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 17 | |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 18 | enum debug_event_state { EVENT_QUEUED, EVENT_SENT, EVENT_CONTINUED }; |
| 19 | |
| 20 | /* debug event */ |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 21 | struct debug_event |
| 22 | { |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 23 | struct object obj; /* object header */ |
| 24 | struct debug_event *next; /* event queue */ |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 25 | struct debug_event *prev; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 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 */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 30 | debug_event_t data; /* event data */ |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 31 | }; |
| 32 | |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 33 | /* debug context */ |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 34 | struct debug_ctx |
| 35 | { |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 36 | struct object obj; /* object header */ |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 37 | struct debug_event *event_head; /* head of pending events queue */ |
| 38 | struct debug_event *event_tail; /* tail of pending events queue */ |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 39 | struct debug_event *to_send; /* next event on the queue to send to debugger */ |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | static void debug_event_dump( struct object *obj, int verbose ); |
| 44 | static int debug_event_signaled( struct object *obj, struct thread *thread ); |
| 45 | static void debug_event_destroy( struct object *obj ); |
| 46 | |
| 47 | static 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 | |
| 64 | static void debug_ctx_dump( struct object *obj, int verbose ); |
| 65 | static int debug_ctx_signaled( struct object *obj, struct thread *thread ); |
| 66 | static void debug_ctx_destroy( struct object *obj ); |
| 67 | |
| 68 | static 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 Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 83 | }; |
| 84 | |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 85 | |
| 86 | /* initialise the fields that do not need to be filled by the client */ |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 87 | static int fill_debug_event( struct debug_event *event, void *arg ) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 88 | { |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 89 | struct process *debugger = event->debugger->process; |
| 90 | struct process *process; |
| 91 | struct thread *thread; |
| 92 | struct process_dll *dll; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 93 | int handle; |
| 94 | |
| 95 | /* some events need special handling */ |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 96 | switch(event->data.code) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 97 | { |
| 98 | case CREATE_THREAD_DEBUG_EVENT: |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 99 | 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 Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 102 | return 0; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 103 | 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 Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 106 | break; |
| 107 | case CREATE_PROCESS_DEBUG_EVENT: |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 108 | 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 Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 112 | return 0; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 113 | event->data.info.create_process.process = handle; |
| 114 | |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 115 | /* documented: THREAD_GET_CONTEXT | THREAD_SET_CONTEXT | THREAD_SUSPEND_RESUME */ |
| 116 | if ((handle = alloc_handle( debugger, thread, THREAD_ALL_ACCESS, FALSE )) == -1) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 117 | { |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 118 | close_handle( debugger, event->data.info.create_process.process ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 119 | return 0; |
| 120 | } |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 121 | event->data.info.create_process.thread = handle; |
| 122 | |
| 123 | handle = -1; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 124 | 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 Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 127 | { |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 128 | close_handle( debugger, event->data.info.create_process.process ); |
| 129 | close_handle( debugger, event->data.info.create_process.thread ); |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 130 | return 0; |
| 131 | } |
| 132 | event->data.info.create_process.file = handle; |
| 133 | event->data.info.create_process.teb = thread->teb; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 134 | event->data.info.create_process.base = process->exe.base; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 135 | event->data.info.create_process.start = thread->entry; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 136 | event->data.info.create_process.dbg_offset = process->exe.dbg_offset; |
| 137 | event->data.info.create_process.dbg_size = process->exe.dbg_size; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 138 | event->data.info.create_process.name = 0; |
| 139 | event->data.info.create_process.unicode = 0; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 140 | break; |
| 141 | case LOAD_DLL_DEBUG_EVENT: |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 142 | 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 Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 153 | break; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 154 | case EXIT_PROCESS_DEBUG_EVENT: |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 155 | process = arg; |
| 156 | event->data.info.exit.exit_code = process->exit_code; |
| 157 | break; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 158 | case EXIT_THREAD_DEBUG_EVENT: |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 159 | thread = arg; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 160 | event->data.info.exit.exit_code = thread->exit_code; |
| 161 | break; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 162 | case UNLOAD_DLL_DEBUG_EVENT: |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 163 | event->data.info.unload_dll.base = arg; |
| 164 | break; |
| 165 | case EXCEPTION_DEBUG_EVENT: |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 166 | case OUTPUT_DEBUG_STRING_EVENT: |
| 167 | case RIP_EVENT: |
| 168 | break; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 169 | } |
| 170 | return 1; |
| 171 | } |
| 172 | |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 173 | /* unlink the first event from the queue */ |
| 174 | static 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 Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 180 | if (debug_ctx->to_send == event) debug_ctx->to_send = event->next; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 181 | event->next = event->prev = NULL; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 182 | release_object( event ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /* link an event at the end of the queue */ |
| 186 | static void link_event( struct debug_ctx *debug_ctx, struct debug_event *event ) |
| 187 | { |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 188 | grab_object( event ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 189 | event->next = NULL; |
| 190 | event->prev = debug_ctx->event_tail; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 191 | debug_ctx->event_tail = event; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 192 | if (event->prev) event->prev->next = event; |
| 193 | else debug_ctx->event_head = event; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 194 | if (!debug_ctx->to_send) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 195 | { |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 196 | debug_ctx->to_send = event; |
| 197 | wake_up( &debug_ctx->obj, 0 ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 198 | } |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 201 | /* build a reply for the wait_debug_event request */ |
| 202 | static void build_wait_debug_reply( struct thread *thread, struct object *obj, int signaled ) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 203 | { |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 204 | struct wait_debug_event_request *req = get_req_ptr( thread ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 205 | |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 206 | if (obj) |
| 207 | { |
| 208 | struct debug_ctx *debug_ctx = (struct debug_ctx *)obj; |
| 209 | struct debug_event *event = debug_ctx->to_send; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 210 | |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 211 | /* 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 217 | req->event.code = event->data.code; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 218 | req->pid = event->sender->process; |
| 219 | req->tid = event->sender; |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 220 | memcpy( &req->event, &event->data, sizeof(req->event) ); |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 221 | } |
| 222 | else /* timeout or error */ |
| 223 | { |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 224 | req->event.code = 0; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 225 | req->pid = 0; |
| 226 | req->tid = 0; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
| 230 | /* build a reply for the send_event request */ |
| 231 | static 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 239 | 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 Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | static 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 250 | debug_event->sender, debug_event->data.code, debug_event->state ); |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static 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 | |
| 260 | static 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 274 | switch(event->data.code) |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 275 | { |
| 276 | case CREATE_THREAD_DEBUG_EVENT: |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 277 | close_handle( debugger, event->data.info.create_thread.handle ); |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 278 | break; |
| 279 | case CREATE_PROCESS_DEBUG_EVENT: |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 280 | 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 Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 284 | break; |
| 285 | case LOAD_DLL_DEBUG_EVENT: |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 286 | if (event->data.info.load_dll.handle != -1) |
| 287 | close_handle( debugger, event->data.info.load_dll.handle ); |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 288 | break; |
| 289 | } |
| 290 | } |
| 291 | release_object( event->sender ); |
| 292 | release_object( event->debugger ); |
| 293 | } |
| 294 | |
| 295 | static 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 | |
| 303 | static 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 | |
| 310 | static 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 Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /* wait for a debug event (or send a reply at once if one is pending) */ |
| 321 | static int wait_for_debug_event( int timeout ) |
| 322 | { |
| 323 | struct debug_ctx *debug_ctx = current->debug_ctx; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 324 | struct object *obj = &debug_ctx->obj; |
| 325 | int flags = 0; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 326 | |
| 327 | if (!debug_ctx) /* current thread is not a debugger */ |
| 328 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 329 | set_error( STATUS_INVALID_HANDLE ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 330 | return 0; |
| 331 | } |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 332 | if (timeout != -1) flags = SELECT_TIMEOUT; |
| 333 | return sleep_on( 1, &obj, flags, timeout, build_wait_debug_reply ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | /* continue a debug event */ |
| 337 | static int continue_debug_event( struct process *process, struct thread *thread, int status ) |
| 338 | { |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 339 | struct debug_event *event; |
| 340 | struct debug_ctx *debug_ctx = current->debug_ctx; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 341 | |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 342 | if (!debug_ctx || process->debugger != current || thread->process != process) goto error; |
Alexandre Julliard | 17cf810 | 1999-11-24 01:22:14 +0000 | [diff] [blame] | 343 | |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 344 | /* find the event in the queue */ |
| 345 | for (event = debug_ctx->event_head; event; event = event->next) |
Alexandre Julliard | 17cf810 | 1999-11-24 01:22:14 +0000 | [diff] [blame] | 346 | { |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 347 | if (event == debug_ctx->to_send) goto error; |
| 348 | if (event->sender == thread) break; |
Alexandre Julliard | 17cf810 | 1999-11-24 01:22:14 +0000 | [diff] [blame] | 349 | } |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 350 | 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 Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 357 | resume_process( process ); |
| 358 | return 1; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 359 | error: |
| 360 | /* not debugging this process, or no such event */ |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 361 | set_error( STATUS_ACCESS_DENIED ); /* FIXME */ |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 362 | return 0; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | /* queue a debug event for a debugger */ |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 366 | static struct debug_event *queue_debug_event( struct thread *thread, int code, void *arg, |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 367 | debug_event_t *data ) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 368 | { |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 369 | struct thread *debugger = thread->process->debugger; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 370 | 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 Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 378 | if (!(event = alloc_object( &debug_event_ops, -1 ))) return NULL; |
| 379 | event->next = NULL; |
| 380 | event->prev = NULL; |
| 381 | event->state = EVENT_QUEUED; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 382 | event->sender = (struct thread *)grab_object( thread ); |
| 383 | event->debugger = (struct thread *)grab_object( debugger ); |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 384 | if (data) memcpy( &event->data, data, sizeof(event->data) ); |
| 385 | event->data.code = code; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 386 | |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 387 | if (!fill_debug_event( event, arg )) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 388 | { |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 389 | event->data.code = -1; /* make sure we don't attempt to close handles */ |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 390 | release_object( event ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 391 | return NULL; |
| 392 | } |
| 393 | |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 394 | link_event( debug_ctx, event ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 395 | suspend_process( thread->process ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 396 | return event; |
| 397 | } |
| 398 | |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 399 | /* generate a debug event from inside the server and queue it */ |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 400 | void generate_debug_event( struct thread *thread, int code, void *arg ) |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 401 | { |
| 402 | if (thread->process->debugger) |
| 403 | { |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 404 | struct debug_event *event = queue_debug_event( thread, code, arg, NULL ); |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 405 | if (event) release_object( event ); |
| 406 | } |
| 407 | } |
| 408 | |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 409 | /* generate all startup events of a given process */ |
| 410 | void 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 Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 429 | /* return a pointer to the context in case the thread is inside an exception event */ |
| 430 | CONTEXT *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 Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 445 | /* attach a process to a debugger thread */ |
| 446 | int 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 Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 453 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 454 | 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 Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 460 | set_error( STATUS_ACCESS_DENIED ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | if (!debugger->debug_ctx) /* need to allocate a context */ |
| 465 | { |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 466 | if (!(debug_ctx = alloc_object( &debug_ctx_ops, -1 ))) return 0; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 467 | debug_ctx->event_head = NULL; |
| 468 | debug_ctx->event_tail = NULL; |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 469 | debug_ctx->to_send = NULL; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 470 | debugger->debug_ctx = debug_ctx; |
| 471 | } |
Alexandre Julliard | 17cf810 | 1999-11-24 01:22:14 +0000 | [diff] [blame] | 472 | process->debugger = debugger; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 473 | return 1; |
| 474 | } |
| 475 | |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 476 | /* a thread is exiting */ |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 477 | void debug_exit_thread( struct thread *thread ) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 478 | { |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 479 | if (thread->debug_ctx) /* this thread is a debugger */ |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 480 | { |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 481 | /* kill all debugged processes */ |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 482 | kill_debugged_processes( thread, thread->exit_code ); |
| 483 | release_object( thread->debug_ctx ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 484 | thread->debug_ctx = NULL; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
| 488 | /* Wait for a debug event */ |
| 489 | DECL_HANDLER(wait_debug_event) |
| 490 | { |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 491 | if (!wait_for_debug_event( req->timeout )) |
| 492 | { |
Alexandre Julliard | 3e2517c | 2000-01-20 18:59:03 +0000 | [diff] [blame] | 493 | req->event.code = 0; |
| 494 | req->pid = NULL; |
| 495 | req->tid = NULL; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
| 499 | /* Continue a debug event */ |
| 500 | DECL_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 Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | /* Start debugging an existing process */ |
| 516 | DECL_HANDLER(debug_process) |
| 517 | { |
| 518 | struct process *process = get_process_from_id( req->pid ); |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 519 | if (!process) return; |
| 520 | if (debugger_attach( process, current )) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 521 | { |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 522 | generate_startup_debug_events( process ); |
| 523 | /* FIXME: breakpoint exception event */ |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 524 | } |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 525 | release_object( process ); |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | /* Send a debug event */ |
| 529 | DECL_HANDLER(send_debug_event) |
| 530 | { |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 531 | struct debug_event *event; |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 532 | int code = req->event.code; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 533 | |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 534 | if ((code <= 0) || (code > RIP_EVENT)) |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 535 | { |
Alexandre Julliard | ff81d78 | 2000-03-08 12:01:30 +0000 | [diff] [blame] | 536 | fatal_protocol_error( current, "send_debug_event: bad code %d\n", code ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 537 | return; |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 538 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 539 | req->status = 0; |
Alexandre Julliard | 05f0b71 | 2000-03-09 18:18:41 +0000 | [diff] [blame^] | 540 | if (current->process->debugger && ((event = queue_debug_event( current, code, |
| 541 | NULL, &req->event )))) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 542 | { |
| 543 | /* wait for continue_debug_event */ |
Alexandre Julliard | 79b1ec8 | 2000-01-04 02:24:43 +0000 | [diff] [blame] | 544 | struct object *obj = &event->obj; |
| 545 | sleep_on( 1, &obj, 0, -1, build_send_event_reply ); |
| 546 | release_object( event ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 547 | } |
Alexandre Julliard | e712e07 | 1999-05-23 19:53:30 +0000 | [diff] [blame] | 548 | } |