1 /* Everything about exec 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"
27 #include "mi/mi-common.h"
31 /* Exec catchpoints. */
33 /* An instance of this type is used to represent an exec catchpoint.
34 A breakpoint is really of this type iff its ops pointer points to
35 CATCH_EXEC_BREAKPOINT_OPS. */
37 struct exec_catchpoint
: public catchpoint
39 exec_catchpoint (struct gdbarch
*gdbarch
, bool temp
, const char *cond_string
)
40 : catchpoint (gdbarch
, temp
, cond_string
)
44 int insert_location (struct bp_location
*) override
;
45 int remove_location (struct bp_location
*,
46 enum remove_bp_reason reason
) override
;
47 int breakpoint_hit (const struct bp_location
*bl
,
48 const address_space
*aspace
,
50 const target_waitstatus
&ws
) override
;
51 enum print_stop_action
print_it (const bpstat
*bs
) const override
;
52 bool print_one (bp_location
**) const override
;
53 void print_mention () const override
;
54 void print_recreate (struct ui_file
*fp
) const override
;
56 /* Filename of a program whose exec triggered this catchpoint.
57 This field is only valid immediately after this catchpoint has
59 gdb::unique_xmalloc_ptr
<char> exec_pathname
;
63 exec_catchpoint::insert_location (struct bp_location
*bl
)
65 return target_insert_exec_catchpoint (inferior_ptid
.pid ());
69 exec_catchpoint::remove_location (struct bp_location
*bl
,
70 enum remove_bp_reason reason
)
72 return target_remove_exec_catchpoint (inferior_ptid
.pid ());
76 exec_catchpoint::breakpoint_hit (const struct bp_location
*bl
,
77 const address_space
*aspace
,
79 const target_waitstatus
&ws
)
81 if (ws
.kind () != TARGET_WAITKIND_EXECD
)
84 exec_pathname
= make_unique_xstrdup (ws
.execd_pathname ());
88 enum print_stop_action
89 exec_catchpoint::print_it (const bpstat
*bs
) const
91 struct ui_out
*uiout
= current_uiout
;
93 annotate_catchpoint (number
);
94 maybe_print_thread_hit_breakpoint (uiout
);
95 if (disposition
== disp_del
)
96 uiout
->text ("Temporary catchpoint ");
98 uiout
->text ("Catchpoint ");
99 if (uiout
->is_mi_like_p ())
101 uiout
->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC
));
102 uiout
->field_string ("disp", bpdisp_text (disposition
));
104 uiout
->field_signed ("bkptno", number
);
105 uiout
->text (" (exec'd ");
106 uiout
->field_string ("new-exec", exec_pathname
.get ());
109 return PRINT_SRC_AND_LOC
;
113 exec_catchpoint::print_one (bp_location
**last_loc
) const
115 struct value_print_options opts
;
116 struct ui_out
*uiout
= current_uiout
;
118 get_user_print_options (&opts
);
120 /* Field 4, the address, is omitted (which makes the columns
121 not line up too nicely with the headers, but the effect
122 is relatively readable). */
123 if (opts
.addressprint
)
124 uiout
->field_skip ("addr");
126 uiout
->text ("exec");
127 if (exec_pathname
!= NULL
)
129 uiout
->text (", program \"");
130 uiout
->field_string ("what", exec_pathname
.get ());
134 if (uiout
->is_mi_like_p ())
135 uiout
->field_string ("catch-type", "exec");
141 exec_catchpoint::print_mention () const
143 gdb_printf (_("Catchpoint %d (exec)"), number
);
146 /* Implement the "print_recreate" method for exec catchpoints. */
149 exec_catchpoint::print_recreate (struct ui_file
*fp
) const
151 gdb_printf (fp
, "catch exec");
152 print_recreate_thread (fp
);
155 /* This function attempts to parse an optional "if <cond>" clause
156 from the arg string. If one is not found, it returns NULL.
158 Else, it returns a pointer to the condition string. (It does not
159 attempt to evaluate the string against a particular block.) And,
160 it updates arg to point to the first character following the parsed
161 if clause in the arg string. */
164 ep_parse_optional_if_clause (const char **arg
)
166 const char *cond_string
;
168 if (((*arg
)[0] != 'i') || ((*arg
)[1] != 'f') || !isspace ((*arg
)[2]))
171 /* Skip the "if" keyword. */
174 /* Skip any extra leading whitespace, and record the start of the
176 *arg
= skip_spaces (*arg
);
179 /* Assume that the condition occupies the remainder of the arg
181 (*arg
) += strlen (cond_string
);
186 /* Commands to deal with catching events, such as signals, exceptions,
187 process start/exit, etc. */
190 catch_exec_command_1 (const char *arg
, int from_tty
,
191 struct cmd_list_element
*command
)
193 struct gdbarch
*gdbarch
= get_current_arch ();
194 const char *cond_string
= NULL
;
195 bool temp
= command
->context () == CATCH_TEMPORARY
;
199 arg
= skip_spaces (arg
);
201 /* The allowed syntax is:
205 First, check if there's an if clause. */
206 cond_string
= ep_parse_optional_if_clause (&arg
);
208 if ((*arg
!= '\0') && !isspace (*arg
))
209 error (_("Junk at end of arguments."));
211 std::unique_ptr
<exec_catchpoint
> c
212 (new exec_catchpoint (gdbarch
, temp
, cond_string
));
214 install_breakpoint (0, std::move (c
), 1);
217 void _initialize_break_catch_exec ();
219 _initialize_break_catch_exec ()
221 add_catch_command ("exec", _("Catch calls to exec."),
222 catch_exec_command_1
,