blob: b280df883e3d8e456b2d0f740872268241929046 [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 Machatacf1679a2012-04-06 19:56:17 +0200273
Petr Machata2b46cfc2012-02-18 11:17:29 +0100274 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200275 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100276 return 0;
277
278 /* Clone symbols first so that we can clone and relink
279 * breakpoints. */
280 struct library *lib;
281 struct library **nlibp = &retp->libraries;
282 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
283 *nlibp = malloc(sizeof(**nlibp));
284 if (*nlibp == NULL
285 || library_clone(*nlibp, lib) < 0) {
286 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200287 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100288
289 /* Error when cloning. Unroll what was done. */
290 for (lib = retp->libraries; lib != NULL; ) {
291 struct library *next = lib->next;
292 library_destroy(lib);
293 free(lib);
294 lib = next;
295 }
Petr Machataba1664b2012-04-28 14:59:05 +0200296 goto fail1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100297 }
298
299 nlibp = &(*nlibp)->next;
300 }
301
302 /* Now clone breakpoints. Symbol relinking is done in
303 * clone_single_bp. */
304 struct clone_single_bp_data data = {
305 .old_proc = proc,
306 .new_proc = retp,
307 .error = 0,
308 };
309 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100310 if (data.error < 0)
311 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100312
Petr Machataded6f972012-04-13 23:15:48 +0200313 /* And finally the call stack. */
314 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
315 retp->callstack_depth = proc->callstack_depth;
316
Petr Machata94078ec2012-01-05 18:07:02 +0100317 size_t i;
318 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100319 struct fetch_context *ctx = retp->callstack[i].fetch_context;
320 if (ctx != NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200321 struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
Petr Machataf6ec08a2012-01-06 16:58:54 +0100322 if (nctx == NULL) {
Petr Machataba1664b2012-04-28 14:59:05 +0200323 size_t j;
324 fail3:
Petr Machataf6ec08a2012-01-06 16:58:54 +0100325 for (j = 0; j < i; ++j) {
326 nctx = retp->callstack[i].fetch_context;
327 fetch_arg_done(nctx);
328 retp->callstack[i].fetch_context = NULL;
329 }
330 goto fail2;
331 }
332 retp->callstack[i].fetch_context = nctx;
333 }
334
Petr Machata94078ec2012-01-05 18:07:02 +0100335 struct value_dict *args = retp->callstack[i].arguments;
336 if (args != NULL) {
Petr Machata94078ec2012-01-05 18:07:02 +0100337 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100338 if (nargs == NULL
339 || val_dict_clone(nargs, args) < 0) {
Petr Machataba1664b2012-04-28 14:59:05 +0200340 size_t j;
341 fail4:
Petr Machata94078ec2012-01-05 18:07:02 +0100342 for (j = 0; j < i; ++j) {
Petr Machataba1664b2012-04-28 14:59:05 +0200343 nargs = retp->callstack[i].arguments;
Petr Machata94078ec2012-01-05 18:07:02 +0100344 val_dict_destroy(nargs);
345 free(nargs);
Petr Machataba1664b2012-04-28 14:59:05 +0200346 retp->callstack[i].arguments = NULL;
Petr Machata94078ec2012-01-05 18:07:02 +0100347 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100348
349 /* Pretend that this round went well,
Petr Machataba1664b2012-04-28 14:59:05 +0200350 * so that fail3 frees I-th
Petr Machataf6ec08a2012-01-06 16:58:54 +0100351 * fetch_context. */
352 ++i;
Petr Machataba1664b2012-04-28 14:59:05 +0200353 goto fail3;
Petr Machata94078ec2012-01-05 18:07:02 +0100354 }
355 retp->callstack[i].arguments = nargs;
356 }
357 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100358
Petr Machata744f2552012-04-15 04:33:18 +0200359 if (arch_process_clone(retp, proc) < 0)
Petr Machataba1664b2012-04-28 14:59:05 +0200360 goto fail4;
Petr Machata744f2552012-04-15 04:33:18 +0200361
Petr Machata2b46cfc2012-02-18 11:17:29 +0100362 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100363}
364
Petr Machata3c516d52011-08-18 03:53:18 +0200365static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200366open_one_pid(pid_t pid)
367{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200368 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100369 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200370 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
371
Petr Machata1974dbc2011-08-19 18:58:01 +0200372 /* Get the filename first. Should the trace_pid fail, we can
373 * easily free it, untracing is more work. */
374 if ((filename = pid2name(pid)) == NULL
375 || trace_pid(pid) < 0) {
376 free(filename);
377 return -1;
378 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100379
Petr Machata75934ad2012-04-14 02:28:03 +0200380 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200381 if (proc == NULL)
382 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200383 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200384
Petr Machata1974dbc2011-08-19 18:58:01 +0200385 return 0;
386}
387
Petr Machata2b46cfc2012-02-18 11:17:29 +0100388static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200389start_one_pid(Process * proc, void * data)
390{
391 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100392 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100393}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100394
Petr Machata9a5420c2011-07-09 11:21:23 +0200395void
396open_pid(pid_t pid)
397{
398 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200399 /* If we are already tracing this guy, we should be seeing all
400 * his children via normal tracing route. */
401 if (pid2proc(pid) != NULL)
402 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200403
Petr Machata3c516d52011-08-18 03:53:18 +0200404 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200405 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200406 fprintf(stderr, "Cannot attach to pid %u: %s\n",
407 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200408 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200409 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200410 }
411
Petr Machata3c516d52011-08-18 03:53:18 +0200412 /* Now attach to all tasks that belong to that PID. There's a
413 * race between process_tasks and open_one_pid. So when we
414 * fail in open_one_pid below, we just do another round.
415 * Chances are that by then that PID will have gone away, and
416 * that's why we have seen the failure. The processes that we
417 * manage to open_one_pid are stopped, so we should eventually
418 * reach a point where process_tasks doesn't give any new
419 * processes (because there's nobody left to produce
420 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200421 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200422 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200423 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200424 pid_t *tasks;
425 size_t ntasks;
426 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200427
Petr Machata3c516d52011-08-18 03:53:18 +0200428 if (process_tasks(pid, &tasks, &ntasks) < 0) {
429 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
430 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100431 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200432 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200433
Petr Machata3c516d52011-08-18 03:53:18 +0200434 have_all = 1;
435 for (i = 0; i < ntasks; ++i)
436 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200437 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200438 have_all = 0;
439
Petr Machata9a5420c2011-07-09 11:21:23 +0200440 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200441
Petr Machata1974dbc2011-08-19 18:58:01 +0200442 if (have_all && old_ntasks == ntasks)
443 break;
444 old_ntasks = ntasks;
445 }
446
Petr Machata93d95df2012-04-17 05:16:19 +0200447 struct Process *leader = pid2proc(pid)->leader;
448
449 /* XXX Is there a way to figure out whether _start has
450 * actually already been hit? */
451 arch_dynlink_done(leader);
452
Petr Machata2f9b78e2012-04-16 21:08:54 +0200453 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200454 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200455}
456
Petr Machata2b46cfc2012-02-18 11:17:29 +0100457static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200458find_proc(Process * proc, void * data)
459{
460 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100461 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200462}
463
Juan Cespedesa8909f72009-04-28 20:02:41 +0200464Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100465pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200466 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
467}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100468
Petr Machatacebb8842011-07-09 11:14:11 +0200469static Process * list_of_processes = NULL;
470
Petr Machatacbe29c62011-09-27 02:27:58 +0200471static void
472unlist_process(Process * proc)
473{
474 Process *tmp;
475
476 if (list_of_processes == proc) {
477 list_of_processes = list_of_processes->next;
478 return;
479 }
480
481 for (tmp = list_of_processes; ; tmp = tmp->next) {
482 /* If the following assert fails, the process wasn't
483 * in the list. */
484 assert(tmp->next != NULL);
485
486 if (tmp->next == proc) {
487 tmp->next = tmp->next->next;
488 return;
489 }
490 }
491}
492
Petr Machata2b46cfc2012-02-18 11:17:29 +0100493struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100494each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100495 enum callback_status(*cb)(struct Process *proc, void *data),
496 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200497{
Petr Machata74132a42012-03-16 02:46:18 +0100498 struct Process *it = start_after == NULL ? list_of_processes
499 : start_after->next;
500
501 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200502 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100503 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100504 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200505 case CBS_FAIL:
506 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100507 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200508 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100509 case CBS_CONT:
510 break;
511 }
Petr Machatacebb8842011-07-09 11:14:11 +0200512 it = next;
513 }
514 return NULL;
515}
Petr Machata9a5420c2011-07-09 11:21:23 +0200516
517Process *
Petr Machata74132a42012-03-16 02:46:18 +0100518each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100519 enum callback_status(*cb)(struct Process *proc, void *data),
520 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200521{
Petr Machata74132a42012-03-16 02:46:18 +0100522 assert(proc != NULL);
523 struct Process *it = start_after == NULL ? proc->leader
524 : start_after->next;
525
Petr Machata9a5420c2011-07-09 11:21:23 +0200526 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100527 struct Process *leader = it->leader;
528 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200529 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100530 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100531 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200532 case CBS_FAIL:
533 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100534 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200535 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100536 case CBS_CONT:
537 break;
538 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200539 it = next;
540 }
541 }
542 return NULL;
543}
544
Petr Machata44965c72012-04-06 19:59:20 +0200545static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200546add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200547{
Petr Machata9a5420c2011-07-09 11:21:23 +0200548 Process ** leaderp = &list_of_processes;
549 if (proc->pid) {
550 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200551 if (tgid == 0)
552 /* Must have been terminated before we managed
553 * to fully attach. */
554 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200555 if (tgid == proc->pid)
556 proc->leader = proc;
557 else {
558 Process * leader = pid2proc(tgid);
559 proc->leader = leader;
560 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200561 leaderp = &leader->next;
562 }
563 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200564
565 if (!was_exec) {
566 proc->next = *leaderp;
567 *leaderp = proc;
568 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200569}
570
Petr Machatacbe29c62011-09-27 02:27:58 +0200571void
572change_process_leader(Process * proc, Process * leader)
573{
574 Process ** leaderp = &list_of_processes;
575 if (proc->leader == leader)
576 return;
577
578 assert(leader != NULL);
579 unlist_process(proc);
580 if (proc != leader)
581 leaderp = &leader->next;
582
583 proc->leader = leader;
584 proc->next = *leaderp;
585 *leaderp = proc;
586}
587
Petr Machata2b46cfc2012-02-18 11:17:29 +0100588static enum callback_status
589clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200590{
591 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
592 proc->pid, proc->leader->pid);
593 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100594 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200595}
596
597void
598remove_process(Process *proc)
599{
Petr Machatacebb8842011-07-09 11:14:11 +0200600 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
601
Petr Machata9a5420c2011-07-09 11:21:23 +0200602 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100603 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200604
Petr Machatacbe29c62011-09-27 02:27:58 +0200605 unlist_process(proc);
Petr Machatacd972582012-01-07 03:02:07 +0100606 process_removed(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200607 process_destroy(proc);
608 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100609}
Petr Machata4007d742011-07-09 11:29:42 +0200610
611void
Petr Machata366c2f42012-02-09 19:34:36 +0100612install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200613{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200614 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200615 assert(proc->event_handler == NULL);
616 proc->event_handler = handler;
617}
618
619void
620destroy_event_handler(Process * proc)
621{
Petr Machata366c2f42012-02-09 19:34:36 +0100622 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200623 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200624 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200625 if (handler->destroy != NULL)
626 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200627 free(handler);
628 proc->event_handler = NULL;
629}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100630
631static enum callback_status
632breakpoint_for_symbol(struct library_symbol *libsym, void *data)
633{
634 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200635 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100636
Petr Machatad5e85562012-04-05 15:18:38 +0200637 /* If there is an artificial breakpoint on the same address,
638 * its libsym will be NULL, and we can smuggle our libsym
639 * there. That artificial breakpoint is there presumably for
640 * the callbacks, which we don't touch. If there is a real
641 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200642 * symbols and ignore extra symbol aliases.
643 *
644 * The other direction is more complicated and currently not
645 * supported. If a breakpoint has custom callbacks, it might
646 * be also custom-allocated, and we would really need to swap
647 * the two: delete the one now in the dictionary, swap values
648 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200649 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
650 libsym->enter_addr);
651 if (bp != NULL) {
652 assert(bp->libsym == NULL);
653 bp->libsym = libsym;
654 return CBS_CONT;
655 }
656
657 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200658 if (bp == NULL
659 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
660 fail:
661 free(bp);
662 return CBS_FAIL;
663 }
664 if (proc_add_breakpoint(proc, bp) < 0) {
665 breakpoint_destroy(bp);
666 goto fail;
667 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100668
Petr Machatafa0c5702012-04-13 18:43:40 +0200669 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200670 proc_remove_breakpoint(proc, bp);
671 breakpoint_destroy(bp);
672 goto fail;
673 }
674
Petr Machata2b46cfc2012-02-18 11:17:29 +0100675 return CBS_CONT;
676}
677
678void
679proc_add_library(struct Process *proc, struct library *lib)
680{
681 assert(lib->next == NULL);
682 lib->next = proc->libraries;
683 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200684 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
685 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100686
687 struct library_symbol *libsym = NULL;
688 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100689 proc)) != NULL)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200690 fprintf(stderr, "couldn't insert breakpoint for %s to %d: %s",
691 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100692}
693
694int
695proc_remove_library(struct Process *proc, struct library *lib)
696{
697 struct library **libp;
698 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
699 if (*libp == lib) {
700 *libp = lib->next;
701 return 0;
702 }
703 return -1;
704}
705
706struct library *
707proc_each_library(struct Process *proc, struct library *it,
708 enum callback_status (*cb)(struct Process *proc,
709 struct library *lib, void *data),
710 void *data)
711{
712 if (it == NULL)
713 it = proc->libraries;
714
715 while (it != NULL) {
716 struct library *next = it->next;
717
718 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200719 case CBS_FAIL:
720 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100721 case CBS_STOP:
722 return it;
723 case CBS_CONT:
724 break;
725 }
726
727 it = next;
728 }
729
730 return NULL;
731}
Petr Machata52dbfb12012-03-29 16:38:26 +0200732
Petr Machataf7fee432012-04-19 17:00:53 +0200733static void
734check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200735{
Petr Machata52dbfb12012-03-29 16:38:26 +0200736 /* Only the group leader should be getting the breakpoints and
737 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200738 assert(proc->leader != NULL);
739 assert(proc->leader == proc);
740 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200741}
Petr Machata52dbfb12012-03-29 16:38:26 +0200742
Petr Machataf7fee432012-04-19 17:00:53 +0200743int
744proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
745{
Petr Machatafa0c5702012-04-13 18:43:40 +0200746 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200747 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200748 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200749
Petr Machataa2416362012-04-06 02:43:34 +0200750 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200751 * assert, but that's not necessary right now. Read the
752 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200753 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200754
Petr Machatafa0c5702012-04-13 18:43:40 +0200755 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200756 fprintf(stderr,
757 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
758 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200759 return -1;
760 }
761
Petr Machata52dbfb12012-03-29 16:38:26 +0200762 return 0;
763}
764
Petr Machataf7fee432012-04-19 17:00:53 +0200765void
Petr Machata52dbfb12012-03-29 16:38:26 +0200766proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
767{
Petr Machataf7fee432012-04-19 17:00:53 +0200768 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
769 proc->pid, breakpoint_name(bp), bp->addr);
770 check_leader(proc);
771 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
772 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200773}
Petr Machatad3cc9882012-04-13 21:40:23 +0200774
775/* Dict doesn't support iteration restarts, so here's this contraption
776 * for now. XXX add restarts to dict. */
777struct each_breakpoint_data
778{
779 void *start;
780 void *end;
781 struct Process *proc;
782 enum callback_status (*cb)(struct Process *proc,
783 struct breakpoint *bp,
784 void *data);
785 void *cb_data;
786};
787
788static void
789each_breakpoint_cb(void *key, void *value, void *d)
790{
791 struct each_breakpoint_data *data = d;
792 if (data->end != NULL)
793 return;
794 if (data->start == key)
795 data->start = NULL;
796
797 if (data->start == NULL) {
798 switch (data->cb(data->proc, value, data->cb_data)) {
799 case CBS_FAIL:
800 /* XXX handle me */
801 case CBS_STOP:
802 data->end = key;
803 case CBS_CONT:
804 return;
805 }
806 }
807}
808
809void *
810proc_each_breakpoint(struct Process *proc, void *start,
811 enum callback_status (*cb)(struct Process *proc,
812 struct breakpoint *bp,
813 void *data), void *data)
814{
815 struct each_breakpoint_data dd = {
816 .start = start,
817 .proc = proc,
818 .cb = cb,
819 .cb_data = data,
820 };
821 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
822 return dd.end;
823}