blob: 1d6ff6dab4608b65a78176e5558e8d3f3cb6bf62 [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 Machata3d0c91c2012-04-14 02:37:38 +020020static void add_process(struct Process *proc, int was_exec);
Petr Machata44965c72012-04-06 19:59:20 +020021
Petr Machata2b46cfc2012-02-18 11:17:29 +010022static int
Petr Machata3d0c91c2012-04-14 02:37:38 +020023process_bare_init(struct Process *proc, const char *filename,
24 pid_t pid, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010025{
Petr Machata3d0c91c2012-04-14 02:37:38 +020026 if (!was_exec) {
27 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020028
Petr Machata3d0c91c2012-04-14 02:37:38 +020029 proc->filename = strdup(filename);
30 if (proc->filename == NULL) {
31 fail:
32 free(proc->filename);
33 if (proc->breakpoints != NULL)
34 dict_clear(proc->breakpoints);
35 return -1;
36 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010037 }
38
39 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020040 proc->pid = pid;
Petr Machata3d0c91c2012-04-14 02:37:38 +020041 add_process(proc, was_exec);
Petr Machata2b46cfc2012-02-18 11:17:29 +010042 if (proc->leader == NULL)
43 goto fail;
44
45 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +020046 proc->breakpoints = dict_init(target_address_hash,
47 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +010048 if (proc->breakpoints == NULL)
49 goto fail;
50 } else {
51 proc->breakpoints = NULL;
52 }
53
Joe Damatoab3b72c2010-10-31 00:21:53 -070054#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020055 proc->unwind_priv = _UPT_create(pid);
56 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070057#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070058
Petr Machata2b46cfc2012-02-18 11:17:29 +010059 return 0;
60}
61
62static void
Petr Machata3d0c91c2012-04-14 02:37:38 +020063process_bare_destroy(struct Process *proc, int was_exec)
Petr Machata2b46cfc2012-02-18 11:17:29 +010064{
Petr Machata2b46cfc2012-02-18 11:17:29 +010065 dict_clear(proc->breakpoints);
Petr Machata3d0c91c2012-04-14 02:37:38 +020066 if (!was_exec) {
67 free(proc->filename);
68 remove_process(proc);
69 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010070}
71
Petr Machata3d0c91c2012-04-14 02:37:38 +020072static int
73process_init_main(struct Process *proc)
Petr Machata2b46cfc2012-02-18 11:17:29 +010074{
Petr Machata18bd8ff2012-04-10 04:32:39 +020075 target_address_t entry;
76 target_address_t interp_bias;
77 if (process_get_entry(proc, &entry, &interp_bias) < 0) {
78 fprintf(stderr, "Couldn't get entry points of process %d\n",
Petr Machata2b46cfc2012-02-18 11:17:29 +010079 proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +010080 return -1;
81 }
82
Petr Machata3d0c91c2012-04-14 02:37:38 +020083 if (breakpoints_init(proc) < 0) {
Petr Machata18bd8ff2012-04-10 04:32:39 +020084 fprintf(stderr, "failed to init breakpoints %d\n",
85 proc->pid);
Petr Machata218c5ff2012-04-15 04:22:39 +020086 return -1;
Petr Machata18bd8ff2012-04-10 04:32:39 +020087 }
88
Petr Machata2b46cfc2012-02-18 11:17:29 +010089 return 0;
90}
91
Petr Machata3d0c91c2012-04-14 02:37:38 +020092int
93process_init(struct Process *proc, const char *filename, pid_t pid)
94{
95 if (process_bare_init(proc, filename, pid, 0) < 0) {
Petr Machata218c5ff2012-04-15 04:22:39 +020096 fail:
Petr Machata3d0c91c2012-04-14 02:37:38 +020097 error(0, errno, "init process %d", pid);
98 return -1;
99 }
100
Petr Machata218c5ff2012-04-15 04:22:39 +0200101 if (proc->leader != proc)
Petr Machata3d0c91c2012-04-14 02:37:38 +0200102 return 0;
Petr Machata218c5ff2012-04-15 04:22:39 +0200103 if (process_init_main(proc) < 0) {
104 process_bare_destroy(proc, 0);
105 goto fail;
106 }
107 return 0;
Petr Machata3d0c91c2012-04-14 02:37:38 +0200108}
109
110static void
111destroy_breakpoint_cb(void *key, void *value, void *data)
112{
113 struct breakpoint *bp = value;
114 breakpoint_destroy(bp);
115 free(bp);
116}
117
118static void
119private_process_destroy(struct Process *proc, int keep_filename)
120{
121 if (!keep_filename)
122 free(proc->filename);
123
124 /* Libraries and symbols. */
125 struct library *lib;
126 for (lib = proc->libraries; lib != NULL; ) {
127 struct library *next = lib->next;
128 library_destroy(lib);
129 free(lib);
130 lib = next;
131 }
132 proc->libraries = NULL;
133
134 /* Breakpoints. */
135 dict_apply_to_all(proc->breakpoints, destroy_breakpoint_cb, NULL);
136 dict_clear(proc->breakpoints);
137 proc->breakpoints = NULL;
138}
139
140void
141process_destroy(struct Process *proc)
142{
143 private_process_destroy(proc, 0);
144}
145
146int
147process_exec(struct Process *proc)
148{
149 private_process_destroy(proc, 1);
150 if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
151 return -1;
152 if (process_init_main(proc) < 0) {
153 process_bare_destroy(proc, 1);
154 return -1;
155 }
156 return 0;
157}
158
Petr Machata2b46cfc2012-02-18 11:17:29 +0100159struct Process *
Petr Machata75934ad2012-04-14 02:28:03 +0200160open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100161{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100162 assert(pid != 0);
163 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200164 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200165 free(proc);
166 return NULL;
167 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100168 return proc;
169}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100170
Petr Machata2b46cfc2012-02-18 11:17:29 +0100171struct clone_single_bp_data {
172 struct Process *old_proc;
173 struct Process *new_proc;
174 int error;
175};
176
Petr Machata2b46cfc2012-02-18 11:17:29 +0100177static void
178clone_single_bp(void *key, void *value, void *u)
179{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100180 struct breakpoint *bp = value;
181 struct clone_single_bp_data *data = u;
182
Petr Machatad3cc9882012-04-13 21:40:23 +0200183 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100184 struct breakpoint *clone = malloc(sizeof(*clone));
185 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200186 || breakpoint_clone(clone, data->new_proc,
187 bp, data->old_proc) < 0) {
188 fail:
189 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100190 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100191 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200192 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
193 breakpoint_destroy(clone);
194 goto fail;
195 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100196}
197
198int
199process_clone(struct Process *retp, struct Process *proc, pid_t pid)
200{
Petr Machata3d0c91c2012-04-14 02:37:38 +0200201 if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100202 fail:
203 error(0, errno, "clone process %d->%d", proc->pid, pid);
204 return -1;
205 }
206
Petr Machatacf1679a2012-04-06 19:56:17 +0200207 retp->tracesysgood = proc->tracesysgood;
208
Petr Machata2b46cfc2012-02-18 11:17:29 +0100209 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200210 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100211 return 0;
212
213 /* Clone symbols first so that we can clone and relink
214 * breakpoints. */
215 struct library *lib;
216 struct library **nlibp = &retp->libraries;
217 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
218 *nlibp = malloc(sizeof(**nlibp));
219 if (*nlibp == NULL
220 || library_clone(*nlibp, lib) < 0) {
221 fail2:
Petr Machata3d0c91c2012-04-14 02:37:38 +0200222 process_bare_destroy(retp, 0);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100223
224 /* Error when cloning. Unroll what was done. */
225 for (lib = retp->libraries; lib != NULL; ) {
226 struct library *next = lib->next;
227 library_destroy(lib);
228 free(lib);
229 lib = next;
230 }
231 goto fail;
232 }
233
234 nlibp = &(*nlibp)->next;
235 }
236
237 /* Now clone breakpoints. Symbol relinking is done in
238 * clone_single_bp. */
239 struct clone_single_bp_data data = {
240 .old_proc = proc,
241 .new_proc = retp,
242 .error = 0,
243 };
244 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
245
Petr Machataded6f972012-04-13 23:15:48 +0200246 /* And finally the call stack. */
247 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
248 retp->callstack_depth = proc->callstack_depth;
249
Petr Machata2b46cfc2012-02-18 11:17:29 +0100250 if (data.error < 0)
251 goto fail2;
252
253 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100254}
255
Petr Machata3c516d52011-08-18 03:53:18 +0200256static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200257open_one_pid(pid_t pid)
258{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200259 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100260 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200261 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
262
Petr Machata1974dbc2011-08-19 18:58:01 +0200263 /* Get the filename first. Should the trace_pid fail, we can
264 * easily free it, untracing is more work. */
265 if ((filename = pid2name(pid)) == NULL
266 || trace_pid(pid) < 0) {
267 free(filename);
268 return -1;
269 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100270
Petr Machata75934ad2012-04-14 02:28:03 +0200271 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200272 if (proc == NULL)
273 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200274 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200275
Petr Machata1974dbc2011-08-19 18:58:01 +0200276 return 0;
277}
278
Petr Machata2b46cfc2012-02-18 11:17:29 +0100279static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200280start_one_pid(Process * proc, void * data)
281{
282 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100283 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100284}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100285
Petr Machata9a5420c2011-07-09 11:21:23 +0200286void
287open_pid(pid_t pid)
288{
289 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200290 /* If we are already tracing this guy, we should be seeing all
291 * his children via normal tracing route. */
292 if (pid2proc(pid) != NULL)
293 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200294
Petr Machata3c516d52011-08-18 03:53:18 +0200295 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200296 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200297 fprintf(stderr, "Cannot attach to pid %u: %s\n",
298 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200299 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200300 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200301 }
302
Petr Machata3c516d52011-08-18 03:53:18 +0200303 /* Now attach to all tasks that belong to that PID. There's a
304 * race between process_tasks and open_one_pid. So when we
305 * fail in open_one_pid below, we just do another round.
306 * Chances are that by then that PID will have gone away, and
307 * that's why we have seen the failure. The processes that we
308 * manage to open_one_pid are stopped, so we should eventually
309 * reach a point where process_tasks doesn't give any new
310 * processes (because there's nobody left to produce
311 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200312 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200313 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200314 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200315 pid_t *tasks;
316 size_t ntasks;
317 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200318
Petr Machata3c516d52011-08-18 03:53:18 +0200319 if (process_tasks(pid, &tasks, &ntasks) < 0) {
320 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
321 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100322 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200323 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200324
Petr Machata3c516d52011-08-18 03:53:18 +0200325 have_all = 1;
326 for (i = 0; i < ntasks; ++i)
327 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200328 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200329 have_all = 0;
330
Petr Machata9a5420c2011-07-09 11:21:23 +0200331 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200332
Petr Machata1974dbc2011-08-19 18:58:01 +0200333 if (have_all && old_ntasks == ntasks)
334 break;
335 old_ntasks = ntasks;
336 }
337
338 /* Done. Now initialize breakpoints and then continue
339 * everyone. */
340 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200341 leader = pid2proc(pid)->leader;
342 enable_all_breakpoints(leader);
343
Petr Machata74132a42012-03-16 02:46:18 +0100344 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200345}
346
Petr Machata2b46cfc2012-02-18 11:17:29 +0100347static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200348find_proc(Process * proc, void * data)
349{
350 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100351 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200352}
353
Juan Cespedesa8909f72009-04-28 20:02:41 +0200354Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100355pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200356 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
357}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100358
Petr Machatacebb8842011-07-09 11:14:11 +0200359static Process * list_of_processes = NULL;
360
Petr Machatacbe29c62011-09-27 02:27:58 +0200361static void
362unlist_process(Process * proc)
363{
364 Process *tmp;
365
366 if (list_of_processes == proc) {
367 list_of_processes = list_of_processes->next;
368 return;
369 }
370
371 for (tmp = list_of_processes; ; tmp = tmp->next) {
372 /* If the following assert fails, the process wasn't
373 * in the list. */
374 assert(tmp->next != NULL);
375
376 if (tmp->next == proc) {
377 tmp->next = tmp->next->next;
378 return;
379 }
380 }
381}
382
Petr Machata2b46cfc2012-02-18 11:17:29 +0100383struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100384each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100385 enum callback_status(*cb)(struct Process *proc, void *data),
386 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200387{
Petr Machata74132a42012-03-16 02:46:18 +0100388 struct Process *it = start_after == NULL ? list_of_processes
389 : start_after->next;
390
391 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200392 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100393 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100394 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200395 case CBS_FAIL:
396 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100397 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200398 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100399 case CBS_CONT:
400 break;
401 }
Petr Machatacebb8842011-07-09 11:14:11 +0200402 it = next;
403 }
404 return NULL;
405}
Petr Machata9a5420c2011-07-09 11:21:23 +0200406
407Process *
Petr Machata74132a42012-03-16 02:46:18 +0100408each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100409 enum callback_status(*cb)(struct Process *proc, void *data),
410 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200411{
Petr Machata74132a42012-03-16 02:46:18 +0100412 assert(proc != NULL);
413 struct Process *it = start_after == NULL ? proc->leader
414 : start_after->next;
415
Petr Machata9a5420c2011-07-09 11:21:23 +0200416 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100417 struct Process *leader = it->leader;
418 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200419 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100420 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100421 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200422 case CBS_FAIL:
423 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100424 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200425 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100426 case CBS_CONT:
427 break;
428 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200429 it = next;
430 }
431 }
432 return NULL;
433}
434
Petr Machata44965c72012-04-06 19:59:20 +0200435static void
Petr Machata3d0c91c2012-04-14 02:37:38 +0200436add_process(struct Process *proc, int was_exec)
Petr Machatacebb8842011-07-09 11:14:11 +0200437{
Petr Machata9a5420c2011-07-09 11:21:23 +0200438 Process ** leaderp = &list_of_processes;
439 if (proc->pid) {
440 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200441 if (tgid == 0)
442 /* Must have been terminated before we managed
443 * to fully attach. */
444 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200445 if (tgid == proc->pid)
446 proc->leader = proc;
447 else {
448 Process * leader = pid2proc(tgid);
449 proc->leader = leader;
450 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200451 leaderp = &leader->next;
452 }
453 }
Petr Machata3d0c91c2012-04-14 02:37:38 +0200454
455 if (!was_exec) {
456 proc->next = *leaderp;
457 *leaderp = proc;
458 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200459}
460
Petr Machatacbe29c62011-09-27 02:27:58 +0200461void
462change_process_leader(Process * proc, Process * leader)
463{
464 Process ** leaderp = &list_of_processes;
465 if (proc->leader == leader)
466 return;
467
468 assert(leader != NULL);
469 unlist_process(proc);
470 if (proc != leader)
471 leaderp = &leader->next;
472
473 proc->leader = leader;
474 proc->next = *leaderp;
475 *leaderp = proc;
476}
477
Petr Machata2b46cfc2012-02-18 11:17:29 +0100478static enum callback_status
479clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200480{
481 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
482 proc->pid, proc->leader->pid);
483 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100484 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200485}
486
Petr Machata69a03e62011-07-09 11:29:12 +0200487static enum ecb_status
488event_for_proc(Event * event, void * data)
489{
490 if (event->proc == data)
491 return ecb_deque;
492 else
493 return ecb_cont;
494}
495
496static void
497delete_events_for(Process * proc)
498{
499 Event * event;
500 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
501 free(event);
502}
503
Petr Machatacebb8842011-07-09 11:14:11 +0200504void
505remove_process(Process *proc)
506{
Petr Machatacebb8842011-07-09 11:14:11 +0200507 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
508
Petr Machata9a5420c2011-07-09 11:21:23 +0200509 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100510 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200511
Petr Machatacbe29c62011-09-27 02:27:58 +0200512 unlist_process(proc);
513 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100514}
Petr Machata4007d742011-07-09 11:29:42 +0200515
516void
Petr Machata366c2f42012-02-09 19:34:36 +0100517install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200518{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200519 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200520 assert(proc->event_handler == NULL);
521 proc->event_handler = handler;
522}
523
524void
525destroy_event_handler(Process * proc)
526{
Petr Machata366c2f42012-02-09 19:34:36 +0100527 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200528 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200529 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200530 if (handler->destroy != NULL)
531 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200532 free(handler);
533 proc->event_handler = NULL;
534}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100535
536static enum callback_status
537breakpoint_for_symbol(struct library_symbol *libsym, void *data)
538{
539 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200540 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100541
Petr Machatad5e85562012-04-05 15:18:38 +0200542 /* If there is an artificial breakpoint on the same address,
543 * its libsym will be NULL, and we can smuggle our libsym
544 * there. That artificial breakpoint is there presumably for
545 * the callbacks, which we don't touch. If there is a real
546 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200547 * symbols and ignore extra symbol aliases.
548 *
549 * The other direction is more complicated and currently not
550 * supported. If a breakpoint has custom callbacks, it might
551 * be also custom-allocated, and we would really need to swap
552 * the two: delete the one now in the dictionary, swap values
553 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200554 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
555 libsym->enter_addr);
556 if (bp != NULL) {
557 assert(bp->libsym == NULL);
558 bp->libsym = libsym;
559 return CBS_CONT;
560 }
561
562 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200563 if (bp == NULL
564 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
565 fail:
566 free(bp);
567 return CBS_FAIL;
568 }
569 if (proc_add_breakpoint(proc, bp) < 0) {
570 breakpoint_destroy(bp);
571 goto fail;
572 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100573
Petr Machatafa0c5702012-04-13 18:43:40 +0200574 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200575 proc_remove_breakpoint(proc, bp);
576 breakpoint_destroy(bp);
577 goto fail;
578 }
579
Petr Machata2b46cfc2012-02-18 11:17:29 +0100580 return CBS_CONT;
581}
582
583void
584proc_add_library(struct Process *proc, struct library *lib)
585{
586 assert(lib->next == NULL);
587 lib->next = proc->libraries;
588 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200589 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
590 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100591
592 struct library_symbol *libsym = NULL;
593 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100594 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100595 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100596}
597
598int
599proc_remove_library(struct Process *proc, struct library *lib)
600{
601 struct library **libp;
602 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
603 if (*libp == lib) {
604 *libp = lib->next;
605 return 0;
606 }
607 return -1;
608}
609
610struct library *
611proc_each_library(struct Process *proc, struct library *it,
612 enum callback_status (*cb)(struct Process *proc,
613 struct library *lib, void *data),
614 void *data)
615{
616 if (it == NULL)
617 it = proc->libraries;
618
619 while (it != NULL) {
620 struct library *next = it->next;
621
622 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200623 case CBS_FAIL:
624 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100625 case CBS_STOP:
626 return it;
627 case CBS_CONT:
628 break;
629 }
630
631 it = next;
632 }
633
634 return NULL;
635}
Petr Machata52dbfb12012-03-29 16:38:26 +0200636
637int
638proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
639{
Petr Machata52dbfb12012-03-29 16:38:26 +0200640 /* Only the group leader should be getting the breakpoints and
641 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200642 assert(proc->leader != NULL);
643 assert(proc->leader == proc);
644 assert(proc->breakpoints != NULL);
Petr Machata52dbfb12012-03-29 16:38:26 +0200645
Petr Machatafa0c5702012-04-13 18:43:40 +0200646 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200647 proc->pid, breakpoint_name(bp), bp->addr);
648
Petr Machataa2416362012-04-06 02:43:34 +0200649 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200650 * assert, but that's not necessary right now. Read the
651 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200652 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200653
Petr Machatafa0c5702012-04-13 18:43:40 +0200654 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200655 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
656 breakpoint_name(bp), bp->addr);
657 return -1;
658 }
659
Petr Machata52dbfb12012-03-29 16:38:26 +0200660 return 0;
661}
662
663int
664proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
665{
666 /* XXX We can't, really. We are missing dict_remove. */
667 assert(!"Not yet implemented!");
668 abort();
669}
Petr Machatad3cc9882012-04-13 21:40:23 +0200670
671/* Dict doesn't support iteration restarts, so here's this contraption
672 * for now. XXX add restarts to dict. */
673struct each_breakpoint_data
674{
675 void *start;
676 void *end;
677 struct Process *proc;
678 enum callback_status (*cb)(struct Process *proc,
679 struct breakpoint *bp,
680 void *data);
681 void *cb_data;
682};
683
684static void
685each_breakpoint_cb(void *key, void *value, void *d)
686{
687 struct each_breakpoint_data *data = d;
688 if (data->end != NULL)
689 return;
690 if (data->start == key)
691 data->start = NULL;
692
693 if (data->start == NULL) {
694 switch (data->cb(data->proc, value, data->cb_data)) {
695 case CBS_FAIL:
696 /* XXX handle me */
697 case CBS_STOP:
698 data->end = key;
699 case CBS_CONT:
700 return;
701 }
702 }
703}
704
705void *
706proc_each_breakpoint(struct Process *proc, void *start,
707 enum callback_status (*cb)(struct Process *proc,
708 struct breakpoint *bp,
709 void *data), void *data)
710{
711 struct each_breakpoint_data dd = {
712 .start = start,
713 .proc = proc,
714 .cb = cb,
715 .cb_data = data,
716 };
717 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
718 return dd.end;
719}