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