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