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