blob: 7568a753cf474e0582f4393f4f931c173ff22590 [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"
Petr Machatae0973cb2012-04-01 19:36:41 +020019#include "filter.h"
Juan Cespedes273ea6d1998-03-14 23:02:40 +010020
Petr Machata2b46cfc2012-02-18 11:17:29 +010021static int
22process_bare_init(struct Process *proc, const char *filename, pid_t pid)
23{
24 fprintf(stderr, "process_bare_init %s %d\n", filename, pid);
25 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) {
43 proc->breakpoints = dict_init(dict_key2hash_int,
44 dict_key_cmp_int);
45 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{
70 fprintf(stderr, "process_init %s %d enable=%d\n", filename, pid, enable);
71 if (process_bare_init(proc, filename, pid) < 0) {
72 error(0, errno, "init process %d", pid);
73 return -1;
74 }
75
76 if (proc->leader == proc && breakpoints_init(proc, enable) < 0) {
77 fprintf(stderr, "failed to init breakpoints %d\n",
78 proc->pid);
79 process_bare_destroy(proc);
80 return -1;
81 }
82
83 return 0;
84}
85
86struct Process *
87open_program(const char *filename, pid_t pid, int enable)
88{
89 fprintf(stderr, "open_program %s %d enable=%d\n",
90 filename, pid, enable);
91 assert(pid != 0);
92 struct Process *proc = malloc(sizeof(*proc));
93 if (proc == NULL
94 || process_init(proc, filename, pid, enable) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +020095 free(proc);
96 return NULL;
97 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010098 return proc;
99}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100100
Petr Machata2b46cfc2012-02-18 11:17:29 +0100101struct clone_single_bp_data {
102 struct Process *old_proc;
103 struct Process *new_proc;
104 int error;
105};
106
107struct find_symbol_data {
108 struct library_symbol *old_libsym;
109 struct library_symbol *found_libsym;
110};
111
112static enum callback_status
113find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
114{
115 struct find_symbol_data *fs = u;
116 fs->found_libsym
117 = library_each_symbol(lib, NULL, library_symbol_equal_cb,
118 fs->old_libsym);
119 return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
120}
121
122static void
123clone_single_bp(void *key, void *value, void *u)
124{
125 target_address_t addr = (target_address_t)key;
126 struct breakpoint *bp = value;
127 struct clone_single_bp_data *data = u;
128
129 /* Find library and symbol that this symbol was linked to. */
130 struct library_symbol *libsym = bp->libsym;
131 struct library *lib = NULL;
132 if (libsym != NULL) {
133 struct find_symbol_data f_data = {
134 .old_libsym = libsym,
135 };
136 lib = proc_each_library(data->old_proc, NULL,
137 find_sym_in_lib, &f_data);
138 assert(lib != NULL);
139 libsym = f_data.found_libsym;
Petr Machata61196a42012-02-07 16:41:03 +0100140 }
Joe Damatoab3b72c2010-10-31 00:21:53 -0700141
Petr Machata2b46cfc2012-02-18 11:17:29 +0100142 /* LIB and LIBSYM now hold the new library and symbol that
143 * correspond to the original breakpoint. Now we can do the
144 * clone itself. */
145 struct breakpoint *clone = malloc(sizeof(*clone));
146 if (clone == NULL
Petr Machata55ac9322012-03-27 03:07:35 +0200147 || breakpoint_init(clone, data->new_proc, addr, libsym) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100148 data->error = -1;
149 return;
150 }
Petr Machata55ac9322012-03-27 03:07:35 +0200151 breakpoint_set_callbacks(clone, bp->cbs);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100152}
153
154int
155process_clone(struct Process *retp, struct Process *proc, pid_t pid)
156{
157 if (process_bare_init(retp, proc->filename, pid) < 0) {
158 fail:
159 error(0, errno, "clone process %d->%d", proc->pid, pid);
160 return -1;
161 }
162
163 /* For non-leader processes, that's all we need to do. */
164 if (proc->leader != proc)
165 return 0;
166
167 /* Clone symbols first so that we can clone and relink
168 * breakpoints. */
169 struct library *lib;
170 struct library **nlibp = &retp->libraries;
171 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
172 *nlibp = malloc(sizeof(**nlibp));
173 if (*nlibp == NULL
174 || library_clone(*nlibp, lib) < 0) {
175 fail2:
176 process_bare_destroy(retp);
177
178 /* Error when cloning. Unroll what was done. */
179 for (lib = retp->libraries; lib != NULL; ) {
180 struct library *next = lib->next;
181 library_destroy(lib);
182 free(lib);
183 lib = next;
184 }
185 goto fail;
186 }
187
188 nlibp = &(*nlibp)->next;
189 }
190
191 /* Now clone breakpoints. Symbol relinking is done in
192 * clone_single_bp. */
193 struct clone_single_bp_data data = {
194 .old_proc = proc,
195 .new_proc = retp,
196 .error = 0,
197 };
198 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
199
200 if (data.error < 0)
201 goto fail2;
202
203 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100204}
205
Petr Machata3c516d52011-08-18 03:53:18 +0200206static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200207open_one_pid(pid_t pid)
208{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200209 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100210 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200211 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
212
Petr Machata1974dbc2011-08-19 18:58:01 +0200213 /* Get the filename first. Should the trace_pid fail, we can
214 * easily free it, untracing is more work. */
215 if ((filename = pid2name(pid)) == NULL
216 || trace_pid(pid) < 0) {
217 free(filename);
218 return -1;
219 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100220
Petr Machata1974dbc2011-08-19 18:58:01 +0200221 proc = open_program(filename, pid, 0);
222 if (proc == NULL)
223 return -1;
Petr Machata602330f2011-07-09 11:15:34 +0200224 trace_set_options(proc, pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200225
Petr Machata1974dbc2011-08-19 18:58:01 +0200226 return 0;
227}
228
Petr Machata2b46cfc2012-02-18 11:17:29 +0100229static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200230start_one_pid(Process * proc, void * data)
231{
232 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100233 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100234}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100235
Petr Machata9a5420c2011-07-09 11:21:23 +0200236void
237open_pid(pid_t pid)
238{
239 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200240 /* If we are already tracing this guy, we should be seeing all
241 * his children via normal tracing route. */
242 if (pid2proc(pid) != NULL)
243 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200244
Petr Machata3c516d52011-08-18 03:53:18 +0200245 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200246 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200247 fprintf(stderr, "Cannot attach to pid %u: %s\n",
248 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200249 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200250 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200251 }
252
Petr Machata3c516d52011-08-18 03:53:18 +0200253 /* Now attach to all tasks that belong to that PID. There's a
254 * race between process_tasks and open_one_pid. So when we
255 * fail in open_one_pid below, we just do another round.
256 * Chances are that by then that PID will have gone away, and
257 * that's why we have seen the failure. The processes that we
258 * manage to open_one_pid are stopped, so we should eventually
259 * reach a point where process_tasks doesn't give any new
260 * processes (because there's nobody left to produce
261 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200262 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200263 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200264 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200265 pid_t *tasks;
266 size_t ntasks;
267 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200268
Petr Machata3c516d52011-08-18 03:53:18 +0200269 if (process_tasks(pid, &tasks, &ntasks) < 0) {
270 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
271 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100272 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200273 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200274
Petr Machata3c516d52011-08-18 03:53:18 +0200275 have_all = 1;
276 for (i = 0; i < ntasks; ++i)
277 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200278 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200279 have_all = 0;
280
Petr Machata9a5420c2011-07-09 11:21:23 +0200281 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200282
Petr Machata1974dbc2011-08-19 18:58:01 +0200283 if (have_all && old_ntasks == ntasks)
284 break;
285 old_ntasks = ntasks;
286 }
287
288 /* Done. Now initialize breakpoints and then continue
289 * everyone. */
290 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200291 leader = pid2proc(pid)->leader;
292 enable_all_breakpoints(leader);
293
Petr Machata74132a42012-03-16 02:46:18 +0100294 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200295}
296
Petr Machata2b46cfc2012-02-18 11:17:29 +0100297static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200298find_proc(Process * proc, void * data)
299{
300 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100301 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200302}
303
Juan Cespedesa8909f72009-04-28 20:02:41 +0200304Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100305pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200306 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
307}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100308
Petr Machatacebb8842011-07-09 11:14:11 +0200309static Process * list_of_processes = NULL;
310
Petr Machatacbe29c62011-09-27 02:27:58 +0200311static void
312unlist_process(Process * proc)
313{
314 Process *tmp;
315
316 if (list_of_processes == proc) {
317 list_of_processes = list_of_processes->next;
318 return;
319 }
320
321 for (tmp = list_of_processes; ; tmp = tmp->next) {
322 /* If the following assert fails, the process wasn't
323 * in the list. */
324 assert(tmp->next != NULL);
325
326 if (tmp->next == proc) {
327 tmp->next = tmp->next->next;
328 return;
329 }
330 }
331}
332
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100334each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100335 enum callback_status(*cb)(struct Process *proc, void *data),
336 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200337{
Petr Machata74132a42012-03-16 02:46:18 +0100338 struct Process *it = start_after == NULL ? list_of_processes
339 : start_after->next;
340
341 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200342 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100343 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100344 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200345 case CBS_FAIL:
346 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100347 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200348 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100349 case CBS_CONT:
350 break;
351 }
Petr Machatacebb8842011-07-09 11:14:11 +0200352 it = next;
353 }
354 return NULL;
355}
Petr Machata9a5420c2011-07-09 11:21:23 +0200356
357Process *
Petr Machata74132a42012-03-16 02:46:18 +0100358each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100359 enum callback_status(*cb)(struct Process *proc, void *data),
360 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200361{
Petr Machata74132a42012-03-16 02:46:18 +0100362 assert(proc != NULL);
363 struct Process *it = start_after == NULL ? proc->leader
364 : start_after->next;
365
Petr Machata9a5420c2011-07-09 11:21:23 +0200366 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100367 struct Process *leader = it->leader;
368 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200369 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100370 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100371 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200372 case CBS_FAIL:
373 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100374 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200375 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100376 case CBS_CONT:
377 break;
378 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200379 it = next;
380 }
381 }
382 return NULL;
383}
384
Petr Machatacebb8842011-07-09 11:14:11 +0200385void
386add_process(Process * proc)
387{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100388 fprintf(stderr, "add_process %d\n", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200389 Process ** leaderp = &list_of_processes;
390 if (proc->pid) {
391 pid_t tgid = process_leader(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100392 fprintf(stderr, " + leader is %d\n", tgid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200393 if (tgid == 0)
394 /* Must have been terminated before we managed
395 * to fully attach. */
396 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200397 if (tgid == proc->pid)
398 proc->leader = proc;
399 else {
400 Process * leader = pid2proc(tgid);
401 proc->leader = leader;
402 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200403 leaderp = &leader->next;
404 }
405 }
406 proc->next = *leaderp;
407 *leaderp = proc;
408}
409
Petr Machatacbe29c62011-09-27 02:27:58 +0200410void
411change_process_leader(Process * proc, Process * leader)
412{
413 Process ** leaderp = &list_of_processes;
414 if (proc->leader == leader)
415 return;
416
417 assert(leader != NULL);
418 unlist_process(proc);
419 if (proc != leader)
420 leaderp = &leader->next;
421
422 proc->leader = leader;
423 proc->next = *leaderp;
424 *leaderp = proc;
425}
426
Petr Machata2b46cfc2012-02-18 11:17:29 +0100427static enum callback_status
428clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200429{
430 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
431 proc->pid, proc->leader->pid);
432 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100433 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200434}
435
Petr Machata69a03e62011-07-09 11:29:12 +0200436static enum ecb_status
437event_for_proc(Event * event, void * data)
438{
439 if (event->proc == data)
440 return ecb_deque;
441 else
442 return ecb_cont;
443}
444
445static void
446delete_events_for(Process * proc)
447{
448 Event * event;
449 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
450 free(event);
451}
452
Petr Machatacebb8842011-07-09 11:14:11 +0200453void
454remove_process(Process *proc)
455{
Petr Machatacebb8842011-07-09 11:14:11 +0200456 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
457
Petr Machata9a5420c2011-07-09 11:21:23 +0200458 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100459 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200460
Petr Machatacbe29c62011-09-27 02:27:58 +0200461 unlist_process(proc);
462 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100463}
Petr Machata4007d742011-07-09 11:29:42 +0200464
465void
Petr Machata366c2f42012-02-09 19:34:36 +0100466install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200467{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200468 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200469 assert(proc->event_handler == NULL);
470 proc->event_handler = handler;
471}
472
473void
474destroy_event_handler(Process * proc)
475{
Petr Machata366c2f42012-02-09 19:34:36 +0100476 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200477 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200478 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200479 if (handler->destroy != NULL)
480 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200481 free(handler);
482 proc->event_handler = NULL;
483}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100484
485static enum callback_status
486breakpoint_for_symbol(struct library_symbol *libsym, void *data)
487{
488 struct Process *proc = data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100489
Petr Machata3fd099b2012-04-03 02:25:42 +0200490 if (!filter_matches_symbol(options.filter, libsym))
491 return CBS_CONT;
492
493 struct breakpoint *bp = malloc(sizeof(*bp));
494 if (bp == NULL
495 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
496 fail:
497 free(bp);
498 return CBS_FAIL;
499 }
500 if (proc_add_breakpoint(proc, bp) < 0) {
501 breakpoint_destroy(bp);
502 goto fail;
503 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100504
505 return CBS_CONT;
506}
507
508void
509proc_add_library(struct Process *proc, struct library *lib)
510{
511 assert(lib->next == NULL);
512 lib->next = proc->libraries;
513 proc->libraries = lib;
Petr Machata0b55b582012-04-02 00:38:46 +0200514 fprintf(stderr, "=== Added library %s@%p (%s) to %d:\n",
515 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100516
Petr Machatae0973cb2012-04-01 19:36:41 +0200517 if (!filter_matches_library(options.filter, lib))
518 return;
519
Petr Machata2b46cfc2012-02-18 11:17:29 +0100520 struct library_symbol *libsym = NULL;
521 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100522 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100523 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100524}
525
526int
527proc_remove_library(struct Process *proc, struct library *lib)
528{
529 struct library **libp;
530 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
531 if (*libp == lib) {
532 *libp = lib->next;
533 return 0;
534 }
535 return -1;
536}
537
538struct library *
539proc_each_library(struct Process *proc, struct library *it,
540 enum callback_status (*cb)(struct Process *proc,
541 struct library *lib, void *data),
542 void *data)
543{
544 if (it == NULL)
545 it = proc->libraries;
546
547 while (it != NULL) {
548 struct library *next = it->next;
549
550 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200551 case CBS_FAIL:
552 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100553 case CBS_STOP:
554 return it;
555 case CBS_CONT:
556 break;
557 }
558
559 it = next;
560 }
561
562 return NULL;
563}
Petr Machata52dbfb12012-03-29 16:38:26 +0200564
565int
566proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
567{
568 struct Process *leader = proc->leader;
569
570 /* Only the group leader should be getting the breakpoints and
571 * thus have ->breakpoint initialized. */
572 assert(leader != NULL);
573 assert(leader->breakpoints != NULL);
574
575 /* Make sure it wasn't inserted yet. */
576 assert(bp->proc == NULL);
577
578 debug(DEBUG_FUNCTION, "proc_insert_breakpoint(pid=%d, %s@%p)",
579 proc->pid, breakpoint_name(bp), bp->addr);
580
581 assert(dict_find_entry(leader->breakpoints, bp->addr) == NULL);
582 if (dict_enter(leader->breakpoints, bp->addr, bp) < 0) {
583 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
584 breakpoint_name(bp), bp->addr);
585 return -1;
586 }
587
588 bp->proc = proc;
589 return 0;
590}
591
592int
593proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
594{
595 /* XXX We can't, really. We are missing dict_remove. */
596 assert(!"Not yet implemented!");
597 abort();
598}