blob: 4da09dfc77fcba2767323dbddf451d0612f173bb [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 Machata3d0c91c2012-04-14 02:37:38 +020045static void add_process(struct Process *proc, int was_exec);
Petr Machata44965c72012-04-06 19:59:20 +020046
Petr Machata2b46cfc2012-02-18 11:17:29 +010047static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020048process_bare_init(struct Process *proc, const char *filename,
49 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010050{
Petr Machata3d0c91c2012-04-14 02:37:38 +020051 if (!was_exec) {
52 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020053
Petr Machata3d0c91c2012-04-14 02:37:38 +020054 proc->filename = strdup(filename);
55 if (proc->filename == NULL) {
56 fail:
57 free(proc->filename);
58 if (proc->breakpoints != NULL)
59 dict_clear(proc->breakpoints);
60 return -1;
61 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010062 }
63
64 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020065 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +020066 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +010067 if (proc->leader == NULL)
68 goto fail;
69
70 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +020071 proc->breakpoints = dict_init(target_address_hash,
72 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +010073 if (proc->breakpoints == NULL)
74 goto fail;
75 } else {
76 proc->breakpoints = NULL;
77 }
78
Joe Damatoab3b72c2010-10-31 00:21:53 -070079#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020080 proc->unwind_priv = _UPT_create(pid);
81 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070082#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070083
Petr Machata2b46cfc2012-02-18 11:17:29 +010084 return 0;
85}
86
87static void
Petr Machata3d0c91c2012-04-14 02:37:38 +020088process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010089{
Petr Machata2b46cfc2012-02-18 11:17:29 +010090 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +020091 if (!was_exec) {
92 free(proc->filename);
93 remove_process(proc);
94 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010095}
96
Petr Machata3d0c91c2012-04-14 02:37:38 +020097static int
98process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +010099{
Petr Machata18bd8ff2012-04-10 04:32:39 +0200100 target_address_t entry;
101 target_address_t interp_bias;
102 if (process_get_entry(proc, &entry, &interp_bias) < 0) {
103 fprintf(stderr, "Couldn't get entry points of process %d\n",
Petr Machata2b46cfc2012-02-18 11:17:29 +0100104 proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100105 return -1;
106 }
107
Petr Machata3d0c91c2012-04-14 02:37:38 +0200108 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +0200109 fprintf(stderr, "failed to init breakpoints %d\n",
110 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +0200111 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +0200112 }
113
Petr Machata2b46cfc2012-02-18 11:17:29 +0100114 return 0;
115}
116
Petr Machata3d0c91c2012-04-14 02:37:38 +0200117int
118process_init(struct Process *proc, const char *filename, pid_t pid)
119{
120 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +0200121 fail:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200122 error(0, errno, "init process %d", pid);
123 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
140static void
141destroy_breakpoint_cb(void *key, void *value, void *data)
142{
143 struct breakpoint *bp = value;
144 breakpoint_destroy(bp);
145 free(bp);
146}
147
148static void
149private_process_destroy(struct Process *proc, int keep_filename)
150{
151 if (!keep_filename)
152 free(proc->filename);
153
154 /* Libraries and symbols. */
155 struct library *lib;
156 for (lib = proc->libraries; lib != NULL; ) {
157 struct library *next = lib->next;
158 library_destroy(lib);
159 free(lib);
160 lib = next;
161 }
162 proc->libraries = NULL;
163
164 /* Breakpoints. */
165 dict_apply_to_all(proc->breakpoints, destroy_breakpoint_cb, NULL);
166 dict_clear(proc->breakpoints);
167 proc->breakpoints = NULL;
168}
169
170void
171process_destroy(struct Process *proc)
172{
173 private_process_destroy(proc, 0);
Petr Machata744f2552012-04-15 04:33:18 +0200174 arch_process_destroy(proc);
Petr Machata3d0c91c2012-04-14 02:37:38 +0200175}
176
177int
178process_exec(struct Process *proc)
179{
Petr Machata744f2552012-04-15 04:33:18 +0200180 /* Call exec first, before we destroy the main state. */
181 if (arch_process_exec(proc) < 0)
182 return -1;
183
Petr Machata3d0c91c2012-04-14 02:37:38 +0200184 private_process_destroy(proc, 1);
185 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
186 return -1;
187 if (process_init_main(proc) < 0) {
188 process_bare_destroy(proc, 1);
189 return -1;
190 }
191 return 0;
192}
193
Petr Machata2b46cfc2012-02-18 11:17:29 +0100194struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200195open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100196{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100197 assert(pid != 0);
198 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200199 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200200 free(proc);
201 return NULL;
202 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100203 return proc;
204}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100205
Petr Machata2b46cfc2012-02-18 11:17:29 +0100206struct clone_single_bp_data {
207 struct Process *old_proc;
208 struct Process *new_proc;
209 int error;
210};
211
Petr Machata2b46cfc2012-02-18 11:17:29 +0100212static void
213clone_single_bp(void *key, void *value, void *u)
214{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215 struct breakpoint *bp = value;
216 struct clone_single_bp_data *data = u;
217
Petr Machatad3cc9882012-04-13 21:40:23 +0200218 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100219 struct breakpoint *clone = malloc(sizeof(*clone));
220 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200221 || breakpoint_clone(clone, data->new_proc,
222 bp, data->old_proc) < 0) {
223 fail:
224 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100225 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100226 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200227 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
228 breakpoint_destroy(clone);
229 goto fail;
230 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100231}
232
233int
234process_clone(struct Process *retp, struct Process *proc, pid_t pid)
235{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200236 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100237 fail:
238 error(0, errno, "clone process %d->%d", proc->pid, pid);
239 return -1;
240 }
241
Petr Machatacf1679a2012-04-06 19:56:17 +0200242 retp->tracesysgood = proc->tracesysgood;
243
Petr Machata2b46cfc2012-02-18 11:17:29 +0100244 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200245 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100246 return 0;
247
248 /* Clone symbols first so that we can clone and relink
249 * breakpoints. */
250 struct library *lib;
251 struct library **nlibp = &retp->libraries;
252 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
253 *nlibp = malloc(sizeof(**nlibp));
254 if (*nlibp == NULL
255 || library_clone(*nlibp, lib) < 0) {
256 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200257 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100258
259 /* Error when cloning. Unroll what was done. */
260 for (lib = retp->libraries; lib != NULL; ) {
261 struct library *next = lib->next;
262 library_destroy(lib);
263 free(lib);
264 lib = next;
265 }
266 goto fail;
267 }
268
269 nlibp = &(*nlibp)->next;
270 }
271
272 /* Now clone breakpoints. Symbol relinking is done in
273 * clone_single_bp. */
274 struct clone_single_bp_data data = {
275 .old_proc = proc,
276 .new_proc = retp,
277 .error = 0,
278 };
279 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
280
Petr Machataded6f972012-04-13 23:15:48 +0200281 /* And finally the call stack. */
282 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
283 retp->callstack_depth = proc->callstack_depth;
284
Petr Machata2b46cfc2012-02-18 11:17:29 +0100285 if (data.error < 0)
286 goto fail2;
287
Petr Machata744f2552012-04-15 04:33:18 +0200288 if (arch_process_clone(retp, proc) < 0)
289 goto fail2;
290
Petr Machata2b46cfc2012-02-18 11:17:29 +0100291 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100292}
293
Petr Machata3c516d52011-08-18 03:53:18 +0200294static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200295open_one_pid(pid_t pid)
296{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200297 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100298 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200299 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
300
Petr Machata1974dbc2011-08-19 18:58:01 +0200301 /* Get the filename first. Should the trace_pid fail, we can
302 * easily free it, untracing is more work. */
303 if ((filename = pid2name(pid)) == NULL
304 || trace_pid(pid) < 0) {
305 free(filename);
306 return -1;
307 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100308
Petr Machata75934ad2012-04-14 02:28:03 +0200309 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200310 if (proc == NULL)
311 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200312 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200313
Petr Machata1974dbc2011-08-19 18:58:01 +0200314 return 0;
315}
316
Petr Machata2b46cfc2012-02-18 11:17:29 +0100317static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200318start_one_pid(Process * proc, void * data)
319{
320 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100321 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100322}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100323
Petr Machata9a5420c2011-07-09 11:21:23 +0200324void
325open_pid(pid_t pid)
326{
327 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200328 /* If we are already tracing this guy, we should be seeing all
329 * his children via normal tracing route. */
330 if (pid2proc(pid) != NULL)
331 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200332
Petr Machata3c516d52011-08-18 03:53:18 +0200333 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200334 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200335 fprintf(stderr, "Cannot attach to pid %u: %s\n",
336 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200337 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200338 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200339 }
340
Petr Machata3c516d52011-08-18 03:53:18 +0200341 /* Now attach to all tasks that belong to that PID. There's a
342 * race between process_tasks and open_one_pid. So when we
343 * fail in open_one_pid below, we just do another round.
344 * Chances are that by then that PID will have gone away, and
345 * that's why we have seen the failure. The processes that we
346 * manage to open_one_pid are stopped, so we should eventually
347 * reach a point where process_tasks doesn't give any new
348 * processes (because there's nobody left to produce
349 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200350 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200351 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200352 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200353 pid_t *tasks;
354 size_t ntasks;
355 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200356
Petr Machata3c516d52011-08-18 03:53:18 +0200357 if (process_tasks(pid, &tasks, &ntasks) < 0) {
358 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
359 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100360 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200361 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200362
Petr Machata3c516d52011-08-18 03:53:18 +0200363 have_all = 1;
364 for (i = 0; i < ntasks; ++i)
365 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200366 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200367 have_all = 0;
368
Petr Machata9a5420c2011-07-09 11:21:23 +0200369 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200370
Petr Machata1974dbc2011-08-19 18:58:01 +0200371 if (have_all && old_ntasks == ntasks)
372 break;
373 old_ntasks = ntasks;
374 }
375
376 /* Done. Now initialize breakpoints and then continue
377 * everyone. */
378 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200379 leader = pid2proc(pid)->leader;
380 enable_all_breakpoints(leader);
381
Petr Machata74132a42012-03-16 02:46:18 +0100382 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200383}
384
Petr Machata2b46cfc2012-02-18 11:17:29 +0100385static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200386find_proc(Process * proc, void * data)
387{
388 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100389 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200390}
391
Juan Cespedesa8909f72009-04-28 20:02:41 +0200392Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100393pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200394 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
395}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100396
Petr Machatacebb8842011-07-09 11:14:11 +0200397static Process * list_of_processes = NULL;
398
Petr Machatacbe29c62011-09-27 02:27:58 +0200399static void
400unlist_process(Process * proc)
401{
402 Process *tmp;
403
404 if (list_of_processes == proc) {
405 list_of_processes = list_of_processes->next;
406 return;
407 }
408
409 for (tmp = list_of_processes; ; tmp = tmp->next) {
410 /* If the following assert fails, the process wasn't
411 * in the list. */
412 assert(tmp->next != NULL);
413
414 if (tmp->next == proc) {
415 tmp->next = tmp->next->next;
416 return;
417 }
418 }
419}
420
Petr Machata2b46cfc2012-02-18 11:17:29 +0100421struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100422each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100423 enum callback_status(*cb)(struct Process *proc, void *data),
424 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200425{
Petr Machata74132a42012-03-16 02:46:18 +0100426 struct Process *it = start_after == NULL ? list_of_processes
427 : start_after->next;
428
429 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200430 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100431 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100432 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200433 case CBS_FAIL:
434 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100435 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200436 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100437 case CBS_CONT:
438 break;
439 }
Petr Machatacebb8842011-07-09 11:14:11 +0200440 it = next;
441 }
442 return NULL;
443}
Petr Machata9a5420c2011-07-09 11:21:23 +0200444
445Process *
Petr Machata74132a42012-03-16 02:46:18 +0100446each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100447 enum callback_status(*cb)(struct Process *proc, void *data),
448 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200449{
Petr Machata74132a42012-03-16 02:46:18 +0100450 assert(proc != NULL);
451 struct Process *it = start_after == NULL ? proc->leader
452 : start_after->next;
453
Petr Machata9a5420c2011-07-09 11:21:23 +0200454 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100455 struct Process *leader = it->leader;
456 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200457 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100458 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100459 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200460 case CBS_FAIL:
461 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100462 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200463 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100464 case CBS_CONT:
465 break;
466 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200467 it = next;
468 }
469 }
470 return NULL;
471}
472
Petr Machata44965c72012-04-06 19:59:20 +0200473static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200474add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200475{
Petr Machata9a5420c2011-07-09 11:21:23 +0200476 Process ** leaderp = &list_of_processes;
477 if (proc->pid) {
478 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200479 if (tgid == 0)
480 /* Must have been terminated before we managed
481 * to fully attach. */
482 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200483 if (tgid == proc->pid)
484 proc->leader = proc;
485 else {
486 Process * leader = pid2proc(tgid);
487 proc->leader = leader;
488 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200489 leaderp = &leader->next;
490 }
491 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200492
493 if (!was_exec) {
494 proc->next = *leaderp;
495 *leaderp = proc;
496 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200497}
498
Petr Machatacbe29c62011-09-27 02:27:58 +0200499void
500change_process_leader(Process * proc, Process * leader)
501{
502 Process ** leaderp = &list_of_processes;
503 if (proc->leader == leader)
504 return;
505
506 assert(leader != NULL);
507 unlist_process(proc);
508 if (proc != leader)
509 leaderp = &leader->next;
510
511 proc->leader = leader;
512 proc->next = *leaderp;
513 *leaderp = proc;
514}
515
Petr Machata2b46cfc2012-02-18 11:17:29 +0100516static enum callback_status
517clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200518{
519 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
520 proc->pid, proc->leader->pid);
521 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100522 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200523}
524
Petr Machata69a03e62011-07-09 11:29:12 +0200525static enum ecb_status
526event_for_proc(Event * event, void * data)
527{
528 if (event->proc == data)
529 return ecb_deque;
530 else
531 return ecb_cont;
532}
533
534static void
535delete_events_for(Process * proc)
536{
537 Event * event;
538 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
539 free(event);
540}
541
Petr Machatacebb8842011-07-09 11:14:11 +0200542void
543remove_process(Process *proc)
544{
Petr Machatacebb8842011-07-09 11:14:11 +0200545 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
546
Petr Machata9a5420c2011-07-09 11:21:23 +0200547 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100548 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200549
Petr Machatacbe29c62011-09-27 02:27:58 +0200550 unlist_process(proc);
551 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100552}
Petr Machata4007d742011-07-09 11:29:42 +0200553
554void
Petr Machata366c2f42012-02-09 19:34:36 +0100555install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200556{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200557 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200558 assert(proc->event_handler == NULL);
559 proc->event_handler = handler;
560}
561
562void
563destroy_event_handler(Process * proc)
564{
Petr Machata366c2f42012-02-09 19:34:36 +0100565 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200566 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200567 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200568 if (handler->destroy != NULL)
569 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200570 free(handler);
571 proc->event_handler = NULL;
572}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100573
574static enum callback_status
575breakpoint_for_symbol(struct library_symbol *libsym, void *data)
576{
577 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200578 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100579
Petr Machatad5e85562012-04-05 15:18:38 +0200580 /* If there is an artificial breakpoint on the same address,
581 * its libsym will be NULL, and we can smuggle our libsym
582 * there. That artificial breakpoint is there presumably for
583 * the callbacks, which we don't touch. If there is a real
584 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200585 * symbols and ignore extra symbol aliases.
586 *
587 * The other direction is more complicated and currently not
588 * supported. If a breakpoint has custom callbacks, it might
589 * be also custom-allocated, and we would really need to swap
590 * the two: delete the one now in the dictionary, swap values
591 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200592 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
593 libsym->enter_addr);
594 if (bp != NULL) {
595 assert(bp->libsym == NULL);
596 bp->libsym = libsym;
597 return CBS_CONT;
598 }
599
600 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200601 if (bp == NULL
602 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
603 fail:
604 free(bp);
605 return CBS_FAIL;
606 }
607 if (proc_add_breakpoint(proc, bp) < 0) {
608 breakpoint_destroy(bp);
609 goto fail;
610 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100611
Petr Machatafa0c5702012-04-13 18:43:40 +0200612 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200613 proc_remove_breakpoint(proc, bp);
614 breakpoint_destroy(bp);
615 goto fail;
616 }
617
Petr Machata2b46cfc2012-02-18 11:17:29 +0100618 return CBS_CONT;
619}
620
621void
622proc_add_library(struct Process *proc, struct library *lib)
623{
624 assert(lib->next == NULL);
625 lib->next = proc->libraries;
626 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200627 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
628 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100629
630 struct library_symbol *libsym = NULL;
631 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100632 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100633 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100634}
635
636int
637proc_remove_library(struct Process *proc, struct library *lib)
638{
639 struct library **libp;
640 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
641 if (*libp == lib) {
642 *libp = lib->next;
643 return 0;
644 }
645 return -1;
646}
647
648struct library *
649proc_each_library(struct Process *proc, struct library *it,
650 enum callback_status (*cb)(struct Process *proc,
651 struct library *lib, void *data),
652 void *data)
653{
654 if (it == NULL)
655 it = proc->libraries;
656
657 while (it != NULL) {
658 struct library *next = it->next;
659
660 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200661 case CBS_FAIL:
662 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100663 case CBS_STOP:
664 return it;
665 case CBS_CONT:
666 break;
667 }
668
669 it = next;
670 }
671
672 return NULL;
673}
Petr Machata52dbfb12012-03-29 16:38:26 +0200674
675int
676proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
677{
Petr Machata52dbfb12012-03-29 16:38:26 +0200678 /* Only the group leader should be getting the breakpoints and
679 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200680 assert(proc->leader != NULL);
681 assert(proc->leader == proc);
682 assert(proc->breakpoints != NULL);
Petr Machata52dbfb12012-03-29 16:38:26 +0200683
Petr Machatafa0c5702012-04-13 18:43:40 +0200684 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200685 proc->pid, breakpoint_name(bp), bp->addr);
686
Petr Machataa2416362012-04-06 02:43:34 +0200687 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200688 * assert, but that's not necessary right now. Read the
689 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200690 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200691
Petr Machatafa0c5702012-04-13 18:43:40 +0200692 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200693 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
694 breakpoint_name(bp), bp->addr);
695 return -1;
696 }
697
Petr Machata52dbfb12012-03-29 16:38:26 +0200698 return 0;
699}
700
701int
702proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
703{
704 /* XXX We can't, really. We are missing dict_remove. */
705 assert(!"Not yet implemented!");
706 abort();
707}
Petr Machatad3cc9882012-04-13 21:40:23 +0200708
709/* Dict doesn't support iteration restarts, so here's this contraption
710 * for now. XXX add restarts to dict. */
711struct each_breakpoint_data
712{
713 void *start;
714 void *end;
715 struct Process *proc;
716 enum callback_status (*cb)(struct Process *proc,
717 struct breakpoint *bp,
718 void *data);
719 void *cb_data;
720};
721
722static void
723each_breakpoint_cb(void *key, void *value, void *d)
724{
725 struct each_breakpoint_data *data = d;
726 if (data->end != NULL)
727 return;
728 if (data->start == key)
729 data->start = NULL;
730
731 if (data->start == NULL) {
732 switch (data->cb(data->proc, value, data->cb_data)) {
733 case CBS_FAIL:
734 /* XXX handle me */
735 case CBS_STOP:
736 data->end = key;
737 case CBS_CONT:
738 return;
739 }
740 }
741}
742
743void *
744proc_each_breakpoint(struct Process *proc, void *start,
745 enum callback_status (*cb)(struct Process *proc,
746 struct breakpoint *bp,
747 void *data), void *data)
748{
749 struct each_breakpoint_data dd = {
750 .start = start,
751 .proc = proc,
752 .cb = cb,
753 .cb_data = data,
754 };
755 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
756 return dd.end;
757}