blob: c50236ba6c7c0cf8e2c086ce7055e6e1b3663267 [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 Machata44965c72012-04-06 19:59:20 +020020static void add_process(struct Process *proc);
21
Petr Machata2b46cfc2012-02-18 11:17:29 +010022static int
23process_bare_init(struct Process *proc, const char *filename, pid_t pid)
24{
Petr Machata2b46cfc2012-02-18 11:17:29 +010025 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020026
Juan Cespedese0660df2009-05-21 18:14:39 +020027 proc->filename = strdup(filename);
Petr Machata2b46cfc2012-02-18 11:17:29 +010028 if (proc->filename == NULL) {
29 fail:
30 free(proc->filename);
31 if (proc->breakpoints != NULL)
32 dict_clear(proc->breakpoints);
33 return -1;
34 }
35
36 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020037 proc->pid = pid;
Petr Machata2b46cfc2012-02-18 11:17:29 +010038 add_process(proc);
39 if (proc->leader == NULL)
40 goto fail;
41
42 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +020043 proc->breakpoints = dict_init(target_address_hash,
44 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +010045 if (proc->breakpoints == NULL)
46 goto fail;
47 } else {
48 proc->breakpoints = NULL;
49 }
50
Joe Damatoab3b72c2010-10-31 00:21:53 -070051#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020052 proc->unwind_priv = _UPT_create(pid);
53 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070054#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070055
Petr Machata2b46cfc2012-02-18 11:17:29 +010056 return 0;
57}
58
59static void
60process_bare_destroy(struct Process *proc)
61{
62 free(proc->filename);
63 dict_clear(proc->breakpoints);
64 remove_process(proc);
65}
66
67int
68process_init(struct Process *proc, const char *filename, pid_t pid, int enable)
69{
Petr Machata2b46cfc2012-02-18 11:17:29 +010070 if (process_bare_init(proc, filename, pid) < 0) {
71 error(0, errno, "init process %d", pid);
72 return -1;
73 }
74
75 if (proc->leader == proc && breakpoints_init(proc, enable) < 0) {
76 fprintf(stderr, "failed to init breakpoints %d\n",
77 proc->pid);
78 process_bare_destroy(proc);
79 return -1;
80 }
81
82 return 0;
83}
84
85struct Process *
86open_program(const char *filename, pid_t pid, int enable)
87{
Petr Machata2b46cfc2012-02-18 11:17:29 +010088 assert(pid != 0);
89 struct Process *proc = malloc(sizeof(*proc));
90 if (proc == NULL
91 || process_init(proc, filename, pid, enable) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +020092 free(proc);
93 return NULL;
94 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010095 return proc;
96}
Juan Cespedes273ea6d1998-03-14 23:02:40 +010097
Petr Machata2b46cfc2012-02-18 11:17:29 +010098struct clone_single_bp_data {
99 struct Process *old_proc;
100 struct Process *new_proc;
101 int error;
102};
103
104struct find_symbol_data {
105 struct library_symbol *old_libsym;
106 struct library_symbol *found_libsym;
107};
108
109static enum callback_status
110find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
111{
112 struct find_symbol_data *fs = u;
113 fs->found_libsym
114 = library_each_symbol(lib, NULL, library_symbol_equal_cb,
115 fs->old_libsym);
116 return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
117}
118
119static void
120clone_single_bp(void *key, void *value, void *u)
121{
122 target_address_t addr = (target_address_t)key;
123 struct breakpoint *bp = value;
124 struct clone_single_bp_data *data = u;
125
126 /* Find library and symbol that this symbol was linked to. */
127 struct library_symbol *libsym = bp->libsym;
128 struct library *lib = NULL;
129 if (libsym != NULL) {
130 struct find_symbol_data f_data = {
131 .old_libsym = libsym,
132 };
133 lib = proc_each_library(data->old_proc, NULL,
134 find_sym_in_lib, &f_data);
135 assert(lib != NULL);
136 libsym = f_data.found_libsym;
Petr Machata61196a42012-02-07 16:41:03 +0100137 }
Joe Damatoab3b72c2010-10-31 00:21:53 -0700138
Petr Machata2b46cfc2012-02-18 11:17:29 +0100139 /* LIB and LIBSYM now hold the new library and symbol that
140 * correspond to the original breakpoint. Now we can do the
141 * clone itself. */
142 struct breakpoint *clone = malloc(sizeof(*clone));
143 if (clone == NULL
Petr Machata55ac9322012-03-27 03:07:35 +0200144 || breakpoint_init(clone, data->new_proc, addr, libsym) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100145 data->error = -1;
146 return;
147 }
Petr Machata55ac9322012-03-27 03:07:35 +0200148 breakpoint_set_callbacks(clone, bp->cbs);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100149}
150
151int
152process_clone(struct Process *retp, struct Process *proc, pid_t pid)
153{
154 if (process_bare_init(retp, proc->filename, pid) < 0) {
155 fail:
156 error(0, errno, "clone process %d->%d", proc->pid, pid);
157 return -1;
158 }
159
Petr Machatacf1679a2012-04-06 19:56:17 +0200160 retp->tracesysgood = proc->tracesysgood;
161
Petr Machata2b46cfc2012-02-18 11:17:29 +0100162 /* For non-leader processes, that's all we need to do. */
163 if (proc->leader != proc)
164 return 0;
165
166 /* Clone symbols first so that we can clone and relink
167 * breakpoints. */
168 struct library *lib;
169 struct library **nlibp = &retp->libraries;
170 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
171 *nlibp = malloc(sizeof(**nlibp));
172 if (*nlibp == NULL
173 || library_clone(*nlibp, lib) < 0) {
174 fail2:
175 process_bare_destroy(retp);
176
177 /* Error when cloning. Unroll what was done. */
178 for (lib = retp->libraries; lib != NULL; ) {
179 struct library *next = lib->next;
180 library_destroy(lib);
181 free(lib);
182 lib = next;
183 }
184 goto fail;
185 }
186
187 nlibp = &(*nlibp)->next;
188 }
189
190 /* Now clone breakpoints. Symbol relinking is done in
191 * clone_single_bp. */
192 struct clone_single_bp_data data = {
193 .old_proc = proc,
194 .new_proc = retp,
195 .error = 0,
196 };
197 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
198
199 if (data.error < 0)
200 goto fail2;
201
202 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100203}
204
Petr Machata3c516d52011-08-18 03:53:18 +0200205static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200206open_one_pid(pid_t pid)
207{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200208 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100209 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200210 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
211
Petr Machata1974dbc2011-08-19 18:58:01 +0200212 /* Get the filename first. Should the trace_pid fail, we can
213 * easily free it, untracing is more work. */
214 if ((filename = pid2name(pid)) == NULL
215 || trace_pid(pid) < 0) {
216 free(filename);
217 return -1;
218 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100219
Petr Machata1974dbc2011-08-19 18:58:01 +0200220 proc = open_program(filename, pid, 0);
221 if (proc == NULL)
222 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200223 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200224
Petr Machata1974dbc2011-08-19 18:58:01 +0200225 return 0;
226}
227
Petr Machata2b46cfc2012-02-18 11:17:29 +0100228static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200229start_one_pid(Process * proc, void * data)
230{
231 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100232 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100233}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100234
Petr Machata9a5420c2011-07-09 11:21:23 +0200235void
236open_pid(pid_t pid)
237{
238 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200239 /* If we are already tracing this guy, we should be seeing all
240 * his children via normal tracing route. */
241 if (pid2proc(pid) != NULL)
242 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200243
Petr Machata3c516d52011-08-18 03:53:18 +0200244 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200245 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200246 fprintf(stderr, "Cannot attach to pid %u: %s\n",
247 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200248 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200249 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200250 }
251
Petr Machata3c516d52011-08-18 03:53:18 +0200252 /* Now attach to all tasks that belong to that PID. There's a
253 * race between process_tasks and open_one_pid. So when we
254 * fail in open_one_pid below, we just do another round.
255 * Chances are that by then that PID will have gone away, and
256 * that's why we have seen the failure. The processes that we
257 * manage to open_one_pid are stopped, so we should eventually
258 * reach a point where process_tasks doesn't give any new
259 * processes (because there's nobody left to produce
260 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200261 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200262 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200263 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200264 pid_t *tasks;
265 size_t ntasks;
266 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200267
Petr Machata3c516d52011-08-18 03:53:18 +0200268 if (process_tasks(pid, &tasks, &ntasks) < 0) {
269 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
270 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100271 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200272 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200273
Petr Machata3c516d52011-08-18 03:53:18 +0200274 have_all = 1;
275 for (i = 0; i < ntasks; ++i)
276 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200277 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200278 have_all = 0;
279
Petr Machata9a5420c2011-07-09 11:21:23 +0200280 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200281
Petr Machata1974dbc2011-08-19 18:58:01 +0200282 if (have_all && old_ntasks == ntasks)
283 break;
284 old_ntasks = ntasks;
285 }
286
287 /* Done. Now initialize breakpoints and then continue
288 * everyone. */
289 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200290 leader = pid2proc(pid)->leader;
291 enable_all_breakpoints(leader);
292
Petr Machata74132a42012-03-16 02:46:18 +0100293 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200294}
295
Petr Machata2b46cfc2012-02-18 11:17:29 +0100296static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200297find_proc(Process * proc, void * data)
298{
299 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100300 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200301}
302
Juan Cespedesa8909f72009-04-28 20:02:41 +0200303Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100304pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200305 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
306}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100307
Petr Machatacebb8842011-07-09 11:14:11 +0200308static Process * list_of_processes = NULL;
309
Petr Machatacbe29c62011-09-27 02:27:58 +0200310static void
311unlist_process(Process * proc)
312{
313 Process *tmp;
314
315 if (list_of_processes == proc) {
316 list_of_processes = list_of_processes->next;
317 return;
318 }
319
320 for (tmp = list_of_processes; ; tmp = tmp->next) {
321 /* If the following assert fails, the process wasn't
322 * in the list. */
323 assert(tmp->next != NULL);
324
325 if (tmp->next == proc) {
326 tmp->next = tmp->next->next;
327 return;
328 }
329 }
330}
331
Petr Machata2b46cfc2012-02-18 11:17:29 +0100332struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100333each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100334 enum callback_status(*cb)(struct Process *proc, void *data),
335 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200336{
Petr Machata74132a42012-03-16 02:46:18 +0100337 struct Process *it = start_after == NULL ? list_of_processes
338 : start_after->next;
339
340 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200341 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100342 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100343 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200344 case CBS_FAIL:
345 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100346 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200347 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100348 case CBS_CONT:
349 break;
350 }
Petr Machatacebb8842011-07-09 11:14:11 +0200351 it = next;
352 }
353 return NULL;
354}
Petr Machata9a5420c2011-07-09 11:21:23 +0200355
356Process *
Petr Machata74132a42012-03-16 02:46:18 +0100357each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100358 enum callback_status(*cb)(struct Process *proc, void *data),
359 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200360{
Petr Machata74132a42012-03-16 02:46:18 +0100361 assert(proc != NULL);
362 struct Process *it = start_after == NULL ? proc->leader
363 : start_after->next;
364
Petr Machata9a5420c2011-07-09 11:21:23 +0200365 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100366 struct Process *leader = it->leader;
367 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200368 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100369 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100370 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200371 case CBS_FAIL:
372 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100373 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200374 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100375 case CBS_CONT:
376 break;
377 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200378 it = next;
379 }
380 }
381 return NULL;
382}
383
Petr Machata44965c72012-04-06 19:59:20 +0200384static void
Petr Machatacebb8842011-07-09 11:14:11 +0200385add_process(Process * proc)
386{
Petr Machata9a5420c2011-07-09 11:21:23 +0200387 Process ** leaderp = &list_of_processes;
388 if (proc->pid) {
389 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200390 if (tgid == 0)
391 /* Must have been terminated before we managed
392 * to fully attach. */
393 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200394 if (tgid == proc->pid)
395 proc->leader = proc;
396 else {
397 Process * leader = pid2proc(tgid);
398 proc->leader = leader;
399 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200400 leaderp = &leader->next;
401 }
402 }
403 proc->next = *leaderp;
404 *leaderp = proc;
405}
406
Petr Machatacbe29c62011-09-27 02:27:58 +0200407void
408change_process_leader(Process * proc, Process * leader)
409{
410 Process ** leaderp = &list_of_processes;
411 if (proc->leader == leader)
412 return;
413
414 assert(leader != NULL);
415 unlist_process(proc);
416 if (proc != leader)
417 leaderp = &leader->next;
418
419 proc->leader = leader;
420 proc->next = *leaderp;
421 *leaderp = proc;
422}
423
Petr Machata2b46cfc2012-02-18 11:17:29 +0100424static enum callback_status
425clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200426{
427 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
428 proc->pid, proc->leader->pid);
429 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100430 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200431}
432
Petr Machata69a03e62011-07-09 11:29:12 +0200433static enum ecb_status
434event_for_proc(Event * event, void * data)
435{
436 if (event->proc == data)
437 return ecb_deque;
438 else
439 return ecb_cont;
440}
441
442static void
443delete_events_for(Process * proc)
444{
445 Event * event;
446 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
447 free(event);
448}
449
Petr Machatacebb8842011-07-09 11:14:11 +0200450void
451remove_process(Process *proc)
452{
Petr Machatacebb8842011-07-09 11:14:11 +0200453 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
454
Petr Machata9a5420c2011-07-09 11:21:23 +0200455 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100456 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200457
Petr Machatacbe29c62011-09-27 02:27:58 +0200458 unlist_process(proc);
459 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100460}
Petr Machata4007d742011-07-09 11:29:42 +0200461
462void
Petr Machata366c2f42012-02-09 19:34:36 +0100463install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200464{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200465 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200466 assert(proc->event_handler == NULL);
467 proc->event_handler = handler;
468}
469
470void
471destroy_event_handler(Process * proc)
472{
Petr Machata366c2f42012-02-09 19:34:36 +0100473 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200474 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200475 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200476 if (handler->destroy != NULL)
477 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200478 free(handler);
479 proc->event_handler = NULL;
480}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100481
482static enum callback_status
483breakpoint_for_symbol(struct library_symbol *libsym, void *data)
484{
485 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200486 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100487
Petr Machatad5e85562012-04-05 15:18:38 +0200488 /* If there is an artificial breakpoint on the same address,
489 * its libsym will be NULL, and we can smuggle our libsym
490 * there. That artificial breakpoint is there presumably for
491 * the callbacks, which we don't touch. If there is a real
492 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200493 * symbols and ignore extra symbol aliases.
494 *
495 * The other direction is more complicated and currently not
496 * supported. If a breakpoint has custom callbacks, it might
497 * be also custom-allocated, and we would really need to swap
498 * the two: delete the one now in the dictionary, swap values
499 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200500 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
501 libsym->enter_addr);
502 if (bp != NULL) {
503 assert(bp->libsym == NULL);
504 bp->libsym = libsym;
505 return CBS_CONT;
506 }
507
508 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200509 if (bp == NULL
510 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
511 fail:
512 free(bp);
513 return CBS_FAIL;
514 }
515 if (proc_add_breakpoint(proc, bp) < 0) {
516 breakpoint_destroy(bp);
517 goto fail;
518 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100519
Petr Machata00928202012-04-07 01:14:24 +0200520 if (breakpoint_turn_on(bp) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200521 proc_remove_breakpoint(proc, bp);
522 breakpoint_destroy(bp);
523 goto fail;
524 }
525
Petr Machata2b46cfc2012-02-18 11:17:29 +0100526 return CBS_CONT;
527}
528
529void
530proc_add_library(struct Process *proc, struct library *lib)
531{
532 assert(lib->next == NULL);
533 lib->next = proc->libraries;
534 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200535 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
536 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100537
538 struct library_symbol *libsym = NULL;
539 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100540 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100541 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100542}
543
544int
545proc_remove_library(struct Process *proc, struct library *lib)
546{
547 struct library **libp;
548 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
549 if (*libp == lib) {
550 *libp = lib->next;
551 return 0;
552 }
553 return -1;
554}
555
556struct library *
557proc_each_library(struct Process *proc, struct library *it,
558 enum callback_status (*cb)(struct Process *proc,
559 struct library *lib, void *data),
560 void *data)
561{
562 if (it == NULL)
563 it = proc->libraries;
564
565 while (it != NULL) {
566 struct library *next = it->next;
567
568 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200569 case CBS_FAIL:
570 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100571 case CBS_STOP:
572 return it;
573 case CBS_CONT:
574 break;
575 }
576
577 it = next;
578 }
579
580 return NULL;
581}
Petr Machata52dbfb12012-03-29 16:38:26 +0200582
583int
584proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
585{
586 struct Process *leader = proc->leader;
587
588 /* Only the group leader should be getting the breakpoints and
589 * thus have ->breakpoint initialized. */
590 assert(leader != NULL);
591 assert(leader->breakpoints != NULL);
592
593 /* Make sure it wasn't inserted yet. */
594 assert(bp->proc == NULL);
595
596 debug(DEBUG_FUNCTION, "proc_insert_breakpoint(pid=%d, %s@%p)",
597 proc->pid, breakpoint_name(bp), bp->addr);
598
Petr Machataa2416362012-04-06 02:43:34 +0200599 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200600 * assert, but that's not necessary right now. Read the
601 * comment in breakpoint_for_symbol. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200602 assert(dict_find_entry(leader->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200603
Petr Machata52dbfb12012-03-29 16:38:26 +0200604 if (dict_enter(leader->breakpoints, bp->addr, bp) < 0) {
605 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
606 breakpoint_name(bp), bp->addr);
607 return -1;
608 }
609
610 bp->proc = proc;
611 return 0;
612}
613
614int
615proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
616{
617 /* XXX We can't, really. We are missing dict_remove. */
618 assert(!"Not yet implemented!");
619 abort();
620}