blob: 6f4f64ef1c7abc284a6e8e561bdc2e319d81646b [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 Machata218c5ff2012-04-15 04:22:39 +0200227 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200228 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200229 if (process_init_main(proc) < 0) {
230 process_bare_destroy(proc, 0);
231 goto fail;
232 }
233 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200234}
235
Petr Machata8ead1cd2012-04-24 18:13:09 +0200236static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100237destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200238{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200239 breakpoint_destroy(bp);
240 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200241 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200242}
243
Petr Machatae0e89ed2012-10-26 22:25:33 +0200244// XXX see comment in handle_event.c
Petr Machata929bd572012-12-17 03:20:34 +0100245void callstack_pop(struct process *proc);
Petr Machatae0e89ed2012-10-26 22:25:33 +0200246
Petr Machata3d0c91c2012-04-14 02:37:38 +0200247static void
Petr Machata929bd572012-12-17 03:20:34 +0100248private_process_destroy(struct process *proc, int was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200249{
Petr Machatae0e89ed2012-10-26 22:25:33 +0200250 /* Pop remaining stack elements. */
251 while (proc->callstack_depth > 0) {
252 /* When this is called just before a process is
253 * destroyed, the breakpoints should either have been
254 * retracted by now, or were killed by exec. In any
255 * case, it's safe to pretend that there are no
256 * breakpoints associated with the stack elements, so
257 * that stack_pop doesn't attempt to destroy them. */
258 size_t i = proc->callstack_depth - 1;
259 if (!proc->callstack[i].is_syscall)
260 proc->callstack[i].return_addr = 0;
261
262 callstack_pop(proc);
263 }
264
265 if (!was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200266 free(proc->filename);
267
Petr Machata8ead1cd2012-04-24 18:13:09 +0200268 /* Libraries and symbols. This is only relevant in
269 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200270 struct library *lib;
271 for (lib = proc->libraries; lib != NULL; ) {
272 struct library *next = lib->next;
273 library_destroy(lib);
274 free(lib);
275 lib = next;
276 }
277 proc->libraries = NULL;
278
279 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200280 if (proc->breakpoints != NULL) {
281 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100282 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
283 free(proc->breakpoints);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200284 proc->breakpoints = NULL;
285 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200286
287 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200288}
289
290void
Petr Machata929bd572012-12-17 03:20:34 +0100291process_destroy(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200292{
Petr Machata744f2552012-04-15 04:33:18 +0200293 arch_process_destroy(proc);
Petr Machata0f6e6d92012-10-26 23:42:17 +0200294 os_process_destroy(proc);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200295 private_process_destroy(proc, 0);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200296}
297
298int
Petr Machata929bd572012-12-17 03:20:34 +0100299process_exec(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200300{
Petr Machata0f6e6d92012-10-26 23:42:17 +0200301 /* Call exec handlers first, before we destroy the main
Petr Machata3cc0cd12012-10-26 22:30:51 +0200302 * state. */
Petr Machata0f6e6d92012-10-26 23:42:17 +0200303 if (arch_process_exec(proc) < 0
304 || os_process_exec(proc) < 0)
Petr Machata744f2552012-04-15 04:33:18 +0200305 return -1;
306
Petr Machata3d0c91c2012-04-14 02:37:38 +0200307 private_process_destroy(proc, 1);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200308
Petr Machata3d0c91c2012-04-14 02:37:38 +0200309 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
310 return -1;
311 if (process_init_main(proc) < 0) {
312 process_bare_destroy(proc, 1);
313 return -1;
314 }
315 return 0;
316}
317
Petr Machata929bd572012-12-17 03:20:34 +0100318struct process *
Petr Machata75934ad2012-04-14 02:28:03 +0200319open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100320{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100321 assert(pid != 0);
Petr Machata929bd572012-12-17 03:20:34 +0100322 struct process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200323 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200324 free(proc);
325 return NULL;
326 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100327 return proc;
328}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100329
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330struct clone_single_bp_data {
Petr Machata929bd572012-12-17 03:20:34 +0100331 struct process *old_proc;
332 struct process *new_proc;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333};
334
Petr Machatad7e4ca82012-11-28 03:38:47 +0100335static enum callback_status
336clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100337{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100338 struct breakpoint *bp = *bpp;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100339 struct clone_single_bp_data *data = u;
340
Petr Machata2b46cfc2012-02-18 11:17:29 +0100341 struct breakpoint *clone = malloc(sizeof(*clone));
342 if (clone == NULL
Petr Machatae5035522013-01-30 17:48:51 +0100343 || breakpoint_clone(clone, data->new_proc, bp) < 0) {
Petr Machatad3cc9882012-04-13 21:40:23 +0200344 fail:
345 free(clone);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100346 return CBS_STOP;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100347 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200348 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
349 breakpoint_destroy(clone);
350 goto fail;
351 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100352 return CBS_CONT;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100353}
354
355int
Petr Machata929bd572012-12-17 03:20:34 +0100356process_clone(struct process *retp, struct process *proc, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100357{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200358 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200359 fail1:
Petr Machataf56d1292013-01-07 21:27:54 +0100360 fprintf(stderr, "Failed to clone process %d to %d: %s\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200361 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100362 return -1;
363 }
364
Petr Machatacf1679a2012-04-06 19:56:17 +0200365 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200366 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400367 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200368
Petr Machata2b46cfc2012-02-18 11:17:29 +0100369 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200370 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100371 return 0;
372
373 /* Clone symbols first so that we can clone and relink
374 * breakpoints. */
375 struct library *lib;
376 struct library **nlibp = &retp->libraries;
Petr Machata1e339e02012-10-27 19:33:20 +0200377 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100378 *nlibp = malloc(sizeof(**nlibp));
Petr Machatad19b9162013-01-07 20:16:57 +0100379
Petr Machata2b46cfc2012-02-18 11:17:29 +0100380 if (*nlibp == NULL
381 || library_clone(*nlibp, lib) < 0) {
Petr Machatad19b9162013-01-07 20:16:57 +0100382 free(*nlibp);
383 *nlibp = NULL;
384
Petr Machata2b46cfc2012-02-18 11:17:29 +0100385 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200386 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100387
388 /* Error when cloning. Unroll what was done. */
389 for (lib = retp->libraries; lib != NULL; ) {
390 struct library *next = lib->next;
391 library_destroy(lib);
392 free(lib);
393 lib = next;
394 }
Petr Machataba1664b2012-04-28 14:59:05 +0200395 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100396 }
397
398 nlibp = &(*nlibp)->next;
399 }
400
401 /* Now clone breakpoints. Symbol relinking is done in
402 * clone_single_bp. */
403 struct clone_single_bp_data data = {
404 .old_proc = proc,
405 .new_proc = retp,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100406 };
Petr Machatad7e4ca82012-11-28 03:38:47 +0100407 if (DICT_EACH(proc->leader->breakpoints,
408 arch_addr_t, struct breakpoint *, NULL,
409 clone_single_bp, &data) != NULL)
Petr Machata94078ec2012-01-05 18:07:02 +0100410 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100411
Petr Machataded6f972012-04-13 23:15:48 +0200412 /* And finally the call stack. */
Petr Machatab6de8412012-10-27 19:31:22 +0200413 /* XXX clearly the callstack handling should be moved to a
414 * separate module and this whole business extracted to
415 * callstack_clone, or callstack_element_clone. */
Petr Machataded6f972012-04-13 23:15:48 +0200416 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
417 retp->callstack_depth = proc->callstack_depth;
418
Petr Machata94078ec2012-01-05 18:07:02 +0100419 size_t i;
420 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machatab6de8412012-10-27 19:31:22 +0200421 struct callstack_element *elem = &retp->callstack[i];
422 struct fetch_context *ctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100423 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200424 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100425 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200426 size_t j;
427 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100428 for (j = 0; j < i; ++j) {
Petr Machataf56d1292013-01-07 21:27:54 +0100429 nctx = retp->callstack[j].fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100430 fetch_arg_done(nctx);
Petr Machatab6de8412012-10-27 19:31:22 +0200431 elem->fetch_context = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100432 }
433 goto fail2;
434 }
Petr Machatab6de8412012-10-27 19:31:22 +0200435 elem->fetch_context = nctx;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100436 }
437
Petr Machataf56d1292013-01-07 21:27:54 +0100438 if (elem->arguments != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100439 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100440 if (nargs == NULL
Petr Machataf56d1292013-01-07 21:27:54 +0100441 || val_dict_clone(nargs, elem->arguments) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200442 size_t j;
Petr Machata94078ec2012-01-05 18:07:02 +0100443 for (j = 0; j < i; ++j) {
Petr Machataf56d1292013-01-07 21:27:54 +0100444 nargs = retp->callstack[j].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100445 val_dict_destroy(nargs);
446 free(nargs);
Petr Machatab6de8412012-10-27 19:31:22 +0200447 elem->arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100448 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100449
450 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200451 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100452 * fetch_context. */
453 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200454 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100455 }
Petr Machatab6de8412012-10-27 19:31:22 +0200456 elem->arguments = nargs;
Petr Machata94078ec2012-01-05 18:07:02 +0100457 }
Petr Machata165b5662012-10-27 19:23:12 +0200458
459 /* If it's not a syscall, we need to find the
460 * corresponding library symbol in the cloned
461 * library. */
462 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
463 struct library_symbol *libfunc = elem->c_un.libfunc;
464 int rc = proc_find_symbol(retp, libfunc,
465 NULL, &elem->c_un.libfunc);
466 assert(rc == 0);
467 }
Petr Machata94078ec2012-01-05 18:07:02 +0100468 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100469
Petr Machata5bf47142012-10-27 19:29:00 +0200470 /* At this point, retp is fully initialized, except for OS and
471 * arch parts, and we can call private_process_destroy. */
472 if (os_process_clone(retp, proc) < 0) {
473 private_process_destroy(retp, 0);
474 return -1;
475 }
476 if (arch_process_clone(retp, proc) < 0) {
477 os_process_destroy(retp);
478 private_process_destroy(retp, 0);
479 return -1;
480 }
Petr Machata744f2552012-04-15 04:33:18 +0200481
Petr Machata2b46cfc2012-02-18 11:17:29 +0100482 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100483}
484
Petr Machata3c516d52011-08-18 03:53:18 +0200485static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200486open_one_pid(pid_t pid)
487{
Petr Machata9a5420c2011-07-09 11:21:23 +0200488 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
489
Petr Machata1974dbc2011-08-19 18:58:01 +0200490 /* Get the filename first. Should the trace_pid fail, we can
491 * easily free it, untracing is more work. */
Petr Machata929bd572012-12-17 03:20:34 +0100492 char *filename = pid2name(pid);
493 if (filename == NULL || trace_pid(pid) < 0) {
Petr Machataef0c74d2012-10-27 00:30:57 +0200494 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200495 free(filename);
496 return -1;
497 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100498
Petr Machata929bd572012-12-17 03:20:34 +0100499 struct process *proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200500 if (proc == NULL)
Petr Machataef0c74d2012-10-27 00:30:57 +0200501 goto fail;
502 free(filename);
Petr Machata3ed2a422012-04-06 17:18:55 +0200503 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200504
Petr Machata1974dbc2011-08-19 18:58:01 +0200505 return 0;
506}
507
Petr Machata2b46cfc2012-02-18 11:17:29 +0100508static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100509start_one_pid(struct process *proc, void *data)
Petr Machata1974dbc2011-08-19 18:58:01 +0200510{
511 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100512 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100513}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100514
Petr Machatadf2c88c2013-03-19 17:55:25 +0100515static enum callback_status
516is_main(struct process *proc, struct library *lib, void *data)
517{
518 return CBS_STOP_IF(lib->type == LT_LIBTYPE_MAIN);
519}
520
521void
522process_hit_start(struct process *proc)
523{
524 struct process *leader = proc->leader;
525 assert(leader != NULL);
526
527 struct library *mainlib
528 = proc_each_library(leader, NULL, is_main, NULL);
529 assert(mainlib != NULL);
530 linkmap_init(leader, mainlib->dyn_addr);
531 arch_dynlink_done(leader);
532}
533
Petr Machata9a5420c2011-07-09 11:21:23 +0200534void
535open_pid(pid_t pid)
536{
537 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200538 /* If we are already tracing this guy, we should be seeing all
539 * his children via normal tracing route. */
540 if (pid2proc(pid) != NULL)
541 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200542
Petr Machata3c516d52011-08-18 03:53:18 +0200543 /* First, see if we can attach the requested PID itself. */
Petr Machata8be68ff2013-03-19 18:00:58 +0100544 if (open_one_pid(pid) < 0) {
Petr Machata3c516d52011-08-18 03:53:18 +0200545 fprintf(stderr, "Cannot attach to pid %u: %s\n",
546 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200547 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200548 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200549 }
550
Petr Machata3c516d52011-08-18 03:53:18 +0200551 /* Now attach to all tasks that belong to that PID. There's a
552 * race between process_tasks and open_one_pid. So when we
553 * fail in open_one_pid below, we just do another round.
554 * Chances are that by then that PID will have gone away, and
555 * that's why we have seen the failure. The processes that we
556 * manage to open_one_pid are stopped, so we should eventually
557 * reach a point where process_tasks doesn't give any new
558 * processes (because there's nobody left to produce
559 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200560 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200561 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200562 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200563 pid_t *tasks;
564 size_t ntasks;
565 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200566
Petr Machata3c516d52011-08-18 03:53:18 +0200567 if (process_tasks(pid, &tasks, &ntasks) < 0) {
568 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
569 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100570 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200571 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200572
Petr Machata3c516d52011-08-18 03:53:18 +0200573 have_all = 1;
574 for (i = 0; i < ntasks; ++i)
575 if (pid2proc(tasks[i]) == NULL
Petr Machata8be68ff2013-03-19 18:00:58 +0100576 && open_one_pid(tasks[i]) < 0)
Petr Machata3c516d52011-08-18 03:53:18 +0200577 have_all = 0;
578
Petr Machata9a5420c2011-07-09 11:21:23 +0200579 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200580
Petr Machata1974dbc2011-08-19 18:58:01 +0200581 if (have_all && old_ntasks == ntasks)
582 break;
583 old_ntasks = ntasks;
584 }
585
Petr Machata929bd572012-12-17 03:20:34 +0100586 struct process *leader = pid2proc(pid)->leader;
Petr Machata93d95df2012-04-17 05:16:19 +0200587
588 /* XXX Is there a way to figure out whether _start has
589 * actually already been hit? */
Petr Machatadf2c88c2013-03-19 17:55:25 +0100590 process_hit_start(leader);
Petr Machata93d95df2012-04-17 05:16:19 +0200591
Petr Machata2f9b78e2012-04-16 21:08:54 +0200592 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200593 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200594}
595
Petr Machata2b46cfc2012-02-18 11:17:29 +0100596static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100597find_proc(struct process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200598{
Petr Machata7725d4b2013-03-19 18:03:00 +0100599 return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data);
Petr Machatacebb8842011-07-09 11:14:11 +0200600}
601
Petr Machata929bd572012-12-17 03:20:34 +0100602struct process *
603pid2proc(pid_t pid)
604{
Petr Machatacebb8842011-07-09 11:14:11 +0200605 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
606}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100607
Petr Machata929bd572012-12-17 03:20:34 +0100608static struct process *list_of_processes = NULL;
Petr Machatacebb8842011-07-09 11:14:11 +0200609
Petr Machatacbe29c62011-09-27 02:27:58 +0200610static void
Petr Machata929bd572012-12-17 03:20:34 +0100611unlist_process(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +0200612{
Petr Machatacbe29c62011-09-27 02:27:58 +0200613 if (list_of_processes == proc) {
614 list_of_processes = list_of_processes->next;
615 return;
616 }
617
Petr Machata929bd572012-12-17 03:20:34 +0100618 struct process *tmp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200619 for (tmp = list_of_processes; ; tmp = tmp->next) {
620 /* If the following assert fails, the process wasn't
621 * in the list. */
622 assert(tmp->next != NULL);
623
624 if (tmp->next == proc) {
625 tmp->next = tmp->next->next;
626 return;
627 }
628 }
629}
630
Petr Machata929bd572012-12-17 03:20:34 +0100631struct process *
632each_process(struct process *start_after,
633 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100634 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200635{
Petr Machata929bd572012-12-17 03:20:34 +0100636 struct process *it = start_after == NULL ? list_of_processes
Petr Machata74132a42012-03-16 02:46:18 +0100637 : start_after->next;
638
639 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200640 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100641 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100642 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200643 case CBS_FAIL:
644 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100645 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200646 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100647 case CBS_CONT:
648 break;
649 }
Petr Machatacebb8842011-07-09 11:14:11 +0200650 it = next;
651 }
652 return NULL;
653}
Petr Machata9a5420c2011-07-09 11:21:23 +0200654
Petr Machata929bd572012-12-17 03:20:34 +0100655struct process *
656each_task(struct process *proc, struct process *start_after,
657 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100658 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200659{
Petr Machata74132a42012-03-16 02:46:18 +0100660 assert(proc != NULL);
Petr Machata929bd572012-12-17 03:20:34 +0100661 struct process *it = start_after == NULL ? proc->leader
Petr Machata74132a42012-03-16 02:46:18 +0100662 : start_after->next;
663
Petr Machata9a5420c2011-07-09 11:21:23 +0200664 if (it != NULL) {
Petr Machata929bd572012-12-17 03:20:34 +0100665 struct process *leader = it->leader;
Petr Machata74132a42012-03-16 02:46:18 +0100666 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200667 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100668 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100669 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200670 case CBS_FAIL:
671 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100672 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200673 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100674 case CBS_CONT:
675 break;
676 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200677 it = next;
678 }
679 }
680 return NULL;
681}
682
Petr Machatabaf00e42013-01-07 19:44:43 +0100683static int
Petr Machata929bd572012-12-17 03:20:34 +0100684add_process(struct process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200685{
Petr Machata929bd572012-12-17 03:20:34 +0100686 struct process **leaderp = &list_of_processes;
Petr Machata9a5420c2011-07-09 11:21:23 +0200687 if (proc->pid) {
688 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200689 if (tgid == 0)
690 /* Must have been terminated before we managed
691 * to fully attach. */
Petr Machatabaf00e42013-01-07 19:44:43 +0100692 return -1;
Petr Machata929bd572012-12-17 03:20:34 +0100693 if (tgid == proc->pid) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200694 proc->leader = proc;
Petr Machata929bd572012-12-17 03:20:34 +0100695 } else {
696 struct process *leader = pid2proc(tgid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200697 proc->leader = leader;
698 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200699 leaderp = &leader->next;
700 }
701 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200702
703 if (!was_exec) {
704 proc->next = *leaderp;
705 *leaderp = proc;
706 }
Petr Machatabaf00e42013-01-07 19:44:43 +0100707 return 0;
Petr Machata9a5420c2011-07-09 11:21:23 +0200708}
709
Petr Machatacbe29c62011-09-27 02:27:58 +0200710void
Petr Machata929bd572012-12-17 03:20:34 +0100711change_process_leader(struct process *proc, struct process *leader)
Petr Machatacbe29c62011-09-27 02:27:58 +0200712{
Petr Machata929bd572012-12-17 03:20:34 +0100713 struct process **leaderp = &list_of_processes;
Petr Machatacbe29c62011-09-27 02:27:58 +0200714 if (proc->leader == leader)
715 return;
716
717 assert(leader != NULL);
718 unlist_process(proc);
719 if (proc != leader)
720 leaderp = &leader->next;
721
722 proc->leader = leader;
723 proc->next = *leaderp;
724 *leaderp = proc;
725}
726
Petr Machata2b46cfc2012-02-18 11:17:29 +0100727static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100728clear_leader(struct process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200729{
730 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
731 proc->pid, proc->leader->pid);
732 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100733 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200734}
735
736void
Petr Machata929bd572012-12-17 03:20:34 +0100737remove_process(struct process *proc)
Petr Machatacebb8842011-07-09 11:14:11 +0200738{
Petr Machatacebb8842011-07-09 11:14:11 +0200739 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
740
Petr Machata9a5420c2011-07-09 11:21:23 +0200741 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100742 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200743
Petr Machatacbe29c62011-09-27 02:27:58 +0200744 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100745 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200746 process_destroy(proc);
747 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100748}
Petr Machata4007d742011-07-09 11:29:42 +0200749
750void
Petr Machata929bd572012-12-17 03:20:34 +0100751install_event_handler(struct process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200752{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200753 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200754 assert(proc->event_handler == NULL);
755 proc->event_handler = handler;
756}
757
758void
Petr Machata929bd572012-12-17 03:20:34 +0100759destroy_event_handler(struct process *proc)
Petr Machata4007d742011-07-09 11:29:42 +0200760{
Petr Machata366c2f42012-02-09 19:34:36 +0100761 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200762 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200763 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200764 if (handler->destroy != NULL)
765 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200766 free(handler);
767 proc->event_handler = NULL;
768}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100769
Petr Machataef2fd272012-09-28 00:43:01 +0200770static int
Petr Machata929bd572012-12-17 03:20:34 +0100771breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100772{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200773 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200774 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100775
Petr Machataef2fd272012-09-28 00:43:01 +0200776 /* Don't enable latent or delayed symbols. */
Petr Machata96f04822012-10-31 03:29:11 +0100777 if (libsym->latent || libsym->delayed) {
778 debug(DEBUG_FUNCTION,
779 "delayed and/or latent breakpoint pid=%d, %s@%p",
780 proc->pid, libsym->name, libsym->enter_addr);
Petr Machataef2fd272012-09-28 00:43:01 +0200781 return 0;
Petr Machata96f04822012-10-31 03:29:11 +0100782 }
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200783
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200784 bp_addr = sym2addr(proc, libsym);
785
Petr Machatad5e85562012-04-05 15:18:38 +0200786 /* If there is an artificial breakpoint on the same address,
787 * its libsym will be NULL, and we can smuggle our libsym
788 * there. That artificial breakpoint is there presumably for
789 * the callbacks, which we don't touch. If there is a real
790 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200791 * symbols and ignore extra symbol aliases.
792 *
793 * The other direction is more complicated and currently not
794 * supported. If a breakpoint has custom callbacks, it might
795 * be also custom-allocated, and we would really need to swap
796 * the two: delete the one now in the dictionary, swap values
797 * around, and put the new breakpoint back in. */
Petr Machata98ff3092013-03-08 22:11:36 +0100798 struct breakpoint *bp;
799 if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
Petr Machata8d58d8b2012-11-12 12:46:03 +0100800 /* MIPS backend makes duplicate requests. This is
801 * likely a bug in the backend. Currently there's no
802 * point assigning more than one symbol to a
803 * breakpoint, because when it hits, we won't know
804 * what to print out. But it's easier to fix it here
805 * before someone who understands MIPS has the time to
806 * look into it. So turn the sanity check off on
807 * MIPS. References:
808 *
809 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
810 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
811 */
812#ifndef __mips__
Petr Machata98ff3092013-03-08 22:11:36 +0100813 assert(bp->libsym == NULL);
814 bp->libsym = libsym;
Petr Machata8d58d8b2012-11-12 12:46:03 +0100815#endif
Petr Machataef2fd272012-09-28 00:43:01 +0200816 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200817 }
818
Petr Machata98ff3092013-03-08 22:11:36 +0100819 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200820 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200821 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200822 fail:
823 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200824 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200825 }
826 if (proc_add_breakpoint(proc, bp) < 0) {
827 breakpoint_destroy(bp);
828 goto fail;
829 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100830
Petr Machatafa0c5702012-04-13 18:43:40 +0200831 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200832 proc_remove_breakpoint(proc, bp);
833 breakpoint_destroy(bp);
834 goto fail;
835 }
836
Petr Machataef2fd272012-09-28 00:43:01 +0200837 return 0;
838}
839
840static enum callback_status
841cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
842{
Petr Machata7725d4b2013-03-19 18:03:00 +0100843 return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0);
Petr Machataef2fd272012-09-28 00:43:01 +0200844}
845
846static int
Petr Machata929bd572012-12-17 03:20:34 +0100847proc_activate_latent_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200848 struct library_symbol *libsym)
849{
850 assert(libsym->latent);
851 libsym->latent = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100852 debug(DEBUG_FUNCTION, "activated latent symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200853 return breakpoint_for_symbol(libsym, proc);
854}
855
856int
Petr Machata929bd572012-12-17 03:20:34 +0100857proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200858 struct library_symbol *libsym)
859{
860 assert(libsym->delayed);
861 libsym->delayed = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100862 debug(DEBUG_FUNCTION, "activated delayed symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200863 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100864}
865
Petr Machataa1f76832012-09-28 00:08:00 +0200866static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100867activate_latent_in(struct process *proc, struct library *lib, void *data)
Petr Machataa1f76832012-09-28 00:08:00 +0200868{
869 struct library_exported_name *exported;
870 for (exported = data; exported != NULL; exported = exported->next) {
871 struct library_symbol *libsym = NULL;
872 while ((libsym = library_each_symbol(lib, libsym,
873 library_symbol_named_cb,
874 (void *)exported->name))
875 != NULL)
876 if (libsym->latent
877 && proc_activate_latent_symbol(proc, libsym) < 0)
878 return CBS_FAIL;
879 }
880 return CBS_CONT;
881}
882
Petr Machata2b46cfc2012-02-18 11:17:29 +0100883void
Petr Machata929bd572012-12-17 03:20:34 +0100884proc_add_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100885{
886 assert(lib->next == NULL);
887 lib->next = proc->libraries;
888 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200889 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
890 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100891
Mark Wielaarddfefa9f2014-01-07 21:00:44 +0100892#if defined(HAVE_LIBDW)
893 if (options.bt_depth > 0) {
894 /* Setup module tracking for libdwfl unwinding. */
895 struct process *leader = proc->leader;
896 Dwfl *dwfl = leader->dwfl;
897 if (dwfl == NULL) {
898 static const Dwfl_Callbacks proc_callbacks = {
899 .find_elf = dwfl_linux_proc_find_elf,
900 .find_debuginfo = dwfl_standard_find_debuginfo
901 };
902 dwfl = dwfl_begin(&proc_callbacks);
903 if (dwfl == NULL)
904 fprintf(stderr,
905 "Couldn't initialize libdwfl unwinding "
906 "for process %d: %s\n", leader->pid,
907 dwfl_errmsg (-1));
908 }
909
910 if (dwfl != NULL) {
911 dwfl_report_begin_add(dwfl);
912 if (dwfl_report_elf(dwfl, lib->soname,
913 lib->pathname, -1,
914 (GElf_Addr) lib->base,
915 false) == NULL)
916 fprintf(stderr,
917 "dwfl_report_elf %s@%p (%s) %d: %s\n",
918 lib->soname, lib->base, lib->pathname,
919 proc->pid, dwfl_errmsg (-1));
920 dwfl_report_end(dwfl, NULL, NULL);
921
922 if (leader->dwfl == NULL) {
923 int r = dwfl_linux_proc_attach(dwfl,
924 leader->pid,
925 true);
926 if (r == 0)
927 leader->dwfl = dwfl;
928 else {
929 const char *msg;
930 dwfl_end(dwfl);
931 if (r < 0)
932 msg = dwfl_errmsg(-1);
933 else
934 msg = strerror(r);
935 fprintf(stderr, "Couldn't initialize "
936 "libdwfl unwinding for "
937 "process %d: %s\n",
938 leader->pid, msg);
939 }
940 }
941 }
942 }
943#endif /* defined(HAVE_LIBDW) */
944
Petr Machataef2fd272012-09-28 00:43:01 +0200945 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100946 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200947 while ((libsym = library_each_symbol(lib, libsym,
948 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100949 proc)) != NULL)
Petr Machata3717f292013-01-06 17:52:00 +0100950 fprintf(stderr,
951 "Couldn't insert breakpoint for %s to %d: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200952 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200953
954 /* Look through export list of the new library and compare it
955 * with latent symbols of all libraries (including this
956 * library itself). */
957 struct library *lib2 = NULL;
958 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
959 lib->exported_names)) != NULL)
960 fprintf(stderr,
Petr Machata3717f292013-01-06 17:52:00 +0100961 "Couldn't activate latent symbols for %s in %d: %s.\n",
Petr Machata5c5d4802013-01-08 23:21:10 +0100962 lib2->soname, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100963}
964
965int
Petr Machata929bd572012-12-17 03:20:34 +0100966proc_remove_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100967{
968 struct library **libp;
969 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
970 if (*libp == lib) {
971 *libp = lib->next;
972 return 0;
973 }
974 return -1;
975}
976
977struct library *
Petr Machata929bd572012-12-17 03:20:34 +0100978proc_each_library(struct process *proc, struct library *it,
979 enum callback_status (*cb)(struct process *proc,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100980 struct library *lib, void *data),
981 void *data)
982{
983 if (it == NULL)
984 it = proc->libraries;
Petr Machatac4763a02013-01-08 23:19:14 +0100985 else
986 it = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100987
988 while (it != NULL) {
989 struct library *next = it->next;
990
991 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200992 case CBS_FAIL:
993 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100994 case CBS_STOP:
995 return it;
996 case CBS_CONT:
997 break;
998 }
999
1000 it = next;
1001 }
1002
1003 return NULL;
1004}
Petr Machata52dbfb12012-03-29 16:38:26 +02001005
Petr Machataf7fee432012-04-19 17:00:53 +02001006static void
Petr Machata929bd572012-12-17 03:20:34 +01001007check_leader(struct process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +02001008{
Petr Machata52dbfb12012-03-29 16:38:26 +02001009 /* Only the group leader should be getting the breakpoints and
1010 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +02001011 assert(proc->leader != NULL);
1012 assert(proc->leader == proc);
1013 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +02001014}
Petr Machata52dbfb12012-03-29 16:38:26 +02001015
Petr Machataf7fee432012-04-19 17:00:53 +02001016int
Petr Machata929bd572012-12-17 03:20:34 +01001017proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machataf7fee432012-04-19 17:00:53 +02001018{
Petr Machatafa0c5702012-04-13 18:43:40 +02001019 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +02001020 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +02001021 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +02001022
Petr Machataa2416362012-04-06 02:43:34 +02001023 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +02001024 * assert, but that's not necessary right now. Read the
1025 * comment in breakpoint_for_symbol. */
Petr Machatad7e4ca82012-11-28 03:38:47 +01001026 assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +02001027
Petr Machatad7e4ca82012-11-28 03:38:47 +01001028 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +02001029 fprintf(stderr,
1030 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
1031 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +02001032 return -1;
1033 }
1034
Petr Machata52dbfb12012-03-29 16:38:26 +02001035 return 0;
1036}
1037
Petr Machataf7fee432012-04-19 17:00:53 +02001038void
Petr Machata929bd572012-12-17 03:20:34 +01001039proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machata52dbfb12012-03-29 16:38:26 +02001040{
Petr Machataf7fee432012-04-19 17:00:53 +02001041 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
1042 proc->pid, breakpoint_name(bp), bp->addr);
1043 check_leader(proc);
Petr Machatad7e4ca82012-11-28 03:38:47 +01001044 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
1045 NULL, NULL, NULL);
1046 assert(rc == 0);
Petr Machata52dbfb12012-03-29 16:38:26 +02001047}
Petr Machatad3cc9882012-04-13 21:40:23 +02001048
Petr Machatad3cc9882012-04-13 21:40:23 +02001049struct each_breakpoint_data
1050{
Petr Machata929bd572012-12-17 03:20:34 +01001051 struct process *proc;
1052 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +02001053 struct breakpoint *bp,
1054 void *data);
1055 void *cb_data;
1056};
1057
Petr Machatad7e4ca82012-11-28 03:38:47 +01001058static enum callback_status
1059each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
Petr Machatad3cc9882012-04-13 21:40:23 +02001060{
1061 struct each_breakpoint_data *data = d;
Petr Machatad7e4ca82012-11-28 03:38:47 +01001062 return data->cb(data->proc, *bpp, data->cb_data);
Petr Machatad3cc9882012-04-13 21:40:23 +02001063}
1064
Petr Machata6bcc0922014-01-09 23:50:07 +01001065arch_addr_t *
1066proc_each_breakpoint(struct process *proc, arch_addr_t *start,
Petr Machata929bd572012-12-17 03:20:34 +01001067 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +02001068 struct breakpoint *bp,
1069 void *data), void *data)
1070{
1071 struct each_breakpoint_data dd = {
Petr Machatad3cc9882012-04-13 21:40:23 +02001072 .proc = proc,
1073 .cb = cb,
1074 .cb_data = data,
1075 };
Petr Machatad7e4ca82012-11-28 03:38:47 +01001076 return DICT_EACH(proc->breakpoints,
1077 arch_addr_t, struct breakpoint *, start,
1078 &each_breakpoint_cb, &dd);
Petr Machatad3cc9882012-04-13 21:40:23 +02001079}
Petr Machata165b5662012-10-27 19:23:12 +02001080
1081int
Petr Machata929bd572012-12-17 03:20:34 +01001082proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +02001083 struct library **retlib, struct library_symbol **retsym)
1084{
1085 struct library *lib = sym->lib;
1086 assert(lib != NULL);
1087
1088 struct library *flib
1089 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1090 if (flib == NULL)
1091 return -1;
1092
1093 struct library_symbol *fsym
1094 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1095 (char *)sym->name);
1096 if (fsym == NULL)
1097 return -1;
1098
1099 if (retlib != NULL)
1100 *retlib = flib;
1101 if (retsym != NULL)
1102 *retsym = fsym;
1103
1104 return 0;
1105}
Petr Machata32405542012-10-31 03:28:39 +01001106
1107struct library_symbol *
Petr Machata929bd572012-12-17 03:20:34 +01001108proc_each_symbol(struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +01001109 enum callback_status (*cb)(struct library_symbol *, void *),
1110 void *data)
1111{
1112 struct library *lib;
Petr Machata32405542012-10-31 03:28:39 +01001113 for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1114 lib != NULL; lib = lib->next) {
1115 start_after = library_each_symbol(lib, start_after, cb, data);
1116 if (start_after != NULL)
1117 return start_after;
1118 }
1119
1120 return NULL;
1121}
Petr Machata653085a2013-01-15 17:40:40 +01001122
1123#define DEF_READER(NAME, SIZE) \
1124 int \
1125 NAME(struct process *proc, arch_addr_t addr, \
1126 uint##SIZE##_t *lp) \
1127 { \
1128 union { \
1129 uint##SIZE##_t dst; \
1130 char buf[0]; \
1131 } u; \
1132 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \
1133 != sizeof(u.dst)) \
1134 return -1; \
1135 *lp = u.dst; \
1136 return 0; \
1137 }
1138
Petr Machatadc70e762013-01-23 00:02:26 +01001139DEF_READER(proc_read_8, 8)
Petr Machata653085a2013-01-15 17:40:40 +01001140DEF_READER(proc_read_16, 16)
1141DEF_READER(proc_read_32, 32)
1142DEF_READER(proc_read_64, 64)
1143
1144#undef DEF_READER