Remove unnecessary line from catch_exec_command_1
[binutils-gdb.git] / gdb / break-catch-exec.c
1 /* Everything about exec catchpoints, for GDB.
2
3 Copyright (C) 1986-2022 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 "inferior.h"
27 #include "mi/mi-common.h"
28 #include "target.h"
29 #include "valprint.h"
30
31 /* Exec catchpoints. */
32
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. */
36
37 struct exec_catchpoint : public breakpoint
38 {
39 int insert_location (struct bp_location *) override;
40 int remove_location (struct bp_location *,
41 enum remove_bp_reason reason) override;
42 int breakpoint_hit (const struct bp_location *bl,
43 const address_space *aspace,
44 CORE_ADDR bp_addr,
45 const target_waitstatus &ws) override;
46 enum print_stop_action print_it (const bpstat *bs) const override;
47 bool print_one (bp_location **) const override;
48 void print_mention () const override;
49 void print_recreate (struct ui_file *fp) const override;
50
51 /* Filename of a program whose exec triggered this catchpoint.
52 This field is only valid immediately after this catchpoint has
53 triggered. */
54 gdb::unique_xmalloc_ptr<char> exec_pathname;
55 };
56
57 int
58 exec_catchpoint::insert_location (struct bp_location *bl)
59 {
60 return target_insert_exec_catchpoint (inferior_ptid.pid ());
61 }
62
63 int
64 exec_catchpoint::remove_location (struct bp_location *bl,
65 enum remove_bp_reason reason)
66 {
67 return target_remove_exec_catchpoint (inferior_ptid.pid ());
68 }
69
70 int
71 exec_catchpoint::breakpoint_hit (const struct bp_location *bl,
72 const address_space *aspace,
73 CORE_ADDR bp_addr,
74 const target_waitstatus &ws)
75 {
76 if (ws.kind () != TARGET_WAITKIND_EXECD)
77 return 0;
78
79 exec_pathname = make_unique_xstrdup (ws.execd_pathname ());
80 return 1;
81 }
82
83 enum print_stop_action
84 exec_catchpoint::print_it (const bpstat *bs) const
85 {
86 struct ui_out *uiout = current_uiout;
87
88 annotate_catchpoint (number);
89 maybe_print_thread_hit_breakpoint (uiout);
90 if (disposition == disp_del)
91 uiout->text ("Temporary catchpoint ");
92 else
93 uiout->text ("Catchpoint ");
94 if (uiout->is_mi_like_p ())
95 {
96 uiout->field_string ("reason", async_reason_lookup (EXEC_ASYNC_EXEC));
97 uiout->field_string ("disp", bpdisp_text (disposition));
98 }
99 uiout->field_signed ("bkptno", number);
100 uiout->text (" (exec'd ");
101 uiout->field_string ("new-exec", exec_pathname.get ());
102 uiout->text ("), ");
103
104 return PRINT_SRC_AND_LOC;
105 }
106
107 bool
108 exec_catchpoint::print_one (bp_location **last_loc) const
109 {
110 struct value_print_options opts;
111 struct ui_out *uiout = current_uiout;
112
113 get_user_print_options (&opts);
114
115 /* Field 4, the address, is omitted (which makes the columns
116 not line up too nicely with the headers, but the effect
117 is relatively readable). */
118 if (opts.addressprint)
119 uiout->field_skip ("addr");
120 annotate_field (5);
121 uiout->text ("exec");
122 if (exec_pathname != NULL)
123 {
124 uiout->text (", program \"");
125 uiout->field_string ("what", exec_pathname.get ());
126 uiout->text ("\" ");
127 }
128
129 if (uiout->is_mi_like_p ())
130 uiout->field_string ("catch-type", "exec");
131
132 return true;
133 }
134
135 void
136 exec_catchpoint::print_mention () const
137 {
138 gdb_printf (_("Catchpoint %d (exec)"), number);
139 }
140
141 /* Implement the "print_recreate" method for exec catchpoints. */
142
143 void
144 exec_catchpoint::print_recreate (struct ui_file *fp) const
145 {
146 gdb_printf (fp, "catch exec");
147 print_recreate_thread (fp);
148 }
149
150 /* This function attempts to parse an optional "if <cond>" clause
151 from the arg string. If one is not found, it returns NULL.
152
153 Else, it returns a pointer to the condition string. (It does not
154 attempt to evaluate the string against a particular block.) And,
155 it updates arg to point to the first character following the parsed
156 if clause in the arg string. */
157
158 const char *
159 ep_parse_optional_if_clause (const char **arg)
160 {
161 const char *cond_string;
162
163 if (((*arg)[0] != 'i') || ((*arg)[1] != 'f') || !isspace ((*arg)[2]))
164 return NULL;
165
166 /* Skip the "if" keyword. */
167 (*arg) += 2;
168
169 /* Skip any extra leading whitespace, and record the start of the
170 condition string. */
171 *arg = skip_spaces (*arg);
172 cond_string = *arg;
173
174 /* Assume that the condition occupies the remainder of the arg
175 string. */
176 (*arg) += strlen (cond_string);
177
178 return cond_string;
179 }
180
181 /* Commands to deal with catching events, such as signals, exceptions,
182 process start/exit, etc. */
183
184 static void
185 catch_exec_command_1 (const char *arg, int from_tty,
186 struct cmd_list_element *command)
187 {
188 struct gdbarch *gdbarch = get_current_arch ();
189 const char *cond_string = NULL;
190 bool temp = command->context () == CATCH_TEMPORARY;
191
192 if (!arg)
193 arg = "";
194 arg = skip_spaces (arg);
195
196 /* The allowed syntax is:
197 catch exec
198 catch exec if <cond>
199
200 First, check if there's an if clause. */
201 cond_string = ep_parse_optional_if_clause (&arg);
202
203 if ((*arg != '\0') && !isspace (*arg))
204 error (_("Junk at end of arguments."));
205
206 std::unique_ptr<exec_catchpoint> c (new exec_catchpoint ());
207 init_catchpoint (c.get (), gdbarch, temp, cond_string);
208
209 install_breakpoint (0, std::move (c), 1);
210 }
211
212 void _initialize_break_catch_exec ();
213 void
214 _initialize_break_catch_exec ()
215 {
216 add_catch_command ("exec", _("Catch calls to exec."),
217 catch_exec_command_1,
218 NULL,
219 CATCH_PERMANENT,
220 CATCH_TEMPORARY);
221 }