blob: b9f269f279dc6329cd330fecf9e6a0396c755a68 [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 Machata44965c72012-04-06 19:59:20 +020020static void add_process(struct Process *proc);
21
Petr Machata2b46cfc2012-02-18 11:17:29 +010022static int
23process_bare_init(struct Process *proc, const char *filename, pid_t pid)
24{
Petr Machata2b46cfc2012-02-18 11:17:29 +010025 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) {
Petr Machataecb082f2012-04-05 02:10:10 +020043 proc->breakpoints = dict_init(target_address_hash,
44 target_address_cmp);
Petr Machata2b46cfc2012-02-18 11:17:29 +010045 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{
Petr Machata2b46cfc2012-02-18 11:17:29 +010070 if (process_bare_init(proc, filename, pid) < 0) {
71 error(0, errno, "init process %d", pid);
72 return -1;
73 }
74
Petr Machata18bd8ff2012-04-10 04:32:39 +020075 /* For secondary threads, this is all that we need to do. */
76 if (proc->leader != proc)
77 return 0;
78
79 target_address_t entry;
80 target_address_t interp_bias;
81 if (process_get_entry(proc, &entry, &interp_bias) < 0) {
82 fprintf(stderr, "Couldn't get entry points of process %d\n",
Petr Machata2b46cfc2012-02-18 11:17:29 +010083 proc->pid);
Petr Machata18bd8ff2012-04-10 04:32:39 +020084 fail:
Petr Machata2b46cfc2012-02-18 11:17:29 +010085 process_bare_destroy(proc);
86 return -1;
87 }
88
Petr Machata18bd8ff2012-04-10 04:32:39 +020089 if (breakpoints_init(proc, enable) < 0) {
90 fprintf(stderr, "failed to init breakpoints %d\n",
91 proc->pid);
92 goto fail;
93 }
94
Petr Machata2b46cfc2012-02-18 11:17:29 +010095 return 0;
96}
97
98struct Process *
99open_program(const char *filename, pid_t pid, int enable)
100{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100101 assert(pid != 0);
102 struct Process *proc = malloc(sizeof(*proc));
103 if (proc == NULL
104 || process_init(proc, filename, pid, enable) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200105 free(proc);
106 return NULL;
107 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100108 return proc;
109}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100110
Petr Machata2b46cfc2012-02-18 11:17:29 +0100111struct clone_single_bp_data {
112 struct Process *old_proc;
113 struct Process *new_proc;
114 int error;
115};
116
117struct find_symbol_data {
118 struct library_symbol *old_libsym;
119 struct library_symbol *found_libsym;
120};
121
122static enum callback_status
123find_sym_in_lib(struct Process *proc, struct library *lib, void *u)
124{
125 struct find_symbol_data *fs = u;
126 fs->found_libsym
127 = library_each_symbol(lib, NULL, library_symbol_equal_cb,
128 fs->old_libsym);
129 return fs->found_libsym != NULL ? CBS_STOP : CBS_CONT;
130}
131
132static void
133clone_single_bp(void *key, void *value, void *u)
134{
135 target_address_t addr = (target_address_t)key;
136 struct breakpoint *bp = value;
137 struct clone_single_bp_data *data = u;
138
139 /* Find library and symbol that this symbol was linked to. */
140 struct library_symbol *libsym = bp->libsym;
141 struct library *lib = NULL;
142 if (libsym != NULL) {
143 struct find_symbol_data f_data = {
144 .old_libsym = libsym,
145 };
146 lib = proc_each_library(data->old_proc, NULL,
147 find_sym_in_lib, &f_data);
148 assert(lib != NULL);
149 libsym = f_data.found_libsym;
Petr Machata61196a42012-02-07 16:41:03 +0100150 }
Joe Damatoab3b72c2010-10-31 00:21:53 -0700151
Petr Machata2b46cfc2012-02-18 11:17:29 +0100152 /* LIB and LIBSYM now hold the new library and symbol that
153 * correspond to the original breakpoint. Now we can do the
154 * clone itself. */
155 struct breakpoint *clone = malloc(sizeof(*clone));
156 if (clone == NULL
Petr Machata55ac9322012-03-27 03:07:35 +0200157 || breakpoint_init(clone, data->new_proc, addr, libsym) < 0) {
Petr Machata2b46cfc2012-02-18 11:17:29 +0100158 data->error = -1;
159 return;
160 }
Petr Machata55ac9322012-03-27 03:07:35 +0200161 breakpoint_set_callbacks(clone, bp->cbs);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100162}
163
164int
165process_clone(struct Process *retp, struct Process *proc, pid_t pid)
166{
167 if (process_bare_init(retp, proc->filename, pid) < 0) {
168 fail:
169 error(0, errno, "clone process %d->%d", proc->pid, pid);
170 return -1;
171 }
172
Petr Machatacf1679a2012-04-06 19:56:17 +0200173 retp->tracesysgood = proc->tracesysgood;
174
Petr Machata2b46cfc2012-02-18 11:17:29 +0100175 /* For non-leader processes, that's all we need to do. */
176 if (proc->leader != proc)
177 return 0;
178
179 /* Clone symbols first so that we can clone and relink
180 * breakpoints. */
181 struct library *lib;
182 struct library **nlibp = &retp->libraries;
183 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
184 *nlibp = malloc(sizeof(**nlibp));
185 if (*nlibp == NULL
186 || library_clone(*nlibp, lib) < 0) {
187 fail2:
188 process_bare_destroy(retp);
189
190 /* Error when cloning. Unroll what was done. */
191 for (lib = retp->libraries; lib != NULL; ) {
192 struct library *next = lib->next;
193 library_destroy(lib);
194 free(lib);
195 lib = next;
196 }
197 goto fail;
198 }
199
200 nlibp = &(*nlibp)->next;
201 }
202
203 /* Now clone breakpoints. Symbol relinking is done in
204 * clone_single_bp. */
205 struct clone_single_bp_data data = {
206 .old_proc = proc,
207 .new_proc = retp,
208 .error = 0,
209 };
210 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
211
212 if (data.error < 0)
213 goto fail2;
214
215 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100216}
217
Petr Machata3c516d52011-08-18 03:53:18 +0200218static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200219open_one_pid(pid_t pid)
220{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200221 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100222 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200223 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
224
Petr Machata1974dbc2011-08-19 18:58:01 +0200225 /* Get the filename first. Should the trace_pid fail, we can
226 * easily free it, untracing is more work. */
227 if ((filename = pid2name(pid)) == NULL
228 || trace_pid(pid) < 0) {
229 free(filename);
230 return -1;
231 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100232
Petr Machata1974dbc2011-08-19 18:58:01 +0200233 proc = open_program(filename, pid, 0);
234 if (proc == NULL)
235 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200236 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200237
Petr Machata1974dbc2011-08-19 18:58:01 +0200238 return 0;
239}
240
Petr Machata2b46cfc2012-02-18 11:17:29 +0100241static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200242start_one_pid(Process * proc, void * data)
243{
244 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100245 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100246}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100247
Petr Machata9a5420c2011-07-09 11:21:23 +0200248void
249open_pid(pid_t pid)
250{
251 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200252 /* If we are already tracing this guy, we should be seeing all
253 * his children via normal tracing route. */
254 if (pid2proc(pid) != NULL)
255 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200256
Petr Machata3c516d52011-08-18 03:53:18 +0200257 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200258 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200259 fprintf(stderr, "Cannot attach to pid %u: %s\n",
260 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200261 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200262 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200263 }
264
Petr Machata3c516d52011-08-18 03:53:18 +0200265 /* Now attach to all tasks that belong to that PID. There's a
266 * race between process_tasks and open_one_pid. So when we
267 * fail in open_one_pid below, we just do another round.
268 * Chances are that by then that PID will have gone away, and
269 * that's why we have seen the failure. The processes that we
270 * manage to open_one_pid are stopped, so we should eventually
271 * reach a point where process_tasks doesn't give any new
272 * processes (because there's nobody left to produce
273 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200274 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200275 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200276 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200277 pid_t *tasks;
278 size_t ntasks;
279 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200280
Petr Machata3c516d52011-08-18 03:53:18 +0200281 if (process_tasks(pid, &tasks, &ntasks) < 0) {
282 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
283 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100284 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200285 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200286
Petr Machata3c516d52011-08-18 03:53:18 +0200287 have_all = 1;
288 for (i = 0; i < ntasks; ++i)
289 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200290 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200291 have_all = 0;
292
Petr Machata9a5420c2011-07-09 11:21:23 +0200293 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200294
Petr Machata1974dbc2011-08-19 18:58:01 +0200295 if (have_all && old_ntasks == ntasks)
296 break;
297 old_ntasks = ntasks;
298 }
299
300 /* Done. Now initialize breakpoints and then continue
301 * everyone. */
302 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200303 leader = pid2proc(pid)->leader;
304 enable_all_breakpoints(leader);
305
Petr Machata74132a42012-03-16 02:46:18 +0100306 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200307}
308
Petr Machata2b46cfc2012-02-18 11:17:29 +0100309static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200310find_proc(Process * proc, void * data)
311{
312 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100313 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200314}
315
Juan Cespedesa8909f72009-04-28 20:02:41 +0200316Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100317pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200318 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
319}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100320
Petr Machatacebb8842011-07-09 11:14:11 +0200321static Process * list_of_processes = NULL;
322
Petr Machatacbe29c62011-09-27 02:27:58 +0200323static void
324unlist_process(Process * proc)
325{
326 Process *tmp;
327
328 if (list_of_processes == proc) {
329 list_of_processes = list_of_processes->next;
330 return;
331 }
332
333 for (tmp = list_of_processes; ; tmp = tmp->next) {
334 /* If the following assert fails, the process wasn't
335 * in the list. */
336 assert(tmp->next != NULL);
337
338 if (tmp->next == proc) {
339 tmp->next = tmp->next->next;
340 return;
341 }
342 }
343}
344
Petr Machata2b46cfc2012-02-18 11:17:29 +0100345struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100346each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100347 enum callback_status(*cb)(struct Process *proc, void *data),
348 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200349{
Petr Machata74132a42012-03-16 02:46:18 +0100350 struct Process *it = start_after == NULL ? list_of_processes
351 : start_after->next;
352
353 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200354 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100355 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100356 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200357 case CBS_FAIL:
358 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100359 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200360 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100361 case CBS_CONT:
362 break;
363 }
Petr Machatacebb8842011-07-09 11:14:11 +0200364 it = next;
365 }
366 return NULL;
367}
Petr Machata9a5420c2011-07-09 11:21:23 +0200368
369Process *
Petr Machata74132a42012-03-16 02:46:18 +0100370each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100371 enum callback_status(*cb)(struct Process *proc, void *data),
372 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200373{
Petr Machata74132a42012-03-16 02:46:18 +0100374 assert(proc != NULL);
375 struct Process *it = start_after == NULL ? proc->leader
376 : start_after->next;
377
Petr Machata9a5420c2011-07-09 11:21:23 +0200378 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100379 struct Process *leader = it->leader;
380 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200381 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100382 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100383 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200384 case CBS_FAIL:
385 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100386 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200387 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100388 case CBS_CONT:
389 break;
390 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200391 it = next;
392 }
393 }
394 return NULL;
395}
396
Petr Machata44965c72012-04-06 19:59:20 +0200397static void
Petr Machatacebb8842011-07-09 11:14:11 +0200398add_process(Process * proc)
399{
Petr Machata9a5420c2011-07-09 11:21:23 +0200400 Process ** leaderp = &list_of_processes;
401 if (proc->pid) {
402 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200403 if (tgid == 0)
404 /* Must have been terminated before we managed
405 * to fully attach. */
406 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200407 if (tgid == proc->pid)
408 proc->leader = proc;
409 else {
410 Process * leader = pid2proc(tgid);
411 proc->leader = leader;
412 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200413 leaderp = &leader->next;
414 }
415 }
416 proc->next = *leaderp;
417 *leaderp = proc;
418}
419
Petr Machatacbe29c62011-09-27 02:27:58 +0200420void
421change_process_leader(Process * proc, Process * leader)
422{
423 Process ** leaderp = &list_of_processes;
424 if (proc->leader == leader)
425 return;
426
427 assert(leader != NULL);
428 unlist_process(proc);
429 if (proc != leader)
430 leaderp = &leader->next;
431
432 proc->leader = leader;
433 proc->next = *leaderp;
434 *leaderp = proc;
435}
436
Petr Machata2b46cfc2012-02-18 11:17:29 +0100437static enum callback_status
438clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200439{
440 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
441 proc->pid, proc->leader->pid);
442 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100443 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200444}
445
Petr Machata69a03e62011-07-09 11:29:12 +0200446static enum ecb_status
447event_for_proc(Event * event, void * data)
448{
449 if (event->proc == data)
450 return ecb_deque;
451 else
452 return ecb_cont;
453}
454
455static void
456delete_events_for(Process * proc)
457{
458 Event * event;
459 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
460 free(event);
461}
462
Petr Machatacebb8842011-07-09 11:14:11 +0200463void
464remove_process(Process *proc)
465{
Petr Machatacebb8842011-07-09 11:14:11 +0200466 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
467
Petr Machata9a5420c2011-07-09 11:21:23 +0200468 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100469 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200470
Petr Machatacbe29c62011-09-27 02:27:58 +0200471 unlist_process(proc);
472 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100473}
Petr Machata4007d742011-07-09 11:29:42 +0200474
475void
Petr Machata366c2f42012-02-09 19:34:36 +0100476install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200477{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200478 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200479 assert(proc->event_handler == NULL);
480 proc->event_handler = handler;
481}
482
483void
484destroy_event_handler(Process * proc)
485{
Petr Machata366c2f42012-02-09 19:34:36 +0100486 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200487 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200488 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200489 if (handler->destroy != NULL)
490 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200491 free(handler);
492 proc->event_handler = NULL;
493}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100494
495static enum callback_status
496breakpoint_for_symbol(struct library_symbol *libsym, void *data)
497{
498 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200499 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100500
Petr Machatad5e85562012-04-05 15:18:38 +0200501 /* If there is an artificial breakpoint on the same address,
502 * its libsym will be NULL, and we can smuggle our libsym
503 * there. That artificial breakpoint is there presumably for
504 * the callbacks, which we don't touch. If there is a real
505 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200506 * symbols and ignore extra symbol aliases.
507 *
508 * The other direction is more complicated and currently not
509 * supported. If a breakpoint has custom callbacks, it might
510 * be also custom-allocated, and we would really need to swap
511 * the two: delete the one now in the dictionary, swap values
512 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200513 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
514 libsym->enter_addr);
515 if (bp != NULL) {
516 assert(bp->libsym == NULL);
517 bp->libsym = libsym;
518 return CBS_CONT;
519 }
520
521 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200522 if (bp == NULL
523 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
524 fail:
525 free(bp);
526 return CBS_FAIL;
527 }
528 if (proc_add_breakpoint(proc, bp) < 0) {
529 breakpoint_destroy(bp);
530 goto fail;
531 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100532
Petr Machatafa0c5702012-04-13 18:43:40 +0200533 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200534 proc_remove_breakpoint(proc, bp);
535 breakpoint_destroy(bp);
536 goto fail;
537 }
538
Petr Machata2b46cfc2012-02-18 11:17:29 +0100539 return CBS_CONT;
540}
541
542void
543proc_add_library(struct Process *proc, struct library *lib)
544{
545 assert(lib->next == NULL);
546 lib->next = proc->libraries;
547 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200548 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
549 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100550
551 struct library_symbol *libsym = NULL;
552 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100553 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100554 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100555}
556
557int
558proc_remove_library(struct Process *proc, struct library *lib)
559{
560 struct library **libp;
561 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
562 if (*libp == lib) {
563 *libp = lib->next;
564 return 0;
565 }
566 return -1;
567}
568
569struct library *
570proc_each_library(struct Process *proc, struct library *it,
571 enum callback_status (*cb)(struct Process *proc,
572 struct library *lib, void *data),
573 void *data)
574{
575 if (it == NULL)
576 it = proc->libraries;
577
578 while (it != NULL) {
579 struct library *next = it->next;
580
581 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200582 case CBS_FAIL:
583 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100584 case CBS_STOP:
585 return it;
586 case CBS_CONT:
587 break;
588 }
589
590 it = next;
591 }
592
593 return NULL;
594}
Petr Machata52dbfb12012-03-29 16:38:26 +0200595
596int
597proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
598{
Petr Machata52dbfb12012-03-29 16:38:26 +0200599 /* Only the group leader should be getting the breakpoints and
600 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200601 assert(proc->leader != NULL);
602 assert(proc->leader == proc);
603 assert(proc->breakpoints != NULL);
Petr Machata52dbfb12012-03-29 16:38:26 +0200604
Petr Machatafa0c5702012-04-13 18:43:40 +0200605 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200606 proc->pid, breakpoint_name(bp), bp->addr);
607
Petr Machataa2416362012-04-06 02:43:34 +0200608 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200609 * assert, but that's not necessary right now. Read the
610 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200611 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200612
Petr Machatafa0c5702012-04-13 18:43:40 +0200613 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200614 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
615 breakpoint_name(bp), bp->addr);
616 return -1;
617 }
618
Petr Machata52dbfb12012-03-29 16:38:26 +0200619 return 0;
620}
621
622int
623proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
624{
625 /* XXX We can't, really. We are missing dict_remove. */
626 assert(!"Not yet implemented!");
627 abort();
628}