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