blob: 3a452abd807f1db7cecaa651ec099bd94eaad6d0 [file] [log] [blame]
Petr Machata64262602012-01-07 03:41:36 +01001/*
2 * This file is part of ltrace.
3 * Copyright (C) 2011,2012 Petr Machata, Red Hat Inc.
4 * 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
46arch_process_init(struct Process *proc)
47{
48 return 0;
49}
50
51void
52arch_process_destroy(struct Process *proc)
53{
54}
55
56int
57arch_process_clone(struct Process *retp, struct Process *proc)
58{
59 return 0;
60}
61
62int
63arch_process_exec(struct Process *proc)
64{
65 return 0;
66}
67#endif
68
Petr Machata93d95df2012-04-17 05:16:19 +020069#ifndef ARCH_HAVE_DYNLINK_DONE
70void
71arch_dynlink_done(struct Process *proc)
72{
73}
74#endif
75
Petr Machata3d0c91c2012-04-14 02:37:38 +020076static void add_process(struct Process *proc, int was_exec);
Petr Machata61686c22012-05-03 18:39:49 +020077static void unlist_process(struct Process *proc);
Petr Machata44965c72012-04-06 19:59:20 +020078
Petr Machatae677c7e2012-10-26 22:23:43 +020079static void
80destroy_unwind(struct Process *proc)
81{
82#if defined(HAVE_LIBUNWIND)
83 _UPT_destroy(proc->unwind_priv);
84 unw_destroy_addr_space(proc->unwind_as);
85#endif /* defined(HAVE_LIBUNWIND) */
86}
87
Petr Machata2b46cfc2012-02-18 11:17:29 +010088static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020089process_bare_init(struct Process *proc, const char *filename,
90 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010091{
Petr Machata3d0c91c2012-04-14 02:37:38 +020092 if (!was_exec) {
93 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020094
Petr Machata3d0c91c2012-04-14 02:37:38 +020095 proc->filename = strdup(filename);
96 if (proc->filename == NULL) {
97 fail:
98 free(proc->filename);
99 if (proc->breakpoints != NULL)
100 dict_clear(proc->breakpoints);
101 return -1;
102 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100103 }
104
105 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +0200106 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200107 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100108 if (proc->leader == NULL)
109 goto fail;
110
111 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +0200112 proc->breakpoints = dict_init(target_address_hash,
113 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100114 if (proc->breakpoints == NULL)
115 goto fail;
116 } else {
117 proc->breakpoints = NULL;
118 }
119
Joe Damatoab3b72c2010-10-31 00:21:53 -0700120#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +0200121 proc->unwind_priv = _UPT_create(pid);
122 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700123#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -0700124
Petr Machata2b46cfc2012-02-18 11:17:29 +0100125 return 0;
126}
127
128static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200129process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100130{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100131 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200132 if (!was_exec) {
133 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200134 unlist_process(proc);
Petr Machatae677c7e2012-10-26 22:23:43 +0200135 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200136 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100137}
138
Petr Machata3d0c91c2012-04-14 02:37:38 +0200139static int
140process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100141{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200142 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200143 fprintf(stderr, "failed to init breakpoints %d\n",
144 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200145 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200146 }
147
Petr Machata2b46cfc2012-02-18 11:17:29 +0100148 return 0;
149}
150
Petr Machata3d0c91c2012-04-14 02:37:38 +0200151int
152process_init(struct Process *proc, const char *filename, pid_t pid)
153{
154 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200155 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200156 fprintf(stderr, "failed to initialize process %d: %s\n",
157 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200158 return -1;
159 }
160
Petr Machata744f2552012-04-15 04:33:18 +0200161 if (arch_process_init(proc) < 0) {
162 process_bare_destroy(proc, 0);
163 goto fail;
164 }
165
Petr Machata218c5ff2012-04-15 04:22:39 +0200166 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200167 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200168 if (process_init_main(proc) < 0) {
169 process_bare_destroy(proc, 0);
170 goto fail;
171 }
172 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200173}
174
Petr Machata8ead1cd2012-04-24 18:13:09 +0200175static enum callback_status
176destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200177{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178 breakpoint_destroy(bp);
179 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200180 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200181}
182
183static void
184private_process_destroy(struct Process *proc, int keep_filename)
185{
186 if (!keep_filename)
187 free(proc->filename);
188
Petr Machata8ead1cd2012-04-24 18:13:09 +0200189 /* Libraries and symbols. This is only relevant in
190 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200191 struct library *lib;
192 for (lib = proc->libraries; lib != NULL; ) {
193 struct library *next = lib->next;
194 library_destroy(lib);
195 free(lib);
196 lib = next;
197 }
198 proc->libraries = NULL;
199
200 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200201 if (proc->breakpoints != NULL) {
202 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
203 dict_clear(proc->breakpoints);
204 proc->breakpoints = NULL;
205 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200206
207 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200208}
209
210void
211process_destroy(struct Process *proc)
212{
213 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200214 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200215}
216
217int
218process_exec(struct Process *proc)
219{
Petr Machata744f2552012-04-15 04:33:18 +0200220 /* Call exec first, before we destroy the main state. */
221 if (arch_process_exec(proc) < 0)
222 return -1;
223
Petr Machata3d0c91c2012-04-14 02:37:38 +0200224 private_process_destroy(proc, 1);
225 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
226 return -1;
227 if (process_init_main(proc) < 0) {
228 process_bare_destroy(proc, 1);
229 return -1;
230 }
231 return 0;
232}
233
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200235open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100236{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100237 assert(pid != 0);
238 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200239 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200240 free(proc);
241 return NULL;
242 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100243 return proc;
244}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100245
Petr Machata2b46cfc2012-02-18 11:17:29 +0100246struct clone_single_bp_data {
247 struct Process *old_proc;
248 struct Process *new_proc;
249 int error;
250};
251
Petr Machata2b46cfc2012-02-18 11:17:29 +0100252static void
253clone_single_bp(void *key, void *value, void *u)
254{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100255 struct breakpoint *bp = value;
256 struct clone_single_bp_data *data = u;
257
Petr Machatad3cc9882012-04-13 21:40:23 +0200258 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100259 struct breakpoint *clone = malloc(sizeof(*clone));
260 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200261 || breakpoint_clone(clone, data->new_proc,
262 bp, data->old_proc) < 0) {
263 fail:
264 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100265 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100266 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200267 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
268 breakpoint_destroy(clone);
269 goto fail;
270 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100271}
272
273int
274process_clone(struct Process *retp, struct Process *proc, pid_t pid)
275{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200276 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200277 fail1:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200278 fprintf(stderr, "failed to clone process %d->%d : %s\n",
279 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100280 return -1;
281 }
282
Petr Machatacf1679a2012-04-06 19:56:17 +0200283 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200284 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400285 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200286
Petr Machata2b46cfc2012-02-18 11:17:29 +0100287 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200288 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289 return 0;
290
291 /* Clone symbols first so that we can clone and relink
292 * breakpoints. */
293 struct library *lib;
294 struct library **nlibp = &retp->libraries;
295 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
296 *nlibp = malloc(sizeof(**nlibp));
297 if (*nlibp == NULL
298 || library_clone(*nlibp, lib) < 0) {
299 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200300 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100301
302 /* Error when cloning. Unroll what was done. */
303 for (lib = retp->libraries; lib != NULL; ) {
304 struct library *next = lib->next;
305 library_destroy(lib);
306 free(lib);
307 lib = next;
308 }
Petr Machataba1664b2012-04-28 14:59:05 +0200309 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100310 }
311
312 nlibp = &(*nlibp)->next;
313 }
314
315 /* Now clone breakpoints. Symbol relinking is done in
316 * clone_single_bp. */
317 struct clone_single_bp_data data = {
318 .old_proc = proc,
319 .new_proc = retp,
320 .error = 0,
321 };
322 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100323 if (data.error < 0)
324 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100325
Petr Machataded6f972012-04-13 23:15:48 +0200326 /* And finally the call stack. */
327 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
328 retp->callstack_depth = proc->callstack_depth;
329
Petr Machata94078ec2012-01-05 18:07:02 +0100330 size_t i;
331 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100332 struct fetch_context *ctx = retp->callstack[i].fetch_context;
333 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200334 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100335 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200336 size_t j;
337 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100338 for (j = 0; j < i; ++j) {
339 nctx = retp->callstack[i].fetch_context;
340 fetch_arg_done(nctx);
341 retp->callstack[i].fetch_context = NULL;
342 }
343 goto fail2;
344 }
345 retp->callstack[i].fetch_context = nctx;
346 }
347
Petr Machata94078ec2012-01-05 18:07:02 +0100348 struct value_dict *args = retp->callstack[i].arguments;
349 if (args != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100350 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100351 if (nargs == NULL
352 || val_dict_clone(nargs, args) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200353 size_t j;
354 fail4:
Petr Machata94078ec2012-01-05 18:07:02 +0100355 for (j = 0; j < i; ++j) {
Petr Machataba1664b2012-04-28 14:59:05 +0200356 nargs = retp->callstack[i].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100357 val_dict_destroy(nargs);
358 free(nargs);
Petr Machataba1664b2012-04-28 14:59:05 +0200359 retp->callstack[i].arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100360 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100361
362 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200363 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100364 * fetch_context. */
365 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200366 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100367 }
368 retp->callstack[i].arguments = nargs;
369 }
370 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100371
Petr Machata744f2552012-04-15 04:33:18 +0200372 if (arch_process_clone(retp, proc) < 0)
Petr Machataba1664b2012-04-28 14:59:05 +0200373 goto fail4;
Petr Machata744f2552012-04-15 04:33:18 +0200374
Petr Machata2b46cfc2012-02-18 11:17:29 +0100375 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100376}
377
Petr Machata3c516d52011-08-18 03:53:18 +0200378static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200379open_one_pid(pid_t pid)
380{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200381 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100382 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200383 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
384
Petr Machata1974dbc2011-08-19 18:58:01 +0200385 /* Get the filename first. Should the trace_pid fail, we can
386 * easily free it, untracing is more work. */
387 if ((filename = pid2name(pid)) == NULL
388 || trace_pid(pid) < 0) {
389 free(filename);
390 return -1;
391 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100392
Petr Machata75934ad2012-04-14 02:28:03 +0200393 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200394 if (proc == NULL)
395 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200396 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200397
Petr Machata1974dbc2011-08-19 18:58:01 +0200398 return 0;
399}
400
Petr Machata2b46cfc2012-02-18 11:17:29 +0100401static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200402start_one_pid(Process * proc, void * data)
403{
404 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100405 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100406}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100407
Petr Machata9a5420c2011-07-09 11:21:23 +0200408void
409open_pid(pid_t pid)
410{
411 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200412 /* If we are already tracing this guy, we should be seeing all
413 * his children via normal tracing route. */
414 if (pid2proc(pid) != NULL)
415 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200416
Petr Machata3c516d52011-08-18 03:53:18 +0200417 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200418 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200419 fprintf(stderr, "Cannot attach to pid %u: %s\n",
420 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200421 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200422 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200423 }
424
Petr Machata3c516d52011-08-18 03:53:18 +0200425 /* Now attach to all tasks that belong to that PID. There's a
426 * race between process_tasks and open_one_pid. So when we
427 * fail in open_one_pid below, we just do another round.
428 * Chances are that by then that PID will have gone away, and
429 * that's why we have seen the failure. The processes that we
430 * manage to open_one_pid are stopped, so we should eventually
431 * reach a point where process_tasks doesn't give any new
432 * processes (because there's nobody left to produce
433 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200434 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200435 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200436 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200437 pid_t *tasks;
438 size_t ntasks;
439 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200440
Petr Machata3c516d52011-08-18 03:53:18 +0200441 if (process_tasks(pid, &tasks, &ntasks) < 0) {
442 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
443 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100444 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200445 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200446
Petr Machata3c516d52011-08-18 03:53:18 +0200447 have_all = 1;
448 for (i = 0; i < ntasks; ++i)
449 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200450 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200451 have_all = 0;
452
Petr Machata9a5420c2011-07-09 11:21:23 +0200453 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200454
Petr Machata1974dbc2011-08-19 18:58:01 +0200455 if (have_all && old_ntasks == ntasks)
456 break;
457 old_ntasks = ntasks;
458 }
459
Petr Machata93d95df2012-04-17 05:16:19 +0200460 struct Process *leader = pid2proc(pid)->leader;
461
462 /* XXX Is there a way to figure out whether _start has
463 * actually already been hit? */
464 arch_dynlink_done(leader);
465
Petr Machata2f9b78e2012-04-16 21:08:54 +0200466 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200467 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200468}
469
Petr Machata2b46cfc2012-02-18 11:17:29 +0100470static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200471find_proc(Process * proc, void * data)
472{
473 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100474 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200475}
476
Juan Cespedesa8909f72009-04-28 20:02:41 +0200477Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100478pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200479 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
480}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100481
Petr Machatacebb8842011-07-09 11:14:11 +0200482static Process * list_of_processes = NULL;
483
Petr Machatacbe29c62011-09-27 02:27:58 +0200484static void
485unlist_process(Process * proc)
486{
487 Process *tmp;
488
489 if (list_of_processes == proc) {
490 list_of_processes = list_of_processes->next;
491 return;
492 }
493
494 for (tmp = list_of_processes; ; tmp = tmp->next) {
495 /* If the following assert fails, the process wasn't
496 * in the list. */
497 assert(tmp->next != NULL);
498
499 if (tmp->next == proc) {
500 tmp->next = tmp->next->next;
501 return;
502 }
503 }
504}
505
Petr Machata2b46cfc2012-02-18 11:17:29 +0100506struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100507each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100508 enum callback_status(*cb)(struct Process *proc, void *data),
509 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200510{
Petr Machata74132a42012-03-16 02:46:18 +0100511 struct Process *it = start_after == NULL ? list_of_processes
512 : start_after->next;
513
514 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200515 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100516 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100517 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200518 case CBS_FAIL:
519 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100520 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200521 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100522 case CBS_CONT:
523 break;
524 }
Petr Machatacebb8842011-07-09 11:14:11 +0200525 it = next;
526 }
527 return NULL;
528}
Petr Machata9a5420c2011-07-09 11:21:23 +0200529
530Process *
Petr Machata74132a42012-03-16 02:46:18 +0100531each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100532 enum callback_status(*cb)(struct Process *proc, void *data),
533 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200534{
Petr Machata74132a42012-03-16 02:46:18 +0100535 assert(proc != NULL);
536 struct Process *it = start_after == NULL ? proc->leader
537 : start_after->next;
538
Petr Machata9a5420c2011-07-09 11:21:23 +0200539 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100540 struct Process *leader = it->leader;
541 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200542 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100543 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100544 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200545 case CBS_FAIL:
546 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100547 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200548 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100549 case CBS_CONT:
550 break;
551 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200552 it = next;
553 }
554 }
555 return NULL;
556}
557
Petr Machata44965c72012-04-06 19:59:20 +0200558static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200559add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200560{
Petr Machata9a5420c2011-07-09 11:21:23 +0200561 Process ** leaderp = &list_of_processes;
562 if (proc->pid) {
563 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200564 if (tgid == 0)
565 /* Must have been terminated before we managed
566 * to fully attach. */
567 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200568 if (tgid == proc->pid)
569 proc->leader = proc;
570 else {
571 Process * leader = pid2proc(tgid);
572 proc->leader = leader;
573 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200574 leaderp = &leader->next;
575 }
576 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200577
578 if (!was_exec) {
579 proc->next = *leaderp;
580 *leaderp = proc;
581 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200582}
583
Petr Machatacbe29c62011-09-27 02:27:58 +0200584void
585change_process_leader(Process * proc, Process * leader)
586{
587 Process ** leaderp = &list_of_processes;
588 if (proc->leader == leader)
589 return;
590
591 assert(leader != NULL);
592 unlist_process(proc);
593 if (proc != leader)
594 leaderp = &leader->next;
595
596 proc->leader = leader;
597 proc->next = *leaderp;
598 *leaderp = proc;
599}
600
Petr Machata2b46cfc2012-02-18 11:17:29 +0100601static enum callback_status
602clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200603{
604 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
605 proc->pid, proc->leader->pid);
606 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100607 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200608}
609
610void
611remove_process(Process *proc)
612{
Petr Machatacebb8842011-07-09 11:14:11 +0200613 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
614
Petr Machata9a5420c2011-07-09 11:21:23 +0200615 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100616 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200617
Petr Machatacbe29c62011-09-27 02:27:58 +0200618 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100619 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200620 process_destroy(proc);
621 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100622}
Petr Machata4007d742011-07-09 11:29:42 +0200623
624void
Petr Machata366c2f42012-02-09 19:34:36 +0100625install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200626{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200627 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200628 assert(proc->event_handler == NULL);
629 proc->event_handler = handler;
630}
631
632void
633destroy_event_handler(Process * proc)
634{
Petr Machata366c2f42012-02-09 19:34:36 +0100635 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200636 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200637 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200638 if (handler->destroy != NULL)
639 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200640 free(handler);
641 proc->event_handler = NULL;
642}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100643
Petr Machataef2fd272012-09-28 00:43:01 +0200644static int
645breakpoint_for_symbol(struct library_symbol *libsym, struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100646{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200647 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200648 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100649
Petr Machataef2fd272012-09-28 00:43:01 +0200650 /* Don't enable latent or delayed symbols. */
651 if (libsym->latent || libsym->delayed)
652 return 0;
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200653
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200654 bp_addr = sym2addr(proc, libsym);
655
Petr Machatad5e85562012-04-05 15:18:38 +0200656 /* If there is an artificial breakpoint on the same address,
657 * its libsym will be NULL, and we can smuggle our libsym
658 * there. That artificial breakpoint is there presumably for
659 * the callbacks, which we don't touch. If there is a real
660 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200661 * symbols and ignore extra symbol aliases.
662 *
663 * The other direction is more complicated and currently not
664 * supported. If a breakpoint has custom callbacks, it might
665 * be also custom-allocated, and we would really need to swap
666 * the two: delete the one now in the dictionary, swap values
667 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200668 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200669 bp_addr);
Petr Machatad5e85562012-04-05 15:18:38 +0200670 if (bp != NULL) {
671 assert(bp->libsym == NULL);
672 bp->libsym = libsym;
Petr Machataef2fd272012-09-28 00:43:01 +0200673 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200674 }
675
676 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200677 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200678 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200679 fail:
680 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200681 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200682 }
683 if (proc_add_breakpoint(proc, bp) < 0) {
684 breakpoint_destroy(bp);
685 goto fail;
686 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100687
Petr Machatafa0c5702012-04-13 18:43:40 +0200688 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200689 proc_remove_breakpoint(proc, bp);
690 breakpoint_destroy(bp);
691 goto fail;
692 }
693
Petr Machataef2fd272012-09-28 00:43:01 +0200694 return 0;
695}
696
697static enum callback_status
698cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
699{
700 return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
701}
702
703static int
704proc_activate_latent_symbol(struct Process *proc,
705 struct library_symbol *libsym)
706{
707 assert(libsym->latent);
708 libsym->latent = 0;
709 return breakpoint_for_symbol(libsym, proc);
710}
711
712int
713proc_activate_delayed_symbol(struct Process *proc,
714 struct library_symbol *libsym)
715{
716 assert(libsym->delayed);
717 libsym->delayed = 0;
718 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100719}
720
Petr Machataa1f76832012-09-28 00:08:00 +0200721static enum callback_status
722activate_latent_in(struct Process *proc, struct library *lib, void *data)
723{
724 struct library_exported_name *exported;
725 for (exported = data; exported != NULL; exported = exported->next) {
726 struct library_symbol *libsym = NULL;
727 while ((libsym = library_each_symbol(lib, libsym,
728 library_symbol_named_cb,
729 (void *)exported->name))
730 != NULL)
731 if (libsym->latent
732 && proc_activate_latent_symbol(proc, libsym) < 0)
733 return CBS_FAIL;
734 }
735 return CBS_CONT;
736}
737
Petr Machata2b46cfc2012-02-18 11:17:29 +0100738void
739proc_add_library(struct Process *proc, struct library *lib)
740{
741 assert(lib->next == NULL);
742 lib->next = proc->libraries;
743 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200744 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
745 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100746
Petr Machataef2fd272012-09-28 00:43:01 +0200747 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100748 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200749 while ((libsym = library_each_symbol(lib, libsym,
750 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100751 proc)) != NULL)
Petr Machataef2fd272012-09-28 00:43:01 +0200752 fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s.",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200753 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200754
755 /* Look through export list of the new library and compare it
756 * with latent symbols of all libraries (including this
757 * library itself). */
758 struct library *lib2 = NULL;
759 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
760 lib->exported_names)) != NULL)
761 fprintf(stderr,
762 "Couldn't activate latent symbols for %s in %d: %s.",
763 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100764}
765
766int
767proc_remove_library(struct Process *proc, struct library *lib)
768{
769 struct library **libp;
770 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
771 if (*libp == lib) {
772 *libp = lib->next;
773 return 0;
774 }
775 return -1;
776}
777
778struct library *
779proc_each_library(struct Process *proc, struct library *it,
780 enum callback_status (*cb)(struct Process *proc,
781 struct library *lib, void *data),
782 void *data)
783{
784 if (it == NULL)
785 it = proc->libraries;
786
787 while (it != NULL) {
788 struct library *next = it->next;
789
790 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200791 case CBS_FAIL:
792 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100793 case CBS_STOP:
794 return it;
795 case CBS_CONT:
796 break;
797 }
798
799 it = next;
800 }
801
802 return NULL;
803}
Petr Machata52dbfb12012-03-29 16:38:26 +0200804
Petr Machataf7fee432012-04-19 17:00:53 +0200805static void
806check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200807{
Petr Machata52dbfb12012-03-29 16:38:26 +0200808 /* Only the group leader should be getting the breakpoints and
809 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200810 assert(proc->leader != NULL);
811 assert(proc->leader == proc);
812 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200813}
Petr Machata52dbfb12012-03-29 16:38:26 +0200814
Petr Machataf7fee432012-04-19 17:00:53 +0200815int
816proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
817{
Petr Machatafa0c5702012-04-13 18:43:40 +0200818 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200819 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200820 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200821
Petr Machataa2416362012-04-06 02:43:34 +0200822 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200823 * assert, but that's not necessary right now. Read the
824 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200825 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200826
Petr Machatafa0c5702012-04-13 18:43:40 +0200827 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200828 fprintf(stderr,
829 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
830 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200831 return -1;
832 }
833
Petr Machata52dbfb12012-03-29 16:38:26 +0200834 return 0;
835}
836
Petr Machataf7fee432012-04-19 17:00:53 +0200837void
Petr Machata52dbfb12012-03-29 16:38:26 +0200838proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
839{
Petr Machataf7fee432012-04-19 17:00:53 +0200840 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
841 proc->pid, breakpoint_name(bp), bp->addr);
842 check_leader(proc);
843 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
844 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200845}
Petr Machatad3cc9882012-04-13 21:40:23 +0200846
847/* Dict doesn't support iteration restarts, so here's this contraption
848 * for now. XXX add restarts to dict. */
849struct each_breakpoint_data
850{
851 void *start;
852 void *end;
853 struct Process *proc;
854 enum callback_status (*cb)(struct Process *proc,
855 struct breakpoint *bp,
856 void *data);
857 void *cb_data;
858};
859
860static void
861each_breakpoint_cb(void *key, void *value, void *d)
862{
863 struct each_breakpoint_data *data = d;
864 if (data->end != NULL)
865 return;
866 if (data->start == key)
867 data->start = NULL;
868
869 if (data->start == NULL) {
870 switch (data->cb(data->proc, value, data->cb_data)) {
871 case CBS_FAIL:
872 /* XXX handle me */
873 case CBS_STOP:
874 data->end = key;
875 case CBS_CONT:
876 return;
877 }
878 }
879}
880
881void *
882proc_each_breakpoint(struct Process *proc, void *start,
883 enum callback_status (*cb)(struct Process *proc,
884 struct breakpoint *bp,
885 void *data), void *data)
886{
887 struct each_breakpoint_data dd = {
888 .start = start,
889 .proc = proc,
890 .cb = cb,
891 .cb_data = data,
892 };
893 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
894 return dd.end;
895}