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