Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of ltrace. |
Petr Machata | 56134ff | 2014-01-10 20:05:15 +0100 | [diff] [blame] | 3 | * Copyright (C) 2012,2013,2014 Petr Machata, Red Hat Inc. |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 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 | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 44 | #include "forward.h" |
Petr Machata | a9fd8f4 | 2012-02-07 13:25:56 +0100 | [diff] [blame] | 45 | |
| 46 | struct bp_callbacks { |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 47 | void (*on_hit)(struct breakpoint *bp, struct process *proc); |
| 48 | void (*on_continue)(struct breakpoint *bp, struct process *proc); |
Petr Machata | 56134ff | 2014-01-10 20:05:15 +0100 | [diff] [blame] | 49 | void (*on_install)(struct breakpoint *bp, struct process *proc); |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 50 | void (*on_retract)(struct breakpoint *bp, struct process *proc); |
Petr Machata | cf98923 | 2013-10-11 21:17:24 +0200 | [diff] [blame] | 51 | |
| 52 | /* Create a new breakpoint that should handle return from the |
| 53 | * function. BP is the breakpoint that was just hit and for |
| 54 | * which we wish to find the corresponding return breakpoint. |
| 55 | * This returns 0 on success (in which case *RET will have |
| 56 | * been initialized to desired breakpoint object, or NULL if |
| 57 | * none is necessary) or a negative value on failure. */ |
| 58 | int (*get_return_bp)(struct breakpoint **ret, |
| 59 | struct breakpoint *bp, struct process *proc); |
Petr Machata | a9fd8f4 | 2012-02-07 13:25:56 +0100 | [diff] [blame] | 60 | }; |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 61 | |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 62 | struct breakpoint { |
Petr Machata | a9fd8f4 | 2012-02-07 13:25:56 +0100 | [diff] [blame] | 63 | struct bp_callbacks *cbs; |
Petr Machata | 29add4f | 2012-02-18 16:38:05 +0100 | [diff] [blame] | 64 | struct library_symbol *libsym; |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 65 | void *addr; |
| 66 | unsigned char orig_value[BREAKPOINT_LENGTH]; |
| 67 | int enabled; |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 68 | struct arch_breakpoint_data arch; |
Petr Machata | 9f819d5 | 2013-10-16 14:46:24 +0200 | [diff] [blame] | 69 | struct os_breakpoint_data os; |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 70 | }; |
| 71 | |
Petr Machata | 2f140bf | 2013-10-11 21:15:19 +0200 | [diff] [blame] | 72 | /* Call ON_HIT handler of BP, if any is set. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 73 | void breakpoint_on_hit(struct breakpoint *bp, struct process *proc); |
Petr Machata | a9fd8f4 | 2012-02-07 13:25:56 +0100 | [diff] [blame] | 74 | |
Petr Machata | 2f140bf | 2013-10-11 21:15:19 +0200 | [diff] [blame] | 75 | /* Call ON_CONTINUE handler of BP. If none is set, call |
Petr Machata | 56a9ea6 | 2012-03-27 03:09:29 +0200 | [diff] [blame] | 76 | * continue_after_breakpoint. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 77 | void breakpoint_on_continue(struct breakpoint *bp, struct process *proc); |
Petr Machata | 56a9ea6 | 2012-03-27 03:09:29 +0200 | [diff] [blame] | 78 | |
Petr Machata | 2f140bf | 2013-10-11 21:15:19 +0200 | [diff] [blame] | 79 | /* Call ON_RETRACT handler of BP, if any is set. This should be |
Petr Machata | 86d3828 | 2012-04-24 18:09:01 +0200 | [diff] [blame] | 80 | * called before the breakpoints are destroyed. The reason for a |
| 81 | * separate interface is that breakpoint_destroy has to be callable |
| 82 | * without PROC. ON_DISABLE might be useful as well, but that would |
| 83 | * be called every time we disable the breakpoint, which is too often |
| 84 | * (a breakpoint has to be disabled every time that we need to execute |
| 85 | * the instruction underneath it). */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 86 | void breakpoint_on_retract(struct breakpoint *bp, struct process *proc); |
Petr Machata | 86d3828 | 2012-04-24 18:09:01 +0200 | [diff] [blame] | 87 | |
Petr Machata | 56134ff | 2014-01-10 20:05:15 +0100 | [diff] [blame] | 88 | /* Call ON_INSTALL handler of BP, if any is set. This should be |
| 89 | * called after the breakpoint is enabled for the first time, not |
| 90 | * every time it's enabled (such as after stepping over a site of a |
| 91 | * temporarily disabled breakpoint). */ |
| 92 | void breakpoint_on_install(struct breakpoint *bp, struct process *proc); |
| 93 | |
Petr Machata | cf98923 | 2013-10-11 21:17:24 +0200 | [diff] [blame] | 94 | /* Call GET_RETURN_BP handler of BP, if any is set. If none is set, |
| 95 | * call CREATE_DEFAULT_RETURN_BP to obtain one. */ |
| 96 | int breakpoint_get_return_bp(struct breakpoint **ret, |
| 97 | struct breakpoint *bp, struct process *proc); |
| 98 | |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 99 | /* Initialize a breakpoint structure. That doesn't actually realize |
| 100 | * the breakpoint. The breakpoint is initially assumed to be |
| 101 | * disabled. orig_value has to be set separately. CBS may be |
| 102 | * NULL. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 103 | int breakpoint_init(struct breakpoint *bp, struct process *proc, |
Petr Machata | bac2da5 | 2012-05-29 00:42:59 +0200 | [diff] [blame] | 104 | arch_addr_t addr, struct library_symbol *libsym); |
Petr Machata | 55ac932 | 2012-03-27 03:07:35 +0200 | [diff] [blame] | 105 | |
Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 106 | /* Make a clone of breakpoint BP into the area of memory pointed to by |
Petr Machata | e503552 | 2013-01-30 17:48:51 +0100 | [diff] [blame] | 107 | * RETP. Symbols of cloned breakpoint are looked up in NEW_PROC. |
Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 108 | * Returns 0 on success or a negative value on failure. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 109 | int breakpoint_clone(struct breakpoint *retp, struct process *new_proc, |
Petr Machata | e503552 | 2013-01-30 17:48:51 +0100 | [diff] [blame] | 110 | struct breakpoint *bp); |
Petr Machata | d3cc988 | 2012-04-13 21:40:23 +0200 | [diff] [blame] | 111 | |
Petr Machata | 55ac932 | 2012-03-27 03:07:35 +0200 | [diff] [blame] | 112 | /* Set callbacks. If CBS is non-NULL, then BP->cbs shall be NULL. */ |
| 113 | void breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs); |
Petr Machata | 2b46cfc | 2012-02-18 11:17:29 +0100 | [diff] [blame] | 114 | |
Petr Machata | 0fe76c6 | 2012-04-14 02:29:30 +0200 | [diff] [blame] | 115 | /* Destroy a breakpoint structure. */ |
Petr Machata | 8cce119 | 2012-03-25 01:37:19 +0100 | [diff] [blame] | 116 | void breakpoint_destroy(struct breakpoint *bp); |
| 117 | |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 118 | /* Call enable_breakpoint the first time it's called. Returns 0 on |
| 119 | * success and a negative value on failure. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 120 | int breakpoint_turn_on(struct breakpoint *bp, struct process *proc); |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 121 | |
| 122 | /* Call disable_breakpoint when turned off the same number of times |
| 123 | * that it was turned on. Returns 0 on success and a negative value |
| 124 | * on failure. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 125 | int breakpoint_turn_off(struct breakpoint *bp, struct process *proc); |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 126 | |
Petr Machata | cf98923 | 2013-10-11 21:17:24 +0200 | [diff] [blame] | 127 | /* Allocate and initialize a default return breakpoint. Returns NULL |
| 128 | * on failure. */ |
| 129 | struct breakpoint *create_default_return_bp(struct process *proc); |
| 130 | |
Petr Machata | dad1b77 | 2013-10-11 16:14:44 +0200 | [diff] [blame] | 131 | /* This allocates and initializes new breakpoint at ADDR, then calls |
| 132 | * INSERT_BREAKPOINT. Returns the new breakpoint or NULL if there are |
| 133 | * errors. */ |
Petr Machata | 02a796e | 2013-10-11 17:24:30 +0200 | [diff] [blame] | 134 | struct breakpoint *insert_breakpoint_at(struct process *proc, arch_addr_t addr, |
| 135 | struct library_symbol *libsym); |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 136 | |
Petr Machata | dad1b77 | 2013-10-11 16:14:44 +0200 | [diff] [blame] | 137 | /* Check if there is a breakpoint on this address already. If yes, |
| 138 | * return that breakpoint instead (BP was not added). If no, try to |
| 139 | * PROC_ADD_BREAKPOINT and BREAKPOINT_TURN_ON. If it all works, |
| 140 | * return BP. Otherwise return NULL. */ |
| 141 | struct breakpoint *insert_breakpoint(struct process *proc, |
| 142 | struct breakpoint *bp); |
| 143 | |
Petr Machata | e9aebd6 | 2012-03-25 01:38:53 +0100 | [diff] [blame] | 144 | /* Name of a symbol associated with BP. May be NULL. */ |
| 145 | const char *breakpoint_name(const struct breakpoint *bp); |
| 146 | |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 147 | /* A library that this breakpoint comes from. May be NULL. */ |
| 148 | struct library *breakpoint_library(const struct breakpoint *bp); |
| 149 | |
Petr Machata | 8cce119 | 2012-03-25 01:37:19 +0100 | [diff] [blame] | 150 | /* Again, this seems to be several interfaces rolled into one: |
| 151 | * - breakpoint_disable |
Petr Machata | 52dbfb1 | 2012-03-29 16:38:26 +0200 | [diff] [blame] | 152 | * - proc_remove_breakpoint |
Petr Machata | 8cce119 | 2012-03-25 01:37:19 +0100 | [diff] [blame] | 153 | * - breakpoint_destroy |
| 154 | * XXX */ |
Petr Machata | b944077 | 2013-10-14 10:36:26 +0200 | [diff] [blame] | 155 | void delete_breakpoint_at(struct process *proc, void *addr); |
| 156 | int delete_breakpoint(struct process *proc, struct breakpoint *bp); |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 157 | |
| 158 | /* XXX some of the following belongs to proc.h/proc.c. */ |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 159 | struct breakpoint *address2bpstruct(struct process *proc, void *addr); |
Petr Machata | 929bd57 | 2012-12-17 03:20:34 +0100 | [diff] [blame] | 160 | void disable_all_breakpoints(struct process *proc); |
| 161 | int breakpoints_init(struct process *proc); |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 162 | |
Petr Machata | 9294d82 | 2012-02-07 12:35:58 +0100 | [diff] [blame] | 163 | #endif /* BREAKPOINT_H */ |