Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #if defined(HAVE_LIBUNWIND) |
| 4 | #include <libunwind.h> |
| 5 | #include <libunwind-ptrace.h> |
| 6 | #endif /* defined(HAVE_LIBUNWIND) */ |
| 7 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 8 | #include <sys/types.h> |
| 9 | #include <string.h> |
| 10 | #include <stdio.h> |
| 11 | #include <errno.h> |
| 12 | #include <stdlib.h> |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 13 | #include <assert.h> |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 14 | |
Juan Cespedes | f728123 | 2009-06-25 16:11:21 +0200 | [diff] [blame] | 15 | #include "common.h" |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 16 | |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 17 | Process * |
Petr Machata | c7585b6 | 2011-07-08 22:58:12 +0200 | [diff] [blame] | 18 | open_program(char *filename, pid_t pid, int enable) { |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 19 | Process *proc; |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 20 | assert(pid != 0); |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 21 | proc = calloc(sizeof(Process), 1); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 22 | if (!proc) { |
| 23 | perror("malloc"); |
| 24 | exit(1); |
| 25 | } |
Juan Cespedes | e0660df | 2009-05-21 18:14:39 +0200 | [diff] [blame] | 26 | proc->filename = strdup(filename); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 27 | proc->breakpoints_enabled = -1; |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 28 | proc->pid = pid; |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 29 | #if defined(HAVE_LIBUNWIND) |
Petr Machata | 1b17dbf | 2011-07-08 19:22:52 +0200 | [diff] [blame] | 30 | proc->unwind_priv = _UPT_create(pid); |
| 31 | proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0); |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 32 | #endif /* defined(HAVE_LIBUNWIND) */ |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 33 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 34 | add_process(proc); |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 35 | assert(proc->leader != NULL); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 36 | |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 37 | if (proc->leader == proc) |
| 38 | breakpoints_init(proc, enable); |
Joe Damato | ab3b72c | 2010-10-31 00:21:53 -0700 | [diff] [blame] | 39 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 40 | return proc; |
| 41 | } |
| 42 | |
Petr Machata | 3c516d5 | 2011-08-18 03:53:18 +0200 | [diff] [blame^] | 43 | static int |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 44 | open_one_pid(pid_t pid) |
| 45 | { |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 46 | Process *proc; |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 47 | char *filename; |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 48 | debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid); |
| 49 | |
Petr Machata | 3c516d5 | 2011-08-18 03:53:18 +0200 | [diff] [blame^] | 50 | if (trace_pid(pid) < 0) |
| 51 | return 0; |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 52 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 53 | filename = pid2name(pid); |
Petr Machata | 3c516d5 | 2011-08-18 03:53:18 +0200 | [diff] [blame^] | 54 | if (filename == NULL) |
| 55 | return 0; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 56 | |
Petr Machata | c7585b6 | 2011-07-08 22:58:12 +0200 | [diff] [blame] | 57 | proc = open_program(filename, pid, 1); |
Petr Machata | 602330f | 2011-07-09 11:15:34 +0200 | [diff] [blame] | 58 | trace_set_options(proc, pid); |
Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 59 | continue_process(pid); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 60 | proc->breakpoints_enabled = 1; |
Petr Machata | 3c516d5 | 2011-08-18 03:53:18 +0200 | [diff] [blame^] | 61 | |
| 62 | return 1; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 63 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 64 | |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 65 | void |
| 66 | open_pid(pid_t pid) |
| 67 | { |
| 68 | debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid); |
Petr Machata | 3c516d5 | 2011-08-18 03:53:18 +0200 | [diff] [blame^] | 69 | /* If we are already tracing this guy, we should be seeing all |
| 70 | * his children via normal tracing route. */ |
| 71 | if (pid2proc(pid) != NULL) |
| 72 | return; |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 73 | |
Petr Machata | 3c516d5 | 2011-08-18 03:53:18 +0200 | [diff] [blame^] | 74 | /* First, see if we can attach the requested PID itself. */ |
| 75 | if (!open_one_pid(pid)) { |
| 76 | fprintf(stderr, "Cannot attach to pid %u: %s\n", |
| 77 | pid, strerror(errno)); |
| 78 | return; |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Petr Machata | 3c516d5 | 2011-08-18 03:53:18 +0200 | [diff] [blame^] | 81 | /* Now attach to all tasks that belong to that PID. There's a |
| 82 | * race between process_tasks and open_one_pid. So when we |
| 83 | * fail in open_one_pid below, we just do another round. |
| 84 | * Chances are that by then that PID will have gone away, and |
| 85 | * that's why we have seen the failure. The processes that we |
| 86 | * manage to open_one_pid are stopped, so we should eventually |
| 87 | * reach a point where process_tasks doesn't give any new |
| 88 | * processes (because there's nobody left to produce |
| 89 | * them). */ |
| 90 | int have_all; |
| 91 | do { |
| 92 | pid_t *tasks; |
| 93 | size_t ntasks; |
| 94 | size_t i; |
| 95 | if (process_tasks(pid, &tasks, &ntasks) < 0) { |
| 96 | fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n", |
| 97 | pid, strerror(errno)); |
| 98 | return; |
| 99 | } |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 100 | |
Petr Machata | 3c516d5 | 2011-08-18 03:53:18 +0200 | [diff] [blame^] | 101 | have_all = 1; |
| 102 | for (i = 0; i < ntasks; ++i) |
| 103 | if (pid2proc(tasks[i]) == NULL |
| 104 | && !open_one_pid(tasks[i])) |
| 105 | have_all = 0; |
| 106 | |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 107 | free(tasks); |
Petr Machata | 3c516d5 | 2011-08-18 03:53:18 +0200 | [diff] [blame^] | 108 | |
| 109 | } while (have_all == 0); |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 110 | } |
| 111 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 112 | static enum pcb_status |
| 113 | find_proc(Process * proc, void * data) |
| 114 | { |
| 115 | pid_t pid = (pid_t)(uintptr_t)data; |
| 116 | return proc->pid == pid ? pcb_stop : pcb_cont; |
| 117 | } |
| 118 | |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 119 | Process * |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 120 | pid2proc(pid_t pid) { |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 121 | return each_process(NULL, &find_proc, (void *)(uintptr_t)pid); |
| 122 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 123 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 124 | |
| 125 | static Process * list_of_processes = NULL; |
| 126 | |
| 127 | Process * |
| 128 | each_process(Process * proc, |
| 129 | enum pcb_status (* cb)(Process * proc, void * data), |
| 130 | void * data) |
| 131 | { |
| 132 | Process * it = proc ?: list_of_processes; |
| 133 | for (; it != NULL; ) { |
| 134 | /* Callback might call remove_process. */ |
| 135 | Process * next = it->next; |
| 136 | if ((*cb) (it, data) == pcb_stop) |
| 137 | return it; |
| 138 | it = next; |
| 139 | } |
| 140 | return NULL; |
| 141 | } |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 142 | |
| 143 | Process * |
| 144 | each_task(Process * it, enum pcb_status (* cb)(Process * proc, void * data), |
| 145 | void * data) |
| 146 | { |
| 147 | if (it != NULL) { |
| 148 | Process * leader = it->leader; |
| 149 | for (; it != NULL && it->leader == leader; ) { |
| 150 | /* Callback might call remove_process. */ |
| 151 | Process * next = it->next; |
| 152 | if ((*cb) (it, data) == pcb_stop) |
| 153 | return it; |
| 154 | it = next; |
| 155 | } |
| 156 | } |
| 157 | return NULL; |
| 158 | } |
| 159 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 160 | void |
| 161 | add_process(Process * proc) |
| 162 | { |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 163 | Process ** leaderp = &list_of_processes; |
| 164 | if (proc->pid) { |
| 165 | pid_t tgid = process_leader(proc->pid); |
| 166 | if (tgid == proc->pid) |
| 167 | proc->leader = proc; |
| 168 | else { |
| 169 | Process * leader = pid2proc(tgid); |
| 170 | proc->leader = leader; |
| 171 | if (leader != NULL) |
| 172 | // NULL: sub-task added before leader? |
| 173 | leaderp = &leader->next; |
| 174 | } |
| 175 | } |
| 176 | proc->next = *leaderp; |
| 177 | *leaderp = proc; |
| 178 | } |
| 179 | |
| 180 | static enum pcb_status |
| 181 | clear_leader(Process * proc, void * data) |
| 182 | { |
| 183 | debug(DEBUG_FUNCTION, "detach_task %d from leader %d", |
| 184 | proc->pid, proc->leader->pid); |
| 185 | proc->leader = NULL; |
| 186 | return pcb_cont; |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 187 | } |
| 188 | |
Petr Machata | 69a03e6 | 2011-07-09 11:29:12 +0200 | [diff] [blame] | 189 | static enum ecb_status |
| 190 | event_for_proc(Event * event, void * data) |
| 191 | { |
| 192 | if (event->proc == data) |
| 193 | return ecb_deque; |
| 194 | else |
| 195 | return ecb_cont; |
| 196 | } |
| 197 | |
| 198 | static void |
| 199 | delete_events_for(Process * proc) |
| 200 | { |
| 201 | Event * event; |
| 202 | while ((event = each_qd_event(&event_for_proc, proc)) != NULL) |
| 203 | free(event); |
| 204 | } |
| 205 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 206 | void |
| 207 | remove_process(Process *proc) |
| 208 | { |
| 209 | Process *tmp, *tmp2; |
| 210 | |
| 211 | debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid); |
| 212 | |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame] | 213 | if (proc->leader == proc) |
| 214 | each_task(proc, &clear_leader, NULL); |
| 215 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 216 | if (list_of_processes == proc) { |
| 217 | tmp = list_of_processes; |
| 218 | list_of_processes = list_of_processes->next; |
Petr Machata | 69a03e6 | 2011-07-09 11:29:12 +0200 | [diff] [blame] | 219 | delete_events_for(tmp); |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 220 | free(tmp); |
| 221 | return; |
| 222 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 223 | tmp = list_of_processes; |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 224 | while (tmp->next) { |
| 225 | if (tmp->next == proc) { |
| 226 | tmp2 = tmp->next; |
| 227 | tmp->next = tmp->next->next; |
Petr Machata | 69a03e6 | 2011-07-09 11:29:12 +0200 | [diff] [blame] | 228 | delete_events_for(tmp2); |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 229 | free(tmp2); |
| 230 | return; |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 231 | } |
| 232 | tmp = tmp->next; |
| 233 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 234 | } |
Petr Machata | 4007d74 | 2011-07-09 11:29:42 +0200 | [diff] [blame] | 235 | |
| 236 | void |
| 237 | install_event_handler(Process * proc, Event_Handler * handler) |
| 238 | { |
Petr Machata | 75dcf7d | 2011-10-06 14:30:19 +0200 | [diff] [blame] | 239 | debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler); |
Petr Machata | 4007d74 | 2011-07-09 11:29:42 +0200 | [diff] [blame] | 240 | assert(proc->event_handler == NULL); |
| 241 | proc->event_handler = handler; |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | destroy_event_handler(Process * proc) |
| 246 | { |
| 247 | Event_Handler * handler = proc->event_handler; |
Petr Machata | 75dcf7d | 2011-10-06 14:30:19 +0200 | [diff] [blame] | 248 | debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler); |
Petr Machata | 4007d74 | 2011-07-09 11:29:42 +0200 | [diff] [blame] | 249 | assert(handler != NULL); |
| 250 | handler->destroy(handler); |
| 251 | free(handler); |
| 252 | proc->event_handler = NULL; |
| 253 | } |