blob: 17bb3cd58a1ccef07777f91220f3d0172b306363 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
Petr Machata6bcc0922014-01-09 23:50:07 +01003 * Copyright (C) 2011,2012,2013,2014 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
Petr Machata64262602012-01-07 03:41:36 +010032#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020033#include "breakpoint.h"
34#include "debug.h"
35#include "fetch.h"
Petr Machataaf1e6032013-01-06 17:16:24 +010036#include "options.h"
Petr Machataba1664b2012-04-28 14:59:05 +020037#include "proc.h"
38#include "value_dict.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010039
Petr Machata0f6e6d92012-10-26 23:42:17 +020040#ifndef OS_HAVE_PROCESS_DATA
41int
Petr Machata929bd572012-12-17 03:20:34 +010042os_process_init(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020043{
44 return 0;
45}
46
47void
Petr Machata929bd572012-12-17 03:20:34 +010048os_process_destroy(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020049{
50}
51
52int
Petr Machata929bd572012-12-17 03:20:34 +010053os_process_clone(struct process *retp, struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020054{
55 return 0;
56}
57
58int
Petr Machata929bd572012-12-17 03:20:34 +010059os_process_exec(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020060{
61 return 0;
62}
63#endif
64
Petr Machata23124cc2013-10-11 21:13:22 +020065#ifndef ARCH_HAVE_PROCESS_DATA
66int
67arch_process_init(struct process *proc)
68{
69 return 0;
70}
71
72void
73arch_process_destroy(struct process *proc)
74{
75}
76
77int
78arch_process_clone(struct process *retp, struct process *proc)
79{
80 return 0;
81}
82
83int
84arch_process_exec(struct process *proc)
85{
86 return 0;
87}
88#endif
89
Petr Machata93d95df2012-04-17 05:16:19 +020090#ifndef ARCH_HAVE_DYNLINK_DONE
91void
Petr Machata929bd572012-12-17 03:20:34 +010092arch_dynlink_done(struct process *proc)
Petr Machata93d95df2012-04-17 05:16:19 +020093{
94}
95#endif
96
Petr Machatabaf00e42013-01-07 19:44:43 +010097static int add_process(struct process *proc, int was_exec);
Petr Machata929bd572012-12-17 03:20:34 +010098static void unlist_process(struct process *proc);
Petr Machata44965c72012-04-06 19:59:20 +020099
Petr Machatae677c7e2012-10-26 22:23:43 +0200100static void
Petr Machata929bd572012-12-17 03:20:34 +0100101destroy_unwind(struct process *proc)
Petr Machatae677c7e2012-10-26 22:23:43 +0200102{
103#if defined(HAVE_LIBUNWIND)
Petr Machataaf1e6032013-01-06 17:16:24 +0100104 if (proc->unwind_priv != NULL)
105 _UPT_destroy(proc->unwind_priv);
106 if (proc->unwind_as != NULL)
107 unw_destroy_addr_space(proc->unwind_as);
Petr Machatae677c7e2012-10-26 22:23:43 +0200108#endif /* defined(HAVE_LIBUNWIND) */
Mark Wielaarddfefa9f2014-01-07 21:00:44 +0100109
110#if defined(HAVE_LIBDW)
111 if (proc->dwfl != NULL)
112 dwfl_end(proc->dwfl);
113#endif /* defined(HAVE_LIBDW) */
Petr Machatae677c7e2012-10-26 22:23:43 +0200114}
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 Machatabaf00e42013-01-07 19:44:43 +0100139 if (add_process(proc, was_exec) < 0)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100140 goto fail;
Petr Machatabaf00e42013-01-07 19:44:43 +0100141 if (proc->leader == NULL) {
142 unlist_and_fail:
143 if (!was_exec)
144 unlist_process(proc);
145 goto fail;
146 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100147
148 if (proc->leader == proc) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100149 proc->breakpoints = malloc(sizeof(*proc->breakpoints));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100150 if (proc->breakpoints == NULL)
Petr Machatabaf00e42013-01-07 19:44:43 +0100151 goto unlist_and_fail;
Petr Machatad7e4ca82012-11-28 03:38:47 +0100152 DICT_INIT(proc->breakpoints,
153 arch_addr_t, struct breakpoint *,
154 arch_addr_hash, arch_addr_eq, NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100155 } else {
156 proc->breakpoints = NULL;
157 }
158
Joe Damatoab3b72c2010-10-31 00:21:53 -0700159#if defined(HAVE_LIBUNWIND)
Petr Machataa2c270e2013-03-11 21:19:13 -0400160 if (options.bt_depth > 0) {
Petr Machataaf1e6032013-01-06 17:16:24 +0100161 proc->unwind_priv = _UPT_create(pid);
162 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700163
Petr Machataaf1e6032013-01-06 17:16:24 +0100164 if (proc->unwind_priv == NULL || proc->unwind_as == NULL) {
165 fprintf(stderr,
166 "Couldn't initialize unwinding "
167 "for process %d\n", proc->pid);
168 destroy_unwind(proc);
169 proc->unwind_priv = NULL;
170 proc->unwind_as = NULL;
171 }
172 }
Petr Machataa2c270e2013-03-11 21:19:13 -0400173#endif /* defined(HAVE_LIBUNWIND) */
Petr Machataaf1e6032013-01-06 17:16:24 +0100174
Mark Wielaarddfefa9f2014-01-07 21:00:44 +0100175#if defined(HAVE_LIBDW)
176 proc->dwfl = NULL; /* Initialize for leader only on first library. */
177#endif /* defined(HAVE_LIBDW) */
178
Petr Machata2b46cfc2012-02-18 11:17:29 +0100179 return 0;
180}
181
182static void
Petr Machata929bd572012-12-17 03:20:34 +0100183process_bare_destroy(struct process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100184{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100185 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
186 free(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200187 if (!was_exec) {
188 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200189 unlist_process(proc);
Petr Machatae677c7e2012-10-26 22:23:43 +0200190 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200191 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100192}
193
Petr Machata3d0c91c2012-04-14 02:37:38 +0200194static int
Petr Machata929bd572012-12-17 03:20:34 +0100195process_init_main(struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100196{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200197 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200198 fprintf(stderr, "failed to init breakpoints %d\n",
199 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200200 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200201 }
202
Petr Machata2b46cfc2012-02-18 11:17:29 +0100203 return 0;
204}
205
Petr Machata3d0c91c2012-04-14 02:37:38 +0200206int
Petr Machata929bd572012-12-17 03:20:34 +0100207process_init(struct process *proc, const char *filename, pid_t pid)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200208{
209 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200210 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200211 fprintf(stderr, "failed to initialize process %d: %s\n",
212 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200213 return -1;
214 }
215
Petr Machata0f6e6d92012-10-26 23:42:17 +0200216 if (os_process_init(proc) < 0) {
217 process_bare_destroy(proc, 0);
218 goto fail;
219 }
220
Petr Machata744f2552012-04-15 04:33:18 +0200221 if (arch_process_init(proc) < 0) {
Petr Machata0f6e6d92012-10-26 23:42:17 +0200222 os_process_destroy(proc);
Petr Machata744f2552012-04-15 04:33:18 +0200223 process_bare_destroy(proc, 0);
224 goto fail;
225 }
226
Petr Machata8137e802014-02-13 16:02:50 +0100227 if (proc->leader != proc) {
228 proc->e_machine = proc->leader->e_machine;
229 proc->e_class = proc->leader->e_class;
230 get_arch_dep(proc);
231 } else if (process_init_main(proc) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200232 process_bare_destroy(proc, 0);
233 goto fail;
234 }
235 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200236}
237
Petr Machata8ead1cd2012-04-24 18:13:09 +0200238static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100239destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200240{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200241 breakpoint_destroy(bp);
242 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200243 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200244}
245
Petr Machatae0e89ed2012-10-26 22:25:33 +0200246// XXX see comment in handle_event.c
Petr Machata929bd572012-12-17 03:20:34 +0100247void callstack_pop(struct process *proc);
Petr Machatae0e89ed2012-10-26 22:25:33 +0200248
Petr Machata3d0c91c2012-04-14 02:37:38 +0200249static void
Petr Machata929bd572012-12-17 03:20:34 +0100250private_process_destroy(struct process *proc, int was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200251{
Petr Machatae0e89ed2012-10-26 22:25:33 +0200252 /* Pop remaining stack elements. */
253 while (proc->callstack_depth > 0) {
254 /* When this is called just before a process is
255 * destroyed, the breakpoints should either have been
256 * retracted by now, or were killed by exec. In any
257 * case, it's safe to pretend that there are no
258 * breakpoints associated with the stack elements, so
259 * that stack_pop doesn't attempt to destroy them. */
260 size_t i = proc->callstack_depth - 1;
261 if (!proc->callstack[i].is_syscall)
262 proc->callstack[i].return_addr = 0;
263
264 callstack_pop(proc);
265 }
266
267 if (!was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200268 free(proc->filename);
269
Petr Machata8ead1cd2012-04-24 18:13:09 +0200270 /* Libraries and symbols. This is only relevant in
271 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200272 struct library *lib;
273 for (lib = proc->libraries; lib != NULL; ) {
274 struct library *next = lib->next;
275 library_destroy(lib);
276 free(lib);
277 lib = next;
278 }
279 proc->libraries = NULL;
280
281 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200282 if (proc->breakpoints != NULL) {
283 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100284 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
285 free(proc->breakpoints);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200286 proc->breakpoints = NULL;
287 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200288
289 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200290}
291
292void
Petr Machata929bd572012-12-17 03:20:34 +0100293process_destroy(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200294{
Petr Machata744f2552012-04-15 04:33:18 +0200295 arch_process_destroy(proc);
Petr Machata0f6e6d92012-10-26 23:42:17 +0200296 os_process_destroy(proc);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200297 private_process_destroy(proc, 0);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200298}
299
300int
Petr Machata929bd572012-12-17 03:20:34 +0100301process_exec(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200302{
Petr Machata0f6e6d92012-10-26 23:42:17 +0200303 /* Call exec handlers first, before we destroy the main
Petr Machata3cc0cd12012-10-26 22:30:51 +0200304 * state. */
Petr Machata0f6e6d92012-10-26 23:42:17 +0200305 if (arch_process_exec(proc) < 0
306 || os_process_exec(proc) < 0)
Petr Machata744f2552012-04-15 04:33:18 +0200307 return -1;
308
Petr Machata3d0c91c2012-04-14 02:37:38 +0200309 private_process_destroy(proc, 1);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200310
Petr Machata3d0c91c2012-04-14 02:37:38 +0200311 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
312 return -1;
313 if (process_init_main(proc) < 0) {
314 process_bare_destroy(proc, 1);
315 return -1;
316 }
317 return 0;
318}
319
Petr Machata929bd572012-12-17 03:20:34 +0100320struct process *
Petr Machata75934ad2012-04-14 02:28:03 +0200321open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100322{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100323 assert(pid != 0);
Petr Machata929bd572012-12-17 03:20:34 +0100324 struct process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200325 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200326 free(proc);
327 return NULL;
328 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100329 return proc;
330}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100331
Petr Machata2b46cfc2012-02-18 11:17:29 +0100332struct clone_single_bp_data {
Petr Machata929bd572012-12-17 03:20:34 +0100333 struct process *old_proc;
334 struct process *new_proc;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100335};
336
Petr Machatad7e4ca82012-11-28 03:38:47 +0100337static enum callback_status
338clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100339{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100340 struct breakpoint *bp = *bpp;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100341 struct clone_single_bp_data *data = u;
342
Petr Machata2b46cfc2012-02-18 11:17:29 +0100343 struct breakpoint *clone = malloc(sizeof(*clone));
344 if (clone == NULL
Petr Machatae5035522013-01-30 17:48:51 +0100345 || breakpoint_clone(clone, data->new_proc, bp) < 0) {
Petr Machatad3cc9882012-04-13 21:40:23 +0200346 fail:
347 free(clone);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100348 return CBS_STOP;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100349 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200350 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
351 breakpoint_destroy(clone);
352 goto fail;
353 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100354 return CBS_CONT;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100355}
356
357int
Petr Machata929bd572012-12-17 03:20:34 +0100358process_clone(struct process *retp, struct process *proc, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100359{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200360 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200361 fail1:
Petr Machataf56d1292013-01-07 21:27:54 +0100362 fprintf(stderr, "Failed to clone process %d to %d: %s\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200363 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100364 return -1;
365 }
366
Petr Machatacf1679a2012-04-06 19:56:17 +0200367 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200368 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400369 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200370
Petr Machata2b46cfc2012-02-18 11:17:29 +0100371 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200372 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100373 return 0;
374
375 /* Clone symbols first so that we can clone and relink
376 * breakpoints. */
377 struct library *lib;
378 struct library **nlibp = &retp->libraries;
Petr Machata1e339e02012-10-27 19:33:20 +0200379 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100380 *nlibp = malloc(sizeof(**nlibp));
Petr Machatad19b9162013-01-07 20:16:57 +0100381
Petr Machata2b46cfc2012-02-18 11:17:29 +0100382 if (*nlibp == NULL
383 || library_clone(*nlibp, lib) < 0) {
Petr Machatad19b9162013-01-07 20:16:57 +0100384 free(*nlibp);
385 *nlibp = NULL;
386
Petr Machata2b46cfc2012-02-18 11:17:29 +0100387 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200388 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100389
390 /* Error when cloning. Unroll what was done. */
391 for (lib = retp->libraries; lib != NULL; ) {
392 struct library *next = lib->next;
393 library_destroy(lib);
394 free(lib);
395 lib = next;
396 }
Petr Machataba1664b2012-04-28 14:59:05 +0200397 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100398 }
399
400 nlibp = &(*nlibp)->next;
401 }
402
403 /* Now clone breakpoints. Symbol relinking is done in
404 * clone_single_bp. */
405 struct clone_single_bp_data data = {
406 .old_proc = proc,
407 .new_proc = retp,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100408 };
Petr Machatad7e4ca82012-11-28 03:38:47 +0100409 if (DICT_EACH(proc->leader->breakpoints,
410 arch_addr_t, struct breakpoint *, NULL,
411 clone_single_bp, &data) != NULL)
Petr Machata94078ec2012-01-05 18:07:02 +0100412 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100413
Petr Machataded6f972012-04-13 23:15:48 +0200414 /* And finally the call stack. */
Petr Machatab6de8412012-10-27 19:31:22 +0200415 /* XXX clearly the callstack handling should be moved to a
416 * separate module and this whole business extracted to
417 * callstack_clone, or callstack_element_clone. */
Petr Machataded6f972012-04-13 23:15:48 +0200418 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
419 retp->callstack_depth = proc->callstack_depth;
420
Petr Machata94078ec2012-01-05 18:07:02 +0100421 size_t i;
422 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machatab6de8412012-10-27 19:31:22 +0200423 struct callstack_element *elem = &retp->callstack[i];
424 struct fetch_context *ctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100425 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200426 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100427 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200428 size_t j;
429 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100430 for (j = 0; j < i; ++j) {
Petr Machataf56d1292013-01-07 21:27:54 +0100431 nctx = retp->callstack[j].fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100432 fetch_arg_done(nctx);
Petr Machatab6de8412012-10-27 19:31:22 +0200433 elem->fetch_context = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100434 }
435 goto fail2;
436 }
Petr Machatab6de8412012-10-27 19:31:22 +0200437 elem->fetch_context = nctx;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100438 }
439
Petr Machataf56d1292013-01-07 21:27:54 +0100440 if (elem->arguments != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100441 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100442 if (nargs == NULL
Petr Machataf56d1292013-01-07 21:27:54 +0100443 || val_dict_clone(nargs, elem->arguments) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200444 size_t j;
Petr Machata94078ec2012-01-05 18:07:02 +0100445 for (j = 0; j < i; ++j) {
Petr Machataf56d1292013-01-07 21:27:54 +0100446 nargs = retp->callstack[j].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100447 val_dict_destroy(nargs);
448 free(nargs);
Petr Machatab6de8412012-10-27 19:31:22 +0200449 elem->arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100450 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100451
452 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200453 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100454 * fetch_context. */
455 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200456 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100457 }
Petr Machatab6de8412012-10-27 19:31:22 +0200458 elem->arguments = nargs;
Petr Machata94078ec2012-01-05 18:07:02 +0100459 }
Petr Machata165b5662012-10-27 19:23:12 +0200460
461 /* If it's not a syscall, we need to find the
462 * corresponding library symbol in the cloned
463 * library. */
464 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
465 struct library_symbol *libfunc = elem->c_un.libfunc;
466 int rc = proc_find_symbol(retp, libfunc,
467 NULL, &elem->c_un.libfunc);
468 assert(rc == 0);
469 }
Petr Machata94078ec2012-01-05 18:07:02 +0100470 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100471
Petr Machata5bf47142012-10-27 19:29:00 +0200472 /* At this point, retp is fully initialized, except for OS and
473 * arch parts, and we can call private_process_destroy. */
474 if (os_process_clone(retp, proc) < 0) {
475 private_process_destroy(retp, 0);
476 return -1;
477 }
478 if (arch_process_clone(retp, proc) < 0) {
479 os_process_destroy(retp);
480 private_process_destroy(retp, 0);
481 return -1;
482 }
Petr Machata744f2552012-04-15 04:33:18 +0200483
Petr Machata2b46cfc2012-02-18 11:17:29 +0100484 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100485}
486
Petr Machata3c516d52011-08-18 03:53:18 +0200487static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200488open_one_pid(pid_t pid)
489{
Petr Machata9a5420c2011-07-09 11:21:23 +0200490 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
491
Petr Machata1974dbc2011-08-19 18:58:01 +0200492 /* Get the filename first. Should the trace_pid fail, we can
493 * easily free it, untracing is more work. */
Petr Machata929bd572012-12-17 03:20:34 +0100494 char *filename = pid2name(pid);
495 if (filename == NULL || trace_pid(pid) < 0) {
Petr Machataef0c74d2012-10-27 00:30:57 +0200496 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200497 free(filename);
498 return -1;
499 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100500
Petr Machata929bd572012-12-17 03:20:34 +0100501 struct process *proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200502 if (proc == NULL)
Petr Machataef0c74d2012-10-27 00:30:57 +0200503 goto fail;
504 free(filename);
Petr Machata3ed2a422012-04-06 17:18:55 +0200505 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200506
Petr Machata1974dbc2011-08-19 18:58:01 +0200507 return 0;
508}
509
Petr Machata2b46cfc2012-02-18 11:17:29 +0100510static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100511start_one_pid(struct process *proc, void *data)
Petr Machata1974dbc2011-08-19 18:58:01 +0200512{
513 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100514 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100515}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100516
Petr Machatadf2c88c2013-03-19 17:55:25 +0100517static enum callback_status
518is_main(struct process *proc, struct library *lib, void *data)
519{
520 return CBS_STOP_IF(lib->type == LT_LIBTYPE_MAIN);
521}
522
523void
524process_hit_start(struct process *proc)
525{
526 struct process *leader = proc->leader;
527 assert(leader != NULL);
528
529 struct library *mainlib
530 = proc_each_library(leader, NULL, is_main, NULL);
531 assert(mainlib != NULL);
532 linkmap_init(leader, mainlib->dyn_addr);
533 arch_dynlink_done(leader);
534}
535
Petr Machata9a5420c2011-07-09 11:21:23 +0200536void
537open_pid(pid_t pid)
538{
539 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200540 /* If we are already tracing this guy, we should be seeing all
541 * his children via normal tracing route. */
542 if (pid2proc(pid) != NULL)
543 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200544
Petr Machata3c516d52011-08-18 03:53:18 +0200545 /* First, see if we can attach the requested PID itself. */
Petr Machata8be68ff2013-03-19 18:00:58 +0100546 if (open_one_pid(pid) < 0) {
Petr Machata3c516d52011-08-18 03:53:18 +0200547 fprintf(stderr, "Cannot attach to pid %u: %s\n",
548 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200549 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200550 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200551 }
552
Petr Machata3c516d52011-08-18 03:53:18 +0200553 /* Now attach to all tasks that belong to that PID. There's a
554 * race between process_tasks and open_one_pid. So when we
555 * fail in open_one_pid below, we just do another round.
556 * Chances are that by then that PID will have gone away, and
557 * that's why we have seen the failure. The processes that we
558 * manage to open_one_pid are stopped, so we should eventually
559 * reach a point where process_tasks doesn't give any new
560 * processes (because there's nobody left to produce
561 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200562 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200563 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200564 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200565 pid_t *tasks;
566 size_t ntasks;
567 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200568
Petr Machata3c516d52011-08-18 03:53:18 +0200569 if (process_tasks(pid, &tasks, &ntasks) < 0) {
570 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
571 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100572 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200573 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200574
Petr Machata3c516d52011-08-18 03:53:18 +0200575 have_all = 1;
576 for (i = 0; i < ntasks; ++i)
577 if (pid2proc(tasks[i]) == NULL
Petr Machata8be68ff2013-03-19 18:00:58 +0100578 && open_one_pid(tasks[i]) < 0)
Petr Machata3c516d52011-08-18 03:53:18 +0200579 have_all = 0;
580
Petr Machata9a5420c2011-07-09 11:21:23 +0200581 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200582
Petr Machata1974dbc2011-08-19 18:58:01 +0200583 if (have_all && old_ntasks == ntasks)
584 break;
585 old_ntasks = ntasks;
586 }
587
Petr Machata929bd572012-12-17 03:20:34 +0100588 struct process *leader = pid2proc(pid)->leader;
Petr Machata93d95df2012-04-17 05:16:19 +0200589
590 /* XXX Is there a way to figure out whether _start has
591 * actually already been hit? */
Petr Machatadf2c88c2013-03-19 17:55:25 +0100592 process_hit_start(leader);
Petr Machata93d95df2012-04-17 05:16:19 +0200593
Petr Machata2f9b78e2012-04-16 21:08:54 +0200594 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200595 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200596}
597
Petr Machata2b46cfc2012-02-18 11:17:29 +0100598static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100599find_proc(struct process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200600{
Petr Machata7725d4b2013-03-19 18:03:00 +0100601 return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data);
Petr Machatacebb8842011-07-09 11:14:11 +0200602}
603
Petr Machata929bd572012-12-17 03:20:34 +0100604struct process *
605pid2proc(pid_t pid)
606{
Petr Machatacebb8842011-07-09 11:14:11 +0200607 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
608}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100609
Petr Machata929bd572012-12-17 03:20:34 +0100610static struct process *list_of_processes = NULL;
Petr Machatacebb8842011-07-09 11:14:11 +0200611
Petr Machatacbe29c62011-09-27 02:27:58 +0200612static void
Petr Machata929bd572012-12-17 03:20:34 +0100613unlist_process(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +0200614{
Petr Machatacbe29c62011-09-27 02:27:58 +0200615 if (list_of_processes == proc) {
616 list_of_processes = list_of_processes->next;
617 return;
618 }
619
Petr Machata929bd572012-12-17 03:20:34 +0100620 struct process *tmp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200621 for (tmp = list_of_processes; ; tmp = tmp->next) {
622 /* If the following assert fails, the process wasn't
623 * in the list. */
624 assert(tmp->next != NULL);
625
626 if (tmp->next == proc) {
627 tmp->next = tmp->next->next;
628 return;
629 }
630 }
631}
632
Petr Machata929bd572012-12-17 03:20:34 +0100633struct process *
634each_process(struct process *start_after,
635 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100636 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200637{
Petr Machata929bd572012-12-17 03:20:34 +0100638 struct process *it = start_after == NULL ? list_of_processes
Petr Machata74132a42012-03-16 02:46:18 +0100639 : start_after->next;
640
641 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200642 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100643 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100644 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200645 case CBS_FAIL:
646 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100647 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200648 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100649 case CBS_CONT:
650 break;
651 }
Petr Machatacebb8842011-07-09 11:14:11 +0200652 it = next;
653 }
654 return NULL;
655}
Petr Machata9a5420c2011-07-09 11:21:23 +0200656
Petr Machata929bd572012-12-17 03:20:34 +0100657struct process *
658each_task(struct process *proc, struct process *start_after,
659 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100660 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200661{
Petr Machata74132a42012-03-16 02:46:18 +0100662 assert(proc != NULL);
Petr Machata929bd572012-12-17 03:20:34 +0100663 struct process *it = start_after == NULL ? proc->leader
Petr Machata74132a42012-03-16 02:46:18 +0100664 : start_after->next;
665
Petr Machata9a5420c2011-07-09 11:21:23 +0200666 if (it != NULL) {
Petr Machata929bd572012-12-17 03:20:34 +0100667 struct process *leader = it->leader;
Petr Machata74132a42012-03-16 02:46:18 +0100668 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200669 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100670 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100671 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200672 case CBS_FAIL:
673 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100674 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200675 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100676 case CBS_CONT:
677 break;
678 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200679 it = next;
680 }
681 }
682 return NULL;
683}
684
Petr Machatabaf00e42013-01-07 19:44:43 +0100685static int
Petr Machata929bd572012-12-17 03:20:34 +0100686add_process(struct process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200687{
Petr Machata929bd572012-12-17 03:20:34 +0100688 struct process **leaderp = &list_of_processes;
Petr Machata9a5420c2011-07-09 11:21:23 +0200689 if (proc->pid) {
690 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200691 if (tgid == 0)
692 /* Must have been terminated before we managed
693 * to fully attach. */
Petr Machatabaf00e42013-01-07 19:44:43 +0100694 return -1;
Petr Machata929bd572012-12-17 03:20:34 +0100695 if (tgid == proc->pid) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200696 proc->leader = proc;
Petr Machata929bd572012-12-17 03:20:34 +0100697 } else {
698 struct process *leader = pid2proc(tgid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200699 proc->leader = leader;
700 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200701 leaderp = &leader->next;
702 }
703 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200704
705 if (!was_exec) {
706 proc->next = *leaderp;
707 *leaderp = proc;
708 }
Petr Machatabaf00e42013-01-07 19:44:43 +0100709 return 0;
Petr Machata9a5420c2011-07-09 11:21:23 +0200710}
711
Petr Machatacbe29c62011-09-27 02:27:58 +0200712void
Petr Machata929bd572012-12-17 03:20:34 +0100713change_process_leader(struct process *proc, struct process *leader)
Petr Machatacbe29c62011-09-27 02:27:58 +0200714{
Petr Machata929bd572012-12-17 03:20:34 +0100715 struct process **leaderp = &list_of_processes;
Petr Machatacbe29c62011-09-27 02:27:58 +0200716 if (proc->leader == leader)
717 return;
718
719 assert(leader != NULL);
720 unlist_process(proc);
721 if (proc != leader)
722 leaderp = &leader->next;
723
724 proc->leader = leader;
725 proc->next = *leaderp;
726 *leaderp = proc;
727}
728
Petr Machata2b46cfc2012-02-18 11:17:29 +0100729static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100730clear_leader(struct process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200731{
732 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
733 proc->pid, proc->leader->pid);
734 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100735 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200736}
737
738void
Petr Machata929bd572012-12-17 03:20:34 +0100739remove_process(struct process *proc)
Petr Machatacebb8842011-07-09 11:14:11 +0200740{
Petr Machatacebb8842011-07-09 11:14:11 +0200741 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
742
Petr Machata9a5420c2011-07-09 11:21:23 +0200743 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100744 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200745
Petr Machatacbe29c62011-09-27 02:27:58 +0200746 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100747 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200748 process_destroy(proc);
749 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100750}
Petr Machata4007d742011-07-09 11:29:42 +0200751
752void
Petr Machata929bd572012-12-17 03:20:34 +0100753install_event_handler(struct process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200754{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200755 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200756 assert(proc->event_handler == NULL);
757 proc->event_handler = handler;
758}
759
760void
Petr Machata929bd572012-12-17 03:20:34 +0100761destroy_event_handler(struct process *proc)
Petr Machata4007d742011-07-09 11:29:42 +0200762{
Petr Machata366c2f42012-02-09 19:34:36 +0100763 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200764 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200765 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200766 if (handler->destroy != NULL)
767 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200768 free(handler);
769 proc->event_handler = NULL;
770}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100771
Petr Machataef2fd272012-09-28 00:43:01 +0200772static int
Petr Machata929bd572012-12-17 03:20:34 +0100773breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100774{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200775 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200776 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100777
Petr Machataef2fd272012-09-28 00:43:01 +0200778 /* Don't enable latent or delayed symbols. */
Petr Machata96f04822012-10-31 03:29:11 +0100779 if (libsym->latent || libsym->delayed) {
780 debug(DEBUG_FUNCTION,
781 "delayed and/or latent breakpoint pid=%d, %s@%p",
782 proc->pid, libsym->name, libsym->enter_addr);
Petr Machataef2fd272012-09-28 00:43:01 +0200783 return 0;
Petr Machata96f04822012-10-31 03:29:11 +0100784 }
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200785
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200786 bp_addr = sym2addr(proc, libsym);
787
Petr Machatad5e85562012-04-05 15:18:38 +0200788 /* If there is an artificial breakpoint on the same address,
789 * its libsym will be NULL, and we can smuggle our libsym
790 * there. That artificial breakpoint is there presumably for
791 * the callbacks, which we don't touch. If there is a real
792 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200793 * symbols and ignore extra symbol aliases.
794 *
795 * The other direction is more complicated and currently not
796 * supported. If a breakpoint has custom callbacks, it might
797 * be also custom-allocated, and we would really need to swap
798 * the two: delete the one now in the dictionary, swap values
799 * around, and put the new breakpoint back in. */
Petr Machata98ff3092013-03-08 22:11:36 +0100800 struct breakpoint *bp;
801 if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
Petr Machata8d58d8b2012-11-12 12:46:03 +0100802 /* MIPS backend makes duplicate requests. This is
803 * likely a bug in the backend. Currently there's no
804 * point assigning more than one symbol to a
805 * breakpoint, because when it hits, we won't know
806 * what to print out. But it's easier to fix it here
807 * before someone who understands MIPS has the time to
808 * look into it. So turn the sanity check off on
809 * MIPS. References:
810 *
811 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
812 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
813 */
814#ifndef __mips__
Petr Machata98ff3092013-03-08 22:11:36 +0100815 assert(bp->libsym == NULL);
816 bp->libsym = libsym;
Petr Machata8d58d8b2012-11-12 12:46:03 +0100817#endif
Petr Machataef2fd272012-09-28 00:43:01 +0200818 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200819 }
820
Petr Machata98ff3092013-03-08 22:11:36 +0100821 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200822 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200823 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200824 fail:
825 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200826 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200827 }
828 if (proc_add_breakpoint(proc, bp) < 0) {
829 breakpoint_destroy(bp);
830 goto fail;
831 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100832
Petr Machatafa0c5702012-04-13 18:43:40 +0200833 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200834 proc_remove_breakpoint(proc, bp);
835 breakpoint_destroy(bp);
836 goto fail;
837 }
838
Petr Machataef2fd272012-09-28 00:43:01 +0200839 return 0;
840}
841
842static enum callback_status
843cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
844{
Petr Machata7725d4b2013-03-19 18:03:00 +0100845 return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0);
Petr Machataef2fd272012-09-28 00:43:01 +0200846}
847
848static int
Petr Machata929bd572012-12-17 03:20:34 +0100849proc_activate_latent_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200850 struct library_symbol *libsym)
851{
852 assert(libsym->latent);
853 libsym->latent = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100854 debug(DEBUG_FUNCTION, "activated latent symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200855 return breakpoint_for_symbol(libsym, proc);
856}
857
858int
Petr Machata929bd572012-12-17 03:20:34 +0100859proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200860 struct library_symbol *libsym)
861{
862 assert(libsym->delayed);
863 libsym->delayed = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100864 debug(DEBUG_FUNCTION, "activated delayed symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200865 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100866}
867
Petr Machataa1f76832012-09-28 00:08:00 +0200868static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100869activate_latent_in(struct process *proc, struct library *lib, void *data)
Petr Machataa1f76832012-09-28 00:08:00 +0200870{
871 struct library_exported_name *exported;
872 for (exported = data; exported != NULL; exported = exported->next) {
873 struct library_symbol *libsym = NULL;
874 while ((libsym = library_each_symbol(lib, libsym,
875 library_symbol_named_cb,
876 (void *)exported->name))
877 != NULL)
878 if (libsym->latent
879 && proc_activate_latent_symbol(proc, libsym) < 0)
880 return CBS_FAIL;
881 }
882 return CBS_CONT;
883}
884
Petr Machata2b46cfc2012-02-18 11:17:29 +0100885void
Petr Machata929bd572012-12-17 03:20:34 +0100886proc_add_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100887{
888 assert(lib->next == NULL);
889 lib->next = proc->libraries;
890 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200891 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
892 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100893
Mark Wielaarddfefa9f2014-01-07 21:00:44 +0100894#if defined(HAVE_LIBDW)
895 if (options.bt_depth > 0) {
896 /* Setup module tracking for libdwfl unwinding. */
897 struct process *leader = proc->leader;
898 Dwfl *dwfl = leader->dwfl;
899 if (dwfl == NULL) {
900 static const Dwfl_Callbacks proc_callbacks = {
901 .find_elf = dwfl_linux_proc_find_elf,
902 .find_debuginfo = dwfl_standard_find_debuginfo
903 };
904 dwfl = dwfl_begin(&proc_callbacks);
905 if (dwfl == NULL)
906 fprintf(stderr,
907 "Couldn't initialize libdwfl unwinding "
908 "for process %d: %s\n", leader->pid,
909 dwfl_errmsg (-1));
910 }
911
912 if (dwfl != NULL) {
913 dwfl_report_begin_add(dwfl);
914 if (dwfl_report_elf(dwfl, lib->soname,
915 lib->pathname, -1,
916 (GElf_Addr) lib->base,
917 false) == NULL)
918 fprintf(stderr,
919 "dwfl_report_elf %s@%p (%s) %d: %s\n",
920 lib->soname, lib->base, lib->pathname,
921 proc->pid, dwfl_errmsg (-1));
922 dwfl_report_end(dwfl, NULL, NULL);
923
924 if (leader->dwfl == NULL) {
925 int r = dwfl_linux_proc_attach(dwfl,
926 leader->pid,
927 true);
928 if (r == 0)
929 leader->dwfl = dwfl;
930 else {
931 const char *msg;
932 dwfl_end(dwfl);
933 if (r < 0)
934 msg = dwfl_errmsg(-1);
935 else
936 msg = strerror(r);
937 fprintf(stderr, "Couldn't initialize "
938 "libdwfl unwinding for "
939 "process %d: %s\n",
940 leader->pid, msg);
941 }
942 }
943 }
944 }
945#endif /* defined(HAVE_LIBDW) */
946
Petr Machataef2fd272012-09-28 00:43:01 +0200947 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100948 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200949 while ((libsym = library_each_symbol(lib, libsym,
950 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100951 proc)) != NULL)
Petr Machata3717f292013-01-06 17:52:00 +0100952 fprintf(stderr,
953 "Couldn't insert breakpoint for %s to %d: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200954 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200955
956 /* Look through export list of the new library and compare it
957 * with latent symbols of all libraries (including this
958 * library itself). */
959 struct library *lib2 = NULL;
960 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
961 lib->exported_names)) != NULL)
962 fprintf(stderr,
Petr Machata3717f292013-01-06 17:52:00 +0100963 "Couldn't activate latent symbols for %s in %d: %s.\n",
Petr Machata5c5d4802013-01-08 23:21:10 +0100964 lib2->soname, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100965}
966
967int
Petr Machata929bd572012-12-17 03:20:34 +0100968proc_remove_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100969{
970 struct library **libp;
971 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
972 if (*libp == lib) {
973 *libp = lib->next;
974 return 0;
975 }
976 return -1;
977}
978
979struct library *
Petr Machata929bd572012-12-17 03:20:34 +0100980proc_each_library(struct process *proc, struct library *it,
981 enum callback_status (*cb)(struct process *proc,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100982 struct library *lib, void *data),
983 void *data)
984{
985 if (it == NULL)
986 it = proc->libraries;
Petr Machatac4763a02013-01-08 23:19:14 +0100987 else
988 it = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100989
990 while (it != NULL) {
991 struct library *next = it->next;
992
993 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200994 case CBS_FAIL:
995 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100996 case CBS_STOP:
997 return it;
998 case CBS_CONT:
999 break;
1000 }
1001
1002 it = next;
1003 }
1004
1005 return NULL;
1006}
Petr Machata52dbfb12012-03-29 16:38:26 +02001007
Petr Machataf7fee432012-04-19 17:00:53 +02001008static void
Petr Machata929bd572012-12-17 03:20:34 +01001009check_leader(struct process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +02001010{
Petr Machata52dbfb12012-03-29 16:38:26 +02001011 /* Only the group leader should be getting the breakpoints and
1012 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +02001013 assert(proc->leader != NULL);
1014 assert(proc->leader == proc);
1015 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +02001016}
Petr Machata52dbfb12012-03-29 16:38:26 +02001017
Petr Machataf7fee432012-04-19 17:00:53 +02001018int
Petr Machata929bd572012-12-17 03:20:34 +01001019proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machataf7fee432012-04-19 17:00:53 +02001020{
Petr Machatafa0c5702012-04-13 18:43:40 +02001021 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +02001022 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +02001023 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +02001024
Petr Machataa2416362012-04-06 02:43:34 +02001025 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +02001026 * assert, but that's not necessary right now. Read the
1027 * comment in breakpoint_for_symbol. */
Petr Machatad7e4ca82012-11-28 03:38:47 +01001028 assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +02001029
Petr Machatad7e4ca82012-11-28 03:38:47 +01001030 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +02001031 fprintf(stderr,
1032 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
1033 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +02001034 return -1;
1035 }
1036
Petr Machata52dbfb12012-03-29 16:38:26 +02001037 return 0;
1038}
1039
Petr Machataf7fee432012-04-19 17:00:53 +02001040void
Petr Machata929bd572012-12-17 03:20:34 +01001041proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machata52dbfb12012-03-29 16:38:26 +02001042{
Petr Machataf7fee432012-04-19 17:00:53 +02001043 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
1044 proc->pid, breakpoint_name(bp), bp->addr);
1045 check_leader(proc);
Petr Machatad7e4ca82012-11-28 03:38:47 +01001046 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
1047 NULL, NULL, NULL);
1048 assert(rc == 0);
Petr Machata52dbfb12012-03-29 16:38:26 +02001049}
Petr Machatad3cc9882012-04-13 21:40:23 +02001050
Petr Machatad3cc9882012-04-13 21:40:23 +02001051struct each_breakpoint_data
1052{
Petr Machata929bd572012-12-17 03:20:34 +01001053 struct process *proc;
1054 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +02001055 struct breakpoint *bp,
1056 void *data);
1057 void *cb_data;
1058};
1059
Petr Machatad7e4ca82012-11-28 03:38:47 +01001060static enum callback_status
1061each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
Petr Machatad3cc9882012-04-13 21:40:23 +02001062{
1063 struct each_breakpoint_data *data = d;
Petr Machatad7e4ca82012-11-28 03:38:47 +01001064 return data->cb(data->proc, *bpp, data->cb_data);
Petr Machatad3cc9882012-04-13 21:40:23 +02001065}
1066
Petr Machata6bcc0922014-01-09 23:50:07 +01001067arch_addr_t *
1068proc_each_breakpoint(struct process *proc, arch_addr_t *start,
Petr Machata929bd572012-12-17 03:20:34 +01001069 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +02001070 struct breakpoint *bp,
1071 void *data), void *data)
1072{
1073 struct each_breakpoint_data dd = {
Petr Machatad3cc9882012-04-13 21:40:23 +02001074 .proc = proc,
1075 .cb = cb,
1076 .cb_data = data,
1077 };
Petr Machatad7e4ca82012-11-28 03:38:47 +01001078 return DICT_EACH(proc->breakpoints,
1079 arch_addr_t, struct breakpoint *, start,
1080 &each_breakpoint_cb, &dd);
Petr Machatad3cc9882012-04-13 21:40:23 +02001081}
Petr Machata165b5662012-10-27 19:23:12 +02001082
1083int
Petr Machata929bd572012-12-17 03:20:34 +01001084proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +02001085 struct library **retlib, struct library_symbol **retsym)
1086{
1087 struct library *lib = sym->lib;
1088 assert(lib != NULL);
1089
1090 struct library *flib
1091 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1092 if (flib == NULL)
1093 return -1;
1094
1095 struct library_symbol *fsym
1096 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1097 (char *)sym->name);
1098 if (fsym == NULL)
1099 return -1;
1100
1101 if (retlib != NULL)
1102 *retlib = flib;
1103 if (retsym != NULL)
1104 *retsym = fsym;
1105
1106 return 0;
1107}
Petr Machata32405542012-10-31 03:28:39 +01001108
1109struct library_symbol *
Petr Machata929bd572012-12-17 03:20:34 +01001110proc_each_symbol(struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +01001111 enum callback_status (*cb)(struct library_symbol *, void *),
1112 void *data)
1113{
1114 struct library *lib;
Petr Machata32405542012-10-31 03:28:39 +01001115 for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1116 lib != NULL; lib = lib->next) {
1117 start_after = library_each_symbol(lib, start_after, cb, data);
1118 if (start_after != NULL)
1119 return start_after;
1120 }
1121
1122 return NULL;
1123}
Petr Machata653085a2013-01-15 17:40:40 +01001124
1125#define DEF_READER(NAME, SIZE) \
1126 int \
1127 NAME(struct process *proc, arch_addr_t addr, \
1128 uint##SIZE##_t *lp) \
1129 { \
1130 union { \
1131 uint##SIZE##_t dst; \
1132 char buf[0]; \
1133 } u; \
1134 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \
1135 != sizeof(u.dst)) \
1136 return -1; \
1137 *lp = u.dst; \
1138 return 0; \
1139 }
1140
Petr Machatadc70e762013-01-23 00:02:26 +01001141DEF_READER(proc_read_8, 8)
Petr Machata653085a2013-01-15 17:40:40 +01001142DEF_READER(proc_read_16, 16)
1143DEF_READER(proc_read_32, 32)
1144DEF_READER(proc_read_64, 64)
1145
1146#undef DEF_READER