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