| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 1 | #include <stdio.h> |
| Juan Cespedes | 504a385 | 2003-02-04 23:24:38 +0100 | [diff] [blame] | 2 | #include <stdlib.h> |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 3 | #include <string.h> |
| 4 | #include <errno.h> |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 5 | #include <unistd.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 6 | #include <sys/types.h> |
| Petr Machata | 89a5360 | 2007-01-25 18:05:44 +0100 | [diff] [blame] | 7 | #include <sys/wait.h> |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 8 | #include "ptrace.h" |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 9 | #include <asm/unistd.h> |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 10 | #include <assert.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 11 | |
| Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 12 | #include "common.h" |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 13 | |
| 14 | /* If the system headers did not provide the constants, hard-code the normal |
| 15 | values. */ |
| 16 | #ifndef PTRACE_EVENT_FORK |
| 17 | |
| 18 | #define PTRACE_OLDSETOPTIONS 21 |
| 19 | #define PTRACE_SETOPTIONS 0x4200 |
| 20 | #define PTRACE_GETEVENTMSG 0x4201 |
| 21 | |
| 22 | /* options set using PTRACE_SETOPTIONS */ |
| 23 | #define PTRACE_O_TRACESYSGOOD 0x00000001 |
| 24 | #define PTRACE_O_TRACEFORK 0x00000002 |
| 25 | #define PTRACE_O_TRACEVFORK 0x00000004 |
| 26 | #define PTRACE_O_TRACECLONE 0x00000008 |
| 27 | #define PTRACE_O_TRACEEXEC 0x00000010 |
| 28 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 |
| 29 | #define PTRACE_O_TRACEEXIT 0x00000040 |
| 30 | |
| 31 | /* Wait extended result codes for the above trace options. */ |
| 32 | #define PTRACE_EVENT_FORK 1 |
| 33 | #define PTRACE_EVENT_VFORK 2 |
| 34 | #define PTRACE_EVENT_CLONE 3 |
| 35 | #define PTRACE_EVENT_EXEC 4 |
| 36 | #define PTRACE_EVENT_VFORK_DONE 5 |
| 37 | #define PTRACE_EVENT_EXIT 6 |
| 38 | |
| 39 | #endif /* PTRACE_EVENT_FORK */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 40 | |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 41 | #ifdef ARCH_HAVE_UMOVELONG |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 42 | extern int arch_umovelong (Process *, void *, long *, arg_type_info *); |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 43 | int |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 44 | umovelong (Process *proc, void *addr, long *result, arg_type_info *info) { |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 45 | return arch_umovelong (proc, addr, result, info); |
| 46 | } |
| 47 | #else |
| 48 | /* Read a single long from the process's memory address 'addr' */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 49 | int |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 50 | umovelong (Process *proc, void *addr, long *result, arg_type_info *info) { |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 51 | long pointed_to; |
| 52 | |
| 53 | errno = 0; |
| 54 | pointed_to = ptrace (PTRACE_PEEKTEXT, proc->pid, addr, 0); |
| 55 | if (pointed_to == -1 && errno) |
| 56 | return -errno; |
| 57 | |
| 58 | *result = pointed_to; |
| Arnaud Patard | f16fcff | 2010-01-08 08:40:19 -0500 | [diff] [blame] | 59 | if (info) { |
| 60 | switch(info->type) { |
| 61 | case ARGTYPE_INT: |
| 62 | *result &= 0x00000000ffffffffUL; |
| 63 | default: |
| 64 | break; |
| 65 | }; |
| 66 | } |
| Luis Machado | 55c5feb | 2008-03-12 15:56:01 +0100 | [diff] [blame] | 67 | return 0; |
| 68 | } |
| 69 | #endif |
| 70 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 71 | void |
| 72 | trace_me(void) { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 73 | debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid()); |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 74 | if (ptrace(PTRACE_TRACEME, 0, 1, 0) < 0) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 75 | perror("PTRACE_TRACEME"); |
| 76 | exit(1); |
| 77 | } |
| 78 | } |
| 79 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 80 | int |
| 81 | trace_pid(pid_t pid) { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 82 | debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 83 | if (ptrace(PTRACE_ATTACH, pid, 1, 0) < 0) { |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 84 | return -1; |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 85 | } |
| Petr Machata | 89a5360 | 2007-01-25 18:05:44 +0100 | [diff] [blame] | 86 | |
| Juan Cespedes | 714ee9d | 2009-04-07 13:28:54 +0200 | [diff] [blame] | 87 | /* man ptrace: PTRACE_ATTACH attaches to the process specified |
| 88 | in pid. The child is sent a SIGSTOP, but will not |
| 89 | necessarily have stopped by the completion of this call; |
| 90 | use wait() to wait for the child to stop. */ |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 91 | if (waitpid (pid, NULL, __WALL) != pid) { |
| Juan Cespedes | 714ee9d | 2009-04-07 13:28:54 +0200 | [diff] [blame] | 92 | perror ("trace_pid: waitpid"); |
| Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 93 | return -1; |
| Juan Cespedes | 714ee9d | 2009-04-07 13:28:54 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 99 | void |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 100 | trace_set_options(Process *proc, pid_t pid) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 101 | if (proc->tracesysgood & 0x80) |
| 102 | return; |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 103 | |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 104 | debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 105 | |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 106 | long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK | |
| 107 | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE | |
| 108 | PTRACE_O_TRACEEXEC; |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 109 | if (ptrace(PTRACE_SETOPTIONS, pid, 0, options) < 0 && |
| 110 | ptrace(PTRACE_OLDSETOPTIONS, pid, 0, options) < 0) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 111 | perror("PTRACE_SETOPTIONS"); |
| 112 | return; |
| 113 | } |
| 114 | proc->tracesysgood |= 0x80; |
| 115 | } |
| 116 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 117 | void |
| 118 | untrace_pid(pid_t pid) { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 119 | debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid); |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 120 | ptrace(PTRACE_DETACH, pid, 1, 0); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 123 | void |
| 124 | continue_after_signal(pid_t pid, int signum) { |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 125 | Process *proc; |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 126 | |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 127 | debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", pid, signum); |
| 128 | |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 129 | proc = pid2proc(pid); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame^] | 130 | ptrace(PTRACE_SYSCALL, pid, 0, signum); |
| 131 | } |
| 132 | |
| 133 | static enum ecb_status |
| 134 | event_for_pid(Event * event, void * data) |
| 135 | { |
| 136 | if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data) |
| 137 | return ecb_yield; |
| 138 | return ecb_cont; |
| 139 | } |
| 140 | |
| 141 | static int |
| 142 | have_events_for(pid_t pid) |
| 143 | { |
| 144 | return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL; |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | continue_process(pid_t pid) |
| 149 | { |
| 150 | debug(DEBUG_PROCESS, "continue_process: pid=%d", pid); |
| 151 | //printf("continue_process %d\n", pid); |
| 152 | |
| 153 | /* Only really continue the process if there are no events in |
| 154 | the queue for this process. Otherwise just for the other |
| 155 | events to arrive. */ |
| 156 | if (!have_events_for(pid)) |
| 157 | /* We always trace syscalls to control fork(), |
| 158 | * clone(), execve()... */ |
| 159 | ptrace(PTRACE_SYSCALL, pid, 0, 0); |
| 160 | else |
| 161 | debug(DEBUG_PROCESS, |
| 162 | "putting off the continue, events in que."); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * This is used for bookkeeping related to PIDs that the event |
| 167 | * handlers work with. */ |
| 168 | struct pid_task { |
| 169 | pid_t pid; |
| 170 | int sigstopped; |
| 171 | int got_event; |
| 172 | int delivered; |
| 173 | } * pids; |
| 174 | |
| 175 | struct pid_set { |
| 176 | struct pid_task * tasks; |
| 177 | size_t count; |
| 178 | size_t alloc; |
| 179 | }; |
| 180 | |
| 181 | /** |
| 182 | * Breakpoint re-enablement. When we hit a breakpoint, we must |
| 183 | * disable it, single-step, and re-enable it. That single-step can be |
| 184 | * done only by one task in a task group, while others are stopped, |
| 185 | * otherwise the processes would race for who sees the breakpoint |
| 186 | * disabled and who doesn't. The following is to keep track of it |
| 187 | * all. |
| 188 | */ |
| 189 | struct process_stopping_handler |
| 190 | { |
| 191 | Event_Handler super; |
| 192 | |
| 193 | /* The task that is doing the re-enablement. */ |
| 194 | Process * task_enabling_breakpoint; |
| 195 | |
| 196 | /* The pointer being re-enabled. */ |
| 197 | Breakpoint * breakpoint_being_enabled; |
| 198 | |
| 199 | enum { |
| 200 | /* We are waiting for everyone to land in t/T. */ |
| 201 | psh_stopping = 0, |
| 202 | |
| 203 | /* We are doing the PTRACE_SINGLESTEP. */ |
| 204 | psh_singlestep, |
| 205 | |
| 206 | /* We are waiting for all the SIGSTOPs to arrive so |
| 207 | * that we can sink them. */ |
| 208 | psh_sinking, |
| 209 | } state; |
| 210 | |
| 211 | struct pid_set pids; |
| 212 | }; |
| 213 | |
| 214 | static enum pcb_status |
| 215 | task_stopped(Process * task, void * data) |
| 216 | { |
| 217 | int status; |
| 218 | |
| 219 | /* If the task is already stopped, don't worry about it. |
| 220 | * Likewise if it managed to become a zombie or terminate in |
| 221 | * the meantime. This can happen when the whole thread group |
| 222 | * is terminating. */ |
| 223 | switch (status = process_status(task->pid)) |
| 224 | case -1: |
| 225 | case 't': |
| 226 | case 'Z': |
| 227 | return pcb_cont; |
| 228 | |
| 229 | return pcb_stop; |
| 230 | } |
| 231 | |
| 232 | static struct pid_task * |
| 233 | get_task_info(struct pid_set * pids, pid_t pid) |
| 234 | { |
| 235 | size_t i; |
| 236 | for (i = 0; i < pids->count; ++i) |
| 237 | if (pids->tasks[i].pid == pid) |
| 238 | return &pids->tasks[i]; |
| 239 | |
| 240 | return NULL; |
| 241 | } |
| 242 | |
| 243 | static struct pid_task * |
| 244 | add_task_info(struct pid_set * pids, pid_t pid) |
| 245 | { |
| 246 | if (pids->count == pids->alloc) { |
| 247 | size_t ns = (2 * pids->alloc) ?: 4; |
| 248 | struct pid_task * n = realloc(pids->tasks, |
| 249 | sizeof(*pids->tasks) * ns); |
| 250 | if (n == NULL) |
| 251 | return NULL; |
| 252 | pids->tasks = n; |
| 253 | pids->alloc = ns; |
| 254 | } |
| 255 | struct pid_task * task_info = &pids->tasks[pids->count++]; |
| 256 | memset(task_info, 0, sizeof(*task_info)); |
| 257 | task_info->pid = pid; |
| 258 | return task_info; |
| 259 | } |
| 260 | |
| 261 | static enum pcb_status |
| 262 | send_sigstop(Process * task, void * data) |
| 263 | { |
| 264 | Process * leader = task->leader; |
| 265 | struct pid_set * pids = data; |
| 266 | |
| 267 | /* Look for pre-existing task record, or add new. */ |
| 268 | struct pid_task * task_info = get_task_info(pids, task->pid); |
| 269 | if (task_info == NULL) |
| 270 | task_info = add_task_info(pids, task->pid); |
| 271 | if (task_info == NULL) { |
| 272 | perror("send_sigstop: add_task_info"); |
| 273 | destroy_event_handler(leader); |
| 274 | /* Signal failure upwards. */ |
| 275 | return pcb_stop; |
| 276 | } |
| 277 | |
| 278 | /* This task still has not been attached to. It should be |
| 279 | stopped by the kernel. */ |
| 280 | if (task->state == STATE_BEING_CREATED) |
| 281 | return pcb_cont; |
| 282 | |
| 283 | /* Don't bother sending SIGSTOP if we are already stopped, or |
| 284 | * if we sent the SIGSTOP already, which happens when we |
| 285 | * inherit the handler from breakpoint re-enablement. */ |
| 286 | if (task_stopped(task, NULL) == pcb_cont) |
| 287 | return pcb_cont; |
| 288 | if (task_info->sigstopped) { |
| 289 | if (!task_info->delivered) |
| 290 | return pcb_cont; |
| 291 | task_info->delivered = 0; |
| 292 | } |
| 293 | |
| 294 | if (task_kill(task->pid, SIGSTOP) >= 0) { |
| 295 | debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid); |
| 296 | task_info->sigstopped = 1; |
| 297 | } else |
| 298 | fprintf(stderr, |
| 299 | "Warning: couldn't send SIGSTOP to %d\n", task->pid); |
| 300 | |
| 301 | return pcb_cont; |
| 302 | } |
| 303 | |
| 304 | static void |
| 305 | process_stopping_done(struct process_stopping_handler * self, Process * leader) |
| 306 | { |
| 307 | debug(DEBUG_PROCESS, "process stopping done %d", |
| 308 | self->task_enabling_breakpoint->pid); |
| 309 | size_t i; |
| 310 | for (i = 0; i < self->pids.count; ++i) |
| 311 | if (self->pids.tasks[i].delivered) |
| 312 | continue_process(self->pids.tasks[i].pid); |
| 313 | continue_process(self->task_enabling_breakpoint->pid); |
| 314 | destroy_event_handler(leader); |
| 315 | } |
| 316 | |
| 317 | static void |
| 318 | handle_stopping_event(struct pid_task * task_info, Event ** eventp) |
| 319 | { |
| 320 | /* Mark all events, so that we know whom to SIGCONT later. */ |
| 321 | if (task_info != NULL && task_info->sigstopped) |
| 322 | task_info->got_event = 1; |
| 323 | |
| 324 | Event * event = *eventp; |
| 325 | |
| 326 | /* In every state, sink SIGSTOP events for tasks that it was |
| 327 | * sent to. */ |
| 328 | if (task_info != NULL |
| 329 | && event->type == EVENT_SIGNAL |
| 330 | && event->e_un.signum == SIGSTOP) { |
| 331 | debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid); |
| 332 | if (task_info->sigstopped |
| 333 | && !task_info->delivered) { |
| 334 | task_info->delivered = 1; |
| 335 | *eventp = NULL; // sink the event |
| 336 | } else |
| 337 | fprintf(stderr, "suspicious: %d got SIGSTOP, but " |
| 338 | "sigstopped=%d and delivered=%d\n", |
| 339 | task_info->pid, task_info->sigstopped, |
| 340 | task_info->delivered); |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 341 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 342 | } |
| 343 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame^] | 344 | /* Some SIGSTOPs may have not been delivered to their respective tasks |
| 345 | * yet. They are still in the queue. If we have seen an event for |
| 346 | * that process, continue it, so that the SIGSTOP can be delivered and |
| 347 | * caught by ltrace. */ |
| 348 | static void |
| 349 | continue_for_sigstop_delivery(struct pid_set * pids) |
| 350 | { |
| 351 | size_t i; |
| 352 | for (i = 0; i < pids->count; ++i) { |
| 353 | if (pids->tasks[i].sigstopped |
| 354 | && !pids->tasks[i].delivered |
| 355 | && pids->tasks[i].got_event) { |
| 356 | debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery", |
| 357 | pids->tasks[i].pid); |
| 358 | ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0); |
| 359 | } |
| 360 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 361 | } |
| 362 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame^] | 363 | static int |
| 364 | event_exit_or_none_p(Event * event) |
| Petr Machata | f789c9c | 2011-07-09 10:54:27 +0200 | [diff] [blame] | 365 | { |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame^] | 366 | return event == NULL |
| 367 | || event->type == EVENT_EXIT |
| 368 | || event->type == EVENT_EXIT_SIGNAL |
| 369 | || event->type == EVENT_NONE; |
| 370 | } |
| 371 | |
| 372 | static int |
| 373 | await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info, |
| 374 | Event * event) |
| 375 | { |
| 376 | /* If we still didn't get our SIGSTOP, continue the process |
| 377 | * and carry on. */ |
| 378 | if (event != NULL && !event_exit_or_none_p(event) |
| 379 | && task_info != NULL && task_info->sigstopped) { |
| 380 | debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery", |
| 381 | task_info->pid); |
| 382 | /* We should get the signal the first thing |
| 383 | * after this, so it should be OK to continue |
| 384 | * even if we are over a breakpoint. */ |
| 385 | ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0); |
| 386 | |
| 387 | } else { |
| 388 | /* If all SIGSTOPs were delivered, uninstall the |
| 389 | * handler and continue everyone. */ |
| 390 | /* XXX I suspect that we should check tasks that are |
| 391 | * still around. Is things are now, there should be a |
| 392 | * race between waiting for everyone to stop and one |
| 393 | * of the tasks exiting. */ |
| 394 | int all_clear = 1; |
| 395 | size_t i; |
| 396 | for (i = 0; i < pids->count; ++i) |
| 397 | if (pids->tasks[i].sigstopped |
| 398 | && !pids->tasks[i].delivered) { |
| 399 | all_clear = 0; |
| 400 | break; |
| 401 | } |
| 402 | return all_clear; |
| 403 | } |
| 404 | |
| 405 | return 0; |
| 406 | } |
| 407 | |
| 408 | /* This event handler is installed when we are in the process of |
| 409 | * stopping the whole thread group to do the pointer re-enablement for |
| 410 | * one of the threads. We pump all events to the queue for later |
| 411 | * processing while we wait for all the threads to stop. When this |
| 412 | * happens, we let the re-enablement thread to PTRACE_SINGLESTEP, |
| 413 | * re-enable, and continue everyone. */ |
| 414 | static Event * |
| 415 | process_stopping_on_event(Event_Handler * super, Event * event) |
| 416 | { |
| 417 | struct process_stopping_handler * self = (void *)super; |
| 418 | Process * task = event->proc; |
| 419 | Process * leader = task->leader; |
| 420 | |
| 421 | debug(DEBUG_PROCESS, |
| 422 | "pid %d; event type %d; state %d", |
| 423 | task->pid, event->type, self->state); |
| 424 | |
| 425 | struct pid_task * task_info = get_task_info(&self->pids, task->pid); |
| 426 | if (task_info == NULL) |
| 427 | fprintf(stderr, "new task??? %d\n", task->pid); |
| 428 | handle_stopping_event(task_info, &event); |
| 429 | |
| 430 | int state = self->state; |
| 431 | int event_to_queue = !event_exit_or_none_p(event); |
| 432 | |
| 433 | switch (state) { |
| 434 | case psh_stopping: |
| 435 | /* If everyone is stopped, singlestep. */ |
| 436 | if (each_task(leader, &task_stopped, NULL) == NULL) { |
| 437 | debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d", |
| 438 | self->task_enabling_breakpoint->pid); |
| 439 | ptrace(PTRACE_SINGLESTEP, |
| 440 | self->task_enabling_breakpoint->pid, 0, 0); |
| 441 | self->state = state = psh_singlestep; |
| 442 | } |
| 443 | break; |
| 444 | |
| 445 | case psh_singlestep: { |
| 446 | /* In singlestep state, breakpoint signifies that we |
| 447 | * have now stepped, and can re-enable the breakpoint. */ |
| 448 | if (event != NULL |
| 449 | && task == self->task_enabling_breakpoint) { |
| 450 | /* Essentially we don't care what event caused |
| 451 | * the thread to stop. We can do the |
| 452 | * re-enablement now. */ |
| 453 | enable_breakpoint(self->task_enabling_breakpoint, |
| 454 | self->breakpoint_being_enabled); |
| 455 | |
| 456 | continue_for_sigstop_delivery(&self->pids); |
| 457 | |
| 458 | self->breakpoint_being_enabled = NULL; |
| 459 | self->state = state = psh_sinking; |
| 460 | |
| 461 | if (event->type == EVENT_BREAKPOINT) |
| 462 | event = NULL; // handled |
| 463 | } else |
| 464 | break; |
| 465 | } |
| 466 | |
| 467 | /* fall-through */ |
| 468 | |
| 469 | case psh_sinking: |
| 470 | if (await_sigstop_delivery(&self->pids, task_info, event)) |
| 471 | process_stopping_done(self, leader); |
| 472 | } |
| 473 | |
| 474 | if (event != NULL && event_to_queue) { |
| 475 | enque_event(event); |
| 476 | event = NULL; // sink the event |
| 477 | } |
| 478 | |
| 479 | return event; |
| 480 | } |
| 481 | |
| 482 | static void |
| 483 | process_stopping_destroy(Event_Handler * super) |
| 484 | { |
| 485 | struct process_stopping_handler * self = (void *)super; |
| 486 | if (self->breakpoint_being_enabled != NULL) |
| 487 | enable_breakpoint(self->task_enabling_breakpoint, |
| 488 | self->breakpoint_being_enabled); |
| 489 | free(self->pids.tasks); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 490 | } |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 491 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 492 | void |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 493 | continue_after_breakpoint(Process *proc, Breakpoint *sbp) |
| 494 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 495 | if (sbp->enabled) |
| Petr Machata | f789c9c | 2011-07-09 10:54:27 +0200 | [diff] [blame] | 496 | disable_breakpoint(proc, sbp); |
| 497 | |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 498 | set_instruction_pointer(proc, sbp->addr); |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 499 | if (sbp->enabled == 0) { |
| 500 | continue_process(proc->pid); |
| 501 | } else { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 502 | debug(DEBUG_PROCESS, |
| 503 | "continue_after_breakpoint: pid=%d, addr=%p", |
| 504 | proc->pid, sbp->addr); |
| Arnaud Patard | f3d1c53 | 2010-01-08 08:40:04 -0500 | [diff] [blame] | 505 | #if defined __sparc__ || defined __ia64___ || defined __mips__ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 506 | /* we don't want to singlestep here */ |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 507 | continue_process(proc->pid); |
| 508 | #else |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame^] | 509 | struct process_stopping_handler * handler |
| 510 | = calloc(sizeof(*handler), 1); |
| 511 | if (handler == NULL) { |
| 512 | perror("malloc breakpoint disable handler"); |
| 513 | fatal: |
| 514 | /* Carry on not bothering to re-enable. */ |
| 515 | continue_process(proc->pid); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | handler->super.on_event = process_stopping_on_event; |
| 520 | handler->super.destroy = process_stopping_destroy; |
| 521 | handler->task_enabling_breakpoint = proc; |
| 522 | handler->breakpoint_being_enabled = sbp; |
| 523 | install_event_handler(proc->leader, &handler->super); |
| 524 | |
| 525 | if (each_task(proc->leader, &send_sigstop, |
| 526 | &handler->pids) != NULL) |
| 527 | goto fatal; |
| 528 | |
| 529 | /* And deliver the first fake event, in case all the |
| 530 | * conditions are already fulfilled. */ |
| 531 | Event ev; |
| 532 | ev.type = EVENT_NONE; |
| 533 | ev.proc = proc; |
| 534 | process_stopping_on_event(&handler->super, &ev); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 535 | #endif |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 536 | } |
| 537 | } |
| 538 | |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 539 | size_t |
| 540 | umovebytes(Process *proc, void *addr, void *laddr, size_t len) { |
| 541 | |
| 542 | union { |
| 543 | long a; |
| 544 | char c[sizeof(long)]; |
| 545 | } a; |
| Zachary T Welch | ba6aca2 | 2010-12-08 18:55:09 -0800 | [diff] [blame] | 546 | int started = 0; |
| 547 | size_t offset = 0, bytes_read = 0; |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 548 | |
| 549 | while (offset < len) { |
| 550 | a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); |
| 551 | if (a.a == -1 && errno) { |
| 552 | if (started && errno == EIO) |
| 553 | return bytes_read; |
| 554 | else |
| 555 | return -1; |
| 556 | } |
| 557 | started = 1; |
| 558 | |
| 559 | if (len - offset >= sizeof(long)) { |
| 560 | memcpy(laddr + offset, &a.c[0], sizeof(long)); |
| 561 | bytes_read += sizeof(long); |
| 562 | } |
| 563 | else { |
| 564 | memcpy(laddr + offset, &a.c[0], len - offset); |
| 565 | bytes_read += (len - offset); |
| 566 | } |
| 567 | offset += sizeof(long); |
| 568 | } |
| 569 | |
| 570 | return bytes_read; |
| 571 | } |
| 572 | |
| Steve Fink | 7bafff0 | 2006-08-07 04:50:42 +0200 | [diff] [blame] | 573 | /* Read a series of bytes starting at the process's memory address |
| 574 | 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes |
| 575 | have been read. |
| 576 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 577 | int |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 578 | umovestr(Process *proc, void *addr, int len, void *laddr) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 579 | union { |
| 580 | long a; |
| 581 | char c[sizeof(long)]; |
| 582 | } a; |
| Zachary T Welch | ba6aca2 | 2010-12-08 18:55:09 -0800 | [diff] [blame] | 583 | unsigned i; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 584 | int offset = 0; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 585 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 586 | while (offset < len) { |
| 587 | a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); |
| 588 | for (i = 0; i < sizeof(long); i++) { |
| Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 589 | if (a.c[i] && offset + (signed)i < len) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 590 | *(char *)(laddr + offset + i) = a.c[i]; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 591 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 592 | *(char *)(laddr + offset + i) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | } |
| 596 | offset += sizeof(long); |
| 597 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 598 | *(char *)(laddr + offset) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 599 | return 0; |
| 600 | } |