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