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