blob: 0398072044dfa5e1a0f4319e62766c72db3a5991 [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 *
Petr Machata0c3377e2012-04-16 18:43:00 +020036 * Service breakpoints like the handling of dlopen would be a
37 * low-level breakpoint, likely without tracepoint attached.
Petr Machata9294d822012-02-07 12:35:58 +010038 *
39 * So that's for sometimes.
40 */
41
Petr Machatad3dc17b2012-03-21 03:23:25 +010042#include "sysdep.h"
Petr Machata2b46cfc2012-02-18 11:17:29 +010043#include "library.h"
Petr Machata9294d822012-02-07 12:35:58 +010044
45struct Process;
Petr Machataa9fd8f42012-02-07 13:25:56 +010046struct breakpoint;
47
48struct bp_callbacks {
Petr Machatad09d2402012-04-13 21:34:08 +020049 void (*on_hit)(struct breakpoint *bp, struct Process *proc);
50 void (*on_continue)(struct breakpoint *bp, struct Process *proc);
Petr Machata86d38282012-04-24 18:09:01 +020051 void (*on_retract)(struct breakpoint *bp, struct Process *proc);
Petr Machataa9fd8f42012-02-07 13:25:56 +010052};
Petr Machata9294d822012-02-07 12:35:58 +010053
Petr Machata9294d822012-02-07 12:35:58 +010054struct breakpoint {
Petr Machataa9fd8f42012-02-07 13:25:56 +010055 struct bp_callbacks *cbs;
Petr Machata29add4f2012-02-18 16:38:05 +010056 struct library_symbol *libsym;
Petr Machata9294d822012-02-07 12:35:58 +010057 void *addr;
58 unsigned char orig_value[BREAKPOINT_LENGTH];
59 int enabled;
Petr Machata2b46cfc2012-02-18 11:17:29 +010060 struct arch_breakpoint_data arch;
Petr Machata9294d822012-02-07 12:35:58 +010061};
62
Petr Machataa9fd8f42012-02-07 13:25:56 +010063/* Call on-hit handler of BP, if any is set. */
64void breakpoint_on_hit(struct breakpoint *bp, struct Process *proc);
65
Petr Machatad09d2402012-04-13 21:34:08 +020066/* Call on-continue handler of BP. If none is set, call
Petr Machata56a9ea62012-03-27 03:09:29 +020067 * continue_after_breakpoint. */
68void breakpoint_on_continue(struct breakpoint *bp, struct Process *proc);
69
Petr Machata86d38282012-04-24 18:09:01 +020070/* Call on-retract handler of BP, if any is set. This should be
71 * called before the breakpoints are destroyed. The reason for a
72 * separate interface is that breakpoint_destroy has to be callable
73 * without PROC. ON_DISABLE might be useful as well, but that would
74 * be called every time we disable the breakpoint, which is too often
75 * (a breakpoint has to be disabled every time that we need to execute
76 * the instruction underneath it). */
77void breakpoint_on_retract(struct breakpoint *bp, struct Process *proc);
78
Petr Machata2b46cfc2012-02-18 11:17:29 +010079/* Initialize a breakpoint structure. That doesn't actually realize
80 * the breakpoint. The breakpoint is initially assumed to be
81 * disabled. orig_value has to be set separately. CBS may be
82 * NULL. */
83int breakpoint_init(struct breakpoint *bp, struct Process *proc,
Petr Machata55ac9322012-03-27 03:07:35 +020084 target_address_t addr, struct library_symbol *libsym);
85
Petr Machatad3cc9882012-04-13 21:40:23 +020086/* Make a clone of breakpoint BP into the area of memory pointed to by
87 * RETP. The original breakpoint was assigned to process OLD_PROC,
88 * the cloned breakpoint will be attached to process NEW_PROC.
89 * Returns 0 on success or a negative value on failure. */
90int breakpoint_clone(struct breakpoint *retp, struct Process *new_proc,
91 struct breakpoint *bp, struct Process *old_proc);
92
Petr Machata55ac9322012-03-27 03:07:35 +020093/* Set callbacks. If CBS is non-NULL, then BP->cbs shall be NULL. */
94void breakpoint_set_callbacks(struct breakpoint *bp, struct bp_callbacks *cbs);
Petr Machata2b46cfc2012-02-18 11:17:29 +010095
Petr Machata0fe76c62012-04-14 02:29:30 +020096/* Destroy a breakpoint structure. */
Petr Machata8cce1192012-03-25 01:37:19 +010097void breakpoint_destroy(struct breakpoint *bp);
98
Petr Machata52dbfb12012-03-29 16:38:26 +020099/* Call enable_breakpoint the first time it's called. Returns 0 on
100 * success and a negative value on failure. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200101int breakpoint_turn_on(struct breakpoint *bp, struct Process *proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200102
103/* Call disable_breakpoint when turned off the same number of times
104 * that it was turned on. Returns 0 on success and a negative value
105 * on failure. */
Petr Machatafa0c5702012-04-13 18:43:40 +0200106int breakpoint_turn_off(struct breakpoint *bp, struct Process *proc);
Petr Machata52dbfb12012-03-29 16:38:26 +0200107
Petr Machata051dffa2012-04-15 04:19:17 +0200108/* Utility function that does what typically needs to be done when a
109 * breakpoint is to be inserted. It checks whether there is another
Petr Machata622581f2012-04-23 19:40:40 +0200110 * breakpoint in PROC->LEADER for given ADDR. If not, it allocates
111 * memory for a new breakpoint on the heap, initializes it, and calls
Petr Machata051dffa2012-04-15 04:19:17 +0200112 * PROC_ADD_BREAKPOINT to add the newly-created breakpoint. For newly
113 * added as well as preexisting breakpoints, it then calls
114 * BREAKPOINT_TURN_ON. If anything fails, it cleans up and returns
115 * NULL. Otherwise it returns the breakpoint for ADDR. */
Petr Machata9294d822012-02-07 12:35:58 +0100116struct breakpoint *insert_breakpoint(struct Process *proc, void *addr,
Petr Machata9df15012012-02-20 12:49:46 +0100117 struct library_symbol *libsym);
Petr Machata9294d822012-02-07 12:35:58 +0100118
Petr Machatae9aebd62012-03-25 01:38:53 +0100119/* Name of a symbol associated with BP. May be NULL. */
120const char *breakpoint_name(const struct breakpoint *bp);
121
Petr Machata52dbfb12012-03-29 16:38:26 +0200122/* A library that this breakpoint comes from. May be NULL. */
123struct library *breakpoint_library(const struct breakpoint *bp);
124
Petr Machata8cce1192012-03-25 01:37:19 +0100125/* Again, this seems to be several interfaces rolled into one:
126 * - breakpoint_disable
Petr Machata52dbfb12012-03-29 16:38:26 +0200127 * - proc_remove_breakpoint
Petr Machata8cce1192012-03-25 01:37:19 +0100128 * - breakpoint_destroy
129 * XXX */
Petr Machata9294d822012-02-07 12:35:58 +0100130void delete_breakpoint(struct Process *proc, void *addr);
131
132/* XXX some of the following belongs to proc.h/proc.c. */
133struct breakpoint *address2bpstruct(struct Process *proc, void *addr);
134void enable_all_breakpoints(struct Process *proc);
135void disable_all_breakpoints(struct Process *proc);
Petr Machata75934ad2012-04-14 02:28:03 +0200136int breakpoints_init(struct Process *proc);
Petr Machata9294d822012-02-07 12:35:58 +0100137
Petr Machata9294d822012-02-07 12:35:58 +0100138#endif /* BREAKPOINT_H */