blob: db3f645db444cf3d38e2f4e97eb57ee1e26ed536 [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"
41#include "proc.h"
42#include "value_dict.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010043
Petr Machata744f2552012-04-15 04:33:18 +020044#ifndef ARCH_HAVE_PROCESS_DATA
45int
Petr Machata929bd572012-12-17 03:20:34 +010046arch_process_init(struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020047{
48 return 0;
49}
50
51void
Petr Machata929bd572012-12-17 03:20:34 +010052arch_process_destroy(struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020053{
54}
55
56int
Petr Machata929bd572012-12-17 03:20:34 +010057arch_process_clone(struct process *retp, struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020058{
59 return 0;
60}
61
62int
Petr Machata929bd572012-12-17 03:20:34 +010063arch_process_exec(struct process *proc)
Petr Machata744f2552012-04-15 04:33:18 +020064{
65 return 0;
66}
67#endif
68
Petr Machata0f6e6d92012-10-26 23:42:17 +020069#ifndef OS_HAVE_PROCESS_DATA
70int
Petr Machata929bd572012-12-17 03:20:34 +010071os_process_init(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020072{
73 return 0;
74}
75
76void
Petr Machata929bd572012-12-17 03:20:34 +010077os_process_destroy(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020078{
79}
80
81int
Petr Machata929bd572012-12-17 03:20:34 +010082os_process_clone(struct process *retp, struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020083{
84 return 0;
85}
86
87int
Petr Machata929bd572012-12-17 03:20:34 +010088os_process_exec(struct process *proc)
Petr Machata0f6e6d92012-10-26 23:42:17 +020089{
90 return 0;
91}
92#endif
93
Petr Machata93d95df2012-04-17 05:16:19 +020094#ifndef ARCH_HAVE_DYNLINK_DONE
95void
Petr Machata929bd572012-12-17 03:20:34 +010096arch_dynlink_done(struct process *proc)
Petr Machata93d95df2012-04-17 05:16:19 +020097{
98}
99#endif
100
Petr Machata929bd572012-12-17 03:20:34 +0100101static void add_process(struct process *proc, int was_exec);
102static void unlist_process(struct process *proc);
Petr Machata44965c72012-04-06 19:59:20 +0200103
Petr Machatae677c7e2012-10-26 22:23:43 +0200104static void
Petr Machata929bd572012-12-17 03:20:34 +0100105destroy_unwind(struct process *proc)
Petr Machatae677c7e2012-10-26 22:23:43 +0200106{
107#if defined(HAVE_LIBUNWIND)
108 _UPT_destroy(proc->unwind_priv);
109 unw_destroy_addr_space(proc->unwind_as);
110#endif /* defined(HAVE_LIBUNWIND) */
111}
112
Petr Machata2b46cfc2012-02-18 11:17:29 +0100113static int
Petr Machata929bd572012-12-17 03:20:34 +0100114process_bare_init(struct process *proc, const char *filename,
Petr Machata3d0c91c2012-04-14 02:37:38 +0200115 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100116{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200117 if (!was_exec) {
118 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +0200119
Petr Machata3d0c91c2012-04-14 02:37:38 +0200120 proc->filename = strdup(filename);
121 if (proc->filename == NULL) {
122 fail:
123 free(proc->filename);
124 if (proc->breakpoints != NULL)
125 dict_clear(proc->breakpoints);
126 return -1;
127 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100128 }
129
130 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +0200131 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200132 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100133 if (proc->leader == NULL)
134 goto fail;
135
136 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +0200137 proc->breakpoints = dict_init(target_address_hash,
138 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100139 if (proc->breakpoints == NULL)
140 goto fail;
141 } else {
142 proc->breakpoints = NULL;
143 }
144
Joe Damatoab3b72c2010-10-31 00:21:53 -0700145#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +0200146 proc->unwind_priv = _UPT_create(pid);
147 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700148#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -0700149
Petr Machata2b46cfc2012-02-18 11:17:29 +0100150 return 0;
151}
152
153static void
Petr Machata929bd572012-12-17 03:20:34 +0100154process_bare_destroy(struct process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100155{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100156 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200157 if (!was_exec) {
158 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200159 unlist_process(proc);
Petr Machatae677c7e2012-10-26 22:23:43 +0200160 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200161 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100162}
163
Petr Machata3d0c91c2012-04-14 02:37:38 +0200164static int
Petr Machata929bd572012-12-17 03:20:34 +0100165process_init_main(struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100166{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200167 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200168 fprintf(stderr, "failed to init breakpoints %d\n",
169 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200170 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200171 }
172
Petr Machata2b46cfc2012-02-18 11:17:29 +0100173 return 0;
174}
175
Petr Machata3d0c91c2012-04-14 02:37:38 +0200176int
Petr Machata929bd572012-12-17 03:20:34 +0100177process_init(struct process *proc, const char *filename, pid_t pid)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178{
179 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200180 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200181 fprintf(stderr, "failed to initialize process %d: %s\n",
182 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200183 return -1;
184 }
185
Petr Machata0f6e6d92012-10-26 23:42:17 +0200186 if (os_process_init(proc) < 0) {
187 process_bare_destroy(proc, 0);
188 goto fail;
189 }
190
Petr Machata744f2552012-04-15 04:33:18 +0200191 if (arch_process_init(proc) < 0) {
Petr Machata0f6e6d92012-10-26 23:42:17 +0200192 os_process_destroy(proc);
Petr Machata744f2552012-04-15 04:33:18 +0200193 process_bare_destroy(proc, 0);
194 goto fail;
195 }
196
Petr Machata218c5ff2012-04-15 04:22:39 +0200197 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200198 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200199 if (process_init_main(proc) < 0) {
200 process_bare_destroy(proc, 0);
201 goto fail;
202 }
203 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200204}
205
Petr Machata8ead1cd2012-04-24 18:13:09 +0200206static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100207destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200208{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200209 breakpoint_destroy(bp);
210 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200211 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200212}
213
Petr Machatae0e89ed2012-10-26 22:25:33 +0200214// XXX see comment in handle_event.c
Petr Machata929bd572012-12-17 03:20:34 +0100215void callstack_pop(struct process *proc);
Petr Machatae0e89ed2012-10-26 22:25:33 +0200216
Petr Machata3d0c91c2012-04-14 02:37:38 +0200217static void
Petr Machata929bd572012-12-17 03:20:34 +0100218private_process_destroy(struct process *proc, int was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200219{
Petr Machatae0e89ed2012-10-26 22:25:33 +0200220 /* Pop remaining stack elements. */
221 while (proc->callstack_depth > 0) {
222 /* When this is called just before a process is
223 * destroyed, the breakpoints should either have been
224 * retracted by now, or were killed by exec. In any
225 * case, it's safe to pretend that there are no
226 * breakpoints associated with the stack elements, so
227 * that stack_pop doesn't attempt to destroy them. */
228 size_t i = proc->callstack_depth - 1;
229 if (!proc->callstack[i].is_syscall)
230 proc->callstack[i].return_addr = 0;
231
232 callstack_pop(proc);
233 }
234
235 if (!was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200236 free(proc->filename);
237
Petr Machata8ead1cd2012-04-24 18:13:09 +0200238 /* Libraries and symbols. This is only relevant in
239 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200240 struct library *lib;
241 for (lib = proc->libraries; lib != NULL; ) {
242 struct library *next = lib->next;
243 library_destroy(lib);
244 free(lib);
245 lib = next;
246 }
247 proc->libraries = NULL;
248
249 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200250 if (proc->breakpoints != NULL) {
251 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
252 dict_clear(proc->breakpoints);
253 proc->breakpoints = NULL;
254 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200255
256 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200257}
258
259void
Petr Machata929bd572012-12-17 03:20:34 +0100260process_destroy(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200261{
Petr Machata744f2552012-04-15 04:33:18 +0200262 arch_process_destroy(proc);
Petr Machata0f6e6d92012-10-26 23:42:17 +0200263 os_process_destroy(proc);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200264 private_process_destroy(proc, 0);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200265}
266
267int
Petr Machata929bd572012-12-17 03:20:34 +0100268process_exec(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200269{
Petr Machata0f6e6d92012-10-26 23:42:17 +0200270 /* Call exec handlers first, before we destroy the main
Petr Machata3cc0cd12012-10-26 22:30:51 +0200271 * state. */
Petr Machata0f6e6d92012-10-26 23:42:17 +0200272 if (arch_process_exec(proc) < 0
273 || os_process_exec(proc) < 0)
Petr Machata744f2552012-04-15 04:33:18 +0200274 return -1;
275
Petr Machata3d0c91c2012-04-14 02:37:38 +0200276 private_process_destroy(proc, 1);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200277
Petr Machata3d0c91c2012-04-14 02:37:38 +0200278 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
279 return -1;
280 if (process_init_main(proc) < 0) {
281 process_bare_destroy(proc, 1);
282 return -1;
283 }
284 return 0;
285}
286
Petr Machata929bd572012-12-17 03:20:34 +0100287struct process *
Petr Machata75934ad2012-04-14 02:28:03 +0200288open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290 assert(pid != 0);
Petr Machata929bd572012-12-17 03:20:34 +0100291 struct process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200292 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200293 free(proc);
294 return NULL;
295 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100296 return proc;
297}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100298
Petr Machata2b46cfc2012-02-18 11:17:29 +0100299struct clone_single_bp_data {
Petr Machata929bd572012-12-17 03:20:34 +0100300 struct process *old_proc;
301 struct process *new_proc;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100302 int error;
303};
304
Petr Machata2b46cfc2012-02-18 11:17:29 +0100305static void
306clone_single_bp(void *key, void *value, void *u)
307{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100308 struct breakpoint *bp = value;
309 struct clone_single_bp_data *data = u;
310
Petr Machata81bc82c2012-10-27 19:26:44 +0200311 /* Don't bother if there were errors anyway. */
312 if (data->error != 0)
313 return;
314
Petr Machata2b46cfc2012-02-18 11:17:29 +0100315 struct breakpoint *clone = malloc(sizeof(*clone));
316 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200317 || breakpoint_clone(clone, data->new_proc,
318 bp, data->old_proc) < 0) {
319 fail:
320 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100321 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100322 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200323 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
324 breakpoint_destroy(clone);
325 goto fail;
326 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100327}
328
329int
Petr Machata929bd572012-12-17 03:20:34 +0100330process_clone(struct process *retp, struct process *proc, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100331{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200332 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200333 fail1:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200334 fprintf(stderr, "failed to clone process %d->%d : %s\n",
335 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100336 return -1;
337 }
338
Petr Machatacf1679a2012-04-06 19:56:17 +0200339 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200340 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400341 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200342
Petr Machata2b46cfc2012-02-18 11:17:29 +0100343 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200344 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100345 return 0;
346
347 /* Clone symbols first so that we can clone and relink
348 * breakpoints. */
349 struct library *lib;
350 struct library **nlibp = &retp->libraries;
Petr Machata1e339e02012-10-27 19:33:20 +0200351 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100352 *nlibp = malloc(sizeof(**nlibp));
353 if (*nlibp == NULL
354 || library_clone(*nlibp, lib) < 0) {
355 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200356 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100357
358 /* Error when cloning. Unroll what was done. */
359 for (lib = retp->libraries; lib != NULL; ) {
360 struct library *next = lib->next;
361 library_destroy(lib);
362 free(lib);
363 lib = next;
364 }
Petr Machataba1664b2012-04-28 14:59:05 +0200365 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100366 }
367
368 nlibp = &(*nlibp)->next;
369 }
370
371 /* Now clone breakpoints. Symbol relinking is done in
372 * clone_single_bp. */
373 struct clone_single_bp_data data = {
374 .old_proc = proc,
375 .new_proc = retp,
376 .error = 0,
377 };
Petr Machata1e339e02012-10-27 19:33:20 +0200378 dict_apply_to_all(proc->leader->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100379 if (data.error < 0)
380 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100381
Petr Machataded6f972012-04-13 23:15:48 +0200382 /* And finally the call stack. */
Petr Machatab6de8412012-10-27 19:31:22 +0200383 /* XXX clearly the callstack handling should be moved to a
384 * separate module and this whole business extracted to
385 * callstack_clone, or callstack_element_clone. */
Petr Machataded6f972012-04-13 23:15:48 +0200386 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
387 retp->callstack_depth = proc->callstack_depth;
388
Petr Machata94078ec2012-01-05 18:07:02 +0100389 size_t i;
390 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machatab6de8412012-10-27 19:31:22 +0200391 struct callstack_element *elem = &retp->callstack[i];
392 struct fetch_context *ctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100393 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200394 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100395 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200396 size_t j;
397 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100398 for (j = 0; j < i; ++j) {
Petr Machatab6de8412012-10-27 19:31:22 +0200399 nctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100400 fetch_arg_done(nctx);
Petr Machatab6de8412012-10-27 19:31:22 +0200401 elem->fetch_context = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100402 }
403 goto fail2;
404 }
Petr Machatab6de8412012-10-27 19:31:22 +0200405 elem->fetch_context = nctx;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100406 }
407
Petr Machatab6de8412012-10-27 19:31:22 +0200408 struct value_dict *args = elem->arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100409 if (args != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100410 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100411 if (nargs == NULL
412 || val_dict_clone(nargs, args) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200413 size_t j;
Petr Machata94078ec2012-01-05 18:07:02 +0100414 for (j = 0; j < i; ++j) {
Petr Machatab6de8412012-10-27 19:31:22 +0200415 nargs = elem->arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100416 val_dict_destroy(nargs);
417 free(nargs);
Petr Machatab6de8412012-10-27 19:31:22 +0200418 elem->arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100419 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100420
421 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200422 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100423 * fetch_context. */
424 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200425 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100426 }
Petr Machatab6de8412012-10-27 19:31:22 +0200427 elem->arguments = nargs;
Petr Machata94078ec2012-01-05 18:07:02 +0100428 }
Petr Machata165b5662012-10-27 19:23:12 +0200429
430 /* If it's not a syscall, we need to find the
431 * corresponding library symbol in the cloned
432 * library. */
433 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
434 struct library_symbol *libfunc = elem->c_un.libfunc;
435 int rc = proc_find_symbol(retp, libfunc,
436 NULL, &elem->c_un.libfunc);
437 assert(rc == 0);
438 }
Petr Machata94078ec2012-01-05 18:07:02 +0100439 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100440
Petr Machata5bf47142012-10-27 19:29:00 +0200441 /* At this point, retp is fully initialized, except for OS and
442 * arch parts, and we can call private_process_destroy. */
443 if (os_process_clone(retp, proc) < 0) {
444 private_process_destroy(retp, 0);
445 return -1;
446 }
447 if (arch_process_clone(retp, proc) < 0) {
448 os_process_destroy(retp);
449 private_process_destroy(retp, 0);
450 return -1;
451 }
Petr Machata744f2552012-04-15 04:33:18 +0200452
Petr Machata2b46cfc2012-02-18 11:17:29 +0100453 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100454}
455
Petr Machata3c516d52011-08-18 03:53:18 +0200456static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200457open_one_pid(pid_t pid)
458{
Petr Machata9a5420c2011-07-09 11:21:23 +0200459 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
460
Petr Machata1974dbc2011-08-19 18:58:01 +0200461 /* Get the filename first. Should the trace_pid fail, we can
462 * easily free it, untracing is more work. */
Petr Machata929bd572012-12-17 03:20:34 +0100463 char *filename = pid2name(pid);
464 if (filename == NULL || trace_pid(pid) < 0) {
Petr Machataef0c74d2012-10-27 00:30:57 +0200465 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200466 free(filename);
467 return -1;
468 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100469
Petr Machata929bd572012-12-17 03:20:34 +0100470 struct process *proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200471 if (proc == NULL)
Petr Machataef0c74d2012-10-27 00:30:57 +0200472 goto fail;
473 free(filename);
Petr Machata3ed2a422012-04-06 17:18:55 +0200474 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200475
Petr Machata1974dbc2011-08-19 18:58:01 +0200476 return 0;
477}
478
Petr Machata2b46cfc2012-02-18 11:17:29 +0100479static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100480start_one_pid(struct process *proc, void *data)
Petr Machata1974dbc2011-08-19 18:58:01 +0200481{
482 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100483 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100484}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100485
Petr Machata9a5420c2011-07-09 11:21:23 +0200486void
487open_pid(pid_t pid)
488{
489 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200490 /* If we are already tracing this guy, we should be seeing all
491 * his children via normal tracing route. */
492 if (pid2proc(pid) != NULL)
493 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200494
Petr Machata3c516d52011-08-18 03:53:18 +0200495 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200496 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200497 fprintf(stderr, "Cannot attach to pid %u: %s\n",
498 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200499 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200500 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200501 }
502
Petr Machata3c516d52011-08-18 03:53:18 +0200503 /* Now attach to all tasks that belong to that PID. There's a
504 * race between process_tasks and open_one_pid. So when we
505 * fail in open_one_pid below, we just do another round.
506 * Chances are that by then that PID will have gone away, and
507 * that's why we have seen the failure. The processes that we
508 * manage to open_one_pid are stopped, so we should eventually
509 * reach a point where process_tasks doesn't give any new
510 * processes (because there's nobody left to produce
511 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200512 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200513 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200514 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200515 pid_t *tasks;
516 size_t ntasks;
517 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200518
Petr Machata3c516d52011-08-18 03:53:18 +0200519 if (process_tasks(pid, &tasks, &ntasks) < 0) {
520 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
521 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100522 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200523 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200524
Petr Machata3c516d52011-08-18 03:53:18 +0200525 have_all = 1;
526 for (i = 0; i < ntasks; ++i)
527 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200528 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200529 have_all = 0;
530
Petr Machata9a5420c2011-07-09 11:21:23 +0200531 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200532
Petr Machata1974dbc2011-08-19 18:58:01 +0200533 if (have_all && old_ntasks == ntasks)
534 break;
535 old_ntasks = ntasks;
536 }
537
Petr Machata929bd572012-12-17 03:20:34 +0100538 struct process *leader = pid2proc(pid)->leader;
Petr Machata93d95df2012-04-17 05:16:19 +0200539
540 /* XXX Is there a way to figure out whether _start has
541 * actually already been hit? */
542 arch_dynlink_done(leader);
543
Petr Machata2f9b78e2012-04-16 21:08:54 +0200544 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200545 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200546}
547
Petr Machata2b46cfc2012-02-18 11:17:29 +0100548static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100549find_proc(struct process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200550{
551 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100552 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200553}
554
Petr Machata929bd572012-12-17 03:20:34 +0100555struct process *
556pid2proc(pid_t pid)
557{
Petr Machatacebb8842011-07-09 11:14:11 +0200558 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
559}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100560
Petr Machata929bd572012-12-17 03:20:34 +0100561static struct process *list_of_processes = NULL;
Petr Machatacebb8842011-07-09 11:14:11 +0200562
Petr Machatacbe29c62011-09-27 02:27:58 +0200563static void
Petr Machata929bd572012-12-17 03:20:34 +0100564unlist_process(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +0200565{
Petr Machatacbe29c62011-09-27 02:27:58 +0200566 if (list_of_processes == proc) {
567 list_of_processes = list_of_processes->next;
568 return;
569 }
570
Petr Machata929bd572012-12-17 03:20:34 +0100571 struct process *tmp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200572 for (tmp = list_of_processes; ; tmp = tmp->next) {
573 /* If the following assert fails, the process wasn't
574 * in the list. */
575 assert(tmp->next != NULL);
576
577 if (tmp->next == proc) {
578 tmp->next = tmp->next->next;
579 return;
580 }
581 }
582}
583
Petr Machata929bd572012-12-17 03:20:34 +0100584struct process *
585each_process(struct process *start_after,
586 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100587 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200588{
Petr Machata929bd572012-12-17 03:20:34 +0100589 struct process *it = start_after == NULL ? list_of_processes
Petr Machata74132a42012-03-16 02:46:18 +0100590 : start_after->next;
591
592 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200593 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100594 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100595 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200596 case CBS_FAIL:
597 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100598 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200599 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100600 case CBS_CONT:
601 break;
602 }
Petr Machatacebb8842011-07-09 11:14:11 +0200603 it = next;
604 }
605 return NULL;
606}
Petr Machata9a5420c2011-07-09 11:21:23 +0200607
Petr Machata929bd572012-12-17 03:20:34 +0100608struct process *
609each_task(struct process *proc, struct process *start_after,
610 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100611 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200612{
Petr Machata74132a42012-03-16 02:46:18 +0100613 assert(proc != NULL);
Petr Machata929bd572012-12-17 03:20:34 +0100614 struct process *it = start_after == NULL ? proc->leader
Petr Machata74132a42012-03-16 02:46:18 +0100615 : start_after->next;
616
Petr Machata9a5420c2011-07-09 11:21:23 +0200617 if (it != NULL) {
Petr Machata929bd572012-12-17 03:20:34 +0100618 struct process *leader = it->leader;
Petr Machata74132a42012-03-16 02:46:18 +0100619 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200620 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100621 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100622 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200623 case CBS_FAIL:
624 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100625 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200626 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100627 case CBS_CONT:
628 break;
629 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200630 it = next;
631 }
632 }
633 return NULL;
634}
635
Petr Machata44965c72012-04-06 19:59:20 +0200636static void
Petr Machata929bd572012-12-17 03:20:34 +0100637add_process(struct process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200638{
Petr Machata929bd572012-12-17 03:20:34 +0100639 struct process **leaderp = &list_of_processes;
Petr Machata9a5420c2011-07-09 11:21:23 +0200640 if (proc->pid) {
641 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200642 if (tgid == 0)
643 /* Must have been terminated before we managed
644 * to fully attach. */
645 return;
Petr Machata929bd572012-12-17 03:20:34 +0100646 if (tgid == proc->pid) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200647 proc->leader = proc;
Petr Machata929bd572012-12-17 03:20:34 +0100648 } else {
649 struct process *leader = pid2proc(tgid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200650 proc->leader = leader;
651 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200652 leaderp = &leader->next;
653 }
654 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200655
656 if (!was_exec) {
657 proc->next = *leaderp;
658 *leaderp = proc;
659 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200660}
661
Petr Machatacbe29c62011-09-27 02:27:58 +0200662void
Petr Machata929bd572012-12-17 03:20:34 +0100663change_process_leader(struct process *proc, struct process *leader)
Petr Machatacbe29c62011-09-27 02:27:58 +0200664{
Petr Machata929bd572012-12-17 03:20:34 +0100665 struct process **leaderp = &list_of_processes;
Petr Machatacbe29c62011-09-27 02:27:58 +0200666 if (proc->leader == leader)
667 return;
668
669 assert(leader != NULL);
670 unlist_process(proc);
671 if (proc != leader)
672 leaderp = &leader->next;
673
674 proc->leader = leader;
675 proc->next = *leaderp;
676 *leaderp = proc;
677}
678
Petr Machata2b46cfc2012-02-18 11:17:29 +0100679static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100680clear_leader(struct process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200681{
682 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
683 proc->pid, proc->leader->pid);
684 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100685 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200686}
687
688void
Petr Machata929bd572012-12-17 03:20:34 +0100689remove_process(struct process *proc)
Petr Machatacebb8842011-07-09 11:14:11 +0200690{
Petr Machatacebb8842011-07-09 11:14:11 +0200691 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
692
Petr Machata9a5420c2011-07-09 11:21:23 +0200693 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100694 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200695
Petr Machatacbe29c62011-09-27 02:27:58 +0200696 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100697 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200698 process_destroy(proc);
699 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100700}
Petr Machata4007d742011-07-09 11:29:42 +0200701
702void
Petr Machata929bd572012-12-17 03:20:34 +0100703install_event_handler(struct process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200704{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200705 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200706 assert(proc->event_handler == NULL);
707 proc->event_handler = handler;
708}
709
710void
Petr Machata929bd572012-12-17 03:20:34 +0100711destroy_event_handler(struct process *proc)
Petr Machata4007d742011-07-09 11:29:42 +0200712{
Petr Machata366c2f42012-02-09 19:34:36 +0100713 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200714 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200715 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200716 if (handler->destroy != NULL)
717 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200718 free(handler);
719 proc->event_handler = NULL;
720}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100721
Petr Machataef2fd272012-09-28 00:43:01 +0200722static int
Petr Machata929bd572012-12-17 03:20:34 +0100723breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100724{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200725 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200726 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100727
Petr Machataef2fd272012-09-28 00:43:01 +0200728 /* Don't enable latent or delayed symbols. */
Petr Machata96f04822012-10-31 03:29:11 +0100729 if (libsym->latent || libsym->delayed) {
730 debug(DEBUG_FUNCTION,
731 "delayed and/or latent breakpoint pid=%d, %s@%p",
732 proc->pid, libsym->name, libsym->enter_addr);
Petr Machataef2fd272012-09-28 00:43:01 +0200733 return 0;
Petr Machata96f04822012-10-31 03:29:11 +0100734 }
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200735
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200736 bp_addr = sym2addr(proc, libsym);
737
Petr Machatad5e85562012-04-05 15:18:38 +0200738 /* If there is an artificial breakpoint on the same address,
739 * its libsym will be NULL, and we can smuggle our libsym
740 * there. That artificial breakpoint is there presumably for
741 * the callbacks, which we don't touch. If there is a real
742 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200743 * symbols and ignore extra symbol aliases.
744 *
745 * The other direction is more complicated and currently not
746 * supported. If a breakpoint has custom callbacks, it might
747 * be also custom-allocated, and we would really need to swap
748 * the two: delete the one now in the dictionary, swap values
749 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200750 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200751 bp_addr);
Petr Machatad5e85562012-04-05 15:18:38 +0200752 if (bp != NULL) {
Petr Machata8d58d8b2012-11-12 12:46:03 +0100753 /* MIPS backend makes duplicate requests. This is
754 * likely a bug in the backend. Currently there's no
755 * point assigning more than one symbol to a
756 * breakpoint, because when it hits, we won't know
757 * what to print out. But it's easier to fix it here
758 * before someone who understands MIPS has the time to
759 * look into it. So turn the sanity check off on
760 * MIPS. References:
761 *
762 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
763 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
764 */
765#ifndef __mips__
Petr Machatad5e85562012-04-05 15:18:38 +0200766 assert(bp->libsym == NULL);
767 bp->libsym = libsym;
Petr Machata8d58d8b2012-11-12 12:46:03 +0100768#endif
Petr Machataef2fd272012-09-28 00:43:01 +0200769 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200770 }
771
772 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200773 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200774 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200775 fail:
776 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200777 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200778 }
779 if (proc_add_breakpoint(proc, bp) < 0) {
780 breakpoint_destroy(bp);
781 goto fail;
782 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100783
Petr Machatafa0c5702012-04-13 18:43:40 +0200784 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200785 proc_remove_breakpoint(proc, bp);
786 breakpoint_destroy(bp);
787 goto fail;
788 }
789
Petr Machataef2fd272012-09-28 00:43:01 +0200790 return 0;
791}
792
793static enum callback_status
794cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
795{
796 return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
797}
798
799static int
Petr Machata929bd572012-12-17 03:20:34 +0100800proc_activate_latent_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200801 struct library_symbol *libsym)
802{
803 assert(libsym->latent);
804 libsym->latent = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100805 debug(DEBUG_FUNCTION, "activated latent symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200806 return breakpoint_for_symbol(libsym, proc);
807}
808
809int
Petr Machata929bd572012-12-17 03:20:34 +0100810proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200811 struct library_symbol *libsym)
812{
813 assert(libsym->delayed);
814 libsym->delayed = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100815 debug(DEBUG_FUNCTION, "activated delayed symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200816 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100817}
818
Petr Machataa1f76832012-09-28 00:08:00 +0200819static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100820activate_latent_in(struct process *proc, struct library *lib, void *data)
Petr Machataa1f76832012-09-28 00:08:00 +0200821{
822 struct library_exported_name *exported;
823 for (exported = data; exported != NULL; exported = exported->next) {
824 struct library_symbol *libsym = NULL;
825 while ((libsym = library_each_symbol(lib, libsym,
826 library_symbol_named_cb,
827 (void *)exported->name))
828 != NULL)
829 if (libsym->latent
830 && proc_activate_latent_symbol(proc, libsym) < 0)
831 return CBS_FAIL;
832 }
833 return CBS_CONT;
834}
835
Petr Machata2b46cfc2012-02-18 11:17:29 +0100836void
Petr Machata929bd572012-12-17 03:20:34 +0100837proc_add_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100838{
839 assert(lib->next == NULL);
840 lib->next = proc->libraries;
841 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200842 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
843 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100844
Petr Machataef2fd272012-09-28 00:43:01 +0200845 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100846 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200847 while ((libsym = library_each_symbol(lib, libsym,
848 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100849 proc)) != NULL)
Petr Machataef2fd272012-09-28 00:43:01 +0200850 fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s.",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200851 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200852
853 /* Look through export list of the new library and compare it
854 * with latent symbols of all libraries (including this
855 * library itself). */
856 struct library *lib2 = NULL;
857 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
858 lib->exported_names)) != NULL)
859 fprintf(stderr,
860 "Couldn't activate latent symbols for %s in %d: %s.",
861 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100862}
863
864int
Petr Machata929bd572012-12-17 03:20:34 +0100865proc_remove_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100866{
867 struct library **libp;
868 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
869 if (*libp == lib) {
870 *libp = lib->next;
871 return 0;
872 }
873 return -1;
874}
875
876struct library *
Petr Machata929bd572012-12-17 03:20:34 +0100877proc_each_library(struct process *proc, struct library *it,
878 enum callback_status (*cb)(struct process *proc,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100879 struct library *lib, void *data),
880 void *data)
881{
882 if (it == NULL)
883 it = proc->libraries;
884
885 while (it != NULL) {
886 struct library *next = it->next;
887
888 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200889 case CBS_FAIL:
890 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100891 case CBS_STOP:
892 return it;
893 case CBS_CONT:
894 break;
895 }
896
897 it = next;
898 }
899
900 return NULL;
901}
Petr Machata52dbfb12012-03-29 16:38:26 +0200902
Petr Machataf7fee432012-04-19 17:00:53 +0200903static void
Petr Machata929bd572012-12-17 03:20:34 +0100904check_leader(struct process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200905{
Petr Machata52dbfb12012-03-29 16:38:26 +0200906 /* Only the group leader should be getting the breakpoints and
907 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200908 assert(proc->leader != NULL);
909 assert(proc->leader == proc);
910 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200911}
Petr Machata52dbfb12012-03-29 16:38:26 +0200912
Petr Machataf7fee432012-04-19 17:00:53 +0200913int
Petr Machata929bd572012-12-17 03:20:34 +0100914proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machataf7fee432012-04-19 17:00:53 +0200915{
Petr Machatafa0c5702012-04-13 18:43:40 +0200916 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200917 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200918 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200919
Petr Machataa2416362012-04-06 02:43:34 +0200920 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200921 * assert, but that's not necessary right now. Read the
922 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200923 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200924
Petr Machatafa0c5702012-04-13 18:43:40 +0200925 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200926 fprintf(stderr,
927 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
928 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200929 return -1;
930 }
931
Petr Machata52dbfb12012-03-29 16:38:26 +0200932 return 0;
933}
934
Petr Machataf7fee432012-04-19 17:00:53 +0200935void
Petr Machata929bd572012-12-17 03:20:34 +0100936proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machata52dbfb12012-03-29 16:38:26 +0200937{
Petr Machataf7fee432012-04-19 17:00:53 +0200938 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
939 proc->pid, breakpoint_name(bp), bp->addr);
940 check_leader(proc);
941 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
942 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200943}
Petr Machatad3cc9882012-04-13 21:40:23 +0200944
945/* Dict doesn't support iteration restarts, so here's this contraption
946 * for now. XXX add restarts to dict. */
947struct each_breakpoint_data
948{
949 void *start;
950 void *end;
Petr Machata929bd572012-12-17 03:20:34 +0100951 struct process *proc;
952 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +0200953 struct breakpoint *bp,
954 void *data);
955 void *cb_data;
956};
957
958static void
959each_breakpoint_cb(void *key, void *value, void *d)
960{
961 struct each_breakpoint_data *data = d;
962 if (data->end != NULL)
963 return;
964 if (data->start == key)
965 data->start = NULL;
966
967 if (data->start == NULL) {
968 switch (data->cb(data->proc, value, data->cb_data)) {
969 case CBS_FAIL:
970 /* XXX handle me */
971 case CBS_STOP:
972 data->end = key;
973 case CBS_CONT:
974 return;
975 }
976 }
977}
978
979void *
Petr Machata929bd572012-12-17 03:20:34 +0100980proc_each_breakpoint(struct process *proc, void *start,
981 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +0200982 struct breakpoint *bp,
983 void *data), void *data)
984{
985 struct each_breakpoint_data dd = {
986 .start = start,
987 .proc = proc,
988 .cb = cb,
989 .cb_data = data,
990 };
991 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
992 return dd.end;
993}
Petr Machata165b5662012-10-27 19:23:12 +0200994
995int
Petr Machata929bd572012-12-17 03:20:34 +0100996proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +0200997 struct library **retlib, struct library_symbol **retsym)
998{
999 struct library *lib = sym->lib;
1000 assert(lib != NULL);
1001
1002 struct library *flib
1003 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1004 if (flib == NULL)
1005 return -1;
1006
1007 struct library_symbol *fsym
1008 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1009 (char *)sym->name);
1010 if (fsym == NULL)
1011 return -1;
1012
1013 if (retlib != NULL)
1014 *retlib = flib;
1015 if (retsym != NULL)
1016 *retsym = fsym;
1017
1018 return 0;
1019}
Petr Machata32405542012-10-31 03:28:39 +01001020
1021struct library_symbol *
Petr Machata929bd572012-12-17 03:20:34 +01001022proc_each_symbol(struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +01001023 enum callback_status (*cb)(struct library_symbol *, void *),
1024 void *data)
1025{
1026 struct library *lib;
Petr Machata32405542012-10-31 03:28:39 +01001027 for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1028 lib != NULL; lib = lib->next) {
1029 start_after = library_each_symbol(lib, start_after, cb, data);
1030 if (start_after != NULL)
1031 return start_after;
1032 }
1033
1034 return NULL;
1035}
Petr Machata653085a2013-01-15 17:40:40 +01001036
1037#define DEF_READER(NAME, SIZE) \
1038 int \
1039 NAME(struct process *proc, arch_addr_t addr, \
1040 uint##SIZE##_t *lp) \
1041 { \
1042 union { \
1043 uint##SIZE##_t dst; \
1044 char buf[0]; \
1045 } u; \
1046 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \
1047 != sizeof(u.dst)) \
1048 return -1; \
1049 *lp = u.dst; \
1050 return 0; \
1051 }
1052
1053DEF_READER(proc_read_16, 16)
1054DEF_READER(proc_read_32, 32)
1055DEF_READER(proc_read_64, 64)
1056
1057#undef DEF_READER