blob: 54afbe06967ed8964a066f2c8d9075a209394bed [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 Machata18bd8ff2012-04-10 04:32:39 +0200107 target_address_t entry;
108 target_address_t interp_bias;
109 if (process_get_entry(proc, &entry, &interp_bias) < 0) {
110 fprintf(stderr, "Couldn't get entry points of process %d\n",
Petr Machata2b46cfc2012-02-18 11:17:29 +0100111 proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100112 return -1;
113 }
114
Petr Machata3d0c91c2012-04-14 02:37:38 +0200115 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200116 fprintf(stderr, "failed to init breakpoints %d\n",
117 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200118 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200119 }
120
Petr Machata2b46cfc2012-02-18 11:17:29 +0100121 return 0;
122}
123
Petr Machata3d0c91c2012-04-14 02:37:38 +0200124int
125process_init(struct Process *proc, const char *filename, pid_t pid)
126{
127 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200128 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200129 fprintf(stderr, "failed to initialize process %d: %s\n",
130 pid, strerror(errno));
Petr Machata3d0c91c2012-04-14 02:37:38 +0200131 return -1;
132 }
133
Petr Machata744f2552012-04-15 04:33:18 +0200134 if (arch_process_init(proc) < 0) {
135 process_bare_destroy(proc, 0);
136 goto fail;
137 }
138
Petr Machata218c5ff2012-04-15 04:22:39 +0200139 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200140 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200141 if (process_init_main(proc) < 0) {
142 process_bare_destroy(proc, 0);
143 goto fail;
144 }
145 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200146}
147
Petr Machata8ead1cd2012-04-24 18:13:09 +0200148static enum callback_status
149destroy_breakpoint_cb(struct Process *proc, struct breakpoint *bp, void *data)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200150{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200151 breakpoint_destroy(bp);
152 free(bp);
Petr Machata8ead1cd2012-04-24 18:13:09 +0200153 return CBS_CONT;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200154}
155
156static void
157private_process_destroy(struct Process *proc, int keep_filename)
158{
159 if (!keep_filename)
160 free(proc->filename);
161
Petr Machata8ead1cd2012-04-24 18:13:09 +0200162 /* Libraries and symbols. This is only relevant in
163 * leader. */
Petr Machata3d0c91c2012-04-14 02:37:38 +0200164 struct library *lib;
165 for (lib = proc->libraries; lib != NULL; ) {
166 struct library *next = lib->next;
167 library_destroy(lib);
168 free(lib);
169 lib = next;
170 }
171 proc->libraries = NULL;
172
173 /* Breakpoints. */
Petr Machata8ead1cd2012-04-24 18:13:09 +0200174 if (proc->breakpoints != NULL) {
175 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
176 dict_clear(proc->breakpoints);
177 proc->breakpoints = NULL;
178 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200179}
180
181void
182process_destroy(struct Process *proc)
183{
184 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200185 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200186}
187
188int
189process_exec(struct Process *proc)
190{
Petr Machata744f2552012-04-15 04:33:18 +0200191 /* Call exec first, before we destroy the main state. */
192 if (arch_process_exec(proc) < 0)
193 return -1;
194
Petr Machata3d0c91c2012-04-14 02:37:38 +0200195 private_process_destroy(proc, 1);
196 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
197 return -1;
198 if (process_init_main(proc) < 0) {
199 process_bare_destroy(proc, 1);
200 return -1;
201 }
202 return 0;
203}
204
Petr Machata2b46cfc2012-02-18 11:17:29 +0100205struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200206open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100207{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100208 assert(pid != 0);
209 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200210 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200211 free(proc);
212 return NULL;
213 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100214 return proc;
215}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100216
Petr Machata2b46cfc2012-02-18 11:17:29 +0100217struct clone_single_bp_data {
218 struct Process *old_proc;
219 struct Process *new_proc;
220 int error;
221};
222
Petr Machata2b46cfc2012-02-18 11:17:29 +0100223static void
224clone_single_bp(void *key, void *value, void *u)
225{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100226 struct breakpoint *bp = value;
227 struct clone_single_bp_data *data = u;
228
Petr Machatad3cc9882012-04-13 21:40:23 +0200229 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100230 struct breakpoint *clone = malloc(sizeof(*clone));
231 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200232 || breakpoint_clone(clone, data->new_proc,
233 bp, data->old_proc) < 0) {
234 fail:
235 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100236 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100237 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200238 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
239 breakpoint_destroy(clone);
240 goto fail;
241 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100242}
243
244int
245process_clone(struct Process *retp, struct Process *proc, pid_t pid)
246{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200247 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100248 fail:
Petr Machatacc0e1e42012-04-25 13:42:07 +0200249 fprintf(stderr, "failed to clone process %d->%d : %s\n",
250 proc->pid, pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251 return -1;
252 }
253
Petr Machatacf1679a2012-04-06 19:56:17 +0200254 retp->tracesysgood = proc->tracesysgood;
Petr Machata2cb124c2012-04-19 18:44:45 +0200255 retp->e_machine = proc->e_machine;
Petr Machatacf1679a2012-04-06 19:56:17 +0200256
Petr Machata2b46cfc2012-02-18 11:17:29 +0100257 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200258 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100259 return 0;
260
261 /* Clone symbols first so that we can clone and relink
262 * breakpoints. */
263 struct library *lib;
264 struct library **nlibp = &retp->libraries;
265 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
266 *nlibp = malloc(sizeof(**nlibp));
267 if (*nlibp == NULL
268 || library_clone(*nlibp, lib) < 0) {
269 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200270 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100271
272 /* Error when cloning. Unroll what was done. */
273 for (lib = retp->libraries; lib != NULL; ) {
274 struct library *next = lib->next;
275 library_destroy(lib);
276 free(lib);
277 lib = next;
278 }
279 goto fail;
280 }
281
282 nlibp = &(*nlibp)->next;
283 }
284
285 /* Now clone breakpoints. Symbol relinking is done in
286 * clone_single_bp. */
287 struct clone_single_bp_data data = {
288 .old_proc = proc,
289 .new_proc = retp,
290 .error = 0,
291 };
292 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
293
Petr Machataded6f972012-04-13 23:15:48 +0200294 /* And finally the call stack. */
295 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
296 retp->callstack_depth = proc->callstack_depth;
297
Petr Machata2b46cfc2012-02-18 11:17:29 +0100298 if (data.error < 0)
299 goto fail2;
300
Petr Machata744f2552012-04-15 04:33:18 +0200301 if (arch_process_clone(retp, proc) < 0)
302 goto fail2;
303
Petr Machata2b46cfc2012-02-18 11:17:29 +0100304 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100305}
306
Petr Machata3c516d52011-08-18 03:53:18 +0200307static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200308open_one_pid(pid_t pid)
309{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200310 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100311 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200312 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
313
Petr Machata1974dbc2011-08-19 18:58:01 +0200314 /* Get the filename first. Should the trace_pid fail, we can
315 * easily free it, untracing is more work. */
316 if ((filename = pid2name(pid)) == NULL
317 || trace_pid(pid) < 0) {
318 free(filename);
319 return -1;
320 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100321
Petr Machata75934ad2012-04-14 02:28:03 +0200322 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200323 if (proc == NULL)
324 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200325 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200326
Petr Machata1974dbc2011-08-19 18:58:01 +0200327 return 0;
328}
329
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200331start_one_pid(Process * proc, void * data)
332{
333 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100334 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100335}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100336
Petr Machata9a5420c2011-07-09 11:21:23 +0200337void
338open_pid(pid_t pid)
339{
340 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200341 /* If we are already tracing this guy, we should be seeing all
342 * his children via normal tracing route. */
343 if (pid2proc(pid) != NULL)
344 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200345
Petr Machata3c516d52011-08-18 03:53:18 +0200346 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200347 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200348 fprintf(stderr, "Cannot attach to pid %u: %s\n",
349 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200350 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200351 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200352 }
353
Petr Machata3c516d52011-08-18 03:53:18 +0200354 /* Now attach to all tasks that belong to that PID. There's a
355 * race between process_tasks and open_one_pid. So when we
356 * fail in open_one_pid below, we just do another round.
357 * Chances are that by then that PID will have gone away, and
358 * that's why we have seen the failure. The processes that we
359 * manage to open_one_pid are stopped, so we should eventually
360 * reach a point where process_tasks doesn't give any new
361 * processes (because there's nobody left to produce
362 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200363 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200364 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200365 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200366 pid_t *tasks;
367 size_t ntasks;
368 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200369
Petr Machata3c516d52011-08-18 03:53:18 +0200370 if (process_tasks(pid, &tasks, &ntasks) < 0) {
371 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
372 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100373 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200374 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200375
Petr Machata3c516d52011-08-18 03:53:18 +0200376 have_all = 1;
377 for (i = 0; i < ntasks; ++i)
378 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200379 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200380 have_all = 0;
381
Petr Machata9a5420c2011-07-09 11:21:23 +0200382 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200383
Petr Machata1974dbc2011-08-19 18:58:01 +0200384 if (have_all && old_ntasks == ntasks)
385 break;
386 old_ntasks = ntasks;
387 }
388
Petr Machata93d95df2012-04-17 05:16:19 +0200389 struct Process *leader = pid2proc(pid)->leader;
390
391 /* XXX Is there a way to figure out whether _start has
392 * actually already been hit? */
393 arch_dynlink_done(leader);
394
Petr Machata2f9b78e2012-04-16 21:08:54 +0200395 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200396 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200397}
398
Petr Machata2b46cfc2012-02-18 11:17:29 +0100399static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200400find_proc(Process * proc, void * data)
401{
402 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100403 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200404}
405
Juan Cespedesa8909f72009-04-28 20:02:41 +0200406Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100407pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200408 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
409}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100410
Petr Machatacebb8842011-07-09 11:14:11 +0200411static Process * list_of_processes = NULL;
412
Petr Machatacbe29c62011-09-27 02:27:58 +0200413static void
414unlist_process(Process * proc)
415{
416 Process *tmp;
417
418 if (list_of_processes == proc) {
419 list_of_processes = list_of_processes->next;
420 return;
421 }
422
423 for (tmp = list_of_processes; ; tmp = tmp->next) {
424 /* If the following assert fails, the process wasn't
425 * in the list. */
426 assert(tmp->next != NULL);
427
428 if (tmp->next == proc) {
429 tmp->next = tmp->next->next;
430 return;
431 }
432 }
433}
434
Petr Machata2b46cfc2012-02-18 11:17:29 +0100435struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100436each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100437 enum callback_status(*cb)(struct Process *proc, void *data),
438 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200439{
Petr Machata74132a42012-03-16 02:46:18 +0100440 struct Process *it = start_after == NULL ? list_of_processes
441 : start_after->next;
442
443 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200444 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100445 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100446 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200447 case CBS_FAIL:
448 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100449 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200450 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100451 case CBS_CONT:
452 break;
453 }
Petr Machatacebb8842011-07-09 11:14:11 +0200454 it = next;
455 }
456 return NULL;
457}
Petr Machata9a5420c2011-07-09 11:21:23 +0200458
459Process *
Petr Machata74132a42012-03-16 02:46:18 +0100460each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100461 enum callback_status(*cb)(struct Process *proc, void *data),
462 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200463{
Petr Machata74132a42012-03-16 02:46:18 +0100464 assert(proc != NULL);
465 struct Process *it = start_after == NULL ? proc->leader
466 : start_after->next;
467
Petr Machata9a5420c2011-07-09 11:21:23 +0200468 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100469 struct Process *leader = it->leader;
470 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200471 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100472 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100473 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200474 case CBS_FAIL:
475 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100476 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200477 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100478 case CBS_CONT:
479 break;
480 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200481 it = next;
482 }
483 }
484 return NULL;
485}
486
Petr Machata44965c72012-04-06 19:59:20 +0200487static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200488add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200489{
Petr Machata9a5420c2011-07-09 11:21:23 +0200490 Process ** leaderp = &list_of_processes;
491 if (proc->pid) {
492 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200493 if (tgid == 0)
494 /* Must have been terminated before we managed
495 * to fully attach. */
496 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200497 if (tgid == proc->pid)
498 proc->leader = proc;
499 else {
500 Process * leader = pid2proc(tgid);
501 proc->leader = leader;
502 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200503 leaderp = &leader->next;
504 }
505 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200506
507 if (!was_exec) {
508 proc->next = *leaderp;
509 *leaderp = proc;
510 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200511}
512
Petr Machatacbe29c62011-09-27 02:27:58 +0200513void
514change_process_leader(Process * proc, Process * leader)
515{
516 Process ** leaderp = &list_of_processes;
517 if (proc->leader == leader)
518 return;
519
520 assert(leader != NULL);
521 unlist_process(proc);
522 if (proc != leader)
523 leaderp = &leader->next;
524
525 proc->leader = leader;
526 proc->next = *leaderp;
527 *leaderp = proc;
528}
529
Petr Machata2b46cfc2012-02-18 11:17:29 +0100530static enum callback_status
531clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200532{
533 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
534 proc->pid, proc->leader->pid);
535 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100536 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200537}
538
Petr Machata69a03e62011-07-09 11:29:12 +0200539static enum ecb_status
540event_for_proc(Event * event, void * data)
541{
542 if (event->proc == data)
543 return ecb_deque;
544 else
545 return ecb_cont;
546}
547
548static void
549delete_events_for(Process * proc)
550{
551 Event * event;
552 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
553 free(event);
554}
555
Petr Machatacebb8842011-07-09 11:14:11 +0200556void
557remove_process(Process *proc)
558{
Petr Machatacebb8842011-07-09 11:14:11 +0200559 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
560
Petr Machata9a5420c2011-07-09 11:21:23 +0200561 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100562 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200563
Petr Machatacbe29c62011-09-27 02:27:58 +0200564 unlist_process(proc);
565 delete_events_for(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200566 process_destroy(proc);
567 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100568}
Petr Machata4007d742011-07-09 11:29:42 +0200569
570void
Petr Machata366c2f42012-02-09 19:34:36 +0100571install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200572{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200573 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200574 assert(proc->event_handler == NULL);
575 proc->event_handler = handler;
576}
577
578void
579destroy_event_handler(Process * proc)
580{
Petr Machata366c2f42012-02-09 19:34:36 +0100581 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200582 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200583 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200584 if (handler->destroy != NULL)
585 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200586 free(handler);
587 proc->event_handler = NULL;
588}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100589
590static enum callback_status
591breakpoint_for_symbol(struct library_symbol *libsym, void *data)
592{
593 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200594 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100595
Petr Machatad5e85562012-04-05 15:18:38 +0200596 /* If there is an artificial breakpoint on the same address,
597 * its libsym will be NULL, and we can smuggle our libsym
598 * there. That artificial breakpoint is there presumably for
599 * the callbacks, which we don't touch. If there is a real
600 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200601 * symbols and ignore extra symbol aliases.
602 *
603 * The other direction is more complicated and currently not
604 * supported. If a breakpoint has custom callbacks, it might
605 * be also custom-allocated, and we would really need to swap
606 * the two: delete the one now in the dictionary, swap values
607 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200608 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
609 libsym->enter_addr);
610 if (bp != NULL) {
611 assert(bp->libsym == NULL);
612 bp->libsym = libsym;
613 return CBS_CONT;
614 }
615
616 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200617 if (bp == NULL
618 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
619 fail:
620 free(bp);
621 return CBS_FAIL;
622 }
623 if (proc_add_breakpoint(proc, bp) < 0) {
624 breakpoint_destroy(bp);
625 goto fail;
626 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100627
Petr Machatafa0c5702012-04-13 18:43:40 +0200628 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200629 proc_remove_breakpoint(proc, bp);
630 breakpoint_destroy(bp);
631 goto fail;
632 }
633
Petr Machata2b46cfc2012-02-18 11:17:29 +0100634 return CBS_CONT;
635}
636
637void
638proc_add_library(struct Process *proc, struct library *lib)
639{
640 assert(lib->next == NULL);
641 lib->next = proc->libraries;
642 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200643 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
644 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100645
646 struct library_symbol *libsym = NULL;
647 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100648 proc)) != NULL)
Petr Machatacc0e1e42012-04-25 13:42:07 +0200649 fprintf(stderr, "couldn't insert breakpoint for %s to %d: %s",
650 libsym->name, proc->pid, strerror(errno));
Petr Machata2b46cfc2012-02-18 11:17:29 +0100651}
652
653int
654proc_remove_library(struct Process *proc, struct library *lib)
655{
656 struct library **libp;
657 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
658 if (*libp == lib) {
659 *libp = lib->next;
660 return 0;
661 }
662 return -1;
663}
664
665struct library *
666proc_each_library(struct Process *proc, struct library *it,
667 enum callback_status (*cb)(struct Process *proc,
668 struct library *lib, void *data),
669 void *data)
670{
671 if (it == NULL)
672 it = proc->libraries;
673
674 while (it != NULL) {
675 struct library *next = it->next;
676
677 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200678 case CBS_FAIL:
679 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100680 case CBS_STOP:
681 return it;
682 case CBS_CONT:
683 break;
684 }
685
686 it = next;
687 }
688
689 return NULL;
690}
Petr Machata52dbfb12012-03-29 16:38:26 +0200691
Petr Machataf7fee432012-04-19 17:00:53 +0200692static void
693check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200694{
Petr Machata52dbfb12012-03-29 16:38:26 +0200695 /* Only the group leader should be getting the breakpoints and
696 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200697 assert(proc->leader != NULL);
698 assert(proc->leader == proc);
699 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200700}
Petr Machata52dbfb12012-03-29 16:38:26 +0200701
Petr Machataf7fee432012-04-19 17:00:53 +0200702int
703proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
704{
Petr Machatafa0c5702012-04-13 18:43:40 +0200705 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200706 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200707 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200708
Petr Machataa2416362012-04-06 02:43:34 +0200709 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200710 * assert, but that's not necessary right now. Read the
711 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200712 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200713
Petr Machatafa0c5702012-04-13 18:43:40 +0200714 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machatacc0e1e42012-04-25 13:42:07 +0200715 fprintf(stderr,
716 "couldn't enter breakpoint %s@%p to dictionary: %s\n",
717 breakpoint_name(bp), bp->addr, strerror(errno));
Petr Machata52dbfb12012-03-29 16:38:26 +0200718 return -1;
719 }
720
Petr Machata52dbfb12012-03-29 16:38:26 +0200721 return 0;
722}
723
Petr Machataf7fee432012-04-19 17:00:53 +0200724void
Petr Machata52dbfb12012-03-29 16:38:26 +0200725proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
726{
Petr Machataf7fee432012-04-19 17:00:53 +0200727 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
728 proc->pid, breakpoint_name(bp), bp->addr);
729 check_leader(proc);
730 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
731 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200732}
Petr Machatad3cc9882012-04-13 21:40:23 +0200733
734/* Dict doesn't support iteration restarts, so here's this contraption
735 * for now. XXX add restarts to dict. */
736struct each_breakpoint_data
737{
738 void *start;
739 void *end;
740 struct Process *proc;
741 enum callback_status (*cb)(struct Process *proc,
742 struct breakpoint *bp,
743 void *data);
744 void *cb_data;
745};
746
747static void
748each_breakpoint_cb(void *key, void *value, void *d)
749{
750 struct each_breakpoint_data *data = d;
751 if (data->end != NULL)
752 return;
753 if (data->start == key)
754 data->start = NULL;
755
756 if (data->start == NULL) {
757 switch (data->cb(data->proc, value, data->cb_data)) {
758 case CBS_FAIL:
759 /* XXX handle me */
760 case CBS_STOP:
761 data->end = key;
762 case CBS_CONT:
763 return;
764 }
765 }
766}
767
768void *
769proc_each_breakpoint(struct Process *proc, void *start,
770 enum callback_status (*cb)(struct Process *proc,
771 struct breakpoint *bp,
772 void *data), void *data)
773{
774 struct each_breakpoint_data dd = {
775 .start = start,
776 .proc = proc,
777 .cb = cb,
778 .cb_data = data,
779 };
780 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
781 return dd.end;
782}