1 /* Memory-access and commands for "inferior" process, for GDB.
3 Copyright (C) 1986-2016 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
36 #include "completer.h"
38 #include "event-top.h"
39 #include "parser-defs.h"
41 #include "reggroups.h"
46 #include "target-descriptions.h"
47 #include "user-regs.h"
48 #include "cli/cli-decode.h"
49 #include "gdbthread.h"
51 #include "inline-frame.h"
52 #include "tracepoint.h"
54 #include "continuations.h"
56 #include "cli/cli-utils.h"
58 #include "thread-fsm.h"
61 /* Local functions: */
63 static void nofp_registers_info (char *, int);
65 static void until_next_command (int);
67 static void until_command (char *, int);
69 static void path_info (char *, int);
71 static void path_command (char *, int);
73 static void unset_command (char *, int);
75 static void float_info (char *, int);
77 static void disconnect_command (char *, int);
79 static void unset_environment_command (char *, int);
81 static void set_environment_command (char *, int);
83 static void environment_info (char *, int);
85 static void program_info (char *, int);
87 static void finish_command (char *, int);
89 static void signal_command (char *, int);
91 static void jump_command (char *, int);
93 static void step_1 (int, int, char *);
95 static void next_command (char *, int);
97 static void step_command (char *, int);
99 static void run_command (char *, int);
101 void _initialize_infcmd (void);
103 #define ERROR_NO_INFERIOR \
104 if (!target_has_execution) error (_("The program is not being run."));
106 /* Scratch area where string containing arguments to give to the
107 program will be stored by 'set args'. As soon as anything is
108 stored, notice_args_set will move it into per-inferior storage.
109 Arguments are separated by spaces. Empty string (pointer to '\0')
112 static char *inferior_args_scratch;
114 /* Scratch area where 'set inferior-tty' will store user-provided value.
115 We'll immediate copy it into per-inferior storage. */
117 static char *inferior_io_terminal_scratch;
119 /* Pid of our debugged inferior, or 0 if no inferior now.
120 Since various parts of infrun.c test this to see whether there is a program
121 being debugged it should be nonzero (currently 3 is used) for remote
124 ptid_t inferior_ptid;
126 /* Address at which inferior stopped. */
130 /* Nonzero if stopped due to completion of a stack dummy routine. */
132 enum stop_stack_kind stop_stack_dummy;
134 /* Nonzero if stopped due to a random (unexpected) signal in inferior
137 int stopped_by_random_signal;
139 /* See inferior.h. */
141 int startup_with_shell = 1;
144 /* Accessor routines. */
146 /* Set the io terminal for the current inferior. Ownership of
147 TERMINAL_NAME is not transferred. */
150 set_inferior_io_terminal (const char *terminal_name)
152 xfree (current_inferior ()->terminal);
153 current_inferior ()->terminal = terminal_name ? xstrdup (terminal_name) : 0;
157 get_inferior_io_terminal (void)
159 return current_inferior ()->terminal;
163 set_inferior_tty_command (char *args, int from_tty,
164 struct cmd_list_element *c)
166 /* CLI has assigned the user-provided value to inferior_io_terminal_scratch.
167 Now route it to current inferior. */
168 set_inferior_io_terminal (inferior_io_terminal_scratch);
172 show_inferior_tty_command (struct ui_file *file, int from_tty,
173 struct cmd_list_element *c, const char *value)
175 /* Note that we ignore the passed-in value in favor of computing it
177 const char *inferior_io_terminal = get_inferior_io_terminal ();
179 if (inferior_io_terminal == NULL)
180 inferior_io_terminal = "";
181 fprintf_filtered (gdb_stdout,
182 _("Terminal for future runs of program being debugged "
183 "is \"%s\".\n"), inferior_io_terminal);
187 get_inferior_args (void)
189 if (current_inferior ()->argc != 0)
193 n = construct_inferior_arguments (current_inferior ()->argc,
194 current_inferior ()->argv);
195 set_inferior_args (n);
199 if (current_inferior ()->args == NULL)
200 current_inferior ()->args = xstrdup ("");
202 return current_inferior ()->args;
205 /* Set the arguments for the current inferior. Ownership of
206 NEWARGS is not transferred. */
209 set_inferior_args (char *newargs)
211 xfree (current_inferior ()->args);
212 current_inferior ()->args = newargs ? xstrdup (newargs) : NULL;
213 current_inferior ()->argc = 0;
214 current_inferior ()->argv = 0;
218 set_inferior_args_vector (int argc, char **argv)
220 current_inferior ()->argc = argc;
221 current_inferior ()->argv = argv;
224 /* Notice when `set args' is run. */
227 set_args_command (char *args, int from_tty, struct cmd_list_element *c)
229 /* CLI has assigned the user-provided value to inferior_args_scratch.
230 Now route it to current inferior. */
231 set_inferior_args (inferior_args_scratch);
234 /* Notice when `show args' is run. */
237 show_args_command (struct ui_file *file, int from_tty,
238 struct cmd_list_element *c, const char *value)
240 /* Note that we ignore the passed-in value in favor of computing it
242 deprecated_show_value_hack (file, from_tty, c, get_inferior_args ());
246 /* Compute command-line string given argument vector. This does the
247 same shell processing as fork_inferior. */
250 construct_inferior_arguments (int argc, char **argv)
254 if (startup_with_shell)
257 /* This holds all the characters considered special to the
259 char *special = "\"!&*|[]{}<>?`~^=;, \t\n";
260 const char quote = '"';
262 /* This holds all the characters considered special to the
263 typical Unix shells. We include `^' because the SunOS
264 /bin/sh treats it as a synonym for `|'. */
265 char *special = "\"!#$&*()\\|[]{}<>?'`~^; \t\n";
266 const char quote = '\'';
272 /* We over-compute the size. It shouldn't matter. */
273 for (i = 0; i < argc; ++i)
274 length += 3 * strlen (argv[i]) + 1 + 2 * (argv[i][0] == '\0');
276 result = (char *) xmalloc (length);
279 for (i = 0; i < argc; ++i)
284 /* Need to handle empty arguments specially. */
285 if (argv[i][0] == '\0')
295 if (strpbrk (argv[i], special))
301 for (cp = argv[i]; *cp; ++cp)
305 /* A newline cannot be quoted with a backslash (it
306 just disappears), only by putting it inside
317 if (strchr (special, *cp) != NULL)
333 /* In this case we can't handle arguments that contain spaces,
334 tabs, or newlines -- see breakup_args(). */
338 for (i = 0; i < argc; ++i)
340 char *cp = strchr (argv[i], ' ');
342 cp = strchr (argv[i], '\t');
344 cp = strchr (argv[i], '\n');
346 error (_("can't handle command-line "
347 "argument containing whitespace"));
348 length += strlen (argv[i]) + 1;
351 result = (char *) xmalloc (length);
353 for (i = 0; i < argc; ++i)
356 strcat (result, " ");
357 strcat (result, argv[i]);
365 /* This function strips the '&' character (indicating background
366 execution) that is added as *the last* of the arguments ARGS of a
367 command. A copy of the incoming ARGS without the '&' is returned,
368 unless the resulting string after stripping is empty, in which case
369 NULL is returned. *BG_CHAR_P is an output boolean that indicates
370 whether the '&' character was found. */
373 strip_bg_char (const char *args, int *bg_char_p)
377 if (args == NULL || *args == '\0')
383 p = args + strlen (args);
387 while (p > args && isspace (p[-1]))
392 return savestring (args, p - args);
398 return xstrdup (args);
401 /* Common actions to take after creating any sort of inferior, by any
402 means (running, attaching, connecting, et cetera). The target
403 should be stopped. */
406 post_create_inferior (struct target_ops *target, int from_tty)
409 /* Be sure we own the terminal in case write operations are performed. */
410 target_terminal_ours_for_output ();
412 /* If the target hasn't taken care of this already, do it now.
413 Targets which need to access registers during to_open,
414 to_create_inferior, or to_attach should do it earlier; but many
416 target_find_description ();
418 /* Now that we know the register layout, retrieve current PC. But
419 if the PC is unavailable (e.g., we're opening a core file with
420 missing registers info), ignore it. */
424 stop_pc = regcache_read_pc (get_current_regcache ());
426 CATCH (ex, RETURN_MASK_ERROR)
428 if (ex.error != NOT_AVAILABLE_ERROR)
429 throw_exception (ex);
435 const unsigned solib_add_generation
436 = current_program_space->solib_add_generation;
438 /* Create the hooks to handle shared library load and unload
440 solib_create_inferior_hook (from_tty);
442 if (current_program_space->solib_add_generation == solib_add_generation)
444 /* The platform-specific hook should load initial shared libraries,
445 but didn't. FROM_TTY will be incorrectly 0 but such solib
446 targets should be fixed anyway. Call it only after the solib
447 target has been initialized by solib_create_inferior_hook. */
450 warning (_("platform-specific solib_create_inferior_hook did "
451 "not load initial shared libraries."));
453 /* If the solist is global across processes, there's no need to
455 if (!gdbarch_has_global_solist (target_gdbarch ()))
456 solib_add (NULL, 0, target, auto_solib_add);
460 /* If the user sets watchpoints before execution having started,
461 then she gets software watchpoints, because GDB can't know which
462 target will end up being pushed, or if it supports hardware
463 watchpoints or not. breakpoint_re_set takes care of promoting
464 watchpoints to hardware watchpoints if possible, however, if this
465 new inferior doesn't load shared libraries or we don't pull in
466 symbols from any other source on this target/arch,
467 breakpoint_re_set is never called. Call it now so that software
468 watchpoints get a chance to be promoted to hardware watchpoints
469 if the now pushed target supports hardware watchpoints. */
470 breakpoint_re_set ();
472 observer_notify_inferior_created (target, from_tty);
475 /* Kill the inferior if already running. This function is designed
476 to be called when we are about to start the execution of the program
477 from the beginning. Ask the user to confirm that he wants to restart
478 the program being debugged when FROM_TTY is non-null. */
481 kill_if_already_running (int from_tty)
483 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
485 /* Bail out before killing the program if we will not be able to
487 target_require_runnable ();
490 && !query (_("The program being debugged has been started already.\n\
491 Start it from the beginning? ")))
492 error (_("Program not restarted."));
497 /* See inferior.h. */
500 prepare_execution_command (struct target_ops *target, int background)
502 /* If we get a request for running in the bg but the target
503 doesn't support it, error out. */
504 if (background && !target->to_can_async_p (target))
505 error (_("Asynchronous execution not supported on this target."));
509 /* If we get a request for running in the fg, then we need to
510 simulate synchronous (fg) execution. Note no cleanup is
511 necessary for this. stdin is re-enabled whenever an error
512 reaches the top level. */
513 async_disable_stdin ();
517 /* Implement the "run" command. If TBREAK_AT_MAIN is set, then insert
518 a temporary breakpoint at the begining of the main program before
519 running the program. */
522 run_command_1 (char *args, int from_tty, int tbreak_at_main)
525 struct cleanup *old_chain;
527 struct ui_out *uiout = current_uiout;
528 struct target_ops *run_target;
530 struct cleanup *args_chain;
534 kill_if_already_running (from_tty);
536 init_wait_for_inferior ();
537 clear_breakpoint_hit_counts ();
539 /* Clean up any leftovers from other runs. Some other things from
540 this function should probably be moved into target_pre_inferior. */
541 target_pre_inferior (from_tty);
543 /* The comment here used to read, "The exec file is re-read every
544 time we do a generic_mourn_inferior, so we just have to worry
545 about the symbol file." The `generic_mourn_inferior' function
546 gets called whenever the program exits. However, suppose the
547 program exits, and *then* the executable file changes? We need
548 to check again here. Since reopen_exec_file doesn't do anything
549 if the timestamp hasn't changed, I don't see the harm. */
553 args = strip_bg_char (args, &async_exec);
554 args_chain = make_cleanup (xfree, args);
556 /* Do validation and preparation before possibly changing anything
559 run_target = find_run_target ();
561 prepare_execution_command (run_target, async_exec);
563 if (non_stop && !run_target->to_supports_non_stop (run_target))
564 error (_("The target does not support running in non-stop mode."));
566 /* Done. Can now set breakpoints, change inferior args, etc. */
568 /* Insert the temporary breakpoint if a location was specified. */
570 tbreak_command (main_name (), 0);
572 exec_file = (char *) get_exec_file (0);
574 /* We keep symbols from add-symbol-file, on the grounds that the
575 user might want to add some symbols before running the program
576 (right?). But sometimes (dynamic loading where the user manually
577 introduces the new symbols with add-symbol-file), the code which
578 the symbols describe does not persist between runs. Currently
579 the user has to manually nuke all symbols between runs if they
580 want them to go away (PR 2207). This is probably reasonable. */
582 /* If there were other args, beside '&', process them. */
584 set_inferior_args (args);
588 ui_out_field_string (uiout, NULL, "Starting program");
589 ui_out_text (uiout, ": ");
591 ui_out_field_string (uiout, "execfile", exec_file);
592 ui_out_spaces (uiout, 1);
593 /* We call get_inferior_args() because we might need to compute
595 ui_out_field_string (uiout, "infargs", get_inferior_args ());
596 ui_out_text (uiout, "\n");
597 ui_out_flush (uiout);
600 /* Done with ARGS. */
601 do_cleanups (args_chain);
603 /* We call get_inferior_args() because we might need to compute
605 run_target->to_create_inferior (run_target, exec_file, get_inferior_args (),
606 environ_vector (current_inferior ()->environment),
608 /* to_create_inferior should push the target, so after this point we
609 shouldn't refer to run_target again. */
612 /* We're starting off a new process. When we get out of here, in
613 non-stop mode, finish the state of all threads of that process,
614 but leave other threads alone, as they may be stopped in internal
615 events --- the frontend shouldn't see them as stopped. In
616 all-stop, always finish the state of all threads, as we may be
617 resuming more than just the new process. */
619 ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
621 ptid = minus_one_ptid;
622 old_chain = make_cleanup (finish_thread_state_cleanup, &ptid);
624 /* Pass zero for FROM_TTY, because at this point the "run" command
625 has done its thing; now we are setting up the running program. */
626 post_create_inferior (¤t_target, 0);
628 /* Start the target running. Do not use -1 continuation as it would skip
629 breakpoint right at the entry point. */
630 proceed (regcache_read_pc (get_current_regcache ()), GDB_SIGNAL_0);
632 /* Since there was no error, there's no need to finish the thread
634 discard_cleanups (old_chain);
638 run_command (char *args, int from_tty)
640 run_command_1 (args, from_tty, 0);
643 /* Start the execution of the program up until the beginning of the main
647 start_command (char *args, int from_tty)
649 /* Some languages such as Ada need to search inside the program
650 minimal symbols for the location where to put the temporary
651 breakpoint before starting. */
652 if (!have_minimal_symbols ())
653 error (_("No symbol table loaded. Use the \"file\" command."));
655 /* Run the program until reaching the main procedure... */
656 run_command_1 (args, from_tty, 1);
660 proceed_thread_callback (struct thread_info *thread, void *arg)
662 /* We go through all threads individually instead of compressing
663 into a single target `resume_all' request, because some threads
664 may be stopped in internal breakpoints/events, or stopped waiting
665 for its turn in the displaced stepping queue (that is, they are
666 running && !executing). The target side has no idea about why
667 the thread is stopped, so a `resume_all' command would resume too
668 much. If/when GDB gains a way to tell the target `hold this
669 thread stopped until I say otherwise', then we can optimize
671 if (!is_stopped (thread->ptid))
674 switch_to_thread (thread->ptid);
675 clear_proceed_status (0);
676 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
681 ensure_valid_thread (void)
683 if (ptid_equal (inferior_ptid, null_ptid)
684 || is_exited (inferior_ptid))
685 error (_("Cannot execute this command without a live selected thread."));
688 /* If the user is looking at trace frames, any resumption of execution
689 is likely to mix up recorded and live target data. So simply
690 disallow those commands. */
693 ensure_not_tfind_mode (void)
695 if (get_traceframe_number () >= 0)
696 error (_("Cannot execute this command while looking at trace frames."));
699 /* Throw an error indicating the current thread is running. */
702 error_is_running (void)
704 error (_("Cannot execute this command while "
705 "the selected thread is running."));
708 /* Calls error_is_running if the current thread is running. */
711 ensure_not_running (void)
713 if (is_running (inferior_ptid))
718 continue_1 (int all_threads)
721 ensure_not_tfind_mode ();
723 if (non_stop && all_threads)
725 /* Don't error out if the current thread is running, because
726 there may be other stopped threads. */
727 struct cleanup *old_chain;
729 /* Backup current thread and selected frame. */
730 old_chain = make_cleanup_restore_current_thread ();
732 iterate_over_threads (proceed_thread_callback, NULL);
734 if (current_ui->prompt_state == PROMPT_BLOCKED)
736 /* If all threads in the target were already running,
737 proceed_thread_callback ends up never calling proceed,
738 and so nothing calls this to put the inferior's terminal
739 settings in effect and remove stdin from the event loop,
740 which we must when running a foreground command. E.g.:
744 <all threads are running now>
747 <no thread was resumed, but the inferior now owns the terminal>
749 target_terminal_inferior ();
752 /* Restore selected ptid. */
753 do_cleanups (old_chain);
757 ensure_valid_thread ();
758 ensure_not_running ();
759 clear_proceed_status (0);
760 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
764 /* continue [-a] [proceed-count] [&] */
767 continue_command (char *args, int from_tty)
771 struct cleanup *args_chain;
775 /* Find out whether we must run in the background. */
776 args = strip_bg_char (args, &async_exec);
777 args_chain = make_cleanup (xfree, args);
781 if (startswith (args, "-a"))
784 args += sizeof ("-a") - 1;
790 if (!non_stop && all_threads)
791 error (_("`-a' is meaningless in all-stop mode."));
793 if (args != NULL && all_threads)
794 error (_("Can't resume all threads and specify "
795 "proceed count simultaneously."));
797 /* If we have an argument left, set proceed count of breakpoint we
804 struct thread_info *tp;
807 tp = find_thread_ptid (inferior_ptid);
811 struct target_waitstatus ws;
813 get_last_target_status (&last_ptid, &ws);
814 tp = find_thread_ptid (last_ptid);
817 bs = tp->control.stop_bpstat;
819 while ((stat = bpstat_num (&bs, &num)) != 0)
822 set_ignore_count (num,
823 parse_and_eval_long (args) - 1,
825 /* set_ignore_count prints a message ending with a period.
826 So print two spaces before "Continuing.". */
828 printf_filtered (" ");
832 if (!stopped && from_tty)
835 ("Not stopped at any breakpoint; argument ignored.\n");
839 /* Done with ARGS. */
840 do_cleanups (args_chain);
843 ensure_not_tfind_mode ();
845 if (!non_stop || !all_threads)
847 ensure_valid_thread ();
848 ensure_not_running ();
851 prepare_execution_command (¤t_target, async_exec);
854 printf_filtered (_("Continuing.\n"));
856 continue_1 (all_threads);
859 /* Record the starting point of a "step" or "next" command. */
862 set_step_frame (void)
864 struct symtab_and_line sal;
866 struct frame_info *frame = get_current_frame ();
867 struct thread_info *tp = inferior_thread ();
869 find_frame_sal (frame, &sal);
870 set_step_info (frame, sal);
871 pc = get_frame_pc (frame);
872 tp->control.step_start_function = find_pc_function (pc);
875 /* Step until outside of current statement. */
878 step_command (char *count_string, int from_tty)
880 step_1 (0, 0, count_string);
883 /* Likewise, but skip over subroutine calls as if single instructions. */
886 next_command (char *count_string, int from_tty)
888 step_1 (1, 0, count_string);
891 /* Likewise, but step only one instruction. */
894 stepi_command (char *count_string, int from_tty)
896 step_1 (0, 1, count_string);
900 nexti_command (char *count_string, int from_tty)
902 step_1 (1, 1, count_string);
906 delete_longjmp_breakpoint_cleanup (void *arg)
908 int thread = * (int *) arg;
909 delete_longjmp_breakpoint (thread);
912 /* Data for the FSM that manages the step/next/stepi/nexti
915 struct step_command_fsm
917 /* The base class. */
918 struct thread_fsm thread_fsm;
920 /* How many steps left in a "step N"-like command. */
923 /* If true, this is a next/nexti, otherwise a step/stepi. */
924 int skip_subroutines;
926 /* If true, this is a stepi/nexti, otherwise a step/step. */
929 /* The thread that the command was run on. */
933 static void step_command_fsm_clean_up (struct thread_fsm *self);
934 static int step_command_fsm_should_stop (struct thread_fsm *self);
935 static enum async_reply_reason
936 step_command_fsm_async_reply_reason (struct thread_fsm *self);
938 /* step_command_fsm's vtable. */
940 static struct thread_fsm_ops step_command_fsm_ops =
943 step_command_fsm_clean_up,
944 step_command_fsm_should_stop,
945 NULL, /* return_value */
946 step_command_fsm_async_reply_reason,
949 /* Allocate a new step_command_fsm. */
951 static struct step_command_fsm *
952 new_step_command_fsm (void)
954 struct step_command_fsm *sm;
956 sm = XCNEW (struct step_command_fsm);
957 thread_fsm_ctor (&sm->thread_fsm, &step_command_fsm_ops);
962 /* Prepare for a step/next/etc. command. Any target resource
963 allocated here is undone in the FSM's clean_up method. */
966 step_command_fsm_prepare (struct step_command_fsm *sm,
967 int skip_subroutines, int single_inst,
968 int count, struct thread_info *thread)
970 sm->skip_subroutines = skip_subroutines;
971 sm->single_inst = single_inst;
973 sm->thread = thread->global_num;
975 /* Leave the si command alone. */
976 if (!sm->single_inst || sm->skip_subroutines)
977 set_longjmp_breakpoint (thread, get_frame_id (get_current_frame ()));
979 thread->control.stepping_command = 1;
982 static int prepare_one_step (struct step_command_fsm *sm);
985 step_1 (int skip_subroutines, int single_inst, char *count_string)
989 struct cleanup *args_chain;
990 struct thread_info *thr;
991 struct step_command_fsm *step_sm;
994 ensure_not_tfind_mode ();
995 ensure_valid_thread ();
996 ensure_not_running ();
998 count_string = strip_bg_char (count_string, &async_exec);
999 args_chain = make_cleanup (xfree, count_string);
1001 prepare_execution_command (¤t_target, async_exec);
1003 count = count_string ? parse_and_eval_long (count_string) : 1;
1005 /* Done with ARGS. */
1006 do_cleanups (args_chain);
1008 clear_proceed_status (1);
1010 /* Setup the execution command state machine to handle all the COUNT
1012 thr = inferior_thread ();
1013 step_sm = new_step_command_fsm ();
1014 thr->thread_fsm = &step_sm->thread_fsm;
1016 step_command_fsm_prepare (step_sm, skip_subroutines,
1017 single_inst, count, thr);
1019 /* Do only one step for now, before returning control to the event
1020 loop. Let the continuation figure out how many other steps we
1021 need to do, and handle them one at the time, through
1023 if (!prepare_one_step (step_sm))
1024 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1029 /* Stepped into an inline frame. Pretend that we've
1031 thread_fsm_clean_up (thr->thread_fsm);
1032 proceeded = normal_stop ();
1034 inferior_event_handler (INF_EXEC_COMPLETE, NULL);
1035 all_uis_check_sync_execution_done ();
1039 /* Implementation of the 'should_stop' FSM method for stepping
1040 commands. Called after we are done with one step operation, to
1041 check whether we need to step again, before we print the prompt and
1042 return control to the user. If count is > 1, returns false, as we
1043 will need to keep going. */
1046 step_command_fsm_should_stop (struct thread_fsm *self)
1048 struct step_command_fsm *sm = (struct step_command_fsm *) self;
1049 struct thread_info *tp = find_thread_global_id (sm->thread);
1051 if (tp->control.stop_step)
1053 /* There are more steps to make, and we did stop due to
1054 ending a stepping range. Do another step. */
1055 if (--sm->count > 0)
1056 return prepare_one_step (sm);
1058 thread_fsm_set_finished (self);
1064 /* Implementation of the 'clean_up' FSM method for stepping commands. */
1067 step_command_fsm_clean_up (struct thread_fsm *self)
1069 struct step_command_fsm *sm = (struct step_command_fsm *) self;
1071 if (!sm->single_inst || sm->skip_subroutines)
1072 delete_longjmp_breakpoint (sm->thread);
1075 /* Implementation of the 'async_reply_reason' FSM method for stepping
1078 static enum async_reply_reason
1079 step_command_fsm_async_reply_reason (struct thread_fsm *self)
1081 return EXEC_ASYNC_END_STEPPING_RANGE;
1084 /* Prepare for one step in "step N". The actual target resumption is
1085 done by the caller. Return true if we're done and should thus
1086 report a stop to the user. Returns false if the target needs to be
1090 prepare_one_step (struct step_command_fsm *sm)
1094 struct frame_info *frame = get_current_frame ();
1096 /* Don't assume THREAD is a valid thread id. It is set to -1 if
1097 the longjmp breakpoint was not required. Use the
1098 INFERIOR_PTID thread instead, which is the same thread when
1100 struct thread_info *tp = inferior_thread ();
1104 if (!sm->single_inst)
1108 /* Step at an inlined function behaves like "down". */
1109 if (!sm->skip_subroutines
1110 && inline_skipped_frames (inferior_ptid))
1114 /* Pretend that we've ran. */
1115 resume_ptid = user_visible_resume_ptid (1);
1116 set_running (resume_ptid, 1);
1118 step_into_inline_frame (inferior_ptid);
1120 return prepare_one_step (sm);
1123 pc = get_frame_pc (frame);
1124 find_pc_line_pc_range (pc,
1125 &tp->control.step_range_start,
1126 &tp->control.step_range_end);
1128 tp->control.may_range_step = 1;
1130 /* If we have no line info, switch to stepi mode. */
1131 if (tp->control.step_range_end == 0 && step_stop_if_no_debug)
1133 tp->control.step_range_start = tp->control.step_range_end = 1;
1134 tp->control.may_range_step = 0;
1136 else if (tp->control.step_range_end == 0)
1140 if (find_pc_partial_function (pc, &name,
1141 &tp->control.step_range_start,
1142 &tp->control.step_range_end) == 0)
1143 error (_("Cannot find bounds of current function"));
1145 target_terminal_ours_for_output ();
1146 printf_filtered (_("Single stepping until exit from function %s,"
1147 "\nwhich has no line number information.\n"),
1153 /* Say we are stepping, but stop after one insn whatever it does. */
1154 tp->control.step_range_start = tp->control.step_range_end = 1;
1155 if (!sm->skip_subroutines)
1157 Don't step over function calls, not even to functions lacking
1159 tp->control.step_over_calls = STEP_OVER_NONE;
1162 if (sm->skip_subroutines)
1163 tp->control.step_over_calls = STEP_OVER_ALL;
1169 thread_fsm_set_finished (&sm->thread_fsm);
1174 /* Continue program at specified address. */
1177 jump_command (char *arg, int from_tty)
1179 struct gdbarch *gdbarch = get_current_arch ();
1181 struct symtabs_and_lines sals;
1182 struct symtab_and_line sal;
1186 struct cleanup *args_chain;
1189 ensure_not_tfind_mode ();
1190 ensure_valid_thread ();
1191 ensure_not_running ();
1193 /* Find out whether we must run in the background. */
1194 arg = strip_bg_char (arg, &async_exec);
1195 args_chain = make_cleanup (xfree, arg);
1197 prepare_execution_command (¤t_target, async_exec);
1200 error_no_arg (_("starting address"));
1202 sals = decode_line_with_last_displayed (arg, DECODE_LINE_FUNFIRSTLINE);
1203 if (sals.nelts != 1)
1205 error (_("Unreasonable jump request"));
1211 /* Done with ARGS. */
1212 do_cleanups (args_chain);
1214 if (sal.symtab == 0 && sal.pc == 0)
1215 error (_("No source file has been specified."));
1217 resolve_sal_pc (&sal); /* May error out. */
1219 /* See if we are trying to jump to another function. */
1220 fn = get_frame_function (get_current_frame ());
1221 sfn = find_pc_function (sal.pc);
1222 if (fn != NULL && sfn != fn)
1224 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal.line,
1225 SYMBOL_PRINT_NAME (fn)))
1227 error (_("Not confirmed."));
1234 struct obj_section *section;
1236 fixup_symbol_section (sfn, 0);
1237 section = SYMBOL_OBJ_SECTION (symbol_objfile (sfn), sfn);
1238 if (section_is_overlay (section)
1239 && !section_is_mapped (section))
1241 if (!query (_("WARNING!!! Destination is in "
1242 "unmapped overlay! Jump anyway? ")))
1244 error (_("Not confirmed."));
1254 printf_filtered (_("Continuing at "));
1255 fputs_filtered (paddress (gdbarch, addr), gdb_stdout);
1256 printf_filtered (".\n");
1259 clear_proceed_status (0);
1260 proceed (addr, GDB_SIGNAL_0);
1263 /* Continue program giving it specified signal. */
1266 signal_command (char *signum_exp, int from_tty)
1268 enum gdb_signal oursig;
1270 struct cleanup *args_chain;
1272 dont_repeat (); /* Too dangerous. */
1274 ensure_not_tfind_mode ();
1275 ensure_valid_thread ();
1276 ensure_not_running ();
1278 /* Find out whether we must run in the background. */
1279 signum_exp = strip_bg_char (signum_exp, &async_exec);
1280 args_chain = make_cleanup (xfree, signum_exp);
1282 prepare_execution_command (¤t_target, async_exec);
1285 error_no_arg (_("signal number"));
1287 /* It would be even slicker to make signal names be valid expressions,
1288 (the type could be "enum $signal" or some such), then the user could
1289 assign them to convenience variables. */
1290 oursig = gdb_signal_from_name (signum_exp);
1292 if (oursig == GDB_SIGNAL_UNKNOWN)
1294 /* No, try numeric. */
1295 int num = parse_and_eval_long (signum_exp);
1298 oursig = GDB_SIGNAL_0;
1300 oursig = gdb_signal_from_command (num);
1303 do_cleanups (args_chain);
1305 /* Look for threads other than the current that this command ends up
1306 resuming too (due to schedlock off), and warn if they'll get a
1307 signal delivered. "signal 0" is used to suppress a previous
1308 signal, but if the current thread is no longer the one that got
1309 the signal, then the user is potentially suppressing the signal
1310 of the wrong thread. */
1313 struct thread_info *tp;
1315 int must_confirm = 0;
1317 /* This indicates what will be resumed. Either a single thread,
1318 a whole process, or all threads of all processes. */
1319 resume_ptid = user_visible_resume_ptid (0);
1321 ALL_NON_EXITED_THREADS (tp)
1323 if (ptid_equal (tp->ptid, inferior_ptid))
1325 if (!ptid_match (tp->ptid, resume_ptid))
1328 if (tp->suspend.stop_signal != GDB_SIGNAL_0
1329 && signal_pass_state (tp->suspend.stop_signal))
1332 printf_unfiltered (_("Note:\n"));
1333 printf_unfiltered (_(" Thread %s previously stopped with signal %s, %s.\n"),
1334 print_thread_id (tp),
1335 gdb_signal_to_name (tp->suspend.stop_signal),
1336 gdb_signal_to_string (tp->suspend.stop_signal));
1342 && !query (_("Continuing thread %s (the current thread) with specified signal will\n"
1343 "still deliver the signals noted above to their respective threads.\n"
1344 "Continue anyway? "),
1345 print_thread_id (inferior_thread ())))
1346 error (_("Not confirmed."));
1351 if (oursig == GDB_SIGNAL_0)
1352 printf_filtered (_("Continuing with no signal.\n"));
1354 printf_filtered (_("Continuing with signal %s.\n"),
1355 gdb_signal_to_name (oursig));
1358 clear_proceed_status (0);
1359 proceed ((CORE_ADDR) -1, oursig);
1362 /* Queue a signal to be delivered to the current thread. */
1365 queue_signal_command (char *signum_exp, int from_tty)
1367 enum gdb_signal oursig;
1368 struct thread_info *tp;
1371 ensure_not_tfind_mode ();
1372 ensure_valid_thread ();
1373 ensure_not_running ();
1375 if (signum_exp == NULL)
1376 error_no_arg (_("signal number"));
1378 /* It would be even slicker to make signal names be valid expressions,
1379 (the type could be "enum $signal" or some such), then the user could
1380 assign them to convenience variables. */
1381 oursig = gdb_signal_from_name (signum_exp);
1383 if (oursig == GDB_SIGNAL_UNKNOWN)
1385 /* No, try numeric. */
1386 int num = parse_and_eval_long (signum_exp);
1389 oursig = GDB_SIGNAL_0;
1391 oursig = gdb_signal_from_command (num);
1394 if (oursig != GDB_SIGNAL_0
1395 && !signal_pass_state (oursig))
1396 error (_("Signal handling set to not pass this signal to the program."));
1398 tp = inferior_thread ();
1399 tp->suspend.stop_signal = oursig;
1402 /* Data for the FSM that manages the until (with no argument)
1405 struct until_next_fsm
1407 /* The base class. */
1408 struct thread_fsm thread_fsm;
1410 /* The thread that as current when the command was executed. */
1414 static int until_next_fsm_should_stop (struct thread_fsm *self);
1415 static void until_next_fsm_clean_up (struct thread_fsm *self);
1416 static enum async_reply_reason
1417 until_next_fsm_async_reply_reason (struct thread_fsm *self);
1419 /* until_next_fsm's vtable. */
1421 static struct thread_fsm_ops until_next_fsm_ops =
1424 until_next_fsm_clean_up,
1425 until_next_fsm_should_stop,
1426 NULL, /* return_value */
1427 until_next_fsm_async_reply_reason,
1430 /* Allocate a new until_next_fsm. */
1432 static struct until_next_fsm *
1433 new_until_next_fsm (int thread)
1435 struct until_next_fsm *sm;
1437 sm = XCNEW (struct until_next_fsm);
1438 thread_fsm_ctor (&sm->thread_fsm, &until_next_fsm_ops);
1440 sm->thread = thread;
1445 /* Implementation of the 'should_stop' FSM method for the until (with
1449 until_next_fsm_should_stop (struct thread_fsm *self)
1451 struct thread_info *tp = inferior_thread ();
1453 if (tp->control.stop_step)
1454 thread_fsm_set_finished (self);
1459 /* Implementation of the 'clean_up' FSM method for the until (with no
1463 until_next_fsm_clean_up (struct thread_fsm *self)
1465 struct until_next_fsm *sm = (struct until_next_fsm *) self;
1467 delete_longjmp_breakpoint (sm->thread);
1470 /* Implementation of the 'async_reply_reason' FSM method for the until
1471 (with no arg) command. */
1473 static enum async_reply_reason
1474 until_next_fsm_async_reply_reason (struct thread_fsm *self)
1476 return EXEC_ASYNC_END_STEPPING_RANGE;
1479 /* Proceed until we reach a different source line with pc greater than
1480 our current one or exit the function. We skip calls in both cases.
1482 Note that eventually this command should probably be changed so
1483 that only source lines are printed out when we hit the breakpoint
1484 we set. This may involve changes to wait_for_inferior and the
1485 proceed status code. */
1488 until_next_command (int from_tty)
1490 struct frame_info *frame;
1492 struct symbol *func;
1493 struct symtab_and_line sal;
1494 struct thread_info *tp = inferior_thread ();
1495 int thread = tp->global_num;
1496 struct cleanup *old_chain;
1497 struct until_next_fsm *sm;
1499 clear_proceed_status (0);
1502 frame = get_current_frame ();
1504 /* Step until either exited from this function or greater
1505 than the current line (if in symbolic section) or pc (if
1508 pc = get_frame_pc (frame);
1509 func = find_pc_function (pc);
1513 struct bound_minimal_symbol msymbol = lookup_minimal_symbol_by_pc (pc);
1515 if (msymbol.minsym == NULL)
1516 error (_("Execution is not within a known function."));
1518 tp->control.step_range_start = BMSYMBOL_VALUE_ADDRESS (msymbol);
1519 /* The upper-bound of step_range is exclusive. In order to make PC
1520 within the range, set the step_range_end with PC + 1. */
1521 tp->control.step_range_end = pc + 1;
1525 sal = find_pc_line (pc, 0);
1527 tp->control.step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
1528 tp->control.step_range_end = sal.end;
1530 tp->control.may_range_step = 1;
1532 tp->control.step_over_calls = STEP_OVER_ALL;
1534 set_longjmp_breakpoint (tp, get_frame_id (frame));
1535 old_chain = make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
1537 sm = new_until_next_fsm (tp->global_num);
1538 tp->thread_fsm = &sm->thread_fsm;
1539 discard_cleanups (old_chain);
1541 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1545 until_command (char *arg, int from_tty)
1548 struct cleanup *args_chain;
1551 ensure_not_tfind_mode ();
1552 ensure_valid_thread ();
1553 ensure_not_running ();
1555 /* Find out whether we must run in the background. */
1556 arg = strip_bg_char (arg, &async_exec);
1557 args_chain = make_cleanup (xfree, arg);
1559 prepare_execution_command (¤t_target, async_exec);
1562 until_break_command (arg, from_tty, 0);
1564 until_next_command (from_tty);
1566 /* Done with ARGS. */
1567 do_cleanups (args_chain);
1571 advance_command (char *arg, int from_tty)
1574 struct cleanup *args_chain;
1577 ensure_not_tfind_mode ();
1578 ensure_valid_thread ();
1579 ensure_not_running ();
1582 error_no_arg (_("a location"));
1584 /* Find out whether we must run in the background. */
1585 arg = strip_bg_char (arg, &async_exec);
1586 args_chain = make_cleanup (xfree, arg);
1588 prepare_execution_command (¤t_target, async_exec);
1590 until_break_command (arg, from_tty, 1);
1592 /* Done with ARGS. */
1593 do_cleanups (args_chain);
1596 /* Return the value of the result of a function at the end of a 'finish'
1597 command/BP. DTOR_DATA (if not NULL) can represent inferior registers
1598 right after an inferior call has finished. */
1601 get_return_value (struct value *function, struct type *value_type)
1603 struct regcache *stop_regs;
1604 struct gdbarch *gdbarch;
1605 struct value *value;
1606 struct cleanup *cleanup;
1608 stop_regs = regcache_dup (get_current_regcache ());
1609 cleanup = make_cleanup_regcache_xfree (stop_regs);
1611 gdbarch = get_regcache_arch (stop_regs);
1613 value_type = check_typedef (value_type);
1614 gdb_assert (TYPE_CODE (value_type) != TYPE_CODE_VOID);
1616 /* FIXME: 2003-09-27: When returning from a nested inferior function
1617 call, it's possible (with no help from the architecture vector)
1618 to locate and return/print a "struct return" value. This is just
1619 a more complicated case of what is already being done in the
1620 inferior function call code. In fact, when inferior function
1621 calls are made async, this will likely be made the norm. */
1623 switch (gdbarch_return_value (gdbarch, function, value_type,
1626 case RETURN_VALUE_REGISTER_CONVENTION:
1627 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
1628 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
1629 value = allocate_value (value_type);
1630 gdbarch_return_value (gdbarch, function, value_type, stop_regs,
1631 value_contents_raw (value), NULL);
1633 case RETURN_VALUE_STRUCT_CONVENTION:
1637 internal_error (__FILE__, __LINE__, _("bad switch"));
1640 do_cleanups (cleanup);
1645 /* The captured function return value/type and its position in the
1648 struct return_value_info
1650 /* The captured return value. May be NULL if we weren't able to
1651 retrieve it. See get_return_value. */
1652 struct value *value;
1654 /* The return type. In some cases, we'll not be able extract the
1655 return value, but we always know the type. */
1658 /* If we captured a value, this is the value history index. */
1659 int value_history_index;
1662 /* Helper for print_return_value. */
1665 print_return_value_1 (struct ui_out *uiout, struct return_value_info *rv)
1667 if (rv->value != NULL)
1669 struct value_print_options opts;
1670 struct ui_file *stb;
1671 struct cleanup *old_chain;
1674 stb = mem_fileopen ();
1675 old_chain = make_cleanup_ui_file_delete (stb);
1676 ui_out_text (uiout, "Value returned is ");
1677 ui_out_field_fmt (uiout, "gdb-result-var", "$%d",
1678 rv->value_history_index);
1679 ui_out_text (uiout, " = ");
1680 get_no_prettyformat_print_options (&opts);
1681 value_print (rv->value, stb, &opts);
1682 ui_out_field_stream (uiout, "return-value", stb);
1683 ui_out_text (uiout, "\n");
1684 do_cleanups (old_chain);
1688 struct cleanup *oldchain;
1691 type_name = type_to_string (rv->type);
1692 oldchain = make_cleanup (xfree, type_name);
1693 ui_out_text (uiout, "Value returned has type: ");
1694 ui_out_field_string (uiout, "return-type", type_name);
1695 ui_out_text (uiout, ".");
1696 ui_out_text (uiout, " Cannot determine contents\n");
1697 do_cleanups (oldchain);
1701 /* Print the result of a function at the end of a 'finish' command.
1702 RV points at an object representing the captured return value/type
1703 and its position in the value history. */
1706 print_return_value (struct ui_out *uiout, struct return_value_info *rv)
1708 if (rv->type == NULL || TYPE_CODE (rv->type) == TYPE_CODE_VOID)
1713 /* print_return_value_1 can throw an exception in some
1714 circumstances. We need to catch this so that we still
1715 delete the breakpoint. */
1716 print_return_value_1 (uiout, rv);
1718 CATCH (ex, RETURN_MASK_ALL)
1720 exception_print (gdb_stdout, ex);
1725 /* Data for the FSM that manages the finish command. */
1727 struct finish_command_fsm
1729 /* The base class. */
1730 struct thread_fsm thread_fsm;
1732 /* The thread that was current when the command was executed. */
1735 /* The momentary breakpoint set at the function's return address in
1737 struct breakpoint *breakpoint;
1739 /* The function that we're stepping out of. */
1740 struct symbol *function;
1742 /* If the FSM finishes successfully, this stores the function's
1744 struct return_value_info return_value;
1747 static int finish_command_fsm_should_stop (struct thread_fsm *self);
1748 static void finish_command_fsm_clean_up (struct thread_fsm *self);
1749 static struct return_value_info *
1750 finish_command_fsm_return_value (struct thread_fsm *self);
1751 static enum async_reply_reason
1752 finish_command_fsm_async_reply_reason (struct thread_fsm *self);
1754 /* finish_command_fsm's vtable. */
1756 static struct thread_fsm_ops finish_command_fsm_ops =
1759 finish_command_fsm_clean_up,
1760 finish_command_fsm_should_stop,
1761 finish_command_fsm_return_value,
1762 finish_command_fsm_async_reply_reason,
1765 /* Allocate a new finish_command_fsm. */
1767 static struct finish_command_fsm *
1768 new_finish_command_fsm (int thread)
1770 struct finish_command_fsm *sm;
1772 sm = XCNEW (struct finish_command_fsm);
1773 thread_fsm_ctor (&sm->thread_fsm, &finish_command_fsm_ops);
1775 sm->thread = thread;
1780 /* Implementation of the 'should_stop' FSM method for the finish
1781 commands. Detects whether the thread stepped out of the function
1782 successfully, and if so, captures the function's return value and
1783 marks the FSM finished. */
1786 finish_command_fsm_should_stop (struct thread_fsm *self)
1788 struct finish_command_fsm *f = (struct finish_command_fsm *) self;
1789 struct return_value_info *rv = &f->return_value;
1790 struct thread_info *tp = find_thread_global_id (f->thread);
1792 if (f->function != NULL
1793 && bpstat_find_breakpoint (tp->control.stop_bpstat,
1794 f->breakpoint) != NULL)
1797 thread_fsm_set_finished (self);
1799 rv->type = TYPE_TARGET_TYPE (SYMBOL_TYPE (f->function));
1800 if (rv->type == NULL)
1801 internal_error (__FILE__, __LINE__,
1802 _("finish_command: function has no target type"));
1804 if (TYPE_CODE (rv->type) != TYPE_CODE_VOID)
1808 func = read_var_value (f->function, NULL, get_current_frame ());
1809 rv->value = get_return_value (func, rv->type);
1810 if (rv->value != NULL)
1811 rv->value_history_index = record_latest_value (rv->value);
1814 else if (tp->control.stop_step)
1816 /* Finishing from an inline frame, or reverse finishing. In
1817 either case, there's no way to retrieve the return value. */
1818 thread_fsm_set_finished (self);
1824 /* Implementation of the 'clean_up' FSM method for the finish
1828 finish_command_fsm_clean_up (struct thread_fsm *self)
1830 struct finish_command_fsm *f = (struct finish_command_fsm *) self;
1832 if (f->breakpoint != NULL)
1834 delete_breakpoint (f->breakpoint);
1835 f->breakpoint = NULL;
1837 delete_longjmp_breakpoint (f->thread);
1840 /* Implementation of the 'return_value' FSM method for the finish
1843 static struct return_value_info *
1844 finish_command_fsm_return_value (struct thread_fsm *self)
1846 struct finish_command_fsm *f = (struct finish_command_fsm *) self;
1848 return &f->return_value;
1851 /* Implementation of the 'async_reply_reason' FSM method for the
1854 static enum async_reply_reason
1855 finish_command_fsm_async_reply_reason (struct thread_fsm *self)
1857 if (execution_direction == EXEC_REVERSE)
1858 return EXEC_ASYNC_END_STEPPING_RANGE;
1860 return EXEC_ASYNC_FUNCTION_FINISHED;
1863 /* finish_backward -- helper function for finish_command. */
1866 finish_backward (struct finish_command_fsm *sm)
1868 struct symtab_and_line sal;
1869 struct thread_info *tp = inferior_thread ();
1871 CORE_ADDR func_addr;
1873 pc = get_frame_pc (get_current_frame ());
1875 if (find_pc_partial_function (pc, NULL, &func_addr, NULL) == 0)
1876 error (_("Cannot find bounds of current function"));
1878 sal = find_pc_line (func_addr, 0);
1880 tp->control.proceed_to_finish = 1;
1881 /* Special case: if we're sitting at the function entry point,
1882 then all we need to do is take a reverse singlestep. We
1883 don't need to set a breakpoint, and indeed it would do us
1886 Note that this can only happen at frame #0, since there's
1887 no way that a function up the stack can have a return address
1888 that's equal to its entry point. */
1892 struct frame_info *frame = get_selected_frame (NULL);
1893 struct gdbarch *gdbarch = get_frame_arch (frame);
1894 struct symtab_and_line sr_sal;
1896 /* Set a step-resume at the function's entry point. Once that's
1897 hit, we'll do one more step backwards. */
1900 sr_sal.pspace = get_frame_program_space (frame);
1901 insert_step_resume_breakpoint_at_sal (gdbarch,
1902 sr_sal, null_frame_id);
1904 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1908 /* We're almost there -- we just need to back up by one more
1910 tp->control.step_range_start = tp->control.step_range_end = 1;
1911 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1915 /* finish_forward -- helper function for finish_command. FRAME is the
1916 frame that called the function we're about to step out of. */
1919 finish_forward (struct finish_command_fsm *sm, struct frame_info *frame)
1921 struct frame_id frame_id = get_frame_id (frame);
1922 struct gdbarch *gdbarch = get_frame_arch (frame);
1923 struct symtab_and_line sal;
1924 struct thread_info *tp = inferior_thread ();
1926 sal = find_pc_line (get_frame_pc (frame), 0);
1927 sal.pc = get_frame_pc (frame);
1929 sm->breakpoint = set_momentary_breakpoint (gdbarch, sal,
1930 get_stack_frame_id (frame),
1933 /* set_momentary_breakpoint invalidates FRAME. */
1936 set_longjmp_breakpoint (tp, frame_id);
1938 /* We want to print return value, please... */
1939 tp->control.proceed_to_finish = 1;
1941 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
1944 /* Skip frames for "finish". */
1946 static struct frame_info *
1947 skip_finish_frames (struct frame_info *frame)
1949 struct frame_info *start;
1955 frame = skip_tailcall_frames (frame);
1959 frame = skip_unwritable_frames (frame);
1963 while (start != frame);
1968 /* "finish": Set a temporary breakpoint at the place the selected
1969 frame will return to, then continue. */
1972 finish_command (char *arg, int from_tty)
1974 struct frame_info *frame;
1976 struct cleanup *args_chain;
1977 struct finish_command_fsm *sm;
1978 struct thread_info *tp;
1981 ensure_not_tfind_mode ();
1982 ensure_valid_thread ();
1983 ensure_not_running ();
1985 /* Find out whether we must run in the background. */
1986 arg = strip_bg_char (arg, &async_exec);
1987 args_chain = make_cleanup (xfree, arg);
1989 prepare_execution_command (¤t_target, async_exec);
1992 error (_("The \"finish\" command does not take any arguments."));
1994 /* Done with ARGS. */
1995 do_cleanups (args_chain);
1997 frame = get_prev_frame (get_selected_frame (_("No selected frame.")));
1999 error (_("\"finish\" not meaningful in the outermost frame."));
2001 clear_proceed_status (0);
2003 tp = inferior_thread ();
2005 sm = new_finish_command_fsm (tp->global_num);
2007 tp->thread_fsm = &sm->thread_fsm;
2009 /* Finishing from an inline frame is completely different. We don't
2010 try to show the "return value" - no way to locate it. */
2011 if (get_frame_type (get_selected_frame (_("No selected frame.")))
2014 /* Claim we are stepping in the calling frame. An empty step
2015 range means that we will stop once we aren't in a function
2016 called by that frame. We don't use the magic "1" value for
2017 step_range_end, because then infrun will think this is nexti,
2018 and not step over the rest of this inlined function call. */
2019 struct symtab_and_line empty_sal;
2021 init_sal (&empty_sal);
2022 set_step_info (frame, empty_sal);
2023 tp->control.step_range_start = get_frame_pc (frame);
2024 tp->control.step_range_end = tp->control.step_range_start;
2025 tp->control.step_over_calls = STEP_OVER_ALL;
2027 /* Print info on the selected frame, including level number but not
2031 printf_filtered (_("Run till exit from "));
2032 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0);
2035 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2039 /* Find the function we will return from. */
2041 sm->function = find_pc_function (get_frame_pc (get_selected_frame (NULL)));
2043 /* Print info on the selected frame, including level number but not
2047 if (execution_direction == EXEC_REVERSE)
2048 printf_filtered (_("Run back to call of "));
2051 if (sm->function != NULL && TYPE_NO_RETURN (sm->function->type)
2052 && !query (_("warning: Function %s does not return normally.\n"
2053 "Try to finish anyway? "),
2054 SYMBOL_PRINT_NAME (sm->function)))
2055 error (_("Not confirmed."));
2056 printf_filtered (_("Run till exit from "));
2059 print_stack_frame (get_selected_frame (NULL), 1, LOCATION, 0);
2062 if (execution_direction == EXEC_REVERSE)
2063 finish_backward (sm);
2066 frame = skip_finish_frames (frame);
2069 error (_("Cannot find the caller frame."));
2071 finish_forward (sm, frame);
2077 program_info (char *args, int from_tty)
2081 struct thread_info *tp;
2084 if (!target_has_execution)
2086 printf_filtered (_("The program being debugged is not being run.\n"));
2091 ptid = inferior_ptid;
2094 struct target_waitstatus ws;
2096 get_last_target_status (&ptid, &ws);
2099 if (ptid_equal (ptid, null_ptid) || is_exited (ptid))
2100 error (_("Invalid selected thread."));
2101 else if (is_running (ptid))
2102 error (_("Selected thread is running."));
2104 tp = find_thread_ptid (ptid);
2105 bs = tp->control.stop_bpstat;
2106 stat = bpstat_num (&bs, &num);
2108 target_files_info ();
2109 printf_filtered (_("Program stopped at %s.\n"),
2110 paddress (target_gdbarch (), stop_pc));
2111 if (tp->control.stop_step)
2112 printf_filtered (_("It stopped after being stepped.\n"));
2115 /* There may be several breakpoints in the same place, so this
2116 isn't as strange as it seems. */
2121 printf_filtered (_("It stopped at a breakpoint "
2122 "that has since been deleted.\n"));
2125 printf_filtered (_("It stopped at breakpoint %d.\n"), num);
2126 stat = bpstat_num (&bs, &num);
2129 else if (tp->suspend.stop_signal != GDB_SIGNAL_0)
2131 printf_filtered (_("It stopped with signal %s, %s.\n"),
2132 gdb_signal_to_name (tp->suspend.stop_signal),
2133 gdb_signal_to_string (tp->suspend.stop_signal));
2138 printf_filtered (_("Type \"info stack\" or \"info "
2139 "registers\" for more information.\n"));
2144 environment_info (char *var, int from_tty)
2148 char *val = get_in_environ (current_inferior ()->environment, var);
2152 puts_filtered (var);
2153 puts_filtered (" = ");
2154 puts_filtered (val);
2155 puts_filtered ("\n");
2159 puts_filtered ("Environment variable \"");
2160 puts_filtered (var);
2161 puts_filtered ("\" not defined.\n");
2166 char **vector = environ_vector (current_inferior ()->environment);
2170 puts_filtered (*vector++);
2171 puts_filtered ("\n");
2177 set_environment_command (char *arg, int from_tty)
2179 char *p, *val, *var;
2183 error_no_arg (_("environment variable and value"));
2185 /* Find seperation between variable name and value. */
2186 p = (char *) strchr (arg, '=');
2187 val = (char *) strchr (arg, ' ');
2189 if (p != 0 && val != 0)
2191 /* We have both a space and an equals. If the space is before the
2192 equals, walk forward over the spaces til we see a nonspace
2193 (possibly the equals). */
2198 /* Now if the = is after the char following the spaces,
2199 take the char following the spaces. */
2203 else if (val != 0 && p == 0)
2207 error_no_arg (_("environment variable to set"));
2209 if (p == 0 || p[1] == 0)
2213 p = arg + strlen (arg); /* So that savestring below will work. */
2217 /* Not setting variable value to null. */
2219 while (*val == ' ' || *val == '\t')
2223 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
2226 var = savestring (arg, p - arg);
2229 printf_filtered (_("Setting environment variable "
2230 "\"%s\" to null value.\n"),
2232 set_in_environ (current_inferior ()->environment, var, "");
2235 set_in_environ (current_inferior ()->environment, var, val);
2240 unset_environment_command (char *var, int from_tty)
2244 /* If there is no argument, delete all environment variables.
2245 Ask for confirmation if reading from the terminal. */
2246 if (!from_tty || query (_("Delete all environment variables? ")))
2248 free_environ (current_inferior ()->environment);
2249 current_inferior ()->environment = make_environ ();
2253 unset_in_environ (current_inferior ()->environment, var);
2256 /* Handle the execution path (PATH variable). */
2258 static const char path_var_name[] = "PATH";
2261 path_info (char *args, int from_tty)
2263 puts_filtered ("Executable and object file path: ");
2264 puts_filtered (get_in_environ (current_inferior ()->environment,
2266 puts_filtered ("\n");
2269 /* Add zero or more directories to the front of the execution path. */
2272 path_command (char *dirname, int from_tty)
2278 env = get_in_environ (current_inferior ()->environment, path_var_name);
2279 /* Can be null if path is not set. */
2282 exec_path = xstrdup (env);
2283 mod_path (dirname, &exec_path);
2284 set_in_environ (current_inferior ()->environment, path_var_name, exec_path);
2287 path_info ((char *) NULL, from_tty);
2291 /* Print out the register NAME with value VAL, to FILE, in the default
2295 default_print_one_register_info (struct ui_file *file,
2299 struct type *regtype = value_type (val);
2300 int print_raw_format;
2302 fputs_filtered (name, file);
2303 print_spaces_filtered (15 - strlen (name), file);
2305 print_raw_format = (value_entirely_available (val)
2306 && !value_optimized_out (val));
2308 /* If virtual format is floating, print it that way, and in raw
2310 if (TYPE_CODE (regtype) == TYPE_CODE_FLT
2311 || TYPE_CODE (regtype) == TYPE_CODE_DECFLOAT)
2313 struct value_print_options opts;
2314 const gdb_byte *valaddr = value_contents_for_printing (val);
2315 enum bfd_endian byte_order = gdbarch_byte_order (get_type_arch (regtype));
2317 get_user_print_options (&opts);
2321 value_contents_for_printing (val),
2322 value_embedded_offset (val), 0,
2323 file, 0, val, &opts, current_language);
2325 if (print_raw_format)
2327 fprintf_filtered (file, "\t(raw ");
2328 print_hex_chars (file, valaddr, TYPE_LENGTH (regtype), byte_order);
2329 fprintf_filtered (file, ")");
2334 struct value_print_options opts;
2336 /* Print the register in hex. */
2337 get_formatted_print_options (&opts, 'x');
2340 value_contents_for_printing (val),
2341 value_embedded_offset (val), 0,
2342 file, 0, val, &opts, current_language);
2343 /* If not a vector register, print it also according to its
2345 if (print_raw_format && TYPE_VECTOR (regtype) == 0)
2347 get_user_print_options (&opts);
2349 fprintf_filtered (file, "\t");
2351 value_contents_for_printing (val),
2352 value_embedded_offset (val), 0,
2353 file, 0, val, &opts, current_language);
2357 fprintf_filtered (file, "\n");
2360 /* Print out the machine register regnum. If regnum is -1, print all
2361 registers (print_all == 1) or all non-float and non-vector
2362 registers (print_all == 0).
2364 For most machines, having all_registers_info() print the
2365 register(s) one per line is good enough. If a different format is
2366 required, (eg, for MIPS or Pyramid 90x, which both have lots of
2367 regs), or there is an existing convention for showing all the
2368 registers, define the architecture method PRINT_REGISTERS_INFO to
2369 provide that format. */
2372 default_print_registers_info (struct gdbarch *gdbarch,
2373 struct ui_file *file,
2374 struct frame_info *frame,
2375 int regnum, int print_all)
2378 const int numregs = gdbarch_num_regs (gdbarch)
2379 + gdbarch_num_pseudo_regs (gdbarch);
2381 for (i = 0; i < numregs; i++)
2383 /* Decide between printing all regs, non-float / vector regs, or
2389 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
2394 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
2404 /* If the register name is empty, it is undefined for this
2405 processor, so don't display anything. */
2406 if (gdbarch_register_name (gdbarch, i) == NULL
2407 || *(gdbarch_register_name (gdbarch, i)) == '\0')
2410 default_print_one_register_info (file,
2411 gdbarch_register_name (gdbarch, i),
2412 value_of_register (i, frame));
2417 registers_info (char *addr_exp, int fpregs)
2419 struct frame_info *frame;
2420 struct gdbarch *gdbarch;
2422 if (!target_has_registers)
2423 error (_("The program has no registers now."));
2424 frame = get_selected_frame (NULL);
2425 gdbarch = get_frame_arch (frame);
2429 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2434 while (*addr_exp != '\0')
2439 /* Skip leading white space. */
2440 addr_exp = skip_spaces (addr_exp);
2442 /* Discard any leading ``$''. Check that there is something
2443 resembling a register following it. */
2444 if (addr_exp[0] == '$')
2446 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
2447 error (_("Missing register name"));
2449 /* Find the start/end of this register name/num/group. */
2451 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
2455 /* Figure out what we've found and display it. */
2457 /* A register name? */
2459 int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start);
2463 /* User registers lie completely outside of the range of
2464 normal registers. Catch them early so that the target
2466 if (regnum >= gdbarch_num_regs (gdbarch)
2467 + gdbarch_num_pseudo_regs (gdbarch))
2469 struct value *regval = value_of_user_reg (regnum, frame);
2470 const char *regname = user_reg_map_regnum_to_name (gdbarch,
2473 /* Print in the same fashion
2474 gdbarch_print_registers_info's default
2475 implementation prints. */
2476 default_print_one_register_info (gdb_stdout,
2481 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2482 frame, regnum, fpregs);
2487 /* A register group? */
2489 struct reggroup *group;
2491 for (group = reggroup_next (gdbarch, NULL);
2493 group = reggroup_next (gdbarch, group))
2495 /* Don't bother with a length check. Should the user
2496 enter a short register group name, go with the first
2497 group that matches. */
2498 if (strncmp (start, reggroup_name (group), end - start) == 0)
2506 regnum < gdbarch_num_regs (gdbarch)
2507 + gdbarch_num_pseudo_regs (gdbarch);
2510 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
2511 gdbarch_print_registers_info (gdbarch,
2519 /* Nothing matched. */
2520 error (_("Invalid register `%.*s'"), (int) (end - start), start);
2525 all_registers_info (char *addr_exp, int from_tty)
2527 registers_info (addr_exp, 1);
2531 nofp_registers_info (char *addr_exp, int from_tty)
2533 registers_info (addr_exp, 0);
2537 print_vector_info (struct ui_file *file,
2538 struct frame_info *frame, const char *args)
2540 struct gdbarch *gdbarch = get_frame_arch (frame);
2542 if (gdbarch_print_vector_info_p (gdbarch))
2543 gdbarch_print_vector_info (gdbarch, file, frame, args);
2547 int printed_something = 0;
2550 regnum < gdbarch_num_regs (gdbarch)
2551 + gdbarch_num_pseudo_regs (gdbarch);
2554 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
2556 printed_something = 1;
2557 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2560 if (!printed_something)
2561 fprintf_filtered (file, "No vector information\n");
2566 vector_info (char *args, int from_tty)
2568 if (!target_has_registers)
2569 error (_("The program has no registers now."));
2571 print_vector_info (gdb_stdout, get_selected_frame (NULL), args);
2574 /* Kill the inferior process. Make us have no inferior. */
2577 kill_command (char *arg, int from_tty)
2579 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2580 It should be a distinct flag that indicates that a target is active, cuz
2581 some targets don't have processes! */
2583 if (ptid_equal (inferior_ptid, null_ptid))
2584 error (_("The program is not being run."));
2585 if (!query (_("Kill the program being debugged? ")))
2586 error (_("Not confirmed."));
2589 /* If we still have other inferiors to debug, then don't mess with
2590 with their threads. */
2591 if (!have_inferiors ())
2593 init_thread_list (); /* Destroy thread info. */
2595 /* Killing off the inferior can leave us with a core file. If
2596 so, print the state we are left in. */
2597 if (target_has_stack)
2599 printf_filtered (_("In %s,\n"), target_longname);
2600 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC, 1);
2603 bfd_cache_close_all ();
2606 /* Used in `attach&' command. ARG is a point to an integer
2607 representing a process id. Proceed threads of this process iff
2608 they stopped due to debugger request, and when they did, they
2609 reported a clean stop (GDB_SIGNAL_0). Do not proceed threads
2610 that have been explicitly been told to stop. */
2613 proceed_after_attach_callback (struct thread_info *thread,
2616 int pid = * (int *) arg;
2618 if (ptid_get_pid (thread->ptid) == pid
2619 && !is_exited (thread->ptid)
2620 && !is_executing (thread->ptid)
2621 && !thread->stop_requested
2622 && thread->suspend.stop_signal == GDB_SIGNAL_0)
2624 switch_to_thread (thread->ptid);
2625 clear_proceed_status (0);
2626 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2633 proceed_after_attach (int pid)
2635 /* Don't error out if the current thread is running, because
2636 there may be other stopped threads. */
2637 struct cleanup *old_chain;
2639 /* Backup current thread and selected frame. */
2640 old_chain = make_cleanup_restore_current_thread ();
2642 iterate_over_threads (proceed_after_attach_callback, &pid);
2644 /* Restore selected ptid. */
2645 do_cleanups (old_chain);
2648 /* See inferior.h. */
2651 setup_inferior (int from_tty)
2653 struct inferior *inferior;
2655 inferior = current_inferior ();
2656 inferior->needs_setup = 0;
2658 /* If no exec file is yet known, try to determine it from the
2660 if (get_exec_file (0) == NULL)
2661 exec_file_locate_attach (ptid_get_pid (inferior_ptid), from_tty);
2664 reopen_exec_file ();
2668 /* Take any necessary post-attaching actions for this platform. */
2669 target_post_attach (ptid_get_pid (inferior_ptid));
2671 post_create_inferior (¤t_target, from_tty);
2674 /* What to do after the first program stops after attaching. */
2675 enum attach_post_wait_mode
2677 /* Do nothing. Leaves threads as they are. */
2678 ATTACH_POST_WAIT_NOTHING,
2680 /* Re-resume threads that are marked running. */
2681 ATTACH_POST_WAIT_RESUME,
2683 /* Stop all threads. */
2684 ATTACH_POST_WAIT_STOP,
2687 /* Called after we've attached to a process and we've seen it stop for
2688 the first time. If ASYNC_EXEC is true, re-resume threads that
2689 should be running. Else if ATTACH, */
2692 attach_post_wait (char *args, int from_tty, enum attach_post_wait_mode mode)
2694 struct inferior *inferior;
2696 inferior = current_inferior ();
2697 inferior->control.stop_soon = NO_STOP_QUIETLY;
2699 if (inferior->needs_setup)
2700 setup_inferior (from_tty);
2702 if (mode == ATTACH_POST_WAIT_RESUME)
2704 /* The user requested an `attach&', so be sure to leave threads
2705 that didn't get a signal running. */
2707 /* Immediatelly resume all suspended threads of this inferior,
2708 and this inferior only. This should have no effect on
2709 already running threads. If a thread has been stopped with a
2710 signal, leave it be. */
2712 proceed_after_attach (inferior->pid);
2715 if (inferior_thread ()->suspend.stop_signal == GDB_SIGNAL_0)
2717 clear_proceed_status (0);
2718 proceed ((CORE_ADDR) -1, GDB_SIGNAL_DEFAULT);
2722 else if (mode == ATTACH_POST_WAIT_STOP)
2724 /* The user requested a plain `attach', so be sure to leave
2725 the inferior stopped. */
2727 /* At least the current thread is already stopped. */
2729 /* In all-stop, by definition, all threads have to be already
2730 stopped at this point. In non-stop, however, although the
2731 selected thread is stopped, others may still be executing.
2732 Be sure to explicitly stop all threads of the process. This
2733 should have no effect on already stopped threads. */
2735 target_stop (pid_to_ptid (inferior->pid));
2736 else if (target_is_non_stop_p ())
2738 struct thread_info *thread;
2739 struct thread_info *lowest = inferior_thread ();
2740 int pid = current_inferior ()->pid;
2742 stop_all_threads ();
2744 /* It's not defined which thread will report the attach
2745 stop. For consistency, always select the thread with
2746 lowest GDB number, which should be the main thread, if it
2748 ALL_NON_EXITED_THREADS (thread)
2750 if (ptid_get_pid (thread->ptid) == pid)
2752 if (thread->inf->num < lowest->inf->num
2753 || thread->per_inf_num < lowest->per_inf_num)
2758 switch_to_thread (lowest->ptid);
2761 /* Tell the user/frontend where we're stopped. */
2763 if (deprecated_attach_hook)
2764 deprecated_attach_hook ();
2768 struct attach_command_continuation_args
2772 enum attach_post_wait_mode mode;
2776 attach_command_continuation (void *args, int err)
2778 struct attach_command_continuation_args *a
2779 = (struct attach_command_continuation_args *) args;
2784 attach_post_wait (a->args, a->from_tty, a->mode);
2788 attach_command_continuation_free_args (void *args)
2790 struct attach_command_continuation_args *a
2791 = (struct attach_command_continuation_args *) args;
2797 /* "attach" command entry point. Takes a program started up outside
2798 of gdb and ``attaches'' to it. This stops it cold in its tracks
2799 and allows us to start debugging it. */
2802 attach_command (char *args, int from_tty)
2805 struct cleanup *args_chain;
2806 struct target_ops *attach_target;
2807 struct inferior *inferior = current_inferior ();
2808 enum attach_post_wait_mode mode;
2810 dont_repeat (); /* Not for the faint of heart */
2812 if (gdbarch_has_global_solist (target_gdbarch ()))
2813 /* Don't complain if all processes share the same symbol
2816 else if (target_has_execution)
2818 if (query (_("A program is being debugged already. Kill it? ")))
2821 error (_("Not killed."));
2824 /* Clean up any leftovers from other runs. Some other things from
2825 this function should probably be moved into target_pre_inferior. */
2826 target_pre_inferior (from_tty);
2828 args = strip_bg_char (args, &async_exec);
2829 args_chain = make_cleanup (xfree, args);
2831 attach_target = find_attach_target ();
2833 prepare_execution_command (attach_target, async_exec);
2835 if (non_stop && !attach_target->to_supports_non_stop (attach_target))
2836 error (_("Cannot attach to this target in non-stop mode"));
2838 attach_target->to_attach (attach_target, args, from_tty);
2839 /* to_attach should push the target, so after this point we
2840 shouldn't refer to attach_target again. */
2841 attach_target = NULL;
2843 /* Set up the "saved terminal modes" of the inferior
2844 based on what modes we are starting it with. */
2845 target_terminal_init ();
2847 /* Install inferior's terminal modes. This may look like a no-op,
2848 as we've just saved them above, however, this does more than
2849 restore terminal settings:
2851 - installs a SIGINT handler that forwards SIGINT to the inferior.
2852 Otherwise a Ctrl-C pressed just while waiting for the initial
2853 stop would end up as a spurious Quit.
2855 - removes stdin from the event loop, which we need if attaching
2856 in the foreground, otherwise on targets that report an initial
2857 stop on attach (which are most) we'd process input/commands
2858 while we're in the event loop waiting for that stop. That is,
2859 before the attach continuation runs and the command is really
2861 target_terminal_inferior ();
2863 /* Set up execution context to know that we should return from
2864 wait_for_inferior as soon as the target reports a stop. */
2865 init_wait_for_inferior ();
2866 clear_proceed_status (0);
2868 inferior->needs_setup = 1;
2870 if (target_is_non_stop_p ())
2872 /* If we find that the current thread isn't stopped, explicitly
2873 do so now, because we're going to install breakpoints and
2877 /* The user requested an `attach&'; stop just one thread. */
2878 target_stop (inferior_ptid);
2880 /* The user requested an `attach', so stop all threads of this
2882 target_stop (pid_to_ptid (ptid_get_pid (inferior_ptid)));
2885 mode = async_exec ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_STOP;
2887 /* Some system don't generate traps when attaching to inferior.
2888 E.g. Mach 3 or GNU hurd. */
2889 if (!target_attach_no_wait)
2891 struct attach_command_continuation_args *a;
2893 /* Careful here. See comments in inferior.h. Basically some
2894 OSes don't ignore SIGSTOPs on continue requests anymore. We
2895 need a way for handle_inferior_event to reset the stop_signal
2896 variable after an attach, and this is what
2897 STOP_QUIETLY_NO_SIGSTOP is for. */
2898 inferior->control.stop_soon = STOP_QUIETLY_NO_SIGSTOP;
2900 /* Wait for stop. */
2901 a = XNEW (struct attach_command_continuation_args);
2902 a->args = xstrdup (args);
2903 a->from_tty = from_tty;
2905 add_inferior_continuation (attach_command_continuation, a,
2906 attach_command_continuation_free_args);
2907 /* Done with ARGS. */
2908 do_cleanups (args_chain);
2910 if (!target_is_async_p ())
2911 mark_infrun_async_event_handler ();
2915 /* Done with ARGS. */
2916 do_cleanups (args_chain);
2918 attach_post_wait (args, from_tty, mode);
2921 /* We had just found out that the target was already attached to an
2922 inferior. PTID points at a thread of this new inferior, that is
2923 the most likely to be stopped right now, but not necessarily so.
2924 The new inferior is assumed to be already added to the inferior
2925 list at this point. If LEAVE_RUNNING, then leave the threads of
2926 this inferior running, except those we've explicitly seen reported
2930 notice_new_inferior (ptid_t ptid, int leave_running, int from_tty)
2932 struct cleanup* old_chain;
2933 enum attach_post_wait_mode mode;
2935 old_chain = make_cleanup (null_cleanup, NULL);
2937 mode = leave_running ? ATTACH_POST_WAIT_RESUME : ATTACH_POST_WAIT_NOTHING;
2939 if (!ptid_equal (inferior_ptid, null_ptid))
2940 make_cleanup_restore_current_thread ();
2942 /* Avoid reading registers -- we haven't fetched the target
2944 switch_to_thread_no_regs (find_thread_ptid (ptid));
2946 /* When we "notice" a new inferior we need to do all the things we
2947 would normally do if we had just attached to it. */
2949 if (is_executing (inferior_ptid))
2951 struct attach_command_continuation_args *a;
2952 struct inferior *inferior = current_inferior ();
2954 /* We're going to install breakpoints, and poke at memory,
2955 ensure that the inferior is stopped for a moment while we do
2957 target_stop (inferior_ptid);
2959 inferior->control.stop_soon = STOP_QUIETLY_REMOTE;
2961 /* Wait for stop before proceeding. */
2962 a = XNEW (struct attach_command_continuation_args);
2963 a->args = xstrdup ("");
2964 a->from_tty = from_tty;
2966 add_inferior_continuation (attach_command_continuation, a,
2967 attach_command_continuation_free_args);
2969 do_cleanups (old_chain);
2973 attach_post_wait ("" /* args */, from_tty, mode);
2975 do_cleanups (old_chain);
2980 * takes a program previously attached to and detaches it.
2981 * The program resumes execution and will no longer stop
2982 * on signals, etc. We better not have left any breakpoints
2983 * in the program or it'll die when it hits one. For this
2984 * to work, it may be necessary for the process to have been
2985 * previously attached. It *might* work if the program was
2986 * started via the normal ptrace (PTRACE_TRACEME).
2990 detach_command (char *args, int from_tty)
2992 dont_repeat (); /* Not for the faint of heart. */
2994 if (ptid_equal (inferior_ptid, null_ptid))
2995 error (_("The program is not being run."));
2997 query_if_trace_running (from_tty);
2999 disconnect_tracing ();
3001 target_detach (args, from_tty);
3003 /* If the solist is global across inferiors, don't clear it when we
3004 detach from a single inferior. */
3005 if (!gdbarch_has_global_solist (target_gdbarch ()))
3006 no_shared_libraries (NULL, from_tty);
3008 /* If we still have inferiors to debug, then don't mess with their
3010 if (!have_inferiors ())
3011 init_thread_list ();
3013 if (deprecated_detach_hook)
3014 deprecated_detach_hook ();
3017 /* Disconnect from the current target without resuming it (leaving it
3018 waiting for a debugger).
3020 We'd better not have left any breakpoints in the program or the
3021 next debugger will get confused. Currently only supported for some
3022 remote targets, since the normal attach mechanisms don't work on
3023 stopped processes on some native platforms (e.g. GNU/Linux). */
3026 disconnect_command (char *args, int from_tty)
3028 dont_repeat (); /* Not for the faint of heart. */
3029 query_if_trace_running (from_tty);
3030 disconnect_tracing ();
3031 target_disconnect (args, from_tty);
3032 no_shared_libraries (NULL, from_tty);
3033 init_thread_list ();
3034 if (deprecated_detach_hook)
3035 deprecated_detach_hook ();
3039 interrupt_target_1 (int all_threads)
3044 ptid = minus_one_ptid;
3046 ptid = inferior_ptid;
3051 target_interrupt (ptid);
3053 /* Tag the thread as having been explicitly requested to stop, so
3054 other parts of gdb know not to resume this thread automatically,
3055 if it was stopped due to an internal event. Limit this to
3056 non-stop mode, as when debugging a multi-threaded application in
3057 all-stop mode, we will only get one stop event --- it's undefined
3058 which thread will report the event. */
3060 set_stop_requested (ptid, 1);
3064 Stop the execution of the target while running in async mode, in
3065 the backgound. In all-stop, stop the whole process. In non-stop
3066 mode, stop the current thread only by default, or stop all threads
3067 if the `-a' switch is used. */
3070 interrupt_command (char *args, int from_tty)
3072 if (target_can_async_p ())
3074 int all_threads = 0;
3076 dont_repeat (); /* Not for the faint of heart. */
3079 && startswith (args, "-a"))
3082 if (!non_stop && all_threads)
3083 error (_("-a is meaningless in all-stop mode."));
3085 interrupt_target_1 (all_threads);
3089 /* See inferior.h. */
3092 default_print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
3093 struct frame_info *frame, const char *args)
3096 int printed_something = 0;
3099 regnum < gdbarch_num_regs (gdbarch)
3100 + gdbarch_num_pseudo_regs (gdbarch);
3103 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
3105 printed_something = 1;
3106 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
3109 if (!printed_something)
3110 fprintf_filtered (file, "No floating-point info "
3111 "available for this processor.\n");
3115 float_info (char *args, int from_tty)
3117 struct frame_info *frame;
3119 if (!target_has_registers)
3120 error (_("The program has no registers now."));
3122 frame = get_selected_frame (NULL);
3123 gdbarch_print_float_info (get_frame_arch (frame), gdb_stdout, frame, args);
3127 unset_command (char *args, int from_tty)
3129 printf_filtered (_("\"unset\" must be followed by the "
3130 "name of an unset subcommand.\n"));
3131 help_list (unsetlist, "unset ", all_commands, gdb_stdout);
3134 /* Implement `info proc' family of commands. */
3137 info_proc_cmd_1 (char *args, enum info_proc_what what, int from_tty)
3139 struct gdbarch *gdbarch = get_current_arch ();
3141 if (!target_info_proc (args, what))
3143 if (gdbarch_info_proc_p (gdbarch))
3144 gdbarch_info_proc (gdbarch, args, what);
3146 error (_("Not supported on this target."));
3150 /* Implement `info proc' when given without any futher parameters. */
3153 info_proc_cmd (char *args, int from_tty)
3155 info_proc_cmd_1 (args, IP_MINIMAL, from_tty);
3158 /* Implement `info proc mappings'. */
3161 info_proc_cmd_mappings (char *args, int from_tty)
3163 info_proc_cmd_1 (args, IP_MAPPINGS, from_tty);
3166 /* Implement `info proc stat'. */
3169 info_proc_cmd_stat (char *args, int from_tty)
3171 info_proc_cmd_1 (args, IP_STAT, from_tty);
3174 /* Implement `info proc status'. */
3177 info_proc_cmd_status (char *args, int from_tty)
3179 info_proc_cmd_1 (args, IP_STATUS, from_tty);
3182 /* Implement `info proc cwd'. */
3185 info_proc_cmd_cwd (char *args, int from_tty)
3187 info_proc_cmd_1 (args, IP_CWD, from_tty);
3190 /* Implement `info proc cmdline'. */
3193 info_proc_cmd_cmdline (char *args, int from_tty)
3195 info_proc_cmd_1 (args, IP_CMDLINE, from_tty);
3198 /* Implement `info proc exe'. */
3201 info_proc_cmd_exe (char *args, int from_tty)
3203 info_proc_cmd_1 (args, IP_EXE, from_tty);
3206 /* Implement `info proc all'. */
3209 info_proc_cmd_all (char *args, int from_tty)
3211 info_proc_cmd_1 (args, IP_ALL, from_tty);
3215 _initialize_infcmd (void)
3217 static struct cmd_list_element *info_proc_cmdlist;
3218 struct cmd_list_element *c = NULL;
3219 const char *cmd_name;
3221 /* Add the filename of the terminal connected to inferior I/O. */
3222 add_setshow_filename_cmd ("inferior-tty", class_run,
3223 &inferior_io_terminal_scratch, _("\
3224 Set terminal for future runs of program being debugged."), _("\
3225 Show terminal for future runs of program being debugged."), _("\
3226 Usage: set inferior-tty /dev/pts/1"),
3227 set_inferior_tty_command,
3228 show_inferior_tty_command,
3229 &setlist, &showlist);
3230 add_com_alias ("tty", "set inferior-tty", class_alias, 0);
3233 add_setshow_string_noescape_cmd (cmd_name, class_run,
3234 &inferior_args_scratch, _("\
3235 Set argument list to give program being debugged when it is started."), _("\
3236 Show argument list to give program being debugged when it is started."), _("\
3237 Follow this command with any number of args, to be passed to the program."),
3240 &setlist, &showlist);
3241 c = lookup_cmd (&cmd_name, setlist, "", -1, 1);
3242 gdb_assert (c != NULL);
3243 set_cmd_completer (c, filename_completer);
3245 c = add_cmd ("environment", no_class, environment_info, _("\
3246 The environment to give the program, or one variable's value.\n\
3247 With an argument VAR, prints the value of environment variable VAR to\n\
3248 give the program being debugged. With no arguments, prints the entire\n\
3249 environment to be given to the program."), &showlist);
3250 set_cmd_completer (c, noop_completer);
3252 add_prefix_cmd ("unset", no_class, unset_command,
3253 _("Complement to certain \"set\" commands."),
3254 &unsetlist, "unset ", 0, &cmdlist);
3256 c = add_cmd ("environment", class_run, unset_environment_command, _("\
3257 Cancel environment variable VAR for the program.\n\
3258 This does not affect the program until the next \"run\" command."),
3260 set_cmd_completer (c, noop_completer);
3262 c = add_cmd ("environment", class_run, set_environment_command, _("\
3263 Set environment variable value to give the program.\n\
3264 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
3265 VALUES of environment variables are uninterpreted strings.\n\
3266 This does not affect the program until the next \"run\" command."),
3268 set_cmd_completer (c, noop_completer);
3270 c = add_com ("path", class_files, path_command, _("\
3271 Add directory DIR(s) to beginning of search path for object files.\n\
3272 $cwd in the path means the current working directory.\n\
3273 This path is equivalent to the $PATH shell variable. It is a list of\n\
3274 directories, separated by colons. These directories are searched to find\n\
3275 fully linked executable files and separately compiled object files as \
3277 set_cmd_completer (c, filename_completer);
3279 c = add_cmd ("paths", no_class, path_info, _("\
3280 Current search path for finding object files.\n\
3281 $cwd in the path means the current working directory.\n\
3282 This path is equivalent to the $PATH shell variable. It is a list of\n\
3283 directories, separated by colons. These directories are searched to find\n\
3284 fully linked executable files and separately compiled object files as \
3287 set_cmd_completer (c, noop_completer);
3289 add_prefix_cmd ("kill", class_run, kill_command,
3290 _("Kill execution of program being debugged."),
3291 &killlist, "kill ", 0, &cmdlist);
3293 add_com ("attach", class_run, attach_command, _("\
3294 Attach to a process or file outside of GDB.\n\
3295 This command attaches to another target, of the same type as your last\n\
3296 \"target\" command (\"info files\" will show your target stack).\n\
3297 The command may take as argument a process id or a device file.\n\
3298 For a process id, you must have permission to send the process a signal,\n\
3299 and it must have the same effective uid as the debugger.\n\
3300 When using \"attach\" with a process id, the debugger finds the\n\
3301 program running in the process, looking first in the current working\n\
3302 directory, or (if not found there) using the source file search path\n\
3303 (see the \"directory\" command). You can also use the \"file\" command\n\
3304 to specify the program, and to load its symbol table."));
3306 add_prefix_cmd ("detach", class_run, detach_command, _("\
3307 Detach a process or file previously attached.\n\
3308 If a process, it is no longer traced, and it continues its execution. If\n\
3309 you were debugging a file, the file is closed and gdb no longer accesses it."),
3310 &detachlist, "detach ", 0, &cmdlist);
3312 add_com ("disconnect", class_run, disconnect_command, _("\
3313 Disconnect from a target.\n\
3314 The target will wait for another debugger to connect. Not available for\n\
3317 c = add_com ("signal", class_run, signal_command, _("\
3318 Continue program with the specified signal.\n\
3319 Usage: signal SIGNAL\n\
3320 The SIGNAL argument is processed the same as the handle command.\n\
3322 An argument of \"0\" means continue the program without sending it a signal.\n\
3323 This is useful in cases where the program stopped because of a signal,\n\
3324 and you want to resume the program while discarding the signal.\n\
3326 In a multi-threaded program the signal is delivered to, or discarded from,\n\
3327 the current thread only."));
3328 set_cmd_completer (c, signal_completer);
3330 c = add_com ("queue-signal", class_run, queue_signal_command, _("\
3331 Queue a signal to be delivered to the current thread when it is resumed.\n\
3332 Usage: queue-signal SIGNAL\n\
3333 The SIGNAL argument is processed the same as the handle command.\n\
3334 It is an error if the handling state of SIGNAL is \"nopass\".\n\
3336 An argument of \"0\" means remove any currently queued signal from\n\
3337 the current thread. This is useful in cases where the program stopped\n\
3338 because of a signal, and you want to resume it while discarding the signal.\n\
3340 In a multi-threaded program the signal is queued with, or discarded from,\n\
3341 the current thread only."));
3342 set_cmd_completer (c, signal_completer);
3344 add_com ("stepi", class_run, stepi_command, _("\
3345 Step one instruction exactly.\n\
3347 Argument N means step N times (or till program stops for another \
3349 add_com_alias ("si", "stepi", class_alias, 0);
3351 add_com ("nexti", class_run, nexti_command, _("\
3352 Step one instruction, but proceed through subroutine calls.\n\
3354 Argument N means step N times (or till program stops for another \
3356 add_com_alias ("ni", "nexti", class_alias, 0);
3358 add_com ("finish", class_run, finish_command, _("\
3359 Execute until selected stack frame returns.\n\
3361 Upon return, the value returned is printed and put in the value history."));
3362 add_com_alias ("fin", "finish", class_run, 1);
3364 add_com ("next", class_run, next_command, _("\
3365 Step program, proceeding through subroutine calls.\n\
3367 Unlike \"step\", if the current source line calls a subroutine,\n\
3368 this command does not enter the subroutine, but instead steps over\n\
3369 the call, in effect treating it as a single source line."));
3370 add_com_alias ("n", "next", class_run, 1);
3372 add_com ("step", class_run, step_command, _("\
3373 Step program until it reaches a different source line.\n\
3375 Argument N means step N times (or till program stops for another \
3377 add_com_alias ("s", "step", class_run, 1);
3379 c = add_com ("until", class_run, until_command, _("\
3380 Execute until the program reaches a source line greater than the current\n\
3381 or a specified location (same args as break command) within the current \
3383 set_cmd_completer (c, location_completer);
3384 add_com_alias ("u", "until", class_run, 1);
3386 c = add_com ("advance", class_run, advance_command, _("\
3387 Continue the program up to the given location (same form as args for break \
3389 Execution will also stop upon exit from the current stack frame."));
3390 set_cmd_completer (c, location_completer);
3392 c = add_com ("jump", class_run, jump_command, _("\
3393 Continue program being debugged at specified line or address.\n\
3394 Usage: jump <location>\n\
3395 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
3396 for an address to start at."));
3397 set_cmd_completer (c, location_completer);
3398 add_com_alias ("j", "jump", class_run, 1);
3400 add_com ("continue", class_run, continue_command, _("\
3401 Continue program being debugged, after signal or breakpoint.\n\
3402 Usage: continue [N]\n\
3403 If proceeding from breakpoint, a number N may be used as an argument,\n\
3404 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
3405 the breakpoint won't break until the Nth time it is reached).\n\
3407 If non-stop mode is enabled, continue only the current thread,\n\
3408 otherwise all the threads in the program are continued. To \n\
3409 continue all stopped threads in non-stop mode, use the -a option.\n\
3410 Specifying -a and an ignore count simultaneously is an error."));
3411 add_com_alias ("c", "cont", class_run, 1);
3412 add_com_alias ("fg", "cont", class_run, 1);
3414 c = add_com ("run", class_run, run_command, _("\
3415 Start debugged program. You may specify arguments to give it.\n\
3416 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
3417 Input and output redirection with \">\", \"<\", or \">>\" are also \
3419 With no arguments, uses arguments last specified (with \"run\" \
3420 or \"set args\").\n\
3421 To cancel previous arguments and run with no arguments,\n\
3422 use \"set args\" without arguments."));
3423 set_cmd_completer (c, filename_completer);
3424 add_com_alias ("r", "run", class_run, 1);
3426 c = add_com ("start", class_run, start_command, _("\
3427 Run the debugged program until the beginning of the main procedure.\n\
3428 You may specify arguments to give to your program, just as with the\n\
3429 \"run\" command."));
3430 set_cmd_completer (c, filename_completer);
3432 add_com ("interrupt", class_run, interrupt_command,
3433 _("Interrupt the execution of the debugged program.\n\
3434 If non-stop mode is enabled, interrupt only the current thread,\n\
3435 otherwise all the threads in the program are stopped. To \n\
3436 interrupt all running threads in non-stop mode, use the -a option."));
3438 c = add_info ("registers", nofp_registers_info, _("\
3439 List of integer registers and their contents, for selected stack frame.\n\
3440 Register name as argument means describe only that register."));
3441 add_info_alias ("r", "registers", 1);
3442 set_cmd_completer (c, reg_or_group_completer);
3444 c = add_info ("all-registers", all_registers_info, _("\
3445 List of all registers and their contents, for selected stack frame.\n\
3446 Register name as argument means describe only that register."));
3447 set_cmd_completer (c, reg_or_group_completer);
3449 add_info ("program", program_info,
3450 _("Execution status of the program."));
3452 add_info ("float", float_info,
3453 _("Print the status of the floating point unit\n"));
3455 add_info ("vector", vector_info,
3456 _("Print the status of the vector unit\n"));
3458 add_prefix_cmd ("proc", class_info, info_proc_cmd,
3460 Show /proc process information about any running process.\n\
3461 Specify any process id, or use the program being debugged by default."),
3462 &info_proc_cmdlist, "info proc ",
3463 1/*allow-unknown*/, &infolist);
3465 add_cmd ("mappings", class_info, info_proc_cmd_mappings, _("\
3466 List of mapped memory regions."),
3467 &info_proc_cmdlist);
3469 add_cmd ("stat", class_info, info_proc_cmd_stat, _("\
3470 List process info from /proc/PID/stat."),
3471 &info_proc_cmdlist);
3473 add_cmd ("status", class_info, info_proc_cmd_status, _("\
3474 List process info from /proc/PID/status."),
3475 &info_proc_cmdlist);
3477 add_cmd ("cwd", class_info, info_proc_cmd_cwd, _("\
3478 List current working directory of the process."),
3479 &info_proc_cmdlist);
3481 add_cmd ("cmdline", class_info, info_proc_cmd_cmdline, _("\
3482 List command line arguments of the process."),
3483 &info_proc_cmdlist);
3485 add_cmd ("exe", class_info, info_proc_cmd_exe, _("\
3486 List absolute filename for executable of the process."),
3487 &info_proc_cmdlist);
3489 add_cmd ("all", class_info, info_proc_cmd_all, _("\
3490 List all available /proc info."),
3491 &info_proc_cmdlist);