* Rename remote-es1800.c to remote-es.c
[deliverable/binutils-gdb.git] / gdb / stack.c
CommitLineData
bd5635a1 1/* Print and select stack frames for GDB, the GNU debugger.
bd5d07d9 2 Copyright 1986, 1987, 1989, 1991, 1992, 1993 Free Software Foundation, Inc.
bd5635a1
RP
3
4This file is part of GDB.
5
cadbb07a 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
cadbb07a
JG
8the Free Software Foundation; either version 2 of the License, or
9(at your option) any later version.
bd5635a1 10
cadbb07a 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
cadbb07a
JG
17along with this program; if not, write to the Free Software
18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
bd5635a1 19
bd5635a1 20#include "defs.h"
b4fde6fa 21#include "value.h"
bd5635a1 22#include "symtab.h"
b4fde6fa
FF
23#include "gdbtypes.h"
24#include "expression.h"
25#include "language.h"
bd5635a1
RP
26#include "frame.h"
27#include "gdbcmd.h"
bd5635a1
RP
28#include "gdbcore.h"
29#include "target.h"
30#include "breakpoint.h"
b4fde6fa 31#include "demangle.h"
df2a1bd7 32#include "inferior.h"
b4fde6fa
FF
33
34static void
35return_command PARAMS ((char *, int));
36
37static void
38down_command PARAMS ((char *, int));
39
40static void
41down_silently_command PARAMS ((char *, int));
42
43static void
44up_command PARAMS ((char *, int));
45
46static void
47up_silently_command PARAMS ((char *, int));
48
49static void
50frame_command PARAMS ((char *, int));
51
52static void
53select_frame_command PARAMS ((char *, int));
54
55static void
56args_info PARAMS ((char *, int));
57
58static void
59print_frame_arg_vars PARAMS ((FRAME, FILE *));
60
61static void
62catch_info PARAMS ((char *, int));
63
64static void
65locals_info PARAMS ((char *, int));
66
67static void
68print_frame_label_vars PARAMS ((FRAME, int, FILE *));
69
70static void
71print_frame_local_vars PARAMS ((FRAME, FILE *));
72
73static int
74print_block_frame_labels PARAMS ((struct block *, int *, FILE *));
75
76static int
77print_block_frame_locals PARAMS ((struct block *, FRAME, FILE *));
78
79static void
80backtrace_command PARAMS ((char *, int));
81
82static FRAME
83parse_frame_specification PARAMS ((char *));
84
85static void
86frame_info PARAMS ((char *, int));
87
bd5635a1
RP
88
89extern int addressprint; /* Print addresses, or stay symbolic only? */
90extern int info_verbose; /* Verbosity of symbol reading msgs */
d8ce1326 91extern int lines_to_list; /* # of lines "list" command shows by default */
bd5635a1 92
b4fde6fa 93/* The "selected" stack frame is used by default for local and arg access.
bd5635a1
RP
94 May be zero, for no selected frame. */
95
96FRAME selected_frame;
97
98/* Level of the selected frame:
99 0 for innermost, 1 for its caller, ...
100 or -1 for frame specified by address with no defined level. */
101
102int selected_frame_level;
103
104/* Nonzero means print the full filename and linenumber
105 when a frame is printed, and do so in a format programs can parse. */
106
107int frame_file_full_name = 0;
108
bd5635a1
RP
109\f
110/* Print a stack frame briefly. FRAME should be the frame id
111 and LEVEL should be its level in the stack (or -1 for level not defined).
112 This prints the level, the function executing, the arguments,
113 and the file name and line number.
114 If the pc is not at the beginning of the source line,
115 the actual pc is printed at the beginning.
116
117 If SOURCE is 1, print the source line as well.
118 If SOURCE is -1, print ONLY the source line. */
119
cadbb07a 120void
bd5635a1
RP
121print_stack_frame (frame, level, source)
122 FRAME frame;
123 int level;
124 int source;
125{
126 struct frame_info *fi;
127
128 fi = get_frame_info (frame);
129
130 print_frame_info (fi, level, source, 1);
131}
132
133void
134print_frame_info (fi, level, source, args)
135 struct frame_info *fi;
136 register int level;
137 int source;
138 int args;
139{
140 struct symtab_and_line sal;
141 struct symbol *func;
142 register char *funname = 0;
bd5d07d9 143 enum language funlang = language_unknown;
bd5635a1
RP
144 int numargs;
145
df2a1bd7
JK
146 if (PC_IN_CALL_DUMMY (fi->pc, read_register (SP_REGNUM), fi->frame))
147 {
148 /* Do this regardless of SOURCE because we don't have any source
149 to list for this frame. */
150 if (level >= 0)
151 printf_filtered ("#%-2d ", level);
152 printf_filtered ("<function called from gdb>\n");
153 return;
154 }
cee86be3
JK
155 if (fi->signal_handler_caller)
156 {
157 /* Do this regardless of SOURCE because we don't have any source
158 to list for this frame. */
159 if (level >= 0)
160 printf_filtered ("#%-2d ", level);
161 printf_filtered ("<signal handler called>\n");
162 return;
163 }
b4fde6fa 164
bd5635a1
RP
165 sal = find_pc_line (fi->pc, fi->next_frame);
166 func = find_pc_function (fi->pc);
167 if (func)
168 {
169 /* In certain pathological cases, the symtabs give the wrong
170 function (when we are in the first function in a file which
171 is compiled without debugging symbols, the previous function
172 is compiled with debugging symbols, and the "foo.o" symbol
173 that is supposed to tell us where the file with debugging symbols
174 ends has been truncated by ar because it is longer than 15
175 characters).
176
b4fde6fa 177 So look in the minimal symbol tables as well, and if it comes
bd5635a1 178 up with a larger address for the function use that instead.
b4fde6fa
FF
179 I don't think this can ever cause any problems; there shouldn't
180 be any minimal symbols in the middle of a function.
181 FIXME: (Not necessarily true. What about text labels) */
182
183 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
184 if (msymbol != NULL
bd5d07d9 185 && (SYMBOL_VALUE_ADDRESS (msymbol)
bd5635a1
RP
186 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
187 {
188 /* In this case we have no way of knowing the source file
189 and line number, so don't print them. */
190 sal.symtab = 0;
191 /* We also don't know anything about the function besides
192 its address and name. */
193 func = 0;
bd5d07d9
FF
194 funname = SYMBOL_NAME (msymbol);
195 funlang = SYMBOL_LANGUAGE (msymbol);
bd5635a1
RP
196 }
197 else
bd5d07d9
FF
198 {
199 funname = SYMBOL_NAME (func);
200 funlang = SYMBOL_LANGUAGE (func);
201 }
bd5635a1
RP
202 }
203 else
204 {
b4fde6fa
FF
205 register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
206 if (msymbol != NULL)
bd5d07d9
FF
207 {
208 funname = SYMBOL_NAME (msymbol);
209 funlang = SYMBOL_LANGUAGE (msymbol);
210 }
bd5635a1
RP
211 }
212
213 if (source >= 0 || !sal.symtab)
214 {
215 if (level >= 0)
216 printf_filtered ("#%-2d ", level);
217 if (addressprint)
218 if (fi->pc != sal.pc || !sal.symtab)
89e0bbcd 219 printf_filtered ("%s in ", local_hex_string(fi->pc));
5e81259d
FF
220 fprintf_symbol_filtered (stdout, funname ? funname : "??", funlang,
221 DMGL_NO_OPTS);
bd5635a1
RP
222 wrap_here (" ");
223 fputs_filtered (" (", stdout);
224 if (args)
225 {
226 FRAME_NUM_ARGS (numargs, fi);
227 print_frame_args (func, fi, numargs, stdout);
228 }
229 printf_filtered (")");
230 if (sal.symtab && sal.symtab->filename)
231 {
232 wrap_here (" ");
233 printf_filtered (" at %s:%d", sal.symtab->filename, sal.line);
234 }
b4fde6fa
FF
235
236#ifdef PC_LOAD_SEGMENT
237 /* If we couldn't print out function name but if can figure out what
238 load segment this pc value is from, at least print out some info
239 about its load segment. */
240 if (!funname) {
241 wrap_here (" ");
242 printf_filtered (" from %s", PC_LOAD_SEGMENT (fi->pc));
243 }
244#endif
bd5635a1
RP
245 printf_filtered ("\n");
246 }
247
248 if ((source != 0) && sal.symtab)
249 {
250 int done = 0;
251 int mid_statement = source < 0 && fi->pc != sal.pc;
252 if (frame_file_full_name)
253 done = identify_source_line (sal.symtab, sal.line, mid_statement);
254 if (!done)
255 {
256 if (addressprint && mid_statement)
89e0bbcd 257 printf_filtered ("%s\t", local_hex_string(fi->pc));
bd5635a1
RP
258 print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
259 }
cadbb07a 260 current_source_line = max (sal.line - lines_to_list/2, 1);
bd5635a1
RP
261 }
262 if (source != 0)
263 set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
264
265 fflush (stdout);
266}
267
bd5635a1
RP
268/*
269 * Read a frame specification in whatever the appropriate format is.
777bef06
JK
270 * Call error() if the specification is in any way invalid (i.e.
271 * this function never returns NULL).
bd5635a1
RP
272 */
273static FRAME
274parse_frame_specification (frame_exp)
275 char *frame_exp;
276{
277 int numargs = 0;
bd5d07d9
FF
278 int arg1, arg2, arg3;
279#define MAXARGS 4
280 int args[MAXARGS];
bd5635a1
RP
281
282 if (frame_exp)
283 {
284 char *addr_string, *p;
285 struct cleanup *tmp_cleanup;
286
287 while (*frame_exp == ' ') frame_exp++;
bd5635a1 288
bd5d07d9 289 while (*frame_exp)
bd5635a1 290 {
bd5d07d9
FF
291 if (numargs > MAXARGS)
292 error ("Too many args in frame specification");
293 /* Parse an argument. */
294 for (p = frame_exp; *p && *p != ' '; p++)
295 ;
bd5635a1
RP
296 addr_string = savestring(frame_exp, p - frame_exp);
297
298 {
299 tmp_cleanup = make_cleanup (free, addr_string);
bd5d07d9 300 args[numargs++] = parse_and_eval_address (addr_string);
bd5635a1
RP
301 do_cleanups (tmp_cleanup);
302 }
303
bd5d07d9 304 /* Skip spaces, move to possible next arg. */
bd5635a1 305 while (*p == ' ') p++;
bd5d07d9 306 frame_exp = p;
bd5635a1
RP
307 }
308 }
309
310 switch (numargs)
311 {
312 case 0:
777bef06
JK
313 if (selected_frame == NULL)
314 error ("No selected frame.");
bd5635a1
RP
315 return selected_frame;
316 /* NOTREACHED */
317 case 1:
318 {
bd5d07d9 319 int level = args[0];
bd5635a1
RP
320 FRAME fid = find_relative_frame (get_current_frame (), &level);
321 FRAME tfid;
322
323 if (level == 0)
324 /* find_relative_frame was successful */
325 return fid;
326
327 /* If (s)he specifies the frame with an address, he deserves what
328 (s)he gets. Still, give the highest one that matches. */
329
330 for (fid = get_current_frame ();
bd5d07d9 331 fid && FRAME_FP (fid) != args[0];
bd5635a1
RP
332 fid = get_prev_frame (fid))
333 ;
334
335 if (fid)
336 while ((tfid = get_prev_frame (fid)) &&
bd5d07d9 337 (FRAME_FP (tfid) == args[0]))
bd5635a1
RP
338 fid = tfid;
339
bd5d07d9
FF
340 /* We couldn't identify the frame as an existing frame, but
341 perhaps we can create one with a single argument.
342 Fall through to default case; it's up to SETUP_ARBITRARY_FRAME
343 to complain if it doesn't like a single arg. */
bd5635a1 344 }
bd5d07d9
FF
345
346 default:
347#ifdef SETUP_ARBITRARY_FRAME
348 return SETUP_ARBITRARY_FRAME (numargs, args);
bd5635a1 349#else
bd5d07d9
FF
350 /* Usual case. Do it here rather than have everyone supply
351 a SETUP_ARBITRARY_FRAME that does this. */
352 if (numargs == 1)
353 return create_new_frame (args[0], 0);
354 error ("Too many args in frame specification");
bd5635a1
RP
355#endif
356 /* NOTREACHED */
357 }
bd5635a1
RP
358 /* NOTREACHED */
359}
360
361/* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
362 that if it is unsure about the answer, it returns 0
363 instead of guessing (this happens on the VAX and i960, for example).
364
365 On most machines, we never have to guess about the args address,
366 so FRAME_ARGS_ADDRESS{,_CORRECT} are the same. */
367#if !defined (FRAME_ARGS_ADDRESS_CORRECT)
368#define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
369#endif
370
371/* Print verbosely the selected frame or the frame at address ADDR.
372 This means absolutely all information in the frame is printed. */
373
374static void
b4fde6fa 375frame_info (addr_exp, from_tty)
bd5635a1 376 char *addr_exp;
b4fde6fa 377 int from_tty;
bd5635a1
RP
378{
379 FRAME frame;
380 struct frame_info *fi;
381 struct frame_saved_regs fsr;
382 struct symtab_and_line sal;
383 struct symbol *func;
89e0bbcd 384 struct symtab *s;
bd5635a1
RP
385 FRAME calling_frame;
386 int i, count;
387 char *funname = 0;
bd5d07d9 388 enum language funlang = language_unknown;
bd5635a1
RP
389
390 if (!target_has_stack)
6fe90fc8 391 error ("No stack.");
bd5635a1
RP
392
393 frame = parse_frame_specification (addr_exp);
394 if (!frame)
395 error ("Invalid frame specified.");
396
397 fi = get_frame_info (frame);
398 sal = find_pc_line (fi->pc, fi->next_frame);
399 func = get_frame_function (frame);
89e0bbcd 400 s = find_pc_symtab(fi->pc);
bd5635a1 401 if (func)
bd5d07d9
FF
402 {
403 funname = SYMBOL_NAME (func);
404 funlang = SYMBOL_LANGUAGE (func);
405 }
bd5635a1
RP
406 else
407 {
b4fde6fa
FF
408 register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
409 if (msymbol != NULL)
bd5d07d9
FF
410 {
411 funname = SYMBOL_NAME (msymbol);
412 funlang = SYMBOL_LANGUAGE (msymbol);
413 }
bd5635a1
RP
414 }
415 calling_frame = get_prev_frame (frame);
416
89e0bbcd
JG
417 if (!addr_exp && selected_frame_level >= 0) {
418 printf_filtered ("Stack level %d, frame at %s:\n",
419 selected_frame_level,
420 local_hex_string(FRAME_FP(frame)));
421 } else {
422 printf_filtered ("Stack frame at %s:\n",
423 local_hex_string(FRAME_FP(frame)));
424 }
425 printf_filtered (" %s = %s",
426 reg_names[PC_REGNUM],
427 local_hex_string(fi->pc));
bd5635a1
RP
428
429 wrap_here (" ");
430 if (funname)
b0077123
JG
431 {
432 printf_filtered (" in ");
5e81259d
FF
433 fprintf_symbol_filtered (stdout, funname, funlang,
434 DMGL_ANSI | DMGL_PARAMS);
b0077123 435 }
bd5635a1
RP
436 wrap_here (" ");
437 if (sal.symtab)
438 printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
439 puts_filtered ("; ");
440 wrap_here (" ");
89e0bbcd
JG
441 printf_filtered ("saved %s %s\n", reg_names[PC_REGNUM],
442 local_hex_string(FRAME_SAVED_PC (frame)));
b4fde6fa
FF
443
444 {
445 int frameless = 0;
446#ifdef FRAMELESS_FUNCTION_INVOCATION
447 FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
448#endif
449 if (frameless)
450 printf_filtered (" (FRAMELESS),");
451 }
452
bd5635a1 453 if (calling_frame)
89e0bbcd
JG
454 printf_filtered (" called by frame at %s",
455 local_hex_string(FRAME_FP (calling_frame)));
bd5635a1
RP
456 if (fi->next_frame && calling_frame)
457 puts_filtered (",");
458 wrap_here (" ");
459 if (fi->next_frame)
89e0bbcd 460 printf_filtered (" caller of frame at %s", local_hex_string(fi->next_frame));
bd5635a1
RP
461 if (fi->next_frame || calling_frame)
462 puts_filtered ("\n");
89e0bbcd
JG
463 if (s)
464 printf_filtered(" source language %s.\n", language_str(s->language));
bd5635a1 465
bd5d07d9
FF
466#ifdef PRINT_EXTRA_FRAME_INFO
467 PRINT_EXTRA_FRAME_INFO (fi);
468#endif
469
bd5635a1
RP
470 {
471 /* Address of the argument list for this frame, or 0. */
472 CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
473 /* Number of args for this frame, or -1 if unknown. */
474 int numargs;
475
476 if (arg_list == 0)
477 printf_filtered (" Arglist at unknown address.\n");
478 else
479 {
89e0bbcd 480 printf_filtered (" Arglist at %s,", local_hex_string(arg_list));
bd5635a1
RP
481
482 FRAME_NUM_ARGS (numargs, fi);
483 if (numargs < 0)
484 puts_filtered (" args: ");
485 else if (numargs == 0)
486 puts_filtered (" no args.");
487 else if (numargs == 1)
488 puts_filtered (" 1 arg: ");
489 else
490 printf_filtered (" %d args: ", numargs);
491 print_frame_args (func, fi, numargs, stdout);
492 puts_filtered ("\n");
493 }
494 }
b4fde6fa
FF
495 {
496 /* Address of the local variables for this frame, or 0. */
497 CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
498
499 if (arg_list == 0)
500 printf_filtered (" Locals at unknown address,");
501 else
502 printf_filtered (" Locals at %s,", local_hex_string(arg_list));
503 }
bd5635a1
RP
504
505#if defined (FRAME_FIND_SAVED_REGS)
506 get_frame_saved_regs (fi, &fsr);
507 /* The sp is special; what's returned isn't the save address, but
508 actually the value of the previous frame's sp. */
89e0bbcd
JG
509 printf_filtered (" Previous frame's sp is %s\n",
510 local_hex_string(fsr.regs[SP_REGNUM]));
bd5635a1
RP
511 count = 0;
512 for (i = 0; i < NUM_REGS; i++)
513 if (fsr.regs[i] && i != SP_REGNUM)
514 {
515 if (count == 0)
516 puts_filtered (" Saved registers:\n ");
517 else
518 puts_filtered (",");
519 wrap_here (" ");
89e0bbcd
JG
520 printf_filtered (" %s at %s", reg_names[i],
521 local_hex_string(fsr.regs[i]));
bd5635a1
RP
522 count++;
523 }
524 if (count)
525 puts_filtered ("\n");
526#endif /* Have FRAME_FIND_SAVED_REGS. */
527}
528
529#if 0
530/* Set a limit on the number of frames printed by default in a
531 backtrace. */
532
533static int backtrace_limit;
534
535static void
536set_backtrace_limit_command (count_exp, from_tty)
537 char *count_exp;
538 int from_tty;
539{
540 int count = parse_and_eval_address (count_exp);
541
542 if (count < 0)
543 error ("Negative argument not meaningful as backtrace limit.");
544
545 backtrace_limit = count;
546}
547
548static void
549backtrace_limit_info (arg, from_tty)
550 char *arg;
551 int from_tty;
552{
553 if (arg)
554 error ("\"Info backtrace-limit\" takes no arguments.");
555
556 printf ("Backtrace limit: %d.\n", backtrace_limit);
557}
558#endif
559
560/* Print briefly all stack frames or just the innermost COUNT frames. */
561
562static void
563backtrace_command (count_exp, from_tty)
564 char *count_exp;
565 int from_tty;
566{
567 struct frame_info *fi;
568 register int count;
569 register FRAME frame;
570 register int i;
571 register FRAME trailing;
572 register int trailing_level;
573
777bef06
JK
574 if (!target_has_stack)
575 error ("No stack.");
576
bd5635a1
RP
577 /* The following code must do two things. First, it must
578 set the variable TRAILING to the frame from which we should start
579 printing. Second, it must set the variable count to the number
580 of frames which we should print, or -1 if all of them. */
581 trailing = get_current_frame ();
582 trailing_level = 0;
583 if (count_exp)
584 {
585 count = parse_and_eval_address (count_exp);
586 if (count < 0)
587 {
588 FRAME current;
589
590 count = -count;
591
592 current = trailing;
593 while (current && count--)
594 {
595 QUIT;
596 current = get_prev_frame (current);
597 }
598
599 /* Will stop when CURRENT reaches the top of the stack. TRAILING
600 will be COUNT below it. */
601 while (current)
602 {
603 QUIT;
604 trailing = get_prev_frame (trailing);
605 current = get_prev_frame (current);
606 trailing_level++;
607 }
608
609 count = -1;
610 }
611 }
612 else
613 count = -1;
614
615 if (info_verbose)
616 {
617 struct partial_symtab *ps;
618
619 /* Read in symbols for all of the frames. Need to do this in
620 a separate pass so that "Reading in symbols for xxx" messages
621 don't screw up the appearance of the backtrace. Also
622 if people have strong opinions against reading symbols for
623 backtrace this may have to be an option. */
624 i = count;
625 for (frame = trailing;
626 frame != NULL && i--;
627 frame = get_prev_frame (frame))
628 {
629 QUIT;
630 fi = get_frame_info (frame);
631 ps = find_pc_psymtab (fi->pc);
632 if (ps)
3de61d8c 633 PSYMTAB_TO_SYMTAB (ps); /* Force syms to come in */
bd5635a1
RP
634 }
635 }
636
637 for (i = 0, frame = trailing;
638 frame && count--;
639 i++, frame = get_prev_frame (frame))
640 {
641 QUIT;
642 fi = get_frame_info (frame);
643 print_frame_info (fi, trailing_level + i, 0, 1);
644 }
645
646 /* If we've stopped before the end, mention that. */
647 if (frame && from_tty)
648 printf_filtered ("(More stack frames follow...)\n");
649}
650\f
651/* Print the local variables of a block B active in FRAME.
652 Return 1 if any variables were printed; 0 otherwise. */
653
654static int
655print_block_frame_locals (b, frame, stream)
656 struct block *b;
657 register FRAME frame;
658 register FILE *stream;
659{
660 int nsyms;
661 register int i;
662 register struct symbol *sym;
663 register int values_printed = 0;
664
665 nsyms = BLOCK_NSYMS (b);
666
667 for (i = 0; i < nsyms; i++)
668 {
669 sym = BLOCK_SYM (b, i);
670 if (SYMBOL_CLASS (sym) == LOC_LOCAL
671 || SYMBOL_CLASS (sym) == LOC_REGISTER
672 || SYMBOL_CLASS (sym) == LOC_STATIC)
673 {
674 values_printed = 1;
bd5d07d9 675 fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
bd5635a1
RP
676 fputs_filtered (" = ", stream);
677 print_variable_value (sym, frame, stream);
678 fprintf_filtered (stream, "\n");
bd5635a1
RP
679 }
680 }
681 return values_printed;
682}
683
cadbb07a 684/* Same, but print labels. */
bd5635a1
RP
685
686static int
cadbb07a 687print_block_frame_labels (b, have_default, stream)
bd5635a1 688 struct block *b;
bd5635a1
RP
689 int *have_default;
690 register FILE *stream;
691{
692 int nsyms;
693 register int i;
694 register struct symbol *sym;
695 register int values_printed = 0;
696
697 nsyms = BLOCK_NSYMS (b);
698
699 for (i = 0; i < nsyms; i++)
700 {
701 sym = BLOCK_SYM (b, i);
bd5d07d9 702 if (STREQ (SYMBOL_NAME (sym), "default"))
bd5635a1
RP
703 {
704 if (*have_default)
705 continue;
706 *have_default = 1;
707 }
708 if (SYMBOL_CLASS (sym) == LOC_LABEL)
709 {
710 struct symtab_and_line sal;
711 sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
712 values_printed = 1;
bd5d07d9 713 fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
bd5635a1 714 if (addressprint)
89e0bbcd
JG
715 fprintf_filtered (stream, " %s",
716 local_hex_string(SYMBOL_VALUE_ADDRESS (sym)));
bd5635a1
RP
717 fprintf_filtered (stream, " in file %s, line %d\n",
718 sal.symtab->filename, sal.line);
bd5635a1
RP
719 }
720 }
721 return values_printed;
722}
723
724/* Print on STREAM all the local variables in frame FRAME,
725 including all the blocks active in that frame
726 at its current pc.
727
728 Returns 1 if the job was done,
729 or 0 if nothing was printed because we have no info
730 on the function running in FRAME. */
731
c4668207 732static void
bd5635a1
RP
733print_frame_local_vars (frame, stream)
734 register FRAME frame;
735 register FILE *stream;
736{
737 register struct block *block = get_frame_block (frame);
738 register int values_printed = 0;
739
740 if (block == 0)
741 {
742 fprintf_filtered (stream, "No symbol table info available.\n");
c4668207 743 return;
bd5635a1
RP
744 }
745
746 while (block != 0)
747 {
748 if (print_block_frame_locals (block, frame, stream))
749 values_printed = 1;
750 /* After handling the function's top-level block, stop.
751 Don't continue to its superblock, the block of
752 per-file symbols. */
753 if (BLOCK_FUNCTION (block))
754 break;
755 block = BLOCK_SUPERBLOCK (block);
756 }
757
758 if (!values_printed)
759 {
760 fprintf_filtered (stream, "No locals.\n");
bd5635a1 761 }
bd5635a1
RP
762}
763
764/* Same, but print labels. */
765
c4668207 766static void
bd5635a1
RP
767print_frame_label_vars (frame, this_level_only, stream)
768 register FRAME frame;
769 int this_level_only;
770 register FILE *stream;
771{
bd5635a1
RP
772 register struct blockvector *bl;
773 register struct block *block = get_frame_block (frame);
774 register int values_printed = 0;
775 int index, have_default = 0;
776 char *blocks_printed;
777 struct frame_info *fi = get_frame_info (frame);
778 CORE_ADDR pc = fi->pc;
779
780 if (block == 0)
781 {
782 fprintf_filtered (stream, "No symbol table info available.\n");
c4668207 783 return;
bd5635a1
RP
784 }
785
786 bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
787 blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
3de61d8c 788 memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
bd5635a1
RP
789
790 while (block != 0)
791 {
792 CORE_ADDR end = BLOCK_END (block) - 4;
793 int last_index;
794
795 if (bl != blockvector_for_pc (end, &index))
796 error ("blockvector blotch");
797 if (BLOCKVECTOR_BLOCK (bl, index) != block)
798 error ("blockvector botch");
799 last_index = BLOCKVECTOR_NBLOCKS (bl);
800 index += 1;
801
802 /* Don't print out blocks that have gone by. */
803 while (index < last_index
804 && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
805 index++;
806
807 while (index < last_index
808 && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
809 {
810 if (blocks_printed[index] == 0)
811 {
cadbb07a 812 if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
bd5635a1
RP
813 values_printed = 1;
814 blocks_printed[index] = 1;
815 }
816 index++;
817 }
818 if (have_default)
c4668207 819 return;
bd5635a1 820 if (values_printed && this_level_only)
c4668207 821 return;
bd5635a1
RP
822
823 /* After handling the function's top-level block, stop.
824 Don't continue to its superblock, the block of
825 per-file symbols. */
826 if (BLOCK_FUNCTION (block))
827 break;
828 block = BLOCK_SUPERBLOCK (block);
829 }
830
831 if (!values_printed && !this_level_only)
832 {
833 fprintf_filtered (stream, "No catches.\n");
bd5635a1 834 }
bd5635a1
RP
835}
836
e1ce8aa5 837/* ARGSUSED */
bd5635a1
RP
838static void
839locals_info (args, from_tty)
840 char *args;
841 int from_tty;
842{
cadbb07a
JG
843 if (!selected_frame)
844 error ("No frame selected.");
bd5635a1
RP
845 print_frame_local_vars (selected_frame, stdout);
846}
847
848static void
b4fde6fa
FF
849catch_info (ignore, from_tty)
850 char *ignore;
851 int from_tty;
bd5635a1 852{
cadbb07a
JG
853 if (!selected_frame)
854 error ("No frame selected.");
bd5635a1
RP
855 print_frame_label_vars (selected_frame, 0, stdout);
856}
857
c4668207 858static void
bd5635a1
RP
859print_frame_arg_vars (frame, stream)
860 register FRAME frame;
861 register FILE *stream;
862{
863 struct symbol *func = get_frame_function (frame);
864 register struct block *b;
865 int nsyms;
866 register int i;
867 register struct symbol *sym, *sym2;
868 register int values_printed = 0;
869
870 if (func == 0)
871 {
872 fprintf_filtered (stream, "No symbol table info available.\n");
c4668207 873 return;
bd5635a1
RP
874 }
875
876 b = SYMBOL_BLOCK_VALUE (func);
877 nsyms = BLOCK_NSYMS (b);
878
879 for (i = 0; i < nsyms; i++)
880 {
881 sym = BLOCK_SYM (b, i);
882 if (SYMBOL_CLASS (sym) == LOC_ARG
883 || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG
884 || SYMBOL_CLASS (sym) == LOC_REF_ARG
5afa2040
JK
885 || SYMBOL_CLASS (sym) == LOC_REGPARM
886 || SYMBOL_CLASS (sym) == LOC_REGPARM_ADDR)
bd5635a1
RP
887 {
888 values_printed = 1;
bd5d07d9 889 fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
bd5635a1 890 fputs_filtered (" = ", stream);
31258e4f
JK
891
892 /* We have to look up the symbol because arguments can have
893 two entries (one a parameter, one a local) and the one we
894 want is the local, which lookup_symbol will find for us.
895 This includes gcc1 (not gcc2) on the sparc when passing a
896 small structure and gcc2 when the argument type is float
897 and it is passed as a double and converted to float by
898 the prologue (in the latter case the type of the LOC_ARG
899 symbol is double and the type of the LOC_LOCAL symbol is
6fe90fc8
JK
900 float). There are also LOC_ARG/LOC_REGISTER pairs which
901 are not combined in symbol-reading. */
31258e4f 902
bd5635a1
RP
903 sym2 = lookup_symbol (SYMBOL_NAME (sym),
904 b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
905 print_variable_value (sym2, frame, stream);
906 fprintf_filtered (stream, "\n");
bd5635a1
RP
907 }
908 }
909
910 if (!values_printed)
911 {
912 fprintf_filtered (stream, "No arguments.\n");
bd5635a1 913 }
bd5635a1
RP
914}
915
916static void
b4fde6fa
FF
917args_info (ignore, from_tty)
918 char *ignore;
919 int from_tty;
bd5635a1 920{
cadbb07a
JG
921 if (!selected_frame)
922 error ("No frame selected.");
bd5635a1
RP
923 print_frame_arg_vars (selected_frame, stdout);
924}
925\f
926/* Select frame FRAME, and note that its stack level is LEVEL.
927 LEVEL may be -1 if an actual level number is not known. */
928
929void
930select_frame (frame, level)
931 FRAME frame;
932 int level;
933{
89e0bbcd
JG
934 register struct symtab *s;
935
bd5635a1
RP
936 selected_frame = frame;
937 selected_frame_level = level;
89e0bbcd
JG
938
939 /* Ensure that symbols for this frame are read in. Also, determine the
940 source language of this frame, and switch to it if desired. */
bd5635a1 941 if (frame)
89e0bbcd
JG
942 {
943 s = find_pc_symtab (get_frame_info (frame)->pc);
944 if (s
91ec58ee 945 && s->language != current_language->la_language
89e0bbcd
JG
946 && s->language != language_unknown
947 && language_mode == language_mode_auto) {
948 set_language(s->language);
949 }
950 }
bd5635a1
RP
951}
952
777bef06
JK
953/* Store the selected frame and its level into *FRAMEP and *LEVELP.
954 If there is no selected frame, *FRAMEP is set to NULL. */
bd5635a1
RP
955
956void
957record_selected_frame (frameaddrp, levelp)
958 FRAME_ADDR *frameaddrp;
959 int *levelp;
960{
b4fde6fa 961 *frameaddrp = selected_frame ? FRAME_FP (selected_frame) : 0;
bd5635a1
RP
962 *levelp = selected_frame_level;
963}
964
965/* Return the symbol-block in which the selected frame is executing.
966 Can return zero under various legitimate circumstances. */
967
968struct block *
969get_selected_block ()
970{
971 if (!target_has_stack)
972 return 0;
973
974 if (!selected_frame)
975 return get_current_block ();
976 return get_frame_block (selected_frame);
977}
978
979/* Find a frame a certain number of levels away from FRAME.
980 LEVEL_OFFSET_PTR points to an int containing the number of levels.
981 Positive means go to earlier frames (up); negative, the reverse.
982 The int that contains the number of levels is counted toward
983 zero as the frames for those levels are found.
984 If the top or bottom frame is reached, that frame is returned,
985 but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
986 how much farther the original request asked to go. */
987
988FRAME
989find_relative_frame (frame, level_offset_ptr)
990 register FRAME frame;
991 register int* level_offset_ptr;
992{
993 register FRAME prev;
d8ce1326 994 register FRAME frame1;
bd5635a1
RP
995
996 /* Going up is simple: just do get_prev_frame enough times
997 or until initial frame is reached. */
998 while (*level_offset_ptr > 0)
999 {
1000 prev = get_prev_frame (frame);
1001 if (prev == 0)
1002 break;
1003 (*level_offset_ptr)--;
1004 frame = prev;
1005 }
d8ce1326 1006 /* Going down is just as simple. */
bd5635a1
RP
1007 if (*level_offset_ptr < 0)
1008 {
cadbb07a
JG
1009 while (*level_offset_ptr < 0) {
1010 frame1 = get_next_frame (frame);
1011 if (!frame1)
1012 break;
1013 frame = frame1;
1014 (*level_offset_ptr)++;
1015 }
bd5635a1
RP
1016 }
1017 return frame;
1018}
1019
c4668207 1020/* The "select_frame" command. With no arg, NOP.
bd5635a1
RP
1021 With arg LEVEL_EXP, select the frame at level LEVEL if it is a
1022 valid level. Otherwise, treat level_exp as an address expression
b0077123 1023 and select it. See parse_frame_specification for more info on proper
bd5635a1
RP
1024 frame expressions. */
1025
c4668207 1026/* ARGSUSED */
bd5635a1 1027static void
c4668207 1028select_frame_command (level_exp, from_tty)
bd5635a1
RP
1029 char *level_exp;
1030 int from_tty;
1031{
1032 register FRAME frame, frame1;
1033 unsigned int level = 0;
1034
1035 if (!target_has_stack)
1036 error ("No stack.");
1037
1038 frame = parse_frame_specification (level_exp);
1039
cadbb07a
JG
1040 /* Try to figure out what level this frame is. But if there is
1041 no current stack, don't error out -- let the user set one. */
1042 frame1 = 0;
1043 if (get_current_frame()) {
1044 for (frame1 = get_prev_frame (0);
1045 frame1 && frame1 != frame;
1046 frame1 = get_prev_frame (frame1))
1047 level++;
1048 }
bd5635a1
RP
1049
1050 if (!frame1)
1051 level = 0;
1052
1053 select_frame (frame, level);
b0077123 1054}
bd5635a1 1055
b0077123 1056/* The "frame" command. With no arg, print selected frame briefly.
c4668207 1057 With arg, behaves like select_frame and then prints the selected
b0077123 1058 frame. */
bd5635a1 1059
b0077123
JG
1060static void
1061frame_command (level_exp, from_tty)
1062 char *level_exp;
1063 int from_tty;
1064{
c4668207 1065 select_frame_command (level_exp, from_tty);
bd5635a1
RP
1066 print_stack_frame (selected_frame, selected_frame_level, 1);
1067}
1068
1069/* Select the frame up one or COUNT stack levels
1070 from the previously selected frame, and print it briefly. */
1071
e1ce8aa5 1072/* ARGSUSED */
bd5635a1
RP
1073static void
1074up_silently_command (count_exp, from_tty)
1075 char *count_exp;
1076 int from_tty;
1077{
1078 register FRAME frame;
1079 int count = 1, count1;
1080 if (count_exp)
1081 count = parse_and_eval_address (count_exp);
1082 count1 = count;
1083
0f552c5f 1084 if (target_has_stack == 0 || selected_frame == 0)
bd5635a1
RP
1085 error ("No stack.");
1086
1087 frame = find_relative_frame (selected_frame, &count1);
1088 if (count1 != 0 && count_exp == 0)
1089 error ("Initial frame selected; you cannot go up.");
1090 select_frame (frame, selected_frame_level + count - count1);
1091}
1092
1093static void
1094up_command (count_exp, from_tty)
1095 char *count_exp;
1096 int from_tty;
1097{
1098 up_silently_command (count_exp, from_tty);
1099 print_stack_frame (selected_frame, selected_frame_level, 1);
1100}
1101
1102/* Select the frame down one or COUNT stack levels
1103 from the previously selected frame, and print it briefly. */
1104
e1ce8aa5 1105/* ARGSUSED */
bd5635a1
RP
1106static void
1107down_silently_command (count_exp, from_tty)
1108 char *count_exp;
1109 int from_tty;
1110{
1111 register FRAME frame;
1112 int count = -1, count1;
1113 if (count_exp)
1114 count = - parse_and_eval_address (count_exp);
1115 count1 = count;
1116
0f552c5f 1117 if (target_has_stack == 0 || selected_frame == 0)
89e0bbcd
JG
1118 error ("No stack.");
1119
bd5635a1
RP
1120 frame = find_relative_frame (selected_frame, &count1);
1121 if (count1 != 0 && count_exp == 0)
1122 error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
1123 select_frame (frame, selected_frame_level + count - count1);
1124}
1125
1126
1127static void
1128down_command (count_exp, from_tty)
1129 char *count_exp;
1130 int from_tty;
1131{
1132 down_silently_command (count_exp, from_tty);
1133 print_stack_frame (selected_frame, selected_frame_level, 1);
1134}
1135\f
1136static void
1137return_command (retval_exp, from_tty)
1138 char *retval_exp;
1139 int from_tty;
1140{
777bef06
JK
1141 struct symbol *thisfun;
1142 FRAME_ADDR selected_frame_addr;
1143 CORE_ADDR selected_frame_pc;
bd5635a1 1144 FRAME frame;
0f552c5f 1145 value return_value;
bd5635a1 1146
777bef06
JK
1147 if (selected_frame == NULL)
1148 error ("No selected frame.");
1149 thisfun = get_frame_function (selected_frame);
1150 selected_frame_addr = FRAME_FP (selected_frame);
1151 selected_frame_pc = (get_frame_info (selected_frame))->pc;
1152
0f552c5f
JG
1153 /* Compute the return value (if any -- possibly getting errors here).
1154 Call VALUE_CONTENTS to make sure we have fully evaluated it, since
1155 it might live in the stack frame we're about to pop. */
1156
1157 if (retval_exp)
1158 {
1159 return_value = parse_and_eval (retval_exp);
3de61d8c 1160 VALUE_CONTENTS (return_value);
0f552c5f
JG
1161 }
1162
bd5635a1
RP
1163 /* If interactive, require confirmation. */
1164
1165 if (from_tty)
1166 {
1167 if (thisfun != 0)
1168 {
bd5d07d9 1169 if (!query ("Make %s return now? ", SYMBOL_SOURCE_NAME (thisfun)))
b4fde6fa
FF
1170 {
1171 error ("Not confirmed.");
1172 /* NOTREACHED */
1173 }
bd5635a1
RP
1174 }
1175 else
1176 if (!query ("Make selected stack frame return now? "))
1177 error ("Not confirmed.");
1178 }
1179
1180 /* Do the real work. Pop until the specified frame is current. We
1181 use this method because the selected_frame is not valid after
1182 a POP_FRAME. The pc comparison makes this work even if the
1183 selected frame shares its fp with another frame. */
1184
1185 while ( selected_frame_addr != FRAME_FP (frame = get_current_frame())
1186 || selected_frame_pc != (get_frame_info (frame))->pc )
1187 POP_FRAME;
1188
1189 /* Then pop that frame. */
1190
1191 POP_FRAME;
1192
1193 /* Compute the return value (if any) and store in the place
1194 for return values. */
1195
1196 if (retval_exp)
0f552c5f 1197 set_return_value (return_value);
bd5635a1
RP
1198
1199 /* If interactive, print the frame that is now current. */
1200
1201 if (from_tty)
1202 frame_command ("0", 1);
1203}
89e0bbcd
JG
1204
1205/* Gets the language of the current frame. */
1206enum language
1207get_frame_language()
1208{
1209 register struct symtab *s;
1210 FRAME fr;
1211 enum language flang; /* The language of the current frame */
1212
1213 fr = get_frame_info(selected_frame);
1214 if(fr)
1215 {
1216 s = find_pc_symtab(fr->pc);
1217 if(s)
1218 flang = s->language;
1219 else
1220 flang = language_unknown;
1221 }
1222 else
1223 flang = language_unknown;
1224
1225 return flang;
1226}
bd5635a1
RP
1227\f
1228void
1229_initialize_stack ()
1230{
1231#if 0
1232 backtrace_limit = 30;
1233#endif
1234
1235 add_com ("return", class_stack, return_command,
1236 "Make selected stack frame return to its caller.\n\
1237Control remains in the debugger, but when you continue\n\
1238execution will resume in the frame above the one now selected.\n\
1239If an argument is given, it is an expression for the value to return.");
1240
1241 add_com ("up", class_stack, up_command,
1242 "Select and print stack frame that called this one.\n\
1243An argument says how many frames up to go.");
1244 add_com ("up-silently", class_support, up_silently_command,
1245 "Same as the `up' command, but does not print anything.\n\
1246This is useful in command scripts.");
1247
1248 add_com ("down", class_stack, down_command,
1249 "Select and print stack frame called by this one.\n\
1250An argument says how many frames down to go.");
1251 add_com_alias ("do", "down", class_stack, 1);
3de61d8c 1252 add_com_alias ("dow", "down", class_stack, 1);
bd5635a1
RP
1253 add_com ("down-silently", class_support, down_silently_command,
1254 "Same as the `down' command, but does not print anything.\n\
1255This is useful in command scripts.");
1256
1257 add_com ("frame", class_stack, frame_command,
1258 "Select and print a stack frame.\n\
1259With no argument, print the selected stack frame. (See also \"info frame\").\n\
1260An argument specifies the frame to select.\n\
1261It can be a stack frame number or the address of the frame.\n\
1262With argument, nothing is printed if input is coming from\n\
1263a command file or a user-defined command.");
1264
1265 add_com_alias ("f", "frame", class_stack, 1);
1266
c4668207 1267 add_com ("select-frame", class_stack, select_frame_command,
b0077123
JG
1268 "Select a stack frame without printing anything.\n\
1269An argument specifies the frame to select.\n\
1270It can be a stack frame number or the address of the frame.\n");
1271
bd5635a1
RP
1272 add_com ("backtrace", class_stack, backtrace_command,
1273 "Print backtrace of all stack frames, or innermost COUNT frames.\n\
1274With a negative argument, print outermost -COUNT frames.");
1275 add_com_alias ("bt", "backtrace", class_stack, 0);
1276 add_com_alias ("where", "backtrace", class_alias, 0);
1277 add_info ("stack", backtrace_command,
1278 "Backtrace of the stack, or innermost COUNT frames.");
1279 add_info_alias ("s", "stack", 1);
1280 add_info ("frame", frame_info,
1281 "All about selected stack frame, or frame at ADDR.");
1282 add_info_alias ("f", "frame", 1);
1283 add_info ("locals", locals_info,
1284 "Local variables of current stack frame.");
1285 add_info ("args", args_info,
1286 "Argument variables of current stack frame.");
1287 add_info ("catch", catch_info,
1288 "Exceptions that can be caught in the current stack frame.");
1289
1290#if 0
1291 add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command,
1292 "Specify maximum number of frames for \"backtrace\" to print by default.",
1293 &setlist);
1294 add_info ("backtrace-limit", backtrace_limit_info,
1295 "The maximum number of frames for \"backtrace\" to print by default.");
1296#endif
1297}
This page took 0.15601 seconds and 4 git commands to generate.