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