blob: 9fefc0e73c32a6b6eefd1203f74fbd988eb9abbc [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{
231 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200232 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200233}
234
235int
236process_exec(struct Process *proc)
237{
Petr Machata744f2552012-04-15 04:33:18 +0200238 /* Call exec first, before we destroy the main state. */
239 if (arch_process_exec(proc) < 0)
240 return -1;
241
Petr Machata3d0c91c2012-04-14 02:37:38 +0200242 private_process_destroy(proc, 1);
243 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
244 return -1;
245 if (process_init_main(proc) < 0) {
246 process_bare_destroy(proc, 1);
247 return -1;
248 }
249 return 0;
250}
251
Petr Machata2b46cfc2012-02-18 11:17:29 +0100252struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200253open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100254{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255 assert(pid != 0);
256 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200257 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200258 free(proc);
259 return NULL;
260 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100261 return proc;
262}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100263
Petr Machata2b46cfc2012-02-18 11:17:29 +0100264struct clone_single_bp_data {
265 struct Process *old_proc;
266 struct Process *new_proc;
267 int error;
268};
269
Petr Machata2b46cfc2012-02-18 11:17:29 +0100270static void
271clone_single_bp(void *key, void *value, void *u)
272{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100273 struct breakpoint *bp = value;
274 struct clone_single_bp_data *data = u;
275
Petr Machatad3cc9882012-04-13 21:40:23 +0200276 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100277 struct breakpoint *clone = malloc(sizeof(*clone));
278 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200279 || breakpoint_clone(clone, data->new_proc,
280 bp, data->old_proc) < 0) {
281 fail:
282 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100283 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100284 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200285 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
286 breakpoint_destroy(clone);
287 goto fail;
288 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289}
290
291int
292process_clone(struct Process *retp, struct Process *proc, pid_t pid)
293{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200294 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200295 fail1:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200296 fprintf(stderr, "failed to clone process %d->%d : %s\n",
297 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100298 return -1;
299 }
300
Petr Machatacf1679a2012-04-06 19:56:17 +0200301 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200302 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400303 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200304
Petr Machata2b46cfc2012-02-18 11:17:29 +0100305 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200306 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100307 return 0;
308
309 /* Clone symbols first so that we can clone and relink
310 * breakpoints. */
311 struct library *lib;
312 struct library **nlibp = &retp->libraries;
313 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
314 *nlibp = malloc(sizeof(**nlibp));
315 if (*nlibp == NULL
316 || library_clone(*nlibp, lib) < 0) {
317 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200318 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100319
320 /* Error when cloning. Unroll what was done. */
321 for (lib = retp->libraries; lib != NULL; ) {
322 struct library *next = lib->next;
323 library_destroy(lib);
324 free(lib);
325 lib = next;
326 }
Petr Machataba1664b2012-04-28 14:59:05 +0200327 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100328 }
329
330 nlibp = &(*nlibp)->next;
331 }
332
333 /* Now clone breakpoints. Symbol relinking is done in
334 * clone_single_bp. */
335 struct clone_single_bp_data data = {
336 .old_proc = proc,
337 .new_proc = retp,
338 .error = 0,
339 };
340 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100341 if (data.error < 0)
342 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100343
Petr Machataded6f972012-04-13 23:15:48 +0200344 /* And finally the call stack. */
345 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
346 retp->callstack_depth = proc->callstack_depth;
347
Petr Machata94078ec2012-01-05 18:07:02 +0100348 size_t i;
349 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100350 struct fetch_context *ctx = retp->callstack[i].fetch_context;
351 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200352 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100353 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200354 size_t j;
355 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100356 for (j = 0; j < i; ++j) {
357 nctx = retp->callstack[i].fetch_context;
358 fetch_arg_done(nctx);
359 retp->callstack[i].fetch_context = NULL;
360 }
361 goto fail2;
362 }
363 retp->callstack[i].fetch_context = nctx;
364 }
365
Petr Machata94078ec2012-01-05 18:07:02 +0100366 struct value_dict *args = retp->callstack[i].arguments;
367 if (args != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100368 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100369 if (nargs == NULL
370 || val_dict_clone(nargs, args) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200371 size_t j;
372 fail4:
Petr Machata94078ec2012-01-05 18:07:02 +0100373 for (j = 0; j < i; ++j) {
Petr Machataba1664b2012-04-28 14:59:05 +0200374 nargs = retp->callstack[i].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100375 val_dict_destroy(nargs);
376 free(nargs);
Petr Machataba1664b2012-04-28 14:59:05 +0200377 retp->callstack[i].arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100378 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100379
380 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200381 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100382 * fetch_context. */
383 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200384 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100385 }
386 retp->callstack[i].arguments = nargs;
387 }
388 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100389
Petr Machata744f2552012-04-15 04:33:18 +0200390 if (arch_process_clone(retp, proc) < 0)
Petr Machataba1664b2012-04-28 14:59:05 +0200391 goto fail4;
Petr Machata744f2552012-04-15 04:33:18 +0200392
Petr Machata2b46cfc2012-02-18 11:17:29 +0100393 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100394}
395
Petr Machata3c516d52011-08-18 03:53:18 +0200396static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200397open_one_pid(pid_t pid)
398{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200399 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100400 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200401 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
402
Petr Machata1974dbc2011-08-19 18:58:01 +0200403 /* Get the filename first. Should the trace_pid fail, we can
404 * easily free it, untracing is more work. */
405 if ((filename = pid2name(pid)) == NULL
406 || trace_pid(pid) < 0) {
407 free(filename);
408 return -1;
409 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100410
Petr Machata75934ad2012-04-14 02:28:03 +0200411 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200412 if (proc == NULL)
413 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200414 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200415
Petr Machata1974dbc2011-08-19 18:58:01 +0200416 return 0;
417}
418
Petr Machata2b46cfc2012-02-18 11:17:29 +0100419static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200420start_one_pid(Process * proc, void * data)
421{
422 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100423 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100424}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100425
Petr Machata9a5420c2011-07-09 11:21:23 +0200426void
427open_pid(pid_t pid)
428{
429 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200430 /* If we are already tracing this guy, we should be seeing all
431 * his children via normal tracing route. */
432 if (pid2proc(pid) != NULL)
433 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200434
Petr Machata3c516d52011-08-18 03:53:18 +0200435 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200436 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200437 fprintf(stderr, "Cannot attach to pid %u: %s\n",
438 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200439 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200440 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200441 }
442
Petr Machata3c516d52011-08-18 03:53:18 +0200443 /* Now attach to all tasks that belong to that PID. There's a
444 * race between process_tasks and open_one_pid. So when we
445 * fail in open_one_pid below, we just do another round.
446 * Chances are that by then that PID will have gone away, and
447 * that's why we have seen the failure. The processes that we
448 * manage to open_one_pid are stopped, so we should eventually
449 * reach a point where process_tasks doesn't give any new
450 * processes (because there's nobody left to produce
451 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200452 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200453 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200454 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200455 pid_t *tasks;
456 size_t ntasks;
457 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200458
Petr Machata3c516d52011-08-18 03:53:18 +0200459 if (process_tasks(pid, &tasks, &ntasks) < 0) {
460 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
461 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100462 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200463 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200464
Petr Machata3c516d52011-08-18 03:53:18 +0200465 have_all = 1;
466 for (i = 0; i < ntasks; ++i)
467 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200468 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200469 have_all = 0;
470
Petr Machata9a5420c2011-07-09 11:21:23 +0200471 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200472
Petr Machata1974dbc2011-08-19 18:58:01 +0200473 if (have_all && old_ntasks == ntasks)
474 break;
475 old_ntasks = ntasks;
476 }
477
Petr Machata93d95df2012-04-17 05:16:19 +0200478 struct Process *leader = pid2proc(pid)->leader;
479
480 /* XXX Is there a way to figure out whether _start has
481 * actually already been hit? */
482 arch_dynlink_done(leader);
483
Petr Machata2f9b78e2012-04-16 21:08:54 +0200484 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200485 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200486}
487
Petr Machata2b46cfc2012-02-18 11:17:29 +0100488static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200489find_proc(Process * proc, void * data)
490{
491 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100492 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200493}
494
Juan Cespedesa8909f72009-04-28 20:02:41 +0200495Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100496pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200497 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
498}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100499
Petr Machatacebb8842011-07-09 11:14:11 +0200500static Process * list_of_processes = NULL;
501
Petr Machatacbe29c62011-09-27 02:27:58 +0200502static void
503unlist_process(Process * proc)
504{
505 Process *tmp;
506
507 if (list_of_processes == proc) {
508 list_of_processes = list_of_processes->next;
509 return;
510 }
511
512 for (tmp = list_of_processes; ; tmp = tmp->next) {
513 /* If the following assert fails, the process wasn't
514 * in the list. */
515 assert(tmp->next != NULL);
516
517 if (tmp->next == proc) {
518 tmp->next = tmp->next->next;
519 return;
520 }
521 }
522}
523
Petr Machata2b46cfc2012-02-18 11:17:29 +0100524struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100525each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100526 enum callback_status(*cb)(struct Process *proc, void *data),
527 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200528{
Petr Machata74132a42012-03-16 02:46:18 +0100529 struct Process *it = start_after == NULL ? list_of_processes
530 : start_after->next;
531
532 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200533 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100534 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100535 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200536 case CBS_FAIL:
537 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100538 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200539 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100540 case CBS_CONT:
541 break;
542 }
Petr Machatacebb8842011-07-09 11:14:11 +0200543 it = next;
544 }
545 return NULL;
546}
Petr Machata9a5420c2011-07-09 11:21:23 +0200547
548Process *
Petr Machata74132a42012-03-16 02:46:18 +0100549each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100550 enum callback_status(*cb)(struct Process *proc, void *data),
551 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200552{
Petr Machata74132a42012-03-16 02:46:18 +0100553 assert(proc != NULL);
554 struct Process *it = start_after == NULL ? proc->leader
555 : start_after->next;
556
Petr Machata9a5420c2011-07-09 11:21:23 +0200557 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100558 struct Process *leader = it->leader;
559 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200560 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100561 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100562 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200563 case CBS_FAIL:
564 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100565 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200566 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100567 case CBS_CONT:
568 break;
569 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200570 it = next;
571 }
572 }
573 return NULL;
574}
575
Petr Machata44965c72012-04-06 19:59:20 +0200576static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200577add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200578{
Petr Machata9a5420c2011-07-09 11:21:23 +0200579 Process ** leaderp = &list_of_processes;
580 if (proc->pid) {
581 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200582 if (tgid == 0)
583 /* Must have been terminated before we managed
584 * to fully attach. */
585 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200586 if (tgid == proc->pid)
587 proc->leader = proc;
588 else {
589 Process * leader = pid2proc(tgid);
590 proc->leader = leader;
591 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200592 leaderp = &leader->next;
593 }
594 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200595
596 if (!was_exec) {
597 proc->next = *leaderp;
598 *leaderp = proc;
599 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200600}
601
Petr Machatacbe29c62011-09-27 02:27:58 +0200602void
603change_process_leader(Process * proc, Process * leader)
604{
605 Process ** leaderp = &list_of_processes;
606 if (proc->leader == leader)
607 return;
608
609 assert(leader != NULL);
610 unlist_process(proc);
611 if (proc != leader)
612 leaderp = &leader->next;
613
614 proc->leader = leader;
615 proc->next = *leaderp;
616 *leaderp = proc;
617}
618
Petr Machata2b46cfc2012-02-18 11:17:29 +0100619static enum callback_status
620clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200621{
622 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
623 proc->pid, proc->leader->pid);
624 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100625 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200626}
627
628void
629remove_process(Process *proc)
630{
Petr Machatacebb8842011-07-09 11:14:11 +0200631 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
632
Petr Machata9a5420c2011-07-09 11:21:23 +0200633 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100634 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200635
Petr Machatacbe29c62011-09-27 02:27:58 +0200636 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100637 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200638 process_destroy(proc);
639 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100640}
Petr Machata4007d742011-07-09 11:29:42 +0200641
642void
Petr Machata366c2f42012-02-09 19:34:36 +0100643install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200644{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200645 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200646 assert(proc->event_handler == NULL);
647 proc->event_handler = handler;
648}
649
650void
651destroy_event_handler(Process * proc)
652{
Petr Machata366c2f42012-02-09 19:34:36 +0100653 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200654 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200655 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200656 if (handler->destroy != NULL)
657 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200658 free(handler);
659 proc->event_handler = NULL;
660}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100661
Petr Machataef2fd272012-09-28 00:43:01 +0200662static int
663breakpoint_for_symbol(struct library_symbol *libsym, struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100664{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200665 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200666 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100667
Petr Machataef2fd272012-09-28 00:43:01 +0200668 /* Don't enable latent or delayed symbols. */
669 if (libsym->latent || libsym->delayed)
670 return 0;
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200671
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200672 bp_addr = sym2addr(proc, libsym);
673
Petr Machatad5e85562012-04-05 15:18:38 +0200674 /* If there is an artificial breakpoint on the same address,
675 * its libsym will be NULL, and we can smuggle our libsym
676 * there. That artificial breakpoint is there presumably for
677 * the callbacks, which we don't touch. If there is a real
678 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200679 * symbols and ignore extra symbol aliases.
680 *
681 * The other direction is more complicated and currently not
682 * supported. If a breakpoint has custom callbacks, it might
683 * be also custom-allocated, and we would really need to swap
684 * the two: delete the one now in the dictionary, swap values
685 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200686 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200687 bp_addr);
Petr Machatad5e85562012-04-05 15:18:38 +0200688 if (bp != NULL) {
689 assert(bp->libsym == NULL);
690 bp->libsym = libsym;
Petr Machataef2fd272012-09-28 00:43:01 +0200691 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200692 }
693
694 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200695 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200696 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200697 fail:
698 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200699 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200700 }
701 if (proc_add_breakpoint(proc, bp) < 0) {
702 breakpoint_destroy(bp);
703 goto fail;
704 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100705
Petr Machatafa0c5702012-04-13 18:43:40 +0200706 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200707 proc_remove_breakpoint(proc, bp);
708 breakpoint_destroy(bp);
709 goto fail;
710 }
711
Petr Machataef2fd272012-09-28 00:43:01 +0200712 return 0;
713}
714
715static enum callback_status
716cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
717{
718 return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
719}
720
721static int
722proc_activate_latent_symbol(struct Process *proc,
723 struct library_symbol *libsym)
724{
725 assert(libsym->latent);
726 libsym->latent = 0;
727 return breakpoint_for_symbol(libsym, proc);
728}
729
730int
731proc_activate_delayed_symbol(struct Process *proc,
732 struct library_symbol *libsym)
733{
734 assert(libsym->delayed);
735 libsym->delayed = 0;
736 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100737}
738
Petr Machataa1f76832012-09-28 00:08:00 +0200739static enum callback_status
740activate_latent_in(struct Process *proc, struct library *lib, void *data)
741{
742 struct library_exported_name *exported;
743 for (exported = data; exported != NULL; exported = exported->next) {
744 struct library_symbol *libsym = NULL;
745 while ((libsym = library_each_symbol(lib, libsym,
746 library_symbol_named_cb,
747 (void *)exported->name))
748 != NULL)
749 if (libsym->latent
750 && proc_activate_latent_symbol(proc, libsym) < 0)
751 return CBS_FAIL;
752 }
753 return CBS_CONT;
754}
755
Petr Machata2b46cfc2012-02-18 11:17:29 +0100756void
757proc_add_library(struct Process *proc, struct library *lib)
758{
759 assert(lib->next == NULL);
760 lib->next = proc->libraries;
761 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200762 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
763 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100764
Petr Machataef2fd272012-09-28 00:43:01 +0200765 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100766 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200767 while ((libsym = library_each_symbol(lib, libsym,
768 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100769 proc)) != NULL)
Petr Machataef2fd272012-09-28 00:43:01 +0200770 fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s.",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200771 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200772
773 /* Look through export list of the new library and compare it
774 * with latent symbols of all libraries (including this
775 * library itself). */
776 struct library *lib2 = NULL;
777 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
778 lib->exported_names)) != NULL)
779 fprintf(stderr,
780 "Couldn't activate latent symbols for %s in %d: %s.",
781 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100782}
783
784int
785proc_remove_library(struct Process *proc, struct library *lib)
786{
787 struct library **libp;
788 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
789 if (*libp == lib) {
790 *libp = lib->next;
791 return 0;
792 }
793 return -1;
794}
795
796struct library *
797proc_each_library(struct Process *proc, struct library *it,
798 enum callback_status (*cb)(struct Process *proc,
799 struct library *lib, void *data),
800 void *data)
801{
802 if (it == NULL)
803 it = proc->libraries;
804
805 while (it != NULL) {
806 struct library *next = it->next;
807
808 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200809 case CBS_FAIL:
810 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100811 case CBS_STOP:
812 return it;
813 case CBS_CONT:
814 break;
815 }
816
817 it = next;
818 }
819
820 return NULL;
821}
Petr Machata52dbfb12012-03-29 16:38:26 +0200822
Petr Machataf7fee432012-04-19 17:00:53 +0200823static void
824check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200825{
Petr Machata52dbfb12012-03-29 16:38:26 +0200826 /* Only the group leader should be getting the breakpoints and
827 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200828 assert(proc->leader != NULL);
829 assert(proc->leader == proc);
830 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200831}
Petr Machata52dbfb12012-03-29 16:38:26 +0200832
Petr Machataf7fee432012-04-19 17:00:53 +0200833int
834proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
835{
Petr Machatafa0c5702012-04-13 18:43:40 +0200836 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200837 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200838 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200839
Petr Machataa2416362012-04-06 02:43:34 +0200840 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200841 * assert, but that's not necessary right now. Read the
842 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200843 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200844
Petr Machatafa0c5702012-04-13 18:43:40 +0200845 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200846 fprintf(stderr,
847 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
848 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200849 return -1;
850 }
851
Petr Machata52dbfb12012-03-29 16:38:26 +0200852 return 0;
853}
854
Petr Machataf7fee432012-04-19 17:00:53 +0200855void
Petr Machata52dbfb12012-03-29 16:38:26 +0200856proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
857{
Petr Machataf7fee432012-04-19 17:00:53 +0200858 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
859 proc->pid, breakpoint_name(bp), bp->addr);
860 check_leader(proc);
861 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
862 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200863}
Petr Machatad3cc9882012-04-13 21:40:23 +0200864
865/* Dict doesn't support iteration restarts, so here's this contraption
866 * for now. XXX add restarts to dict. */
867struct each_breakpoint_data
868{
869 void *start;
870 void *end;
871 struct Process *proc;
872 enum callback_status (*cb)(struct Process *proc,
873 struct breakpoint *bp,
874 void *data);
875 void *cb_data;
876};
877
878static void
879each_breakpoint_cb(void *key, void *value, void *d)
880{
881 struct each_breakpoint_data *data = d;
882 if (data->end != NULL)
883 return;
884 if (data->start == key)
885 data->start = NULL;
886
887 if (data->start == NULL) {
888 switch (data->cb(data->proc, value, data->cb_data)) {
889 case CBS_FAIL:
890 /* XXX handle me */
891 case CBS_STOP:
892 data->end = key;
893 case CBS_CONT:
894 return;
895 }
896 }
897}
898
899void *
900proc_each_breakpoint(struct Process *proc, void *start,
901 enum callback_status (*cb)(struct Process *proc,
902 struct breakpoint *bp,
903 void *data), void *data)
904{
905 struct each_breakpoint_data dd = {
906 .start = start,
907 .proc = proc,
908 .cb = cb,
909 .cb_data = data,
910 };
911 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
912 return dd.end;
913}