blob: 79af573d1726c5d4f0a34da6124921fdd081e7d9 [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
186 if (data.error < 0)
187 goto fail2;
188
189 return 0;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100190}
191
Petr Machata3c516d52011-08-18 03:53:18 +0200192static int
Petr Machata9a5420c2011-07-09 11:21:23 +0200193open_one_pid(pid_t pid)
194{
Juan Cespedesa8909f72009-04-28 20:02:41 +0200195 Process *proc;
Ian Wienand2d45b1a2006-02-20 22:48:07 +0100196 char *filename;
Petr Machata9a5420c2011-07-09 11:21:23 +0200197 debug(DEBUG_PROCESS, "open_one_pid(pid=%d)", pid);
198
Petr Machata1974dbc2011-08-19 18:58:01 +0200199 /* Get the filename first. Should the trace_pid fail, we can
200 * easily free it, untracing is more work. */
201 if ((filename = pid2name(pid)) == NULL
202 || trace_pid(pid) < 0) {
203 free(filename);
204 return -1;
205 }
Juan Cespedes35d70631998-03-15 14:05:40 +0100206
Petr Machata1974dbc2011-08-19 18:58:01 +0200207 proc = open_program(filename, pid, 0);
208 if (proc == NULL)
209 return -1;
Petr Machata3ed2a422012-04-06 17:18:55 +0200210 trace_set_options(proc);
Petr Machata3c516d52011-08-18 03:53:18 +0200211
Petr Machata1974dbc2011-08-19 18:58:01 +0200212 return 0;
213}
214
Petr Machata2b46cfc2012-02-18 11:17:29 +0100215static enum callback_status
Petr Machata1974dbc2011-08-19 18:58:01 +0200216start_one_pid(Process * proc, void * data)
217{
218 continue_process(proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100219 return CBS_CONT;
Juan Cespedes273ea6d1998-03-14 23:02:40 +0100220}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100221
Petr Machata9a5420c2011-07-09 11:21:23 +0200222void
223open_pid(pid_t pid)
224{
225 debug(DEBUG_PROCESS, "open_pid(pid=%d)", pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200226 /* If we are already tracing this guy, we should be seeing all
227 * his children via normal tracing route. */
228 if (pid2proc(pid) != NULL)
229 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200230
Petr Machata3c516d52011-08-18 03:53:18 +0200231 /* First, see if we can attach the requested PID itself. */
Petr Machata1974dbc2011-08-19 18:58:01 +0200232 if (open_one_pid(pid)) {
Petr Machata3c516d52011-08-18 03:53:18 +0200233 fprintf(stderr, "Cannot attach to pid %u: %s\n",
234 pid, strerror(errno));
Petr Machatacec06ec2012-04-10 13:31:55 +0200235 trace_fail_warning(pid);
Petr Machata3c516d52011-08-18 03:53:18 +0200236 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200237 }
238
Petr Machata3c516d52011-08-18 03:53:18 +0200239 /* Now attach to all tasks that belong to that PID. There's a
240 * race between process_tasks and open_one_pid. So when we
241 * fail in open_one_pid below, we just do another round.
242 * Chances are that by then that PID will have gone away, and
243 * that's why we have seen the failure. The processes that we
244 * manage to open_one_pid are stopped, so we should eventually
245 * reach a point where process_tasks doesn't give any new
246 * processes (because there's nobody left to produce
247 * them). */
Petr Machata1974dbc2011-08-19 18:58:01 +0200248 size_t old_ntasks = 0;
Petr Machata3c516d52011-08-18 03:53:18 +0200249 int have_all;
Petr Machata1974dbc2011-08-19 18:58:01 +0200250 while (1) {
Petr Machata3c516d52011-08-18 03:53:18 +0200251 pid_t *tasks;
252 size_t ntasks;
253 size_t i;
Petr Machata1974dbc2011-08-19 18:58:01 +0200254
Petr Machata3c516d52011-08-18 03:53:18 +0200255 if (process_tasks(pid, &tasks, &ntasks) < 0) {
256 fprintf(stderr, "Cannot obtain tasks of pid %u: %s\n",
257 pid, strerror(errno));
Petr Machatafed1e8d2012-02-07 02:06:29 +0100258 break;
Petr Machata3c516d52011-08-18 03:53:18 +0200259 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200260
Petr Machata3c516d52011-08-18 03:53:18 +0200261 have_all = 1;
262 for (i = 0; i < ntasks; ++i)
263 if (pid2proc(tasks[i]) == NULL
Petr Machata1974dbc2011-08-19 18:58:01 +0200264 && open_one_pid(tasks[i]))
Petr Machata3c516d52011-08-18 03:53:18 +0200265 have_all = 0;
266
Petr Machata9a5420c2011-07-09 11:21:23 +0200267 free(tasks);
Petr Machata3c516d52011-08-18 03:53:18 +0200268
Petr Machata1974dbc2011-08-19 18:58:01 +0200269 if (have_all && old_ntasks == ntasks)
270 break;
271 old_ntasks = ntasks;
272 }
273
274 /* Done. Now initialize breakpoints and then continue
275 * everyone. */
276 Process * leader;
Petr Machata1974dbc2011-08-19 18:58:01 +0200277 leader = pid2proc(pid)->leader;
278 enable_all_breakpoints(leader);
279
Petr Machata74132a42012-03-16 02:46:18 +0100280 each_task(pid2proc(pid)->leader, NULL, start_one_pid, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200281}
282
Petr Machata2b46cfc2012-02-18 11:17:29 +0100283static enum callback_status
Petr Machatacebb8842011-07-09 11:14:11 +0200284find_proc(Process * proc, void * data)
285{
286 pid_t pid = (pid_t)(uintptr_t)data;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100287 return proc->pid == pid ? CBS_STOP : CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200288}
289
Juan Cespedesa8909f72009-04-28 20:02:41 +0200290Process *
Juan Cespedese74c80d2009-02-11 11:32:31 +0100291pid2proc(pid_t pid) {
Petr Machatacebb8842011-07-09 11:14:11 +0200292 return each_process(NULL, &find_proc, (void *)(uintptr_t)pid);
293}
Juan Cespedese74c80d2009-02-11 11:32:31 +0100294
Petr Machatacebb8842011-07-09 11:14:11 +0200295static Process * list_of_processes = NULL;
296
Petr Machatacbe29c62011-09-27 02:27:58 +0200297static void
298unlist_process(Process * proc)
299{
300 Process *tmp;
301
302 if (list_of_processes == proc) {
303 list_of_processes = list_of_processes->next;
304 return;
305 }
306
307 for (tmp = list_of_processes; ; tmp = tmp->next) {
308 /* If the following assert fails, the process wasn't
309 * in the list. */
310 assert(tmp->next != NULL);
311
312 if (tmp->next == proc) {
313 tmp->next = tmp->next->next;
314 return;
315 }
316 }
317}
318
Petr Machata2b46cfc2012-02-18 11:17:29 +0100319struct Process *
Petr Machata74132a42012-03-16 02:46:18 +0100320each_process(struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100321 enum callback_status(*cb)(struct Process *proc, void *data),
322 void *data)
Petr Machatacebb8842011-07-09 11:14:11 +0200323{
Petr Machata74132a42012-03-16 02:46:18 +0100324 struct Process *it = start_after == NULL ? list_of_processes
325 : start_after->next;
326
327 while (it != NULL) {
Petr Machatacebb8842011-07-09 11:14:11 +0200328 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100329 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100330 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200331 case CBS_FAIL:
332 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100333 case CBS_STOP:
Petr Machatacebb8842011-07-09 11:14:11 +0200334 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100335 case CBS_CONT:
336 break;
337 }
Petr Machatacebb8842011-07-09 11:14:11 +0200338 it = next;
339 }
340 return NULL;
341}
Petr Machata9a5420c2011-07-09 11:21:23 +0200342
343Process *
Petr Machata74132a42012-03-16 02:46:18 +0100344each_task(struct Process *proc, struct Process *start_after,
Petr Machata2b46cfc2012-02-18 11:17:29 +0100345 enum callback_status(*cb)(struct Process *proc, void *data),
346 void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200347{
Petr Machata74132a42012-03-16 02:46:18 +0100348 assert(proc != NULL);
349 struct Process *it = start_after == NULL ? proc->leader
350 : start_after->next;
351
Petr Machata9a5420c2011-07-09 11:21:23 +0200352 if (it != NULL) {
Petr Machata74132a42012-03-16 02:46:18 +0100353 struct Process *leader = it->leader;
354 while (it != NULL && it->leader == leader) {
Petr Machata9a5420c2011-07-09 11:21:23 +0200355 /* Callback might call remove_process. */
Petr Machata74132a42012-03-16 02:46:18 +0100356 struct Process *next = it->next;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100357 switch ((*cb)(it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200358 case CBS_FAIL:
359 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100360 case CBS_STOP:
Petr Machata9a5420c2011-07-09 11:21:23 +0200361 return it;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100362 case CBS_CONT:
363 break;
364 }
Petr Machata9a5420c2011-07-09 11:21:23 +0200365 it = next;
366 }
367 }
368 return NULL;
369}
370
Petr Machata44965c72012-04-06 19:59:20 +0200371static void
Petr Machatacebb8842011-07-09 11:14:11 +0200372add_process(Process * proc)
373{
Petr Machata9a5420c2011-07-09 11:21:23 +0200374 Process ** leaderp = &list_of_processes;
375 if (proc->pid) {
376 pid_t tgid = process_leader(proc->pid);
Petr Machata1974dbc2011-08-19 18:58:01 +0200377 if (tgid == 0)
378 /* Must have been terminated before we managed
379 * to fully attach. */
380 return;
Petr Machata9a5420c2011-07-09 11:21:23 +0200381 if (tgid == proc->pid)
382 proc->leader = proc;
383 else {
384 Process * leader = pid2proc(tgid);
385 proc->leader = leader;
386 if (leader != NULL)
Petr Machata9a5420c2011-07-09 11:21:23 +0200387 leaderp = &leader->next;
388 }
389 }
390 proc->next = *leaderp;
391 *leaderp = proc;
392}
393
Petr Machatacbe29c62011-09-27 02:27:58 +0200394void
395change_process_leader(Process * proc, Process * leader)
396{
397 Process ** leaderp = &list_of_processes;
398 if (proc->leader == leader)
399 return;
400
401 assert(leader != NULL);
402 unlist_process(proc);
403 if (proc != leader)
404 leaderp = &leader->next;
405
406 proc->leader = leader;
407 proc->next = *leaderp;
408 *leaderp = proc;
409}
410
Petr Machata2b46cfc2012-02-18 11:17:29 +0100411static enum callback_status
412clear_leader(struct Process *proc, void *data)
Petr Machata9a5420c2011-07-09 11:21:23 +0200413{
414 debug(DEBUG_FUNCTION, "detach_task %d from leader %d",
415 proc->pid, proc->leader->pid);
416 proc->leader = NULL;
Petr Machata2b46cfc2012-02-18 11:17:29 +0100417 return CBS_CONT;
Petr Machatacebb8842011-07-09 11:14:11 +0200418}
419
Petr Machata69a03e62011-07-09 11:29:12 +0200420static enum ecb_status
421event_for_proc(Event * event, void * data)
422{
423 if (event->proc == data)
424 return ecb_deque;
425 else
426 return ecb_cont;
427}
428
429static void
430delete_events_for(Process * proc)
431{
432 Event * event;
433 while ((event = each_qd_event(&event_for_proc, proc)) != NULL)
434 free(event);
435}
436
Petr Machatacebb8842011-07-09 11:14:11 +0200437void
438remove_process(Process *proc)
439{
Petr Machatacebb8842011-07-09 11:14:11 +0200440 debug(DEBUG_FUNCTION, "remove_proc(pid=%d)", proc->pid);
441
Petr Machata9a5420c2011-07-09 11:21:23 +0200442 if (proc->leader == proc)
Petr Machata74132a42012-03-16 02:46:18 +0100443 each_task(proc, NULL, &clear_leader, NULL);
Petr Machata9a5420c2011-07-09 11:21:23 +0200444
Petr Machatacbe29c62011-09-27 02:27:58 +0200445 unlist_process(proc);
446 delete_events_for(proc);
Juan Cespedese74c80d2009-02-11 11:32:31 +0100447}
Petr Machata4007d742011-07-09 11:29:42 +0200448
449void
Petr Machata366c2f42012-02-09 19:34:36 +0100450install_event_handler(Process *proc, struct event_handler *handler)
Petr Machata4007d742011-07-09 11:29:42 +0200451{
Petr Machata75dcf7d2011-10-06 14:30:19 +0200452 debug(DEBUG_FUNCTION, "install_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200453 assert(proc->event_handler == NULL);
454 proc->event_handler = handler;
455}
456
457void
458destroy_event_handler(Process * proc)
459{
Petr Machata366c2f42012-02-09 19:34:36 +0100460 struct event_handler *handler = proc->event_handler;
Petr Machata75dcf7d2011-10-06 14:30:19 +0200461 debug(DEBUG_FUNCTION, "destroy_event_handler(pid=%d, %p)", proc->pid, handler);
Petr Machata4007d742011-07-09 11:29:42 +0200462 assert(handler != NULL);
Petr Machatacbe29c62011-09-27 02:27:58 +0200463 if (handler->destroy != NULL)
464 handler->destroy(handler);
Petr Machata4007d742011-07-09 11:29:42 +0200465 free(handler);
466 proc->event_handler = NULL;
467}
Petr Machata2b46cfc2012-02-18 11:17:29 +0100468
469static enum callback_status
470breakpoint_for_symbol(struct library_symbol *libsym, void *data)
471{
472 struct Process *proc = data;
Petr Machatad5e85562012-04-05 15:18:38 +0200473 assert(proc->leader == proc);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100474
Petr Machatad5e85562012-04-05 15:18:38 +0200475 /* If there is an artificial breakpoint on the same address,
476 * its libsym will be NULL, and we can smuggle our libsym
477 * there. That artificial breakpoint is there presumably for
478 * the callbacks, which we don't touch. If there is a real
479 * breakpoint, then this is a bug. ltrace-elf.c should filter
Petr Machataa2416362012-04-06 02:43:34 +0200480 * symbols and ignore extra symbol aliases.
481 *
482 * The other direction is more complicated and currently not
483 * supported. If a breakpoint has custom callbacks, it might
484 * be also custom-allocated, and we would really need to swap
485 * the two: delete the one now in the dictionary, swap values
486 * around, and put the new breakpoint back in. */
Petr Machatad5e85562012-04-05 15:18:38 +0200487 struct breakpoint *bp = dict_find_entry(proc->breakpoints,
488 libsym->enter_addr);
489 if (bp != NULL) {
490 assert(bp->libsym == NULL);
491 bp->libsym = libsym;
492 return CBS_CONT;
493 }
494
495 bp = malloc(sizeof(*bp));
Petr Machata3fd099b2012-04-03 02:25:42 +0200496 if (bp == NULL
497 || breakpoint_init(bp, proc, libsym->enter_addr, libsym) < 0) {
498 fail:
499 free(bp);
500 return CBS_FAIL;
501 }
502 if (proc_add_breakpoint(proc, bp) < 0) {
503 breakpoint_destroy(bp);
504 goto fail;
505 }
Petr Machata2b46cfc2012-02-18 11:17:29 +0100506
Petr Machatafa0c5702012-04-13 18:43:40 +0200507 if (breakpoint_turn_on(bp, proc) < 0) {
Petr Machata76dd9292012-04-03 13:02:06 +0200508 proc_remove_breakpoint(proc, bp);
509 breakpoint_destroy(bp);
510 goto fail;
511 }
512
Petr Machata2b46cfc2012-02-18 11:17:29 +0100513 return CBS_CONT;
514}
515
516void
517proc_add_library(struct Process *proc, struct library *lib)
518{
519 assert(lib->next == NULL);
520 lib->next = proc->libraries;
521 proc->libraries = lib;
Petr Machata8b00d5b2012-04-06 16:05:10 +0200522 debug(DEBUG_PROCESS, "added library %s@%p (%s) to %d",
523 lib->soname, lib->base, lib->pathname, proc->pid);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100524
525 struct library_symbol *libsym = NULL;
526 while ((libsym = library_each_symbol(lib, libsym, breakpoint_for_symbol,
Petr Machata74132a42012-03-16 02:46:18 +0100527 proc)) != NULL)
Petr Machata2b46cfc2012-02-18 11:17:29 +0100528 error(0, errno, "insert breakpoint for %s", libsym->name);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100529}
530
531int
532proc_remove_library(struct Process *proc, struct library *lib)
533{
534 struct library **libp;
535 for (libp = &proc->libraries; *libp != NULL; libp = &(*libp)->next)
536 if (*libp == lib) {
537 *libp = lib->next;
538 return 0;
539 }
540 return -1;
541}
542
543struct library *
544proc_each_library(struct Process *proc, struct library *it,
545 enum callback_status (*cb)(struct Process *proc,
546 struct library *lib, void *data),
547 void *data)
548{
549 if (it == NULL)
550 it = proc->libraries;
551
552 while (it != NULL) {
553 struct library *next = it->next;
554
555 switch (cb(proc, it, data)) {
Petr Machataef7fa372012-03-28 02:05:36 +0200556 case CBS_FAIL:
557 /* XXX handle me */
Petr Machata2b46cfc2012-02-18 11:17:29 +0100558 case CBS_STOP:
559 return it;
560 case CBS_CONT:
561 break;
562 }
563
564 it = next;
565 }
566
567 return NULL;
568}
Petr Machata52dbfb12012-03-29 16:38:26 +0200569
570int
571proc_add_breakpoint(struct Process *proc, struct breakpoint *bp)
572{
Petr Machata52dbfb12012-03-29 16:38:26 +0200573 /* Only the group leader should be getting the breakpoints and
574 * thus have ->breakpoint initialized. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200575 assert(proc->leader != NULL);
576 assert(proc->leader == proc);
577 assert(proc->breakpoints != NULL);
Petr Machata52dbfb12012-03-29 16:38:26 +0200578
Petr Machatafa0c5702012-04-13 18:43:40 +0200579 debug(DEBUG_FUNCTION, "proc_add_breakpoint(pid=%d, %s@%p)",
Petr Machata52dbfb12012-03-29 16:38:26 +0200580 proc->pid, breakpoint_name(bp), bp->addr);
581
Petr Machataa2416362012-04-06 02:43:34 +0200582 /* XXX We might merge bp->libsym instead of the following
Petr Machata00928202012-04-07 01:14:24 +0200583 * assert, but that's not necessary right now. Read the
584 * comment in breakpoint_for_symbol. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200585 assert(dict_find_entry(proc->breakpoints, bp->addr) == NULL);
Petr Machataa2416362012-04-06 02:43:34 +0200586
Petr Machatafa0c5702012-04-13 18:43:40 +0200587 if (dict_enter(proc->breakpoints, bp->addr, bp) < 0) {
Petr Machata52dbfb12012-03-29 16:38:26 +0200588 error(0, errno, "couldn't enter breakpoint %s@%p to dictionary",
589 breakpoint_name(bp), bp->addr);
590 return -1;
591 }
592
Petr Machata52dbfb12012-03-29 16:38:26 +0200593 return 0;
594}
595
596int
597proc_remove_breakpoint(struct Process *proc, struct breakpoint *bp)
598{
599 /* XXX We can't, really. We are missing dict_remove. */
600 assert(!"Not yet implemented!");
601 abort();
602}
Petr Machatad3cc9882012-04-13 21:40:23 +0200603
604/* Dict doesn't support iteration restarts, so here's this contraption
605 * for now. XXX add restarts to dict. */
606struct each_breakpoint_data
607{
608 void *start;
609 void *end;
610 struct Process *proc;
611 enum callback_status (*cb)(struct Process *proc,
612 struct breakpoint *bp,
613 void *data);
614 void *cb_data;
615};
616
617static void
618each_breakpoint_cb(void *key, void *value, void *d)
619{
620 struct each_breakpoint_data *data = d;
621 if (data->end != NULL)
622 return;
623 if (data->start == key)
624 data->start = NULL;
625
626 if (data->start == NULL) {
627 switch (data->cb(data->proc, value, data->cb_data)) {
628 case CBS_FAIL:
629 /* XXX handle me */
630 case CBS_STOP:
631 data->end = key;
632 case CBS_CONT:
633 return;
634 }
635 }
636}
637
638void *
639proc_each_breakpoint(struct Process *proc, void *start,
640 enum callback_status (*cb)(struct Process *proc,
641 struct breakpoint *bp,
642 void *data), void *data)
643{
644 struct each_breakpoint_data dd = {
645 .start = start,
646 .proc = proc,
647 .cb = cb,
648 .cb_data = data,
649 };
650 dict_apply_to_all(proc->breakpoints, &each_breakpoint_cb, &dd);
651 return dd.end;
652}