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