blob: 590d01643151f3d5322fe000197e5c3657c9ab56 [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 Machata2b46cfc2012-02-18 11:17:29 +010020static int
21process_bare_init(struct Process *proc, const char *filename, pid_t pid)
22{
Petr Machata2b46cfc2012-02-18 11:17:29 +010023 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020024
Juan Cespedese0660df2009-05-21 18:14:39 +020025 proc->filename = strdup(filename);
Petr Machata2b46cfc2012-02-18 11:17:29 +010026 if (proc->filename == NULL) {
27 fail:
28 free(proc->filename);
29 if (proc->breakpoints != NULL)
30 dict_clear(proc->breakpoints);
31 return -1;
32 }
33
34 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020035 proc->pid = pid;
Petr Machata2b46cfc2012-02-18 11:17:29 +010036 add_process(proc);
37 if (proc->leader == NULL)
38 goto fail;
39
40 if (proc->leader == proc) {
Petr Machataecb082f2012-04-05 02:10:10 +020041 proc->breakpoints = dict_init(target_address_hash,
42 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +010043 if (proc->breakpoints == NULL)
44 goto fail;
45 } else {
46 proc->breakpoints = NULL;
47 }
48
Joe Damatoab3b72c2010-10-31 00:21:53 -070049#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020050 proc->unwind_priv = _UPT_create(pid);
51 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070052#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070053
Petr Machata2b46cfc2012-02-18 11:17:29 +010054 return 0;
55}
56
57static void
58process_bare_destroy(struct Process *proc)
59{
60 free(proc->filename);
61 dict_clear(proc->breakpoints);
62 remove_process(proc);
63}
64
65int
66process_init(struct Process *proc, const char *filename, pid_t pid, int enable)
67{
Petr Machata2b46cfc2012-02-18 11:17:29 +010068 if (process_bare_init(proc, filename, pid) < 0) {
69 error(0, errno, "init process %d", pid);
70 return -1;
71 }
72
73 if (proc->leader == proc && breakpoints_init(proc, enable) < 0) {
74 fprintf(stderr, "failed to init breakpoints %d\n",
75 proc->pid);
76 process_bare_destroy(proc);
77 return -1;
78 }
79
80 return 0;
81}
82
83struct Process *
84open_program(const char *filename, pid_t pid, int enable)
85{
Petr Machata2b46cfc2012-02-18 11:17:29 +010086 assert(pid != 0);
87 struct Process *proc = malloc(sizeof(*proc));
88 if (proc == NULL
89 || process_init(proc, filename, pid, enable) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +020090 free(proc);
91 return NULL;
92 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010093 return proc;
94}
Juan Cespedes273ea6d1998-03-14 23:02:40 +010095
Petr Machata2b46cfc2012-02-18 11:17:29 +010096struct clone_single_bp_data {
97 struct Process *old_proc;
98 struct Process *new_proc;
99 int error;
100};
101
102struct find_symbol_data {
103 struct library_symbol *old_libsym;
104 struct library_symbol *found_libsym;
105};
106
107static enum callback_status
108find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
109{
110 struct find_symbol_data *fs = u;
111 fs->found_libsym
112 = library_each_symbol(lib, NULL, library_symbol_equal_cb,
113 fs->old_libsym);
114 return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
115}
116
117static void
118clone_single_bp(void *key, void *value, void *u)
119{
120 target_address_t addr = (target_address_t)key;
121 struct breakpoint *bp = value;
122 struct clone_single_bp_data *data = u;
123
124 /* Find library and symbol that this symbol was linked to. */
125 struct library_symbol *libsym = bp->libsym;
126 struct library *lib = NULL;
127 if (libsym != NULL) {
128 struct find_symbol_data f_data = {
129 .old_libsym = libsym,
130 };
131 lib = proc_each_library(data->old_proc, NULL,
132 find_sym_in_lib, &f_data);
133 assert(lib != NULL);
134 libsym = f_data.found_libsym;
Petr Machata61196a42012-02-07 16:41:03 +0100135 }
Joe Damatoab3b72c2010-10-31 00:21:53 -0700136
Petr Machata2b46cfc2012-02-18 11:17:29 +0100137 /* LIB and LIBSYM now hold the new library and symbol that
138 * correspond to the original breakpoint. Now we can do the
139 * clone itself. */
140 struct breakpoint *clone = malloc(sizeof(*clone));
141 if (clone == NULL
Petr Machata55ac9322012-03-27 03:07:35 +0200142 || breakpoint_init(clone, data->new_proc, addr, libsym) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100143 data->error = -1;
144 return;
145 }
Petr Machata55ac9322012-03-27 03:07:35 +0200146 breakpoint_set_callbacks(clone, bp->cbs);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100147}
148
149int
150process_clone(struct Process *retp, struct Process *proc, pid_t pid)
151{
152 if (process_bare_init(retp, proc->filename, pid) < 0) {
153 fail:
154 error(0, errno, "clone process %d->%d", proc->pid, pid);
155 return -1;
156 }
157
Petr Machatacf1679a2012-04-06 19:56:17 +0200158 retp->tracesysgood = proc->tracesysgood;
159
Petr Machata2b46cfc2012-02-18 11:17:29 +0100160 /* For non-leader processes, that's all we need to do. */
161 if (proc->leader != proc)
162 return 0;
163
164 /* Clone symbols first so that we can clone and relink
165 * breakpoints. */
166 struct library *lib;
167 struct library **nlibp = &retp->libraries;
168 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
169 *nlibp = malloc(sizeof(**nlibp));
170 if (*nlibp == NULL
171 || library_clone(*nlibp, lib) < 0) {
172 fail2:
173 process_bare_destroy(retp);
174
175 /* Error when cloning. Unroll what was done. */
176 for (lib = retp->libraries; lib != NULL; ) {
177 struct library *next = lib->next;
178 library_destroy(lib);
179 free(lib);
180 lib = next;
181 }
182 goto fail;
183 }
184
185 nlibp = &(*nlibp)->next;
186 }
187
188 /* Now clone breakpoints. Symbol relinking is done in
189 * clone_single_bp. */
190 struct clone_single_bp_data data = {
191 .old_proc = proc,
192 .new_proc = retp,
193 .error = 0,
194 };
195 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
196
197 if (data.error < 0)
198 goto fail2;
199
200 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100201}
202
Petr Machata3c516d52011-08-18 03:53:18 +0200203static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200204open_one_pid(pid_t pid)
205{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200206 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100207 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200208 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
209
Petr Machata1974dbc2011-08-19 18:58:01 +0200210 /* Get the filename first. Should the trace_pid fail, we can
211 * easily free it, untracing is more work. */
212 if ((filename = pid2name(pid)) == NULL
213 || trace_pid(pid) < 0) {
214 free(filename);
215 return -1;
216 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100217
Petr Machata1974dbc2011-08-19 18:58:01 +0200218 proc = open_program(filename, pid, 0);
219 if (proc == NULL)
220 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200221 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200222
Petr Machata1974dbc2011-08-19 18:58:01 +0200223 return 0;
224}
225
Petr Machata2b46cfc2012-02-18 11:17:29 +0100226static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200227start_one_pid(Process * proc, void * data)
228{
229 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100230 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100231}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100232
Petr Machata9a5420c2011-07-09 11:21:23 +0200233void
234open_pid(pid_t pid)
235{
236 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200237 /* If we are already tracing this guy, we should be seeing all
238 * his children via normal tracing route. */
239 if (pid2proc(pid) != NULL)
240 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200241
Petr Machata3c516d52011-08-18 03:53:18 +0200242 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200243 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200244 fprintf(stderr, "Cannot attach to pid %u: %s\n",
245 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200246 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200247 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200248 }
249
Petr Machata3c516d52011-08-18 03:53:18 +0200250 /* Now attach to all tasks that belong to that PID. There's a
251 * race between process_tasks and open_one_pid. So when we
252 * fail in open_one_pid below, we just do another round.
253 * Chances are that by then that PID will have gone away, and
254 * that's why we have seen the failure. The processes that we
255 * manage to open_one_pid are stopped, so we should eventually
256 * reach a point where process_tasks doesn't give any new
257 * processes (because there's nobody left to produce
258 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200259 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200260 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200261 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200262 pid_t *tasks;
263 size_t ntasks;
264 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200265
Petr Machata3c516d52011-08-18 03:53:18 +0200266 if (process_tasks(pid, &tasks, &ntasks) < 0) {
267 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
268 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100269 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200270 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200271
Petr Machata3c516d52011-08-18 03:53:18 +0200272 have_all = 1;
273 for (i = 0; i < ntasks; ++i)
274 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200275 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200276 have_all = 0;
277
Petr Machata9a5420c2011-07-09 11:21:23 +0200278 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200279
Petr Machata1974dbc2011-08-19 18:58:01 +0200280 if (have_all && old_ntasks == ntasks)
281 break;
282 old_ntasks = ntasks;
283 }
284
285 /* Done. Now initialize breakpoints and then continue
286 * everyone. */
287 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200288 leader = pid2proc(pid)->leader;
289 enable_all_breakpoints(leader);
290
Petr Machata74132a42012-03-16 02:46:18 +0100291 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200292}
293
Petr Machata2b46cfc2012-02-18 11:17:29 +0100294static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200295find_proc(Process * proc, void * data)
296{
297 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100298 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200299}
300
Juan Cespedesa8909f72009-04-28 20:02:41 +0200301Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100302pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200303 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
304}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100305
Petr Machatacebb8842011-07-09 11:14:11 +0200306static Process * list_of_processes = NULL;
307
Petr Machatacbe29c62011-09-27 02:27:58 +0200308static void
309unlist_process(Process * proc)
310{
311 Process *tmp;
312
313 if (list_of_processes == proc) {
314 list_of_processes = list_of_processes->next;
315 return;
316 }
317
318 for (tmp = list_of_processes; ; tmp = tmp->next) {
319 /* If the following assert fails, the process wasn't
320 * in the list. */
321 assert(tmp->next != NULL);
322
323 if (tmp->next == proc) {
324 tmp->next = tmp->next->next;
325 return;
326 }
327 }
328}
329
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100331each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100332 enum callback_status(*cb)(struct Process *proc, void *data),
333 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200334{
Petr Machata74132a42012-03-16 02:46:18 +0100335 struct Process *it = start_after == NULL ? list_of_processes
336 : start_after->next;
337
338 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200339 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100340 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100341 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200342 case CBS_FAIL:
343 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100344 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200345 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100346 case CBS_CONT:
347 break;
348 }
Petr Machatacebb8842011-07-09 11:14:11 +0200349 it = next;
350 }
351 return NULL;
352}
Petr Machata9a5420c2011-07-09 11:21:23 +0200353
354Process *
Petr Machata74132a42012-03-16 02:46:18 +0100355each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100356 enum callback_status(*cb)(struct Process *proc, void *data),
357 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200358{
Petr Machata74132a42012-03-16 02:46:18 +0100359 assert(proc != NULL);
360 struct Process *it = start_after == NULL ? proc->leader
361 : start_after->next;
362
Petr Machata9a5420c2011-07-09 11:21:23 +0200363 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100364 struct Process *leader = it->leader;
365 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200366 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100367 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100368 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200369 case CBS_FAIL:
370 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100371 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200372 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100373 case CBS_CONT:
374 break;
375 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200376 it = next;
377 }
378 }
379 return NULL;
380}
381
Petr Machatacebb8842011-07-09 11:14:11 +0200382void
383add_process(Process * proc)
384{
Petr Machata9a5420c2011-07-09 11:21:23 +0200385 Process ** leaderp = &list_of_processes;
386 if (proc->pid) {
387 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200388 if (tgid == 0)
389 /* Must have been terminated before we managed
390 * to fully attach. */
391 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200392 if (tgid == proc->pid)
393 proc->leader = proc;
394 else {
395 Process * leader = pid2proc(tgid);
396 proc->leader = leader;
397 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200398 leaderp = &leader->next;
399 }
400 }
401 proc->next = *leaderp;
402 *leaderp = proc;
403}
404
Petr Machatacbe29c62011-09-27 02:27:58 +0200405void
406change_process_leader(Process * proc, Process * leader)
407{
408 Process ** leaderp = &list_of_processes;
409 if (proc->leader == leader)
410 return;
411
412 assert(leader != NULL);
413 unlist_process(proc);
414 if (proc != leader)
415 leaderp = &leader->next;
416
417 proc->leader = leader;
418 proc->next = *leaderp;
419 *leaderp = proc;
420}
421
Petr Machata2b46cfc2012-02-18 11:17:29 +0100422static enum callback_status
423clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200424{
425 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
426 proc->pid, proc->leader->pid);
427 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100428 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200429}
430
Petr Machata69a03e62011-07-09 11:29:12 +0200431static enum ecb_status
432event_for_proc(Event * event, void * data)
433{
434 if (event->proc == data)
435 return ecb_deque;
436 else
437 return ecb_cont;
438}
439
440static void
441delete_events_for(Process * proc)
442{
443 Event * event;
444 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
445 free(event);
446}
447
Petr Machatacebb8842011-07-09 11:14:11 +0200448void
449remove_process(Process *proc)
450{
Petr Machatacebb8842011-07-09 11:14:11 +0200451 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
452
Petr Machata9a5420c2011-07-09 11:21:23 +0200453 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100454 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200455
Petr Machatacbe29c62011-09-27 02:27:58 +0200456 unlist_process(proc);
457 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100458}
Petr Machata4007d742011-07-09 11:29:42 +0200459
460void
Petr Machata366c2f42012-02-09 19:34:36 +0100461install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200462{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200463 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200464 assert(proc->event_handler == NULL);
465 proc->event_handler = handler;
466}
467
468void
469destroy_event_handler(Process * proc)
470{
Petr Machata366c2f42012-02-09 19:34:36 +0100471 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200472 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200473 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200474 if (handler->destroy != NULL)
475 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200476 free(handler);
477 proc->event_handler = NULL;
478}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100479
480static enum callback_status
481breakpoint_for_symbol(struct library_symbol *libsym, void *data)
482{
483 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200484 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100485
Petr Machatad5e85562012-04-05 15:18:38 +0200486 /* If there is an artificial breakpoint on the same address,
487 * its libsym will be NULL, and we can smuggle our libsym
488 * there. That artificial breakpoint is there presumably for
489 * the callbacks, which we don't touch. If there is a real
490 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200491 * symbols and ignore extra symbol aliases.
492 *
493 * The other direction is more complicated and currently not
494 * supported. If a breakpoint has custom callbacks, it might
495 * be also custom-allocated, and we would really need to swap
496 * the two: delete the one now in the dictionary, swap values
497 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200498 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
499 libsym->enter_addr);
500 if (bp != NULL) {
501 assert(bp->libsym == NULL);
502 bp->libsym = libsym;
503 return CBS_CONT;
504 }
505
506 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200507 if (bp == NULL
508 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
509 fail:
510 free(bp);
511 return CBS_FAIL;
512 }
513 if (proc_add_breakpoint(proc, bp) < 0) {
514 breakpoint_destroy(bp);
515 goto fail;
516 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100517
Petr Machata76dd9292012-04-03 13:02:06 +0200518 /* If this is dlopened library, turn on the breakpoint right
519 * away. */
520 if (proc->fixed_libs != NULL
521 && breakpoint_turn_on(bp) < 0) {
522 proc_remove_breakpoint(proc, bp);
523 breakpoint_destroy(bp);
524 goto fail;
525 }
526
Petr Machata2b46cfc2012-02-18 11:17:29 +0100527 return CBS_CONT;
528}
529
530void
531proc_add_library(struct Process *proc, struct library *lib)
532{
533 assert(lib->next == NULL);
534 lib->next = proc->libraries;
535 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200536 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
537 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100538
539 struct library_symbol *libsym = NULL;
540 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100541 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100542 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100543}
544
545int
546proc_remove_library(struct Process *proc, struct library *lib)
547{
548 struct library **libp;
549 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
550 if (*libp == lib) {
551 *libp = lib->next;
552 return 0;
553 }
554 return -1;
555}
556
557struct library *
558proc_each_library(struct Process *proc, struct library *it,
559 enum callback_status (*cb)(struct Process *proc,
560 struct library *lib, void *data),
561 void *data)
562{
563 if (it == NULL)
564 it = proc->libraries;
565
566 while (it != NULL) {
567 struct library *next = it->next;
568
569 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200570 case CBS_FAIL:
571 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100572 case CBS_STOP:
573 return it;
574 case CBS_CONT:
575 break;
576 }
577
578 it = next;
579 }
580
581 return NULL;
582}
Petr Machata52dbfb12012-03-29 16:38:26 +0200583
584int
585proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
586{
587 struct Process *leader = proc->leader;
588
589 /* Only the group leader should be getting the breakpoints and
590 * thus have ->breakpoint initialized. */
591 assert(leader != NULL);
592 assert(leader->breakpoints != NULL);
593
594 /* Make sure it wasn't inserted yet. */
595 assert(bp->proc == NULL);
596
597 debug(DEBUG_FUNCTION, "proc_insert_breakpoint(pid=%d, %s@%p)",
598 proc->pid, breakpoint_name(bp), bp->addr);
599
Petr Machataa2416362012-04-06 02:43:34 +0200600 /* XXX We might merge bp->libsym instead of the following
601 * assert, but that's not necessary right now. Look into
602 * breakpoint_for_symbol. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200603 assert(dict_find_entry(leader->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200604
Petr Machata52dbfb12012-03-29 16:38:26 +0200605 if (dict_enter(leader->breakpoints, bp->addr, bp) < 0) {
606 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
607 breakpoint_name(bp), bp->addr);
608 return -1;
609 }
610
611 bp->proc = proc;
612 return 0;
613}
614
615int
616proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
617{
618 /* XXX We can't, really. We are missing dict_remove. */
619 assert(!"Not yet implemented!");
620 abort();
621}