| Petr Machata | 6426260 | 2012-01-07 03:41:36 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
| 3 | * Copyright (C) 2007,2011,2012 Petr Machata, Red Hat Inc. |
| 4 | * Copyright (C) 2010 Joe Damato |
| 5 | * Copyright (C) 1998,2002,2003,2004,2008,2009 Juan Cespedes |
| 6 | * Copyright (C) 2006 Ian Wienand |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation; either version 2 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 | * General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 21 | * 02110-1301 USA |
| 22 | */ |
| 23 | |
| Petr Machata | cec06ec | 2012-04-10 13:31:55 +0200 | [diff] [blame] | 24 | #include "config.h" |
| 25 | |
| 26 | #include <asm/unistd.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <sys/wait.h> |
| 29 | #include <assert.h> |
| 30 | #include <errno.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 31 | #include <stdio.h> |
| Juan Cespedes | 504a385 | 2003-02-04 23:24:38 +0100 | [diff] [blame] | 32 | #include <stdlib.h> |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 33 | #include <string.h> |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 34 | #include <unistd.h> |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 35 | |
| Petr Machata | cec06ec | 2012-04-10 13:31:55 +0200 | [diff] [blame] | 36 | #ifdef HAVE_LIBSELINUX |
| 37 | # include <selinux/selinux.h> |
| 38 | #endif |
| 39 | |
| Petr Machata | 9282254 | 2012-03-28 00:31:14 +0200 | [diff] [blame] | 40 | #include "linux-gnu/trace.h" |
| Petr Machata | 6426260 | 2012-01-07 03:41:36 +0100 | [diff] [blame] | 41 | #include "backend.h" |
| Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 42 | #include "breakpoint.h" |
| 43 | #include "debug.h" |
| Petr Machata | cd97258 | 2012-01-07 03:02:07 +0100 | [diff] [blame] | 44 | #include "events.h" |
| Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 45 | #include "options.h" |
| 46 | #include "proc.h" |
| 47 | #include "ptrace.h" |
| 48 | #include "type.h" |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 49 | |
| 50 | /* If the system headers did not provide the constants, hard-code the normal |
| 51 | values. */ |
| 52 | #ifndef PTRACE_EVENT_FORK |
| 53 | |
| 54 | #define PTRACE_OLDSETOPTIONS 21 |
| 55 | #define PTRACE_SETOPTIONS 0x4200 |
| 56 | #define PTRACE_GETEVENTMSG 0x4201 |
| 57 | |
| 58 | /* options set using PTRACE_SETOPTIONS */ |
| 59 | #define PTRACE_O_TRACESYSGOOD 0x00000001 |
| 60 | #define PTRACE_O_TRACEFORK 0x00000002 |
| 61 | #define PTRACE_O_TRACEVFORK 0x00000004 |
| 62 | #define PTRACE_O_TRACECLONE 0x00000008 |
| 63 | #define PTRACE_O_TRACEEXEC 0x00000010 |
| 64 | #define PTRACE_O_TRACEVFORKDONE 0x00000020 |
| 65 | #define PTRACE_O_TRACEEXIT 0x00000040 |
| 66 | |
| 67 | /* Wait extended result codes for the above trace options. */ |
| 68 | #define PTRACE_EVENT_FORK 1 |
| 69 | #define PTRACE_EVENT_VFORK 2 |
| 70 | #define PTRACE_EVENT_CLONE 3 |
| 71 | #define PTRACE_EVENT_EXEC 4 |
| 72 | #define PTRACE_EVENT_VFORK_DONE 5 |
| 73 | #define PTRACE_EVENT_EXIT 6 |
| 74 | |
| 75 | #endif /* PTRACE_EVENT_FORK */ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 76 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 77 | void |
| Petr Machata | cec06ec | 2012-04-10 13:31:55 +0200 | [diff] [blame] | 78 | trace_fail_warning(pid_t pid) |
| 79 | { |
| 80 | /* This was adapted from GDB. */ |
| 81 | #ifdef HAVE_LIBSELINUX |
| 82 | static int checked = 0; |
| 83 | if (checked) |
| 84 | return; |
| 85 | checked = 1; |
| 86 | |
| 87 | /* -1 is returned for errors, 0 if it has no effect, 1 if |
| 88 | * PTRACE_ATTACH is forbidden. */ |
| 89 | if (security_get_boolean_active("deny_ptrace") == 1) |
| 90 | fprintf(stderr, |
| 91 | "The SELinux boolean 'deny_ptrace' is enabled, which may prevent ltrace from\n" |
| 92 | "tracing other processes. You can disable this process attach protection by\n" |
| 93 | "issuing 'setsebool deny_ptrace=0' in the superuser context.\n"); |
| 94 | #endif /* HAVE_LIBSELINUX */ |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | trace_me(void) |
| 99 | { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 100 | debug(DEBUG_PROCESS, "trace_me: pid=%d", getpid()); |
| Petr Machata | c897cb7 | 2012-05-05 01:26:58 +0200 | [diff] [blame] | 101 | if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) { |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 102 | perror("PTRACE_TRACEME"); |
| Petr Machata | cec06ec | 2012-04-10 13:31:55 +0200 | [diff] [blame] | 103 | trace_fail_warning(getpid()); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 104 | exit(1); |
| 105 | } |
| 106 | } |
| 107 | |
| Petr Machata | b4f9e0c | 2012-02-07 01:57:59 +0100 | [diff] [blame] | 108 | /* There's a (hopefully) brief period of time after the child process |
| Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 109 | * forks when we can't trace it yet. Here we wait for kernel to |
| Petr Machata | b4f9e0c | 2012-02-07 01:57:59 +0100 | [diff] [blame] | 110 | * prepare the process. */ |
| Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 111 | int |
| Petr Machata | b4f9e0c | 2012-02-07 01:57:59 +0100 | [diff] [blame] | 112 | wait_for_proc(pid_t pid) |
| 113 | { |
| Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 114 | /* man ptrace: PTRACE_ATTACH attaches to the process specified |
| 115 | in pid. The child is sent a SIGSTOP, but will not |
| 116 | necessarily have stopped by the completion of this call; |
| 117 | use wait() to wait for the child to stop. */ |
| 118 | if (waitpid(pid, NULL, __WALL) != pid) { |
| 119 | perror ("trace_pid: waitpid"); |
| 120 | return -1; |
| Petr Machata | b4f9e0c | 2012-02-07 01:57:59 +0100 | [diff] [blame] | 121 | } |
| 122 | |
| Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 123 | return 0; |
| Petr Machata | b4f9e0c | 2012-02-07 01:57:59 +0100 | [diff] [blame] | 124 | } |
| 125 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 126 | int |
| Petr Machata | cec06ec | 2012-04-10 13:31:55 +0200 | [diff] [blame] | 127 | trace_pid(pid_t pid) |
| 128 | { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 129 | debug(DEBUG_PROCESS, "trace_pid: pid=%d", pid); |
| Petr Machata | cec06ec | 2012-04-10 13:31:55 +0200 | [diff] [blame] | 130 | /* This shouldn't emit error messages, as there are legitimate |
| 131 | * reasons that the PID can't be attached: like it may have |
| 132 | * already ended. */ |
| Petr Machata | c897cb7 | 2012-05-05 01:26:58 +0200 | [diff] [blame] | 133 | if (ptrace(PTRACE_ATTACH, pid, 0, 0) < 0) |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 134 | return -1; |
| Petr Machata | 89a5360 | 2007-01-25 18:05:44 +0100 | [diff] [blame] | 135 | |
| Petr Machata | c805c62 | 2012-03-02 00:10:37 +0100 | [diff] [blame] | 136 | return wait_for_proc(pid); |
| Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 137 | } |
| 138 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 139 | void |
| Petr Machata | 3ed2a42 | 2012-04-06 17:18:55 +0200 | [diff] [blame] | 140 | trace_set_options(struct Process *proc) |
| 141 | { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 142 | if (proc->tracesysgood & 0x80) |
| 143 | return; |
| Petr Machata | 55ed83b | 2007-05-17 16:24:15 +0200 | [diff] [blame] | 144 | |
| Petr Machata | 3ed2a42 | 2012-04-06 17:18:55 +0200 | [diff] [blame] | 145 | pid_t pid = proc->pid; |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 146 | debug(DEBUG_PROCESS, "trace_set_options: pid=%d", pid); |
| Juan Cespedes | cd8976d | 2009-05-14 13:47:58 +0200 | [diff] [blame] | 147 | |
| Juan Cespedes | 1e58313 | 2009-04-07 18:17:11 +0200 | [diff] [blame] | 148 | long options = PTRACE_O_TRACESYSGOOD | PTRACE_O_TRACEFORK | |
| 149 | PTRACE_O_TRACEVFORK | PTRACE_O_TRACECLONE | |
| 150 | PTRACE_O_TRACEEXEC; |
| Petr Machata | c897cb7 | 2012-05-05 01:26:58 +0200 | [diff] [blame] | 151 | if (ptrace(PTRACE_SETOPTIONS, pid, 0, (void *)options) < 0 && |
| 152 | ptrace(PTRACE_OLDSETOPTIONS, pid, 0, (void *)options) < 0) { |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 153 | perror("PTRACE_SETOPTIONS"); |
| 154 | return; |
| 155 | } |
| 156 | proc->tracesysgood |= 0x80; |
| 157 | } |
| 158 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 159 | void |
| 160 | untrace_pid(pid_t pid) { |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 161 | debug(DEBUG_PROCESS, "untrace_pid: pid=%d", pid); |
| Petr Machata | c897cb7 | 2012-05-05 01:26:58 +0200 | [diff] [blame] | 162 | ptrace(PTRACE_DETACH, pid, 0, 0); |
| Juan Cespedes | 1fe93d5 | 1998-03-13 00:29:21 +0100 | [diff] [blame] | 163 | } |
| 164 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 165 | void |
| Petr Machata | c897cb7 | 2012-05-05 01:26:58 +0200 | [diff] [blame] | 166 | continue_after_signal(pid_t pid, int signum) |
| 167 | { |
| 168 | debug(DEBUG_PROCESS, "continue_after_signal: pid=%d, signum=%d", |
| 169 | pid, signum); |
| 170 | ptrace(PTRACE_SYSCALL, pid, 0, (void *)(uintptr_t)signum); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static enum ecb_status |
| 174 | event_for_pid(Event * event, void * data) |
| 175 | { |
| 176 | if (event->proc != NULL && event->proc->pid == (pid_t)(uintptr_t)data) |
| 177 | return ecb_yield; |
| 178 | return ecb_cont; |
| 179 | } |
| 180 | |
| 181 | static int |
| 182 | have_events_for(pid_t pid) |
| 183 | { |
| 184 | return each_qd_event(event_for_pid, (void *)(uintptr_t)pid) != NULL; |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | continue_process(pid_t pid) |
| 189 | { |
| 190 | debug(DEBUG_PROCESS, "continue_process: pid=%d", pid); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 191 | |
| 192 | /* Only really continue the process if there are no events in |
| Petr Machata | 36d1982 | 2011-10-21 16:03:45 +0200 | [diff] [blame] | 193 | the queue for this process. Otherwise just wait for the |
| 194 | other events to arrive. */ |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 195 | if (!have_events_for(pid)) |
| 196 | /* We always trace syscalls to control fork(), |
| 197 | * clone(), execve()... */ |
| 198 | ptrace(PTRACE_SYSCALL, pid, 0, 0); |
| 199 | else |
| 200 | debug(DEBUG_PROCESS, |
| 201 | "putting off the continue, events in que."); |
| 202 | } |
| 203 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 204 | static struct pid_task * |
| 205 | get_task_info(struct pid_set * pids, pid_t pid) |
| 206 | { |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame] | 207 | assert(pid != 0); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 208 | size_t i; |
| 209 | for (i = 0; i < pids->count; ++i) |
| 210 | if (pids->tasks[i].pid == pid) |
| 211 | return &pids->tasks[i]; |
| 212 | |
| 213 | return NULL; |
| 214 | } |
| 215 | |
| 216 | static struct pid_task * |
| 217 | add_task_info(struct pid_set * pids, pid_t pid) |
| 218 | { |
| 219 | if (pids->count == pids->alloc) { |
| 220 | size_t ns = (2 * pids->alloc) ?: 4; |
| 221 | struct pid_task * n = realloc(pids->tasks, |
| 222 | sizeof(*pids->tasks) * ns); |
| 223 | if (n == NULL) |
| 224 | return NULL; |
| 225 | pids->tasks = n; |
| 226 | pids->alloc = ns; |
| 227 | } |
| 228 | struct pid_task * task_info = &pids->tasks[pids->count++]; |
| 229 | memset(task_info, 0, sizeof(*task_info)); |
| 230 | task_info->pid = pid; |
| 231 | return task_info; |
| 232 | } |
| 233 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 234 | static enum callback_status |
| 235 | task_stopped(struct Process *task, void *data) |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 236 | { |
| 237 | enum process_status st = process_status(task->pid); |
| 238 | if (data != NULL) |
| 239 | *(enum process_status *)data = st; |
| 240 | |
| 241 | /* If the task is already stopped, don't worry about it. |
| 242 | * Likewise if it managed to become a zombie or terminate in |
| 243 | * the meantime. This can happen when the whole thread group |
| 244 | * is terminating. */ |
| 245 | switch (st) { |
| 246 | case ps_invalid: |
| 247 | case ps_tracing_stop: |
| 248 | case ps_zombie: |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 249 | return CBS_CONT; |
| Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 250 | case ps_sleeping: |
| Petr Machata | 36d1982 | 2011-10-21 16:03:45 +0200 | [diff] [blame] | 251 | case ps_stop: |
| 252 | case ps_other: |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 253 | return CBS_STOP; |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 254 | } |
| Petr Machata | 36d1982 | 2011-10-21 16:03:45 +0200 | [diff] [blame] | 255 | |
| 256 | abort (); |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /* Task is blocked if it's stopped, or if it's a vfork parent. */ |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 260 | static enum callback_status |
| 261 | task_blocked(struct Process *task, void *data) |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 262 | { |
| 263 | struct pid_set * pids = data; |
| 264 | struct pid_task * task_info = get_task_info(pids, task->pid); |
| 265 | if (task_info != NULL |
| 266 | && task_info->vforked) |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 267 | return CBS_CONT; |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 268 | |
| 269 | return task_stopped(task, NULL); |
| 270 | } |
| 271 | |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 272 | static Event *process_vfork_on_event(struct event_handler *super, Event *event); |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 273 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 274 | static enum callback_status |
| 275 | task_vforked(struct Process *task, void *data) |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 276 | { |
| 277 | if (task->event_handler != NULL |
| 278 | && task->event_handler->on_event == &process_vfork_on_event) |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 279 | return CBS_STOP; |
| 280 | return CBS_CONT; |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static int |
| Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 284 | is_vfork_parent(struct Process *task) |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 285 | { |
| Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 286 | return each_task(task->leader, NULL, &task_vforked, NULL) != NULL; |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 287 | } |
| 288 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 289 | static enum callback_status |
| 290 | send_sigstop(struct Process *task, void *data) |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 291 | { |
| 292 | Process * leader = task->leader; |
| 293 | struct pid_set * pids = data; |
| 294 | |
| 295 | /* Look for pre-existing task record, or add new. */ |
| 296 | struct pid_task * task_info = get_task_info(pids, task->pid); |
| 297 | if (task_info == NULL) |
| 298 | task_info = add_task_info(pids, task->pid); |
| 299 | if (task_info == NULL) { |
| 300 | perror("send_sigstop: add_task_info"); |
| 301 | destroy_event_handler(leader); |
| 302 | /* Signal failure upwards. */ |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 303 | return CBS_STOP; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | /* This task still has not been attached to. It should be |
| 307 | stopped by the kernel. */ |
| 308 | if (task->state == STATE_BEING_CREATED) |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 309 | return CBS_CONT; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 310 | |
| 311 | /* Don't bother sending SIGSTOP if we are already stopped, or |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 312 | * if we sent the SIGSTOP already, which happens when we are |
| 313 | * handling "onexit" and inherited the handler from breakpoint |
| 314 | * re-enablement. */ |
| 315 | enum process_status st; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 316 | if (task_stopped(task, &st) == CBS_CONT) |
| 317 | return CBS_CONT; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 318 | if (task_info->sigstopped) { |
| 319 | if (!task_info->delivered) |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 320 | return CBS_CONT; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 321 | task_info->delivered = 0; |
| 322 | } |
| 323 | |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 324 | /* Also don't attempt to stop the process if it's a parent of |
| 325 | * vforked process. We set up event handler specially to hint |
| 326 | * us. In that case parent is in D state, which we use to |
| 327 | * weed out unnecessary looping. */ |
| 328 | if (st == ps_sleeping |
| 329 | && is_vfork_parent (task)) { |
| 330 | task_info->vforked = 1; |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 331 | return CBS_CONT; |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 332 | } |
| 333 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 334 | if (task_kill(task->pid, SIGSTOP) >= 0) { |
| 335 | debug(DEBUG_PROCESS, "send SIGSTOP to %d", task->pid); |
| 336 | task_info->sigstopped = 1; |
| 337 | } else |
| 338 | fprintf(stderr, |
| 339 | "Warning: couldn't send SIGSTOP to %d\n", task->pid); |
| 340 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 341 | return CBS_CONT; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 342 | } |
| 343 | |
| Petr Machata | 73894bd | 2011-08-20 23:47:34 +0200 | [diff] [blame] | 344 | /* On certain kernels, detaching right after a singlestep causes the |
| 345 | tracee to be killed with a SIGTRAP (that even though the singlestep |
| 346 | was properly caught by waitpid. The ugly workaround is to put a |
| 347 | breakpoint where IP points and let the process continue. After |
| 348 | this the breakpoint can be retracted and the process detached. */ |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 349 | static void |
| Petr Machata | 73894bd | 2011-08-20 23:47:34 +0200 | [diff] [blame] | 350 | ugly_workaround(Process * proc) |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 351 | { |
| 352 | void * ip = get_instruction_pointer(proc); |
| Petr Machata | bc37326 | 2012-02-07 23:31:15 +0100 | [diff] [blame] | 353 | struct breakpoint *sbp = dict_find_entry(proc->leader->breakpoints, ip); |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 354 | if (sbp != NULL) |
| 355 | enable_breakpoint(proc, sbp); |
| 356 | else |
| Petr Machata | 9df1501 | 2012-02-20 12:49:46 +0100 | [diff] [blame] | 357 | insert_breakpoint(proc, ip, NULL); |
| Petr Machata | 73894bd | 2011-08-20 23:47:34 +0200 | [diff] [blame] | 358 | ptrace(PTRACE_CONT, proc->pid, 0, 0); |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | static void |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 362 | process_stopping_done(struct process_stopping_handler * self, Process * leader) |
| 363 | { |
| 364 | debug(DEBUG_PROCESS, "process stopping done %d", |
| 365 | self->task_enabling_breakpoint->pid); |
| 366 | size_t i; |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 367 | if (!self->exiting) { |
| 368 | for (i = 0; i < self->pids.count; ++i) |
| 369 | if (self->pids.tasks[i].pid != 0 |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 370 | && (self->pids.tasks[i].delivered |
| 371 | || self->pids.tasks[i].sysret)) |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 372 | continue_process(self->pids.tasks[i].pid); |
| 373 | continue_process(self->task_enabling_breakpoint->pid); |
| 374 | destroy_event_handler(leader); |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | if (self->exiting) { |
| 378 | ugly_workaround: |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 379 | self->state = psh_ugly_workaround; |
| Petr Machata | 73894bd | 2011-08-20 23:47:34 +0200 | [diff] [blame] | 380 | ugly_workaround(self->task_enabling_breakpoint); |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 381 | } else { |
| 382 | switch ((self->ugly_workaround_p)(self)) { |
| 383 | case CBS_FAIL: |
| 384 | /* xxx handle me */ |
| 385 | case CBS_STOP: |
| 386 | break; |
| 387 | case CBS_CONT: |
| 388 | goto ugly_workaround; |
| 389 | } |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | /* Before we detach, we need to make sure that task's IP is on the |
| 394 | * edge of an instruction. So for tasks that have a breakpoint event |
| 395 | * in the queue, we adjust the instruction pointer, just like |
| 396 | * continue_after_breakpoint does. */ |
| 397 | static enum ecb_status |
| 398 | undo_breakpoint(Event * event, void * data) |
| 399 | { |
| 400 | if (event != NULL |
| 401 | && event->proc->leader == data |
| 402 | && event->type == EVENT_BREAKPOINT) |
| 403 | set_instruction_pointer(event->proc, event->e_un.brk_addr); |
| 404 | return ecb_cont; |
| 405 | } |
| 406 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 407 | static enum callback_status |
| 408 | untrace_task(struct Process *task, void *data) |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 409 | { |
| 410 | if (task != data) |
| 411 | untrace_pid(task->pid); |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 412 | return CBS_CONT; |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 413 | } |
| 414 | |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 415 | static enum callback_status |
| 416 | remove_task(struct Process *task, void *data) |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 417 | { |
| 418 | /* Don't untrace leader just yet. */ |
| 419 | if (task != data) |
| 420 | remove_process(task); |
| Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 421 | return CBS_CONT; |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 422 | } |
| 423 | |
| Petr Machata | 86d3828 | 2012-04-24 18:09:01 +0200 | [diff] [blame] | 424 | static enum callback_status |
| 425 | retract_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data) |
| 426 | { |
| 427 | breakpoint_on_retract(bp, proc); |
| 428 | return CBS_CONT; |
| 429 | } |
| 430 | |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 431 | static void |
| 432 | detach_process(Process * leader) |
| 433 | { |
| 434 | each_qd_event(&undo_breakpoint, leader); |
| 435 | disable_all_breakpoints(leader); |
| Petr Machata | 86d3828 | 2012-04-24 18:09:01 +0200 | [diff] [blame] | 436 | proc_each_breakpoint(leader, NULL, retract_breakpoint_cb, NULL); |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 437 | |
| 438 | /* Now untrace the process, if it was attached to by -p. */ |
| 439 | struct opt_p_t * it; |
| 440 | for (it = opt_p; it != NULL; it = it->next) { |
| 441 | Process * proc = pid2proc(it->pid); |
| 442 | if (proc == NULL) |
| 443 | continue; |
| 444 | if (proc->leader == leader) { |
| Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 445 | each_task(leader, NULL, &untrace_task, NULL); |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | } |
| Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 449 | each_task(leader, NULL, &remove_task, leader); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 450 | destroy_event_handler(leader); |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 451 | remove_task(leader, NULL); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | static void |
| 455 | handle_stopping_event(struct pid_task * task_info, Event ** eventp) |
| 456 | { |
| 457 | /* Mark all events, so that we know whom to SIGCONT later. */ |
| Petr Machata | 3c9b629 | 2011-08-20 15:05:41 +0200 | [diff] [blame] | 458 | if (task_info != NULL) |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 459 | task_info->got_event = 1; |
| 460 | |
| 461 | Event * event = *eventp; |
| 462 | |
| 463 | /* In every state, sink SIGSTOP events for tasks that it was |
| 464 | * sent to. */ |
| 465 | if (task_info != NULL |
| 466 | && event->type == EVENT_SIGNAL |
| 467 | && event->e_un.signum == SIGSTOP) { |
| 468 | debug(DEBUG_PROCESS, "SIGSTOP delivered to %d", task_info->pid); |
| 469 | if (task_info->sigstopped |
| 470 | && !task_info->delivered) { |
| 471 | task_info->delivered = 1; |
| 472 | *eventp = NULL; // sink the event |
| 473 | } else |
| 474 | fprintf(stderr, "suspicious: %d got SIGSTOP, but " |
| 475 | "sigstopped=%d and delivered=%d\n", |
| 476 | task_info->pid, task_info->sigstopped, |
| 477 | task_info->delivered); |
| Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 478 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 479 | } |
| 480 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 481 | /* Some SIGSTOPs may have not been delivered to their respective tasks |
| 482 | * yet. They are still in the queue. If we have seen an event for |
| 483 | * that process, continue it, so that the SIGSTOP can be delivered and |
| Petr Machata | 36d1982 | 2011-10-21 16:03:45 +0200 | [diff] [blame] | 484 | * caught by ltrace. We don't mind that the process is after |
| 485 | * breakpoint (and therefore potentially doesn't have aligned IP), |
| 486 | * because the signal will be delivered without the process actually |
| 487 | * starting. */ |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 488 | static void |
| 489 | continue_for_sigstop_delivery(struct pid_set * pids) |
| 490 | { |
| 491 | size_t i; |
| 492 | for (i = 0; i < pids->count; ++i) { |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame] | 493 | if (pids->tasks[i].pid != 0 |
| 494 | && pids->tasks[i].sigstopped |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 495 | && !pids->tasks[i].delivered |
| 496 | && pids->tasks[i].got_event) { |
| 497 | debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery", |
| 498 | pids->tasks[i].pid); |
| 499 | ptrace(PTRACE_SYSCALL, pids->tasks[i].pid, 0, 0); |
| 500 | } |
| 501 | } |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 502 | } |
| 503 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 504 | static int |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame] | 505 | event_exit_p(Event * event) |
| 506 | { |
| 507 | return event != NULL && (event->type == EVENT_EXIT |
| 508 | || event->type == EVENT_EXIT_SIGNAL); |
| 509 | } |
| 510 | |
| 511 | static int |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 512 | event_exit_or_none_p(Event * event) |
| Petr Machata | f789c9c | 2011-07-09 10:54:27 +0200 | [diff] [blame] | 513 | { |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame] | 514 | return event == NULL || event_exit_p(event) |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 515 | || event->type == EVENT_NONE; |
| 516 | } |
| 517 | |
| 518 | static int |
| 519 | await_sigstop_delivery(struct pid_set * pids, struct pid_task * task_info, |
| 520 | Event * event) |
| 521 | { |
| 522 | /* If we still didn't get our SIGSTOP, continue the process |
| 523 | * and carry on. */ |
| 524 | if (event != NULL && !event_exit_or_none_p(event) |
| 525 | && task_info != NULL && task_info->sigstopped) { |
| 526 | debug(DEBUG_PROCESS, "continue %d for SIGSTOP delivery", |
| 527 | task_info->pid); |
| 528 | /* We should get the signal the first thing |
| 529 | * after this, so it should be OK to continue |
| 530 | * even if we are over a breakpoint. */ |
| 531 | ptrace(PTRACE_SYSCALL, task_info->pid, 0, 0); |
| 532 | |
| 533 | } else { |
| 534 | /* If all SIGSTOPs were delivered, uninstall the |
| 535 | * handler and continue everyone. */ |
| 536 | /* XXX I suspect that we should check tasks that are |
| 537 | * still around. Is things are now, there should be a |
| 538 | * race between waiting for everyone to stop and one |
| 539 | * of the tasks exiting. */ |
| 540 | int all_clear = 1; |
| 541 | size_t i; |
| 542 | for (i = 0; i < pids->count; ++i) |
| Petr Machata | 750ca8c | 2011-10-06 14:29:34 +0200 | [diff] [blame] | 543 | if (pids->tasks[i].pid != 0 |
| 544 | && pids->tasks[i].sigstopped |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 545 | && !pids->tasks[i].delivered) { |
| 546 | all_clear = 0; |
| 547 | break; |
| 548 | } |
| 549 | return all_clear; |
| 550 | } |
| 551 | |
| 552 | return 0; |
| 553 | } |
| 554 | |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 555 | static int |
| 556 | all_stops_accountable(struct pid_set * pids) |
| 557 | { |
| 558 | size_t i; |
| 559 | for (i = 0; i < pids->count; ++i) |
| 560 | if (pids->tasks[i].pid != 0 |
| 561 | && !pids->tasks[i].got_event |
| 562 | && !have_events_for(pids->tasks[i].pid)) |
| 563 | return 0; |
| 564 | return 1; |
| 565 | } |
| 566 | |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 567 | /* The protocol is: 0 for success, negative for failure, positive if |
| 568 | * default singlestep is to be used. */ |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 569 | int arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp, |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 570 | int (*add_cb)(void *addr, void *data), |
| 571 | void *add_cb_data); |
| 572 | |
| 573 | #ifndef ARCH_HAVE_ATOMIC_SINGLESTEP |
| 574 | int |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 575 | arch_atomic_singlestep(struct Process *proc, struct breakpoint *sbp, |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 576 | int (*add_cb)(void *addr, void *data), |
| 577 | void *add_cb_data) |
| Petr Machata | 06986d5 | 2011-11-02 13:22:46 +0100 | [diff] [blame] | 578 | { |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 579 | return 1; |
| 580 | } |
| 581 | #endif |
| 582 | |
| Petr Machata | 42748ac | 2012-04-19 04:24:25 +0200 | [diff] [blame] | 583 | static Event *process_stopping_on_event(struct event_handler *super, |
| 584 | Event *event); |
| 585 | |
| 586 | static void |
| 587 | remove_atomic_breakpoints(struct Process *proc) |
| 588 | { |
| Petr Machata | 24a4704 | 2012-04-19 18:15:08 +0200 | [diff] [blame] | 589 | struct process_stopping_handler *self |
| 590 | = (void *)proc->leader->event_handler; |
| 591 | assert(self != NULL); |
| Petr Machata | 42748ac | 2012-04-19 04:24:25 +0200 | [diff] [blame] | 592 | assert(self->super.on_event == process_stopping_on_event); |
| 593 | |
| 594 | int ct = sizeof(self->atomic_skip_bp_addrs) |
| 595 | / sizeof(*self->atomic_skip_bp_addrs); |
| 596 | int i; |
| 597 | for (i = 0; i < ct; ++i) |
| 598 | if (self->atomic_skip_bp_addrs[i] != 0) { |
| Petr Machata | 622581f | 2012-04-23 19:40:40 +0200 | [diff] [blame] | 599 | delete_breakpoint(proc, self->atomic_skip_bp_addrs[i]); |
| Petr Machata | 42748ac | 2012-04-19 04:24:25 +0200 | [diff] [blame] | 600 | self->atomic_skip_bp_addrs[i] = 0; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | static void |
| 605 | atomic_singlestep_bp_on_hit(struct breakpoint *bp, struct Process *proc) |
| 606 | { |
| 607 | remove_atomic_breakpoints(proc); |
| 608 | } |
| 609 | |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 610 | static int |
| 611 | atomic_singlestep_add_bp(void *addr, void *data) |
| 612 | { |
| 613 | struct process_stopping_handler *self = data; |
| 614 | struct Process *proc = self->task_enabling_breakpoint; |
| 615 | |
| Petr Machata | 42748ac | 2012-04-19 04:24:25 +0200 | [diff] [blame] | 616 | int ct = sizeof(self->atomic_skip_bp_addrs) |
| 617 | / sizeof(*self->atomic_skip_bp_addrs); |
| 618 | int i; |
| 619 | for (i = 0; i < ct; ++i) |
| 620 | if (self->atomic_skip_bp_addrs[i] == 0) { |
| 621 | self->atomic_skip_bp_addrs[i] = addr; |
| 622 | static struct bp_callbacks cbs = { |
| 623 | .on_hit = atomic_singlestep_bp_on_hit, |
| 624 | }; |
| 625 | struct breakpoint *bp |
| Petr Machata | 622581f | 2012-04-23 19:40:40 +0200 | [diff] [blame] | 626 | = insert_breakpoint(proc, addr, NULL); |
| Petr Machata | 42748ac | 2012-04-19 04:24:25 +0200 | [diff] [blame] | 627 | breakpoint_set_callbacks(bp, &cbs); |
| 628 | return 0; |
| 629 | } |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 630 | |
| Petr Machata | 42748ac | 2012-04-19 04:24:25 +0200 | [diff] [blame] | 631 | assert(!"Too many atomic singlestep breakpoints!"); |
| 632 | abort(); |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | static int |
| 636 | singlestep(struct process_stopping_handler *self) |
| 637 | { |
| 638 | struct Process *proc = self->task_enabling_breakpoint; |
| 639 | |
| 640 | int status = arch_atomic_singlestep(self->task_enabling_breakpoint, |
| 641 | self->breakpoint_being_enabled, |
| 642 | &atomic_singlestep_add_bp, self); |
| 643 | |
| 644 | /* Propagate failure and success. */ |
| 645 | if (status <= 0) |
| 646 | return status; |
| 647 | |
| 648 | /* Otherwise do the default action: singlestep. */ |
| Petr Machata | 06986d5 | 2011-11-02 13:22:46 +0100 | [diff] [blame] | 649 | debug(1, "PTRACE_SINGLESTEP"); |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 650 | if (ptrace(PTRACE_SINGLESTEP, proc->pid, 0, 0)) { |
| Petr Machata | 06986d5 | 2011-11-02 13:22:46 +0100 | [diff] [blame] | 651 | perror("PTRACE_SINGLESTEP"); |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 652 | return -1; |
| 653 | } |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static void |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 658 | post_singlestep(struct process_stopping_handler *self, |
| 659 | struct Event **eventp) |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 660 | { |
| 661 | continue_for_sigstop_delivery(&self->pids); |
| 662 | |
| Petr Machata | f1fd7cd | 2012-04-19 03:05:58 +0200 | [diff] [blame] | 663 | if (*eventp != NULL && (*eventp)->type == EVENT_BREAKPOINT) |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 664 | *eventp = NULL; // handled |
| 665 | |
| Petr Machata | 42748ac | 2012-04-19 04:24:25 +0200 | [diff] [blame] | 666 | struct Process *proc = self->task_enabling_breakpoint; |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 667 | |
| Petr Machata | 42748ac | 2012-04-19 04:24:25 +0200 | [diff] [blame] | 668 | remove_atomic_breakpoints(proc); |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 669 | self->breakpoint_being_enabled = NULL; |
| 670 | } |
| 671 | |
| 672 | static void |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 673 | singlestep_error(struct process_stopping_handler *self) |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 674 | { |
| 675 | struct Process *teb = self->task_enabling_breakpoint; |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 676 | struct breakpoint *sbp = self->breakpoint_being_enabled; |
| Petr Machata | 1e2a4dd | 2012-04-15 04:35:48 +0200 | [diff] [blame] | 677 | fprintf(stderr, "%d couldn't continue when handling %s (%p) at %p\n", |
| Petr Machata | ba1664b | 2012-04-28 14:59:05 +0200 | [diff] [blame] | 678 | teb->pid, breakpoint_name(sbp), sbp->addr, |
| 679 | get_instruction_pointer(teb)); |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 680 | delete_breakpoint(teb->leader, sbp->addr); |
| Petr Machata | 06986d5 | 2011-11-02 13:22:46 +0100 | [diff] [blame] | 681 | } |
| 682 | |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 683 | static void |
| Petr Machata | 1e2a4dd | 2012-04-15 04:35:48 +0200 | [diff] [blame] | 684 | pt_continue(struct process_stopping_handler *self) |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 685 | { |
| 686 | struct Process *teb = self->task_enabling_breakpoint; |
| Petr Machata | 1e2a4dd | 2012-04-15 04:35:48 +0200 | [diff] [blame] | 687 | debug(1, "PTRACE_CONT"); |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 688 | ptrace(PTRACE_CONT, teb->pid, 0, 0); |
| 689 | } |
| 690 | |
| 691 | static void |
| 692 | pt_singlestep(struct process_stopping_handler *self) |
| 693 | { |
| 694 | if (singlestep(self) < 0) |
| 695 | singlestep_error(self); |
| Petr Machata | 1e2a4dd | 2012-04-15 04:35:48 +0200 | [diff] [blame] | 696 | } |
| 697 | |
| 698 | static void |
| 699 | disable_and(struct process_stopping_handler *self, |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 700 | void (*do_this)(struct process_stopping_handler *self)) |
| Petr Machata | 1e2a4dd | 2012-04-15 04:35:48 +0200 | [diff] [blame] | 701 | { |
| 702 | struct Process *teb = self->task_enabling_breakpoint; |
| 703 | debug(DEBUG_PROCESS, "all stopped, now singlestep/cont %d", teb->pid); |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 704 | if (self->breakpoint_being_enabled->enabled) |
| 705 | disable_breakpoint(teb, self->breakpoint_being_enabled); |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 706 | (do_this)(self); |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 707 | self->state = psh_singlestep; |
| 708 | } |
| 709 | |
| Petr Machata | 1e2a4dd | 2012-04-15 04:35:48 +0200 | [diff] [blame] | 710 | void |
| 711 | linux_ptrace_disable_and_singlestep(struct process_stopping_handler *self) |
| 712 | { |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 713 | disable_and(self, &pt_singlestep); |
| Petr Machata | 1e2a4dd | 2012-04-15 04:35:48 +0200 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | void |
| 717 | linux_ptrace_disable_and_continue(struct process_stopping_handler *self) |
| 718 | { |
| 719 | disable_and(self, &pt_continue); |
| 720 | } |
| 721 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 722 | /* This event handler is installed when we are in the process of |
| 723 | * stopping the whole thread group to do the pointer re-enablement for |
| 724 | * one of the threads. We pump all events to the queue for later |
| 725 | * processing while we wait for all the threads to stop. When this |
| 726 | * happens, we let the re-enablement thread to PTRACE_SINGLESTEP, |
| 727 | * re-enable, and continue everyone. */ |
| 728 | static Event * |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 729 | process_stopping_on_event(struct event_handler *super, Event *event) |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 730 | { |
| 731 | struct process_stopping_handler * self = (void *)super; |
| 732 | Process * task = event->proc; |
| 733 | Process * leader = task->leader; |
| Petr Machata | e21264e | 2011-10-06 14:30:33 +0200 | [diff] [blame] | 734 | Process * teb = self->task_enabling_breakpoint; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 735 | |
| 736 | debug(DEBUG_PROCESS, |
| Petr Machata | 9ace574 | 2012-04-14 02:29:57 +0200 | [diff] [blame] | 737 | "process_stopping_on_event: pid %d; event type %d; state %d", |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 738 | task->pid, event->type, self->state); |
| 739 | |
| 740 | struct pid_task * task_info = get_task_info(&self->pids, task->pid); |
| 741 | if (task_info == NULL) |
| 742 | fprintf(stderr, "new task??? %d\n", task->pid); |
| 743 | handle_stopping_event(task_info, &event); |
| 744 | |
| 745 | int state = self->state; |
| 746 | int event_to_queue = !event_exit_or_none_p(event); |
| 747 | |
| Petr Machata | 18c9707 | 2011-10-06 14:30:11 +0200 | [diff] [blame] | 748 | /* Deactivate the entry if the task exits. */ |
| 749 | if (event_exit_p(event) && task_info != NULL) |
| 750 | task_info->pid = 0; |
| 751 | |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 752 | /* Always handle sysrets. Whether sysret occurred and what |
| 753 | * sys it rets from may need to be determined based on process |
| 754 | * stack, so we need to keep that in sync with reality. Note |
| 755 | * that we don't continue the process after the sysret is |
| 756 | * handled. See continue_after_syscall. */ |
| 757 | if (event != NULL && event->type == EVENT_SYSRET) { |
| 758 | debug(1, "%d LT_EV_SYSRET", event->proc->pid); |
| 759 | event_to_queue = 0; |
| 760 | task_info->sysret = 1; |
| 761 | } |
| 762 | |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 763 | switch (state) { |
| 764 | case psh_stopping: |
| 765 | /* If everyone is stopped, singlestep. */ |
| Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 766 | if (each_task(leader, NULL, &task_blocked, |
| 767 | &self->pids) == NULL) { |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 768 | (self->on_all_stopped)(self); |
| 769 | state = self->state; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 770 | } |
| 771 | break; |
| 772 | |
| Petr Machata | 06986d5 | 2011-11-02 13:22:46 +0100 | [diff] [blame] | 773 | case psh_singlestep: |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 774 | /* In singlestep state, breakpoint signifies that we |
| 775 | * have now stepped, and can re-enable the breakpoint. */ |
| Petr Machata | e21264e | 2011-10-06 14:30:33 +0200 | [diff] [blame] | 776 | if (event != NULL && task == teb) { |
| Petr Machata | d5d93c4 | 2011-10-21 16:41:10 +0200 | [diff] [blame] | 777 | |
| Petr Machata | 42748ac | 2012-04-19 04:24:25 +0200 | [diff] [blame] | 778 | /* If this was caused by a real breakpoint, as |
| 779 | * opposed to a singlestep, assume that it's |
| 780 | * an artificial breakpoint installed for some |
| 781 | * reason for the re-enablement. In that case |
| 782 | * handle it. */ |
| 783 | if (event->type == EVENT_BREAKPOINT) { |
| 784 | target_address_t ip |
| 785 | = get_instruction_pointer(task); |
| 786 | struct breakpoint *other |
| 787 | = address2bpstruct(leader, ip); |
| 788 | if (other != NULL) |
| 789 | breakpoint_on_hit(other, task); |
| 790 | } |
| 791 | |
| Petr Machata | 36f40e7 | 2012-03-28 02:07:19 +0200 | [diff] [blame] | 792 | /* If we got SIGNAL instead of BREAKPOINT, |
| 793 | * then this is not singlestep at all. */ |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 794 | if (event->type == EVENT_SIGNAL) { |
| 795 | do_singlestep: |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 796 | if (singlestep(self) < 0) { |
| Petr Machata | 9e1e969 | 2012-04-19 02:29:38 +0200 | [diff] [blame] | 797 | singlestep_error(self); |
| 798 | post_singlestep(self, &event); |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 799 | goto psh_sinking; |
| 800 | } |
| Petr Machata | d5d93c4 | 2011-10-21 16:41:10 +0200 | [diff] [blame] | 801 | break; |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 802 | } else { |
| 803 | switch ((self->keep_stepping_p)(self)) { |
| 804 | case CBS_FAIL: |
| 805 | /* XXX handle me */ |
| 806 | case CBS_STOP: |
| 807 | break; |
| 808 | case CBS_CONT: |
| Petr Machata | 949a56a | 2012-04-03 00:32:03 +0200 | [diff] [blame] | 809 | /* Sink singlestep event. */ |
| 810 | if (event->type == EVENT_BREAKPOINT) |
| 811 | event = NULL; |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 812 | goto do_singlestep; |
| 813 | } |
| Petr Machata | d5d93c4 | 2011-10-21 16:41:10 +0200 | [diff] [blame] | 814 | } |
| 815 | |
| Petr Machata | 1e2a4dd | 2012-04-15 04:35:48 +0200 | [diff] [blame] | 816 | /* Re-enable the breakpoint that we are |
| 817 | * stepping over. */ |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 818 | struct breakpoint *sbp = self->breakpoint_being_enabled; |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 819 | if (sbp->enabled) |
| 820 | enable_breakpoint(teb, sbp); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 821 | |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 822 | post_singlestep(self, &event); |
| 823 | goto psh_sinking; |
| 824 | } |
| 825 | break; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 826 | |
| Petr Machata | a266acb | 2012-04-12 23:50:23 +0200 | [diff] [blame] | 827 | psh_sinking: |
| 828 | state = self->state = psh_sinking; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 829 | case psh_sinking: |
| 830 | if (await_sigstop_delivery(&self->pids, task_info, event)) |
| 831 | process_stopping_done(self, leader); |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 832 | break; |
| 833 | |
| 834 | case psh_ugly_workaround: |
| 835 | if (event == NULL) |
| 836 | break; |
| 837 | if (event->type == EVENT_BREAKPOINT) { |
| 838 | undo_breakpoint(event, leader); |
| 839 | if (task == teb) |
| 840 | self->task_enabling_breakpoint = NULL; |
| 841 | } |
| 842 | if (self->task_enabling_breakpoint == NULL |
| 843 | && all_stops_accountable(&self->pids)) { |
| 844 | undo_breakpoint(event, leader); |
| 845 | detach_process(leader); |
| 846 | event = NULL; // handled |
| 847 | } |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | if (event != NULL && event_to_queue) { |
| 851 | enque_event(event); |
| 852 | event = NULL; // sink the event |
| 853 | } |
| 854 | |
| 855 | return event; |
| 856 | } |
| 857 | |
| 858 | static void |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 859 | process_stopping_destroy(struct event_handler *super) |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 860 | { |
| 861 | struct process_stopping_handler * self = (void *)super; |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 862 | free(self->pids.tasks); |
| Juan Cespedes | 5e01f65 | 1998-03-08 22:31:44 +0100 | [diff] [blame] | 863 | } |
| Juan Cespedes | 8cc1b9d | 2002-03-01 19:54:23 +0100 | [diff] [blame] | 864 | |
| Petr Machata | 36f40e7 | 2012-03-28 02:07:19 +0200 | [diff] [blame] | 865 | static enum callback_status |
| 866 | no(struct process_stopping_handler *self) |
| 867 | { |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 868 | return CBS_STOP; |
| Petr Machata | 36f40e7 | 2012-03-28 02:07:19 +0200 | [diff] [blame] | 869 | } |
| 870 | |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 871 | int |
| 872 | process_install_stopping_handler(struct Process *proc, struct breakpoint *sbp, |
| Petr Machata | 36f40e7 | 2012-03-28 02:07:19 +0200 | [diff] [blame] | 873 | void (*as)(struct process_stopping_handler *), |
| 874 | enum callback_status (*ks) |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 875 | (struct process_stopping_handler *), |
| 876 | enum callback_status (*uw) |
| Petr Machata | 36f40e7 | 2012-03-28 02:07:19 +0200 | [diff] [blame] | 877 | (struct process_stopping_handler *)) |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 878 | { |
| Petr Machata | 9ace574 | 2012-04-14 02:29:57 +0200 | [diff] [blame] | 879 | debug(DEBUG_FUNCTION, |
| 880 | "process_install_stopping_handler: pid=%d", proc->pid); |
| 881 | |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 882 | struct process_stopping_handler *handler = calloc(sizeof(*handler), 1); |
| 883 | if (handler == NULL) |
| 884 | return -1; |
| 885 | |
| Petr Machata | 36f40e7 | 2012-03-28 02:07:19 +0200 | [diff] [blame] | 886 | if (as == NULL) |
| Petr Machata | 1e2a4dd | 2012-04-15 04:35:48 +0200 | [diff] [blame] | 887 | as = &linux_ptrace_disable_and_singlestep; |
| Petr Machata | 36f40e7 | 2012-03-28 02:07:19 +0200 | [diff] [blame] | 888 | if (ks == NULL) |
| 889 | ks = &no; |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 890 | if (uw == NULL) |
| 891 | uw = &no; |
| Petr Machata | 36f40e7 | 2012-03-28 02:07:19 +0200 | [diff] [blame] | 892 | |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 893 | handler->super.on_event = process_stopping_on_event; |
| 894 | handler->super.destroy = process_stopping_destroy; |
| 895 | handler->task_enabling_breakpoint = proc; |
| 896 | handler->breakpoint_being_enabled = sbp; |
| Petr Machata | 36f40e7 | 2012-03-28 02:07:19 +0200 | [diff] [blame] | 897 | handler->on_all_stopped = as; |
| 898 | handler->keep_stepping_p = ks; |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 899 | handler->ugly_workaround_p = uw; |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 900 | |
| 901 | install_event_handler(proc->leader, &handler->super); |
| 902 | |
| 903 | if (each_task(proc->leader, NULL, &send_sigstop, |
| 904 | &handler->pids) != NULL) { |
| 905 | destroy_event_handler(proc); |
| 906 | return -1; |
| 907 | } |
| 908 | |
| 909 | /* And deliver the first fake event, in case all the |
| 910 | * conditions are already fulfilled. */ |
| 911 | Event ev = { |
| 912 | .type = EVENT_NONE, |
| 913 | .proc = proc, |
| 914 | }; |
| 915 | process_stopping_on_event(&handler->super, &ev); |
| 916 | |
| 917 | return 0; |
| 918 | } |
| 919 | |
| Juan Cespedes | f135052 | 2008-12-16 18:19:58 +0100 | [diff] [blame] | 920 | void |
| Petr Machata | bc37326 | 2012-02-07 23:31:15 +0100 | [diff] [blame] | 921 | continue_after_breakpoint(Process *proc, struct breakpoint *sbp) |
| Petr Machata | 2662768 | 2011-07-08 18:15:32 +0200 | [diff] [blame] | 922 | { |
| Petr Machata | 9ace574 | 2012-04-14 02:29:57 +0200 | [diff] [blame] | 923 | debug(DEBUG_PROCESS, |
| 924 | "continue_after_breakpoint: pid=%d, addr=%p", |
| 925 | proc->pid, sbp->addr); |
| 926 | |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 927 | set_instruction_pointer(proc, sbp->addr); |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 928 | |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 929 | if (sbp->enabled == 0) { |
| 930 | continue_process(proc->pid); |
| 931 | } else { |
| Arnaud Patard | f3d1c53 | 2010-01-08 08:40:04 -0500 | [diff] [blame] | 932 | #if defined __sparc__ || defined __ia64___ || defined __mips__ |
| Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 933 | /* we don't want to singlestep here */ |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 934 | continue_process(proc->pid); |
| 935 | #else |
| Petr Machata | cb9a28d | 2012-03-28 11:11:32 +0200 | [diff] [blame] | 936 | if (process_install_stopping_handler |
| 937 | (proc, sbp, NULL, NULL, NULL) < 0) { |
| Petr Machata | c1aa958 | 2012-03-27 23:54:01 +0200 | [diff] [blame] | 938 | perror("process_stopping_handler_create"); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 939 | /* Carry on not bothering to re-enable. */ |
| 940 | continue_process(proc->pid); |
| Petr Machata | 98f0992 | 2011-07-09 10:55:29 +0200 | [diff] [blame] | 941 | } |
| Juan Cespedes | 5c3fe06 | 2004-06-14 18:08:37 +0200 | [diff] [blame] | 942 | #endif |
| Juan Cespedes | 8f8282f | 2002-03-03 18:58:40 +0100 | [diff] [blame] | 943 | } |
| 944 | } |
| 945 | |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 946 | /** |
| 947 | * Ltrace exit. When we are about to exit, we have to go through all |
| 948 | * the processes, stop them all, remove all the breakpoints, and then |
| 949 | * detach the processes that we attached to using -p. If we left the |
| 950 | * other tasks running, they might hit stray return breakpoints and |
| 951 | * produce artifacts, so we better stop everyone, even if it's a bit |
| 952 | * of extra work. |
| 953 | */ |
| 954 | struct ltrace_exiting_handler |
| 955 | { |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 956 | struct event_handler super; |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 957 | struct pid_set pids; |
| 958 | }; |
| 959 | |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 960 | static Event * |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 961 | ltrace_exiting_on_event(struct event_handler *super, Event *event) |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 962 | { |
| 963 | struct ltrace_exiting_handler * self = (void *)super; |
| 964 | Process * task = event->proc; |
| 965 | Process * leader = task->leader; |
| 966 | |
| Petr Machata | 9ace574 | 2012-04-14 02:29:57 +0200 | [diff] [blame] | 967 | debug(DEBUG_PROCESS, |
| 968 | "ltrace_exiting_on_event: pid %d; event type %d", |
| 969 | task->pid, event->type); |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 970 | |
| 971 | struct pid_task * task_info = get_task_info(&self->pids, task->pid); |
| 972 | handle_stopping_event(task_info, &event); |
| 973 | |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 974 | if (event != NULL && event->type == EVENT_BREAKPOINT) |
| 975 | undo_breakpoint(event, leader); |
| Petr Machata | 4b9f4d9 | 2011-08-20 04:07:05 +0200 | [diff] [blame] | 976 | |
| 977 | if (await_sigstop_delivery(&self->pids, task_info, event) |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 978 | && all_stops_accountable(&self->pids)) |
| 979 | detach_process(leader); |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 980 | |
| 981 | /* Sink all non-exit events. We are about to exit, so we |
| 982 | * don't bother with queuing them. */ |
| 983 | if (event_exit_or_none_p(event)) |
| 984 | return event; |
| Petr Machata | 13d5df7 | 2011-08-19 23:15:15 +0200 | [diff] [blame] | 985 | |
| Petr Machata | 13d5df7 | 2011-08-19 23:15:15 +0200 | [diff] [blame] | 986 | return NULL; |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | static void |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 990 | ltrace_exiting_destroy(struct event_handler *super) |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 991 | { |
| 992 | struct ltrace_exiting_handler * self = (void *)super; |
| 993 | free(self->pids.tasks); |
| 994 | } |
| 995 | |
| 996 | static int |
| 997 | ltrace_exiting_install_handler(Process * proc) |
| 998 | { |
| 999 | /* Only install to leader. */ |
| 1000 | if (proc->leader != proc) |
| 1001 | return 0; |
| 1002 | |
| 1003 | /* Perhaps we are already installed, if the user passed |
| 1004 | * several -p options that are tasks of one process. */ |
| 1005 | if (proc->event_handler != NULL |
| 1006 | && proc->event_handler->on_event == <race_exiting_on_event) |
| 1007 | return 0; |
| 1008 | |
| Petr Machata | 590c808 | 2011-08-20 22:45:26 +0200 | [diff] [blame] | 1009 | /* If stopping handler is already present, let it do the |
| 1010 | * work. */ |
| 1011 | if (proc->event_handler != NULL) { |
| 1012 | assert(proc->event_handler->on_event |
| 1013 | == &process_stopping_on_event); |
| 1014 | struct process_stopping_handler * other |
| 1015 | = (void *)proc->event_handler; |
| 1016 | other->exiting = 1; |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 1020 | struct ltrace_exiting_handler * handler |
| 1021 | = calloc(sizeof(*handler), 1); |
| 1022 | if (handler == NULL) { |
| 1023 | perror("malloc exiting handler"); |
| 1024 | fatal: |
| 1025 | /* XXXXXXXXXXXXXXXXXXX fixme */ |
| 1026 | return -1; |
| 1027 | } |
| 1028 | |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 1029 | handler->super.on_event = ltrace_exiting_on_event; |
| 1030 | handler->super.destroy = ltrace_exiting_destroy; |
| 1031 | install_event_handler(proc->leader, &handler->super); |
| 1032 | |
| Petr Machata | 74132a4 | 2012-03-16 02:46:18 +0100 | [diff] [blame] | 1033 | if (each_task(proc->leader, NULL, &send_sigstop, |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 1034 | &handler->pids) != NULL) |
| 1035 | goto fatal; |
| 1036 | |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1040 | /* |
| 1041 | * When the traced process vforks, it's suspended until the child |
| 1042 | * process calls _exit or exec*. In the meantime, the two share the |
| 1043 | * address space. |
| 1044 | * |
| 1045 | * The child process should only ever call _exit or exec*, but we |
| 1046 | * can't count on that (it's not the role of ltrace to policy, but to |
| 1047 | * observe). In any case, we will _at least_ have to deal with |
| 1048 | * removal of vfork return breakpoint (which we have to smuggle back |
| 1049 | * in, so that the parent can see it, too), and introduction of exec* |
| 1050 | * return breakpoint. Since we already have both breakpoint actions |
| 1051 | * to deal with, we might as well support it all. |
| 1052 | * |
| 1053 | * The gist is that we pretend that the child is in a thread group |
| 1054 | * with its parent, and handle it as a multi-threaded case, with the |
| 1055 | * exception that we know that the parent is blocked, and don't |
| 1056 | * attempt to stop it. When the child execs, we undo the setup. |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1057 | */ |
| 1058 | |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1059 | struct process_vfork_handler |
| 1060 | { |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 1061 | struct event_handler super; |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1062 | void * bp_addr; |
| 1063 | }; |
| 1064 | |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1065 | static Event * |
| Petr Machata | 366c2f4 | 2012-02-09 19:34:36 +0100 | [diff] [blame] | 1066 | process_vfork_on_event(struct event_handler *super, Event *event) |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1067 | { |
| Petr Machata | 9ace574 | 2012-04-14 02:29:57 +0200 | [diff] [blame] | 1068 | debug(DEBUG_PROCESS, |
| 1069 | "process_vfork_on_event: pid %d; event type %d", |
| 1070 | event->proc->pid, event->type); |
| 1071 | |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1072 | struct process_vfork_handler * self = (void *)super; |
| Petr Machata | bc37326 | 2012-02-07 23:31:15 +0100 | [diff] [blame] | 1073 | struct breakpoint *sbp; |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1074 | assert(self != NULL); |
| 1075 | |
| 1076 | switch (event->type) { |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1077 | case EVENT_BREAKPOINT: |
| 1078 | /* Remember the vfork return breakpoint. */ |
| Petr Machata | 77e8fa8 | 2012-04-19 18:36:32 +0200 | [diff] [blame] | 1079 | if (self->bp_addr == 0) |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1080 | self->bp_addr = event->e_un.brk_addr; |
| 1081 | break; |
| 1082 | |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1083 | case EVENT_EXIT: |
| 1084 | case EVENT_EXIT_SIGNAL: |
| 1085 | case EVENT_EXEC: |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1086 | /* Smuggle back in the vfork return breakpoint, so |
| 1087 | * that our parent can trip over it once again. */ |
| Petr Machata | 77e8fa8 | 2012-04-19 18:36:32 +0200 | [diff] [blame] | 1088 | if (self->bp_addr != 0) { |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1089 | sbp = dict_find_entry(event->proc->leader->breakpoints, |
| 1090 | self->bp_addr); |
| 1091 | if (sbp != NULL) |
| Petr Machata | 77e8fa8 | 2012-04-19 18:36:32 +0200 | [diff] [blame] | 1092 | assert(sbp->libsym == NULL); |
| 1093 | /* We don't mind failing that, it's not a big |
| 1094 | * deal to not display one extra vfork return. */ |
| 1095 | insert_breakpoint(event->proc->parent, |
| 1096 | self->bp_addr, NULL); |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1097 | } |
| 1098 | |
| Petr Machata | ba9911f | 2011-09-27 21:09:47 +0200 | [diff] [blame] | 1099 | continue_process(event->proc->parent->pid); |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1100 | |
| 1101 | /* Remove the leader that we artificially set up |
| 1102 | * earlier. */ |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1103 | change_process_leader(event->proc, event->proc); |
| 1104 | destroy_event_handler(event->proc); |
| 1105 | |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1106 | default: |
| 1107 | ; |
| 1108 | } |
| 1109 | |
| 1110 | return event; |
| 1111 | } |
| 1112 | |
| 1113 | void |
| 1114 | continue_after_vfork(Process * proc) |
| 1115 | { |
| 1116 | debug(DEBUG_PROCESS, "continue_after_vfork: pid=%d", proc->pid); |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1117 | struct process_vfork_handler * handler = calloc(sizeof(*handler), 1); |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1118 | if (handler == NULL) { |
| 1119 | perror("malloc vfork handler"); |
| 1120 | /* Carry on not bothering to treat the process as |
| 1121 | * necessary. */ |
| 1122 | continue_process(proc->parent->pid); |
| 1123 | return; |
| 1124 | } |
| 1125 | |
| 1126 | /* We must set up custom event handler, so that we see |
| 1127 | * exec/exit events for the task itself. */ |
| Petr Machata | 134a108 | 2011-09-27 20:25:58 +0200 | [diff] [blame] | 1128 | handler->super.on_event = process_vfork_on_event; |
| 1129 | install_event_handler(proc, &handler->super); |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1130 | |
| 1131 | /* Make sure that the child is sole thread. */ |
| 1132 | assert(proc->leader == proc); |
| 1133 | assert(proc->next == NULL || proc->next->leader != proc); |
| 1134 | |
| 1135 | /* Make sure that the child's parent is properly set up. */ |
| 1136 | assert(proc->parent != NULL); |
| 1137 | assert(proc->parent->leader != NULL); |
| 1138 | |
| 1139 | change_process_leader(proc, proc->parent->leader); |
| Petr Machata | cbe29c6 | 2011-09-27 02:27:58 +0200 | [diff] [blame] | 1140 | } |
| 1141 | |
| Petr Machata | 9d29b3e | 2011-11-09 16:46:56 +0100 | [diff] [blame] | 1142 | static int |
| 1143 | is_mid_stopping(Process *proc) |
| 1144 | { |
| 1145 | return proc != NULL |
| 1146 | && proc->event_handler != NULL |
| 1147 | && proc->event_handler->on_event == &process_stopping_on_event; |
| 1148 | } |
| 1149 | |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 1150 | void |
| 1151 | continue_after_syscall(Process * proc, int sysnum, int ret_p) |
| 1152 | { |
| 1153 | /* Don't continue if we are mid-stopping. */ |
| Petr Machata | 9d29b3e | 2011-11-09 16:46:56 +0100 | [diff] [blame] | 1154 | if (ret_p && (is_mid_stopping(proc) || is_mid_stopping(proc->leader))) { |
| 1155 | debug(DEBUG_PROCESS, |
| 1156 | "continue_after_syscall: don't continue %d", |
| 1157 | proc->pid); |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 1158 | return; |
| Petr Machata | 9d29b3e | 2011-11-09 16:46:56 +0100 | [diff] [blame] | 1159 | } |
| Petr Machata | 43d2fe5 | 2011-11-02 13:25:49 +0100 | [diff] [blame] | 1160 | continue_process(proc->pid); |
| 1161 | } |
| 1162 | |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 1163 | /* If ltrace gets SIGINT, the processes directly or indirectly run by |
| 1164 | * ltrace get it too. We just have to wait long enough for the signal |
| 1165 | * to be delivered and the process terminated, which we notice and |
| 1166 | * exit ltrace, too. So there's not much we need to do there. We |
| 1167 | * want to keep tracing those processes as usual, in case they just |
| 1168 | * SIG_IGN the SIGINT to do their shutdown etc. |
| 1169 | * |
| 1170 | * For processes ran on the background, we want to install an exit |
| 1171 | * handler that stops all the threads, removes all breakpoints, and |
| 1172 | * detaches. |
| 1173 | */ |
| 1174 | void |
| Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 1175 | os_ltrace_exiting(void) |
| Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 1176 | { |
| 1177 | struct opt_p_t * it; |
| 1178 | for (it = opt_p; it != NULL; it = it->next) { |
| 1179 | Process * proc = pid2proc(it->pid); |
| 1180 | if (proc == NULL || proc->leader == NULL) |
| 1181 | continue; |
| 1182 | if (ltrace_exiting_install_handler(proc->leader) < 0) |
| 1183 | fprintf(stderr, |
| 1184 | "Couldn't install exiting handler for %d.\n", |
| 1185 | proc->pid); |
| 1186 | } |
| 1187 | } |
| 1188 | |
| Petr Machata | ffe4cd2 | 2012-04-11 18:01:44 +0200 | [diff] [blame] | 1189 | int |
| 1190 | os_ltrace_exiting_sighandler(void) |
| 1191 | { |
| 1192 | extern int linux_in_waitpid; |
| 1193 | if (linux_in_waitpid) { |
| 1194 | os_ltrace_exiting(); |
| 1195 | return 1; |
| 1196 | } |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 1200 | size_t |
| 1201 | umovebytes(Process *proc, void *addr, void *laddr, size_t len) { |
| 1202 | |
| 1203 | union { |
| 1204 | long a; |
| 1205 | char c[sizeof(long)]; |
| 1206 | } a; |
| Zachary T Welch | ba6aca2 | 2010-12-08 18:55:09 -0800 | [diff] [blame] | 1207 | int started = 0; |
| 1208 | size_t offset = 0, bytes_read = 0; |
| Joe Damato | dfa3fa3 | 2010-11-08 15:47:35 -0800 | [diff] [blame] | 1209 | |
| 1210 | while (offset < len) { |
| 1211 | a.a = ptrace(PTRACE_PEEKTEXT, proc->pid, addr + offset, 0); |
| 1212 | if (a.a == -1 && errno) { |
| 1213 | if (started && errno == EIO) |
| 1214 | return bytes_read; |
| 1215 | else |
| 1216 | return -1; |
| 1217 | } |
| 1218 | started = 1; |
| 1219 | |
| 1220 | if (len - offset >= sizeof(long)) { |
| 1221 | memcpy(laddr + offset, &a.c[0], sizeof(long)); |
| 1222 | bytes_read += sizeof(long); |
| 1223 | } |
| 1224 | else { |
| 1225 | memcpy(laddr + offset, &a.c[0], len - offset); |
| 1226 | bytes_read += (len - offset); |
| 1227 | } |
| 1228 | offset += sizeof(long); |
| 1229 | } |
| 1230 | |
| 1231 | return bytes_read; |
| 1232 | } |