blob: 372c7142a23e4af761ff6f1e4eda330c0d0ca1b4 [file] [log] [blame]
Petr Machata9294d822012-02-07 12:35:58 +01001/*
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 *
36 * Tracepoints are provided by the main binary as well as by any
37 * opened libraries: every time an ELF file is mapped into the address
38 * space, a new set of tracepoints is extracted, and filtered
39 * according to user settings. Those tracepoints that are left are
40 * then realized, and the tracing starts.
41 *
42 * A scheme like this would take care of gradually introducing
43 * breakpoints when the library is mapped, and therefore ready, and
44 * would avoid certain hacks. For example on PPC64, we don't actually
45 * add breakpoints to PLT. Instead, we read the PLT (which contains
46 * addresses, not code), to figure out where to put the breakpoints.
47 * In prelinked code, that address is non-zero, and points to an
48 * address that's not yet mapped. ptrace then fails when we try to
49 * add the breakpoint.
50 *
51 * Ideally, return breakpoints would be just a special kind of
52 * tracepoint that has attached some magic. Or a feature of a
53 * tracepoint. Service breakpoints like the handling of dlopen would
54 * be a low-level breakpoint, likely without tracepoint attached.
55 *
56 * So that's for sometimes.
57 */
58
Petr Machatad3dc17b2012-03-21 03:23:25 +010059#include "sysdep.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010060#include "library.h"
Petr Machata9294d822012-02-07 12:35:58 +010061
62struct Process;
Petr Machataa9fd8f42012-02-07 13:25:56 +010063struct breakpoint;
64
65struct bp_callbacks {
Petr Machatad09d2402012-04-13 21:34:08 +020066 void (*on_hit)(struct breakpoint *bp, struct Process *proc);
67 void (*on_continue)(struct breakpoint *bp, struct Process *proc);
Petr Machataa9fd8f42012-02-07 13:25:56 +010068};
Petr Machata9294d822012-02-07 12:35:58 +010069
Petr Machata9294d822012-02-07 12:35:58 +010070struct breakpoint {
Petr Machataa9fd8f42012-02-07 13:25:56 +010071 struct bp_callbacks *cbs;
Petr Machata29add4f2012-02-18 16:38:05 +010072 struct library_symbol *libsym;
Petr Machata9294d822012-02-07 12:35:58 +010073 void *addr;
74 unsigned char orig_value[BREAKPOINT_LENGTH];
75 int enabled;
Petr Machata2b46cfc2012-02-18 11:17:29 +010076 struct arch_breakpoint_data arch;
Petr Machata9294d822012-02-07 12:35:58 +010077};
78
Petr Machataa9fd8f42012-02-07 13:25:56 +010079/* Call on-hit handler of BP, if any is set. */
80void breakpoint_on_hit(struct breakpoint *bp, struct Process *proc);
81
Petr Machatad09d2402012-04-13 21:34:08 +020082/* Call on-continue handler of BP. If none is set, call
Petr Machata56a9ea62012-03-27 03:09:29 +020083 * continue_after_breakpoint. */
84void breakpoint_on_continue(struct breakpoint *bp, struct Process *proc);
85
Petr Machata2b46cfc2012-02-18 11:17:29 +010086/* Initialize a breakpoint structure. That doesn't actually realize
87 * the breakpoint. The breakpoint is initially assumed to be
88 * disabled. orig_value has to be set separately. CBS may be
89 * NULL. */
90int breakpoint_init(struct breakpoint *bp, struct Process *proc,
Petr Machata55ac9322012-03-27 03:07:35 +020091 target_address_t addr, struct library_symbol *libsym);
92
Petr Machatad3cc9882012-04-13 21:40:23 +020093/* Make a clone of breakpoint BP into the area of memory pointed to by
94 * RETP. The original breakpoint was assigned to process OLD_PROC,
95 * the cloned breakpoint will be attached to process NEW_PROC.
96 * Returns 0 on success or a negative value on failure. */
97int breakpoint_clone(struct breakpoint *retp, struct Process *new_proc,
98 struct breakpoint *bp, struct Process *old_proc);
99
Petr Machata55ac9322012-03-27 03:07:35 +0200100/* Set callbacks. If CBS is non-NULL, then BP->cbs shall be NULL. */
101void breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs);
Petr Machata2b46cfc2012-02-18 11:17:29 +0100102
Petr Machata8cce1192012-03-25 01:37:19 +0100103/* XXX this is currently not called anywhere. */
104void breakpoint_destroy(struct breakpoint *bp);
105
Petr Machata52dbfb12012-03-29 16:38:26 +0200106/* Call enable_breakpoint the first time it's called. Returns 0 on
107 * success and a negative value on failure. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200108int breakpoint_turn_on(struct breakpoint *bp, struct Process *proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200109
110/* Call disable_breakpoint when turned off the same number of times
111 * that it was turned on. Returns 0 on success and a negative value
112 * on failure. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200113int breakpoint_turn_off(struct breakpoint *bp, struct Process *proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200114
115/* This is actually several functions rolled in one:
116 * - malloc
Petr Machata9294d822012-02-07 12:35:58 +0100117 * - breakpoint_init
Petr Machata52dbfb12012-03-29 16:38:26 +0200118 * - proc_add_breakpoint
Petr Machata9294d822012-02-07 12:35:58 +0100119 * - breakpoint_enable
120 * XXX I think it should be broken up somehow. */
121struct breakpoint *insert_breakpoint(struct Process *proc, void *addr,
Petr Machata9df15012012-02-20 12:49:46 +0100122 struct library_symbol *libsym);
Petr Machata9294d822012-02-07 12:35:58 +0100123
Petr Machatae9aebd62012-03-25 01:38:53 +0100124/* Name of a symbol associated with BP. May be NULL. */
125const char *breakpoint_name(const struct breakpoint *bp);
126
Petr Machata52dbfb12012-03-29 16:38:26 +0200127/* A library that this breakpoint comes from. May be NULL. */
128struct library *breakpoint_library(const struct breakpoint *bp);
129
Petr Machata8cce1192012-03-25 01:37:19 +0100130/* Again, this seems to be several interfaces rolled into one:
131 * - breakpoint_disable
Petr Machata52dbfb12012-03-29 16:38:26 +0200132 * - proc_remove_breakpoint
Petr Machata8cce1192012-03-25 01:37:19 +0100133 * - breakpoint_destroy
134 * XXX */
Petr Machata9294d822012-02-07 12:35:58 +0100135void delete_breakpoint(struct Process *proc, void *addr);
136
137/* XXX some of the following belongs to proc.h/proc.c. */
138struct breakpoint *address2bpstruct(struct Process *proc, void *addr);
139void enable_all_breakpoints(struct Process *proc);
140void disable_all_breakpoints(struct Process *proc);
141int breakpoints_init(struct Process *proc, int enable);
142
Petr Machata9294d822012-02-07 12:35:58 +0100143#endif /* BREAKPOINT_H */