blob: 3f5789ea437dc69e5bf93ae0ba85b465107a4927 [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
Petr Machata2f9b78e2012-04-16 21:08:54 +0200376 /* Done. Continue everyone. */
Petr Machata74132a42012-03-16 02:46:18 +0100377 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200378}
379
Petr Machata2b46cfc2012-02-18 11:17:29 +0100380static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200381find_proc(Process * proc, void * data)
382{
383 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100384 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200385}
386
Juan Cespedesa8909f72009-04-28 20:02:41 +0200387Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100388pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200389 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
390}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100391
Petr Machatacebb8842011-07-09 11:14:11 +0200392static Process * list_of_processes = NULL;
393
Petr Machatacbe29c62011-09-27 02:27:58 +0200394static void
395unlist_process(Process * proc)
396{
397 Process *tmp;
398
399 if (list_of_processes == proc) {
400 list_of_processes = list_of_processes->next;
401 return;
402 }
403
404 for (tmp = list_of_processes; ; tmp = tmp->next) {
405 /* If the following assert fails, the process wasn't
406 * in the list. */
407 assert(tmp->next != NULL);
408
409 if (tmp->next == proc) {
410 tmp->next = tmp->next->next;
411 return;
412 }
413 }
414}
415
Petr Machata2b46cfc2012-02-18 11:17:29 +0100416struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100417each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100418 enum callback_status(*cb)(struct Process *proc, void *data),
419 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200420{
Petr Machata74132a42012-03-16 02:46:18 +0100421 struct Process *it = start_after == NULL ? list_of_processes
422 : start_after->next;
423
424 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200425 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100426 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100427 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200428 case CBS_FAIL:
429 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100430 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200431 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100432 case CBS_CONT:
433 break;
434 }
Petr Machatacebb8842011-07-09 11:14:11 +0200435 it = next;
436 }
437 return NULL;
438}
Petr Machata9a5420c2011-07-09 11:21:23 +0200439
440Process *
Petr Machata74132a42012-03-16 02:46:18 +0100441each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100442 enum callback_status(*cb)(struct Process *proc, void *data),
443 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200444{
Petr Machata74132a42012-03-16 02:46:18 +0100445 assert(proc != NULL);
446 struct Process *it = start_after == NULL ? proc->leader
447 : start_after->next;
448
Petr Machata9a5420c2011-07-09 11:21:23 +0200449 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100450 struct Process *leader = it->leader;
451 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200452 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100453 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100454 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200455 case CBS_FAIL:
456 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100457 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200458 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100459 case CBS_CONT:
460 break;
461 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200462 it = next;
463 }
464 }
465 return NULL;
466}
467
Petr Machata44965c72012-04-06 19:59:20 +0200468static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200469add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200470{
Petr Machata9a5420c2011-07-09 11:21:23 +0200471 Process ** leaderp = &list_of_processes;
472 if (proc->pid) {
473 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200474 if (tgid == 0)
475 /* Must have been terminated before we managed
476 * to fully attach. */
477 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200478 if (tgid == proc->pid)
479 proc->leader = proc;
480 else {
481 Process * leader = pid2proc(tgid);
482 proc->leader = leader;
483 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200484 leaderp = &leader->next;
485 }
486 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200487
488 if (!was_exec) {
489 proc->next = *leaderp;
490 *leaderp = proc;
491 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200492}
493
Petr Machatacbe29c62011-09-27 02:27:58 +0200494void
495change_process_leader(Process * proc, Process * leader)
496{
497 Process ** leaderp = &list_of_processes;
498 if (proc->leader == leader)
499 return;
500
501 assert(leader != NULL);
502 unlist_process(proc);
503 if (proc != leader)
504 leaderp = &leader->next;
505
506 proc->leader = leader;
507 proc->next = *leaderp;
508 *leaderp = proc;
509}
510
Petr Machata2b46cfc2012-02-18 11:17:29 +0100511static enum callback_status
512clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200513{
514 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
515 proc->pid, proc->leader->pid);
516 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100517 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200518}
519
Petr Machata69a03e62011-07-09 11:29:12 +0200520static enum ecb_status
521event_for_proc(Event * event, void * data)
522{
523 if (event->proc == data)
524 return ecb_deque;
525 else
526 return ecb_cont;
527}
528
529static void
530delete_events_for(Process * proc)
531{
532 Event * event;
533 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
534 free(event);
535}
536
Petr Machatacebb8842011-07-09 11:14:11 +0200537void
538remove_process(Process *proc)
539{
Petr Machatacebb8842011-07-09 11:14:11 +0200540 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
541
Petr Machata9a5420c2011-07-09 11:21:23 +0200542 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100543 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200544
Petr Machatacbe29c62011-09-27 02:27:58 +0200545 unlist_process(proc);
546 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100547}
Petr Machata4007d742011-07-09 11:29:42 +0200548
549void
Petr Machata366c2f42012-02-09 19:34:36 +0100550install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200551{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200552 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200553 assert(proc->event_handler == NULL);
554 proc->event_handler = handler;
555}
556
557void
558destroy_event_handler(Process * proc)
559{
Petr Machata366c2f42012-02-09 19:34:36 +0100560 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200561 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200562 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200563 if (handler->destroy != NULL)
564 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200565 free(handler);
566 proc->event_handler = NULL;
567}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100568
569static enum callback_status
570breakpoint_for_symbol(struct library_symbol *libsym, void *data)
571{
572 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200573 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100574
Petr Machatad5e85562012-04-05 15:18:38 +0200575 /* If there is an artificial breakpoint on the same address,
576 * its libsym will be NULL, and we can smuggle our libsym
577 * there. That artificial breakpoint is there presumably for
578 * the callbacks, which we don't touch. If there is a real
579 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200580 * symbols and ignore extra symbol aliases.
581 *
582 * The other direction is more complicated and currently not
583 * supported. If a breakpoint has custom callbacks, it might
584 * be also custom-allocated, and we would really need to swap
585 * the two: delete the one now in the dictionary, swap values
586 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200587 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
588 libsym->enter_addr);
589 if (bp != NULL) {
590 assert(bp->libsym == NULL);
591 bp->libsym = libsym;
592 return CBS_CONT;
593 }
594
595 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200596 if (bp == NULL
597 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
598 fail:
599 free(bp);
600 return CBS_FAIL;
601 }
602 if (proc_add_breakpoint(proc, bp) < 0) {
603 breakpoint_destroy(bp);
604 goto fail;
605 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100606
Petr Machatafa0c5702012-04-13 18:43:40 +0200607 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200608 proc_remove_breakpoint(proc, bp);
609 breakpoint_destroy(bp);
610 goto fail;
611 }
612
Petr Machata2b46cfc2012-02-18 11:17:29 +0100613 return CBS_CONT;
614}
615
616void
617proc_add_library(struct Process *proc, struct library *lib)
618{
619 assert(lib->next == NULL);
620 lib->next = proc->libraries;
621 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200622 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
623 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100624
625 struct library_symbol *libsym = NULL;
626 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100627 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100628 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100629}
630
631int
632proc_remove_library(struct Process *proc, struct library *lib)
633{
634 struct library **libp;
635 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
636 if (*libp == lib) {
637 *libp = lib->next;
638 return 0;
639 }
640 return -1;
641}
642
643struct library *
644proc_each_library(struct Process *proc, struct library *it,
645 enum callback_status (*cb)(struct Process *proc,
646 struct library *lib, void *data),
647 void *data)
648{
649 if (it == NULL)
650 it = proc->libraries;
651
652 while (it != NULL) {
653 struct library *next = it->next;
654
655 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200656 case CBS_FAIL:
657 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100658 case CBS_STOP:
659 return it;
660 case CBS_CONT:
661 break;
662 }
663
664 it = next;
665 }
666
667 return NULL;
668}
Petr Machata52dbfb12012-03-29 16:38:26 +0200669
670int
671proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
672{
Petr Machata52dbfb12012-03-29 16:38:26 +0200673 /* Only the group leader should be getting the breakpoints and
674 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200675 assert(proc->leader != NULL);
676 assert(proc->leader == proc);
677 assert(proc->breakpoints != NULL);
Petr Machata52dbfb12012-03-29 16:38:26 +0200678
Petr Machatafa0c5702012-04-13 18:43:40 +0200679 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200680 proc->pid, breakpoint_name(bp), bp->addr);
681
Petr Machataa2416362012-04-06 02:43:34 +0200682 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200683 * assert, but that's not necessary right now. Read the
684 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200685 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200686
Petr Machatafa0c5702012-04-13 18:43:40 +0200687 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200688 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
689 breakpoint_name(bp), bp->addr);
690 return -1;
691 }
692
Petr Machata52dbfb12012-03-29 16:38:26 +0200693 return 0;
694}
695
696int
697proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
698{
699 /* XXX We can't, really. We are missing dict_remove. */
700 assert(!"Not yet implemented!");
701 abort();
702}
Petr Machatad3cc9882012-04-13 21:40:23 +0200703
704/* Dict doesn't support iteration restarts, so here's this contraption
705 * for now. XXX add restarts to dict. */
706struct each_breakpoint_data
707{
708 void *start;
709 void *end;
710 struct Process *proc;
711 enum callback_status (*cb)(struct Process *proc,
712 struct breakpoint *bp,
713 void *data);
714 void *cb_data;
715};
716
717static void
718each_breakpoint_cb(void *key, void *value, void *d)
719{
720 struct each_breakpoint_data *data = d;
721 if (data->end != NULL)
722 return;
723 if (data->start == key)
724 data->start = NULL;
725
726 if (data->start == NULL) {
727 switch (data->cb(data->proc, value, data->cb_data)) {
728 case CBS_FAIL:
729 /* XXX handle me */
730 case CBS_STOP:
731 data->end = key;
732 case CBS_CONT:
733 return;
734 }
735 }
736}
737
738void *
739proc_each_breakpoint(struct Process *proc, void *start,
740 enum callback_status (*cb)(struct Process *proc,
741 struct breakpoint *bp,
742 void *data), void *data)
743{
744 struct each_breakpoint_data dd = {
745 .start = start,
746 .proc = proc,
747 .cb = cb,
748 .cb_data = data,
749 };
750 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
751 return dd.end;
752}