]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/stack.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / gdb / stack.c
1 /* Print and select stack frames for GDB, the GNU debugger.
2 Copyright 1986, 87, 89, 91, 92, 93, 94, 95, 96, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include <ctype.h>
22 #include "defs.h"
23 #include "gdb_string.h"
24 #include "value.h"
25 #include "symtab.h"
26 #include "gdbtypes.h"
27 #include "expression.h"
28 #include "language.h"
29 #include "frame.h"
30 #include "gdbcmd.h"
31 #include "gdbcore.h"
32 #include "target.h"
33 #include "breakpoint.h"
34 #include "demangle.h"
35 #include "inferior.h"
36 #include "annotate.h"
37 #include "symfile.h"
38 #include "objfiles.h"
39
40 /* Prototypes for exported functions. */
41
42 void args_info PARAMS ((char *, int));
43
44 void locals_info PARAMS ((char *, int));
45
46 void (*selected_frame_level_changed_hook) PARAMS ((int));
47
48 void _initialize_stack PARAMS ((void));
49
50 /* Prototypes for local functions. */
51
52 static void return_command PARAMS ((char *, int));
53
54 static void down_command PARAMS ((char *, int));
55
56 static void down_silently_base PARAMS ((char *));
57
58 static void down_silently_command PARAMS ((char *, int));
59
60 static void up_command PARAMS ((char *, int));
61
62 static void up_silently_base PARAMS ((char *));
63
64 static void up_silently_command PARAMS ((char *, int));
65
66 void frame_command PARAMS ((char *, int));
67
68 static void select_frame_command PARAMS ((char *, int));
69
70 static void print_frame_arg_vars PARAMS ((struct frame_info *, GDB_FILE *));
71
72 static void catch_info PARAMS ((char *, int));
73
74 static void args_plus_locals_info PARAMS ((char *, int));
75
76 static void print_frame_label_vars PARAMS ((struct frame_info *,
77 int,
78 GDB_FILE *));
79
80 static void print_frame_local_vars PARAMS ((struct frame_info *,
81 int,
82 GDB_FILE *));
83
84 static int print_block_frame_labels PARAMS ((struct block *, int *,
85 GDB_FILE *));
86
87 static int print_block_frame_locals PARAMS ((struct block *,
88 struct frame_info *,
89 int,
90 GDB_FILE *));
91
92 static void print_frame_info_base PARAMS ((struct frame_info *, int, int, int));
93
94 static void print_stack_frame_base PARAMS ((struct frame_info *, int, int));
95
96 static void backtrace_command PARAMS ((char *, int));
97
98 struct frame_info *parse_frame_specification PARAMS ((char *));
99
100 static void frame_info PARAMS ((char *, int));
101
102 extern int addressprint; /* Print addresses, or stay symbolic only? */
103 extern int info_verbose; /* Verbosity of symbol reading msgs */
104 extern int lines_to_list; /* # of lines "list" command shows by default */
105
106 /* The "selected" stack frame is used by default for local and arg access.
107 May be zero, for no selected frame. */
108
109 struct frame_info *selected_frame;
110
111 /* Level of the selected frame:
112 0 for innermost, 1 for its caller, ...
113 or -1 for frame specified by address with no defined level. */
114
115 int selected_frame_level;
116
117 /* Zero means do things normally; we are interacting directly with the
118 user. One means print the full filename and linenumber when a
119 frame is printed, and do so in a format emacs18/emacs19.22 can
120 parse. Two means print similar annotations, but in many more
121 cases and in a slightly different syntax. */
122
123 int annotation_level = 0;
124
125 \f
126 struct print_stack_frame_args {
127 struct frame_info *fi;
128 int level;
129 int source;
130 int args;
131 };
132
133 static int print_stack_frame_base_stub PARAMS ((char *));
134
135 /* Show and print the frame arguments.
136 Pass the args the way catch_errors wants them. */
137 static int
138 show_and_print_stack_frame_stub (args)
139 char *args;
140 {
141 struct print_stack_frame_args *p = (struct print_stack_frame_args *)args;
142
143 /* Reversed order of these so tuiDo() doesn't occur
144 * in the middle of "Breakpoint 1 ... [location]" printing = RT
145 */
146 if (tui_version)
147 print_frame_info_base (p->fi, p->level, p->source, p->args);
148 print_frame_info (p->fi, p->level, p->source, p->args);
149
150 return 0;
151 }
152
153 /* Show or print the frame arguments.
154 Pass the args the way catch_errors wants them. */
155 static int
156 print_stack_frame_stub (args)
157 char *args;
158 {
159 struct print_stack_frame_args *p = (struct print_stack_frame_args *)args;
160
161 if (tui_version)
162 print_frame_info (p->fi, p->level, p->source, p->args);
163 else
164 print_frame_info_base (p->fi, p->level, p->source, p->args);
165 return 0;
166 }
167
168 /* Print a stack frame briefly. FRAME_INFI should be the frame info
169 and LEVEL should be its level in the stack (or -1 for level not defined).
170
171 /* Pass the args the way catch_errors wants them. */
172 static int
173 print_stack_frame_base_stub (args)
174 char *args;
175 {
176 struct print_stack_frame_args *p = (struct print_stack_frame_args *)args;
177
178 print_frame_info_base (p->fi, p->level, p->source, p->args);
179 return 0;
180 }
181
182 /* print the frame arguments to the terminal.
183 Pass the args the way catch_errors wants them. */
184 static int
185 print_only_stack_frame_stub (args)
186 char *args;
187 {
188 struct print_stack_frame_args *p = (struct print_stack_frame_args *)args;
189
190 print_frame_info_base (p->fi, p->level, p->source, p->args);
191 return 0;
192 }
193
194 /* Print a stack frame briefly. FRAME_INFI should be the frame info
195 and LEVEL should be its level in the stack (or -1 for level not defined).
196 This prints the level, the function executing, the arguments,
197 and the file name and line number.
198 If the pc is not at the beginning of the source line,
199 the actual pc is printed at the beginning.
200
201 If SOURCE is 1, print the source line as well.
202 If SOURCE is -1, print ONLY the source line. */
203
204 static void
205 print_stack_frame_base (fi, level, source)
206 struct frame_info *fi;
207 int level;
208 int source;
209 {
210 struct print_stack_frame_args args;
211
212 args.fi = fi;
213 args.level = level;
214 args.source = source;
215 args.args = 1;
216
217 catch_errors (print_stack_frame_stub, (char *)&args, "", RETURN_MASK_ALL);
218 }
219
220 /* Show and print a stack frame briefly. FRAME_INFI should be the frame info
221 and LEVEL should be its level in the stack (or -1 for level not defined).
222 This prints the level, the function executing, the arguments,
223 and the file name and line number.
224 If the pc is not at the beginning of the source line,
225 the actual pc is printed at the beginning.
226
227 If SOURCE is 1, print the source line as well.
228 If SOURCE is -1, print ONLY the source line. */
229
230 void
231 show_and_print_stack_frame (fi, level, source)
232 struct frame_info *fi;
233 int level;
234 int source;
235 {
236 struct print_stack_frame_args args;
237
238 args.fi = fi;
239 args.level = level;
240 args.source = source;
241 args.args = 1;
242
243 catch_errors (show_and_print_stack_frame_stub, (char *)&args, "", RETURN_MASK_ALL);
244 }
245
246
247 /* Show or print a stack frame briefly. FRAME_INFI should be the frame info
248 and LEVEL should be its level in the stack (or -1 for level not defined).
249 This prints the level, the function executing, the arguments,
250 and the file name and line number.
251 If the pc is not at the beginning of the source line,
252 the actual pc is printed at the beginning.
253
254 If SOURCE is 1, print the source line as well.
255 If SOURCE is -1, print ONLY the source line. */
256
257 void
258 print_stack_frame (fi, level, source)
259 struct frame_info *fi;
260 int level;
261 int source;
262 {
263 struct print_stack_frame_args args;
264
265 args.fi = fi;
266 args.level = level;
267 args.source = source;
268 args.args = 1;
269
270 catch_errors (print_stack_frame_stub, (char *)&args, "", RETURN_MASK_ALL);
271 }
272
273 /* Print a stack frame briefly. FRAME_INFI should be the frame info
274 and LEVEL should be its level in the stack (or -1 for level not defined).
275 This prints the level, the function executing, the arguments,
276 and the file name and line number.
277 If the pc is not at the beginning of the source line,
278 the actual pc is printed at the beginning.
279
280 If SOURCE is 1, print the source line as well.
281 If SOURCE is -1, print ONLY the source line. */
282
283 void
284 print_only_stack_frame (fi, level, source)
285 struct frame_info *fi;
286 int level;
287 int source;
288 {
289 struct print_stack_frame_args args;
290
291 args.fi = fi;
292 args.level = level;
293 args.source = source;
294 args.args = 1;
295
296 catch_errors (print_only_stack_frame_stub,
297 (char *)&args, "", RETURN_MASK_ALL);
298 }
299
300 struct print_args_args {
301 struct symbol *func;
302 struct frame_info *fi;
303 };
304
305 static int print_args_stub PARAMS ((char *));
306
307 /* Pass the args the way catch_errors wants them. */
308
309 static int
310 print_args_stub (args)
311 char *args;
312 {
313 int numargs;
314 struct print_args_args *p = (struct print_args_args *)args;
315
316 FRAME_NUM_ARGS (numargs, (p->fi));
317 print_frame_args (p->func, p->fi, numargs, gdb_stdout);
318 return 0;
319 }
320
321 /* Print information about a frame for frame "fi" at level "level".
322 * Used in "where" output, also used to emit breakpoint or step messages.
323 * LEVEL is the level of the frame, or -1 if it is the innermost frame
324 * but we don't want to print the level.
325 * The meaning of the SOURCE argument is:
326 * -1: Print only source line
327 * 0: Print only location
328 * 1: Print location and source line
329 */
330
331 static void
332 print_frame_info_base (fi, level, source, args)
333 struct frame_info *fi;
334 int level;
335 int source;
336 int args;
337 {
338 struct symtab_and_line sal;
339 struct symbol *func;
340 register char *funname = 0;
341 enum language funlang = language_unknown;
342
343 #if 0
344 char buf[MAX_REGISTER_RAW_SIZE];
345 CORE_ADDR sp;
346
347 /* On the 68k, this spends too much time in m68k_find_saved_regs. */
348
349 /* Get the value of SP_REGNUM relative to the frame. */
350 get_saved_register (buf, (int *)NULL, (CORE_ADDR *)NULL,
351 FRAME_INFO_ID (fi), SP_REGNUM, (enum lval_type *)NULL);
352 sp = extract_address (buf, REGISTER_RAW_SIZE (SP_REGNUM));
353
354 /* This is not a perfect test, because if a function alloca's some
355 memory, puts some code there, and then jumps into it, then the test
356 will succeed even though there is no call dummy. Probably best is
357 to check for a bp_call_dummy breakpoint. */
358 if (PC_IN_CALL_DUMMY (fi->pc, sp, fi->frame))
359 #else
360 if (frame_in_dummy (fi))
361 #endif
362 {
363 annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
364
365 /* Do this regardless of SOURCE because we don't have any source
366 to list for this frame. */
367 if (level >= 0)
368 printf_filtered ("#%-2d ", level);
369 annotate_function_call ();
370 printf_filtered ("<function called from gdb>\n");
371 annotate_frame_end ();
372 return;
373 }
374 if (fi->signal_handler_caller)
375 {
376 annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
377
378 /* Do this regardless of SOURCE because we don't have any source
379 to list for this frame. */
380 if (level >= 0)
381 printf_filtered ("#%-2d ", level);
382 annotate_signal_handler_caller ();
383 printf_filtered ("<signal handler called>\n");
384 annotate_frame_end ();
385 return;
386 }
387
388 /* If fi is not the innermost frame, that normally means that fi->pc
389 points to *after* the call instruction, and we want to get the line
390 containing the call, never the next line. But if the next frame is
391 a signal_handler_caller or a dummy frame, then the next frame was
392 not entered as the result of a call, and we want to get the line
393 containing fi->pc. */
394 sal =
395 find_pc_line (fi->pc,
396 fi->next != NULL
397 && !fi->next->signal_handler_caller
398 && !frame_in_dummy (fi->next));
399
400 func = find_pc_function (fi->pc);
401 if (func)
402 {
403 /* In certain pathological cases, the symtabs give the wrong
404 function (when we are in the first function in a file which
405 is compiled without debugging symbols, the previous function
406 is compiled with debugging symbols, and the "foo.o" symbol
407 that is supposed to tell us where the file with debugging symbols
408 ends has been truncated by ar because it is longer than 15
409 characters). This also occurs if the user uses asm() to create
410 a function but not stabs for it (in a file compiled -g).
411
412 So look in the minimal symbol tables as well, and if it comes
413 up with a larger address for the function use that instead.
414 I don't think this can ever cause any problems; there shouldn't
415 be any minimal symbols in the middle of a function; if this is
416 ever changed many parts of GDB will need to be changed (and we'll
417 create a find_pc_minimal_function or some such). */
418
419 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
420 if (msymbol != NULL
421 && (SYMBOL_VALUE_ADDRESS (msymbol)
422 > BLOCK_START (SYMBOL_BLOCK_VALUE (func))))
423 {
424 #if 0
425 /* There is no particular reason to think the line number
426 information is wrong. Someone might have just put in
427 a label with asm() but left the line numbers alone. */
428 /* In this case we have no way of knowing the source file
429 and line number, so don't print them. */
430 sal.symtab = 0;
431 #endif
432 /* We also don't know anything about the function besides
433 its address and name. */
434 func = 0;
435 funname = SYMBOL_NAME (msymbol);
436 funlang = SYMBOL_LANGUAGE (msymbol);
437 }
438 else
439 {
440 /* I'd like to use SYMBOL_SOURCE_NAME() here, to display
441 * the demangled name that we already have stored in
442 * the symbol table, but we stored a version with
443 * DMGL_PARAMS turned on, and here we don't want
444 * to display parameters. So call the demangler again,
445 * with DMGL_ANSI only. RT
446 * (Yes, I know that printf_symbol_filtered() will
447 * again try to demangle the name on the fly, but
448 * the issue is that if cplus_demangle() fails here,
449 * it'll fail there too. So we want to catch the failure
450 * ("demangled==NULL" case below) here, while we still
451 * have our hands on the function symbol.)
452 */
453 char * demangled;
454 funname = SYMBOL_NAME (func);
455 funlang = SYMBOL_LANGUAGE (func);
456 if (funlang == language_cplus) {
457 demangled = cplus_demangle (funname, DMGL_ANSI);
458 if (demangled == NULL)
459 /* If the demangler fails, try the demangled name
460 * from the symbol table. This'll have parameters,
461 * but that's preferable to diplaying a mangled name.
462 */
463 funname = SYMBOL_SOURCE_NAME (func);
464 }
465 }
466 }
467 else
468 {
469 struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
470 if (msymbol != NULL)
471 {
472 funname = SYMBOL_NAME (msymbol);
473 funlang = SYMBOL_LANGUAGE (msymbol);
474 }
475 }
476
477 if (source >= 0 || !sal.symtab)
478 {
479 annotate_frame_begin (level == -1 ? 0 : level, fi->pc);
480
481 if (level >= 0)
482 printf_filtered ("#%-2d ", level);
483 if (addressprint)
484 if (fi->pc != sal.pc || !sal.symtab)
485 {
486 annotate_frame_address ();
487 print_address_numeric (fi->pc, 1, gdb_stdout);
488 annotate_frame_address_end ();
489 printf_filtered (" in ");
490 }
491 annotate_frame_function_name ();
492 fprintf_symbol_filtered (gdb_stdout, funname ? funname : "??", funlang,
493 DMGL_ANSI);
494 wrap_here (" ");
495 annotate_frame_args ();
496 fputs_filtered (" (", gdb_stdout);
497 if (args)
498 {
499 struct print_args_args args;
500 args.fi = fi;
501 args.func = func;
502 catch_errors (print_args_stub, (char *)&args, "", RETURN_MASK_ALL);
503 QUIT;
504 }
505 printf_filtered (")");
506 if (sal.symtab && sal.symtab->filename)
507 {
508 annotate_frame_source_begin ();
509 wrap_here (" ");
510 printf_filtered (" at ");
511 annotate_frame_source_file ();
512 printf_filtered ("%s", sal.symtab->filename);
513 annotate_frame_source_file_end ();
514 printf_filtered (":");
515 annotate_frame_source_line ();
516 printf_filtered ("%d", sal.line);
517 annotate_frame_source_end ();
518 }
519
520 #ifdef PC_LOAD_SEGMENT
521 /* If we couldn't print out function name but if can figure out what
522 load segment this pc value is from, at least print out some info
523 about its load segment. */
524 if (!funname)
525 {
526 annotate_frame_where ();
527 wrap_here (" ");
528 printf_filtered (" from %s", PC_LOAD_SEGMENT (fi->pc));
529 }
530 #endif
531 #ifdef PC_SOLIB
532 if (!funname || (!sal.symtab || !sal.symtab->filename))
533 {
534 char *lib = PC_SOLIB (fi->pc);
535 if (lib)
536 {
537 annotate_frame_where ();
538 wrap_here (" ");
539 printf_filtered (" from %s", lib);
540 }
541 }
542 #endif
543 printf_filtered ("\n");
544 }
545
546 if ((source != 0) && sal.symtab)
547 {
548 int done = 0;
549 int mid_statement = source < 0 && fi->pc != sal.pc;
550 if (annotation_level)
551 done = identify_source_line (sal.symtab, sal.line, mid_statement,
552 fi->pc);
553 if (!done)
554 {
555 if (addressprint && mid_statement && !tui_version)
556 {
557 print_address_numeric (fi->pc, 1, gdb_stdout);
558 printf_filtered ("\t");
559 }
560 if (print_frame_info_listing_hook)
561 print_frame_info_listing_hook (sal.symtab, sal.line, sal.line + 1, 0);
562 else if (!tui_version)
563 print_source_lines (sal.symtab, sal.line, sal.line + 1, 0);
564 }
565 current_source_line = max (sal.line - lines_to_list/2, 1);
566 }
567 if (source != 0)
568 set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
569
570 annotate_frame_end ();
571
572 gdb_flush (gdb_stdout);
573 }
574 \f
575
576 void
577 stack_publish_stopped_with_no_frame()
578 {
579 TUIDO(((TuiOpaqueFuncPtr)tuiUpdateOnEnd));
580
581 return;
582 }
583
584 /* Show or print the frame info. If this is the tui, it will be shown in
585 the source display */
586 void
587 print_frame_info(fi, level, source, args)
588 struct frame_info *fi;
589 register int level;
590 int source;
591 int args;
592 {
593 if (!tui_version)
594 print_frame_info_base(fi, level, source, args);
595 else
596 {
597 if (fi && (frame_in_dummy(fi) || fi->signal_handler_caller))
598 print_frame_info_base(fi, level, source, args);
599 else
600 {
601 TUIDO(((TuiOpaqueFuncPtr)tui_vShowFrameInfo, fi));
602 }
603 }
604 }
605
606 /* Show the frame info. If this is the tui, it will be shown in
607 the source display otherwise, nothing is done */
608 void
609 show_stack_frame(fi)
610 struct frame_info *fi;
611 {
612 TUIDO(((TuiOpaqueFuncPtr)tui_vShowFrameInfo, fi));
613 }
614
615 \f
616 /* Read a frame specification in whatever the appropriate format is.
617 Call error() if the specification is in any way invalid (i.e.
618 this function never returns NULL). */
619
620 struct frame_info *
621 parse_frame_specification (frame_exp)
622 char *frame_exp;
623 {
624 int numargs = 0;
625 #define MAXARGS 4
626 CORE_ADDR args[MAXARGS];
627
628 if (frame_exp)
629 {
630 char *addr_string, *p;
631 struct cleanup *tmp_cleanup;
632
633 while (*frame_exp == ' ') frame_exp++;
634
635 while (*frame_exp)
636 {
637 if (numargs > MAXARGS)
638 error ("Too many args in frame specification");
639 /* Parse an argument. */
640 for (p = frame_exp; *p && *p != ' '; p++)
641 ;
642 addr_string = savestring(frame_exp, p - frame_exp);
643
644 {
645 tmp_cleanup = make_cleanup (free, addr_string);
646 args[numargs++] = parse_and_eval_address (addr_string);
647 do_cleanups (tmp_cleanup);
648 }
649
650 /* Skip spaces, move to possible next arg. */
651 while (*p == ' ') p++;
652 frame_exp = p;
653 }
654 }
655
656 switch (numargs)
657 {
658 case 0:
659 if (selected_frame == NULL)
660 error ("No selected frame.");
661 return selected_frame;
662 /* NOTREACHED */
663 case 1:
664 {
665 int level = args[0];
666 struct frame_info *fid =
667 find_relative_frame (get_current_frame (), &level);
668 struct frame_info *tfid;
669
670 if (level == 0)
671 /* find_relative_frame was successful */
672 return fid;
673
674 /* If SETUP_ARBITRARY_FRAME is defined, then frame specifications
675 take at least 2 addresses. It is important to detect this case
676 here so that "frame 100" does not give a confusing error message
677 like "frame specification requires two addresses". This of course
678 does not solve the "frame 100" problem for machines on which
679 a frame specification can be made with one address. To solve
680 that, we need a new syntax for a specifying a frame by address.
681 I think the cleanest syntax is $frame(0x45) ($frame(0x23,0x45) for
682 two args, etc.), but people might think that is too much typing,
683 so I guess *0x23,0x45 would be a possible alternative (commas
684 really should be used instead of spaces to delimit; using spaces
685 normally works in an expression). */
686 #ifdef SETUP_ARBITRARY_FRAME
687 error ("No frame %d", args[0]);
688 #endif
689
690 /* If (s)he specifies the frame with an address, he deserves what
691 (s)he gets. Still, give the highest one that matches. */
692
693 for (fid = get_current_frame ();
694 fid && fid->frame != args[0];
695 fid = get_prev_frame (fid))
696 ;
697
698 if (fid)
699 while ((tfid = get_prev_frame (fid)) &&
700 (tfid->frame == args[0]))
701 fid = tfid;
702
703 /* We couldn't identify the frame as an existing frame, but
704 perhaps we can create one with a single argument. */
705 }
706
707 default:
708 #ifdef SETUP_ARBITRARY_FRAME
709 return SETUP_ARBITRARY_FRAME (numargs, args);
710 #else
711 /* Usual case. Do it here rather than have everyone supply
712 a SETUP_ARBITRARY_FRAME that does this. */
713 if (numargs == 1)
714 return create_new_frame (args[0], 0);
715 error ("Too many args in frame specification");
716 #endif
717 /* NOTREACHED */
718 }
719 /* NOTREACHED */
720 }
721
722 /* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
723 that if it is unsure about the answer, it returns 0
724 instead of guessing (this happens on the VAX and i960, for example).
725
726 On most machines, we never have to guess about the args address,
727 so FRAME_ARGS_ADDRESS{,_CORRECT} are the same. */
728 #if !defined (FRAME_ARGS_ADDRESS_CORRECT)
729 #define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
730 #endif
731
732 /* Print verbosely the selected frame or the frame at address ADDR.
733 This means absolutely all information in the frame is printed. */
734
735 static void
736 frame_info (addr_exp, from_tty)
737 char *addr_exp;
738 int from_tty;
739 {
740 struct frame_info *fi;
741 struct symtab_and_line sal;
742 struct symbol *func;
743 struct symtab *s;
744 struct frame_info *calling_frame_info;
745 int i, count, numregs;
746 char *funname = 0;
747 enum language funlang = language_unknown;
748
749 if (!target_has_stack)
750 error ("No stack.");
751
752 fi = parse_frame_specification (addr_exp);
753 if (fi == NULL)
754 error ("Invalid frame specified.");
755
756 sal = find_pc_line (fi->pc,
757 fi->next != NULL
758 && !fi->next->signal_handler_caller
759 && !frame_in_dummy (fi->next));
760 func = get_frame_function (fi);
761 s = find_pc_symtab(fi->pc);
762 if (func)
763 {
764 /* I'd like to use SYMBOL_SOURCE_NAME() here, to display
765 * the demangled name that we already have stored in
766 * the symbol table, but we stored a version with
767 * DMGL_PARAMS turned on, and here we don't want
768 * to display parameters. So call the demangler again,
769 * with DMGL_ANSI only. RT
770 * (Yes, I know that printf_symbol_filtered() will
771 * again try to demangle the name on the fly, but
772 * the issue is that if cplus_demangle() fails here,
773 * it'll fail there too. So we want to catch the failure
774 * ("demangled==NULL" case below) here, while we still
775 * have our hands on the function symbol.)
776 */
777 char * demangled;
778 funname = SYMBOL_NAME (func);
779 funlang = SYMBOL_LANGUAGE (func);
780 if (funlang == language_cplus)
781 {
782 demangled = cplus_demangle (funname, DMGL_ANSI);
783 /* If the demangler fails, try the demangled name
784 * from the symbol table. This'll have parameters,
785 * but that's preferable to diplaying a mangled name.
786 */
787 if (demangled == NULL)
788 funname = SYMBOL_SOURCE_NAME (func);
789 }
790 }
791 else
792 {
793 register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (fi->pc);
794 if (msymbol != NULL)
795 {
796 funname = SYMBOL_NAME (msymbol);
797 funlang = SYMBOL_LANGUAGE (msymbol);
798 }
799 }
800 calling_frame_info = get_prev_frame (fi);
801
802 if (!addr_exp && selected_frame_level >= 0)
803 {
804 printf_filtered ("Stack level %d, frame at ", selected_frame_level);
805 print_address_numeric (fi->frame, 1, gdb_stdout);
806 printf_filtered (":\n");
807 }
808 else
809 {
810 printf_filtered ("Stack frame at ");
811 print_address_numeric (fi->frame, 1, gdb_stdout);
812 printf_filtered (":\n");
813 }
814 printf_filtered (" %s = ", REGISTER_NAME (PC_REGNUM));
815 print_address_numeric (fi->pc, 1, gdb_stdout);
816
817 wrap_here (" ");
818 if (funname)
819 {
820 printf_filtered (" in ");
821 fprintf_symbol_filtered (gdb_stdout, funname, funlang,
822 DMGL_ANSI | DMGL_PARAMS);
823 }
824 wrap_here (" ");
825 if (sal.symtab)
826 printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
827 puts_filtered ("; ");
828 wrap_here (" ");
829 printf_filtered ("saved %s ", REGISTER_NAME (PC_REGNUM));
830 print_address_numeric (FRAME_SAVED_PC (fi), 1, gdb_stdout);
831 printf_filtered ("\n");
832
833 {
834 int frameless = 0;
835 #ifdef FRAMELESS_FUNCTION_INVOCATION
836 FRAMELESS_FUNCTION_INVOCATION (fi, frameless);
837 #endif
838 if (frameless)
839 printf_filtered (" (FRAMELESS),");
840 }
841
842 if (calling_frame_info)
843 {
844 printf_filtered (" called by frame at ");
845 print_address_numeric (calling_frame_info->frame, 1, gdb_stdout);
846 }
847 if (fi->next && calling_frame_info)
848 puts_filtered (",");
849 wrap_here (" ");
850 if (fi->next)
851 {
852 printf_filtered (" caller of frame at ");
853 print_address_numeric (fi->next->frame, 1, gdb_stdout);
854 }
855 if (fi->next || calling_frame_info)
856 puts_filtered ("\n");
857 if (s)
858 printf_filtered (" source language %s.\n", language_str (s->language));
859
860 #ifdef PRINT_EXTRA_FRAME_INFO
861 PRINT_EXTRA_FRAME_INFO (fi);
862 #endif
863
864 {
865 /* Address of the argument list for this frame, or 0. */
866 CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
867 /* Number of args for this frame, or -1 if unknown. */
868 int numargs;
869
870 if (arg_list == 0)
871 printf_filtered (" Arglist at unknown address.\n");
872 else
873 {
874 printf_filtered (" Arglist at ");
875 print_address_numeric (arg_list, 1, gdb_stdout);
876 printf_filtered (",");
877
878 FRAME_NUM_ARGS (numargs, fi);
879 if (numargs < 0)
880 puts_filtered (" args: ");
881 else if (numargs == 0)
882 puts_filtered (" no args.");
883 else if (numargs == 1)
884 puts_filtered (" 1 arg: ");
885 else
886 printf_filtered (" %d args: ", numargs);
887 print_frame_args (func, fi, numargs, gdb_stdout);
888 puts_filtered ("\n");
889 }
890 }
891 {
892 /* Address of the local variables for this frame, or 0. */
893 CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
894
895 if (arg_list == 0)
896 printf_filtered (" Locals at unknown address,");
897 else
898 {
899 printf_filtered (" Locals at ");
900 print_address_numeric (arg_list, 1, gdb_stdout);
901 printf_filtered (",");
902 }
903 }
904
905 FRAME_INIT_SAVED_REGS (fi);
906 if (fi->saved_regs != NULL)
907 {
908 /* The sp is special; what's returned isn't the save address, but
909 actually the value of the previous frame's sp. */
910 printf_filtered (" Previous frame's sp is ");
911 print_address_numeric (fi->saved_regs[SP_REGNUM], 1, gdb_stdout);
912 printf_filtered ("\n");
913 count = 0;
914 numregs = ARCH_NUM_REGS;
915 for (i = 0; i < numregs; i++)
916 if (fi->saved_regs[i] && i != SP_REGNUM)
917 {
918 if (count == 0)
919 puts_filtered (" Saved registers:\n ");
920 else
921 puts_filtered (",");
922 wrap_here (" ");
923 printf_filtered (" %s at ", REGISTER_NAME (i));
924 print_address_numeric (fi->saved_regs[i], 1, gdb_stdout);
925 count++;
926 }
927 if (count)
928 puts_filtered ("\n");
929 }
930 else
931 {
932 /* We could get some information about saved registers by
933 calling get_saved_register on each register. Which info goes
934 with which frame is necessarily lost, however, and I suspect
935 that the users don't care whether they get the info. */
936 puts_filtered ("\n");
937 }
938 }
939
940 #if 0
941 /* Set a limit on the number of frames printed by default in a
942 backtrace. */
943
944 static int backtrace_limit;
945
946 static void
947 set_backtrace_limit_command (count_exp, from_tty)
948 char *count_exp;
949 int from_tty;
950 {
951 int count = parse_and_eval_address (count_exp);
952
953 if (count < 0)
954 error ("Negative argument not meaningful as backtrace limit.");
955
956 backtrace_limit = count;
957 }
958
959 static void
960 backtrace_limit_info (arg, from_tty)
961 char *arg;
962 int from_tty;
963 {
964 if (arg)
965 error ("\"Info backtrace-limit\" takes no arguments.");
966
967 printf_unfiltered ("Backtrace limit: %d.\n", backtrace_limit);
968 }
969 #endif
970
971 /* Print briefly all stack frames or just the innermost COUNT frames. */
972
973 static void
974 backtrace_command_1 (count_exp, show_locals, from_tty)
975 char *count_exp;
976 int show_locals;
977 int from_tty;
978 {
979 struct frame_info *fi;
980 register int count;
981 register int i;
982 register struct frame_info *trailing;
983 register int trailing_level;
984
985 if (!target_has_stack)
986 error ("No stack.");
987
988 /* The following code must do two things. First, it must
989 set the variable TRAILING to the frame from which we should start
990 printing. Second, it must set the variable count to the number
991 of frames which we should print, or -1 if all of them. */
992 trailing = get_current_frame ();
993 trailing_level = 0;
994 if (count_exp)
995 {
996 count = parse_and_eval_address (count_exp);
997 if (count < 0)
998 {
999 struct frame_info *current;
1000
1001 count = -count;
1002
1003 current = trailing;
1004 while (current && count--)
1005 {
1006 QUIT;
1007 current = get_prev_frame (current);
1008 }
1009
1010 /* Will stop when CURRENT reaches the top of the stack. TRAILING
1011 will be COUNT below it. */
1012 while (current)
1013 {
1014 QUIT;
1015 trailing = get_prev_frame (trailing);
1016 current = get_prev_frame (current);
1017 trailing_level++;
1018 }
1019
1020 count = -1;
1021 }
1022 }
1023 else
1024 count = -1;
1025
1026 if (info_verbose)
1027 {
1028 struct partial_symtab *ps;
1029
1030 /* Read in symbols for all of the frames. Need to do this in
1031 a separate pass so that "Reading in symbols for xxx" messages
1032 don't screw up the appearance of the backtrace. Also
1033 if people have strong opinions against reading symbols for
1034 backtrace this may have to be an option. */
1035 i = count;
1036 for (fi = trailing;
1037 fi != NULL && i--;
1038 fi = get_prev_frame (fi))
1039 {
1040 QUIT;
1041 ps = find_pc_psymtab (fi->pc);
1042 if (ps)
1043 PSYMTAB_TO_SYMTAB (ps); /* Force syms to come in */
1044 }
1045 }
1046
1047 for (i = 0, fi = trailing;
1048 fi && count--;
1049 i++, fi = get_prev_frame (fi))
1050 {
1051 QUIT;
1052
1053 /* Don't use print_stack_frame; if an error() occurs it probably
1054 means further attempts to backtrace would fail (on the other
1055 hand, perhaps the code does or could be fixed to make sure
1056 the frame->prev field gets set to NULL in that case). */
1057 print_frame_info_base (fi, trailing_level + i, 0, 1);
1058 if (show_locals)
1059 print_frame_local_vars(fi, 1, gdb_stdout);
1060 }
1061
1062 /* If we've stopped before the end, mention that. */
1063 if (fi && from_tty)
1064 printf_filtered ("(More stack frames follow...)\n");
1065 }
1066
1067 static void
1068 backtrace_command (arg, from_tty)
1069 char *arg;
1070 int from_tty;
1071 {
1072 struct cleanup *old_chain = (struct cleanup *)NULL;
1073 char **argv = (char **)NULL;
1074 int argIndicatingFullTrace = (-1), totArgLen = 0, argc = 0;
1075 char *argPtr = arg;
1076
1077 if (arg != (char *)NULL)
1078 {
1079 int i;
1080
1081 argv = buildargv(arg);
1082 old_chain = make_cleanup(freeargv, (char *)argv);
1083 argc = 0;
1084 for (i = 0; (argv[i] != (char *)NULL); i++)
1085 {
1086 int j;
1087
1088 for (j = 0; (j < strlen(argv[i])); j++)
1089 argv[i][j] = tolower(argv[i][j]);
1090
1091 if (argIndicatingFullTrace < 0 && subsetCompare(argv[i], "full"))
1092 argIndicatingFullTrace = argc;
1093 else
1094 {
1095 argc++;
1096 totArgLen += strlen(argv[i]);
1097 }
1098 }
1099 totArgLen += argc;
1100 if (argIndicatingFullTrace >= 0)
1101 {
1102 if (totArgLen > 0)
1103 {
1104 argPtr = (char *)xmalloc(totArgLen + 1);
1105 if (!argPtr)
1106 nomem(0);
1107 else
1108 {
1109 memset(argPtr, 0, totArgLen + 1);
1110 for (i = 0; (i < (argc + 1)); i++)
1111 {
1112 if (i != argIndicatingFullTrace)
1113 {
1114 strcat(argPtr, argv[i]);
1115 strcat(argPtr, " ");
1116 }
1117 }
1118 }
1119 }
1120 else
1121 argPtr = (char *)NULL;
1122 }
1123 }
1124
1125 backtrace_command_1 (argPtr, (argIndicatingFullTrace >= 0), from_tty);
1126
1127 if (argIndicatingFullTrace >= 0 && totArgLen > 0)
1128 free(argPtr);
1129
1130 if (old_chain)
1131 do_cleanups(old_chain);
1132 }
1133
1134 static void
1135 backtrace_full_command (arg, from_tty)
1136 char *arg;
1137 int from_tty;
1138 {
1139 backtrace_command_1 (arg, 1, from_tty);
1140 }
1141
1142 \f
1143 /* Print the local variables of a block B active in FRAME.
1144 Return 1 if any variables were printed; 0 otherwise. */
1145
1146 static int
1147 print_block_frame_locals (b, fi, num_tabs, stream)
1148 struct block *b;
1149 register struct frame_info *fi;
1150 int num_tabs;
1151 register GDB_FILE *stream;
1152 {
1153 int nsyms;
1154 register int i, j;
1155 register struct symbol *sym;
1156 register int values_printed = 0;
1157
1158 nsyms = BLOCK_NSYMS (b);
1159
1160 for (i = 0; i < nsyms; i++)
1161 {
1162 sym = BLOCK_SYM (b, i);
1163 switch (SYMBOL_CLASS (sym))
1164 {
1165 case LOC_LOCAL:
1166 case LOC_REGISTER:
1167 case LOC_STATIC:
1168 case LOC_BASEREG:
1169 values_printed = 1;
1170 for (j = 0; j < num_tabs; j++)
1171 fputs_filtered("\t", stream);
1172 fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1173 fputs_filtered (" = ", stream);
1174 print_variable_value (sym, fi, stream);
1175 fprintf_filtered (stream, "\n");
1176 break;
1177
1178 default:
1179 /* Ignore symbols which are not locals. */
1180 break;
1181 }
1182 }
1183 return values_printed;
1184 }
1185
1186 /* Same, but print labels. */
1187
1188 static int
1189 print_block_frame_labels (b, have_default, stream)
1190 struct block *b;
1191 int *have_default;
1192 register GDB_FILE *stream;
1193 {
1194 int nsyms;
1195 register int i;
1196 register struct symbol *sym;
1197 register int values_printed = 0;
1198
1199 nsyms = BLOCK_NSYMS (b);
1200
1201 for (i = 0; i < nsyms; i++)
1202 {
1203 sym = BLOCK_SYM (b, i);
1204 if (STREQ (SYMBOL_NAME (sym), "default"))
1205 {
1206 if (*have_default)
1207 continue;
1208 *have_default = 1;
1209 }
1210 if (SYMBOL_CLASS (sym) == LOC_LABEL)
1211 {
1212 struct symtab_and_line sal;
1213 sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
1214 values_printed = 1;
1215 fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1216 if (addressprint)
1217 {
1218 fprintf_filtered (stream, " ");
1219 print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, stream);
1220 }
1221 fprintf_filtered (stream, " in file %s, line %d\n",
1222 sal.symtab->filename, sal.line);
1223 }
1224 }
1225 return values_printed;
1226 }
1227
1228 /* Print on STREAM all the local variables in frame FRAME,
1229 including all the blocks active in that frame
1230 at its current pc.
1231
1232 Returns 1 if the job was done,
1233 or 0 if nothing was printed because we have no info
1234 on the function running in FRAME. */
1235
1236 static void
1237 print_frame_local_vars (fi, num_tabs, stream)
1238 register struct frame_info *fi;
1239 register int num_tabs;
1240 register GDB_FILE *stream;
1241 {
1242 register struct block *block = get_frame_block (fi);
1243 register int values_printed = 0;
1244
1245 if (block == 0)
1246 {
1247 fprintf_filtered (stream, "No symbol table info available.\n");
1248 return;
1249 }
1250
1251 while (block != 0)
1252 {
1253 if (print_block_frame_locals (block, fi, num_tabs, stream))
1254 values_printed = 1;
1255 /* After handling the function's top-level block, stop.
1256 Don't continue to its superblock, the block of
1257 per-file symbols. */
1258 if (BLOCK_FUNCTION (block))
1259 break;
1260 block = BLOCK_SUPERBLOCK (block);
1261 }
1262
1263 if (!values_printed)
1264 {
1265 fprintf_filtered (stream, "No locals.\n");
1266 }
1267 }
1268
1269 /* Same, but print labels. */
1270
1271 static void
1272 print_frame_label_vars (fi, this_level_only, stream)
1273 register struct frame_info *fi;
1274 int this_level_only;
1275 register GDB_FILE *stream;
1276 {
1277 register struct blockvector *bl;
1278 register struct block *block = get_frame_block (fi);
1279 register int values_printed = 0;
1280 int index, have_default = 0;
1281 char *blocks_printed;
1282 CORE_ADDR pc = fi->pc;
1283
1284 if (block == 0)
1285 {
1286 fprintf_filtered (stream, "No symbol table info available.\n");
1287 return;
1288 }
1289
1290 bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
1291 blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1292 memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1293
1294 while (block != 0)
1295 {
1296 CORE_ADDR end = BLOCK_END (block) - 4;
1297 int last_index;
1298
1299 if (bl != blockvector_for_pc (end, &index))
1300 error ("blockvector blotch");
1301 if (BLOCKVECTOR_BLOCK (bl, index) != block)
1302 error ("blockvector botch");
1303 last_index = BLOCKVECTOR_NBLOCKS (bl);
1304 index += 1;
1305
1306 /* Don't print out blocks that have gone by. */
1307 while (index < last_index
1308 && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
1309 index++;
1310
1311 while (index < last_index
1312 && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
1313 {
1314 if (blocks_printed[index] == 0)
1315 {
1316 if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
1317 values_printed = 1;
1318 blocks_printed[index] = 1;
1319 }
1320 index++;
1321 }
1322 if (have_default)
1323 return;
1324 if (values_printed && this_level_only)
1325 return;
1326
1327 /* After handling the function's top-level block, stop.
1328 Don't continue to its superblock, the block of
1329 per-file symbols. */
1330 if (BLOCK_FUNCTION (block))
1331 break;
1332 block = BLOCK_SUPERBLOCK (block);
1333 }
1334
1335 if (!values_printed && !this_level_only)
1336 {
1337 fprintf_filtered (stream, "No catches.\n");
1338 }
1339 }
1340
1341 /* ARGSUSED */
1342 void
1343 locals_info (args, from_tty)
1344 char *args;
1345 int from_tty;
1346 {
1347 if (!selected_frame)
1348 error ("No frame selected.");
1349 print_frame_local_vars (selected_frame, 0, gdb_stdout);
1350 }
1351
1352 static void
1353 catch_info (ignore, from_tty)
1354 char *ignore;
1355 int from_tty;
1356 {
1357 struct symtab_and_line * sal;
1358
1359 /* Check for target support for exception handling */
1360 sal = target_enable_exception_callback (EX_EVENT_CATCH, 1);
1361 if (sal)
1362 {
1363 /* Currently not handling this */
1364 /* Ideally, here we should interact with the C++ runtime
1365 system to find the list of active handlers, etc. */
1366 fprintf_filtered (gdb_stdout, "Info catch not supported with this target/compiler combination.\n");
1367 #if 0
1368 if (!selected_frame)
1369 error ("No frame selected.");
1370 #endif
1371 }
1372 else
1373 {
1374 /* Assume g++ compiled code -- old v 4.16 behaviour */
1375 if (!selected_frame)
1376 error ("No frame selected.");
1377
1378 print_frame_label_vars (selected_frame, 0, gdb_stdout);
1379 }
1380 }
1381
1382 static void
1383 print_frame_arg_vars (fi, stream)
1384 register struct frame_info *fi;
1385 register GDB_FILE *stream;
1386 {
1387 struct symbol *func = get_frame_function (fi);
1388 register struct block *b;
1389 int nsyms;
1390 register int i;
1391 register struct symbol *sym, *sym2;
1392 register int values_printed = 0;
1393
1394 if (func == 0)
1395 {
1396 fprintf_filtered (stream, "No symbol table info available.\n");
1397 return;
1398 }
1399
1400 b = SYMBOL_BLOCK_VALUE (func);
1401 nsyms = BLOCK_NSYMS (b);
1402
1403 for (i = 0; i < nsyms; i++)
1404 {
1405 sym = BLOCK_SYM (b, i);
1406 switch (SYMBOL_CLASS (sym))
1407 {
1408 case LOC_ARG:
1409 case LOC_LOCAL_ARG:
1410 case LOC_REF_ARG:
1411 case LOC_REGPARM:
1412 case LOC_REGPARM_ADDR:
1413 case LOC_BASEREG_ARG:
1414 values_printed = 1;
1415 fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1416 fputs_filtered (" = ", stream);
1417
1418 /* We have to look up the symbol because arguments can have
1419 two entries (one a parameter, one a local) and the one we
1420 want is the local, which lookup_symbol will find for us.
1421 This includes gcc1 (not gcc2) on the sparc when passing a
1422 small structure and gcc2 when the argument type is float
1423 and it is passed as a double and converted to float by
1424 the prologue (in the latter case the type of the LOC_ARG
1425 symbol is double and the type of the LOC_LOCAL symbol is
1426 float). There are also LOC_ARG/LOC_REGISTER pairs which
1427 are not combined in symbol-reading. */
1428
1429 sym2 = lookup_symbol (SYMBOL_NAME (sym),
1430 b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL);
1431 print_variable_value (sym2, fi, stream);
1432 fprintf_filtered (stream, "\n");
1433 break;
1434
1435 default:
1436 /* Don't worry about things which aren't arguments. */
1437 break;
1438 }
1439 }
1440
1441 if (!values_printed)
1442 {
1443 fprintf_filtered (stream, "No arguments.\n");
1444 }
1445 }
1446
1447 void
1448 args_info (ignore, from_tty)
1449 char *ignore;
1450 int from_tty;
1451 {
1452 if (!selected_frame)
1453 error ("No frame selected.");
1454 print_frame_arg_vars (selected_frame, gdb_stdout);
1455 }
1456
1457
1458 static void
1459 args_plus_locals_info(ignore, from_tty)
1460 char *ignore;
1461 int from_tty;
1462 {
1463 args_info(ignore, from_tty);
1464 locals_info(ignore, from_tty);
1465 }
1466
1467 \f
1468 /* Select frame FI, and note that its stack level is LEVEL.
1469 LEVEL may be -1 if an actual level number is not known. */
1470
1471 void
1472 select_frame (fi, level)
1473 struct frame_info *fi;
1474 int level;
1475 {
1476 register struct symtab *s;
1477
1478 selected_frame = fi;
1479 selected_frame_level = level;
1480 if (selected_frame_level_changed_hook)
1481 selected_frame_level_changed_hook (level);
1482
1483 /* Ensure that symbols for this frame are read in. Also, determine the
1484 source language of this frame, and switch to it if desired. */
1485 if (fi)
1486 {
1487 s = find_pc_symtab (fi->pc);
1488 if (s
1489 && s->language != current_language->la_language
1490 && s->language != language_unknown
1491 && language_mode == language_mode_auto) {
1492 set_language(s->language);
1493 }
1494 /* elz: this if here fixes the problem with the pc not being displayed
1495 in the tui asm layout, with no debug symbols. The value of s
1496 would be 0 here, and select_source_symtab would abort the
1497 command by calling the 'error' function*/
1498 if (s)
1499 {
1500 TUIDO(((TuiOpaqueFuncPtr)tui_vSelectSourceSymtab, s));
1501 }
1502 }
1503 }
1504
1505 \f
1506 /* Select frame FI, noting that its stack level is LEVEL. Also print
1507 the stack frame and show the source if this is the tui version. */
1508 void
1509 select_and_print_frame(fi, level)
1510 struct frame_info *fi;
1511 int level;
1512 {
1513 select_frame(fi, level);
1514 if (fi)
1515 {
1516 print_stack_frame(fi, level, 1);
1517 TUIDO(((TuiOpaqueFuncPtr)tui_vCheckDataValues, fi));
1518 }
1519 }
1520
1521 \f
1522 /* Select frame FI, noting that its stack level is LEVEL. Be silent if
1523 not the TUI */
1524 void
1525 select_and_maybe_print_frame(fi, level)
1526 struct frame_info *fi;
1527 int level;
1528 {
1529 if (!tui_version)
1530 select_frame(fi, level);
1531 else
1532 select_and_print_frame(fi, level);
1533 }
1534
1535
1536 /* Store the selected frame and its level into *FRAMEP and *LEVELP.
1537 If there is no selected frame, *FRAMEP is set to NULL. */
1538
1539 void
1540 record_selected_frame (frameaddrp, levelp)
1541 CORE_ADDR *frameaddrp;
1542 int *levelp;
1543 {
1544 *frameaddrp = selected_frame ? selected_frame->frame : 0;
1545 *levelp = selected_frame_level;
1546 }
1547
1548 /* Return the symbol-block in which the selected frame is executing.
1549 Can return zero under various legitimate circumstances. */
1550
1551 struct block *
1552 get_selected_block ()
1553 {
1554 if (!target_has_stack)
1555 return 0;
1556
1557 if (!selected_frame)
1558 return get_current_block ();
1559 return get_frame_block (selected_frame);
1560 }
1561
1562 /* Find a frame a certain number of levels away from FRAME.
1563 LEVEL_OFFSET_PTR points to an int containing the number of levels.
1564 Positive means go to earlier frames (up); negative, the reverse.
1565 The int that contains the number of levels is counted toward
1566 zero as the frames for those levels are found.
1567 If the top or bottom frame is reached, that frame is returned,
1568 but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
1569 how much farther the original request asked to go. */
1570
1571 struct frame_info *
1572 find_relative_frame (frame, level_offset_ptr)
1573 register struct frame_info *frame;
1574 register int *level_offset_ptr;
1575 {
1576 register struct frame_info *prev;
1577 register struct frame_info *frame1;
1578
1579 /* Going up is simple: just do get_prev_frame enough times
1580 or until initial frame is reached. */
1581 while (*level_offset_ptr > 0)
1582 {
1583 prev = get_prev_frame (frame);
1584 if (prev == 0)
1585 break;
1586 (*level_offset_ptr)--;
1587 frame = prev;
1588 }
1589 /* Going down is just as simple. */
1590 if (*level_offset_ptr < 0)
1591 {
1592 while (*level_offset_ptr < 0) {
1593 frame1 = get_next_frame (frame);
1594 if (!frame1)
1595 break;
1596 frame = frame1;
1597 (*level_offset_ptr)++;
1598 }
1599 }
1600 return frame;
1601 }
1602
1603 /* The "select_frame" command. With no arg, NOP.
1604 With arg LEVEL_EXP, select the frame at level LEVEL if it is a
1605 valid level. Otherwise, treat level_exp as an address expression
1606 and select it. See parse_frame_specification for more info on proper
1607 frame expressions. */
1608
1609 /* ARGSUSED */
1610 static void
1611 select_frame_command (level_exp, from_tty)
1612 char *level_exp;
1613 int from_tty;
1614 {
1615 register struct frame_info *frame, *frame1;
1616 unsigned int level = 0;
1617
1618 if (!target_has_stack)
1619 error ("No stack.");
1620
1621 frame = parse_frame_specification (level_exp);
1622
1623 /* Try to figure out what level this frame is. But if there is
1624 no current stack, don't error out -- let the user set one. */
1625 frame1 = 0;
1626 if (get_current_frame()) {
1627 for (frame1 = get_prev_frame (0);
1628 frame1 && frame1 != frame;
1629 frame1 = get_prev_frame (frame1))
1630 level++;
1631 }
1632
1633 if (!frame1)
1634 level = 0;
1635
1636 select_frame (frame, level);
1637 }
1638
1639 /* The "frame" command. With no arg, print selected frame briefly.
1640 With arg, behaves like select_frame and then prints the selected
1641 frame. */
1642
1643 void
1644 frame_command (level_exp, from_tty)
1645 char *level_exp;
1646 int from_tty;
1647 {
1648 select_frame_command (level_exp, from_tty);
1649 show_and_print_stack_frame (selected_frame, selected_frame_level, 1);
1650 }
1651
1652 /* The XDB Compatibility command to print the current frame. */
1653
1654 void
1655 current_frame_command (level_exp, from_tty)
1656 char *level_exp;
1657 int from_tty;
1658 {
1659 if (target_has_stack == 0 || selected_frame == 0)
1660 error ("No stack.");
1661 print_only_stack_frame (selected_frame, selected_frame_level, 1);
1662 }
1663
1664 /* Select the frame up one or COUNT stack levels
1665 from the previously selected frame, and print it briefly. */
1666
1667 /* ARGSUSED */
1668 static void
1669 up_silently_base (count_exp)
1670 char *count_exp;
1671 {
1672 register struct frame_info *fi;
1673 int count = 1, count1;
1674 if (count_exp)
1675 count = parse_and_eval_address (count_exp);
1676 count1 = count;
1677
1678 if (target_has_stack == 0 || selected_frame == 0)
1679 error ("No stack.");
1680
1681 fi = find_relative_frame (selected_frame, &count1);
1682 if (count1 != 0 && count_exp == 0)
1683 error ("Initial frame selected; you cannot go up.");
1684 select_frame (fi, selected_frame_level + count - count1);
1685 }
1686
1687 static void
1688 up_silently_command (count_exp, from_tty)
1689 char *count_exp;
1690 int from_tty;
1691 {
1692 up_silently_base(count_exp);
1693 if (tui_version)
1694 print_stack_frame (selected_frame, selected_frame_level, 1);
1695 }
1696
1697 static void
1698 up_command (count_exp, from_tty)
1699 char *count_exp;
1700 int from_tty;
1701 {
1702 up_silently_base (count_exp);
1703 show_and_print_stack_frame (selected_frame, selected_frame_level, 1);
1704 }
1705
1706 /* Select the frame down one or COUNT stack levels
1707 from the previously selected frame, and print it briefly. */
1708
1709 /* ARGSUSED */
1710 static void
1711 down_silently_base (count_exp)
1712 char *count_exp;
1713 {
1714 register struct frame_info *frame;
1715 int count = -1, count1;
1716 if (count_exp)
1717 count = - parse_and_eval_address (count_exp);
1718 count1 = count;
1719
1720 if (target_has_stack == 0 || selected_frame == 0)
1721 error ("No stack.");
1722
1723 frame = find_relative_frame (selected_frame, &count1);
1724 if (count1 != 0 && count_exp == 0)
1725 {
1726
1727 /* We only do this if count_exp is not specified. That way "down"
1728 means to really go down (and let me know if that is
1729 impossible), but "down 9999" can be used to mean go all the way
1730 down without getting an error. */
1731
1732 error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
1733 }
1734
1735 select_frame (frame, selected_frame_level + count - count1);
1736 }
1737
1738 /* ARGSUSED */
1739 static void
1740 down_silently_command (count_exp, from_tty)
1741 char *count_exp;
1742 int from_tty;
1743 {
1744 down_silently_base (count_exp);
1745 if (tui_version)
1746 print_stack_frame (selected_frame, selected_frame_level, 1);
1747 }
1748
1749 static void
1750 down_command (count_exp, from_tty)
1751 char *count_exp;
1752 int from_tty;
1753 {
1754 down_silently_base (count_exp);
1755 show_and_print_stack_frame (selected_frame, selected_frame_level, 1);
1756 }
1757 \f
1758 static void
1759 return_command (retval_exp, from_tty)
1760 char *retval_exp;
1761 int from_tty;
1762 {
1763 struct symbol *thisfun;
1764 CORE_ADDR selected_frame_addr;
1765 CORE_ADDR selected_frame_pc;
1766 struct frame_info *frame;
1767 value_ptr return_value = NULL;
1768
1769 if (selected_frame == NULL)
1770 error ("No selected frame.");
1771 thisfun = get_frame_function (selected_frame);
1772 selected_frame_addr = FRAME_FP (selected_frame);
1773 selected_frame_pc = selected_frame->pc;
1774
1775 /* Compute the return value (if any -- possibly getting errors here). */
1776
1777 if (retval_exp)
1778 {
1779 struct type *return_type = NULL;
1780
1781 return_value = parse_and_eval (retval_exp);
1782
1783 /* Cast return value to the return type of the function. */
1784 if (thisfun != NULL)
1785 return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
1786 if (return_type == NULL)
1787 return_type = builtin_type_int;
1788 return_value = value_cast (return_type, return_value);
1789
1790 /* Make sure we have fully evaluated it, since
1791 it might live in the stack frame we're about to pop. */
1792 if (VALUE_LAZY (return_value))
1793 value_fetch_lazy (return_value);
1794 }
1795
1796 /* If interactive, require confirmation. */
1797
1798 if (from_tty)
1799 {
1800 if (thisfun != 0)
1801 {
1802 if (!query ("Make %s return now? ", SYMBOL_SOURCE_NAME (thisfun)))
1803 {
1804 error ("Not confirmed.");
1805 /* NOTREACHED */
1806 }
1807 }
1808 else
1809 if (!query ("Make selected stack frame return now? "))
1810 error ("Not confirmed.");
1811 }
1812
1813 /* Do the real work. Pop until the specified frame is current. We
1814 use this method because the selected_frame is not valid after
1815 a POP_FRAME. The pc comparison makes this work even if the
1816 selected frame shares its fp with another frame. */
1817
1818 while (selected_frame_addr != (frame = get_current_frame())->frame
1819 || selected_frame_pc != frame->pc)
1820 POP_FRAME;
1821
1822 /* Then pop that frame. */
1823
1824 POP_FRAME;
1825
1826 /* Compute the return value (if any) and store in the place
1827 for return values. */
1828
1829 if (retval_exp)
1830 set_return_value (return_value);
1831
1832 /* If interactive, print the frame that is now current. */
1833
1834 if (from_tty)
1835 frame_command ("0", 1);
1836 else
1837 select_frame_command ("0", 0);
1838 }
1839
1840 /* Sets the scope to input function name, provided that the
1841 function is within the current stack frame */
1842 struct function_bounds
1843 {
1844 CORE_ADDR low, high;
1845 };
1846
1847 static void
1848 func_command (arg, from_tty)
1849 char *arg;
1850 int from_tty;
1851 {
1852 if (arg != (char *)NULL)
1853 {
1854 struct frame_info *fp;
1855 int found = 0;
1856 struct symtabs_and_lines sals;
1857 int i;
1858 int level = 1;
1859 struct function_bounds *funcBounds = (struct function_bounds *)NULL;
1860
1861 fp = parse_frame_specification("0");
1862 sals = decode_line_spec(arg, 1);
1863 funcBounds = (struct function_bounds *)xmalloc(
1864 sizeof(struct function_bounds) * sals.nelts);
1865 for (i = 0; (i < sals.nelts && !found); i++)
1866 {
1867 if (sals.sals[i].pc == (CORE_ADDR)0 ||
1868 find_pc_partial_function(sals.sals[i].pc,
1869 (char **)NULL,
1870 &funcBounds[i].low,
1871 &funcBounds[i].high) == 0)
1872 {
1873 funcBounds[i].low =
1874 funcBounds[i].high = (CORE_ADDR)NULL;
1875 }
1876 }
1877
1878 do
1879 {
1880 for (i = 0; (i < sals.nelts && !found); i++)
1881 found = (fp->pc >= funcBounds[i].low &&
1882 fp->pc < funcBounds[i].high);
1883 if (!found)
1884 {
1885 level = 1;
1886 fp = find_relative_frame(fp, &level);
1887 }
1888 } while (!found && level == 0);
1889
1890 if (funcBounds)
1891 free(funcBounds);
1892
1893 if (!found)
1894 printf_filtered("'%s' not within current stack frame.\n", arg);
1895 else if (fp != selected_frame) /* set the stack frame here! */
1896 select_and_print_frame(fp);
1897 }
1898 }
1899
1900 /* Gets the language of the current frame. */
1901
1902 enum language
1903 get_frame_language()
1904 {
1905 register struct symtab *s;
1906 enum language flang; /* The language of the current frame */
1907
1908 if (selected_frame)
1909 {
1910 s = find_pc_symtab(selected_frame->pc);
1911 if (s)
1912 flang = s->language;
1913 else
1914 flang = language_unknown;
1915 }
1916 else
1917 flang = language_unknown;
1918
1919 return flang;
1920 }
1921 \f
1922 void
1923 _initialize_stack ()
1924 {
1925 #if 0
1926 backtrace_limit = 30;
1927 #endif
1928
1929 add_com ("return", class_stack, return_command,
1930 "Make selected stack frame return to its caller.\n\
1931 Control remains in the debugger, but when you continue\n\
1932 execution will resume in the frame above the one now selected.\n\
1933 If an argument is given, it is an expression for the value to return.");
1934
1935 add_com ("up", class_stack, up_command,
1936 "Select and print stack frame that called this one.\n\
1937 An argument says how many frames up to go.");
1938 add_com ("up-silently", class_support, up_silently_command,
1939 "Same as the `up' command, but does not print anything.\n\
1940 This is useful in command scripts.");
1941
1942 add_com ("down", class_stack, down_command,
1943 "Select and print stack frame called by this one.\n\
1944 An argument says how many frames down to go.");
1945 add_com_alias ("do", "down", class_stack, 1);
1946 add_com_alias ("dow", "down", class_stack, 1);
1947 add_com ("down-silently", class_support, down_silently_command,
1948 "Same as the `down' command, but does not print anything.\n\
1949 This is useful in command scripts.");
1950
1951 add_com ("frame", class_stack, frame_command,
1952 "Select and print a stack frame.\n\
1953 With no argument, print the selected stack frame. (See also \"info frame\").\n\
1954 An argument specifies the frame to select.\n\
1955 It can be a stack frame number or the address of the frame.\n\
1956 With argument, nothing is printed if input is coming from\n\
1957 a command file or a user-defined command.");
1958
1959 add_com_alias ("f", "frame", class_stack, 1);
1960
1961 if (xdb_commands)
1962 {
1963 add_com("L", class_stack, current_frame_command,
1964 "Print the current stack frame.\n");
1965 add_com_alias ("V", "frame", class_stack, 1);
1966 }
1967 add_com ("select-frame", class_stack, select_frame_command,
1968 "Select a stack frame without printing anything.\n\
1969 An argument specifies the frame to select.\n\
1970 It can be a stack frame number or the address of the frame.\n");
1971
1972 add_com ("backtrace", class_stack, backtrace_command,
1973 "Print backtrace of all stack frames, or innermost COUNT frames.\n\
1974 With a negative argument, print outermost -COUNT frames.\n\
1975 Use of the 'full' qualifier also prints the values of the local variables.\n");
1976 add_com_alias ("bt", "backtrace", class_stack, 0);
1977 if (xdb_commands)
1978 {
1979 add_com_alias ("t", "backtrace", class_stack, 0);
1980 add_com ("T", class_stack, backtrace_full_command,
1981 "Print backtrace of all stack frames, or innermost COUNT frames \n\
1982 and the values of the local variables.\n\
1983 With a negative argument, print outermost -COUNT frames.\n\
1984 Usage: T <count>\n");
1985 }
1986
1987 add_com_alias ("where", "backtrace", class_alias, 0);
1988 add_info ("stack", backtrace_command,
1989 "Backtrace of the stack, or innermost COUNT frames.");
1990 add_info_alias ("s", "stack", 1);
1991 add_info ("frame", frame_info,
1992 "All about selected stack frame, or frame at ADDR.");
1993 add_info_alias ("f", "frame", 1);
1994 add_info ("locals", locals_info,
1995 "Local variables of current stack frame.");
1996 add_info ("args", args_info,
1997 "Argument variables of current stack frame.");
1998 if (xdb_commands)
1999 add_com("l", class_info, args_plus_locals_info,
2000 "Argument and local variables of current stack frame.");
2001
2002 if (dbx_commands)
2003 add_com("func", class_stack, func_command,
2004 "Select the stack frame that contains <func>.\nUsage: func <name>\n");
2005
2006 add_info ("catch", catch_info,
2007 "Exceptions that can be caught in the current stack frame.");
2008
2009 #if 0
2010 add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command,
2011 "Specify maximum number of frames for \"backtrace\" to print by default.",
2012 &setlist);
2013 add_info ("backtrace-limit", backtrace_limit_info,
2014 "The maximum number of frames for \"backtrace\" to print by default.");
2015 #endif
2016 }