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