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