blob: 6b0cf97948addbb1c73fb6cec8d0ce57de797775 [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 Machata0f6e6d92012-10-26 23:42:17 +020069#ifndef OS_HAVE_PROCESS_DATA
70int
71os_process_init(struct Process *proc)
72{
73 return 0;
74}
75
76void
77os_process_destroy(struct Process *proc)
78{
79}
80
81int
82os_process_clone(struct Process *retp, struct Process *proc)
83{
84 return 0;
85}
86
87int
88os_process_exec(struct Process *proc)
89{
90 return 0;
91}
92#endif
93
Petr Machata93d95df2012-04-17 05:16:19 +020094#ifndef ARCH_HAVE_DYNLINK_DONE
95void
96arch_dynlink_done(struct Process *proc)
97{
98}
99#endif
100
Petr Machata3d0c91c2012-04-14 02:37:38 +0200101static void add_process(struct Process *proc, int was_exec);
Petr Machata61686c22012-05-03 18:39:49 +0200102static void unlist_process(struct Process *proc);
Petr Machata44965c72012-04-06 19:59:20 +0200103
Petr Machatae677c7e2012-10-26 22:23:43 +0200104static void
105destroy_unwind(struct Process *proc)
106{
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 Machata3d0c91c2012-04-14 02:37:38 +0200114process_bare_init(struct Process *proc, const char *filename,
115 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100116{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200117 if (!was_exec) {
118 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +0200119
Petr Machata3d0c91c2012-04-14 02:37:38 +0200120 proc->filename = strdup(filename);
121 if (proc->filename == NULL) {
122 fail:
123 free(proc->filename);
124 if (proc->breakpoints != NULL)
125 dict_clear(proc->breakpoints);
126 return -1;
127 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100128 }
129
130 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +0200131 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200132 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100133 if (proc->leader == NULL)
134 goto fail;
135
136 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +0200137 proc->breakpoints = dict_init(target_address_hash,
138 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100139 if (proc->breakpoints == NULL)
140 goto fail;
141 } else {
142 proc->breakpoints = NULL;
143 }
144
Joe Damatoab3b72c2010-10-31 00:21:53 -0700145#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +0200146 proc->unwind_priv = _UPT_create(pid);
147 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700148#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -0700149
Petr Machata2b46cfc2012-02-18 11:17:29 +0100150 return 0;
151}
152
153static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200154process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100155{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100156 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200157 if (!was_exec) {
158 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200159 unlist_process(proc);
Petr Machatae677c7e2012-10-26 22:23:43 +0200160 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200161 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100162}
163
Petr Machata3d0c91c2012-04-14 02:37:38 +0200164static int
165process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100166{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200167 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200168 fprintf(stderr, "failed to init breakpoints %d\n",
169 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200170 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200171 }
172
Petr Machata2b46cfc2012-02-18 11:17:29 +0100173 return 0;
174}
175
Petr Machata3d0c91c2012-04-14 02:37:38 +0200176int
177process_init(struct Process *proc, const char *filename, pid_t pid)
178{
179 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200180 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200181 fprintf(stderr, "failed to initialize process %d: %s\n",
182 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200183 return -1;
184 }
185
Petr Machata0f6e6d92012-10-26 23:42:17 +0200186 if (os_process_init(proc) < 0) {
187 process_bare_destroy(proc, 0);
188 goto fail;
189 }
190
Petr Machata744f2552012-04-15 04:33:18 +0200191 if (arch_process_init(proc) < 0) {
Petr Machata0f6e6d92012-10-26 23:42:17 +0200192 os_process_destroy(proc);
Petr Machata744f2552012-04-15 04:33:18 +0200193 process_bare_destroy(proc, 0);
194 goto fail;
195 }
196
Petr Machata218c5ff2012-04-15 04:22:39 +0200197 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200198 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200199 if (process_init_main(proc) < 0) {
200 process_bare_destroy(proc, 0);
201 goto fail;
202 }
203 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200204}
205
Petr Machata8ead1cd2012-04-24 18:13:09 +0200206static enum callback_status
207destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200208{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200209 breakpoint_destroy(bp);
210 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200211 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200212}
213
Petr Machatae0e89ed2012-10-26 22:25:33 +0200214// XXX see comment in handle_event.c
215void callstack_pop(struct Process *proc);
216
Petr Machata3d0c91c2012-04-14 02:37:38 +0200217static void
Petr Machatae0e89ed2012-10-26 22:25:33 +0200218private_process_destroy(struct Process *proc, int was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200219{
Petr Machatae0e89ed2012-10-26 22:25:33 +0200220 /* Pop remaining stack elements. */
221 while (proc->callstack_depth > 0) {
222 /* When this is called just before a process is
223 * destroyed, the breakpoints should either have been
224 * retracted by now, or were killed by exec. In any
225 * case, it's safe to pretend that there are no
226 * breakpoints associated with the stack elements, so
227 * that stack_pop doesn't attempt to destroy them. */
228 size_t i = proc->callstack_depth - 1;
229 if (!proc->callstack[i].is_syscall)
230 proc->callstack[i].return_addr = 0;
231
232 callstack_pop(proc);
233 }
234
235 if (!was_exec)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200236 free(proc->filename);
237
Petr Machata8ead1cd2012-04-24 18:13:09 +0200238 /* Libraries and symbols. This is only relevant in
239 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200240 struct library *lib;
241 for (lib = proc->libraries; lib != NULL; ) {
242 struct library *next = lib->next;
243 library_destroy(lib);
244 free(lib);
245 lib = next;
246 }
247 proc->libraries = NULL;
248
249 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200250 if (proc->breakpoints != NULL) {
251 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
252 dict_clear(proc->breakpoints);
253 proc->breakpoints = NULL;
254 }
Petr Machatae677c7e2012-10-26 22:23:43 +0200255
256 destroy_unwind(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200257}
258
259void
260process_destroy(struct Process *proc)
261{
Petr Machata744f2552012-04-15 04:33:18 +0200262 arch_process_destroy(proc);
Petr Machata0f6e6d92012-10-26 23:42:17 +0200263 os_process_destroy(proc);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200264 private_process_destroy(proc, 0);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200265}
266
267int
268process_exec(struct Process *proc)
269{
Petr Machata0f6e6d92012-10-26 23:42:17 +0200270 /* Call exec handlers first, before we destroy the main
Petr Machata3cc0cd12012-10-26 22:30:51 +0200271 * state. */
Petr Machata0f6e6d92012-10-26 23:42:17 +0200272 if (arch_process_exec(proc) < 0
273 || os_process_exec(proc) < 0)
Petr Machata744f2552012-04-15 04:33:18 +0200274 return -1;
275
Petr Machata3d0c91c2012-04-14 02:37:38 +0200276 private_process_destroy(proc, 1);
Petr Machata3cc0cd12012-10-26 22:30:51 +0200277
Petr Machata3d0c91c2012-04-14 02:37:38 +0200278 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
279 return -1;
280 if (process_init_main(proc) < 0) {
281 process_bare_destroy(proc, 1);
282 return -1;
283 }
284 return 0;
285}
286
Petr Machata2b46cfc2012-02-18 11:17:29 +0100287struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200288open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290 assert(pid != 0);
291 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200292 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200293 free(proc);
294 return NULL;
295 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100296 return proc;
297}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100298
Petr Machata2b46cfc2012-02-18 11:17:29 +0100299struct clone_single_bp_data {
300 struct Process *old_proc;
301 struct Process *new_proc;
302 int error;
303};
304
Petr Machata2b46cfc2012-02-18 11:17:29 +0100305static void
306clone_single_bp(void *key, void *value, void *u)
307{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100308 struct breakpoint *bp = value;
309 struct clone_single_bp_data *data = u;
310
Petr Machatad3cc9882012-04-13 21:40:23 +0200311 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312 struct breakpoint *clone = malloc(sizeof(*clone));
313 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200314 || breakpoint_clone(clone, data->new_proc,
315 bp, data->old_proc) < 0) {
316 fail:
317 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100318 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100319 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200320 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
321 breakpoint_destroy(clone);
322 goto fail;
323 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100324}
325
326int
327process_clone(struct Process *retp, struct Process *proc, pid_t pid)
328{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200329 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200330 fail1:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200331 fprintf(stderr, "failed to clone process %d->%d : %s\n",
332 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333 return -1;
334 }
335
Petr Machatacf1679a2012-04-06 19:56:17 +0200336 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200337 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400338 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200339
Petr Machata2b46cfc2012-02-18 11:17:29 +0100340 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200341 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100342 return 0;
343
344 /* Clone symbols first so that we can clone and relink
345 * breakpoints. */
346 struct library *lib;
347 struct library **nlibp = &retp->libraries;
348 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
349 *nlibp = malloc(sizeof(**nlibp));
350 if (*nlibp == NULL
351 || library_clone(*nlibp, lib) < 0) {
352 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200353 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100354
355 /* Error when cloning. Unroll what was done. */
356 for (lib = retp->libraries; lib != NULL; ) {
357 struct library *next = lib->next;
358 library_destroy(lib);
359 free(lib);
360 lib = next;
361 }
Petr Machataba1664b2012-04-28 14:59:05 +0200362 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100363 }
364
365 nlibp = &(*nlibp)->next;
366 }
367
368 /* Now clone breakpoints. Symbol relinking is done in
369 * clone_single_bp. */
370 struct clone_single_bp_data data = {
371 .old_proc = proc,
372 .new_proc = retp,
373 .error = 0,
374 };
375 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100376 if (data.error < 0)
377 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100378
Petr Machataded6f972012-04-13 23:15:48 +0200379 /* And finally the call stack. */
380 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
381 retp->callstack_depth = proc->callstack_depth;
382
Petr Machata94078ec2012-01-05 18:07:02 +0100383 size_t i;
384 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100385 struct fetch_context *ctx = retp->callstack[i].fetch_context;
386 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200387 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100388 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200389 size_t j;
390 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100391 for (j = 0; j < i; ++j) {
392 nctx = retp->callstack[i].fetch_context;
393 fetch_arg_done(nctx);
394 retp->callstack[i].fetch_context = NULL;
395 }
396 goto fail2;
397 }
398 retp->callstack[i].fetch_context = nctx;
399 }
400
Petr Machata94078ec2012-01-05 18:07:02 +0100401 struct value_dict *args = retp->callstack[i].arguments;
402 if (args != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100403 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100404 if (nargs == NULL
405 || val_dict_clone(nargs, args) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200406 size_t j;
407 fail4:
Petr Machata94078ec2012-01-05 18:07:02 +0100408 for (j = 0; j < i; ++j) {
Petr Machataba1664b2012-04-28 14:59:05 +0200409 nargs = retp->callstack[i].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100410 val_dict_destroy(nargs);
411 free(nargs);
Petr Machataba1664b2012-04-28 14:59:05 +0200412 retp->callstack[i].arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100413 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100414
415 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200416 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100417 * fetch_context. */
418 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200419 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100420 }
421 retp->callstack[i].arguments = nargs;
422 }
423 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100424
Petr Machata0f6e6d92012-10-26 23:42:17 +0200425 if (os_process_clone(retp, proc) < 0
426 || arch_process_clone(retp, proc) < 0)
Petr Machataba1664b2012-04-28 14:59:05 +0200427 goto fail4;
Petr Machata744f2552012-04-15 04:33:18 +0200428
Petr Machata2b46cfc2012-02-18 11:17:29 +0100429 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100430}
431
Petr Machata3c516d52011-08-18 03:53:18 +0200432static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200433open_one_pid(pid_t pid)
434{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200435 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100436 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200437 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
438
Petr Machata1974dbc2011-08-19 18:58:01 +0200439 /* Get the filename first. Should the trace_pid fail, we can
440 * easily free it, untracing is more work. */
441 if ((filename = pid2name(pid)) == NULL
442 || trace_pid(pid) < 0) {
443 free(filename);
444 return -1;
445 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100446
Petr Machata75934ad2012-04-14 02:28:03 +0200447 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200448 if (proc == NULL)
449 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200450 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200451
Petr Machata1974dbc2011-08-19 18:58:01 +0200452 return 0;
453}
454
Petr Machata2b46cfc2012-02-18 11:17:29 +0100455static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200456start_one_pid(Process * proc, void * data)
457{
458 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100459 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100460}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100461
Petr Machata9a5420c2011-07-09 11:21:23 +0200462void
463open_pid(pid_t pid)
464{
465 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200466 /* If we are already tracing this guy, we should be seeing all
467 * his children via normal tracing route. */
468 if (pid2proc(pid) != NULL)
469 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200470
Petr Machata3c516d52011-08-18 03:53:18 +0200471 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200472 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200473 fprintf(stderr, "Cannot attach to pid %u: %s\n",
474 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200475 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200476 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200477 }
478
Petr Machata3c516d52011-08-18 03:53:18 +0200479 /* Now attach to all tasks that belong to that PID. There's a
480 * race between process_tasks and open_one_pid. So when we
481 * fail in open_one_pid below, we just do another round.
482 * Chances are that by then that PID will have gone away, and
483 * that's why we have seen the failure. The processes that we
484 * manage to open_one_pid are stopped, so we should eventually
485 * reach a point where process_tasks doesn't give any new
486 * processes (because there's nobody left to produce
487 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200488 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200489 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200490 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200491 pid_t *tasks;
492 size_t ntasks;
493 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200494
Petr Machata3c516d52011-08-18 03:53:18 +0200495 if (process_tasks(pid, &tasks, &ntasks) < 0) {
496 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
497 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100498 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200499 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200500
Petr Machata3c516d52011-08-18 03:53:18 +0200501 have_all = 1;
502 for (i = 0; i < ntasks; ++i)
503 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200504 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200505 have_all = 0;
506
Petr Machata9a5420c2011-07-09 11:21:23 +0200507 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200508
Petr Machata1974dbc2011-08-19 18:58:01 +0200509 if (have_all && old_ntasks == ntasks)
510 break;
511 old_ntasks = ntasks;
512 }
513
Petr Machata93d95df2012-04-17 05:16:19 +0200514 struct Process *leader = pid2proc(pid)->leader;
515
516 /* XXX Is there a way to figure out whether _start has
517 * actually already been hit? */
518 arch_dynlink_done(leader);
519
Petr Machata2f9b78e2012-04-16 21:08:54 +0200520 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200521 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200522}
523
Petr Machata2b46cfc2012-02-18 11:17:29 +0100524static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200525find_proc(Process * proc, void * data)
526{
527 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100528 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200529}
530
Juan Cespedesa8909f72009-04-28 20:02:41 +0200531Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100532pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200533 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
534}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100535
Petr Machatacebb8842011-07-09 11:14:11 +0200536static Process * list_of_processes = NULL;
537
Petr Machatacbe29c62011-09-27 02:27:58 +0200538static void
539unlist_process(Process * proc)
540{
541 Process *tmp;
542
543 if (list_of_processes == proc) {
544 list_of_processes = list_of_processes->next;
545 return;
546 }
547
548 for (tmp = list_of_processes; ; tmp = tmp->next) {
549 /* If the following assert fails, the process wasn't
550 * in the list. */
551 assert(tmp->next != NULL);
552
553 if (tmp->next == proc) {
554 tmp->next = tmp->next->next;
555 return;
556 }
557 }
558}
559
Petr Machata2b46cfc2012-02-18 11:17:29 +0100560struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100561each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100562 enum callback_status(*cb)(struct Process *proc, void *data),
563 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200564{
Petr Machata74132a42012-03-16 02:46:18 +0100565 struct Process *it = start_after == NULL ? list_of_processes
566 : start_after->next;
567
568 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200569 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100570 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100571 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200572 case CBS_FAIL:
573 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100574 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200575 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100576 case CBS_CONT:
577 break;
578 }
Petr Machatacebb8842011-07-09 11:14:11 +0200579 it = next;
580 }
581 return NULL;
582}
Petr Machata9a5420c2011-07-09 11:21:23 +0200583
584Process *
Petr Machata74132a42012-03-16 02:46:18 +0100585each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100586 enum callback_status(*cb)(struct Process *proc, void *data),
587 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200588{
Petr Machata74132a42012-03-16 02:46:18 +0100589 assert(proc != NULL);
590 struct Process *it = start_after == NULL ? proc->leader
591 : start_after->next;
592
Petr Machata9a5420c2011-07-09 11:21:23 +0200593 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100594 struct Process *leader = it->leader;
595 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200596 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100597 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100598 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200599 case CBS_FAIL:
600 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100601 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200602 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100603 case CBS_CONT:
604 break;
605 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200606 it = next;
607 }
608 }
609 return NULL;
610}
611
Petr Machata44965c72012-04-06 19:59:20 +0200612static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200613add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200614{
Petr Machata9a5420c2011-07-09 11:21:23 +0200615 Process ** leaderp = &list_of_processes;
616 if (proc->pid) {
617 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200618 if (tgid == 0)
619 /* Must have been terminated before we managed
620 * to fully attach. */
621 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200622 if (tgid == proc->pid)
623 proc->leader = proc;
624 else {
625 Process * leader = pid2proc(tgid);
626 proc->leader = leader;
627 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200628 leaderp = &leader->next;
629 }
630 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200631
632 if (!was_exec) {
633 proc->next = *leaderp;
634 *leaderp = proc;
635 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200636}
637
Petr Machatacbe29c62011-09-27 02:27:58 +0200638void
639change_process_leader(Process * proc, Process * leader)
640{
641 Process ** leaderp = &list_of_processes;
642 if (proc->leader == leader)
643 return;
644
645 assert(leader != NULL);
646 unlist_process(proc);
647 if (proc != leader)
648 leaderp = &leader->next;
649
650 proc->leader = leader;
651 proc->next = *leaderp;
652 *leaderp = proc;
653}
654
Petr Machata2b46cfc2012-02-18 11:17:29 +0100655static enum callback_status
656clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200657{
658 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
659 proc->pid, proc->leader->pid);
660 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100661 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200662}
663
664void
665remove_process(Process *proc)
666{
Petr Machatacebb8842011-07-09 11:14:11 +0200667 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
668
Petr Machata9a5420c2011-07-09 11:21:23 +0200669 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100670 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200671
Petr Machatacbe29c62011-09-27 02:27:58 +0200672 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100673 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200674 process_destroy(proc);
675 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100676}
Petr Machata4007d742011-07-09 11:29:42 +0200677
678void
Petr Machata366c2f42012-02-09 19:34:36 +0100679install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200680{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200681 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200682 assert(proc->event_handler == NULL);
683 proc->event_handler = handler;
684}
685
686void
687destroy_event_handler(Process * proc)
688{
Petr Machata366c2f42012-02-09 19:34:36 +0100689 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200690 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200691 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200692 if (handler->destroy != NULL)
693 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200694 free(handler);
695 proc->event_handler = NULL;
696}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100697
Petr Machataef2fd272012-09-28 00:43:01 +0200698static int
699breakpoint_for_symbol(struct library_symbol *libsym, struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100700{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200701 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200702 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100703
Petr Machataef2fd272012-09-28 00:43:01 +0200704 /* Don't enable latent or delayed symbols. */
705 if (libsym->latent || libsym->delayed)
706 return 0;
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200707
Edgar E. Iglesiasf97b1872012-10-01 12:43:34 +0200708 bp_addr = sym2addr(proc, libsym);
709
Petr Machatad5e85562012-04-05 15:18:38 +0200710 /* If there is an artificial breakpoint on the same address,
711 * its libsym will be NULL, and we can smuggle our libsym
712 * there. That artificial breakpoint is there presumably for
713 * the callbacks, which we don't touch. If there is a real
714 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200715 * symbols and ignore extra symbol aliases.
716 *
717 * The other direction is more complicated and currently not
718 * supported. If a breakpoint has custom callbacks, it might
719 * be also custom-allocated, and we would really need to swap
720 * the two: delete the one now in the dictionary, swap values
721 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200722 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200723 bp_addr);
Petr Machatad5e85562012-04-05 15:18:38 +0200724 if (bp != NULL) {
725 assert(bp->libsym == NULL);
726 bp->libsym = libsym;
Petr Machataef2fd272012-09-28 00:43:01 +0200727 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200728 }
729
730 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200731 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200732 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200733 fail:
734 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200735 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200736 }
737 if (proc_add_breakpoint(proc, bp) < 0) {
738 breakpoint_destroy(bp);
739 goto fail;
740 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100741
Petr Machatafa0c5702012-04-13 18:43:40 +0200742 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200743 proc_remove_breakpoint(proc, bp);
744 breakpoint_destroy(bp);
745 goto fail;
746 }
747
Petr Machataef2fd272012-09-28 00:43:01 +0200748 return 0;
749}
750
751static enum callback_status
752cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
753{
754 return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
755}
756
757static int
758proc_activate_latent_symbol(struct Process *proc,
759 struct library_symbol *libsym)
760{
761 assert(libsym->latent);
762 libsym->latent = 0;
763 return breakpoint_for_symbol(libsym, proc);
764}
765
766int
767proc_activate_delayed_symbol(struct Process *proc,
768 struct library_symbol *libsym)
769{
770 assert(libsym->delayed);
771 libsym->delayed = 0;
772 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100773}
774
Petr Machataa1f76832012-09-28 00:08:00 +0200775static enum callback_status
776activate_latent_in(struct Process *proc, struct library *lib, void *data)
777{
778 struct library_exported_name *exported;
779 for (exported = data; exported != NULL; exported = exported->next) {
780 struct library_symbol *libsym = NULL;
781 while ((libsym = library_each_symbol(lib, libsym,
782 library_symbol_named_cb,
783 (void *)exported->name))
784 != NULL)
785 if (libsym->latent
786 && proc_activate_latent_symbol(proc, libsym) < 0)
787 return CBS_FAIL;
788 }
789 return CBS_CONT;
790}
791
Petr Machata2b46cfc2012-02-18 11:17:29 +0100792void
793proc_add_library(struct Process *proc, struct library *lib)
794{
795 assert(lib->next == NULL);
796 lib->next = proc->libraries;
797 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200798 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
799 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100800
Petr Machataef2fd272012-09-28 00:43:01 +0200801 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100802 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200803 while ((libsym = library_each_symbol(lib, libsym,
804 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100805 proc)) != NULL)
Petr Machataef2fd272012-09-28 00:43:01 +0200806 fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s.",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200807 libsym->name, proc->pid, strerror(errno));
Petr Machataa1f76832012-09-28 00:08:00 +0200808
809 /* Look through export list of the new library and compare it
810 * with latent symbols of all libraries (including this
811 * library itself). */
812 struct library *lib2 = NULL;
813 while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
814 lib->exported_names)) != NULL)
815 fprintf(stderr,
816 "Couldn't activate latent symbols for %s in %d: %s.",
817 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100818}
819
820int
821proc_remove_library(struct Process *proc, struct library *lib)
822{
823 struct library **libp;
824 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
825 if (*libp == lib) {
826 *libp = lib->next;
827 return 0;
828 }
829 return -1;
830}
831
832struct library *
833proc_each_library(struct Process *proc, struct library *it,
834 enum callback_status (*cb)(struct Process *proc,
835 struct library *lib, void *data),
836 void *data)
837{
838 if (it == NULL)
839 it = proc->libraries;
840
841 while (it != NULL) {
842 struct library *next = it->next;
843
844 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200845 case CBS_FAIL:
846 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100847 case CBS_STOP:
848 return it;
849 case CBS_CONT:
850 break;
851 }
852
853 it = next;
854 }
855
856 return NULL;
857}
Petr Machata52dbfb12012-03-29 16:38:26 +0200858
Petr Machataf7fee432012-04-19 17:00:53 +0200859static void
860check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200861{
Petr Machata52dbfb12012-03-29 16:38:26 +0200862 /* Only the group leader should be getting the breakpoints and
863 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200864 assert(proc->leader != NULL);
865 assert(proc->leader == proc);
866 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200867}
Petr Machata52dbfb12012-03-29 16:38:26 +0200868
Petr Machataf7fee432012-04-19 17:00:53 +0200869int
870proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
871{
Petr Machatafa0c5702012-04-13 18:43:40 +0200872 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200873 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200874 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200875
Petr Machataa2416362012-04-06 02:43:34 +0200876 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200877 * assert, but that's not necessary right now. Read the
878 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200879 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200880
Petr Machatafa0c5702012-04-13 18:43:40 +0200881 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200882 fprintf(stderr,
883 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
884 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200885 return -1;
886 }
887
Petr Machata52dbfb12012-03-29 16:38:26 +0200888 return 0;
889}
890
Petr Machataf7fee432012-04-19 17:00:53 +0200891void
Petr Machata52dbfb12012-03-29 16:38:26 +0200892proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
893{
Petr Machataf7fee432012-04-19 17:00:53 +0200894 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
895 proc->pid, breakpoint_name(bp), bp->addr);
896 check_leader(proc);
897 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
898 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200899}
Petr Machatad3cc9882012-04-13 21:40:23 +0200900
901/* Dict doesn't support iteration restarts, so here's this contraption
902 * for now. XXX add restarts to dict. */
903struct each_breakpoint_data
904{
905 void *start;
906 void *end;
907 struct Process *proc;
908 enum callback_status (*cb)(struct Process *proc,
909 struct breakpoint *bp,
910 void *data);
911 void *cb_data;
912};
913
914static void
915each_breakpoint_cb(void *key, void *value, void *d)
916{
917 struct each_breakpoint_data *data = d;
918 if (data->end != NULL)
919 return;
920 if (data->start == key)
921 data->start = NULL;
922
923 if (data->start == NULL) {
924 switch (data->cb(data->proc, value, data->cb_data)) {
925 case CBS_FAIL:
926 /* XXX handle me */
927 case CBS_STOP:
928 data->end = key;
929 case CBS_CONT:
930 return;
931 }
932 }
933}
934
935void *
936proc_each_breakpoint(struct Process *proc, void *start,
937 enum callback_status (*cb)(struct Process *proc,
938 struct breakpoint *bp,
939 void *data), void *data)
940{
941 struct each_breakpoint_data dd = {
942 .start = start,
943 .proc = proc,
944 .cb = cb,
945 .cb_data = data,
946 };
947 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
948 return dd.end;
949}