blob: ded0c95f21d4d2c664486fb9b2d7df4f8add6a73 [file] [log] [blame]
Joe Damatoab3b72c2010-10-31 00:21:53 -07001#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 Cespedes273ea6d1998-03-14 23:02:40 +01008#include <sys/types.h>
9#include <string.h>
10#include <stdio.h>
11#include <errno.h>
12#include <stdlib.h>
Petr Machata1b17dbf2011-07-08 19:22:52 +020013#include <assert.h>
Petr Machata1974dbc2011-08-19 18:58:01 +020014#include <error.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +010015
Juan Cespedesf7281232009-06-25 16:11:21 +020016#include "common.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010017
Juan Cespedesa8909f72009-04-28 20:02:41 +020018Process *
Petr Machatac7585b62011-07-08 22:58:12 +020019open_program(char *filename, pid_t pid, int enable) {
Juan Cespedesa8909f72009-04-28 20:02:41 +020020 Process *proc;
Petr Machata1b17dbf2011-07-08 19:22:52 +020021 assert(pid != 0);
Juan Cespedesa8909f72009-04-28 20:02:41 +020022 proc = calloc(sizeof(Process), 1);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010023 if (!proc) {
24 perror("malloc");
25 exit(1);
26 }
Petr Machata1974dbc2011-08-19 18:58:01 +020027
Juan Cespedese0660df2009-05-21 18:14:39 +020028 proc->filename = strdup(filename);
Juan Cespedes273ea6d1998-03-14 23:02:40 +010029 proc->breakpoints_enabled = -1;
Petr Machata1b17dbf2011-07-08 19:22:52 +020030 proc->pid = pid;
Joe Damatoab3b72c2010-10-31 00:21:53 -070031#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020032 proc->unwind_priv = _UPT_create(pid);
33 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070034#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070035
Petr Machatacebb8842011-07-09 11:14:11 +020036 add_process(proc);
Petr Machata1974dbc2011-08-19 18:58:01 +020037 if (proc->leader == NULL) {
38 free(proc);
39 return NULL;
40 }
Juan Cespedes273ea6d1998-03-14 23:02:40 +010041
Petr Machata9a5420c2011-07-09 11:21:23 +020042 if (proc->leader == proc)
Petr Machata1974dbc2011-08-19 18:58:01 +020043 if (breakpoints_init(proc, enable)) {
44 fprintf(stderr, "failed to init breakpoints %d\n",
45 proc->pid);
46 remove_process(proc);
47 return NULL;
48 }
Joe Damatoab3b72c2010-10-31 00:21:53 -070049
Juan Cespedes273ea6d1998-03-14 23:02:40 +010050 return proc;
51}
52
Petr Machata3c516d52011-08-18 03:53:18 +020053static int
Petr Machata9a5420c2011-07-09 11:21:23 +020054open_one_pid(pid_t pid)
55{
Juan Cespedesa8909f72009-04-28 20:02:41 +020056 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +010057 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +020058 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
59
Petr Machata1974dbc2011-08-19 18:58:01 +020060 /* Get the filename first. Should the trace_pid fail, we can
61 * easily free it, untracing is more work. */
62 if ((filename = pid2name(pid)) == NULL
63 || trace_pid(pid) < 0) {
64 free(filename);
65 return -1;
66 }
Juan Cespedes35d70631998-03-15 14:05:40 +010067
Petr Machata1974dbc2011-08-19 18:58:01 +020068 proc = open_program(filename, pid, 0);
69 if (proc == NULL)
70 return -1;
Petr Machata602330f2011-07-09 11:15:34 +020071 trace_set_options(proc, pid);
Petr Machata3c516d52011-08-18 03:53:18 +020072
Petr Machata1974dbc2011-08-19 18:58:01 +020073 return 0;
74}
75
76enum pcb_status
77start_one_pid(Process * proc, void * data)
78{
79 continue_process(proc->pid);
80 proc->breakpoints_enabled = 1;
81 return pcb_cont;
Juan Cespedes273ea6d1998-03-14 23:02:40 +010082}
Juan Cespedese74c80d2009-02-11 11:32:31 +010083
Petr Machata9a5420c2011-07-09 11:21:23 +020084void
85open_pid(pid_t pid)
86{
87 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +020088 /* If we are already tracing this guy, we should be seeing all
89 * his children via normal tracing route. */
90 if (pid2proc(pid) != NULL)
91 return;
Petr Machata9a5420c2011-07-09 11:21:23 +020092
Petr Machata3c516d52011-08-18 03:53:18 +020093 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +020094 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +020095 fprintf(stderr, "Cannot attach to pid %u: %s\n",
96 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +020097 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +020098 return;
Petr Machata9a5420c2011-07-09 11:21:23 +020099 }
100
Petr Machata3c516d52011-08-18 03:53:18 +0200101 /* Now attach to all tasks that belong to that PID. There's a
102 * race between process_tasks and open_one_pid. So when we
103 * fail in open_one_pid below, we just do another round.
104 * Chances are that by then that PID will have gone away, and
105 * that's why we have seen the failure. The processes that we
106 * manage to open_one_pid are stopped, so we should eventually
107 * reach a point where process_tasks doesn't give any new
108 * processes (because there's nobody left to produce
109 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200110 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200111 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200112 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200113 pid_t *tasks;
114 size_t ntasks;
115 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200116
Petr Machata3c516d52011-08-18 03:53:18 +0200117 if (process_tasks(pid, &tasks, &ntasks) < 0) {
118 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
119 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100120 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200121 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200122
Petr Machata3c516d52011-08-18 03:53:18 +0200123 have_all = 1;
124 for (i = 0; i < ntasks; ++i)
125 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200126 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200127 have_all = 0;
128
Petr Machata9a5420c2011-07-09 11:21:23 +0200129 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200130
Petr Machata1974dbc2011-08-19 18:58:01 +0200131 if (have_all && old_ntasks == ntasks)
132 break;
133 old_ntasks = ntasks;
134 }
135
136 /* Done. Now initialize breakpoints and then continue
137 * everyone. */
138 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200139 leader = pid2proc(pid)->leader;
140 enable_all_breakpoints(leader);
141
142 each_task(pid2proc(pid)->leader, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200143}
144
Petr Machatacebb8842011-07-09 11:14:11 +0200145static enum pcb_status
146find_proc(Process * proc, void * data)
147{
148 pid_t pid = (pid_t)(uintptr_t)data;
149 return proc->pid == pid ? pcb_stop : pcb_cont;
150}
151
Juan Cespedesa8909f72009-04-28 20:02:41 +0200152Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100153pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200154 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
155}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100156
Petr Machatacebb8842011-07-09 11:14:11 +0200157static Process * list_of_processes = NULL;
158
Petr Machatacbe29c62011-09-27 02:27:58 +0200159static void
160unlist_process(Process * proc)
161{
162 Process *tmp;
163
164 if (list_of_processes == proc) {
165 list_of_processes = list_of_processes->next;
166 return;
167 }
168
169 for (tmp = list_of_processes; ; tmp = tmp->next) {
170 /* If the following assert fails, the process wasn't
171 * in the list. */
172 assert(tmp->next != NULL);
173
174 if (tmp->next == proc) {
175 tmp->next = tmp->next->next;
176 return;
177 }
178 }
179}
180
Petr Machatacebb8842011-07-09 11:14:11 +0200181Process *
182each_process(Process * proc,
183 enum pcb_status (* cb)(Process * proc, void * data),
184 void * data)
185{
186 Process * it = proc ?: list_of_processes;
187 for (; it != NULL; ) {
188 /* Callback might call remove_process. */
189 Process * next = it->next;
190 if ((*cb) (it, data) == pcb_stop)
191 return it;
192 it = next;
193 }
194 return NULL;
195}
Petr Machata9a5420c2011-07-09 11:21:23 +0200196
197Process *
198each_task(Process * it, enum pcb_status (* cb)(Process * proc, void * data),
199 void * data)
200{
201 if (it != NULL) {
202 Process * leader = it->leader;
203 for (; it != NULL && it->leader == leader; ) {
204 /* Callback might call remove_process. */
205 Process * next = it->next;
206 if ((*cb) (it, data) == pcb_stop)
207 return it;
208 it = next;
209 }
210 }
211 return NULL;
212}
213
Petr Machatacebb8842011-07-09 11:14:11 +0200214void
215add_process(Process * proc)
216{
Petr Machata9a5420c2011-07-09 11:21:23 +0200217 Process ** leaderp = &list_of_processes;
218 if (proc->pid) {
219 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200220 if (tgid == 0)
221 /* Must have been terminated before we managed
222 * to fully attach. */
223 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200224 if (tgid == proc->pid)
225 proc->leader = proc;
226 else {
227 Process * leader = pid2proc(tgid);
228 proc->leader = leader;
229 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200230 leaderp = &leader->next;
231 }
232 }
233 proc->next = *leaderp;
234 *leaderp = proc;
235}
236
Petr Machatacbe29c62011-09-27 02:27:58 +0200237void
238change_process_leader(Process * proc, Process * leader)
239{
240 Process ** leaderp = &list_of_processes;
241 if (proc->leader == leader)
242 return;
243
244 assert(leader != NULL);
245 unlist_process(proc);
246 if (proc != leader)
247 leaderp = &leader->next;
248
249 proc->leader = leader;
250 proc->next = *leaderp;
251 *leaderp = proc;
252}
253
Petr Machata9a5420c2011-07-09 11:21:23 +0200254static enum pcb_status
255clear_leader(Process * proc, void * data)
256{
257 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
258 proc->pid, proc->leader->pid);
259 proc->leader = NULL;
260 return pcb_cont;
Petr Machatacebb8842011-07-09 11:14:11 +0200261}
262
Petr Machata69a03e62011-07-09 11:29:12 +0200263static enum ecb_status
264event_for_proc(Event * event, void * data)
265{
266 if (event->proc == data)
267 return ecb_deque;
268 else
269 return ecb_cont;
270}
271
272static void
273delete_events_for(Process * proc)
274{
275 Event * event;
276 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
277 free(event);
278}
279
Petr Machatacebb8842011-07-09 11:14:11 +0200280void
281remove_process(Process *proc)
282{
Petr Machatacebb8842011-07-09 11:14:11 +0200283 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
284
Petr Machata9a5420c2011-07-09 11:21:23 +0200285 if (proc->leader == proc)
286 each_task(proc, &clear_leader, NULL);
287
Petr Machatacbe29c62011-09-27 02:27:58 +0200288 unlist_process(proc);
289 delete_events_for(proc);
290 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100291}
Petr Machata4007d742011-07-09 11:29:42 +0200292
293void
294install_event_handler(Process * proc, Event_Handler * handler)
295{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200296 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200297 assert(proc->event_handler == NULL);
298 proc->event_handler = handler;
299}
300
301void
302destroy_event_handler(Process * proc)
303{
304 Event_Handler * handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200305 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200306 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200307 if (handler->destroy != NULL)
308 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200309 free(handler);
310 proc->event_handler = NULL;
311}