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