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