blob: e3badd536d243103957a24f030faa316137d77b0 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
Petr Machata653085a2013-01-15 17:40:40 +01003 * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
Petr Machata64262602012-01-07 03:41:36 +01004 * Copyright (C) 2010 Joe Damato
5 * Copyright (C) 1998,2009 Juan Cespedes
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA
21 */
22
Joe Damatoab3b72c2010-10-31 00:21:53 -070023#include "config.h"
24
Petr Machataba1664b2012-04-28 14:59:05 +020025#include <sys/types.h>
26#include <assert.h>
27#include <errno.h>
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
31
Joe Damatoab3b72c2010-10-31 00:21:53 -070032#if defined(HAVE_LIBUNWIND)
33#include <libunwind.h>
34#include <libunwind-ptrace.h>
35#endif /* defined(HAVE_LIBUNWIND) */
36
Petr Machata64262602012-01-07 03:41:36 +010037#include "backend.h"
Petr Machataba1664b2012-04-28 14:59:05 +020038#include "breakpoint.h"
39#include "debug.h"
40#include "fetch.h"
Petr Machataaf1e6032013-01-06 17:16:24 +010041#include "options.h"
Petr Machataba1664b2012-04-28 14:59:05 +020042#include "proc.h"
43#include "value_dict.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010044
Petr Machata744f2552012-04-15 04:33:18 +020045#ifndef ARCH_HAVE_PROCESS_DATA
46int
Petr Machata929bd572012-12-17 03:20:34 +010047arch_process_init(struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020048{
49 return 0;
50}
51
52void
Petr Machata929bd572012-12-17 03:20:34 +010053arch_process_destroy(struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020054{
55}
56
57int
Petr Machata929bd572012-12-17 03:20:34 +010058arch_process_clone(struct process *retp, struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020059{
60 return 0;
61}
62
63int
Petr Machata929bd572012-12-17 03:20:34 +010064arch_process_exec(struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020065{
66 return 0;
67}
68#endif
69
Petr Machata0f6e6d92012-10-26 23:42:17 +020070#ifndef OS_HAVE_PROCESS_DATA
71int
Petr Machata929bd572012-12-17 03:20:34 +010072os_process_init(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020073{
74 return 0;
75}
76
77void
Petr Machata929bd572012-12-17 03:20:34 +010078os_process_destroy(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020079{
80}
81
82int
Petr Machata929bd572012-12-17 03:20:34 +010083os_process_clone(struct process *retp, struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020084{
85 return 0;
86}
87
88int
Petr Machata929bd572012-12-17 03:20:34 +010089os_process_exec(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020090{
91 return 0;
92}
93#endif
94
Petr Machata93d95df2012-04-17 05:16:19 +020095#ifndef ARCH_HAVE_DYNLINK_DONE
96void
Petr Machata929bd572012-12-17 03:20:34 +010097arch_dynlink_done(struct process *proc)
Petr Machata93d95df2012-04-17 05:16:19 +020098{
99}
100#endif
101
Petr Machatabaf00e42013-01-07 19:44:43 +0100102static int add_process(struct process *proc, int was_exec);
Petr Machata929bd572012-12-17 03:20:34 +0100103static void unlist_process(struct process *proc);
Petr Machata44965c72012-04-06 19:59:20 +0200104
Petr Machatae677c7e2012-10-26 22:23:43 +0200105static void
Petr Machata929bd572012-12-17 03:20:34 +0100106destroy_unwind(struct process *proc)
Petr Machatae677c7e2012-10-26 22:23:43 +0200107{
108#if defined(HAVE_LIBUNWIND)
Petr Machataaf1e6032013-01-06 17:16:24 +0100109 if (proc->unwind_priv != NULL)
110 _UPT_destroy(proc->unwind_priv);
111 if (proc->unwind_as != NULL)
112 unw_destroy_addr_space(proc->unwind_as);
Petr Machatae677c7e2012-10-26 22:23:43 +0200113#endif /* defined(HAVE_LIBUNWIND) */
114}
115
Petr Machata2b46cfc2012-02-18 11:17:29 +0100116static int
Petr Machata929bd572012-12-17 03:20:34 +0100117process_bare_init(struct process *proc, const char *filename,
Petr Machata3d0c91c2012-04-14 02:37:38 +0200118 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100119{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200120 if (!was_exec) {
121 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +0200122
Petr Machata3d0c91c2012-04-14 02:37:38 +0200123 proc->filename = strdup(filename);
124 if (proc->filename == NULL) {
125 fail:
126 free(proc->filename);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100127 if (proc->breakpoints != NULL) {
128 dict_destroy(proc->breakpoints,
129 NULL, NULL, NULL);
130 free(proc->breakpoints);
131 proc->breakpoints = NULL;
132 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200133 return -1;
134 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100135 }
136
137 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +0200138 proc->pid = pid;
Petr 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
Petr Machata2b46cfc2012-02-18 11:17:29 +0100175 return 0;
176}
177
178static void
Petr Machata929bd572012-12-17 03:20:34 +0100179process_bare_destroy(struct process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100180{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100181 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
182 free(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200183 if (!was_exec) {
184 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200185 unlist_process(proc);
Petr Machatae677c7e2012-10-26 22:23:43 +0200186 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200187 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100188}
189
Petr Machata3d0c91c2012-04-14 02:37:38 +0200190static int
Petr Machata929bd572012-12-17 03:20:34 +0100191process_init_main(struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100192{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200193 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200194 fprintf(stderr, "failed to init breakpoints %d\n",
195 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200196 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200197 }
198
Petr Machata2b46cfc2012-02-18 11:17:29 +0100199 return 0;
200}
201
Petr Machata3d0c91c2012-04-14 02:37:38 +0200202int
Petr Machata929bd572012-12-17 03:20:34 +0100203process_init(struct process *proc, const char *filename, pid_t pid)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200204{
205 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200206 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200207 fprintf(stderr, "failed to initialize process %d: %s\n",
208 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200209 return -1;
210 }
211
Petr Machata0f6e6d92012-10-26 23:42:17 +0200212 if (os_process_init(proc) < 0) {
213 process_bare_destroy(proc, 0);
214 goto fail;
215 }
216
Petr Machata744f2552012-04-15 04:33:18 +0200217 if (arch_process_init(proc) < 0) {
Petr Machata0f6e6d92012-10-26 23:42:17 +0200218 os_process_destroy(proc);
Petr Machata744f2552012-04-15 04:33:18 +0200219 process_bare_destroy(proc, 0);
220 goto fail;
221 }
222
Petr Machata218c5ff2012-04-15 04:22:39 +0200223 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200224 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200225 if (process_init_main(proc) < 0) {
226 process_bare_destroy(proc, 0);
227 goto fail;
228 }
229 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200230}
231
Petr Machata8ead1cd2012-04-24 18:13:09 +0200232static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100233destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200234{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200235 breakpoint_destroy(bp);
236 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200237 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200238}
239
Petr Machatae0e89ed2012-10-26 22:25:33 +0200240// XXX see comment in handle_event.c
Petr Machata929bd572012-12-17 03:20:34 +0100241void callstack_pop(struct process *proc);
Petr Machatae0e89ed2012-10-26 22:25:33 +0200242
Petr Machata3d0c91c2012-04-14 02:37:38 +0200243static void
Petr Machata929bd572012-12-17 03:20:34 +0100244private_process_destroy(struct process *proc, int was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200245{
Petr Machatae0e89ed2012-10-26 22:25:33 +0200246 /* Pop remaining stack elements. */
247 while (proc->callstack_depth > 0) {
248 /* When this is called just before a process is
249 * destroyed, the breakpoints should either have been
250 * retracted by now, or were killed by exec. In any
251 * case, it's safe to pretend that there are no
252 * breakpoints associated with the stack elements, so
253 * that stack_pop doesn't attempt to destroy them. */
254 size_t i = proc->callstack_depth - 1;
255 if (!proc->callstack[i].is_syscall)
256 proc->callstack[i].return_addr = 0;
257
258 callstack_pop(proc);
259 }
260
261 if (!was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200262 free(proc->filename);
263
Petr Machata8ead1cd2012-04-24 18:13:09 +0200264 /* Libraries and symbols. This is only relevant in
265 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200266 struct library *lib;
267 for (lib = proc->libraries; lib != NULL; ) {
268 struct library *next = lib->next;
269 library_destroy(lib);
270 free(lib);
271 lib = next;
272 }
273 proc->libraries = NULL;
274
275 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200276 if (proc->breakpoints != NULL) {
277 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100278 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
279 free(proc->breakpoints);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200280 proc->breakpoints = NULL;
281 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200282
283 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200284}
285
286void
Petr Machata929bd572012-12-17 03:20:34 +0100287process_destroy(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200288{
Petr Machata744f2552012-04-15 04:33:18 +0200289 arch_process_destroy(proc);
Petr Machata0f6e6d92012-10-26 23:42:17 +0200290 os_process_destroy(proc);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200291 private_process_destroy(proc, 0);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200292}
293
294int
Petr Machata929bd572012-12-17 03:20:34 +0100295process_exec(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200296{
Petr Machata0f6e6d92012-10-26 23:42:17 +0200297 /* Call exec handlers first, before we destroy the main
Petr Machata3cc0cd12012-10-26 22:30:51 +0200298 * state. */
Petr Machata0f6e6d92012-10-26 23:42:17 +0200299 if (arch_process_exec(proc) < 0
300 || os_process_exec(proc) < 0)
Petr Machata744f2552012-04-15 04:33:18 +0200301 return -1;
302
Petr Machata3d0c91c2012-04-14 02:37:38 +0200303 private_process_destroy(proc, 1);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200304
Petr Machata3d0c91c2012-04-14 02:37:38 +0200305 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
306 return -1;
307 if (process_init_main(proc) < 0) {
308 process_bare_destroy(proc, 1);
309 return -1;
310 }
311 return 0;
312}
313
Petr Machata929bd572012-12-17 03:20:34 +0100314struct process *
Petr Machata75934ad2012-04-14 02:28:03 +0200315open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100316{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100317 assert(pid != 0);
Petr Machata929bd572012-12-17 03:20:34 +0100318 struct process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200319 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200320 free(proc);
321 return NULL;
322 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100323 return proc;
324}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100325
Petr Machata2b46cfc2012-02-18 11:17:29 +0100326struct clone_single_bp_data {
Petr Machata929bd572012-12-17 03:20:34 +0100327 struct process *old_proc;
328 struct process *new_proc;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100329};
330
Petr Machatad7e4ca82012-11-28 03:38:47 +0100331static enum callback_status
332clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100334 struct breakpoint *bp = *bpp;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100335 struct clone_single_bp_data *data = u;
336
Petr Machata2b46cfc2012-02-18 11:17:29 +0100337 struct breakpoint *clone = malloc(sizeof(*clone));
338 if (clone == NULL
Petr Machatae5035522013-01-30 17:48:51 +0100339 || breakpoint_clone(clone, data->new_proc, bp) < 0) {
Petr Machatad3cc9882012-04-13 21:40:23 +0200340 fail:
341 free(clone);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100342 return CBS_STOP;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100343 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200344 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
345 breakpoint_destroy(clone);
346 goto fail;
347 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100348 return CBS_CONT;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100349}
350
351int
Petr Machata929bd572012-12-17 03:20:34 +0100352process_clone(struct process *retp, struct process *proc, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100353{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200354 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200355 fail1:
Petr Machataf56d1292013-01-07 21:27:54 +0100356 fprintf(stderr, "Failed to clone process %d to %d: %s\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200357 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100358 return -1;
359 }
360
Petr Machatacf1679a2012-04-06 19:56:17 +0200361 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200362 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400363 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200364
Petr Machata2b46cfc2012-02-18 11:17:29 +0100365 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200366 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100367 return 0;
368
369 /* Clone symbols first so that we can clone and relink
370 * breakpoints. */
371 struct library *lib;
372 struct library **nlibp = &retp->libraries;
Petr Machata1e339e02012-10-27 19:33:20 +0200373 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100374 *nlibp = malloc(sizeof(**nlibp));
Petr Machatad19b9162013-01-07 20:16:57 +0100375
Petr Machata2b46cfc2012-02-18 11:17:29 +0100376 if (*nlibp == NULL
377 || library_clone(*nlibp, lib) < 0) {
Petr Machatad19b9162013-01-07 20:16:57 +0100378 free(*nlibp);
379 *nlibp = NULL;
380
Petr Machata2b46cfc2012-02-18 11:17:29 +0100381 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200382 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100383
384 /* Error when cloning. Unroll what was done. */
385 for (lib = retp->libraries; lib != NULL; ) {
386 struct library *next = lib->next;
387 library_destroy(lib);
388 free(lib);
389 lib = next;
390 }
Petr Machataba1664b2012-04-28 14:59:05 +0200391 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100392 }
393
394 nlibp = &(*nlibp)->next;
395 }
396
397 /* Now clone breakpoints. Symbol relinking is done in
398 * clone_single_bp. */
399 struct clone_single_bp_data data = {
400 .old_proc = proc,
401 .new_proc = retp,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100402 };
Petr Machatad7e4ca82012-11-28 03:38:47 +0100403 if (DICT_EACH(proc->leader->breakpoints,
404 arch_addr_t, struct breakpoint *, NULL,
405 clone_single_bp, &data) != NULL)
Petr Machata94078ec2012-01-05 18:07:02 +0100406 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100407
Petr Machataded6f972012-04-13 23:15:48 +0200408 /* And finally the call stack. */
Petr Machatab6de8412012-10-27 19:31:22 +0200409 /* XXX clearly the callstack handling should be moved to a
410 * separate module and this whole business extracted to
411 * callstack_clone, or callstack_element_clone. */
Petr Machataded6f972012-04-13 23:15:48 +0200412 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
413 retp->callstack_depth = proc->callstack_depth;
414
Petr Machata94078ec2012-01-05 18:07:02 +0100415 size_t i;
416 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machatab6de8412012-10-27 19:31:22 +0200417 struct callstack_element *elem = &retp->callstack[i];
418 struct fetch_context *ctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100419 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200420 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100421 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200422 size_t j;
423 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100424 for (j = 0; j < i; ++j) {
Petr Machataf56d1292013-01-07 21:27:54 +0100425 nctx = retp->callstack[j].fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100426 fetch_arg_done(nctx);
Petr Machatab6de8412012-10-27 19:31:22 +0200427 elem->fetch_context = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100428 }
429 goto fail2;
430 }
Petr Machatab6de8412012-10-27 19:31:22 +0200431 elem->fetch_context = nctx;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100432 }
433
Petr Machataf56d1292013-01-07 21:27:54 +0100434 if (elem->arguments != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100435 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100436 if (nargs == NULL
Petr Machataf56d1292013-01-07 21:27:54 +0100437 || val_dict_clone(nargs, elem->arguments) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200438 size_t j;
Petr Machata94078ec2012-01-05 18:07:02 +0100439 for (j = 0; j < i; ++j) {
Petr Machataf56d1292013-01-07 21:27:54 +0100440 nargs = retp->callstack[j].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100441 val_dict_destroy(nargs);
442 free(nargs);
Petr Machatab6de8412012-10-27 19:31:22 +0200443 elem->arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100444 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100445
446 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200447 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100448 * fetch_context. */
449 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200450 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100451 }
Petr Machatab6de8412012-10-27 19:31:22 +0200452 elem->arguments = nargs;
Petr Machata94078ec2012-01-05 18:07:02 +0100453 }
Petr Machata165b5662012-10-27 19:23:12 +0200454
455 /* If it's not a syscall, we need to find the
456 * corresponding library symbol in the cloned
457 * library. */
458 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
459 struct library_symbol *libfunc = elem->c_un.libfunc;
460 int rc = proc_find_symbol(retp, libfunc,
461 NULL, &elem->c_un.libfunc);
462 assert(rc == 0);
463 }
Petr Machata94078ec2012-01-05 18:07:02 +0100464 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100465
Petr Machata5bf47142012-10-27 19:29:00 +0200466 /* At this point, retp is fully initialized, except for OS and
467 * arch parts, and we can call private_process_destroy. */
468 if (os_process_clone(retp, proc) < 0) {
469 private_process_destroy(retp, 0);
470 return -1;
471 }
472 if (arch_process_clone(retp, proc) < 0) {
473 os_process_destroy(retp);
474 private_process_destroy(retp, 0);
475 return -1;
476 }
Petr Machata744f2552012-04-15 04:33:18 +0200477
Petr Machata2b46cfc2012-02-18 11:17:29 +0100478 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100479}
480
Petr Machata3c516d52011-08-18 03:53:18 +0200481static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200482open_one_pid(pid_t pid)
483{
Petr Machata9a5420c2011-07-09 11:21:23 +0200484 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
485
Petr Machata1974dbc2011-08-19 18:58:01 +0200486 /* Get the filename first. Should the trace_pid fail, we can
487 * easily free it, untracing is more work. */
Petr Machata929bd572012-12-17 03:20:34 +0100488 char *filename = pid2name(pid);
489 if (filename == NULL || trace_pid(pid) < 0) {
Petr Machataef0c74d2012-10-27 00:30:57 +0200490 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200491 free(filename);
492 return -1;
493 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100494
Petr Machata929bd572012-12-17 03:20:34 +0100495 struct process *proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200496 if (proc == NULL)
Petr Machataef0c74d2012-10-27 00:30:57 +0200497 goto fail;
498 free(filename);
Petr Machata3ed2a422012-04-06 17:18:55 +0200499 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200500
Petr Machata1974dbc2011-08-19 18:58:01 +0200501 return 0;
502}
503
Petr Machata2b46cfc2012-02-18 11:17:29 +0100504static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100505start_one_pid(struct process *proc, void *data)
Petr Machata1974dbc2011-08-19 18:58:01 +0200506{
507 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100508 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100509}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100510
Petr Machatadf2c88c2013-03-19 17:55:25 +0100511static enum callback_status
512is_main(struct process *proc, struct library *lib, void *data)
513{
514 return CBS_STOP_IF(lib->type == LT_LIBTYPE_MAIN);
515}
516
517void
518process_hit_start(struct process *proc)
519{
520 struct process *leader = proc->leader;
521 assert(leader != NULL);
522
523 struct library *mainlib
524 = proc_each_library(leader, NULL, is_main, NULL);
525 assert(mainlib != NULL);
526 linkmap_init(leader, mainlib->dyn_addr);
527 arch_dynlink_done(leader);
528}
529
Petr Machata9a5420c2011-07-09 11:21:23 +0200530void
531open_pid(pid_t pid)
532{
533 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200534 /* If we are already tracing this guy, we should be seeing all
535 * his children via normal tracing route. */
536 if (pid2proc(pid) != NULL)
537 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200538
Petr Machata3c516d52011-08-18 03:53:18 +0200539 /* First, see if we can attach the requested PID itself. */
Petr Machata8be68ff2013-03-19 18:00:58 +0100540 if (open_one_pid(pid) < 0) {
Petr Machata3c516d52011-08-18 03:53:18 +0200541 fprintf(stderr, "Cannot attach to pid %u: %s\n",
542 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200543 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200544 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200545 }
546
Petr Machata3c516d52011-08-18 03:53:18 +0200547 /* Now attach to all tasks that belong to that PID. There's a
548 * race between process_tasks and open_one_pid. So when we
549 * fail in open_one_pid below, we just do another round.
550 * Chances are that by then that PID will have gone away, and
551 * that's why we have seen the failure. The processes that we
552 * manage to open_one_pid are stopped, so we should eventually
553 * reach a point where process_tasks doesn't give any new
554 * processes (because there's nobody left to produce
555 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200556 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200557 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200558 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200559 pid_t *tasks;
560 size_t ntasks;
561 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200562
Petr Machata3c516d52011-08-18 03:53:18 +0200563 if (process_tasks(pid, &tasks, &ntasks) < 0) {
564 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
565 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100566 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200567 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200568
Petr Machata3c516d52011-08-18 03:53:18 +0200569 have_all = 1;
570 for (i = 0; i < ntasks; ++i)
571 if (pid2proc(tasks[i]) == NULL
Petr Machata8be68ff2013-03-19 18:00:58 +0100572 && open_one_pid(tasks[i]) < 0)
Petr Machata3c516d52011-08-18 03:53:18 +0200573 have_all = 0;
574
Petr Machata9a5420c2011-07-09 11:21:23 +0200575 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200576
Petr Machata1974dbc2011-08-19 18:58:01 +0200577 if (have_all && old_ntasks == ntasks)
578 break;
579 old_ntasks = ntasks;
580 }
581
Petr Machata929bd572012-12-17 03:20:34 +0100582 struct process *leader = pid2proc(pid)->leader;
Petr Machata93d95df2012-04-17 05:16:19 +0200583
584 /* XXX Is there a way to figure out whether _start has
585 * actually already been hit? */
Petr Machatadf2c88c2013-03-19 17:55:25 +0100586 process_hit_start(leader);
Petr Machata93d95df2012-04-17 05:16:19 +0200587
Petr Machata2f9b78e2012-04-16 21:08:54 +0200588 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200589 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200590}
591
Petr Machata2b46cfc2012-02-18 11:17:29 +0100592static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100593find_proc(struct process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200594{
Petr Machata7725d4b2013-03-19 18:03:00 +0100595 return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data);
Petr Machatacebb8842011-07-09 11:14:11 +0200596}
597
Petr Machata929bd572012-12-17 03:20:34 +0100598struct process *
599pid2proc(pid_t pid)
600{
Petr Machatacebb8842011-07-09 11:14:11 +0200601 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
602}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100603
Petr Machata929bd572012-12-17 03:20:34 +0100604static struct process *list_of_processes = NULL;
Petr Machatacebb8842011-07-09 11:14:11 +0200605
Petr Machatacbe29c62011-09-27 02:27:58 +0200606static void
Petr Machata929bd572012-12-17 03:20:34 +0100607unlist_process(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +0200608{
Petr Machatacbe29c62011-09-27 02:27:58 +0200609 if (list_of_processes == proc) {
610 list_of_processes = list_of_processes->next;
611 return;
612 }
613
Petr Machata929bd572012-12-17 03:20:34 +0100614 struct process *tmp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200615 for (tmp = list_of_processes; ; tmp = tmp->next) {
616 /* If the following assert fails, the process wasn't
617 * in the list. */
618 assert(tmp->next != NULL);
619
620 if (tmp->next == proc) {
621 tmp->next = tmp->next->next;
622 return;
623 }
624 }
625}
626
Petr Machata929bd572012-12-17 03:20:34 +0100627struct process *
628each_process(struct process *start_after,
629 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100630 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200631{
Petr Machata929bd572012-12-17 03:20:34 +0100632 struct process *it = start_after == NULL ? list_of_processes
Petr Machata74132a42012-03-16 02:46:18 +0100633 : start_after->next;
634
635 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200636 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100637 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100638 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200639 case CBS_FAIL:
640 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100641 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200642 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100643 case CBS_CONT:
644 break;
645 }
Petr Machatacebb8842011-07-09 11:14:11 +0200646 it = next;
647 }
648 return NULL;
649}
Petr Machata9a5420c2011-07-09 11:21:23 +0200650
Petr Machata929bd572012-12-17 03:20:34 +0100651struct process *
652each_task(struct process *proc, struct process *start_after,
653 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100654 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200655{
Petr Machata74132a42012-03-16 02:46:18 +0100656 assert(proc != NULL);
Petr Machata929bd572012-12-17 03:20:34 +0100657 struct process *it = start_after == NULL ? proc->leader
Petr Machata74132a42012-03-16 02:46:18 +0100658 : start_after->next;
659
Petr Machata9a5420c2011-07-09 11:21:23 +0200660 if (it != NULL) {
Petr Machata929bd572012-12-17 03:20:34 +0100661 struct process *leader = it->leader;
Petr Machata74132a42012-03-16 02:46:18 +0100662 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200663 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100664 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100665 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200666 case CBS_FAIL:
667 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100668 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200669 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100670 case CBS_CONT:
671 break;
672 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200673 it = next;
674 }
675 }
676 return NULL;
677}
678
Petr Machatabaf00e42013-01-07 19:44:43 +0100679static int
Petr Machata929bd572012-12-17 03:20:34 +0100680add_process(struct process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200681{
Petr Machata929bd572012-12-17 03:20:34 +0100682 struct process **leaderp = &list_of_processes;
Petr Machata9a5420c2011-07-09 11:21:23 +0200683 if (proc->pid) {
684 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200685 if (tgid == 0)
686 /* Must have been terminated before we managed
687 * to fully attach. */
Petr Machatabaf00e42013-01-07 19:44:43 +0100688 return -1;
Petr Machata929bd572012-12-17 03:20:34 +0100689 if (tgid == proc->pid) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200690 proc->leader = proc;
Petr Machata929bd572012-12-17 03:20:34 +0100691 } else {
692 struct process *leader = pid2proc(tgid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200693 proc->leader = leader;
694 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200695 leaderp = &leader->next;
696 }
697 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200698
699 if (!was_exec) {
700 proc->next = *leaderp;
701 *leaderp = proc;
702 }
Petr Machatabaf00e42013-01-07 19:44:43 +0100703 return 0;
Petr Machata9a5420c2011-07-09 11:21:23 +0200704}
705
Petr Machatacbe29c62011-09-27 02:27:58 +0200706void
Petr Machata929bd572012-12-17 03:20:34 +0100707change_process_leader(struct process *proc, struct process *leader)
Petr Machatacbe29c62011-09-27 02:27:58 +0200708{
Petr Machata929bd572012-12-17 03:20:34 +0100709 struct process **leaderp = &list_of_processes;
Petr Machatacbe29c62011-09-27 02:27:58 +0200710 if (proc->leader == leader)
711 return;
712
713 assert(leader != NULL);
714 unlist_process(proc);
715 if (proc != leader)
716 leaderp = &leader->next;
717
718 proc->leader = leader;
719 proc->next = *leaderp;
720 *leaderp = proc;
721}
722
Petr Machata2b46cfc2012-02-18 11:17:29 +0100723static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100724clear_leader(struct process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200725{
726 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
727 proc->pid, proc->leader->pid);
728 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100729 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200730}
731
732void
Petr Machata929bd572012-12-17 03:20:34 +0100733remove_process(struct process *proc)
Petr Machatacebb8842011-07-09 11:14:11 +0200734{
Petr Machatacebb8842011-07-09 11:14:11 +0200735 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
736
Petr Machata9a5420c2011-07-09 11:21:23 +0200737 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100738 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200739
Petr Machatacbe29c62011-09-27 02:27:58 +0200740 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100741 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200742 process_destroy(proc);
743 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100744}
Petr Machata4007d742011-07-09 11:29:42 +0200745
746void
Petr Machata929bd572012-12-17 03:20:34 +0100747install_event_handler(struct process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200748{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200749 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200750 assert(proc->event_handler == NULL);
751 proc->event_handler = handler;
752}
753
754void
Petr Machata929bd572012-12-17 03:20:34 +0100755destroy_event_handler(struct process *proc)
Petr Machata4007d742011-07-09 11:29:42 +0200756{
Petr Machata366c2f42012-02-09 19:34:36 +0100757 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200758 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200759 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200760 if (handler->destroy != NULL)
761 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200762 free(handler);
763 proc->event_handler = NULL;
764}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100765
Petr Machataef2fd272012-09-28 00:43:01 +0200766static int
Petr Machata929bd572012-12-17 03:20:34 +0100767breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100768{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200769 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200770 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100771
Petr Machataef2fd272012-09-28 00:43:01 +0200772 /* Don't enable latent or delayed symbols. */
Petr Machata96f04822012-10-31 03:29:11 +0100773 if (libsym->latent || libsym->delayed) {
774 debug(DEBUG_FUNCTION,
775 "delayed and/or latent breakpoint pid=%d, %s@%p",
776 proc->pid, libsym->name, libsym->enter_addr);
Petr Machataef2fd272012-09-28 00:43:01 +0200777 return 0;
Petr Machata96f04822012-10-31 03:29:11 +0100778 }
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200779
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200780 bp_addr = sym2addr(proc, libsym);
781
Petr Machatad5e85562012-04-05 15:18:38 +0200782 /* If there is an artificial breakpoint on the same address,
783 * its libsym will be NULL, and we can smuggle our libsym
784 * there. That artificial breakpoint is there presumably for
785 * the callbacks, which we don't touch. If there is a real
786 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200787 * symbols and ignore extra symbol aliases.
788 *
789 * The other direction is more complicated and currently not
790 * supported. If a breakpoint has custom callbacks, it might
791 * be also custom-allocated, and we would really need to swap
792 * the two: delete the one now in the dictionary, swap values
793 * around, and put the new breakpoint back in. */
Petr Machata98ff3092013-03-08 22:11:36 +0100794 struct breakpoint *bp;
795 if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
Petr Machata8d58d8b2012-11-12 12:46:03 +0100796 /* MIPS backend makes duplicate requests. This is
797 * likely a bug in the backend. Currently there's no
798 * point assigning more than one symbol to a
799 * breakpoint, because when it hits, we won't know
800 * what to print out. But it's easier to fix it here
801 * before someone who understands MIPS has the time to
802 * look into it. So turn the sanity check off on
803 * MIPS. References:
804 *
805 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
806 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
807 */
808#ifndef __mips__
Petr Machata98ff3092013-03-08 22:11:36 +0100809 assert(bp->libsym == NULL);
810 bp->libsym = libsym;
Petr Machata8d58d8b2012-11-12 12:46:03 +0100811#endif
Petr Machataef2fd272012-09-28 00:43:01 +0200812 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200813 }
814
Petr Machata98ff3092013-03-08 22:11:36 +0100815 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200816 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200817 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200818 fail:
819 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200820 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200821 }
822 if (proc_add_breakpoint(proc, bp) < 0) {
823 breakpoint_destroy(bp);
824 goto fail;
825 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100826
Petr Machatafa0c5702012-04-13 18:43:40 +0200827 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200828 proc_remove_breakpoint(proc, bp);
829 breakpoint_destroy(bp);
830 goto fail;
831 }
832
Petr Machataef2fd272012-09-28 00:43:01 +0200833 return 0;
834}
835
836static enum callback_status
837cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
838{
Petr Machata7725d4b2013-03-19 18:03:00 +0100839 return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0);
Petr Machataef2fd272012-09-28 00:43:01 +0200840}
841
842static int
Petr Machata929bd572012-12-17 03:20:34 +0100843proc_activate_latent_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200844 struct library_symbol *libsym)
845{
846 assert(libsym->latent);
847 libsym->latent = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100848 debug(DEBUG_FUNCTION, "activated latent symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200849 return breakpoint_for_symbol(libsym, proc);
850}
851
852int
Petr Machata929bd572012-12-17 03:20:34 +0100853proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200854 struct library_symbol *libsym)
855{
856 assert(libsym->delayed);
857 libsym->delayed = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100858 debug(DEBUG_FUNCTION, "activated delayed symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200859 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100860}
861
Petr Machataa1f76832012-09-28 00:08:00 +0200862static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100863activate_latent_in(struct process *proc, struct library *lib, void *data)
Petr Machataa1f76832012-09-28 00:08:00 +0200864{
865 struct library_exported_name *exported;
866 for (exported = data; exported != NULL; exported = exported->next) {
867 struct library_symbol *libsym = NULL;
868 while ((libsym = library_each_symbol(lib, libsym,
869 library_symbol_named_cb,
870 (void *)exported->name))
871 != NULL)
872 if (libsym->latent
873 && proc_activate_latent_symbol(proc, libsym) < 0)
874 return CBS_FAIL;
875 }
876 return CBS_CONT;
877}
878
Petr Machata2b46cfc2012-02-18 11:17:29 +0100879void
Petr Machata929bd572012-12-17 03:20:34 +0100880proc_add_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100881{
882 assert(lib->next == NULL);
883 lib->next = proc->libraries;
884 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200885 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
886 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100887
Petr Machataef2fd272012-09-28 00:43:01 +0200888 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100889 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200890 while ((libsym = library_each_symbol(lib, libsym,
891 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100892 proc)) != NULL)
Petr Machata3717f292013-01-06 17:52:00 +0100893 fprintf(stderr,
894 "Couldn't insert breakpoint for %s to %d: %s.\n",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200895 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200896
897 /* Look through export list of the new library and compare it
898 * with latent symbols of all libraries (including this
899 * library itself). */
900 struct library *lib2 = NULL;
901 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
902 lib->exported_names)) != NULL)
903 fprintf(stderr,
Petr Machata3717f292013-01-06 17:52:00 +0100904 "Couldn't activate latent symbols for %s in %d: %s.\n",
Petr Machata5c5d4802013-01-08 23:21:10 +0100905 lib2->soname, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100906}
907
908int
Petr Machata929bd572012-12-17 03:20:34 +0100909proc_remove_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100910{
911 struct library **libp;
912 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
913 if (*libp == lib) {
914 *libp = lib->next;
915 return 0;
916 }
917 return -1;
918}
919
920struct library *
Petr Machata929bd572012-12-17 03:20:34 +0100921proc_each_library(struct process *proc, struct library *it,
922 enum callback_status (*cb)(struct process *proc,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100923 struct library *lib, void *data),
924 void *data)
925{
926 if (it == NULL)
927 it = proc->libraries;
Petr Machatac4763a02013-01-08 23:19:14 +0100928 else
929 it = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100930
931 while (it != NULL) {
932 struct library *next = it->next;
933
934 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200935 case CBS_FAIL:
936 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100937 case CBS_STOP:
938 return it;
939 case CBS_CONT:
940 break;
941 }
942
943 it = next;
944 }
945
946 return NULL;
947}
Petr Machata52dbfb12012-03-29 16:38:26 +0200948
Petr Machataf7fee432012-04-19 17:00:53 +0200949static void
Petr Machata929bd572012-12-17 03:20:34 +0100950check_leader(struct process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200951{
Petr Machata52dbfb12012-03-29 16:38:26 +0200952 /* Only the group leader should be getting the breakpoints and
953 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200954 assert(proc->leader != NULL);
955 assert(proc->leader == proc);
956 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200957}
Petr Machata52dbfb12012-03-29 16:38:26 +0200958
Petr Machataf7fee432012-04-19 17:00:53 +0200959int
Petr Machata929bd572012-12-17 03:20:34 +0100960proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machataf7fee432012-04-19 17:00:53 +0200961{
Petr Machatafa0c5702012-04-13 18:43:40 +0200962 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200963 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200964 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200965
Petr Machataa2416362012-04-06 02:43:34 +0200966 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200967 * assert, but that's not necessary right now. Read the
968 * comment in breakpoint_for_symbol. */
Petr Machatad7e4ca82012-11-28 03:38:47 +0100969 assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200970
Petr Machatad7e4ca82012-11-28 03:38:47 +0100971 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200972 fprintf(stderr,
973 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
974 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200975 return -1;
976 }
977
Petr Machata52dbfb12012-03-29 16:38:26 +0200978 return 0;
979}
980
Petr Machataf7fee432012-04-19 17:00:53 +0200981void
Petr Machata929bd572012-12-17 03:20:34 +0100982proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machata52dbfb12012-03-29 16:38:26 +0200983{
Petr Machataf7fee432012-04-19 17:00:53 +0200984 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
985 proc->pid, breakpoint_name(bp), bp->addr);
986 check_leader(proc);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100987 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
988 NULL, NULL, NULL);
989 assert(rc == 0);
Petr Machata52dbfb12012-03-29 16:38:26 +0200990}
Petr Machatad3cc9882012-04-13 21:40:23 +0200991
Petr Machatad3cc9882012-04-13 21:40:23 +0200992struct each_breakpoint_data
993{
Petr Machata929bd572012-12-17 03:20:34 +0100994 struct process *proc;
995 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +0200996 struct breakpoint *bp,
997 void *data);
998 void *cb_data;
999};
1000
Petr Machatad7e4ca82012-11-28 03:38:47 +01001001static enum callback_status
1002each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
Petr Machatad3cc9882012-04-13 21:40:23 +02001003{
1004 struct each_breakpoint_data *data = d;
Petr Machatad7e4ca82012-11-28 03:38:47 +01001005 return data->cb(data->proc, *bpp, data->cb_data);
Petr Machatad3cc9882012-04-13 21:40:23 +02001006}
1007
1008void *
Petr Machata929bd572012-12-17 03:20:34 +01001009proc_each_breakpoint(struct process *proc, void *start,
1010 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +02001011 struct breakpoint *bp,
1012 void *data), void *data)
1013{
1014 struct each_breakpoint_data dd = {
Petr Machatad3cc9882012-04-13 21:40:23 +02001015 .proc = proc,
1016 .cb = cb,
1017 .cb_data = data,
1018 };
Petr Machatad7e4ca82012-11-28 03:38:47 +01001019 return DICT_EACH(proc->breakpoints,
1020 arch_addr_t, struct breakpoint *, start,
1021 &each_breakpoint_cb, &dd);
Petr Machatad3cc9882012-04-13 21:40:23 +02001022}
Petr Machata165b5662012-10-27 19:23:12 +02001023
1024int
Petr Machata929bd572012-12-17 03:20:34 +01001025proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +02001026 struct library **retlib, struct library_symbol **retsym)
1027{
1028 struct library *lib = sym->lib;
1029 assert(lib != NULL);
1030
1031 struct library *flib
1032 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1033 if (flib == NULL)
1034 return -1;
1035
1036 struct library_symbol *fsym
1037 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1038 (char *)sym->name);
1039 if (fsym == NULL)
1040 return -1;
1041
1042 if (retlib != NULL)
1043 *retlib = flib;
1044 if (retsym != NULL)
1045 *retsym = fsym;
1046
1047 return 0;
1048}
Petr Machata32405542012-10-31 03:28:39 +01001049
1050struct library_symbol *
Petr Machata929bd572012-12-17 03:20:34 +01001051proc_each_symbol(struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +01001052 enum callback_status (*cb)(struct library_symbol *, void *),
1053 void *data)
1054{
1055 struct library *lib;
Petr Machata32405542012-10-31 03:28:39 +01001056 for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1057 lib != NULL; lib = lib->next) {
1058 start_after = library_each_symbol(lib, start_after, cb, data);
1059 if (start_after != NULL)
1060 return start_after;
1061 }
1062
1063 return NULL;
1064}
Petr Machata653085a2013-01-15 17:40:40 +01001065
1066#define DEF_READER(NAME, SIZE) \
1067 int \
1068 NAME(struct process *proc, arch_addr_t addr, \
1069 uint##SIZE##_t *lp) \
1070 { \
1071 union { \
1072 uint##SIZE##_t dst; \
1073 char buf[0]; \
1074 } u; \
1075 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \
1076 != sizeof(u.dst)) \
1077 return -1; \
1078 *lp = u.dst; \
1079 return 0; \
1080 }
1081
Petr Machatadc70e762013-01-23 00:02:26 +01001082DEF_READER(proc_read_8, 8)
Petr Machata653085a2013-01-15 17:40:40 +01001083DEF_READER(proc_read_16, 16)
1084DEF_READER(proc_read_32, 32)
1085DEF_READER(proc_read_64, 64)
1086
1087#undef DEF_READER