2 * This file is part of ltrace.
3 * Copyright (C) 2006,2007,2011,2012,2013 Petr Machata, Red Hat Inc.
4 * Copyright (C) 2009 Juan Cespedes
5 * Copyright (C) 1998,2001,2002,2003,2007,2008,2009 Juan Cespedes
6 * Copyright (C) 2006 Ian Wienand
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
33 #include <sys/ptrace.h>
37 #include "breakpoint.h"
40 #include "ltrace-elf.h"
43 #ifndef ARCH_HAVE_TRANSLATE_ADDRESS
45 arch_translate_address_dyn(struct process *proc,
46 arch_addr_t addr, arch_addr_t *ret)
54 arch_translate_address(struct ltelf *lte,
55 arch_addr_t addr, arch_addr_t *ret)
63 breakpoint_on_hit(struct breakpoint *bp, struct process *proc)
66 if (bp->cbs != NULL && bp->cbs->on_hit != NULL)
67 (bp->cbs->on_hit)(bp, proc);
71 breakpoint_on_continue(struct breakpoint *bp, struct process *proc)
74 if (bp->cbs != NULL && bp->cbs->on_continue != NULL)
75 (bp->cbs->on_continue)(bp, proc);
77 continue_after_breakpoint(proc, bp);
81 breakpoint_on_retract(struct breakpoint *bp, struct process *proc)
84 if (bp->cbs != NULL && bp->cbs->on_retract != NULL)
85 (bp->cbs->on_retract)(bp, proc);
89 breakpoint_get_return_bp(struct breakpoint **ret,
90 struct breakpoint *bp, struct process *proc)
93 if (bp->cbs != NULL && bp->cbs->get_return_bp != NULL)
94 return (bp->cbs->get_return_bp)(ret, bp, proc);
96 if ((*ret = create_default_return_bp(proc)) == NULL)
102 /*****************************************************************************/
105 address2bpstruct(struct process *proc, arch_addr_t addr)
107 assert(proc != NULL);
108 assert(proc->breakpoints != NULL);
109 assert(proc->leader == proc);
110 debug(DEBUG_FUNCTION, "address2bpstruct(pid=%d, addr=%p)", proc->pid, addr);
112 struct breakpoint *found;
113 if (DICT_FIND_VAL(proc->breakpoints, &addr, &found) < 0)
118 #ifndef OS_HAVE_BREAKPOINT_DATA
120 os_breakpoint_init(struct process *proc, struct breakpoint *sbp)
126 os_breakpoint_destroy(struct breakpoint *sbp)
131 os_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
137 #ifndef ARCH_HAVE_BREAKPOINT_DATA
139 arch_breakpoint_init(struct process *proc, struct breakpoint *sbp)
145 arch_breakpoint_destroy(struct breakpoint *sbp)
150 arch_breakpoint_clone(struct breakpoint *retp, struct breakpoint *sbp)
157 breakpoint_init_base(struct breakpoint *bp,
158 arch_addr_t addr, struct library_symbol *libsym)
162 memset(bp->orig_value, 0, sizeof(bp->orig_value));
167 /* On second thought, I don't think we need PROC. All the translation
168 * (arch_translate_address in particular) should be doable using
169 * static lookups of various sections in the ELF file. We shouldn't
170 * need process for anything. */
172 breakpoint_init(struct breakpoint *bp, struct process *proc,
173 arch_addr_t addr, struct library_symbol *libsym)
175 breakpoint_init_base(bp, addr, libsym);
176 if (os_breakpoint_init(proc, bp) < 0)
178 if (arch_breakpoint_init(proc, bp) < 0) {
179 os_breakpoint_destroy(bp);
186 breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs)
189 assert(bp->cbs == NULL);
194 breakpoint_destroy(struct breakpoint *bp)
198 arch_breakpoint_destroy(bp);
199 os_breakpoint_destroy(bp);
203 breakpoint_clone(struct breakpoint *retp, struct process *new_proc,
204 struct breakpoint *bp)
206 struct library_symbol *libsym = NULL;
207 if (bp->libsym != NULL) {
208 int rc = proc_find_symbol(new_proc, bp->libsym, NULL, &libsym);
212 breakpoint_init_base(retp, bp->addr, libsym);
213 memcpy(retp->orig_value, bp->orig_value, sizeof(bp->orig_value));
214 retp->enabled = bp->enabled;
215 if (os_breakpoint_clone(retp, bp) < 0)
217 if (arch_breakpoint_clone(retp, bp) < 0) {
218 os_breakpoint_destroy(retp);
221 breakpoint_set_callbacks(retp, bp->cbs);
226 breakpoint_turn_on(struct breakpoint *bp, struct process *proc)
229 if (bp->enabled == 1) {
230 assert(proc->pid != 0);
231 enable_breakpoint(proc, bp);
237 breakpoint_turn_off(struct breakpoint *bp, struct process *proc)
240 if (bp->enabled == 0)
241 disable_breakpoint(proc, bp);
242 assert(bp->enabled >= 0);
247 create_default_return_bp(struct process *proc)
249 struct breakpoint *bp = malloc(sizeof *bp);
250 arch_addr_t return_addr = get_return_addr(proc, proc->stack_pointer);
251 if (return_addr == 0 || bp == NULL
252 || breakpoint_init(bp, proc, return_addr, NULL) < 0) {
260 insert_breakpoint_at(struct process *proc, arch_addr_t addr,
261 struct library_symbol *libsym)
263 debug(DEBUG_FUNCTION,
264 "insert_breakpoint_at(pid=%d, addr=%p, symbol=%s)",
265 proc->pid, addr, libsym ? libsym->name : "NULL");
269 struct breakpoint *bp = malloc(sizeof *bp);
270 if (bp == NULL || breakpoint_init(bp, proc, addr, libsym) < 0) {
275 /* N.B. (and XXX): BP->addr might differ from ADDR. On ARM
276 * this is a real possibility. The problem here is that to
277 * create a return breakpoint ltrace calls get_return_addr and
278 * then insert_breakpoint_at. So get_return_addr needs to
279 * encode all the information necessary for breakpoint_init
280 * into the address itself, so ADDR is potentially
283 struct breakpoint *tmp = insert_breakpoint(proc, bp);
285 breakpoint_destroy(bp);
292 insert_breakpoint(struct process *proc, struct breakpoint *bp)
294 /* Only the group leader should be getting the breakpoints and
295 * thus have ->breakpoint initialized. */
296 struct process *leader = proc->leader;
297 assert(leader != NULL);
298 assert(leader->breakpoints != NULL);
300 /* XXX what we need to do instead is have a list of
301 * breakpoints that are enabled at this address. The
302 * following works if every breakpoint is the same and there's
303 * no extra data, but that doesn't hold anymore. For now it
304 * will suffice, about the only realistic case where we need
305 * to have more than one breakpoint per address is return from
306 * a recursive library call. */
307 struct breakpoint *ext_bp = bp;
308 if (DICT_FIND_VAL(leader->breakpoints, &bp->addr, &ext_bp) != 0) {
309 if (proc_add_breakpoint(leader, bp) < 0)
314 if (breakpoint_turn_on(ext_bp, proc) < 0) {
316 proc_remove_breakpoint(leader, bp);
324 delete_breakpoint_at(struct process *proc, arch_addr_t addr)
326 debug(DEBUG_FUNCTION, "delete_breakpoint_at(pid=%d, addr=%p)",
329 struct process *leader = proc->leader;
330 assert(leader != NULL);
332 struct breakpoint *bp = NULL;
333 DICT_FIND_VAL(leader->breakpoints, &addr, &bp);
336 if (delete_breakpoint(proc, bp) < 0) {
337 fprintf(stderr, "Couldn't turn off the breakpoint %s@%p\n",
338 breakpoint_name(bp), bp->addr);
343 delete_breakpoint(struct process *proc, struct breakpoint *bp)
345 struct process *leader = proc->leader;
346 assert(leader != NULL);
348 if (breakpoint_turn_off(bp, proc) < 0)
351 if (bp->enabled == 0) {
352 proc_remove_breakpoint(leader, bp);
353 breakpoint_destroy(bp);
361 breakpoint_name(const struct breakpoint *bp)
364 return bp->libsym != NULL ? bp->libsym->name : NULL;
368 breakpoint_library(const struct breakpoint *bp)
371 return bp->libsym != NULL ? bp->libsym->lib : NULL;
374 static enum callback_status
375 enable_bp_cb(arch_addr_t *addr, struct breakpoint **bpp, void *data)
377 struct process *proc = data;
378 debug(DEBUG_FUNCTION, "enable_bp_cb(pid=%d)", proc->pid);
380 enable_breakpoint(proc, *bpp);
385 enable_all_breakpoints(struct process *proc)
387 debug(DEBUG_FUNCTION, "enable_all_breakpoints(pid=%d)", proc->pid);
389 debug(1, "Enabling breakpoints for pid %u...", proc->pid);
390 if (proc->breakpoints != NULL)
391 DICT_EACH(proc->breakpoints, arch_addr_t, struct breakpoint *,
392 NULL, enable_bp_cb, proc);
395 static enum callback_status
396 disable_bp_cb(arch_addr_t *addr, struct breakpoint **bpp, void *data)
398 struct process *proc = data;
399 debug(DEBUG_FUNCTION, "disable_bp_cb(pid=%d)", proc->pid);
401 disable_breakpoint(proc, *bpp);
406 disable_all_breakpoints(struct process *proc)
408 debug(DEBUG_FUNCTION, "disable_all_breakpoints(pid=%d)", proc->pid);
409 assert(proc->leader == proc);
410 DICT_EACH(proc->breakpoints, arch_addr_t, struct breakpoint *,
411 NULL, disable_bp_cb, proc);
415 entry_breakpoint_on_hit(struct breakpoint *bp, struct process *proc)
417 if (proc == NULL || proc->leader == NULL)
419 delete_breakpoint_at(proc, bp->addr);
420 process_hit_start(proc);
424 entry_breakpoint_init(struct process *proc,
425 struct breakpoint *bp, arch_addr_t addr,
429 int err = breakpoint_init(bp, proc, addr, NULL);
433 static struct bp_callbacks entry_callbacks = {
434 .on_hit = entry_breakpoint_on_hit,
436 bp->cbs = &entry_callbacks;
441 breakpoints_init(struct process *proc)
443 debug(DEBUG_FUNCTION, "breakpoints_init(pid=%d)", proc->pid);
445 /* XXX breakpoint dictionary should be initialized
446 * outside. Here we just put in breakpoints. */
447 assert(proc->breakpoints != NULL);
449 /* Only the thread group leader should hold the breakpoints. */
450 assert(proc->leader == proc);
452 /* N.B. the following used to be conditional on this, and
453 * maybe it still needs to be. */
454 assert(proc->filename != NULL);
456 struct library *lib = ltelf_read_main_binary(proc, proc->filename);
457 struct breakpoint *entry_bp = NULL;
460 switch ((int)(lib != NULL)) {
464 proc_remove_library(proc, lib);
465 proc_remove_breakpoint(proc, entry_bp);
467 breakpoint_destroy(entry_bp);
469 library_destroy(lib);
475 entry_bp = malloc(sizeof(*entry_bp));
477 || (entry_breakpoint_init(proc, entry_bp,
478 lib->entry, lib)) < 0) {
480 "Couldn't initialize entry breakpoint for PID %d.\n"
481 "Some tracing events may be missed.\n", proc->pid);
487 if ((result = proc_add_breakpoint(proc, entry_bp)) < 0)
491 if ((result = breakpoint_turn_on(entry_bp, proc)) < 0)
494 proc_add_library(proc, lib);
496 proc->callstack_depth = 0;