blob: 627e93ccb0fd2c8cd68ffef875d731bd678d2666 [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"
Petr Machata9294d822012-02-07 12:35:58 +010017#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010018#include "proc.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010019
Petr Machata744f2552012-04-15 04:33:18 +020020#ifndef ARCH_HAVE_PROCESS_DATA
21int
22arch_process_init(struct Process *proc)
23{
24 return 0;
25}
26
27void
28arch_process_destroy(struct Process *proc)
29{
30}
31
32int
33arch_process_clone(struct Process *retp, struct Process *proc)
34{
35 return 0;
36}
37
38int
39arch_process_exec(struct Process *proc)
40{
41 return 0;
42}
43#endif
44
Petr Machata93d95df2012-04-17 05:16:19 +020045#ifndef ARCH_HAVE_DYNLINK_DONE
46void
47arch_dynlink_done(struct Process *proc)
48{
49}
50#endif
51
Petr Machata3d0c91c2012-04-14 02:37:38 +020052static void add_process(struct Process *proc, int was_exec);
Petr Machata44965c72012-04-06 19:59:20 +020053
Petr Machata2b46cfc2012-02-18 11:17:29 +010054static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020055process_bare_init(struct Process *proc, const char *filename,
56 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010057{
Petr Machata3d0c91c2012-04-14 02:37:38 +020058 if (!was_exec) {
59 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020060
Petr Machata3d0c91c2012-04-14 02:37:38 +020061 proc->filename = strdup(filename);
62 if (proc->filename == NULL) {
63 fail:
64 free(proc->filename);
65 if (proc->breakpoints != NULL)
66 dict_clear(proc->breakpoints);
67 return -1;
68 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010069 }
70
71 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020072 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +020073 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +010074 if (proc->leader == NULL)
75 goto fail;
76
77 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +020078 proc->breakpoints = dict_init(target_address_hash,
79 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +010080 if (proc->breakpoints == NULL)
81 goto fail;
82 } else {
83 proc->breakpoints = NULL;
84 }
85
Joe Damatoab3b72c2010-10-31 00:21:53 -070086#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020087 proc->unwind_priv = _UPT_create(pid);
88 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070089#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070090
Petr Machata2b46cfc2012-02-18 11:17:29 +010091 return 0;
92}
93
94static void
Petr Machata3d0c91c2012-04-14 02:37:38 +020095process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010096{
Petr Machata2b46cfc2012-02-18 11:17:29 +010097 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +020098 if (!was_exec) {
99 free(proc->filename);
100 remove_process(proc);
101 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100102}
103
Petr Machata3d0c91c2012-04-14 02:37:38 +0200104static int
105process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100106{
Petr Machata18bd8ff2012-04-10 04:32:39 +0200107 target_address_t entry;
108 target_address_t interp_bias;
109 if (process_get_entry(proc, &entry, &interp_bias) < 0) {
110 fprintf(stderr, "Couldn't get entry points of process %d\n",
Petr Machata2b46cfc2012-02-18 11:17:29 +0100111 proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100112 return -1;
113 }
114
Petr Machata3d0c91c2012-04-14 02:37:38 +0200115 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200116 fprintf(stderr, "failed to init breakpoints %d\n",
117 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200118 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200119 }
120
Petr Machata2b46cfc2012-02-18 11:17:29 +0100121 return 0;
122}
123
Petr Machata3d0c91c2012-04-14 02:37:38 +0200124int
125process_init(struct Process *proc, const char *filename, pid_t pid)
126{
127 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200128 fail:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200129 error(0, errno, "init process %d", pid);
130 return -1;
131 }
132
Petr Machata744f2552012-04-15 04:33:18 +0200133 if (arch_process_init(proc) < 0) {
134 process_bare_destroy(proc, 0);
135 goto fail;
136 }
137
Petr Machata218c5ff2012-04-15 04:22:39 +0200138 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200139 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200140 if (process_init_main(proc) < 0) {
141 process_bare_destroy(proc, 0);
142 goto fail;
143 }
144 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200145}
146
Petr Machata8ead1cd2012-04-24 18:13:09 +0200147static enum callback_status
148destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200149{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200150 breakpoint_destroy(bp);
151 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200152 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200153}
154
155static void
156private_process_destroy(struct Process *proc, int keep_filename)
157{
158 if (!keep_filename)
159 free(proc->filename);
160
Petr Machata8ead1cd2012-04-24 18:13:09 +0200161 /* Libraries and symbols. This is only relevant in
162 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200163 struct library *lib;
164 for (lib = proc->libraries; lib != NULL; ) {
165 struct library *next = lib->next;
166 library_destroy(lib);
167 free(lib);
168 lib = next;
169 }
170 proc->libraries = NULL;
171
172 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200173 if (proc->breakpoints != NULL) {
174 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
175 dict_clear(proc->breakpoints);
176 proc->breakpoints = NULL;
177 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178}
179
180void
181process_destroy(struct Process *proc)
182{
183 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200184 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200185}
186
187int
188process_exec(struct Process *proc)
189{
Petr Machata744f2552012-04-15 04:33:18 +0200190 /* Call exec first, before we destroy the main state. */
191 if (arch_process_exec(proc) < 0)
192 return -1;
193
Petr Machata3d0c91c2012-04-14 02:37:38 +0200194 private_process_destroy(proc, 1);
195 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
196 return -1;
197 if (process_init_main(proc) < 0) {
198 process_bare_destroy(proc, 1);
199 return -1;
200 }
201 return 0;
202}
203
Petr Machata2b46cfc2012-02-18 11:17:29 +0100204struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200205open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100206{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100207 assert(pid != 0);
208 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200209 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200210 free(proc);
211 return NULL;
212 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100213 return proc;
214}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100215
Petr Machata2b46cfc2012-02-18 11:17:29 +0100216struct clone_single_bp_data {
217 struct Process *old_proc;
218 struct Process *new_proc;
219 int error;
220};
221
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222static void
223clone_single_bp(void *key, void *value, void *u)
224{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100225 struct breakpoint *bp = value;
226 struct clone_single_bp_data *data = u;
227
Petr Machatad3cc9882012-04-13 21:40:23 +0200228 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100229 struct breakpoint *clone = malloc(sizeof(*clone));
230 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200231 || breakpoint_clone(clone, data->new_proc,
232 bp, data->old_proc) < 0) {
233 fail:
234 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100235 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100236 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200237 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
238 breakpoint_destroy(clone);
239 goto fail;
240 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100241}
242
243int
244process_clone(struct Process *retp, struct Process *proc, pid_t pid)
245{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200246 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100247 fail:
248 error(0, errno, "clone process %d->%d", proc->pid, pid);
249 return -1;
250 }
251
Petr Machatacf1679a2012-04-06 19:56:17 +0200252 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200253 retp->e_machine = proc->e_machine;
Petr Machatacf1679a2012-04-06 19:56:17 +0200254
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200256 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100257 return 0;
258
259 /* Clone symbols first so that we can clone and relink
260 * breakpoints. */
261 struct library *lib;
262 struct library **nlibp = &retp->libraries;
263 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
264 *nlibp = malloc(sizeof(**nlibp));
265 if (*nlibp == NULL
266 || library_clone(*nlibp, lib) < 0) {
267 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200268 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100269
270 /* Error when cloning. Unroll what was done. */
271 for (lib = retp->libraries; lib != NULL; ) {
272 struct library *next = lib->next;
273 library_destroy(lib);
274 free(lib);
275 lib = next;
276 }
277 goto fail;
278 }
279
280 nlibp = &(*nlibp)->next;
281 }
282
283 /* Now clone breakpoints. Symbol relinking is done in
284 * clone_single_bp. */
285 struct clone_single_bp_data data = {
286 .old_proc = proc,
287 .new_proc = retp,
288 .error = 0,
289 };
290 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
291
Petr Machataded6f972012-04-13 23:15:48 +0200292 /* And finally the call stack. */
293 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
294 retp->callstack_depth = proc->callstack_depth;
295
Petr Machata2b46cfc2012-02-18 11:17:29 +0100296 if (data.error < 0)
297 goto fail2;
298
Petr Machata744f2552012-04-15 04:33:18 +0200299 if (arch_process_clone(retp, proc) < 0)
300 goto fail2;
301
Petr Machata2b46cfc2012-02-18 11:17:29 +0100302 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100303}
304
Petr Machata3c516d52011-08-18 03:53:18 +0200305static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200306open_one_pid(pid_t pid)
307{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200308 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100309 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200310 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
311
Petr Machata1974dbc2011-08-19 18:58:01 +0200312 /* Get the filename first. Should the trace_pid fail, we can
313 * easily free it, untracing is more work. */
314 if ((filename = pid2name(pid)) == NULL
315 || trace_pid(pid) < 0) {
316 free(filename);
317 return -1;
318 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100319
Petr Machata75934ad2012-04-14 02:28:03 +0200320 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200321 if (proc == NULL)
322 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200323 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200324
Petr Machata1974dbc2011-08-19 18:58:01 +0200325 return 0;
326}
327
Petr Machata2b46cfc2012-02-18 11:17:29 +0100328static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200329start_one_pid(Process * proc, void * data)
330{
331 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100332 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100333}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100334
Petr Machata9a5420c2011-07-09 11:21:23 +0200335void
336open_pid(pid_t pid)
337{
338 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200339 /* If we are already tracing this guy, we should be seeing all
340 * his children via normal tracing route. */
341 if (pid2proc(pid) != NULL)
342 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200343
Petr Machata3c516d52011-08-18 03:53:18 +0200344 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200345 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200346 fprintf(stderr, "Cannot attach to pid %u: %s\n",
347 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200348 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200349 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200350 }
351
Petr Machata3c516d52011-08-18 03:53:18 +0200352 /* Now attach to all tasks that belong to that PID. There's a
353 * race between process_tasks and open_one_pid. So when we
354 * fail in open_one_pid below, we just do another round.
355 * Chances are that by then that PID will have gone away, and
356 * that's why we have seen the failure. The processes that we
357 * manage to open_one_pid are stopped, so we should eventually
358 * reach a point where process_tasks doesn't give any new
359 * processes (because there's nobody left to produce
360 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200361 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200362 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200363 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200364 pid_t *tasks;
365 size_t ntasks;
366 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200367
Petr Machata3c516d52011-08-18 03:53:18 +0200368 if (process_tasks(pid, &tasks, &ntasks) < 0) {
369 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
370 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100371 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200372 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200373
Petr Machata3c516d52011-08-18 03:53:18 +0200374 have_all = 1;
375 for (i = 0; i < ntasks; ++i)
376 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200377 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200378 have_all = 0;
379
Petr Machata9a5420c2011-07-09 11:21:23 +0200380 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200381
Petr Machata1974dbc2011-08-19 18:58:01 +0200382 if (have_all && old_ntasks == ntasks)
383 break;
384 old_ntasks = ntasks;
385 }
386
Petr Machata93d95df2012-04-17 05:16:19 +0200387 struct Process *leader = pid2proc(pid)->leader;
388
389 /* XXX Is there a way to figure out whether _start has
390 * actually already been hit? */
391 arch_dynlink_done(leader);
392
Petr Machata2f9b78e2012-04-16 21:08:54 +0200393 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200394 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200395}
396
Petr Machata2b46cfc2012-02-18 11:17:29 +0100397static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200398find_proc(Process * proc, void * data)
399{
400 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100401 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200402}
403
Juan Cespedesa8909f72009-04-28 20:02:41 +0200404Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100405pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200406 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
407}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100408
Petr Machatacebb8842011-07-09 11:14:11 +0200409static Process * list_of_processes = NULL;
410
Petr Machatacbe29c62011-09-27 02:27:58 +0200411static void
412unlist_process(Process * proc)
413{
414 Process *tmp;
415
416 if (list_of_processes == proc) {
417 list_of_processes = list_of_processes->next;
418 return;
419 }
420
421 for (tmp = list_of_processes; ; tmp = tmp->next) {
422 /* If the following assert fails, the process wasn't
423 * in the list. */
424 assert(tmp->next != NULL);
425
426 if (tmp->next == proc) {
427 tmp->next = tmp->next->next;
428 return;
429 }
430 }
431}
432
Petr Machata2b46cfc2012-02-18 11:17:29 +0100433struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100434each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100435 enum callback_status(*cb)(struct Process *proc, void *data),
436 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200437{
Petr Machata74132a42012-03-16 02:46:18 +0100438 struct Process *it = start_after == NULL ? list_of_processes
439 : start_after->next;
440
441 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200442 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100443 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100444 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200445 case CBS_FAIL:
446 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100447 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200448 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100449 case CBS_CONT:
450 break;
451 }
Petr Machatacebb8842011-07-09 11:14:11 +0200452 it = next;
453 }
454 return NULL;
455}
Petr Machata9a5420c2011-07-09 11:21:23 +0200456
457Process *
Petr Machata74132a42012-03-16 02:46:18 +0100458each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100459 enum callback_status(*cb)(struct Process *proc, void *data),
460 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200461{
Petr Machata74132a42012-03-16 02:46:18 +0100462 assert(proc != NULL);
463 struct Process *it = start_after == NULL ? proc->leader
464 : start_after->next;
465
Petr Machata9a5420c2011-07-09 11:21:23 +0200466 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100467 struct Process *leader = it->leader;
468 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200469 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100470 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100471 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200472 case CBS_FAIL:
473 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100474 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200475 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100476 case CBS_CONT:
477 break;
478 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200479 it = next;
480 }
481 }
482 return NULL;
483}
484
Petr Machata44965c72012-04-06 19:59:20 +0200485static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200486add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200487{
Petr Machata9a5420c2011-07-09 11:21:23 +0200488 Process ** leaderp = &list_of_processes;
489 if (proc->pid) {
490 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200491 if (tgid == 0)
492 /* Must have been terminated before we managed
493 * to fully attach. */
494 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200495 if (tgid == proc->pid)
496 proc->leader = proc;
497 else {
498 Process * leader = pid2proc(tgid);
499 proc->leader = leader;
500 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200501 leaderp = &leader->next;
502 }
503 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200504
505 if (!was_exec) {
506 proc->next = *leaderp;
507 *leaderp = proc;
508 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200509}
510
Petr Machatacbe29c62011-09-27 02:27:58 +0200511void
512change_process_leader(Process * proc, Process * leader)
513{
514 Process ** leaderp = &list_of_processes;
515 if (proc->leader == leader)
516 return;
517
518 assert(leader != NULL);
519 unlist_process(proc);
520 if (proc != leader)
521 leaderp = &leader->next;
522
523 proc->leader = leader;
524 proc->next = *leaderp;
525 *leaderp = proc;
526}
527
Petr Machata2b46cfc2012-02-18 11:17:29 +0100528static enum callback_status
529clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200530{
531 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
532 proc->pid, proc->leader->pid);
533 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100534 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200535}
536
Petr Machata69a03e62011-07-09 11:29:12 +0200537static enum ecb_status
538event_for_proc(Event * event, void * data)
539{
540 if (event->proc == data)
541 return ecb_deque;
542 else
543 return ecb_cont;
544}
545
546static void
547delete_events_for(Process * proc)
548{
549 Event * event;
550 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
551 free(event);
552}
553
Petr Machatacebb8842011-07-09 11:14:11 +0200554void
555remove_process(Process *proc)
556{
Petr Machatacebb8842011-07-09 11:14:11 +0200557 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
558
Petr Machata9a5420c2011-07-09 11:21:23 +0200559 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100560 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200561
Petr Machatacbe29c62011-09-27 02:27:58 +0200562 unlist_process(proc);
563 delete_events_for(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200564 process_destroy(proc);
565 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100566}
Petr Machata4007d742011-07-09 11:29:42 +0200567
568void
Petr Machata366c2f42012-02-09 19:34:36 +0100569install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200570{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200571 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200572 assert(proc->event_handler == NULL);
573 proc->event_handler = handler;
574}
575
576void
577destroy_event_handler(Process * proc)
578{
Petr Machata366c2f42012-02-09 19:34:36 +0100579 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200580 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200581 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200582 if (handler->destroy != NULL)
583 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200584 free(handler);
585 proc->event_handler = NULL;
586}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100587
588static enum callback_status
589breakpoint_for_symbol(struct library_symbol *libsym, void *data)
590{
591 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200592 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100593
Petr Machatad5e85562012-04-05 15:18:38 +0200594 /* If there is an artificial breakpoint on the same address,
595 * its libsym will be NULL, and we can smuggle our libsym
596 * there. That artificial breakpoint is there presumably for
597 * the callbacks, which we don't touch. If there is a real
598 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200599 * symbols and ignore extra symbol aliases.
600 *
601 * The other direction is more complicated and currently not
602 * supported. If a breakpoint has custom callbacks, it might
603 * be also custom-allocated, and we would really need to swap
604 * the two: delete the one now in the dictionary, swap values
605 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200606 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
607 libsym->enter_addr);
608 if (bp != NULL) {
609 assert(bp->libsym == NULL);
610 bp->libsym = libsym;
611 return CBS_CONT;
612 }
613
614 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200615 if (bp == NULL
616 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
617 fail:
618 free(bp);
619 return CBS_FAIL;
620 }
621 if (proc_add_breakpoint(proc, bp) < 0) {
622 breakpoint_destroy(bp);
623 goto fail;
624 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100625
Petr Machatafa0c5702012-04-13 18:43:40 +0200626 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200627 proc_remove_breakpoint(proc, bp);
628 breakpoint_destroy(bp);
629 goto fail;
630 }
631
Petr Machata2b46cfc2012-02-18 11:17:29 +0100632 return CBS_CONT;
633}
634
635void
636proc_add_library(struct Process *proc, struct library *lib)
637{
638 assert(lib->next == NULL);
639 lib->next = proc->libraries;
640 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200641 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
642 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100643
644 struct library_symbol *libsym = NULL;
645 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100646 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100647 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100648}
649
650int
651proc_remove_library(struct Process *proc, struct library *lib)
652{
653 struct library **libp;
654 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
655 if (*libp == lib) {
656 *libp = lib->next;
657 return 0;
658 }
659 return -1;
660}
661
662struct library *
663proc_each_library(struct Process *proc, struct library *it,
664 enum callback_status (*cb)(struct Process *proc,
665 struct library *lib, void *data),
666 void *data)
667{
668 if (it == NULL)
669 it = proc->libraries;
670
671 while (it != NULL) {
672 struct library *next = it->next;
673
674 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200675 case CBS_FAIL:
676 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100677 case CBS_STOP:
678 return it;
679 case CBS_CONT:
680 break;
681 }
682
683 it = next;
684 }
685
686 return NULL;
687}
Petr Machata52dbfb12012-03-29 16:38:26 +0200688
Petr Machataf7fee432012-04-19 17:00:53 +0200689static void
690check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200691{
Petr Machata52dbfb12012-03-29 16:38:26 +0200692 /* Only the group leader should be getting the breakpoints and
693 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200694 assert(proc->leader != NULL);
695 assert(proc->leader == proc);
696 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200697}
Petr Machata52dbfb12012-03-29 16:38:26 +0200698
Petr Machataf7fee432012-04-19 17:00:53 +0200699int
700proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
701{
Petr Machatafa0c5702012-04-13 18:43:40 +0200702 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200703 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200704 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200705
Petr Machataa2416362012-04-06 02:43:34 +0200706 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200707 * assert, but that's not necessary right now. Read the
708 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200709 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200710
Petr Machatafa0c5702012-04-13 18:43:40 +0200711 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200712 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
713 breakpoint_name(bp), bp->addr);
714 return -1;
715 }
716
Petr Machata52dbfb12012-03-29 16:38:26 +0200717 return 0;
718}
719
Petr Machataf7fee432012-04-19 17:00:53 +0200720void
Petr Machata52dbfb12012-03-29 16:38:26 +0200721proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
722{
Petr Machataf7fee432012-04-19 17:00:53 +0200723 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
724 proc->pid, breakpoint_name(bp), bp->addr);
725 check_leader(proc);
726 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
727 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200728}
Petr Machatad3cc9882012-04-13 21:40:23 +0200729
730/* Dict doesn't support iteration restarts, so here's this contraption
731 * for now. XXX add restarts to dict. */
732struct each_breakpoint_data
733{
734 void *start;
735 void *end;
736 struct Process *proc;
737 enum callback_status (*cb)(struct Process *proc,
738 struct breakpoint *bp,
739 void *data);
740 void *cb_data;
741};
742
743static void
744each_breakpoint_cb(void *key, void *value, void *d)
745{
746 struct each_breakpoint_data *data = d;
747 if (data->end != NULL)
748 return;
749 if (data->start == key)
750 data->start = NULL;
751
752 if (data->start == NULL) {
753 switch (data->cb(data->proc, value, data->cb_data)) {
754 case CBS_FAIL:
755 /* XXX handle me */
756 case CBS_STOP:
757 data->end = key;
758 case CBS_CONT:
759 return;
760 }
761 }
762}
763
764void *
765proc_each_breakpoint(struct Process *proc, void *start,
766 enum callback_status (*cb)(struct Process *proc,
767 struct breakpoint *bp,
768 void *data), void *data)
769{
770 struct each_breakpoint_data dd = {
771 .start = start,
772 .proc = proc,
773 .cb = cb,
774 .cb_data = data,
775 };
776 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
777 return dd.end;
778}