blob: 46d7b7c7b98d770b65c3dc8ecd1351f24894d097 [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{
23 fprintf(stderr, "process_bare_init %s %d\n", filename, pid);
24 memset(proc, 0, sizeof(*proc));
Petr Machata1974dbc2011-08-19 18:58:01 +020025
Juan Cespedese0660df2009-05-21 18:14:39 +020026 proc->filename = strdup(filename);
Petr Machata2b46cfc2012-02-18 11:17:29 +010027 if (proc->filename == NULL) {
28 fail:
29 free(proc->filename);
30 if (proc->breakpoints != NULL)
31 dict_clear(proc->breakpoints);
32 return -1;
33 }
34
35 /* Add process so that we know who the leader is. */
Petr Machata1b17dbf2011-07-08 19:22:52 +020036 proc->pid = pid;
Petr Machata2b46cfc2012-02-18 11:17:29 +010037 add_process(proc);
38 if (proc->leader == NULL)
39 goto fail;
40
41 if (proc->leader == proc) {
42 proc->breakpoints = dict_init(dict_key2hash_int,
43 dict_key_cmp_int);
44 if (proc->breakpoints == NULL)
45 goto fail;
46 } else {
47 proc->breakpoints = NULL;
48 }
49
Joe Damatoab3b72c2010-10-31 00:21:53 -070050#if defined(HAVE_LIBUNWIND)
Petr Machata1b17dbf2011-07-08 19:22:52 +020051 proc->unwind_priv = _UPT_create(pid);
52 proc->unwind_as = unw_create_addr_space(&_UPT_accessors, 0);
Joe Damatoab3b72c2010-10-31 00:21:53 -070053#endif /* defined(HAVE_LIBUNWIND) */
Joe Damatoab3b72c2010-10-31 00:21:53 -070054
Petr Machata2b46cfc2012-02-18 11:17:29 +010055 return 0;
56}
57
58static void
59process_bare_destroy(struct Process *proc)
60{
61 free(proc->filename);
62 dict_clear(proc->breakpoints);
63 remove_process(proc);
64}
65
66int
67process_init(struct Process *proc, const char *filename, pid_t pid, int enable)
68{
69 fprintf(stderr, "process_init %s %d enable=%d\n", filename, pid, enable);
70 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{
88 fprintf(stderr, "open_program %s %d enable=%d\n",
89 filename, pid, enable);
90 assert(pid != 0);
91 struct Process *proc = malloc(sizeof(*proc));
92 if (proc == NULL
93 || process_init(proc, filename, pid, enable) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +020094 free(proc);
95 return NULL;
96 }
Petr Machata2b46cfc2012-02-18 11:17:29 +010097 return proc;
98}
Juan Cespedes273ea6d1998-03-14 23:02:40 +010099
Petr Machata2b46cfc2012-02-18 11:17:29 +0100100struct clone_single_bp_data {
101 struct Process *old_proc;
102 struct Process *new_proc;
103 int error;
104};
105
106struct find_symbol_data {
107 struct library_symbol *old_libsym;
108 struct library_symbol *found_libsym;
109};
110
111static enum callback_status
112find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
113{
114 struct find_symbol_data *fs = u;
115 fs->found_libsym
116 = library_each_symbol(lib, NULL, library_symbol_equal_cb,
117 fs->old_libsym);
118 return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
119}
120
121static void
122clone_single_bp(void *key, void *value, void *u)
123{
124 target_address_t addr = (target_address_t)key;
125 struct breakpoint *bp = value;
126 struct clone_single_bp_data *data = u;
127
128 /* Find library and symbol that this symbol was linked to. */
129 struct library_symbol *libsym = bp->libsym;
130 struct library *lib = NULL;
131 if (libsym != NULL) {
132 struct find_symbol_data f_data = {
133 .old_libsym = libsym,
134 };
135 lib = proc_each_library(data->old_proc, NULL,
136 find_sym_in_lib, &f_data);
137 assert(lib != NULL);
138 libsym = f_data.found_libsym;
Petr Machata61196a42012-02-07 16:41:03 +0100139 }
Joe Damatoab3b72c2010-10-31 00:21:53 -0700140
Petr Machata2b46cfc2012-02-18 11:17:29 +0100141 /* LIB and LIBSYM now hold the new library and symbol that
142 * correspond to the original breakpoint. Now we can do the
143 * clone itself. */
144 struct breakpoint *clone = malloc(sizeof(*clone));
145 if (clone == NULL
Petr Machata55ac9322012-03-27 03:07:35 +0200146 || breakpoint_init(clone, data->new_proc, addr, libsym) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100147 data->error = -1;
148 return;
149 }
Petr Machata55ac9322012-03-27 03:07:35 +0200150 breakpoint_set_callbacks(clone, bp->cbs);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100151}
152
153int
154process_clone(struct Process *retp, struct Process *proc, pid_t pid)
155{
156 if (process_bare_init(retp, proc->filename, pid) < 0) {
157 fail:
158 error(0, errno, "clone process %d->%d", proc->pid, pid);
159 return -1;
160 }
161
162 /* 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 Machata602330f2011-07-09 11:15:34 +0200223 trace_set_options(proc, pid);
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 Machatacebb8842011-07-09 11:14:11 +0200384void
385add_process(Process * proc)
386{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100387 fprintf(stderr, "add_process %d\n", proc->pid);
Petr Machata9a5420c2011-07-09 11:21:23 +0200388 Process ** leaderp = &list_of_processes;
389 if (proc->pid) {
390 pid_t tgid = process_leader(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100391 fprintf(stderr, " + leader is %d\n", tgid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200392 if (tgid == 0)
393 /* Must have been terminated before we managed
394 * to fully attach. */
395 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200396 if (tgid == proc->pid)
397 proc->leader = proc;
398 else {
399 Process * leader = pid2proc(tgid);
400 proc->leader = leader;
401 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200402 leaderp = &leader->next;
403 }
404 }
405 proc->next = *leaderp;
406 *leaderp = proc;
407}
408
Petr Machatacbe29c62011-09-27 02:27:58 +0200409void
410change_process_leader(Process * proc, Process * leader)
411{
412 Process ** leaderp = &list_of_processes;
413 if (proc->leader == leader)
414 return;
415
416 assert(leader != NULL);
417 unlist_process(proc);
418 if (proc != leader)
419 leaderp = &leader->next;
420
421 proc->leader = leader;
422 proc->next = *leaderp;
423 *leaderp = proc;
424}
425
Petr Machata2b46cfc2012-02-18 11:17:29 +0100426static enum callback_status
427clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200428{
429 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
430 proc->pid, proc->leader->pid);
431 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100432 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200433}
434
Petr Machata69a03e62011-07-09 11:29:12 +0200435static enum ecb_status
436event_for_proc(Event * event, void * data)
437{
438 if (event->proc == data)
439 return ecb_deque;
440 else
441 return ecb_cont;
442}
443
444static void
445delete_events_for(Process * proc)
446{
447 Event * event;
448 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
449 free(event);
450}
451
Petr Machatacebb8842011-07-09 11:14:11 +0200452void
453remove_process(Process *proc)
454{
Petr Machatacebb8842011-07-09 11:14:11 +0200455 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
456
Petr Machata9a5420c2011-07-09 11:21:23 +0200457 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100458 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200459
Petr Machatacbe29c62011-09-27 02:27:58 +0200460 unlist_process(proc);
461 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100462}
Petr Machata4007d742011-07-09 11:29:42 +0200463
464void
Petr Machata366c2f42012-02-09 19:34:36 +0100465install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200466{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200467 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200468 assert(proc->event_handler == NULL);
469 proc->event_handler = handler;
470}
471
472void
473destroy_event_handler(Process * proc)
474{
Petr Machata366c2f42012-02-09 19:34:36 +0100475 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200476 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200477 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200478 if (handler->destroy != NULL)
479 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200480 free(handler);
481 proc->event_handler = NULL;
482}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100483
484static enum callback_status
485breakpoint_for_symbol(struct library_symbol *libsym, void *data)
486{
487 struct Process *proc = data;
488 fprintf(stderr, " %s@%p\n", libsym->name, libsym->enter_addr);
489
Petr Machata9df15012012-02-20 12:49:46 +0100490 if (insert_breakpoint(proc, libsym->enter_addr, libsym) == NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100491 return CBS_STOP;
492
493 return CBS_CONT;
494}
495
496void
497proc_add_library(struct Process *proc, struct library *lib)
498{
499 assert(lib->next == NULL);
500 lib->next = proc->libraries;
501 proc->libraries = lib;
502 fprintf(stderr, "=== Added library %s@%p to %d:\n",
503 lib->name, lib->base, proc->pid);
504
505 struct library_symbol *libsym = NULL;
506 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100507 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100508 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100509}
510
511int
512proc_remove_library(struct Process *proc, struct library *lib)
513{
514 struct library **libp;
515 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
516 if (*libp == lib) {
517 *libp = lib->next;
518 return 0;
519 }
520 return -1;
521}
522
523struct library *
524proc_each_library(struct Process *proc, struct library *it,
525 enum callback_status (*cb)(struct Process *proc,
526 struct library *lib, void *data),
527 void *data)
528{
529 if (it == NULL)
530 it = proc->libraries;
531
532 while (it != NULL) {
533 struct library *next = it->next;
534
535 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200536 case CBS_FAIL:
537 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100538 case CBS_STOP:
539 return it;
540 case CBS_CONT:
541 break;
542 }
543
544 it = next;
545 }
546
547 return NULL;
548}
Petr Machata52dbfb12012-03-29 16:38:26 +0200549
550int
551proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
552{
553 struct Process *leader = proc->leader;
554
555 /* Only the group leader should be getting the breakpoints and
556 * thus have ->breakpoint initialized. */
557 assert(leader != NULL);
558 assert(leader->breakpoints != NULL);
559
560 /* Make sure it wasn't inserted yet. */
561 assert(bp->proc == NULL);
562
563 debug(DEBUG_FUNCTION, "proc_insert_breakpoint(pid=%d, %s@%p)",
564 proc->pid, breakpoint_name(bp), bp->addr);
565
566 assert(dict_find_entry(leader->breakpoints, bp->addr) == NULL);
567 if (dict_enter(leader->breakpoints, bp->addr, bp) < 0) {
568 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
569 breakpoint_name(bp), bp->addr);
570 return -1;
571 }
572
573 bp->proc = proc;
574 return 0;
575}
576
577int
578proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
579{
580 /* XXX We can't, really. We are missing dict_remove. */
581 assert(!"Not yet implemented!");
582 abort();
583}