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