blob: cd8f6cdcfb6779f5063eace1280b186d220cac88 [file] [log] [blame]
Joe Damatoab3b72c2010-10-31 00:21:53 -07001#include "config.h"
2
3#if defined(HAVE_LIBUNWIND)
4#include <libunwind.h>
5#include <libunwind-ptrace.h>
6#endif /* defined(HAVE_LIBUNWIND) */
7
Juan Cespedes273ea6d1998-03-14 23:02:40 +01008#include <sys/types.h>
9#include <string.h>
10#include <stdio.h>
11#include <errno.h>
12#include <stdlib.h>
Petr Machata1b17dbf2011-07-08 19:22:52 +020013#include <assert.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +010014
Juan Cespedesf7281232009-06-25 16:11:21 +020015#include "common.h"
Petr Machata9294d822012-02-07 12:35:58 +010016#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010017#include "proc.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010018
Petr Machata744f2552012-04-15 04:33:18 +020019#ifndef ARCH_HAVE_PROCESS_DATA
20int
21arch_process_init(struct Process *proc)
22{
23 return 0;
24}
25
26void
27arch_process_destroy(struct Process *proc)
28{
29}
30
31int
32arch_process_clone(struct Process *retp, struct Process *proc)
33{
34 return 0;
35}
36
37int
38arch_process_exec(struct Process *proc)
39{
40 return 0;
41}
42#endif
43
Petr Machata93d95df2012-04-17 05:16:19 +020044#ifndef ARCH_HAVE_DYNLINK_DONE
45void
46arch_dynlink_done(struct Process *proc)
47{
48}
49#endif
50
Petr Machata3d0c91c2012-04-14 02:37:38 +020051static void add_process(struct Process *proc, int was_exec);
Petr Machata61686c22012-05-03 18:39:49 +020052static void unlist_process(struct Process *proc);
Petr Machata44965c72012-04-06 19:59:20 +020053
Petr Machata2b46cfc2012-02-18 11:17:29 +010054static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020055process_bare_init(struct Process *proc, const char *filename,
56 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010057{
Petr Machata3d0c91c2012-04-14 02:37:38 +020058 if (!was_exec) {
59 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020060
Petr Machata3d0c91c2012-04-14 02:37:38 +020061 proc->filename = strdup(filename);
62 if (proc->filename == NULL) {
63 fail:
64 free(proc->filename);
65 if (proc->breakpoints != NULL)
66 dict_clear(proc->breakpoints);
67 return -1;
68 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010069 }
70
71 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020072 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +020073 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +010074 if (proc->leader == NULL)
75 goto fail;
76
77 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +020078 proc->breakpoints = dict_init(target_address_hash,
79 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +010080 if (proc->breakpoints == NULL)
81 goto fail;
82 } else {
83 proc->breakpoints = NULL;
84 }
85
Joe Damatoab3b72c2010-10-31 00:21:53 -070086#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020087 proc->unwind_priv = _UPT_create(pid);
88 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070089#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070090
Petr Machata2b46cfc2012-02-18 11:17:29 +010091 return 0;
92}
93
94static void
Petr Machata3d0c91c2012-04-14 02:37:38 +020095process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010096{
Petr Machata2b46cfc2012-02-18 11:17:29 +010097 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +020098 if (!was_exec) {
99 free(proc->filename);
Petr Machata61686c22012-05-03 18:39:49 +0200100 unlist_process(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200101 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100102}
103
Petr Machata3d0c91c2012-04-14 02:37:38 +0200104static int
105process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100106{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200107 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200108 fprintf(stderr, "failed to init breakpoints %d\n",
109 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200110 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200111 }
112
Petr Machata2b46cfc2012-02-18 11:17:29 +0100113 return 0;
114}
115
Petr Machata3d0c91c2012-04-14 02:37:38 +0200116int
117process_init(struct Process *proc, const char *filename, pid_t pid)
118{
119 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200120 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200121 fprintf(stderr, "failed to initialize process %d: %s\n",
122 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200123 return -1;
124 }
125
Petr Machata744f2552012-04-15 04:33:18 +0200126 if (arch_process_init(proc) < 0) {
127 process_bare_destroy(proc, 0);
128 goto fail;
129 }
130
Petr Machata218c5ff2012-04-15 04:22:39 +0200131 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200132 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200133 if (process_init_main(proc) < 0) {
134 process_bare_destroy(proc, 0);
135 goto fail;
136 }
137 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200138}
139
Petr Machata8ead1cd2012-04-24 18:13:09 +0200140static enum callback_status
141destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200142{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200143 breakpoint_destroy(bp);
144 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200145 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200146}
147
148static void
149private_process_destroy(struct Process *proc, int keep_filename)
150{
151 if (!keep_filename)
152 free(proc->filename);
153
Petr Machata8ead1cd2012-04-24 18:13:09 +0200154 /* Libraries and symbols. This is only relevant in
155 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200156 struct library *lib;
157 for (lib = proc->libraries; lib != NULL; ) {
158 struct library *next = lib->next;
159 library_destroy(lib);
160 free(lib);
161 lib = next;
162 }
163 proc->libraries = NULL;
164
165 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200166 if (proc->breakpoints != NULL) {
167 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
168 dict_clear(proc->breakpoints);
169 proc->breakpoints = NULL;
170 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200171}
172
173void
174process_destroy(struct Process *proc)
175{
176 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200177 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200178}
179
180int
181process_exec(struct Process *proc)
182{
Petr Machata744f2552012-04-15 04:33:18 +0200183 /* Call exec first, before we destroy the main state. */
184 if (arch_process_exec(proc) < 0)
185 return -1;
186
Petr Machata3d0c91c2012-04-14 02:37:38 +0200187 private_process_destroy(proc, 1);
188 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
189 return -1;
190 if (process_init_main(proc) < 0) {
191 process_bare_destroy(proc, 1);
192 return -1;
193 }
194 return 0;
195}
196
Petr Machata2b46cfc2012-02-18 11:17:29 +0100197struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200198open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100199{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100200 assert(pid != 0);
201 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200202 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200203 free(proc);
204 return NULL;
205 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100206 return proc;
207}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100208
Petr Machata2b46cfc2012-02-18 11:17:29 +0100209struct clone_single_bp_data {
210 struct Process *old_proc;
211 struct Process *new_proc;
212 int error;
213};
214
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215static void
216clone_single_bp(void *key, void *value, void *u)
217{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100218 struct breakpoint *bp = value;
219 struct clone_single_bp_data *data = u;
220
Petr Machatad3cc9882012-04-13 21:40:23 +0200221 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222 struct breakpoint *clone = malloc(sizeof(*clone));
223 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200224 || breakpoint_clone(clone, data->new_proc,
225 bp, data->old_proc) < 0) {
226 fail:
227 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100228 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100229 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200230 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
231 breakpoint_destroy(clone);
232 goto fail;
233 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100234}
235
236int
237process_clone(struct Process *retp, struct Process *proc, pid_t pid)
238{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200239 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100240 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200241 fprintf(stderr, "failed to clone process %d->%d : %s\n",
242 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100243 return -1;
244 }
245
Petr Machatacf1679a2012-04-06 19:56:17 +0200246 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200247 retp->e_machine = proc->e_machine;
Petr Machatacf1679a2012-04-06 19:56:17 +0200248
Petr Machata2b46cfc2012-02-18 11:17:29 +0100249 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200250 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251 return 0;
252
253 /* Clone symbols first so that we can clone and relink
254 * breakpoints. */
255 struct library *lib;
256 struct library **nlibp = &retp->libraries;
257 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
258 *nlibp = malloc(sizeof(**nlibp));
259 if (*nlibp == NULL
260 || library_clone(*nlibp, lib) < 0) {
261 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200262 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100263
264 /* Error when cloning. Unroll what was done. */
265 for (lib = retp->libraries; lib != NULL; ) {
266 struct library *next = lib->next;
267 library_destroy(lib);
268 free(lib);
269 lib = next;
270 }
271 goto fail;
272 }
273
274 nlibp = &(*nlibp)->next;
275 }
276
277 /* Now clone breakpoints. Symbol relinking is done in
278 * clone_single_bp. */
279 struct clone_single_bp_data data = {
280 .old_proc = proc,
281 .new_proc = retp,
282 .error = 0,
283 };
284 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
Petr Machata94078ec2012-01-05 18:07:02 +0100285 if (data.error < 0)
286 goto fail2;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100287
Petr Machataded6f972012-04-13 23:15:48 +0200288 /* And finally the call stack. */
289 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
290 retp->callstack_depth = proc->callstack_depth;
291
Petr Machata94078ec2012-01-05 18:07:02 +0100292 size_t i;
293 for (i = 0; i < retp->callstack_depth; ++i) {
Petr Machataf6ec08a2012-01-06 16:58:54 +0100294 struct fetch_context *ctx = retp->callstack[i].fetch_context;
295 if (ctx != NULL) {
296 struct fetch_context *nctx = fetch_arg_clone(p, ctx);
297 if (nctx == NULL) {
298 int j;
299 release1:
300 for (j = 0; j < i; ++j) {
301 nctx = retp->callstack[i].fetch_context;
302 fetch_arg_done(nctx);
303 retp->callstack[i].fetch_context = NULL;
304 }
305 goto fail2;
306 }
307 retp->callstack[i].fetch_context = nctx;
308 }
309
Petr Machata94078ec2012-01-05 18:07:02 +0100310 struct value_dict *args = retp->callstack[i].arguments;
311 if (args != NULL) {
312 fail3:
313 struct value_dict *nargs = malloc(sizeof(*nargs));
Petr Machata94078ec2012-01-05 18:07:02 +0100314 if (nargs == NULL
315 || val_dict_clone(nargs, args) < 0) {
316
317 int j;
318 for (j = 0; j < i; ++j) {
319 nargs = p->callstack[i].arguments;
320 val_dict_destroy(nargs);
321 free(nargs);
322 p->callstack[i].arguments = NULL;
323 }
Petr Machataf6ec08a2012-01-06 16:58:54 +0100324
325 /* Pretend that this round went well,
326 * so that release1 frees I-th
327 * fetch_context. */
328 ++i;
329 goto release1;
Petr Machata94078ec2012-01-05 18:07:02 +0100330 }
331 retp->callstack[i].arguments = nargs;
332 }
333 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100334
Petr Machata744f2552012-04-15 04:33:18 +0200335 if (arch_process_clone(retp, proc) < 0)
Petr Machata94078ec2012-01-05 18:07:02 +0100336 goto fail3;
Petr Machata744f2552012-04-15 04:33:18 +0200337
Petr Machata2b46cfc2012-02-18 11:17:29 +0100338 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100339}
340
Petr Machata3c516d52011-08-18 03:53:18 +0200341static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200342open_one_pid(pid_t pid)
343{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200344 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100345 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200346 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
347
Petr Machata1974dbc2011-08-19 18:58:01 +0200348 /* Get the filename first. Should the trace_pid fail, we can
349 * easily free it, untracing is more work. */
350 if ((filename = pid2name(pid)) == NULL
351 || trace_pid(pid) < 0) {
352 free(filename);
353 return -1;
354 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100355
Petr Machata75934ad2012-04-14 02:28:03 +0200356 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200357 if (proc == NULL)
358 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200359 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200360
Petr Machata1974dbc2011-08-19 18:58:01 +0200361 return 0;
362}
363
Petr Machata2b46cfc2012-02-18 11:17:29 +0100364static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200365start_one_pid(Process * proc, void * data)
366{
367 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100368 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100369}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100370
Petr Machata9a5420c2011-07-09 11:21:23 +0200371void
372open_pid(pid_t pid)
373{
374 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200375 /* If we are already tracing this guy, we should be seeing all
376 * his children via normal tracing route. */
377 if (pid2proc(pid) != NULL)
378 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200379
Petr Machata3c516d52011-08-18 03:53:18 +0200380 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200381 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200382 fprintf(stderr, "Cannot attach to pid %u: %s\n",
383 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200384 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200385 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200386 }
387
Petr Machata3c516d52011-08-18 03:53:18 +0200388 /* Now attach to all tasks that belong to that PID. There's a
389 * race between process_tasks and open_one_pid. So when we
390 * fail in open_one_pid below, we just do another round.
391 * Chances are that by then that PID will have gone away, and
392 * that's why we have seen the failure. The processes that we
393 * manage to open_one_pid are stopped, so we should eventually
394 * reach a point where process_tasks doesn't give any new
395 * processes (because there's nobody left to produce
396 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200397 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200398 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200399 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200400 pid_t *tasks;
401 size_t ntasks;
402 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200403
Petr Machata3c516d52011-08-18 03:53:18 +0200404 if (process_tasks(pid, &tasks, &ntasks) < 0) {
405 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
406 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100407 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200408 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200409
Petr Machata3c516d52011-08-18 03:53:18 +0200410 have_all = 1;
411 for (i = 0; i < ntasks; ++i)
412 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200413 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200414 have_all = 0;
415
Petr Machata9a5420c2011-07-09 11:21:23 +0200416 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200417
Petr Machata1974dbc2011-08-19 18:58:01 +0200418 if (have_all && old_ntasks == ntasks)
419 break;
420 old_ntasks = ntasks;
421 }
422
Petr Machata93d95df2012-04-17 05:16:19 +0200423 struct Process *leader = pid2proc(pid)->leader;
424
425 /* XXX Is there a way to figure out whether _start has
426 * actually already been hit? */
427 arch_dynlink_done(leader);
428
Petr Machata2f9b78e2012-04-16 21:08:54 +0200429 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200430 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200431}
432
Petr Machata2b46cfc2012-02-18 11:17:29 +0100433static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200434find_proc(Process * proc, void * data)
435{
436 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100437 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200438}
439
Juan Cespedesa8909f72009-04-28 20:02:41 +0200440Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100441pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200442 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
443}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100444
Petr Machatacebb8842011-07-09 11:14:11 +0200445static Process * list_of_processes = NULL;
446
Petr Machatacbe29c62011-09-27 02:27:58 +0200447static void
448unlist_process(Process * proc)
449{
450 Process *tmp;
451
452 if (list_of_processes == proc) {
453 list_of_processes = list_of_processes->next;
454 return;
455 }
456
457 for (tmp = list_of_processes; ; tmp = tmp->next) {
458 /* If the following assert fails, the process wasn't
459 * in the list. */
460 assert(tmp->next != NULL);
461
462 if (tmp->next == proc) {
463 tmp->next = tmp->next->next;
464 return;
465 }
466 }
467}
468
Petr Machata2b46cfc2012-02-18 11:17:29 +0100469struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100470each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100471 enum callback_status(*cb)(struct Process *proc, void *data),
472 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200473{
Petr Machata74132a42012-03-16 02:46:18 +0100474 struct Process *it = start_after == NULL ? list_of_processes
475 : start_after->next;
476
477 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200478 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100479 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100480 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200481 case CBS_FAIL:
482 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100483 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200484 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100485 case CBS_CONT:
486 break;
487 }
Petr Machatacebb8842011-07-09 11:14:11 +0200488 it = next;
489 }
490 return NULL;
491}
Petr Machata9a5420c2011-07-09 11:21:23 +0200492
493Process *
Petr Machata74132a42012-03-16 02:46:18 +0100494each_task(struct Process *proc, 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 Machata9a5420c2011-07-09 11:21:23 +0200497{
Petr Machata74132a42012-03-16 02:46:18 +0100498 assert(proc != NULL);
499 struct Process *it = start_after == NULL ? proc->leader
500 : start_after->next;
501
Petr Machata9a5420c2011-07-09 11:21:23 +0200502 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100503 struct Process *leader = it->leader;
504 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200505 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100506 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100507 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200508 case CBS_FAIL:
509 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100510 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200511 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100512 case CBS_CONT:
513 break;
514 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200515 it = next;
516 }
517 }
518 return NULL;
519}
520
Petr Machata44965c72012-04-06 19:59:20 +0200521static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200522add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200523{
Petr Machata9a5420c2011-07-09 11:21:23 +0200524 Process ** leaderp = &list_of_processes;
525 if (proc->pid) {
526 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200527 if (tgid == 0)
528 /* Must have been terminated before we managed
529 * to fully attach. */
530 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200531 if (tgid == proc->pid)
532 proc->leader = proc;
533 else {
534 Process * leader = pid2proc(tgid);
535 proc->leader = leader;
536 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200537 leaderp = &leader->next;
538 }
539 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200540
541 if (!was_exec) {
542 proc->next = *leaderp;
543 *leaderp = proc;
544 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200545}
546
Petr Machatacbe29c62011-09-27 02:27:58 +0200547void
548change_process_leader(Process * proc, Process * leader)
549{
550 Process ** leaderp = &list_of_processes;
551 if (proc->leader == leader)
552 return;
553
554 assert(leader != NULL);
555 unlist_process(proc);
556 if (proc != leader)
557 leaderp = &leader->next;
558
559 proc->leader = leader;
560 proc->next = *leaderp;
561 *leaderp = proc;
562}
563
Petr Machata2b46cfc2012-02-18 11:17:29 +0100564static enum callback_status
565clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200566{
567 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
568 proc->pid, proc->leader->pid);
569 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100570 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200571}
572
Petr Machata69a03e62011-07-09 11:29:12 +0200573static enum ecb_status
574event_for_proc(Event * event, void * data)
575{
576 if (event->proc == data)
577 return ecb_deque;
578 else
579 return ecb_cont;
580}
581
582static void
583delete_events_for(Process * proc)
584{
585 Event * event;
586 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
587 free(event);
588}
589
Petr Machatacebb8842011-07-09 11:14:11 +0200590void
591remove_process(Process *proc)
592{
Petr Machatacebb8842011-07-09 11:14:11 +0200593 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
594
Petr Machata9a5420c2011-07-09 11:21:23 +0200595 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100596 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200597
Petr Machatacbe29c62011-09-27 02:27:58 +0200598 unlist_process(proc);
599 delete_events_for(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200600 process_destroy(proc);
601 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100602}
Petr Machata4007d742011-07-09 11:29:42 +0200603
604void
Petr Machata366c2f42012-02-09 19:34:36 +0100605install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200606{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200607 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200608 assert(proc->event_handler == NULL);
609 proc->event_handler = handler;
610}
611
612void
613destroy_event_handler(Process * proc)
614{
Petr Machata366c2f42012-02-09 19:34:36 +0100615 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200616 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200617 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200618 if (handler->destroy != NULL)
619 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200620 free(handler);
621 proc->event_handler = NULL;
622}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100623
624static enum callback_status
625breakpoint_for_symbol(struct library_symbol *libsym, void *data)
626{
627 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200628 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100629
Petr Machatad5e85562012-04-05 15:18:38 +0200630 /* If there is an artificial breakpoint on the same address,
631 * its libsym will be NULL, and we can smuggle our libsym
632 * there. That artificial breakpoint is there presumably for
633 * the callbacks, which we don't touch. If there is a real
634 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200635 * symbols and ignore extra symbol aliases.
636 *
637 * The other direction is more complicated and currently not
638 * supported. If a breakpoint has custom callbacks, it might
639 * be also custom-allocated, and we would really need to swap
640 * the two: delete the one now in the dictionary, swap values
641 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200642 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
643 libsym->enter_addr);
644 if (bp != NULL) {
645 assert(bp->libsym == NULL);
646 bp->libsym = libsym;
647 return CBS_CONT;
648 }
649
650 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200651 if (bp == NULL
652 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
653 fail:
654 free(bp);
655 return CBS_FAIL;
656 }
657 if (proc_add_breakpoint(proc, bp) < 0) {
658 breakpoint_destroy(bp);
659 goto fail;
660 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100661
Petr Machatafa0c5702012-04-13 18:43:40 +0200662 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200663 proc_remove_breakpoint(proc, bp);
664 breakpoint_destroy(bp);
665 goto fail;
666 }
667
Petr Machata2b46cfc2012-02-18 11:17:29 +0100668 return CBS_CONT;
669}
670
671void
672proc_add_library(struct Process *proc, struct library *lib)
673{
674 assert(lib->next == NULL);
675 lib->next = proc->libraries;
676 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200677 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
678 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100679
680 struct library_symbol *libsym = NULL;
681 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100682 proc)) != NULL)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200683 fprintf(stderr, "couldn't insert breakpoint for %s to %d: %s",
684 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100685}
686
687int
688proc_remove_library(struct Process *proc, struct library *lib)
689{
690 struct library **libp;
691 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
692 if (*libp == lib) {
693 *libp = lib->next;
694 return 0;
695 }
696 return -1;
697}
698
699struct library *
700proc_each_library(struct Process *proc, struct library *it,
701 enum callback_status (*cb)(struct Process *proc,
702 struct library *lib, void *data),
703 void *data)
704{
705 if (it == NULL)
706 it = proc->libraries;
707
708 while (it != NULL) {
709 struct library *next = it->next;
710
711 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200712 case CBS_FAIL:
713 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100714 case CBS_STOP:
715 return it;
716 case CBS_CONT:
717 break;
718 }
719
720 it = next;
721 }
722
723 return NULL;
724}
Petr Machata52dbfb12012-03-29 16:38:26 +0200725
Petr Machataf7fee432012-04-19 17:00:53 +0200726static void
727check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200728{
Petr Machata52dbfb12012-03-29 16:38:26 +0200729 /* Only the group leader should be getting the breakpoints and
730 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200731 assert(proc->leader != NULL);
732 assert(proc->leader == proc);
733 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200734}
Petr Machata52dbfb12012-03-29 16:38:26 +0200735
Petr Machataf7fee432012-04-19 17:00:53 +0200736int
737proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
738{
Petr Machatafa0c5702012-04-13 18:43:40 +0200739 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200740 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200741 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200742
Petr Machataa2416362012-04-06 02:43:34 +0200743 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200744 * assert, but that's not necessary right now. Read the
745 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200746 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200747
Petr Machatafa0c5702012-04-13 18:43:40 +0200748 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200749 fprintf(stderr,
750 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
751 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200752 return -1;
753 }
754
Petr Machata52dbfb12012-03-29 16:38:26 +0200755 return 0;
756}
757
Petr Machataf7fee432012-04-19 17:00:53 +0200758void
Petr Machata52dbfb12012-03-29 16:38:26 +0200759proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
760{
Petr Machataf7fee432012-04-19 17:00:53 +0200761 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
762 proc->pid, breakpoint_name(bp), bp->addr);
763 check_leader(proc);
764 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
765 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200766}
Petr Machatad3cc9882012-04-13 21:40:23 +0200767
768/* Dict doesn't support iteration restarts, so here's this contraption
769 * for now. XXX add restarts to dict. */
770struct each_breakpoint_data
771{
772 void *start;
773 void *end;
774 struct Process *proc;
775 enum callback_status (*cb)(struct Process *proc,
776 struct breakpoint *bp,
777 void *data);
778 void *cb_data;
779};
780
781static void
782each_breakpoint_cb(void *key, void *value, void *d)
783{
784 struct each_breakpoint_data *data = d;
785 if (data->end != NULL)
786 return;
787 if (data->start == key)
788 data->start = NULL;
789
790 if (data->start == NULL) {
791 switch (data->cb(data->proc, value, data->cb_data)) {
792 case CBS_FAIL:
793 /* XXX handle me */
794 case CBS_STOP:
795 data->end = key;
796 case CBS_CONT:
797 return;
798 }
799 }
800}
801
802void *
803proc_each_breakpoint(struct Process *proc, void *start,
804 enum callback_status (*cb)(struct Process *proc,
805 struct breakpoint *bp,
806 void *data), void *data)
807{
808 struct each_breakpoint_data dd = {
809 .start = start,
810 .proc = proc,
811 .cb = cb,
812 .cb_data = data,
813 };
814 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
815 return dd.end;
816}