blob: d6937f0f8db3b7c358ff83e35659edcac6f7055e [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
25#if defined(HAVE_LIBUNWIND)
26#include <libunwind.h>
27#include <libunwind-ptrace.h>
28#endif /* defined(HAVE_LIBUNWIND) */
29
Juan Cespedes273ea6d1998-03-14 23:02:40 +010030#include <sys/types.h>
31#include <string.h>
32#include <stdio.h>
33#include <errno.h>
34#include <stdlib.h>
Petr Machata1b17dbf2011-07-08 19:22:52 +020035#include <assert.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +010036
Petr Machata9294d822012-02-07 12:35:58 +010037#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010038#include "proc.h"
Petr Machata64262602012-01-07 03:41:36 +010039#include "backend.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010040
Petr Machata744f2552012-04-15 04:33:18 +020041#ifndef ARCH_HAVE_PROCESS_DATA
42int
43arch_process_init(struct Process *proc)
44{
45 return 0;
46}
47
48void
49arch_process_destroy(struct Process *proc)
50{
51}
52
53int
54arch_process_clone(struct Process *retp, struct Process *proc)
55{
56 return 0;
57}
58
59int
60arch_process_exec(struct Process *proc)
61{
62 return 0;
63}
64#endif
65
Petr Machata93d95df2012-04-17 05:16:19 +020066#ifndef ARCH_HAVE_DYNLINK_DONE
67void
68arch_dynlink_done(struct Process *proc)
69{
70}
71#endif
72
Petr Machata3d0c91c2012-04-14 02:37:38 +020073static void add_process(struct Process *proc, int was_exec);
Petr Machata61686c22012-05-03 18:39:49 +020074static void unlist_process(struct Process *proc);
Petr Machata44965c72012-04-06 19:59:20 +020075
Petr Machata2b46cfc2012-02-18 11:17:29 +010076static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020077process_bare_init(struct Process *proc, const char *filename,
78 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010079{
Petr Machata3d0c91c2012-04-14 02:37:38 +020080 if (!was_exec) {
81 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020082
Petr Machata3d0c91c2012-04-14 02:37:38 +020083 proc->filename = strdup(filename);
84 if (proc->filename == NULL) {
85 fail:
86 free(proc->filename);
87 if (proc->breakpoints != NULL)
88 dict_clear(proc->breakpoints);
89 return -1;
90 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010091 }
92
93 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020094 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +020095 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +010096 if (proc->leader == NULL)
97 goto fail;
98
99 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +0200100 proc->breakpoints = dict_init(target_address_hash,
101 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100102 if (proc->breakpoints == NULL)
103 goto fail;
104 } else {
105 proc->breakpoints = NULL;
106 }
107
Joe Damatoab3b72c2010-10-31 00:21:53 -0700108#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +0200109 proc->unwind_priv = _UPT_create(pid);
110 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700111#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -0700112
Petr Machata2b46cfc2012-02-18 11:17:29 +0100113 return 0;
114}
115
116static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200117process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100118{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100119 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200120 if (!was_exec) {
121 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200122 unlist_process(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200123 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100124}
125
Petr Machata3d0c91c2012-04-14 02:37:38 +0200126static int
127process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100128{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200129 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200130 fprintf(stderr, "failed to init breakpoints %d\n",
131 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200132 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200133 }
134
Petr Machata2b46cfc2012-02-18 11:17:29 +0100135 return 0;
136}
137
Petr Machata3d0c91c2012-04-14 02:37:38 +0200138int
139process_init(struct Process *proc, const char *filename, pid_t pid)
140{
141 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200142 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200143 fprintf(stderr, "failed to initialize process %d: %s\n",
144 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200145 return -1;
146 }
147
Petr Machata744f2552012-04-15 04:33:18 +0200148 if (arch_process_init(proc) < 0) {
149 process_bare_destroy(proc, 0);
150 goto fail;
151 }
152
Petr Machata218c5ff2012-04-15 04:22:39 +0200153 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200154 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200155 if (process_init_main(proc) < 0) {
156 process_bare_destroy(proc, 0);
157 goto fail;
158 }
159 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200160}
161
Petr Machata8ead1cd2012-04-24 18:13:09 +0200162static enum callback_status
163destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200164{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200165 breakpoint_destroy(bp);
166 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200167 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200168}
169
170static void
171private_process_destroy(struct Process *proc, int keep_filename)
172{
173 if (!keep_filename)
174 free(proc->filename);
175
Petr Machata8ead1cd2012-04-24 18:13:09 +0200176 /* Libraries and symbols. This is only relevant in
177 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178 struct library *lib;
179 for (lib = proc->libraries; lib != NULL; ) {
180 struct library *next = lib->next;
181 library_destroy(lib);
182 free(lib);
183 lib = next;
184 }
185 proc->libraries = NULL;
186
187 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200188 if (proc->breakpoints != NULL) {
189 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
190 dict_clear(proc->breakpoints);
191 proc->breakpoints = NULL;
192 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200193}
194
195void
196process_destroy(struct Process *proc)
197{
198 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200199 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200200}
201
202int
203process_exec(struct Process *proc)
204{
Petr Machata744f2552012-04-15 04:33:18 +0200205 /* Call exec first, before we destroy the main state. */
206 if (arch_process_exec(proc) < 0)
207 return -1;
208
Petr Machata3d0c91c2012-04-14 02:37:38 +0200209 private_process_destroy(proc, 1);
210 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
211 return -1;
212 if (process_init_main(proc) < 0) {
213 process_bare_destroy(proc, 1);
214 return -1;
215 }
216 return 0;
217}
218
Petr Machata2b46cfc2012-02-18 11:17:29 +0100219struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200220open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100221{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222 assert(pid != 0);
223 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200224 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200225 free(proc);
226 return NULL;
227 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100228 return proc;
229}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100230
Petr Machata2b46cfc2012-02-18 11:17:29 +0100231struct clone_single_bp_data {
232 struct Process *old_proc;
233 struct Process *new_proc;
234 int error;
235};
236
Petr Machata2b46cfc2012-02-18 11:17:29 +0100237static void
238clone_single_bp(void *key, void *value, void *u)
239{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100240 struct breakpoint *bp = value;
241 struct clone_single_bp_data *data = u;
242
Petr Machatad3cc9882012-04-13 21:40:23 +0200243 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100244 struct breakpoint *clone = malloc(sizeof(*clone));
245 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200246 || breakpoint_clone(clone, data->new_proc,
247 bp, data->old_proc) < 0) {
248 fail:
249 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100250 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200252 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
253 breakpoint_destroy(clone);
254 goto fail;
255 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100256}
257
258int
259process_clone(struct Process *retp, struct Process *proc, pid_t pid)
260{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200261 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100262 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200263 fprintf(stderr, "failed to clone process %d->%d : %s\n",
264 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100265 return -1;
266 }
267
Petr Machatacf1679a2012-04-06 19:56:17 +0200268 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200269 retp->e_machine = proc->e_machine;
Petr Machatacf1679a2012-04-06 19:56:17 +0200270
Petr Machata2b46cfc2012-02-18 11:17:29 +0100271 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200272 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100273 return 0;
274
275 /* Clone symbols first so that we can clone and relink
276 * breakpoints. */
277 struct library *lib;
278 struct library **nlibp = &retp->libraries;
279 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
280 *nlibp = malloc(sizeof(**nlibp));
281 if (*nlibp == NULL
282 || library_clone(*nlibp, lib) < 0) {
283 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200284 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100285
286 /* Error when cloning. Unroll what was done. */
287 for (lib = retp->libraries; lib != NULL; ) {
288 struct library *next = lib->next;
289 library_destroy(lib);
290 free(lib);
291 lib = next;
292 }
293 goto fail;
294 }
295
296 nlibp = &(*nlibp)->next;
297 }
298
299 /* Now clone breakpoints. Symbol relinking is done in
300 * clone_single_bp. */
301 struct clone_single_bp_data data = {
302 .old_proc = proc,
303 .new_proc = retp,
304 .error = 0,
305 };
306 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100307 if (data.error < 0)
308 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100309
Petr Machataded6f972012-04-13 23:15:48 +0200310 /* And finally the call stack. */
311 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
312 retp->callstack_depth = proc->callstack_depth;
313
Petr Machata94078ec2012-01-05 18:07:02 +0100314 size_t i;
315 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100316 struct fetch_context *ctx = retp->callstack[i].fetch_context;
317 if (ctx != NULL) {
318 struct fetch_context *nctx = fetch_arg_clone(p, ctx);
319 if (nctx == NULL) {
320 int j;
321 release1:
322 for (j = 0; j < i; ++j) {
323 nctx = retp->callstack[i].fetch_context;
324 fetch_arg_done(nctx);
325 retp->callstack[i].fetch_context = NULL;
326 }
327 goto fail2;
328 }
329 retp->callstack[i].fetch_context = nctx;
330 }
331
Petr Machata94078ec2012-01-05 18:07:02 +0100332 struct value_dict *args = retp->callstack[i].arguments;
333 if (args != NULL) {
334 fail3:
335 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100336 if (nargs == NULL
337 || val_dict_clone(nargs, args) < 0) {
338
339 int j;
340 for (j = 0; j < i; ++j) {
341 nargs = p->callstack[i].arguments;
342 val_dict_destroy(nargs);
343 free(nargs);
344 p->callstack[i].arguments = NULL;
345 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100346
347 /* Pretend that this round went well,
348 * so that release1 frees I-th
349 * fetch_context. */
350 ++i;
351 goto release1;
Petr Machata94078ec2012-01-05 18:07:02 +0100352 }
353 retp->callstack[i].arguments = nargs;
354 }
355 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100356
Petr Machata744f2552012-04-15 04:33:18 +0200357 if (arch_process_clone(retp, proc) < 0)
Petr Machata94078ec2012-01-05 18:07:02 +0100358 goto fail3;
Petr Machata744f2552012-04-15 04:33:18 +0200359
Petr Machata2b46cfc2012-02-18 11:17:29 +0100360 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100361}
362
Petr Machata3c516d52011-08-18 03:53:18 +0200363static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200364open_one_pid(pid_t pid)
365{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200366 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100367 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200368 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
369
Petr Machata1974dbc2011-08-19 18:58:01 +0200370 /* Get the filename first. Should the trace_pid fail, we can
371 * easily free it, untracing is more work. */
372 if ((filename = pid2name(pid)) == NULL
373 || trace_pid(pid) < 0) {
374 free(filename);
375 return -1;
376 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100377
Petr Machata75934ad2012-04-14 02:28:03 +0200378 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200379 if (proc == NULL)
380 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200381 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200382
Petr Machata1974dbc2011-08-19 18:58:01 +0200383 return 0;
384}
385
Petr Machata2b46cfc2012-02-18 11:17:29 +0100386static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200387start_one_pid(Process * proc, void * data)
388{
389 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100390 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100391}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100392
Petr Machata9a5420c2011-07-09 11:21:23 +0200393void
394open_pid(pid_t pid)
395{
396 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200397 /* If we are already tracing this guy, we should be seeing all
398 * his children via normal tracing route. */
399 if (pid2proc(pid) != NULL)
400 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200401
Petr Machata3c516d52011-08-18 03:53:18 +0200402 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200403 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200404 fprintf(stderr, "Cannot attach to pid %u: %s\n",
405 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200406 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200407 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200408 }
409
Petr Machata3c516d52011-08-18 03:53:18 +0200410 /* Now attach to all tasks that belong to that PID. There's a
411 * race between process_tasks and open_one_pid. So when we
412 * fail in open_one_pid below, we just do another round.
413 * Chances are that by then that PID will have gone away, and
414 * that's why we have seen the failure. The processes that we
415 * manage to open_one_pid are stopped, so we should eventually
416 * reach a point where process_tasks doesn't give any new
417 * processes (because there's nobody left to produce
418 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200419 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200420 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200421 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200422 pid_t *tasks;
423 size_t ntasks;
424 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200425
Petr Machata3c516d52011-08-18 03:53:18 +0200426 if (process_tasks(pid, &tasks, &ntasks) < 0) {
427 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
428 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100429 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200430 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200431
Petr Machata3c516d52011-08-18 03:53:18 +0200432 have_all = 1;
433 for (i = 0; i < ntasks; ++i)
434 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200435 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200436 have_all = 0;
437
Petr Machata9a5420c2011-07-09 11:21:23 +0200438 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200439
Petr Machata1974dbc2011-08-19 18:58:01 +0200440 if (have_all && old_ntasks == ntasks)
441 break;
442 old_ntasks = ntasks;
443 }
444
Petr Machata93d95df2012-04-17 05:16:19 +0200445 struct Process *leader = pid2proc(pid)->leader;
446
447 /* XXX Is there a way to figure out whether _start has
448 * actually already been hit? */
449 arch_dynlink_done(leader);
450
Petr Machata2f9b78e2012-04-16 21:08:54 +0200451 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200452 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200453}
454
Petr Machata2b46cfc2012-02-18 11:17:29 +0100455static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200456find_proc(Process * proc, void * data)
457{
458 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100459 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200460}
461
Juan Cespedesa8909f72009-04-28 20:02:41 +0200462Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100463pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200464 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
465}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100466
Petr Machatacebb8842011-07-09 11:14:11 +0200467static Process * list_of_processes = NULL;
468
Petr Machatacbe29c62011-09-27 02:27:58 +0200469static void
470unlist_process(Process * proc)
471{
472 Process *tmp;
473
474 if (list_of_processes == proc) {
475 list_of_processes = list_of_processes->next;
476 return;
477 }
478
479 for (tmp = list_of_processes; ; tmp = tmp->next) {
480 /* If the following assert fails, the process wasn't
481 * in the list. */
482 assert(tmp->next != NULL);
483
484 if (tmp->next == proc) {
485 tmp->next = tmp->next->next;
486 return;
487 }
488 }
489}
490
Petr Machata2b46cfc2012-02-18 11:17:29 +0100491struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100492each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100493 enum callback_status(*cb)(struct Process *proc, void *data),
494 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200495{
Petr Machata74132a42012-03-16 02:46:18 +0100496 struct Process *it = start_after == NULL ? list_of_processes
497 : start_after->next;
498
499 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200500 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100501 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100502 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200503 case CBS_FAIL:
504 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100505 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200506 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100507 case CBS_CONT:
508 break;
509 }
Petr Machatacebb8842011-07-09 11:14:11 +0200510 it = next;
511 }
512 return NULL;
513}
Petr Machata9a5420c2011-07-09 11:21:23 +0200514
515Process *
Petr Machata74132a42012-03-16 02:46:18 +0100516each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100517 enum callback_status(*cb)(struct Process *proc, void *data),
518 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200519{
Petr Machata74132a42012-03-16 02:46:18 +0100520 assert(proc != NULL);
521 struct Process *it = start_after == NULL ? proc->leader
522 : start_after->next;
523
Petr Machata9a5420c2011-07-09 11:21:23 +0200524 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100525 struct Process *leader = it->leader;
526 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200527 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100528 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100529 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200530 case CBS_FAIL:
531 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100532 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200533 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100534 case CBS_CONT:
535 break;
536 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200537 it = next;
538 }
539 }
540 return NULL;
541}
542
Petr Machata44965c72012-04-06 19:59:20 +0200543static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200544add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200545{
Petr Machata9a5420c2011-07-09 11:21:23 +0200546 Process ** leaderp = &list_of_processes;
547 if (proc->pid) {
548 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200549 if (tgid == 0)
550 /* Must have been terminated before we managed
551 * to fully attach. */
552 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200553 if (tgid == proc->pid)
554 proc->leader = proc;
555 else {
556 Process * leader = pid2proc(tgid);
557 proc->leader = leader;
558 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200559 leaderp = &leader->next;
560 }
561 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200562
563 if (!was_exec) {
564 proc->next = *leaderp;
565 *leaderp = proc;
566 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200567}
568
Petr Machatacbe29c62011-09-27 02:27:58 +0200569void
570change_process_leader(Process * proc, Process * leader)
571{
572 Process ** leaderp = &list_of_processes;
573 if (proc->leader == leader)
574 return;
575
576 assert(leader != NULL);
577 unlist_process(proc);
578 if (proc != leader)
579 leaderp = &leader->next;
580
581 proc->leader = leader;
582 proc->next = *leaderp;
583 *leaderp = proc;
584}
585
Petr Machata2b46cfc2012-02-18 11:17:29 +0100586static enum callback_status
587clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200588{
589 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
590 proc->pid, proc->leader->pid);
591 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100592 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200593}
594
595void
596remove_process(Process *proc)
597{
Petr Machatacebb8842011-07-09 11:14:11 +0200598 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
599
Petr Machata9a5420c2011-07-09 11:21:23 +0200600 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100601 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200602
Petr Machatacbe29c62011-09-27 02:27:58 +0200603 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100604 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200605 process_destroy(proc);
606 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100607}
Petr Machata4007d742011-07-09 11:29:42 +0200608
609void
Petr Machata366c2f42012-02-09 19:34:36 +0100610install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200611{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200612 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200613 assert(proc->event_handler == NULL);
614 proc->event_handler = handler;
615}
616
617void
618destroy_event_handler(Process * proc)
619{
Petr Machata366c2f42012-02-09 19:34:36 +0100620 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200621 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200622 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200623 if (handler->destroy != NULL)
624 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200625 free(handler);
626 proc->event_handler = NULL;
627}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100628
629static enum callback_status
630breakpoint_for_symbol(struct library_symbol *libsym, void *data)
631{
632 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200633 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100634
Petr Machatad5e85562012-04-05 15:18:38 +0200635 /* If there is an artificial breakpoint on the same address,
636 * its libsym will be NULL, and we can smuggle our libsym
637 * there. That artificial breakpoint is there presumably for
638 * the callbacks, which we don't touch. If there is a real
639 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200640 * symbols and ignore extra symbol aliases.
641 *
642 * The other direction is more complicated and currently not
643 * supported. If a breakpoint has custom callbacks, it might
644 * be also custom-allocated, and we would really need to swap
645 * the two: delete the one now in the dictionary, swap values
646 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200647 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
648 libsym->enter_addr);
649 if (bp != NULL) {
650 assert(bp->libsym == NULL);
651 bp->libsym = libsym;
652 return CBS_CONT;
653 }
654
655 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200656 if (bp == NULL
657 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
658 fail:
659 free(bp);
660 return CBS_FAIL;
661 }
662 if (proc_add_breakpoint(proc, bp) < 0) {
663 breakpoint_destroy(bp);
664 goto fail;
665 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100666
Petr Machatafa0c5702012-04-13 18:43:40 +0200667 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200668 proc_remove_breakpoint(proc, bp);
669 breakpoint_destroy(bp);
670 goto fail;
671 }
672
Petr Machata2b46cfc2012-02-18 11:17:29 +0100673 return CBS_CONT;
674}
675
676void
677proc_add_library(struct Process *proc, struct library *lib)
678{
679 assert(lib->next == NULL);
680 lib->next = proc->libraries;
681 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200682 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
683 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100684
685 struct library_symbol *libsym = NULL;
686 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100687 proc)) != NULL)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200688 fprintf(stderr, "couldn't insert breakpoint for %s to %d: %s",
689 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100690}
691
692int
693proc_remove_library(struct Process *proc, struct library *lib)
694{
695 struct library **libp;
696 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
697 if (*libp == lib) {
698 *libp = lib->next;
699 return 0;
700 }
701 return -1;
702}
703
704struct library *
705proc_each_library(struct Process *proc, struct library *it,
706 enum callback_status (*cb)(struct Process *proc,
707 struct library *lib, void *data),
708 void *data)
709{
710 if (it == NULL)
711 it = proc->libraries;
712
713 while (it != NULL) {
714 struct library *next = it->next;
715
716 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200717 case CBS_FAIL:
718 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100719 case CBS_STOP:
720 return it;
721 case CBS_CONT:
722 break;
723 }
724
725 it = next;
726 }
727
728 return NULL;
729}
Petr Machata52dbfb12012-03-29 16:38:26 +0200730
Petr Machataf7fee432012-04-19 17:00:53 +0200731static void
732check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200733{
Petr Machata52dbfb12012-03-29 16:38:26 +0200734 /* Only the group leader should be getting the breakpoints and
735 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200736 assert(proc->leader != NULL);
737 assert(proc->leader == proc);
738 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200739}
Petr Machata52dbfb12012-03-29 16:38:26 +0200740
Petr Machataf7fee432012-04-19 17:00:53 +0200741int
742proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
743{
Petr Machatafa0c5702012-04-13 18:43:40 +0200744 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200745 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200746 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200747
Petr Machataa2416362012-04-06 02:43:34 +0200748 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200749 * assert, but that's not necessary right now. Read the
750 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200751 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200752
Petr Machatafa0c5702012-04-13 18:43:40 +0200753 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200754 fprintf(stderr,
755 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
756 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200757 return -1;
758 }
759
Petr Machata52dbfb12012-03-29 16:38:26 +0200760 return 0;
761}
762
Petr Machataf7fee432012-04-19 17:00:53 +0200763void
Petr Machata52dbfb12012-03-29 16:38:26 +0200764proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
765{
Petr Machataf7fee432012-04-19 17:00:53 +0200766 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
767 proc->pid, breakpoint_name(bp), bp->addr);
768 check_leader(proc);
769 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
770 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200771}
Petr Machatad3cc9882012-04-13 21:40:23 +0200772
773/* Dict doesn't support iteration restarts, so here's this contraption
774 * for now. XXX add restarts to dict. */
775struct each_breakpoint_data
776{
777 void *start;
778 void *end;
779 struct Process *proc;
780 enum callback_status (*cb)(struct Process *proc,
781 struct breakpoint *bp,
782 void *data);
783 void *cb_data;
784};
785
786static void
787each_breakpoint_cb(void *key, void *value, void *d)
788{
789 struct each_breakpoint_data *data = d;
790 if (data->end != NULL)
791 return;
792 if (data->start == key)
793 data->start = NULL;
794
795 if (data->start == NULL) {
796 switch (data->cb(data->proc, value, data->cb_data)) {
797 case CBS_FAIL:
798 /* XXX handle me */
799 case CBS_STOP:
800 data->end = key;
801 case CBS_CONT:
802 return;
803 }
804 }
805}
806
807void *
808proc_each_breakpoint(struct Process *proc, void *start,
809 enum callback_status (*cb)(struct Process *proc,
810 struct breakpoint *bp,
811 void *data), void *data)
812{
813 struct each_breakpoint_data dd = {
814 .start = start,
815 .proc = proc,
816 .cb = cb,
817 .cb_data = data,
818 };
819 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
820 return dd.end;
821}