3c9de9ee38deb1b7210452c3b21fd36d13cde0fa
[platform/upstream/ltrace.git] / breakpoint.h
1 /*
2  * This file is part of ltrace.
3  * Copyright (C) 2012, 2013 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  *
36  * Service breakpoints like the handling of dlopen would be a
37  * low-level breakpoint, likely without tracepoint attached.
38  *
39  * So that's for sometimes.
40  */
41
42 #include "sysdep.h"
43 #include "library.h"
44 #include "forward.h"
45
46 struct bp_callbacks {
47         void (*on_hit)(struct breakpoint *bp, struct process *proc);
48         void (*on_continue)(struct breakpoint *bp, struct process *proc);
49         void (*on_retract)(struct breakpoint *bp, struct process *proc);
50
51         /* Create a new breakpoint that should handle return from the
52          * function.  BP is the breakpoint that was just hit and for
53          * which we wish to find the corresponding return breakpoint.
54          * This returns 0 on success (in which case *RET will have
55          * been initialized to desired breakpoint object, or NULL if
56          * none is necessary) or a negative value on failure.  */
57         int (*get_return_bp)(struct breakpoint **ret,
58                              struct breakpoint *bp, struct process *proc);
59 };
60
61 struct breakpoint {
62         struct bp_callbacks *cbs;
63         struct library_symbol *libsym;
64         void *addr;
65         unsigned char orig_value[BREAKPOINT_LENGTH];
66         int enabled;
67         struct arch_breakpoint_data arch;
68         struct os_breakpoint_data os;
69 };
70
71 /* Call ON_HIT handler of BP, if any is set.  */
72 void breakpoint_on_hit(struct breakpoint *bp, struct process *proc);
73
74 /* Call ON_CONTINUE handler of BP.  If none is set, call
75  * continue_after_breakpoint.  */
76 void breakpoint_on_continue(struct breakpoint *bp, struct process *proc);
77
78 /* Call ON_RETRACT handler of BP, if any is set.  This should be
79  * called before the breakpoints are destroyed.  The reason for a
80  * separate interface is that breakpoint_destroy has to be callable
81  * without PROC.  ON_DISABLE might be useful as well, but that would
82  * be called every time we disable the breakpoint, which is too often
83  * (a breakpoint has to be disabled every time that we need to execute
84  * the instruction underneath it).  */
85 void breakpoint_on_retract(struct breakpoint *bp, struct process *proc);
86
87 /* Call GET_RETURN_BP handler of BP, if any is set.  If none is set,
88  * call CREATE_DEFAULT_RETURN_BP to obtain one.  */
89 int breakpoint_get_return_bp(struct breakpoint **ret,
90                              struct breakpoint *bp, struct process *proc);
91
92 /* Initialize a breakpoint structure.  That doesn't actually realize
93  * the breakpoint.  The breakpoint is initially assumed to be
94  * disabled.  orig_value has to be set separately.  CBS may be
95  * NULL.  */
96 int breakpoint_init(struct breakpoint *bp, struct process *proc,
97                     arch_addr_t addr, struct library_symbol *libsym);
98
99 /* Make a clone of breakpoint BP into the area of memory pointed to by
100  * RETP.  Symbols of cloned breakpoint are looked up in NEW_PROC.
101  * Returns 0 on success or a negative value on failure.  */
102 int breakpoint_clone(struct breakpoint *retp, struct process *new_proc,
103                      struct breakpoint *bp);
104
105 /* Set callbacks.  If CBS is non-NULL, then BP->cbs shall be NULL.  */
106 void breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs);
107
108 /* Destroy a breakpoint structure.   */
109 void breakpoint_destroy(struct breakpoint *bp);
110
111 /* Call enable_breakpoint the first time it's called.  Returns 0 on
112  * success and a negative value on failure.  */
113 int breakpoint_turn_on(struct breakpoint *bp, struct process *proc);
114
115 /* Call disable_breakpoint when turned off the same number of times
116  * that it was turned on.  Returns 0 on success and a negative value
117  * on failure.  */
118 int breakpoint_turn_off(struct breakpoint *bp, struct process *proc);
119
120 /* Allocate and initialize a default return breakpoint.  Returns NULL
121  * on failure.  */
122 struct breakpoint *create_default_return_bp(struct process *proc);
123
124 /* This allocates and initializes new breakpoint at ADDR, then calls
125  * INSERT_BREAKPOINT.  Returns the new breakpoint or NULL if there are
126  * errors.  */
127 struct breakpoint *insert_breakpoint_at(struct process *proc, arch_addr_t addr,
128                                         struct library_symbol *libsym);
129
130 /* Check if there is a breakpoint on this address already.  If yes,
131  * return that breakpoint instead (BP was not added).  If no, try to
132  * PROC_ADD_BREAKPOINT and BREAKPOINT_TURN_ON.  If it all works,
133  * return BP.  Otherwise return NULL.  */
134 struct breakpoint *insert_breakpoint(struct process *proc,
135                                      struct breakpoint *bp);
136
137 /* Name of a symbol associated with BP.  May be NULL.  */
138 const char *breakpoint_name(const struct breakpoint *bp);
139
140 /* A library that this breakpoint comes from.  May be NULL.  */
141 struct library *breakpoint_library(const struct breakpoint *bp);
142
143 /* Again, this seems to be several interfaces rolled into one:
144  *  - breakpoint_disable
145  *  - proc_remove_breakpoint
146  *  - breakpoint_destroy
147  * XXX */
148 void delete_breakpoint_at(struct process *proc, void *addr);
149 int delete_breakpoint(struct process *proc, struct breakpoint *bp);
150
151 /* XXX some of the following belongs to proc.h/proc.c.  */
152 struct breakpoint *address2bpstruct(struct process *proc, void *addr);
153 void enable_all_breakpoints(struct process *proc);
154 void disable_all_breakpoints(struct process *proc);
155 int breakpoints_init(struct process *proc);
156
157 #endif /* BREAKPOINT_H */