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