blob: 77f20ecb0f068895ba04f05b2aa80e37dfab4211 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
Petr Machata653085a2013-01-15 17:40:40 +01003 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata64262602012-01-07 03:41:36 +01004 * 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"
Petr Machataaf1e6032013-01-06 17:16:24 +010041#include "options.h"
Petr Machataba1664b2012-04-28 14:59:05 +020042#include "proc.h"
43#include "value_dict.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010044
Petr Machata744f2552012-04-15 04:33:18 +020045#ifndef ARCH_HAVE_PROCESS_DATA
46int
Petr Machata929bd572012-12-17 03:20:34 +010047arch_process_init(struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020048{
49 return 0;
50}
51
52void
Petr Machata929bd572012-12-17 03:20:34 +010053arch_process_destroy(struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020054{
55}
56
57int
Petr Machata929bd572012-12-17 03:20:34 +010058arch_process_clone(struct process *retp, struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020059{
60 return 0;
61}
62
63int
Petr Machata929bd572012-12-17 03:20:34 +010064arch_process_exec(struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020065{
66 return 0;
67}
68#endif
69
Petr Machata0f6e6d92012-10-26 23:42:17 +020070#ifndef OS_HAVE_PROCESS_DATA
71int
Petr Machata929bd572012-12-17 03:20:34 +010072os_process_init(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020073{
74 return 0;
75}
76
77void
Petr Machata929bd572012-12-17 03:20:34 +010078os_process_destroy(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020079{
80}
81
82int
Petr Machata929bd572012-12-17 03:20:34 +010083os_process_clone(struct process *retp, struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020084{
85 return 0;
86}
87
88int
Petr Machata929bd572012-12-17 03:20:34 +010089os_process_exec(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020090{
91 return 0;
92}
93#endif
94
Petr Machata93d95df2012-04-17 05:16:19 +020095#ifndef ARCH_HAVE_DYNLINK_DONE
96void
Petr Machata929bd572012-12-17 03:20:34 +010097arch_dynlink_done(struct process *proc)
Petr Machata93d95df2012-04-17 05:16:19 +020098{
99}
100#endif
101
Petr Machata929bd572012-12-17 03:20:34 +0100102static void add_process(struct process *proc, int was_exec);
103static void unlist_process(struct process *proc);
Petr Machata44965c72012-04-06 19:59:20 +0200104
Petr Machatae677c7e2012-10-26 22:23:43 +0200105static void
Petr Machata929bd572012-12-17 03:20:34 +0100106destroy_unwind(struct process *proc)
Petr Machatae677c7e2012-10-26 22:23:43 +0200107{
108#if defined(HAVE_LIBUNWIND)
Petr Machataaf1e6032013-01-06 17:16:24 +0100109 if (proc->unwind_priv != NULL)
110 _UPT_destroy(proc->unwind_priv);
111 if (proc->unwind_as != NULL)
112 unw_destroy_addr_space(proc->unwind_as);
Petr Machatae677c7e2012-10-26 22:23:43 +0200113#endif /* defined(HAVE_LIBUNWIND) */
114}
115
Petr Machata2b46cfc2012-02-18 11:17:29 +0100116static int
Petr Machata929bd572012-12-17 03:20:34 +0100117process_bare_init(struct process *proc, const char *filename,
Petr Machata3d0c91c2012-04-14 02:37:38 +0200118 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100119{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200120 if (!was_exec) {
121 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +0200122
Petr Machata3d0c91c2012-04-14 02:37:38 +0200123 proc->filename = strdup(filename);
124 if (proc->filename == NULL) {
125 fail:
126 free(proc->filename);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100127 if (proc->breakpoints != NULL) {
128 dict_destroy(proc->breakpoints,
129 NULL, NULL, NULL);
130 free(proc->breakpoints);
131 proc->breakpoints = NULL;
132 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200133 return -1;
134 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100135 }
136
137 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +0200138 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200139 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100140 if (proc->leader == NULL)
141 goto fail;
142
143 if (proc->leader == proc) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100144 proc->breakpoints = malloc(sizeof(*proc->breakpoints));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100145 if (proc->breakpoints == NULL)
146 goto fail;
Petr Machatad7e4ca82012-11-28 03:38:47 +0100147 DICT_INIT(proc->breakpoints,
148 arch_addr_t, struct breakpoint *,
149 arch_addr_hash, arch_addr_eq, NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100150 } else {
151 proc->breakpoints = NULL;
152 }
153
Petr Machataaf1e6032013-01-06 17:16:24 +0100154 if (options.bt_depth > 0) {
Joe Damatoab3b72c2010-10-31 00:21:53 -0700155#if defined(HAVE_LIBUNWIND)
Petr Machataaf1e6032013-01-06 17:16:24 +0100156 proc->unwind_priv = _UPT_create(pid);
157 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700158#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -0700159
Petr Machataaf1e6032013-01-06 17:16:24 +0100160 if (proc->unwind_priv == NULL || proc->unwind_as == NULL) {
161 fprintf(stderr,
162 "Couldn't initialize unwinding "
163 "for process %d\n", proc->pid);
164 destroy_unwind(proc);
165 proc->unwind_priv = NULL;
166 proc->unwind_as = NULL;
167 }
168 }
169
Petr Machata2b46cfc2012-02-18 11:17:29 +0100170 return 0;
171}
172
173static void
Petr Machata929bd572012-12-17 03:20:34 +0100174process_bare_destroy(struct process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100175{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100176 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
177 free(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178 if (!was_exec) {
179 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200180 unlist_process(proc);
Petr Machatae677c7e2012-10-26 22:23:43 +0200181 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200182 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100183}
184
Petr Machata3d0c91c2012-04-14 02:37:38 +0200185static int
Petr Machata929bd572012-12-17 03:20:34 +0100186process_init_main(struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100187{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200188 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200189 fprintf(stderr, "failed to init breakpoints %d\n",
190 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200191 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200192 }
193
Petr Machata2b46cfc2012-02-18 11:17:29 +0100194 return 0;
195}
196
Petr Machata3d0c91c2012-04-14 02:37:38 +0200197int
Petr Machata929bd572012-12-17 03:20:34 +0100198process_init(struct process *proc, const char *filename, pid_t pid)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200199{
200 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200201 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200202 fprintf(stderr, "failed to initialize process %d: %s\n",
203 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200204 return -1;
205 }
206
Petr Machata0f6e6d92012-10-26 23:42:17 +0200207 if (os_process_init(proc) < 0) {
208 process_bare_destroy(proc, 0);
209 goto fail;
210 }
211
Petr Machata744f2552012-04-15 04:33:18 +0200212 if (arch_process_init(proc) < 0) {
Petr Machata0f6e6d92012-10-26 23:42:17 +0200213 os_process_destroy(proc);
Petr Machata744f2552012-04-15 04:33:18 +0200214 process_bare_destroy(proc, 0);
215 goto fail;
216 }
217
Petr Machata218c5ff2012-04-15 04:22:39 +0200218 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200219 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200220 if (process_init_main(proc) < 0) {
221 process_bare_destroy(proc, 0);
222 goto fail;
223 }
224 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200225}
226
Petr Machata8ead1cd2012-04-24 18:13:09 +0200227static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100228destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200229{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200230 breakpoint_destroy(bp);
231 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200232 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200233}
234
Petr Machatae0e89ed2012-10-26 22:25:33 +0200235// XXX see comment in handle_event.c
Petr Machata929bd572012-12-17 03:20:34 +0100236void callstack_pop(struct process *proc);
Petr Machatae0e89ed2012-10-26 22:25:33 +0200237
Petr Machata3d0c91c2012-04-14 02:37:38 +0200238static void
Petr Machata929bd572012-12-17 03:20:34 +0100239private_process_destroy(struct process *proc, int was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200240{
Petr Machatae0e89ed2012-10-26 22:25:33 +0200241 /* Pop remaining stack elements. */
242 while (proc->callstack_depth > 0) {
243 /* When this is called just before a process is
244 * destroyed, the breakpoints should either have been
245 * retracted by now, or were killed by exec. In any
246 * case, it's safe to pretend that there are no
247 * breakpoints associated with the stack elements, so
248 * that stack_pop doesn't attempt to destroy them. */
249 size_t i = proc->callstack_depth - 1;
250 if (!proc->callstack[i].is_syscall)
251 proc->callstack[i].return_addr = 0;
252
253 callstack_pop(proc);
254 }
255
256 if (!was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200257 free(proc->filename);
258
Petr Machata8ead1cd2012-04-24 18:13:09 +0200259 /* Libraries and symbols. This is only relevant in
260 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200261 struct library *lib;
262 for (lib = proc->libraries; lib != NULL; ) {
263 struct library *next = lib->next;
264 library_destroy(lib);
265 free(lib);
266 lib = next;
267 }
268 proc->libraries = NULL;
269
270 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200271 if (proc->breakpoints != NULL) {
272 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100273 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
274 free(proc->breakpoints);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200275 proc->breakpoints = NULL;
276 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200277
278 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200279}
280
281void
Petr Machata929bd572012-12-17 03:20:34 +0100282process_destroy(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200283{
Petr Machata744f2552012-04-15 04:33:18 +0200284 arch_process_destroy(proc);
Petr Machata0f6e6d92012-10-26 23:42:17 +0200285 os_process_destroy(proc);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200286 private_process_destroy(proc, 0);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200287}
288
289int
Petr Machata929bd572012-12-17 03:20:34 +0100290process_exec(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200291{
Petr Machata0f6e6d92012-10-26 23:42:17 +0200292 /* Call exec handlers first, before we destroy the main
Petr Machata3cc0cd12012-10-26 22:30:51 +0200293 * state. */
Petr Machata0f6e6d92012-10-26 23:42:17 +0200294 if (arch_process_exec(proc) < 0
295 || os_process_exec(proc) < 0)
Petr Machata744f2552012-04-15 04:33:18 +0200296 return -1;
297
Petr Machata3d0c91c2012-04-14 02:37:38 +0200298 private_process_destroy(proc, 1);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200299
Petr Machata3d0c91c2012-04-14 02:37:38 +0200300 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
301 return -1;
302 if (process_init_main(proc) < 0) {
303 process_bare_destroy(proc, 1);
304 return -1;
305 }
306 return 0;
307}
308
Petr Machata929bd572012-12-17 03:20:34 +0100309struct process *
Petr Machata75934ad2012-04-14 02:28:03 +0200310open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100311{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312 assert(pid != 0);
Petr Machata929bd572012-12-17 03:20:34 +0100313 struct process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200314 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200315 free(proc);
316 return NULL;
317 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100318 return proc;
319}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100320
Petr Machata2b46cfc2012-02-18 11:17:29 +0100321struct clone_single_bp_data {
Petr Machata929bd572012-12-17 03:20:34 +0100322 struct process *old_proc;
323 struct process *new_proc;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100324};
325
Petr Machatad7e4ca82012-11-28 03:38:47 +0100326static enum callback_status
327clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100328{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100329 struct breakpoint *bp = *bpp;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330 struct clone_single_bp_data *data = u;
331
Petr Machata2b46cfc2012-02-18 11:17:29 +0100332 struct breakpoint *clone = malloc(sizeof(*clone));
333 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200334 || breakpoint_clone(clone, data->new_proc,
335 bp, data->old_proc) < 0) {
336 fail:
337 free(clone);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100338 return CBS_STOP;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100339 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200340 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
341 breakpoint_destroy(clone);
342 goto fail;
343 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100344 return CBS_CONT;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100345}
346
347int
Petr Machata929bd572012-12-17 03:20:34 +0100348process_clone(struct process *retp, struct process *proc, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100349{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200350 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200351 fail1:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200352 fprintf(stderr, "failed to clone process %d->%d : %s\n",
353 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100354 return -1;
355 }
356
Petr Machatacf1679a2012-04-06 19:56:17 +0200357 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200358 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400359 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200360
Petr Machata2b46cfc2012-02-18 11:17:29 +0100361 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200362 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100363 return 0;
364
365 /* Clone symbols first so that we can clone and relink
366 * breakpoints. */
367 struct library *lib;
368 struct library **nlibp = &retp->libraries;
Petr Machata1e339e02012-10-27 19:33:20 +0200369 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100370 *nlibp = malloc(sizeof(**nlibp));
371 if (*nlibp == NULL
372 || library_clone(*nlibp, lib) < 0) {
373 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200374 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100375
376 /* Error when cloning. Unroll what was done. */
377 for (lib = retp->libraries; lib != NULL; ) {
378 struct library *next = lib->next;
379 library_destroy(lib);
380 free(lib);
381 lib = next;
382 }
Petr Machataba1664b2012-04-28 14:59:05 +0200383 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100384 }
385
386 nlibp = &(*nlibp)->next;
387 }
388
389 /* Now clone breakpoints. Symbol relinking is done in
390 * clone_single_bp. */
391 struct clone_single_bp_data data = {
392 .old_proc = proc,
393 .new_proc = retp,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100394 };
Petr Machatad7e4ca82012-11-28 03:38:47 +0100395 if (DICT_EACH(proc->leader->breakpoints,
396 arch_addr_t, struct breakpoint *, NULL,
397 clone_single_bp, &data) != NULL)
Petr Machata94078ec2012-01-05 18:07:02 +0100398 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100399
Petr Machataded6f972012-04-13 23:15:48 +0200400 /* And finally the call stack. */
Petr Machatab6de8412012-10-27 19:31:22 +0200401 /* XXX clearly the callstack handling should be moved to a
402 * separate module and this whole business extracted to
403 * callstack_clone, or callstack_element_clone. */
Petr Machataded6f972012-04-13 23:15:48 +0200404 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
405 retp->callstack_depth = proc->callstack_depth;
406
Petr Machata94078ec2012-01-05 18:07:02 +0100407 size_t i;
408 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machatab6de8412012-10-27 19:31:22 +0200409 struct callstack_element *elem = &retp->callstack[i];
410 struct fetch_context *ctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100411 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200412 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100413 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200414 size_t j;
415 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100416 for (j = 0; j < i; ++j) {
Petr Machatab6de8412012-10-27 19:31:22 +0200417 nctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100418 fetch_arg_done(nctx);
Petr Machatab6de8412012-10-27 19:31:22 +0200419 elem->fetch_context = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100420 }
421 goto fail2;
422 }
Petr Machatab6de8412012-10-27 19:31:22 +0200423 elem->fetch_context = nctx;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100424 }
425
Petr Machatab6de8412012-10-27 19:31:22 +0200426 struct value_dict *args = elem->arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100427 if (args != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100428 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100429 if (nargs == NULL
430 || val_dict_clone(nargs, args) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200431 size_t j;
Petr Machata94078ec2012-01-05 18:07:02 +0100432 for (j = 0; j < i; ++j) {
Petr Machatab6de8412012-10-27 19:31:22 +0200433 nargs = elem->arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100434 val_dict_destroy(nargs);
435 free(nargs);
Petr Machatab6de8412012-10-27 19:31:22 +0200436 elem->arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100437 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100438
439 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200440 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100441 * fetch_context. */
442 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200443 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100444 }
Petr Machatab6de8412012-10-27 19:31:22 +0200445 elem->arguments = nargs;
Petr Machata94078ec2012-01-05 18:07:02 +0100446 }
Petr Machata165b5662012-10-27 19:23:12 +0200447
448 /* If it's not a syscall, we need to find the
449 * corresponding library symbol in the cloned
450 * library. */
451 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
452 struct library_symbol *libfunc = elem->c_un.libfunc;
453 int rc = proc_find_symbol(retp, libfunc,
454 NULL, &elem->c_un.libfunc);
455 assert(rc == 0);
456 }
Petr Machata94078ec2012-01-05 18:07:02 +0100457 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100458
Petr Machata5bf47142012-10-27 19:29:00 +0200459 /* At this point, retp is fully initialized, except for OS and
460 * arch parts, and we can call private_process_destroy. */
461 if (os_process_clone(retp, proc) < 0) {
462 private_process_destroy(retp, 0);
463 return -1;
464 }
465 if (arch_process_clone(retp, proc) < 0) {
466 os_process_destroy(retp);
467 private_process_destroy(retp, 0);
468 return -1;
469 }
Petr Machata744f2552012-04-15 04:33:18 +0200470
Petr Machata2b46cfc2012-02-18 11:17:29 +0100471 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100472}
473
Petr Machata3c516d52011-08-18 03:53:18 +0200474static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200475open_one_pid(pid_t pid)
476{
Petr Machata9a5420c2011-07-09 11:21:23 +0200477 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
478
Petr Machata1974dbc2011-08-19 18:58:01 +0200479 /* Get the filename first. Should the trace_pid fail, we can
480 * easily free it, untracing is more work. */
Petr Machata929bd572012-12-17 03:20:34 +0100481 char *filename = pid2name(pid);
482 if (filename == NULL || trace_pid(pid) < 0) {
Petr Machataef0c74d2012-10-27 00:30:57 +0200483 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200484 free(filename);
485 return -1;
486 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100487
Petr Machata929bd572012-12-17 03:20:34 +0100488 struct process *proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200489 if (proc == NULL)
Petr Machataef0c74d2012-10-27 00:30:57 +0200490 goto fail;
491 free(filename);
Petr Machata3ed2a422012-04-06 17:18:55 +0200492 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200493
Petr Machata1974dbc2011-08-19 18:58:01 +0200494 return 0;
495}
496
Petr Machata2b46cfc2012-02-18 11:17:29 +0100497static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100498start_one_pid(struct process *proc, void *data)
Petr Machata1974dbc2011-08-19 18:58:01 +0200499{
500 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100501 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100502}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100503
Petr Machata9a5420c2011-07-09 11:21:23 +0200504void
505open_pid(pid_t pid)
506{
507 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200508 /* If we are already tracing this guy, we should be seeing all
509 * his children via normal tracing route. */
510 if (pid2proc(pid) != NULL)
511 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200512
Petr Machata3c516d52011-08-18 03:53:18 +0200513 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200514 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200515 fprintf(stderr, "Cannot attach to pid %u: %s\n",
516 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200517 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200518 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200519 }
520
Petr Machata3c516d52011-08-18 03:53:18 +0200521 /* Now attach to all tasks that belong to that PID. There's a
522 * race between process_tasks and open_one_pid. So when we
523 * fail in open_one_pid below, we just do another round.
524 * Chances are that by then that PID will have gone away, and
525 * that's why we have seen the failure. The processes that we
526 * manage to open_one_pid are stopped, so we should eventually
527 * reach a point where process_tasks doesn't give any new
528 * processes (because there's nobody left to produce
529 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200530 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200531 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200532 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200533 pid_t *tasks;
534 size_t ntasks;
535 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200536
Petr Machata3c516d52011-08-18 03:53:18 +0200537 if (process_tasks(pid, &tasks, &ntasks) < 0) {
538 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
539 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100540 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200541 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200542
Petr Machata3c516d52011-08-18 03:53:18 +0200543 have_all = 1;
544 for (i = 0; i < ntasks; ++i)
545 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200546 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200547 have_all = 0;
548
Petr Machata9a5420c2011-07-09 11:21:23 +0200549 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200550
Petr Machata1974dbc2011-08-19 18:58:01 +0200551 if (have_all && old_ntasks == ntasks)
552 break;
553 old_ntasks = ntasks;
554 }
555
Petr Machata929bd572012-12-17 03:20:34 +0100556 struct process *leader = pid2proc(pid)->leader;
Petr Machata93d95df2012-04-17 05:16:19 +0200557
558 /* XXX Is there a way to figure out whether _start has
559 * actually already been hit? */
560 arch_dynlink_done(leader);
561
Petr Machata2f9b78e2012-04-16 21:08:54 +0200562 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200563 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200564}
565
Petr Machata2b46cfc2012-02-18 11:17:29 +0100566static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100567find_proc(struct process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200568{
569 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100570 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200571}
572
Petr Machata929bd572012-12-17 03:20:34 +0100573struct process *
574pid2proc(pid_t pid)
575{
Petr Machatacebb8842011-07-09 11:14:11 +0200576 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
577}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100578
Petr Machata929bd572012-12-17 03:20:34 +0100579static struct process *list_of_processes = NULL;
Petr Machatacebb8842011-07-09 11:14:11 +0200580
Petr Machatacbe29c62011-09-27 02:27:58 +0200581static void
Petr Machata929bd572012-12-17 03:20:34 +0100582unlist_process(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +0200583{
Petr Machatacbe29c62011-09-27 02:27:58 +0200584 if (list_of_processes == proc) {
585 list_of_processes = list_of_processes->next;
586 return;
587 }
588
Petr Machata929bd572012-12-17 03:20:34 +0100589 struct process *tmp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200590 for (tmp = list_of_processes; ; tmp = tmp->next) {
591 /* If the following assert fails, the process wasn't
592 * in the list. */
593 assert(tmp->next != NULL);
594
595 if (tmp->next == proc) {
596 tmp->next = tmp->next->next;
597 return;
598 }
599 }
600}
601
Petr Machata929bd572012-12-17 03:20:34 +0100602struct process *
603each_process(struct process *start_after,
604 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100605 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200606{
Petr Machata929bd572012-12-17 03:20:34 +0100607 struct process *it = start_after == NULL ? list_of_processes
Petr Machata74132a42012-03-16 02:46:18 +0100608 : start_after->next;
609
610 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200611 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100612 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100613 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200614 case CBS_FAIL:
615 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100616 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200617 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100618 case CBS_CONT:
619 break;
620 }
Petr Machatacebb8842011-07-09 11:14:11 +0200621 it = next;
622 }
623 return NULL;
624}
Petr Machata9a5420c2011-07-09 11:21:23 +0200625
Petr Machata929bd572012-12-17 03:20:34 +0100626struct process *
627each_task(struct process *proc, struct process *start_after,
628 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100629 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200630{
Petr Machata74132a42012-03-16 02:46:18 +0100631 assert(proc != NULL);
Petr Machata929bd572012-12-17 03:20:34 +0100632 struct process *it = start_after == NULL ? proc->leader
Petr Machata74132a42012-03-16 02:46:18 +0100633 : start_after->next;
634
Petr Machata9a5420c2011-07-09 11:21:23 +0200635 if (it != NULL) {
Petr Machata929bd572012-12-17 03:20:34 +0100636 struct process *leader = it->leader;
Petr Machata74132a42012-03-16 02:46:18 +0100637 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200638 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100639 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100640 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200641 case CBS_FAIL:
642 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100643 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200644 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100645 case CBS_CONT:
646 break;
647 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200648 it = next;
649 }
650 }
651 return NULL;
652}
653
Petr Machata44965c72012-04-06 19:59:20 +0200654static void
Petr Machata929bd572012-12-17 03:20:34 +0100655add_process(struct process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200656{
Petr Machata929bd572012-12-17 03:20:34 +0100657 struct process **leaderp = &list_of_processes;
Petr Machata9a5420c2011-07-09 11:21:23 +0200658 if (proc->pid) {
659 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200660 if (tgid == 0)
661 /* Must have been terminated before we managed
662 * to fully attach. */
663 return;
Petr Machata929bd572012-12-17 03:20:34 +0100664 if (tgid == proc->pid) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200665 proc->leader = proc;
Petr Machata929bd572012-12-17 03:20:34 +0100666 } else {
667 struct process *leader = pid2proc(tgid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200668 proc->leader = leader;
669 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200670 leaderp = &leader->next;
671 }
672 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200673
674 if (!was_exec) {
675 proc->next = *leaderp;
676 *leaderp = proc;
677 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200678}
679
Petr Machatacbe29c62011-09-27 02:27:58 +0200680void
Petr Machata929bd572012-12-17 03:20:34 +0100681change_process_leader(struct process *proc, struct process *leader)
Petr Machatacbe29c62011-09-27 02:27:58 +0200682{
Petr Machata929bd572012-12-17 03:20:34 +0100683 struct process **leaderp = &list_of_processes;
Petr Machatacbe29c62011-09-27 02:27:58 +0200684 if (proc->leader == leader)
685 return;
686
687 assert(leader != NULL);
688 unlist_process(proc);
689 if (proc != leader)
690 leaderp = &leader->next;
691
692 proc->leader = leader;
693 proc->next = *leaderp;
694 *leaderp = proc;
695}
696
Petr Machata2b46cfc2012-02-18 11:17:29 +0100697static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100698clear_leader(struct process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200699{
700 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
701 proc->pid, proc->leader->pid);
702 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100703 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200704}
705
706void
Petr Machata929bd572012-12-17 03:20:34 +0100707remove_process(struct process *proc)
Petr Machatacebb8842011-07-09 11:14:11 +0200708{
Petr Machatacebb8842011-07-09 11:14:11 +0200709 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
710
Petr Machata9a5420c2011-07-09 11:21:23 +0200711 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100712 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200713
Petr Machatacbe29c62011-09-27 02:27:58 +0200714 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100715 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200716 process_destroy(proc);
717 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100718}
Petr Machata4007d742011-07-09 11:29:42 +0200719
720void
Petr Machata929bd572012-12-17 03:20:34 +0100721install_event_handler(struct process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200722{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200723 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200724 assert(proc->event_handler == NULL);
725 proc->event_handler = handler;
726}
727
728void
Petr Machata929bd572012-12-17 03:20:34 +0100729destroy_event_handler(struct process *proc)
Petr Machata4007d742011-07-09 11:29:42 +0200730{
Petr Machata366c2f42012-02-09 19:34:36 +0100731 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200732 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200733 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200734 if (handler->destroy != NULL)
735 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200736 free(handler);
737 proc->event_handler = NULL;
738}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100739
Petr Machataef2fd272012-09-28 00:43:01 +0200740static int
Petr Machata929bd572012-12-17 03:20:34 +0100741breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100742{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200743 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200744 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100745
Petr Machataef2fd272012-09-28 00:43:01 +0200746 /* Don't enable latent or delayed symbols. */
Petr Machata96f04822012-10-31 03:29:11 +0100747 if (libsym->latent || libsym->delayed) {
748 debug(DEBUG_FUNCTION,
749 "delayed and/or latent breakpoint pid=%d, %s@%p",
750 proc->pid, libsym->name, libsym->enter_addr);
Petr Machataef2fd272012-09-28 00:43:01 +0200751 return 0;
Petr Machata96f04822012-10-31 03:29:11 +0100752 }
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200753
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200754 bp_addr = sym2addr(proc, libsym);
755
Petr Machatad5e85562012-04-05 15:18:38 +0200756 /* If there is an artificial breakpoint on the same address,
757 * its libsym will be NULL, and we can smuggle our libsym
758 * there. That artificial breakpoint is there presumably for
759 * the callbacks, which we don't touch. If there is a real
760 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200761 * symbols and ignore extra symbol aliases.
762 *
763 * The other direction is more complicated and currently not
764 * supported. If a breakpoint has custom callbacks, it might
765 * be also custom-allocated, and we would really need to swap
766 * the two: delete the one now in the dictionary, swap values
767 * around, and put the new breakpoint back in. */
Petr Machata98ff3092013-03-08 22:11:36 +0100768 struct breakpoint *bp;
769 if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
Petr Machata8d58d8b2012-11-12 12:46:03 +0100770 /* MIPS backend makes duplicate requests. This is
771 * likely a bug in the backend. Currently there's no
772 * point assigning more than one symbol to a
773 * breakpoint, because when it hits, we won't know
774 * what to print out. But it's easier to fix it here
775 * before someone who understands MIPS has the time to
776 * look into it. So turn the sanity check off on
777 * MIPS. References:
778 *
779 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
780 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
781 */
782#ifndef __mips__
Petr Machata98ff3092013-03-08 22:11:36 +0100783 assert(bp->libsym == NULL);
784 bp->libsym = libsym;
Petr Machata8d58d8b2012-11-12 12:46:03 +0100785#endif
Petr Machataef2fd272012-09-28 00:43:01 +0200786 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200787 }
788
Petr Machata98ff3092013-03-08 22:11:36 +0100789 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200790 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200791 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200792 fail:
793 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200794 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200795 }
796 if (proc_add_breakpoint(proc, bp) < 0) {
797 breakpoint_destroy(bp);
798 goto fail;
799 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100800
Petr Machatafa0c5702012-04-13 18:43:40 +0200801 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200802 proc_remove_breakpoint(proc, bp);
803 breakpoint_destroy(bp);
804 goto fail;
805 }
806
Petr Machataef2fd272012-09-28 00:43:01 +0200807 return 0;
808}
809
810static enum callback_status
811cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
812{
813 return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
814}
815
816static int
Petr Machata929bd572012-12-17 03:20:34 +0100817proc_activate_latent_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200818 struct library_symbol *libsym)
819{
820 assert(libsym->latent);
821 libsym->latent = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100822 debug(DEBUG_FUNCTION, "activated latent symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200823 return breakpoint_for_symbol(libsym, proc);
824}
825
826int
Petr Machata929bd572012-12-17 03:20:34 +0100827proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200828 struct library_symbol *libsym)
829{
830 assert(libsym->delayed);
831 libsym->delayed = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100832 debug(DEBUG_FUNCTION, "activated delayed symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200833 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100834}
835
Petr Machataa1f76832012-09-28 00:08:00 +0200836static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100837activate_latent_in(struct process *proc, struct library *lib, void *data)
Petr Machataa1f76832012-09-28 00:08:00 +0200838{
839 struct library_exported_name *exported;
840 for (exported = data; exported != NULL; exported = exported->next) {
841 struct library_symbol *libsym = NULL;
842 while ((libsym = library_each_symbol(lib, libsym,
843 library_symbol_named_cb,
844 (void *)exported->name))
845 != NULL)
846 if (libsym->latent
847 && proc_activate_latent_symbol(proc, libsym) < 0)
848 return CBS_FAIL;
849 }
850 return CBS_CONT;
851}
852
Petr Machata2b46cfc2012-02-18 11:17:29 +0100853void
Petr Machata929bd572012-12-17 03:20:34 +0100854proc_add_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100855{
856 assert(lib->next == NULL);
857 lib->next = proc->libraries;
858 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200859 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
860 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100861
Petr Machataef2fd272012-09-28 00:43:01 +0200862 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100863 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200864 while ((libsym = library_each_symbol(lib, libsym,
865 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100866 proc)) != NULL)
Petr Machataef2fd272012-09-28 00:43:01 +0200867 fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s.",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200868 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200869
870 /* Look through export list of the new library and compare it
871 * with latent symbols of all libraries (including this
872 * library itself). */
873 struct library *lib2 = NULL;
874 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
875 lib->exported_names)) != NULL)
876 fprintf(stderr,
877 "Couldn't activate latent symbols for %s in %d: %s.",
878 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100879}
880
881int
Petr Machata929bd572012-12-17 03:20:34 +0100882proc_remove_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100883{
884 struct library **libp;
885 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
886 if (*libp == lib) {
887 *libp = lib->next;
888 return 0;
889 }
890 return -1;
891}
892
893struct library *
Petr Machata929bd572012-12-17 03:20:34 +0100894proc_each_library(struct process *proc, struct library *it,
895 enum callback_status (*cb)(struct process *proc,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100896 struct library *lib, void *data),
897 void *data)
898{
899 if (it == NULL)
900 it = proc->libraries;
901
902 while (it != NULL) {
903 struct library *next = it->next;
904
905 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200906 case CBS_FAIL:
907 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100908 case CBS_STOP:
909 return it;
910 case CBS_CONT:
911 break;
912 }
913
914 it = next;
915 }
916
917 return NULL;
918}
Petr Machata52dbfb12012-03-29 16:38:26 +0200919
Petr Machataf7fee432012-04-19 17:00:53 +0200920static void
Petr Machata929bd572012-12-17 03:20:34 +0100921check_leader(struct process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200922{
Petr Machata52dbfb12012-03-29 16:38:26 +0200923 /* Only the group leader should be getting the breakpoints and
924 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200925 assert(proc->leader != NULL);
926 assert(proc->leader == proc);
927 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200928}
Petr Machata52dbfb12012-03-29 16:38:26 +0200929
Petr Machataf7fee432012-04-19 17:00:53 +0200930int
Petr Machata929bd572012-12-17 03:20:34 +0100931proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machataf7fee432012-04-19 17:00:53 +0200932{
Petr Machatafa0c5702012-04-13 18:43:40 +0200933 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200934 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200935 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200936
Petr Machataa2416362012-04-06 02:43:34 +0200937 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200938 * assert, but that's not necessary right now. Read the
939 * comment in breakpoint_for_symbol. */
Petr Machatad7e4ca82012-11-28 03:38:47 +0100940 assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200941
Petr Machatad7e4ca82012-11-28 03:38:47 +0100942 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200943 fprintf(stderr,
944 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
945 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200946 return -1;
947 }
948
Petr Machata52dbfb12012-03-29 16:38:26 +0200949 return 0;
950}
951
Petr Machataf7fee432012-04-19 17:00:53 +0200952void
Petr Machata929bd572012-12-17 03:20:34 +0100953proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machata52dbfb12012-03-29 16:38:26 +0200954{
Petr Machataf7fee432012-04-19 17:00:53 +0200955 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
956 proc->pid, breakpoint_name(bp), bp->addr);
957 check_leader(proc);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100958 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
959 NULL, NULL, NULL);
960 assert(rc == 0);
Petr Machata52dbfb12012-03-29 16:38:26 +0200961}
Petr Machatad3cc9882012-04-13 21:40:23 +0200962
Petr Machatad3cc9882012-04-13 21:40:23 +0200963struct each_breakpoint_data
964{
Petr Machata929bd572012-12-17 03:20:34 +0100965 struct process *proc;
966 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +0200967 struct breakpoint *bp,
968 void *data);
969 void *cb_data;
970};
971
Petr Machatad7e4ca82012-11-28 03:38:47 +0100972static enum callback_status
973each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
Petr Machatad3cc9882012-04-13 21:40:23 +0200974{
975 struct each_breakpoint_data *data = d;
Petr Machatad7e4ca82012-11-28 03:38:47 +0100976 return data->cb(data->proc, *bpp, data->cb_data);
Petr Machatad3cc9882012-04-13 21:40:23 +0200977}
978
979void *
Petr Machata929bd572012-12-17 03:20:34 +0100980proc_each_breakpoint(struct process *proc, void *start,
981 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +0200982 struct breakpoint *bp,
983 void *data), void *data)
984{
985 struct each_breakpoint_data dd = {
Petr Machatad3cc9882012-04-13 21:40:23 +0200986 .proc = proc,
987 .cb = cb,
988 .cb_data = data,
989 };
Petr Machatad7e4ca82012-11-28 03:38:47 +0100990 return DICT_EACH(proc->breakpoints,
991 arch_addr_t, struct breakpoint *, start,
992 &each_breakpoint_cb, &dd);
Petr Machatad3cc9882012-04-13 21:40:23 +0200993}
Petr Machata165b5662012-10-27 19:23:12 +0200994
995int
Petr Machata929bd572012-12-17 03:20:34 +0100996proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +0200997 struct library **retlib, struct library_symbol **retsym)
998{
999 struct library *lib = sym->lib;
1000 assert(lib != NULL);
1001
1002 struct library *flib
1003 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1004 if (flib == NULL)
1005 return -1;
1006
1007 struct library_symbol *fsym
1008 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1009 (char *)sym->name);
1010 if (fsym == NULL)
1011 return -1;
1012
1013 if (retlib != NULL)
1014 *retlib = flib;
1015 if (retsym != NULL)
1016 *retsym = fsym;
1017
1018 return 0;
1019}
Petr Machata32405542012-10-31 03:28:39 +01001020
1021struct library_symbol *
Petr Machata929bd572012-12-17 03:20:34 +01001022proc_each_symbol(struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +01001023 enum callback_status (*cb)(struct library_symbol *, void *),
1024 void *data)
1025{
1026 struct library *lib;
Petr Machata32405542012-10-31 03:28:39 +01001027 for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1028 lib != NULL; lib = lib->next) {
1029 start_after = library_each_symbol(lib, start_after, cb, data);
1030 if (start_after != NULL)
1031 return start_after;
1032 }
1033
1034 return NULL;
1035}
Petr Machata653085a2013-01-15 17:40:40 +01001036
1037#define DEF_READER(NAME, SIZE) \
1038 int \
1039 NAME(struct process *proc, arch_addr_t addr, \
1040 uint##SIZE##_t *lp) \
1041 { \
1042 union { \
1043 uint##SIZE##_t dst; \
1044 char buf[0]; \
1045 } u; \
1046 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \
1047 != sizeof(u.dst)) \
1048 return -1; \
1049 *lp = u.dst; \
1050 return 0; \
1051 }
1052
1053DEF_READER(proc_read_16, 16)
1054DEF_READER(proc_read_32, 32)
1055DEF_READER(proc_read_64, 64)
1056
1057#undef DEF_READER