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