blob: c525359c58df6f5d5e162f2405bc54fa4b11bbe2 [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 *
Petr Machata75934ad2012-04-14 02:28:03 +020099open_program(const char *filename, pid_t pid)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100100{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100101 assert(pid != 0);
102 struct Process *proc = malloc(sizeof(*proc));
Petr Machata75934ad2012-04-14 02:28:03 +0200103 if (proc == NULL || process_init(proc, filename, pid) < 0) {
Petr Machata1974dbc2011-08-19 18:58:01 +0200104 free(proc);
105 return NULL;
106 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100107 return proc;
108}
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100109
Petr Machata2b46cfc2012-02-18 11:17:29 +0100110struct clone_single_bp_data {
111 struct Process *old_proc;
112 struct Process *new_proc;
113 int error;
114};
115
Petr Machata2b46cfc2012-02-18 11:17:29 +0100116static void
117clone_single_bp(void *key, void *value, void *u)
118{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100119 struct breakpoint *bp = value;
120 struct clone_single_bp_data *data = u;
121
Petr Machatad3cc9882012-04-13 21:40:23 +0200122 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100123 struct breakpoint *clone = malloc(sizeof(*clone));
124 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200125 || breakpoint_clone(clone, data->new_proc,
126 bp, data->old_proc) < 0) {
127 fail:
128 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100129 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100130 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200131 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
132 breakpoint_destroy(clone);
133 goto fail;
134 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100135}
136
137int
138process_clone(struct Process *retp, struct Process *proc, pid_t pid)
139{
140 if (process_bare_init(retp, proc->filename, pid) < 0) {
141 fail:
142 error(0, errno, "clone process %d->%d", proc->pid, pid);
143 return -1;
144 }
145
Petr Machatacf1679a2012-04-06 19:56:17 +0200146 retp->tracesysgood = proc->tracesysgood;
147
Petr Machata2b46cfc2012-02-18 11:17:29 +0100148 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200149 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100150 return 0;
151
152 /* Clone symbols first so that we can clone and relink
153 * breakpoints. */
154 struct library *lib;
155 struct library **nlibp = &retp->libraries;
156 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
157 *nlibp = malloc(sizeof(**nlibp));
158 if (*nlibp == NULL
159 || library_clone(*nlibp, lib) < 0) {
160 fail2:
161 process_bare_destroy(retp);
162
163 /* Error when cloning. Unroll what was done. */
164 for (lib = retp->libraries; lib != NULL; ) {
165 struct library *next = lib->next;
166 library_destroy(lib);
167 free(lib);
168 lib = next;
169 }
170 goto fail;
171 }
172
173 nlibp = &(*nlibp)->next;
174 }
175
176 /* Now clone breakpoints. Symbol relinking is done in
177 * clone_single_bp. */
178 struct clone_single_bp_data data = {
179 .old_proc = proc,
180 .new_proc = retp,
181 .error = 0,
182 };
183 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
184
Petr Machataded6f972012-04-13 23:15:48 +0200185 /* And finally the call stack. */
186 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
187 retp->callstack_depth = proc->callstack_depth;
188
Petr Machata2b46cfc2012-02-18 11:17:29 +0100189 if (data.error < 0)
190 goto fail2;
191
192 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100193}
194
Petr Machata3c516d52011-08-18 03:53:18 +0200195static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200196open_one_pid(pid_t pid)
197{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200198 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100199 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200200 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
201
Petr Machata1974dbc2011-08-19 18:58:01 +0200202 /* Get the filename first. Should the trace_pid fail, we can
203 * easily free it, untracing is more work. */
204 if ((filename = pid2name(pid)) == NULL
205 || trace_pid(pid) < 0) {
206 free(filename);
207 return -1;
208 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100209
Petr Machata75934ad2012-04-14 02:28:03 +0200210 proc = open_program(filename, pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200211 if (proc == NULL)
212 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200213 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200214
Petr Machata1974dbc2011-08-19 18:58:01 +0200215 return 0;
216}
217
Petr Machata2b46cfc2012-02-18 11:17:29 +0100218static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200219start_one_pid(Process * proc, void * data)
220{
221 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100222 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100223}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100224
Petr Machata9a5420c2011-07-09 11:21:23 +0200225void
226open_pid(pid_t pid)
227{
228 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200229 /* If we are already tracing this guy, we should be seeing all
230 * his children via normal tracing route. */
231 if (pid2proc(pid) != NULL)
232 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200233
Petr Machata3c516d52011-08-18 03:53:18 +0200234 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200235 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200236 fprintf(stderr, "Cannot attach to pid %u: %s\n",
237 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200238 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200239 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200240 }
241
Petr Machata3c516d52011-08-18 03:53:18 +0200242 /* Now attach to all tasks that belong to that PID. There's a
243 * race between process_tasks and open_one_pid. So when we
244 * fail in open_one_pid below, we just do another round.
245 * Chances are that by then that PID will have gone away, and
246 * that's why we have seen the failure. The processes that we
247 * manage to open_one_pid are stopped, so we should eventually
248 * reach a point where process_tasks doesn't give any new
249 * processes (because there's nobody left to produce
250 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200251 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200252 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200253 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200254 pid_t *tasks;
255 size_t ntasks;
256 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200257
Petr Machata3c516d52011-08-18 03:53:18 +0200258 if (process_tasks(pid, &tasks, &ntasks) < 0) {
259 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
260 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100261 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200262 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200263
Petr Machata3c516d52011-08-18 03:53:18 +0200264 have_all = 1;
265 for (i = 0; i < ntasks; ++i)
266 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200267 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200268 have_all = 0;
269
Petr Machata9a5420c2011-07-09 11:21:23 +0200270 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200271
Petr Machata1974dbc2011-08-19 18:58:01 +0200272 if (have_all && old_ntasks == ntasks)
273 break;
274 old_ntasks = ntasks;
275 }
276
277 /* Done. Now initialize breakpoints and then continue
278 * everyone. */
279 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200280 leader = pid2proc(pid)->leader;
281 enable_all_breakpoints(leader);
282
Petr Machata74132a42012-03-16 02:46:18 +0100283 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200284}
285
Petr Machata2b46cfc2012-02-18 11:17:29 +0100286static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200287find_proc(Process * proc, void * data)
288{
289 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100290 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200291}
292
Juan Cespedesa8909f72009-04-28 20:02:41 +0200293Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100294pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200295 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
296}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100297
Petr Machatacebb8842011-07-09 11:14:11 +0200298static Process * list_of_processes = NULL;
299
Petr Machatacbe29c62011-09-27 02:27:58 +0200300static void
301unlist_process(Process * proc)
302{
303 Process *tmp;
304
305 if (list_of_processes == proc) {
306 list_of_processes = list_of_processes->next;
307 return;
308 }
309
310 for (tmp = list_of_processes; ; tmp = tmp->next) {
311 /* If the following assert fails, the process wasn't
312 * in the list. */
313 assert(tmp->next != NULL);
314
315 if (tmp->next == proc) {
316 tmp->next = tmp->next->next;
317 return;
318 }
319 }
320}
321
Petr Machata2b46cfc2012-02-18 11:17:29 +0100322struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100323each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100324 enum callback_status(*cb)(struct Process *proc, void *data),
325 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200326{
Petr Machata74132a42012-03-16 02:46:18 +0100327 struct Process *it = start_after == NULL ? list_of_processes
328 : start_after->next;
329
330 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200331 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100332 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200334 case CBS_FAIL:
335 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100336 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200337 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100338 case CBS_CONT:
339 break;
340 }
Petr Machatacebb8842011-07-09 11:14:11 +0200341 it = next;
342 }
343 return NULL;
344}
Petr Machata9a5420c2011-07-09 11:21:23 +0200345
346Process *
Petr Machata74132a42012-03-16 02:46:18 +0100347each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100348 enum callback_status(*cb)(struct Process *proc, void *data),
349 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200350{
Petr Machata74132a42012-03-16 02:46:18 +0100351 assert(proc != NULL);
352 struct Process *it = start_after == NULL ? proc->leader
353 : start_after->next;
354
Petr Machata9a5420c2011-07-09 11:21:23 +0200355 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100356 struct Process *leader = it->leader;
357 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200358 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100359 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100360 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200361 case CBS_FAIL:
362 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100363 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200364 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100365 case CBS_CONT:
366 break;
367 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200368 it = next;
369 }
370 }
371 return NULL;
372}
373
Petr Machata44965c72012-04-06 19:59:20 +0200374static void
Petr Machatacebb8842011-07-09 11:14:11 +0200375add_process(Process * proc)
376{
Petr Machata9a5420c2011-07-09 11:21:23 +0200377 Process ** leaderp = &list_of_processes;
378 if (proc->pid) {
379 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200380 if (tgid == 0)
381 /* Must have been terminated before we managed
382 * to fully attach. */
383 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200384 if (tgid == proc->pid)
385 proc->leader = proc;
386 else {
387 Process * leader = pid2proc(tgid);
388 proc->leader = leader;
389 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200390 leaderp = &leader->next;
391 }
392 }
393 proc->next = *leaderp;
394 *leaderp = proc;
395}
396
Petr Machatacbe29c62011-09-27 02:27:58 +0200397void
398change_process_leader(Process * proc, Process * leader)
399{
400 Process ** leaderp = &list_of_processes;
401 if (proc->leader == leader)
402 return;
403
404 assert(leader != NULL);
405 unlist_process(proc);
406 if (proc != leader)
407 leaderp = &leader->next;
408
409 proc->leader = leader;
410 proc->next = *leaderp;
411 *leaderp = proc;
412}
413
Petr Machata2b46cfc2012-02-18 11:17:29 +0100414static enum callback_status
415clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200416{
417 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
418 proc->pid, proc->leader->pid);
419 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100420 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200421}
422
Petr Machata69a03e62011-07-09 11:29:12 +0200423static enum ecb_status
424event_for_proc(Event * event, void * data)
425{
426 if (event->proc == data)
427 return ecb_deque;
428 else
429 return ecb_cont;
430}
431
432static void
433delete_events_for(Process * proc)
434{
435 Event * event;
436 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
437 free(event);
438}
439
Petr Machatacebb8842011-07-09 11:14:11 +0200440void
441remove_process(Process *proc)
442{
Petr Machatacebb8842011-07-09 11:14:11 +0200443 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
444
Petr Machata9a5420c2011-07-09 11:21:23 +0200445 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100446 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200447
Petr Machatacbe29c62011-09-27 02:27:58 +0200448 unlist_process(proc);
449 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100450}
Petr Machata4007d742011-07-09 11:29:42 +0200451
452void
Petr Machata366c2f42012-02-09 19:34:36 +0100453install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200454{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200455 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200456 assert(proc->event_handler == NULL);
457 proc->event_handler = handler;
458}
459
460void
461destroy_event_handler(Process * proc)
462{
Petr Machata366c2f42012-02-09 19:34:36 +0100463 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200464 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200465 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200466 if (handler->destroy != NULL)
467 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200468 free(handler);
469 proc->event_handler = NULL;
470}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100471
472static enum callback_status
473breakpoint_for_symbol(struct library_symbol *libsym, void *data)
474{
475 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200476 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100477
Petr Machatad5e85562012-04-05 15:18:38 +0200478 /* If there is an artificial breakpoint on the same address,
479 * its libsym will be NULL, and we can smuggle our libsym
480 * there. That artificial breakpoint is there presumably for
481 * the callbacks, which we don't touch. If there is a real
482 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200483 * symbols and ignore extra symbol aliases.
484 *
485 * The other direction is more complicated and currently not
486 * supported. If a breakpoint has custom callbacks, it might
487 * be also custom-allocated, and we would really need to swap
488 * the two: delete the one now in the dictionary, swap values
489 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200490 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
491 libsym->enter_addr);
492 if (bp != NULL) {
493 assert(bp->libsym == NULL);
494 bp->libsym = libsym;
495 return CBS_CONT;
496 }
497
498 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200499 if (bp == NULL
500 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
501 fail:
502 free(bp);
503 return CBS_FAIL;
504 }
505 if (proc_add_breakpoint(proc, bp) < 0) {
506 breakpoint_destroy(bp);
507 goto fail;
508 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100509
Petr Machatafa0c5702012-04-13 18:43:40 +0200510 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200511 proc_remove_breakpoint(proc, bp);
512 breakpoint_destroy(bp);
513 goto fail;
514 }
515
Petr Machata2b46cfc2012-02-18 11:17:29 +0100516 return CBS_CONT;
517}
518
519void
520proc_add_library(struct Process *proc, struct library *lib)
521{
522 assert(lib->next == NULL);
523 lib->next = proc->libraries;
524 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200525 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
526 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100527
528 struct library_symbol *libsym = NULL;
529 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100530 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100531 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100532}
533
534int
535proc_remove_library(struct Process *proc, struct library *lib)
536{
537 struct library **libp;
538 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
539 if (*libp == lib) {
540 *libp = lib->next;
541 return 0;
542 }
543 return -1;
544}
545
546struct library *
547proc_each_library(struct Process *proc, struct library *it,
548 enum callback_status (*cb)(struct Process *proc,
549 struct library *lib, void *data),
550 void *data)
551{
552 if (it == NULL)
553 it = proc->libraries;
554
555 while (it != NULL) {
556 struct library *next = it->next;
557
558 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200559 case CBS_FAIL:
560 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100561 case CBS_STOP:
562 return it;
563 case CBS_CONT:
564 break;
565 }
566
567 it = next;
568 }
569
570 return NULL;
571}
Petr Machata52dbfb12012-03-29 16:38:26 +0200572
573int
574proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
575{
Petr Machata52dbfb12012-03-29 16:38:26 +0200576 /* Only the group leader should be getting the breakpoints and
577 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200578 assert(proc->leader != NULL);
579 assert(proc->leader == proc);
580 assert(proc->breakpoints != NULL);
Petr Machata52dbfb12012-03-29 16:38:26 +0200581
Petr Machatafa0c5702012-04-13 18:43:40 +0200582 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200583 proc->pid, breakpoint_name(bp), bp->addr);
584
Petr Machataa2416362012-04-06 02:43:34 +0200585 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200586 * assert, but that's not necessary right now. Read the
587 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200588 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200589
Petr Machatafa0c5702012-04-13 18:43:40 +0200590 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200591 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
592 breakpoint_name(bp), bp->addr);
593 return -1;
594 }
595
Petr Machata52dbfb12012-03-29 16:38:26 +0200596 return 0;
597}
598
599int
600proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
601{
602 /* XXX We can't, really. We are missing dict_remove. */
603 assert(!"Not yet implemented!");
604 abort();
605}
Petr Machatad3cc9882012-04-13 21:40:23 +0200606
607/* Dict doesn't support iteration restarts, so here's this contraption
608 * for now. XXX add restarts to dict. */
609struct each_breakpoint_data
610{
611 void *start;
612 void *end;
613 struct Process *proc;
614 enum callback_status (*cb)(struct Process *proc,
615 struct breakpoint *bp,
616 void *data);
617 void *cb_data;
618};
619
620static void
621each_breakpoint_cb(void *key, void *value, void *d)
622{
623 struct each_breakpoint_data *data = d;
624 if (data->end != NULL)
625 return;
626 if (data->start == key)
627 data->start = NULL;
628
629 if (data->start == NULL) {
630 switch (data->cb(data->proc, value, data->cb_data)) {
631 case CBS_FAIL:
632 /* XXX handle me */
633 case CBS_STOP:
634 data->end = key;
635 case CBS_CONT:
636 return;
637 }
638 }
639}
640
641void *
642proc_each_breakpoint(struct Process *proc, void *start,
643 enum callback_status (*cb)(struct Process *proc,
644 struct breakpoint *bp,
645 void *data), void *data)
646{
647 struct each_breakpoint_data dd = {
648 .start = start,
649 .proc = proc,
650 .cb = cb,
651 .cb_data = data,
652 };
653 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
654 return dd.end;
655}