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