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 | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 43 | static void |
| 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 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 50 | |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 51 | if (trace_pid(pid) < 0) { |
| 52 | fprintf(stderr, "Cannot attach to pid %u: %s\n", pid, |
| 53 | strerror(errno)); |
Juan Cespedes | 35d7063 | 1998-03-15 14:05:40 +0100 | [diff] [blame] | 54 | return; |
| 55 | } |
| 56 | |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 57 | filename = pid2name(pid); |
| 58 | |
| 59 | if (!filename) { |
Juan Cespedes | 8d1b92b | 2009-07-03 10:39:34 +0200 | [diff] [blame] | 60 | fprintf(stderr, "Cannot trace pid %u: %s\n", pid, |
Ian Wienand | 2d45b1a | 2006-02-20 22:48:07 +0100 | [diff] [blame] | 61 | strerror(errno)); |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 62 | return; |
| 63 | } |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 64 | |
Petr Machata | c7585b6 | 2011-07-08 22:58:12 +0200 | [diff] [blame] | 65 | proc = open_program(filename, pid, 1); |
Juan Cespedes | aee0931 | 2007-08-31 18:49:48 +0200 | [diff] [blame] | 66 | continue_process(pid); |
Ian Wienand | 9a2ad35 | 2006-02-20 22:44:45 +0100 | [diff] [blame] | 67 | proc->breakpoints_enabled = 1; |
Juan Cespedes | 273ea6d | 1998-03-14 23:02:40 +0100 | [diff] [blame] | 68 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 69 | |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 70 | void |
| 71 | open_pid(pid_t pid) |
| 72 | { |
| 73 | debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid); |
| 74 | pid_t *tasks; |
| 75 | size_t ntasks; |
| 76 | int should_free = 1; |
| 77 | if (process_tasks(pid, &tasks, &ntasks) < 0) { |
| 78 | fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n", pid, |
| 79 | strerror(errno)); |
| 80 | |
| 81 | // Attach at least this one. |
| 82 | tasks = &pid; |
| 83 | ntasks = 1; |
| 84 | should_free = 0; |
| 85 | } |
| 86 | |
| 87 | size_t i; |
| 88 | for (i = 0; i < ntasks; ++i) |
| 89 | open_one_pid(tasks[i]); |
| 90 | |
| 91 | if (should_free) |
| 92 | free(tasks); |
| 93 | } |
| 94 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 95 | static enum pcb_status |
| 96 | find_proc(Process * proc, void * data) |
| 97 | { |
| 98 | pid_t pid = (pid_t)(uintptr_t)data; |
| 99 | return proc->pid == pid ? pcb_stop : pcb_cont; |
| 100 | } |
| 101 | |
Juan Cespedes | a8909f7 | 2009-04-28 20:02:41 +0200 | [diff] [blame] | 102 | Process * |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 103 | pid2proc(pid_t pid) { |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 104 | return each_process(NULL, &find_proc, (void *)(uintptr_t)pid); |
| 105 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 106 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 107 | |
| 108 | static Process * list_of_processes = NULL; |
| 109 | |
| 110 | Process * |
| 111 | each_process(Process * proc, |
| 112 | enum pcb_status (* cb)(Process * proc, void * data), |
| 113 | void * data) |
| 114 | { |
| 115 | Process * it = proc ?: list_of_processes; |
| 116 | for (; it != NULL; ) { |
| 117 | /* Callback might call remove_process. */ |
| 118 | Process * next = it->next; |
| 119 | if ((*cb) (it, data) == pcb_stop) |
| 120 | return it; |
| 121 | it = next; |
| 122 | } |
| 123 | return NULL; |
| 124 | } |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 125 | |
| 126 | Process * |
| 127 | each_task(Process * it, enum pcb_status (* cb)(Process * proc, void * data), |
| 128 | void * data) |
| 129 | { |
| 130 | if (it != NULL) { |
| 131 | Process * leader = it->leader; |
| 132 | for (; it != NULL && it->leader == leader; ) { |
| 133 | /* Callback might call remove_process. */ |
| 134 | Process * next = it->next; |
| 135 | if ((*cb) (it, data) == pcb_stop) |
| 136 | return it; |
| 137 | it = next; |
| 138 | } |
| 139 | } |
| 140 | return NULL; |
| 141 | } |
| 142 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 143 | void |
| 144 | add_process(Process * proc) |
| 145 | { |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 146 | Process ** leaderp = &list_of_processes; |
| 147 | if (proc->pid) { |
| 148 | pid_t tgid = process_leader(proc->pid); |
| 149 | if (tgid == proc->pid) |
| 150 | proc->leader = proc; |
| 151 | else { |
| 152 | Process * leader = pid2proc(tgid); |
| 153 | proc->leader = leader; |
| 154 | if (leader != NULL) |
| 155 | // NULL: sub-task added before leader? |
| 156 | leaderp = &leader->next; |
| 157 | } |
| 158 | } |
| 159 | proc->next = *leaderp; |
| 160 | *leaderp = proc; |
| 161 | } |
| 162 | |
| 163 | static enum pcb_status |
| 164 | clear_leader(Process * proc, void * data) |
| 165 | { |
| 166 | debug(DEBUG_FUNCTION, "detach_task %d from leader %d", |
| 167 | proc->pid, proc->leader->pid); |
| 168 | proc->leader = NULL; |
| 169 | return pcb_cont; |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | void |
| 173 | remove_process(Process *proc) |
| 174 | { |
| 175 | Process *tmp, *tmp2; |
| 176 | |
| 177 | debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid); |
| 178 | |
Petr Machata | 9a5420c | 2011-07-09 11:21:23 +0200 | [diff] [blame^] | 179 | if (proc->leader == proc) |
| 180 | each_task(proc, &clear_leader, NULL); |
| 181 | |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 182 | if (list_of_processes == proc) { |
| 183 | tmp = list_of_processes; |
| 184 | list_of_processes = list_of_processes->next; |
| 185 | free(tmp); |
| 186 | return; |
| 187 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 188 | tmp = list_of_processes; |
Petr Machata | cebb884 | 2011-07-09 11:14:11 +0200 | [diff] [blame] | 189 | while (tmp->next) { |
| 190 | if (tmp->next == proc) { |
| 191 | tmp2 = tmp->next; |
| 192 | tmp->next = tmp->next->next; |
| 193 | free(tmp2); |
| 194 | return; |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 195 | } |
| 196 | tmp = tmp->next; |
| 197 | } |
Juan Cespedes | e74c80d | 2009-02-11 11:32:31 +0100 | [diff] [blame] | 198 | } |