blob: ffc2029c39c1997f47c18b9bb049b3c6fe0d390e [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);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100124 if (proc->breakpoints != NULL) {
125 dict_destroy(proc->breakpoints,
126 NULL, NULL, NULL);
127 free(proc->breakpoints);
128 proc->breakpoints = NULL;
129 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200130 return -1;
131 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100132 }
133
134 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +0200135 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200136 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100137 if (proc->leader == NULL)
138 goto fail;
139
140 if (proc->leader == proc) {
Petr Machatad7e4ca82012-11-28 03:38:47 +0100141 proc->breakpoints = malloc(sizeof(*proc->breakpoints));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100142 if (proc->breakpoints == NULL)
143 goto fail;
Petr Machatad7e4ca82012-11-28 03:38:47 +0100144 DICT_INIT(proc->breakpoints,
145 arch_addr_t, struct breakpoint *,
146 arch_addr_hash, arch_addr_eq, NULL);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100147 } else {
148 proc->breakpoints = NULL;
149 }
150
Joe Damatoab3b72c2010-10-31 00:21:53 -0700151#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +0200152 proc->unwind_priv = _UPT_create(pid);
153 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700154#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -0700155
Petr Machata2b46cfc2012-02-18 11:17:29 +0100156 return 0;
157}
158
159static void
Petr Machata929bd572012-12-17 03:20:34 +0100160process_bare_destroy(struct process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100161{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100162 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
163 free(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200164 if (!was_exec) {
165 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200166 unlist_process(proc);
Petr Machatae677c7e2012-10-26 22:23:43 +0200167 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200168 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100169}
170
Petr Machata3d0c91c2012-04-14 02:37:38 +0200171static int
Petr Machata929bd572012-12-17 03:20:34 +0100172process_init_main(struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100173{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200174 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200175 fprintf(stderr, "failed to init breakpoints %d\n",
176 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200177 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200178 }
179
Petr Machata2b46cfc2012-02-18 11:17:29 +0100180 return 0;
181}
182
Petr Machata3d0c91c2012-04-14 02:37:38 +0200183int
Petr Machata929bd572012-12-17 03:20:34 +0100184process_init(struct process *proc, const char *filename, pid_t pid)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200185{
186 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200187 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200188 fprintf(stderr, "failed to initialize process %d: %s\n",
189 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200190 return -1;
191 }
192
Petr Machata0f6e6d92012-10-26 23:42:17 +0200193 if (os_process_init(proc) < 0) {
194 process_bare_destroy(proc, 0);
195 goto fail;
196 }
197
Petr Machata744f2552012-04-15 04:33:18 +0200198 if (arch_process_init(proc) < 0) {
Petr Machata0f6e6d92012-10-26 23:42:17 +0200199 os_process_destroy(proc);
Petr Machata744f2552012-04-15 04:33:18 +0200200 process_bare_destroy(proc, 0);
201 goto fail;
202 }
203
Petr Machata218c5ff2012-04-15 04:22:39 +0200204 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200205 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200206 if (process_init_main(proc) < 0) {
207 process_bare_destroy(proc, 0);
208 goto fail;
209 }
210 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200211}
212
Petr Machata8ead1cd2012-04-24 18:13:09 +0200213static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100214destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200215{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200216 breakpoint_destroy(bp);
217 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200218 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200219}
220
Petr Machatae0e89ed2012-10-26 22:25:33 +0200221// XXX see comment in handle_event.c
Petr Machata929bd572012-12-17 03:20:34 +0100222void callstack_pop(struct process *proc);
Petr Machatae0e89ed2012-10-26 22:25:33 +0200223
Petr Machata3d0c91c2012-04-14 02:37:38 +0200224static void
Petr Machata929bd572012-12-17 03:20:34 +0100225private_process_destroy(struct process *proc, int was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200226{
Petr Machatae0e89ed2012-10-26 22:25:33 +0200227 /* Pop remaining stack elements. */
228 while (proc->callstack_depth > 0) {
229 /* When this is called just before a process is
230 * destroyed, the breakpoints should either have been
231 * retracted by now, or were killed by exec. In any
232 * case, it's safe to pretend that there are no
233 * breakpoints associated with the stack elements, so
234 * that stack_pop doesn't attempt to destroy them. */
235 size_t i = proc->callstack_depth - 1;
236 if (!proc->callstack[i].is_syscall)
237 proc->callstack[i].return_addr = 0;
238
239 callstack_pop(proc);
240 }
241
242 if (!was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200243 free(proc->filename);
244
Petr Machata8ead1cd2012-04-24 18:13:09 +0200245 /* Libraries and symbols. This is only relevant in
246 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200247 struct library *lib;
248 for (lib = proc->libraries; lib != NULL; ) {
249 struct library *next = lib->next;
250 library_destroy(lib);
251 free(lib);
252 lib = next;
253 }
254 proc->libraries = NULL;
255
256 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200257 if (proc->breakpoints != NULL) {
258 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100259 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
260 free(proc->breakpoints);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200261 proc->breakpoints = NULL;
262 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200263
264 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200265}
266
267void
Petr Machata929bd572012-12-17 03:20:34 +0100268process_destroy(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200269{
Petr Machata744f2552012-04-15 04:33:18 +0200270 arch_process_destroy(proc);
Petr Machata0f6e6d92012-10-26 23:42:17 +0200271 os_process_destroy(proc);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200272 private_process_destroy(proc, 0);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200273}
274
275int
Petr Machata929bd572012-12-17 03:20:34 +0100276process_exec(struct process *proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200277{
Petr Machata0f6e6d92012-10-26 23:42:17 +0200278 /* Call exec handlers first, before we destroy the main
Petr Machata3cc0cd12012-10-26 22:30:51 +0200279 * state. */
Petr Machata0f6e6d92012-10-26 23:42:17 +0200280 if (arch_process_exec(proc) < 0
281 || os_process_exec(proc) < 0)
Petr Machata744f2552012-04-15 04:33:18 +0200282 return -1;
283
Petr Machata3d0c91c2012-04-14 02:37:38 +0200284 private_process_destroy(proc, 1);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200285
Petr Machata3d0c91c2012-04-14 02:37:38 +0200286 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
287 return -1;
288 if (process_init_main(proc) < 0) {
289 process_bare_destroy(proc, 1);
290 return -1;
291 }
292 return 0;
293}
294
Petr Machata929bd572012-12-17 03:20:34 +0100295struct process *
Petr Machata75934ad2012-04-14 02:28:03 +0200296open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100297{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100298 assert(pid != 0);
Petr Machata929bd572012-12-17 03:20:34 +0100299 struct process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200300 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200301 free(proc);
302 return NULL;
303 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100304 return proc;
305}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100306
Petr Machata2b46cfc2012-02-18 11:17:29 +0100307struct clone_single_bp_data {
Petr Machata929bd572012-12-17 03:20:34 +0100308 struct process *old_proc;
309 struct process *new_proc;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100310};
311
Petr Machatad7e4ca82012-11-28 03:38:47 +0100312static enum callback_status
313clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100314{
Petr Machatad7e4ca82012-11-28 03:38:47 +0100315 struct breakpoint *bp = *bpp;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100316 struct clone_single_bp_data *data = u;
317
Petr Machata2b46cfc2012-02-18 11:17:29 +0100318 struct breakpoint *clone = malloc(sizeof(*clone));
319 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200320 || breakpoint_clone(clone, data->new_proc,
321 bp, data->old_proc) < 0) {
322 fail:
323 free(clone);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100324 return CBS_STOP;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100325 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200326 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
327 breakpoint_destroy(clone);
328 goto fail;
329 }
Petr Machatad7e4ca82012-11-28 03:38:47 +0100330 return CBS_CONT;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100331}
332
333int
Petr Machata929bd572012-12-17 03:20:34 +0100334process_clone(struct process *retp, struct process *proc, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100335{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200336 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200337 fail1:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200338 fprintf(stderr, "failed to clone process %d->%d : %s\n",
339 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100340 return -1;
341 }
342
Petr Machatacf1679a2012-04-06 19:56:17 +0200343 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200344 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400345 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200346
Petr Machata2b46cfc2012-02-18 11:17:29 +0100347 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200348 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100349 return 0;
350
351 /* Clone symbols first so that we can clone and relink
352 * breakpoints. */
353 struct library *lib;
354 struct library **nlibp = &retp->libraries;
Petr Machata1e339e02012-10-27 19:33:20 +0200355 for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100356 *nlibp = malloc(sizeof(**nlibp));
357 if (*nlibp == NULL
358 || library_clone(*nlibp, lib) < 0) {
359 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200360 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100361
362 /* Error when cloning. Unroll what was done. */
363 for (lib = retp->libraries; lib != NULL; ) {
364 struct library *next = lib->next;
365 library_destroy(lib);
366 free(lib);
367 lib = next;
368 }
Petr Machataba1664b2012-04-28 14:59:05 +0200369 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100370 }
371
372 nlibp = &(*nlibp)->next;
373 }
374
375 /* Now clone breakpoints. Symbol relinking is done in
376 * clone_single_bp. */
377 struct clone_single_bp_data data = {
378 .old_proc = proc,
379 .new_proc = retp,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100380 };
Petr Machatad7e4ca82012-11-28 03:38:47 +0100381 if (DICT_EACH(proc->leader->breakpoints,
382 arch_addr_t, struct breakpoint *, NULL,
383 clone_single_bp, &data) != NULL)
Petr Machata94078ec2012-01-05 18:07:02 +0100384 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100385
Petr Machataded6f972012-04-13 23:15:48 +0200386 /* And finally the call stack. */
Petr Machatab6de8412012-10-27 19:31:22 +0200387 /* XXX clearly the callstack handling should be moved to a
388 * separate module and this whole business extracted to
389 * callstack_clone, or callstack_element_clone. */
Petr Machataded6f972012-04-13 23:15:48 +0200390 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
391 retp->callstack_depth = proc->callstack_depth;
392
Petr Machata94078ec2012-01-05 18:07:02 +0100393 size_t i;
394 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machatab6de8412012-10-27 19:31:22 +0200395 struct callstack_element *elem = &retp->callstack[i];
396 struct fetch_context *ctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100397 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200398 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100399 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200400 size_t j;
401 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100402 for (j = 0; j < i; ++j) {
Petr Machatab6de8412012-10-27 19:31:22 +0200403 nctx = elem->fetch_context;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100404 fetch_arg_done(nctx);
Petr Machatab6de8412012-10-27 19:31:22 +0200405 elem->fetch_context = NULL;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100406 }
407 goto fail2;
408 }
Petr Machatab6de8412012-10-27 19:31:22 +0200409 elem->fetch_context = nctx;
Petr Machataf6ec08a2012-01-06 16:58:54 +0100410 }
411
Petr Machatab6de8412012-10-27 19:31:22 +0200412 struct value_dict *args = elem->arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100413 if (args != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100414 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100415 if (nargs == NULL
416 || val_dict_clone(nargs, args) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200417 size_t j;
Petr Machata94078ec2012-01-05 18:07:02 +0100418 for (j = 0; j < i; ++j) {
Petr Machatab6de8412012-10-27 19:31:22 +0200419 nargs = elem->arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100420 val_dict_destroy(nargs);
421 free(nargs);
Petr Machatab6de8412012-10-27 19:31:22 +0200422 elem->arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100423 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100424
425 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200426 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100427 * fetch_context. */
428 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200429 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100430 }
Petr Machatab6de8412012-10-27 19:31:22 +0200431 elem->arguments = nargs;
Petr Machata94078ec2012-01-05 18:07:02 +0100432 }
Petr Machata165b5662012-10-27 19:23:12 +0200433
434 /* If it's not a syscall, we need to find the
435 * corresponding library symbol in the cloned
436 * library. */
437 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
438 struct library_symbol *libfunc = elem->c_un.libfunc;
439 int rc = proc_find_symbol(retp, libfunc,
440 NULL, &elem->c_un.libfunc);
441 assert(rc == 0);
442 }
Petr Machata94078ec2012-01-05 18:07:02 +0100443 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100444
Petr Machata5bf47142012-10-27 19:29:00 +0200445 /* At this point, retp is fully initialized, except for OS and
446 * arch parts, and we can call private_process_destroy. */
447 if (os_process_clone(retp, proc) < 0) {
448 private_process_destroy(retp, 0);
449 return -1;
450 }
451 if (arch_process_clone(retp, proc) < 0) {
452 os_process_destroy(retp);
453 private_process_destroy(retp, 0);
454 return -1;
455 }
Petr Machata744f2552012-04-15 04:33:18 +0200456
Petr Machata2b46cfc2012-02-18 11:17:29 +0100457 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100458}
459
Petr Machata3c516d52011-08-18 03:53:18 +0200460static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200461open_one_pid(pid_t pid)
462{
Petr Machata9a5420c2011-07-09 11:21:23 +0200463 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
464
Petr Machata1974dbc2011-08-19 18:58:01 +0200465 /* Get the filename first. Should the trace_pid fail, we can
466 * easily free it, untracing is more work. */
Petr Machata929bd572012-12-17 03:20:34 +0100467 char *filename = pid2name(pid);
468 if (filename == NULL || trace_pid(pid) < 0) {
Petr Machataef0c74d2012-10-27 00:30:57 +0200469 fail:
Petr Machata1974dbc2011-08-19 18:58:01 +0200470 free(filename);
471 return -1;
472 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100473
Petr Machata929bd572012-12-17 03:20:34 +0100474 struct process *proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200475 if (proc == NULL)
Petr Machataef0c74d2012-10-27 00:30:57 +0200476 goto fail;
477 free(filename);
Petr Machata3ed2a422012-04-06 17:18:55 +0200478 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200479
Petr Machata1974dbc2011-08-19 18:58:01 +0200480 return 0;
481}
482
Petr Machata2b46cfc2012-02-18 11:17:29 +0100483static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100484start_one_pid(struct process *proc, void *data)
Petr Machata1974dbc2011-08-19 18:58:01 +0200485{
486 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100487 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100488}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100489
Petr Machata9a5420c2011-07-09 11:21:23 +0200490void
491open_pid(pid_t pid)
492{
493 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200494 /* If we are already tracing this guy, we should be seeing all
495 * his children via normal tracing route. */
496 if (pid2proc(pid) != NULL)
497 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200498
Petr Machata3c516d52011-08-18 03:53:18 +0200499 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200500 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200501 fprintf(stderr, "Cannot attach to pid %u: %s\n",
502 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200503 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200504 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200505 }
506
Petr Machata3c516d52011-08-18 03:53:18 +0200507 /* Now attach to all tasks that belong to that PID. There's a
508 * race between process_tasks and open_one_pid. So when we
509 * fail in open_one_pid below, we just do another round.
510 * Chances are that by then that PID will have gone away, and
511 * that's why we have seen the failure. The processes that we
512 * manage to open_one_pid are stopped, so we should eventually
513 * reach a point where process_tasks doesn't give any new
514 * processes (because there's nobody left to produce
515 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200516 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200517 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200518 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200519 pid_t *tasks;
520 size_t ntasks;
521 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200522
Petr Machata3c516d52011-08-18 03:53:18 +0200523 if (process_tasks(pid, &tasks, &ntasks) < 0) {
524 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
525 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100526 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200527 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200528
Petr Machata3c516d52011-08-18 03:53:18 +0200529 have_all = 1;
530 for (i = 0; i < ntasks; ++i)
531 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200532 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200533 have_all = 0;
534
Petr Machata9a5420c2011-07-09 11:21:23 +0200535 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200536
Petr Machata1974dbc2011-08-19 18:58:01 +0200537 if (have_all && old_ntasks == ntasks)
538 break;
539 old_ntasks = ntasks;
540 }
541
Petr Machata929bd572012-12-17 03:20:34 +0100542 struct process *leader = pid2proc(pid)->leader;
Petr Machata93d95df2012-04-17 05:16:19 +0200543
544 /* XXX Is there a way to figure out whether _start has
545 * actually already been hit? */
546 arch_dynlink_done(leader);
547
Petr Machata2f9b78e2012-04-16 21:08:54 +0200548 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200549 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200550}
551
Petr Machata2b46cfc2012-02-18 11:17:29 +0100552static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100553find_proc(struct process *proc, void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200554{
555 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100556 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200557}
558
Petr Machata929bd572012-12-17 03:20:34 +0100559struct process *
560pid2proc(pid_t pid)
561{
Petr Machatacebb8842011-07-09 11:14:11 +0200562 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
563}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100564
Petr Machata929bd572012-12-17 03:20:34 +0100565static struct process *list_of_processes = NULL;
Petr Machatacebb8842011-07-09 11:14:11 +0200566
Petr Machatacbe29c62011-09-27 02:27:58 +0200567static void
Petr Machata929bd572012-12-17 03:20:34 +0100568unlist_process(struct process *proc)
Petr Machatacbe29c62011-09-27 02:27:58 +0200569{
Petr Machatacbe29c62011-09-27 02:27:58 +0200570 if (list_of_processes == proc) {
571 list_of_processes = list_of_processes->next;
572 return;
573 }
574
Petr Machata929bd572012-12-17 03:20:34 +0100575 struct process *tmp;
Petr Machatacbe29c62011-09-27 02:27:58 +0200576 for (tmp = list_of_processes; ; tmp = tmp->next) {
577 /* If the following assert fails, the process wasn't
578 * in the list. */
579 assert(tmp->next != NULL);
580
581 if (tmp->next == proc) {
582 tmp->next = tmp->next->next;
583 return;
584 }
585 }
586}
587
Petr Machata929bd572012-12-17 03:20:34 +0100588struct process *
589each_process(struct process *start_after,
590 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100591 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200592{
Petr Machata929bd572012-12-17 03:20:34 +0100593 struct process *it = start_after == NULL ? list_of_processes
Petr Machata74132a42012-03-16 02:46:18 +0100594 : start_after->next;
595
596 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200597 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100598 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100599 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200600 case CBS_FAIL:
601 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100602 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200603 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100604 case CBS_CONT:
605 break;
606 }
Petr Machatacebb8842011-07-09 11:14:11 +0200607 it = next;
608 }
609 return NULL;
610}
Petr Machata9a5420c2011-07-09 11:21:23 +0200611
Petr Machata929bd572012-12-17 03:20:34 +0100612struct process *
613each_task(struct process *proc, struct process *start_after,
614 enum callback_status(*cb)(struct process *proc, void *data),
Petr Machata2b46cfc2012-02-18 11:17:29 +0100615 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200616{
Petr Machata74132a42012-03-16 02:46:18 +0100617 assert(proc != NULL);
Petr Machata929bd572012-12-17 03:20:34 +0100618 struct process *it = start_after == NULL ? proc->leader
Petr Machata74132a42012-03-16 02:46:18 +0100619 : start_after->next;
620
Petr Machata9a5420c2011-07-09 11:21:23 +0200621 if (it != NULL) {
Petr Machata929bd572012-12-17 03:20:34 +0100622 struct process *leader = it->leader;
Petr Machata74132a42012-03-16 02:46:18 +0100623 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200624 /* Callback might call remove_process. */
Petr Machata929bd572012-12-17 03:20:34 +0100625 struct process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100626 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200627 case CBS_FAIL:
628 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100629 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200630 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100631 case CBS_CONT:
632 break;
633 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200634 it = next;
635 }
636 }
637 return NULL;
638}
639
Petr Machata44965c72012-04-06 19:59:20 +0200640static void
Petr Machata929bd572012-12-17 03:20:34 +0100641add_process(struct process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200642{
Petr Machata929bd572012-12-17 03:20:34 +0100643 struct process **leaderp = &list_of_processes;
Petr Machata9a5420c2011-07-09 11:21:23 +0200644 if (proc->pid) {
645 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200646 if (tgid == 0)
647 /* Must have been terminated before we managed
648 * to fully attach. */
649 return;
Petr Machata929bd572012-12-17 03:20:34 +0100650 if (tgid == proc->pid) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200651 proc->leader = proc;
Petr Machata929bd572012-12-17 03:20:34 +0100652 } else {
653 struct process *leader = pid2proc(tgid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200654 proc->leader = leader;
655 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200656 leaderp = &leader->next;
657 }
658 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200659
660 if (!was_exec) {
661 proc->next = *leaderp;
662 *leaderp = proc;
663 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200664}
665
Petr Machatacbe29c62011-09-27 02:27:58 +0200666void
Petr Machata929bd572012-12-17 03:20:34 +0100667change_process_leader(struct process *proc, struct process *leader)
Petr Machatacbe29c62011-09-27 02:27:58 +0200668{
Petr Machata929bd572012-12-17 03:20:34 +0100669 struct process **leaderp = &list_of_processes;
Petr Machatacbe29c62011-09-27 02:27:58 +0200670 if (proc->leader == leader)
671 return;
672
673 assert(leader != NULL);
674 unlist_process(proc);
675 if (proc != leader)
676 leaderp = &leader->next;
677
678 proc->leader = leader;
679 proc->next = *leaderp;
680 *leaderp = proc;
681}
682
Petr Machata2b46cfc2012-02-18 11:17:29 +0100683static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100684clear_leader(struct process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200685{
686 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
687 proc->pid, proc->leader->pid);
688 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100689 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200690}
691
692void
Petr Machata929bd572012-12-17 03:20:34 +0100693remove_process(struct process *proc)
Petr Machatacebb8842011-07-09 11:14:11 +0200694{
Petr Machatacebb8842011-07-09 11:14:11 +0200695 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
696
Petr Machata9a5420c2011-07-09 11:21:23 +0200697 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100698 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200699
Petr Machatacbe29c62011-09-27 02:27:58 +0200700 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100701 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200702 process_destroy(proc);
703 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100704}
Petr Machata4007d742011-07-09 11:29:42 +0200705
706void
Petr Machata929bd572012-12-17 03:20:34 +0100707install_event_handler(struct process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200708{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200709 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200710 assert(proc->event_handler == NULL);
711 proc->event_handler = handler;
712}
713
714void
Petr Machata929bd572012-12-17 03:20:34 +0100715destroy_event_handler(struct process *proc)
Petr Machata4007d742011-07-09 11:29:42 +0200716{
Petr Machata366c2f42012-02-09 19:34:36 +0100717 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200718 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200719 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200720 if (handler->destroy != NULL)
721 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200722 free(handler);
723 proc->event_handler = NULL;
724}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100725
Petr Machataef2fd272012-09-28 00:43:01 +0200726static int
Petr Machata929bd572012-12-17 03:20:34 +0100727breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100728{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200729 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200730 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100731
Petr Machataef2fd272012-09-28 00:43:01 +0200732 /* Don't enable latent or delayed symbols. */
Petr Machata96f04822012-10-31 03:29:11 +0100733 if (libsym->latent || libsym->delayed) {
734 debug(DEBUG_FUNCTION,
735 "delayed and/or latent breakpoint pid=%d, %s@%p",
736 proc->pid, libsym->name, libsym->enter_addr);
Petr Machataef2fd272012-09-28 00:43:01 +0200737 return 0;
Petr Machata96f04822012-10-31 03:29:11 +0100738 }
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200739
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200740 bp_addr = sym2addr(proc, libsym);
741
Petr Machatad5e85562012-04-05 15:18:38 +0200742 /* If there is an artificial breakpoint on the same address,
743 * its libsym will be NULL, and we can smuggle our libsym
744 * there. That artificial breakpoint is there presumably for
745 * the callbacks, which we don't touch. If there is a real
746 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200747 * symbols and ignore extra symbol aliases.
748 *
749 * The other direction is more complicated and currently not
750 * supported. If a breakpoint has custom callbacks, it might
751 * be also custom-allocated, and we would really need to swap
752 * the two: delete the one now in the dictionary, swap values
753 * around, and put the new breakpoint back in. */
Petr Machata98ff3092013-03-08 22:11:36 +0100754 struct breakpoint *bp;
755 if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
Petr Machata8d58d8b2012-11-12 12:46:03 +0100756 /* MIPS backend makes duplicate requests. This is
757 * likely a bug in the backend. Currently there's no
758 * point assigning more than one symbol to a
759 * breakpoint, because when it hits, we won't know
760 * what to print out. But it's easier to fix it here
761 * before someone who understands MIPS has the time to
762 * look into it. So turn the sanity check off on
763 * MIPS. References:
764 *
765 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
766 * http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
767 */
768#ifndef __mips__
Petr Machata98ff3092013-03-08 22:11:36 +0100769 assert(bp->libsym == NULL);
770 bp->libsym = libsym;
Petr Machata8d58d8b2012-11-12 12:46:03 +0100771#endif
Petr Machataef2fd272012-09-28 00:43:01 +0200772 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200773 }
774
Petr Machata98ff3092013-03-08 22:11:36 +0100775 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200776 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200777 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200778 fail:
779 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200780 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200781 }
782 if (proc_add_breakpoint(proc, bp) < 0) {
783 breakpoint_destroy(bp);
784 goto fail;
785 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100786
Petr Machatafa0c5702012-04-13 18:43:40 +0200787 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200788 proc_remove_breakpoint(proc, bp);
789 breakpoint_destroy(bp);
790 goto fail;
791 }
792
Petr Machataef2fd272012-09-28 00:43:01 +0200793 return 0;
794}
795
796static enum callback_status
797cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
798{
799 return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
800}
801
802static int
Petr Machata929bd572012-12-17 03:20:34 +0100803proc_activate_latent_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200804 struct library_symbol *libsym)
805{
806 assert(libsym->latent);
807 libsym->latent = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100808 debug(DEBUG_FUNCTION, "activated latent symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200809 return breakpoint_for_symbol(libsym, proc);
810}
811
812int
Petr Machata929bd572012-12-17 03:20:34 +0100813proc_activate_delayed_symbol(struct process *proc,
Petr Machataef2fd272012-09-28 00:43:01 +0200814 struct library_symbol *libsym)
815{
816 assert(libsym->delayed);
817 libsym->delayed = 0;
Petr Machata96f04822012-10-31 03:29:11 +0100818 debug(DEBUG_FUNCTION, "activated delayed symbol");
Petr Machataef2fd272012-09-28 00:43:01 +0200819 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100820}
821
Petr Machataa1f76832012-09-28 00:08:00 +0200822static enum callback_status
Petr Machata929bd572012-12-17 03:20:34 +0100823activate_latent_in(struct process *proc, struct library *lib, void *data)
Petr Machataa1f76832012-09-28 00:08:00 +0200824{
825 struct library_exported_name *exported;
826 for (exported = data; exported != NULL; exported = exported->next) {
827 struct library_symbol *libsym = NULL;
828 while ((libsym = library_each_symbol(lib, libsym,
829 library_symbol_named_cb,
830 (void *)exported->name))
831 != NULL)
832 if (libsym->latent
833 && proc_activate_latent_symbol(proc, libsym) < 0)
834 return CBS_FAIL;
835 }
836 return CBS_CONT;
837}
838
Petr Machata2b46cfc2012-02-18 11:17:29 +0100839void
Petr Machata929bd572012-12-17 03:20:34 +0100840proc_add_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100841{
842 assert(lib->next == NULL);
843 lib->next = proc->libraries;
844 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200845 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
846 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100847
Petr Machataef2fd272012-09-28 00:43:01 +0200848 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100849 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200850 while ((libsym = library_each_symbol(lib, libsym,
851 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100852 proc)) != NULL)
Petr Machataef2fd272012-09-28 00:43:01 +0200853 fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s.",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200854 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200855
856 /* Look through export list of the new library and compare it
857 * with latent symbols of all libraries (including this
858 * library itself). */
859 struct library *lib2 = NULL;
860 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
861 lib->exported_names)) != NULL)
862 fprintf(stderr,
863 "Couldn't activate latent symbols for %s in %d: %s.",
864 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100865}
866
867int
Petr Machata929bd572012-12-17 03:20:34 +0100868proc_remove_library(struct process *proc, struct library *lib)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100869{
870 struct library **libp;
871 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
872 if (*libp == lib) {
873 *libp = lib->next;
874 return 0;
875 }
876 return -1;
877}
878
879struct library *
Petr Machata929bd572012-12-17 03:20:34 +0100880proc_each_library(struct process *proc, struct library *it,
881 enum callback_status (*cb)(struct process *proc,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100882 struct library *lib, void *data),
883 void *data)
884{
885 if (it == NULL)
886 it = proc->libraries;
887
888 while (it != NULL) {
889 struct library *next = it->next;
890
891 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200892 case CBS_FAIL:
893 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100894 case CBS_STOP:
895 return it;
896 case CBS_CONT:
897 break;
898 }
899
900 it = next;
901 }
902
903 return NULL;
904}
Petr Machata52dbfb12012-03-29 16:38:26 +0200905
Petr Machataf7fee432012-04-19 17:00:53 +0200906static void
Petr Machata929bd572012-12-17 03:20:34 +0100907check_leader(struct process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200908{
Petr Machata52dbfb12012-03-29 16:38:26 +0200909 /* Only the group leader should be getting the breakpoints and
910 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200911 assert(proc->leader != NULL);
912 assert(proc->leader == proc);
913 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200914}
Petr Machata52dbfb12012-03-29 16:38:26 +0200915
Petr Machataf7fee432012-04-19 17:00:53 +0200916int
Petr Machata929bd572012-12-17 03:20:34 +0100917proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machataf7fee432012-04-19 17:00:53 +0200918{
Petr Machatafa0c5702012-04-13 18:43:40 +0200919 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200920 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200921 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200922
Petr Machataa2416362012-04-06 02:43:34 +0200923 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200924 * assert, but that's not necessary right now. Read the
925 * comment in breakpoint_for_symbol. */
Petr Machatad7e4ca82012-11-28 03:38:47 +0100926 assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200927
Petr Machatad7e4ca82012-11-28 03:38:47 +0100928 if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200929 fprintf(stderr,
930 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
931 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200932 return -1;
933 }
934
Petr Machata52dbfb12012-03-29 16:38:26 +0200935 return 0;
936}
937
Petr Machataf7fee432012-04-19 17:00:53 +0200938void
Petr Machata929bd572012-12-17 03:20:34 +0100939proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
Petr Machata52dbfb12012-03-29 16:38:26 +0200940{
Petr Machataf7fee432012-04-19 17:00:53 +0200941 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
942 proc->pid, breakpoint_name(bp), bp->addr);
943 check_leader(proc);
Petr Machatad7e4ca82012-11-28 03:38:47 +0100944 int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
945 NULL, NULL, NULL);
946 assert(rc == 0);
Petr Machata52dbfb12012-03-29 16:38:26 +0200947}
Petr Machatad3cc9882012-04-13 21:40:23 +0200948
Petr Machatad3cc9882012-04-13 21:40:23 +0200949struct each_breakpoint_data
950{
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
Petr Machatad7e4ca82012-11-28 03:38:47 +0100958static enum callback_status
959each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
Petr Machatad3cc9882012-04-13 21:40:23 +0200960{
961 struct each_breakpoint_data *data = d;
Petr Machatad7e4ca82012-11-28 03:38:47 +0100962 return data->cb(data->proc, *bpp, data->cb_data);
Petr Machatad3cc9882012-04-13 21:40:23 +0200963}
964
965void *
Petr Machata929bd572012-12-17 03:20:34 +0100966proc_each_breakpoint(struct process *proc, void *start,
967 enum callback_status (*cb)(struct process *proc,
Petr Machatad3cc9882012-04-13 21:40:23 +0200968 struct breakpoint *bp,
969 void *data), void *data)
970{
971 struct each_breakpoint_data dd = {
Petr Machatad3cc9882012-04-13 21:40:23 +0200972 .proc = proc,
973 .cb = cb,
974 .cb_data = data,
975 };
Petr Machatad7e4ca82012-11-28 03:38:47 +0100976 return DICT_EACH(proc->breakpoints,
977 arch_addr_t, struct breakpoint *, start,
978 &each_breakpoint_cb, &dd);
Petr Machatad3cc9882012-04-13 21:40:23 +0200979}
Petr Machata165b5662012-10-27 19:23:12 +0200980
981int
Petr Machata929bd572012-12-17 03:20:34 +0100982proc_find_symbol(struct process *proc, struct library_symbol *sym,
Petr Machata165b5662012-10-27 19:23:12 +0200983 struct library **retlib, struct library_symbol **retsym)
984{
985 struct library *lib = sym->lib;
986 assert(lib != NULL);
987
988 struct library *flib
989 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
990 if (flib == NULL)
991 return -1;
992
993 struct library_symbol *fsym
994 = library_each_symbol(flib, NULL, library_symbol_named_cb,
995 (char *)sym->name);
996 if (fsym == NULL)
997 return -1;
998
999 if (retlib != NULL)
1000 *retlib = flib;
1001 if (retsym != NULL)
1002 *retsym = fsym;
1003
1004 return 0;
1005}
Petr Machata32405542012-10-31 03:28:39 +01001006
1007struct library_symbol *
Petr Machata929bd572012-12-17 03:20:34 +01001008proc_each_symbol(struct process *proc, struct library_symbol *start_after,
Petr Machata32405542012-10-31 03:28:39 +01001009 enum callback_status (*cb)(struct library_symbol *, void *),
1010 void *data)
1011{
1012 struct library *lib;
Petr Machata32405542012-10-31 03:28:39 +01001013 for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1014 lib != NULL; lib = lib->next) {
1015 start_after = library_each_symbol(lib, start_after, cb, data);
1016 if (start_after != NULL)
1017 return start_after;
1018 }
1019
1020 return NULL;
1021}
Petr Machata653085a2013-01-15 17:40:40 +01001022
1023#define DEF_READER(NAME, SIZE) \
1024 int \
1025 NAME(struct process *proc, arch_addr_t addr, \
1026 uint##SIZE##_t *lp) \
1027 { \
1028 union { \
1029 uint##SIZE##_t dst; \
1030 char buf[0]; \
1031 } u; \
1032 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst)) \
1033 != sizeof(u.dst)) \
1034 return -1; \
1035 *lp = u.dst; \
1036 return 0; \
1037 }
1038
1039DEF_READER(proc_read_16, 16)
1040DEF_READER(proc_read_32, 32)
1041DEF_READER(proc_read_64, 64)
1042
1043#undef DEF_READER