blob: f75df2439b66249035371152ae4a613449f827f7 [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
158 /* For non-leader processes, that's all we need to do. */
159 if (proc->leader != proc)
160 return 0;
161
162 /* Clone symbols first so that we can clone and relink
163 * breakpoints. */
164 struct library *lib;
165 struct library **nlibp = &retp->libraries;
166 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
167 *nlibp = malloc(sizeof(**nlibp));
168 if (*nlibp == NULL
169 || library_clone(*nlibp, lib) < 0) {
170 fail2:
171 process_bare_destroy(retp);
172
173 /* Error when cloning. Unroll what was done. */
174 for (lib = retp->libraries; lib != NULL; ) {
175 struct library *next = lib->next;
176 library_destroy(lib);
177 free(lib);
178 lib = next;
179 }
180 goto fail;
181 }
182
183 nlibp = &(*nlibp)->next;
184 }
185
186 /* Now clone breakpoints. Symbol relinking is done in
187 * clone_single_bp. */
188 struct clone_single_bp_data data = {
189 .old_proc = proc,
190 .new_proc = retp,
191 .error = 0,
192 };
193 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
194
195 if (data.error < 0)
196 goto fail2;
197
198 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100199}
200
Petr Machata3c516d52011-08-18 03:53:18 +0200201static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200202open_one_pid(pid_t pid)
203{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200204 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100205 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200206 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
207
Petr Machata1974dbc2011-08-19 18:58:01 +0200208 /* Get the filename first. Should the trace_pid fail, we can
209 * easily free it, untracing is more work. */
210 if ((filename = pid2name(pid)) == NULL
211 || trace_pid(pid) < 0) {
212 free(filename);
213 return -1;
214 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100215
Petr Machata1974dbc2011-08-19 18:58:01 +0200216 proc = open_program(filename, pid, 0);
217 if (proc == NULL)
218 return -1;
Petr Machata602330f2011-07-09 11:15:34 +0200219 trace_set_options(proc, pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200220
Petr Machata1974dbc2011-08-19 18:58:01 +0200221 return 0;
222}
223
Petr Machata2b46cfc2012-02-18 11:17:29 +0100224static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200225start_one_pid(Process * proc, void * data)
226{
227 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100228 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100229}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100230
Petr Machata9a5420c2011-07-09 11:21:23 +0200231void
232open_pid(pid_t pid)
233{
234 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200235 /* If we are already tracing this guy, we should be seeing all
236 * his children via normal tracing route. */
237 if (pid2proc(pid) != NULL)
238 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200239
Petr Machata3c516d52011-08-18 03:53:18 +0200240 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200241 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200242 fprintf(stderr, "Cannot attach to pid %u: %s\n",
243 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200244 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200245 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200246 }
247
Petr Machata3c516d52011-08-18 03:53:18 +0200248 /* Now attach to all tasks that belong to that PID. There's a
249 * race between process_tasks and open_one_pid. So when we
250 * fail in open_one_pid below, we just do another round.
251 * Chances are that by then that PID will have gone away, and
252 * that's why we have seen the failure. The processes that we
253 * manage to open_one_pid are stopped, so we should eventually
254 * reach a point where process_tasks doesn't give any new
255 * processes (because there's nobody left to produce
256 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200257 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200258 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200259 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200260 pid_t *tasks;
261 size_t ntasks;
262 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200263
Petr Machata3c516d52011-08-18 03:53:18 +0200264 if (process_tasks(pid, &tasks, &ntasks) < 0) {
265 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
266 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100267 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200268 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200269
Petr Machata3c516d52011-08-18 03:53:18 +0200270 have_all = 1;
271 for (i = 0; i < ntasks; ++i)
272 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200273 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200274 have_all = 0;
275
Petr Machata9a5420c2011-07-09 11:21:23 +0200276 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200277
Petr Machata1974dbc2011-08-19 18:58:01 +0200278 if (have_all && old_ntasks == ntasks)
279 break;
280 old_ntasks = ntasks;
281 }
282
283 /* Done. Now initialize breakpoints and then continue
284 * everyone. */
285 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200286 leader = pid2proc(pid)->leader;
287 enable_all_breakpoints(leader);
288
Petr Machata74132a42012-03-16 02:46:18 +0100289 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200290}
291
Petr Machata2b46cfc2012-02-18 11:17:29 +0100292static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200293find_proc(Process * proc, void * data)
294{
295 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100296 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200297}
298
Juan Cespedesa8909f72009-04-28 20:02:41 +0200299Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100300pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200301 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
302}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100303
Petr Machatacebb8842011-07-09 11:14:11 +0200304static Process * list_of_processes = NULL;
305
Petr Machatacbe29c62011-09-27 02:27:58 +0200306static void
307unlist_process(Process * proc)
308{
309 Process *tmp;
310
311 if (list_of_processes == proc) {
312 list_of_processes = list_of_processes->next;
313 return;
314 }
315
316 for (tmp = list_of_processes; ; tmp = tmp->next) {
317 /* If the following assert fails, the process wasn't
318 * in the list. */
319 assert(tmp->next != NULL);
320
321 if (tmp->next == proc) {
322 tmp->next = tmp->next->next;
323 return;
324 }
325 }
326}
327
Petr Machata2b46cfc2012-02-18 11:17:29 +0100328struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100329each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330 enum callback_status(*cb)(struct Process *proc, void *data),
331 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200332{
Petr Machata74132a42012-03-16 02:46:18 +0100333 struct Process *it = start_after == NULL ? list_of_processes
334 : start_after->next;
335
336 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200337 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100338 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100339 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200340 case CBS_FAIL:
341 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100342 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200343 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100344 case CBS_CONT:
345 break;
346 }
Petr Machatacebb8842011-07-09 11:14:11 +0200347 it = next;
348 }
349 return NULL;
350}
Petr Machata9a5420c2011-07-09 11:21:23 +0200351
352Process *
Petr Machata74132a42012-03-16 02:46:18 +0100353each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100354 enum callback_status(*cb)(struct Process *proc, void *data),
355 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200356{
Petr Machata74132a42012-03-16 02:46:18 +0100357 assert(proc != NULL);
358 struct Process *it = start_after == NULL ? proc->leader
359 : start_after->next;
360
Petr Machata9a5420c2011-07-09 11:21:23 +0200361 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100362 struct Process *leader = it->leader;
363 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200364 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100365 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100366 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200367 case CBS_FAIL:
368 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100369 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200370 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100371 case CBS_CONT:
372 break;
373 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200374 it = next;
375 }
376 }
377 return NULL;
378}
379
Petr Machatacebb8842011-07-09 11:14:11 +0200380void
381add_process(Process * proc)
382{
Petr Machata9a5420c2011-07-09 11:21:23 +0200383 Process ** leaderp = &list_of_processes;
384 if (proc->pid) {
385 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200386 if (tgid == 0)
387 /* Must have been terminated before we managed
388 * to fully attach. */
389 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200390 if (tgid == proc->pid)
391 proc->leader = proc;
392 else {
393 Process * leader = pid2proc(tgid);
394 proc->leader = leader;
395 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200396 leaderp = &leader->next;
397 }
398 }
399 proc->next = *leaderp;
400 *leaderp = proc;
401}
402
Petr Machatacbe29c62011-09-27 02:27:58 +0200403void
404change_process_leader(Process * proc, Process * leader)
405{
406 Process ** leaderp = &list_of_processes;
407 if (proc->leader == leader)
408 return;
409
410 assert(leader != NULL);
411 unlist_process(proc);
412 if (proc != leader)
413 leaderp = &leader->next;
414
415 proc->leader = leader;
416 proc->next = *leaderp;
417 *leaderp = proc;
418}
419
Petr Machata2b46cfc2012-02-18 11:17:29 +0100420static enum callback_status
421clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200422{
423 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
424 proc->pid, proc->leader->pid);
425 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100426 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200427}
428
Petr Machata69a03e62011-07-09 11:29:12 +0200429static enum ecb_status
430event_for_proc(Event * event, void * data)
431{
432 if (event->proc == data)
433 return ecb_deque;
434 else
435 return ecb_cont;
436}
437
438static void
439delete_events_for(Process * proc)
440{
441 Event * event;
442 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
443 free(event);
444}
445
Petr Machatacebb8842011-07-09 11:14:11 +0200446void
447remove_process(Process *proc)
448{
Petr Machatacebb8842011-07-09 11:14:11 +0200449 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
450
Petr Machata9a5420c2011-07-09 11:21:23 +0200451 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100452 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200453
Petr Machatacbe29c62011-09-27 02:27:58 +0200454 unlist_process(proc);
455 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100456}
Petr Machata4007d742011-07-09 11:29:42 +0200457
458void
Petr Machata366c2f42012-02-09 19:34:36 +0100459install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200460{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200461 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200462 assert(proc->event_handler == NULL);
463 proc->event_handler = handler;
464}
465
466void
467destroy_event_handler(Process * proc)
468{
Petr Machata366c2f42012-02-09 19:34:36 +0100469 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200470 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200471 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200472 if (handler->destroy != NULL)
473 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200474 free(handler);
475 proc->event_handler = NULL;
476}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100477
478static enum callback_status
479breakpoint_for_symbol(struct library_symbol *libsym, void *data)
480{
481 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200482 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100483
Petr Machatad5e85562012-04-05 15:18:38 +0200484 /* If there is an artificial breakpoint on the same address,
485 * its libsym will be NULL, and we can smuggle our libsym
486 * there. That artificial breakpoint is there presumably for
487 * the callbacks, which we don't touch. If there is a real
488 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200489 * symbols and ignore extra symbol aliases.
490 *
491 * The other direction is more complicated and currently not
492 * supported. If a breakpoint has custom callbacks, it might
493 * be also custom-allocated, and we would really need to swap
494 * the two: delete the one now in the dictionary, swap values
495 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200496 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
497 libsym->enter_addr);
498 if (bp != NULL) {
499 assert(bp->libsym == NULL);
500 bp->libsym = libsym;
501 return CBS_CONT;
502 }
503
504 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200505 if (bp == NULL
506 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
507 fail:
508 free(bp);
509 return CBS_FAIL;
510 }
511 if (proc_add_breakpoint(proc, bp) < 0) {
512 breakpoint_destroy(bp);
513 goto fail;
514 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100515
Petr Machata76dd9292012-04-03 13:02:06 +0200516 /* If this is dlopened library, turn on the breakpoint right
517 * away. */
518 if (proc->fixed_libs != NULL
519 && breakpoint_turn_on(bp) < 0) {
520 proc_remove_breakpoint(proc, bp);
521 breakpoint_destroy(bp);
522 goto fail;
523 }
524
Petr Machata2b46cfc2012-02-18 11:17:29 +0100525 return CBS_CONT;
526}
527
528void
529proc_add_library(struct Process *proc, struct library *lib)
530{
531 assert(lib->next == NULL);
532 lib->next = proc->libraries;
533 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200534 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
535 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100536
537 struct library_symbol *libsym = NULL;
538 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100539 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100540 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100541}
542
543int
544proc_remove_library(struct Process *proc, struct library *lib)
545{
546 struct library **libp;
547 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
548 if (*libp == lib) {
549 *libp = lib->next;
550 return 0;
551 }
552 return -1;
553}
554
555struct library *
556proc_each_library(struct Process *proc, struct library *it,
557 enum callback_status (*cb)(struct Process *proc,
558 struct library *lib, void *data),
559 void *data)
560{
561 if (it == NULL)
562 it = proc->libraries;
563
564 while (it != NULL) {
565 struct library *next = it->next;
566
567 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200568 case CBS_FAIL:
569 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100570 case CBS_STOP:
571 return it;
572 case CBS_CONT:
573 break;
574 }
575
576 it = next;
577 }
578
579 return NULL;
580}
Petr Machata52dbfb12012-03-29 16:38:26 +0200581
582int
583proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
584{
585 struct Process *leader = proc->leader;
586
587 /* Only the group leader should be getting the breakpoints and
588 * thus have ->breakpoint initialized. */
589 assert(leader != NULL);
590 assert(leader->breakpoints != NULL);
591
592 /* Make sure it wasn't inserted yet. */
593 assert(bp->proc == NULL);
594
595 debug(DEBUG_FUNCTION, "proc_insert_breakpoint(pid=%d, %s@%p)",
596 proc->pid, breakpoint_name(bp), bp->addr);
597
Petr Machataa2416362012-04-06 02:43:34 +0200598 /* XXX We might merge bp->libsym instead of the following
599 * assert, but that's not necessary right now. Look into
600 * breakpoint_for_symbol. */
Petr Machata52dbfb12012-03-29 16:38:26 +0200601 assert(dict_find_entry(leader->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200602
Petr Machata52dbfb12012-03-29 16:38:26 +0200603 if (dict_enter(leader->breakpoints, bp->addr, bp) < 0) {
604 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
605 breakpoint_name(bp), bp->addr);
606 return -1;
607 }
608
609 bp->proc = proc;
610 return 0;
611}
612
613int
614proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
615{
616 /* XXX We can't, really. We are missing dict_remove. */
617 assert(!"Not yet implemented!");
618 abort();
619}