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