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