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 breakpoint
39 /* Filename of a program whose exec triggered this catchpoint.
40 This field is only valid immediately after this catchpoint has
42 gdb::unique_xmalloc_ptr
<char> exec_pathname
;
46 insert_catch_exec (struct bp_location
*bl
)
48 return target_insert_exec_catchpoint (inferior_ptid
.pid ());
52 remove_catch_exec (struct bp_location
*bl
, enum remove_bp_reason reason
)
54 return target_remove_exec_catchpoint (inferior_ptid
.pid ());
58 breakpoint_hit_catch_exec (const struct bp_location
*bl
,
59 const address_space
*aspace
, CORE_ADDR bp_addr
,
60 const target_waitstatus
&ws
)
62 struct exec_catchpoint
*c
= (struct exec_catchpoint
*) bl
->owner
;
64 if (ws
.kind () != TARGET_WAITKIND_EXECD
)
67 c
->exec_pathname
= make_unique_xstrdup (ws
.execd_pathname ());
71 static enum print_stop_action
72 print_it_catch_exec (bpstat
*bs
)
74 struct ui_out
*uiout
= current_uiout
;
75 struct breakpoint
*b
= bs
->breakpoint_at
;
76 struct exec_catchpoint
*c
= (struct exec_catchpoint
*) b
;
78 annotate_catchpoint (b
->number
);
79 maybe_print_thread_hit_breakpoint (uiout
);
80 if (b
->disposition
== disp_del
)
81 uiout
->text ("Temporary catchpoint ");
83 uiout
->text ("Catchpoint ");
84 if (uiout
->is_mi_like_p ())
86 uiout
->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC
));
87 uiout
->field_string ("disp", bpdisp_text (b
->disposition
));
89 uiout
->field_signed ("bkptno", b
->number
);
90 uiout
->text (" (exec'd ");
91 uiout
->field_string ("new-exec", c
->exec_pathname
.get ());
94 return PRINT_SRC_AND_LOC
;
98 print_one_catch_exec (struct breakpoint
*b
, struct bp_location
**last_loc
)
100 struct exec_catchpoint
*c
= (struct exec_catchpoint
*) b
;
101 struct value_print_options opts
;
102 struct ui_out
*uiout
= current_uiout
;
104 get_user_print_options (&opts
);
106 /* Field 4, the address, is omitted (which makes the columns
107 not line up too nicely with the headers, but the effect
108 is relatively readable). */
109 if (opts
.addressprint
)
110 uiout
->field_skip ("addr");
112 uiout
->text ("exec");
113 if (c
->exec_pathname
!= NULL
)
115 uiout
->text (", program \"");
116 uiout
->field_string ("what", c
->exec_pathname
.get ());
120 if (uiout
->is_mi_like_p ())
121 uiout
->field_string ("catch-type", "exec");
125 print_mention_catch_exec (struct breakpoint
*b
)
127 gdb_printf (_("Catchpoint %d (exec)"), b
->number
);
130 /* Implement the "print_recreate" breakpoint_ops method for exec
134 print_recreate_catch_exec (struct breakpoint
*b
, struct ui_file
*fp
)
136 gdb_printf (fp
, "catch exec");
137 print_recreate_thread (b
, fp
);
140 static struct breakpoint_ops catch_exec_breakpoint_ops
;
142 /* This function attempts to parse an optional "if <cond>" clause
143 from the arg string. If one is not found, it returns NULL.
145 Else, it returns a pointer to the condition string. (It does not
146 attempt to evaluate the string against a particular block.) And,
147 it updates arg to point to the first character following the parsed
148 if clause in the arg string. */
151 ep_parse_optional_if_clause (const char **arg
)
153 const char *cond_string
;
155 if (((*arg
)[0] != 'i') || ((*arg
)[1] != 'f') || !isspace ((*arg
)[2]))
158 /* Skip the "if" keyword. */
161 /* Skip any extra leading whitespace, and record the start of the
163 *arg
= skip_spaces (*arg
);
166 /* Assume that the condition occupies the remainder of the arg
168 (*arg
) += strlen (cond_string
);
173 /* Commands to deal with catching events, such as signals, exceptions,
174 process start/exit, etc. */
177 catch_exec_command_1 (const char *arg
, int from_tty
,
178 struct cmd_list_element
*command
)
180 struct gdbarch
*gdbarch
= get_current_arch ();
181 const char *cond_string
= NULL
;
182 bool temp
= command
->context () == CATCH_TEMPORARY
;
186 arg
= skip_spaces (arg
);
188 /* The allowed syntax is:
192 First, check if there's an if clause. */
193 cond_string
= ep_parse_optional_if_clause (&arg
);
195 if ((*arg
!= '\0') && !isspace (*arg
))
196 error (_("Junk at end of arguments."));
198 std::unique_ptr
<exec_catchpoint
> c (new exec_catchpoint ());
199 init_catchpoint (c
.get (), gdbarch
, temp
, cond_string
,
200 &catch_exec_breakpoint_ops
);
201 c
->exec_pathname
.reset ();
203 install_breakpoint (0, std::move (c
), 1);
209 struct breakpoint_ops
*ops
;
211 initialize_breakpoint_ops ();
213 /* Exec catchpoints. */
214 ops
= &catch_exec_breakpoint_ops
;
215 *ops
= base_breakpoint_ops
;
216 ops
->insert_location
= insert_catch_exec
;
217 ops
->remove_location
= remove_catch_exec
;
218 ops
->breakpoint_hit
= breakpoint_hit_catch_exec
;
219 ops
->print_it
= print_it_catch_exec
;
220 ops
->print_one
= print_one_catch_exec
;
221 ops
->print_mention
= print_mention_catch_exec
;
222 ops
->print_recreate
= print_recreate_catch_exec
;
225 void _initialize_break_catch_exec ();
227 _initialize_break_catch_exec ()
231 add_catch_command ("exec", _("Catch calls to exec."),
232 catch_exec_command_1
,