| 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 |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 167 | * handlers work with. |
| 168 | */ |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 169 | struct pid_task { |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 170 | pid_t pid; /* This may be 0 for tasks that exited |
| 171 | * mid-handling. */ |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 172 | int sigstopped; |
| 173 | int got_event; |
| 174 | int delivered; |
| 175 | } * pids; |
| 176 | |
| 177 | struct pid_set { |
| 178 | struct pid_task * tasks; |
| 179 | size_t count; |
| 180 | size_t alloc; |
| 181 | }; |
| 182 | |
| 183 | /** |
| 184 | * Breakpoint re-enablement. When we hit a breakpoint, we must |
| 185 | * disable it, single-step, and re-enable it. That single-step can be |
| 186 | * done only by one task in a task group, while others are stopped, |
| 187 | * otherwise the processes would race for who sees the breakpoint |
| 188 | * disabled and who doesn't. The following is to keep track of it |
| 189 | * all. |
| 190 | */ |
| 191 | struct process_stopping_handler |
| 192 | { |
| 193 | Event_Handler super; |
| 194 | |
| 195 | /* The task that is doing the re-enablement. */ |
| 196 | Process * task_enabling_breakpoint; |
| 197 | |
| 198 | /* The pointer being re-enabled. */ |
| 199 | Breakpoint * breakpoint_being_enabled; |
| 200 | |
| 201 | enum { |
| 202 | /* We are waiting for everyone to land in t/T. */ |
| 203 | psh_stopping = 0, |
| 204 | |
| 205 | /* We are doing the PTRACE_SINGLESTEP. */ |
| 206 | psh_singlestep, |
| 207 | |
| 208 | /* We are waiting for all the SIGSTOPs to arrive so |
| 209 | * that we can sink them. */ |
| 210 | psh_sinking, |
| 211 | } state; |
| 212 | |
| 213 | struct pid_set pids; |
| 214 | }; |
| 215 | |
| 216 | static enum pcb_status |
| 217 | task_stopped(Process * task, void * data) |
| 218 | { |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 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. */ |
| Petr Machata | 617ff0b | 2011-10-06 14:23:24 +0200 | [diff] [blame] | 223 | switch (process_status(task->pid)) { |
| 224 | case ps_invalid: |
| 225 | case ps_tracing_stop: |
| 226 | case ps_zombie: |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 227 | return pcb_cont; |
| Petr Machata | 617ff0b | 2011-10-06 14:23:24 +0200 | [diff] [blame] | 228 | default: |
| 229 | return pcb_stop; |
| 230 | } |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | static struct pid_task * |
| 234 | get_task_info(struct pid_set * pids, pid_t pid) |
| 235 | { |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 236 | assert(pid != 0); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 237 | size_t i; |
| 238 | for (i = 0; i < pids->count; ++i) |
| 239 | if (pids->tasks[i].pid == pid) |
| 240 | return &pids->tasks[i]; |
| 241 | |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static struct pid_task * |
| 246 | add_task_info(struct pid_set * pids, pid_t pid) |
| 247 | { |
| 248 | if (pids->count == pids->alloc) { |
| 249 | size_t ns = (2 * pids->alloc) ?: 4; |
| 250 | struct pid_task * n = realloc(pids->tasks, |
| 251 | sizeof(*pids->tasks) * ns); |
| 252 | if (n == NULL) |
| 253 | return NULL; |
| 254 | pids->tasks = n; |
| 255 | pids->alloc = ns; |
| 256 | } |
| 257 | struct pid_task * task_info = &pids->tasks[pids->count++]; |
| 258 | memset(task_info, 0, sizeof(*task_info)); |
| 259 | task_info->pid = pid; |
| 260 | return task_info; |
| 261 | } |
| 262 | |
| 263 | static enum pcb_status |
| 264 | send_sigstop(Process * task, void * data) |
| 265 | { |
| 266 | Process * leader = task->leader; |
| 267 | struct pid_set * pids = data; |
| 268 | |
| 269 | /* Look for pre-existing task record, or add new. */ |
| 270 | struct pid_task * task_info = get_task_info(pids, task->pid); |
| 271 | if (task_info == NULL) |
| 272 | task_info = add_task_info(pids, task->pid); |
| 273 | if (task_info == NULL) { |
| 274 | perror("send_sigstop: add_task_info"); |
| 275 | destroy_event_handler(leader); |
| 276 | /* Signal failure upwards. */ |
| 277 | return pcb_stop; |
| 278 | } |
| 279 | |
| 280 | /* This task still has not been attached to. It should be |
| 281 | stopped by the kernel. */ |
| 282 | if (task->state == STATE_BEING_CREATED) |
| 283 | return pcb_cont; |
| 284 | |
| 285 | /* Don't bother sending SIGSTOP if we are already stopped, or |
| 286 | * if we sent the SIGSTOP already, which happens when we |
| 287 | * inherit the handler from breakpoint re-enablement. */ |
| 288 | if (task_stopped(task, NULL) == pcb_cont) |
| 289 | return pcb_cont; |
| 290 | if (task_info->sigstopped) { |
| 291 | if (!task_info->delivered) |
| 292 | return pcb_cont; |
| 293 | task_info->delivered = 0; |
| 294 | } |
| 295 | |
| 296 | if (task_kill(task->pid, SIGSTOP) >= 0) { |
| 297 | debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid); |
| 298 | task_info->sigstopped = 1; |
| 299 | } else |
| 300 | fprintf(stderr, |
| 301 | "Warning: couldn't send SIGSTOP to %d\n", task->pid); |
| 302 | |
| 303 | return pcb_cont; |
| 304 | } |
| 305 | |
| 306 | static void |
| 307 | process_stopping_done(struct process_stopping_handler * self, Process * leader) |
| 308 | { |
| 309 | debug(DEBUG_PROCESS, "process stopping done %d", |
| 310 | self->task_enabling_breakpoint->pid); |
| 311 | size_t i; |
| 312 | for (i = 0; i < self->pids.count; ++i) |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 313 | if (self->pids.tasks[i].pid != 0 |
| 314 | && self->pids.tasks[i].delivered) |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 315 | continue_process(self->pids.tasks[i].pid); |
| 316 | continue_process(self->task_enabling_breakpoint->pid); |
| 317 | destroy_event_handler(leader); |
| 318 | } |
| 319 | |
| 320 | static void |
| 321 | handle_stopping_event(struct pid_task * task_info, Event ** eventp) |
| 322 | { |
| 323 | /* Mark all events, so that we know whom to SIGCONT later. */ |
| 324 | if (task_info != NULL && task_info->sigstopped) |
| 325 | task_info->got_event = 1; |
| 326 | |
| 327 | Event * event = *eventp; |
| 328 | |
| 329 | /* In every state, sink SIGSTOP events for tasks that it was |
| 330 | * sent to. */ |
| 331 | if (task_info != NULL |
| 332 | && event->type == EVENT_SIGNAL |
| 333 | && event->e_un.signum == SIGSTOP) { |
| 334 | debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid); |
| 335 | if (task_info->sigstopped |
| 336 | && !task_info->delivered) { |
| 337 | task_info->delivered = 1; |
| 338 | *eventp = NULL; // sink the event |
| 339 | } else |
| 340 | fprintf(stderr, "suspicious: %d got SIGSTOP, but " |
| 341 | "sigstopped=%d and delivered=%d\n", |
| 342 | task_info->pid, task_info->sigstopped, |
| 343 | task_info->delivered); |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 344 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 345 | } |
| 346 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 347 | /* Some SIGSTOPs may have not been delivered to their respective tasks |
| 348 | * yet. They are still in the queue. If we have seen an event for |
| 349 | * that process, continue it, so that the SIGSTOP can be delivered and |
| 350 | * caught by ltrace. */ |
| 351 | static void |
| 352 | continue_for_sigstop_delivery(struct pid_set * pids) |
| 353 | { |
| 354 | size_t i; |
| 355 | for (i = 0; i < pids->count; ++i) { |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 356 | if (pids->tasks[i].pid != 0 |
| 357 | && pids->tasks[i].sigstopped |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 358 | && !pids->tasks[i].delivered |
| 359 | && pids->tasks[i].got_event) { |
| 360 | debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery", |
| 361 | pids->tasks[i].pid); |
| 362 | ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0); |
| 363 | } |
| 364 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 365 | } |
| 366 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 367 | static int |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 368 | event_exit_p(Event * event) |
| 369 | { |
| 370 | return event != NULL && (event->type == EVENT_EXIT |
| 371 | || event->type == EVENT_EXIT_SIGNAL); |
| 372 | } |
| 373 | |
| 374 | static int |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 375 | event_exit_or_none_p(Event * event) |
| Petr Machata | f789c9c | 2011-07-09 10:54:27 +0200 | [diff] [blame] | 376 | { |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 377 | return event == NULL || event_exit_p(event) |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 378 | || event->type == EVENT_NONE; |
| 379 | } |
| 380 | |
| 381 | static int |
| 382 | await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info, |
| 383 | Event * event) |
| 384 | { |
| 385 | /* If we still didn't get our SIGSTOP, continue the process |
| 386 | * and carry on. */ |
| 387 | if (event != NULL && !event_exit_or_none_p(event) |
| 388 | && task_info != NULL && task_info->sigstopped) { |
| 389 | debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery", |
| 390 | task_info->pid); |
| 391 | /* We should get the signal the first thing |
| 392 | * after this, so it should be OK to continue |
| 393 | * even if we are over a breakpoint. */ |
| 394 | ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0); |
| 395 | |
| 396 | } else { |
| 397 | /* If all SIGSTOPs were delivered, uninstall the |
| 398 | * handler and continue everyone. */ |
| 399 | /* XXX I suspect that we should check tasks that are |
| 400 | * still around. Is things are now, there should be a |
| 401 | * race between waiting for everyone to stop and one |
| 402 | * of the tasks exiting. */ |
| 403 | int all_clear = 1; |
| 404 | size_t i; |
| 405 | for (i = 0; i < pids->count; ++i) |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 406 | if (pids->tasks[i].pid != 0 |
| 407 | && pids->tasks[i].sigstopped |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 408 | && !pids->tasks[i].delivered) { |
| 409 | all_clear = 0; |
| 410 | break; |
| 411 | } |
| 412 | return all_clear; |
| 413 | } |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | /* This event handler is installed when we are in the process of |
| 419 | * stopping the whole thread group to do the pointer re-enablement for |
| 420 | * one of the threads. We pump all events to the queue for later |
| 421 | * processing while we wait for all the threads to stop. When this |
| 422 | * happens, we let the re-enablement thread to PTRACE_SINGLESTEP, |
| 423 | * re-enable, and continue everyone. */ |
| 424 | static Event * |
| 425 | process_stopping_on_event(Event_Handler * super, Event * event) |
| 426 | { |
| 427 | struct process_stopping_handler * self = (void *)super; |
| 428 | Process * task = event->proc; |
| 429 | Process * leader = task->leader; |
| 430 | |
| 431 | debug(DEBUG_PROCESS, |
| 432 | "pid %d; event type %d; state %d", |
| 433 | task->pid, event->type, self->state); |
| 434 | |
| 435 | struct pid_task * task_info = get_task_info(&self->pids, task->pid); |
| 436 | if (task_info == NULL) |
| 437 | fprintf(stderr, "new task??? %d\n", task->pid); |
| 438 | handle_stopping_event(task_info, &event); |
| 439 | |
| 440 | int state = self->state; |
| 441 | int event_to_queue = !event_exit_or_none_p(event); |
| 442 | |
| 443 | switch (state) { |
| 444 | case psh_stopping: |
| 445 | /* If everyone is stopped, singlestep. */ |
| 446 | if (each_task(leader, &task_stopped, NULL) == NULL) { |
| 447 | debug(DEBUG_PROCESS, "all stopped, now SINGLESTEP %d", |
| 448 | self->task_enabling_breakpoint->pid); |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 449 | if (ptrace(PTRACE_SINGLESTEP, |
| 450 | self->task_enabling_breakpoint->pid, 0, 0)) |
| 451 | perror("PTRACE_SINGLESTEP"); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 452 | self->state = state = psh_singlestep; |
| 453 | } |
| 454 | break; |
| 455 | |
| 456 | case psh_singlestep: { |
| 457 | /* In singlestep state, breakpoint signifies that we |
| 458 | * have now stepped, and can re-enable the breakpoint. */ |
| 459 | if (event != NULL |
| 460 | && task == self->task_enabling_breakpoint) { |
| 461 | /* Essentially we don't care what event caused |
| 462 | * the thread to stop. We can do the |
| 463 | * re-enablement now. */ |
| 464 | enable_breakpoint(self->task_enabling_breakpoint, |
| 465 | self->breakpoint_being_enabled); |
| 466 | |
| 467 | continue_for_sigstop_delivery(&self->pids); |
| 468 | |
| 469 | self->breakpoint_being_enabled = NULL; |
| 470 | self->state = state = psh_sinking; |
| 471 | |
| 472 | if (event->type == EVENT_BREAKPOINT) |
| 473 | event = NULL; // handled |
| 474 | } else |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | /* fall-through */ |
| 479 | |
| 480 | case psh_sinking: |
| 481 | if (await_sigstop_delivery(&self->pids, task_info, event)) |
| 482 | process_stopping_done(self, leader); |
| 483 | } |
| 484 | |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 485 | if (event_exit_p(event) && task_info != NULL) |
| 486 | task_info->pid = 0; |
| 487 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 488 | if (event != NULL && event_to_queue) { |
| 489 | enque_event(event); |
| 490 | event = NULL; // sink the event |
| 491 | } |
| 492 | |
| 493 | return event; |
| 494 | } |
| 495 | |
| 496 | static void |
| 497 | process_stopping_destroy(Event_Handler * super) |
| 498 | { |
| 499 | struct process_stopping_handler * self = (void *)super; |
| 500 | if (self->breakpoint_being_enabled != NULL) |
| 501 | enable_breakpoint(self->task_enabling_breakpoint, |
| 502 | self->breakpoint_being_enabled); |
| 503 | free(self->pids.tasks); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 504 | } |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 505 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 506 | void |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 507 | continue_after_breakpoint(Process *proc, Breakpoint *sbp) |
| 508 | { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 509 | if (sbp->enabled) |
| Petr Machata | f789c9c | 2011-07-09 10:54:27 +0200 | [diff] [blame] | 510 | disable_breakpoint(proc, sbp); |
| 511 | |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 512 | set_instruction_pointer(proc, sbp->addr); |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 513 | if (sbp->enabled == 0) { |
| 514 | continue_process(proc->pid); |
| 515 | } else { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 516 | debug(DEBUG_PROCESS, |
| 517 | "continue_after_breakpoint: pid=%d, addr=%p", |
| 518 | proc->pid, sbp->addr); |
| Arnaud Patard | f3d1c53 | 2010-01-08 08:40:04 -0500 | [diff] [blame] | 519 | #if defined __sparc__ || defined __ia64___ || defined __mips__ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 520 | /* we don't want to singlestep here */ |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 521 | continue_process(proc->pid); |
| 522 | #else |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 523 | struct process_stopping_handler * handler |
| 524 | = calloc(sizeof(*handler), 1); |
| 525 | if (handler == NULL) { |
| 526 | perror("malloc breakpoint disable handler"); |
| 527 | fatal: |
| 528 | /* Carry on not bothering to re-enable. */ |
| 529 | continue_process(proc->pid); |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | handler->super.on_event = process_stopping_on_event; |
| 534 | handler->super.destroy = process_stopping_destroy; |
| 535 | handler->task_enabling_breakpoint = proc; |
| 536 | handler->breakpoint_being_enabled = sbp; |
| 537 | install_event_handler(proc->leader, &handler->super); |
| 538 | |
| 539 | if (each_task(proc->leader, &send_sigstop, |
| 540 | &handler->pids) != NULL) |
| 541 | goto fatal; |
| 542 | |
| 543 | /* And deliver the first fake event, in case all the |
| 544 | * conditions are already fulfilled. */ |
| 545 | Event ev; |
| 546 | ev.type = EVENT_NONE; |
| 547 | ev.proc = proc; |
| 548 | process_stopping_on_event(&handler->super, &ev); |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 549 | #endif |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 550 | } |
| 551 | } |
| 552 | |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 553 | /** |
| 554 | * Ltrace exit. When we are about to exit, we have to go through all |
| 555 | * the processes, stop them all, remove all the breakpoints, and then |
| 556 | * detach the processes that we attached to using -p. If we left the |
| 557 | * other tasks running, they might hit stray return breakpoints and |
| 558 | * produce artifacts, so we better stop everyone, even if it's a bit |
| 559 | * of extra work. |
| 560 | */ |
| 561 | struct ltrace_exiting_handler |
| 562 | { |
| 563 | Event_Handler super; |
| 564 | struct pid_set pids; |
| 565 | }; |
| 566 | |
| 567 | static enum pcb_status |
| 568 | remove_task(Process * task, void * data) |
| 569 | { |
| 570 | /* Don't untrace leader just yet. */ |
| 571 | if (task != data) |
| 572 | remove_process(task); |
| 573 | return pcb_cont; |
| 574 | } |
| 575 | |
| 576 | static enum pcb_status |
| 577 | untrace_task(Process * task, void * data) |
| 578 | { |
| 579 | untrace_pid(task->pid); |
| 580 | return pcb_cont; |
| 581 | } |
| 582 | |
| 583 | static Event * |
| 584 | ltrace_exiting_on_event(Event_Handler * super, Event * event) |
| 585 | { |
| 586 | struct ltrace_exiting_handler * self = (void *)super; |
| 587 | Process * task = event->proc; |
| 588 | Process * leader = task->leader; |
| 589 | |
| 590 | debug(DEBUG_PROCESS, "pid %d; event type %d", task->pid, event->type); |
| 591 | |
| 592 | struct pid_task * task_info = get_task_info(&self->pids, task->pid); |
| 593 | handle_stopping_event(task_info, &event); |
| 594 | |
| 595 | if (await_sigstop_delivery(&self->pids, task_info, event)) { |
| 596 | debug(DEBUG_PROCESS, "all SIGSTOPs delivered %d", leader->pid); |
| 597 | disable_all_breakpoints(leader); |
| 598 | |
| 599 | /* Now untrace the process, if it was attached to by -p. */ |
| 600 | struct opt_p_t * it; |
| 601 | for (it = opt_p; it != NULL; it = it->next) { |
| 602 | Process * proc = pid2proc(it->pid); |
| 603 | if (proc == NULL) |
| 604 | continue; |
| 605 | if (proc->leader == leader) { |
| 606 | each_task(leader, &untrace_task, NULL); |
| 607 | break; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | each_task(leader, &remove_task, leader); |
| 612 | destroy_event_handler(leader); |
| 613 | remove_task(leader, NULL); |
| 614 | return NULL; |
| 615 | } |
| 616 | |
| 617 | /* Sink all non-exit events. We are about to exit, so we |
| 618 | * don't bother with queuing them. */ |
| 619 | if (event_exit_or_none_p(event)) |
| 620 | return event; |
| 621 | else |
| 622 | return NULL; |
| 623 | } |
| 624 | |
| 625 | static void |
| 626 | ltrace_exiting_destroy(Event_Handler * super) |
| 627 | { |
| 628 | struct ltrace_exiting_handler * self = (void *)super; |
| 629 | free(self->pids.tasks); |
| 630 | } |
| 631 | |
| 632 | static int |
| 633 | ltrace_exiting_install_handler(Process * proc) |
| 634 | { |
| 635 | /* Only install to leader. */ |
| 636 | if (proc->leader != proc) |
| 637 | return 0; |
| 638 | |
| 639 | /* Perhaps we are already installed, if the user passed |
| 640 | * several -p options that are tasks of one process. */ |
| 641 | if (proc->event_handler != NULL |
| 642 | && proc->event_handler->on_event == <race_exiting_on_event) |
| 643 | return 0; |
| 644 | |
| 645 | struct ltrace_exiting_handler * handler |
| 646 | = calloc(sizeof(*handler), 1); |
| 647 | if (handler == NULL) { |
| 648 | perror("malloc exiting handler"); |
| 649 | fatal: |
| 650 | /* XXXXXXXXXXXXXXXXXXX fixme */ |
| 651 | return -1; |
| 652 | } |
| 653 | |
| 654 | /* If we are in the middle of breakpoint, extract the |
| 655 | * pid-state information from that handler so that we can take |
| 656 | * over the SIGSTOP handling. */ |
| 657 | if (proc->event_handler != NULL) { |
| 658 | debug(DEBUG_PROCESS, "taking over breakpoint handling"); |
| 659 | assert(proc->event_handler->on_event |
| 660 | == &process_stopping_on_event); |
| 661 | struct process_stopping_handler * other |
| 662 | = (void *)proc->event_handler; |
| 663 | size_t i; |
| 664 | for (i = 0; i < other->pids.count; ++i) { |
| 665 | struct pid_task * oti = &other->pids.tasks[i]; |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame^] | 666 | if (oti->pid == 0) |
| 667 | continue; |
| 668 | |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 669 | struct pid_task * task_info |
| 670 | = add_task_info(&handler->pids, oti->pid); |
| 671 | if (task_info == NULL) { |
| 672 | perror("ltrace_exiting_install_handler" |
| 673 | ":add_task_info"); |
| 674 | goto fatal; |
| 675 | } |
| 676 | /* Copy over the state. */ |
| 677 | *task_info = *oti; |
| 678 | } |
| 679 | |
| 680 | /* And destroy the original handler. */ |
| 681 | destroy_event_handler(proc); |
| 682 | } |
| 683 | |
| 684 | handler->super.on_event = ltrace_exiting_on_event; |
| 685 | handler->super.destroy = ltrace_exiting_destroy; |
| 686 | install_event_handler(proc->leader, &handler->super); |
| 687 | |
| 688 | if (each_task(proc->leader, &send_sigstop, |
| 689 | &handler->pids) != NULL) |
| 690 | goto fatal; |
| 691 | |
| 692 | return 0; |
| 693 | } |
| 694 | |
| 695 | /* If ltrace gets SIGINT, the processes directly or indirectly run by |
| 696 | * ltrace get it too. We just have to wait long enough for the signal |
| 697 | * to be delivered and the process terminated, which we notice and |
| 698 | * exit ltrace, too. So there's not much we need to do there. We |
| 699 | * want to keep tracing those processes as usual, in case they just |
| 700 | * SIG_IGN the SIGINT to do their shutdown etc. |
| 701 | * |
| 702 | * For processes ran on the background, we want to install an exit |
| 703 | * handler that stops all the threads, removes all breakpoints, and |
| 704 | * detaches. |
| 705 | */ |
| 706 | void |
| 707 | ltrace_exiting(void) |
| 708 | { |
| 709 | struct opt_p_t * it; |
| 710 | for (it = opt_p; it != NULL; it = it->next) { |
| 711 | Process * proc = pid2proc(it->pid); |
| 712 | if (proc == NULL || proc->leader == NULL) |
| 713 | continue; |
| 714 | if (ltrace_exiting_install_handler(proc->leader) < 0) |
| 715 | fprintf(stderr, |
| 716 | "Couldn't install exiting handler for %d.\n", |
| 717 | proc->pid); |
| 718 | } |
| 719 | } |
| 720 | |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 721 | size_t |
| 722 | umovebytes(Process *proc, void *addr, void *laddr, size_t len) { |
| 723 | |
| 724 | union { |
| 725 | long a; |
| 726 | char c[sizeof(long)]; |
| 727 | } a; |
| Zachary T Welch | ba6aca2 | 2010-12-08 18:55:09 -0800 | [diff] [blame] | 728 | int started = 0; |
| 729 | size_t offset = 0, bytes_read = 0; |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 730 | |
| 731 | while (offset < len) { |
| 732 | a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); |
| 733 | if (a.a == -1 && errno) { |
| 734 | if (started && errno == EIO) |
| 735 | return bytes_read; |
| 736 | else |
| 737 | return -1; |
| 738 | } |
| 739 | started = 1; |
| 740 | |
| 741 | if (len - offset >= sizeof(long)) { |
| 742 | memcpy(laddr + offset, &a.c[0], sizeof(long)); |
| 743 | bytes_read += sizeof(long); |
| 744 | } |
| 745 | else { |
| 746 | memcpy(laddr + offset, &a.c[0], len - offset); |
| 747 | bytes_read += (len - offset); |
| 748 | } |
| 749 | offset += sizeof(long); |
| 750 | } |
| 751 | |
| 752 | return bytes_read; |
| 753 | } |
| 754 | |
| Steve Fink | 7bafff0 | 2006-08-07 04:50:42 +0200 | [diff] [blame] | 755 | /* Read a series of bytes starting at the process's memory address |
| 756 | 'addr' and continuing until a NUL ('\0') is seen or 'len' bytes |
| 757 | have been read. |
| 758 | */ |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 759 | int |
| Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 760 | umovestr(Process *proc, void *addr, int len, void *laddr) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 761 | union { |
| 762 | long a; |
| 763 | char c[sizeof(long)]; |
| 764 | } a; |
| Zachary T Welch | ba6aca2 | 2010-12-08 18:55:09 -0800 | [diff] [blame] | 765 | unsigned i; |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 766 | int offset = 0; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 767 | |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 768 | while (offset < len) { |
| 769 | a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); |
| 770 | for (i = 0; i < sizeof(long); i++) { |
| Paul Gilliam | 3f1219f | 2006-04-24 18:25:38 +0200 | [diff] [blame] | 771 | if (a.c[i] && offset + (signed)i < len) { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 772 | *(char *)(laddr + offset + i) = a.c[i]; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 773 | } else { |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 774 | *(char *)(laddr + offset + i) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 775 | return 0; |
| 776 | } |
| 777 | } |
| 778 | offset += sizeof(long); |
| 779 | } |
| Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 780 | *(char *)(laddr + offset) = '\0'; |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 781 | return 0; |
| 782 | } |