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