blob: 09e6d9b6392ff6893b6367d4c501142fe3038089 [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) */
109}
110
Petr Machata2b46cfc2012-02-18 11:17:29 +0100111static int
Petr Machata929bd572012-12-17 03:20:34 +0100112process_bare_init(struct process *proc, const char *filename,
Petr Machata3d0c91c2012-04-14 02:37:38 +0200113 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100114{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200115 if (!was_exec) {
116 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +0200117
Petr Machata3d0c91c2012-04-14 02:37:38 +0200118 proc->filename = strdup(filename);
119 if (proc->filename == NULL) {
120 fail:
121 free(proc->filename);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100122 if (proc->breakpoints != NULL) {
123 dict_destroy(proc->breakpoints,
124 NULL, NULL, NULL);
125 free(proc->breakpoints);
126 proc->breakpoints = NULL;
127 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200128 return -1;
129 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100130 }
131
132 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +0200133 proc->pid = pid;
Petr Machatabaf00e42013-01-07 19:44:43 +0100134 if (add_process(proc, was_exec) < 0)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100135 goto fail;
Petr Machatabaf00e42013-01-07 19:44:43 +0100136 if (proc->leader == NULL) {
137 unlist_and_fail:
138 if (!was_exec)
139 unlist_process(proc);
140 goto fail;
141 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100142
143 if (proc->leader == proc) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100144 proc->breakpoints = malloc(sizeof(*proc->breakpoints));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100145 if (proc->breakpoints == NULL)
Petr Machatabaf00e42013-01-07 19:44:43 +0100146 goto unlist_and_fail;
Petr Machatad7e4ca82012-11-28 03:38:47 +0100147 DICT_INIT(proc->breakpoints,
148 arch_addr_t, struct breakpoint *,
149 arch_addr_hash, arch_addr_eq, NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100150 } else {
151 proc->breakpoints = NULL;
152 }
153
Joe Damatoab3b72c2010-10-31 00:21:53 -0700154#if defined(HAVE_LIBUNWIND)
Petr Machataa2c270e2013-03-11 21:19:13 -0400155 if (options.bt_depth > 0) {
Petr Machataaf1e6032013-01-06 17:16:24 +0100156 proc->unwind_priv = _UPT_create(pid);
157 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700158
Petr Machataaf1e6032013-01-06 17:16:24 +0100159 if (proc->unwind_priv == NULL || proc->unwind_as == NULL) {
160 fprintf(stderr,
161 "Couldn't initialize unwinding "
162 "for process %d\n", proc->pid);
163 destroy_unwind(proc);
164 proc->unwind_priv = NULL;
165 proc->unwind_as = NULL;
166 }
167 }
Petr Machataa2c270e2013-03-11 21:19:13 -0400168#endif /* defined(HAVE_LIBUNWIND) */
Petr Machataaf1e6032013-01-06 17:16:24 +0100169
Petr Machata2b46cfc2012-02-18 11:17:29 +0100170 return 0;
171}
172
173static void
Petr Machata929bd572012-12-17 03:20:34 +0100174process_bare_destroy(struct process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100175{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100176 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
177 free(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178 if (!was_exec) {
179 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200180 unlist_process(proc);
Petr Machatae677c7e2012-10-26 22:23:43 +0200181 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200182 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100183}
184
Petr Machata3d0c91c2012-04-14 02:37:38 +0200185static int
Petr Machata929bd572012-12-17 03:20:34 +0100186process_init_main(struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100187{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200188 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200189 fprintf(stderr, "failed to init breakpoints %d\n",
190 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200191 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200192 }
193
Petr Machata2b46cfc2012-02-18 11:17:29 +0100194 return 0;
195}
196
Petr Machata3d0c91c2012-04-14 02:37:38 +0200197int
Petr Machata929bd572012-12-17 03:20:34 +0100198process_init(struct process *proc, const char *filename, pid_t pid)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200199{
200 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200201 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200202 fprintf(stderr, "failed to initialize process %d: %s\n",
203 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200204 return -1;
205 }
206
Petr Machata0f6e6d92012-10-26 23:42:17 +0200207 if (os_process_init(proc) < 0) {
208 process_bare_destroy(proc, 0);
209 goto fail;
210 }
211
Petr Machata744f2552012-04-15 04:33:18 +0200212 if (arch_process_init(proc) < 0) {
Petr Machata0f6e6d92012-10-26 23:42:17 +0200213 os_process_destroy(proc);
Petr Machata744f2552012-04-15 04:33:18 +0200214 process_bare_destroy(proc, 0);
215 goto fail;
216 }
217
Petr Machata218c5ff2012-04-15 04:22:39 +0200218 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200219 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200220 if (process_init_main(proc) < 0) {
221 process_bare_destroy(proc, 0);
222 goto fail;
223 }
224 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200225}
226
Petr Machata8ead1cd2012-04-24 18:13:09 +0200227static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100228destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200229{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200230 breakpoint_destroy(bp);
231 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200232 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200233}
234
Petr Machatae0e89ed2012-10-26 22:25:33 +0200235// XXX see comment in handle_event.c
Petr Machata929bd572012-12-17 03:20:34 +0100236void callstack_pop(struct process *proc);
Petr Machatae0e89ed2012-10-26 22:25:33 +0200237
Petr Machata3d0c91c2012-04-14 02:37:38 +0200238static void
Petr Machata929bd572012-12-17 03:20:34 +0100239private_process_destroy(struct process *proc, int was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200240{
Petr Machatae0e89ed2012-10-26 22:25:33 +0200241 /* Pop remaining stack elements. */
242 while (proc->callstack_depth > 0) {
243 /* When this is called just before a process is
244 * destroyed, the breakpoints should either have been
245 * retracted by now, or were killed by exec. In any
246 * case, it's safe to pretend that there are no
247 * breakpoints associated with the stack elements, so
248 * that stack_pop doesn't attempt to destroy them. */
249 size_t i = proc->callstack_depth - 1;
250 if (!proc->callstack[i].is_syscall)
251 proc->callstack[i].return_addr = 0;
252
253 callstack_pop(proc);
254 }
255
256 if (!was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200257 free(proc->filename);
258
Petr Machata8ead1cd2012-04-24 18:13:09 +0200259 /* Libraries and symbols. This is only relevant in
260 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200261 struct library *lib;
262 for (lib = proc->libraries; lib != NULL; ) {
263 struct library *next = lib->next;
264 library_destroy(lib);
265 free(lib);
266 lib = next;
267 }
268 proc->libraries = NULL;
269
270 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200271 if (proc->breakpoints != NULL) {
272 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100273 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
274 free(proc->breakpoints);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200275 proc->breakpoints = NULL;
276 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200277
278 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200279}
280
281void
Petr Machata929bd572012-12-17 03:20:34 +0100282process_destroy(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200283{
Petr Machata744f2552012-04-15 04:33:18 +0200284 arch_process_destroy(proc);
Petr Machata0f6e6d92012-10-26 23:42:17 +0200285 os_process_destroy(proc);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200286 private_process_destroy(proc, 0);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200287}
288
289int
Petr Machata929bd572012-12-17 03:20:34 +0100290process_exec(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200291{
Petr Machata0f6e6d92012-10-26 23:42:17 +0200292 /* Call exec handlers first, before we destroy the main
Petr Machata3cc0cd12012-10-26 22:30:51 +0200293 * state. */
Petr Machata0f6e6d92012-10-26 23:42:17 +0200294 if (arch_process_exec(proc) < 0
295 || os_process_exec(proc) < 0)
Petr Machata744f2552012-04-15 04:33:18 +0200296 return -1;
297
Petr Machata3d0c91c2012-04-14 02:37:38 +0200298 private_process_destroy(proc, 1);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200299
Petr Machata3d0c91c2012-04-14 02:37:38 +0200300 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
301 return -1;
302 if (process_init_main(proc) < 0) {
303 process_bare_destroy(proc, 1);
304 return -1;
305 }
306 return 0;
307}
308
Petr Machata929bd572012-12-17 03:20:34 +0100309struct process *
Petr Machata75934ad2012-04-14 02:28:03 +0200310open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100311{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312 assert(pid != 0);
Petr Machata929bd572012-12-17 03:20:34 +0100313 struct process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200314 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200315 free(proc);
316 return NULL;
317 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100318 return proc;
319}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100320
Petr Machata2b46cfc2012-02-18 11:17:29 +0100321struct clone_single_bp_data {
Petr Machata929bd572012-12-17 03:20:34 +0100322 struct process *old_proc;
323 struct process *new_proc;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100324};
325
Petr Machatad7e4ca82012-11-28 03:38:47 +0100326static enum callback_status
327clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100328{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100329 struct breakpoint *bp = *bpp;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330 struct clone_single_bp_data *data = u;
331
Petr Machata2b46cfc2012-02-18 11:17:29 +0100332 struct breakpoint *clone = malloc(sizeof(*clone));
333 if (clone == NULL
Petr Machatae5035522013-01-30 17:48:51 +0100334 || breakpoint_clone(clone, data->new_proc, bp) < 0) {
Petr Machatad3cc9882012-04-13 21:40:23 +0200335 fail:
336 free(clone);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100337 return CBS_STOP;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100338 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200339 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
340 breakpoint_destroy(clone);
341 goto fail;
342 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100343 return CBS_CONT;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100344}
345
346int
Petr Machata929bd572012-12-17 03:20:34 +0100347process_clone(struct process *retp, struct process *proc, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100348{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200349 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200350 fail1:
Petr Machataf56d1292013-01-07 21:27:54 +0100351 fprintf(stderr, "Failed to clone process %d to %d: %s\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200352 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100353 return -1;
354 }
355
Petr Machatacf1679a2012-04-06 19:56:17 +0200356 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200357 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400358 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200359
Petr Machata2b46cfc2012-02-18 11:17:29 +0100360 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200361 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100362 return 0;
363
364 /* Clone symbols first so that we can clone and relink
365 * breakpoints. */
366 struct library *lib;
367 struct library **nlibp = &retp->libraries;
Petr Machata1e339e02012-10-27 19:33:20 +0200368 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100369 *nlibp = malloc(sizeof(**nlibp));
Petr Machatad19b9162013-01-07 20:16:57 +0100370
Petr Machata2b46cfc2012-02-18 11:17:29 +0100371 if (*nlibp == NULL
372 || library_clone(*nlibp, lib) < 0) {
Petr Machatad19b9162013-01-07 20:16:57 +0100373 free(*nlibp);
374 *nlibp = NULL;
375
Petr Machata2b46cfc2012-02-18 11:17:29 +0100376 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200377 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100378
379 /* Error when cloning. Unroll what was done. */
380 for (lib = retp->libraries; lib != NULL; ) {
381 struct library *next = lib->next;
382 library_destroy(lib);
383 free(lib);
384 lib = next;
385 }
Petr Machataba1664b2012-04-28 14:59:05 +0200386 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100387 }
388
389 nlibp = &(*nlibp)->next;
390 }
391
392 /* Now clone breakpoints. Symbol relinking is done in
393 * clone_single_bp. */
394 struct clone_single_bp_data data = {
395 .old_proc = proc,
396 .new_proc = retp,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100397 };
Petr Machatad7e4ca82012-11-28 03:38:47 +0100398 if (DICT_EACH(proc->leader->breakpoints,
399 arch_addr_t, struct breakpoint *, NULL,
400 clone_single_bp, &data) != NULL)
Petr Machata94078ec2012-01-05 18:07:02 +0100401 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100402
Petr Machataded6f972012-04-13 23:15:48 +0200403 /* And finally the call stack. */
Petr Machatab6de8412012-10-27 19:31:22 +0200404 /* XXX clearly the callstack handling should be moved to a
405 * separate module and this whole business extracted to
406 * callstack_clone, or callstack_element_clone. */
Petr Machataded6f972012-04-13 23:15:48 +0200407 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
408 retp->callstack_depth = proc->callstack_depth;
409
Petr Machata94078ec2012-01-05 18:07:02 +0100410 size_t i;
411 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machatab6de8412012-10-27 19:31:22 +0200412 struct callstack_element *elem = &retp->callstack[i];
413 struct fetch_context *ctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100414 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200415 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100416 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200417 size_t j;
418 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100419 for (j = 0; j < i; ++j) {
Petr Machataf56d1292013-01-07 21:27:54 +0100420 nctx = retp->callstack[j].fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100421 fetch_arg_done(nctx);
Petr Machatab6de8412012-10-27 19:31:22 +0200422 elem->fetch_context = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100423 }
424 goto fail2;
425 }
Petr Machatab6de8412012-10-27 19:31:22 +0200426 elem->fetch_context = nctx;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100427 }
428
Petr Machataf56d1292013-01-07 21:27:54 +0100429 if (elem->arguments != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100430 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100431 if (nargs == NULL
Petr Machataf56d1292013-01-07 21:27:54 +0100432 || val_dict_clone(nargs, elem->arguments) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200433 size_t j;
Petr Machata94078ec2012-01-05 18:07:02 +0100434 for (j = 0; j < i; ++j) {
Petr Machataf56d1292013-01-07 21:27:54 +0100435 nargs = retp->callstack[j].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100436 val_dict_destroy(nargs);
437 free(nargs);
Petr Machatab6de8412012-10-27 19:31:22 +0200438 elem->arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100439 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100440
441 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200442 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100443 * fetch_context. */
444 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200445 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100446 }
Petr Machatab6de8412012-10-27 19:31:22 +0200447 elem->arguments = nargs;
Petr Machata94078ec2012-01-05 18:07:02 +0100448 }
Petr Machata165b5662012-10-27 19:23:12 +0200449
450 /* If it's not a syscall, we need to find the
451 * corresponding library symbol in the cloned
452 * library. */
453 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
454 struct library_symbol *libfunc = elem->c_un.libfunc;
455 int rc = proc_find_symbol(retp, libfunc,
456 NULL, &elem->c_un.libfunc);
457 assert(rc == 0);
458 }
Petr Machata94078ec2012-01-05 18:07:02 +0100459 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100460
Petr Machata5bf47142012-10-27 19:29:00 +0200461 /* At this point, retp is fully initialized, except for OS and
462 * arch parts, and we can call private_process_destroy. */
463 if (os_process_clone(retp, proc) < 0) {
464 private_process_destroy(retp, 0);
465 return -1;
466 }
467 if (arch_process_clone(retp, proc) < 0) {
468 os_process_destroy(retp);
469 private_process_destroy(retp, 0);
470 return -1;
471 }
Petr Machata744f2552012-04-15 04:33:18 +0200472
Petr Machata2b46cfc2012-02-18 11:17:29 +0100473 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100474}
475
Petr Machata3c516d52011-08-18 03:53:18 +0200476static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200477open_one_pid(pid_t pid)
478{
Petr Machata9a5420c2011-07-09 11:21:23 +0200479 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
480
Petr Machata1974dbc2011-08-19 18:58:01 +0200481 /* Get the filename first. Should the trace_pid fail, we can
482 * easily free it, untracing is more work. */
Petr Machata929bd572012-12-17 03:20:34 +0100483 char *filename = pid2name(pid);
484 if (filename == NULL || trace_pid(pid) < 0) {
Petr Machataef0c74d2012-10-27 00:30:57 +0200485 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200486 free(filename);
487 return -1;
488 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100489
Petr Machata929bd572012-12-17 03:20:34 +0100490 struct process *proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200491 if (proc == NULL)
Petr Machataef0c74d2012-10-27 00:30:57 +0200492 goto fail;
493 free(filename);
Petr Machata3ed2a422012-04-06 17:18:55 +0200494 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200495
Petr Machata1974dbc2011-08-19 18:58:01 +0200496 return 0;
497}
498
Petr Machata2b46cfc2012-02-18 11:17:29 +0100499static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100500start_one_pid(struct process *proc, void *data)
Petr Machata1974dbc2011-08-19 18:58:01 +0200501{
502 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100503 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100504}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100505
Petr Machatadf2c88c2013-03-19 17:55:25 +0100506static enum callback_status
507is_main(struct process *proc, struct library *lib, void *data)
508{
509 return CBS_STOP_IF(lib->type == LT_LIBTYPE_MAIN);
510}
511
512void
513process_hit_start(struct process *proc)
514{
515 struct process *leader = proc->leader;
516 assert(leader != NULL);
517
518 struct library *mainlib
519 = proc_each_library(leader, NULL, is_main, NULL);
520 assert(mainlib != NULL);
521 linkmap_init(leader, mainlib->dyn_addr);
522 arch_dynlink_done(leader);
523}
524
Petr Machata9a5420c2011-07-09 11:21:23 +0200525void
526open_pid(pid_t pid)
527{
528 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200529 /* If we are already tracing this guy, we should be seeing all
530 * his children via normal tracing route. */
531 if (pid2proc(pid) != NULL)
532 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200533
Petr Machata3c516d52011-08-18 03:53:18 +0200534 /* First, see if we can attach the requested PID itself. */
Petr Machata8be68ff2013-03-19 18:00:58 +0100535 if (open_one_pid(pid) < 0) {
Petr Machata3c516d52011-08-18 03:53:18 +0200536 fprintf(stderr, "Cannot attach to pid %u: %s\n",
537 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200538 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200539 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200540 }
541
Petr Machata3c516d52011-08-18 03:53:18 +0200542 /* Now attach to all tasks that belong to that PID. There's a
543 * race between process_tasks and open_one_pid. So when we
544 * fail in open_one_pid below, we just do another round.
545 * Chances are that by then that PID will have gone away, and
546 * that's why we have seen the failure. The processes that we
547 * manage to open_one_pid are stopped, so we should eventually
548 * reach a point where process_tasks doesn't give any new
549 * processes (because there's nobody left to produce
550 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200551 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200552 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200553 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200554 pid_t *tasks;
555 size_t ntasks;
556 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200557
Petr Machata3c516d52011-08-18 03:53:18 +0200558 if (process_tasks(pid, &tasks, &ntasks) < 0) {
559 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
560 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100561 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200562 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200563
Petr Machata3c516d52011-08-18 03:53:18 +0200564 have_all = 1;
565 for (i = 0; i < ntasks; ++i)
566 if (pid2proc(tasks[i]) == NULL
Petr Machata8be68ff2013-03-19 18:00:58 +0100567 && open_one_pid(tasks[i]) < 0)
Petr Machata3c516d52011-08-18 03:53:18 +0200568 have_all = 0;
569
Petr Machata9a5420c2011-07-09 11:21:23 +0200570 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200571
Petr Machata1974dbc2011-08-19 18:58:01 +0200572 if (have_all && old_ntasks == ntasks)
573 break;
574 old_ntasks = ntasks;
575 }
576
Petr Machata929bd572012-12-17 03:20:34 +0100577 struct process *leader = pid2proc(pid)->leader;
Petr Machata93d95df2012-04-17 05:16:19 +0200578
579 /* XXX Is there a way to figure out whether _start has
580 * actually already been hit? */
Petr Machatadf2c88c2013-03-19 17:55:25 +0100581 process_hit_start(leader);
Petr Machata93d95df2012-04-17 05:16:19 +0200582
Petr Machata2f9b78e2012-04-16 21:08:54 +0200583 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200584 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200585}
586
Petr Machata2b46cfc2012-02-18 11:17:29 +0100587static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100588find_proc(struct process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200589{
Petr Machata7725d4b2013-03-19 18:03:00 +0100590 return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data);
Petr Machatacebb8842011-07-09 11:14:11 +0200591}
592
Petr Machata929bd572012-12-17 03:20:34 +0100593struct process *
594pid2proc(pid_t pid)
595{
Petr Machatacebb8842011-07-09 11:14:11 +0200596 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
597}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100598
Petr Machata929bd572012-12-17 03:20:34 +0100599static struct process *list_of_processes = NULL;
Petr Machatacebb8842011-07-09 11:14:11 +0200600
Petr Machatacbe29c62011-09-27 02:27:58 +0200601static void
Petr Machata929bd572012-12-17 03:20:34 +0100602unlist_process(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +0200603{
Petr Machatacbe29c62011-09-27 02:27:58 +0200604 if (list_of_processes == proc) {
605 list_of_processes = list_of_processes->next;
606 return;
607 }
608
Petr Machata929bd572012-12-17 03:20:34 +0100609 struct process *tmp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200610 for (tmp = list_of_processes; ; tmp = tmp->next) {
611 /* If the following assert fails, the process wasn't
612 * in the list. */
613 assert(tmp->next != NULL);
614
615 if (tmp->next == proc) {
616 tmp->next = tmp->next->next;
617 return;
618 }
619 }
620}
621
Petr Machata929bd572012-12-17 03:20:34 +0100622struct process *
623each_process(struct process *start_after,
624 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100625 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200626{
Petr Machata929bd572012-12-17 03:20:34 +0100627 struct process *it = start_after == NULL ? list_of_processes
Petr Machata74132a42012-03-16 02:46:18 +0100628 : start_after->next;
629
630 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200631 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100632 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100633 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200634 case CBS_FAIL:
635 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100636 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200637 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100638 case CBS_CONT:
639 break;
640 }
Petr Machatacebb8842011-07-09 11:14:11 +0200641 it = next;
642 }
643 return NULL;
644}
Petr Machata9a5420c2011-07-09 11:21:23 +0200645
Petr Machata929bd572012-12-17 03:20:34 +0100646struct process *
647each_task(struct process *proc, struct process *start_after,
648 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100649 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200650{
Petr Machata74132a42012-03-16 02:46:18 +0100651 assert(proc != NULL);
Petr Machata929bd572012-12-17 03:20:34 +0100652 struct process *it = start_after == NULL ? proc->leader
Petr Machata74132a42012-03-16 02:46:18 +0100653 : start_after->next;
654
Petr Machata9a5420c2011-07-09 11:21:23 +0200655 if (it != NULL) {
Petr Machata929bd572012-12-17 03:20:34 +0100656 struct process *leader = it->leader;
Petr Machata74132a42012-03-16 02:46:18 +0100657 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200658 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100659 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100660 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200661 case CBS_FAIL:
662 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100663 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200664 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100665 case CBS_CONT:
666 break;
667 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200668 it = next;
669 }
670 }
671 return NULL;
672}
673
Petr Machatabaf00e42013-01-07 19:44:43 +0100674static int
Petr Machata929bd572012-12-17 03:20:34 +0100675add_process(struct process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200676{
Petr Machata929bd572012-12-17 03:20:34 +0100677 struct process **leaderp = &list_of_processes;
Petr Machata9a5420c2011-07-09 11:21:23 +0200678 if (proc->pid) {
679 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200680 if (tgid == 0)
681 /* Must have been terminated before we managed
682 * to fully attach. */
Petr Machatabaf00e42013-01-07 19:44:43 +0100683 return -1;
Petr Machata929bd572012-12-17 03:20:34 +0100684 if (tgid == proc->pid) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200685 proc->leader = proc;
Petr Machata929bd572012-12-17 03:20:34 +0100686 } else {
687 struct process *leader = pid2proc(tgid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200688 proc->leader = leader;
689 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200690 leaderp = &leader->next;
691 }
692 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200693
694 if (!was_exec) {
695 proc->next = *leaderp;
696 *leaderp = proc;
697 }
Petr Machatabaf00e42013-01-07 19:44:43 +0100698 return 0;
Petr Machata9a5420c2011-07-09 11:21:23 +0200699}
700
Petr Machatacbe29c62011-09-27 02:27:58 +0200701void
Petr Machata929bd572012-12-17 03:20:34 +0100702change_process_leader(struct process *proc, struct process *leader)
Petr Machatacbe29c62011-09-27 02:27:58 +0200703{
Petr Machata929bd572012-12-17 03:20:34 +0100704 struct process **leaderp = &list_of_processes;
Petr Machatacbe29c62011-09-27 02:27:58 +0200705 if (proc->leader == leader)
706 return;
707
708 assert(leader != NULL);
709 unlist_process(proc);
710 if (proc != leader)
711 leaderp = &leader->next;
712
713 proc->leader = leader;
714 proc->next = *leaderp;
715 *leaderp = proc;
716}
717
Petr Machata2b46cfc2012-02-18 11:17:29 +0100718static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100719clear_leader(struct process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200720{
721 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
722 proc->pid, proc->leader->pid);
723 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100724 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200725}
726
727void
Petr Machata929bd572012-12-17 03:20:34 +0100728remove_process(struct process *proc)
Petr Machatacebb8842011-07-09 11:14:11 +0200729{
Petr Machatacebb8842011-07-09 11:14:11 +0200730 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
731
Petr Machata9a5420c2011-07-09 11:21:23 +0200732 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100733 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200734
Petr Machatacbe29c62011-09-27 02:27:58 +0200735 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100736 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200737 process_destroy(proc);
738 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100739}
Petr Machata4007d742011-07-09 11:29:42 +0200740
741void
Petr Machata929bd572012-12-17 03:20:34 +0100742install_event_handler(struct process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200743{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200744 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200745 assert(proc->event_handler == NULL);
746 proc->event_handler = handler;
747}
748
749void
Petr Machata929bd572012-12-17 03:20:34 +0100750destroy_event_handler(struct process *proc)
Petr Machata4007d742011-07-09 11:29:42 +0200751{
Petr Machata366c2f42012-02-09 19:34:36 +0100752 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200753 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200754 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200755 if (handler->destroy != NULL)
756 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200757 free(handler);
758 proc->event_handler = NULL;
759}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100760
Petr Machataef2fd272012-09-28 00:43:01 +0200761static int
Petr Machata929bd572012-12-17 03:20:34 +0100762breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100763{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200764 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200765 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100766
Petr Machataef2fd272012-09-28 00:43:01 +0200767 /* Don't enable latent or delayed symbols. */
Petr Machata96f04822012-10-31 03:29:11 +0100768 if (libsym->latent || libsym->delayed) {
769 debug(DEBUG_FUNCTION,
770 "delayed and/or latent breakpoint pid=%d, %s@%p",
771 proc->pid, libsym->name, libsym->enter_addr);
Petr Machataef2fd272012-09-28 00:43:01 +0200772 return 0;
Petr Machata96f04822012-10-31 03:29:11 +0100773 }
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200774
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200775 bp_addr = sym2addr(proc, libsym);
776
Petr Machatad5e85562012-04-05 15:18:38 +0200777 /* If there is an artificial breakpoint on the same address,
778 * its libsym will be NULL, and we can smuggle our libsym
779 * there. That artificial breakpoint is there presumably for
780 * the callbacks, which we don't touch. If there is a real
781 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200782 * symbols and ignore extra symbol aliases.
783 *
784 * The other direction is more complicated and currently not
785 * supported. If a breakpoint has custom callbacks, it might
786 * be also custom-allocated, and we would really need to swap
787 * the two: delete the one now in the dictionary, swap values
788 * around, and put the new breakpoint back in. */
Petr Machata98ff3092013-03-08 22:11:36 +0100789 struct breakpoint *bp;
790 if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
Petr Machata8d58d8b2012-11-12 12:46:03 +0100791 /* MIPS backend makes duplicate requests. This is
792 * likely a bug in the backend. Currently there's no
793 * point assigning more than one symbol to a
794 * breakpoint, because when it hits, we won't know
795 * what to print out. But it's easier to fix it here
796 * before someone who understands MIPS has the time to
797 * look into it. So turn the sanity check off on
798 * MIPS. References:
799 *
800 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
801 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
802 */
803#ifndef __mips__
Petr Machata98ff3092013-03-08 22:11:36 +0100804 assert(bp->libsym == NULL);
805 bp->libsym = libsym;
Petr Machata8d58d8b2012-11-12 12:46:03 +0100806#endif
Petr Machataef2fd272012-09-28 00:43:01 +0200807 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200808 }
809
Petr Machata98ff3092013-03-08 22:11:36 +0100810 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200811 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200812 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200813 fail:
814 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200815 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200816 }
817 if (proc_add_breakpoint(proc, bp) < 0) {
818 breakpoint_destroy(bp);
819 goto fail;
820 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100821
Petr Machatafa0c5702012-04-13 18:43:40 +0200822 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200823 proc_remove_breakpoint(proc, bp);
824 breakpoint_destroy(bp);
825 goto fail;
826 }
827
Petr Machataef2fd272012-09-28 00:43:01 +0200828 return 0;
829}
830
831static enum callback_status
832cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
833{
Petr Machata7725d4b2013-03-19 18:03:00 +0100834 return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0);
Petr Machataef2fd272012-09-28 00:43:01 +0200835}
836
837static int
Petr Machata929bd572012-12-17 03:20:34 +0100838proc_activate_latent_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200839 struct library_symbol *libsym)
840{
841 assert(libsym->latent);
842 libsym->latent = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100843 debug(DEBUG_FUNCTION, "activated latent symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200844 return breakpoint_for_symbol(libsym, proc);
845}
846
847int
Petr Machata929bd572012-12-17 03:20:34 +0100848proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200849 struct library_symbol *libsym)
850{
851 assert(libsym->delayed);
852 libsym->delayed = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100853 debug(DEBUG_FUNCTION, "activated delayed symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200854 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100855}
856
Petr Machataa1f76832012-09-28 00:08:00 +0200857static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100858activate_latent_in(struct process *proc, struct library *lib, void *data)
Petr Machataa1f76832012-09-28 00:08:00 +0200859{
860 struct library_exported_name *exported;
861 for (exported = data; exported != NULL; exported = exported->next) {
862 struct library_symbol *libsym = NULL;
863 while ((libsym = library_each_symbol(lib, libsym,
864 library_symbol_named_cb,
865 (void *)exported->name))
866 != NULL)
867 if (libsym->latent
868 && proc_activate_latent_symbol(proc, libsym) < 0)
869 return CBS_FAIL;
870 }
871 return CBS_CONT;
872}
873
Petr Machata2b46cfc2012-02-18 11:17:29 +0100874void
Petr Machata929bd572012-12-17 03:20:34 +0100875proc_add_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100876{
877 assert(lib->next == NULL);
878 lib->next = proc->libraries;
879 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200880 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
881 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100882
Petr Machataef2fd272012-09-28 00:43:01 +0200883 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100884 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200885 while ((libsym = library_each_symbol(lib, libsym,
886 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100887 proc)) != NULL)
Petr Machata3717f292013-01-06 17:52:00 +0100888 fprintf(stderr,
889 "Couldn't insert breakpoint for %s to %d: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200890 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200891
892 /* Look through export list of the new library and compare it
893 * with latent symbols of all libraries (including this
894 * library itself). */
895 struct library *lib2 = NULL;
896 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
897 lib->exported_names)) != NULL)
898 fprintf(stderr,
Petr Machata3717f292013-01-06 17:52:00 +0100899 "Couldn't activate latent symbols for %s in %d: %s.\n",
Petr Machata5c5d4802013-01-08 23:21:10 +0100900 lib2->soname, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100901}
902
903int
Petr Machata929bd572012-12-17 03:20:34 +0100904proc_remove_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100905{
906 struct library **libp;
907 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
908 if (*libp == lib) {
909 *libp = lib->next;
910 return 0;
911 }
912 return -1;
913}
914
915struct library *
Petr Machata929bd572012-12-17 03:20:34 +0100916proc_each_library(struct process *proc, struct library *it,
917 enum callback_status (*cb)(struct process *proc,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100918 struct library *lib, void *data),
919 void *data)
920{
921 if (it == NULL)
922 it = proc->libraries;
Petr Machatac4763a02013-01-08 23:19:14 +0100923 else
924 it = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100925
926 while (it != NULL) {
927 struct library *next = it->next;
928
929 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200930 case CBS_FAIL:
931 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100932 case CBS_STOP:
933 return it;
934 case CBS_CONT:
935 break;
936 }
937
938 it = next;
939 }
940
941 return NULL;
942}
Petr Machata52dbfb12012-03-29 16:38:26 +0200943
Petr Machataf7fee432012-04-19 17:00:53 +0200944static void
Petr Machata929bd572012-12-17 03:20:34 +0100945check_leader(struct process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200946{
Petr Machata52dbfb12012-03-29 16:38:26 +0200947 /* Only the group leader should be getting the breakpoints and
948 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200949 assert(proc->leader != NULL);
950 assert(proc->leader == proc);
951 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200952}
Petr Machata52dbfb12012-03-29 16:38:26 +0200953
Petr Machataf7fee432012-04-19 17:00:53 +0200954int
Petr Machata929bd572012-12-17 03:20:34 +0100955proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machataf7fee432012-04-19 17:00:53 +0200956{
Petr Machatafa0c5702012-04-13 18:43:40 +0200957 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200958 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200959 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200960
Petr Machataa2416362012-04-06 02:43:34 +0200961 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200962 * assert, but that's not necessary right now. Read the
963 * comment in breakpoint_for_symbol. */
Petr Machatad7e4ca82012-11-28 03:38:47 +0100964 assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200965
Petr Machatad7e4ca82012-11-28 03:38:47 +0100966 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200967 fprintf(stderr,
968 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
969 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200970 return -1;
971 }
972
Petr Machata52dbfb12012-03-29 16:38:26 +0200973 return 0;
974}
975
Petr Machataf7fee432012-04-19 17:00:53 +0200976void
Petr Machata929bd572012-12-17 03:20:34 +0100977proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machata52dbfb12012-03-29 16:38:26 +0200978{
Petr Machataf7fee432012-04-19 17:00:53 +0200979 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
980 proc->pid, breakpoint_name(bp), bp->addr);
981 check_leader(proc);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100982 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
983 NULL, NULL, NULL);
984 assert(rc == 0);
Petr Machata52dbfb12012-03-29 16:38:26 +0200985}
Petr Machatad3cc9882012-04-13 21:40:23 +0200986
Petr Machatad3cc9882012-04-13 21:40:23 +0200987struct each_breakpoint_data
988{
Petr Machata929bd572012-12-17 03:20:34 +0100989 struct process *proc;
990 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +0200991 struct breakpoint *bp,
992 void *data);
993 void *cb_data;
994};
995
Petr Machatad7e4ca82012-11-28 03:38:47 +0100996static enum callback_status
997each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
Petr Machatad3cc9882012-04-13 21:40:23 +0200998{
999 struct each_breakpoint_data *data = d;
Petr Machatad7e4ca82012-11-28 03:38:47 +01001000 return data->cb(data->proc, *bpp, data->cb_data);
Petr Machatad3cc9882012-04-13 21:40:23 +02001001}
1002
Petr Machata6bcc0922014-01-09 23:50:07 +01001003arch_addr_t *
1004proc_each_breakpoint(struct process *proc, arch_addr_t *start,
Petr Machata929bd572012-12-17 03:20:34 +01001005 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +02001006 struct breakpoint *bp,
1007 void *data), void *data)
1008{
1009 struct each_breakpoint_data dd = {
Petr Machatad3cc9882012-04-13 21:40:23 +02001010 .proc = proc,
1011 .cb = cb,
1012 .cb_data = data,
1013 };
Petr Machatad7e4ca82012-11-28 03:38:47 +01001014 return DICT_EACH(proc->breakpoints,
1015 arch_addr_t, struct breakpoint *, start,
1016 &each_breakpoint_cb, &dd);
Petr Machatad3cc9882012-04-13 21:40:23 +02001017}
Petr Machata165b5662012-10-27 19:23:12 +02001018
1019int
Petr Machata929bd572012-12-17 03:20:34 +01001020proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +02001021 struct library **retlib, struct library_symbol **retsym)
1022{
1023 struct library *lib = sym->lib;
1024 assert(lib != NULL);
1025
1026 struct library *flib
1027 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1028 if (flib == NULL)
1029 return -1;
1030
1031 struct library_symbol *fsym
1032 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1033 (char *)sym->name);
1034 if (fsym == NULL)
1035 return -1;
1036
1037 if (retlib != NULL)
1038 *retlib = flib;
1039 if (retsym != NULL)
1040 *retsym = fsym;
1041
1042 return 0;
1043}
Petr Machata32405542012-10-31 03:28:39 +01001044
1045struct library_symbol *
Petr Machata929bd572012-12-17 03:20:34 +01001046proc_each_symbol(struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +01001047 enum callback_status (*cb)(struct library_symbol *, void *),
1048 void *data)
1049{
1050 struct library *lib;
Petr Machata32405542012-10-31 03:28:39 +01001051 for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1052 lib != NULL; lib = lib->next) {
1053 start_after = library_each_symbol(lib, start_after, cb, data);
1054 if (start_after != NULL)
1055 return start_after;
1056 }
1057
1058 return NULL;
1059}
Petr Machata653085a2013-01-15 17:40:40 +01001060
1061#define DEF_READER(NAME, SIZE) \
1062 int \
1063 NAME(struct process *proc, arch_addr_t addr, \
1064 uint##SIZE##_t *lp) \
1065 { \
1066 union { \
1067 uint##SIZE##_t dst; \
1068 char buf[0]; \
1069 } u; \
1070 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \
1071 != sizeof(u.dst)) \
1072 return -1; \
1073 *lp = u.dst; \
1074 return 0; \
1075 }
1076
Petr Machatadc70e762013-01-23 00:02:26 +01001077DEF_READER(proc_read_8, 8)
Petr Machata653085a2013-01-15 17:40:40 +01001078DEF_READER(proc_read_16, 16)
1079DEF_READER(proc_read_32, 32)
1080DEF_READER(proc_read_64, 64)
1081
1082#undef DEF_READER