* dsrec.c (load_srec, make_srec): Use bfd_get_section_size instead of
[binutils-gdb.git] / gdb / infcmd.c
1 /* Memory-access and commands for "inferior" process, for GDB.
2 Copyright 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
3 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22
23 #include "defs.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 <ctype.h>
47 #include "gdb_assert.h"
48
49 /* Functions exported for general use, in inferior.h: */
50
51 void all_registers_info (char *, int);
52
53 void registers_info (char *, int);
54
55 void nexti_command (char *, int);
56
57 void stepi_command (char *, int);
58
59 void continue_command (char *, int);
60
61 void interrupt_target_command (char *args, int from_tty);
62
63 /* Local functions: */
64
65 static void nofp_registers_info (char *, int);
66
67 static void print_return_value (int struct_return, struct type *value_type);
68
69 static void finish_command_continuation (struct continuation_arg *);
70
71 static void until_next_command (int);
72
73 static void until_command (char *, int);
74
75 static void path_info (char *, int);
76
77 static void path_command (char *, int);
78
79 static void unset_command (char *, int);
80
81 static void float_info (char *, int);
82
83 static void detach_command (char *, int);
84
85 static void disconnect_command (char *, int);
86
87 static void unset_environment_command (char *, int);
88
89 static void set_environment_command (char *, int);
90
91 static void environment_info (char *, int);
92
93 static void program_info (char *, int);
94
95 static void finish_command (char *, int);
96
97 static void signal_command (char *, int);
98
99 static void jump_command (char *, int);
100
101 static void step_1 (int, int, char *);
102 static void step_once (int skip_subroutines, int single_inst, int count);
103 static void step_1_continuation (struct continuation_arg *arg);
104
105 static void next_command (char *, int);
106
107 static void step_command (char *, int);
108
109 static void run_command (char *, int);
110
111 static void run_no_args_command (char *args, int from_tty);
112
113 static void go_command (char *line_no, int from_tty);
114
115 static int strip_bg_char (char **);
116
117 void _initialize_infcmd (void);
118
119 #define GO_USAGE "Usage: go <location>\n"
120
121 #define ERROR_NO_INFERIOR \
122 if (!target_has_execution) error ("The program is not being run.");
123
124 /* String containing arguments to give to the program, separated by spaces.
125 Empty string (pointer to '\0') means no args. */
126
127 static char *inferior_args;
128
129 /* The inferior arguments as a vector. If INFERIOR_ARGC is nonzero,
130 then we must compute INFERIOR_ARGS from this (via the target). */
131
132 static int inferior_argc;
133 static char **inferior_argv;
134
135 /* File name for default use for standard in/out in the inferior. */
136
137 char *inferior_io_terminal;
138
139 /* Pid of our debugged inferior, or 0 if no inferior now.
140 Since various parts of infrun.c test this to see whether there is a program
141 being debugged it should be nonzero (currently 3 is used) for remote
142 debugging. */
143
144 ptid_t inferior_ptid;
145
146 /* Last signal that the inferior received (why it stopped). */
147
148 enum target_signal stop_signal;
149
150 /* Address at which inferior stopped. */
151
152 CORE_ADDR stop_pc;
153
154 /* Chain containing status of breakpoint(s) that we have stopped at. */
155
156 bpstat stop_bpstat;
157
158 /* Flag indicating that a command has proceeded the inferior past the
159 current breakpoint. */
160
161 int breakpoint_proceeded;
162
163 /* Nonzero if stopped due to a step command. */
164
165 int stop_step;
166
167 /* Nonzero if stopped due to completion of a stack dummy routine. */
168
169 int stop_stack_dummy;
170
171 /* Nonzero if stopped due to a random (unexpected) signal in inferior
172 process. */
173
174 int stopped_by_random_signal;
175
176 /* Range to single step within.
177 If this is nonzero, respond to a single-step signal
178 by continuing to step if the pc is in this range. */
179
180 CORE_ADDR step_range_start; /* Inclusive */
181 CORE_ADDR step_range_end; /* Exclusive */
182
183 /* Stack frame address as of when stepping command was issued.
184 This is how we know when we step into a subroutine call,
185 and how to set the frame for the breakpoint used to step out. */
186
187 struct frame_id step_frame_id;
188
189 enum step_over_calls_kind step_over_calls;
190
191 /* If stepping, nonzero means step count is > 1
192 so don't print frame next time inferior stops
193 if it stops due to stepping. */
194
195 int step_multi;
196
197 /* Environment to use for running inferior,
198 in format described in environ.h. */
199
200 struct environ *inferior_environ;
201 \f
202 /* Accessor routines. */
203
204 char *
205 get_inferior_args (void)
206 {
207 if (inferior_argc != 0)
208 {
209 char *n, *old;
210
211 n = gdbarch_construct_inferior_arguments (current_gdbarch,
212 inferior_argc, inferior_argv);
213 old = set_inferior_args (n);
214 xfree (old);
215 }
216
217 if (inferior_args == NULL)
218 inferior_args = xstrdup ("");
219
220 return inferior_args;
221 }
222
223 char *
224 set_inferior_args (char *newargs)
225 {
226 char *saved_args = inferior_args;
227
228 inferior_args = newargs;
229 inferior_argc = 0;
230 inferior_argv = 0;
231
232 return saved_args;
233 }
234
235 void
236 set_inferior_args_vector (int argc, char **argv)
237 {
238 inferior_argc = argc;
239 inferior_argv = argv;
240 }
241
242 /* Notice when `set args' is run. */
243 static void
244 notice_args_set (char *args, int from_tty, struct cmd_list_element *c)
245 {
246 inferior_argc = 0;
247 inferior_argv = 0;
248 }
249
250 /* Notice when `show args' is run. */
251 static void
252 notice_args_read (char *args, int from_tty, struct cmd_list_element *c)
253 {
254 /* Might compute the value. */
255 get_inferior_args ();
256 }
257
258 \f
259 /* Compute command-line string given argument vector. This does the
260 same shell processing as fork_inferior. */
261 char *
262 construct_inferior_arguments (struct gdbarch *gdbarch, int argc, char **argv)
263 {
264 char *result;
265
266 if (STARTUP_WITH_SHELL)
267 {
268 /* This holds all the characters considered special to the
269 typical Unix shells. We include `^' because the SunOS
270 /bin/sh treats it as a synonym for `|'. */
271 char *special = "\"!#$&*()\\|[]{}<>?'\"`~^; \t\n";
272 int i;
273 int length = 0;
274 char *out, *cp;
275
276 /* We over-compute the size. It shouldn't matter. */
277 for (i = 0; i < argc; ++i)
278 length += 2 * strlen (argv[i]) + 1 + 2 * (argv[i][0] == '\0');
279
280 result = (char *) xmalloc (length);
281 out = result;
282
283 for (i = 0; i < argc; ++i)
284 {
285 if (i > 0)
286 *out++ = ' ';
287
288 /* Need to handle empty arguments specially. */
289 if (argv[i][0] == '\0')
290 {
291 *out++ = '\'';
292 *out++ = '\'';
293 }
294 else
295 {
296 for (cp = argv[i]; *cp; ++cp)
297 {
298 if (strchr (special, *cp) != NULL)
299 *out++ = '\\';
300 *out++ = *cp;
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 inferior_io_terminal = savestring (file, strlen (file));
376 }
377
378 /* Kill the inferior if already running. This function is designed
379 to be called when we are about to start the execution of the program
380 from the beginning. Ask the user to confirm that he wants to restart
381 the program being debugged when FROM_TTY is non-null. */
382
383 void
384 kill_if_already_running (int from_tty)
385 {
386 if (! ptid_equal (inferior_ptid, null_ptid) && target_has_execution)
387 {
388 if (from_tty
389 && !query ("The program being debugged has been started already.\n\
390 Start it from the beginning? "))
391 error ("Program not restarted.");
392 target_kill ();
393 #if defined(SOLIB_RESTART)
394 SOLIB_RESTART ();
395 #endif
396 init_wait_for_inferior ();
397 }
398 }
399
400 static void
401 run_command (char *args, int from_tty)
402 {
403 char *exec_file;
404
405 dont_repeat ();
406
407 kill_if_already_running (from_tty);
408 clear_breakpoint_hit_counts ();
409
410 /* Purge old solib objfiles. */
411 objfile_purge_solibs ();
412
413 do_run_cleanups (NULL);
414
415 /* The comment here used to read, "The exec file is re-read every
416 time we do a generic_mourn_inferior, so we just have to worry
417 about the symbol file." The `generic_mourn_inferior' function
418 gets called whenever the program exits. However, suppose the
419 program exits, and *then* the executable file changes? We need
420 to check again here. Since reopen_exec_file doesn't do anything
421 if the timestamp hasn't changed, I don't see the harm. */
422 reopen_exec_file ();
423 reread_symbols ();
424
425 exec_file = (char *) get_exec_file (0);
426
427 /* We keep symbols from add-symbol-file, on the grounds that the
428 user might want to add some symbols before running the program
429 (right?). But sometimes (dynamic loading where the user manually
430 introduces the new symbols with add-symbol-file), the code which
431 the symbols describe does not persist between runs. Currently
432 the user has to manually nuke all symbols between runs if they
433 want them to go away (PR 2207). This is probably reasonable. */
434
435 if (!args)
436 {
437 if (event_loop_p && target_can_async_p ())
438 async_disable_stdin ();
439 }
440 else
441 {
442 int async_exec = strip_bg_char (&args);
443
444 /* If we get a request for running in the bg but the target
445 doesn't support it, error out. */
446 if (event_loop_p && async_exec && !target_can_async_p ())
447 error ("Asynchronous execution not supported on this target.");
448
449 /* If we don't get a request of running in the bg, then we need
450 to simulate synchronous (fg) execution. */
451 if (event_loop_p && !async_exec && target_can_async_p ())
452 {
453 /* Simulate synchronous execution */
454 async_disable_stdin ();
455 }
456
457 /* If there were other args, beside '&', process them. */
458 if (args)
459 {
460 char *old_args = set_inferior_args (xstrdup (args));
461 xfree (old_args);
462 }
463 }
464
465 if (from_tty)
466 {
467 ui_out_field_string (uiout, NULL, "Starting program");
468 ui_out_text (uiout, ": ");
469 if (exec_file)
470 ui_out_field_string (uiout, "execfile", exec_file);
471 ui_out_spaces (uiout, 1);
472 /* We call get_inferior_args() because we might need to compute
473 the value now. */
474 ui_out_field_string (uiout, "infargs", get_inferior_args ());
475 ui_out_text (uiout, "\n");
476 ui_out_flush (uiout);
477 }
478
479 /* We call get_inferior_args() because we might need to compute
480 the value now. */
481 target_create_inferior (exec_file, get_inferior_args (),
482 environ_vector (inferior_environ), from_tty);
483 }
484
485
486 static void
487 run_no_args_command (char *args, int from_tty)
488 {
489 char *old_args = set_inferior_args (xstrdup (""));
490 xfree (old_args);
491 }
492 \f
493
494 /* Start the execution of the program up until the beginning of the main
495 program. */
496
497 static void
498 start_command (char *args, int from_tty)
499 {
500 /* Some languages such as Ada need to search inside the program
501 minimal symbols for the location where to put the temporary
502 breakpoint before starting. */
503 if (!have_minimal_symbols ())
504 error ("No symbol table loaded. Use the \"file\" command.");
505
506 /* If the inferior is already running, we want to ask the user if we
507 should restart it or not before we insert the temporary breakpoint.
508 This makes sure that this command doesn't have any side effect if
509 the user changes its mind. */
510 kill_if_already_running (from_tty);
511
512 /* Insert the temporary breakpoint, and run... */
513 tbreak_command (main_name (), 0);
514 run_command (args, from_tty);
515 }
516
517 void
518 continue_command (char *proc_count_exp, int from_tty)
519 {
520 int async_exec = 0;
521 ERROR_NO_INFERIOR;
522
523 /* Find out whether we must run in the background. */
524 if (proc_count_exp != NULL)
525 async_exec = strip_bg_char (&proc_count_exp);
526
527 /* If we must run in the background, but the target can't do it,
528 error out. */
529 if (event_loop_p && async_exec && !target_can_async_p ())
530 error ("Asynchronous execution not supported on this target.");
531
532 /* If we are not asked to run in the bg, then prepare to run in the
533 foreground, synchronously. */
534 if (event_loop_p && !async_exec && target_can_async_p ())
535 {
536 /* Simulate synchronous execution */
537 async_disable_stdin ();
538 }
539
540 /* If have argument (besides '&'), set proceed count of breakpoint
541 we stopped at. */
542 if (proc_count_exp != NULL)
543 {
544 bpstat bs = stop_bpstat;
545 int num = bpstat_num (&bs);
546 if (num == 0 && from_tty)
547 {
548 printf_filtered
549 ("Not stopped at any breakpoint; argument ignored.\n");
550 }
551 while (num != 0)
552 {
553 set_ignore_count (num,
554 parse_and_eval_long (proc_count_exp) - 1,
555 from_tty);
556 /* set_ignore_count prints a message ending with a period.
557 So print two spaces before "Continuing.". */
558 if (from_tty)
559 printf_filtered (" ");
560 num = bpstat_num (&bs);
561 }
562 }
563
564 if (from_tty)
565 printf_filtered ("Continuing.\n");
566
567 clear_proceed_status ();
568
569 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
570 }
571 \f
572 /* Step until outside of current statement. */
573
574 static void
575 step_command (char *count_string, int from_tty)
576 {
577 step_1 (0, 0, count_string);
578 }
579
580 /* Likewise, but skip over subroutine calls as if single instructions. */
581
582 static void
583 next_command (char *count_string, int from_tty)
584 {
585 step_1 (1, 0, count_string);
586 }
587
588 /* Likewise, but step only one instruction. */
589
590 void
591 stepi_command (char *count_string, int from_tty)
592 {
593 step_1 (0, 1, count_string);
594 }
595
596 void
597 nexti_command (char *count_string, int from_tty)
598 {
599 step_1 (1, 1, count_string);
600 }
601
602 static void
603 disable_longjmp_breakpoint_cleanup (void *ignore)
604 {
605 disable_longjmp_breakpoint ();
606 }
607
608 static void
609 step_1 (int skip_subroutines, int single_inst, char *count_string)
610 {
611 int count = 1;
612 struct frame_info *frame;
613 struct cleanup *cleanups = 0;
614 int async_exec = 0;
615
616 ERROR_NO_INFERIOR;
617
618 if (count_string)
619 async_exec = strip_bg_char (&count_string);
620
621 /* If we get a request for running in the bg but the target
622 doesn't support it, error out. */
623 if (event_loop_p && async_exec && !target_can_async_p ())
624 error ("Asynchronous execution not supported on this target.");
625
626 /* If we don't get a request of running in the bg, then we need
627 to simulate synchronous (fg) execution. */
628 if (event_loop_p && !async_exec && target_can_async_p ())
629 {
630 /* Simulate synchronous execution */
631 async_disable_stdin ();
632 }
633
634 count = count_string ? parse_and_eval_long (count_string) : 1;
635
636 if (!single_inst || skip_subroutines) /* leave si command alone */
637 {
638 enable_longjmp_breakpoint ();
639 if (!event_loop_p || !target_can_async_p ())
640 cleanups = make_cleanup (disable_longjmp_breakpoint_cleanup, 0 /*ignore*/);
641 else
642 make_exec_cleanup (disable_longjmp_breakpoint_cleanup, 0 /*ignore*/);
643 }
644
645 /* In synchronous case, all is well, just use the regular for loop. */
646 if (!event_loop_p || !target_can_async_p ())
647 {
648 for (; count > 0; count--)
649 {
650 clear_proceed_status ();
651
652 frame = get_current_frame ();
653 if (!frame) /* Avoid coredump here. Why tho? */
654 error ("No current frame");
655 step_frame_id = get_frame_id (frame);
656
657 if (!single_inst)
658 {
659 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
660 if (step_range_end == 0)
661 {
662 char *name;
663 if (find_pc_partial_function (stop_pc, &name, &step_range_start,
664 &step_range_end) == 0)
665 error ("Cannot find bounds of current function");
666
667 target_terminal_ours ();
668 printf_filtered ("\
669 Single stepping until exit from function %s, \n\
670 which has no line number information.\n", name);
671 }
672 }
673 else
674 {
675 /* Say we are stepping, but stop after one insn whatever it does. */
676 step_range_start = step_range_end = 1;
677 if (!skip_subroutines)
678 /* It is stepi.
679 Don't step over function calls, not even to functions lacking
680 line numbers. */
681 step_over_calls = STEP_OVER_NONE;
682 }
683
684 if (skip_subroutines)
685 step_over_calls = STEP_OVER_ALL;
686
687 step_multi = (count > 1);
688 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
689
690 if (!stop_step)
691 break;
692 }
693
694 if (!single_inst || skip_subroutines)
695 do_cleanups (cleanups);
696 return;
697 }
698 /* In case of asynchronous target things get complicated, do only
699 one step for now, before returning control to the event loop. Let
700 the continuation figure out how many other steps we need to do,
701 and handle them one at the time, through step_once(). */
702 else
703 {
704 if (event_loop_p && target_can_async_p ())
705 step_once (skip_subroutines, single_inst, count);
706 }
707 }
708
709 /* Called after we are done with one step operation, to check whether
710 we need to step again, before we print the prompt and return control
711 to the user. If count is > 1, we will need to do one more call to
712 proceed(), via step_once(). Basically it is like step_once and
713 step_1_continuation are co-recursive. */
714 static void
715 step_1_continuation (struct continuation_arg *arg)
716 {
717 int count;
718 int skip_subroutines;
719 int single_inst;
720
721 skip_subroutines = arg->data.integer;
722 single_inst = arg->next->data.integer;
723 count = arg->next->next->data.integer;
724
725 if (stop_step)
726 step_once (skip_subroutines, single_inst, count - 1);
727 else
728 if (!single_inst || skip_subroutines)
729 do_exec_cleanups (ALL_CLEANUPS);
730 }
731
732 /* Do just one step operation. If count >1 we will have to set up a
733 continuation to be done after the target stops (after this one
734 step). This is useful to implement the 'step n' kind of commands, in
735 case of asynchronous targets. We had to split step_1 into two parts,
736 one to be done before proceed() and one afterwards. This function is
737 called in case of step n with n>1, after the first step operation has
738 been completed.*/
739 static void
740 step_once (int skip_subroutines, int single_inst, int count)
741 {
742 struct continuation_arg *arg1;
743 struct continuation_arg *arg2;
744 struct continuation_arg *arg3;
745 struct frame_info *frame;
746
747 if (count > 0)
748 {
749 clear_proceed_status ();
750
751 frame = get_current_frame ();
752 if (!frame) /* Avoid coredump here. Why tho? */
753 error ("No current frame");
754 step_frame_id = get_frame_id (frame);
755
756 if (!single_inst)
757 {
758 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
759
760 /* If we have no line info, switch to stepi mode. */
761 if (step_range_end == 0 && step_stop_if_no_debug)
762 {
763 step_range_start = step_range_end = 1;
764 }
765 else if (step_range_end == 0)
766 {
767 char *name;
768 if (find_pc_partial_function (stop_pc, &name, &step_range_start,
769 &step_range_end) == 0)
770 error ("Cannot find bounds of current function");
771
772 target_terminal_ours ();
773 printf_filtered ("\
774 Single stepping until exit from function %s, \n\
775 which has no line number information.\n", name);
776 }
777 }
778 else
779 {
780 /* Say we are stepping, but stop after one insn whatever it does. */
781 step_range_start = step_range_end = 1;
782 if (!skip_subroutines)
783 /* It is stepi.
784 Don't step over function calls, not even to functions lacking
785 line numbers. */
786 step_over_calls = STEP_OVER_NONE;
787 }
788
789 if (skip_subroutines)
790 step_over_calls = STEP_OVER_ALL;
791
792 step_multi = (count > 1);
793 arg1 =
794 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
795 arg2 =
796 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
797 arg3 =
798 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
799 arg1->next = arg2;
800 arg1->data.integer = skip_subroutines;
801 arg2->next = arg3;
802 arg2->data.integer = single_inst;
803 arg3->next = NULL;
804 arg3->data.integer = count;
805 add_intermediate_continuation (step_1_continuation, arg1);
806 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
807 }
808 }
809
810 \f
811 /* Continue program at specified address. */
812
813 static void
814 jump_command (char *arg, int from_tty)
815 {
816 CORE_ADDR addr;
817 struct symtabs_and_lines sals;
818 struct symtab_and_line sal;
819 struct symbol *fn;
820 struct symbol *sfn;
821 int async_exec = 0;
822
823 ERROR_NO_INFERIOR;
824
825 /* Find out whether we must run in the background. */
826 if (arg != NULL)
827 async_exec = strip_bg_char (&arg);
828
829 /* If we must run in the background, but the target can't do it,
830 error out. */
831 if (event_loop_p && async_exec && !target_can_async_p ())
832 error ("Asynchronous execution not supported on this target.");
833
834 /* If we are not asked to run in the bg, then prepare to run in the
835 foreground, synchronously. */
836 if (event_loop_p && !async_exec && target_can_async_p ())
837 {
838 /* Simulate synchronous execution */
839 async_disable_stdin ();
840 }
841
842 if (!arg)
843 error_no_arg ("starting address");
844
845 sals = decode_line_spec_1 (arg, 1);
846 if (sals.nelts != 1)
847 {
848 error ("Unreasonable jump request");
849 }
850
851 sal = sals.sals[0];
852 xfree (sals.sals);
853
854 if (sal.symtab == 0 && sal.pc == 0)
855 error ("No source file has been specified.");
856
857 resolve_sal_pc (&sal); /* May error out */
858
859 /* See if we are trying to jump to another function. */
860 fn = get_frame_function (get_current_frame ());
861 sfn = find_pc_function (sal.pc);
862 if (fn != NULL && sfn != fn)
863 {
864 if (!query ("Line %d is not in `%s'. Jump anyway? ", sal.line,
865 SYMBOL_PRINT_NAME (fn)))
866 {
867 error ("Not confirmed.");
868 /* NOTREACHED */
869 }
870 }
871
872 if (sfn != NULL)
873 {
874 fixup_symbol_section (sfn, 0);
875 if (section_is_overlay (SYMBOL_BFD_SECTION (sfn)) &&
876 !section_is_mapped (SYMBOL_BFD_SECTION (sfn)))
877 {
878 if (!query ("WARNING!!! Destination is in unmapped overlay! Jump anyway? "))
879 {
880 error ("Not confirmed.");
881 /* NOTREACHED */
882 }
883 }
884 }
885
886 addr = sal.pc;
887
888 if (from_tty)
889 {
890 printf_filtered ("Continuing at ");
891 print_address_numeric (addr, 1, gdb_stdout);
892 printf_filtered (".\n");
893 }
894
895 clear_proceed_status ();
896 proceed (addr, TARGET_SIGNAL_0, 0);
897 }
898 \f
899
900 /* Go to line or address in current procedure */
901 static void
902 go_command (char *line_no, int from_tty)
903 {
904 if (line_no == (char *) NULL || !*line_no)
905 printf_filtered (GO_USAGE);
906 else
907 {
908 tbreak_command (line_no, from_tty);
909 jump_command (line_no, from_tty);
910 }
911 }
912 \f
913
914 /* Continue program giving it specified signal. */
915
916 static void
917 signal_command (char *signum_exp, int from_tty)
918 {
919 enum target_signal oursig;
920
921 dont_repeat (); /* Too dangerous. */
922 ERROR_NO_INFERIOR;
923
924 if (!signum_exp)
925 error_no_arg ("signal number");
926
927 /* It would be even slicker to make signal names be valid expressions,
928 (the type could be "enum $signal" or some such), then the user could
929 assign them to convenience variables. */
930 oursig = target_signal_from_name (signum_exp);
931
932 if (oursig == TARGET_SIGNAL_UNKNOWN)
933 {
934 /* No, try numeric. */
935 int num = parse_and_eval_long (signum_exp);
936
937 if (num == 0)
938 oursig = TARGET_SIGNAL_0;
939 else
940 oursig = target_signal_from_command (num);
941 }
942
943 if (from_tty)
944 {
945 if (oursig == TARGET_SIGNAL_0)
946 printf_filtered ("Continuing with no signal.\n");
947 else
948 printf_filtered ("Continuing with signal %s.\n",
949 target_signal_to_name (oursig));
950 }
951
952 clear_proceed_status ();
953 /* "signal 0" should not get stuck if we are stopped at a breakpoint.
954 FIXME: Neither should "signal foo" but when I tried passing
955 (CORE_ADDR)-1 unconditionally I got a testsuite failure which I haven't
956 tried to track down yet. */
957 proceed (oursig == TARGET_SIGNAL_0 ? (CORE_ADDR) -1 : stop_pc, oursig, 0);
958 }
959
960 /* Proceed until we reach a different source line with pc greater than
961 our current one or exit the function. We skip calls in both cases.
962
963 Note that eventually this command should probably be changed so
964 that only source lines are printed out when we hit the breakpoint
965 we set. This may involve changes to wait_for_inferior and the
966 proceed status code. */
967
968 static void
969 until_next_command (int from_tty)
970 {
971 struct frame_info *frame;
972 CORE_ADDR pc;
973 struct symbol *func;
974 struct symtab_and_line sal;
975
976 clear_proceed_status ();
977
978 frame = get_current_frame ();
979
980 /* Step until either exited from this function or greater
981 than the current line (if in symbolic section) or pc (if
982 not). */
983
984 pc = read_pc ();
985 func = find_pc_function (pc);
986
987 if (!func)
988 {
989 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
990
991 if (msymbol == NULL)
992 error ("Execution is not within a known function.");
993
994 step_range_start = SYMBOL_VALUE_ADDRESS (msymbol);
995 step_range_end = pc;
996 }
997 else
998 {
999 sal = find_pc_line (pc, 0);
1000
1001 step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
1002 step_range_end = sal.end;
1003 }
1004
1005 step_over_calls = STEP_OVER_ALL;
1006 step_frame_id = get_frame_id (frame);
1007
1008 step_multi = 0; /* Only one call to proceed */
1009
1010 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 1);
1011 }
1012
1013 static void
1014 until_command (char *arg, int from_tty)
1015 {
1016 int async_exec = 0;
1017
1018 if (!target_has_execution)
1019 error ("The program is not running.");
1020
1021 /* Find out whether we must run in the background. */
1022 if (arg != NULL)
1023 async_exec = strip_bg_char (&arg);
1024
1025 /* If we must run in the background, but the target can't do it,
1026 error out. */
1027 if (event_loop_p && async_exec && !target_can_async_p ())
1028 error ("Asynchronous execution not supported on this target.");
1029
1030 /* If we are not asked to run in the bg, then prepare to run in the
1031 foreground, synchronously. */
1032 if (event_loop_p && !async_exec && target_can_async_p ())
1033 {
1034 /* Simulate synchronous execution */
1035 async_disable_stdin ();
1036 }
1037
1038 if (arg)
1039 until_break_command (arg, from_tty, 0);
1040 else
1041 until_next_command (from_tty);
1042 }
1043
1044 static void
1045 advance_command (char *arg, int from_tty)
1046 {
1047 int async_exec = 0;
1048
1049 if (!target_has_execution)
1050 error ("The program is not running.");
1051
1052 if (arg == NULL)
1053 error_no_arg ("a location");
1054
1055 /* Find out whether we must run in the background. */
1056 if (arg != NULL)
1057 async_exec = strip_bg_char (&arg);
1058
1059 /* If we must run in the background, but the target can't do it,
1060 error out. */
1061 if (event_loop_p && async_exec && !target_can_async_p ())
1062 error ("Asynchronous execution not supported on this target.");
1063
1064 /* If we are not asked to run in the bg, then prepare to run in the
1065 foreground, synchronously. */
1066 if (event_loop_p && !async_exec && target_can_async_p ())
1067 {
1068 /* Simulate synchronous execution. */
1069 async_disable_stdin ();
1070 }
1071
1072 until_break_command (arg, from_tty, 1);
1073 }
1074 \f
1075 /* Print the result of a function at the end of a 'finish' command. */
1076
1077 static void
1078 print_return_value (int struct_return, struct type *value_type)
1079 {
1080 struct gdbarch *gdbarch = current_gdbarch;
1081 struct cleanup *old_chain;
1082 struct ui_stream *stb;
1083 struct value *value;
1084
1085 gdb_assert (TYPE_CODE (value_type) != TYPE_CODE_VOID);
1086
1087 /* FIXME: 2003-09-27: When returning from a nested inferior function
1088 call, it's possible (with no help from the architecture vector)
1089 to locate and return/print a "struct return" value. This is just
1090 a more complicated case of what is already being done in in the
1091 inferior function call code. In fact, when inferior function
1092 calls are made async, this will likely be made the norm. */
1093
1094 if (gdbarch_return_value_p (gdbarch))
1095 {
1096 switch (gdbarch_return_value (gdbarch, value_type, NULL, NULL, NULL))
1097 {
1098 case RETURN_VALUE_REGISTER_CONVENTION:
1099 case RETURN_VALUE_ABI_RETURNS_ADDRESS:
1100 value = allocate_value (value_type);
1101 CHECK_TYPEDEF (value_type);
1102 gdbarch_return_value (current_gdbarch, value_type, stop_registers,
1103 VALUE_CONTENTS_RAW (value), NULL);
1104 break;
1105 case RETURN_VALUE_STRUCT_CONVENTION:
1106 value = NULL;
1107 break;
1108 default:
1109 internal_error (__FILE__, __LINE__, "bad switch");
1110 }
1111 }
1112 else if (struct_return && DEPRECATED_EXTRACT_STRUCT_VALUE_ADDRESS_P ())
1113 {
1114 CORE_ADDR addr = DEPRECATED_EXTRACT_STRUCT_VALUE_ADDRESS (stop_registers);
1115 if (!addr)
1116 error ("Function return value unknown.");
1117 value = value_at (value_type, addr, NULL);
1118 }
1119 else
1120 {
1121 value = allocate_value (value_type);
1122 EXTRACT_RETURN_VALUE (value_type, stop_registers,
1123 VALUE_CONTENTS_RAW (value));
1124 }
1125
1126 if (value)
1127 {
1128 /* Print it. */
1129 stb = ui_out_stream_new (uiout);
1130 old_chain = make_cleanup_ui_out_stream_delete (stb);
1131 ui_out_text (uiout, "Value returned is ");
1132 ui_out_field_fmt (uiout, "gdb-result-var", "$%d",
1133 record_latest_value (value));
1134 ui_out_text (uiout, " = ");
1135 value_print (value, stb->stream, 0, Val_no_prettyprint);
1136 ui_out_field_stream (uiout, "return-value", stb);
1137 ui_out_text (uiout, "\n");
1138 do_cleanups (old_chain);
1139 }
1140 else
1141 {
1142 ui_out_text (uiout, "Value returned has type: ");
1143 ui_out_field_string (uiout, "return-type", TYPE_NAME (value_type));
1144 ui_out_text (uiout, ".");
1145 ui_out_text (uiout, " Cannot determine contents\n");
1146 }
1147 }
1148
1149 /* Stuff that needs to be done by the finish command after the target
1150 has stopped. In asynchronous mode, we wait for the target to stop
1151 in the call to poll or select in the event loop, so it is
1152 impossible to do all the stuff as part of the finish_command
1153 function itself. The only chance we have to complete this command
1154 is in fetch_inferior_event, which is called by the event loop as
1155 soon as it detects that the target has stopped. This function is
1156 called via the cmd_continuation pointer. */
1157
1158 static void
1159 finish_command_continuation (struct continuation_arg *arg)
1160 {
1161 struct symbol *function;
1162 struct breakpoint *breakpoint;
1163 struct cleanup *cleanups;
1164
1165 breakpoint = (struct breakpoint *) arg->data.pointer;
1166 function = (struct symbol *) arg->next->data.pointer;
1167 cleanups = (struct cleanup *) arg->next->next->data.pointer;
1168
1169 if (bpstat_find_breakpoint (stop_bpstat, breakpoint) != NULL
1170 && function != NULL)
1171 {
1172 struct type *value_type;
1173 int struct_return;
1174 int gcc_compiled;
1175
1176 value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
1177 if (!value_type)
1178 internal_error (__FILE__, __LINE__,
1179 "finish_command: function has no target type");
1180
1181 if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
1182 {
1183 do_exec_cleanups (cleanups);
1184 return;
1185 }
1186
1187 CHECK_TYPEDEF (value_type);
1188 gcc_compiled = BLOCK_GCC_COMPILED (SYMBOL_BLOCK_VALUE (function));
1189 struct_return = using_struct_return (value_type, gcc_compiled);
1190
1191 print_return_value (struct_return, value_type);
1192 }
1193
1194 do_exec_cleanups (cleanups);
1195 }
1196
1197 /* "finish": Set a temporary breakpoint at the place the selected
1198 frame will return to, then continue. */
1199
1200 static void
1201 finish_command (char *arg, int from_tty)
1202 {
1203 struct symtab_and_line sal;
1204 struct frame_info *frame;
1205 struct symbol *function;
1206 struct breakpoint *breakpoint;
1207 struct cleanup *old_chain;
1208 struct continuation_arg *arg1, *arg2, *arg3;
1209
1210 int async_exec = 0;
1211
1212 /* Find out whether we must run in the background. */
1213 if (arg != NULL)
1214 async_exec = strip_bg_char (&arg);
1215
1216 /* If we must run in the background, but the target can't do it,
1217 error out. */
1218 if (event_loop_p && async_exec && !target_can_async_p ())
1219 error ("Asynchronous execution not supported on this target.");
1220
1221 /* If we are not asked to run in the bg, then prepare to run in the
1222 foreground, synchronously. */
1223 if (event_loop_p && !async_exec && target_can_async_p ())
1224 {
1225 /* Simulate synchronous execution. */
1226 async_disable_stdin ();
1227 }
1228
1229 if (arg)
1230 error ("The \"finish\" command does not take any arguments.");
1231 if (!target_has_execution)
1232 error ("The program is not running.");
1233 if (deprecated_selected_frame == NULL)
1234 error ("No selected frame.");
1235
1236 frame = get_prev_frame (deprecated_selected_frame);
1237 if (frame == 0)
1238 error ("\"finish\" not meaningful in the outermost frame.");
1239
1240 clear_proceed_status ();
1241
1242 sal = find_pc_line (get_frame_pc (frame), 0);
1243 sal.pc = get_frame_pc (frame);
1244
1245 breakpoint = set_momentary_breakpoint (sal, get_frame_id (frame), bp_finish);
1246
1247 if (!event_loop_p || !target_can_async_p ())
1248 old_chain = make_cleanup_delete_breakpoint (breakpoint);
1249 else
1250 old_chain = make_exec_cleanup_delete_breakpoint (breakpoint);
1251
1252 /* Find the function we will return from. */
1253
1254 function = find_pc_function (get_frame_pc (deprecated_selected_frame));
1255
1256 /* Print info on the selected frame, including level number but not
1257 source. */
1258 if (from_tty)
1259 {
1260 printf_filtered ("Run till exit from ");
1261 print_stack_frame (get_selected_frame (), 1, LOCATION);
1262 }
1263
1264 /* If running asynchronously and the target support asynchronous
1265 execution, set things up for the rest of the finish command to be
1266 completed later on, when gdb has detected that the target has
1267 stopped, in fetch_inferior_event. */
1268 if (event_loop_p && target_can_async_p ())
1269 {
1270 arg1 =
1271 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1272 arg2 =
1273 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1274 arg3 =
1275 (struct continuation_arg *) xmalloc (sizeof (struct continuation_arg));
1276 arg1->next = arg2;
1277 arg2->next = arg3;
1278 arg3->next = NULL;
1279 arg1->data.pointer = breakpoint;
1280 arg2->data.pointer = function;
1281 arg3->data.pointer = old_chain;
1282 add_continuation (finish_command_continuation, arg1);
1283 }
1284
1285 proceed_to_finish = 1; /* We want stop_registers, please... */
1286 proceed ((CORE_ADDR) -1, TARGET_SIGNAL_DEFAULT, 0);
1287
1288 /* Do this only if not running asynchronously or if the target
1289 cannot do async execution. Otherwise, complete this command when
1290 the target actually stops, in fetch_inferior_event. */
1291 if (!event_loop_p || !target_can_async_p ())
1292 {
1293 /* Did we stop at our breakpoint? */
1294 if (bpstat_find_breakpoint (stop_bpstat, breakpoint) != NULL
1295 && function != NULL)
1296 {
1297 struct type *value_type;
1298 int struct_return;
1299 int gcc_compiled;
1300
1301 value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
1302 if (!value_type)
1303 internal_error (__FILE__, __LINE__,
1304 "finish_command: function has no target type");
1305
1306 /* FIXME: Shouldn't we do the cleanups before returning? */
1307 if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
1308 return;
1309
1310 CHECK_TYPEDEF (value_type);
1311 gcc_compiled = BLOCK_GCC_COMPILED (SYMBOL_BLOCK_VALUE (function));
1312 struct_return = using_struct_return (value_type, gcc_compiled);
1313
1314 print_return_value (struct_return, value_type);
1315 }
1316
1317 do_cleanups (old_chain);
1318 }
1319 }
1320 \f
1321
1322 static void
1323 program_info (char *args, int from_tty)
1324 {
1325 bpstat bs = stop_bpstat;
1326 int num = bpstat_num (&bs);
1327
1328 if (!target_has_execution)
1329 {
1330 printf_filtered ("The program being debugged is not being run.\n");
1331 return;
1332 }
1333
1334 target_files_info ();
1335 printf_filtered ("Program stopped at %s.\n",
1336 local_hex_string ((unsigned long) stop_pc));
1337 if (stop_step)
1338 printf_filtered ("It stopped after being stepped.\n");
1339 else if (num != 0)
1340 {
1341 /* There may be several breakpoints in the same place, so this
1342 isn't as strange as it seems. */
1343 while (num != 0)
1344 {
1345 if (num < 0)
1346 {
1347 printf_filtered ("It stopped at a breakpoint that has ");
1348 printf_filtered ("since been deleted.\n");
1349 }
1350 else
1351 printf_filtered ("It stopped at breakpoint %d.\n", num);
1352 num = bpstat_num (&bs);
1353 }
1354 }
1355 else if (stop_signal != TARGET_SIGNAL_0)
1356 {
1357 printf_filtered ("It stopped with signal %s, %s.\n",
1358 target_signal_to_name (stop_signal),
1359 target_signal_to_string (stop_signal));
1360 }
1361
1362 if (!from_tty)
1363 {
1364 printf_filtered ("Type \"info stack\" or \"info registers\" ");
1365 printf_filtered ("for more information.\n");
1366 }
1367 }
1368 \f
1369 static void
1370 environment_info (char *var, int from_tty)
1371 {
1372 if (var)
1373 {
1374 char *val = get_in_environ (inferior_environ, var);
1375 if (val)
1376 {
1377 puts_filtered (var);
1378 puts_filtered (" = ");
1379 puts_filtered (val);
1380 puts_filtered ("\n");
1381 }
1382 else
1383 {
1384 puts_filtered ("Environment variable \"");
1385 puts_filtered (var);
1386 puts_filtered ("\" not defined.\n");
1387 }
1388 }
1389 else
1390 {
1391 char **vector = environ_vector (inferior_environ);
1392 while (*vector)
1393 {
1394 puts_filtered (*vector++);
1395 puts_filtered ("\n");
1396 }
1397 }
1398 }
1399
1400 static void
1401 set_environment_command (char *arg, int from_tty)
1402 {
1403 char *p, *val, *var;
1404 int nullset = 0;
1405
1406 if (arg == 0)
1407 error_no_arg ("environment variable and value");
1408
1409 /* Find seperation between variable name and value */
1410 p = (char *) strchr (arg, '=');
1411 val = (char *) strchr (arg, ' ');
1412
1413 if (p != 0 && val != 0)
1414 {
1415 /* We have both a space and an equals. If the space is before the
1416 equals, walk forward over the spaces til we see a nonspace
1417 (possibly the equals). */
1418 if (p > val)
1419 while (*val == ' ')
1420 val++;
1421
1422 /* Now if the = is after the char following the spaces,
1423 take the char following the spaces. */
1424 if (p > val)
1425 p = val - 1;
1426 }
1427 else if (val != 0 && p == 0)
1428 p = val;
1429
1430 if (p == arg)
1431 error_no_arg ("environment variable to set");
1432
1433 if (p == 0 || p[1] == 0)
1434 {
1435 nullset = 1;
1436 if (p == 0)
1437 p = arg + strlen (arg); /* So that savestring below will work */
1438 }
1439 else
1440 {
1441 /* Not setting variable value to null */
1442 val = p + 1;
1443 while (*val == ' ' || *val == '\t')
1444 val++;
1445 }
1446
1447 while (p != arg && (p[-1] == ' ' || p[-1] == '\t'))
1448 p--;
1449
1450 var = savestring (arg, p - arg);
1451 if (nullset)
1452 {
1453 printf_filtered ("Setting environment variable ");
1454 printf_filtered ("\"%s\" to null value.\n", var);
1455 set_in_environ (inferior_environ, var, "");
1456 }
1457 else
1458 set_in_environ (inferior_environ, var, val);
1459 xfree (var);
1460 }
1461
1462 static void
1463 unset_environment_command (char *var, int from_tty)
1464 {
1465 if (var == 0)
1466 {
1467 /* If there is no argument, delete all environment variables.
1468 Ask for confirmation if reading from the terminal. */
1469 if (!from_tty || query ("Delete all environment variables? "))
1470 {
1471 free_environ (inferior_environ);
1472 inferior_environ = make_environ ();
1473 }
1474 }
1475 else
1476 unset_in_environ (inferior_environ, var);
1477 }
1478
1479 /* Handle the execution path (PATH variable) */
1480
1481 static const char path_var_name[] = "PATH";
1482
1483 static void
1484 path_info (char *args, int from_tty)
1485 {
1486 puts_filtered ("Executable and object file path: ");
1487 puts_filtered (get_in_environ (inferior_environ, path_var_name));
1488 puts_filtered ("\n");
1489 }
1490
1491 /* Add zero or more directories to the front of the execution path. */
1492
1493 static void
1494 path_command (char *dirname, int from_tty)
1495 {
1496 char *exec_path;
1497 char *env;
1498 dont_repeat ();
1499 env = get_in_environ (inferior_environ, path_var_name);
1500 /* Can be null if path is not set */
1501 if (!env)
1502 env = "";
1503 exec_path = xstrdup (env);
1504 mod_path (dirname, &exec_path);
1505 set_in_environ (inferior_environ, path_var_name, exec_path);
1506 xfree (exec_path);
1507 if (from_tty)
1508 path_info ((char *) NULL, from_tty);
1509 }
1510 \f
1511
1512 /* Print out the machine register regnum. If regnum is -1, print all
1513 registers (print_all == 1) or all non-float and non-vector
1514 registers (print_all == 0).
1515
1516 For most machines, having all_registers_info() print the
1517 register(s) one per line is good enough. If a different format is
1518 required, (eg, for MIPS or Pyramid 90x, which both have lots of
1519 regs), or there is an existing convention for showing all the
1520 registers, define the architecture method PRINT_REGISTERS_INFO to
1521 provide that format. */
1522
1523 void
1524 default_print_registers_info (struct gdbarch *gdbarch,
1525 struct ui_file *file,
1526 struct frame_info *frame,
1527 int regnum, int print_all)
1528 {
1529 int i;
1530 const int numregs = NUM_REGS + NUM_PSEUDO_REGS;
1531 char buffer[MAX_REGISTER_SIZE];
1532
1533 if (DEPRECATED_DO_REGISTERS_INFO_P ())
1534 {
1535 DEPRECATED_DO_REGISTERS_INFO (regnum, print_all);
1536 return;
1537 }
1538
1539 for (i = 0; i < numregs; i++)
1540 {
1541 /* Decide between printing all regs, non-float / vector regs, or
1542 specific reg. */
1543 if (regnum == -1)
1544 {
1545 if (print_all)
1546 {
1547 if (!gdbarch_register_reggroup_p (gdbarch, i, all_reggroup))
1548 continue;
1549 }
1550 else
1551 {
1552 if (!gdbarch_register_reggroup_p (gdbarch, i, general_reggroup))
1553 continue;
1554 }
1555 }
1556 else
1557 {
1558 if (i != regnum)
1559 continue;
1560 }
1561
1562 /* If the register name is empty, it is undefined for this
1563 processor, so don't display anything. */
1564 if (REGISTER_NAME (i) == NULL || *(REGISTER_NAME (i)) == '\0')
1565 continue;
1566
1567 fputs_filtered (REGISTER_NAME (i), file);
1568 print_spaces_filtered (15 - strlen (REGISTER_NAME (i)), file);
1569
1570 /* Get the data in raw format. */
1571 if (! frame_register_read (frame, i, buffer))
1572 {
1573 fprintf_filtered (file, "*value not available*\n");
1574 continue;
1575 }
1576
1577 /* If virtual format is floating, print it that way, and in raw
1578 hex. */
1579 if (TYPE_CODE (register_type (current_gdbarch, i)) == TYPE_CODE_FLT)
1580 {
1581 int j;
1582
1583 val_print (register_type (current_gdbarch, i), buffer, 0, 0,
1584 file, 0, 1, 0, Val_pretty_default);
1585
1586 fprintf_filtered (file, "\t(raw 0x");
1587 for (j = 0; j < DEPRECATED_REGISTER_RAW_SIZE (i); j++)
1588 {
1589 int idx;
1590 if (TARGET_BYTE_ORDER == BFD_ENDIAN_BIG)
1591 idx = j;
1592 else
1593 idx = DEPRECATED_REGISTER_RAW_SIZE (i) - 1 - j;
1594 fprintf_filtered (file, "%02x", (unsigned char) buffer[idx]);
1595 }
1596 fprintf_filtered (file, ")");
1597 }
1598 else
1599 {
1600 /* Print the register in hex. */
1601 val_print (register_type (current_gdbarch, i), buffer, 0, 0,
1602 file, 'x', 1, 0, Val_pretty_default);
1603 /* If not a vector register, print it also according to its
1604 natural format. */
1605 if (TYPE_VECTOR (register_type (current_gdbarch, i)) == 0)
1606 {
1607 fprintf_filtered (file, "\t");
1608 val_print (register_type (current_gdbarch, i), buffer, 0, 0,
1609 file, 0, 1, 0, Val_pretty_default);
1610 }
1611 }
1612
1613 fprintf_filtered (file, "\n");
1614 }
1615 }
1616
1617 void
1618 registers_info (char *addr_exp, int fpregs)
1619 {
1620 int regnum, numregs;
1621 char *end;
1622
1623 if (!target_has_registers)
1624 error ("The program has no registers now.");
1625 if (deprecated_selected_frame == NULL)
1626 error ("No selected frame.");
1627
1628 if (!addr_exp)
1629 {
1630 gdbarch_print_registers_info (current_gdbarch, gdb_stdout,
1631 deprecated_selected_frame, -1, fpregs);
1632 return;
1633 }
1634
1635 while (*addr_exp != '\0')
1636 {
1637 char *start;
1638 const char *end;
1639
1640 /* Keep skipping leading white space. */
1641 if (isspace ((*addr_exp)))
1642 {
1643 addr_exp++;
1644 continue;
1645 }
1646
1647 /* Discard any leading ``$''. Check that there is something
1648 resembling a register following it. */
1649 if (addr_exp[0] == '$')
1650 addr_exp++;
1651 if (isspace ((*addr_exp)) || (*addr_exp) == '\0')
1652 error ("Missing register name");
1653
1654 /* Find the start/end of this register name/num/group. */
1655 start = addr_exp;
1656 while ((*addr_exp) != '\0' && !isspace ((*addr_exp)))
1657 addr_exp++;
1658 end = addr_exp;
1659
1660 /* Figure out what we've found and display it. */
1661
1662 /* A register name? */
1663 {
1664 int regnum = frame_map_name_to_regnum (deprecated_selected_frame,
1665 start, end - start);
1666 if (regnum >= 0)
1667 {
1668 gdbarch_print_registers_info (current_gdbarch, gdb_stdout,
1669 deprecated_selected_frame, regnum, fpregs);
1670 continue;
1671 }
1672 }
1673
1674 /* A register number? (how portable is this one?). */
1675 {
1676 char *endptr;
1677 int regnum = strtol (start, &endptr, 0);
1678 if (endptr == end
1679 && regnum >= 0
1680 && regnum < NUM_REGS + NUM_PSEUDO_REGS)
1681 {
1682 gdbarch_print_registers_info (current_gdbarch, gdb_stdout,
1683 deprecated_selected_frame, regnum, fpregs);
1684 continue;
1685 }
1686 }
1687
1688 /* A register group? */
1689 {
1690 struct reggroup *group;
1691 for (group = reggroup_next (current_gdbarch, NULL);
1692 group != NULL;
1693 group = reggroup_next (current_gdbarch, group))
1694 {
1695 /* Don't bother with a length check. Should the user
1696 enter a short register group name, go with the first
1697 group that matches. */
1698 if (strncmp (start, reggroup_name (group), end - start) == 0)
1699 break;
1700 }
1701 if (group != NULL)
1702 {
1703 int regnum;
1704 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
1705 {
1706 if (gdbarch_register_reggroup_p (current_gdbarch, regnum,
1707 group))
1708 gdbarch_print_registers_info (current_gdbarch,
1709 gdb_stdout, deprecated_selected_frame,
1710 regnum, fpregs);
1711 }
1712 continue;
1713 }
1714 }
1715
1716 /* Nothing matched. */
1717 error ("Invalid register `%.*s'", (int) (end - start), start);
1718 }
1719 }
1720
1721 void
1722 all_registers_info (char *addr_exp, int from_tty)
1723 {
1724 registers_info (addr_exp, 1);
1725 }
1726
1727 static void
1728 nofp_registers_info (char *addr_exp, int from_tty)
1729 {
1730 registers_info (addr_exp, 0);
1731 }
1732
1733 static void
1734 print_vector_info (struct gdbarch *gdbarch, struct ui_file *file,
1735 struct frame_info *frame, const char *args)
1736 {
1737 if (!target_has_registers)
1738 error ("The program has no registers now.");
1739 if (deprecated_selected_frame == NULL)
1740 error ("No selected frame.");
1741
1742 if (gdbarch_print_vector_info_p (gdbarch))
1743 gdbarch_print_vector_info (gdbarch, file, frame, args);
1744 else
1745 {
1746 int regnum;
1747 int printed_something = 0;
1748
1749 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
1750 {
1751 if (gdbarch_register_reggroup_p (gdbarch, regnum, vector_reggroup))
1752 {
1753 printed_something = 1;
1754 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
1755 }
1756 }
1757 if (!printed_something)
1758 fprintf_filtered (file, "No vector information\n");
1759 }
1760 }
1761
1762 static void
1763 vector_info (char *args, int from_tty)
1764 {
1765 print_vector_info (current_gdbarch, gdb_stdout, deprecated_selected_frame, args);
1766 }
1767 \f
1768
1769 /*
1770 * TODO:
1771 * Should save/restore the tty state since it might be that the
1772 * program to be debugged was started on this tty and it wants
1773 * the tty in some state other than what we want. If it's running
1774 * on another terminal or without a terminal, then saving and
1775 * restoring the tty state is a harmless no-op.
1776 * This only needs to be done if we are attaching to a process.
1777 */
1778
1779 /*
1780 attach_command --
1781 takes a program started up outside of gdb and ``attaches'' to it.
1782 This stops it cold in its tracks and allows us to start debugging it.
1783 and wait for the trace-trap that results from attaching. */
1784
1785 void
1786 attach_command (char *args, int from_tty)
1787 {
1788 char *exec_file;
1789 char *full_exec_path = NULL;
1790
1791 dont_repeat (); /* Not for the faint of heart */
1792
1793 if (target_has_execution)
1794 {
1795 if (query ("A program is being debugged already. Kill it? "))
1796 target_kill ();
1797 else
1798 error ("Not killed.");
1799 }
1800
1801 target_attach (args, from_tty);
1802
1803 /* Set up the "saved terminal modes" of the inferior
1804 based on what modes we are starting it with. */
1805 target_terminal_init ();
1806
1807 /* Install inferior's terminal modes. */
1808 target_terminal_inferior ();
1809
1810 /* Set up execution context to know that we should return from
1811 wait_for_inferior as soon as the target reports a stop. */
1812 init_wait_for_inferior ();
1813 clear_proceed_status ();
1814
1815 /* No traps are generated when attaching to inferior under Mach 3
1816 or GNU hurd. */
1817 #ifndef ATTACH_NO_WAIT
1818 /* Careful here. See comments in inferior.h. Basically some OSes
1819 don't ignore SIGSTOPs on continue requests anymore. We need a
1820 way for handle_inferior_event to reset the stop_signal variable
1821 after an attach, and this is what STOP_QUIETLY_NO_SIGSTOP is for. */
1822 stop_soon = STOP_QUIETLY_NO_SIGSTOP;
1823 wait_for_inferior ();
1824 stop_soon = NO_STOP_QUIETLY;
1825 #endif
1826
1827 /*
1828 * If no exec file is yet known, try to determine it from the
1829 * process itself.
1830 */
1831 exec_file = (char *) get_exec_file (0);
1832 if (!exec_file)
1833 {
1834 exec_file = target_pid_to_exec_file (PIDGET (inferior_ptid));
1835 if (exec_file)
1836 {
1837 /* It's possible we don't have a full path, but rather just a
1838 filename. Some targets, such as HP-UX, don't provide the
1839 full path, sigh.
1840
1841 Attempt to qualify the filename against the source path.
1842 (If that fails, we'll just fall back on the original
1843 filename. Not much more we can do...)
1844 */
1845 if (!source_full_path_of (exec_file, &full_exec_path))
1846 full_exec_path = savestring (exec_file, strlen (exec_file));
1847
1848 exec_file_attach (full_exec_path, from_tty);
1849 symbol_file_add_main (full_exec_path, from_tty);
1850 }
1851 }
1852 else
1853 {
1854 reopen_exec_file ();
1855 reread_symbols ();
1856 }
1857
1858 #ifdef SOLIB_ADD
1859 /* Add shared library symbols from the newly attached process, if any. */
1860 SOLIB_ADD ((char *) 0, from_tty, &current_target, auto_solib_add);
1861 re_enable_breakpoints_in_shlibs ();
1862 #endif
1863
1864 /* Take any necessary post-attaching actions for this platform.
1865 */
1866 target_post_attach (PIDGET (inferior_ptid));
1867
1868 normal_stop ();
1869
1870 if (deprecated_attach_hook)
1871 deprecated_attach_hook ();
1872 }
1873
1874 /*
1875 * detach_command --
1876 * takes a program previously attached to and detaches it.
1877 * The program resumes execution and will no longer stop
1878 * on signals, etc. We better not have left any breakpoints
1879 * in the program or it'll die when it hits one. For this
1880 * to work, it may be necessary for the process to have been
1881 * previously attached. It *might* work if the program was
1882 * started via the normal ptrace (PTRACE_TRACEME).
1883 */
1884
1885 static void
1886 detach_command (char *args, int from_tty)
1887 {
1888 dont_repeat (); /* Not for the faint of heart */
1889 target_detach (args, from_tty);
1890 #if defined(SOLIB_RESTART)
1891 SOLIB_RESTART ();
1892 #endif
1893 if (deprecated_detach_hook)
1894 deprecated_detach_hook ();
1895 }
1896
1897 /* Disconnect from the current target without resuming it (leaving it
1898 waiting for a debugger).
1899
1900 We'd better not have left any breakpoints in the program or the
1901 next debugger will get confused. Currently only supported for some
1902 remote targets, since the normal attach mechanisms don't work on
1903 stopped processes on some native platforms (e.g. GNU/Linux). */
1904
1905 static void
1906 disconnect_command (char *args, int from_tty)
1907 {
1908 dont_repeat (); /* Not for the faint of heart */
1909 target_disconnect (args, from_tty);
1910 #if defined(SOLIB_RESTART)
1911 SOLIB_RESTART ();
1912 #endif
1913 if (deprecated_detach_hook)
1914 deprecated_detach_hook ();
1915 }
1916
1917 /* Stop the execution of the target while running in async mode, in
1918 the backgound. */
1919 void
1920 interrupt_target_command (char *args, int from_tty)
1921 {
1922 if (event_loop_p && target_can_async_p ())
1923 {
1924 dont_repeat (); /* Not for the faint of heart */
1925 target_stop ();
1926 }
1927 }
1928
1929 static void
1930 print_float_info (struct gdbarch *gdbarch, struct ui_file *file,
1931 struct frame_info *frame, const char *args)
1932 {
1933 if (!target_has_registers)
1934 error ("The program has no registers now.");
1935 if (deprecated_selected_frame == NULL)
1936 error ("No selected frame.");
1937
1938 if (gdbarch_print_float_info_p (gdbarch))
1939 gdbarch_print_float_info (gdbarch, file, frame, args);
1940 else
1941 {
1942 int regnum;
1943 int printed_something = 0;
1944
1945 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
1946 {
1947 if (gdbarch_register_reggroup_p (gdbarch, regnum, float_reggroup))
1948 {
1949 printed_something = 1;
1950 gdbarch_print_registers_info (gdbarch, file, frame, regnum, 1);
1951 }
1952 }
1953 if (!printed_something)
1954 fprintf_filtered (file, "\
1955 No floating-point info available for this processor.\n");
1956 }
1957 }
1958
1959 static void
1960 float_info (char *args, int from_tty)
1961 {
1962 print_float_info (current_gdbarch, gdb_stdout, deprecated_selected_frame, args);
1963 }
1964 \f
1965 static void
1966 unset_command (char *args, int from_tty)
1967 {
1968 printf_filtered ("\"unset\" must be followed by the name of ");
1969 printf_filtered ("an unset subcommand.\n");
1970 help_list (unsetlist, "unset ", -1, gdb_stdout);
1971 }
1972
1973 void
1974 _initialize_infcmd (void)
1975 {
1976 struct cmd_list_element *c;
1977
1978 c = add_com ("tty", class_run, tty_command,
1979 "Set terminal for future runs of program being debugged.");
1980 set_cmd_completer (c, filename_completer);
1981
1982 c = add_set_cmd ("args", class_run, var_string_noescape,
1983 (char *) &inferior_args,
1984 "Set argument list to give program being debugged when it is started.\n\
1985 Follow this command with any number of args, to be passed to the program.",
1986 &setlist);
1987 set_cmd_completer (c, filename_completer);
1988 set_cmd_sfunc (c, notice_args_set);
1989 c = add_show_from_set (c, &showlist);
1990 set_cmd_sfunc (c, notice_args_read);
1991
1992 c = add_cmd
1993 ("environment", no_class, environment_info,
1994 "The environment to give the program, or one variable's value.\n\
1995 With an argument VAR, prints the value of environment variable VAR to\n\
1996 give the program being debugged. With no arguments, prints the entire\n\
1997 environment to be given to the program.", &showlist);
1998 set_cmd_completer (c, noop_completer);
1999
2000 add_prefix_cmd ("unset", no_class, unset_command,
2001 "Complement to certain \"set\" commands.",
2002 &unsetlist, "unset ", 0, &cmdlist);
2003
2004 c = add_cmd ("environment", class_run, unset_environment_command,
2005 "Cancel environment variable VAR for the program.\n\
2006 This does not affect the program until the next \"run\" command.",
2007 &unsetlist);
2008 set_cmd_completer (c, noop_completer);
2009
2010 c = add_cmd ("environment", class_run, set_environment_command,
2011 "Set environment variable value to give the program.\n\
2012 Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
2013 VALUES of environment variables are uninterpreted strings.\n\
2014 This does not affect the program until the next \"run\" command.",
2015 &setlist);
2016 set_cmd_completer (c, noop_completer);
2017
2018 c = add_com ("path", class_files, path_command,
2019 "Add directory DIR(s) to beginning of search path for object files.\n\
2020 $cwd in the path means the current working directory.\n\
2021 This path is equivalent to the $PATH shell variable. It is a list of\n\
2022 directories, separated by colons. These directories are searched to find\n\
2023 fully linked executable files and separately compiled object files as needed.");
2024 set_cmd_completer (c, filename_completer);
2025
2026 c = add_cmd ("paths", no_class, path_info,
2027 "Current search path for finding object files.\n\
2028 $cwd in the path means the current working directory.\n\
2029 This path is equivalent to the $PATH shell variable. It is a list of\n\
2030 directories, separated by colons. These directories are searched to find\n\
2031 fully linked executable files and separately compiled object files as needed.",
2032 &showlist);
2033 set_cmd_completer (c, noop_completer);
2034
2035 add_com ("attach", class_run, attach_command,
2036 "Attach to a process or file outside of GDB.\n\
2037 This command attaches to another target, of the same type as your last\n\
2038 \"target\" command (\"info files\" will show your target stack).\n\
2039 The command may take as argument a process id or a device file.\n\
2040 For a process id, you must have permission to send the process a signal,\n\
2041 and it must have the same effective uid as the debugger.\n\
2042 When using \"attach\" with a process id, the debugger finds the\n\
2043 program running in the process, looking first in the current working\n\
2044 directory, or (if not found there) using the source file search path\n\
2045 (see the \"directory\" command). You can also use the \"file\" command\n\
2046 to specify the program, and to load its symbol table.");
2047
2048 add_com ("detach", class_run, detach_command,
2049 "Detach a process or file previously attached.\n\
2050 If a process, it is no longer traced, and it continues its execution. If\n\
2051 you were debugging a file, the file is closed and gdb no longer accesses it.");
2052
2053 add_com ("disconnect", class_run, disconnect_command,
2054 "Disconnect from a target.\n\
2055 The target will wait for another debugger to connect. Not available for\n\
2056 all targets.");
2057
2058 add_com ("signal", class_run, signal_command,
2059 "Continue program giving it signal specified by the argument.\n\
2060 An argument of \"0\" means continue program without giving it a signal.");
2061
2062 add_com ("stepi", class_run, stepi_command,
2063 "Step one instruction exactly.\n\
2064 Argument N means do this N times (or till program stops for another reason).");
2065 add_com_alias ("si", "stepi", class_alias, 0);
2066
2067 add_com ("nexti", class_run, nexti_command,
2068 "Step one instruction, but proceed through subroutine calls.\n\
2069 Argument N means do this N times (or till program stops for another reason).");
2070 add_com_alias ("ni", "nexti", class_alias, 0);
2071
2072 add_com ("finish", class_run, finish_command,
2073 "Execute until selected stack frame returns.\n\
2074 Upon return, the value returned is printed and put in the value history.");
2075
2076 add_com ("next", class_run, next_command,
2077 "Step program, proceeding through subroutine calls.\n\
2078 Like the \"step\" command as long as subroutine calls do not happen;\n\
2079 when they do, the call is treated as one instruction.\n\
2080 Argument N means do this N times (or till program stops for another reason).");
2081 add_com_alias ("n", "next", class_run, 1);
2082 if (xdb_commands)
2083 add_com_alias ("S", "next", class_run, 1);
2084
2085 add_com ("step", class_run, step_command,
2086 "Step program until it reaches a different source line.\n\
2087 Argument N means do this N times (or till program stops for another reason).");
2088 add_com_alias ("s", "step", class_run, 1);
2089
2090 c = add_com ("until", class_run, until_command,
2091 "Execute until the program reaches a source line greater than the current\n\
2092 or a specified location (same args as break command) within the current frame.");
2093 set_cmd_completer (c, location_completer);
2094 add_com_alias ("u", "until", class_run, 1);
2095
2096 c = add_com ("advance", class_run, advance_command,
2097 "Continue the program up to the given location (same form as args for break command).\n\
2098 Execution will also stop upon exit from the current stack frame.");
2099 set_cmd_completer (c, location_completer);
2100
2101 c = add_com ("jump", class_run, jump_command,
2102 "Continue program being debugged at specified line or address.\n\
2103 Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
2104 for an address to start at.");
2105 set_cmd_completer (c, location_completer);
2106
2107 if (xdb_commands)
2108 {
2109 c = add_com ("go", class_run, go_command,
2110 "Usage: go <location>\n\
2111 Continue program being debugged, stopping at specified line or \n\
2112 address.\n\
2113 Give as argument either LINENUM or *ADDR, where ADDR is an \n\
2114 expression for an address to start at.\n\
2115 This command is a combination of tbreak and jump.");
2116 set_cmd_completer (c, location_completer);
2117 }
2118
2119 if (xdb_commands)
2120 add_com_alias ("g", "go", class_run, 1);
2121
2122 add_com ("continue", class_run, continue_command,
2123 "Continue program being debugged, after signal or breakpoint.\n\
2124 If proceeding from breakpoint, a number N may be used as an argument,\n\
2125 which means to set the ignore count of that breakpoint to N - 1 (so that\n\
2126 the breakpoint won't break until the Nth time it is reached).");
2127 add_com_alias ("c", "cont", class_run, 1);
2128 add_com_alias ("fg", "cont", class_run, 1);
2129
2130 c = add_com ("run", class_run, run_command,
2131 "Start debugged program. You may specify arguments to give it.\n\
2132 Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
2133 Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
2134 With no arguments, uses arguments last specified (with \"run\" or \"set args\").\n\
2135 To cancel previous arguments and run with no arguments,\n\
2136 use \"set args\" without arguments.");
2137 set_cmd_completer (c, filename_completer);
2138 add_com_alias ("r", "run", class_run, 1);
2139 if (xdb_commands)
2140 add_com ("R", class_run, run_no_args_command,
2141 "Start debugged program with no arguments.");
2142
2143 c = add_com ("start", class_run, start_command,
2144 "\
2145 Run the debugged program until the beginning of the main procedure.\n\
2146 You may specify arguments to give to your program, just as with the\n\
2147 \"run\" command.");
2148 set_cmd_completer (c, filename_completer);
2149
2150 add_com ("interrupt", class_run, interrupt_target_command,
2151 "Interrupt the execution of the debugged program.");
2152
2153 add_info ("registers", nofp_registers_info,
2154 "List of integer registers and their contents, for selected stack frame.\n\
2155 Register name as argument means describe only that register.");
2156 add_info_alias ("r", "registers", 1);
2157
2158 if (xdb_commands)
2159 add_com ("lr", class_info, nofp_registers_info,
2160 "List of integer registers and their contents, for selected stack frame.\n\
2161 Register name as argument means describe only that register.");
2162 add_info ("all-registers", all_registers_info,
2163 "List of all registers and their contents, for selected stack frame.\n\
2164 Register name as argument means describe only that register.");
2165
2166 add_info ("program", program_info,
2167 "Execution status of the program.");
2168
2169 add_info ("float", float_info,
2170 "Print the status of the floating point unit\n");
2171
2172 add_info ("vector", vector_info,
2173 "Print the status of the vector unit\n");
2174
2175 inferior_environ = make_environ ();
2176 init_environ (inferior_environ);
2177 }