blob: eb3e552d6cc07d18ca47470bd507c44d26008351 [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 Machata2b46cfc2012-02-18 11:17:29 +010079static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020080process_bare_init(struct Process *proc, const char *filename,
81 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010082{
Petr Machata3d0c91c2012-04-14 02:37:38 +020083 if (!was_exec) {
84 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020085
Petr Machata3d0c91c2012-04-14 02:37:38 +020086 proc->filename = strdup(filename);
87 if (proc->filename == NULL) {
88 fail:
89 free(proc->filename);
90 if (proc->breakpoints != NULL)
91 dict_clear(proc->breakpoints);
92 return -1;
93 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010094 }
95
96 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020097 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +020098 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +010099 if (proc->leader == NULL)
100 goto fail;
101
102 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +0200103 proc->breakpoints = dict_init(target_address_hash,
104 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100105 if (proc->breakpoints == NULL)
106 goto fail;
107 } else {
108 proc->breakpoints = NULL;
109 }
110
Joe Damatoab3b72c2010-10-31 00:21:53 -0700111#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +0200112 proc->unwind_priv = _UPT_create(pid);
113 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -0700114#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -0700115
Petr Machata2b46cfc2012-02-18 11:17:29 +0100116 return 0;
117}
118
119static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200120process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100121{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100122 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200123 if (!was_exec) {
124 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200125 unlist_process(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200126 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100127}
128
Petr Machata3d0c91c2012-04-14 02:37:38 +0200129static int
130process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100131{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200132 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200133 fprintf(stderr, "failed to init breakpoints %d\n",
134 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200135 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200136 }
137
Petr Machata2b46cfc2012-02-18 11:17:29 +0100138 return 0;
139}
140
Petr Machata3d0c91c2012-04-14 02:37:38 +0200141int
142process_init(struct Process *proc, const char *filename, pid_t pid)
143{
144 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200145 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200146 fprintf(stderr, "failed to initialize process %d: %s\n",
147 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200148 return -1;
149 }
150
Petr Machata744f2552012-04-15 04:33:18 +0200151 if (arch_process_init(proc) < 0) {
152 process_bare_destroy(proc, 0);
153 goto fail;
154 }
155
Petr Machata218c5ff2012-04-15 04:22:39 +0200156 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200157 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200158 if (process_init_main(proc) < 0) {
159 process_bare_destroy(proc, 0);
160 goto fail;
161 }
162 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200163}
164
Petr Machata8ead1cd2012-04-24 18:13:09 +0200165static enum callback_status
166destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200167{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200168 breakpoint_destroy(bp);
169 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200170 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200171}
172
173static void
174private_process_destroy(struct Process *proc, int keep_filename)
175{
176 if (!keep_filename)
177 free(proc->filename);
178
Petr Machata8ead1cd2012-04-24 18:13:09 +0200179 /* Libraries and symbols. This is only relevant in
180 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200181 struct library *lib;
182 for (lib = proc->libraries; lib != NULL; ) {
183 struct library *next = lib->next;
184 library_destroy(lib);
185 free(lib);
186 lib = next;
187 }
188 proc->libraries = NULL;
189
190 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200191 if (proc->breakpoints != NULL) {
192 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
193 dict_clear(proc->breakpoints);
194 proc->breakpoints = NULL;
195 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200196}
197
198void
199process_destroy(struct Process *proc)
200{
201 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200202 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200203}
204
205int
206process_exec(struct Process *proc)
207{
Petr Machata744f2552012-04-15 04:33:18 +0200208 /* Call exec first, before we destroy the main state. */
209 if (arch_process_exec(proc) < 0)
210 return -1;
211
Petr Machata3d0c91c2012-04-14 02:37:38 +0200212 private_process_destroy(proc, 1);
213 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
214 return -1;
215 if (process_init_main(proc) < 0) {
216 process_bare_destroy(proc, 1);
217 return -1;
218 }
219 return 0;
220}
221
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200223open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100224{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100225 assert(pid != 0);
226 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200227 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200228 free(proc);
229 return NULL;
230 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100231 return proc;
232}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100233
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234struct clone_single_bp_data {
235 struct Process *old_proc;
236 struct Process *new_proc;
237 int error;
238};
239
Petr Machata2b46cfc2012-02-18 11:17:29 +0100240static void
241clone_single_bp(void *key, void *value, void *u)
242{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100243 struct breakpoint *bp = value;
244 struct clone_single_bp_data *data = u;
245
Petr Machatad3cc9882012-04-13 21:40:23 +0200246 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100247 struct breakpoint *clone = malloc(sizeof(*clone));
248 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200249 || breakpoint_clone(clone, data->new_proc,
250 bp, data->old_proc) < 0) {
251 fail:
252 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100253 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100254 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200255 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
256 breakpoint_destroy(clone);
257 goto fail;
258 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100259}
260
261int
262process_clone(struct Process *retp, struct Process *proc, pid_t pid)
263{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200264 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200265 fail1:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200266 fprintf(stderr, "failed to clone process %d->%d : %s\n",
267 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100268 return -1;
269 }
270
Petr Machatacf1679a2012-04-06 19:56:17 +0200271 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200272 retp->e_machine = proc->e_machine;
Petr Machata4d4e1b82012-05-30 11:08:39 -0400273 retp->e_class = proc->e_class;
Petr Machatacf1679a2012-04-06 19:56:17 +0200274
Petr Machata2b46cfc2012-02-18 11:17:29 +0100275 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200276 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100277 return 0;
278
279 /* Clone symbols first so that we can clone and relink
280 * breakpoints. */
281 struct library *lib;
282 struct library **nlibp = &retp->libraries;
283 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
284 *nlibp = malloc(sizeof(**nlibp));
285 if (*nlibp == NULL
286 || library_clone(*nlibp, lib) < 0) {
287 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200288 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100289
290 /* Error when cloning. Unroll what was done. */
291 for (lib = retp->libraries; lib != NULL; ) {
292 struct library *next = lib->next;
293 library_destroy(lib);
294 free(lib);
295 lib = next;
296 }
Petr Machataba1664b2012-04-28 14:59:05 +0200297 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100298 }
299
300 nlibp = &(*nlibp)->next;
301 }
302
303 /* Now clone breakpoints. Symbol relinking is done in
304 * clone_single_bp. */
305 struct clone_single_bp_data data = {
306 .old_proc = proc,
307 .new_proc = retp,
308 .error = 0,
309 };
310 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100311 if (data.error < 0)
312 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100313
Petr Machataded6f972012-04-13 23:15:48 +0200314 /* And finally the call stack. */
315 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
316 retp->callstack_depth = proc->callstack_depth;
317
Petr Machata94078ec2012-01-05 18:07:02 +0100318 size_t i;
319 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100320 struct fetch_context *ctx = retp->callstack[i].fetch_context;
321 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200322 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100323 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200324 size_t j;
325 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100326 for (j = 0; j < i; ++j) {
327 nctx = retp->callstack[i].fetch_context;
328 fetch_arg_done(nctx);
329 retp->callstack[i].fetch_context = NULL;
330 }
331 goto fail2;
332 }
333 retp->callstack[i].fetch_context = nctx;
334 }
335
Petr Machata94078ec2012-01-05 18:07:02 +0100336 struct value_dict *args = retp->callstack[i].arguments;
337 if (args != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100338 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100339 if (nargs == NULL
340 || val_dict_clone(nargs, args) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200341 size_t j;
342 fail4:
Petr Machata94078ec2012-01-05 18:07:02 +0100343 for (j = 0; j < i; ++j) {
Petr Machataba1664b2012-04-28 14:59:05 +0200344 nargs = retp->callstack[i].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100345 val_dict_destroy(nargs);
346 free(nargs);
Petr Machataba1664b2012-04-28 14:59:05 +0200347 retp->callstack[i].arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100348 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100349
350 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200351 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100352 * fetch_context. */
353 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200354 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100355 }
356 retp->callstack[i].arguments = nargs;
357 }
358 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100359
Petr Machata744f2552012-04-15 04:33:18 +0200360 if (arch_process_clone(retp, proc) < 0)
Petr Machataba1664b2012-04-28 14:59:05 +0200361 goto fail4;
Petr Machata744f2552012-04-15 04:33:18 +0200362
Petr Machata2b46cfc2012-02-18 11:17:29 +0100363 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100364}
365
Petr Machata3c516d52011-08-18 03:53:18 +0200366static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200367open_one_pid(pid_t pid)
368{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200369 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100370 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200371 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
372
Petr Machata1974dbc2011-08-19 18:58:01 +0200373 /* Get the filename first. Should the trace_pid fail, we can
374 * easily free it, untracing is more work. */
375 if ((filename = pid2name(pid)) == NULL
376 || trace_pid(pid) < 0) {
377 free(filename);
378 return -1;
379 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100380
Petr Machata75934ad2012-04-14 02:28:03 +0200381 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200382 if (proc == NULL)
383 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200384 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200385
Petr Machata1974dbc2011-08-19 18:58:01 +0200386 return 0;
387}
388
Petr Machata2b46cfc2012-02-18 11:17:29 +0100389static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200390start_one_pid(Process * proc, void * data)
391{
392 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100393 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100394}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100395
Petr Machata9a5420c2011-07-09 11:21:23 +0200396void
397open_pid(pid_t pid)
398{
399 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200400 /* If we are already tracing this guy, we should be seeing all
401 * his children via normal tracing route. */
402 if (pid2proc(pid) != NULL)
403 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200404
Petr Machata3c516d52011-08-18 03:53:18 +0200405 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200406 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200407 fprintf(stderr, "Cannot attach to pid %u: %s\n",
408 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200409 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200410 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200411 }
412
Petr Machata3c516d52011-08-18 03:53:18 +0200413 /* Now attach to all tasks that belong to that PID. There's a
414 * race between process_tasks and open_one_pid. So when we
415 * fail in open_one_pid below, we just do another round.
416 * Chances are that by then that PID will have gone away, and
417 * that's why we have seen the failure. The processes that we
418 * manage to open_one_pid are stopped, so we should eventually
419 * reach a point where process_tasks doesn't give any new
420 * processes (because there's nobody left to produce
421 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200422 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200423 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200424 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200425 pid_t *tasks;
426 size_t ntasks;
427 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200428
Petr Machata3c516d52011-08-18 03:53:18 +0200429 if (process_tasks(pid, &tasks, &ntasks) < 0) {
430 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
431 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100432 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200433 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200434
Petr Machata3c516d52011-08-18 03:53:18 +0200435 have_all = 1;
436 for (i = 0; i < ntasks; ++i)
437 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200438 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200439 have_all = 0;
440
Petr Machata9a5420c2011-07-09 11:21:23 +0200441 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200442
Petr Machata1974dbc2011-08-19 18:58:01 +0200443 if (have_all && old_ntasks == ntasks)
444 break;
445 old_ntasks = ntasks;
446 }
447
Petr Machata93d95df2012-04-17 05:16:19 +0200448 struct Process *leader = pid2proc(pid)->leader;
449
450 /* XXX Is there a way to figure out whether _start has
451 * actually already been hit? */
452 arch_dynlink_done(leader);
453
Petr Machata2f9b78e2012-04-16 21:08:54 +0200454 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200455 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200456}
457
Petr Machata2b46cfc2012-02-18 11:17:29 +0100458static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200459find_proc(Process * proc, void * data)
460{
461 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100462 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200463}
464
Juan Cespedesa8909f72009-04-28 20:02:41 +0200465Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100466pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200467 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
468}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100469
Petr Machatacebb8842011-07-09 11:14:11 +0200470static Process * list_of_processes = NULL;
471
Petr Machatacbe29c62011-09-27 02:27:58 +0200472static void
473unlist_process(Process * proc)
474{
475 Process *tmp;
476
477 if (list_of_processes == proc) {
478 list_of_processes = list_of_processes->next;
479 return;
480 }
481
482 for (tmp = list_of_processes; ; tmp = tmp->next) {
483 /* If the following assert fails, the process wasn't
484 * in the list. */
485 assert(tmp->next != NULL);
486
487 if (tmp->next == proc) {
488 tmp->next = tmp->next->next;
489 return;
490 }
491 }
492}
493
Petr Machata2b46cfc2012-02-18 11:17:29 +0100494struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100495each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100496 enum callback_status(*cb)(struct Process *proc, void *data),
497 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200498{
Petr Machata74132a42012-03-16 02:46:18 +0100499 struct Process *it = start_after == NULL ? list_of_processes
500 : start_after->next;
501
502 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200503 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100504 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100505 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200506 case CBS_FAIL:
507 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100508 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200509 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100510 case CBS_CONT:
511 break;
512 }
Petr Machatacebb8842011-07-09 11:14:11 +0200513 it = next;
514 }
515 return NULL;
516}
Petr Machata9a5420c2011-07-09 11:21:23 +0200517
518Process *
Petr Machata74132a42012-03-16 02:46:18 +0100519each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100520 enum callback_status(*cb)(struct Process *proc, void *data),
521 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200522{
Petr Machata74132a42012-03-16 02:46:18 +0100523 assert(proc != NULL);
524 struct Process *it = start_after == NULL ? proc->leader
525 : start_after->next;
526
Petr Machata9a5420c2011-07-09 11:21:23 +0200527 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100528 struct Process *leader = it->leader;
529 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200530 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100531 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100532 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200533 case CBS_FAIL:
534 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100535 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200536 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100537 case CBS_CONT:
538 break;
539 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200540 it = next;
541 }
542 }
543 return NULL;
544}
545
Petr Machata44965c72012-04-06 19:59:20 +0200546static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200547add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200548{
Petr Machata9a5420c2011-07-09 11:21:23 +0200549 Process ** leaderp = &list_of_processes;
550 if (proc->pid) {
551 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200552 if (tgid == 0)
553 /* Must have been terminated before we managed
554 * to fully attach. */
555 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200556 if (tgid == proc->pid)
557 proc->leader = proc;
558 else {
559 Process * leader = pid2proc(tgid);
560 proc->leader = leader;
561 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200562 leaderp = &leader->next;
563 }
564 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200565
566 if (!was_exec) {
567 proc->next = *leaderp;
568 *leaderp = proc;
569 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200570}
571
Petr Machatacbe29c62011-09-27 02:27:58 +0200572void
573change_process_leader(Process * proc, Process * leader)
574{
575 Process ** leaderp = &list_of_processes;
576 if (proc->leader == leader)
577 return;
578
579 assert(leader != NULL);
580 unlist_process(proc);
581 if (proc != leader)
582 leaderp = &leader->next;
583
584 proc->leader = leader;
585 proc->next = *leaderp;
586 *leaderp = proc;
587}
588
Petr Machata2b46cfc2012-02-18 11:17:29 +0100589static enum callback_status
590clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200591{
592 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
593 proc->pid, proc->leader->pid);
594 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100595 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200596}
597
598void
599remove_process(Process *proc)
600{
Petr Machatacebb8842011-07-09 11:14:11 +0200601 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
602
Petr Machata9a5420c2011-07-09 11:21:23 +0200603 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100604 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200605
Petr Machatacbe29c62011-09-27 02:27:58 +0200606 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100607 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200608 process_destroy(proc);
609 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100610}
Petr Machata4007d742011-07-09 11:29:42 +0200611
612void
Petr Machata366c2f42012-02-09 19:34:36 +0100613install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200614{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200615 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200616 assert(proc->event_handler == NULL);
617 proc->event_handler = handler;
618}
619
620void
621destroy_event_handler(Process * proc)
622{
Petr Machata366c2f42012-02-09 19:34:36 +0100623 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200624 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200625 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200626 if (handler->destroy != NULL)
627 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200628 free(handler);
629 proc->event_handler = NULL;
630}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100631
Petr Machataef2fd272012-09-28 00:43:01 +0200632static int
633breakpoint_for_symbol(struct library_symbol *libsym, struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100634{
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200635 arch_addr_t bp_addr;
Petr Machatad5e85562012-04-05 15:18:38 +0200636 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100637
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200638 bp_addr = sym2addr(proc, libsym);
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200639
640 /* For external function pointers, MIPS brings in stub-less funcs
641 * that point to zero at startup. These symbols get resolved by
642 * the dynamic linker and are ready to use at arch_dynlink_done().
643 *
644 * Allow the backend to add these into the process representation
645 * but don't put breakpoints at this point. Let the backend fix that
Petr Machataef2fd272012-09-28 00:43:01 +0200646 * up later.
647 *
648 * XXX This should be changed to delayed symbols. */
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200649 if (bp_addr == 0 && libsym->plt_type == LS_TOPLT_GOTONLY) {
650 /* Don't add breakpoints yet. */
651 return CBS_CONT;
652 }
Petr Machataef2fd272012-09-28 00:43:01 +0200653 /* Don't enable latent or delayed symbols. */
654 if (libsym->latent || libsym->delayed)
655 return 0;
Edgar E. Iglesias6ef7b252012-09-27 17:02:38 +0200656
Petr Machatad5e85562012-04-05 15:18:38 +0200657 /* If there is an artificial breakpoint on the same address,
658 * its libsym will be NULL, and we can smuggle our libsym
659 * there. That artificial breakpoint is there presumably for
660 * the callbacks, which we don't touch. If there is a real
661 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200662 * symbols and ignore extra symbol aliases.
663 *
664 * The other direction is more complicated and currently not
665 * supported. If a breakpoint has custom callbacks, it might
666 * be also custom-allocated, and we would really need to swap
667 * the two: delete the one now in the dictionary, swap values
668 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200669 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200670 bp_addr);
Petr Machatad5e85562012-04-05 15:18:38 +0200671 if (bp != NULL) {
672 assert(bp->libsym == NULL);
673 bp->libsym = libsym;
Petr Machataef2fd272012-09-28 00:43:01 +0200674 return 0;
Petr Machatad5e85562012-04-05 15:18:38 +0200675 }
676
677 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200678 if (bp == NULL
Edgar E. Iglesiasad640472012-09-27 12:07:34 +0200679 || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
Petr Machata3fd099b2012-04-03 02:25:42 +0200680 fail:
681 free(bp);
Petr Machataef2fd272012-09-28 00:43:01 +0200682 return -1;
Petr Machata3fd099b2012-04-03 02:25:42 +0200683 }
684 if (proc_add_breakpoint(proc, bp) < 0) {
685 breakpoint_destroy(bp);
686 goto fail;
687 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100688
Petr Machatafa0c5702012-04-13 18:43:40 +0200689 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200690 proc_remove_breakpoint(proc, bp);
691 breakpoint_destroy(bp);
692 goto fail;
693 }
694
Petr Machataef2fd272012-09-28 00:43:01 +0200695 return 0;
696}
697
698static enum callback_status
699cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
700{
701 return breakpoint_for_symbol(libsym, data) < 0 ? CBS_FAIL : CBS_CONT;
702}
703
704static int
705proc_activate_latent_symbol(struct Process *proc,
706 struct library_symbol *libsym)
707{
708 assert(libsym->latent);
709 libsym->latent = 0;
710 return breakpoint_for_symbol(libsym, proc);
711}
712
713int
714proc_activate_delayed_symbol(struct Process *proc,
715 struct library_symbol *libsym)
716{
717 assert(libsym->delayed);
718 libsym->delayed = 0;
719 return breakpoint_for_symbol(libsym, proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100720}
721
722void
723proc_add_library(struct Process *proc, struct library *lib)
724{
725 assert(lib->next == NULL);
726 lib->next = proc->libraries;
727 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200728 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
729 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100730
Petr Machataef2fd272012-09-28 00:43:01 +0200731 /* Insert breakpoints for all active (non-latent) symbols. */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100732 struct library_symbol *libsym = NULL;
Petr Machataef2fd272012-09-28 00:43:01 +0200733 while ((libsym = library_each_symbol(lib, libsym,
734 cb_breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100735 proc)) != NULL)
Petr Machataef2fd272012-09-28 00:43:01 +0200736 fprintf(stderr, "Couldn't insert breakpoint for %s to %d: %s.",
Petr Machatacc0e1e42012-04-25 13:42:07 +0200737 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100738}
739
740int
741proc_remove_library(struct Process *proc, struct library *lib)
742{
743 struct library **libp;
744 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
745 if (*libp == lib) {
746 *libp = lib->next;
747 return 0;
748 }
749 return -1;
750}
751
752struct library *
753proc_each_library(struct Process *proc, struct library *it,
754 enum callback_status (*cb)(struct Process *proc,
755 struct library *lib, void *data),
756 void *data)
757{
758 if (it == NULL)
759 it = proc->libraries;
760
761 while (it != NULL) {
762 struct library *next = it->next;
763
764 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200765 case CBS_FAIL:
766 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100767 case CBS_STOP:
768 return it;
769 case CBS_CONT:
770 break;
771 }
772
773 it = next;
774 }
775
776 return NULL;
777}
Petr Machata52dbfb12012-03-29 16:38:26 +0200778
Petr Machataf7fee432012-04-19 17:00:53 +0200779static void
780check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200781{
Petr Machata52dbfb12012-03-29 16:38:26 +0200782 /* Only the group leader should be getting the breakpoints and
783 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200784 assert(proc->leader != NULL);
785 assert(proc->leader == proc);
786 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200787}
Petr Machata52dbfb12012-03-29 16:38:26 +0200788
Petr Machataf7fee432012-04-19 17:00:53 +0200789int
790proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
791{
Petr Machatafa0c5702012-04-13 18:43:40 +0200792 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200793 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200794 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200795
Petr Machataa2416362012-04-06 02:43:34 +0200796 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200797 * assert, but that's not necessary right now. Read the
798 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200799 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200800
Petr Machatafa0c5702012-04-13 18:43:40 +0200801 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200802 fprintf(stderr,
803 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
804 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200805 return -1;
806 }
807
Petr Machata52dbfb12012-03-29 16:38:26 +0200808 return 0;
809}
810
Petr Machataf7fee432012-04-19 17:00:53 +0200811void
Petr Machata52dbfb12012-03-29 16:38:26 +0200812proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
813{
Petr Machataf7fee432012-04-19 17:00:53 +0200814 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
815 proc->pid, breakpoint_name(bp), bp->addr);
816 check_leader(proc);
817 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
818 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200819}
Petr Machatad3cc9882012-04-13 21:40:23 +0200820
821/* Dict doesn't support iteration restarts, so here's this contraption
822 * for now. XXX add restarts to dict. */
823struct each_breakpoint_data
824{
825 void *start;
826 void *end;
827 struct Process *proc;
828 enum callback_status (*cb)(struct Process *proc,
829 struct breakpoint *bp,
830 void *data);
831 void *cb_data;
832};
833
834static void
835each_breakpoint_cb(void *key, void *value, void *d)
836{
837 struct each_breakpoint_data *data = d;
838 if (data->end != NULL)
839 return;
840 if (data->start == key)
841 data->start = NULL;
842
843 if (data->start == NULL) {
844 switch (data->cb(data->proc, value, data->cb_data)) {
845 case CBS_FAIL:
846 /* XXX handle me */
847 case CBS_STOP:
848 data->end = key;
849 case CBS_CONT:
850 return;
851 }
852 }
853}
854
855void *
856proc_each_breakpoint(struct Process *proc, void *start,
857 enum callback_status (*cb)(struct Process *proc,
858 struct breakpoint *bp,
859 void *data), void *data)
860{
861 struct each_breakpoint_data dd = {
862 .start = start,
863 .proc = proc,
864 .cb = cb,
865 .cb_data = data,
866 };
867 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
868 return dd.end;
869}