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