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