check return value of unw_get_reg and unw_get_proc_name
[platform/upstream/ltrace.git] / proc.c
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2011,2012,2013 Petr Machata, Red Hat Inc.
4  * Copyright (C) 2010 Joe Damato
5  * Copyright (C) 1998,2009 Juan Cespedes
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22
23 #include "config.h"
24
25 #include <sys/types.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "backend.h"
33 #include "breakpoint.h"
34 #include "debug.h"
35 #include "fetch.h"
36 #include "options.h"
37 #include "proc.h"
38 #include "value_dict.h"
39
40 #ifndef OS_HAVE_PROCESS_DATA
41 int
42 os_process_init(struct process *proc)
43 {
44         return 0;
45 }
46
47 void
48 os_process_destroy(struct process *proc)
49 {
50 }
51
52 int
53 os_process_clone(struct process *retp, struct process *proc)
54 {
55         return 0;
56 }
57
58 int
59 os_process_exec(struct process *proc)
60 {
61         return 0;
62 }
63 #endif
64
65 #ifndef ARCH_HAVE_PROCESS_DATA
66 int
67 arch_process_init(struct process *proc)
68 {
69         return 0;
70 }
71
72 void
73 arch_process_destroy(struct process *proc)
74 {
75 }
76
77 int
78 arch_process_clone(struct process *retp, struct process *proc)
79 {
80         return 0;
81 }
82
83 int
84 arch_process_exec(struct process *proc)
85 {
86         return 0;
87 }
88 #endif
89
90 #ifndef ARCH_HAVE_DYNLINK_DONE
91 void
92 arch_dynlink_done(struct process *proc)
93 {
94 }
95 #endif
96
97 static int add_process(struct process *proc, int was_exec);
98 static void unlist_process(struct process *proc);
99
100 static void
101 destroy_unwind(struct process *proc)
102 {
103 #if defined(HAVE_LIBUNWIND)
104         if (proc->unwind_priv != NULL)
105                 _UPT_destroy(proc->unwind_priv);
106         if (proc->unwind_as != NULL)
107                 unw_destroy_addr_space(proc->unwind_as);
108 #endif /* defined(HAVE_LIBUNWIND) */
109 }
110
111 static int
112 process_bare_init(struct process *proc, const char *filename,
113                   pid_t pid, int was_exec)
114 {
115         if (!was_exec) {
116                 memset(proc, 0, sizeof(*proc));
117
118                 proc->filename = strdup(filename);
119                 if (proc->filename == NULL) {
120                 fail:
121                         free(proc->filename);
122                         if (proc->breakpoints != NULL) {
123                                 dict_destroy(proc->breakpoints,
124                                              NULL, NULL, NULL);
125                                 free(proc->breakpoints);
126                                 proc->breakpoints = NULL;
127                         }
128                         return -1;
129                 }
130         }
131
132         /* Add process so that we know who the leader is.  */
133         proc->pid = pid;
134         if (add_process(proc, was_exec) < 0)
135                 goto fail;
136         if (proc->leader == NULL) {
137         unlist_and_fail:
138                 if (!was_exec)
139                         unlist_process(proc);
140                 goto fail;
141         }
142
143         if (proc->leader == proc) {
144                 proc->breakpoints = malloc(sizeof(*proc->breakpoints));
145                 if (proc->breakpoints == NULL)
146                         goto unlist_and_fail;
147                 DICT_INIT(proc->breakpoints,
148                           arch_addr_t, struct breakpoint *,
149                           arch_addr_hash, arch_addr_eq, NULL);
150         } else {
151                 proc->breakpoints = NULL;
152         }
153
154 #if defined(HAVE_LIBUNWIND)
155         if (options.bt_depth > 0) {
156                 proc->unwind_priv = _UPT_create(pid);
157                 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
158
159                 if (proc->unwind_priv == NULL || proc->unwind_as == NULL) {
160                         fprintf(stderr,
161                                 "Couldn't initialize unwinding "
162                                 "for process %d\n", proc->pid);
163                         destroy_unwind(proc);
164                         proc->unwind_priv = NULL;
165                         proc->unwind_as = NULL;
166                 }
167         }
168 #endif /* defined(HAVE_LIBUNWIND) */
169
170         return 0;
171 }
172
173 static void
174 process_bare_destroy(struct process *proc, int was_exec)
175 {
176         dict_destroy(proc->breakpoints, NULL, NULL, NULL);
177         free(proc->breakpoints);
178         if (!was_exec) {
179                 free(proc->filename);
180                 unlist_process(proc);
181                 destroy_unwind(proc);
182         }
183 }
184
185 static int
186 process_init_main(struct process *proc)
187 {
188         if (breakpoints_init(proc) < 0) {
189                 fprintf(stderr, "failed to init breakpoints %d\n",
190                         proc->pid);
191                 return -1;
192         }
193
194         return 0;
195 }
196
197 int
198 process_init(struct process *proc, const char *filename, pid_t pid)
199 {
200         if (process_bare_init(proc, filename, pid, 0) < 0) {
201         fail:
202                 fprintf(stderr, "failed to initialize process %d: %s\n",
203                         pid, strerror(errno));
204                 return -1;
205         }
206
207         if (os_process_init(proc) < 0) {
208                 process_bare_destroy(proc, 0);
209                 goto fail;
210         }
211
212         if (arch_process_init(proc) < 0) {
213                 os_process_destroy(proc);
214                 process_bare_destroy(proc, 0);
215                 goto fail;
216         }
217
218         if (proc->leader != proc)
219                 return 0;
220         if (process_init_main(proc) < 0) {
221                 process_bare_destroy(proc, 0);
222                 goto fail;
223         }
224         return 0;
225 }
226
227 static enum callback_status
228 destroy_breakpoint_cb(struct process *proc, struct breakpoint *bp, void *data)
229 {
230         breakpoint_destroy(bp);
231         free(bp);
232         return CBS_CONT;
233 }
234
235 // XXX see comment in handle_event.c
236 void callstack_pop(struct process *proc);
237
238 static void
239 private_process_destroy(struct process *proc, int was_exec)
240 {
241         /* Pop remaining stack elements.  */
242         while (proc->callstack_depth > 0) {
243                 /* When this is called just before a process is
244                  * destroyed, the breakpoints should either have been
245                  * retracted by now, or were killed by exec.  In any
246                  * case, it's safe to pretend that there are no
247                  * breakpoints associated with the stack elements, so
248                  * that stack_pop doesn't attempt to destroy them.  */
249                 size_t i = proc->callstack_depth - 1;
250                 if (!proc->callstack[i].is_syscall)
251                         proc->callstack[i].return_addr = 0;
252
253                 callstack_pop(proc);
254         }
255
256         if (!was_exec)
257                 free(proc->filename);
258
259         /* Libraries and symbols.  This is only relevant in
260          * leader.  */
261         struct library *lib;
262         for (lib = proc->libraries; lib != NULL; ) {
263                 struct library *next = lib->next;
264                 library_destroy(lib);
265                 free(lib);
266                 lib = next;
267         }
268         proc->libraries = NULL;
269
270         /* Breakpoints.  */
271         if (proc->breakpoints != NULL) {
272                 proc_each_breakpoint(proc, NULL, destroy_breakpoint_cb, NULL);
273                 dict_destroy(proc->breakpoints, NULL, NULL, NULL);
274                 free(proc->breakpoints);
275                 proc->breakpoints = NULL;
276         }
277
278         destroy_unwind(proc);
279 }
280
281 void
282 process_destroy(struct process *proc)
283 {
284         arch_process_destroy(proc);
285         os_process_destroy(proc);
286         private_process_destroy(proc, 0);
287 }
288
289 int
290 process_exec(struct process *proc)
291 {
292         /* Call exec handlers first, before we destroy the main
293          * state.  */
294         if (arch_process_exec(proc) < 0
295             || os_process_exec(proc) < 0)
296                 return -1;
297
298         private_process_destroy(proc, 1);
299
300         if (process_bare_init(proc, NULL, proc->pid, 1) < 0)
301                 return -1;
302         if (process_init_main(proc) < 0) {
303                 process_bare_destroy(proc, 1);
304                 return -1;
305         }
306         return 0;
307 }
308
309 struct process *
310 open_program(const char *filename, pid_t pid)
311 {
312         assert(pid != 0);
313         struct process *proc = malloc(sizeof(*proc));
314         if (proc == NULL || process_init(proc, filename, pid) < 0) {
315                 free(proc);
316                 return NULL;
317         }
318         return proc;
319 }
320
321 struct clone_single_bp_data {
322         struct process *old_proc;
323         struct process *new_proc;
324 };
325
326 static enum callback_status
327 clone_single_bp(arch_addr_t *key, struct breakpoint **bpp, void *u)
328 {
329         struct breakpoint *bp = *bpp;
330         struct clone_single_bp_data *data = u;
331
332         struct breakpoint *clone = malloc(sizeof(*clone));
333         if (clone == NULL
334             || breakpoint_clone(clone, data->new_proc, bp) < 0) {
335         fail:
336                 free(clone);
337                 return CBS_STOP;
338         }
339         if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
340                 breakpoint_destroy(clone);
341                 goto fail;
342         }
343         return CBS_CONT;
344 }
345
346 int
347 process_clone(struct process *retp, struct process *proc, pid_t pid)
348 {
349         if (process_bare_init(retp, proc->filename, pid, 0) < 0) {
350         fail1:
351                 fprintf(stderr, "Failed to clone process %d to %d: %s\n",
352                         proc->pid, pid, strerror(errno));
353                 return -1;
354         }
355
356         retp->tracesysgood = proc->tracesysgood;
357         retp->e_machine = proc->e_machine;
358         retp->e_class = proc->e_class;
359
360         /* For non-leader processes, that's all we need to do.  */
361         if (retp->leader != retp)
362                 return 0;
363
364         /* Clone symbols first so that we can clone and relink
365          * breakpoints.  */
366         struct library *lib;
367         struct library **nlibp = &retp->libraries;
368         for (lib = proc->leader->libraries; lib != NULL; lib = lib->next) {
369                 *nlibp = malloc(sizeof(**nlibp));
370
371                 if (*nlibp == NULL
372                     || library_clone(*nlibp, lib) < 0) {
373                         free(*nlibp);
374                         *nlibp = NULL;
375
376                 fail2:
377                         process_bare_destroy(retp, 0);
378
379                         /* Error when cloning.  Unroll what was done.  */
380                         for (lib = retp->libraries; lib != NULL; ) {
381                                 struct library *next = lib->next;
382                                 library_destroy(lib);
383                                 free(lib);
384                                 lib = next;
385                         }
386                         goto fail1;
387                 }
388
389                 nlibp = &(*nlibp)->next;
390         }
391
392         /* Now clone breakpoints.  Symbol relinking is done in
393          * clone_single_bp.  */
394         struct clone_single_bp_data data = {
395                 .old_proc = proc,
396                 .new_proc = retp,
397         };
398         if (DICT_EACH(proc->leader->breakpoints,
399                       arch_addr_t, struct breakpoint *, NULL,
400                       clone_single_bp, &data) != NULL)
401                 goto fail2;
402
403         /* And finally the call stack.  */
404         /* XXX clearly the callstack handling should be moved to a
405          * separate module and this whole business extracted to
406          * callstack_clone, or callstack_element_clone.  */
407         memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
408         retp->callstack_depth = proc->callstack_depth;
409
410         size_t i;
411         for (i = 0; i < retp->callstack_depth; ++i) {
412                 struct callstack_element *elem = &retp->callstack[i];
413                 struct fetch_context *ctx = elem->fetch_context;
414                 if (ctx != NULL) {
415                         struct fetch_context *nctx = fetch_arg_clone(retp, ctx);
416                         if (nctx == NULL) {
417                                 size_t j;
418                         fail3:
419                                 for (j = 0; j < i; ++j) {
420                                         nctx = retp->callstack[j].fetch_context;
421                                         fetch_arg_done(nctx);
422                                         elem->fetch_context = NULL;
423                                 }
424                                 goto fail2;
425                         }
426                         elem->fetch_context = nctx;
427                 }
428
429                 if (elem->arguments != NULL) {
430                         struct value_dict *nargs = malloc(sizeof(*nargs));
431                         if (nargs == NULL
432                             || val_dict_clone(nargs, elem->arguments) < 0) {
433                                 size_t j;
434                                 for (j = 0; j < i; ++j) {
435                                         nargs = retp->callstack[j].arguments;
436                                         val_dict_destroy(nargs);
437                                         free(nargs);
438                                         elem->arguments = NULL;
439                                 }
440
441                                 /* Pretend that this round went well,
442                                  * so that fail3 frees I-th
443                                  * fetch_context.  */
444                                 ++i;
445                                 goto fail3;
446                         }
447                         elem->arguments = nargs;
448                 }
449
450                 /* If it's not a syscall, we need to find the
451                  * corresponding library symbol in the cloned
452                  * library.  */
453                 if (!elem->is_syscall && elem->c_un.libfunc != NULL) {
454                         struct library_symbol *libfunc = elem->c_un.libfunc;
455                         int rc = proc_find_symbol(retp, libfunc,
456                                                   NULL, &elem->c_un.libfunc);
457                         assert(rc == 0);
458                 }
459         }
460
461         /* At this point, retp is fully initialized, except for OS and
462          * arch parts, and we can call private_process_destroy.  */
463         if (os_process_clone(retp, proc) < 0) {
464                 private_process_destroy(retp, 0);
465                 return -1;
466         }
467         if (arch_process_clone(retp, proc) < 0) {
468                 os_process_destroy(retp);
469                 private_process_destroy(retp, 0);
470                 return -1;
471         }
472
473         return 0;
474 }
475
476 static int
477 open_one_pid(pid_t pid)
478 {
479         debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
480
481         /* Get the filename first.  Should the trace_pid fail, we can
482          * easily free it, untracing is more work.  */
483         char *filename = pid2name(pid);
484         if (filename == NULL || trace_pid(pid) < 0) {
485         fail:
486                 free(filename);
487                 return -1;
488         }
489
490         struct process *proc = open_program(filename, pid);
491         if (proc == NULL)
492                 goto fail;
493         free(filename);
494         trace_set_options(proc);
495
496         return 0;
497 }
498
499 static enum callback_status
500 start_one_pid(struct process *proc, void *data)
501 {
502         continue_process(proc->pid);
503         return CBS_CONT;
504 }
505
506 static enum callback_status
507 is_main(struct process *proc, struct library *lib, void *data)
508 {
509         return CBS_STOP_IF(lib->type == LT_LIBTYPE_MAIN);
510 }
511
512 void
513 process_hit_start(struct process *proc)
514 {
515         struct process *leader = proc->leader;
516         assert(leader != NULL);
517
518         struct library *mainlib
519                 = proc_each_library(leader, NULL, is_main, NULL);
520         assert(mainlib != NULL);
521         linkmap_init(leader, mainlib->dyn_addr);
522         arch_dynlink_done(leader);
523 }
524
525 void
526 open_pid(pid_t pid)
527 {
528         debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
529         /* If we are already tracing this guy, we should be seeing all
530          * his children via normal tracing route.  */
531         if (pid2proc(pid) != NULL)
532                 return;
533
534         /* First, see if we can attach the requested PID itself.  */
535         if (open_one_pid(pid) < 0) {
536                 fprintf(stderr, "Cannot attach to pid %u: %s\n",
537                         pid, strerror(errno));
538                 trace_fail_warning(pid);
539                 return;
540         }
541
542         /* Now attach to all tasks that belong to that PID.  There's a
543          * race between process_tasks and open_one_pid.  So when we
544          * fail in open_one_pid below, we just do another round.
545          * Chances are that by then that PID will have gone away, and
546          * that's why we have seen the failure.  The processes that we
547          * manage to open_one_pid are stopped, so we should eventually
548          * reach a point where process_tasks doesn't give any new
549          * processes (because there's nobody left to produce
550          * them).  */
551         size_t old_ntasks = 0;
552         int have_all;
553         while (1) {
554                 pid_t *tasks;
555                 size_t ntasks;
556                 size_t i;
557
558                 if (process_tasks(pid, &tasks, &ntasks) < 0) {
559                         fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
560                                 pid, strerror(errno));
561                         break;
562                 }
563
564                 have_all = 1;
565                 for (i = 0; i < ntasks; ++i)
566                         if (pid2proc(tasks[i]) == NULL
567                             && open_one_pid(tasks[i]) < 0)
568                                 have_all = 0;
569
570                 free(tasks);
571
572                 if (have_all && old_ntasks == ntasks)
573                         break;
574                 old_ntasks = ntasks;
575         }
576
577         struct process *leader = pid2proc(pid)->leader;
578
579         /* XXX Is there a way to figure out whether _start has
580          * actually already been hit?  */
581         process_hit_start(leader);
582
583         /* Done.  Continue everyone.  */
584         each_task(leader, NULL, start_one_pid, NULL);
585 }
586
587 static enum callback_status
588 find_proc(struct process *proc, void *data)
589 {
590         return CBS_STOP_IF(proc->pid == (pid_t)(uintptr_t)data);
591 }
592
593 struct process *
594 pid2proc(pid_t pid)
595 {
596         return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
597 }
598
599 static struct process *list_of_processes = NULL;
600
601 static void
602 unlist_process(struct process *proc)
603 {
604         if (list_of_processes == proc) {
605                 list_of_processes = list_of_processes->next;
606                 return;
607         }
608
609         struct process *tmp;
610         for (tmp = list_of_processes; ; tmp = tmp->next) {
611                 /* If the following assert fails, the process wasn't
612                  * in the list.  */
613                 assert(tmp->next != NULL);
614
615                 if (tmp->next == proc) {
616                         tmp->next = tmp->next->next;
617                         return;
618                 }
619         }
620 }
621
622 struct process *
623 each_process(struct process *start_after,
624              enum callback_status(*cb)(struct process *proc, void *data),
625              void *data)
626 {
627         struct process *it = start_after == NULL ? list_of_processes
628                 : start_after->next;
629
630         while (it != NULL) {
631                 /* Callback might call remove_process.  */
632                 struct process *next = it->next;
633                 switch ((*cb)(it, data)) {
634                 case CBS_FAIL:
635                         /* XXX handle me */
636                 case CBS_STOP:
637                         return it;
638                 case CBS_CONT:
639                         break;
640                 }
641                 it = next;
642         }
643         return NULL;
644 }
645
646 struct process *
647 each_task(struct process *proc, struct process *start_after,
648           enum callback_status(*cb)(struct process *proc, void *data),
649           void *data)
650 {
651         assert(proc != NULL);
652         struct process *it = start_after == NULL ? proc->leader
653                 : start_after->next;
654
655         if (it != NULL) {
656                 struct process *leader = it->leader;
657                 while (it != NULL && it->leader == leader) {
658                         /* Callback might call remove_process.  */
659                         struct process *next = it->next;
660                         switch ((*cb)(it, data)) {
661                         case CBS_FAIL:
662                                 /* XXX handle me */
663                         case CBS_STOP:
664                                 return it;
665                         case CBS_CONT:
666                                 break;
667                         }
668                         it = next;
669                 }
670         }
671         return NULL;
672 }
673
674 static int
675 add_process(struct process *proc, int was_exec)
676 {
677         struct process **leaderp = &list_of_processes;
678         if (proc->pid) {
679                 pid_t tgid = process_leader(proc->pid);
680                 if (tgid == 0)
681                         /* Must have been terminated before we managed
682                          * to fully attach.  */
683                         return -1;
684                 if (tgid == proc->pid) {
685                         proc->leader = proc;
686                 } else {
687                         struct process *leader = pid2proc(tgid);
688                         proc->leader = leader;
689                         if (leader != NULL)
690                                 leaderp = &leader->next;
691                 }
692         }
693
694         if (!was_exec) {
695                 proc->next = *leaderp;
696                 *leaderp = proc;
697         }
698         return 0;
699 }
700
701 void
702 change_process_leader(struct process *proc, struct process *leader)
703 {
704         struct process **leaderp = &list_of_processes;
705         if (proc->leader == leader)
706                 return;
707
708         assert(leader != NULL);
709         unlist_process(proc);
710         if (proc != leader)
711                 leaderp = &leader->next;
712
713         proc->leader = leader;
714         proc->next = *leaderp;
715         *leaderp = proc;
716 }
717
718 static enum callback_status
719 clear_leader(struct process *proc, void *data)
720 {
721         debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
722               proc->pid, proc->leader->pid);
723         proc->leader = NULL;
724         return CBS_CONT;
725 }
726
727 void
728 remove_process(struct process *proc)
729 {
730         debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
731
732         if (proc->leader == proc)
733                 each_task(proc, NULL, &clear_leader, NULL);
734
735         unlist_process(proc);
736         process_removed(proc);
737         process_destroy(proc);
738         free(proc);
739 }
740
741 void
742 install_event_handler(struct process *proc, struct event_handler *handler)
743 {
744         debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
745         assert(proc->event_handler == NULL);
746         proc->event_handler = handler;
747 }
748
749 void
750 destroy_event_handler(struct process *proc)
751 {
752         struct event_handler *handler = proc->event_handler;
753         debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
754         assert(handler != NULL);
755         if (handler->destroy != NULL)
756                 handler->destroy(handler);
757         free(handler);
758         proc->event_handler = NULL;
759 }
760
761 static int
762 breakpoint_for_symbol(struct library_symbol *libsym, struct process *proc)
763 {
764         arch_addr_t bp_addr;
765         assert(proc->leader == proc);
766
767         /* Don't enable latent or delayed symbols.  */
768         if (libsym->latent || libsym->delayed) {
769                 debug(DEBUG_FUNCTION,
770                       "delayed and/or latent breakpoint pid=%d, %s@%p",
771                       proc->pid, libsym->name, libsym->enter_addr);
772                 return 0;
773         }
774
775         bp_addr = sym2addr(proc, libsym);
776
777         /* If there is an artificial breakpoint on the same address,
778          * its libsym will be NULL, and we can smuggle our libsym
779          * there.  That artificial breakpoint is there presumably for
780          * the callbacks, which we don't touch.  If there is a real
781          * breakpoint, then this is a bug.  ltrace-elf.c should filter
782          * symbols and ignore extra symbol aliases.
783          *
784          * The other direction is more complicated and currently not
785          * supported.  If a breakpoint has custom callbacks, it might
786          * be also custom-allocated, and we would really need to swap
787          * the two: delete the one now in the dictionary, swap values
788          * around, and put the new breakpoint back in.  */
789         struct breakpoint *bp;
790         if (DICT_FIND_VAL(proc->breakpoints, &bp_addr, &bp) == 0) {
791                 /* MIPS backend makes duplicate requests.  This is
792                  * likely a bug in the backend.  Currently there's no
793                  * point assigning more than one symbol to a
794                  * breakpoint, because when it hits, we won't know
795                  * what to print out.  But it's easier to fix it here
796                  * before someone who understands MIPS has the time to
797                  * look into it.  So turn the sanity check off on
798                  * MIPS.  References:
799                  *
800                  *   http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000764.html
801                  *   http://lists.alioth.debian.org/pipermail/ltrace-devel/2012-November/000770.html
802                  */
803 #ifndef __mips__
804                 assert(bp->libsym == NULL);
805                 bp->libsym = libsym;
806 #endif
807                 return 0;
808         }
809
810         bp = malloc(sizeof(*bp));
811         if (bp == NULL
812             || breakpoint_init(bp, proc, bp_addr, libsym) < 0) {
813         fail:
814                 free(bp);
815                 return -1;
816         }
817         if (proc_add_breakpoint(proc, bp) < 0) {
818                 breakpoint_destroy(bp);
819                 goto fail;
820         }
821
822         if (breakpoint_turn_on(bp, proc) < 0) {
823                 proc_remove_breakpoint(proc, bp);
824                 breakpoint_destroy(bp);
825                 goto fail;
826         }
827
828         return 0;
829 }
830
831 static enum callback_status
832 cb_breakpoint_for_symbol(struct library_symbol *libsym, void *data)
833 {
834         return CBS_STOP_IF(breakpoint_for_symbol(libsym, data) < 0);
835 }
836
837 static int
838 proc_activate_latent_symbol(struct process *proc,
839                             struct library_symbol *libsym)
840 {
841         assert(libsym->latent);
842         libsym->latent = 0;
843         debug(DEBUG_FUNCTION, "activated latent symbol");
844         return breakpoint_for_symbol(libsym, proc);
845 }
846
847 int
848 proc_activate_delayed_symbol(struct process *proc,
849                              struct library_symbol *libsym)
850 {
851         assert(libsym->delayed);
852         libsym->delayed = 0;
853         debug(DEBUG_FUNCTION, "activated delayed symbol");
854         return breakpoint_for_symbol(libsym, proc);
855 }
856
857 static enum callback_status
858 activate_latent_in(struct process *proc, struct library *lib, void *data)
859 {
860         struct library_exported_name *exported;
861         for (exported = data; exported != NULL; exported = exported->next) {
862                 struct library_symbol *libsym = NULL;
863                 while ((libsym = library_each_symbol(lib, libsym,
864                                                      library_symbol_named_cb,
865                                                      (void *)exported->name))
866                        != NULL)
867                         if (libsym->latent
868                             && proc_activate_latent_symbol(proc, libsym) < 0)
869                                 return CBS_FAIL;
870         }
871         return CBS_CONT;
872 }
873
874 void
875 proc_add_library(struct process *proc, struct library *lib)
876 {
877         assert(lib->next == NULL);
878         lib->next = proc->libraries;
879         proc->libraries = lib;
880         debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
881               lib->soname, lib->base, lib->pathname, proc->pid);
882
883         /* Insert breakpoints for all active (non-latent) symbols.  */
884         struct library_symbol *libsym = NULL;
885         while ((libsym = library_each_symbol(lib, libsym,
886                                              cb_breakpoint_for_symbol,
887                                              proc)) != NULL)
888                 fprintf(stderr,
889                         "Couldn't insert breakpoint for %s to %d: %s.\n",
890                         libsym->name, proc->pid, strerror(errno));
891
892         /* Look through export list of the new library and compare it
893          * with latent symbols of all libraries (including this
894          * library itself).  */
895         struct library *lib2 = NULL;
896         while ((lib2 = proc_each_library(proc, lib2, activate_latent_in,
897                                          lib->exported_names)) != NULL)
898                 fprintf(stderr,
899                         "Couldn't activate latent symbols for %s in %d: %s.\n",
900                         lib2->soname, proc->pid, strerror(errno));
901 }
902
903 int
904 proc_remove_library(struct process *proc, struct library *lib)
905 {
906         struct library **libp;
907         for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
908                 if (*libp == lib) {
909                         *libp = lib->next;
910                         return 0;
911                 }
912         return -1;
913 }
914
915 struct library *
916 proc_each_library(struct process *proc, struct library *it,
917                   enum callback_status (*cb)(struct process *proc,
918                                              struct library *lib, void *data),
919                   void *data)
920 {
921         if (it == NULL)
922                 it = proc->libraries;
923         else
924                 it = it->next;
925
926         while (it != NULL) {
927                 struct library *next = it->next;
928
929                 switch (cb(proc, it, data)) {
930                 case CBS_FAIL:
931                         /* XXX handle me */
932                 case CBS_STOP:
933                         return it;
934                 case CBS_CONT:
935                         break;
936                 }
937
938                 it = next;
939         }
940
941         return NULL;
942 }
943
944 static void
945 check_leader(struct process *proc)
946 {
947         /* Only the group leader should be getting the breakpoints and
948          * thus have ->breakpoint initialized.  */
949         assert(proc->leader != NULL);
950         assert(proc->leader == proc);
951         assert(proc->breakpoints != NULL);
952 }
953
954 int
955 proc_add_breakpoint(struct process *proc, struct breakpoint *bp)
956 {
957         debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
958               proc->pid, breakpoint_name(bp), bp->addr);
959         check_leader(proc);
960
961         /* XXX We might merge bp->libsym instead of the following
962          * assert, but that's not necessary right now.  Read the
963          * comment in breakpoint_for_symbol.  */
964         assert(dict_find(proc->breakpoints, &bp->addr) == NULL);
965
966         if (DICT_INSERT(proc->breakpoints, &bp->addr, &bp) < 0) {
967                 fprintf(stderr,
968                         "couldn't enter breakpoint %s@%p to dictionary: %s\n",
969                         breakpoint_name(bp), bp->addr, strerror(errno));
970                 return -1;
971         }
972
973         return 0;
974 }
975
976 void
977 proc_remove_breakpoint(struct process *proc, struct breakpoint *bp)
978 {
979         debug(DEBUG_FUNCTION, "proc_remove_breakpoint(pid=%d, %s@%p)",
980               proc->pid, breakpoint_name(bp), bp->addr);
981         check_leader(proc);
982         int rc = DICT_ERASE(proc->breakpoints, &bp->addr, struct breakpoint *,
983                             NULL, NULL, NULL);
984         assert(rc == 0);
985 }
986
987 struct each_breakpoint_data
988 {
989         struct process *proc;
990         enum callback_status (*cb)(struct process *proc,
991                                    struct breakpoint *bp,
992                                    void *data);
993         void *cb_data;
994 };
995
996 static enum callback_status
997 each_breakpoint_cb(arch_addr_t *key, struct breakpoint **bpp, void *d)
998 {
999         struct each_breakpoint_data *data = d;
1000         return data->cb(data->proc, *bpp, data->cb_data);
1001 }
1002
1003 void *
1004 proc_each_breakpoint(struct process *proc, void *start,
1005                      enum callback_status (*cb)(struct process *proc,
1006                                                 struct breakpoint *bp,
1007                                                 void *data), void *data)
1008 {
1009         struct each_breakpoint_data dd = {
1010                 .proc = proc,
1011                 .cb = cb,
1012                 .cb_data = data,
1013         };
1014         return DICT_EACH(proc->breakpoints,
1015                          arch_addr_t, struct breakpoint *, start,
1016                          &each_breakpoint_cb, &dd);
1017 }
1018
1019 int
1020 proc_find_symbol(struct process *proc, struct library_symbol *sym,
1021                  struct library **retlib, struct library_symbol **retsym)
1022 {
1023         struct library *lib = sym->lib;
1024         assert(lib != NULL);
1025
1026         struct library *flib
1027                 = proc_each_library(proc, NULL, library_with_key_cb, &lib->key);
1028         if (flib == NULL)
1029                 return -1;
1030
1031         struct library_symbol *fsym
1032                 = library_each_symbol(flib, NULL, library_symbol_named_cb,
1033                                       (char *)sym->name);
1034         if (fsym == NULL)
1035                 return -1;
1036
1037         if (retlib != NULL)
1038                 *retlib = flib;
1039         if (retsym != NULL)
1040                 *retsym = fsym;
1041
1042         return 0;
1043 }
1044
1045 struct library_symbol *
1046 proc_each_symbol(struct process *proc, struct library_symbol *start_after,
1047                  enum callback_status (*cb)(struct library_symbol *, void *),
1048                  void *data)
1049 {
1050         struct library *lib;
1051         for (lib = start_after != NULL ? start_after->lib : proc->libraries;
1052              lib != NULL; lib = lib->next) {
1053                 start_after = library_each_symbol(lib, start_after, cb, data);
1054                 if (start_after != NULL)
1055                         return start_after;
1056         }
1057
1058         return NULL;
1059 }
1060
1061 #define DEF_READER(NAME, SIZE)                                          \
1062         int                                                             \
1063         NAME(struct process *proc, arch_addr_t addr,                    \
1064              uint##SIZE##_t *lp)                                        \
1065         {                                                               \
1066                 union {                                                 \
1067                         uint##SIZE##_t dst;                             \
1068                         char buf[0];                                    \
1069                 } u;                                                    \
1070                 if (umovebytes(proc, addr, &u.buf, sizeof(u.dst))       \
1071                     != sizeof(u.dst))                                   \
1072                         return -1;                                      \
1073                 *lp = u.dst;                                            \
1074                 return 0;                                               \
1075         }
1076
1077 DEF_READER(proc_read_8, 8)
1078 DEF_READER(proc_read_16, 16)
1079 DEF_READER(proc_read_32, 32)
1080 DEF_READER(proc_read_64, 64)
1081
1082 #undef DEF_READER