]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/infcmd.c
* defs.h (STRCMP, STREQ, STREQN): New macros.
[thirdparty/binutils-gdb.git] / gdb / infcmd.c
CommitLineData
ee0613d1
JG
1/* Memory-access and commands for "inferior" (child) process, for GDB.
2 Copyright 1986, 1987, 1988, 1989, 1991, 1992 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
ee0613d1 6This program is free software; you can redistribute it and/or modify
bd5635a1 7it under the terms of the GNU General Public License as published by
ee0613d1
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
ee0613d1 11This program is distributed in the hope that it will be useful,
bd5635a1
RP
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
ee0613d1
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
1304f099 20#include "defs.h"
bd5635a1
RP
21#include <signal.h>
22#include <sys/param.h>
23#include <string.h>
bd5635a1 24#include "symtab.h"
1304f099 25#include "gdbtypes.h"
bd5635a1
RP
26#include "frame.h"
27#include "inferior.h"
28#include "environ.h"
29#include "value.h"
30#include "gdbcmd.h"
31#include "gdbcore.h"
32#include "target.h"
33
1304f099
JG
34static void
35continue_command PARAMS ((char *, int));
36
37static void
38until_next_command PARAMS ((int));
39
40static void
41until_command PARAMS ((char *, int));
42
43static void
44path_info PARAMS ((char *, int));
45
46static void
47path_command PARAMS ((char *, int));
48
49static void
50unset_command PARAMS ((char *, int));
51
52static void
53float_info PARAMS ((char *, int));
54
55static void
56detach_command PARAMS ((char *, int));
57
58static void
59nofp_registers_info PARAMS ((char *, int));
60
61static void
62all_registers_info PARAMS ((char *, int));
63
64static void
65registers_info PARAMS ((char *, int));
66
67static void
68do_registers_info PARAMS ((int, int));
69
70static void
71unset_environment_command PARAMS ((char *, int));
72
73static void
74set_environment_command PARAMS ((char *, int));
75
76static void
77environment_info PARAMS ((char *, int));
78
79static void
80program_info PARAMS ((char *, int));
81
82static void
83finish_command PARAMS ((char *, int));
84
85static void
86signal_command PARAMS ((char *, int));
87
88static void
89jump_command PARAMS ((char *, int));
90
91static void
92step_1 PARAMS ((int, int, char *));
93
94static void
95nexti_command PARAMS ((char *, int));
bd5635a1 96
1304f099
JG
97static void
98stepi_command PARAMS ((char *, int));
99
100static void
101next_command PARAMS ((char *, int));
102
103static void
104step_command PARAMS ((char *, int));
105
106static void
107run_command PARAMS ((char *, int));
bd5635a1
RP
108
109#define ERROR_NO_INFERIOR \
110 if (!target_has_execution) error ("The program is not being run.");
111
112/* String containing arguments to give to the program, separated by spaces.
113 Empty string (pointer to '\0') means no args. */
114
115static char *inferior_args;
116
117/* File name for default use for standard in/out in the inferior. */
118
119char *inferior_io_terminal;
120
121/* Pid of our debugged inferior, or 0 if no inferior now.
122 Since various parts of infrun.c test this to see whether there is a program
123 being debugged it should be nonzero (currently 3 is used) for remote
124 debugging. */
125
126int inferior_pid;
127
128/* Last signal that the inferior received (why it stopped). */
129
130int stop_signal;
131
132/* Address at which inferior stopped. */
133
134CORE_ADDR stop_pc;
135
136/* Stack frame when program stopped. */
137
138FRAME_ADDR stop_frame_address;
139
140/* Chain containing status of breakpoint(s) that we have stopped at. */
141
142bpstat stop_bpstat;
143
144/* Flag indicating that a command has proceeded the inferior past the
145 current breakpoint. */
146
147int breakpoint_proceeded;
148
149/* Nonzero if stopped due to a step command. */
150
151int stop_step;
152
153/* Nonzero if stopped due to completion of a stack dummy routine. */
154
155int stop_stack_dummy;
156
157/* Nonzero if stopped due to a random (unexpected) signal in inferior
158 process. */
159
160int stopped_by_random_signal;
161
162/* Range to single step within.
163 If this is nonzero, respond to a single-step signal
164 by continuing to step if the pc is in this range. */
165
166CORE_ADDR step_range_start; /* Inclusive */
167CORE_ADDR step_range_end; /* Exclusive */
168
169/* Stack frame address as of when stepping command was issued.
170 This is how we know when we step into a subroutine call,
171 and how to set the frame for the breakpoint used to step out. */
172
173FRAME_ADDR step_frame_address;
174
175/* 1 means step over all subroutine calls.
b5a3d2aa 176 0 means don't step over calls (used by stepi).
bd5635a1
RP
177 -1 means step over calls to undebuggable functions. */
178
179int step_over_calls;
180
181/* If stepping, nonzero means step count is > 1
182 so don't print frame next time inferior stops
183 if it stops due to stepping. */
184
185int step_multi;
186
187/* Environment to use for running inferior,
188 in format described in environ.h. */
189
190struct environ *inferior_environ;
191
bd5635a1 192\f
e1ce8aa5 193/* ARGSUSED */
bd5635a1
RP
194void
195tty_command (file, from_tty)
196 char *file;
197 int from_tty;
198{
199 if (file == 0)
200 error_no_arg ("terminal name for running target process");
201
202 inferior_io_terminal = savestring (file, strlen (file));
203}
204
205static void
206run_command (args, from_tty)
207 char *args;
208 int from_tty;
209{
210 char *exec_file;
211
212 dont_repeat ();
213
214 if (inferior_pid)
215 {
216 if (
217 !query ("The program being debugged has been started already.\n\
218Start it from the beginning? "))
219 error ("Program not restarted.");
ee0613d1 220 target_kill ();
bd5635a1
RP
221 }
222
223 exec_file = (char *) get_exec_file (0);
224
1304f099 225 /* The exec file is re-read every time we do a generic_mourn_inferior, so
bd5635a1
RP
226 we just have to worry about the symbol file. */
227 reread_symbols ();
228
229 if (args)
230 {
231 char *cmd;
ee0613d1 232 cmd = concat ("set args ", args, NULL);
bd5635a1
RP
233 make_cleanup (free, cmd);
234 execute_command (cmd, from_tty);
235 }
236
237 if (from_tty)
238 {
b5a3d2aa
SG
239 puts_filtered("Starting program: ");
240 if (exec_file)
241 puts_filtered(exec_file);
242 puts_filtered(" ");
243 puts_filtered(inferior_args);
244 puts_filtered("\n");
bd5635a1
RP
245 fflush (stdout);
246 }
247
248 target_create_inferior (exec_file, inferior_args,
249 environ_vector (inferior_environ));
250}
251\f
1304f099 252static void
bd5635a1
RP
253continue_command (proc_count_exp, from_tty)
254 char *proc_count_exp;
255 int from_tty;
256{
257 ERROR_NO_INFERIOR;
258
259 /* If have argument, set proceed count of breakpoint we stopped at. */
260
261 if (proc_count_exp != NULL)
262 {
263 bpstat bs = stop_bpstat;
264 int num = bpstat_num (&bs);
265 if (num == 0 && from_tty)
266 {
267 printf_filtered
268 ("Not stopped at any breakpoint; argument ignored.\n");
269 }
270 while (num != 0)
271 {
272 set_ignore_count (num,
273 parse_and_eval_address (proc_count_exp) - 1,
274 from_tty);
275 /* set_ignore_count prints a message ending with a period.
276 So print two spaces before "Continuing.". */
277 if (from_tty)
1304f099 278 printf_filtered (" ");
bd5635a1
RP
279 num = bpstat_num (&bs);
280 }
281 }
282
283 if (from_tty)
1304f099 284 printf_filtered ("Continuing.\n");
bd5635a1
RP
285
286 clear_proceed_status ();
287
288 proceed ((CORE_ADDR) -1, -1, 0);
289}
290\f
291/* Step until outside of current statement. */
bd5635a1 292
e1ce8aa5 293/* ARGSUSED */
bd5635a1
RP
294static void
295step_command (count_string, from_tty)
1304f099 296 char *count_string;
bd5635a1
RP
297 int from_tty;
298{
299 step_1 (0, 0, count_string);
300}
301
302/* Likewise, but skip over subroutine calls as if single instructions. */
303
e1ce8aa5 304/* ARGSUSED */
bd5635a1
RP
305static void
306next_command (count_string, from_tty)
1304f099 307 char *count_string;
bd5635a1
RP
308 int from_tty;
309{
310 step_1 (1, 0, count_string);
311}
312
313/* Likewise, but step only one instruction. */
314
e1ce8aa5 315/* ARGSUSED */
bd5635a1
RP
316static void
317stepi_command (count_string, from_tty)
1304f099 318 char *count_string;
bd5635a1
RP
319 int from_tty;
320{
321 step_1 (0, 1, count_string);
322}
323
e1ce8aa5 324/* ARGSUSED */
bd5635a1
RP
325static void
326nexti_command (count_string, from_tty)
1304f099 327 char *count_string;
bd5635a1
RP
328 int from_tty;
329{
330 step_1 (1, 1, count_string);
331}
332
333static void
334step_1 (skip_subroutines, single_inst, count_string)
335 int skip_subroutines;
336 int single_inst;
337 char *count_string;
338{
339 register int count = 1;
340 FRAME fr;
1304f099 341 struct cleanup *cleanups = 0;
bd5635a1
RP
342
343 ERROR_NO_INFERIOR;
344 count = count_string ? parse_and_eval_address (count_string) : 1;
345
1304f099
JG
346 if (!single_inst || skip_subroutines) /* leave si command alone */
347 {
348 enable_longjmp_breakpoint();
349 cleanups = make_cleanup(disable_longjmp_breakpoint, 0);
350 }
351
bd5635a1
RP
352 for (; count > 0; count--)
353 {
354 clear_proceed_status ();
355
bd5635a1
RP
356 fr = get_current_frame ();
357 if (!fr) /* Avoid coredump here. Why tho? */
358 error ("No current frame");
359 step_frame_address = FRAME_FP (fr);
360
361 if (! single_inst)
362 {
363 find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
364 if (step_range_end == 0)
365 {
1304f099 366 struct minimal_symbol *msymbol;
bd5635a1 367
1304f099 368 msymbol = lookup_minimal_symbol_by_pc (stop_pc);
bd5635a1 369 target_terminal_ours ();
1304f099 370 printf_filtered ("Current function has no line number information.\n");
bd5635a1
RP
371 fflush (stdout);
372
373 /* No info or after _etext ("Can't happen") */
2e4964ad 374 if (msymbol == NULL || SYMBOL_NAME (msymbol + 1) == NULL)
bd5635a1
RP
375 error ("No data available on pc function.");
376
1304f099 377 printf_filtered ("Single stepping until function exit.\n");
bd5635a1
RP
378 fflush (stdout);
379
2e4964ad
FF
380 step_range_start = SYMBOL_VALUE_ADDRESS (msymbol);
381 step_range_end = SYMBOL_VALUE_ADDRESS (msymbol + 1);
bd5635a1
RP
382 }
383 }
384 else
385 {
386 /* Say we are stepping, but stop after one insn whatever it does.
387 Don't step through subroutine calls even to undebuggable
388 functions. */
389 step_range_start = step_range_end = 1;
390 if (!skip_subroutines)
391 step_over_calls = 0;
392 }
393
394 if (skip_subroutines)
395 step_over_calls = 1;
396
397 step_multi = (count > 1);
398 proceed ((CORE_ADDR) -1, -1, 1);
399 if (! stop_step)
400 break;
401#if defined (SHIFT_INST_REGS)
402 write_register (NNPC_REGNUM, read_register (NPC_REGNUM));
403 write_register (NPC_REGNUM, read_register (PC_REGNUM));
404#endif
405 }
1304f099
JG
406
407 if (!single_inst || skip_subroutines)
408 do_cleanups(cleanups);
bd5635a1
RP
409}
410\f
411/* Continue program at specified address. */
412
413static void
414jump_command (arg, from_tty)
415 char *arg;
416 int from_tty;
417{
418 register CORE_ADDR addr;
419 struct symtabs_and_lines sals;
420 struct symtab_and_line sal;
b4fde6fa
FF
421 struct symbol *fn;
422 struct symbol *sfn;
bd5635a1
RP
423
424 ERROR_NO_INFERIOR;
425
426 if (!arg)
427 error_no_arg ("starting address");
428
429 sals = decode_line_spec_1 (arg, 1);
430 if (sals.nelts != 1)
431 {
432 error ("Unreasonable jump request");
433 }
434
435 sal = sals.sals[0];
1304f099 436 free ((PTR)sals.sals);
bd5635a1
RP
437
438 if (sal.symtab == 0 && sal.pc == 0)
439 error ("No source file has been specified.");
440
ee0613d1 441 resolve_sal_pc (&sal); /* May error out */
bd5635a1 442
b4fde6fa
FF
443 /* See if we are trying to jump to another function. */
444 fn = get_frame_function (get_current_frame ());
445 sfn = find_pc_function (sal.pc);
446 if (fn != NULL && sfn != fn)
447 {
2e4964ad
FF
448 if (!query ("Line %d is not in `%s'. Jump anyway? ", sal.line,
449 SYMBOL_SOURCE_NAME (fn)))
b4fde6fa
FF
450 {
451 error ("Not confirmed.");
452 /* NOTREACHED */
453 }
b4fde6fa 454 }
bd5635a1 455
bd5635a1
RP
456 addr = ADDR_BITS_SET (sal.pc);
457
458 if (from_tty)
1304f099 459 printf_filtered ("Continuing at %s.\n", local_hex_string(addr));
bd5635a1
RP
460
461 clear_proceed_status ();
462 proceed (addr, 0, 0);
463}
464
465/* Continue program giving it specified signal. */
466
467static void
468signal_command (signum_exp, from_tty)
469 char *signum_exp;
470 int from_tty;
471{
472 register int signum;
473
474 dont_repeat (); /* Too dangerous. */
475 ERROR_NO_INFERIOR;
476
477 if (!signum_exp)
478 error_no_arg ("signal number");
479
480 signum = parse_and_eval_address (signum_exp);
481
482 if (from_tty)
1304f099 483 printf_filtered ("Continuing with signal %d.\n", signum);
bd5635a1
RP
484
485 clear_proceed_status ();
486 proceed (stop_pc, signum, 0);
487}
488
489/* Execute a "stack dummy", a piece of code stored in the stack
490 by the debugger to be executed in the inferior.
491
492 To call: first, do PUSH_DUMMY_FRAME.
493 Then push the contents of the dummy. It should end with a breakpoint insn.
494 Then call here, passing address at which to start the dummy.
495
496 The contents of all registers are saved before the dummy frame is popped
497 and copied into the buffer BUFFER.
498
499 The dummy's frame is automatically popped whenever that break is hit.
500 If that is the first time the program stops, run_stack_dummy
501 returns to its caller with that frame already gone.
502 Otherwise, the caller never gets returned to. */
503
ee0613d1 504/* DEBUG HOOK: 4 => return instead of letting the stack dummy run. */
bd5635a1
RP
505
506static int stack_dummy_testing = 0;
507
508void
509run_stack_dummy (addr, buffer)
510 CORE_ADDR addr;
511 char buffer[REGISTER_BYTES];
512{
513 /* Now proceed, having reached the desired place. */
514 clear_proceed_status ();
515 if (stack_dummy_testing & 4)
516 {
517 POP_FRAME;
518 return;
519 }
520 proceed_to_finish = 1; /* We want stop_registers, please... */
521 proceed (addr, 0, 0);
522
523 if (!stop_stack_dummy)
524 /* This used to say
525 "Cannot continue previously requested operation". */
526 error ("\
527The program being debugged stopped while in a function called from GDB.\n\
528The expression which contained the function call has been discarded.");
529
530 /* On return, the stack dummy has been popped already. */
531
4ed3a9ea 532 memcpy (buffer, stop_registers, sizeof stop_registers);
bd5635a1
RP
533}
534\f
535/* Proceed until we reach a different source line with pc greater than
536 our current one or exit the function. We skip calls in both cases.
537
538 Note that eventually this command should probably be changed so
539 that only source lines are printed out when we hit the breakpoint
540 we set. I'm going to postpone this until after a hopeful rewrite
541 of wait_for_inferior and the proceed status code. -- randy */
542
e1ce8aa5 543/* ARGSUSED */
1304f099 544static void
bd5635a1
RP
545until_next_command (from_tty)
546 int from_tty;
547{
548 FRAME frame;
549 CORE_ADDR pc;
550 struct symbol *func;
551 struct symtab_and_line sal;
552
553 clear_proceed_status ();
554
555 frame = get_current_frame ();
556
557 /* Step until either exited from this function or greater
558 than the current line (if in symbolic section) or pc (if
559 not). */
560
561 pc = read_pc ();
562 func = find_pc_function (pc);
563
564 if (!func)
565 {
1304f099 566 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
bd5635a1 567
1304f099 568 if (msymbol == NULL)
bd5635a1
RP
569 error ("Execution is not within a known function.");
570
2e4964ad 571 step_range_start = SYMBOL_VALUE_ADDRESS (msymbol);
bd5635a1
RP
572 step_range_end = pc;
573 }
574 else
575 {
576 sal = find_pc_line (pc, 0);
577
578 step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
579 step_range_end = sal.end;
580 }
581
582 step_over_calls = 1;
583 step_frame_address = FRAME_FP (frame);
584
585 step_multi = 0; /* Only one call to proceed */
586
587 proceed ((CORE_ADDR) -1, -1, 1);
588}
589
1304f099 590static void
bd5635a1
RP
591until_command (arg, from_tty)
592 char *arg;
593 int from_tty;
594{
595 if (!target_has_execution)
596 error ("The program is not running.");
597 if (arg)
598 until_break_command (arg, from_tty);
599 else
600 until_next_command (from_tty);
601}
602\f
603/* "finish": Set a temporary breakpoint at the place
604 the selected frame will return to, then continue. */
605
606static void
607finish_command (arg, from_tty)
608 char *arg;
609 int from_tty;
610{
611 struct symtab_and_line sal;
612 register FRAME frame;
613 struct frame_info *fi;
614 register struct symbol *function;
1304f099
JG
615 struct breakpoint *breakpoint;
616 struct cleanup *old_chain;
bd5635a1
RP
617
618 if (arg)
619 error ("The \"finish\" command does not take any arguments.");
620 if (!target_has_execution)
621 error ("The program is not running.");
777bef06
JK
622 if (selected_frame == NULL)
623 error ("No selected frame.");
bd5635a1
RP
624
625 frame = get_prev_frame (selected_frame);
626 if (frame == 0)
627 error ("\"finish\" not meaningful in the outermost frame.");
628
629 clear_proceed_status ();
630
631 fi = get_frame_info (frame);
632 sal = find_pc_line (fi->pc, 0);
633 sal.pc = fi->pc;
1304f099
JG
634
635 breakpoint = set_momentary_breakpoint (sal, frame, bp_finish);
636
637 old_chain = make_cleanup(delete_breakpoint, breakpoint);
bd5635a1
RP
638
639 /* Find the function we will return from. */
640
641 fi = get_frame_info (selected_frame);
642 function = find_pc_function (fi->pc);
643
ee0613d1
JG
644 /* Print info on the selected frame, including level number
645 but not source. */
bd5635a1
RP
646 if (from_tty)
647 {
ee0613d1
JG
648 printf_filtered ("Run till exit from ");
649 print_stack_frame (selected_frame, selected_frame_level, 0);
bd5635a1
RP
650 }
651
652 proceed_to_finish = 1; /* We want stop_registers, please... */
653 proceed ((CORE_ADDR) -1, -1, 0);
654
1304f099
JG
655 /* Did we stop at our breakpoint? */
656 if (bpstat_find_breakpoint(stop_bpstat, breakpoint) != NULL
657 && function != 0)
bd5635a1
RP
658 {
659 struct type *value_type;
660 register value val;
661 CORE_ADDR funcaddr;
662
663 value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
664 if (!value_type)
665 fatal ("internal: finish_command: function has no target type");
666
667 if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
668 return;
669
670 funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
671
672 val = value_being_returned (value_type, stop_registers,
673 using_struct_return (value_of_variable (function),
674 funcaddr,
675 value_type,
676 BLOCK_GCC_COMPILED (SYMBOL_BLOCK_VALUE (function))));
677
ee0613d1 678 printf_filtered ("Value returned is $%d = ", record_latest_value (val));
bd5635a1 679 value_print (val, stdout, 0, Val_no_prettyprint);
ee0613d1 680 printf_filtered ("\n");
bd5635a1 681 }
1304f099 682 do_cleanups(old_chain);
bd5635a1
RP
683}
684\f
e1ce8aa5 685/* ARGSUSED */
bd5635a1
RP
686static void
687program_info (args, from_tty)
688 char *args;
689 int from_tty;
690{
691 bpstat bs = stop_bpstat;
692 int num = bpstat_num (&bs);
693
694 if (!target_has_execution)
695 {
1304f099 696 printf_filtered ("The program being debugged is not being run.\n");
bd5635a1
RP
697 return;
698 }
699
700 target_files_info ();
1304f099 701 printf_filtered ("Program stopped at %s.\n", local_hex_string(stop_pc));
bd5635a1 702 if (stop_step)
1304f099 703 printf_filtered ("It stopped after being stepped.\n");
bd5635a1
RP
704 else if (num != 0)
705 {
706 /* There may be several breakpoints in the same place, so this
707 isn't as strange as it seems. */
708 while (num != 0)
709 {
710 if (num < 0)
1304f099 711 printf_filtered ("It stopped at a breakpoint that has since been deleted.\n");
bd5635a1 712 else
1304f099 713 printf_filtered ("It stopped at breakpoint %d.\n", num);
bd5635a1
RP
714 num = bpstat_num (&bs);
715 }
716 }
717 else if (stop_signal) {
718#ifdef PRINT_RANDOM_SIGNAL
719 PRINT_RANDOM_SIGNAL (stop_signal);
720#else
1304f099
JG
721 printf_filtered ("It stopped with signal %d (%s).\n",
722 stop_signal, safe_strsignal (stop_signal));
bd5635a1
RP
723#endif
724 }
725
726 if (!from_tty)
1304f099 727 printf_filtered ("Type \"info stack\" or \"info registers\" for more information.\n");
bd5635a1
RP
728}
729\f
730static void
1304f099 731environment_info (var, from_tty)
bd5635a1 732 char *var;
1304f099 733 int from_tty;
bd5635a1
RP
734{
735 if (var)
736 {
737 register char *val = get_in_environ (inferior_environ, var);
738 if (val)
631f7a9f
JG
739 {
740 puts_filtered (var);
741 puts_filtered (" = ");
742 puts_filtered (val);
743 puts_filtered ("\n");
744 }
bd5635a1 745 else
631f7a9f
JG
746 {
747 puts_filtered ("Environment variable \"");
748 puts_filtered (var);
749 puts_filtered ("\" not defined.\n");
750 }
bd5635a1
RP
751 }
752 else
753 {
754 register char **vector = environ_vector (inferior_environ);
755 while (*vector)
631f7a9f
JG
756 {
757 puts_filtered (*vector++);
758 puts_filtered ("\n");
759 }
bd5635a1
RP
760 }
761}
762
763static void
1304f099 764set_environment_command (arg, from_tty)
bd5635a1 765 char *arg;
1304f099 766 int from_tty;
bd5635a1
RP
767{
768 register char *p, *val, *var;
769 int nullset = 0;
770
771 if (arg == 0)
772 error_no_arg ("environment variable and value");
773
774 /* Find seperation between variable name and value */
775 p = (char *) strchr (arg, '=');
776 val = (char *) strchr (arg, ' ');
777
778 if (p != 0 && val != 0)
779 {
780 /* We have both a space and an equals. If the space is before the
631f7a9f
JG
781 equals, walk forward over the spaces til we see a nonspace
782 (possibly the equals). */
bd5635a1
RP
783 if (p > val)
784 while (*val == ' ')
785 val++;
786
631f7a9f
JG
787 /* Now if the = is after the char following the spaces,
788 take the char following the spaces. */
789 if (p > val)
b5a3d2aa 790 p = val - 1;
bd5635a1
RP
791 }
792 else if (val != 0 && p == 0)
793 p = val;
794
795 if (p == arg)
796 error_no_arg ("environment variable to set");
797
798 if (p == 0 || p[1] == 0)
799 {
800 nullset = 1;
801 if (p == 0)
802 p = arg + strlen (arg); /* So that savestring below will work */
803 }
804 else
805 {
806 /* Not setting variable value to null */
807 val = p + 1;
808 while (*val == ' ' || *val == '\t')
809 val++;
810 }
811
812 while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
813
814 var = savestring (arg, p - arg);
815 if (nullset)
816 {
1304f099 817 printf_filtered ("Setting environment variable \"%s\" to null value.\n", var);
bd5635a1
RP
818 set_in_environ (inferior_environ, var, "");
819 }
820 else
821 set_in_environ (inferior_environ, var, val);
822 free (var);
823}
824
825static void
826unset_environment_command (var, from_tty)
827 char *var;
828 int from_tty;
829{
830 if (var == 0)
831 {
832 /* If there is no argument, delete all environment variables.
833 Ask for confirmation if reading from the terminal. */
834 if (!from_tty || query ("Delete all environment variables? "))
835 {
836 free_environ (inferior_environ);
837 inferior_environ = make_environ ();
838 }
839 }
840 else
841 unset_in_environ (inferior_environ, var);
842}
843
844/* Handle the execution path (PATH variable) */
845
a8a69e63 846static const char path_var_name[] = "PATH";
bd5635a1 847
e1ce8aa5 848/* ARGSUSED */
1304f099 849static void
bd5635a1
RP
850path_info (args, from_tty)
851 char *args;
852 int from_tty;
853{
b5a3d2aa
SG
854 puts_filtered ("Executable and object file path: ");
855 puts_filtered (get_in_environ (inferior_environ, path_var_name));
856 puts_filtered ("\n");
bd5635a1
RP
857}
858
859/* Add zero or more directories to the front of the execution path. */
860
1304f099 861static void
bd5635a1
RP
862path_command (dirname, from_tty)
863 char *dirname;
864 int from_tty;
865{
866 char *exec_path;
867
868 dont_repeat ();
869 exec_path = strsave (get_in_environ (inferior_environ, path_var_name));
e1ce8aa5 870 mod_path (dirname, &exec_path);
bd5635a1
RP
871 set_in_environ (inferior_environ, path_var_name, exec_path);
872 free (exec_path);
873 if (from_tty)
e1ce8aa5 874 path_info ((char *)NULL, from_tty);
bd5635a1
RP
875}
876\f
877CORE_ADDR
878read_pc ()
879{
880 return ADDR_BITS_REMOVE ((CORE_ADDR) read_register (PC_REGNUM));
881}
882
883void
884write_pc (val)
885 CORE_ADDR val;
886{
887 write_register (PC_REGNUM, (long) val);
888#ifdef NPC_REGNUM
889 write_register (NPC_REGNUM, (long) val+4);
890#endif
891 pc_changed = 0;
892}
893
1304f099 894const char * const reg_names[] = REGISTER_NAMES;
bd5635a1
RP
895
896/* Print out the machine register regnum. If regnum is -1,
ee0613d1
JG
897 print all registers (fpregs == 1) or all non-float registers
898 (fpregs == 0).
899
bd5635a1
RP
900 For most machines, having all_registers_info() print the
901 register(s) one per line is good enough. If a different format
ee0613d1 902 is required, (eg, for MIPS or Pyramid 90x, which both have
bd5635a1 903 lots of regs), or there is an existing convention for showing
ee0613d1 904 all the registers, define the macro DO_REGISTERS_INFO(regnum, fp)
bd5635a1 905 to provide that format. */
ee0613d1 906
bd5635a1 907#if !defined (DO_REGISTERS_INFO)
ee0613d1
JG
908#define DO_REGISTERS_INFO(regnum, fp) do_registers_info(regnum, fp)
909static void
910do_registers_info (regnum, fpregs)
bd5635a1 911 int regnum;
ee0613d1 912 int fpregs;
bd5635a1
RP
913{
914 register int i;
915
bd5635a1
RP
916 for (i = 0; i < NUM_REGS; i++)
917 {
918 char raw_buffer[MAX_REGISTER_RAW_SIZE];
919 char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
920
ee0613d1
JG
921 /* Decide between printing all regs, nonfloat regs, or specific reg. */
922 if (regnum == -1) {
923 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT && !fpregs)
924 continue;
925 } else {
926 if (i != regnum)
927 continue;
928 }
bd5635a1
RP
929
930 fputs_filtered (reg_names[i], stdout);
931 print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
932
933 /* Get the data in raw format, then convert also to virtual format. */
934 if (read_relative_register_raw_bytes (i, raw_buffer))
935 {
936 printf_filtered ("Invalid register contents\n");
937 continue;
938 }
939
b5a3d2aa 940 REGISTER_CONVERT_TO_VIRTUAL (i, raw_buffer, virtual_buffer);
bd5635a1
RP
941
942 /* If virtual format is floating, print it that way, and in raw hex. */
943 if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
944 && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
945 {
946 register int j;
947
948 val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
949 stdout, 0, 1, 0, Val_pretty_default);
950
951 printf_filtered ("\t(raw 0x");
952 for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
953 printf_filtered ("%02x", (unsigned char)raw_buffer[j]);
954 printf_filtered (")");
955 }
956
957/* FIXME! val_print probably can handle all of these cases now... */
958
959 /* Else if virtual format is too long for printf,
960 print in hex a byte at a time. */
961 else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
962 {
963 register int j;
964 printf_filtered ("0x");
965 for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
966 printf_filtered ("%02x", (unsigned char)virtual_buffer[j]);
967 }
968 /* Else print as integer in hex and in decimal. */
969 else
970 {
971 val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
972 stdout, 'x', 1, 0, Val_pretty_default);
973 printf_filtered ("\t");
974 val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
975 stdout, 0, 1, 0, Val_pretty_default);
976 }
977
978 /* The SPARC wants to print even-numbered float regs as doubles
979 in addition to printing them as floats. */
980#ifdef PRINT_REGISTER_HOOK
981 PRINT_REGISTER_HOOK (i);
982#endif
983
984 printf_filtered ("\n");
985 }
986}
987#endif /* no DO_REGISTERS_INFO. */
988
989static void
ee0613d1 990registers_info (addr_exp, fpregs)
bd5635a1 991 char *addr_exp;
ee0613d1 992 int fpregs;
bd5635a1
RP
993{
994 int regnum;
b5a3d2aa 995 register char *end;
bd5635a1
RP
996
997 if (!target_has_registers)
998 error ("The program has no registers now.");
999
b5a3d2aa 1000 if (!addr_exp)
bd5635a1 1001 {
b5a3d2aa
SG
1002 DO_REGISTERS_INFO(-1, fpregs);
1003 return;
bd5635a1 1004 }
bd5635a1 1005
b5a3d2aa
SG
1006 do
1007 {
1008 if (addr_exp[0] == '$')
1009 addr_exp++;
1010 end = addr_exp;
1011 while (*end != '\0' && *end != ' ' && *end != '\t')
1012 ++end;
1013 for (regnum = 0; regnum < NUM_REGS; regnum++)
1014 if (!strncmp (addr_exp, reg_names[regnum], end - addr_exp)
1015 && strlen (reg_names[regnum]) == end - addr_exp)
1016 goto found;
1017 if (*addr_exp >= '0' && *addr_exp <= '9')
1018 regnum = atoi (addr_exp); /* Take a number */
1019 if (regnum >= NUM_REGS) /* Bad name, or bad number */
1020 error ("%.*s: invalid register", end - addr_exp, addr_exp);
1021
1022found:
1023 DO_REGISTERS_INFO(regnum, fpregs);
1024
1025 addr_exp = end;
1026 while (*addr_exp == ' ' || *addr_exp == '\t')
1027 ++addr_exp;
1028 } while (*addr_exp != '\0');
ee0613d1
JG
1029}
1030
1031static void
1304f099 1032all_registers_info (addr_exp, from_tty)
ee0613d1 1033 char *addr_exp;
1304f099 1034 int from_tty;
ee0613d1
JG
1035{
1036 registers_info (addr_exp, 1);
1037}
1038
1039static void
1304f099 1040nofp_registers_info (addr_exp, from_tty)
ee0613d1 1041 char *addr_exp;
1304f099 1042 int from_tty;
ee0613d1
JG
1043{
1044 registers_info (addr_exp, 0);
bd5635a1
RP
1045}
1046\f
1047/*
1048 * TODO:
1049 * Should save/restore the tty state since it might be that the
1050 * program to be debugged was started on this tty and it wants
1051 * the tty in some state other than what we want. If it's running
1052 * on another terminal or without a terminal, then saving and
1053 * restoring the tty state is a harmless no-op.
1054 * This only needs to be done if we are attaching to a process.
1055 */
1056
1057/*
b5a3d2aa
SG
1058 attach_command --
1059 takes a program started up outside of gdb and ``attaches'' to it.
1060 This stops it cold in its tracks and allows us to start debugging it.
1061 and wait for the trace-trap that results from attaching. */
1062
bd5635a1
RP
1063void
1064attach_command (args, from_tty)
1065 char *args;
1066 int from_tty;
1067{
f266e564 1068 dont_repeat (); /* Not for the faint of heart */
b5a3d2aa
SG
1069
1070 if (target_has_execution)
1071 {
1072 if (query ("A program is being debugged already. Kill it? "))
1073 target_kill ();
1074 else
1075 error ("Inferior not killed.");
1076 }
1077
bd5635a1 1078 target_attach (args, from_tty);
b5a3d2aa
SG
1079
1080 /* Set up the "saved terminal modes" of the inferior
1081 based on what modes we are starting it with. */
1082 target_terminal_init ();
1083
1084 /* Install inferior's terminal modes. */
1085 target_terminal_inferior ();
1086
1087 /* Set up execution context to know that we should return from
1088 wait_for_inferior as soon as the target reports a stop. */
1089 init_wait_for_inferior ();
1090 clear_proceed_status ();
1091 stop_soon_quietly = 1;
1092
1093 wait_for_inferior ();
1094
1095#ifdef SOLIB_ADD
1096 /* Add shared library symbols from the newly attached process, if any. */
1097 SOLIB_ADD ((char *)0, from_tty, (struct target_ops *)0);
1098#endif
1099
1100 normal_stop ();
bd5635a1
RP
1101}
1102
1103/*
1104 * detach_command --
1105 * takes a program previously attached to and detaches it.
1106 * The program resumes execution and will no longer stop
1107 * on signals, etc. We better not have left any breakpoints
1108 * in the program or it'll die when it hits one. For this
1109 * to work, it may be necessary for the process to have been
1110 * previously attached. It *might* work if the program was
1111 * started via the normal ptrace (PTRACE_TRACEME).
1112 */
1113
1114static void
1115detach_command (args, from_tty)
1116 char *args;
1117 int from_tty;
1118{
f266e564 1119 dont_repeat (); /* Not for the faint of heart */
bd5635a1
RP
1120 target_detach (args, from_tty);
1121}
1122
1123/* ARGSUSED */
1124static void
1304f099 1125float_info (addr_exp, from_tty)
bd5635a1 1126 char *addr_exp;
1304f099 1127 int from_tty;
bd5635a1
RP
1128{
1129#ifdef FLOAT_INFO
1130 FLOAT_INFO;
1131#else
1304f099 1132 printf_filtered ("No floating point info available for this processor.\n");
bd5635a1
RP
1133#endif
1134}
1135\f
f266e564
JK
1136/* ARGSUSED */
1137static void
1138unset_command (args, from_tty)
1139 char *args;
1140 int from_tty;
1141{
1304f099 1142 printf_filtered ("\"unset\" must be followed by the name of an unset subcommand.\n");
f266e564
JK
1143 help_list (unsetlist, "unset ", -1, stdout);
1144}
1145
bd5635a1
RP
1146void
1147_initialize_infcmd ()
1148{
1149 struct cmd_list_element *c;
1150
1151 add_com ("tty", class_run, tty_command,
1152 "Set terminal for future runs of program being debugged.");
1153
1154 add_show_from_set
1155 (add_set_cmd ("args", class_run, var_string_noescape, (char *)&inferior_args,
1156
1157"Set arguments to give program being debugged when it is started.\n\
1158Follow this command with any number of args, to be passed to the program.",
1159 &setlist),
1160 &showlist);
1161
1162 c = add_cmd
1163 ("environment", no_class, environment_info,
1164 "The environment to give the program, or one variable's value.\n\
1165With an argument VAR, prints the value of environment variable VAR to\n\
1166give the program being debugged. With no arguments, prints the entire\n\
1167environment to be given to the program.", &showlist);
1168 c->completer = noop_completer;
1169
f266e564
JK
1170 add_prefix_cmd ("unset", no_class, unset_command,
1171 "Complement to certain \"set\" commands",
1172 &unsetlist, "unset ", 0, &cmdlist);
1173
bd5635a1
RP
1174 c = add_cmd ("environment", class_run, unset_environment_command,
1175 "Cancel environment variable VAR for the program.\n\
1176This does not affect the program until the next \"run\" command.",
f266e564 1177 &unsetlist);
bd5635a1
RP
1178 c->completer = noop_completer;
1179
1180 c = add_cmd ("environment", class_run, set_environment_command,
1181 "Set environment variable value to give the program.\n\
1182Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
1183VALUES of environment variables are uninterpreted strings.\n\
1184This does not affect the program until the next \"run\" command.",
1185 &setlist);
1186 c->completer = noop_completer;
1187
1188 add_com ("path", class_files, path_command,
1189 "Add directory DIR(s) to beginning of search path for object files.\n\
1190$cwd in the path means the current working directory.\n\
1191This path is equivalent to the $PATH shell variable. It is a list of\n\
1192directories, separated by colons. These directories are searched to find\n\
1193fully linked executable files and separately compiled object files as needed.");
1194
ee0613d1 1195 c = add_cmd ("paths", no_class, path_info,
bd5635a1
RP
1196 "Current search path for finding object files.\n\
1197$cwd in the path means the current working directory.\n\
1198This path is equivalent to the $PATH shell variable. It is a list of\n\
1199directories, separated by colons. These directories are searched to find\n\
ee0613d1
JG
1200fully linked executable files and separately compiled object files as needed.", &showlist);
1201 c->completer = noop_completer;
bd5635a1
RP
1202
1203 add_com ("attach", class_run, attach_command,
1204 "Attach to a process or file outside of GDB.\n\
1205This command attaches to another target, of the same type as your last\n\
1206`target' command (`info files' will show your target stack).\n\
1207The command may take as argument a process id or a device file.\n\
1208For a process id, you must have permission to send the process a signal,\n\
1209and it must have the same effective uid as the debugger.\n\
1210When using \"attach\", you should use the \"file\" command to specify\n\
1211the program running in the process, and to load its symbol table.");
1212
1213 add_com ("detach", class_run, detach_command,
1214 "Detach a process or file previously attached.\n\
1215If a process, it is no longer traced, and it continues its execution. If you\n\
1216were debugging a file, the file is closed and gdb no longer accesses it.");
1217
1218 add_com ("signal", class_run, signal_command,
1219 "Continue program giving it signal number SIGNUMBER.");
1220
1221 add_com ("stepi", class_run, stepi_command,
1222 "Step one instruction exactly.\n\
1223Argument N means do this N times (or till program stops for another reason).");
1224 add_com_alias ("si", "stepi", class_alias, 0);
1225
1226 add_com ("nexti", class_run, nexti_command,
1227 "Step one instruction, but proceed through subroutine calls.\n\
1228Argument N means do this N times (or till program stops for another reason).");
1229 add_com_alias ("ni", "nexti", class_alias, 0);
1230
1231 add_com ("finish", class_run, finish_command,
1232 "Execute until selected stack frame returns.\n\
1233Upon return, the value returned is printed and put in the value history.");
1234
1235 add_com ("next", class_run, next_command,
1236 "Step program, proceeding through subroutine calls.\n\
1237Like the \"step\" command as long as subroutine calls do not happen;\n\
1238when they do, the call is treated as one instruction.\n\
1239Argument N means do this N times (or till program stops for another reason).");
1240 add_com_alias ("n", "next", class_run, 1);
1241
1242 add_com ("step", class_run, step_command,
1243 "Step program until it reaches a different source line.\n\
1244Argument N means do this N times (or till program stops for another reason).");
1245 add_com_alias ("s", "step", class_run, 1);
1246
1247 add_com ("until", class_run, until_command,
1248 "Execute until the program reaches a source line greater than the current\n\
1249or a specified line or address or function (same args as break command).\n\
1250Execution will also stop upon exit from the current stack frame.");
1251 add_com_alias ("u", "until", class_run, 1);
1252
1253 add_com ("jump", class_run, jump_command,
1254 "Continue program being debugged at specified line or address.\n\
1255Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
1256for an address to start at.");
1257
1258 add_com ("continue", class_run, continue_command,
1259 "Continue program being debugged, after signal or breakpoint.\n\
1260If proceeding from breakpoint, a number N may be used as an argument:\n\
1261then the same breakpoint won't break until the Nth time it is reached.");
1262 add_com_alias ("c", "cont", class_run, 1);
1263 add_com_alias ("fg", "cont", class_run, 1);
1264
1265 add_com ("run", class_run, run_command,
1266 "Start debugged program. You may specify arguments to give it.\n\
1267Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
1268Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
b4fde6fa 1269With no arguments, uses arguments last specified (with \"run\" or \"set args\").\n\
bd5635a1
RP
1270To cancel previous arguments and run with no arguments,\n\
1271use \"set args\" without arguments.");
1272 add_com_alias ("r", "run", class_run, 1);
1273
ee0613d1
JG
1274 add_info ("registers", nofp_registers_info,
1275 "List of integer registers and their contents, for selected stack frame.\n\
1276Register name as argument means describe only that register.");
1277
1278 add_info ("all-registers", all_registers_info,
1279"List of all registers and their contents, for selected stack frame.\n\
bd5635a1
RP
1280Register name as argument means describe only that register.");
1281
1282 add_info ("program", program_info,
1283 "Execution status of the program.");
1284
1285 add_info ("float", float_info,
1286 "Print the status of the floating point unit\n");
1287
1288 inferior_args = savestring ("", 1); /* Initially no args */
1289 inferior_environ = make_environ ();
1290 init_environ (inferior_environ);
1291}