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