blob: 7a8b0b53b0f668fd30e95a6752053e8607441ed7 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2010 Joe Damato
5 * Copyright (C) 1998,2009 Juan Cespedes
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
Joe Damatoab3b72c2010-10-31 00:21:53 -070023#include "config.h"
24
Petr Machataba1664b2012-04-28 14:59:05 +020025#include <sys/types.h>
26#include <assert.h>
27#include <errno.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
Joe Damatoab3b72c2010-10-31 00:21:53 -070032#if defined(HAVE_LIBUNWIND)
33#include <libunwind.h>
34#include <libunwind-ptrace.h>
35#endif /* defined(HAVE_LIBUNWIND) */
36
Petr Machata64262602012-01-07 03:41:36 +010037#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020038#include "breakpoint.h"
39#include "debug.h"
40#include "fetch.h"
41#include "proc.h"
42#include "value_dict.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010043
Petr Machata744f2552012-04-15 04:33:18 +020044#ifndef ARCH_HAVE_PROCESS_DATA
45int
46arch_process_init(struct Process *proc)
47{
48 return 0;
49}
50
51void
52arch_process_destroy(struct Process *proc)
53{
54}
55
56int
57arch_process_clone(struct Process *retp, struct Process *proc)
58{
59 return 0;
60}
61
62int
63arch_process_exec(struct Process *proc)
64{
65 return 0;
66}
67#endif
68
Petr Machata93d95df2012-04-17 05:16:19 +020069#ifndef ARCH_HAVE_DYNLINK_DONE
70void
71arch_dynlink_done(struct Process *proc)
72{
73}
74#endif
75
Petr Machata3d0c91c2012-04-14 02:37:38 +020076static void add_process(struct Process *proc, int was_exec);
Petr Machata61686c22012-05-03 18:39:49 +020077static void unlist_process(struct Process *proc);
Petr Machata44965c72012-04-06 19:59:20 +020078
Petr Machatae677c7e2012-10-26 22:23:43 +020079static void
80destroy_unwind(struct Process *proc)
81{
82#if defined(HAVE_LIBUNWIND)
83 _UPT_destroy(proc->unwind_priv);
84 unw_destroy_addr_space(proc->unwind_as);
85#endif /* defined(HAVE_LIBUNWIND) */
86}
87
Petr Machata2b46cfc2012-02-18 11:17:29 +010088static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020089process_bare_init(struct Process *proc, const char *filename,
90 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010091{
Petr Machata3d0c91c2012-04-14 02:37:38 +020092 if (!was_exec) {
93 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020094
Petr Machata3d0c91c2012-04-14 02:37:38 +020095 proc->filename = strdup(filename);
96 if (proc->filename == NULL) {
97 fail:
98 free(proc->filename);
99 if (proc->breakpoints != NULL)
100 dict_clear(proc->breakpoints);
101 return -1;
102 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100103 }
104
105 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +0200106 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200107 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100108 if (proc->leader == NULL)
109 goto fail;
110
111 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +0200112 proc->breakpoints = dict_init(target_address_hash,
113 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100114 if (proc->breakpoints == NULL)
115 goto fail;
116 } else {
117 proc->breakpoints = NULL;
118 }
119
Joe Damatoab3b72c2010-10-31 00:21:53 -0700120#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +0200121 proc->unwind_priv = _UPT_create(pid);
122 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700123#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -0700124
Petr Machata2b46cfc2012-02-18 11:17:29 +0100125 return 0;
126}
127
128static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200129process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100130{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100131 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200132 if (!was_exec) {
133 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200134 unlist_process(proc);
Petr Machatae677c7e2012-10-26 22:23:43 +0200135 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200136 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100137}
138
Petr Machata3d0c91c2012-04-14 02:37:38 +0200139static int
140process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100141{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200142 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200143 fprintf(stderr, "failed to init breakpoints %d\n",
144 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200145 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200146 }
147
Petr Machata2b46cfc2012-02-18 11:17:29 +0100148 return 0;
149}
150
Petr Machata3d0c91c2012-04-14 02:37:38 +0200151int
152process_init(struct Process *proc, const char *filename, pid_t pid)
153{
154 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200155 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200156 fprintf(stderr, "failed to initialize process %d: %s\n",
157 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200158 return -1;
159 }
160
Petr Machata744f2552012-04-15 04:33:18 +0200161 if (arch_process_init(proc) < 0) {
162 process_bare_destroy(proc, 0);
163 goto fail;
164 }
165
Petr Machata218c5ff2012-04-15 04:22:39 +0200166 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200167 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200168 if (process_init_main(proc) < 0) {
169 process_bare_destroy(proc, 0);
170 goto fail;
171 }
172 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200173}
174
Petr Machata8ead1cd2012-04-24 18:13:09 +0200175static enum callback_status
176destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200177{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178 breakpoint_destroy(bp);
179 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200180 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200181}
182
Petr Machatae0e89ed2012-10-26 22:25:33 +0200183// XXX see comment in handle_event.c
184void callstack_pop(struct Process *proc);
185
Petr Machata3d0c91c2012-04-14 02:37:38 +0200186static void
Petr Machatae0e89ed2012-10-26 22:25:33 +0200187private_process_destroy(struct Process *proc, int was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200188{
Petr Machatae0e89ed2012-10-26 22:25:33 +0200189 /* Pop remaining stack elements. */
190 while (proc->callstack_depth > 0) {
191 /* When this is called just before a process is
192 * destroyed, the breakpoints should either have been
193 * retracted by now, or were killed by exec. In any
194 * case, it's safe to pretend that there are no
195 * breakpoints associated with the stack elements, so
196 * that stack_pop doesn't attempt to destroy them. */
197 size_t i = proc->callstack_depth - 1;
198 if (!proc->callstack[i].is_syscall)
199 proc->callstack[i].return_addr = 0;
200
201 callstack_pop(proc);
202 }
203
204 if (!was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200205 free(proc->filename);
206
Petr Machata8ead1cd2012-04-24 18:13:09 +0200207 /* Libraries and symbols. This is only relevant in
208 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200209 struct library *lib;
210 for (lib = proc->libraries; lib != NULL; ) {
211 struct library *next = lib->next;
212 library_destroy(lib);
213 free(lib);
214 lib = next;
215 }
216 proc->libraries = NULL;
217
218 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200219 if (proc->breakpoints != NULL) {
220 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
221 dict_clear(proc->breakpoints);
222 proc->breakpoints = NULL;
223 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200224
225 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200226}
227
228void
229process_destroy(struct Process *proc)
230{
Petr Machata744f2552012-04-15 04:33:18 +0200231 arch_process_destroy(proc);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200232 private_process_destroy(proc, 0);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200233}
234
235int
236process_exec(struct Process *proc)
237{
Petr Machata3cc0cd12012-10-26 22:30:51 +0200238 /* Call exec handler first, before we destroy the main
239 * state. */
Petr Machata744f2552012-04-15 04:33:18 +0200240 if (arch_process_exec(proc) < 0)
241 return -1;
242
Petr Machata3d0c91c2012-04-14 02:37:38 +0200243 private_process_destroy(proc, 1);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200244
Petr Machata3d0c91c2012-04-14 02:37:38 +0200245 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
246 return -1;
247 if (process_init_main(proc) < 0) {
248 process_bare_destroy(proc, 1);
249 return -1;
250 }
251 return 0;
252}
253
Petr Machata2b46cfc2012-02-18 11:17:29 +0100254struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200255open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100257 assert(pid != 0);
258 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200259 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200260 free(proc);
261 return NULL;
262 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263 return proc;
264}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100265
Petr Machata2b46cfc2012-02-18 11:17:29 +0100266struct clone_single_bp_data {
267 struct Process *old_proc;
268 struct Process *new_proc;
269 int error;
270};
271
Petr Machata2b46cfc2012-02-18 11:17:29 +0100272static void
273clone_single_bp(void *key, void *value, void *u)
274{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100275 struct breakpoint *bp = value;
276 struct clone_single_bp_data *data = u;
277
Petr Machatad3cc9882012-04-13 21:40:23 +0200278 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100279 struct breakpoint *clone = malloc(sizeof(*clone));
280 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200281 || breakpoint_clone(clone, data->new_proc,
282 bp, data->old_proc) < 0) {
283 fail:
284 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100285 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100286 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200287 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
288 breakpoint_destroy(clone);
289 goto fail;
290 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100291}
292
293int
294process_clone(struct Process *retp, struct Process *proc, pid_t pid)
295{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200296 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200297 fail1:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200298 fprintf(stderr, "failed to clone process %d->%d : %s\n",
299 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100300 return -1;
301 }
302
Petr Machatacf1679a2012-04-06 19:56:17 +0200303 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200304 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400305 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200306
Petr Machata2b46cfc2012-02-18 11:17:29 +0100307 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200308 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100309 return 0;
310
311 /* Clone symbols first so that we can clone and relink
312 * breakpoints. */
313 struct library *lib;
314 struct library **nlibp = &retp->libraries;
315 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
316 *nlibp = malloc(sizeof(**nlibp));
317 if (*nlibp == NULL
318 || library_clone(*nlibp, lib) < 0) {
319 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200320 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100321
322 /* Error when cloning. Unroll what was done. */
323 for (lib = retp->libraries; lib != NULL; ) {
324 struct library *next = lib->next;
325 library_destroy(lib);
326 free(lib);
327 lib = next;
328 }
Petr Machataba1664b2012-04-28 14:59:05 +0200329 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330 }
331
332 nlibp = &(*nlibp)->next;
333 }
334
335 /* Now clone breakpoints. Symbol relinking is done in
336 * clone_single_bp. */
337 struct clone_single_bp_data data = {
338 .old_proc = proc,
339 .new_proc = retp,
340 .error = 0,
341 };
342 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100343 if (data.error < 0)
344 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100345
Petr Machataded6f972012-04-13 23:15:48 +0200346 /* And finally the call stack. */
347 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
348 retp->callstack_depth = proc->callstack_depth;
349
Petr Machata94078ec2012-01-05 18:07:02 +0100350 size_t i;
351 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100352 struct fetch_context *ctx = retp->callstack[i].fetch_context;
353 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200354 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100355 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200356 size_t j;
357 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100358 for (j = 0; j < i; ++j) {
359 nctx = retp->callstack[i].fetch_context;
360 fetch_arg_done(nctx);
361 retp->callstack[i].fetch_context = NULL;
362 }
363 goto fail2;
364 }
365 retp->callstack[i].fetch_context = nctx;
366 }
367
Petr Machata94078ec2012-01-05 18:07:02 +0100368 struct value_dict *args = retp->callstack[i].arguments;
369 if (args != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100370 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100371 if (nargs == NULL
372 || val_dict_clone(nargs, args) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200373 size_t j;
374 fail4:
Petr Machata94078ec2012-01-05 18:07:02 +0100375 for (j = 0; j < i; ++j) {
Petr Machataba1664b2012-04-28 14:59:05 +0200376 nargs = retp->callstack[i].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100377 val_dict_destroy(nargs);
378 free(nargs);
Petr Machataba1664b2012-04-28 14:59:05 +0200379 retp->callstack[i].arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100380 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100381
382 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200383 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100384 * fetch_context. */
385 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200386 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100387 }
388 retp->callstack[i].arguments = nargs;
389 }
390 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100391
Petr Machata744f2552012-04-15 04:33:18 +0200392 if (arch_process_clone(retp, proc) < 0)
Petr Machataba1664b2012-04-28 14:59:05 +0200393 goto fail4;
Petr Machata744f2552012-04-15 04:33:18 +0200394
Petr Machata2b46cfc2012-02-18 11:17:29 +0100395 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100396}
397
Petr Machata3c516d52011-08-18 03:53:18 +0200398static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200399open_one_pid(pid_t pid)
400{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200401 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100402 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200403 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
404
Petr Machata1974dbc2011-08-19 18:58:01 +0200405 /* Get the filename first. Should the trace_pid fail, we can
406 * easily free it, untracing is more work. */
407 if ((filename = pid2name(pid)) == NULL
408 || trace_pid(pid) < 0) {
409 free(filename);
410 return -1;
411 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100412
Petr Machata75934ad2012-04-14 02:28:03 +0200413 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200414 if (proc == NULL)
415 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200416 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200417
Petr Machata1974dbc2011-08-19 18:58:01 +0200418 return 0;
419}
420
Petr Machata2b46cfc2012-02-18 11:17:29 +0100421static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200422start_one_pid(Process * proc, void * data)
423{
424 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100425 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100426}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100427
Petr Machata9a5420c2011-07-09 11:21:23 +0200428void
429open_pid(pid_t pid)
430{
431 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200432 /* If we are already tracing this guy, we should be seeing all
433 * his children via normal tracing route. */
434 if (pid2proc(pid) != NULL)
435 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200436
Petr Machata3c516d52011-08-18 03:53:18 +0200437 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200438 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200439 fprintf(stderr, "Cannot attach to pid %u: %s\n",
440 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200441 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200442 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200443 }
444
Petr Machata3c516d52011-08-18 03:53:18 +0200445 /* Now attach to all tasks that belong to that PID. There's a
446 * race between process_tasks and open_one_pid. So when we
447 * fail in open_one_pid below, we just do another round.
448 * Chances are that by then that PID will have gone away, and
449 * that's why we have seen the failure. The processes that we
450 * manage to open_one_pid are stopped, so we should eventually
451 * reach a point where process_tasks doesn't give any new
452 * processes (because there's nobody left to produce
453 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200454 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200455 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200456 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200457 pid_t *tasks;
458 size_t ntasks;
459 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200460
Petr Machata3c516d52011-08-18 03:53:18 +0200461 if (process_tasks(pid, &tasks, &ntasks) < 0) {
462 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
463 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100464 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200465 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200466
Petr Machata3c516d52011-08-18 03:53:18 +0200467 have_all = 1;
468 for (i = 0; i < ntasks; ++i)
469 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200470 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200471 have_all = 0;
472
Petr Machata9a5420c2011-07-09 11:21:23 +0200473 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200474
Petr Machata1974dbc2011-08-19 18:58:01 +0200475 if (have_all && old_ntasks == ntasks)
476 break;
477 old_ntasks = ntasks;
478 }
479
Petr Machata93d95df2012-04-17 05:16:19 +0200480 struct Process *leader = pid2proc(pid)->leader;
481
482 /* XXX Is there a way to figure out whether _start has
483 * actually already been hit? */
484 arch_dynlink_done(leader);
485
Petr Machata2f9b78e2012-04-16 21:08:54 +0200486 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200487 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200488}
489
Petr Machata2b46cfc2012-02-18 11:17:29 +0100490static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200491find_proc(Process * proc, void * data)
492{
493 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100494 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200495}
496
Juan Cespedesa8909f72009-04-28 20:02:41 +0200497Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100498pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200499 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
500}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100501
Petr Machatacebb8842011-07-09 11:14:11 +0200502static Process * list_of_processes = NULL;
503
Petr Machatacbe29c62011-09-27 02:27:58 +0200504static void
505unlist_process(Process * proc)
506{
507 Process *tmp;
508
509 if (list_of_processes == proc) {
510 list_of_processes = list_of_processes->next;
511 return;
512 }
513
514 for (tmp = list_of_processes; ; tmp = tmp->next) {
515 /* If the following assert fails, the process wasn't
516 * in the list. */
517 assert(tmp->next != NULL);
518
519 if (tmp->next == proc) {
520 tmp->next = tmp->next->next;
521 return;
522 }
523 }
524}
525
Petr Machata2b46cfc2012-02-18 11:17:29 +0100526struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100527each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100528 enum callback_status(*cb)(struct Process *proc, void *data),
529 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200530{
Petr Machata74132a42012-03-16 02:46:18 +0100531 struct Process *it = start_after == NULL ? list_of_processes
532 : start_after->next;
533
534 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200535 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100536 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100537 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200538 case CBS_FAIL:
539 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100540 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200541 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100542 case CBS_CONT:
543 break;
544 }
Petr Machatacebb8842011-07-09 11:14:11 +0200545 it = next;
546 }
547 return NULL;
548}
Petr Machata9a5420c2011-07-09 11:21:23 +0200549
550Process *
Petr Machata74132a42012-03-16 02:46:18 +0100551each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100552 enum callback_status(*cb)(struct Process *proc, void *data),
553 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200554{
Petr Machata74132a42012-03-16 02:46:18 +0100555 assert(proc != NULL);
556 struct Process *it = start_after == NULL ? proc->leader
557 : start_after->next;
558
Petr Machata9a5420c2011-07-09 11:21:23 +0200559 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100560 struct Process *leader = it->leader;
561 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200562 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100563 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100564 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200565 case CBS_FAIL:
566 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100567 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200568 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100569 case CBS_CONT:
570 break;
571 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200572 it = next;
573 }
574 }
575 return NULL;
576}
577
Petr Machata44965c72012-04-06 19:59:20 +0200578static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200579add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200580{
Petr Machata9a5420c2011-07-09 11:21:23 +0200581 Process ** leaderp = &list_of_processes;
582 if (proc->pid) {
583 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200584 if (tgid == 0)
585 /* Must have been terminated before we managed
586 * to fully attach. */
587 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200588 if (tgid == proc->pid)
589 proc->leader = proc;
590 else {
591 Process * leader = pid2proc(tgid);
592 proc->leader = leader;
593 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200594 leaderp = &leader->next;
595 }
596 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200597
598 if (!was_exec) {
599 proc->next = *leaderp;
600 *leaderp = proc;
601 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200602}
603
Petr Machatacbe29c62011-09-27 02:27:58 +0200604void
605change_process_leader(Process * proc, Process * leader)
606{
607 Process ** leaderp = &list_of_processes;
608 if (proc->leader == leader)
609 return;
610
611 assert(leader != NULL);
612 unlist_process(proc);
613 if (proc != leader)
614 leaderp = &leader->next;
615
616 proc->leader = leader;
617 proc->next = *leaderp;
618 *leaderp = proc;
619}
620
Petr Machata2b46cfc2012-02-18 11:17:29 +0100621static enum callback_status
622clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200623{
624 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
625 proc->pid, proc->leader->pid);
626 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100627 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200628}
629
630void
631remove_process(Process *proc)
632{
Petr Machatacebb8842011-07-09 11:14:11 +0200633 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
634
Petr Machata9a5420c2011-07-09 11:21:23 +0200635 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100636 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200637
Petr Machatacbe29c62011-09-27 02:27:58 +0200638 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100639 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200640 process_destroy(proc);
641 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100642}
Petr Machata4007d742011-07-09 11:29:42 +0200643
644void
Petr Machata366c2f42012-02-09 19:34:36 +0100645install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200646{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200647 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200648 assert(proc->event_handler == NULL);
649 proc->event_handler = handler;
650}
651
652void
653destroy_event_handler(Process * proc)
654{
Petr Machata366c2f42012-02-09 19:34:36 +0100655 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200656 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200657 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200658 if (handler->destroy != NULL)
659 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200660 free(handler);
661 proc->event_handler = NULL;
662}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100663
Petr Machataef2fd272012-09-28 00:43:01 +0200664static int
665breakpoint_for_symbol(struct library_symbol *libsym, struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100666{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200667 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200668 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100669
Petr Machataef2fd272012-09-28 00:43:01 +0200670 /* Don't enable latent or delayed symbols. */
671 if (libsym->latent || libsym->delayed)
672 return 0;
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200673
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200674 bp_addr = sym2addr(proc, libsym);
675
Petr Machatad5e85562012-04-05 15:18:38 +0200676 /* If there is an artificial breakpoint on the same address,
677 * its libsym will be NULL, and we can smuggle our libsym
678 * there. That artificial breakpoint is there presumably for
679 * the callbacks, which we don't touch. If there is a real
680 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200681 * symbols and ignore extra symbol aliases.
682 *
683 * The other direction is more complicated and currently not
684 * supported. If a breakpoint has custom callbacks, it might
685 * be also custom-allocated, and we would really need to swap
686 * the two: delete the one now in the dictionary, swap values
687 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200688 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200689 bp_addr);
Petr Machatad5e85562012-04-05 15:18:38 +0200690 if (bp != NULL) {
691 assert(bp->libsym == NULL);
692 bp->libsym = libsym;
Petr Machataef2fd272012-09-28 00:43:01 +0200693 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200694 }
695
696 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200697 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200698 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200699 fail:
700 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200701 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200702 }
703 if (proc_add_breakpoint(proc, bp) < 0) {
704 breakpoint_destroy(bp);
705 goto fail;
706 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100707
Petr Machatafa0c5702012-04-13 18:43:40 +0200708 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200709 proc_remove_breakpoint(proc, bp);
710 breakpoint_destroy(bp);
711 goto fail;
712 }
713
Petr Machataef2fd272012-09-28 00:43:01 +0200714 return 0;
715}
716
717static enum callback_status
718cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
719{
720 return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
721}
722
723static int
724proc_activate_latent_symbol(struct Process *proc,
725 struct library_symbol *libsym)
726{
727 assert(libsym->latent);
728 libsym->latent = 0;
729 return breakpoint_for_symbol(libsym, proc);
730}
731
732int
733proc_activate_delayed_symbol(struct Process *proc,
734 struct library_symbol *libsym)
735{
736 assert(libsym->delayed);
737 libsym->delayed = 0;
738 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100739}
740
Petr Machataa1f76832012-09-28 00:08:00 +0200741static enum callback_status
742activate_latent_in(struct Process *proc, struct library *lib, void *data)
743{
744 struct library_exported_name *exported;
745 for (exported = data; exported != NULL; exported = exported->next) {
746 struct library_symbol *libsym = NULL;
747 while ((libsym = library_each_symbol(lib, libsym,
748 library_symbol_named_cb,
749 (void *)exported->name))
750 != NULL)
751 if (libsym->latent
752 && proc_activate_latent_symbol(proc, libsym) < 0)
753 return CBS_FAIL;
754 }
755 return CBS_CONT;
756}
757
Petr Machata2b46cfc2012-02-18 11:17:29 +0100758void
759proc_add_library(struct Process *proc, struct library *lib)
760{
761 assert(lib->next == NULL);
762 lib->next = proc->libraries;
763 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200764 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
765 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100766
Petr Machataef2fd272012-09-28 00:43:01 +0200767 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100768 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200769 while ((libsym = library_each_symbol(lib, libsym,
770 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100771 proc)) != NULL)
Petr Machataef2fd272012-09-28 00:43:01 +0200772 fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s.",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200773 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200774
775 /* Look through export list of the new library and compare it
776 * with latent symbols of all libraries (including this
777 * library itself). */
778 struct library *lib2 = NULL;
779 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
780 lib->exported_names)) != NULL)
781 fprintf(stderr,
782 "Couldn't activate latent symbols for %s in %d: %s.",
783 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100784}
785
786int
787proc_remove_library(struct Process *proc, struct library *lib)
788{
789 struct library **libp;
790 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
791 if (*libp == lib) {
792 *libp = lib->next;
793 return 0;
794 }
795 return -1;
796}
797
798struct library *
799proc_each_library(struct Process *proc, struct library *it,
800 enum callback_status (*cb)(struct Process *proc,
801 struct library *lib, void *data),
802 void *data)
803{
804 if (it == NULL)
805 it = proc->libraries;
806
807 while (it != NULL) {
808 struct library *next = it->next;
809
810 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200811 case CBS_FAIL:
812 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100813 case CBS_STOP:
814 return it;
815 case CBS_CONT:
816 break;
817 }
818
819 it = next;
820 }
821
822 return NULL;
823}
Petr Machata52dbfb12012-03-29 16:38:26 +0200824
Petr Machataf7fee432012-04-19 17:00:53 +0200825static void
826check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200827{
Petr Machata52dbfb12012-03-29 16:38:26 +0200828 /* Only the group leader should be getting the breakpoints and
829 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200830 assert(proc->leader != NULL);
831 assert(proc->leader == proc);
832 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200833}
Petr Machata52dbfb12012-03-29 16:38:26 +0200834
Petr Machataf7fee432012-04-19 17:00:53 +0200835int
836proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
837{
Petr Machatafa0c5702012-04-13 18:43:40 +0200838 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200839 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200840 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200841
Petr Machataa2416362012-04-06 02:43:34 +0200842 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200843 * assert, but that's not necessary right now. Read the
844 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200845 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200846
Petr Machatafa0c5702012-04-13 18:43:40 +0200847 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200848 fprintf(stderr,
849 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
850 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200851 return -1;
852 }
853
Petr Machata52dbfb12012-03-29 16:38:26 +0200854 return 0;
855}
856
Petr Machataf7fee432012-04-19 17:00:53 +0200857void
Petr Machata52dbfb12012-03-29 16:38:26 +0200858proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
859{
Petr Machataf7fee432012-04-19 17:00:53 +0200860 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
861 proc->pid, breakpoint_name(bp), bp->addr);
862 check_leader(proc);
863 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
864 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200865}
Petr Machatad3cc9882012-04-13 21:40:23 +0200866
867/* Dict doesn't support iteration restarts, so here's this contraption
868 * for now. XXX add restarts to dict. */
869struct each_breakpoint_data
870{
871 void *start;
872 void *end;
873 struct Process *proc;
874 enum callback_status (*cb)(struct Process *proc,
875 struct breakpoint *bp,
876 void *data);
877 void *cb_data;
878};
879
880static void
881each_breakpoint_cb(void *key, void *value, void *d)
882{
883 struct each_breakpoint_data *data = d;
884 if (data->end != NULL)
885 return;
886 if (data->start == key)
887 data->start = NULL;
888
889 if (data->start == NULL) {
890 switch (data->cb(data->proc, value, data->cb_data)) {
891 case CBS_FAIL:
892 /* XXX handle me */
893 case CBS_STOP:
894 data->end = key;
895 case CBS_CONT:
896 return;
897 }
898 }
899}
900
901void *
902proc_each_breakpoint(struct Process *proc, void *start,
903 enum callback_status (*cb)(struct Process *proc,
904 struct breakpoint *bp,
905 void *data), void *data)
906{
907 struct each_breakpoint_data dd = {
908 .start = start,
909 .proc = proc,
910 .cb = cb,
911 .cb_data = data,
912 };
913 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
914 return dd.end;
915}