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