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