*** empty log message ***
[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"
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
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
AC
217 annotate_frame_address ();
218 ui_out_field_core_addr (uiout, "addr", fi->pc);
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;
358 funname = SYMBOL_NAME (msymbol);
359 funlang = SYMBOL_LANGUAGE (msymbol);
360 }
361 else
362 {
c5394b80
JM
363 /* I'd like to use SYMBOL_SOURCE_NAME() here, to display the
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;
c906108c
SS
375 funname = SYMBOL_NAME (func);
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. */
c5aa993b
JM
384 funname = SYMBOL_SOURCE_NAME (func);
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 {
393 funname = SYMBOL_NAME (msymbol);
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
602/* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except
603 that if it is unsure about the answer, it returns 0
604 instead of guessing (this happens on the VAX and i960, for example).
605
606 On most machines, we never have to guess about the args address,
607 so FRAME_ARGS_ADDRESS{,_CORRECT} are the same. */
608#if !defined (FRAME_ARGS_ADDRESS_CORRECT)
609#define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS
610#endif
611
612/* Print verbosely the selected frame or the frame at address ADDR.
613 This means absolutely all information in the frame is printed. */
614
615static void
fba45db2 616frame_info (char *addr_exp, int from_tty)
c906108c
SS
617{
618 struct frame_info *fi;
619 struct symtab_and_line sal;
620 struct symbol *func;
621 struct symtab *s;
622 struct frame_info *calling_frame_info;
623 int i, count, numregs;
624 char *funname = 0;
625 enum language funlang = language_unknown;
626
627 if (!target_has_stack)
628 error ("No stack.");
629
630 fi = parse_frame_specification (addr_exp);
631 if (fi == NULL)
632 error ("Invalid frame specified.");
633
1058bca7 634 find_frame_sal (fi, &sal);
c906108c 635 func = get_frame_function (fi);
1058bca7
AC
636 /* FIXME: cagney/2002-11-28: Why bother? Won't sal.symtab contain
637 the same value. */
bdd78e62 638 s = find_pc_symtab (get_frame_pc (fi));
c906108c
SS
639 if (func)
640 {
641 /* I'd like to use SYMBOL_SOURCE_NAME() here, to display
642 * the demangled name that we already have stored in
643 * the symbol table, but we stored a version with
644 * DMGL_PARAMS turned on, and here we don't want
645 * to display parameters. So call the demangler again,
646 * with DMGL_ANSI only. RT
647 * (Yes, I know that printf_symbol_filtered() will
648 * again try to demangle the name on the fly, but
649 * the issue is that if cplus_demangle() fails here,
650 * it'll fail there too. So we want to catch the failure
651 * ("demangled==NULL" case below) here, while we still
652 * have our hands on the function symbol.)
653 */
c5aa993b
JM
654 char *demangled;
655 funname = SYMBOL_NAME (func);
656 funlang = SYMBOL_LANGUAGE (func);
657 if (funlang == language_cplus)
658 {
659 demangled = cplus_demangle (funname, DMGL_ANSI);
660 /* If the demangler fails, try the demangled name
661 * from the symbol table. This'll have parameters,
662 * but that's preferable to diplaying a mangled name.
663 */
664 if (demangled == NULL)
665 funname = SYMBOL_SOURCE_NAME (func);
666 }
c906108c
SS
667 }
668 else
669 {
bdd78e62 670 register struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (get_frame_pc (fi));
c906108c
SS
671 if (msymbol != NULL)
672 {
673 funname = SYMBOL_NAME (msymbol);
674 funlang = SYMBOL_LANGUAGE (msymbol);
675 }
676 }
677 calling_frame_info = get_prev_frame (fi);
678
6e7f8b9c 679 if (!addr_exp && frame_relative_level (deprecated_selected_frame) >= 0)
c906108c 680 {
b31da25e 681 printf_filtered ("Stack level %d, frame at ",
6e7f8b9c 682 frame_relative_level (deprecated_selected_frame));
8b36eed8 683 print_address_numeric (get_frame_base (fi), 1, gdb_stdout);
c906108c
SS
684 printf_filtered (":\n");
685 }
686 else
687 {
688 printf_filtered ("Stack frame at ");
8b36eed8 689 print_address_numeric (get_frame_base (fi), 1, gdb_stdout);
c906108c
SS
690 printf_filtered (":\n");
691 }
692 printf_filtered (" %s = ", REGISTER_NAME (PC_REGNUM));
bdd78e62 693 print_address_numeric (get_frame_pc (fi), 1, gdb_stdout);
c906108c
SS
694
695 wrap_here (" ");
696 if (funname)
697 {
698 printf_filtered (" in ");
699 fprintf_symbol_filtered (gdb_stdout, funname, funlang,
700 DMGL_ANSI | DMGL_PARAMS);
701 }
702 wrap_here (" ");
703 if (sal.symtab)
704 printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line);
705 puts_filtered ("; ");
706 wrap_here (" ");
707 printf_filtered ("saved %s ", REGISTER_NAME (PC_REGNUM));
f18c5a73 708 print_address_numeric (frame_pc_unwind (fi), 1, gdb_stdout);
c906108c
SS
709 printf_filtered ("\n");
710
711 {
392a587b
JM
712 int frameless;
713 frameless = FRAMELESS_FUNCTION_INVOCATION (fi);
c906108c
SS
714 if (frameless)
715 printf_filtered (" (FRAMELESS),");
716 }
717
718 if (calling_frame_info)
719 {
720 printf_filtered (" called by frame at ");
8b36eed8
AC
721 print_address_numeric (get_frame_base (calling_frame_info),
722 1, gdb_stdout);
c906108c 723 }
75e3c1f9 724 if (get_next_frame (fi) && calling_frame_info)
c906108c
SS
725 puts_filtered (",");
726 wrap_here (" ");
75e3c1f9 727 if (get_next_frame (fi))
c906108c
SS
728 {
729 printf_filtered (" caller of frame at ");
8b36eed8
AC
730 print_address_numeric (get_frame_base (get_next_frame (fi)), 1,
731 gdb_stdout);
c906108c 732 }
75e3c1f9 733 if (get_next_frame (fi) || calling_frame_info)
c906108c
SS
734 puts_filtered ("\n");
735 if (s)
1058bca7
AC
736 printf_filtered (" source language %s.\n",
737 language_str (s->language));
c906108c
SS
738
739#ifdef PRINT_EXTRA_FRAME_INFO
740 PRINT_EXTRA_FRAME_INFO (fi);
741#endif
742
743 {
744 /* Address of the argument list for this frame, or 0. */
745 CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi);
746 /* Number of args for this frame, or -1 if unknown. */
747 int numargs;
748
749 if (arg_list == 0)
750 printf_filtered (" Arglist at unknown address.\n");
751 else
752 {
753 printf_filtered (" Arglist at ");
754 print_address_numeric (arg_list, 1, gdb_stdout);
755 printf_filtered (",");
756
392a587b 757 numargs = FRAME_NUM_ARGS (fi);
c906108c
SS
758 if (numargs < 0)
759 puts_filtered (" args: ");
760 else if (numargs == 0)
761 puts_filtered (" no args.");
762 else if (numargs == 1)
763 puts_filtered (" 1 arg: ");
764 else
765 printf_filtered (" %d args: ", numargs);
766 print_frame_args (func, fi, numargs, gdb_stdout);
767 puts_filtered ("\n");
768 }
769 }
770 {
771 /* Address of the local variables for this frame, or 0. */
772 CORE_ADDR arg_list = FRAME_LOCALS_ADDRESS (fi);
773
774 if (arg_list == 0)
775 printf_filtered (" Locals at unknown address,");
776 else
777 {
778 printf_filtered (" Locals at ");
779 print_address_numeric (arg_list, 1, gdb_stdout);
780 printf_filtered (",");
781 }
782 }
783
8f871025
AC
784 if (FRAME_INIT_SAVED_REGS_P ()
785 && get_frame_saved_regs (fi) == NULL)
4f460812
AC
786 FRAME_INIT_SAVED_REGS (fi);
787 /* Print as much information as possible on the location of all the
788 registers. */
789 {
790 enum lval_type lval;
791 int optimized;
792 CORE_ADDR addr;
793 int realnum;
794 int count;
795 int i;
796 int need_nl = 1;
797
798 /* The sp is special; what's displayed isn't the save address, but
799 the value of the previous frame's sp. This is a legacy thing,
800 at one stage the frame cached the previous frame's SP instead
801 of its address, hence it was easiest to just display the cached
802 value. */
803 if (SP_REGNUM >= 0)
804 {
805 /* Find out the location of the saved stack pointer with out
806 actually evaluating it. */
807 frame_register_unwind (fi, SP_REGNUM, &optimized, &lval, &addr,
808 &realnum, NULL);
809 if (!optimized && lval == not_lval)
c906108c 810 {
4f460812
AC
811 void *value = alloca (MAX_REGISTER_RAW_SIZE);
812 CORE_ADDR sp;
813 frame_register_unwind (fi, SP_REGNUM, &optimized, &lval, &addr,
814 &realnum, value);
815 sp = extract_address (value, REGISTER_RAW_SIZE (SP_REGNUM));
816 printf_filtered (" Previous frame's sp is ");
817 print_address_numeric (sp, 1, gdb_stdout);
818 printf_filtered ("\n");
819 need_nl = 0;
c906108c 820 }
4f460812
AC
821 else if (!optimized && lval == lval_memory)
822 {
823 printf_filtered (" Previous frame's sp at ");
824 print_address_numeric (addr, 1, gdb_stdout);
825 printf_filtered ("\n");
826 need_nl = 0;
827 }
828 else if (!optimized && lval == lval_register)
829 {
830 printf_filtered (" Previous frame's sp in %s\n",
831 REGISTER_NAME (realnum));
832 need_nl = 0;
833 }
834 /* else keep quiet. */
835 }
836
837 count = 0;
838 numregs = NUM_REGS + NUM_PSEUDO_REGS;
839 for (i = 0; i < numregs; i++)
840 if (i != SP_REGNUM)
841 {
842 /* Find out the location of the saved register without
843 fetching the corresponding value. */
844 frame_register_unwind (fi, i, &optimized, &lval, &addr, &realnum,
845 NULL);
846 /* For moment, only display registers that were saved on the
847 stack. */
848 if (!optimized && lval == lval_memory)
849 {
850 if (count == 0)
851 puts_filtered (" Saved registers:\n ");
852 else
853 puts_filtered (",");
854 wrap_here (" ");
855 printf_filtered (" %s at ", REGISTER_NAME (i));
856 print_address_numeric (addr, 1, gdb_stdout);
857 count++;
858 }
859 }
860 if (count || need_nl)
c906108c 861 puts_filtered ("\n");
4f460812 862 }
c906108c
SS
863}
864
865#if 0
866/* Set a limit on the number of frames printed by default in a
867 backtrace. */
868
869static int backtrace_limit;
870
871static void
fba45db2 872set_backtrace_limit_command (char *count_exp, int from_tty)
c906108c 873{
bb518678 874 int count = parse_and_eval_long (count_exp);
c906108c
SS
875
876 if (count < 0)
877 error ("Negative argument not meaningful as backtrace limit.");
878
879 backtrace_limit = count;
880}
881
882static void
fba45db2 883backtrace_limit_info (char *arg, int from_tty)
c906108c
SS
884{
885 if (arg)
886 error ("\"Info backtrace-limit\" takes no arguments.");
887
888 printf_unfiltered ("Backtrace limit: %d.\n", backtrace_limit);
889}
890#endif
891
892/* Print briefly all stack frames or just the innermost COUNT frames. */
893
a14ed312
KB
894static void backtrace_command_1 (char *count_exp, int show_locals,
895 int from_tty);
c906108c 896static void
fba45db2 897backtrace_command_1 (char *count_exp, int show_locals, int from_tty)
c906108c
SS
898{
899 struct frame_info *fi;
900 register int count;
901 register int i;
902 register struct frame_info *trailing;
903 register int trailing_level;
904
905 if (!target_has_stack)
906 error ("No stack.");
907
908 /* The following code must do two things. First, it must
909 set the variable TRAILING to the frame from which we should start
910 printing. Second, it must set the variable count to the number
911 of frames which we should print, or -1 if all of them. */
912 trailing = get_current_frame ();
d082b2bb
AC
913
914 /* The target can be in a state where there is no valid frames
915 (e.g., just connected). */
916 if (trailing == NULL)
917 error ("No stack.");
918
c906108c
SS
919 trailing_level = 0;
920 if (count_exp)
921 {
bb518678 922 count = parse_and_eval_long (count_exp);
c906108c
SS
923 if (count < 0)
924 {
925 struct frame_info *current;
926
927 count = -count;
928
929 current = trailing;
930 while (current && count--)
931 {
932 QUIT;
933 current = get_prev_frame (current);
934 }
c5aa993b 935
c906108c
SS
936 /* Will stop when CURRENT reaches the top of the stack. TRAILING
937 will be COUNT below it. */
938 while (current)
939 {
940 QUIT;
941 trailing = get_prev_frame (trailing);
942 current = get_prev_frame (current);
943 trailing_level++;
944 }
c5aa993b 945
c906108c
SS
946 count = -1;
947 }
948 }
949 else
950 count = -1;
951
952 if (info_verbose)
953 {
954 struct partial_symtab *ps;
c5aa993b 955
c906108c 956 /* Read in symbols for all of the frames. Need to do this in
c5aa993b
JM
957 a separate pass so that "Reading in symbols for xxx" messages
958 don't screw up the appearance of the backtrace. Also
959 if people have strong opinions against reading symbols for
960 backtrace this may have to be an option. */
c906108c
SS
961 i = count;
962 for (fi = trailing;
963 fi != NULL && i--;
964 fi = get_prev_frame (fi))
965 {
966 QUIT;
6f0eaa01 967 ps = find_pc_psymtab (frame_address_in_block (fi));
c906108c
SS
968 if (ps)
969 PSYMTAB_TO_SYMTAB (ps); /* Force syms to come in */
970 }
971 }
972
973 for (i = 0, fi = trailing;
974 fi && count--;
975 i++, fi = get_prev_frame (fi))
976 {
977 QUIT;
978
979 /* Don't use print_stack_frame; if an error() occurs it probably
c5aa993b
JM
980 means further attempts to backtrace would fail (on the other
981 hand, perhaps the code does or could be fixed to make sure
982 the frame->prev field gets set to NULL in that case). */
7789c6f5 983 print_frame_info (fi, trailing_level + i, 0, 1);
c906108c 984 if (show_locals)
c5aa993b 985 print_frame_local_vars (fi, 1, gdb_stdout);
c906108c
SS
986 }
987
988 /* If we've stopped before the end, mention that. */
989 if (fi && from_tty)
990 printf_filtered ("(More stack frames follow...)\n");
991}
992
993static void
fba45db2 994backtrace_command (char *arg, int from_tty)
c906108c 995{
c5aa993b
JM
996 struct cleanup *old_chain = (struct cleanup *) NULL;
997 char **argv = (char **) NULL;
998 int argIndicatingFullTrace = (-1), totArgLen = 0, argc = 0;
999 char *argPtr = arg;
c906108c 1000
c5aa993b 1001 if (arg != (char *) NULL)
c906108c
SS
1002 {
1003 int i;
1004
c5aa993b 1005 argv = buildargv (arg);
7a292a7a 1006 old_chain = make_cleanup_freeargv (argv);
c906108c 1007 argc = 0;
c5aa993b
JM
1008 for (i = 0; (argv[i] != (char *) NULL); i++)
1009 {
745b8ca0 1010 unsigned int j;
c5aa993b
JM
1011
1012 for (j = 0; (j < strlen (argv[i])); j++)
1013 argv[i][j] = tolower (argv[i][j]);
1014
1015 if (argIndicatingFullTrace < 0 && subset_compare (argv[i], "full"))
1016 argIndicatingFullTrace = argc;
1017 else
1018 {
1019 argc++;
1020 totArgLen += strlen (argv[i]);
1021 }
1022 }
c906108c
SS
1023 totArgLen += argc;
1024 if (argIndicatingFullTrace >= 0)
c5aa993b
JM
1025 {
1026 if (totArgLen > 0)
1027 {
1028 argPtr = (char *) xmalloc (totArgLen + 1);
1029 if (!argPtr)
1030 nomem (0);
1031 else
1032 {
1033 memset (argPtr, 0, totArgLen + 1);
1034 for (i = 0; (i < (argc + 1)); i++)
1035 {
1036 if (i != argIndicatingFullTrace)
1037 {
1038 strcat (argPtr, argv[i]);
1039 strcat (argPtr, " ");
1040 }
1041 }
1042 }
1043 }
1044 else
1045 argPtr = (char *) NULL;
1046 }
c906108c
SS
1047 }
1048
1049 backtrace_command_1 (argPtr, (argIndicatingFullTrace >= 0), from_tty);
1050
1051 if (argIndicatingFullTrace >= 0 && totArgLen > 0)
b8c9b27d 1052 xfree (argPtr);
c906108c
SS
1053
1054 if (old_chain)
c5aa993b 1055 do_cleanups (old_chain);
c906108c
SS
1056}
1057
a14ed312 1058static void backtrace_full_command (char *arg, int from_tty);
c906108c 1059static void
fba45db2 1060backtrace_full_command (char *arg, int from_tty)
c906108c
SS
1061{
1062 backtrace_command_1 (arg, 1, from_tty);
1063}
c906108c 1064\f
c5aa993b 1065
c906108c
SS
1066/* Print the local variables of a block B active in FRAME.
1067 Return 1 if any variables were printed; 0 otherwise. */
1068
1069static int
fba45db2
KB
1070print_block_frame_locals (struct block *b, register struct frame_info *fi,
1071 int num_tabs, register struct ui_file *stream)
c906108c 1072{
c906108c
SS
1073 register int i, j;
1074 register struct symbol *sym;
1075 register int values_printed = 0;
1076
e88c90f2 1077 ALL_BLOCK_SYMBOLS (b, i, sym)
c906108c 1078 {
c906108c
SS
1079 switch (SYMBOL_CLASS (sym))
1080 {
1081 case LOC_LOCAL:
1082 case LOC_REGISTER:
1083 case LOC_STATIC:
1084 case LOC_BASEREG:
1085 values_printed = 1;
1086 for (j = 0; j < num_tabs; j++)
c5aa993b 1087 fputs_filtered ("\t", stream);
c906108c
SS
1088 fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1089 fputs_filtered (" = ", stream);
1090 print_variable_value (sym, fi, stream);
1091 fprintf_filtered (stream, "\n");
1092 break;
1093
1094 default:
1095 /* Ignore symbols which are not locals. */
1096 break;
1097 }
1098 }
1099 return values_printed;
1100}
1101
1102/* Same, but print labels. */
1103
1104static int
fba45db2
KB
1105print_block_frame_labels (struct block *b, int *have_default,
1106 register struct ui_file *stream)
c906108c 1107{
c906108c
SS
1108 register int i;
1109 register struct symbol *sym;
1110 register int values_printed = 0;
1111
e88c90f2 1112 ALL_BLOCK_SYMBOLS (b, i, sym)
c906108c 1113 {
c906108c
SS
1114 if (STREQ (SYMBOL_NAME (sym), "default"))
1115 {
1116 if (*have_default)
1117 continue;
1118 *have_default = 1;
1119 }
1120 if (SYMBOL_CLASS (sym) == LOC_LABEL)
1121 {
1122 struct symtab_and_line sal;
1123 sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0);
1124 values_printed = 1;
1125 fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1126 if (addressprint)
1127 {
1128 fprintf_filtered (stream, " ");
1129 print_address_numeric (SYMBOL_VALUE_ADDRESS (sym), 1, stream);
1130 }
1131 fprintf_filtered (stream, " in file %s, line %d\n",
1132 sal.symtab->filename, sal.line);
1133 }
1134 }
1135 return values_printed;
1136}
1137
1138/* Print on STREAM all the local variables in frame FRAME,
1139 including all the blocks active in that frame
1140 at its current pc.
1141
1142 Returns 1 if the job was done,
1143 or 0 if nothing was printed because we have no info
1144 on the function running in FRAME. */
1145
1146static void
fba45db2
KB
1147print_frame_local_vars (register struct frame_info *fi, register int num_tabs,
1148 register struct ui_file *stream)
c906108c 1149{
ae767bfb 1150 register struct block *block = get_frame_block (fi, 0);
c906108c
SS
1151 register int values_printed = 0;
1152
1153 if (block == 0)
1154 {
1155 fprintf_filtered (stream, "No symbol table info available.\n");
1156 return;
1157 }
c5aa993b 1158
c906108c
SS
1159 while (block != 0)
1160 {
1161 if (print_block_frame_locals (block, fi, num_tabs, stream))
1162 values_printed = 1;
1163 /* After handling the function's top-level block, stop.
c5aa993b
JM
1164 Don't continue to its superblock, the block of
1165 per-file symbols. */
c906108c
SS
1166 if (BLOCK_FUNCTION (block))
1167 break;
1168 block = BLOCK_SUPERBLOCK (block);
1169 }
1170
1171 if (!values_printed)
1172 {
1173 fprintf_filtered (stream, "No locals.\n");
1174 }
1175}
1176
1177/* Same, but print labels. */
1178
1179static void
fba45db2
KB
1180print_frame_label_vars (register struct frame_info *fi, int this_level_only,
1181 register struct ui_file *stream)
c906108c
SS
1182{
1183 register struct blockvector *bl;
ae767bfb 1184 register struct block *block = get_frame_block (fi, 0);
c906108c
SS
1185 register int values_printed = 0;
1186 int index, have_default = 0;
1187 char *blocks_printed;
bdd78e62 1188 CORE_ADDR pc = get_frame_pc (fi);
c906108c
SS
1189
1190 if (block == 0)
1191 {
1192 fprintf_filtered (stream, "No symbol table info available.\n");
1193 return;
1194 }
1195
1196 bl = blockvector_for_pc (BLOCK_END (block) - 4, &index);
1197 blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1198 memset (blocks_printed, 0, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char));
1199
1200 while (block != 0)
1201 {
1202 CORE_ADDR end = BLOCK_END (block) - 4;
1203 int last_index;
1204
1205 if (bl != blockvector_for_pc (end, &index))
1206 error ("blockvector blotch");
1207 if (BLOCKVECTOR_BLOCK (bl, index) != block)
1208 error ("blockvector botch");
1209 last_index = BLOCKVECTOR_NBLOCKS (bl);
1210 index += 1;
1211
1212 /* Don't print out blocks that have gone by. */
1213 while (index < last_index
1214 && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc)
1215 index++;
1216
1217 while (index < last_index
1218 && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end)
1219 {
1220 if (blocks_printed[index] == 0)
1221 {
1222 if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream))
1223 values_printed = 1;
1224 blocks_printed[index] = 1;
1225 }
1226 index++;
1227 }
1228 if (have_default)
1229 return;
1230 if (values_printed && this_level_only)
1231 return;
1232
1233 /* After handling the function's top-level block, stop.
c5aa993b
JM
1234 Don't continue to its superblock, the block of
1235 per-file symbols. */
c906108c
SS
1236 if (BLOCK_FUNCTION (block))
1237 break;
1238 block = BLOCK_SUPERBLOCK (block);
1239 }
1240
1241 if (!values_printed && !this_level_only)
1242 {
1243 fprintf_filtered (stream, "No catches.\n");
1244 }
1245}
1246
1247/* ARGSUSED */
1248void
fba45db2 1249locals_info (char *args, int from_tty)
c906108c 1250{
6e7f8b9c 1251 if (!deprecated_selected_frame)
c906108c 1252 error ("No frame selected.");
6e7f8b9c 1253 print_frame_local_vars (deprecated_selected_frame, 0, gdb_stdout);
c906108c
SS
1254}
1255
1256static void
fba45db2 1257catch_info (char *ignore, int from_tty)
c906108c 1258{
c5aa993b 1259 struct symtab_and_line *sal;
c906108c 1260
c5aa993b 1261 /* Check for target support for exception handling */
c906108c
SS
1262 sal = target_enable_exception_callback (EX_EVENT_CATCH, 1);
1263 if (sal)
1264 {
1265 /* Currently not handling this */
1266 /* Ideally, here we should interact with the C++ runtime
1267 system to find the list of active handlers, etc. */
1268 fprintf_filtered (gdb_stdout, "Info catch not supported with this target/compiler combination.\n");
1269#if 0
6e7f8b9c 1270 if (!deprecated_selected_frame)
c5aa993b 1271 error ("No frame selected.");
c906108c
SS
1272#endif
1273 }
1274 else
1275 {
c5aa993b 1276 /* Assume g++ compiled code -- old v 4.16 behaviour */
6e7f8b9c 1277 if (!deprecated_selected_frame)
c5aa993b
JM
1278 error ("No frame selected.");
1279
6e7f8b9c 1280 print_frame_label_vars (deprecated_selected_frame, 0, gdb_stdout);
c906108c
SS
1281 }
1282}
1283
1284static void
fba45db2
KB
1285print_frame_arg_vars (register struct frame_info *fi,
1286 register struct ui_file *stream)
c906108c
SS
1287{
1288 struct symbol *func = get_frame_function (fi);
1289 register struct block *b;
c906108c
SS
1290 register int i;
1291 register struct symbol *sym, *sym2;
1292 register int values_printed = 0;
1293
1294 if (func == 0)
1295 {
1296 fprintf_filtered (stream, "No symbol table info available.\n");
1297 return;
1298 }
1299
1300 b = SYMBOL_BLOCK_VALUE (func);
e88c90f2 1301 ALL_BLOCK_SYMBOLS (b, i, sym)
c906108c 1302 {
c906108c
SS
1303 switch (SYMBOL_CLASS (sym))
1304 {
1305 case LOC_ARG:
1306 case LOC_LOCAL_ARG:
1307 case LOC_REF_ARG:
1308 case LOC_REGPARM:
1309 case LOC_REGPARM_ADDR:
1310 case LOC_BASEREG_ARG:
1311 values_printed = 1;
1312 fputs_filtered (SYMBOL_SOURCE_NAME (sym), stream);
1313 fputs_filtered (" = ", stream);
1314
1315 /* We have to look up the symbol because arguments can have
1316 two entries (one a parameter, one a local) and the one we
1317 want is the local, which lookup_symbol will find for us.
1318 This includes gcc1 (not gcc2) on the sparc when passing a
1319 small structure and gcc2 when the argument type is float
1320 and it is passed as a double and converted to float by
1321 the prologue (in the latter case the type of the LOC_ARG
1322 symbol is double and the type of the LOC_LOCAL symbol is
1323 float). There are also LOC_ARG/LOC_REGISTER pairs which
1324 are not combined in symbol-reading. */
1325
1326 sym2 = lookup_symbol (SYMBOL_NAME (sym),
c5aa993b 1327 b, VAR_NAMESPACE, (int *) NULL, (struct symtab **) NULL);
c906108c
SS
1328 print_variable_value (sym2, fi, stream);
1329 fprintf_filtered (stream, "\n");
1330 break;
1331
1332 default:
1333 /* Don't worry about things which aren't arguments. */
1334 break;
1335 }
1336 }
c906108c
SS
1337 if (!values_printed)
1338 {
1339 fprintf_filtered (stream, "No arguments.\n");
1340 }
1341}
1342
1343void
fba45db2 1344args_info (char *ignore, int from_tty)
c906108c 1345{
6e7f8b9c 1346 if (!deprecated_selected_frame)
c906108c 1347 error ("No frame selected.");
6e7f8b9c 1348 print_frame_arg_vars (deprecated_selected_frame, gdb_stdout);
c906108c
SS
1349}
1350
1351
1352static void
fba45db2 1353args_plus_locals_info (char *ignore, int from_tty)
c906108c 1354{
c5aa993b
JM
1355 args_info (ignore, from_tty);
1356 locals_info (ignore, from_tty);
c906108c 1357}
c906108c 1358\f
c5aa993b 1359
0f7d239c
AC
1360/* Select frame FI. Also print the stack frame and show the source if
1361 this is the tui version. */
bedfa57b
AC
1362static void
1363select_and_print_frame (struct frame_info *fi)
c906108c 1364{
0f7d239c 1365 select_frame (fi);
c906108c
SS
1366 if (fi)
1367 {
bedfa57b 1368 print_stack_frame (fi, frame_relative_level (fi), 1);
c906108c
SS
1369 }
1370}
c906108c 1371\f
c906108c 1372/* Return the symbol-block in which the selected frame is executing.
ae767bfb
JB
1373 Can return zero under various legitimate circumstances.
1374
1375 If ADDR_IN_BLOCK is non-zero, set *ADDR_IN_BLOCK to the relevant
1376 code address within the block returned. We use this to decide
1377 which macros are in scope. */
c906108c
SS
1378
1379struct block *
ae767bfb 1380get_selected_block (CORE_ADDR *addr_in_block)
c906108c
SS
1381{
1382 if (!target_has_stack)
1383 return 0;
1384
8c69fc49
AC
1385 /* NOTE: cagney/2002-11-28: Why go to all this effort to not create
1386 a selected/current frame? Perhaphs this function is called,
1387 indirectly, by WFI in "infrun.c" where avoiding the creation of
1388 an inner most frame is very important (it slows down single
1389 step). I suspect, though that this was true in the deep dark
1390 past but is no longer the case. A mindless look at all the
1391 callers tends to support this theory. I think we should be able
1392 to assume that there is always a selcted frame. */
6e7f8b9c
AC
1393 /* gdb_assert (deprecated_selected_frame != NULL); So, do you feel
1394 lucky? */
1395 if (!deprecated_selected_frame)
8c69fc49
AC
1396 {
1397 CORE_ADDR pc = read_pc ();
1398 if (addr_in_block != NULL)
1399 *addr_in_block = pc;
1400 return block_for_pc (pc);
1401 }
6e7f8b9c 1402 return get_frame_block (deprecated_selected_frame, addr_in_block);
c906108c
SS
1403}
1404
1405/* Find a frame a certain number of levels away from FRAME.
1406 LEVEL_OFFSET_PTR points to an int containing the number of levels.
1407 Positive means go to earlier frames (up); negative, the reverse.
1408 The int that contains the number of levels is counted toward
1409 zero as the frames for those levels are found.
1410 If the top or bottom frame is reached, that frame is returned,
1411 but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
1412 how much farther the original request asked to go. */
1413
1414struct frame_info *
fba45db2
KB
1415find_relative_frame (register struct frame_info *frame,
1416 register int *level_offset_ptr)
c906108c
SS
1417{
1418 register struct frame_info *prev;
1419 register struct frame_info *frame1;
1420
1421 /* Going up is simple: just do get_prev_frame enough times
1422 or until initial frame is reached. */
1423 while (*level_offset_ptr > 0)
1424 {
1425 prev = get_prev_frame (frame);
1426 if (prev == 0)
1427 break;
1428 (*level_offset_ptr)--;
1429 frame = prev;
1430 }
1431 /* Going down is just as simple. */
1432 if (*level_offset_ptr < 0)
1433 {
c5aa993b
JM
1434 while (*level_offset_ptr < 0)
1435 {
1436 frame1 = get_next_frame (frame);
1437 if (!frame1)
1438 break;
1439 frame = frame1;
1440 (*level_offset_ptr)++;
1441 }
c906108c
SS
1442 }
1443 return frame;
1444}
1445
1446/* The "select_frame" command. With no arg, NOP.
1447 With arg LEVEL_EXP, select the frame at level LEVEL if it is a
1448 valid level. Otherwise, treat level_exp as an address expression
1449 and select it. See parse_frame_specification for more info on proper
1450 frame expressions. */
1451
1452/* ARGSUSED */
8b93c638 1453void
fba45db2 1454select_frame_command_wrapper (char *level_exp, int from_tty)
8b93c638
JM
1455{
1456 select_frame_command (level_exp, from_tty);
1457}
8926118c 1458
c906108c 1459static void
fba45db2 1460select_frame_command (char *level_exp, int from_tty)
c906108c 1461{
bfb3754e 1462 struct frame_info *frame;
6e7f8b9c 1463 int level = frame_relative_level (deprecated_selected_frame);
c906108c
SS
1464
1465 if (!target_has_stack)
1466 error ("No stack.");
1467
1468 frame = parse_frame_specification (level_exp);
1469
0f7d239c 1470 select_frame (frame);
6e7f8b9c
AC
1471 if (level != frame_relative_level (deprecated_selected_frame))
1472 selected_frame_level_changed_event (frame_relative_level (deprecated_selected_frame));
c906108c
SS
1473}
1474
1475/* The "frame" command. With no arg, print selected frame briefly.
1476 With arg, behaves like select_frame and then prints the selected
1477 frame. */
1478
1479void
fba45db2 1480frame_command (char *level_exp, int from_tty)
c906108c
SS
1481{
1482 select_frame_command (level_exp, from_tty);
7789c6f5
EZ
1483 print_stack_frame (deprecated_selected_frame,
1484 frame_relative_level (deprecated_selected_frame), 1);
c906108c
SS
1485}
1486
1487/* The XDB Compatibility command to print the current frame. */
1488
7a292a7a 1489static void
fba45db2 1490current_frame_command (char *level_exp, int from_tty)
c906108c 1491{
6e7f8b9c 1492 if (target_has_stack == 0 || deprecated_selected_frame == 0)
c5aa993b 1493 error ("No stack.");
7789c6f5 1494 print_stack_frame (deprecated_selected_frame,
6e7f8b9c 1495 frame_relative_level (deprecated_selected_frame), 1);
7a292a7a 1496}
c906108c
SS
1497
1498/* Select the frame up one or COUNT stack levels
1499 from the previously selected frame, and print it briefly. */
1500
1501/* ARGSUSED */
1502static void
fba45db2 1503up_silently_base (char *count_exp)
c906108c
SS
1504{
1505 register struct frame_info *fi;
1506 int count = 1, count1;
1507 if (count_exp)
bb518678 1508 count = parse_and_eval_long (count_exp);
c906108c 1509 count1 = count;
c5aa993b 1510
6e7f8b9c 1511 if (target_has_stack == 0 || deprecated_selected_frame == 0)
c906108c
SS
1512 error ("No stack.");
1513
6e7f8b9c 1514 fi = find_relative_frame (deprecated_selected_frame, &count1);
c906108c
SS
1515 if (count1 != 0 && count_exp == 0)
1516 error ("Initial frame selected; you cannot go up.");
0f7d239c 1517 select_frame (fi);
6e7f8b9c 1518 selected_frame_level_changed_event (frame_relative_level (deprecated_selected_frame));
c906108c
SS
1519}
1520
1521static void
fba45db2 1522up_silently_command (char *count_exp, int from_tty)
c906108c 1523{
c5aa993b 1524 up_silently_base (count_exp);
c906108c
SS
1525}
1526
1527static void
fba45db2 1528up_command (char *count_exp, int from_tty)
c906108c
SS
1529{
1530 up_silently_base (count_exp);
7789c6f5
EZ
1531 print_stack_frame (deprecated_selected_frame,
1532 frame_relative_level (deprecated_selected_frame), 1);
c906108c
SS
1533}
1534
1535/* Select the frame down one or COUNT stack levels
1536 from the previously selected frame, and print it briefly. */
1537
1538/* ARGSUSED */
1539static void
fba45db2 1540down_silently_base (char *count_exp)
c906108c
SS
1541{
1542 register struct frame_info *frame;
1543 int count = -1, count1;
1544 if (count_exp)
bb518678 1545 count = -parse_and_eval_long (count_exp);
c906108c 1546 count1 = count;
c5aa993b 1547
6e7f8b9c 1548 if (target_has_stack == 0 || deprecated_selected_frame == 0)
c906108c
SS
1549 error ("No stack.");
1550
6e7f8b9c 1551 frame = find_relative_frame (deprecated_selected_frame, &count1);
c906108c
SS
1552 if (count1 != 0 && count_exp == 0)
1553 {
1554
1555 /* We only do this if count_exp is not specified. That way "down"
c5aa993b
JM
1556 means to really go down (and let me know if that is
1557 impossible), but "down 9999" can be used to mean go all the way
1558 down without getting an error. */
c906108c
SS
1559
1560 error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
1561 }
1562
0f7d239c 1563 select_frame (frame);
6e7f8b9c 1564 selected_frame_level_changed_event (frame_relative_level (deprecated_selected_frame));
c906108c
SS
1565}
1566
1567/* ARGSUSED */
1568static void
fba45db2 1569down_silently_command (char *count_exp, int from_tty)
c906108c
SS
1570{
1571 down_silently_base (count_exp);
c906108c
SS
1572}
1573
1574static void
fba45db2 1575down_command (char *count_exp, int from_tty)
c906108c
SS
1576{
1577 down_silently_base (count_exp);
7789c6f5
EZ
1578 print_stack_frame (deprecated_selected_frame,
1579 frame_relative_level (deprecated_selected_frame), 1);
c906108c
SS
1580}
1581\f
8b93c638 1582void
fba45db2 1583return_command (char *retval_exp, int from_tty)
c906108c
SS
1584{
1585 struct symbol *thisfun;
1586 CORE_ADDR selected_frame_addr;
1587 CORE_ADDR selected_frame_pc;
1588 struct frame_info *frame;
3d6d86c6 1589 struct value *return_value = NULL;
c906108c 1590
6e7f8b9c 1591 if (deprecated_selected_frame == NULL)
c906108c 1592 error ("No selected frame.");
6e7f8b9c
AC
1593 thisfun = get_frame_function (deprecated_selected_frame);
1594 selected_frame_addr = get_frame_base (deprecated_selected_frame);
bdd78e62 1595 selected_frame_pc = get_frame_pc (deprecated_selected_frame);
c906108c
SS
1596
1597 /* Compute the return value (if any -- possibly getting errors here). */
1598
1599 if (retval_exp)
1600 {
1601 struct type *return_type = NULL;
1602
1603 return_value = parse_and_eval (retval_exp);
1604
1605 /* Cast return value to the return type of the function. */
1606 if (thisfun != NULL)
1607 return_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (thisfun));
1608 if (return_type == NULL)
1609 return_type = builtin_type_int;
1610 return_value = value_cast (return_type, return_value);
1611
1612 /* Make sure we have fully evaluated it, since
c5aa993b 1613 it might live in the stack frame we're about to pop. */
c906108c
SS
1614 if (VALUE_LAZY (return_value))
1615 value_fetch_lazy (return_value);
1616 }
1617
1618 /* If interactive, require confirmation. */
1619
1620 if (from_tty)
1621 {
1622 if (thisfun != 0)
1623 {
1624 if (!query ("Make %s return now? ", SYMBOL_SOURCE_NAME (thisfun)))
1625 {
1626 error ("Not confirmed.");
1627 /* NOTREACHED */
1628 }
1629 }
c5aa993b
JM
1630 else if (!query ("Make selected stack frame return now? "))
1631 error ("Not confirmed.");
c906108c
SS
1632 }
1633
dbe9fe58
AC
1634 /* FIXME: cagney/2003-01-18: Rather than pop each frame in turn,
1635 this code should just go straight to the relevant frame and pop
1636 that. */
1637
c906108c 1638 /* Do the real work. Pop until the specified frame is current. We
6e7f8b9c 1639 use this method because the deprecated_selected_frame is not valid after
c906108c
SS
1640 a POP_FRAME. The pc comparison makes this work even if the
1641 selected frame shares its fp with another frame. */
1642
8b36eed8 1643 while (selected_frame_addr != get_frame_base (frame = get_current_frame ())
bdd78e62 1644 || selected_frame_pc != get_frame_pc (frame))
dbe9fe58 1645 frame_pop (get_current_frame ());
c906108c
SS
1646
1647 /* Then pop that frame. */
1648
dbe9fe58 1649 frame_pop (get_current_frame ());
c906108c
SS
1650
1651 /* Compute the return value (if any) and store in the place
1652 for return values. */
1653
1654 if (retval_exp)
1655 set_return_value (return_value);
1656
1a2aab69
PS
1657 /* If we are at the end of a call dummy now, pop the dummy frame too. */
1658
dbe9fe58
AC
1659 /* FIXME: cagney/2003-01-18: This is silly. Instead of popping all
1660 the frames except the dummy, and then, as an afterthought,
1661 popping the dummy frame, this code should just pop through to the
1662 dummy frame. */
1663
1a2aab69 1664 if (CALL_DUMMY_HAS_COMPLETED (read_pc(), read_sp (),
c193f6ac 1665 get_frame_base (get_current_frame ())))
dbe9fe58 1666 frame_pop (get_current_frame ());
1a2aab69 1667
c906108c
SS
1668 /* If interactive, print the frame that is now current. */
1669
1670 if (from_tty)
1671 frame_command ("0", 1);
1672 else
1673 select_frame_command ("0", 0);
1674}
1675
1676/* Sets the scope to input function name, provided that the
1677 function is within the current stack frame */
1678
1679struct function_bounds
1680{
1681 CORE_ADDR low, high;
1682};
1683
a14ed312 1684static void func_command (char *arg, int from_tty);
c906108c 1685static void
fba45db2 1686func_command (char *arg, int from_tty)
c906108c
SS
1687{
1688 struct frame_info *fp;
1689 int found = 0;
1690 struct symtabs_and_lines sals;
1691 int i;
1692 int level = 1;
1693 struct function_bounds *func_bounds = (struct function_bounds *) NULL;
1694
1695 if (arg != (char *) NULL)
1696 return;
1697
1698 fp = parse_frame_specification ("0");
1699 sals = decode_line_spec (arg, 1);
1700 func_bounds = (struct function_bounds *) xmalloc (
1701 sizeof (struct function_bounds) * sals.nelts);
1702 for (i = 0; (i < sals.nelts && !found); i++)
1703 {
1704 if (sals.sals[i].pc == (CORE_ADDR) 0 ||
1705 find_pc_partial_function (sals.sals[i].pc,
1706 (char **) NULL,
1707 &func_bounds[i].low,
1708 &func_bounds[i].high) == 0)
1709 {
1710 func_bounds[i].low =
1711 func_bounds[i].high = (CORE_ADDR) NULL;
1712 }
1713 }
1714
1715 do
1716 {
1717 for (i = 0; (i < sals.nelts && !found); i++)
bdd78e62
AC
1718 found = (get_frame_pc (fp) >= func_bounds[i].low &&
1719 get_frame_pc (fp) < func_bounds[i].high);
c906108c
SS
1720 if (!found)
1721 {
1722 level = 1;
1723 fp = find_relative_frame (fp, &level);
1724 }
1725 }
1726 while (!found && level == 0);
1727
1728 if (func_bounds)
b8c9b27d 1729 xfree (func_bounds);
c906108c
SS
1730
1731 if (!found)
1732 printf_filtered ("'%s' not within current stack frame.\n", arg);
6e7f8b9c 1733 else if (fp != deprecated_selected_frame)
bedfa57b 1734 select_and_print_frame (fp);
c906108c
SS
1735}
1736
1737/* Gets the language of the current frame. */
1738
1739enum language
fba45db2 1740get_frame_language (void)
c906108c
SS
1741{
1742 register struct symtab *s;
1743 enum language flang; /* The language of the current frame */
c5aa993b 1744
6e7f8b9c 1745 if (deprecated_selected_frame)
c906108c 1746 {
bdd78e62 1747 s = find_pc_symtab (get_frame_pc (deprecated_selected_frame));
c906108c
SS
1748 if (s)
1749 flang = s->language;
1750 else
1751 flang = language_unknown;
1752 }
1753 else
1754 flang = language_unknown;
1755
1756 return flang;
1757}
1758\f
1759void
fba45db2 1760_initialize_stack (void)
c906108c 1761{
c5aa993b 1762#if 0
c906108c
SS
1763 backtrace_limit = 30;
1764#endif
1765
1766 add_com ("return", class_stack, return_command,
1767 "Make selected stack frame return to its caller.\n\
1768Control remains in the debugger, but when you continue\n\
1769execution will resume in the frame above the one now selected.\n\
1770If an argument is given, it is an expression for the value to return.");
1771
1772 add_com ("up", class_stack, up_command,
1773 "Select and print stack frame that called this one.\n\
1774An argument says how many frames up to go.");
1775 add_com ("up-silently", class_support, up_silently_command,
1776 "Same as the `up' command, but does not print anything.\n\
1777This is useful in command scripts.");
1778
1779 add_com ("down", class_stack, down_command,
1780 "Select and print stack frame called by this one.\n\
1781An argument says how many frames down to go.");
1782 add_com_alias ("do", "down", class_stack, 1);
1783 add_com_alias ("dow", "down", class_stack, 1);
1784 add_com ("down-silently", class_support, down_silently_command,
1785 "Same as the `down' command, but does not print anything.\n\
1786This is useful in command scripts.");
1787
1788 add_com ("frame", class_stack, frame_command,
1789 "Select and print a stack frame.\n\
1790With no argument, print the selected stack frame. (See also \"info frame\").\n\
1791An argument specifies the frame to select.\n\
1792It can be a stack frame number or the address of the frame.\n\
1793With argument, nothing is printed if input is coming from\n\
1794a command file or a user-defined command.");
1795
1796 add_com_alias ("f", "frame", class_stack, 1);
1797
1798 if (xdb_commands)
1799 {
c5aa993b
JM
1800 add_com ("L", class_stack, current_frame_command,
1801 "Print the current stack frame.\n");
c906108c
SS
1802 add_com_alias ("V", "frame", class_stack, 1);
1803 }
1804 add_com ("select-frame", class_stack, select_frame_command,
1805 "Select a stack frame without printing anything.\n\
1806An argument specifies the frame to select.\n\
1807It can be a stack frame number or the address of the frame.\n");
1808
1809 add_com ("backtrace", class_stack, backtrace_command,
1810 "Print backtrace of all stack frames, or innermost COUNT frames.\n\
1811With a negative argument, print outermost -COUNT frames.\n\
1812Use of the 'full' qualifier also prints the values of the local variables.\n");
1813 add_com_alias ("bt", "backtrace", class_stack, 0);
1814 if (xdb_commands)
1815 {
1816 add_com_alias ("t", "backtrace", class_stack, 0);
1817 add_com ("T", class_stack, backtrace_full_command,
c5aa993b 1818 "Print backtrace of all stack frames, or innermost COUNT frames \n\
c906108c
SS
1819and the values of the local variables.\n\
1820With a negative argument, print outermost -COUNT frames.\n\
1821Usage: T <count>\n");
1822 }
1823
1824 add_com_alias ("where", "backtrace", class_alias, 0);
1825 add_info ("stack", backtrace_command,
1826 "Backtrace of the stack, or innermost COUNT frames.");
1827 add_info_alias ("s", "stack", 1);
1828 add_info ("frame", frame_info,
1829 "All about selected stack frame, or frame at ADDR.");
1830 add_info_alias ("f", "frame", 1);
1831 add_info ("locals", locals_info,
1832 "Local variables of current stack frame.");
1833 add_info ("args", args_info,
1834 "Argument variables of current stack frame.");
1835 if (xdb_commands)
c5aa993b
JM
1836 add_com ("l", class_info, args_plus_locals_info,
1837 "Argument and local variables of current stack frame.");
c906108c
SS
1838
1839 if (dbx_commands)
c5aa993b
JM
1840 add_com ("func", class_stack, func_command,
1841 "Select the stack frame that contains <func>.\nUsage: func <name>\n");
c906108c
SS
1842
1843 add_info ("catch", catch_info,
1844 "Exceptions that can be caught in the current stack frame.");
1845
1846#if 0
c5aa993b
JM
1847 add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command,
1848 "Specify maximum number of frames for \"backtrace\" to print by default.",
c906108c
SS
1849 &setlist);
1850 add_info ("backtrace-limit", backtrace_limit_info,
c5aa993b 1851 "The maximum number of frames for \"backtrace\" to print by default.");
c906108c
SS
1852#endif
1853}
This page took 0.490388 seconds and 4 git commands to generate.