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