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