blob: fb07758cff56d8a3620ad8b392841f525f78c653 [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
Petr Machata2b46cfc2012-02-18 11:17:29 +0100117static void
118clone_single_bp(void *key, void *value, void *u)
119{
Petr Machata2b46cfc2012-02-18 11:17:29 +0100120 struct breakpoint *bp = value;
121 struct clone_single_bp_data *data = u;
122
Petr Machatad3cc9882012-04-13 21:40:23 +0200123 data->error = 0;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100124 struct breakpoint *clone = malloc(sizeof(*clone));
125 if (clone == NULL
Petr Machatad3cc9882012-04-13 21:40:23 +0200126 || breakpoint_clone(clone, data->new_proc,
127 bp, data->old_proc) < 0) {
128 fail:
129 free(clone);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100130 data->error = -1;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100131 }
Petr Machatad3cc9882012-04-13 21:40:23 +0200132 if (proc_add_breakpoint(data->new_proc->leader, clone) < 0) {
133 breakpoint_destroy(clone);
134 goto fail;
135 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100136}
137
138int
139process_clone(struct Process *retp, struct Process *proc, pid_t pid)
140{
141 if (process_bare_init(retp, proc->filename, pid) < 0) {
142 fail:
143 error(0, errno, "clone process %d->%d", proc->pid, pid);
144 return -1;
145 }
146
Petr Machatacf1679a2012-04-06 19:56:17 +0200147 retp->tracesysgood = proc->tracesysgood;
148
Petr Machata2b46cfc2012-02-18 11:17:29 +0100149 /* For non-leader processes, that's all we need to do. */
Petr Machatad3cc9882012-04-13 21:40:23 +0200150 if (retp->leader != retp)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100151 return 0;
152
153 /* Clone symbols first so that we can clone and relink
154 * breakpoints. */
155 struct library *lib;
156 struct library **nlibp = &retp->libraries;
157 for (lib = proc->libraries; lib != NULL; lib = lib->next) {
158 *nlibp = malloc(sizeof(**nlibp));
159 if (*nlibp == NULL
160 || library_clone(*nlibp, lib) < 0) {
161 fail2:
162 process_bare_destroy(retp);
163
164 /* Error when cloning. Unroll what was done. */
165 for (lib = retp->libraries; lib != NULL; ) {
166 struct library *next = lib->next;
167 library_destroy(lib);
168 free(lib);
169 lib = next;
170 }
171 goto fail;
172 }
173
174 nlibp = &(*nlibp)->next;
175 }
176
177 /* Now clone breakpoints. Symbol relinking is done in
178 * clone_single_bp. */
179 struct clone_single_bp_data data = {
180 .old_proc = proc,
181 .new_proc = retp,
182 .error = 0,
183 };
184 dict_apply_to_all(proc->breakpoints, &clone_single_bp, &data);
185
Petr Machataded6f972012-04-13 23:15:48 +0200186 /* And finally the call stack. */
187 memcpy(retp->callstack, proc->callstack, sizeof(retp->callstack));
188 retp->callstack_depth = proc->callstack_depth;
189
Petr Machata2b46cfc2012-02-18 11:17:29 +0100190 if (data.error < 0)
191 goto fail2;
192
193 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100194}
195
Petr Machata3c516d52011-08-18 03:53:18 +0200196static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200197open_one_pid(pid_t pid)
198{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200199 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100200 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200201 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
202
Petr Machata1974dbc2011-08-19 18:58:01 +0200203 /* Get the filename first. Should the trace_pid fail, we can
204 * easily free it, untracing is more work. */
205 if ((filename = pid2name(pid)) == NULL
206 || trace_pid(pid) < 0) {
207 free(filename);
208 return -1;
209 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100210
Petr Machata1974dbc2011-08-19 18:58:01 +0200211 proc = open_program(filename, pid, 0);
212 if (proc == NULL)
213 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200214 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200215
Petr Machata1974dbc2011-08-19 18:58:01 +0200216 return 0;
217}
218
Petr Machata2b46cfc2012-02-18 11:17:29 +0100219static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200220start_one_pid(Process * proc, void * data)
221{
222 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100223 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100224}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100225
Petr Machata9a5420c2011-07-09 11:21:23 +0200226void
227open_pid(pid_t pid)
228{
229 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200230 /* If we are already tracing this guy, we should be seeing all
231 * his children via normal tracing route. */
232 if (pid2proc(pid) != NULL)
233 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200234
Petr Machata3c516d52011-08-18 03:53:18 +0200235 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200236 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200237 fprintf(stderr, "Cannot attach to pid %u: %s\n",
238 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200239 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200240 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200241 }
242
Petr Machata3c516d52011-08-18 03:53:18 +0200243 /* Now attach to all tasks that belong to that PID. There's a
244 * race between process_tasks and open_one_pid. So when we
245 * fail in open_one_pid below, we just do another round.
246 * Chances are that by then that PID will have gone away, and
247 * that's why we have seen the failure. The processes that we
248 * manage to open_one_pid are stopped, so we should eventually
249 * reach a point where process_tasks doesn't give any new
250 * processes (because there's nobody left to produce
251 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200252 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200253 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200254 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200255 pid_t *tasks;
256 size_t ntasks;
257 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200258
Petr Machata3c516d52011-08-18 03:53:18 +0200259 if (process_tasks(pid, &tasks, &ntasks) < 0) {
260 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
261 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100262 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200263 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200264
Petr Machata3c516d52011-08-18 03:53:18 +0200265 have_all = 1;
266 for (i = 0; i < ntasks; ++i)
267 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200268 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200269 have_all = 0;
270
Petr Machata9a5420c2011-07-09 11:21:23 +0200271 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200272
Petr Machata1974dbc2011-08-19 18:58:01 +0200273 if (have_all && old_ntasks == ntasks)
274 break;
275 old_ntasks = ntasks;
276 }
277
278 /* Done. Now initialize breakpoints and then continue
279 * everyone. */
280 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200281 leader = pid2proc(pid)->leader;
282 enable_all_breakpoints(leader);
283
Petr Machata74132a42012-03-16 02:46:18 +0100284 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200285}
286
Petr Machata2b46cfc2012-02-18 11:17:29 +0100287static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200288find_proc(Process * proc, void * data)
289{
290 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100291 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200292}
293
Juan Cespedesa8909f72009-04-28 20:02:41 +0200294Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100295pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200296 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
297}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100298
Petr Machatacebb8842011-07-09 11:14:11 +0200299static Process * list_of_processes = NULL;
300
Petr Machatacbe29c62011-09-27 02:27:58 +0200301static void
302unlist_process(Process * proc)
303{
304 Process *tmp;
305
306 if (list_of_processes == proc) {
307 list_of_processes = list_of_processes->next;
308 return;
309 }
310
311 for (tmp = list_of_processes; ; tmp = tmp->next) {
312 /* If the following assert fails, the process wasn't
313 * in the list. */
314 assert(tmp->next != NULL);
315
316 if (tmp->next == proc) {
317 tmp->next = tmp->next->next;
318 return;
319 }
320 }
321}
322
Petr Machata2b46cfc2012-02-18 11:17:29 +0100323struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100324each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100325 enum callback_status(*cb)(struct Process *proc, void *data),
326 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200327{
Petr Machata74132a42012-03-16 02:46:18 +0100328 struct Process *it = start_after == NULL ? list_of_processes
329 : start_after->next;
330
331 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200332 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100333 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100334 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200335 case CBS_FAIL:
336 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100337 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200338 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100339 case CBS_CONT:
340 break;
341 }
Petr Machatacebb8842011-07-09 11:14:11 +0200342 it = next;
343 }
344 return NULL;
345}
Petr Machata9a5420c2011-07-09 11:21:23 +0200346
347Process *
Petr Machata74132a42012-03-16 02:46:18 +0100348each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100349 enum callback_status(*cb)(struct Process *proc, void *data),
350 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200351{
Petr Machata74132a42012-03-16 02:46:18 +0100352 assert(proc != NULL);
353 struct Process *it = start_after == NULL ? proc->leader
354 : start_after->next;
355
Petr Machata9a5420c2011-07-09 11:21:23 +0200356 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100357 struct Process *leader = it->leader;
358 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200359 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100360 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100361 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200362 case CBS_FAIL:
363 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100364 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200365 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100366 case CBS_CONT:
367 break;
368 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200369 it = next;
370 }
371 }
372 return NULL;
373}
374
Petr Machata44965c72012-04-06 19:59:20 +0200375static void
Petr Machatacebb8842011-07-09 11:14:11 +0200376add_process(Process * proc)
377{
Petr Machata9a5420c2011-07-09 11:21:23 +0200378 Process ** leaderp = &list_of_processes;
379 if (proc->pid) {
380 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200381 if (tgid == 0)
382 /* Must have been terminated before we managed
383 * to fully attach. */
384 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200385 if (tgid == proc->pid)
386 proc->leader = proc;
387 else {
388 Process * leader = pid2proc(tgid);
389 proc->leader = leader;
390 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200391 leaderp = &leader->next;
392 }
393 }
394 proc->next = *leaderp;
395 *leaderp = proc;
396}
397
Petr Machatacbe29c62011-09-27 02:27:58 +0200398void
399change_process_leader(Process * proc, Process * leader)
400{
401 Process ** leaderp = &list_of_processes;
402 if (proc->leader == leader)
403 return;
404
405 assert(leader != NULL);
406 unlist_process(proc);
407 if (proc != leader)
408 leaderp = &leader->next;
409
410 proc->leader = leader;
411 proc->next = *leaderp;
412 *leaderp = proc;
413}
414
Petr Machata2b46cfc2012-02-18 11:17:29 +0100415static enum callback_status
416clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200417{
418 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
419 proc->pid, proc->leader->pid);
420 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100421 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200422}
423
Petr Machata69a03e62011-07-09 11:29:12 +0200424static enum ecb_status
425event_for_proc(Event * event, void * data)
426{
427 if (event->proc == data)
428 return ecb_deque;
429 else
430 return ecb_cont;
431}
432
433static void
434delete_events_for(Process * proc)
435{
436 Event * event;
437 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
438 free(event);
439}
440
Petr Machatacebb8842011-07-09 11:14:11 +0200441void
442remove_process(Process *proc)
443{
Petr Machatacebb8842011-07-09 11:14:11 +0200444 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
445
Petr Machata9a5420c2011-07-09 11:21:23 +0200446 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100447 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200448
Petr Machatacbe29c62011-09-27 02:27:58 +0200449 unlist_process(proc);
450 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100451}
Petr Machata4007d742011-07-09 11:29:42 +0200452
453void
Petr Machata366c2f42012-02-09 19:34:36 +0100454install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200455{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200456 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200457 assert(proc->event_handler == NULL);
458 proc->event_handler = handler;
459}
460
461void
462destroy_event_handler(Process * proc)
463{
Petr Machata366c2f42012-02-09 19:34:36 +0100464 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200465 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200466 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200467 if (handler->destroy != NULL)
468 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200469 free(handler);
470 proc->event_handler = NULL;
471}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100472
473static enum callback_status
474breakpoint_for_symbol(struct library_symbol *libsym, void *data)
475{
476 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200477 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100478
Petr Machatad5e85562012-04-05 15:18:38 +0200479 /* If there is an artificial breakpoint on the same address,
480 * its libsym will be NULL, and we can smuggle our libsym
481 * there. That artificial breakpoint is there presumably for
482 * the callbacks, which we don't touch. If there is a real
483 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200484 * symbols and ignore extra symbol aliases.
485 *
486 * The other direction is more complicated and currently not
487 * supported. If a breakpoint has custom callbacks, it might
488 * be also custom-allocated, and we would really need to swap
489 * the two: delete the one now in the dictionary, swap values
490 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200491 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
492 libsym->enter_addr);
493 if (bp != NULL) {
494 assert(bp->libsym == NULL);
495 bp->libsym = libsym;
496 return CBS_CONT;
497 }
498
499 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200500 if (bp == NULL
501 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
502 fail:
503 free(bp);
504 return CBS_FAIL;
505 }
506 if (proc_add_breakpoint(proc, bp) < 0) {
507 breakpoint_destroy(bp);
508 goto fail;
509 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100510
Petr Machatafa0c5702012-04-13 18:43:40 +0200511 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200512 proc_remove_breakpoint(proc, bp);
513 breakpoint_destroy(bp);
514 goto fail;
515 }
516
Petr Machata2b46cfc2012-02-18 11:17:29 +0100517 return CBS_CONT;
518}
519
520void
521proc_add_library(struct Process *proc, struct library *lib)
522{
523 assert(lib->next == NULL);
524 lib->next = proc->libraries;
525 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200526 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
527 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100528
529 struct library_symbol *libsym = NULL;
530 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100531 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100532 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100533}
534
535int
536proc_remove_library(struct Process *proc, struct library *lib)
537{
538 struct library **libp;
539 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
540 if (*libp == lib) {
541 *libp = lib->next;
542 return 0;
543 }
544 return -1;
545}
546
547struct library *
548proc_each_library(struct Process *proc, struct library *it,
549 enum callback_status (*cb)(struct Process *proc,
550 struct library *lib, void *data),
551 void *data)
552{
553 if (it == NULL)
554 it = proc->libraries;
555
556 while (it != NULL) {
557 struct library *next = it->next;
558
559 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200560 case CBS_FAIL:
561 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100562 case CBS_STOP:
563 return it;
564 case CBS_CONT:
565 break;
566 }
567
568 it = next;
569 }
570
571 return NULL;
572}
Petr Machata52dbfb12012-03-29 16:38:26 +0200573
574int
575proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
576{
Petr Machata52dbfb12012-03-29 16:38:26 +0200577 /* Only the group leader should be getting the breakpoints and
578 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200579 assert(proc->leader != NULL);
580 assert(proc->leader == proc);
581 assert(proc->breakpoints != NULL);
Petr Machata52dbfb12012-03-29 16:38:26 +0200582
Petr Machatafa0c5702012-04-13 18:43:40 +0200583 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200584 proc->pid, breakpoint_name(bp), bp->addr);
585
Petr Machataa2416362012-04-06 02:43:34 +0200586 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200587 * assert, but that's not necessary right now. Read the
588 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200589 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200590
Petr Machatafa0c5702012-04-13 18:43:40 +0200591 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200592 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
593 breakpoint_name(bp), bp->addr);
594 return -1;
595 }
596
Petr Machata52dbfb12012-03-29 16:38:26 +0200597 return 0;
598}
599
600int
601proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
602{
603 /* XXX We can't, really. We are missing dict_remove. */
604 assert(!"Not yet implemented!");
605 abort();
606}
Petr Machatad3cc9882012-04-13 21:40:23 +0200607
608/* Dict doesn't support iteration restarts, so here's this contraption
609 * for now. XXX add restarts to dict. */
610struct each_breakpoint_data
611{
612 void *start;
613 void *end;
614 struct Process *proc;
615 enum callback_status (*cb)(struct Process *proc,
616 struct breakpoint *bp,
617 void *data);
618 void *cb_data;
619};
620
621static void
622each_breakpoint_cb(void *key, void *value, void *d)
623{
624 struct each_breakpoint_data *data = d;
625 if (data->end != NULL)
626 return;
627 if (data->start == key)
628 data->start = NULL;
629
630 if (data->start == NULL) {
631 switch (data->cb(data->proc, value, data->cb_data)) {
632 case CBS_FAIL:
633 /* XXX handle me */
634 case CBS_STOP:
635 data->end = key;
636 case CBS_CONT:
637 return;
638 }
639 }
640}
641
642void *
643proc_each_breakpoint(struct Process *proc, void *start,
644 enum callback_status (*cb)(struct Process *proc,
645 struct breakpoint *bp,
646 void *data), void *data)
647{
648 struct each_breakpoint_data dd = {
649 .start = start,
650 .proc = proc,
651 .cb = cb,
652 .cb_data = data,
653 };
654 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
655 return dd.end;
656}