Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
| 3 | * Copyright (C) 2012 Petr Machata, Red Hat Inc. |
| 4 | * Copyright (C) 2009 Juan Cespedes |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of the |
| 9 | * License, or (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 19 | * 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef BREAKPOINT_H |
| 23 | #define BREAKPOINT_H |
| 24 | |
| 25 | /* XXX This is currently a very weak abstraction. We would like to |
| 26 | * much expand this to allow things like breakpoints on SDT probes and |
| 27 | * such. |
| 28 | * |
| 29 | * In particular, we would like to add a tracepoint abstraction. |
| 30 | * Tracepoint is a traceable feature--e.g. an exact address, a DWARF |
| 31 | * symbol, an ELF symbol, a PLT entry, or an SDT probe. Tracepoints |
| 32 | * are named and the user can configure which of them he wants to |
| 33 | * enable. Realized tracepoints enable breakpoints, which are a |
| 34 | * low-level realization of high-level tracepoint. |
| 35 | * |
Petr Machata | 0c3377e | 2012-04-16 18:43:00 +0200 | [diff] [blame] | 36 | * Service breakpoints like the handling of dlopen would be a |
| 37 | * low-level breakpoint, likely without tracepoint attached. |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 38 | * |
| 39 | * So that's for sometimes. |
| 40 | */ |
| 41 | |
Petr Machata | d3dc17b | 2012-03-21 03:23:25 +0100 | [diff] [blame] | 42 | #include "sysdep.h" |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 43 | #include "library.h" |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 44 | |
| 45 | struct Process; |
Petr Machata | a9fd8f4 | 2012-02-07 13:25:56 +0100 | [diff] [blame] | 46 | struct breakpoint; |
| 47 | |
| 48 | struct bp_callbacks { |
Petr Machata | d09d240 | 2012-04-13 21:34:08 +0200 | [diff] [blame] | 49 | void (*on_hit)(struct breakpoint *bp, struct Process *proc); |
| 50 | void (*on_continue)(struct breakpoint *bp, struct Process *proc); |
Petr Machata | a9fd8f4 | 2012-02-07 13:25:56 +0100 | [diff] [blame] | 51 | }; |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 52 | |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 53 | struct breakpoint { |
Petr Machata | a9fd8f4 | 2012-02-07 13:25:56 +0100 | [diff] [blame] | 54 | struct bp_callbacks *cbs; |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 55 | struct library_symbol *libsym; |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 56 | void *addr; |
| 57 | unsigned char orig_value[BREAKPOINT_LENGTH]; |
| 58 | int enabled; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 59 | struct arch_breakpoint_data arch; |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 60 | }; |
| 61 | |
Petr Machata | a9fd8f4 | 2012-02-07 13:25:56 +0100 | [diff] [blame] | 62 | /* Call on-hit handler of BP, if any is set. */ |
| 63 | void breakpoint_on_hit(struct breakpoint *bp, struct Process *proc); |
| 64 | |
Petr Machata | d09d240 | 2012-04-13 21:34:08 +0200 | [diff] [blame] | 65 | /* Call on-continue handler of BP. If none is set, call |
Petr Machata | 56a9ea6 | 2012-03-27 03:09:29 +0200 | [diff] [blame] | 66 | * continue_after_breakpoint. */ |
| 67 | void breakpoint_on_continue(struct breakpoint *bp, struct Process *proc); |
| 68 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 69 | /* Initialize a breakpoint structure. That doesn't actually realize |
| 70 | * the breakpoint. The breakpoint is initially assumed to be |
| 71 | * disabled. orig_value has to be set separately. CBS may be |
| 72 | * NULL. */ |
| 73 | int breakpoint_init(struct breakpoint *bp, struct Process *proc, |
Petr Machata | 55ac932 | 2012-03-27 03:07:35 +0200 | [diff] [blame] | 74 | target_address_t addr, struct library_symbol *libsym); |
| 75 | |
Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 76 | /* Make a clone of breakpoint BP into the area of memory pointed to by |
| 77 | * RETP. The original breakpoint was assigned to process OLD_PROC, |
| 78 | * the cloned breakpoint will be attached to process NEW_PROC. |
| 79 | * Returns 0 on success or a negative value on failure. */ |
| 80 | int breakpoint_clone(struct breakpoint *retp, struct Process *new_proc, |
| 81 | struct breakpoint *bp, struct Process *old_proc); |
| 82 | |
Petr Machata | 55ac932 | 2012-03-27 03:07:35 +0200 | [diff] [blame] | 83 | /* Set callbacks. If CBS is non-NULL, then BP->cbs shall be NULL. */ |
| 84 | void breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 85 | |
Petr Machata | 0fe76c6 | 2012-04-14 02:29:30 +0200 | [diff] [blame] | 86 | /* Destroy a breakpoint structure. */ |
Petr Machata | 8cce119 | 2012-03-25 01:37:19 +0100 | [diff] [blame] | 87 | void breakpoint_destroy(struct breakpoint *bp); |
| 88 | |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 89 | /* Call enable_breakpoint the first time it's called. Returns 0 on |
| 90 | * success and a negative value on failure. */ |
Petr Machata | fa0c570 | 2012-04-13 18:43:40 +0200 | [diff] [blame] | 91 | int breakpoint_turn_on(struct breakpoint *bp, struct Process *proc); |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 92 | |
| 93 | /* Call disable_breakpoint when turned off the same number of times |
| 94 | * that it was turned on. Returns 0 on success and a negative value |
| 95 | * on failure. */ |
Petr Machata | fa0c570 | 2012-04-13 18:43:40 +0200 | [diff] [blame] | 96 | int breakpoint_turn_off(struct breakpoint *bp, struct Process *proc); |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 97 | |
Petr Machata | 051dffa | 2012-04-15 04:19:17 +0200 | [diff] [blame] | 98 | /* Utility function that does what typically needs to be done when a |
| 99 | * breakpoint is to be inserted. It checks whether there is another |
Petr Machata | 622581f | 2012-04-23 19:40:40 +0200 | [diff] [blame^] | 100 | * breakpoint in PROC->LEADER for given ADDR. If not, it allocates |
| 101 | * memory for a new breakpoint on the heap, initializes it, and calls |
Petr Machata | 051dffa | 2012-04-15 04:19:17 +0200 | [diff] [blame] | 102 | * PROC_ADD_BREAKPOINT to add the newly-created breakpoint. For newly |
| 103 | * added as well as preexisting breakpoints, it then calls |
| 104 | * BREAKPOINT_TURN_ON. If anything fails, it cleans up and returns |
| 105 | * NULL. Otherwise it returns the breakpoint for ADDR. */ |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 106 | struct breakpoint *insert_breakpoint(struct Process *proc, void *addr, |
Petr Machata | 9df1501 | 2012-02-20 12:49:46 +0100 | [diff] [blame] | 107 | struct library_symbol *libsym); |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 108 | |
Petr Machata | e9aebd6 | 2012-03-25 01:38:53 +0100 | [diff] [blame] | 109 | /* Name of a symbol associated with BP. May be NULL. */ |
| 110 | const char *breakpoint_name(const struct breakpoint *bp); |
| 111 | |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 112 | /* A library that this breakpoint comes from. May be NULL. */ |
| 113 | struct library *breakpoint_library(const struct breakpoint *bp); |
| 114 | |
Petr Machata | 8cce119 | 2012-03-25 01:37:19 +0100 | [diff] [blame] | 115 | /* Again, this seems to be several interfaces rolled into one: |
| 116 | * - breakpoint_disable |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 117 | * - proc_remove_breakpoint |
Petr Machata | 8cce119 | 2012-03-25 01:37:19 +0100 | [diff] [blame] | 118 | * - breakpoint_destroy |
| 119 | * XXX */ |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 120 | void delete_breakpoint(struct Process *proc, void *addr); |
| 121 | |
| 122 | /* XXX some of the following belongs to proc.h/proc.c. */ |
| 123 | struct breakpoint *address2bpstruct(struct Process *proc, void *addr); |
| 124 | void enable_all_breakpoints(struct Process *proc); |
| 125 | void disable_all_breakpoints(struct Process *proc); |
Petr Machata | 75934ad | 2012-04-14 02:28:03 +0200 | [diff] [blame] | 126 | int breakpoints_init(struct Process *proc); |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 127 | |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 128 | #endif /* BREAKPOINT_H */ |