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