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