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