]>
Commit | Line | Data |
---|---|---|
c906108c | 1 | /* Memory-access and commands for "inferior" process, for GDB. |
990a07ab | 2 | |
4a94e368 | 3 | Copyright (C) 1986-2022 Free Software Foundation, Inc. |
c906108c | 4 | |
c5aa993b | 5 | This file is part of GDB. |
c906108c | 6 | |
c5aa993b JM |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
c5aa993b | 10 | (at your option) any later version. |
c906108c | 11 | |
c5aa993b JM |
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. | |
c906108c | 16 | |
c5aa993b | 17 | You should have received a copy of the GNU General Public License |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
c906108c SS |
19 | |
20 | #include "defs.h" | |
5af949e3 | 21 | #include "arch-utils.h" |
c906108c SS |
22 | #include "symtab.h" |
23 | #include "gdbtypes.h" | |
24 | #include "frame.h" | |
25 | #include "inferior.h" | |
45741a9c | 26 | #include "infrun.h" |
268a13a5 | 27 | #include "gdbsupport/environ.h" |
c906108c SS |
28 | #include "value.h" |
29 | #include "gdbcmd.h" | |
1adeb98a | 30 | #include "symfile.h" |
c906108c SS |
31 | #include "gdbcore.h" |
32 | #include "target.h" | |
33 | #include "language.h" | |
c906108c | 34 | #include "objfiles.h" |
c94fdfd0 | 35 | #include "completer.h" |
8b93c638 | 36 | #include "ui-out.h" |
36160dc4 | 37 | #include "regcache.h" |
f9418c0f | 38 | #include "reggroups.h" |
fe898f56 | 39 | #include "block.h" |
a77053c2 | 40 | #include "solib.h" |
f9418c0f | 41 | #include <ctype.h> |
76727919 | 42 | #include "observable.h" |
424163ea | 43 | #include "target-descriptions.h" |
ad842144 | 44 | #include "user-regs.h" |
a0ef4274 | 45 | #include "gdbthread.h" |
79a45b7d | 46 | #include "valprint.h" |
edb3359d | 47 | #include "inline-frame.h" |
573cda03 | 48 | #include "tracepoint.h" |
09cee04b | 49 | #include "inf-loop.h" |
f8eba3c6 | 50 | #include "linespec.h" |
243a9253 | 51 | #include "thread-fsm.h" |
3b12939d | 52 | #include "top.h" |
8980e177 | 53 | #include "interps.h" |
4a4c04f1 | 54 | #include "skip.h" |
268a13a5 | 55 | #include "gdbsupport/gdb_optional.h" |
b46a8d7c | 56 | #include "source.h" |
7f6aba03 | 57 | #include "cli/cli-style.h" |
d5551862 | 58 | |
a58dd373 EZ |
59 | /* Local functions: */ |
60 | ||
a14ed312 | 61 | static void until_next_command (int); |
c906108c | 62 | |
0b39b52e | 63 | static void step_1 (int, int, const char *); |
c906108c | 64 | |
c906108c | 65 | #define ERROR_NO_INFERIOR \ |
55f6301a | 66 | if (!target_has_execution ()) error (_("The program is not being run.")); |
c906108c | 67 | |
3e43a32a MS |
68 | /* Scratch area where string containing arguments to give to the |
69 | program will be stored by 'set args'. As soon as anything is | |
70 | stored, notice_args_set will move it into per-inferior storage. | |
1777feb0 | 71 | Arguments are separated by spaces. Empty string (pointer to '\0') |
3e43a32a | 72 | means no args. */ |
c906108c | 73 | |
e0700ba4 | 74 | static std::string inferior_args_scratch; |
c906108c | 75 | |
d092c5a2 SDJ |
76 | /* Scratch area where the new cwd will be stored by 'set cwd'. */ |
77 | ||
e0700ba4 | 78 | static std::string inferior_cwd_scratch; |
d092c5a2 | 79 | |
3f81c18a VP |
80 | /* Scratch area where 'set inferior-tty' will store user-provided value. |
81 | We'll immediate copy it into per-inferior storage. */ | |
552c04a7 | 82 | |
e0700ba4 | 83 | static std::string inferior_io_terminal_scratch; |
c906108c SS |
84 | |
85 | /* Pid of our debugged inferior, or 0 if no inferior now. | |
86 | Since various parts of infrun.c test this to see whether there is a program | |
87 | being debugged it should be nonzero (currently 3 is used) for remote | |
88 | debugging. */ | |
89 | ||
39f77062 | 90 | ptid_t inferior_ptid; |
c906108c | 91 | |
c906108c SS |
92 | /* Nonzero if stopped due to completion of a stack dummy routine. */ |
93 | ||
aa7d318d | 94 | enum stop_stack_kind stop_stack_dummy; |
c906108c SS |
95 | |
96 | /* Nonzero if stopped due to a random (unexpected) signal in inferior | |
97 | process. */ | |
98 | ||
99 | int stopped_by_random_signal; | |
100 | ||
5ffa6ca3 TT |
101 | |
102 | /* Whether "finish" should print the value. */ | |
103 | ||
104 | static bool finish_print = true; | |
105 | ||
c906108c | 106 | \f |
3f81c18a VP |
107 | |
108 | static void | |
eb4c3f4a | 109 | set_inferior_tty_command (const char *args, int from_tty, |
3f81c18a VP |
110 | struct cmd_list_element *c) |
111 | { | |
112 | /* CLI has assigned the user-provided value to inferior_io_terminal_scratch. | |
113 | Now route it to current inferior. */ | |
05779d57 | 114 | current_inferior ()->set_tty (inferior_io_terminal_scratch); |
3f81c18a VP |
115 | } |
116 | ||
117 | static void | |
118 | show_inferior_tty_command (struct ui_file *file, int from_tty, | |
119 | struct cmd_list_element *c, const char *value) | |
120 | { | |
121 | /* Note that we ignore the passed-in value in favor of computing it | |
122 | directly. */ | |
4e93ea6e | 123 | const std::string &inferior_tty = current_inferior ()->tty (); |
abbb1732 | 124 | |
6cb06a8c TT |
125 | gdb_printf (file, |
126 | _("Terminal for future runs of program being debugged " | |
127 | "is \"%s\".\n"), inferior_tty.c_str ()); | |
3cb3b8df BR |
128 | } |
129 | ||
552c04a7 TT |
130 | void |
131 | set_inferior_args_vector (int argc, char **argv) | |
132 | { | |
a69e37dc SM |
133 | gdb::array_view<char * const> args (argv, argc); |
134 | std::string n = construct_inferior_arguments (args); | |
fd2dec2a | 135 | current_inferior ()->set_args (std::move (n)); |
552c04a7 TT |
136 | } |
137 | ||
138 | /* Notice when `set args' is run. */ | |
6339bfc4 | 139 | |
552c04a7 | 140 | static void |
eb4c3f4a | 141 | set_args_command (const char *args, int from_tty, struct cmd_list_element *c) |
552c04a7 | 142 | { |
3f81c18a VP |
143 | /* CLI has assigned the user-provided value to inferior_args_scratch. |
144 | Now route it to current inferior. */ | |
e5169525 | 145 | current_inferior ()->set_args (inferior_args_scratch); |
552c04a7 TT |
146 | } |
147 | ||
148 | /* Notice when `show args' is run. */ | |
6339bfc4 | 149 | |
552c04a7 | 150 | static void |
3f81c18a VP |
151 | show_args_command (struct ui_file *file, int from_tty, |
152 | struct cmd_list_element *c, const char *value) | |
552c04a7 | 153 | { |
258c00cc TT |
154 | /* Note that we ignore the passed-in value in favor of computing it |
155 | directly. */ | |
e5169525 | 156 | deprecated_show_value_hack (file, from_tty, c, |
fd2dec2a | 157 | current_inferior ()->args ().c_str ()); |
552c04a7 TT |
158 | } |
159 | ||
268a13a5 | 160 | /* See gdbsupport/common-inferior.h. */ |
d092c5a2 | 161 | |
11bd012e | 162 | const std::string & |
d092c5a2 SDJ |
163 | get_inferior_cwd () |
164 | { | |
90cc31c9 | 165 | return current_inferior ()->cwd (); |
d092c5a2 SDJ |
166 | } |
167 | ||
168 | /* Handle the 'set cwd' command. */ | |
169 | ||
170 | static void | |
eb4c3f4a | 171 | set_cwd_command (const char *args, int from_tty, struct cmd_list_element *c) |
d092c5a2 | 172 | { |
11bd012e | 173 | current_inferior ()->set_cwd (inferior_cwd_scratch); |
d092c5a2 SDJ |
174 | } |
175 | ||
176 | /* Handle the 'show cwd' command. */ | |
177 | ||
178 | static void | |
179 | show_cwd_command (struct ui_file *file, int from_tty, | |
180 | struct cmd_list_element *c, const char *value) | |
181 | { | |
11bd012e | 182 | const std::string &cwd = current_inferior ()->cwd (); |
d092c5a2 | 183 | |
11bd012e | 184 | if (cwd.empty ()) |
6cb06a8c TT |
185 | gdb_printf (file, |
186 | _("\ | |
d092c5a2 | 187 | You have not set the inferior's current working directory.\n\ |
bc3b087d SDJ |
188 | The inferior will inherit GDB's cwd if native debugging, or the remote\n\ |
189 | server's cwd if remote debugging.\n")); | |
d092c5a2 | 190 | else |
6cb06a8c TT |
191 | gdb_printf (file, |
192 | _("Current working directory that will be used " | |
193 | "when starting the inferior is \"%s\".\n"), | |
194 | cwd.c_str ()); | |
d092c5a2 SDJ |
195 | } |
196 | ||
552c04a7 | 197 | |
6c4486e6 PA |
198 | /* This function strips the '&' character (indicating background |
199 | execution) that is added as *the last* of the arguments ARGS of a | |
200 | command. A copy of the incoming ARGS without the '&' is returned, | |
201 | unless the resulting string after stripping is empty, in which case | |
202 | NULL is returned. *BG_CHAR_P is an output boolean that indicates | |
203 | whether the '&' character was found. */ | |
6339bfc4 | 204 | |
6be9a197 | 205 | static gdb::unique_xmalloc_ptr<char> |
6c4486e6 | 206 | strip_bg_char (const char *args, int *bg_char_p) |
43ff13b4 | 207 | { |
6c4486e6 | 208 | const char *p; |
c5aa993b | 209 | |
6c4486e6 PA |
210 | if (args == NULL || *args == '\0') |
211 | { | |
212 | *bg_char_p = 0; | |
213 | return NULL; | |
214 | } | |
c5aa993b | 215 | |
6c4486e6 PA |
216 | p = args + strlen (args); |
217 | if (p[-1] == '&') | |
43ff13b4 | 218 | { |
6c4486e6 PA |
219 | p--; |
220 | while (p > args && isspace (p[-1])) | |
221 | p--; | |
222 | ||
223 | *bg_char_p = 1; | |
224 | if (p != args) | |
6be9a197 TT |
225 | return gdb::unique_xmalloc_ptr<char> |
226 | (savestring (args, p - args)); | |
6c4486e6 | 227 | else |
6be9a197 | 228 | return gdb::unique_xmalloc_ptr<char> (nullptr); |
43ff13b4 | 229 | } |
6c4486e6 PA |
230 | |
231 | *bg_char_p = 0; | |
b02f78f9 | 232 | return make_unique_xstrdup (args); |
43ff13b4 JM |
233 | } |
234 | ||
281b533b DJ |
235 | /* Common actions to take after creating any sort of inferior, by any |
236 | means (running, attaching, connecting, et cetera). The target | |
237 | should be stopped. */ | |
238 | ||
239 | void | |
a7aba266 | 240 | post_create_inferior (int from_tty) |
281b533b | 241 | { |
ce406537 | 242 | |
b79599ff | 243 | /* Be sure we own the terminal in case write operations are performed. */ |
223ffa71 | 244 | target_terminal::ours_for_output (); |
b79599ff | 245 | |
1f9d9e32 AB |
246 | infrun_debug_show_threads ("threads in the newly created inferior", |
247 | current_inferior ()->non_exited_threads ()); | |
248 | ||
424163ea DJ |
249 | /* If the target hasn't taken care of this already, do it now. |
250 | Targets which need to access registers during to_open, | |
251 | to_create_inferior, or to_attach should do it earlier; but many | |
252 | don't need to. */ | |
253 | target_find_description (); | |
254 | ||
ce406537 PA |
255 | /* Now that we know the register layout, retrieve current PC. But |
256 | if the PC is unavailable (e.g., we're opening a core file with | |
257 | missing registers info), ignore it. */ | |
f2ffa92b PA |
258 | thread_info *thr = inferior_thread (); |
259 | ||
351031f2 | 260 | thr->clear_stop_pc (); |
a70b8144 | 261 | try |
ce406537 | 262 | { |
8dc3273e | 263 | regcache *rc = get_thread_regcache (thr); |
1edb66d8 | 264 | thr->set_stop_pc (regcache_read_pc (rc)); |
ce406537 | 265 | } |
230d2906 | 266 | catch (const gdb_exception_error &ex) |
7556d4a4 PA |
267 | { |
268 | if (ex.error != NOT_AVAILABLE_ERROR) | |
eedc3f4f | 269 | throw; |
7556d4a4 | 270 | } |
f698437e | 271 | |
7e10abd1 | 272 | if (current_program_space->exec_bfd ()) |
50c71eaf | 273 | { |
2eff07b3 PP |
274 | const unsigned solib_add_generation |
275 | = current_program_space->solib_add_generation; | |
276 | ||
122373f7 SM |
277 | scoped_restore restore_in_initial_library_scan |
278 | = make_scoped_restore (¤t_inferior ()->in_initial_library_scan, | |
279 | true); | |
280 | ||
9353355f DJ |
281 | /* Create the hooks to handle shared library load and unload |
282 | events. */ | |
268a4a75 | 283 | solib_create_inferior_hook (from_tty); |
268a4a75 | 284 | |
2eff07b3 PP |
285 | if (current_program_space->solib_add_generation == solib_add_generation) |
286 | { | |
287 | /* The platform-specific hook should load initial shared libraries, | |
288 | but didn't. FROM_TTY will be incorrectly 0 but such solib | |
289 | targets should be fixed anyway. Call it only after the solib | |
290 | target has been initialized by solib_create_inferior_hook. */ | |
291 | ||
292 | if (info_verbose) | |
293 | warning (_("platform-specific solib_create_inferior_hook did " | |
294 | "not load initial shared libraries.")); | |
295 | ||
296 | /* If the solist is global across processes, there's no need to | |
297 | refetch it here. */ | |
f5656ead | 298 | if (!gdbarch_has_global_solist (target_gdbarch ())) |
e696b3ad | 299 | solib_add (NULL, 0, auto_solib_add); |
2eff07b3 | 300 | } |
9353355f DJ |
301 | } |
302 | ||
ea5d7a99 PM |
303 | /* If the user sets watchpoints before execution having started, |
304 | then she gets software watchpoints, because GDB can't know which | |
305 | target will end up being pushed, or if it supports hardware | |
306 | watchpoints or not. breakpoint_re_set takes care of promoting | |
307 | watchpoints to hardware watchpoints if possible, however, if this | |
308 | new inferior doesn't load shared libraries or we don't pull in | |
309 | symbols from any other source on this target/arch, | |
310 | breakpoint_re_set is never called. Call it now so that software | |
311 | watchpoints get a chance to be promoted to hardware watchpoints | |
312 | if the now pushed target supports hardware watchpoints. */ | |
313 | breakpoint_re_set (); | |
314 | ||
a0ff652f | 315 | gdb::observers::inferior_created.notify (current_inferior ()); |
281b533b DJ |
316 | } |
317 | ||
a4d5f2e0 JB |
318 | /* Kill the inferior if already running. This function is designed |
319 | to be called when we are about to start the execution of the program | |
320 | from the beginning. Ask the user to confirm that he wants to restart | |
321 | the program being debugged when FROM_TTY is non-null. */ | |
c906108c | 322 | |
8edfe269 | 323 | static void |
a4d5f2e0 JB |
324 | kill_if_already_running (int from_tty) |
325 | { | |
55f6301a | 326 | if (inferior_ptid != null_ptid && target_has_execution ()) |
c906108c | 327 | { |
8edfe269 DJ |
328 | /* Bail out before killing the program if we will not be able to |
329 | restart it. */ | |
330 | target_require_runnable (); | |
331 | ||
adf40b2e | 332 | if (from_tty |
9e2f0ad4 HZ |
333 | && !query (_("The program being debugged has been started already.\n\ |
334 | Start it from the beginning? "))) | |
8a3fe4f8 | 335 | error (_("Program not restarted.")); |
c906108c | 336 | target_kill (); |
c906108c | 337 | } |
a4d5f2e0 JB |
338 | } |
339 | ||
329ea579 | 340 | /* See inferior.h. */ |
8bc2fe48 | 341 | |
329ea579 | 342 | void |
8bc2fe48 PA |
343 | prepare_execution_command (struct target_ops *target, int background) |
344 | { | |
345 | /* If we get a request for running in the bg but the target | |
346 | doesn't support it, error out. */ | |
0c1e6e26 | 347 | if (background && !target_can_async_p (target)) |
8bc2fe48 PA |
348 | error (_("Asynchronous execution not supported on this target.")); |
349 | ||
0b333c5e | 350 | if (!background) |
8bc2fe48 | 351 | { |
0b333c5e PA |
352 | /* If we get a request for running in the fg, then we need to |
353 | simulate synchronous (fg) execution. Note no cleanup is | |
354 | necessary for this. stdin is re-enabled whenever an error | |
355 | reaches the top level. */ | |
a8836c93 | 356 | all_uis_on_sync_execution_starting (); |
8bc2fe48 PA |
357 | } |
358 | } | |
359 | ||
4e5a4f58 JB |
360 | /* Determine how the new inferior will behave. */ |
361 | ||
362 | enum run_how | |
363 | { | |
364 | /* Run program without any explicit stop during startup. */ | |
365 | RUN_NORMAL, | |
366 | ||
367 | /* Stop at the beginning of the program's main function. */ | |
368 | RUN_STOP_AT_MAIN, | |
369 | ||
370 | /* Stop at the first instruction of the program. */ | |
371 | RUN_STOP_AT_FIRST_INSN | |
372 | }; | |
373 | ||
374 | /* Implement the "run" command. Force a stop during program start if | |
375 | requested by RUN_HOW. */ | |
f67a969f | 376 | |
a4d5f2e0 | 377 | static void |
0b39b52e | 378 | run_command_1 (const char *args, int from_tty, enum run_how run_how) |
a4d5f2e0 | 379 | { |
7c5ded6a | 380 | const char *exec_file; |
79a45e25 | 381 | struct ui_out *uiout = current_uiout; |
b3ccfe11 | 382 | struct target_ops *run_target; |
6c4486e6 | 383 | int async_exec; |
c906108c | 384 | |
a4d5f2e0 JB |
385 | dont_repeat (); |
386 | ||
1192f124 SM |
387 | scoped_disable_commit_resumed disable_commit_resumed ("running"); |
388 | ||
a4d5f2e0 | 389 | kill_if_already_running (from_tty); |
3c35e65b UW |
390 | |
391 | init_wait_for_inferior (); | |
c906108c SS |
392 | clear_breakpoint_hit_counts (); |
393 | ||
fd79ecee DJ |
394 | /* Clean up any leftovers from other runs. Some other things from |
395 | this function should probably be moved into target_pre_inferior. */ | |
396 | target_pre_inferior (from_tty); | |
397 | ||
39ad761d JB |
398 | /* The comment here used to read, "The exec file is re-read every |
399 | time we do a generic_mourn_inferior, so we just have to worry | |
400 | about the symbol file." The `generic_mourn_inferior' function | |
401 | gets called whenever the program exits. However, suppose the | |
402 | program exits, and *then* the executable file changes? We need | |
403 | to check again here. Since reopen_exec_file doesn't do anything | |
404 | if the timestamp hasn't changed, I don't see the harm. */ | |
405 | reopen_exec_file (); | |
9dec38d3 | 406 | reread_symbols (from_tty); |
c906108c | 407 | |
6be9a197 TT |
408 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec); |
409 | args = stripped.get (); | |
f67a969f | 410 | |
8bc2fe48 PA |
411 | /* Do validation and preparation before possibly changing anything |
412 | in the inferior. */ | |
39ad761d | 413 | |
b3ccfe11 TT |
414 | run_target = find_run_target (); |
415 | ||
8bc2fe48 PA |
416 | prepare_execution_command (run_target, async_exec); |
417 | ||
f6ac5f3d | 418 | if (non_stop && !run_target->supports_non_stop ()) |
9908b566 VP |
419 | error (_("The target does not support running in non-stop mode.")); |
420 | ||
8bc2fe48 PA |
421 | /* Done. Can now set breakpoints, change inferior args, etc. */ |
422 | ||
4e5a4f58 JB |
423 | /* Insert temporary breakpoint in main function if requested. */ |
424 | if (run_how == RUN_STOP_AT_MAIN) | |
e242fd12 SM |
425 | { |
426 | std::string arg = string_printf ("-qualified %s", main_name ()); | |
427 | tbreak_command (arg.c_str (), 0); | |
428 | } | |
8bc2fe48 | 429 | |
7c5ded6a | 430 | exec_file = get_exec_file (0); |
8bc2fe48 | 431 | |
c906108c SS |
432 | /* We keep symbols from add-symbol-file, on the grounds that the |
433 | user might want to add some symbols before running the program | |
434 | (right?). But sometimes (dynamic loading where the user manually | |
435 | introduces the new symbols with add-symbol-file), the code which | |
436 | the symbols describe does not persist between runs. Currently | |
437 | the user has to manually nuke all symbols between runs if they | |
438 | want them to go away (PR 2207). This is probably reasonable. */ | |
439 | ||
8bc2fe48 PA |
440 | /* If there were other args, beside '&', process them. */ |
441 | if (args != NULL) | |
e5169525 | 442 | current_inferior ()->set_args (args); |
c906108c SS |
443 | |
444 | if (from_tty) | |
445 | { | |
112e8700 SM |
446 | uiout->field_string (NULL, "Starting program"); |
447 | uiout->text (": "); | |
8b93c638 | 448 | if (exec_file) |
972f7a4b TT |
449 | uiout->field_string ("execfile", exec_file, |
450 | file_name_style.style ()); | |
112e8700 | 451 | uiout->spaces (1); |
e5169525 | 452 | uiout->field_string ("infargs", current_inferior ()->args ()); |
112e8700 SM |
453 | uiout->text ("\n"); |
454 | uiout->flush (); | |
c906108c SS |
455 | } |
456 | ||
f6ac5f3d | 457 | run_target->create_inferior (exec_file, |
e5169525 | 458 | current_inferior ()->args (), |
f6ac5f3d PA |
459 | current_inferior ()->environment.envp (), |
460 | from_tty); | |
b3ccfe11 TT |
461 | /* to_create_inferior should push the target, so after this point we |
462 | shouldn't refer to run_target again. */ | |
463 | run_target = NULL; | |
281b533b | 464 | |
1f9d9e32 AB |
465 | infrun_debug_show_threads ("immediately after create_process", |
466 | current_inferior ()->non_exited_threads ()); | |
467 | ||
29f49a6a PA |
468 | /* We're starting off a new process. When we get out of here, in |
469 | non-stop mode, finish the state of all threads of that process, | |
470 | but leave other threads alone, as they may be stopped in internal | |
471 | events --- the frontend shouldn't see them as stopped. In | |
472 | all-stop, always finish the state of all threads, as we may be | |
473 | resuming more than just the new process. */ | |
5b6d1e4f PA |
474 | process_stratum_target *finish_target; |
475 | ptid_t finish_ptid; | |
476 | if (non_stop) | |
477 | { | |
478 | finish_target = current_inferior ()->process_target (); | |
479 | finish_ptid = ptid_t (current_inferior ()->pid); | |
480 | } | |
481 | else | |
482 | { | |
483 | finish_target = nullptr; | |
484 | finish_ptid = minus_one_ptid; | |
485 | } | |
486 | scoped_finish_thread_state finish_state (finish_target, finish_ptid); | |
29f49a6a | 487 | |
de1b3c3d PA |
488 | /* Pass zero for FROM_TTY, because at this point the "run" command |
489 | has done its thing; now we are setting up the running program. */ | |
a7aba266 | 490 | post_create_inferior (0); |
281b533b | 491 | |
4e5a4f58 JB |
492 | /* Queue a pending event so that the program stops immediately. */ |
493 | if (run_how == RUN_STOP_AT_FIRST_INSN) | |
494 | { | |
495 | thread_info *thr = inferior_thread (); | |
1edb66d8 | 496 | target_waitstatus ws; |
183be222 | 497 | ws.set_stopped (GDB_SIGNAL_0); |
1edb66d8 | 498 | thr->set_pending_waitstatus (ws); |
4e5a4f58 JB |
499 | } |
500 | ||
74d1f91e JK |
501 | /* Start the target running. Do not use -1 continuation as it would skip |
502 | breakpoint right at the entry point. */ | |
64ce06e4 | 503 | proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0); |
c906108c | 504 | |
29f49a6a PA |
505 | /* Since there was no error, there's no need to finish the thread |
506 | states here. */ | |
731f534f | 507 | finish_state.release (); |
1192f124 SM |
508 | |
509 | disable_commit_resumed.reset_and_commit (); | |
29f49a6a | 510 | } |
c906108c | 511 | |
f67a969f | 512 | static void |
0b39b52e | 513 | run_command (const char *args, int from_tty) |
f67a969f | 514 | { |
4e5a4f58 | 515 | run_command_1 (args, from_tty, RUN_NORMAL); |
f67a969f JB |
516 | } |
517 | ||
a4d5f2e0 JB |
518 | /* Start the execution of the program up until the beginning of the main |
519 | program. */ | |
520 | ||
521 | static void | |
0b39b52e | 522 | start_command (const char *args, int from_tty) |
a4d5f2e0 JB |
523 | { |
524 | /* Some languages such as Ada need to search inside the program | |
525 | minimal symbols for the location where to put the temporary | |
526 | breakpoint before starting. */ | |
527 | if (!have_minimal_symbols ()) | |
8a3fe4f8 | 528 | error (_("No symbol table loaded. Use the \"file\" command.")); |
a4d5f2e0 | 529 | |
f67a969f | 530 | /* Run the program until reaching the main procedure... */ |
4e5a4f58 JB |
531 | run_command_1 (args, from_tty, RUN_STOP_AT_MAIN); |
532 | } | |
533 | ||
534 | /* Start the execution of the program stopping at the first | |
535 | instruction. */ | |
536 | ||
537 | static void | |
0b39b52e | 538 | starti_command (const char *args, int from_tty) |
4e5a4f58 JB |
539 | { |
540 | run_command_1 (args, from_tty, RUN_STOP_AT_FIRST_INSN); | |
a4d5f2e0 JB |
541 | } |
542 | ||
8cae4b3f PA |
543 | static int |
544 | proceed_thread_callback (struct thread_info *thread, void *arg) | |
545 | { | |
74531fed PA |
546 | /* We go through all threads individually instead of compressing |
547 | into a single target `resume_all' request, because some threads | |
548 | may be stopped in internal breakpoints/events, or stopped waiting | |
549 | for its turn in the displaced stepping queue (that is, they are | |
550 | running && !executing). The target side has no idea about why | |
551 | the thread is stopped, so a `resume_all' command would resume too | |
552 | much. If/when GDB gains a way to tell the target `hold this | |
553 | thread stopped until I say otherwise', then we can optimize | |
554 | this. */ | |
00431a78 | 555 | if (thread->state != THREAD_STOPPED) |
8cae4b3f PA |
556 | return 0; |
557 | ||
5b6d1e4f PA |
558 | if (!thread->inf->has_execution ()) |
559 | return 0; | |
560 | ||
00431a78 | 561 | switch_to_thread (thread); |
70509625 | 562 | clear_proceed_status (0); |
64ce06e4 | 563 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
8cae4b3f PA |
564 | return 0; |
565 | } | |
566 | ||
70221824 | 567 | static void |
d729566a PA |
568 | ensure_valid_thread (void) |
569 | { | |
00431a78 PA |
570 | if (inferior_ptid == null_ptid |
571 | || inferior_thread ()->state == THREAD_EXITED) | |
3e43a32a | 572 | error (_("Cannot execute this command without a live selected thread.")); |
d729566a PA |
573 | } |
574 | ||
573cda03 | 575 | /* If the user is looking at trace frames, any resumption of execution |
1777feb0 | 576 | is likely to mix up recorded and live target data. So simply |
573cda03 SS |
577 | disallow those commands. */ |
578 | ||
70221824 | 579 | static void |
573cda03 SS |
580 | ensure_not_tfind_mode (void) |
581 | { | |
582 | if (get_traceframe_number () >= 0) | |
3e43a32a | 583 | error (_("Cannot execute this command while looking at trace frames.")); |
573cda03 SS |
584 | } |
585 | ||
3d3fef6b YQ |
586 | /* Throw an error indicating the current thread is running. */ |
587 | ||
588 | static void | |
589 | error_is_running (void) | |
590 | { | |
591 | error (_("Cannot execute this command while " | |
592 | "the selected thread is running.")); | |
593 | } | |
594 | ||
595 | /* Calls error_is_running if the current thread is running. */ | |
596 | ||
597 | static void | |
598 | ensure_not_running (void) | |
599 | { | |
00431a78 | 600 | if (inferior_thread ()->state == THREAD_RUNNING) |
3d3fef6b YQ |
601 | error_is_running (); |
602 | } | |
603 | ||
77ebaa5a VP |
604 | void |
605 | continue_1 (int all_threads) | |
606 | { | |
3d488bfc | 607 | ERROR_NO_INFERIOR; |
573cda03 | 608 | ensure_not_tfind_mode (); |
3d488bfc | 609 | |
77ebaa5a VP |
610 | if (non_stop && all_threads) |
611 | { | |
612 | /* Don't error out if the current thread is running, because | |
abbb1732 | 613 | there may be other stopped threads. */ |
77ebaa5a | 614 | |
5ed8105e PA |
615 | /* Backup current thread and selected frame and restore on scope |
616 | exit. */ | |
617 | scoped_restore_current_thread restore_thread; | |
6f4cb31c SM |
618 | scoped_disable_commit_resumed disable_commit_resumed |
619 | ("continue all threads in non-stop"); | |
77ebaa5a VP |
620 | |
621 | iterate_over_threads (proceed_thread_callback, NULL); | |
622 | ||
3b12939d | 623 | if (current_ui->prompt_state == PROMPT_BLOCKED) |
0ff33695 PA |
624 | { |
625 | /* If all threads in the target were already running, | |
626 | proceed_thread_callback ends up never calling proceed, | |
627 | and so nothing calls this to put the inferior's terminal | |
628 | settings in effect and remove stdin from the event loop, | |
629 | which we must when running a foreground command. E.g.: | |
630 | ||
631 | (gdb) c -a& | |
632 | Continuing. | |
633 | <all threads are running now> | |
634 | (gdb) c -a | |
635 | Continuing. | |
636 | <no thread was resumed, but the inferior now owns the terminal> | |
637 | */ | |
223ffa71 | 638 | target_terminal::inferior (); |
0ff33695 | 639 | } |
6f4cb31c SM |
640 | |
641 | disable_commit_resumed.reset_and_commit (); | |
77ebaa5a VP |
642 | } |
643 | else | |
644 | { | |
d729566a | 645 | ensure_valid_thread (); |
77ebaa5a | 646 | ensure_not_running (); |
70509625 | 647 | clear_proceed_status (0); |
64ce06e4 | 648 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
77ebaa5a VP |
649 | } |
650 | } | |
651 | ||
8cae4b3f | 652 | /* continue [-a] [proceed-count] [&] */ |
6339bfc4 | 653 | |
1dd5fedc | 654 | static void |
0b39b52e | 655 | continue_command (const char *args, int from_tty) |
c906108c | 656 | { |
6c4486e6 | 657 | int async_exec; |
5b6d1e4f | 658 | bool all_threads_p = false; |
6c4486e6 | 659 | |
c906108c SS |
660 | ERROR_NO_INFERIOR; |
661 | ||
1777feb0 | 662 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
663 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec); |
664 | args = stripped.get (); | |
43ff13b4 | 665 | |
8cae4b3f PA |
666 | if (args != NULL) |
667 | { | |
61012eef | 668 | if (startswith (args, "-a")) |
8cae4b3f | 669 | { |
5b6d1e4f | 670 | all_threads_p = true; |
8cae4b3f PA |
671 | args += sizeof ("-a") - 1; |
672 | if (*args == '\0') | |
673 | args = NULL; | |
674 | } | |
675 | } | |
676 | ||
5b6d1e4f | 677 | if (!non_stop && all_threads_p) |
8cae4b3f PA |
678 | error (_("`-a' is meaningless in all-stop mode.")); |
679 | ||
5b6d1e4f | 680 | if (args != NULL && all_threads_p) |
3e43a32a MS |
681 | error (_("Can't resume all threads and specify " |
682 | "proceed count simultaneously.")); | |
8cae4b3f PA |
683 | |
684 | /* If we have an argument left, set proceed count of breakpoint we | |
685 | stopped at. */ | |
686 | if (args != NULL) | |
c906108c | 687 | { |
313f3b21 | 688 | bpstat *bs = nullptr; |
8671a17b PA |
689 | int num, stat; |
690 | int stopped = 0; | |
347bddb7 PA |
691 | struct thread_info *tp; |
692 | ||
693 | if (non_stop) | |
00431a78 | 694 | tp = inferior_thread (); |
347bddb7 PA |
695 | else |
696 | { | |
5b6d1e4f | 697 | process_stratum_target *last_target; |
347bddb7 | 698 | ptid_t last_ptid; |
347bddb7 | 699 | |
5b6d1e4f PA |
700 | get_last_target_status (&last_target, &last_ptid, nullptr); |
701 | tp = find_thread_ptid (last_target, last_ptid); | |
347bddb7 PA |
702 | } |
703 | if (tp != NULL) | |
16c381f0 | 704 | bs = tp->control.stop_bpstat; |
8671a17b PA |
705 | |
706 | while ((stat = bpstat_num (&bs, &num)) != 0) | |
707 | if (stat > 0) | |
708 | { | |
709 | set_ignore_count (num, | |
8cae4b3f | 710 | parse_and_eval_long (args) - 1, |
8671a17b PA |
711 | from_tty); |
712 | /* set_ignore_count prints a message ending with a period. | |
713 | So print two spaces before "Continuing.". */ | |
714 | if (from_tty) | |
6cb06a8c | 715 | gdb_printf (" "); |
8671a17b PA |
716 | stopped = 1; |
717 | } | |
718 | ||
719 | if (!stopped && from_tty) | |
c906108c | 720 | { |
6cb06a8c | 721 | gdb_printf |
c906108c SS |
722 | ("Not stopped at any breakpoint; argument ignored.\n"); |
723 | } | |
c906108c SS |
724 | } |
725 | ||
3b12939d PA |
726 | ERROR_NO_INFERIOR; |
727 | ensure_not_tfind_mode (); | |
728 | ||
5b6d1e4f | 729 | if (!non_stop || !all_threads_p) |
3b12939d PA |
730 | { |
731 | ensure_valid_thread (); | |
732 | ensure_not_running (); | |
733 | } | |
734 | ||
328d42d8 | 735 | prepare_execution_command (current_inferior ()->top_target (), async_exec); |
3b12939d | 736 | |
c906108c | 737 | if (from_tty) |
6cb06a8c | 738 | gdb_printf (_("Continuing.\n")); |
c906108c | 739 | |
5b6d1e4f | 740 | continue_1 (all_threads_p); |
c906108c SS |
741 | } |
742 | \f | |
29734269 | 743 | /* Record in TP the starting point of a "step" or "next" command. */ |
edb3359d DJ |
744 | |
745 | static void | |
29734269 | 746 | set_step_frame (thread_info *tp) |
edb3359d | 747 | { |
29734269 SM |
748 | /* This can be removed once this function no longer implicitly relies on the |
749 | inferior_ptid value. */ | |
750 | gdb_assert (inferior_ptid == tp->ptid); | |
751 | ||
51abb421 | 752 | frame_info *frame = get_current_frame (); |
edb3359d | 753 | |
51abb421 | 754 | symtab_and_line sal = find_frame_sal (frame); |
29734269 | 755 | set_step_info (tp, frame, sal); |
51abb421 PA |
756 | |
757 | CORE_ADDR pc = get_frame_pc (frame); | |
64ce06e4 | 758 | tp->control.step_start_function = find_pc_function (pc); |
edb3359d DJ |
759 | } |
760 | ||
c906108c SS |
761 | /* Step until outside of current statement. */ |
762 | ||
c906108c | 763 | static void |
0b39b52e | 764 | step_command (const char *count_string, int from_tty) |
c906108c SS |
765 | { |
766 | step_1 (0, 0, count_string); | |
767 | } | |
768 | ||
769 | /* Likewise, but skip over subroutine calls as if single instructions. */ | |
770 | ||
c906108c | 771 | static void |
0b39b52e | 772 | next_command (const char *count_string, int from_tty) |
c906108c SS |
773 | { |
774 | step_1 (1, 0, count_string); | |
775 | } | |
776 | ||
777 | /* Likewise, but step only one instruction. */ | |
778 | ||
1dd5fedc | 779 | static void |
0b39b52e | 780 | stepi_command (const char *count_string, int from_tty) |
c906108c SS |
781 | { |
782 | step_1 (0, 1, count_string); | |
783 | } | |
784 | ||
1dd5fedc | 785 | static void |
0b39b52e | 786 | nexti_command (const char *count_string, int from_tty) |
c906108c SS |
787 | { |
788 | step_1 (1, 1, count_string); | |
789 | } | |
790 | ||
243a9253 PA |
791 | /* Data for the FSM that manages the step/next/stepi/nexti |
792 | commands. */ | |
793 | ||
46e3ed7f | 794 | struct step_command_fsm : public thread_fsm |
243a9253 | 795 | { |
243a9253 PA |
796 | /* How many steps left in a "step N"-like command. */ |
797 | int count; | |
798 | ||
799 | /* If true, this is a next/nexti, otherwise a step/stepi. */ | |
800 | int skip_subroutines; | |
801 | ||
802 | /* If true, this is a stepi/nexti, otherwise a step/step. */ | |
803 | int single_inst; | |
243a9253 | 804 | |
46e3ed7f TT |
805 | explicit step_command_fsm (struct interp *cmd_interp) |
806 | : thread_fsm (cmd_interp) | |
807 | { | |
808 | } | |
243a9253 | 809 | |
46e3ed7f TT |
810 | void clean_up (struct thread_info *thread) override; |
811 | bool should_stop (struct thread_info *thread) override; | |
812 | enum async_reply_reason do_async_reply_reason () override; | |
243a9253 PA |
813 | }; |
814 | ||
243a9253 PA |
815 | /* Prepare for a step/next/etc. command. Any target resource |
816 | allocated here is undone in the FSM's clean_up method. */ | |
817 | ||
818 | static void | |
819 | step_command_fsm_prepare (struct step_command_fsm *sm, | |
820 | int skip_subroutines, int single_inst, | |
821 | int count, struct thread_info *thread) | |
822 | { | |
823 | sm->skip_subroutines = skip_subroutines; | |
824 | sm->single_inst = single_inst; | |
825 | sm->count = count; | |
243a9253 PA |
826 | |
827 | /* Leave the si command alone. */ | |
828 | if (!sm->single_inst || sm->skip_subroutines) | |
829 | set_longjmp_breakpoint (thread, get_frame_id (get_current_frame ())); | |
830 | ||
831 | thread->control.stepping_command = 1; | |
832 | } | |
833 | ||
29734269 | 834 | static int prepare_one_step (thread_info *, struct step_command_fsm *sm); |
243a9253 | 835 | |
c906108c | 836 | static void |
0b39b52e | 837 | step_1 (int skip_subroutines, int single_inst, const char *count_string) |
c906108c | 838 | { |
243a9253 | 839 | int count; |
6c4486e6 | 840 | int async_exec; |
243a9253 PA |
841 | struct thread_info *thr; |
842 | struct step_command_fsm *step_sm; | |
c5aa993b | 843 | |
c906108c | 844 | ERROR_NO_INFERIOR; |
573cda03 | 845 | ensure_not_tfind_mode (); |
d729566a | 846 | ensure_valid_thread (); |
94cc34af | 847 | ensure_not_running (); |
43ff13b4 | 848 | |
6be9a197 TT |
849 | gdb::unique_xmalloc_ptr<char> stripped |
850 | = strip_bg_char (count_string, &async_exec); | |
851 | count_string = stripped.get (); | |
c5aa993b | 852 | |
328d42d8 | 853 | prepare_execution_command (current_inferior ()->top_target (), async_exec); |
43ff13b4 | 854 | |
bb518678 | 855 | count = count_string ? parse_and_eval_long (count_string) : 1; |
c906108c | 856 | |
243a9253 | 857 | clear_proceed_status (1); |
611c83ae | 858 | |
243a9253 PA |
859 | /* Setup the execution command state machine to handle all the COUNT |
860 | steps. */ | |
861 | thr = inferior_thread (); | |
46e3ed7f | 862 | step_sm = new step_command_fsm (command_interp ()); |
573269a8 | 863 | thr->set_thread_fsm (std::unique_ptr<thread_fsm> (step_sm)); |
611c83ae | 864 | |
243a9253 PA |
865 | step_command_fsm_prepare (step_sm, skip_subroutines, |
866 | single_inst, count, thr); | |
c2d11a7d | 867 | |
0b333c5e PA |
868 | /* Do only one step for now, before returning control to the event |
869 | loop. Let the continuation figure out how many other steps we | |
870 | need to do, and handle them one at the time, through | |
871 | step_once. */ | |
29734269 | 872 | if (!prepare_one_step (thr, step_sm)) |
243a9253 PA |
873 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
874 | else | |
875 | { | |
3b12939d PA |
876 | int proceeded; |
877 | ||
243a9253 PA |
878 | /* Stepped into an inline frame. Pretend that we've |
879 | stopped. */ | |
573269a8 | 880 | thr->thread_fsm ()->clean_up (thr); |
3b12939d PA |
881 | proceeded = normal_stop (); |
882 | if (!proceeded) | |
b1a35af2 | 883 | inferior_event_handler (INF_EXEC_COMPLETE); |
3b12939d | 884 | all_uis_check_sync_execution_done (); |
243a9253 | 885 | } |
c2d11a7d | 886 | } |
c906108c | 887 | |
243a9253 PA |
888 | /* Implementation of the 'should_stop' FSM method for stepping |
889 | commands. Called after we are done with one step operation, to | |
890 | check whether we need to step again, before we print the prompt and | |
891 | return control to the user. If count is > 1, returns false, as we | |
892 | will need to keep going. */ | |
6339bfc4 | 893 | |
46e3ed7f TT |
894 | bool |
895 | step_command_fsm::should_stop (struct thread_info *tp) | |
c2d11a7d | 896 | { |
243a9253 | 897 | if (tp->control.stop_step) |
f13468d9 | 898 | { |
243a9253 PA |
899 | /* There are more steps to make, and we did stop due to |
900 | ending a stepping range. Do another step. */ | |
46e3ed7f | 901 | if (--count > 0) |
29734269 | 902 | return prepare_one_step (tp, this); |
af679fd0 | 903 | |
46e3ed7f | 904 | set_finished (); |
f107f563 | 905 | } |
af679fd0 | 906 | |
46e3ed7f | 907 | return true; |
c2d11a7d JM |
908 | } |
909 | ||
243a9253 | 910 | /* Implementation of the 'clean_up' FSM method for stepping commands. */ |
37d94800 | 911 | |
46e3ed7f TT |
912 | void |
913 | step_command_fsm::clean_up (struct thread_info *thread) | |
bfec99b2 | 914 | { |
46e3ed7f | 915 | if (!single_inst || skip_subroutines) |
8980e177 | 916 | delete_longjmp_breakpoint (thread->global_num); |
243a9253 PA |
917 | } |
918 | ||
919 | /* Implementation of the 'async_reply_reason' FSM method for stepping | |
920 | commands. */ | |
921 | ||
46e3ed7f TT |
922 | enum async_reply_reason |
923 | step_command_fsm::do_async_reply_reason () | |
243a9253 PA |
924 | { |
925 | return EXEC_ASYNC_END_STEPPING_RANGE; | |
926 | } | |
927 | ||
928 | /* Prepare for one step in "step N". The actual target resumption is | |
929 | done by the caller. Return true if we're done and should thus | |
930 | report a stop to the user. Returns false if the target needs to be | |
931 | resumed. */ | |
c2d11a7d | 932 | |
243a9253 | 933 | static int |
29734269 | 934 | prepare_one_step (thread_info *tp, struct step_command_fsm *sm) |
243a9253 | 935 | { |
29734269 SM |
936 | /* This can be removed once this function no longer implicitly relies on the |
937 | inferior_ptid value. */ | |
938 | gdb_assert (inferior_ptid == tp->ptid); | |
939 | ||
243a9253 | 940 | if (sm->count > 0) |
c906108c | 941 | { |
243a9253 PA |
942 | struct frame_info *frame = get_current_frame (); |
943 | ||
29734269 | 944 | set_step_frame (tp); |
c906108c | 945 | |
243a9253 | 946 | if (!sm->single_inst) |
c906108c | 947 | { |
1641cfcc PA |
948 | CORE_ADDR pc; |
949 | ||
edb3359d | 950 | /* Step at an inlined function behaves like "down". */ |
243a9253 | 951 | if (!sm->skip_subroutines |
00431a78 | 952 | && inline_skipped_frames (tp)) |
edb3359d | 953 | { |
09cee04b | 954 | ptid_t resume_ptid; |
4a4c04f1 BE |
955 | const char *fn = NULL; |
956 | symtab_and_line sal; | |
957 | struct symbol *sym; | |
09cee04b PA |
958 | |
959 | /* Pretend that we've ran. */ | |
960 | resume_ptid = user_visible_resume_ptid (1); | |
5b6d1e4f | 961 | set_running (tp->inf->process_target (), resume_ptid, true); |
09cee04b | 962 | |
00431a78 | 963 | step_into_inline_frame (tp); |
4a4c04f1 BE |
964 | |
965 | frame = get_current_frame (); | |
966 | sal = find_frame_sal (frame); | |
967 | sym = get_frame_function (frame); | |
968 | ||
969 | if (sym != NULL) | |
970 | fn = sym->print_name (); | |
971 | ||
972 | if (sal.line == 0 | |
973 | || !function_name_is_marked_for_skip (fn, sal)) | |
974 | { | |
975 | sm->count--; | |
29734269 | 976 | return prepare_one_step (tp, sm); |
4a4c04f1 | 977 | } |
edb3359d DJ |
978 | } |
979 | ||
1641cfcc PA |
980 | pc = get_frame_pc (frame); |
981 | find_pc_line_pc_range (pc, | |
16c381f0 JK |
982 | &tp->control.step_range_start, |
983 | &tp->control.step_range_end); | |
5fbbeb29 | 984 | |
340d00fb TV |
985 | /* There's a problem in gcc (PR gcc/98780) that causes missing line |
986 | table entries, which results in a too large stepping range. | |
987 | Use inlined_subroutine info to make the range more narrow. */ | |
988 | if (inline_skipped_frames (tp) > 0) | |
989 | { | |
990 | symbol *sym = inline_skipped_symbol (tp); | |
66d7f48f | 991 | if (sym->aclass () == LOC_BLOCK) |
340d00fb | 992 | { |
4aeddc50 | 993 | const block *block = sym->value_block (); |
4b8791e1 SM |
994 | if (block->end () < tp->control.step_range_end) |
995 | tp->control.step_range_end = block->end (); | |
340d00fb TV |
996 | } |
997 | } | |
998 | ||
c1e36e3e PA |
999 | tp->control.may_range_step = 1; |
1000 | ||
5fbbeb29 | 1001 | /* If we have no line info, switch to stepi mode. */ |
16c381f0 | 1002 | if (tp->control.step_range_end == 0 && step_stop_if_no_debug) |
c1e36e3e PA |
1003 | { |
1004 | tp->control.step_range_start = tp->control.step_range_end = 1; | |
1005 | tp->control.may_range_step = 0; | |
1006 | } | |
16c381f0 | 1007 | else if (tp->control.step_range_end == 0) |
c906108c | 1008 | { |
2c02bd72 | 1009 | const char *name; |
abbb1732 | 1010 | |
1641cfcc | 1011 | if (find_pc_partial_function (pc, &name, |
16c381f0 JK |
1012 | &tp->control.step_range_start, |
1013 | &tp->control.step_range_end) == 0) | |
8a3fe4f8 | 1014 | error (_("Cannot find bounds of current function")); |
c906108c | 1015 | |
223ffa71 | 1016 | target_terminal::ours_for_output (); |
6cb06a8c TT |
1017 | gdb_printf (_("Single stepping until exit from function %s," |
1018 | "\nwhich has no line number information.\n"), | |
1019 | name); | |
c906108c SS |
1020 | } |
1021 | } | |
1022 | else | |
1023 | { | |
1024 | /* Say we are stepping, but stop after one insn whatever it does. */ | |
16c381f0 | 1025 | tp->control.step_range_start = tp->control.step_range_end = 1; |
243a9253 | 1026 | if (!sm->skip_subroutines) |
c906108c SS |
1027 | /* It is stepi. |
1028 | Don't step over function calls, not even to functions lacking | |
1029 | line numbers. */ | |
16c381f0 | 1030 | tp->control.step_over_calls = STEP_OVER_NONE; |
c906108c SS |
1031 | } |
1032 | ||
243a9253 | 1033 | if (sm->skip_subroutines) |
16c381f0 | 1034 | tp->control.step_over_calls = STEP_OVER_ALL; |
c906108c | 1035 | |
243a9253 | 1036 | return 0; |
c906108c | 1037 | } |
243a9253 PA |
1038 | |
1039 | /* Done. */ | |
46e3ed7f | 1040 | sm->set_finished (); |
243a9253 | 1041 | return 1; |
c906108c | 1042 | } |
c2d11a7d | 1043 | |
c906108c SS |
1044 | \f |
1045 | /* Continue program at specified address. */ | |
1046 | ||
1047 | static void | |
0b39b52e | 1048 | jump_command (const char *arg, int from_tty) |
c906108c | 1049 | { |
5af949e3 | 1050 | struct gdbarch *gdbarch = get_current_arch (); |
52f0bd74 | 1051 | CORE_ADDR addr; |
c906108c SS |
1052 | struct symbol *fn; |
1053 | struct symbol *sfn; | |
6c4486e6 | 1054 | int async_exec; |
c5aa993b | 1055 | |
c906108c | 1056 | ERROR_NO_INFERIOR; |
573cda03 | 1057 | ensure_not_tfind_mode (); |
d729566a | 1058 | ensure_valid_thread (); |
94cc34af | 1059 | ensure_not_running (); |
c906108c | 1060 | |
1777feb0 | 1061 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
1062 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); |
1063 | arg = stripped.get (); | |
43ff13b4 | 1064 | |
328d42d8 | 1065 | prepare_execution_command (current_inferior ()->top_target (), async_exec); |
43ff13b4 | 1066 | |
c906108c | 1067 | if (!arg) |
e2e0b3e5 | 1068 | error_no_arg (_("starting address")); |
c906108c | 1069 | |
6c5b2ebe PA |
1070 | std::vector<symtab_and_line> sals |
1071 | = decode_line_with_last_displayed (arg, DECODE_LINE_FUNFIRSTLINE); | |
1072 | if (sals.size () != 1) | |
1073 | error (_("Unreasonable jump request")); | |
c906108c | 1074 | |
6c5b2ebe PA |
1075 | symtab_and_line &sal = sals[0]; |
1076 | ||
c906108c | 1077 | if (sal.symtab == 0 && sal.pc == 0) |
8a3fe4f8 | 1078 | error (_("No source file has been specified.")); |
c906108c | 1079 | |
1777feb0 | 1080 | resolve_sal_pc (&sal); /* May error out. */ |
c906108c | 1081 | |
1777feb0 | 1082 | /* See if we are trying to jump to another function. */ |
c906108c SS |
1083 | fn = get_frame_function (get_current_frame ()); |
1084 | sfn = find_pc_function (sal.pc); | |
1085 | if (fn != NULL && sfn != fn) | |
1086 | { | |
9e2f0ad4 | 1087 | if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal.line, |
987012b8 | 1088 | fn->print_name ())) |
c906108c | 1089 | { |
8a3fe4f8 | 1090 | error (_("Not confirmed.")); |
c906108c SS |
1091 | /* NOTREACHED */ |
1092 | } | |
1093 | } | |
1094 | ||
c5aa993b | 1095 | if (sfn != NULL) |
c906108c | 1096 | { |
253342b8 DE |
1097 | struct obj_section *section; |
1098 | ||
c906108c | 1099 | fixup_symbol_section (sfn, 0); |
e19b2d94 | 1100 | section = sfn->obj_section (sfn->objfile ()); |
253342b8 DE |
1101 | if (section_is_overlay (section) |
1102 | && !section_is_mapped (section)) | |
c906108c | 1103 | { |
3e43a32a MS |
1104 | if (!query (_("WARNING!!! Destination is in " |
1105 | "unmapped overlay! Jump anyway? "))) | |
c906108c | 1106 | { |
8a3fe4f8 | 1107 | error (_("Not confirmed.")); |
c906108c SS |
1108 | /* NOTREACHED */ |
1109 | } | |
1110 | } | |
1111 | } | |
1112 | ||
c906108c SS |
1113 | addr = sal.pc; |
1114 | ||
1115 | if (from_tty) | |
1116 | { | |
6cb06a8c | 1117 | gdb_printf (_("Continuing at ")); |
0426ad51 | 1118 | gdb_puts (paddress (gdbarch, addr)); |
6cb06a8c | 1119 | gdb_printf (".\n"); |
c906108c SS |
1120 | } |
1121 | ||
70509625 | 1122 | clear_proceed_status (0); |
64ce06e4 | 1123 | proceed (addr, GDB_SIGNAL_0); |
c906108c | 1124 | } |
c906108c | 1125 | \f |
c906108c SS |
1126 | /* Continue program giving it specified signal. */ |
1127 | ||
1128 | static void | |
0b39b52e | 1129 | signal_command (const char *signum_exp, int from_tty) |
c906108c | 1130 | { |
2ea28649 | 1131 | enum gdb_signal oursig; |
6c4486e6 | 1132 | int async_exec; |
c906108c SS |
1133 | |
1134 | dont_repeat (); /* Too dangerous. */ | |
1135 | ERROR_NO_INFERIOR; | |
573cda03 | 1136 | ensure_not_tfind_mode (); |
d729566a | 1137 | ensure_valid_thread (); |
94cc34af | 1138 | ensure_not_running (); |
c906108c | 1139 | |
32c1e744 | 1140 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
1141 | gdb::unique_xmalloc_ptr<char> stripped |
1142 | = strip_bg_char (signum_exp, &async_exec); | |
1143 | signum_exp = stripped.get (); | |
32c1e744 | 1144 | |
328d42d8 | 1145 | prepare_execution_command (current_inferior ()->top_target (), async_exec); |
32c1e744 | 1146 | |
c906108c | 1147 | if (!signum_exp) |
e2e0b3e5 | 1148 | error_no_arg (_("signal number")); |
c906108c SS |
1149 | |
1150 | /* It would be even slicker to make signal names be valid expressions, | |
1151 | (the type could be "enum $signal" or some such), then the user could | |
1152 | assign them to convenience variables. */ | |
2ea28649 | 1153 | oursig = gdb_signal_from_name (signum_exp); |
c906108c | 1154 | |
a493e3e2 | 1155 | if (oursig == GDB_SIGNAL_UNKNOWN) |
c906108c SS |
1156 | { |
1157 | /* No, try numeric. */ | |
bb518678 | 1158 | int num = parse_and_eval_long (signum_exp); |
c906108c SS |
1159 | |
1160 | if (num == 0) | |
a493e3e2 | 1161 | oursig = GDB_SIGNAL_0; |
c906108c | 1162 | else |
2ea28649 | 1163 | oursig = gdb_signal_from_command (num); |
c906108c SS |
1164 | } |
1165 | ||
70509625 PA |
1166 | /* Look for threads other than the current that this command ends up |
1167 | resuming too (due to schedlock off), and warn if they'll get a | |
1168 | signal delivered. "signal 0" is used to suppress a previous | |
1169 | signal, but if the current thread is no longer the one that got | |
1170 | the signal, then the user is potentially suppressing the signal | |
1171 | of the wrong thread. */ | |
1172 | if (!non_stop) | |
1173 | { | |
70509625 PA |
1174 | int must_confirm = 0; |
1175 | ||
1176 | /* This indicates what will be resumed. Either a single thread, | |
1177 | a whole process, or all threads of all processes. */ | |
08036331 | 1178 | ptid_t resume_ptid = user_visible_resume_ptid (0); |
5b6d1e4f PA |
1179 | process_stratum_target *resume_target |
1180 | = user_visible_resume_target (resume_ptid); | |
70509625 | 1181 | |
5b6d1e4f PA |
1182 | thread_info *current = inferior_thread (); |
1183 | ||
1184 | for (thread_info *tp : all_non_exited_threads (resume_target, resume_ptid)) | |
70509625 | 1185 | { |
5b6d1e4f | 1186 | if (tp == current) |
70509625 | 1187 | continue; |
70509625 | 1188 | |
1edb66d8 SM |
1189 | if (tp->stop_signal () != GDB_SIGNAL_0 |
1190 | && signal_pass_state (tp->stop_signal ())) | |
70509625 PA |
1191 | { |
1192 | if (!must_confirm) | |
6cb06a8c TT |
1193 | gdb_printf (_("Note:\n")); |
1194 | gdb_printf (_(" Thread %s previously stopped with signal %s, %s.\n"), | |
1195 | print_thread_id (tp), | |
1196 | gdb_signal_to_name (tp->stop_signal ()), | |
1197 | gdb_signal_to_string (tp->stop_signal ())); | |
70509625 PA |
1198 | must_confirm = 1; |
1199 | } | |
1200 | } | |
1201 | ||
1202 | if (must_confirm | |
43792cf0 | 1203 | && !query (_("Continuing thread %s (the current thread) with specified signal will\n" |
70509625 PA |
1204 | "still deliver the signals noted above to their respective threads.\n" |
1205 | "Continue anyway? "), | |
43792cf0 | 1206 | print_thread_id (inferior_thread ()))) |
70509625 PA |
1207 | error (_("Not confirmed.")); |
1208 | } | |
1209 | ||
c906108c SS |
1210 | if (from_tty) |
1211 | { | |
a493e3e2 | 1212 | if (oursig == GDB_SIGNAL_0) |
6cb06a8c | 1213 | gdb_printf (_("Continuing with no signal.\n")); |
c906108c | 1214 | else |
6cb06a8c TT |
1215 | gdb_printf (_("Continuing with signal %s.\n"), |
1216 | gdb_signal_to_name (oursig)); | |
c906108c SS |
1217 | } |
1218 | ||
70509625 | 1219 | clear_proceed_status (0); |
64ce06e4 | 1220 | proceed ((CORE_ADDR) -1, oursig); |
c906108c SS |
1221 | } |
1222 | ||
81219e53 DE |
1223 | /* Queue a signal to be delivered to the current thread. */ |
1224 | ||
1225 | static void | |
0b39b52e | 1226 | queue_signal_command (const char *signum_exp, int from_tty) |
81219e53 DE |
1227 | { |
1228 | enum gdb_signal oursig; | |
1229 | struct thread_info *tp; | |
1230 | ||
1231 | ERROR_NO_INFERIOR; | |
1232 | ensure_not_tfind_mode (); | |
1233 | ensure_valid_thread (); | |
1234 | ensure_not_running (); | |
1235 | ||
1236 | if (signum_exp == NULL) | |
1237 | error_no_arg (_("signal number")); | |
1238 | ||
1239 | /* It would be even slicker to make signal names be valid expressions, | |
1240 | (the type could be "enum $signal" or some such), then the user could | |
1241 | assign them to convenience variables. */ | |
1242 | oursig = gdb_signal_from_name (signum_exp); | |
1243 | ||
1244 | if (oursig == GDB_SIGNAL_UNKNOWN) | |
1245 | { | |
1246 | /* No, try numeric. */ | |
1247 | int num = parse_and_eval_long (signum_exp); | |
1248 | ||
1249 | if (num == 0) | |
1250 | oursig = GDB_SIGNAL_0; | |
1251 | else | |
1252 | oursig = gdb_signal_from_command (num); | |
1253 | } | |
1254 | ||
1255 | if (oursig != GDB_SIGNAL_0 | |
1256 | && !signal_pass_state (oursig)) | |
1257 | error (_("Signal handling set to not pass this signal to the program.")); | |
1258 | ||
1259 | tp = inferior_thread (); | |
1edb66d8 | 1260 | tp->set_stop_signal (oursig); |
81219e53 DE |
1261 | } |
1262 | ||
cfc31633 PA |
1263 | /* Data for the FSM that manages the until (with no argument) |
1264 | command. */ | |
1265 | ||
46e3ed7f | 1266 | struct until_next_fsm : public thread_fsm |
fa4cd53f | 1267 | { |
cfc31633 | 1268 | /* The thread that as current when the command was executed. */ |
fa4cd53f | 1269 | int thread; |
cfc31633 | 1270 | |
46e3ed7f TT |
1271 | until_next_fsm (struct interp *cmd_interp, int thread) |
1272 | : thread_fsm (cmd_interp), | |
1273 | thread (thread) | |
1274 | { | |
1275 | } | |
cfc31633 | 1276 | |
46e3ed7f TT |
1277 | bool should_stop (struct thread_info *thread) override; |
1278 | void clean_up (struct thread_info *thread) override; | |
1279 | enum async_reply_reason do_async_reply_reason () override; | |
cfc31633 PA |
1280 | }; |
1281 | ||
cfc31633 PA |
1282 | /* Implementation of the 'should_stop' FSM method for the until (with |
1283 | no arg) command. */ | |
1284 | ||
46e3ed7f TT |
1285 | bool |
1286 | until_next_fsm::should_stop (struct thread_info *tp) | |
cfc31633 | 1287 | { |
cfc31633 | 1288 | if (tp->control.stop_step) |
46e3ed7f | 1289 | set_finished (); |
cfc31633 | 1290 | |
46e3ed7f | 1291 | return true; |
cfc31633 PA |
1292 | } |
1293 | ||
1294 | /* Implementation of the 'clean_up' FSM method for the until (with no | |
1295 | arg) command. */ | |
186c406b | 1296 | |
46e3ed7f TT |
1297 | void |
1298 | until_next_fsm::clean_up (struct thread_info *thread) | |
186c406b | 1299 | { |
8980e177 | 1300 | delete_longjmp_breakpoint (thread->global_num); |
cfc31633 PA |
1301 | } |
1302 | ||
1303 | /* Implementation of the 'async_reply_reason' FSM method for the until | |
1304 | (with no arg) command. */ | |
1305 | ||
46e3ed7f TT |
1306 | enum async_reply_reason |
1307 | until_next_fsm::do_async_reply_reason () | |
cfc31633 PA |
1308 | { |
1309 | return EXEC_ASYNC_END_STEPPING_RANGE; | |
186c406b TT |
1310 | } |
1311 | ||
c906108c SS |
1312 | /* Proceed until we reach a different source line with pc greater than |
1313 | our current one or exit the function. We skip calls in both cases. | |
1314 | ||
1315 | Note that eventually this command should probably be changed so | |
1316 | that only source lines are printed out when we hit the breakpoint | |
1317 | we set. This may involve changes to wait_for_inferior and the | |
1318 | proceed status code. */ | |
1319 | ||
c906108c | 1320 | static void |
fba45db2 | 1321 | until_next_command (int from_tty) |
c906108c SS |
1322 | { |
1323 | struct frame_info *frame; | |
1324 | CORE_ADDR pc; | |
1325 | struct symbol *func; | |
1326 | struct symtab_and_line sal; | |
4e1c45ea | 1327 | struct thread_info *tp = inferior_thread (); |
5d5658a1 | 1328 | int thread = tp->global_num; |
cfc31633 | 1329 | struct until_next_fsm *sm; |
c5aa993b | 1330 | |
70509625 | 1331 | clear_proceed_status (0); |
29734269 | 1332 | set_step_frame (tp); |
c906108c SS |
1333 | |
1334 | frame = get_current_frame (); | |
1335 | ||
1336 | /* Step until either exited from this function or greater | |
1337 | than the current line (if in symbolic section) or pc (if | |
1777feb0 | 1338 | not). */ |
c906108c | 1339 | |
1c7819ef | 1340 | pc = get_frame_pc (frame); |
c906108c | 1341 | func = find_pc_function (pc); |
c5aa993b | 1342 | |
c906108c SS |
1343 | if (!func) |
1344 | { | |
7cbd4a93 | 1345 | struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc); |
c5aa993b | 1346 | |
7cbd4a93 | 1347 | if (msymbol.minsym == NULL) |
8a3fe4f8 | 1348 | error (_("Execution is not within a known function.")); |
c5aa993b | 1349 | |
4aeddc50 | 1350 | tp->control.step_range_start = msymbol.value_address (); |
7e09a223 YQ |
1351 | /* The upper-bound of step_range is exclusive. In order to make PC |
1352 | within the range, set the step_range_end with PC + 1. */ | |
1353 | tp->control.step_range_end = pc + 1; | |
c906108c SS |
1354 | } |
1355 | else | |
1356 | { | |
1357 | sal = find_pc_line (pc, 0); | |
c5aa993b | 1358 | |
6395b628 | 1359 | tp->control.step_range_start = func->value_block ()->entry_pc (); |
16c381f0 | 1360 | tp->control.step_range_end = sal.end; |
9ab50efc BL |
1361 | |
1362 | /* By setting the step_range_end based on the current pc, we are | |
1363 | assuming that the last line table entry for any given source line | |
1364 | will have is_stmt set to true. This is not necessarily the case, | |
1365 | there may be additional entries for the same source line with | |
1366 | is_stmt set false. Consider the following code: | |
1367 | ||
1368 | for (int i = 0; i < 10; i++) | |
1369 | loop_body (); | |
1370 | ||
1371 | Clang-13, will generate multiple line table entries at the end of | |
1372 | the loop all associated with the 'for' line. The first of these | |
1373 | entries is marked is_stmt true, but the other entries are is_stmt | |
1374 | false. | |
1375 | ||
1376 | If we only use the values in SAL, then our stepping range may not | |
1377 | extend to the end of the loop. The until command will reach the | |
1378 | end of the range, find a non is_stmt instruction, and step to the | |
1379 | next is_stmt instruction. This stopping point, however, will be | |
1380 | inside the loop, which is not what we wanted. | |
1381 | ||
1382 | Instead, we now check any subsequent line table entries to see if | |
1383 | they are for the same line. If they are, and they are marked | |
1384 | is_stmt false, then we extend the end of our stepping range. | |
1385 | ||
1386 | When we finish this process the end of the stepping range will | |
1387 | point either to a line with a different line number, or, will | |
1388 | point at an address for the same line number that is marked as a | |
1389 | statement. */ | |
1390 | ||
1391 | struct symtab_and_line final_sal | |
1392 | = find_pc_line (tp->control.step_range_end, 0); | |
1393 | ||
1394 | while (final_sal.line == sal.line && final_sal.symtab == sal.symtab | |
1395 | && !final_sal.is_stmt) | |
1396 | { | |
1397 | tp->control.step_range_end = final_sal.end; | |
1398 | final_sal = find_pc_line (final_sal.end, 0); | |
1399 | } | |
c906108c | 1400 | } |
c1e36e3e | 1401 | tp->control.may_range_step = 1; |
c5aa993b | 1402 | |
16c381f0 | 1403 | tp->control.step_over_calls = STEP_OVER_ALL; |
c906108c | 1404 | |
186c406b | 1405 | set_longjmp_breakpoint (tp, get_frame_id (frame)); |
5419bdae | 1406 | delete_longjmp_breakpoint_cleanup lj_deleter (thread); |
186c406b | 1407 | |
46e3ed7f | 1408 | sm = new until_next_fsm (command_interp (), tp->global_num); |
573269a8 | 1409 | tp->set_thread_fsm (std::unique_ptr<thread_fsm> (sm)); |
5419bdae | 1410 | lj_deleter.release (); |
fa4cd53f | 1411 | |
cfc31633 | 1412 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
c906108c SS |
1413 | } |
1414 | ||
c5aa993b | 1415 | static void |
0b39b52e | 1416 | until_command (const char *arg, int from_tty) |
c906108c | 1417 | { |
6c4486e6 | 1418 | int async_exec; |
43ff13b4 | 1419 | |
4247603b | 1420 | ERROR_NO_INFERIOR; |
573cda03 | 1421 | ensure_not_tfind_mode (); |
4247603b PA |
1422 | ensure_valid_thread (); |
1423 | ensure_not_running (); | |
573cda03 | 1424 | |
1777feb0 | 1425 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
1426 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); |
1427 | arg = stripped.get (); | |
43ff13b4 | 1428 | |
328d42d8 | 1429 | prepare_execution_command (current_inferior ()->top_target (), async_exec); |
43ff13b4 | 1430 | |
c906108c | 1431 | if (arg) |
ae66c1fc | 1432 | until_break_command (arg, from_tty, 0); |
c906108c SS |
1433 | else |
1434 | until_next_command (from_tty); | |
1435 | } | |
ae66c1fc EZ |
1436 | |
1437 | static void | |
0b39b52e | 1438 | advance_command (const char *arg, int from_tty) |
ae66c1fc | 1439 | { |
6c4486e6 | 1440 | int async_exec; |
ae66c1fc | 1441 | |
4247603b | 1442 | ERROR_NO_INFERIOR; |
573cda03 | 1443 | ensure_not_tfind_mode (); |
4247603b PA |
1444 | ensure_valid_thread (); |
1445 | ensure_not_running (); | |
573cda03 | 1446 | |
ae66c1fc | 1447 | if (arg == NULL) |
e2e0b3e5 | 1448 | error_no_arg (_("a location")); |
ae66c1fc EZ |
1449 | |
1450 | /* Find out whether we must run in the background. */ | |
6be9a197 TT |
1451 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); |
1452 | arg = stripped.get (); | |
ae66c1fc | 1453 | |
328d42d8 | 1454 | prepare_execution_command (current_inferior ()->top_target (), async_exec); |
ae66c1fc EZ |
1455 | |
1456 | until_break_command (arg, from_tty, 1); | |
1457 | } | |
c906108c | 1458 | \f |
e6b36367 | 1459 | /* See inferior.h. */ |
f941662f | 1460 | |
cc72b2a2 | 1461 | struct value * |
e6b36367 | 1462 | get_return_value (struct symbol *func_symbol, struct value *function) |
11cf8741 | 1463 | { |
215c69dc YQ |
1464 | regcache *stop_regs = get_current_regcache (); |
1465 | struct gdbarch *gdbarch = stop_regs->arch (); | |
44e5158b AC |
1466 | struct value *value; |
1467 | ||
e6b36367 LS |
1468 | struct type *value_type |
1469 | = check_typedef (TYPE_TARGET_TYPE (func_symbol->type ())); | |
78134374 | 1470 | gdb_assert (value_type->code () != TYPE_CODE_VOID); |
11cf8741 | 1471 | |
0b35f123 LS |
1472 | if (is_nocall_function (check_typedef (::value_type (function)))) |
1473 | { | |
1474 | warning (_("Function '%s' does not follow the target calling " | |
1475 | "convention, cannot determine its returned value."), | |
1476 | func_symbol->print_name ()); | |
1477 | ||
1478 | return nullptr; | |
1479 | } | |
1480 | ||
92ad9cd9 AC |
1481 | /* FIXME: 2003-09-27: When returning from a nested inferior function |
1482 | call, it's possible (with no help from the architecture vector) | |
1483 | to locate and return/print a "struct return" value. This is just | |
7a9dd1b2 | 1484 | a more complicated case of what is already being done in the |
92ad9cd9 AC |
1485 | inferior function call code. In fact, when inferior function |
1486 | calls are made async, this will likely be made the norm. */ | |
31db7b6c | 1487 | |
6a3a010b | 1488 | switch (gdbarch_return_value (gdbarch, function, value_type, |
24b21115 | 1489 | NULL, NULL, NULL)) |
44e5158b | 1490 | { |
750eb019 AC |
1491 | case RETURN_VALUE_REGISTER_CONVENTION: |
1492 | case RETURN_VALUE_ABI_RETURNS_ADDRESS: | |
181fc57c | 1493 | case RETURN_VALUE_ABI_PRESERVES_ADDRESS: |
44e5158b | 1494 | value = allocate_value (value_type); |
215c69dc | 1495 | gdbarch_return_value (gdbarch, function, value_type, stop_regs, |
50888e42 | 1496 | value_contents_raw (value).data (), NULL); |
750eb019 AC |
1497 | break; |
1498 | case RETURN_VALUE_STRUCT_CONVENTION: | |
1499 | value = NULL; | |
1500 | break; | |
1501 | default: | |
e2e0b3e5 | 1502 | internal_error (__FILE__, __LINE__, _("bad switch")); |
44e5158b | 1503 | } |
bb472c1e | 1504 | |
cc72b2a2 KP |
1505 | return value; |
1506 | } | |
1507 | ||
243a9253 PA |
1508 | /* The captured function return value/type and its position in the |
1509 | value history. */ | |
cc72b2a2 | 1510 | |
243a9253 | 1511 | struct return_value_info |
cc72b2a2 | 1512 | { |
243a9253 PA |
1513 | /* The captured return value. May be NULL if we weren't able to |
1514 | retrieve it. See get_return_value. */ | |
1515 | struct value *value; | |
1516 | ||
1517 | /* The return type. In some cases, we'll not be able extract the | |
1518 | return value, but we always know the type. */ | |
1519 | struct type *type; | |
1520 | ||
1521 | /* If we captured a value, this is the value history index. */ | |
1522 | int value_history_index; | |
1523 | }; | |
1524 | ||
1525 | /* Helper for print_return_value. */ | |
cc72b2a2 | 1526 | |
243a9253 PA |
1527 | static void |
1528 | print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv) | |
1529 | { | |
1530 | if (rv->value != NULL) | |
31db7b6c MK |
1531 | { |
1532 | /* Print it. */ | |
112e8700 SM |
1533 | uiout->text ("Value returned is "); |
1534 | uiout->field_fmt ("gdb-result-var", "$%d", | |
d7e74731 | 1535 | rv->value_history_index); |
112e8700 | 1536 | uiout->text (" = "); |
d7e74731 | 1537 | |
5ffa6ca3 | 1538 | if (finish_print) |
000439d5 | 1539 | { |
5ffa6ca3 TT |
1540 | struct value_print_options opts; |
1541 | get_user_print_options (&opts); | |
1542 | ||
000439d5 TT |
1543 | string_file stb; |
1544 | value_print (rv->value, &stb, &opts); | |
1545 | uiout->field_stream ("return-value", stb); | |
1546 | } | |
1547 | else | |
7f6aba03 TT |
1548 | uiout->field_string ("return-value", _("<not displayed>"), |
1549 | metadata_style.style ()); | |
112e8700 | 1550 | uiout->text ("\n"); |
31db7b6c MK |
1551 | } |
1552 | else | |
1553 | { | |
2f408ecb | 1554 | std::string type_name = type_to_string (rv->type); |
112e8700 | 1555 | uiout->text ("Value returned has type: "); |
8dd8c8d4 | 1556 | uiout->field_string ("return-type", type_name); |
112e8700 SM |
1557 | uiout->text ("."); |
1558 | uiout->text (" Cannot determine contents\n"); | |
31db7b6c | 1559 | } |
11cf8741 JM |
1560 | } |
1561 | ||
243a9253 PA |
1562 | /* Print the result of a function at the end of a 'finish' command. |
1563 | RV points at an object representing the captured return value/type | |
1564 | and its position in the value history. */ | |
1565 | ||
1566 | void | |
1567 | print_return_value (struct ui_out *uiout, struct return_value_info *rv) | |
1568 | { | |
f097f5ad | 1569 | if (rv->type == NULL |
78134374 | 1570 | || check_typedef (rv->type)->code () == TYPE_CODE_VOID) |
243a9253 PA |
1571 | return; |
1572 | ||
a70b8144 | 1573 | try |
243a9253 PA |
1574 | { |
1575 | /* print_return_value_1 can throw an exception in some | |
1576 | circumstances. We need to catch this so that we still | |
1577 | delete the breakpoint. */ | |
1578 | print_return_value_1 (uiout, rv); | |
1579 | } | |
230d2906 | 1580 | catch (const gdb_exception &ex) |
243a9253 PA |
1581 | { |
1582 | exception_print (gdb_stdout, ex); | |
1583 | } | |
243a9253 PA |
1584 | } |
1585 | ||
1586 | /* Data for the FSM that manages the finish command. */ | |
f941662f | 1587 | |
46e3ed7f | 1588 | struct finish_command_fsm : public thread_fsm |
43ff13b4 | 1589 | { |
243a9253 PA |
1590 | /* The momentary breakpoint set at the function's return address in |
1591 | the caller. */ | |
46e3ed7f | 1592 | breakpoint_up breakpoint; |
243a9253 PA |
1593 | |
1594 | /* The function that we're stepping out of. */ | |
46e3ed7f | 1595 | struct symbol *function = nullptr; |
8a6c4031 | 1596 | |
243a9253 PA |
1597 | /* If the FSM finishes successfully, this stores the function's |
1598 | return value. */ | |
46e3ed7f | 1599 | struct return_value_info return_value_info {}; |
c5aa993b | 1600 | |
46e3ed7f TT |
1601 | explicit finish_command_fsm (struct interp *cmd_interp) |
1602 | : thread_fsm (cmd_interp) | |
1603 | { | |
1604 | } | |
243a9253 | 1605 | |
46e3ed7f TT |
1606 | bool should_stop (struct thread_info *thread) override; |
1607 | void clean_up (struct thread_info *thread) override; | |
1608 | struct return_value_info *return_value () override; | |
1609 | enum async_reply_reason do_async_reply_reason () override; | |
1610 | }; | |
347bddb7 | 1611 | |
243a9253 PA |
1612 | /* Implementation of the 'should_stop' FSM method for the finish |
1613 | commands. Detects whether the thread stepped out of the function | |
1614 | successfully, and if so, captures the function's return value and | |
1615 | marks the FSM finished. */ | |
1616 | ||
46e3ed7f TT |
1617 | bool |
1618 | finish_command_fsm::should_stop (struct thread_info *tp) | |
243a9253 | 1619 | { |
46e3ed7f | 1620 | struct return_value_info *rv = &return_value_info; |
243a9253 | 1621 | |
46e3ed7f | 1622 | if (function != NULL |
243a9253 | 1623 | && bpstat_find_breakpoint (tp->control.stop_bpstat, |
46e3ed7f | 1624 | breakpoint.get ()) != NULL) |
43ff13b4 | 1625 | { |
243a9253 | 1626 | /* We're done. */ |
46e3ed7f | 1627 | set_finished (); |
bfec99b2 | 1628 | |
5f9c5a63 | 1629 | rv->type = TYPE_TARGET_TYPE (function->type ()); |
243a9253 PA |
1630 | if (rv->type == NULL) |
1631 | internal_error (__FILE__, __LINE__, | |
1632 | _("finish_command: function has no target type")); | |
f5871ec0 | 1633 | |
78134374 | 1634 | if (check_typedef (rv->type)->code () != TYPE_CODE_VOID) |
40c549d6 | 1635 | { |
243a9253 PA |
1636 | struct value *func; |
1637 | ||
46e3ed7f | 1638 | func = read_var_value (function, NULL, get_current_frame ()); |
e6b36367 | 1639 | rv->value = get_return_value (function, func); |
aca20ec4 KB |
1640 | if (rv->value != NULL) |
1641 | rv->value_history_index = record_latest_value (rv->value); | |
243a9253 PA |
1642 | } |
1643 | } | |
1644 | else if (tp->control.stop_step) | |
1645 | { | |
1646 | /* Finishing from an inline frame, or reverse finishing. In | |
1647 | either case, there's no way to retrieve the return value. */ | |
46e3ed7f | 1648 | set_finished (); |
243a9253 | 1649 | } |
fa4cd53f | 1650 | |
46e3ed7f | 1651 | return true; |
243a9253 | 1652 | } |
40c549d6 | 1653 | |
243a9253 PA |
1654 | /* Implementation of the 'clean_up' FSM method for the finish |
1655 | commands. */ | |
fa4cd53f | 1656 | |
46e3ed7f TT |
1657 | void |
1658 | finish_command_fsm::clean_up (struct thread_info *thread) | |
243a9253 | 1659 | { |
46e3ed7f | 1660 | breakpoint.reset (); |
8980e177 | 1661 | delete_longjmp_breakpoint (thread->global_num); |
243a9253 | 1662 | } |
f941662f | 1663 | |
243a9253 PA |
1664 | /* Implementation of the 'return_value' FSM method for the finish |
1665 | commands. */ | |
1666 | ||
46e3ed7f TT |
1667 | struct return_value_info * |
1668 | finish_command_fsm::return_value () | |
243a9253 | 1669 | { |
46e3ed7f | 1670 | return &return_value_info; |
43ff13b4 JM |
1671 | } |
1672 | ||
243a9253 PA |
1673 | /* Implementation of the 'async_reply_reason' FSM method for the |
1674 | finish commands. */ | |
1675 | ||
46e3ed7f TT |
1676 | enum async_reply_reason |
1677 | finish_command_fsm::do_async_reply_reason () | |
604ead4a | 1678 | { |
243a9253 PA |
1679 | if (execution_direction == EXEC_REVERSE) |
1680 | return EXEC_ASYNC_END_STEPPING_RANGE; | |
1681 | else | |
1682 | return EXEC_ASYNC_FUNCTION_FINISHED; | |
604ead4a PA |
1683 | } |
1684 | ||
b2175913 MS |
1685 | /* finish_backward -- helper function for finish_command. */ |
1686 | ||
1687 | static void | |
243a9253 | 1688 | finish_backward (struct finish_command_fsm *sm) |
b2175913 MS |
1689 | { |
1690 | struct symtab_and_line sal; | |
1691 | struct thread_info *tp = inferior_thread (); | |
1c7819ef | 1692 | CORE_ADDR pc; |
b2175913 | 1693 | CORE_ADDR func_addr; |
b2175913 | 1694 | |
1c7819ef PA |
1695 | pc = get_frame_pc (get_current_frame ()); |
1696 | ||
1697 | if (find_pc_partial_function (pc, NULL, &func_addr, NULL) == 0) | |
1f267ae3 | 1698 | error (_("Cannot find bounds of current function")); |
b2175913 MS |
1699 | |
1700 | sal = find_pc_line (func_addr, 0); | |
1701 | ||
9da8c2a0 | 1702 | tp->control.proceed_to_finish = 1; |
b2175913 MS |
1703 | /* Special case: if we're sitting at the function entry point, |
1704 | then all we need to do is take a reverse singlestep. We | |
1705 | don't need to set a breakpoint, and indeed it would do us | |
1706 | no good to do so. | |
1707 | ||
1708 | Note that this can only happen at frame #0, since there's | |
1709 | no way that a function up the stack can have a return address | |
1710 | that's equal to its entry point. */ | |
1711 | ||
1c7819ef | 1712 | if (sal.pc != pc) |
b2175913 | 1713 | { |
a6d9a66e UW |
1714 | struct frame_info *frame = get_selected_frame (NULL); |
1715 | struct gdbarch *gdbarch = get_frame_arch (frame); | |
9da8c2a0 PA |
1716 | |
1717 | /* Set a step-resume at the function's entry point. Once that's | |
1718 | hit, we'll do one more step backwards. */ | |
51abb421 | 1719 | symtab_and_line sr_sal; |
9da8c2a0 PA |
1720 | sr_sal.pc = sal.pc; |
1721 | sr_sal.pspace = get_frame_program_space (frame); | |
1722 | insert_step_resume_breakpoint_at_sal (gdbarch, | |
1723 | sr_sal, null_frame_id); | |
a6d9a66e | 1724 | |
64ce06e4 | 1725 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
b2175913 MS |
1726 | } |
1727 | else | |
b2175913 | 1728 | { |
9da8c2a0 PA |
1729 | /* We're almost there -- we just need to back up by one more |
1730 | single-step. */ | |
16c381f0 | 1731 | tp->control.step_range_start = tp->control.step_range_end = 1; |
64ce06e4 | 1732 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
b2175913 | 1733 | } |
b2175913 MS |
1734 | } |
1735 | ||
243a9253 PA |
1736 | /* finish_forward -- helper function for finish_command. FRAME is the |
1737 | frame that called the function we're about to step out of. */ | |
b2175913 MS |
1738 | |
1739 | static void | |
243a9253 | 1740 | finish_forward (struct finish_command_fsm *sm, struct frame_info *frame) |
b2175913 | 1741 | { |
def166f6 | 1742 | struct frame_id frame_id = get_frame_id (frame); |
a6d9a66e | 1743 | struct gdbarch *gdbarch = get_frame_arch (frame); |
b2175913 MS |
1744 | struct symtab_and_line sal; |
1745 | struct thread_info *tp = inferior_thread (); | |
b2175913 MS |
1746 | |
1747 | sal = find_pc_line (get_frame_pc (frame), 0); | |
1748 | sal.pc = get_frame_pc (frame); | |
1749 | ||
243a9253 PA |
1750 | sm->breakpoint = set_momentary_breakpoint (gdbarch, sal, |
1751 | get_stack_frame_id (frame), | |
46e3ed7f | 1752 | bp_finish); |
b2175913 | 1753 | |
c70a6932 JK |
1754 | /* set_momentary_breakpoint invalidates FRAME. */ |
1755 | frame = NULL; | |
1756 | ||
def166f6 | 1757 | set_longjmp_breakpoint (tp, frame_id); |
186c406b | 1758 | |
46c03469 | 1759 | /* We want to print return value, please... */ |
16c381f0 | 1760 | tp->control.proceed_to_finish = 1; |
b2175913 | 1761 | |
243a9253 | 1762 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
b2175913 MS |
1763 | } |
1764 | ||
e3b5daf9 MM |
1765 | /* Skip frames for "finish". */ |
1766 | ||
1767 | static struct frame_info * | |
1768 | skip_finish_frames (struct frame_info *frame) | |
1769 | { | |
1770 | struct frame_info *start; | |
1771 | ||
1772 | do | |
1773 | { | |
1774 | start = frame; | |
1775 | ||
1776 | frame = skip_tailcall_frames (frame); | |
1777 | if (frame == NULL) | |
1778 | break; | |
1779 | ||
1780 | frame = skip_unwritable_frames (frame); | |
1781 | if (frame == NULL) | |
1782 | break; | |
1783 | } | |
1784 | while (start != frame); | |
1785 | ||
1786 | return frame; | |
1787 | } | |
1788 | ||
f941662f MK |
1789 | /* "finish": Set a temporary breakpoint at the place the selected |
1790 | frame will return to, then continue. */ | |
c906108c SS |
1791 | |
1792 | static void | |
0b39b52e | 1793 | finish_command (const char *arg, int from_tty) |
c906108c | 1794 | { |
52f0bd74 | 1795 | struct frame_info *frame; |
6c4486e6 | 1796 | int async_exec; |
243a9253 PA |
1797 | struct finish_command_fsm *sm; |
1798 | struct thread_info *tp; | |
43ff13b4 | 1799 | |
4247603b | 1800 | ERROR_NO_INFERIOR; |
573cda03 | 1801 | ensure_not_tfind_mode (); |
4247603b PA |
1802 | ensure_valid_thread (); |
1803 | ensure_not_running (); | |
573cda03 | 1804 | |
f941662f | 1805 | /* Find out whether we must run in the background. */ |
6be9a197 TT |
1806 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (arg, &async_exec); |
1807 | arg = stripped.get (); | |
43ff13b4 | 1808 | |
328d42d8 | 1809 | prepare_execution_command (current_inferior ()->top_target (), async_exec); |
c906108c SS |
1810 | |
1811 | if (arg) | |
8a3fe4f8 | 1812 | error (_("The \"finish\" command does not take any arguments.")); |
c906108c | 1813 | |
206415a3 | 1814 | frame = get_prev_frame (get_selected_frame (_("No selected frame."))); |
c906108c | 1815 | if (frame == 0) |
8a3fe4f8 | 1816 | error (_("\"finish\" not meaningful in the outermost frame.")); |
c906108c | 1817 | |
70509625 | 1818 | clear_proceed_status (0); |
c906108c | 1819 | |
243a9253 PA |
1820 | tp = inferior_thread (); |
1821 | ||
46e3ed7f | 1822 | sm = new finish_command_fsm (command_interp ()); |
243a9253 | 1823 | |
573269a8 | 1824 | tp->set_thread_fsm (std::unique_ptr<thread_fsm> (sm)); |
243a9253 | 1825 | |
edb3359d | 1826 | /* Finishing from an inline frame is completely different. We don't |
243a9253 | 1827 | try to show the "return value" - no way to locate it. */ |
edb3359d DJ |
1828 | if (get_frame_type (get_selected_frame (_("No selected frame."))) |
1829 | == INLINE_FRAME) | |
1830 | { | |
1831 | /* Claim we are stepping in the calling frame. An empty step | |
1832 | range means that we will stop once we aren't in a function | |
1833 | called by that frame. We don't use the magic "1" value for | |
1834 | step_range_end, because then infrun will think this is nexti, | |
1835 | and not step over the rest of this inlined function call. */ | |
29734269 | 1836 | set_step_info (tp, frame, {}); |
16c381f0 JK |
1837 | tp->control.step_range_start = get_frame_pc (frame); |
1838 | tp->control.step_range_end = tp->control.step_range_start; | |
1839 | tp->control.step_over_calls = STEP_OVER_ALL; | |
edb3359d DJ |
1840 | |
1841 | /* Print info on the selected frame, including level number but not | |
1842 | source. */ | |
1843 | if (from_tty) | |
1844 | { | |
6cb06a8c | 1845 | gdb_printf (_("Run till exit from ")); |
08d72866 | 1846 | print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0); |
edb3359d DJ |
1847 | } |
1848 | ||
64ce06e4 | 1849 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
edb3359d DJ |
1850 | return; |
1851 | } | |
1852 | ||
c906108c SS |
1853 | /* Find the function we will return from. */ |
1854 | ||
243a9253 | 1855 | sm->function = find_pc_function (get_frame_pc (get_selected_frame (NULL))); |
c906108c | 1856 | |
f941662f MK |
1857 | /* Print info on the selected frame, including level number but not |
1858 | source. */ | |
c906108c SS |
1859 | if (from_tty) |
1860 | { | |
b2175913 | 1861 | if (execution_direction == EXEC_REVERSE) |
6cb06a8c | 1862 | gdb_printf (_("Run back to call of ")); |
b2175913 | 1863 | else |
743649fd | 1864 | { |
5f9c5a63 | 1865 | if (sm->function != NULL && TYPE_NO_RETURN (sm->function->type ()) |
743649fd MW |
1866 | && !query (_("warning: Function %s does not return normally.\n" |
1867 | "Try to finish anyway? "), | |
987012b8 | 1868 | sm->function->print_name ())) |
743649fd | 1869 | error (_("Not confirmed.")); |
6cb06a8c | 1870 | gdb_printf (_("Run till exit from ")); |
743649fd | 1871 | } |
b2175913 | 1872 | |
08d72866 | 1873 | print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0); |
c906108c SS |
1874 | } |
1875 | ||
b2175913 | 1876 | if (execution_direction == EXEC_REVERSE) |
243a9253 | 1877 | finish_backward (sm); |
b2175913 | 1878 | else |
33b4777c | 1879 | { |
e3b5daf9 | 1880 | frame = skip_finish_frames (frame); |
7eb89530 | 1881 | |
33b4777c MM |
1882 | if (frame == NULL) |
1883 | error (_("Cannot find the caller frame.")); | |
1884 | ||
1885 | finish_forward (sm, frame); | |
1886 | } | |
c906108c SS |
1887 | } |
1888 | \f | |
f941662f | 1889 | |
c906108c | 1890 | static void |
1d12d88f | 1891 | info_program_command (const char *args, int from_tty) |
c906108c | 1892 | { |
313f3b21 | 1893 | bpstat *bs; |
347bddb7 | 1894 | int num, stat; |
347bddb7 | 1895 | ptid_t ptid; |
5b6d1e4f | 1896 | process_stratum_target *proc_target; |
c5aa993b | 1897 | |
55f6301a | 1898 | if (!target_has_execution ()) |
c906108c | 1899 | { |
6cb06a8c | 1900 | gdb_printf (_("The program being debugged is not being run.\n")); |
c906108c SS |
1901 | return; |
1902 | } | |
1903 | ||
347bddb7 | 1904 | if (non_stop) |
5b6d1e4f PA |
1905 | { |
1906 | ptid = inferior_ptid; | |
1907 | proc_target = current_inferior ()->process_target (); | |
1908 | } | |
347bddb7 | 1909 | else |
5b6d1e4f | 1910 | get_last_target_status (&proc_target, &ptid, nullptr); |
347bddb7 | 1911 | |
9e7f3bbb | 1912 | if (ptid == null_ptid || ptid == minus_one_ptid) |
00431a78 PA |
1913 | error (_("No selected thread.")); |
1914 | ||
5b6d1e4f | 1915 | thread_info *tp = find_thread_ptid (proc_target, ptid); |
00431a78 PA |
1916 | |
1917 | if (tp->state == THREAD_EXITED) | |
347bddb7 | 1918 | error (_("Invalid selected thread.")); |
00431a78 | 1919 | else if (tp->state == THREAD_RUNNING) |
347bddb7 PA |
1920 | error (_("Selected thread is running.")); |
1921 | ||
16c381f0 | 1922 | bs = tp->control.stop_bpstat; |
347bddb7 PA |
1923 | stat = bpstat_num (&bs, &num); |
1924 | ||
c906108c | 1925 | target_files_info (); |
6cb06a8c TT |
1926 | gdb_printf (_("Program stopped at %s.\n"), |
1927 | paddress (target_gdbarch (), tp->stop_pc ())); | |
16c381f0 | 1928 | if (tp->control.stop_step) |
6cb06a8c | 1929 | gdb_printf (_("It stopped after being stepped.\n")); |
8671a17b | 1930 | else if (stat != 0) |
c906108c SS |
1931 | { |
1932 | /* There may be several breakpoints in the same place, so this | |
dda83cd7 | 1933 | isn't as strange as it seems. */ |
8671a17b | 1934 | while (stat != 0) |
c906108c | 1935 | { |
8671a17b | 1936 | if (stat < 0) |
c906108c | 1937 | { |
6cb06a8c TT |
1938 | gdb_printf (_("It stopped at a breakpoint " |
1939 | "that has since been deleted.\n")); | |
c906108c SS |
1940 | } |
1941 | else | |
6cb06a8c | 1942 | gdb_printf (_("It stopped at breakpoint %d.\n"), num); |
8671a17b | 1943 | stat = bpstat_num (&bs, &num); |
c906108c SS |
1944 | } |
1945 | } | |
1edb66d8 | 1946 | else if (tp->stop_signal () != GDB_SIGNAL_0) |
c906108c | 1947 | { |
6cb06a8c TT |
1948 | gdb_printf (_("It stopped with signal %s, %s.\n"), |
1949 | gdb_signal_to_name (tp->stop_signal ()), | |
1950 | gdb_signal_to_string (tp->stop_signal ())); | |
c906108c SS |
1951 | } |
1952 | ||
0d41ba00 | 1953 | if (from_tty) |
c906108c | 1954 | { |
6cb06a8c TT |
1955 | gdb_printf (_("Type \"info stack\" or \"info " |
1956 | "registers\" for more information.\n")); | |
c906108c SS |
1957 | } |
1958 | } | |
1959 | \f | |
1960 | static void | |
69f476a3 | 1961 | environment_info (const char *var, int from_tty) |
c906108c SS |
1962 | { |
1963 | if (var) | |
1964 | { | |
9a6c7d9c | 1965 | const char *val = current_inferior ()->environment.get (var); |
abbb1732 | 1966 | |
c906108c SS |
1967 | if (val) |
1968 | { | |
0426ad51 TT |
1969 | gdb_puts (var); |
1970 | gdb_puts (" = "); | |
1971 | gdb_puts (val); | |
1972 | gdb_puts ("\n"); | |
c906108c SS |
1973 | } |
1974 | else | |
1975 | { | |
0426ad51 TT |
1976 | gdb_puts ("Environment variable \""); |
1977 | gdb_puts (var); | |
1978 | gdb_puts ("\" not defined.\n"); | |
c906108c SS |
1979 | } |
1980 | } | |
1981 | else | |
1982 | { | |
9a6c7d9c | 1983 | char **envp = current_inferior ()->environment.envp (); |
abbb1732 | 1984 | |
9a6c7d9c | 1985 | for (int idx = 0; envp[idx] != NULL; ++idx) |
c906108c | 1986 | { |
0426ad51 TT |
1987 | gdb_puts (envp[idx]); |
1988 | gdb_puts ("\n"); | |
c906108c SS |
1989 | } |
1990 | } | |
1991 | } | |
1992 | ||
1993 | static void | |
69f476a3 | 1994 | set_environment_command (const char *arg, int from_tty) |
c906108c | 1995 | { |
69f476a3 | 1996 | const char *p, *val; |
c906108c SS |
1997 | int nullset = 0; |
1998 | ||
1999 | if (arg == 0) | |
e2e0b3e5 | 2000 | error_no_arg (_("environment variable and value")); |
c906108c | 2001 | |
85102364 | 2002 | /* Find separation between variable name and value. */ |
c906108c SS |
2003 | p = (char *) strchr (arg, '='); |
2004 | val = (char *) strchr (arg, ' '); | |
2005 | ||
2006 | if (p != 0 && val != 0) | |
2007 | { | |
2008 | /* We have both a space and an equals. If the space is before the | |
dda83cd7 SM |
2009 | equals, walk forward over the spaces til we see a nonspace |
2010 | (possibly the equals). */ | |
c906108c SS |
2011 | if (p > val) |
2012 | while (*val == ' ') | |
2013 | val++; | |
2014 | ||
2015 | /* Now if the = is after the char following the spaces, | |
dda83cd7 | 2016 | take the char following the spaces. */ |
c906108c SS |
2017 | if (p > val) |
2018 | p = val - 1; | |
2019 | } | |
2020 | else if (val != 0 && p == 0) | |
2021 | p = val; | |
2022 | ||
2023 | if (p == arg) | |
e2e0b3e5 | 2024 | error_no_arg (_("environment variable to set")); |
c906108c SS |
2025 | |
2026 | if (p == 0 || p[1] == 0) | |
2027 | { | |
2028 | nullset = 1; | |
2029 | if (p == 0) | |
1777feb0 | 2030 | p = arg + strlen (arg); /* So that savestring below will work. */ |
c906108c SS |
2031 | } |
2032 | else | |
2033 | { | |
1777feb0 | 2034 | /* Not setting variable value to null. */ |
c906108c SS |
2035 | val = p + 1; |
2036 | while (*val == ' ' || *val == '\t') | |
2037 | val++; | |
2038 | } | |
2039 | ||
c5aa993b JM |
2040 | while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) |
2041 | p--; | |
c906108c | 2042 | |
69f476a3 | 2043 | std::string var (arg, p - arg); |
c906108c SS |
2044 | if (nullset) |
2045 | { | |
6cb06a8c TT |
2046 | gdb_printf (_("Setting environment variable " |
2047 | "\"%s\" to null value.\n"), | |
2048 | var.c_str ()); | |
69f476a3 | 2049 | current_inferior ()->environment.set (var.c_str (), ""); |
c906108c SS |
2050 | } |
2051 | else | |
69f476a3 | 2052 | current_inferior ()->environment.set (var.c_str (), val); |
c906108c SS |
2053 | } |
2054 | ||
2055 | static void | |
69f476a3 | 2056 | unset_environment_command (const char *var, int from_tty) |
c906108c SS |
2057 | { |
2058 | if (var == 0) | |
2059 | { | |
2060 | /* If there is no argument, delete all environment variables. | |
dda83cd7 | 2061 | Ask for confirmation if reading from the terminal. */ |
e2e0b3e5 | 2062 | if (!from_tty || query (_("Delete all environment variables? "))) |
206726fb | 2063 | current_inferior ()->environment.clear (); |
c906108c SS |
2064 | } |
2065 | else | |
9a6c7d9c | 2066 | current_inferior ()->environment.unset (var); |
c906108c SS |
2067 | } |
2068 | ||
1777feb0 | 2069 | /* Handle the execution path (PATH variable). */ |
c906108c SS |
2070 | |
2071 | static const char path_var_name[] = "PATH"; | |
2072 | ||
c906108c | 2073 | static void |
69f476a3 | 2074 | path_info (const char *args, int from_tty) |
c906108c | 2075 | { |
0426ad51 TT |
2076 | gdb_puts ("Executable and object file path: "); |
2077 | gdb_puts (current_inferior ()->environment.get (path_var_name)); | |
2078 | gdb_puts ("\n"); | |
c906108c SS |
2079 | } |
2080 | ||
2081 | /* Add zero or more directories to the front of the execution path. */ | |
2082 | ||
2083 | static void | |
0b39b52e | 2084 | path_command (const char *dirname, int from_tty) |
c906108c | 2085 | { |
a121b7c1 | 2086 | const char *env; |
abbb1732 | 2087 | |
c906108c | 2088 | dont_repeat (); |
9a6c7d9c | 2089 | env = current_inferior ()->environment.get (path_var_name); |
1777feb0 | 2090 | /* Can be null if path is not set. */ |
c906108c SS |
2091 | if (!env) |
2092 | env = ""; | |
e0700ba4 SM |
2093 | std::string exec_path = env; |
2094 | mod_path (dirname, exec_path); | |
2095 | current_inferior ()->environment.set (path_var_name, exec_path.c_str ()); | |
c906108c | 2096 | if (from_tty) |
cafb3438 | 2097 | path_info (NULL, from_tty); |
c906108c | 2098 | } |
c906108c | 2099 | \f |
c5aa993b | 2100 | |
e813d34a RK |
2101 | static void |
2102 | pad_to_column (string_file &stream, int col) | |
2103 | { | |
2104 | /* At least one space must be printed to separate columns. */ | |
2105 | stream.putc (' '); | |
2106 | const int size = stream.size (); | |
2107 | if (size < col) | |
2108 | stream.puts (n_spaces (col - size)); | |
2109 | } | |
2110 | ||
1292279a PA |
2111 | /* Print out the register NAME with value VAL, to FILE, in the default |
2112 | fashion. */ | |
2113 | ||
2114 | static void | |
2115 | default_print_one_register_info (struct ui_file *file, | |
2116 | const char *name, | |
2117 | struct value *val) | |
2118 | { | |
2119 | struct type *regtype = value_type (val); | |
f69d9aef | 2120 | int print_raw_format; |
e813d34a RK |
2121 | string_file format_stream; |
2122 | enum tab_stops | |
2123 | { | |
2124 | value_column_1 = 15, | |
2125 | /* Give enough room for "0x", 16 hex digits and two spaces in | |
dda83cd7 | 2126 | preceding column. */ |
e813d34a RK |
2127 | value_column_2 = value_column_1 + 2 + 16 + 2, |
2128 | }; | |
1292279a | 2129 | |
e813d34a RK |
2130 | format_stream.puts (name); |
2131 | pad_to_column (format_stream, value_column_1); | |
1292279a | 2132 | |
f69d9aef AB |
2133 | print_raw_format = (value_entirely_available (val) |
2134 | && !value_optimized_out (val)); | |
1292279a PA |
2135 | |
2136 | /* If virtual format is floating, print it that way, and in raw | |
2137 | hex. */ | |
78134374 SM |
2138 | if (regtype->code () == TYPE_CODE_FLT |
2139 | || regtype->code () == TYPE_CODE_DECFLOAT) | |
1292279a | 2140 | { |
1292279a | 2141 | struct value_print_options opts; |
50888e42 | 2142 | const gdb_byte *valaddr = value_contents_for_printing (val).data (); |
34877895 | 2143 | enum bfd_endian byte_order = type_byte_order (regtype); |
1292279a PA |
2144 | |
2145 | get_user_print_options (&opts); | |
2146 | opts.deref_ref = 1; | |
2147 | ||
3444c526 | 2148 | common_val_print (val, &format_stream, 0, &opts, current_language); |
1292279a | 2149 | |
f69d9aef AB |
2150 | if (print_raw_format) |
2151 | { | |
e813d34a RK |
2152 | pad_to_column (format_stream, value_column_2); |
2153 | format_stream.puts ("(raw "); | |
2154 | print_hex_chars (&format_stream, valaddr, TYPE_LENGTH (regtype), | |
2155 | byte_order, true); | |
2156 | format_stream.putc (')'); | |
f69d9aef | 2157 | } |
1292279a PA |
2158 | } |
2159 | else | |
2160 | { | |
2161 | struct value_print_options opts; | |
2162 | ||
2163 | /* Print the register in hex. */ | |
2164 | get_formatted_print_options (&opts, 'x'); | |
2165 | opts.deref_ref = 1; | |
3444c526 | 2166 | common_val_print (val, &format_stream, 0, &opts, current_language); |
1292279a PA |
2167 | /* If not a vector register, print it also according to its |
2168 | natural format. */ | |
bd63c870 | 2169 | if (print_raw_format && regtype->is_vector () == 0) |
1292279a | 2170 | { |
e813d34a | 2171 | pad_to_column (format_stream, value_column_2); |
1292279a PA |
2172 | get_user_print_options (&opts); |
2173 | opts.deref_ref = 1; | |
3444c526 | 2174 | common_val_print (val, &format_stream, 0, &opts, current_language); |
1292279a PA |
2175 | } |
2176 | } | |
2177 | ||
0426ad51 | 2178 | gdb_puts (format_stream.c_str (), file); |
6cb06a8c | 2179 | gdb_printf (file, "\n"); |
1292279a PA |
2180 | } |
2181 | ||
1777feb0 | 2182 | /* Print out the machine register regnum. If regnum is -1, print all |
0ab7a791 AC |
2183 | registers (print_all == 1) or all non-float and non-vector |
2184 | registers (print_all == 0). | |
c906108c SS |
2185 | |
2186 | For most machines, having all_registers_info() print the | |
0ab7a791 AC |
2187 | register(s) one per line is good enough. If a different format is |
2188 | required, (eg, for MIPS or Pyramid 90x, which both have lots of | |
2189 | regs), or there is an existing convention for showing all the | |
2190 | registers, define the architecture method PRINT_REGISTERS_INFO to | |
2191 | provide that format. */ | |
c906108c | 2192 | |
666e11c5 | 2193 | void |
0ab7a791 AC |
2194 | default_print_registers_info (struct gdbarch *gdbarch, |
2195 | struct ui_file *file, | |
2196 | struct frame_info *frame, | |
2197 | int regnum, int print_all) | |
c906108c | 2198 | { |
0ab7a791 | 2199 | int i; |
f6efe3f8 | 2200 | const int numregs = gdbarch_num_cooked_regs (gdbarch); |
0ab7a791 | 2201 | |
c906108c SS |
2202 | for (i = 0; i < numregs; i++) |
2203 | { | |
4782dc19 | 2204 | /* Decide between printing all regs, non-float / vector regs, or |
dda83cd7 | 2205 | specific reg. */ |
c5aa993b JM |
2206 | if (regnum == -1) |
2207 | { | |
f9418c0f | 2208 | if (print_all) |
4782dc19 | 2209 | { |
f9418c0f | 2210 | if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup)) |
4782dc19 | 2211 | continue; |
f9418c0f AC |
2212 | } |
2213 | else | |
2214 | { | |
2215 | if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup)) | |
4782dc19 AC |
2216 | continue; |
2217 | } | |
c5aa993b JM |
2218 | } |
2219 | else | |
2220 | { | |
2221 | if (i != regnum) | |
2222 | continue; | |
2223 | } | |
c906108c SS |
2224 | |
2225 | /* If the register name is empty, it is undefined for this | |
dda83cd7 | 2226 | processor, so don't display anything. */ |
a4bd449d UW |
2227 | if (gdbarch_register_name (gdbarch, i) == NULL |
2228 | || *(gdbarch_register_name (gdbarch, i)) == '\0') | |
c906108c SS |
2229 | continue; |
2230 | ||
1292279a PA |
2231 | default_print_one_register_info (file, |
2232 | gdbarch_register_name (gdbarch, i), | |
901461f8 | 2233 | value_of_register (i, frame)); |
c906108c SS |
2234 | } |
2235 | } | |
c906108c | 2236 | |
c906108c | 2237 | void |
1d12d88f | 2238 | registers_info (const char *addr_exp, int fpregs) |
c906108c | 2239 | { |
206415a3 | 2240 | struct frame_info *frame; |
a4bd449d | 2241 | struct gdbarch *gdbarch; |
c906108c | 2242 | |
9dccd06e | 2243 | if (!target_has_registers ()) |
8a3fe4f8 | 2244 | error (_("The program has no registers now.")); |
206415a3 | 2245 | frame = get_selected_frame (NULL); |
a4bd449d | 2246 | gdbarch = get_frame_arch (frame); |
c906108c SS |
2247 | |
2248 | if (!addr_exp) | |
2249 | { | |
a4bd449d | 2250 | gdbarch_print_registers_info (gdbarch, gdb_stdout, |
206415a3 | 2251 | frame, -1, fpregs); |
c906108c SS |
2252 | return; |
2253 | } | |
2254 | ||
f9418c0f | 2255 | while (*addr_exp != '\0') |
c5aa993b | 2256 | { |
1d12d88f | 2257 | const char *start; |
f9418c0f | 2258 | const char *end; |
c906108c | 2259 | |
529480d0 KS |
2260 | /* Skip leading white space. */ |
2261 | addr_exp = skip_spaces (addr_exp); | |
c906108c | 2262 | |
f9418c0f | 2263 | /* Discard any leading ``$''. Check that there is something |
dda83cd7 | 2264 | resembling a register following it. */ |
f9418c0f AC |
2265 | if (addr_exp[0] == '$') |
2266 | addr_exp++; | |
2267 | if (isspace ((*addr_exp)) || (*addr_exp) == '\0') | |
8a3fe4f8 | 2268 | error (_("Missing register name")); |
c906108c | 2269 | |
f9418c0f AC |
2270 | /* Find the start/end of this register name/num/group. */ |
2271 | start = addr_exp; | |
2272 | while ((*addr_exp) != '\0' && !isspace ((*addr_exp))) | |
2273 | addr_exp++; | |
2274 | end = addr_exp; | |
ad842144 | 2275 | |
f9418c0f AC |
2276 | /* Figure out what we've found and display it. */ |
2277 | ||
2278 | /* A register name? */ | |
2279 | { | |
029a67e4 | 2280 | int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start); |
abbb1732 | 2281 | |
f9418c0f AC |
2282 | if (regnum >= 0) |
2283 | { | |
ad842144 MR |
2284 | /* User registers lie completely outside of the range of |
2285 | normal registers. Catch them early so that the target | |
2286 | never sees them. */ | |
f6efe3f8 | 2287 | if (regnum >= gdbarch_num_cooked_regs (gdbarch)) |
ad842144 | 2288 | { |
1292279a PA |
2289 | struct value *regval = value_of_user_reg (regnum, frame); |
2290 | const char *regname = user_reg_map_regnum_to_name (gdbarch, | |
2291 | regnum); | |
2292 | ||
2293 | /* Print in the same fashion | |
2294 | gdbarch_print_registers_info's default | |
2295 | implementation prints. */ | |
2296 | default_print_one_register_info (gdb_stdout, | |
2297 | regname, | |
2298 | regval); | |
ad842144 MR |
2299 | } |
2300 | else | |
2301 | gdbarch_print_registers_info (gdbarch, gdb_stdout, | |
2302 | frame, regnum, fpregs); | |
f9418c0f AC |
2303 | continue; |
2304 | } | |
2305 | } | |
ad842144 | 2306 | |
f9418c0f AC |
2307 | /* A register group? */ |
2308 | { | |
1bca9b1e AB |
2309 | const struct reggroup *group = nullptr; |
2310 | for (const struct reggroup *g : gdbarch_reggroups (gdbarch)) | |
f9418c0f AC |
2311 | { |
2312 | /* Don't bother with a length check. Should the user | |
2313 | enter a short register group name, go with the first | |
2314 | group that matches. */ | |
af7ce09b | 2315 | if (strncmp (start, g->name (), end - start) == 0) |
1bca9b1e AB |
2316 | { |
2317 | group = g; | |
2318 | break; | |
2319 | } | |
f9418c0f | 2320 | } |
6c7d17ba | 2321 | if (group != NULL) |
f9418c0f AC |
2322 | { |
2323 | int regnum; | |
abbb1732 | 2324 | |
f57d151a | 2325 | for (regnum = 0; |
f6efe3f8 | 2326 | regnum < gdbarch_num_cooked_regs (gdbarch); |
f57d151a | 2327 | regnum++) |
f9418c0f | 2328 | { |
a4bd449d UW |
2329 | if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) |
2330 | gdbarch_print_registers_info (gdbarch, | |
206415a3 | 2331 | gdb_stdout, frame, |
f9418c0f AC |
2332 | regnum, fpregs); |
2333 | } | |
2334 | continue; | |
2335 | } | |
2336 | } | |
c906108c | 2337 | |
f9418c0f | 2338 | /* Nothing matched. */ |
8a3fe4f8 | 2339 | error (_("Invalid register `%.*s'"), (int) (end - start), start); |
c5aa993b | 2340 | } |
c906108c SS |
2341 | } |
2342 | ||
1dd5fedc | 2343 | static void |
1d12d88f | 2344 | info_all_registers_command (const char *addr_exp, int from_tty) |
c906108c SS |
2345 | { |
2346 | registers_info (addr_exp, 1); | |
2347 | } | |
2348 | ||
a58dd373 | 2349 | static void |
1d12d88f | 2350 | info_registers_command (const char *addr_exp, int from_tty) |
c906108c SS |
2351 | { |
2352 | registers_info (addr_exp, 0); | |
2353 | } | |
e76f1f2e AC |
2354 | |
2355 | static void | |
d80b854b | 2356 | print_vector_info (struct ui_file *file, |
e76f1f2e AC |
2357 | struct frame_info *frame, const char *args) |
2358 | { | |
d80b854b UW |
2359 | struct gdbarch *gdbarch = get_frame_arch (frame); |
2360 | ||
e76f1f2e AC |
2361 | if (gdbarch_print_vector_info_p (gdbarch)) |
2362 | gdbarch_print_vector_info (gdbarch, file, frame, args); | |
2363 | else | |
2364 | { | |
2365 | int regnum; | |
2366 | int printed_something = 0; | |
ab4327e0 | 2367 | |
f6efe3f8 | 2368 | for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) |
e76f1f2e | 2369 | { |
f9418c0f | 2370 | if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup)) |
e76f1f2e AC |
2371 | { |
2372 | printed_something = 1; | |
e76f1f2e | 2373 | gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1); |
e76f1f2e AC |
2374 | } |
2375 | } | |
2376 | if (!printed_something) | |
6cb06a8c | 2377 | gdb_printf (file, "No vector information\n"); |
e76f1f2e AC |
2378 | } |
2379 | } | |
2380 | ||
2381 | static void | |
1d12d88f | 2382 | info_vector_command (const char *args, int from_tty) |
e76f1f2e | 2383 | { |
9dccd06e | 2384 | if (!target_has_registers ()) |
206415a3 DJ |
2385 | error (_("The program has no registers now.")); |
2386 | ||
d80b854b | 2387 | print_vector_info (gdb_stdout, get_selected_frame (NULL), args); |
e76f1f2e | 2388 | } |
c906108c | 2389 | \f |
5fd62852 PA |
2390 | /* Kill the inferior process. Make us have no inferior. */ |
2391 | ||
2392 | static void | |
981a3fb3 | 2393 | kill_command (const char *arg, int from_tty) |
5fd62852 PA |
2394 | { |
2395 | /* FIXME: This should not really be inferior_ptid (or target_has_execution). | |
2396 | It should be a distinct flag that indicates that a target is active, cuz | |
1777feb0 | 2397 | some targets don't have processes! */ |
5fd62852 | 2398 | |
d7e15655 | 2399 | if (inferior_ptid == null_ptid) |
5fd62852 PA |
2400 | error (_("The program is not being run.")); |
2401 | if (!query (_("Kill the program being debugged? "))) | |
2402 | error (_("Not confirmed.")); | |
f67c0c91 | 2403 | |
249b5733 PA |
2404 | int pid = current_inferior ()->pid; |
2405 | /* Save the pid as a string before killing the inferior, since that | |
2406 | may unpush the current target, and we need the string after. */ | |
f2907e49 | 2407 | std::string pid_str = target_pid_to_str (ptid_t (pid)); |
f67c0c91 SDJ |
2408 | int infnum = current_inferior ()->num; |
2409 | ||
5fd62852 | 2410 | target_kill (); |
34fda50b | 2411 | bfd_cache_close_all (); |
5fd62852 | 2412 | |
f67c0c91 | 2413 | if (print_inferior_events) |
6cb06a8c TT |
2414 | gdb_printf (_("[Inferior %d (%s) killed]\n"), |
2415 | infnum, pid_str.c_str ()); | |
5fd62852 | 2416 | } |
c5aa993b | 2417 | |
08036331 | 2418 | /* Used in `attach&' command. Proceed threads of inferior INF iff |
74531fed | 2419 | they stopped due to debugger request, and when they did, they |
08036331 PA |
2420 | reported a clean stop (GDB_SIGNAL_0). Do not proceed threads that |
2421 | have been explicitly been told to stop. */ | |
74531fed PA |
2422 | |
2423 | static void | |
08036331 | 2424 | proceed_after_attach (inferior *inf) |
74531fed PA |
2425 | { |
2426 | /* Don't error out if the current thread is running, because | |
2427 | there may be other stopped threads. */ | |
74531fed PA |
2428 | |
2429 | /* Backup current thread and selected frame. */ | |
5ed8105e | 2430 | scoped_restore_current_thread restore_thread; |
74531fed | 2431 | |
08036331 | 2432 | for (thread_info *thread : inf->non_exited_threads ()) |
611841bb | 2433 | if (!thread->executing () |
08036331 | 2434 | && !thread->stop_requested |
1edb66d8 | 2435 | && thread->stop_signal () == GDB_SIGNAL_0) |
08036331 PA |
2436 | { |
2437 | switch_to_thread (thread); | |
2438 | clear_proceed_status (0); | |
2439 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); | |
2440 | } | |
74531fed PA |
2441 | } |
2442 | ||
6efcd9a8 | 2443 | /* See inferior.h. */ |
c906108c | 2444 | |
6efcd9a8 PA |
2445 | void |
2446 | setup_inferior (int from_tty) | |
9356cf8d | 2447 | { |
d6b48e9c | 2448 | struct inferior *inferior; |
9356cf8d | 2449 | |
d6b48e9c | 2450 | inferior = current_inferior (); |
6efcd9a8 | 2451 | inferior->needs_setup = 0; |
9356cf8d PA |
2452 | |
2453 | /* If no exec file is yet known, try to determine it from the | |
2454 | process itself. */ | |
a10de604 | 2455 | if (get_exec_file (0) == NULL) |
e99b03dc | 2456 | exec_file_locate_attach (inferior_ptid.pid (), 1, from_tty); |
9356cf8d PA |
2457 | else |
2458 | { | |
2459 | reopen_exec_file (); | |
9dec38d3 | 2460 | reread_symbols (from_tty); |
9356cf8d PA |
2461 | } |
2462 | ||
2463 | /* Take any necessary post-attaching actions for this platform. */ | |
e99b03dc | 2464 | target_post_attach (inferior_ptid.pid ()); |
9356cf8d | 2465 | |
a7aba266 | 2466 | post_create_inferior (from_tty); |
6efcd9a8 PA |
2467 | } |
2468 | ||
2469 | /* What to do after the first program stops after attaching. */ | |
2470 | enum attach_post_wait_mode | |
2471 | { | |
2472 | /* Do nothing. Leaves threads as they are. */ | |
2473 | ATTACH_POST_WAIT_NOTHING, | |
2474 | ||
2475 | /* Re-resume threads that are marked running. */ | |
2476 | ATTACH_POST_WAIT_RESUME, | |
2477 | ||
2478 | /* Stop all threads. */ | |
2479 | ATTACH_POST_WAIT_STOP, | |
2480 | }; | |
2481 | ||
2482 | /* Called after we've attached to a process and we've seen it stop for | |
6fee5eee TBA |
2483 | the first time. Resume, stop, or don't touch the threads according |
2484 | to MODE. */ | |
6efcd9a8 PA |
2485 | |
2486 | static void | |
27d0790a | 2487 | attach_post_wait (int from_tty, enum attach_post_wait_mode mode) |
6efcd9a8 PA |
2488 | { |
2489 | struct inferior *inferior; | |
9356cf8d | 2490 | |
6efcd9a8 PA |
2491 | inferior = current_inferior (); |
2492 | inferior->control.stop_soon = NO_STOP_QUIETLY; | |
2493 | ||
2494 | if (inferior->needs_setup) | |
2495 | setup_inferior (from_tty); | |
2496 | ||
2497 | if (mode == ATTACH_POST_WAIT_RESUME) | |
74531fed PA |
2498 | { |
2499 | /* The user requested an `attach&', so be sure to leave threads | |
2500 | that didn't get a signal running. */ | |
2501 | ||
6fee5eee | 2502 | /* Immediately resume all suspended threads of this inferior, |
74531fed PA |
2503 | and this inferior only. This should have no effect on |
2504 | already running threads. If a thread has been stopped with a | |
2505 | signal, leave it be. */ | |
2506 | if (non_stop) | |
08036331 | 2507 | proceed_after_attach (inferior); |
74531fed PA |
2508 | else |
2509 | { | |
1edb66d8 | 2510 | if (inferior_thread ()->stop_signal () == GDB_SIGNAL_0) |
74531fed | 2511 | { |
70509625 | 2512 | clear_proceed_status (0); |
64ce06e4 | 2513 | proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT); |
74531fed PA |
2514 | } |
2515 | } | |
2516 | } | |
6efcd9a8 | 2517 | else if (mode == ATTACH_POST_WAIT_STOP) |
9356cf8d | 2518 | { |
74531fed PA |
2519 | /* The user requested a plain `attach', so be sure to leave |
2520 | the inferior stopped. */ | |
2521 | ||
74531fed PA |
2522 | /* At least the current thread is already stopped. */ |
2523 | ||
2524 | /* In all-stop, by definition, all threads have to be already | |
2525 | stopped at this point. In non-stop, however, although the | |
2526 | selected thread is stopped, others may still be executing. | |
2527 | Be sure to explicitly stop all threads of the process. This | |
2528 | should have no effect on already stopped threads. */ | |
066f6b6e | 2529 | if (non_stop) |
f2907e49 | 2530 | target_stop (ptid_t (inferior->pid)); |
066f6b6e PA |
2531 | else if (target_is_non_stop_p ()) |
2532 | { | |
066f6b6e | 2533 | struct thread_info *lowest = inferior_thread (); |
066f6b6e | 2534 | |
4f5539f0 | 2535 | stop_all_threads ("attaching"); |
066f6b6e PA |
2536 | |
2537 | /* It's not defined which thread will report the attach | |
2538 | stop. For consistency, always select the thread with | |
2539 | lowest GDB number, which should be the main thread, if it | |
2540 | still exists. */ | |
08036331 PA |
2541 | for (thread_info *thread : current_inferior ()->non_exited_threads ()) |
2542 | if (thread->inf->num < lowest->inf->num | |
2543 | || thread->per_inf_num < lowest->per_inf_num) | |
2544 | lowest = thread; | |
066f6b6e | 2545 | |
00431a78 | 2546 | switch_to_thread (lowest); |
066f6b6e | 2547 | } |
74531fed PA |
2548 | |
2549 | /* Tell the user/frontend where we're stopped. */ | |
9356cf8d PA |
2550 | normal_stop (); |
2551 | if (deprecated_attach_hook) | |
2552 | deprecated_attach_hook (); | |
2553 | } | |
2554 | } | |
2555 | ||
6efcd9a8 PA |
2556 | /* "attach" command entry point. Takes a program started up outside |
2557 | of gdb and ``attaches'' to it. This stops it cold in its tracks | |
2558 | and allows us to start debugging it. */ | |
2559 | ||
c906108c | 2560 | void |
0b39b52e | 2561 | attach_command (const char *args, int from_tty) |
c906108c | 2562 | { |
6c4486e6 | 2563 | int async_exec; |
b3ccfe11 | 2564 | struct target_ops *attach_target; |
6efcd9a8 PA |
2565 | struct inferior *inferior = current_inferior (); |
2566 | enum attach_post_wait_mode mode; | |
c906108c | 2567 | |
c5aa993b | 2568 | dont_repeat (); /* Not for the faint of heart */ |
c906108c | 2569 | |
1192f124 SM |
2570 | scoped_disable_commit_resumed disable_commit_resumed ("attaching"); |
2571 | ||
f5656ead | 2572 | if (gdbarch_has_global_solist (target_gdbarch ())) |
2567c7d9 PA |
2573 | /* Don't complain if all processes share the same symbol |
2574 | space. */ | |
8a305172 | 2575 | ; |
55f6301a | 2576 | else if (target_has_execution ()) |
c906108c | 2577 | { |
9e2f0ad4 | 2578 | if (query (_("A program is being debugged already. Kill it? "))) |
c906108c SS |
2579 | target_kill (); |
2580 | else | |
8a3fe4f8 | 2581 | error (_("Not killed.")); |
c906108c SS |
2582 | } |
2583 | ||
fd79ecee DJ |
2584 | /* Clean up any leftovers from other runs. Some other things from |
2585 | this function should probably be moved into target_pre_inferior. */ | |
2586 | target_pre_inferior (from_tty); | |
2587 | ||
6be9a197 TT |
2588 | gdb::unique_xmalloc_ptr<char> stripped = strip_bg_char (args, &async_exec); |
2589 | args = stripped.get (); | |
8bc2fe48 | 2590 | |
b3ccfe11 TT |
2591 | attach_target = find_attach_target (); |
2592 | ||
8bc2fe48 PA |
2593 | prepare_execution_command (attach_target, async_exec); |
2594 | ||
f6ac5f3d | 2595 | if (non_stop && !attach_target->supports_non_stop ()) |
9908b566 VP |
2596 | error (_("Cannot attach to this target in non-stop mode")); |
2597 | ||
f6ac5f3d | 2598 | attach_target->attach (args, from_tty); |
b3ccfe11 TT |
2599 | /* to_attach should push the target, so after this point we |
2600 | shouldn't refer to attach_target again. */ | |
2601 | attach_target = NULL; | |
c906108c | 2602 | |
1f9d9e32 AB |
2603 | infrun_debug_show_threads ("immediately after attach", |
2604 | current_inferior ()->non_exited_threads ()); | |
993248f4 | 2605 | |
e05523bd JB |
2606 | /* Enable async mode if it is supported by the target. */ |
2607 | if (target_can_async_p ()) | |
2608 | target_async (1); | |
2609 | ||
c906108c SS |
2610 | /* Set up the "saved terminal modes" of the inferior |
2611 | based on what modes we are starting it with. */ | |
223ffa71 | 2612 | target_terminal::init (); |
c906108c | 2613 | |
7180e04a PA |
2614 | /* Install inferior's terminal modes. This may look like a no-op, |
2615 | as we've just saved them above, however, this does more than | |
2616 | restore terminal settings: | |
2617 | ||
2618 | - installs a SIGINT handler that forwards SIGINT to the inferior. | |
2619 | Otherwise a Ctrl-C pressed just while waiting for the initial | |
2620 | stop would end up as a spurious Quit. | |
2621 | ||
2622 | - removes stdin from the event loop, which we need if attaching | |
2623 | in the foreground, otherwise on targets that report an initial | |
2624 | stop on attach (which are most) we'd process input/commands | |
2625 | while we're in the event loop waiting for that stop. That is, | |
2626 | before the attach continuation runs and the command is really | |
2627 | finished. */ | |
223ffa71 | 2628 | target_terminal::inferior (); |
7180e04a | 2629 | |
c906108c SS |
2630 | /* Set up execution context to know that we should return from |
2631 | wait_for_inferior as soon as the target reports a stop. */ | |
2632 | init_wait_for_inferior (); | |
c906108c | 2633 | |
6efcd9a8 PA |
2634 | inferior->needs_setup = 1; |
2635 | ||
fbea99ea | 2636 | if (target_is_non_stop_p ()) |
74531fed PA |
2637 | { |
2638 | /* If we find that the current thread isn't stopped, explicitly | |
2639 | do so now, because we're going to install breakpoints and | |
2640 | poke at memory. */ | |
2641 | ||
2642 | if (async_exec) | |
2643 | /* The user requested an `attach&'; stop just one thread. */ | |
2644 | target_stop (inferior_ptid); | |
2645 | else | |
2646 | /* The user requested an `attach', so stop all threads of this | |
2647 | inferior. */ | |
e99b03dc | 2648 | target_stop (ptid_t (inferior_ptid.pid ())); |
74531fed PA |
2649 | } |
2650 | ||
a2fedca9 PW |
2651 | /* Check for exec file mismatch, and let the user solve it. */ |
2652 | validate_exec_file (from_tty); | |
2653 | ||
6efcd9a8 PA |
2654 | mode = async_exec ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_STOP; |
2655 | ||
dc177b7a PA |
2656 | /* Some system don't generate traps when attaching to inferior. |
2657 | E.g. Mach 3 or GNU hurd. */ | |
f6ac5f3d | 2658 | if (!target_attach_no_wait ()) |
c5aa993b | 2659 | { |
1777feb0 | 2660 | /* Careful here. See comments in inferior.h. Basically some |
dc177b7a PA |
2661 | OSes don't ignore SIGSTOPs on continue requests anymore. We |
2662 | need a way for handle_inferior_event to reset the stop_signal | |
2663 | variable after an attach, and this is what | |
2664 | STOP_QUIETLY_NO_SIGSTOP is for. */ | |
16c381f0 | 2665 | inferior->control.stop_soon = STOP_QUIETLY_NO_SIGSTOP; |
c5aa993b | 2666 | |
3b12939d | 2667 | /* Wait for stop. */ |
4efeb0d3 | 2668 | inferior->add_continuation ([=] () |
c4c493de TBA |
2669 | { |
2670 | attach_post_wait (from_tty, mode); | |
2671 | }); | |
0b333c5e | 2672 | |
5b6d1e4f PA |
2673 | /* Let infrun consider waiting for events out of this |
2674 | target. */ | |
2675 | inferior->process_target ()->threads_executing = true; | |
2676 | ||
0b333c5e PA |
2677 | if (!target_is_async_p ()) |
2678 | mark_infrun_async_event_handler (); | |
2679 | return; | |
dc177b7a | 2680 | } |
5b6d1e4f | 2681 | else |
27d0790a | 2682 | attach_post_wait (from_tty, mode); |
1192f124 SM |
2683 | |
2684 | disable_commit_resumed.reset_and_commit (); | |
c906108c SS |
2685 | } |
2686 | ||
1941c569 PA |
2687 | /* We had just found out that the target was already attached to an |
2688 | inferior. PTID points at a thread of this new inferior, that is | |
2689 | the most likely to be stopped right now, but not necessarily so. | |
2690 | The new inferior is assumed to be already added to the inferior | |
2691 | list at this point. If LEAVE_RUNNING, then leave the threads of | |
2692 | this inferior running, except those we've explicitly seen reported | |
2693 | as stopped. */ | |
2694 | ||
2695 | void | |
8a82de58 | 2696 | notice_new_inferior (thread_info *thr, bool leave_running, int from_tty) |
1941c569 | 2697 | { |
5ed8105e PA |
2698 | enum attach_post_wait_mode mode |
2699 | = leave_running ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_NOTHING; | |
1941c569 | 2700 | |
5ed8105e | 2701 | gdb::optional<scoped_restore_current_thread> restore_thread; |
1941c569 | 2702 | |
5ed8105e PA |
2703 | if (inferior_ptid != null_ptid) |
2704 | restore_thread.emplace (); | |
1941c569 | 2705 | |
6efcd9a8 PA |
2706 | /* Avoid reading registers -- we haven't fetched the target |
2707 | description yet. */ | |
00431a78 | 2708 | switch_to_thread_no_regs (thr); |
1941c569 PA |
2709 | |
2710 | /* When we "notice" a new inferior we need to do all the things we | |
2711 | would normally do if we had just attached to it. */ | |
2712 | ||
611841bb | 2713 | if (thr->executing ()) |
1941c569 PA |
2714 | { |
2715 | struct inferior *inferior = current_inferior (); | |
2716 | ||
2717 | /* We're going to install breakpoints, and poke at memory, | |
2718 | ensure that the inferior is stopped for a moment while we do | |
2719 | that. */ | |
2720 | target_stop (inferior_ptid); | |
2721 | ||
16c381f0 | 2722 | inferior->control.stop_soon = STOP_QUIETLY_REMOTE; |
1941c569 PA |
2723 | |
2724 | /* Wait for stop before proceeding. */ | |
4efeb0d3 | 2725 | inferior->add_continuation ([=] () |
c4c493de TBA |
2726 | { |
2727 | attach_post_wait (from_tty, mode); | |
2728 | }); | |
1941c569 | 2729 | |
0b333c5e | 2730 | return; |
1941c569 PA |
2731 | } |
2732 | ||
27d0790a | 2733 | attach_post_wait (from_tty, mode); |
1941c569 PA |
2734 | } |
2735 | ||
c906108c SS |
2736 | /* |
2737 | * detach_command -- | |
2738 | * takes a program previously attached to and detaches it. | |
2739 | * The program resumes execution and will no longer stop | |
2740 | * on signals, etc. We better not have left any breakpoints | |
2741 | * in the program or it'll die when it hits one. For this | |
2742 | * to work, it may be necessary for the process to have been | |
2743 | * previously attached. It *might* work if the program was | |
2744 | * started via the normal ptrace (PTRACE_TRACEME). | |
2745 | */ | |
2746 | ||
6418d433 | 2747 | void |
981a3fb3 | 2748 | detach_command (const char *args, int from_tty) |
c906108c | 2749 | { |
1f5d0fc9 | 2750 | dont_repeat (); /* Not for the faint of heart. */ |
d729566a | 2751 | |
d7e15655 | 2752 | if (inferior_ptid == null_ptid) |
d729566a PA |
2753 | error (_("The program is not being run.")); |
2754 | ||
1192f124 SM |
2755 | scoped_disable_commit_resumed disable_commit_resumed ("detaching"); |
2756 | ||
2f9d54cf PA |
2757 | query_if_trace_running (from_tty); |
2758 | ||
2759 | disconnect_tracing (); | |
d5551862 | 2760 | |
408f6686 PA |
2761 | /* Hold a strong reference to the target while (maybe) |
2762 | detaching the parent. Otherwise detaching could close the | |
2763 | target. */ | |
2764 | auto target_ref | |
2765 | = target_ops_ref::new_reference (current_inferior ()->process_target ()); | |
2766 | ||
2767 | /* Save this before detaching, since detaching may unpush the | |
2768 | process_stratum target. */ | |
2769 | bool was_non_stop_p = target_is_non_stop_p (); | |
2770 | ||
6e1e1966 | 2771 | target_detach (current_inferior (), from_tty); |
50c71eaf | 2772 | |
63000888 PA |
2773 | /* The current inferior process was just detached successfully. Get |
2774 | rid of breakpoints that no longer make sense. Note we don't do | |
2775 | this within target_detach because that is also used when | |
2776 | following child forks, and in that case we will want to transfer | |
2777 | breakpoints to the child, not delete them. */ | |
2778 | breakpoint_init_inferior (inf_exited); | |
2779 | ||
50c71eaf PA |
2780 | /* If the solist is global across inferiors, don't clear it when we |
2781 | detach from a single inferior. */ | |
f5656ead | 2782 | if (!gdbarch_has_global_solist (target_gdbarch ())) |
50c71eaf | 2783 | no_shared_libraries (NULL, from_tty); |
8a305172 | 2784 | |
9a4105ab AC |
2785 | if (deprecated_detach_hook) |
2786 | deprecated_detach_hook (); | |
408f6686 PA |
2787 | |
2788 | if (!was_non_stop_p) | |
2789 | restart_after_all_stop_detach (as_process_stratum_target (target_ref.get ())); | |
1192f124 SM |
2790 | |
2791 | disable_commit_resumed.reset_and_commit (); | |
c906108c SS |
2792 | } |
2793 | ||
6ad8ae5c DJ |
2794 | /* Disconnect from the current target without resuming it (leaving it |
2795 | waiting for a debugger). | |
2796 | ||
2797 | We'd better not have left any breakpoints in the program or the | |
2798 | next debugger will get confused. Currently only supported for some | |
2799 | remote targets, since the normal attach mechanisms don't work on | |
2800 | stopped processes on some native platforms (e.g. GNU/Linux). */ | |
2801 | ||
2802 | static void | |
0b39b52e | 2803 | disconnect_command (const char *args, int from_tty) |
6ad8ae5c | 2804 | { |
1777feb0 | 2805 | dont_repeat (); /* Not for the faint of heart. */ |
2f9d54cf PA |
2806 | query_if_trace_running (from_tty); |
2807 | disconnect_tracing (); | |
6ad8ae5c | 2808 | target_disconnect (args, from_tty); |
e85a822c | 2809 | no_shared_libraries (NULL, from_tty); |
a0ef4274 | 2810 | init_thread_list (); |
9a4105ab AC |
2811 | if (deprecated_detach_hook) |
2812 | deprecated_detach_hook (); | |
6ad8ae5c DJ |
2813 | } |
2814 | ||
5b6d1e4f PA |
2815 | /* Stop PTID in the current target, and tag the PTID threads as having |
2816 | been explicitly requested to stop. PTID can be a thread, a | |
2817 | process, or minus_one_ptid, meaning all threads of all inferiors of | |
2818 | the current target. */ | |
e42de8c7 | 2819 | |
5b6d1e4f PA |
2820 | static void |
2821 | stop_current_target_threads_ns (ptid_t ptid) | |
2822 | { | |
2823 | target_stop (ptid); | |
252fbfc8 PA |
2824 | |
2825 | /* Tag the thread as having been explicitly requested to stop, so | |
2826 | other parts of gdb know not to resume this thread automatically, | |
2827 | if it was stopped due to an internal event. Limit this to | |
2828 | non-stop mode, as when debugging a multi-threaded application in | |
2829 | all-stop mode, we will only get one stop event --- it's undefined | |
2830 | which thread will report the event. */ | |
5b6d1e4f PA |
2831 | set_stop_requested (current_inferior ()->process_target (), |
2832 | ptid, 1); | |
2833 | } | |
2834 | ||
2835 | /* See inferior.h. */ | |
2836 | ||
2837 | void | |
2838 | interrupt_target_1 (bool all_threads) | |
2839 | { | |
1192f124 SM |
2840 | scoped_disable_commit_resumed disable_commit_resumed ("interrupting"); |
2841 | ||
252fbfc8 | 2842 | if (non_stop) |
5b6d1e4f PA |
2843 | { |
2844 | if (all_threads) | |
2845 | { | |
2846 | scoped_restore_current_thread restore_thread; | |
2847 | ||
2848 | for (inferior *inf : all_inferiors ()) | |
2849 | { | |
2850 | switch_to_inferior_no_thread (inf); | |
2851 | stop_current_target_threads_ns (minus_one_ptid); | |
2852 | } | |
2853 | } | |
2854 | else | |
2855 | stop_current_target_threads_ns (inferior_ptid); | |
2856 | } | |
2857 | else | |
2858 | target_interrupt (); | |
1192f124 SM |
2859 | |
2860 | disable_commit_resumed.reset_and_commit (); | |
77ebaa5a VP |
2861 | } |
2862 | ||
6339bfc4 DE |
2863 | /* interrupt [-a] |
2864 | Stop the execution of the target while running in async mode, in | |
17e5269b | 2865 | the background. In all-stop, stop the whole process. In non-stop |
8cae4b3f PA |
2866 | mode, stop the current thread only by default, or stop all threads |
2867 | if the `-a' switch is used. */ | |
2868 | ||
1dd5fedc | 2869 | static void |
0b39b52e | 2870 | interrupt_command (const char *args, int from_tty) |
43ff13b4 | 2871 | { |
362646f5 | 2872 | if (target_can_async_p ()) |
43ff13b4 | 2873 | { |
8cae4b3f PA |
2874 | int all_threads = 0; |
2875 | ||
1777feb0 | 2876 | dont_repeat (); /* Not for the faint of heart. */ |
94cc34af | 2877 | |
8cae4b3f | 2878 | if (args != NULL |
61012eef | 2879 | && startswith (args, "-a")) |
8cae4b3f PA |
2880 | all_threads = 1; |
2881 | ||
77ebaa5a | 2882 | interrupt_target_1 (all_threads); |
43ff13b4 JM |
2883 | } |
2884 | } | |
2885 | ||
cc86d1cb YQ |
2886 | /* See inferior.h. */ |
2887 | ||
2888 | void | |
2889 | default_print_float_info (struct gdbarch *gdbarch, struct ui_file *file, | |
2890 | struct frame_info *frame, const char *args) | |
c906108c | 2891 | { |
cc86d1cb YQ |
2892 | int regnum; |
2893 | int printed_something = 0; | |
d80b854b | 2894 | |
f6efe3f8 | 2895 | for (regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) |
23e3a7ac | 2896 | { |
cc86d1cb | 2897 | if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup)) |
23e3a7ac | 2898 | { |
cc86d1cb YQ |
2899 | printed_something = 1; |
2900 | gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1); | |
23e3a7ac | 2901 | } |
23e3a7ac | 2902 | } |
cc86d1cb | 2903 | if (!printed_something) |
6cb06a8c TT |
2904 | gdb_printf (file, "No floating-point info " |
2905 | "available for this processor.\n"); | |
23e3a7ac AC |
2906 | } |
2907 | ||
2908 | static void | |
1d12d88f | 2909 | info_float_command (const char *args, int from_tty) |
23e3a7ac | 2910 | { |
cc86d1cb YQ |
2911 | struct frame_info *frame; |
2912 | ||
9dccd06e | 2913 | if (!target_has_registers ()) |
206415a3 DJ |
2914 | error (_("The program has no registers now.")); |
2915 | ||
cc86d1cb YQ |
2916 | frame = get_selected_frame (NULL); |
2917 | gdbarch_print_float_info (get_frame_arch (frame), gdb_stdout, frame, args); | |
c906108c SS |
2918 | } |
2919 | \f | |
145b16a9 UW |
2920 | /* Implement `info proc' family of commands. */ |
2921 | ||
2922 | static void | |
69f476a3 | 2923 | info_proc_cmd_1 (const char *args, enum info_proc_what what, int from_tty) |
145b16a9 | 2924 | { |
3030c96e UW |
2925 | struct gdbarch *gdbarch = get_current_arch (); |
2926 | ||
451b7c33 TT |
2927 | if (!target_info_proc (args, what)) |
2928 | { | |
2929 | if (gdbarch_info_proc_p (gdbarch)) | |
2930 | gdbarch_info_proc (gdbarch, args, what); | |
2931 | else | |
2932 | error (_("Not supported on this target.")); | |
2933 | } | |
145b16a9 UW |
2934 | } |
2935 | ||
85102364 | 2936 | /* Implement `info proc' when given without any further parameters. */ |
145b16a9 UW |
2937 | |
2938 | static void | |
981a3fb3 | 2939 | info_proc_cmd (const char *args, int from_tty) |
145b16a9 UW |
2940 | { |
2941 | info_proc_cmd_1 (args, IP_MINIMAL, from_tty); | |
2942 | } | |
2943 | ||
2944 | /* Implement `info proc mappings'. */ | |
2945 | ||
2946 | static void | |
69f476a3 | 2947 | info_proc_cmd_mappings (const char *args, int from_tty) |
145b16a9 UW |
2948 | { |
2949 | info_proc_cmd_1 (args, IP_MAPPINGS, from_tty); | |
2950 | } | |
2951 | ||
2952 | /* Implement `info proc stat'. */ | |
2953 | ||
2954 | static void | |
69f476a3 | 2955 | info_proc_cmd_stat (const char *args, int from_tty) |
145b16a9 UW |
2956 | { |
2957 | info_proc_cmd_1 (args, IP_STAT, from_tty); | |
2958 | } | |
2959 | ||
2960 | /* Implement `info proc status'. */ | |
2961 | ||
2962 | static void | |
69f476a3 | 2963 | info_proc_cmd_status (const char *args, int from_tty) |
145b16a9 UW |
2964 | { |
2965 | info_proc_cmd_1 (args, IP_STATUS, from_tty); | |
2966 | } | |
2967 | ||
2968 | /* Implement `info proc cwd'. */ | |
2969 | ||
2970 | static void | |
69f476a3 | 2971 | info_proc_cmd_cwd (const char *args, int from_tty) |
145b16a9 UW |
2972 | { |
2973 | info_proc_cmd_1 (args, IP_CWD, from_tty); | |
2974 | } | |
2975 | ||
2976 | /* Implement `info proc cmdline'. */ | |
2977 | ||
2978 | static void | |
69f476a3 | 2979 | info_proc_cmd_cmdline (const char *args, int from_tty) |
145b16a9 UW |
2980 | { |
2981 | info_proc_cmd_1 (args, IP_CMDLINE, from_tty); | |
2982 | } | |
2983 | ||
2984 | /* Implement `info proc exe'. */ | |
2985 | ||
2986 | static void | |
69f476a3 | 2987 | info_proc_cmd_exe (const char *args, int from_tty) |
145b16a9 UW |
2988 | { |
2989 | info_proc_cmd_1 (args, IP_EXE, from_tty); | |
2990 | } | |
2991 | ||
e98ee8c4 JB |
2992 | /* Implement `info proc files'. */ |
2993 | ||
2994 | static void | |
2995 | info_proc_cmd_files (const char *args, int from_tty) | |
2996 | { | |
2997 | info_proc_cmd_1 (args, IP_FILES, from_tty); | |
2998 | } | |
2999 | ||
145b16a9 UW |
3000 | /* Implement `info proc all'. */ |
3001 | ||
3002 | static void | |
69f476a3 | 3003 | info_proc_cmd_all (const char *args, int from_tty) |
145b16a9 UW |
3004 | { |
3005 | info_proc_cmd_1 (args, IP_ALL, from_tty); | |
3006 | } | |
3007 | ||
000439d5 TT |
3008 | /* Implement `show print finish'. */ |
3009 | ||
3010 | static void | |
3011 | show_print_finish (struct ui_file *file, int from_tty, | |
3012 | struct cmd_list_element *c, | |
3013 | const char *value) | |
3014 | { | |
6cb06a8c | 3015 | gdb_printf (file, _("\ |
000439d5 | 3016 | Printing of return value after `finish' is %s.\n"), |
6cb06a8c | 3017 | value); |
000439d5 TT |
3018 | } |
3019 | ||
3020 | ||
4e5a4f58 JB |
3021 | /* This help string is used for the run, start, and starti commands. |
3022 | It is defined as a macro to prevent duplication. */ | |
3023 | ||
3024 | #define RUN_ARGS_HELP \ | |
3025 | "You may specify arguments to give it.\n\ | |
3026 | Args may include \"*\", or \"[...]\"; they are expanded using the\n\ | |
3027 | shell that will start the program (specified by the \"$SHELL\" environment\n\ | |
3028 | variable). Input and output redirection with \">\", \"<\", or \">>\"\n\ | |
3029 | are also allowed.\n\ | |
3030 | \n\ | |
3031 | With no arguments, uses arguments last specified (with \"run\" or \n\ | |
3032 | \"set args\"). To cancel previous arguments and run with no arguments,\n\ | |
3033 | use \"set args\" without arguments.\n\ | |
3034 | \n\ | |
3035 | To start the inferior without using a shell, use \"set startup-with-shell off\"." | |
3036 | ||
6c265988 | 3037 | void _initialize_infcmd (); |
c906108c | 3038 | void |
6c265988 | 3039 | _initialize_infcmd () |
c906108c | 3040 | { |
145b16a9 | 3041 | static struct cmd_list_element *info_proc_cmdlist; |
3cb3b8df | 3042 | struct cmd_list_element *c = NULL; |
6f937416 | 3043 | const char *cmd_name; |
3cb3b8df | 3044 | |
1777feb0 | 3045 | /* Add the filename of the terminal connected to inferior I/O. */ |
0a1ddfa6 SM |
3046 | add_setshow_optional_filename_cmd ("inferior-tty", class_run, |
3047 | &inferior_io_terminal_scratch, _("\ | |
3cb3b8df BR |
3048 | Set terminal for future runs of program being debugged."), _("\ |
3049 | Show terminal for future runs of program being debugged."), _("\ | |
0a1ddfa6 SM |
3050 | Usage: set inferior-tty [TTY]\n\n\ |
3051 | If TTY is omitted, the default behavior of using the same terminal as GDB\n\ | |
3052 | is restored."), | |
3053 | set_inferior_tty_command, | |
3054 | show_inferior_tty_command, | |
3055 | &setlist, &showlist); | |
21873064 | 3056 | cmd_name = "inferior-tty"; |
cf00cd6f | 3057 | c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1); |
21873064 | 3058 | gdb_assert (c != NULL); |
57b4f16e | 3059 | add_alias_cmd ("tty", c, class_run, 0, &cmdlist); |
c906108c | 3060 | |
6ace3df1 YQ |
3061 | cmd_name = "args"; |
3062 | add_setshow_string_noescape_cmd (cmd_name, class_run, | |
3063 | &inferior_args_scratch, _("\ | |
b4b4ac0b AC |
3064 | Set argument list to give program being debugged when it is started."), _("\ |
3065 | Show argument list to give program being debugged when it is started."), _("\ | |
3066 | Follow this command with any number of args, to be passed to the program."), | |
6ace3df1 YQ |
3067 | set_args_command, |
3068 | show_args_command, | |
3069 | &setlist, &showlist); | |
cf00cd6f | 3070 | c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1); |
6ace3df1 YQ |
3071 | gdb_assert (c != NULL); |
3072 | set_cmd_completer (c, filename_completer); | |
c906108c | 3073 | |
d092c5a2 SDJ |
3074 | cmd_name = "cwd"; |
3075 | add_setshow_string_noescape_cmd (cmd_name, class_run, | |
3076 | &inferior_cwd_scratch, _("\ | |
3077 | Set the current working directory to be used when the inferior is started.\n\ | |
3078 | Changing this setting does not have any effect on inferiors that are\n\ | |
3079 | already running."), | |
3080 | _("\ | |
3081 | Show the current working directory that is used when the inferior is started."), | |
3082 | _("\ | |
3083 | Use this command to change the current working directory that will be used\n\ | |
3084 | when the inferior is started. This setting does not affect GDB's current\n\ | |
3085 | working directory."), | |
3086 | set_cwd_command, | |
3087 | show_cwd_command, | |
3088 | &setlist, &showlist); | |
cf00cd6f | 3089 | c = lookup_cmd (&cmd_name, setlist, "", NULL, -1, 1); |
d092c5a2 SDJ |
3090 | gdb_assert (c != NULL); |
3091 | set_cmd_completer (c, filename_completer); | |
3092 | ||
1a966eab AC |
3093 | c = add_cmd ("environment", no_class, environment_info, _("\ |
3094 | The environment to give the program, or one variable's value.\n\ | |
c906108c SS |
3095 | With an argument VAR, prints the value of environment variable VAR to\n\ |
3096 | give the program being debugged. With no arguments, prints the entire\n\ | |
1a966eab | 3097 | environment to be given to the program."), &showlist); |
5ba2abeb | 3098 | set_cmd_completer (c, noop_completer); |
c906108c | 3099 | |
0743fc83 TT |
3100 | add_basic_prefix_cmd ("unset", no_class, |
3101 | _("Complement to certain \"set\" commands."), | |
2f822da5 | 3102 | &unsetlist, 0, &cmdlist); |
c5aa993b | 3103 | |
1a966eab AC |
3104 | c = add_cmd ("environment", class_run, unset_environment_command, _("\ |
3105 | Cancel environment variable VAR for the program.\n\ | |
3106 | This does not affect the program until the next \"run\" command."), | |
c5aa993b | 3107 | &unsetlist); |
5ba2abeb | 3108 | set_cmd_completer (c, noop_completer); |
c906108c | 3109 | |
1a966eab AC |
3110 | c = add_cmd ("environment", class_run, set_environment_command, _("\ |
3111 | Set environment variable value to give the program.\n\ | |
c906108c SS |
3112 | Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\ |
3113 | VALUES of environment variables are uninterpreted strings.\n\ | |
1a966eab | 3114 | This does not affect the program until the next \"run\" command."), |
c5aa993b | 3115 | &setlist); |
5ba2abeb | 3116 | set_cmd_completer (c, noop_completer); |
c5aa993b | 3117 | |
1bedd215 AC |
3118 | c = add_com ("path", class_files, path_command, _("\ |
3119 | Add directory DIR(s) to beginning of search path for object files.\n\ | |
c906108c SS |
3120 | $cwd in the path means the current working directory.\n\ |
3121 | This path is equivalent to the $PATH shell variable. It is a list of\n\ | |
3122 | directories, separated by colons. These directories are searched to find\n\ | |
3e43a32a MS |
3123 | fully linked executable files and separately compiled object files as \ |
3124 | needed.")); | |
5ba2abeb | 3125 | set_cmd_completer (c, filename_completer); |
c906108c | 3126 | |
1a966eab AC |
3127 | c = add_cmd ("paths", no_class, path_info, _("\ |
3128 | Current search path for finding object files.\n\ | |
c906108c SS |
3129 | $cwd in the path means the current working directory.\n\ |
3130 | This path is equivalent to the $PATH shell variable. It is a list of\n\ | |
3131 | directories, separated by colons. These directories are searched to find\n\ | |
3e43a32a MS |
3132 | fully linked executable files and separately compiled object files as \ |
3133 | needed."), | |
c906108c | 3134 | &showlist); |
5ba2abeb | 3135 | set_cmd_completer (c, noop_completer); |
c906108c | 3136 | |
2277426b PA |
3137 | add_prefix_cmd ("kill", class_run, kill_command, |
3138 | _("Kill execution of program being debugged."), | |
2f822da5 | 3139 | &killlist, 0, &cmdlist); |
5fd62852 | 3140 | |
1bedd215 AC |
3141 | add_com ("attach", class_run, attach_command, _("\ |
3142 | Attach to a process or file outside of GDB.\n\ | |
c906108c SS |
3143 | This command attaches to another target, of the same type as your last\n\ |
3144 | \"target\" command (\"info files\" will show your target stack).\n\ | |
3145 | The command may take as argument a process id or a device file.\n\ | |
3146 | For a process id, you must have permission to send the process a signal,\n\ | |
3147 | and it must have the same effective uid as the debugger.\n\ | |
3148 | When using \"attach\" with a process id, the debugger finds the\n\ | |
3149 | program running in the process, looking first in the current working\n\ | |
3150 | directory, or (if not found there) using the source file search path\n\ | |
3151 | (see the \"directory\" command). You can also use the \"file\" command\n\ | |
1bedd215 | 3152 | to specify the program, and to load its symbol table.")); |
c906108c | 3153 | |
f73adfeb | 3154 | add_prefix_cmd ("detach", class_run, detach_command, _("\ |
1bedd215 | 3155 | Detach a process or file previously attached.\n\ |
c906108c | 3156 | If a process, it is no longer traced, and it continues its execution. If\n\ |
f73adfeb | 3157 | you were debugging a file, the file is closed and gdb no longer accesses it."), |
2f822da5 | 3158 | &detachlist, 0, &cmdlist); |
c906108c | 3159 | |
1bedd215 AC |
3160 | add_com ("disconnect", class_run, disconnect_command, _("\ |
3161 | Disconnect from a target.\n\ | |
6ad8ae5c | 3162 | The target will wait for another debugger to connect. Not available for\n\ |
1bedd215 | 3163 | all targets.")); |
6ad8ae5c | 3164 | |
de0bea00 | 3165 | c = add_com ("signal", class_run, signal_command, _("\ |
486c7739 MF |
3166 | Continue program with the specified signal.\n\ |
3167 | Usage: signal SIGNAL\n\ | |
2edda2ff | 3168 | The SIGNAL argument is processed the same as the handle command.\n\ |
486c7739 MF |
3169 | \n\ |
3170 | An argument of \"0\" means continue the program without sending it a signal.\n\ | |
3171 | This is useful in cases where the program stopped because of a signal,\n\ | |
81219e53 DE |
3172 | and you want to resume the program while discarding the signal.\n\ |
3173 | \n\ | |
3174 | In a multi-threaded program the signal is delivered to, or discarded from,\n\ | |
3175 | the current thread only.")); | |
3176 | set_cmd_completer (c, signal_completer); | |
3177 | ||
3178 | c = add_com ("queue-signal", class_run, queue_signal_command, _("\ | |
3179 | Queue a signal to be delivered to the current thread when it is resumed.\n\ | |
3180 | Usage: queue-signal SIGNAL\n\ | |
3181 | The SIGNAL argument is processed the same as the handle command.\n\ | |
3182 | It is an error if the handling state of SIGNAL is \"nopass\".\n\ | |
3183 | \n\ | |
3184 | An argument of \"0\" means remove any currently queued signal from\n\ | |
3185 | the current thread. This is useful in cases where the program stopped\n\ | |
3186 | because of a signal, and you want to resume it while discarding the signal.\n\ | |
3187 | \n\ | |
3188 | In a multi-threaded program the signal is queued with, or discarded from,\n\ | |
3189 | the current thread only.")); | |
de0bea00 | 3190 | set_cmd_completer (c, signal_completer); |
c906108c | 3191 | |
3947f654 SM |
3192 | cmd_list_element *stepi_cmd |
3193 | = add_com ("stepi", class_run, stepi_command, _("\ | |
1bedd215 | 3194 | Step one instruction exactly.\n\ |
486c7739 MF |
3195 | Usage: stepi [N]\n\ |
3196 | Argument N means step N times (or till program stops for another \ | |
3e43a32a | 3197 | reason).")); |
3947f654 | 3198 | add_com_alias ("si", stepi_cmd, class_run, 0); |
c906108c | 3199 | |
3947f654 SM |
3200 | cmd_list_element *nexti_cmd |
3201 | = add_com ("nexti", class_run, nexti_command, _("\ | |
1bedd215 | 3202 | Step one instruction, but proceed through subroutine calls.\n\ |
486c7739 MF |
3203 | Usage: nexti [N]\n\ |
3204 | Argument N means step N times (or till program stops for another \ | |
3e43a32a | 3205 | reason).")); |
3947f654 | 3206 | add_com_alias ("ni", nexti_cmd, class_run, 0); |
c906108c | 3207 | |
3947f654 SM |
3208 | cmd_list_element *finish_cmd |
3209 | = add_com ("finish", class_run, finish_command, _("\ | |
1bedd215 | 3210 | Execute until selected stack frame returns.\n\ |
486c7739 | 3211 | Usage: finish\n\ |
1bedd215 | 3212 | Upon return, the value returned is printed and put in the value history.")); |
3947f654 | 3213 | add_com_alias ("fin", finish_cmd, class_run, 1); |
c906108c | 3214 | |
3947f654 SM |
3215 | cmd_list_element *next_cmd |
3216 | = add_com ("next", class_run, next_command, _("\ | |
1bedd215 | 3217 | Step program, proceeding through subroutine calls.\n\ |
486c7739 MF |
3218 | Usage: next [N]\n\ |
3219 | Unlike \"step\", if the current source line calls a subroutine,\n\ | |
3220 | this command does not enter the subroutine, but instead steps over\n\ | |
dbf6a605 | 3221 | the call, in effect treating it as a single source line.")); |
3947f654 | 3222 | add_com_alias ("n", next_cmd, class_run, 1); |
c906108c | 3223 | |
3947f654 SM |
3224 | cmd_list_element *step_cmd |
3225 | = add_com ("step", class_run, step_command, _("\ | |
1bedd215 | 3226 | Step program until it reaches a different source line.\n\ |
486c7739 MF |
3227 | Usage: step [N]\n\ |
3228 | Argument N means step N times (or till program stops for another \ | |
3e43a32a | 3229 | reason).")); |
3947f654 | 3230 | add_com_alias ("s", step_cmd, class_run, 1); |
c906108c | 3231 | |
3947f654 SM |
3232 | cmd_list_element *until_cmd |
3233 | = add_com ("until", class_run, until_command, _("\ | |
590042fc | 3234 | Execute until past the current line or past a LOCATION.\n\ |
1bedd215 | 3235 | Execute until the program reaches a source line greater than the current\n\ |
3e43a32a MS |
3236 | or a specified location (same args as break command) within the current \ |
3237 | frame.")); | |
3947f654 SM |
3238 | set_cmd_completer (until_cmd, location_completer); |
3239 | add_com_alias ("u", until_cmd, class_run, 1); | |
c5aa993b | 3240 | |
1bedd215 | 3241 | c = add_com ("advance", class_run, advance_command, _("\ |
3e43a32a MS |
3242 | Continue the program up to the given location (same form as args for break \ |
3243 | command).\n\ | |
1bedd215 | 3244 | Execution will also stop upon exit from the current stack frame.")); |
ae66c1fc EZ |
3245 | set_cmd_completer (c, location_completer); |
3246 | ||
3947f654 SM |
3247 | cmd_list_element *jump_cmd |
3248 | = add_com ("jump", class_run, jump_command, _("\ | |
1bedd215 | 3249 | Continue program being debugged at specified line or address.\n\ |
0a8ba311 | 3250 | Usage: jump LOCATION\n\ |
c906108c | 3251 | Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\ |
1bedd215 | 3252 | for an address to start at.")); |
3947f654 SM |
3253 | set_cmd_completer (jump_cmd, location_completer); |
3254 | add_com_alias ("j", jump_cmd, class_run, 1); | |
c906108c | 3255 | |
3947f654 SM |
3256 | cmd_list_element *continue_cmd |
3257 | = add_com ("continue", class_run, continue_command, _("\ | |
1bedd215 | 3258 | Continue program being debugged, after signal or breakpoint.\n\ |
486c7739 | 3259 | Usage: continue [N]\n\ |
c906108c SS |
3260 | If proceeding from breakpoint, a number N may be used as an argument,\n\ |
3261 | which means to set the ignore count of that breakpoint to N - 1 (so that\n\ | |
8cae4b3f PA |
3262 | the breakpoint won't break until the Nth time it is reached).\n\ |
3263 | \n\ | |
3264 | If non-stop mode is enabled, continue only the current thread,\n\ | |
3265 | otherwise all the threads in the program are continued. To \n\ | |
3266 | continue all stopped threads in non-stop mode, use the -a option.\n\ | |
3267 | Specifying -a and an ignore count simultaneously is an error.")); | |
3947f654 SM |
3268 | add_com_alias ("c", continue_cmd, class_run, 1); |
3269 | add_com_alias ("fg", continue_cmd, class_run, 1); | |
c906108c | 3270 | |
3947f654 SM |
3271 | cmd_list_element *run_cmd |
3272 | = add_com ("run", class_run, run_command, _("\ | |
4e5a4f58 JB |
3273 | Start debugged program.\n" |
3274 | RUN_ARGS_HELP)); | |
3947f654 SM |
3275 | set_cmd_completer (run_cmd, filename_completer); |
3276 | add_com_alias ("r", run_cmd, class_run, 1); | |
c906108c | 3277 | |
1bedd215 | 3278 | c = add_com ("start", class_run, start_command, _("\ |
4e5a4f58 JB |
3279 | Start the debugged program stopping at the beginning of the main procedure.\n" |
3280 | RUN_ARGS_HELP)); | |
3281 | set_cmd_completer (c, filename_completer); | |
3282 | ||
3283 | c = add_com ("starti", class_run, starti_command, _("\ | |
3284 | Start the debugged program stopping at the first instruction.\n" | |
3285 | RUN_ARGS_HELP)); | |
a4d5f2e0 JB |
3286 | set_cmd_completer (c, filename_completer); |
3287 | ||
0a07590b | 3288 | add_com ("interrupt", class_run, interrupt_command, |
df983543 | 3289 | _("Interrupt the execution of the debugged program.\n\ |
8cae4b3f PA |
3290 | If non-stop mode is enabled, interrupt only the current thread,\n\ |
3291 | otherwise all the threads in the program are stopped. To \n\ | |
3292 | interrupt all running threads in non-stop mode, use the -a option.")); | |
43ff13b4 | 3293 | |
e0f25bd9 SM |
3294 | cmd_list_element *info_registers_cmd |
3295 | = add_info ("registers", info_registers_command, _("\ | |
1bedd215 | 3296 | List of integer registers and their contents, for selected stack frame.\n\ |
b67d92b0 SH |
3297 | One or more register names as argument means describe the given registers.\n\ |
3298 | One or more register group names as argument means describe the registers\n\ | |
3299 | in the named register groups.")); | |
e0f25bd9 SM |
3300 | add_info_alias ("r", info_registers_cmd, 1); |
3301 | set_cmd_completer (info_registers_cmd, reg_or_group_completer); | |
c906108c | 3302 | |
11db9430 | 3303 | c = add_info ("all-registers", info_all_registers_command, _("\ |
1bedd215 | 3304 | List of all registers and their contents, for selected stack frame.\n\ |
b67d92b0 SH |
3305 | One or more register names as argument means describe the given registers.\n\ |
3306 | One or more register group names as argument means describe the registers\n\ | |
3307 | in the named register groups.")); | |
71c24708 | 3308 | set_cmd_completer (c, reg_or_group_completer); |
c906108c | 3309 | |
11db9430 | 3310 | add_info ("program", info_program_command, |
1bedd215 | 3311 | _("Execution status of the program.")); |
c906108c | 3312 | |
11db9430 | 3313 | add_info ("float", info_float_command, |
590042fc | 3314 | _("Print the status of the floating point unit.")); |
c906108c | 3315 | |
11db9430 | 3316 | add_info ("vector", info_vector_command, |
590042fc | 3317 | _("Print the status of the vector unit.")); |
145b16a9 UW |
3318 | |
3319 | add_prefix_cmd ("proc", class_info, info_proc_cmd, | |
3320 | _("\ | |
73f1bd76 | 3321 | Show additional information about a process.\n\ |
145b16a9 | 3322 | Specify any process id, or use the program being debugged by default."), |
2f822da5 | 3323 | &info_proc_cmdlist, |
145b16a9 UW |
3324 | 1/*allow-unknown*/, &infolist); |
3325 | ||
3326 | add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\ | |
73f1bd76 | 3327 | List memory regions mapped by the specified process."), |
145b16a9 UW |
3328 | &info_proc_cmdlist); |
3329 | ||
3330 | add_cmd ("stat", class_info, info_proc_cmd_stat, _("\ | |
3331 | List process info from /proc/PID/stat."), | |
3332 | &info_proc_cmdlist); | |
3333 | ||
3334 | add_cmd ("status", class_info, info_proc_cmd_status, _("\ | |
3335 | List process info from /proc/PID/status."), | |
3336 | &info_proc_cmdlist); | |
3337 | ||
3338 | add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\ | |
73f1bd76 | 3339 | List current working directory of the specified process."), |
145b16a9 UW |
3340 | &info_proc_cmdlist); |
3341 | ||
3342 | add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\ | |
73f1bd76 | 3343 | List command line arguments of the specified process."), |
145b16a9 UW |
3344 | &info_proc_cmdlist); |
3345 | ||
3346 | add_cmd ("exe", class_info, info_proc_cmd_exe, _("\ | |
73f1bd76 | 3347 | List absolute filename for executable of the specified process."), |
145b16a9 UW |
3348 | &info_proc_cmdlist); |
3349 | ||
e98ee8c4 JB |
3350 | add_cmd ("files", class_info, info_proc_cmd_files, _("\ |
3351 | List files opened by the specified process."), | |
3352 | &info_proc_cmdlist); | |
3353 | ||
145b16a9 | 3354 | add_cmd ("all", class_info, info_proc_cmd_all, _("\ |
73f1bd76 | 3355 | List all available info about the specified process."), |
145b16a9 | 3356 | &info_proc_cmdlist); |
000439d5 TT |
3357 | |
3358 | add_setshow_boolean_cmd ("finish", class_support, | |
5ffa6ca3 | 3359 | &finish_print, _("\ |
000439d5 TT |
3360 | Set whether `finish' prints the return value."), _("\ |
3361 | Show whether `finish' prints the return value."), NULL, | |
3362 | NULL, | |
3363 | show_print_finish, | |
3364 | &setprintlist, &showprintlist); | |
c906108c | 3365 | } |