1 /* Everything about load/unload catchpoints, for GDB.
3 Copyright (C) 1986-2022 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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"
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. */
36 struct solib_catchpoint
: public breakpoint
38 int insert_location (struct bp_location
*) override
;
39 int remove_location (struct bp_location
*,
40 enum remove_bp_reason reason
) override
;
41 int breakpoint_hit (const struct bp_location
*bl
,
42 const address_space
*aspace
,
44 const target_waitstatus
&ws
) override
;
45 void check_status (struct bpstat
*bs
) override
;
46 enum print_stop_action
print_it (struct bpstat
*bs
) override
;
47 bool print_one (struct bp_location
**) override
;
48 void print_mention () override
;
49 void print_recreate (struct ui_file
*fp
) override
;
51 /* True for "catch load", false for "catch unload". */
54 /* Regular expression to match, if any. COMPILED is only valid when
56 gdb::unique_xmalloc_ptr
<char> regex
;
57 std::unique_ptr
<compiled_regex
> compiled
;
61 solib_catchpoint::insert_location (struct bp_location
*ignore
)
67 solib_catchpoint::remove_location (struct bp_location
*ignore
,
68 enum remove_bp_reason reason
)
74 solib_catchpoint::breakpoint_hit (const struct bp_location
*bl
,
75 const address_space
*aspace
,
77 const target_waitstatus
&ws
)
79 if (ws
.kind () == TARGET_WAITKIND_LOADED
)
82 for (breakpoint
*other
: all_breakpoints ())
84 if (other
== bl
->owner
)
87 if (other
->type
!= bp_shlib_event
)
90 if (pspace
!= NULL
&& other
->pspace
!= pspace
)
93 for (bp_location
*other_bl
: other
->locations ())
95 if (other
->breakpoint_hit (other_bl
, aspace
, bp_addr
, ws
))
104 solib_catchpoint::check_status (struct bpstat
*bs
)
108 for (so_list
*iter
: current_program_space
->added_solibs
)
111 || compiled
->exec (iter
->so_name
, 0, NULL
, 0) == 0)
117 for (const std::string
&iter
: current_program_space
->deleted_solibs
)
120 || compiled
->exec (iter
.c_str (), 0, NULL
, 0) == 0)
126 bs
->print_it
= print_it_noop
;
129 enum print_stop_action
130 solib_catchpoint::print_it (bpstat
*bs
)
132 struct breakpoint
*b
= bs
->breakpoint_at
;
133 struct ui_out
*uiout
= current_uiout
;
135 annotate_catchpoint (b
->number
);
136 maybe_print_thread_hit_breakpoint (uiout
);
137 if (b
->disposition
== disp_del
)
138 uiout
->text ("Temporary catchpoint ");
140 uiout
->text ("Catchpoint ");
141 uiout
->field_signed ("bkptno", b
->number
);
143 if (uiout
->is_mi_like_p ())
144 uiout
->field_string ("disp", bpdisp_text (b
->disposition
));
145 print_solib_event (true);
146 return PRINT_SRC_AND_LOC
;
150 solib_catchpoint::print_one (struct bp_location
**locs
)
152 struct value_print_options opts
;
153 struct ui_out
*uiout
= current_uiout
;
155 get_user_print_options (&opts
);
156 /* Field 4, the address, is omitted (which makes the columns not
157 line up too nicely with the headers, but the effect is relatively
159 if (opts
.addressprint
)
162 uiout
->field_skip ("addr");
170 msg
= string_printf (_("load of library matching %s"),
173 msg
= _("load of library");
178 msg
= string_printf (_("unload of library matching %s"),
181 msg
= _("unload of library");
183 uiout
->field_string ("what", msg
);
185 if (uiout
->is_mi_like_p ())
186 uiout
->field_string ("catch-type", is_load
? "load" : "unload");
192 solib_catchpoint::print_mention ()
194 gdb_printf (_("Catchpoint %d (%s)"), number
,
195 is_load
? "load" : "unload");
199 solib_catchpoint::print_recreate (struct ui_file
*fp
)
201 gdb_printf (fp
, "%s %s",
202 disposition
== disp_del
? "tcatch" : "catch",
203 is_load
? "load" : "unload");
205 gdb_printf (fp
, " %s", regex
.get ());
206 gdb_printf (fp
, "\n");
209 /* See breakpoint.h. */
212 add_solib_catchpoint (const char *arg
, bool is_load
, bool is_temp
, bool enabled
)
214 struct gdbarch
*gdbarch
= get_current_arch ();
218 arg
= skip_spaces (arg
);
220 std::unique_ptr
<solib_catchpoint
> c (new solib_catchpoint ());
224 c
->compiled
.reset (new compiled_regex (arg
, REG_NOSUB
,
225 _("Invalid regexp")));
226 c
->regex
= make_unique_xstrdup (arg
);
229 c
->is_load
= is_load
;
230 init_catchpoint (c
.get (), gdbarch
, is_temp
, NULL
);
232 c
->enable_state
= enabled
? bp_enabled
: bp_disabled
;
234 install_breakpoint (0, std::move (c
), 1);
237 /* A helper function that does all the work for "catch load" and
241 catch_load_or_unload (const char *arg
, int from_tty
, int is_load
,
242 struct cmd_list_element
*command
)
244 const int enabled
= 1;
245 bool temp
= command
->context () == CATCH_TEMPORARY
;
247 add_solib_catchpoint (arg
, is_load
, temp
, enabled
);
251 catch_load_command_1 (const char *arg
, int from_tty
,
252 struct cmd_list_element
*command
)
254 catch_load_or_unload (arg
, from_tty
, 1, command
);
258 catch_unload_command_1 (const char *arg
, int from_tty
,
259 struct cmd_list_element
*command
)
261 catch_load_or_unload (arg
, from_tty
, 0, command
);
264 void _initialize_break_catch_load ();
266 _initialize_break_catch_load ()
268 add_catch_command ("load", _("Catch loads of shared libraries.\n\
269 Usage: catch load [REGEX]\n\
270 If REGEX is given, only stop for libraries matching the regular expression."),
271 catch_load_command_1
,
275 add_catch_command ("unload", _("Catch unloads of shared libraries.\n\
276 Usage: catch unload [REGEX]\n\
277 If REGEX is given, only stop for libraries matching the regular expression."),
278 catch_unload_command_1
,