b1ccf9f99588474956d1a14b85e059935ae4fc45
[deliverable/binutils-gdb.git] / gdb / infcmd.c
1 /* Memory-access and commands for "inferior" process, for GDB.
2
3 Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
4 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
5 2008, 2009 Free Software Foundation, Inc.
6
7 This file is part of GDB.
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include "defs.h"
23 #include <signal.h>
24 #include "gdb_string.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "frame.h"
28 #include "inferior.h"
29 #include "environ.h"
30 #include "value.h"
31 #include "gdbcmd.h"
32 #include "symfile.h"
33 #include "gdbcore.h"
34 #include "target.h"
35 #include "language.h"
36 #include "symfile.h"
37 #include "objfiles.h"
38 #include "completer.h"
39 #include "ui-out.h"
40 #include "event-top.h"
41 #include "parser-defs.h"
42 #include "regcache.h"
43 #include "reggroups.h"
44 #include "block.h"
45 #include "solib.h"
46 #include <ctype.h>
47 #include "gdb_assert.h"
48 #include "observer.h"
49 #include "target-descriptions.h"
50 #include "user-regs.h"
51 #include "exceptions.h"
52 #include "cli/cli-decode.h"
53 #include "gdbthread.h"
54 #include "valprint.h"
55 #include "inline-frame.h"
56
57 /* Functions exported for general use, in inferior.h: */
58
59 void all_registers_info (char *, int);
60
61 void registers_info (char *, int);
62
63 void nexti_command (char *, int);
64
65 void stepi_command (char *, int);
66
67 void continue_command (char *, int);
68
69 void interrupt_target_command (char *args, int from_tty);
70
71 /* Local functions: */
72
73 static void nofp_registers_info (char *, int);
74
75 static void print_return_value (struct type *func_type,
76 struct type *value_type);
77
78 static void until_next_command (int);
79
80 static void until_command (char *, int);
81
82 static void path_info (char *, int);
83
84 static void path_command (char *, int);
85
86 static void unset_command (char *, int);
87
88 static void float_info (char *, int);
89
90 static void disconnect_command (char *, int);
91
92 static void unset_environment_command (char *, int);
93
94 static void set_environment_command (char *, int);
95
96 static void environment_info (char *, int);
97
98 static void program_info (char *, int);
99
100 static void finish_command (char *, int);
101
102 static void signal_command (char *, int);
103
104 static void jump_command (char *, int);
105
106 static void step_1 (int, int, char *);
107 static void step_once (int skip_subroutines, int single_inst, int count, int thread);
108
109 static void next_command (char *, int);
110
111 static void step_command (char *, int);
112
113 static void run_command (char *, int);
114
115 static void run_no_args_command (char *args, int from_tty);
116
117 static void go_command (char *line_no, int from_tty);
118
119 static int strip_bg_char (char **);
120
121 void _initialize_infcmd (void);
122
123 #define ERROR_NO_INFERIOR \
124 if (!target_has_execution) error (_("The program is not being run."));
125
126 /* String containing arguments to give to the program, separated by spaces.
127 Empty string (pointer to '\0') means no args. */
128
129 static char *inferior_args;
130
131 /* The inferior arguments as a vector. If INFERIOR_ARGC is nonzero,
132 then we must compute INFERIOR_ARGS from this (via the target). */
133
134 static int inferior_argc;
135 static char **inferior_argv;
136
137 /* File name for default use for standard in/out in the inferior. */
138
139 static char *inferior_io_terminal;
140
141 /* Pid of our debugged inferior, or 0 if no inferior now.
142 Since various parts of infrun.c test this to see whether there is a program
143 being debugged it should be nonzero (currently 3 is used) for remote
144 debugging. */
145
146 ptid_t inferior_ptid;
147
148 /* Address at which inferior stopped. */
149
150 CORE_ADDR stop_pc;
151
152 /* Flag indicating that a command has proceeded the inferior past the
153 current breakpoint. */
154
155 int breakpoint_proceeded;
156
157 /* Nonzero if stopped due to completion of a stack dummy routine. */
158
159 int stop_stack_dummy;
160
161 /* Nonzero if stopped due to a random (unexpected) signal in inferior
162 process. */
163
164 int stopped_by_random_signal;
165
166 /* Environment to use for running inferior,
167 in format described in environ.h. */
168
169 struct gdb_environ *inferior_environ;
170 \f
171 /* Accessor routines. */
172
173 void
174 set_inferior_io_terminal (const char *terminal_name)
175 {
176 if (inferior_io_terminal)
177 xfree (inferior_io_terminal);
178
179 if (!terminal_name)
180 inferior_io_terminal = NULL;
181 else
182 inferior_io_terminal = xstrdup (terminal_name);
183 }
184
185 const char *
186 get_inferior_io_terminal (void)
187 {
188 return inferior_io_terminal;
189 }
190
191 char *
192 get_inferior_args (void)
193 {
194 if (inferior_argc != 0)
195 {
196 char *n, *old;
197
198 n = construct_inferior_arguments (inferior_argc, inferior_argv);
199 old = set_inferior_args (n);
200 xfree (old);
201 }
202
203 if (inferior_args == NULL)
204 inferior_args = xstrdup ("");
205
206 return inferior_args;
207 }
208
209 char *
210 set_inferior_args (char *newargs)
211 {
212 char *saved_args = inferior_args;
213
214 inferior_args = newargs;
215 inferior_argc = 0;
216 inferior_argv = 0;
217
218 return saved_args;
219 }
220
221 void
222 set_inferior_args_vector (int argc, char **argv)
223 {
224 inferior_argc = argc;
225 inferior_argv = argv;
226 }
227
228 /* Notice when `set args' is run. */
229 static void
230 notice_args_set (char *args, int from_tty, struct cmd_list_element *c)
231 {
232 inferior_argc = 0;
233 inferior_argv = 0;
234 }
235
236 /* Notice when `show args' is run. */
237 static void
238 notice_args_read (struct ui_file *file, int from_tty,
239 struct cmd_list_element *c, const char *value)
240 {
241 /* Note that we ignore the passed-in value in favor of computing it
242 directly. */
243 deprecated_show_value_hack (file, from_tty, c, get_inferior_args ());
244 }
245
246 \f
247 /* Compute command-line string given argument vector. This does the
248 same shell processing as fork_inferior. */
249 char *
250 construct_inferior_arguments (int argc, char **argv)
251 {
252 char *result;
253
254 if (STARTUP_WITH_SHELL)
255 {
256 /* This holds all the characters considered special to the
257 typical Unix shells. We include `^' because the SunOS
258 /bin/sh treats it as a synonym for `|'. */
259 char *special = "\"!#$&*()\\|[]{}<>?'\"`~^; \t\n";
260 int i;
261 int length = 0;
262 char *out, *cp;
263
264 /* We over-compute the size. It shouldn't matter. */
265 for (i = 0; i < argc; ++i)
266 length += 3 * strlen (argv[i]) + 1 + 2 * (argv[i][0] == '\0');
267
268 result = (char *) xmalloc (length);
269 out = result;
270
271 for (i = 0; i < argc; ++i)
272 {
273 if (i > 0)
274 *out++ = ' ';
275
276 /* Need to handle empty arguments specially. */
277 if (argv[i][0] == '\0')
278 {
279 *out++ = '\'';
280 *out++ = '\'';
281 }
282 else
283 {
284 for (cp = argv[i]; *cp; ++cp)
285 {
286 if (*cp == '\n')
287 {
288 /* A newline cannot be quoted with a backslash (it
289 just disappears), only by putting it inside
290 quotes. */
291 *out++ = '\'';
292 *out++ = '\n';
293 *out++ = '\'';
294 }
295 else
296 {
297 if (strchr (special, *cp) != NULL)
298 *out++ = '\\';
299 *out++ = *cp;
300 }
301 }
302 }
303 }
304 *out = '\0';
305 }
306 else
307 {
308 /* In this case we can't handle arguments that contain spaces,
309 tabs, or newlines -- see breakup_args(). */
310 int i;
311 int length = 0;
312
313 for (i = 0; i < argc; ++i)
314 {
315 char *cp = strchr (argv[i], ' ');
316 if (cp == NULL)
317 cp = strchr (argv[i], '\t');
318 if (cp == NULL)
319 cp = strchr (argv[i], '\n');
320 if (cp != NULL)
321 error (_("can't handle command-line argument containing whitespace"));
322 length += strlen (argv[i]) + 1;
323 }
324
325 result = (char *) xmalloc (length);
326 result[0] = '\0';
327 for (i = 0; i < argc; ++i)
328 {
329 if (i > 0)
330 strcat (result, " ");
331 strcat (result, argv[i]);
332 }
333 }
334
335 return result;
336 }
337 \f
338
339 /* This function detects whether or not a '&' character (indicating
340 background execution) has been added as *the last* of the arguments ARGS
341 of a command. If it has, it removes it and returns 1. Otherwise it
342 does nothing and returns 0. */
343 static int
344 strip_bg_char (char **args)
345 {
346 char *p = NULL;
347
348 p = strchr (*args, '&');
349
350 if (p)
351 {
352 if (p == (*args + strlen (*args) - 1))
353 {
354 if (strlen (*args) > 1)
355 {
356 do
357 p--;
358 while (*p == ' ' || *p == '\t');
359 *(p + 1) = '\0';
360 }
361 else
362 *args = 0;
363 return 1;
364 }
365 }
366 return 0;
367 }
368
369 void
370 tty_command (char *file, int from_tty)
371 {
372 if (file == 0)
373 error_no_arg (_("terminal name for running target process"));
374
375 set_inferior_io_terminal (file);
376 }
377
378 /* Common actions to take after creating any sort of inferior, by any
379 means (running, attaching, connecting, et cetera). The target
380 should be stopped. */
381
382 void
383 post_create_inferior (struct target_ops *target, int from_tty)
384 {
385 /* Be sure we own the terminal in case write operations are performed. */
386 target_terminal_ours ();
387
388 /* If the target hasn't taken care of this already, do it now.
389 Targets which need to access registers during to_open,
390 to_create_inferior, or to_attach should do it earlier; but many
391 don't need to. */
392 target_find_description ();
393
394 /* Now that we know the register layout, retrieve current PC. */
395 stop_pc = regcache_read_pc (get_current_regcache ());
396
397 /* If the solist is global across processes, there's no need to
398 refetch it here. */
399 if (exec_bfd && !gdbarch_has_global_solist (target_gdbarch))
400 {
401 /* Sometimes the platform-specific hook loads initial shared
402 libraries, and sometimes it doesn't. Try to do so first, so
403 that we can add them with the correct value for FROM_TTY.
404 If we made all the inferior hook methods consistent,
405 this call could be removed. */
406 #ifdef SOLIB_ADD
407 SOLIB_ADD (NULL, from_tty, target, auto_solib_add);
408 #else
409 solib_add (NULL, from_tty, target, auto_solib_add);
410 #endif
411 }
412
413 if (exec_bfd)
414 {
415 /* Create the hooks to handle shared library load and unload
416 events. */
417 #ifdef SOLIB_CREATE_INFERIOR_HOOK
418 SOLIB_CREATE_INFERIOR_HOOK (PIDGET (inferior_ptid));
419 #else
420 solib_create_inferior_hook ();
421 #endif
422 }
423
424 /* If the user sets watchpoints before execution having started,
425 then she gets software watchpoints, because GDB can't know which
426 target will end up being pushed, or if it supports hardware
427 watchpoints or not. breakpoint_re_set takes care of promoting
428 watchpoints to hardware watchpoints if possible, however, if this
429 new inferior doesn't load shared libraries or we don't pull in
430 symbols from any other source on this target/arch,
431 breakpoint_re_set is never called. Call it now so that software
432 watchpoints get a chance to be promoted to hardware watchpoints
433 if the now pushed target supports hardware watchpoints. */
434 breakpoint_re_set ();
435
436 observer_notify_inferior_created (target, from_tty);
437 }
438
439 /* Kill the inferior if already running. This function is designed
440 to be called when we are about to start the execution of the program
441 from the beginning. Ask the user to confirm that he wants to restart
442 the program being debugged when FROM_TTY is non-null. */
443
444 static void
445 kill_if_already_running (int from_tty)
446 {
447 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
448 {
449 /* Bail out before killing the program if we will not be able to
450 restart it. */
451 target_require_runnable ();
452
453 if (from_tty
454 && !query (_("The program being debugged has been started already.\n\
455 Start it from the beginning? ")))
456 error (_("Program not restarted."));
457 target_kill ();
458 }
459 }
460
461 /* Implement the "run" command. If TBREAK_AT_MAIN is set, then insert
462 a temporary breakpoint at the begining of the main program before
463 running the program. */
464
465 static void
466 run_command_1 (char *args, int from_tty, int tbreak_at_main)
467 {
468 char *exec_file;
469 struct cleanup *old_chain;
470 ptid_t ptid;
471
472 dont_repeat ();
473
474 kill_if_already_running (from_tty);
475
476 init_wait_for_inferior ();
477 clear_breakpoint_hit_counts ();
478
479 /* Clean up any leftovers from other runs. Some other things from
480 this function should probably be moved into target_pre_inferior. */
481 target_pre_inferior (from_tty);
482
483 /* The comment here used to read, "The exec file is re-read every
484 time we do a generic_mourn_inferior, so we just have to worry
485 about the symbol file." The `generic_mourn_inferior' function
486 gets called whenever the program exits. However, suppose the
487 program exits, and *then* the executable file changes? We need
488 to check again here. Since reopen_exec_file doesn't do anything
489 if the timestamp hasn't changed, I don't see the harm. */
490 reopen_exec_file ();
491 reread_symbols ();
492
493 /* Insert the temporary breakpoint if a location was specified. */
494 if (tbreak_at_main)
495 tbreak_command (main_name (), 0);
496
497 exec_file = (char *) get_exec_file (0);
498
499 if (non_stop && !target_supports_non_stop ())
500 error (_("The target does not support running in non-stop mode."));
501
502 /* We keep symbols from add-symbol-file, on the grounds that the
503 user might want to add some symbols before running the program
504 (right?). But sometimes (dynamic loading where the user manually
505 introduces the new symbols with add-symbol-file), the code which
506 the symbols describe does not persist between runs. Currently
507 the user has to manually nuke all symbols between runs if they
508 want them to go away (PR 2207). This is probably reasonable. */
509
510 if (!args)
511 {
512 if (target_can_async_p ())
513 async_disable_stdin ();
514 }
515 else
516 {
517 int async_exec = strip_bg_char (&args);
518
519 /* If we get a request for running in the bg but the target
520 doesn't support it, error out. */
521 if (async_exec && !target_can_async_p ())
522 error (_("Asynchronous execution not supported on this target."));
523
524 /* If we don't get a request of running in the bg, then we need
525 to simulate synchronous (fg) execution. */
526 if (!async_exec && target_can_async_p ())
527 {
528 /* Simulate synchronous execution */
529 async_disable_stdin ();
530 }
531
532 /* If there were other args, beside '&', process them. */
533 if (args)
534 {
535 char *old_args = set_inferior_args (xstrdup (args));
536 xfree (old_args);
537 }
538 }
539
540 if (from_tty)
541 {
542 ui_out_field_string (uiout, NULL, "Starting program");
543 ui_out_text (uiout, ": ");
544 if (exec_file)
545 ui_out_field_string (uiout, "execfile", exec_file);
546 ui_out_spaces (uiout, 1);
547 /* We call get_inferior_args() because we might need to compute
548 the value now. */
549 ui_out_field_string (uiout, "infargs", get_inferior_args ());
550 ui_out_text (uiout, "\n");
551 ui_out_flush (uiout);
552 }
553
554 /* We call get_inferior_args() because we might need to compute
555 the value now. */
556 target_create_inferior (exec_file, get_inferior_args (),
557 environ_vector (inferior_environ), from_tty);
558
559 /* We're starting off a new process. When we get out of here, in
560 non-stop mode, finish the state of all threads of that process,
561 but leave other threads alone, as they may be stopped in internal
562 events --- the frontend shouldn't see them as stopped. In
563 all-stop, always finish the state of all threads, as we may be
564 resuming more than just the new process. */
565 if (non_stop)
566 ptid = pid_to_ptid (ptid_get_pid (inferior_ptid));
567 else
568 ptid = minus_one_ptid;
569 old_chain = make_cleanup (finish_thread_state_cleanup, &ptid);
570
571 /* Pass zero for FROM_TTY, because at this point the "run" command
572 has done its thing; now we are setting up the running program. */
573 post_create_inferior (&current_target, 0);
574
575 /* Start the target running. */
576 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_0, 0);
577
578 /* Since there was no error, there's no need to finish the thread
579 states here. */
580 discard_cleanups (old_chain);
581 }
582
583 static void
584 run_command (char *args, int from_tty)
585 {
586 run_command_1 (args, from_tty, 0);
587 }
588
589 static void
590 run_no_args_command (char *args, int from_tty)
591 {
592 char *old_args = set_inferior_args (xstrdup (""));
593 xfree (old_args);
594 }
595 \f
596
597 /* Start the execution of the program up until the beginning of the main
598 program. */
599
600 static void
601 start_command (char *args, int from_tty)
602 {
603 /* Some languages such as Ada need to search inside the program
604 minimal symbols for the location where to put the temporary
605 breakpoint before starting. */
606 if (!have_minimal_symbols ())
607 error (_("No symbol table loaded. Use the \"file\" command."));
608
609 /* Run the program until reaching the main procedure... */
610 run_command_1 (args, from_tty, 1);
611 }
612
613 static int
614 proceed_thread_callback (struct thread_info *thread, void *arg)
615 {
616 /* We go through all threads individually instead of compressing
617 into a single target `resume_all' request, because some threads
618 may be stopped in internal breakpoints/events, or stopped waiting
619 for its turn in the displaced stepping queue (that is, they are
620 running && !executing). The target side has no idea about why
621 the thread is stopped, so a `resume_all' command would resume too
622 much. If/when GDB gains a way to tell the target `hold this
623 thread stopped until I say otherwise', then we can optimize
624 this. */
625 if (!is_stopped (thread->ptid))
626 return 0;
627
628 switch_to_thread (thread->ptid);
629 clear_proceed_status ();
630 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
631 return 0;
632 }
633
634 void
635 ensure_valid_thread (void)
636 {
637 if (ptid_equal (inferior_ptid, null_ptid)
638 || is_exited (inferior_ptid))
639 error (_("\
640 Cannot execute this command without a live selected thread."));
641 }
642
643 void
644 continue_1 (int all_threads)
645 {
646 ERROR_NO_INFERIOR;
647
648 if (non_stop && all_threads)
649 {
650 /* Don't error out if the current thread is running, because
651 there may be other stopped threads. */
652 struct cleanup *old_chain;
653
654 /* Backup current thread and selected frame. */
655 old_chain = make_cleanup_restore_current_thread ();
656
657 iterate_over_threads (proceed_thread_callback, NULL);
658
659 /* Restore selected ptid. */
660 do_cleanups (old_chain);
661 }
662 else
663 {
664 ensure_valid_thread ();
665 ensure_not_running ();
666 clear_proceed_status ();
667 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
668 }
669 }
670
671 /* continue [-a] [proceed-count] [&] */
672 void
673 continue_command (char *args, int from_tty)
674 {
675 int async_exec = 0;
676 int all_threads = 0;
677 ERROR_NO_INFERIOR;
678
679 /* Find out whether we must run in the background. */
680 if (args != NULL)
681 async_exec = strip_bg_char (&args);
682
683 /* If we must run in the background, but the target can't do it,
684 error out. */
685 if (async_exec && !target_can_async_p ())
686 error (_("Asynchronous execution not supported on this target."));
687
688 /* If we are not asked to run in the bg, then prepare to run in the
689 foreground, synchronously. */
690 if (!async_exec && target_can_async_p ())
691 {
692 /* Simulate synchronous execution */
693 async_disable_stdin ();
694 }
695
696 if (args != NULL)
697 {
698 if (strncmp (args, "-a", sizeof ("-a") - 1) == 0)
699 {
700 all_threads = 1;
701 args += sizeof ("-a") - 1;
702 if (*args == '\0')
703 args = NULL;
704 }
705 }
706
707 if (!non_stop && all_threads)
708 error (_("`-a' is meaningless in all-stop mode."));
709
710 if (args != NULL && all_threads)
711 error (_("\
712 Can't resume all threads and specify proceed count simultaneously."));
713
714 /* If we have an argument left, set proceed count of breakpoint we
715 stopped at. */
716 if (args != NULL)
717 {
718 bpstat bs = NULL;
719 int num, stat;
720 int stopped = 0;
721 struct thread_info *tp;
722
723 if (non_stop)
724 tp = find_thread_ptid (inferior_ptid);
725 else
726 {
727 ptid_t last_ptid;
728 struct target_waitstatus ws;
729
730 get_last_target_status (&last_ptid, &ws);
731 tp = find_thread_ptid (last_ptid);
732 }
733 if (tp != NULL)
734 bs = tp->stop_bpstat;
735
736 while ((stat = bpstat_num (&bs, &num)) != 0)
737 if (stat > 0)
738 {
739 set_ignore_count (num,
740 parse_and_eval_long (args) - 1,
741 from_tty);
742 /* set_ignore_count prints a message ending with a period.
743 So print two spaces before "Continuing.". */
744 if (from_tty)
745 printf_filtered (" ");
746 stopped = 1;
747 }
748
749 if (!stopped && from_tty)
750 {
751 printf_filtered
752 ("Not stopped at any breakpoint; argument ignored.\n");
753 }
754 }
755
756 if (from_tty)
757 printf_filtered (_("Continuing.\n"));
758
759 continue_1 (all_threads);
760 }
761 \f
762 /* Record the starting point of a "step" or "next" command. */
763
764 static void
765 set_step_frame (void)
766 {
767 struct symtab_and_line sal;
768
769 find_frame_sal (get_current_frame (), &sal);
770 set_step_info (get_current_frame (), sal);
771 }
772
773 /* Step until outside of current statement. */
774
775 static void
776 step_command (char *count_string, int from_tty)
777 {
778 step_1 (0, 0, count_string);
779 }
780
781 /* Likewise, but skip over subroutine calls as if single instructions. */
782
783 static void
784 next_command (char *count_string, int from_tty)
785 {
786 step_1 (1, 0, count_string);
787 }
788
789 /* Likewise, but step only one instruction. */
790
791 void
792 stepi_command (char *count_string, int from_tty)
793 {
794 step_1 (0, 1, count_string);
795 }
796
797 void
798 nexti_command (char *count_string, int from_tty)
799 {
800 step_1 (1, 1, count_string);
801 }
802
803 static void
804 delete_longjmp_breakpoint_cleanup (void *arg)
805 {
806 int thread = * (int *) arg;
807 delete_longjmp_breakpoint (thread);
808 }
809
810 static void
811 step_1 (int skip_subroutines, int single_inst, char *count_string)
812 {
813 int count = 1;
814 struct frame_info *frame;
815 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
816 int async_exec = 0;
817 int thread = -1;
818
819 ERROR_NO_INFERIOR;
820 ensure_valid_thread ();
821 ensure_not_running ();
822
823 if (count_string)
824 async_exec = strip_bg_char (&count_string);
825
826 /* If we get a request for running in the bg but the target
827 doesn't support it, error out. */
828 if (async_exec && !target_can_async_p ())
829 error (_("Asynchronous execution not supported on this target."));
830
831 /* If we don't get a request of running in the bg, then we need
832 to simulate synchronous (fg) execution. */
833 if (!async_exec && target_can_async_p ())
834 {
835 /* Simulate synchronous execution */
836 async_disable_stdin ();
837 }
838
839 count = count_string ? parse_and_eval_long (count_string) : 1;
840
841 if (!single_inst || skip_subroutines) /* leave si command alone */
842 {
843 if (in_thread_list (inferior_ptid))
844 thread = pid_to_thread_id (inferior_ptid);
845
846 set_longjmp_breakpoint (thread);
847
848 make_cleanup (delete_longjmp_breakpoint_cleanup, &thread);
849 }
850
851 /* In synchronous case, all is well; each step_once call will step once. */
852 if (!target_can_async_p ())
853 {
854 for (; count > 0; count--)
855 {
856 struct thread_info *tp;
857 step_once (skip_subroutines, single_inst, count, thread);
858
859 if (target_has_execution
860 && !ptid_equal (inferior_ptid, null_ptid))
861 tp = inferior_thread ();
862 else
863 tp = NULL;
864
865 if (!tp || !tp->stop_step || !tp->step_multi)
866 {
867 /* If we stopped for some reason that is not stepping
868 there are no further steps to make. */
869 if (tp)
870 tp->step_multi = 0;
871 break;
872 }
873 }
874
875 do_cleanups (cleanups);
876 }
877 else
878 {
879 /* In the case of an asynchronous target things get complicated;
880 do only one step for now, before returning control to the
881 event loop. Let the continuation figure out how many other
882 steps we need to do, and handle them one at the time, through
883 step_once. */
884 step_once (skip_subroutines, single_inst, count, thread);
885
886 /* We are running, and the continuation is installed. It will
887 disable the longjmp breakpoint as appropriate. */
888 discard_cleanups (cleanups);
889 }
890 }
891
892 struct step_1_continuation_args
893 {
894 int count;
895 int skip_subroutines;
896 int single_inst;
897 int thread;
898 };
899
900 /* Called after we are done with one step operation, to check whether
901 we need to step again, before we print the prompt and return control
902 to the user. If count is > 1, we will need to do one more call to
903 proceed(), via step_once(). Basically it is like step_once and
904 step_1_continuation are co-recursive. */
905 static void
906 step_1_continuation (void *args)
907 {
908 struct step_1_continuation_args *a = args;
909
910 if (target_has_execution)
911 {
912 struct thread_info *tp;
913
914 tp = inferior_thread ();
915 if (tp->step_multi && tp->stop_step)
916 {
917 /* There are more steps to make, and we did stop due to
918 ending a stepping range. Do another step. */
919 step_once (a->skip_subroutines, a->single_inst,
920 a->count - 1, a->thread);
921 return;
922 }
923 tp->step_multi = 0;
924 }
925
926 /* We either stopped for some reason that is not stepping, or there
927 are no further steps to make. Cleanup. */
928 if (!a->single_inst || a->skip_subroutines)
929 delete_longjmp_breakpoint (a->thread);
930 }
931
932 /* Do just one step operation. This is useful to implement the 'step
933 n' kind of commands. In case of asynchronous targets, we will have
934 to set up a continuation to be done after the target stops (after
935 this one step). For synch targets, the caller handles further
936 stepping. */
937
938 static void
939 step_once (int skip_subroutines, int single_inst, int count, int thread)
940 {
941 struct frame_info *frame = get_current_frame ();
942
943 if (count > 0)
944 {
945 /* Don't assume THREAD is a valid thread id. It is set to -1 if
946 the longjmp breakpoint was not required. Use the
947 INFERIOR_PTID thread instead, which is the same thread when
948 THREAD is set. */
949 struct thread_info *tp = inferior_thread ();
950 clear_proceed_status ();
951 set_step_frame ();
952
953 if (!single_inst)
954 {
955 CORE_ADDR pc;
956
957 /* Step at an inlined function behaves like "down". */
958 if (!skip_subroutines && !single_inst
959 && inline_skipped_frames (inferior_ptid))
960 {
961 step_into_inline_frame (inferior_ptid);
962 if (count > 1)
963 step_once (skip_subroutines, single_inst, count - 1, thread);
964 else
965 /* Pretend that we've stopped. */
966 normal_stop ();
967 return;
968 }
969
970 pc = get_frame_pc (frame);
971 find_pc_line_pc_range (pc,
972 &tp->step_range_start, &tp->step_range_end);
973
974 /* If we have no line info, switch to stepi mode. */
975 if (tp->step_range_end == 0 && step_stop_if_no_debug)
976 tp->step_range_start = tp->step_range_end = 1;
977 else if (tp->step_range_end == 0)
978 {
979 char *name;
980 if (find_pc_partial_function (pc, &name,
981 &tp->step_range_start,
982 &tp->step_range_end) == 0)
983 error (_("Cannot find bounds of current function"));
984
985 target_terminal_ours ();
986 printf_filtered (_("\
987 Single stepping until exit from function %s, \n\
988 which has no line number information.\n"), name);
989 }
990 }
991 else
992 {
993 /* Say we are stepping, but stop after one insn whatever it does. */
994 tp->step_range_start = tp->step_range_end = 1;
995 if (!skip_subroutines)
996 /* It is stepi.
997 Don't step over function calls, not even to functions lacking
998 line numbers. */
999 tp->step_over_calls = STEP_OVER_NONE;
1000 }
1001
1002 if (skip_subroutines)
1003 tp->step_over_calls = STEP_OVER_ALL;
1004
1005 tp->step_multi = (count > 1);
1006 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
1007
1008 /* For async targets, register a continuation to do any
1009 additional steps. For sync targets, the caller will handle
1010 further stepping. */
1011 if (target_can_async_p ())
1012 {
1013 struct step_1_continuation_args *args;
1014
1015 args = xmalloc (sizeof (*args));
1016 args->skip_subroutines = skip_subroutines;
1017 args->single_inst = single_inst;
1018 args->count = count;
1019 args->thread = thread;
1020
1021 add_intermediate_continuation (tp, step_1_continuation, args, xfree);
1022 }
1023 }
1024 }
1025
1026 \f
1027 /* Continue program at specified address. */
1028
1029 static void
1030 jump_command (char *arg, int from_tty)
1031 {
1032 CORE_ADDR addr;
1033 struct symtabs_and_lines sals;
1034 struct symtab_and_line sal;
1035 struct symbol *fn;
1036 struct symbol *sfn;
1037 int async_exec = 0;
1038
1039 ERROR_NO_INFERIOR;
1040 ensure_valid_thread ();
1041 ensure_not_running ();
1042
1043 /* Find out whether we must run in the background. */
1044 if (arg != NULL)
1045 async_exec = strip_bg_char (&arg);
1046
1047 /* If we must run in the background, but the target can't do it,
1048 error out. */
1049 if (async_exec && !target_can_async_p ())
1050 error (_("Asynchronous execution not supported on this target."));
1051
1052 if (!arg)
1053 error_no_arg (_("starting address"));
1054
1055 sals = decode_line_spec_1 (arg, 1);
1056 if (sals.nelts != 1)
1057 {
1058 error (_("Unreasonable jump request"));
1059 }
1060
1061 sal = sals.sals[0];
1062 xfree (sals.sals);
1063
1064 if (sal.symtab == 0 && sal.pc == 0)
1065 error (_("No source file has been specified."));
1066
1067 resolve_sal_pc (&sal); /* May error out */
1068
1069 /* See if we are trying to jump to another function. */
1070 fn = get_frame_function (get_current_frame ());
1071 sfn = find_pc_function (sal.pc);
1072 if (fn != NULL && sfn != fn)
1073 {
1074 if (!query (_("Line %d is not in `%s'. Jump anyway? "), sal.line,
1075 SYMBOL_PRINT_NAME (fn)))
1076 {
1077 error (_("Not confirmed."));
1078 /* NOTREACHED */
1079 }
1080 }
1081
1082 if (sfn != NULL)
1083 {
1084 fixup_symbol_section (sfn, 0);
1085 if (section_is_overlay (SYMBOL_OBJ_SECTION (sfn)) &&
1086 !section_is_mapped (SYMBOL_OBJ_SECTION (sfn)))
1087 {
1088 if (!query (_("WARNING!!! Destination is in unmapped overlay! Jump anyway? ")))
1089 {
1090 error (_("Not confirmed."));
1091 /* NOTREACHED */
1092 }
1093 }
1094 }
1095
1096 addr = sal.pc;
1097
1098 if (from_tty)
1099 {
1100 printf_filtered (_("Continuing at "));
1101 fputs_filtered (paddress (addr), gdb_stdout);
1102 printf_filtered (".\n");
1103 }
1104
1105 /* If we are not asked to run in the bg, then prepare to run in the
1106 foreground, synchronously. */
1107 if (!async_exec && target_can_async_p ())
1108 {
1109 /* Simulate synchronous execution */
1110 async_disable_stdin ();
1111 }
1112
1113 clear_proceed_status ();
1114 proceed (addr, TARGET_SIGNAL_0, 0);
1115 }
1116 \f
1117
1118 /* Go to line or address in current procedure */
1119 static void
1120 go_command (char *line_no, int from_tty)
1121 {
1122 if (line_no == (char *) NULL || !*line_no)
1123 printf_filtered (_("Usage: go <location>\n"));
1124 else
1125 {
1126 tbreak_command (line_no, from_tty);
1127 jump_command (line_no, from_tty);
1128 }
1129 }
1130 \f
1131
1132 /* Continue program giving it specified signal. */
1133
1134 static void
1135 signal_command (char *signum_exp, int from_tty)
1136 {
1137 enum target_signal oursig;
1138 int async_exec = 0;
1139
1140 dont_repeat (); /* Too dangerous. */
1141 ERROR_NO_INFERIOR;
1142 ensure_valid_thread ();
1143 ensure_not_running ();
1144
1145 /* Find out whether we must run in the background. */
1146 if (signum_exp != NULL)
1147 async_exec = strip_bg_char (&signum_exp);
1148
1149 /* If we must run in the background, but the target can't do it,
1150 error out. */
1151 if (async_exec && !target_can_async_p ())
1152 error (_("Asynchronous execution not supported on this target."));
1153
1154 /* If we are not asked to run in the bg, then prepare to run in the
1155 foreground, synchronously. */
1156 if (!async_exec && target_can_async_p ())
1157 {
1158 /* Simulate synchronous execution. */
1159 async_disable_stdin ();
1160 }
1161
1162 if (!signum_exp)
1163 error_no_arg (_("signal number"));
1164
1165 /* It would be even slicker to make signal names be valid expressions,
1166 (the type could be "enum $signal" or some such), then the user could
1167 assign them to convenience variables. */
1168 oursig = target_signal_from_name (signum_exp);
1169
1170 if (oursig == TARGET_SIGNAL_UNKNOWN)
1171 {
1172 /* No, try numeric. */
1173 int num = parse_and_eval_long (signum_exp);
1174
1175 if (num == 0)
1176 oursig = TARGET_SIGNAL_0;
1177 else
1178 oursig = target_signal_from_command (num);
1179 }
1180
1181 if (from_tty)
1182 {
1183 if (oursig == TARGET_SIGNAL_0)
1184 printf_filtered (_("Continuing with no signal.\n"));
1185 else
1186 printf_filtered (_("Continuing with signal %s.\n"),
1187 target_signal_to_name (oursig));
1188 }
1189
1190 clear_proceed_status ();
1191 proceed ((CORE_ADDR) -1, oursig, 0);
1192 }
1193
1194 /* Proceed until we reach a different source line with pc greater than
1195 our current one or exit the function. We skip calls in both cases.
1196
1197 Note that eventually this command should probably be changed so
1198 that only source lines are printed out when we hit the breakpoint
1199 we set. This may involve changes to wait_for_inferior and the
1200 proceed status code. */
1201
1202 static void
1203 until_next_command (int from_tty)
1204 {
1205 struct frame_info *frame;
1206 CORE_ADDR pc;
1207 struct symbol *func;
1208 struct symtab_and_line sal;
1209 struct thread_info *tp = inferior_thread ();
1210
1211 clear_proceed_status ();
1212 set_step_frame ();
1213
1214 frame = get_current_frame ();
1215
1216 /* Step until either exited from this function or greater
1217 than the current line (if in symbolic section) or pc (if
1218 not). */
1219
1220 pc = get_frame_pc (frame);
1221 func = find_pc_function (pc);
1222
1223 if (!func)
1224 {
1225 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
1226
1227 if (msymbol == NULL)
1228 error (_("Execution is not within a known function."));
1229
1230 tp->step_range_start = SYMBOL_VALUE_ADDRESS (msymbol);
1231 tp->step_range_end = pc;
1232 }
1233 else
1234 {
1235 sal = find_pc_line (pc, 0);
1236
1237 tp->step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
1238 tp->step_range_end = sal.end;
1239 }
1240
1241 tp->step_over_calls = STEP_OVER_ALL;
1242
1243 tp->step_multi = 0; /* Only one call to proceed */
1244
1245 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
1246 }
1247
1248 static void
1249 until_command (char *arg, int from_tty)
1250 {
1251 int async_exec = 0;
1252
1253 if (!target_has_execution)
1254 error (_("The program is not running."));
1255
1256 /* Find out whether we must run in the background. */
1257 if (arg != NULL)
1258 async_exec = strip_bg_char (&arg);
1259
1260 /* If we must run in the background, but the target can't do it,
1261 error out. */
1262 if (async_exec && !target_can_async_p ())
1263 error (_("Asynchronous execution not supported on this target."));
1264
1265 /* If we are not asked to run in the bg, then prepare to run in the
1266 foreground, synchronously. */
1267 if (!async_exec && target_can_async_p ())
1268 {
1269 /* Simulate synchronous execution */
1270 async_disable_stdin ();
1271 }
1272
1273 if (arg)
1274 until_break_command (arg, from_tty, 0);
1275 else
1276 until_next_command (from_tty);
1277 }
1278
1279 static void
1280 advance_command (char *arg, int from_tty)
1281 {
1282 int async_exec = 0;
1283
1284 if (!target_has_execution)
1285 error (_("The program is not running."));
1286
1287 if (arg == NULL)
1288 error_no_arg (_("a location"));
1289
1290 /* Find out whether we must run in the background. */
1291 if (arg != NULL)
1292 async_exec = strip_bg_char (&arg);
1293
1294 /* If we must run in the background, but the target can't do it,
1295 error out. */
1296 if (async_exec && !target_can_async_p ())
1297 error (_("Asynchronous execution not supported on this target."));
1298
1299 /* If we are not asked to run in the bg, then prepare to run in the
1300 foreground, synchronously. */
1301 if (!async_exec && target_can_async_p ())
1302 {
1303 /* Simulate synchronous execution. */
1304 async_disable_stdin ();
1305 }
1306
1307 until_break_command (arg, from_tty, 1);
1308 }
1309 \f
1310 /* Print the result of a function at the end of a 'finish' command. */
1311
1312 static void
1313 print_return_value (struct type *func_type, struct type *value_type)
1314 {
1315 struct gdbarch *gdbarch = get_regcache_arch (stop_registers);
1316 struct cleanup *old_chain;
1317 struct ui_stream *stb;
1318 struct value *value;
1319
1320 CHECK_TYPEDEF (value_type);
1321 gdb_assert (TYPE_CODE (value_type) != TYPE_CODE_VOID);
1322
1323 /* FIXME: 2003-09-27: When returning from a nested inferior function
1324 call, it's possible (with no help from the architecture vector)
1325 to locate and return/print a "struct return" value. This is just
1326 a more complicated case of what is already being done in in the
1327 inferior function call code. In fact, when inferior function
1328 calls are made async, this will likely be made the norm. */
1329
1330 switch (gdbarch_return_value (gdbarch, func_type, value_type,
1331 NULL, NULL, NULL))
1332 {
1333 case RETURN_VALUE_REGISTER_CONVENTION:
1334 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
1335 case RETURN_VALUE_ABI_PRESERVES_ADDRESS:
1336 value = allocate_value (value_type);
1337 gdbarch_return_value (gdbarch, func_type, value_type, stop_registers,
1338 value_contents_raw (value), NULL);
1339 break;
1340 case RETURN_VALUE_STRUCT_CONVENTION:
1341 value = NULL;
1342 break;
1343 default:
1344 internal_error (__FILE__, __LINE__, _("bad switch"));
1345 }
1346
1347 if (value)
1348 {
1349 struct value_print_options opts;
1350
1351 /* Print it. */
1352 stb = ui_out_stream_new (uiout);
1353 old_chain = make_cleanup_ui_out_stream_delete (stb);
1354 ui_out_text (uiout, "Value returned is ");
1355 ui_out_field_fmt (uiout, "gdb-result-var", "$%d",
1356 record_latest_value (value));
1357 ui_out_text (uiout, " = ");
1358 get_raw_print_options (&opts);
1359 value_print (value, stb->stream, &opts);
1360 ui_out_field_stream (uiout, "return-value", stb);
1361 ui_out_text (uiout, "\n");
1362 do_cleanups (old_chain);
1363 }
1364 else
1365 {
1366 ui_out_text (uiout, "Value returned has type: ");
1367 ui_out_field_string (uiout, "return-type", TYPE_NAME (value_type));
1368 ui_out_text (uiout, ".");
1369 ui_out_text (uiout, " Cannot determine contents\n");
1370 }
1371 }
1372
1373 /* Stuff that needs to be done by the finish command after the target
1374 has stopped. In asynchronous mode, we wait for the target to stop
1375 in the call to poll or select in the event loop, so it is
1376 impossible to do all the stuff as part of the finish_command
1377 function itself. The only chance we have to complete this command
1378 is in fetch_inferior_event, which is called by the event loop as
1379 soon as it detects that the target has stopped. This function is
1380 called via the cmd_continuation pointer. */
1381
1382 struct finish_command_continuation_args
1383 {
1384 struct breakpoint *breakpoint;
1385 struct symbol *function;
1386 };
1387
1388 static void
1389 finish_command_continuation (void *arg)
1390 {
1391 struct finish_command_continuation_args *a = arg;
1392 struct thread_info *tp = NULL;
1393 bpstat bs = NULL;
1394
1395 if (!ptid_equal (inferior_ptid, null_ptid)
1396 && target_has_execution
1397 && is_stopped (inferior_ptid))
1398 {
1399 tp = inferior_thread ();
1400 bs = tp->stop_bpstat;
1401 }
1402
1403 if (bpstat_find_breakpoint (bs, a->breakpoint) != NULL
1404 && a->function != NULL)
1405 {
1406 struct type *value_type;
1407
1408 value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (a->function));
1409 if (!value_type)
1410 internal_error (__FILE__, __LINE__,
1411 _("finish_command: function has no target type"));
1412
1413 if (TYPE_CODE (value_type) != TYPE_CODE_VOID)
1414 print_return_value (SYMBOL_TYPE (a->function), value_type);
1415 }
1416
1417 /* We suppress normal call of normal_stop observer and do it here so
1418 that the *stopped notification includes the return value. */
1419 if (bs != NULL && tp->proceed_to_finish)
1420 observer_notify_normal_stop (bs, 1 /* print frame */);
1421 delete_breakpoint (a->breakpoint);
1422 }
1423
1424 static void
1425 finish_command_continuation_free_arg (void *arg)
1426 {
1427 xfree (arg);
1428 }
1429
1430 /* finish_backward -- helper function for finish_command. */
1431
1432 static void
1433 finish_backward (struct symbol *function)
1434 {
1435 struct symtab_and_line sal;
1436 struct thread_info *tp = inferior_thread ();
1437 struct breakpoint *breakpoint;
1438 struct cleanup *old_chain;
1439 CORE_ADDR pc;
1440 CORE_ADDR func_addr;
1441 int back_up;
1442
1443 pc = get_frame_pc (get_current_frame ());
1444
1445 if (find_pc_partial_function (pc, NULL, &func_addr, NULL) == 0)
1446 internal_error (__FILE__, __LINE__,
1447 _("Finish: couldn't find function."));
1448
1449 sal = find_pc_line (func_addr, 0);
1450
1451 /* We don't need a return value. */
1452 tp->proceed_to_finish = 0;
1453 /* Special case: if we're sitting at the function entry point,
1454 then all we need to do is take a reverse singlestep. We
1455 don't need to set a breakpoint, and indeed it would do us
1456 no good to do so.
1457
1458 Note that this can only happen at frame #0, since there's
1459 no way that a function up the stack can have a return address
1460 that's equal to its entry point. */
1461
1462 if (sal.pc != pc)
1463 {
1464 /* Set breakpoint and continue. */
1465 breakpoint =
1466 set_momentary_breakpoint (sal,
1467 get_stack_frame_id (get_selected_frame (NULL)),
1468 bp_breakpoint);
1469 /* Tell the breakpoint to keep quiet. We won't be done
1470 until we've done another reverse single-step. */
1471 make_breakpoint_silent (breakpoint);
1472 old_chain = make_cleanup_delete_breakpoint (breakpoint);
1473 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
1474 /* We will be stopped when proceed returns. */
1475 back_up = bpstat_find_breakpoint (tp->stop_bpstat, breakpoint) != NULL;
1476 do_cleanups (old_chain);
1477 }
1478 else
1479 back_up = 1;
1480 if (back_up)
1481 {
1482 /* If in fact we hit the step-resume breakpoint (and not
1483 some other breakpoint), then we're almost there --
1484 we just need to back up by one more single-step. */
1485 tp->step_range_start = tp->step_range_end = 1;
1486 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
1487 }
1488 return;
1489 }
1490
1491 /* finish_forward -- helper function for finish_command. */
1492
1493 static void
1494 finish_forward (struct symbol *function, struct frame_info *frame)
1495 {
1496 struct symtab_and_line sal;
1497 struct thread_info *tp = inferior_thread ();
1498 struct breakpoint *breakpoint;
1499 struct cleanup *old_chain;
1500 struct finish_command_continuation_args *cargs;
1501
1502 sal = find_pc_line (get_frame_pc (frame), 0);
1503 sal.pc = get_frame_pc (frame);
1504
1505 breakpoint = set_momentary_breakpoint (sal, get_stack_frame_id (frame),
1506 bp_finish);
1507
1508 old_chain = make_cleanup_delete_breakpoint (breakpoint);
1509
1510 tp->proceed_to_finish = 1; /* We want stop_registers, please... */
1511 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
1512
1513 cargs = xmalloc (sizeof (*cargs));
1514
1515 cargs->breakpoint = breakpoint;
1516 cargs->function = function;
1517 add_continuation (tp, finish_command_continuation, cargs,
1518 finish_command_continuation_free_arg);
1519
1520 discard_cleanups (old_chain);
1521 if (!target_can_async_p ())
1522 do_all_continuations ();
1523 }
1524
1525 /* "finish": Set a temporary breakpoint at the place the selected
1526 frame will return to, then continue. */
1527
1528 static void
1529 finish_command (char *arg, int from_tty)
1530 {
1531 struct frame_info *frame;
1532 struct symbol *function;
1533
1534 int async_exec = 0;
1535
1536 /* Find out whether we must run in the background. */
1537 if (arg != NULL)
1538 async_exec = strip_bg_char (&arg);
1539
1540 /* If we must run in the background, but the target can't do it,
1541 error out. */
1542 if (async_exec && !target_can_async_p ())
1543 error (_("Asynchronous execution not supported on this target."));
1544
1545 /* Don't try to async in reverse. */
1546 if (async_exec && execution_direction == EXEC_REVERSE)
1547 error (_("Asynchronous 'finish' not supported in reverse."));
1548
1549 /* If we are not asked to run in the bg, then prepare to run in the
1550 foreground, synchronously. */
1551 if (!async_exec && target_can_async_p ())
1552 {
1553 /* Simulate synchronous execution. */
1554 async_disable_stdin ();
1555 }
1556
1557 if (arg)
1558 error (_("The \"finish\" command does not take any arguments."));
1559 if (!target_has_execution)
1560 error (_("The program is not running."));
1561
1562 frame = get_prev_frame (get_selected_frame (_("No selected frame.")));
1563 if (frame == 0)
1564 error (_("\"finish\" not meaningful in the outermost frame."));
1565
1566 clear_proceed_status ();
1567
1568 /* Finishing from an inline frame is completely different. We don't
1569 try to show the "return value" - no way to locate it. So we do
1570 not need a completion. */
1571 if (get_frame_type (get_selected_frame (_("No selected frame.")))
1572 == INLINE_FRAME)
1573 {
1574 /* Claim we are stepping in the calling frame. An empty step
1575 range means that we will stop once we aren't in a function
1576 called by that frame. We don't use the magic "1" value for
1577 step_range_end, because then infrun will think this is nexti,
1578 and not step over the rest of this inlined function call. */
1579 struct thread_info *tp = inferior_thread ();
1580 struct symtab_and_line empty_sal;
1581 init_sal (&empty_sal);
1582 set_step_info (frame, empty_sal);
1583 tp->step_range_start = tp->step_range_end = get_frame_pc (frame);
1584 tp->step_over_calls = STEP_OVER_ALL;
1585
1586 /* Print info on the selected frame, including level number but not
1587 source. */
1588 if (from_tty)
1589 {
1590 printf_filtered (_("Run till exit from "));
1591 print_stack_frame (get_selected_frame (NULL), 1, LOCATION);
1592 }
1593
1594 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
1595 return;
1596 }
1597
1598 /* Find the function we will return from. */
1599
1600 function = find_pc_function (get_frame_pc (get_selected_frame (NULL)));
1601
1602 /* Print info on the selected frame, including level number but not
1603 source. */
1604 if (from_tty)
1605 {
1606 if (execution_direction == EXEC_REVERSE)
1607 printf_filtered (_("Run back to call of "));
1608 else
1609 printf_filtered (_("Run till exit from "));
1610
1611 print_stack_frame (get_selected_frame (NULL), 1, LOCATION);
1612 }
1613
1614 if (execution_direction == EXEC_REVERSE)
1615 finish_backward (function);
1616 else
1617 finish_forward (function, frame);
1618 }
1619 \f
1620
1621 static void
1622 program_info (char *args, int from_tty)
1623 {
1624 bpstat bs;
1625 int num, stat;
1626 struct thread_info *tp;
1627 ptid_t ptid;
1628
1629 if (!target_has_execution)
1630 {
1631 printf_filtered (_("The program being debugged is not being run.\n"));
1632 return;
1633 }
1634
1635 if (non_stop)
1636 ptid = inferior_ptid;
1637 else
1638 {
1639 struct target_waitstatus ws;
1640 get_last_target_status (&ptid, &ws);
1641 }
1642
1643 if (ptid_equal (ptid, null_ptid) || is_exited (ptid))
1644 error (_("Invalid selected thread."));
1645 else if (is_running (ptid))
1646 error (_("Selected thread is running."));
1647
1648 tp = find_thread_ptid (ptid);
1649 bs = tp->stop_bpstat;
1650 stat = bpstat_num (&bs, &num);
1651
1652 target_files_info ();
1653 printf_filtered (_("Program stopped at %s.\n"), paddress (stop_pc));
1654 if (tp->stop_step)
1655 printf_filtered (_("It stopped after being stepped.\n"));
1656 else if (stat != 0)
1657 {
1658 /* There may be several breakpoints in the same place, so this
1659 isn't as strange as it seems. */
1660 while (stat != 0)
1661 {
1662 if (stat < 0)
1663 {
1664 printf_filtered (_("\
1665 It stopped at a breakpoint that has since been deleted.\n"));
1666 }
1667 else
1668 printf_filtered (_("It stopped at breakpoint %d.\n"), num);
1669 stat = bpstat_num (&bs, &num);
1670 }
1671 }
1672 else if (tp->stop_signal != TARGET_SIGNAL_0)
1673 {
1674 printf_filtered (_("It stopped with signal %s, %s.\n"),
1675 target_signal_to_name (tp->stop_signal),
1676 target_signal_to_string (tp->stop_signal));
1677 }
1678
1679 if (!from_tty)
1680 {
1681 printf_filtered (_("\
1682 Type \"info stack\" or \"info registers\" for more information.\n"));
1683 }
1684 }
1685 \f
1686 static void
1687 environment_info (char *var, int from_tty)
1688 {
1689 if (var)
1690 {
1691 char *val = get_in_environ (inferior_environ, var);
1692 if (val)
1693 {
1694 puts_filtered (var);
1695 puts_filtered (" = ");
1696 puts_filtered (val);
1697 puts_filtered ("\n");
1698 }
1699 else
1700 {
1701 puts_filtered ("Environment variable \"");
1702 puts_filtered (var);
1703 puts_filtered ("\" not defined.\n");
1704 }
1705 }
1706 else
1707 {
1708 char **vector = environ_vector (inferior_environ);
1709 while (*vector)
1710 {
1711 puts_filtered (*vector++);
1712 puts_filtered ("\n");
1713 }
1714 }
1715 }
1716
1717 static void
1718 set_environment_command (char *arg, int from_tty)
1719 {
1720 char *p, *val, *var;
1721 int nullset = 0;
1722
1723 if (arg == 0)
1724 error_no_arg (_("environment variable and value"));
1725
1726 /* Find seperation between variable name and value */
1727 p = (char *) strchr (arg, '=');
1728 val = (char *) strchr (arg, ' ');
1729
1730 if (p != 0 && val != 0)
1731 {
1732 /* We have both a space and an equals. If the space is before the
1733 equals, walk forward over the spaces til we see a nonspace
1734 (possibly the equals). */
1735 if (p > val)
1736 while (*val == ' ')
1737 val++;
1738
1739 /* Now if the = is after the char following the spaces,
1740 take the char following the spaces. */
1741 if (p > val)
1742 p = val - 1;
1743 }
1744 else if (val != 0 && p == 0)
1745 p = val;
1746
1747 if (p == arg)
1748 error_no_arg (_("environment variable to set"));
1749
1750 if (p == 0 || p[1] == 0)
1751 {
1752 nullset = 1;
1753 if (p == 0)
1754 p = arg + strlen (arg); /* So that savestring below will work */
1755 }
1756 else
1757 {
1758 /* Not setting variable value to null */
1759 val = p + 1;
1760 while (*val == ' ' || *val == '\t')
1761 val++;
1762 }
1763
1764 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
1765 p--;
1766
1767 var = savestring (arg, p - arg);
1768 if (nullset)
1769 {
1770 printf_filtered (_("\
1771 Setting environment variable \"%s\" to null value.\n"),
1772 var);
1773 set_in_environ (inferior_environ, var, "");
1774 }
1775 else
1776 set_in_environ (inferior_environ, var, val);
1777 xfree (var);
1778 }
1779
1780 static void
1781 unset_environment_command (char *var, int from_tty)
1782 {
1783 if (var == 0)
1784 {
1785 /* If there is no argument, delete all environment variables.
1786 Ask for confirmation if reading from the terminal. */
1787 if (!from_tty || query (_("Delete all environment variables? ")))
1788 {
1789 free_environ (inferior_environ);
1790 inferior_environ = make_environ ();
1791 }
1792 }
1793 else
1794 unset_in_environ (inferior_environ, var);
1795 }
1796
1797 /* Handle the execution path (PATH variable) */
1798
1799 static const char path_var_name[] = "PATH";
1800
1801 static void
1802 path_info (char *args, int from_tty)
1803 {
1804 puts_filtered ("Executable and object file path: ");
1805 puts_filtered (get_in_environ (inferior_environ, path_var_name));
1806 puts_filtered ("\n");
1807 }
1808
1809 /* Add zero or more directories to the front of the execution path. */
1810
1811 static void
1812 path_command (char *dirname, int from_tty)
1813 {
1814 char *exec_path;
1815 char *env;
1816 dont_repeat ();
1817 env = get_in_environ (inferior_environ, path_var_name);
1818 /* Can be null if path is not set */
1819 if (!env)
1820 env = "";
1821 exec_path = xstrdup (env);
1822 mod_path (dirname, &exec_path);
1823 set_in_environ (inferior_environ, path_var_name, exec_path);
1824 xfree (exec_path);
1825 if (from_tty)
1826 path_info ((char *) NULL, from_tty);
1827 }
1828 \f
1829
1830 /* Print out the machine register regnum. If regnum is -1, print all
1831 registers (print_all == 1) or all non-float and non-vector
1832 registers (print_all == 0).
1833
1834 For most machines, having all_registers_info() print the
1835 register(s) one per line is good enough. If a different format is
1836 required, (eg, for MIPS or Pyramid 90x, which both have lots of
1837 regs), or there is an existing convention for showing all the
1838 registers, define the architecture method PRINT_REGISTERS_INFO to
1839 provide that format. */
1840
1841 void
1842 default_print_registers_info (struct gdbarch *gdbarch,
1843 struct ui_file *file,
1844 struct frame_info *frame,
1845 int regnum, int print_all)
1846 {
1847 int i;
1848 const int numregs = gdbarch_num_regs (gdbarch)
1849 + gdbarch_num_pseudo_regs (gdbarch);
1850 gdb_byte buffer[MAX_REGISTER_SIZE];
1851
1852 for (i = 0; i < numregs; i++)
1853 {
1854 /* Decide between printing all regs, non-float / vector regs, or
1855 specific reg. */
1856 if (regnum == -1)
1857 {
1858 if (print_all)
1859 {
1860 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
1861 continue;
1862 }
1863 else
1864 {
1865 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
1866 continue;
1867 }
1868 }
1869 else
1870 {
1871 if (i != regnum)
1872 continue;
1873 }
1874
1875 /* If the register name is empty, it is undefined for this
1876 processor, so don't display anything. */
1877 if (gdbarch_register_name (gdbarch, i) == NULL
1878 || *(gdbarch_register_name (gdbarch, i)) == '\0')
1879 continue;
1880
1881 fputs_filtered (gdbarch_register_name (gdbarch, i), file);
1882 print_spaces_filtered (15 - strlen (gdbarch_register_name
1883 (gdbarch, i)), file);
1884
1885 /* Get the data in raw format. */
1886 if (! frame_register_read (frame, i, buffer))
1887 {
1888 fprintf_filtered (file, "*value not available*\n");
1889 continue;
1890 }
1891
1892 /* If virtual format is floating, print it that way, and in raw
1893 hex. */
1894 if (TYPE_CODE (register_type (gdbarch, i)) == TYPE_CODE_FLT
1895 || TYPE_CODE (register_type (gdbarch, i)) == TYPE_CODE_DECFLOAT)
1896 {
1897 int j;
1898 struct value_print_options opts;
1899
1900 get_user_print_options (&opts);
1901 opts.deref_ref = 1;
1902 val_print (register_type (gdbarch, i), buffer, 0, 0,
1903 file, 0, &opts, current_language);
1904
1905 fprintf_filtered (file, "\t(raw 0x");
1906 for (j = 0; j < register_size (gdbarch, i); j++)
1907 {
1908 int idx;
1909 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
1910 idx = j;
1911 else
1912 idx = register_size (gdbarch, i) - 1 - j;
1913 fprintf_filtered (file, "%02x", (unsigned char) buffer[idx]);
1914 }
1915 fprintf_filtered (file, ")");
1916 }
1917 else
1918 {
1919 struct value_print_options opts;
1920
1921 /* Print the register in hex. */
1922 get_formatted_print_options (&opts, 'x');
1923 opts.deref_ref = 1;
1924 val_print (register_type (gdbarch, i), buffer, 0, 0,
1925 file, 0, &opts,
1926 current_language);
1927 /* If not a vector register, print it also according to its
1928 natural format. */
1929 if (TYPE_VECTOR (register_type (gdbarch, i)) == 0)
1930 {
1931 get_user_print_options (&opts);
1932 opts.deref_ref = 1;
1933 fprintf_filtered (file, "\t");
1934 val_print (register_type (gdbarch, i), buffer, 0, 0,
1935 file, 0, &opts, current_language);
1936 }
1937 }
1938
1939 fprintf_filtered (file, "\n");
1940 }
1941 }
1942
1943 void
1944 registers_info (char *addr_exp, int fpregs)
1945 {
1946 struct frame_info *frame;
1947 struct gdbarch *gdbarch;
1948 int regnum, numregs;
1949 char *end;
1950
1951 if (!target_has_registers)
1952 error (_("The program has no registers now."));
1953 frame = get_selected_frame (NULL);
1954 gdbarch = get_frame_arch (frame);
1955
1956 if (!addr_exp)
1957 {
1958 gdbarch_print_registers_info (gdbarch, gdb_stdout,
1959 frame, -1, fpregs);
1960 return;
1961 }
1962
1963 while (*addr_exp != '\0')
1964 {
1965 char *start;
1966 const char *end;
1967
1968 /* Keep skipping leading white space. */
1969 if (isspace ((*addr_exp)))
1970 {
1971 addr_exp++;
1972 continue;
1973 }
1974
1975 /* Discard any leading ``$''. Check that there is something
1976 resembling a register following it. */
1977 if (addr_exp[0] == '$')
1978 addr_exp++;
1979 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
1980 error (_("Missing register name"));
1981
1982 /* Find the start/end of this register name/num/group. */
1983 start = addr_exp;
1984 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
1985 addr_exp++;
1986 end = addr_exp;
1987
1988 /* Figure out what we've found and display it. */
1989
1990 /* A register name? */
1991 {
1992 int regnum = user_reg_map_name_to_regnum (gdbarch, start, end - start);
1993 if (regnum >= 0)
1994 {
1995 /* User registers lie completely outside of the range of
1996 normal registers. Catch them early so that the target
1997 never sees them. */
1998 if (regnum >= gdbarch_num_regs (gdbarch)
1999 + gdbarch_num_pseudo_regs (gdbarch))
2000 {
2001 struct value_print_options opts;
2002 struct value *val = value_of_user_reg (regnum, frame);
2003
2004 printf_filtered ("%s: ", start);
2005 get_formatted_print_options (&opts, 'x');
2006 print_scalar_formatted (value_contents (val),
2007 check_typedef (value_type (val)),
2008 &opts, 0, gdb_stdout);
2009 printf_filtered ("\n");
2010 }
2011 else
2012 gdbarch_print_registers_info (gdbarch, gdb_stdout,
2013 frame, regnum, fpregs);
2014 continue;
2015 }
2016 }
2017
2018 /* A register group? */
2019 {
2020 struct reggroup *group;
2021 for (group = reggroup_next (gdbarch, NULL);
2022 group != NULL;
2023 group = reggroup_next (gdbarch, group))
2024 {
2025 /* Don't bother with a length check. Should the user
2026 enter a short register group name, go with the first
2027 group that matches. */
2028 if (strncmp (start, reggroup_name (group), end - start) == 0)
2029 break;
2030 }
2031 if (group != NULL)
2032 {
2033 int regnum;
2034 for (regnum = 0;
2035 regnum < gdbarch_num_regs (gdbarch)
2036 + gdbarch_num_pseudo_regs (gdbarch);
2037 regnum++)
2038 {
2039 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
2040 gdbarch_print_registers_info (gdbarch,
2041 gdb_stdout, frame,
2042 regnum, fpregs);
2043 }
2044 continue;
2045 }
2046 }
2047
2048 /* Nothing matched. */
2049 error (_("Invalid register `%.*s'"), (int) (end - start), start);
2050 }
2051 }
2052
2053 void
2054 all_registers_info (char *addr_exp, int from_tty)
2055 {
2056 registers_info (addr_exp, 1);
2057 }
2058
2059 static void
2060 nofp_registers_info (char *addr_exp, int from_tty)
2061 {
2062 registers_info (addr_exp, 0);
2063 }
2064
2065 static void
2066 print_vector_info (struct ui_file *file,
2067 struct frame_info *frame, const char *args)
2068 {
2069 struct gdbarch *gdbarch = get_frame_arch (frame);
2070
2071 if (gdbarch_print_vector_info_p (gdbarch))
2072 gdbarch_print_vector_info (gdbarch, file, frame, args);
2073 else
2074 {
2075 int regnum;
2076 int printed_something = 0;
2077
2078 for (regnum = 0;
2079 regnum < gdbarch_num_regs (gdbarch)
2080 + gdbarch_num_pseudo_regs (gdbarch);
2081 regnum++)
2082 {
2083 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
2084 {
2085 printed_something = 1;
2086 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2087 }
2088 }
2089 if (!printed_something)
2090 fprintf_filtered (file, "No vector information\n");
2091 }
2092 }
2093
2094 static void
2095 vector_info (char *args, int from_tty)
2096 {
2097 if (!target_has_registers)
2098 error (_("The program has no registers now."));
2099
2100 print_vector_info (gdb_stdout, get_selected_frame (NULL), args);
2101 }
2102 \f
2103 /* Kill the inferior process. Make us have no inferior. */
2104
2105 static void
2106 kill_command (char *arg, int from_tty)
2107 {
2108 /* FIXME: This should not really be inferior_ptid (or target_has_execution).
2109 It should be a distinct flag that indicates that a target is active, cuz
2110 some targets don't have processes! */
2111
2112 if (ptid_equal (inferior_ptid, null_ptid))
2113 error (_("The program is not being run."));
2114 if (!query (_("Kill the program being debugged? ")))
2115 error (_("Not confirmed."));
2116 target_kill ();
2117
2118 /* If we still have other inferiors to debug, then don't mess with
2119 with their threads. */
2120 if (!have_inferiors ())
2121 {
2122 init_thread_list (); /* Destroy thread info */
2123
2124 /* Killing off the inferior can leave us with a core file. If
2125 so, print the state we are left in. */
2126 if (target_has_stack)
2127 {
2128 printf_filtered (_("In %s,\n"), target_longname);
2129 print_stack_frame (get_selected_frame (NULL), 1, SRC_AND_LOC);
2130 }
2131 }
2132 bfd_cache_close_all ();
2133 }
2134
2135 /* Used in `attach&' command. ARG is a point to an integer
2136 representing a process id. Proceed threads of this process iff
2137 they stopped due to debugger request, and when they did, they
2138 reported a clean stop (TARGET_SIGNAL_0). Do not proceed threads
2139 that have been explicitly been told to stop. */
2140
2141 static int
2142 proceed_after_attach_callback (struct thread_info *thread,
2143 void *arg)
2144 {
2145 int pid = * (int *) arg;
2146
2147 if (ptid_get_pid (thread->ptid) == pid
2148 && !is_exited (thread->ptid)
2149 && !is_executing (thread->ptid)
2150 && !thread->stop_requested
2151 && thread->stop_signal == TARGET_SIGNAL_0)
2152 {
2153 switch_to_thread (thread->ptid);
2154 clear_proceed_status ();
2155 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
2156 }
2157
2158 return 0;
2159 }
2160
2161 static void
2162 proceed_after_attach (int pid)
2163 {
2164 /* Don't error out if the current thread is running, because
2165 there may be other stopped threads. */
2166 struct cleanup *old_chain;
2167
2168 /* Backup current thread and selected frame. */
2169 old_chain = make_cleanup_restore_current_thread ();
2170
2171 iterate_over_threads (proceed_after_attach_callback, &pid);
2172
2173 /* Restore selected ptid. */
2174 do_cleanups (old_chain);
2175 }
2176
2177 /*
2178 * TODO:
2179 * Should save/restore the tty state since it might be that the
2180 * program to be debugged was started on this tty and it wants
2181 * the tty in some state other than what we want. If it's running
2182 * on another terminal or without a terminal, then saving and
2183 * restoring the tty state is a harmless no-op.
2184 * This only needs to be done if we are attaching to a process.
2185 */
2186
2187 /*
2188 attach_command --
2189 takes a program started up outside of gdb and ``attaches'' to it.
2190 This stops it cold in its tracks and allows us to start debugging it.
2191 and wait for the trace-trap that results from attaching. */
2192
2193 static void
2194 attach_command_post_wait (char *args, int from_tty, int async_exec)
2195 {
2196 char *exec_file;
2197 char *full_exec_path = NULL;
2198 struct inferior *inferior;
2199
2200 inferior = current_inferior ();
2201 inferior->stop_soon = NO_STOP_QUIETLY;
2202
2203 /* If no exec file is yet known, try to determine it from the
2204 process itself. */
2205 exec_file = (char *) get_exec_file (0);
2206 if (!exec_file)
2207 {
2208 exec_file = target_pid_to_exec_file (PIDGET (inferior_ptid));
2209 if (exec_file)
2210 {
2211 /* It's possible we don't have a full path, but rather just a
2212 filename. Some targets, such as HP-UX, don't provide the
2213 full path, sigh.
2214
2215 Attempt to qualify the filename against the source path.
2216 (If that fails, we'll just fall back on the original
2217 filename. Not much more we can do...)
2218 */
2219 if (!source_full_path_of (exec_file, &full_exec_path))
2220 full_exec_path = xstrdup (exec_file);
2221
2222 exec_file_attach (full_exec_path, from_tty);
2223 symbol_file_add_main (full_exec_path, from_tty);
2224 }
2225 }
2226 else
2227 {
2228 reopen_exec_file ();
2229 reread_symbols ();
2230 }
2231
2232 /* Take any necessary post-attaching actions for this platform. */
2233 target_post_attach (PIDGET (inferior_ptid));
2234
2235 post_create_inferior (&current_target, from_tty);
2236
2237 /* Install inferior's terminal modes. */
2238 target_terminal_inferior ();
2239
2240 if (async_exec)
2241 {
2242 /* The user requested an `attach&', so be sure to leave threads
2243 that didn't get a signal running. */
2244
2245 /* Immediatelly resume all suspended threads of this inferior,
2246 and this inferior only. This should have no effect on
2247 already running threads. If a thread has been stopped with a
2248 signal, leave it be. */
2249 if (non_stop)
2250 proceed_after_attach (inferior->pid);
2251 else
2252 {
2253 if (inferior_thread ()->stop_signal == TARGET_SIGNAL_0)
2254 {
2255 clear_proceed_status ();
2256 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
2257 }
2258 }
2259 }
2260 else
2261 {
2262 /* The user requested a plain `attach', so be sure to leave
2263 the inferior stopped. */
2264
2265 if (target_can_async_p ())
2266 async_enable_stdin ();
2267
2268 /* At least the current thread is already stopped. */
2269
2270 /* In all-stop, by definition, all threads have to be already
2271 stopped at this point. In non-stop, however, although the
2272 selected thread is stopped, others may still be executing.
2273 Be sure to explicitly stop all threads of the process. This
2274 should have no effect on already stopped threads. */
2275 if (non_stop)
2276 target_stop (pid_to_ptid (inferior->pid));
2277
2278 /* Tell the user/frontend where we're stopped. */
2279 normal_stop ();
2280 if (deprecated_attach_hook)
2281 deprecated_attach_hook ();
2282 }
2283 }
2284
2285 struct attach_command_continuation_args
2286 {
2287 char *args;
2288 int from_tty;
2289 int async_exec;
2290 };
2291
2292 static void
2293 attach_command_continuation (void *args)
2294 {
2295 struct attach_command_continuation_args *a = args;
2296 attach_command_post_wait (a->args, a->from_tty, a->async_exec);
2297 }
2298
2299 static void
2300 attach_command_continuation_free_args (void *args)
2301 {
2302 struct attach_command_continuation_args *a = args;
2303 xfree (a->args);
2304 xfree (a);
2305 }
2306
2307 void
2308 attach_command (char *args, int from_tty)
2309 {
2310 char *exec_file;
2311 char *full_exec_path = NULL;
2312 int async_exec = 0;
2313 struct cleanup *back_to = make_cleanup (null_cleanup, NULL);
2314
2315 dont_repeat (); /* Not for the faint of heart */
2316
2317 if (gdbarch_has_global_solist (target_gdbarch))
2318 /* Don't complain if all processes share the same symbol
2319 space. */
2320 ;
2321 else if (target_has_execution)
2322 {
2323 if (query (_("A program is being debugged already. Kill it? ")))
2324 target_kill ();
2325 else
2326 error (_("Not killed."));
2327 }
2328
2329 /* Clean up any leftovers from other runs. Some other things from
2330 this function should probably be moved into target_pre_inferior. */
2331 target_pre_inferior (from_tty);
2332
2333 if (non_stop && !target_supports_non_stop ())
2334 error (_("Cannot attach to this target in non-stop mode"));
2335
2336 if (args)
2337 {
2338 async_exec = strip_bg_char (&args);
2339
2340 /* If we get a request for running in the bg but the target
2341 doesn't support it, error out. */
2342 if (async_exec && !target_can_async_p ())
2343 error (_("Asynchronous execution not supported on this target."));
2344 }
2345
2346 /* If we don't get a request of running in the bg, then we need
2347 to simulate synchronous (fg) execution. */
2348 if (!async_exec && target_can_async_p ())
2349 {
2350 /* Simulate synchronous execution */
2351 async_disable_stdin ();
2352 make_cleanup ((make_cleanup_ftype *)async_enable_stdin, NULL);
2353 }
2354
2355 target_attach (args, from_tty);
2356
2357 /* Set up the "saved terminal modes" of the inferior
2358 based on what modes we are starting it with. */
2359 target_terminal_init ();
2360
2361 /* Set up execution context to know that we should return from
2362 wait_for_inferior as soon as the target reports a stop. */
2363 init_wait_for_inferior ();
2364 clear_proceed_status ();
2365
2366 if (non_stop)
2367 {
2368 /* If we find that the current thread isn't stopped, explicitly
2369 do so now, because we're going to install breakpoints and
2370 poke at memory. */
2371
2372 if (async_exec)
2373 /* The user requested an `attach&'; stop just one thread. */
2374 target_stop (inferior_ptid);
2375 else
2376 /* The user requested an `attach', so stop all threads of this
2377 inferior. */
2378 target_stop (pid_to_ptid (ptid_get_pid (inferior_ptid)));
2379 }
2380
2381 /* Some system don't generate traps when attaching to inferior.
2382 E.g. Mach 3 or GNU hurd. */
2383 if (!target_attach_no_wait)
2384 {
2385 struct inferior *inferior = current_inferior ();
2386
2387 /* Careful here. See comments in inferior.h. Basically some
2388 OSes don't ignore SIGSTOPs on continue requests anymore. We
2389 need a way for handle_inferior_event to reset the stop_signal
2390 variable after an attach, and this is what
2391 STOP_QUIETLY_NO_SIGSTOP is for. */
2392 inferior->stop_soon = STOP_QUIETLY_NO_SIGSTOP;
2393
2394 if (target_can_async_p ())
2395 {
2396 /* sync_execution mode. Wait for stop. */
2397 struct attach_command_continuation_args *a;
2398
2399 a = xmalloc (sizeof (*a));
2400 a->args = xstrdup (args);
2401 a->from_tty = from_tty;
2402 a->async_exec = async_exec;
2403 add_inferior_continuation (attach_command_continuation, a,
2404 attach_command_continuation_free_args);
2405 discard_cleanups (back_to);
2406 return;
2407 }
2408
2409 wait_for_inferior (0);
2410 }
2411
2412 attach_command_post_wait (args, from_tty, async_exec);
2413 discard_cleanups (back_to);
2414 }
2415
2416 /* We had just found out that the target was already attached to an
2417 inferior. PTID points at a thread of this new inferior, that is
2418 the most likely to be stopped right now, but not necessarily so.
2419 The new inferior is assumed to be already added to the inferior
2420 list at this point. If LEAVE_RUNNING, then leave the threads of
2421 this inferior running, except those we've explicitly seen reported
2422 as stopped. */
2423
2424 void
2425 notice_new_inferior (ptid_t ptid, int leave_running, int from_tty)
2426 {
2427 struct cleanup* old_chain;
2428 int async_exec;
2429
2430 old_chain = make_cleanup (null_cleanup, NULL);
2431
2432 /* If in non-stop, leave threads as running as they were. If
2433 they're stopped for some reason other than us telling it to, the
2434 target reports a signal != TARGET_SIGNAL_0. We don't try to
2435 resume threads with such a stop signal. */
2436 async_exec = non_stop;
2437
2438 if (!ptid_equal (inferior_ptid, null_ptid))
2439 make_cleanup_restore_current_thread ();
2440
2441 switch_to_thread (ptid);
2442
2443 /* When we "notice" a new inferior we need to do all the things we
2444 would normally do if we had just attached to it. */
2445
2446 if (is_executing (inferior_ptid))
2447 {
2448 struct inferior *inferior = current_inferior ();
2449
2450 /* We're going to install breakpoints, and poke at memory,
2451 ensure that the inferior is stopped for a moment while we do
2452 that. */
2453 target_stop (inferior_ptid);
2454
2455 inferior->stop_soon = STOP_QUIETLY_REMOTE;
2456
2457 /* Wait for stop before proceeding. */
2458 if (target_can_async_p ())
2459 {
2460 struct attach_command_continuation_args *a;
2461
2462 a = xmalloc (sizeof (*a));
2463 a->args = xstrdup ("");
2464 a->from_tty = from_tty;
2465 a->async_exec = async_exec;
2466 add_inferior_continuation (attach_command_continuation, a,
2467 attach_command_continuation_free_args);
2468
2469 do_cleanups (old_chain);
2470 return;
2471 }
2472 else
2473 wait_for_inferior (0);
2474 }
2475
2476 async_exec = leave_running;
2477 attach_command_post_wait ("" /* args */, from_tty, async_exec);
2478
2479 do_cleanups (old_chain);
2480 }
2481
2482 /*
2483 * detach_command --
2484 * takes a program previously attached to and detaches it.
2485 * The program resumes execution and will no longer stop
2486 * on signals, etc. We better not have left any breakpoints
2487 * in the program or it'll die when it hits one. For this
2488 * to work, it may be necessary for the process to have been
2489 * previously attached. It *might* work if the program was
2490 * started via the normal ptrace (PTRACE_TRACEME).
2491 */
2492
2493 void
2494 detach_command (char *args, int from_tty)
2495 {
2496 dont_repeat (); /* Not for the faint of heart. */
2497
2498 if (ptid_equal (inferior_ptid, null_ptid))
2499 error (_("The program is not being run."));
2500
2501 target_detach (args, from_tty);
2502
2503 /* If the solist is global across inferiors, don't clear it when we
2504 detach from a single inferior. */
2505 if (!gdbarch_has_global_solist (target_gdbarch))
2506 no_shared_libraries (NULL, from_tty);
2507
2508 /* If we still have inferiors to debug, then don't mess with their
2509 threads. */
2510 if (!have_inferiors ())
2511 init_thread_list ();
2512
2513 if (deprecated_detach_hook)
2514 deprecated_detach_hook ();
2515 }
2516
2517 /* Disconnect from the current target without resuming it (leaving it
2518 waiting for a debugger).
2519
2520 We'd better not have left any breakpoints in the program or the
2521 next debugger will get confused. Currently only supported for some
2522 remote targets, since the normal attach mechanisms don't work on
2523 stopped processes on some native platforms (e.g. GNU/Linux). */
2524
2525 static void
2526 disconnect_command (char *args, int from_tty)
2527 {
2528 dont_repeat (); /* Not for the faint of heart */
2529 target_disconnect (args, from_tty);
2530 no_shared_libraries (NULL, from_tty);
2531 init_thread_list ();
2532 if (deprecated_detach_hook)
2533 deprecated_detach_hook ();
2534 }
2535
2536 void
2537 interrupt_target_1 (int all_threads)
2538 {
2539 ptid_t ptid;
2540 if (all_threads)
2541 ptid = minus_one_ptid;
2542 else
2543 ptid = inferior_ptid;
2544 target_stop (ptid);
2545
2546 /* Tag the thread as having been explicitly requested to stop, so
2547 other parts of gdb know not to resume this thread automatically,
2548 if it was stopped due to an internal event. Limit this to
2549 non-stop mode, as when debugging a multi-threaded application in
2550 all-stop mode, we will only get one stop event --- it's undefined
2551 which thread will report the event. */
2552 if (non_stop)
2553 set_stop_requested (ptid, 1);
2554 }
2555
2556 /* Stop the execution of the target while running in async mode, in
2557 the backgound. In all-stop, stop the whole process. In non-stop
2558 mode, stop the current thread only by default, or stop all threads
2559 if the `-a' switch is used. */
2560
2561 /* interrupt [-a] */
2562 void
2563 interrupt_target_command (char *args, int from_tty)
2564 {
2565 if (target_can_async_p ())
2566 {
2567 int all_threads = 0;
2568
2569 dont_repeat (); /* Not for the faint of heart */
2570
2571 if (args != NULL
2572 && strncmp (args, "-a", sizeof ("-a") - 1) == 0)
2573 all_threads = 1;
2574
2575 if (!non_stop && all_threads)
2576 error (_("-a is meaningless in all-stop mode."));
2577
2578 interrupt_target_1 (all_threads);
2579 }
2580 }
2581
2582 static void
2583 print_float_info (struct ui_file *file,
2584 struct frame_info *frame, const char *args)
2585 {
2586 struct gdbarch *gdbarch = get_frame_arch (frame);
2587
2588 if (gdbarch_print_float_info_p (gdbarch))
2589 gdbarch_print_float_info (gdbarch, file, frame, args);
2590 else
2591 {
2592 int regnum;
2593 int printed_something = 0;
2594
2595 for (regnum = 0;
2596 regnum < gdbarch_num_regs (gdbarch)
2597 + gdbarch_num_pseudo_regs (gdbarch);
2598 regnum++)
2599 {
2600 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
2601 {
2602 printed_something = 1;
2603 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
2604 }
2605 }
2606 if (!printed_something)
2607 fprintf_filtered (file, "\
2608 No floating-point info available for this processor.\n");
2609 }
2610 }
2611
2612 static void
2613 float_info (char *args, int from_tty)
2614 {
2615 if (!target_has_registers)
2616 error (_("The program has no registers now."));
2617
2618 print_float_info (gdb_stdout, get_selected_frame (NULL), args);
2619 }
2620 \f
2621 static void
2622 unset_command (char *args, int from_tty)
2623 {
2624 printf_filtered (_("\
2625 \"unset\" must be followed by the name of an unset subcommand.\n"));
2626 help_list (unsetlist, "unset ", -1, gdb_stdout);
2627 }
2628
2629 void
2630 _initialize_infcmd (void)
2631 {
2632 struct cmd_list_element *c = NULL;
2633
2634 /* add the filename of the terminal connected to inferior I/O */
2635 add_setshow_filename_cmd ("inferior-tty", class_run,
2636 &inferior_io_terminal, _("\
2637 Set terminal for future runs of program being debugged."), _("\
2638 Show terminal for future runs of program being debugged."), _("\
2639 Usage: set inferior-tty /dev/pts/1"), NULL, NULL, &setlist, &showlist);
2640 add_com_alias ("tty", "set inferior-tty", class_alias, 0);
2641
2642 add_setshow_optional_filename_cmd ("args", class_run,
2643 &inferior_args, _("\
2644 Set argument list to give program being debugged when it is started."), _("\
2645 Show argument list to give program being debugged when it is started."), _("\
2646 Follow this command with any number of args, to be passed to the program."),
2647 notice_args_set,
2648 notice_args_read,
2649 &setlist, &showlist);
2650
2651 c = add_cmd ("environment", no_class, environment_info, _("\
2652 The environment to give the program, or one variable's value.\n\
2653 With an argument VAR, prints the value of environment variable VAR to\n\
2654 give the program being debugged. With no arguments, prints the entire\n\
2655 environment to be given to the program."), &showlist);
2656 set_cmd_completer (c, noop_completer);
2657
2658 add_prefix_cmd ("unset", no_class, unset_command,
2659 _("Complement to certain \"set\" commands."),
2660 &unsetlist, "unset ", 0, &cmdlist);
2661
2662 c = add_cmd ("environment", class_run, unset_environment_command, _("\
2663 Cancel environment variable VAR for the program.\n\
2664 This does not affect the program until the next \"run\" command."),
2665 &unsetlist);
2666 set_cmd_completer (c, noop_completer);
2667
2668 c = add_cmd ("environment", class_run, set_environment_command, _("\
2669 Set environment variable value to give the program.\n\
2670 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
2671 VALUES of environment variables are uninterpreted strings.\n\
2672 This does not affect the program until the next \"run\" command."),
2673 &setlist);
2674 set_cmd_completer (c, noop_completer);
2675
2676 c = add_com ("path", class_files, path_command, _("\
2677 Add directory DIR(s) to beginning of search path for object files.\n\
2678 $cwd in the path means the current working directory.\n\
2679 This path is equivalent to the $PATH shell variable. It is a list of\n\
2680 directories, separated by colons. These directories are searched to find\n\
2681 fully linked executable files and separately compiled object files as needed."));
2682 set_cmd_completer (c, filename_completer);
2683
2684 c = add_cmd ("paths", no_class, path_info, _("\
2685 Current search path for finding object files.\n\
2686 $cwd in the path means the current working directory.\n\
2687 This path is equivalent to the $PATH shell variable. It is a list of\n\
2688 directories, separated by colons. These directories are searched to find\n\
2689 fully linked executable files and separately compiled object files as needed."),
2690 &showlist);
2691 set_cmd_completer (c, noop_completer);
2692
2693 add_com ("kill", class_run, kill_command,
2694 _("Kill execution of program being debugged."));
2695
2696 add_com ("attach", class_run, attach_command, _("\
2697 Attach to a process or file outside of GDB.\n\
2698 This command attaches to another target, of the same type as your last\n\
2699 \"target\" command (\"info files\" will show your target stack).\n\
2700 The command may take as argument a process id or a device file.\n\
2701 For a process id, you must have permission to send the process a signal,\n\
2702 and it must have the same effective uid as the debugger.\n\
2703 When using \"attach\" with a process id, the debugger finds the\n\
2704 program running in the process, looking first in the current working\n\
2705 directory, or (if not found there) using the source file search path\n\
2706 (see the \"directory\" command). You can also use the \"file\" command\n\
2707 to specify the program, and to load its symbol table."));
2708
2709 add_prefix_cmd ("detach", class_run, detach_command, _("\
2710 Detach a process or file previously attached.\n\
2711 If a process, it is no longer traced, and it continues its execution. If\n\
2712 you were debugging a file, the file is closed and gdb no longer accesses it."),
2713 &detachlist, "detach ", 0, &cmdlist);
2714
2715 add_com ("disconnect", class_run, disconnect_command, _("\
2716 Disconnect from a target.\n\
2717 The target will wait for another debugger to connect. Not available for\n\
2718 all targets."));
2719
2720 add_com ("signal", class_run, signal_command, _("\
2721 Continue program giving it signal specified by the argument.\n\
2722 An argument of \"0\" means continue program without giving it a signal."));
2723
2724 add_com ("stepi", class_run, stepi_command, _("\
2725 Step one instruction exactly.\n\
2726 Argument N means do this N times (or till program stops for another reason)."));
2727 add_com_alias ("si", "stepi", class_alias, 0);
2728
2729 add_com ("nexti", class_run, nexti_command, _("\
2730 Step one instruction, but proceed through subroutine calls.\n\
2731 Argument N means do this N times (or till program stops for another reason)."));
2732 add_com_alias ("ni", "nexti", class_alias, 0);
2733
2734 add_com ("finish", class_run, finish_command, _("\
2735 Execute until selected stack frame returns.\n\
2736 Upon return, the value returned is printed and put in the value history."));
2737 add_com_alias ("fin", "finish", class_run, 1);
2738
2739 add_com ("next", class_run, next_command, _("\
2740 Step program, proceeding through subroutine calls.\n\
2741 Like the \"step\" command as long as subroutine calls do not happen;\n\
2742 when they do, the call is treated as one instruction.\n\
2743 Argument N means do this N times (or till program stops for another reason)."));
2744 add_com_alias ("n", "next", class_run, 1);
2745 if (xdb_commands)
2746 add_com_alias ("S", "next", class_run, 1);
2747
2748 add_com ("step", class_run, step_command, _("\
2749 Step program until it reaches a different source line.\n\
2750 Argument N means do this N times (or till program stops for another reason)."));
2751 add_com_alias ("s", "step", class_run, 1);
2752
2753 c = add_com ("until", class_run, until_command, _("\
2754 Execute until the program reaches a source line greater than the current\n\
2755 or a specified location (same args as break command) within the current frame."));
2756 set_cmd_completer (c, location_completer);
2757 add_com_alias ("u", "until", class_run, 1);
2758
2759 c = add_com ("advance", class_run, advance_command, _("\
2760 Continue the program up to the given location (same form as args for break command).\n\
2761 Execution will also stop upon exit from the current stack frame."));
2762 set_cmd_completer (c, location_completer);
2763
2764 c = add_com ("jump", class_run, jump_command, _("\
2765 Continue program being debugged at specified line or address.\n\
2766 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
2767 for an address to start at."));
2768 set_cmd_completer (c, location_completer);
2769
2770 if (xdb_commands)
2771 {
2772 c = add_com ("go", class_run, go_command, _("\
2773 Usage: go <location>\n\
2774 Continue program being debugged, stopping at specified line or \n\
2775 address.\n\
2776 Give as argument either LINENUM or *ADDR, where ADDR is an \n\
2777 expression for an address to start at.\n\
2778 This command is a combination of tbreak and jump."));
2779 set_cmd_completer (c, location_completer);
2780 }
2781
2782 if (xdb_commands)
2783 add_com_alias ("g", "go", class_run, 1);
2784
2785 c = add_com ("continue", class_run, continue_command, _("\
2786 Continue program being debugged, after signal or breakpoint.\n\
2787 If proceeding from breakpoint, a number N may be used as an argument,\n\
2788 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
2789 the breakpoint won't break until the Nth time it is reached).\n\
2790 \n\
2791 If non-stop mode is enabled, continue only the current thread,\n\
2792 otherwise all the threads in the program are continued. To \n\
2793 continue all stopped threads in non-stop mode, use the -a option.\n\
2794 Specifying -a and an ignore count simultaneously is an error."));
2795 add_com_alias ("c", "cont", class_run, 1);
2796 add_com_alias ("fg", "cont", class_run, 1);
2797
2798 c = add_com ("run", class_run, run_command, _("\
2799 Start debugged program. You may specify arguments to give it.\n\
2800 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
2801 Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
2802 With no arguments, uses arguments last specified (with \"run\" or \"set args\").\n\
2803 To cancel previous arguments and run with no arguments,\n\
2804 use \"set args\" without arguments."));
2805 set_cmd_completer (c, filename_completer);
2806 add_com_alias ("r", "run", class_run, 1);
2807 if (xdb_commands)
2808 add_com ("R", class_run, run_no_args_command,
2809 _("Start debugged program with no arguments."));
2810
2811 c = add_com ("start", class_run, start_command, _("\
2812 Run the debugged program until the beginning of the main procedure.\n\
2813 You may specify arguments to give to your program, just as with the\n\
2814 \"run\" command."));
2815 set_cmd_completer (c, filename_completer);
2816
2817 c = add_com ("interrupt", class_run, interrupt_target_command,
2818 _("Interrupt the execution of the debugged program.\n\
2819 If non-stop mode is enabled, interrupt only the current thread,\n\
2820 otherwise all the threads in the program are stopped. To \n\
2821 interrupt all running threads in non-stop mode, use the -a option."));
2822
2823 add_info ("registers", nofp_registers_info, _("\
2824 List of integer registers and their contents, for selected stack frame.\n\
2825 Register name as argument means describe only that register."));
2826 add_info_alias ("r", "registers", 1);
2827
2828 if (xdb_commands)
2829 add_com ("lr", class_info, nofp_registers_info, _("\
2830 List of integer registers and their contents, for selected stack frame.\n\
2831 Register name as argument means describe only that register."));
2832 add_info ("all-registers", all_registers_info, _("\
2833 List of all registers and their contents, for selected stack frame.\n\
2834 Register name as argument means describe only that register."));
2835
2836 add_info ("program", program_info,
2837 _("Execution status of the program."));
2838
2839 add_info ("float", float_info,
2840 _("Print the status of the floating point unit\n"));
2841
2842 add_info ("vector", vector_info,
2843 _("Print the status of the vector unit\n"));
2844
2845 inferior_environ = make_environ ();
2846 init_environ (inferior_environ);
2847 }
This page took 0.151222 seconds and 4 git commands to generate.