blob: fb026381eaf8b3ab5f539ffaf4e55815ee14f129 [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>
Petr Machata1974dbc2011-08-19 18:58:01 +020014#include <error.h>
Juan Cespedes273ea6d1998-03-14 23:02:40 +010015
Juan Cespedesf7281232009-06-25 16:11:21 +020016#include "common.h"
Petr Machata9294d822012-02-07 12:35:58 +010017#include "breakpoint.h"
Petr Machata366c2f42012-02-09 19:34:36 +010018#include "proc.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010019
Petr Machata744f2552012-04-15 04:33:18 +020020#ifndef ARCH_HAVE_PROCESS_DATA
21int
22arch_process_init(struct Process *proc)
23{
24 return 0;
25}
26
27void
28arch_process_destroy(struct Process *proc)
29{
30}
31
32int
33arch_process_clone(struct Process *retp, struct Process *proc)
34{
35 return 0;
36}
37
38int
39arch_process_exec(struct Process *proc)
40{
41 return 0;
42}
43#endif
44
Petr Machata93d95df2012-04-17 05:16:19 +020045#ifndef ARCH_HAVE_DYNLINK_DONE
46void
47arch_dynlink_done(struct Process *proc)
48{
49}
50#endif
51
Petr Machata3d0c91c2012-04-14 02:37:38 +020052static void add_process(struct Process *proc, int was_exec);
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);
100 remove_process(proc);
101 }
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 Machata3d0c91c2012-04-14 02:37:38 +0200129 error(0, errno, "init process %d", pid);
130 return -1;
131 }
132
Petr Machata744f2552012-04-15 04:33:18 +0200133 if (arch_process_init(proc) < 0) {
134 process_bare_destroy(proc, 0);
135 goto fail;
136 }
137
Petr Machata218c5ff2012-04-15 04:22:39 +0200138 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200139 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200140 if (process_init_main(proc) < 0) {
141 process_bare_destroy(proc, 0);
142 goto fail;
143 }
144 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200145}
146
147static void
148destroy_breakpoint_cb(void *key, void *value, void *data)
149{
150 struct breakpoint *bp = value;
151 breakpoint_destroy(bp);
152 free(bp);
153}
154
155static void
156private_process_destroy(struct Process *proc, int keep_filename)
157{
158 if (!keep_filename)
159 free(proc->filename);
160
161 /* Libraries and symbols. */
162 struct library *lib;
163 for (lib = proc->libraries; lib != NULL; ) {
164 struct library *next = lib->next;
165 library_destroy(lib);
166 free(lib);
167 lib = next;
168 }
169 proc->libraries = NULL;
170
171 /* Breakpoints. */
172 dict_apply_to_all(proc->breakpoints, destroy_breakpoint_cb, NULL);
173 dict_clear(proc->breakpoints);
174 proc->breakpoints = NULL;
175}
176
177void
178process_destroy(struct Process *proc)
179{
180 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200181 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200182}
183
184int
185process_exec(struct Process *proc)
186{
Petr Machata744f2552012-04-15 04:33:18 +0200187 /* Call exec first, before we destroy the main state. */
188 if (arch_process_exec(proc) < 0)
189 return -1;
190
Petr Machata3d0c91c2012-04-14 02:37:38 +0200191 private_process_destroy(proc, 1);
192 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
193 return -1;
194 if (process_init_main(proc) < 0) {
195 process_bare_destroy(proc, 1);
196 return -1;
197 }
198 return 0;
199}
200
Petr Machata2b46cfc2012-02-18 11:17:29 +0100201struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200202open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100203{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100204 assert(pid != 0);
205 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200206 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200207 free(proc);
208 return NULL;
209 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100210 return proc;
211}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100212
Petr Machata2b46cfc2012-02-18 11:17:29 +0100213struct clone_single_bp_data {
214 struct Process *old_proc;
215 struct Process *new_proc;
216 int error;
217};
218
Petr Machata2b46cfc2012-02-18 11:17:29 +0100219static void
220clone_single_bp(void *key, void *value, void *u)
221{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222 struct breakpoint *bp = value;
223 struct clone_single_bp_data *data = u;
224
Petr Machatad3cc9882012-04-13 21:40:23 +0200225 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100226 struct breakpoint *clone = malloc(sizeof(*clone));
227 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200228 || breakpoint_clone(clone, data->new_proc,
229 bp, data->old_proc) < 0) {
230 fail:
231 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100232 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100233 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200234 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
235 breakpoint_destroy(clone);
236 goto fail;
237 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100238}
239
240int
241process_clone(struct Process *retp, struct Process *proc, pid_t pid)
242{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200243 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100244 fail:
245 error(0, errno, "clone process %d->%d", proc->pid, pid);
246 return -1;
247 }
248
Petr Machatacf1679a2012-04-06 19:56:17 +0200249 retp->tracesysgood = proc->tracesysgood;
250
Petr Machata2b46cfc2012-02-18 11:17:29 +0100251 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200252 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100253 return 0;
254
255 /* Clone symbols first so that we can clone and relink
256 * breakpoints. */
257 struct library *lib;
258 struct library **nlibp = &retp->libraries;
259 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
260 *nlibp = malloc(sizeof(**nlibp));
261 if (*nlibp == NULL
262 || library_clone(*nlibp, lib) < 0) {
263 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200264 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100265
266 /* Error when cloning. Unroll what was done. */
267 for (lib = retp->libraries; lib != NULL; ) {
268 struct library *next = lib->next;
269 library_destroy(lib);
270 free(lib);
271 lib = next;
272 }
273 goto fail;
274 }
275
276 nlibp = &(*nlibp)->next;
277 }
278
279 /* Now clone breakpoints. Symbol relinking is done in
280 * clone_single_bp. */
281 struct clone_single_bp_data data = {
282 .old_proc = proc,
283 .new_proc = retp,
284 .error = 0,
285 };
286 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
287
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 Machata2b46cfc2012-02-18 11:17:29 +0100292 if (data.error < 0)
293 goto fail2;
294
Petr Machata744f2552012-04-15 04:33:18 +0200295 if (arch_process_clone(retp, proc) < 0)
296 goto fail2;
297
Petr Machata2b46cfc2012-02-18 11:17:29 +0100298 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100299}
300
Petr Machata3c516d52011-08-18 03:53:18 +0200301static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200302open_one_pid(pid_t pid)
303{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200304 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100305 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200306 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
307
Petr Machata1974dbc2011-08-19 18:58:01 +0200308 /* Get the filename first. Should the trace_pid fail, we can
309 * easily free it, untracing is more work. */
310 if ((filename = pid2name(pid)) == NULL
311 || trace_pid(pid) < 0) {
312 free(filename);
313 return -1;
314 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100315
Petr Machata75934ad2012-04-14 02:28:03 +0200316 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200317 if (proc == NULL)
318 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200319 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200320
Petr Machata1974dbc2011-08-19 18:58:01 +0200321 return 0;
322}
323
Petr Machata2b46cfc2012-02-18 11:17:29 +0100324static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200325start_one_pid(Process * proc, void * data)
326{
327 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100328 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100329}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100330
Petr Machata9a5420c2011-07-09 11:21:23 +0200331void
332open_pid(pid_t pid)
333{
334 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200335 /* If we are already tracing this guy, we should be seeing all
336 * his children via normal tracing route. */
337 if (pid2proc(pid) != NULL)
338 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200339
Petr Machata3c516d52011-08-18 03:53:18 +0200340 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200341 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200342 fprintf(stderr, "Cannot attach to pid %u: %s\n",
343 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200344 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200345 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200346 }
347
Petr Machata3c516d52011-08-18 03:53:18 +0200348 /* Now attach to all tasks that belong to that PID. There's a
349 * race between process_tasks and open_one_pid. So when we
350 * fail in open_one_pid below, we just do another round.
351 * Chances are that by then that PID will have gone away, and
352 * that's why we have seen the failure. The processes that we
353 * manage to open_one_pid are stopped, so we should eventually
354 * reach a point where process_tasks doesn't give any new
355 * processes (because there's nobody left to produce
356 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200357 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200358 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200359 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200360 pid_t *tasks;
361 size_t ntasks;
362 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200363
Petr Machata3c516d52011-08-18 03:53:18 +0200364 if (process_tasks(pid, &tasks, &ntasks) < 0) {
365 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
366 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100367 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200368 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200369
Petr Machata3c516d52011-08-18 03:53:18 +0200370 have_all = 1;
371 for (i = 0; i < ntasks; ++i)
372 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200373 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200374 have_all = 0;
375
Petr Machata9a5420c2011-07-09 11:21:23 +0200376 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200377
Petr Machata1974dbc2011-08-19 18:58:01 +0200378 if (have_all && old_ntasks == ntasks)
379 break;
380 old_ntasks = ntasks;
381 }
382
Petr Machata93d95df2012-04-17 05:16:19 +0200383 struct Process *leader = pid2proc(pid)->leader;
384
385 /* XXX Is there a way to figure out whether _start has
386 * actually already been hit? */
387 arch_dynlink_done(leader);
388
Petr Machata2f9b78e2012-04-16 21:08:54 +0200389 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200390 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200391}
392
Petr Machata2b46cfc2012-02-18 11:17:29 +0100393static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200394find_proc(Process * proc, void * data)
395{
396 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100397 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200398}
399
Juan Cespedesa8909f72009-04-28 20:02:41 +0200400Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100401pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200402 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
403}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100404
Petr Machatacebb8842011-07-09 11:14:11 +0200405static Process * list_of_processes = NULL;
406
Petr Machatacbe29c62011-09-27 02:27:58 +0200407static void
408unlist_process(Process * proc)
409{
410 Process *tmp;
411
412 if (list_of_processes == proc) {
413 list_of_processes = list_of_processes->next;
414 return;
415 }
416
417 for (tmp = list_of_processes; ; tmp = tmp->next) {
418 /* If the following assert fails, the process wasn't
419 * in the list. */
420 assert(tmp->next != NULL);
421
422 if (tmp->next == proc) {
423 tmp->next = tmp->next->next;
424 return;
425 }
426 }
427}
428
Petr Machata2b46cfc2012-02-18 11:17:29 +0100429struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100430each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100431 enum callback_status(*cb)(struct Process *proc, void *data),
432 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200433{
Petr Machata74132a42012-03-16 02:46:18 +0100434 struct Process *it = start_after == NULL ? list_of_processes
435 : start_after->next;
436
437 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200438 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100439 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100440 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200441 case CBS_FAIL:
442 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100443 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200444 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100445 case CBS_CONT:
446 break;
447 }
Petr Machatacebb8842011-07-09 11:14:11 +0200448 it = next;
449 }
450 return NULL;
451}
Petr Machata9a5420c2011-07-09 11:21:23 +0200452
453Process *
Petr Machata74132a42012-03-16 02:46:18 +0100454each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100455 enum callback_status(*cb)(struct Process *proc, void *data),
456 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200457{
Petr Machata74132a42012-03-16 02:46:18 +0100458 assert(proc != NULL);
459 struct Process *it = start_after == NULL ? proc->leader
460 : start_after->next;
461
Petr Machata9a5420c2011-07-09 11:21:23 +0200462 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100463 struct Process *leader = it->leader;
464 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200465 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100466 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100467 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200468 case CBS_FAIL:
469 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100470 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200471 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100472 case CBS_CONT:
473 break;
474 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200475 it = next;
476 }
477 }
478 return NULL;
479}
480
Petr Machata44965c72012-04-06 19:59:20 +0200481static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200482add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200483{
Petr Machata9a5420c2011-07-09 11:21:23 +0200484 Process ** leaderp = &list_of_processes;
485 if (proc->pid) {
486 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200487 if (tgid == 0)
488 /* Must have been terminated before we managed
489 * to fully attach. */
490 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200491 if (tgid == proc->pid)
492 proc->leader = proc;
493 else {
494 Process * leader = pid2proc(tgid);
495 proc->leader = leader;
496 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200497 leaderp = &leader->next;
498 }
499 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200500
501 if (!was_exec) {
502 proc->next = *leaderp;
503 *leaderp = proc;
504 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200505}
506
Petr Machatacbe29c62011-09-27 02:27:58 +0200507void
508change_process_leader(Process * proc, Process * leader)
509{
510 Process ** leaderp = &list_of_processes;
511 if (proc->leader == leader)
512 return;
513
514 assert(leader != NULL);
515 unlist_process(proc);
516 if (proc != leader)
517 leaderp = &leader->next;
518
519 proc->leader = leader;
520 proc->next = *leaderp;
521 *leaderp = proc;
522}
523
Petr Machata2b46cfc2012-02-18 11:17:29 +0100524static enum callback_status
525clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200526{
527 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
528 proc->pid, proc->leader->pid);
529 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100530 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200531}
532
Petr Machata69a03e62011-07-09 11:29:12 +0200533static enum ecb_status
534event_for_proc(Event * event, void * data)
535{
536 if (event->proc == data)
537 return ecb_deque;
538 else
539 return ecb_cont;
540}
541
542static void
543delete_events_for(Process * proc)
544{
545 Event * event;
546 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
547 free(event);
548}
549
Petr Machatacebb8842011-07-09 11:14:11 +0200550void
551remove_process(Process *proc)
552{
Petr Machatacebb8842011-07-09 11:14:11 +0200553 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
554
Petr Machata9a5420c2011-07-09 11:21:23 +0200555 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100556 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200557
Petr Machatacbe29c62011-09-27 02:27:58 +0200558 unlist_process(proc);
559 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100560}
Petr Machata4007d742011-07-09 11:29:42 +0200561
562void
Petr Machata366c2f42012-02-09 19:34:36 +0100563install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200564{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200565 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200566 assert(proc->event_handler == NULL);
567 proc->event_handler = handler;
568}
569
570void
571destroy_event_handler(Process * proc)
572{
Petr Machata366c2f42012-02-09 19:34:36 +0100573 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200574 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200575 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200576 if (handler->destroy != NULL)
577 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200578 free(handler);
579 proc->event_handler = NULL;
580}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100581
582static enum callback_status
583breakpoint_for_symbol(struct library_symbol *libsym, void *data)
584{
585 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200586 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100587
Petr Machatad5e85562012-04-05 15:18:38 +0200588 /* If there is an artificial breakpoint on the same address,
589 * its libsym will be NULL, and we can smuggle our libsym
590 * there. That artificial breakpoint is there presumably for
591 * the callbacks, which we don't touch. If there is a real
592 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200593 * symbols and ignore extra symbol aliases.
594 *
595 * The other direction is more complicated and currently not
596 * supported. If a breakpoint has custom callbacks, it might
597 * be also custom-allocated, and we would really need to swap
598 * the two: delete the one now in the dictionary, swap values
599 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200600 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
601 libsym->enter_addr);
602 if (bp != NULL) {
603 assert(bp->libsym == NULL);
604 bp->libsym = libsym;
605 return CBS_CONT;
606 }
607
608 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200609 if (bp == NULL
610 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
611 fail:
612 free(bp);
613 return CBS_FAIL;
614 }
615 if (proc_add_breakpoint(proc, bp) < 0) {
616 breakpoint_destroy(bp);
617 goto fail;
618 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100619
Petr Machatafa0c5702012-04-13 18:43:40 +0200620 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200621 proc_remove_breakpoint(proc, bp);
622 breakpoint_destroy(bp);
623 goto fail;
624 }
625
Petr Machata2b46cfc2012-02-18 11:17:29 +0100626 return CBS_CONT;
627}
628
629void
630proc_add_library(struct Process *proc, struct library *lib)
631{
632 assert(lib->next == NULL);
633 lib->next = proc->libraries;
634 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200635 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
636 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100637
638 struct library_symbol *libsym = NULL;
639 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100640 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100641 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100642}
643
644int
645proc_remove_library(struct Process *proc, struct library *lib)
646{
647 struct library **libp;
648 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
649 if (*libp == lib) {
650 *libp = lib->next;
651 return 0;
652 }
653 return -1;
654}
655
656struct library *
657proc_each_library(struct Process *proc, struct library *it,
658 enum callback_status (*cb)(struct Process *proc,
659 struct library *lib, void *data),
660 void *data)
661{
662 if (it == NULL)
663 it = proc->libraries;
664
665 while (it != NULL) {
666 struct library *next = it->next;
667
668 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200669 case CBS_FAIL:
670 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100671 case CBS_STOP:
672 return it;
673 case CBS_CONT:
674 break;
675 }
676
677 it = next;
678 }
679
680 return NULL;
681}
Petr Machata52dbfb12012-03-29 16:38:26 +0200682
Petr Machataf7fee432012-04-19 17:00:53 +0200683static void
684check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200685{
Petr Machata52dbfb12012-03-29 16:38:26 +0200686 /* Only the group leader should be getting the breakpoints and
687 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200688 assert(proc->leader != NULL);
689 assert(proc->leader == proc);
690 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200691}
Petr Machata52dbfb12012-03-29 16:38:26 +0200692
Petr Machataf7fee432012-04-19 17:00:53 +0200693int
694proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
695{
Petr Machatafa0c5702012-04-13 18:43:40 +0200696 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200697 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200698 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200699
Petr Machataa2416362012-04-06 02:43:34 +0200700 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200701 * assert, but that's not necessary right now. Read the
702 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200703 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200704
Petr Machatafa0c5702012-04-13 18:43:40 +0200705 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200706 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
707 breakpoint_name(bp), bp->addr);
708 return -1;
709 }
710
Petr Machata52dbfb12012-03-29 16:38:26 +0200711 return 0;
712}
713
Petr Machataf7fee432012-04-19 17:00:53 +0200714void
Petr Machata52dbfb12012-03-29 16:38:26 +0200715proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
716{
Petr Machataf7fee432012-04-19 17:00:53 +0200717 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
718 proc->pid, breakpoint_name(bp), bp->addr);
719 check_leader(proc);
720 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
721 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200722}
Petr Machatad3cc9882012-04-13 21:40:23 +0200723
724/* Dict doesn't support iteration restarts, so here's this contraption
725 * for now. XXX add restarts to dict. */
726struct each_breakpoint_data
727{
728 void *start;
729 void *end;
730 struct Process *proc;
731 enum callback_status (*cb)(struct Process *proc,
732 struct breakpoint *bp,
733 void *data);
734 void *cb_data;
735};
736
737static void
738each_breakpoint_cb(void *key, void *value, void *d)
739{
740 struct each_breakpoint_data *data = d;
741 if (data->end != NULL)
742 return;
743 if (data->start == key)
744 data->start = NULL;
745
746 if (data->start == NULL) {
747 switch (data->cb(data->proc, value, data->cb_data)) {
748 case CBS_FAIL:
749 /* XXX handle me */
750 case CBS_STOP:
751 data->end = key;
752 case CBS_CONT:
753 return;
754 }
755 }
756}
757
758void *
759proc_each_breakpoint(struct Process *proc, void *start,
760 enum callback_status (*cb)(struct Process *proc,
761 struct breakpoint *bp,
762 void *data), void *data)
763{
764 struct each_breakpoint_data dd = {
765 .start = start,
766 .proc = proc,
767 .cb = cb,
768 .cb_data = data,
769 };
770 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
771 return dd.end;
772}