GDB (xrefs)
Loading...
Searching...
No Matches
break-catch-load.c
Go to the documentation of this file.
1/* Everything about load/unload catchpoints, for GDB.
2
3 Copyright (C) 1986-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21
22#include "annotate.h"
23#include "arch-utils.h"
24#include "breakpoint.h"
25#include "cli/cli-decode.h"
26#include "mi/mi-common.h"
27#include "progspace.h"
28#include "solist.h"
29#include "target.h"
30#include "valprint.h"
31
32/* An instance of this type is used to represent an solib catchpoint.
33 A breakpoint is really of this type iff its ops pointer points to
34 CATCH_SOLIB_BREAKPOINT_OPS. */
35
37{
38 solib_catchpoint (struct gdbarch *gdbarch, bool temp,
39 const char *cond_string,
40 bool is_load_, const char *arg)
42 is_load (is_load_),
43 regex (arg == nullptr ? nullptr : make_unique_xstrdup (arg)),
44 compiled (arg == nullptr
45 ? nullptr
46 : new compiled_regex (arg, REG_NOSUB, _("Invalid regexp")))
47 {
48 }
49
50 int insert_location (struct bp_location *) override;
51 int remove_location (struct bp_location *,
52 enum remove_bp_reason reason) override;
53 int breakpoint_hit (const struct bp_location *bl,
54 const address_space *aspace,
55 CORE_ADDR bp_addr,
56 const target_waitstatus &ws) override;
57 void check_status (struct bpstat *bs) override;
58 enum print_stop_action print_it (const bpstat *bs) const override;
59 bool print_one (const bp_location **) const override;
60 void print_mention () const override;
61 void print_recreate (struct ui_file *fp) const override;
62
63 /* True for "catch load", false for "catch unload". */
64 bool is_load;
65
66 /* Regular expression to match, if any. COMPILED is only valid when
67 REGEX is non-NULL. */
68 gdb::unique_xmalloc_ptr<char> regex;
69 std::unique_ptr<compiled_regex> compiled;
70};
71
72int
74{
75 return 0;
76}
77
78int
80 enum remove_bp_reason reason)
81{
82 return 0;
83}
84
85int
87 const address_space *aspace,
88 CORE_ADDR bp_addr,
89 const target_waitstatus &ws)
90{
91 if (ws.kind () == TARGET_WAITKIND_LOADED)
92 return 1;
93
94 for (breakpoint &other : all_breakpoints ())
95 {
96 if (&other == bl->owner)
97 continue;
98
99 if (other.type != bp_shlib_event)
100 continue;
101
102 if (pspace != NULL && other.pspace != pspace)
103 continue;
104
105 for (bp_location &other_bl : other.locations ())
106 {
107 if (other.breakpoint_hit (&other_bl, aspace, bp_addr, ws))
108 return 1;
109 }
110 }
111
112 return 0;
113}
114
115void
117{
118 if (is_load)
119 {
121 {
122 if (!regex
123 || compiled->exec (iter->so_name, 0, NULL, 0) == 0)
124 return;
125 }
126 }
127 else
128 {
129 for (const std::string &iter : current_program_space->deleted_solibs)
130 {
131 if (!regex
132 || compiled->exec (iter.c_str (), 0, NULL, 0) == 0)
133 return;
134 }
135 }
136
137 bs->stop = false;
139}
140
143{
144 struct ui_out *uiout = current_uiout;
145
148 if (this->disposition == disp_del)
149 uiout->text ("Temporary catchpoint ");
150 else
151 uiout->text ("Catchpoint ");
152 uiout->field_signed ("bkptno", this->number);
153 uiout->text ("\n");
154 if (uiout->is_mi_like_p ())
155 uiout->field_string ("disp", bpdisp_text (this->disposition));
156 print_solib_event (true);
157 return PRINT_SRC_AND_LOC;
158}
159
160bool
162{
163 struct value_print_options opts;
164 struct ui_out *uiout = current_uiout;
165
167 /* Field 4, the address, is omitted (which makes the columns not
168 line up too nicely with the headers, but the effect is relatively
169 readable). */
170 if (opts.addressprint)
171 {
172 annotate_field (4);
173 uiout->field_skip ("addr");
174 }
175
176 std::string msg;
177 annotate_field (5);
178 if (is_load)
179 {
180 if (regex)
181 msg = string_printf (_("load of library matching %s"),
182 regex.get ());
183 else
184 msg = _("load of library");
185 }
186 else
187 {
188 if (regex)
189 msg = string_printf (_("unload of library matching %s"),
190 regex.get ());
191 else
192 msg = _("unload of library");
193 }
194 uiout->field_string ("what", msg);
195
196 if (uiout->is_mi_like_p ())
197 uiout->field_string ("catch-type", is_load ? "load" : "unload");
198
199 return true;
200}
201
202void
204{
205 gdb_printf (_("Catchpoint %d (%s)"), number,
206 is_load ? "load" : "unload");
207}
208
209void
211{
212 gdb_printf (fp, "%s %s",
213 disposition == disp_del ? "tcatch" : "catch",
214 is_load ? "load" : "unload");
215 if (regex)
216 gdb_printf (fp, " %s", regex.get ());
217 gdb_printf (fp, "\n");
218}
219
220/* See breakpoint.h. */
221
222void
223add_solib_catchpoint (const char *arg, bool is_load, bool is_temp, bool enabled)
224{
225 struct gdbarch *gdbarch = get_current_arch ();
226
227 if (!arg)
228 arg = "";
229 arg = skip_spaces (arg);
230 if (*arg == '\0')
231 arg = nullptr;
232
233 auto c = gdb::make_unique<solib_catchpoint> (gdbarch, is_temp, nullptr,
234 is_load, arg);
235
236 c->enable_state = enabled ? bp_enabled : bp_disabled;
237
238 install_breakpoint (0, std::move (c), 1);
239}
240
241/* A helper function that does all the work for "catch load" and
242 "catch unload". */
243
244static void
245catch_load_or_unload (const char *arg, int from_tty, int is_load,
246 struct cmd_list_element *command)
247{
248 const int enabled = 1;
249 bool temp = command->context () == CATCH_TEMPORARY;
250
251 add_solib_catchpoint (arg, is_load, temp, enabled);
252}
253
254static void
255catch_load_command_1 (const char *arg, int from_tty,
256 struct cmd_list_element *command)
257{
258 catch_load_or_unload (arg, from_tty, 1, command);
259}
260
261static void
262catch_unload_command_1 (const char *arg, int from_tty,
263 struct cmd_list_element *command)
264{
265 catch_load_or_unload (arg, from_tty, 0, command);
266}
267
269void
271{
272 add_catch_command ("load", _("Catch loads of shared libraries.\n\
273Usage: catch load [REGEX]\n\
274If REGEX is given, only stop for libraries matching the regular expression."),
276 NULL,
279 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
280Usage: catch unload [REGEX]\n\
281If REGEX is given, only stop for libraries matching the regular expression."),
283 NULL,
286}
void annotate_field(int num)
Definition annotate.c:172
void annotate_catchpoint(int num)
Definition annotate.c:82
struct gdbarch * get_current_arch(void)
Definition arch-utils.c:846
static void catch_load_command_1(const char *arg, int from_tty, struct cmd_list_element *command)
void add_solib_catchpoint(const char *arg, bool is_load, bool is_temp, bool enabled)
static void catch_load_or_unload(const char *arg, int from_tty, int is_load, struct cmd_list_element *command)
void _initialize_break_catch_load()
static void catch_unload_command_1(const char *arg, int from_tty, struct cmd_list_element *command)
void add_catch_command(const char *name, const char *docstring, cmd_func_ftype *func, completer_ftype *completer, void *user_data_catch, void *user_data_tcatch)
const char * bpdisp_text(enum bpdisp disp)
Definition breakpoint.c:505
breakpoint * install_breakpoint(int internal, std::unique_ptr< breakpoint > &&arg, int update_gll)
void print_solib_event(bool is_catchpoint)
breakpoint_range all_breakpoints()
Definition breakpoint.c:704
void maybe_print_thread_hit_breakpoint(struct ui_out *uiout)
#define CATCH_PERMANENT
@ disp_del
Definition breakpoint.h:237
@ print_it_noop
@ bp_shlib_event
Definition breakpoint.h:149
#define CATCH_TEMPORARY
print_stop_action
Definition breakpoint.h:542
@ PRINT_SRC_AND_LOC
Definition breakpoint.h:548
@ bp_disabled
Definition breakpoint.h:218
@ bp_enabled
Definition breakpoint.h:220
remove_bp_reason
Definition breakpoint.h:64
breakpoint * owner
Definition breakpoint.h:345
void field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
Definition ui-out.c:511
void field_signed(const char *fldname, LONGEST value)
Definition ui-out.c:437
void field_skip(const char *fldname)
Definition ui-out.c:499
void text(const char *string)
Definition ui-out.c:566
bool is_mi_like_p() const
Definition ui-out.c:810
struct program_space * current_program_space
Definition progspace.c:40
enum bp_print_how print_it
bool stop
gdb::unique_xmalloc_ptr< char > cond_string
Definition breakpoint.h:850
program_space * pspace
Definition breakpoint.h:829
bpdisp disposition
Definition breakpoint.h:802
void * context() const
Definition cli-decode.h:109
std::vector< std::string > deleted_solibs
Definition progspace.h:375
std::vector< struct so_list * > added_solibs
Definition progspace.h:371
int breakpoint_hit(const struct bp_location *bl, const address_space *aspace, CORE_ADDR bp_addr, const target_waitstatus &ws) override
int remove_location(struct bp_location *, enum remove_bp_reason reason) override
void print_mention() const override
int insert_location(struct bp_location *) override
solib_catchpoint(struct gdbarch *gdbarch, bool temp, const char *cond_string, bool is_load_, const char *arg)
std::unique_ptr< compiled_regex > compiled
void check_status(struct bpstat *bs) override
void print_recreate(struct ui_file *fp) const override
gdb::unique_xmalloc_ptr< char > regex
enum print_stop_action print_it(const bpstat *bs) const override
bool print_one(const bp_location **) const override
target_waitkind kind() const
Definition waitstatus.h:345
#define current_uiout
Definition ui-out.h:40
void gdb_printf(struct ui_file *stream, const char *format,...)
Definition utils.c:1886
void get_user_print_options(struct value_print_options *opts)
Definition valprint.c:135
@ TARGET_WAITKIND_LOADED
Definition waitstatus.h:44
#define nullptr
Definition x86-cpuid.h:28