blob: b201c44a5cdc2df1ba7ff31e1dd3634db327ac6d [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;
Petr Machata2cb124c2012-04-19 18:44:45 +0200250 retp->e_machine = proc->e_machine;
Petr Machatacf1679a2012-04-06 19:56:17 +0200251
Petr Machata2b46cfc2012-02-18 11:17:29 +0100252 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200253 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100254 return 0;
255
256 /* Clone symbols first so that we can clone and relink
257 * breakpoints. */
258 struct library *lib;
259 struct library **nlibp = &retp->libraries;
260 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
261 *nlibp = malloc(sizeof(**nlibp));
262 if (*nlibp == NULL
263 || library_clone(*nlibp, lib) < 0) {
264 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200265 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100266
267 /* Error when cloning. Unroll what was done. */
268 for (lib = retp->libraries; lib != NULL; ) {
269 struct library *next = lib->next;
270 library_destroy(lib);
271 free(lib);
272 lib = next;
273 }
274 goto fail;
275 }
276
277 nlibp = &(*nlibp)->next;
278 }
279
280 /* Now clone breakpoints. Symbol relinking is done in
281 * clone_single_bp. */
282 struct clone_single_bp_data data = {
283 .old_proc = proc,
284 .new_proc = retp,
285 .error = 0,
286 };
287 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
288
Petr Machataded6f972012-04-13 23:15:48 +0200289 /* And finally the call stack. */
290 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
291 retp->callstack_depth = proc->callstack_depth;
292
Petr Machata2b46cfc2012-02-18 11:17:29 +0100293 if (data.error < 0)
294 goto fail2;
295
Petr Machata744f2552012-04-15 04:33:18 +0200296 if (arch_process_clone(retp, proc) < 0)
297 goto fail2;
298
Petr Machata2b46cfc2012-02-18 11:17:29 +0100299 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100300}
301
Petr Machata3c516d52011-08-18 03:53:18 +0200302static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200303open_one_pid(pid_t pid)
304{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200305 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100306 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200307 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
308
Petr Machata1974dbc2011-08-19 18:58:01 +0200309 /* Get the filename first. Should the trace_pid fail, we can
310 * easily free it, untracing is more work. */
311 if ((filename = pid2name(pid)) == NULL
312 || trace_pid(pid) < 0) {
313 free(filename);
314 return -1;
315 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100316
Petr Machata75934ad2012-04-14 02:28:03 +0200317 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200318 if (proc == NULL)
319 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200320 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200321
Petr Machata1974dbc2011-08-19 18:58:01 +0200322 return 0;
323}
324
Petr Machata2b46cfc2012-02-18 11:17:29 +0100325static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200326start_one_pid(Process * proc, void * data)
327{
328 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100329 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100330}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100331
Petr Machata9a5420c2011-07-09 11:21:23 +0200332void
333open_pid(pid_t pid)
334{
335 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200336 /* If we are already tracing this guy, we should be seeing all
337 * his children via normal tracing route. */
338 if (pid2proc(pid) != NULL)
339 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200340
Petr Machata3c516d52011-08-18 03:53:18 +0200341 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200342 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200343 fprintf(stderr, "Cannot attach to pid %u: %s\n",
344 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200345 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200346 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200347 }
348
Petr Machata3c516d52011-08-18 03:53:18 +0200349 /* Now attach to all tasks that belong to that PID. There's a
350 * race between process_tasks and open_one_pid. So when we
351 * fail in open_one_pid below, we just do another round.
352 * Chances are that by then that PID will have gone away, and
353 * that's why we have seen the failure. The processes that we
354 * manage to open_one_pid are stopped, so we should eventually
355 * reach a point where process_tasks doesn't give any new
356 * processes (because there's nobody left to produce
357 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200358 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200359 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200360 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200361 pid_t *tasks;
362 size_t ntasks;
363 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200364
Petr Machata3c516d52011-08-18 03:53:18 +0200365 if (process_tasks(pid, &tasks, &ntasks) < 0) {
366 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
367 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100368 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200369 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200370
Petr Machata3c516d52011-08-18 03:53:18 +0200371 have_all = 1;
372 for (i = 0; i < ntasks; ++i)
373 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200374 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200375 have_all = 0;
376
Petr Machata9a5420c2011-07-09 11:21:23 +0200377 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200378
Petr Machata1974dbc2011-08-19 18:58:01 +0200379 if (have_all && old_ntasks == ntasks)
380 break;
381 old_ntasks = ntasks;
382 }
383
Petr Machata93d95df2012-04-17 05:16:19 +0200384 struct Process *leader = pid2proc(pid)->leader;
385
386 /* XXX Is there a way to figure out whether _start has
387 * actually already been hit? */
388 arch_dynlink_done(leader);
389
Petr Machata2f9b78e2012-04-16 21:08:54 +0200390 /* Done. Continue everyone. */
Petr Machata93d95df2012-04-17 05:16:19 +0200391 each_task(leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200392}
393
Petr Machata2b46cfc2012-02-18 11:17:29 +0100394static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200395find_proc(Process * proc, void * data)
396{
397 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100398 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200399}
400
Juan Cespedesa8909f72009-04-28 20:02:41 +0200401Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100402pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200403 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
404}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100405
Petr Machatacebb8842011-07-09 11:14:11 +0200406static Process * list_of_processes = NULL;
407
Petr Machatacbe29c62011-09-27 02:27:58 +0200408static void
409unlist_process(Process * proc)
410{
411 Process *tmp;
412
413 if (list_of_processes == proc) {
414 list_of_processes = list_of_processes->next;
415 return;
416 }
417
418 for (tmp = list_of_processes; ; tmp = tmp->next) {
419 /* If the following assert fails, the process wasn't
420 * in the list. */
421 assert(tmp->next != NULL);
422
423 if (tmp->next == proc) {
424 tmp->next = tmp->next->next;
425 return;
426 }
427 }
428}
429
Petr Machata2b46cfc2012-02-18 11:17:29 +0100430struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100431each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100432 enum callback_status(*cb)(struct Process *proc, void *data),
433 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200434{
Petr Machata74132a42012-03-16 02:46:18 +0100435 struct Process *it = start_after == NULL ? list_of_processes
436 : start_after->next;
437
438 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200439 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100440 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100441 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200442 case CBS_FAIL:
443 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100444 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200445 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100446 case CBS_CONT:
447 break;
448 }
Petr Machatacebb8842011-07-09 11:14:11 +0200449 it = next;
450 }
451 return NULL;
452}
Petr Machata9a5420c2011-07-09 11:21:23 +0200453
454Process *
Petr Machata74132a42012-03-16 02:46:18 +0100455each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100456 enum callback_status(*cb)(struct Process *proc, void *data),
457 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200458{
Petr Machata74132a42012-03-16 02:46:18 +0100459 assert(proc != NULL);
460 struct Process *it = start_after == NULL ? proc->leader
461 : start_after->next;
462
Petr Machata9a5420c2011-07-09 11:21:23 +0200463 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100464 struct Process *leader = it->leader;
465 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200466 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100467 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100468 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200469 case CBS_FAIL:
470 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100471 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200472 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100473 case CBS_CONT:
474 break;
475 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200476 it = next;
477 }
478 }
479 return NULL;
480}
481
Petr Machata44965c72012-04-06 19:59:20 +0200482static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200483add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200484{
Petr Machata9a5420c2011-07-09 11:21:23 +0200485 Process ** leaderp = &list_of_processes;
486 if (proc->pid) {
487 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200488 if (tgid == 0)
489 /* Must have been terminated before we managed
490 * to fully attach. */
491 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200492 if (tgid == proc->pid)
493 proc->leader = proc;
494 else {
495 Process * leader = pid2proc(tgid);
496 proc->leader = leader;
497 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200498 leaderp = &leader->next;
499 }
500 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200501
502 if (!was_exec) {
503 proc->next = *leaderp;
504 *leaderp = proc;
505 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200506}
507
Petr Machatacbe29c62011-09-27 02:27:58 +0200508void
509change_process_leader(Process * proc, Process * leader)
510{
511 Process ** leaderp = &list_of_processes;
512 if (proc->leader == leader)
513 return;
514
515 assert(leader != NULL);
516 unlist_process(proc);
517 if (proc != leader)
518 leaderp = &leader->next;
519
520 proc->leader = leader;
521 proc->next = *leaderp;
522 *leaderp = proc;
523}
524
Petr Machata2b46cfc2012-02-18 11:17:29 +0100525static enum callback_status
526clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200527{
528 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
529 proc->pid, proc->leader->pid);
530 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100531 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200532}
533
Petr Machata69a03e62011-07-09 11:29:12 +0200534static enum ecb_status
535event_for_proc(Event * event, void * data)
536{
537 if (event->proc == data)
538 return ecb_deque;
539 else
540 return ecb_cont;
541}
542
543static void
544delete_events_for(Process * proc)
545{
546 Event * event;
547 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
548 free(event);
549}
550
Petr Machatacebb8842011-07-09 11:14:11 +0200551void
552remove_process(Process *proc)
553{
Petr Machatacebb8842011-07-09 11:14:11 +0200554 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
555
Petr Machata9a5420c2011-07-09 11:21:23 +0200556 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100557 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200558
Petr Machatacbe29c62011-09-27 02:27:58 +0200559 unlist_process(proc);
560 delete_events_for(proc);
Petr Machata9b87e822012-04-24 18:12:10 +0200561 process_destroy(proc);
562 free(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100563}
Petr Machata4007d742011-07-09 11:29:42 +0200564
565void
Petr Machata366c2f42012-02-09 19:34:36 +0100566install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200567{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200568 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200569 assert(proc->event_handler == NULL);
570 proc->event_handler = handler;
571}
572
573void
574destroy_event_handler(Process * proc)
575{
Petr Machata366c2f42012-02-09 19:34:36 +0100576 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200577 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200578 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200579 if (handler->destroy != NULL)
580 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200581 free(handler);
582 proc->event_handler = NULL;
583}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100584
585static enum callback_status
586breakpoint_for_symbol(struct library_symbol *libsym, void *data)
587{
588 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200589 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100590
Petr Machatad5e85562012-04-05 15:18:38 +0200591 /* If there is an artificial breakpoint on the same address,
592 * its libsym will be NULL, and we can smuggle our libsym
593 * there. That artificial breakpoint is there presumably for
594 * the callbacks, which we don't touch. If there is a real
595 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200596 * symbols and ignore extra symbol aliases.
597 *
598 * The other direction is more complicated and currently not
599 * supported. If a breakpoint has custom callbacks, it might
600 * be also custom-allocated, and we would really need to swap
601 * the two: delete the one now in the dictionary, swap values
602 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200603 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
604 libsym->enter_addr);
605 if (bp != NULL) {
606 assert(bp->libsym == NULL);
607 bp->libsym = libsym;
608 return CBS_CONT;
609 }
610
611 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200612 if (bp == NULL
613 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
614 fail:
615 free(bp);
616 return CBS_FAIL;
617 }
618 if (proc_add_breakpoint(proc, bp) < 0) {
619 breakpoint_destroy(bp);
620 goto fail;
621 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100622
Petr Machatafa0c5702012-04-13 18:43:40 +0200623 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200624 proc_remove_breakpoint(proc, bp);
625 breakpoint_destroy(bp);
626 goto fail;
627 }
628
Petr Machata2b46cfc2012-02-18 11:17:29 +0100629 return CBS_CONT;
630}
631
632void
633proc_add_library(struct Process *proc, struct library *lib)
634{
635 assert(lib->next == NULL);
636 lib->next = proc->libraries;
637 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200638 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
639 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100640
641 struct library_symbol *libsym = NULL;
642 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100643 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100644 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100645}
646
647int
648proc_remove_library(struct Process *proc, struct library *lib)
649{
650 struct library **libp;
651 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
652 if (*libp == lib) {
653 *libp = lib->next;
654 return 0;
655 }
656 return -1;
657}
658
659struct library *
660proc_each_library(struct Process *proc, struct library *it,
661 enum callback_status (*cb)(struct Process *proc,
662 struct library *lib, void *data),
663 void *data)
664{
665 if (it == NULL)
666 it = proc->libraries;
667
668 while (it != NULL) {
669 struct library *next = it->next;
670
671 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200672 case CBS_FAIL:
673 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100674 case CBS_STOP:
675 return it;
676 case CBS_CONT:
677 break;
678 }
679
680 it = next;
681 }
682
683 return NULL;
684}
Petr Machata52dbfb12012-03-29 16:38:26 +0200685
Petr Machataf7fee432012-04-19 17:00:53 +0200686static void
687check_leader(struct Process *proc)
Petr Machata52dbfb12012-03-29 16:38:26 +0200688{
Petr Machata52dbfb12012-03-29 16:38:26 +0200689 /* Only the group leader should be getting the breakpoints and
690 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200691 assert(proc->leader != NULL);
692 assert(proc->leader == proc);
693 assert(proc->breakpoints != NULL);
Petr Machataf7fee432012-04-19 17:00:53 +0200694}
Petr Machata52dbfb12012-03-29 16:38:26 +0200695
Petr Machataf7fee432012-04-19 17:00:53 +0200696int
697proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
698{
Petr Machatafa0c5702012-04-13 18:43:40 +0200699 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200700 proc->pid, breakpoint_name(bp), bp->addr);
Petr Machataf7fee432012-04-19 17:00:53 +0200701 check_leader(proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200702
Petr Machataa2416362012-04-06 02:43:34 +0200703 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200704 * assert, but that's not necessary right now. Read the
705 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200706 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200707
Petr Machatafa0c5702012-04-13 18:43:40 +0200708 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200709 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
710 breakpoint_name(bp), bp->addr);
711 return -1;
712 }
713
Petr Machata52dbfb12012-03-29 16:38:26 +0200714 return 0;
715}
716
Petr Machataf7fee432012-04-19 17:00:53 +0200717void
Petr Machata52dbfb12012-03-29 16:38:26 +0200718proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
719{
Petr Machataf7fee432012-04-19 17:00:53 +0200720 debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
721 proc->pid, breakpoint_name(bp), bp->addr);
722 check_leader(proc);
723 struct breakpoint *removed = dict_remove(proc->breakpoints, bp->addr);
724 assert(removed == bp);
Petr Machata52dbfb12012-03-29 16:38:26 +0200725}
Petr Machatad3cc9882012-04-13 21:40:23 +0200726
727/* Dict doesn't support iteration restarts, so here's this contraption
728 * for now. XXX add restarts to dict. */
729struct each_breakpoint_data
730{
731 void *start;
732 void *end;
733 struct Process *proc;
734 enum callback_status (*cb)(struct Process *proc,
735 struct breakpoint *bp,
736 void *data);
737 void *cb_data;
738};
739
740static void
741each_breakpoint_cb(void *key, void *value, void *d)
742{
743 struct each_breakpoint_data *data = d;
744 if (data->end != NULL)
745 return;
746 if (data->start == key)
747 data->start = NULL;
748
749 if (data->start == NULL) {
750 switch (data->cb(data->proc, value, data->cb_data)) {
751 case CBS_FAIL:
752 /* XXX handle me */
753 case CBS_STOP:
754 data->end = key;
755 case CBS_CONT:
756 return;
757 }
758 }
759}
760
761void *
762proc_each_breakpoint(struct Process *proc, void *start,
763 enum callback_status (*cb)(struct Process *proc,
764 struct breakpoint *bp,
765 void *data), void *data)
766{
767 struct each_breakpoint_data dd = {
768 .start = start,
769 .proc = proc,
770 .cb = cb,
771 .cb_data = data,
772 };
773 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
774 return dd.end;
775}