Commit | Line | Data |
---|---|---|
bd5635a1 | 1 | /* Print and select stack frames for GDB, the GNU debugger. |
89e0bbcd | 2 | Copyright (C) 1986, 1987, 1989, 1991 Free Software Foundation, Inc. |
bd5635a1 RP |
3 | |
4 | This file is part of GDB. | |
5 | ||
cadbb07a | 6 | This program is free software; you can redistribute it and/or modify |
bd5635a1 | 7 | it under the terms of the GNU General Public License as published by |
cadbb07a JG |
8 | the Free Software Foundation; either version 2 of the License, or |
9 | (at your option) any later version. | |
bd5635a1 | 10 | |
cadbb07a | 11 | This program is distributed in the hope that it will be useful, |
bd5635a1 RP |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
cadbb07a JG |
17 | along with this program; if not, write to the Free Software |
18 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
bd5635a1 RP |
19 | |
20 | #include <stdio.h> | |
21 | ||
22 | #include "defs.h" | |
23 | #include "param.h" | |
89e0bbcd | 24 | #include "language.h" |
bd5635a1 RP |
25 | #include "symtab.h" |
26 | #include "frame.h" | |
27 | #include "gdbcmd.h" | |
28 | #include "value.h" | |
29 | #include "gdbcore.h" | |
30 | #include "target.h" | |
31 | #include "breakpoint.h" | |
32 | ||
33 | extern int addressprint; /* Print addresses, or stay symbolic only? */ | |
34 | extern int info_verbose; /* Verbosity of symbol reading msgs */ | |
d8ce1326 | 35 | extern int lines_to_list; /* # of lines "list" command shows by default */ |
bd5635a1 RP |
36 | extern char *reg_names[]; /* Names of registers */ |
37 | ||
38 | /* Thie "selected" stack frame is used by default for local and arg access. | |
39 | May be zero, for no selected frame. */ | |
40 | ||
41 | FRAME selected_frame; | |
42 | ||
43 | /* Level of the selected frame: | |
44 | 0 for innermost, 1 for its caller, ... | |
45 | or -1 for frame specified by address with no defined level. */ | |
46 | ||
47 | int selected_frame_level; | |
48 | ||
49 | /* Nonzero means print the full filename and linenumber | |
50 | when a frame is printed, and do so in a format programs can parse. */ | |
51 | ||
52 | int frame_file_full_name = 0; | |
53 | ||
54 | void print_frame_info (); | |
55 | \f | |
56 | /* Print a stack frame briefly. FRAME should be the frame id | |
57 | and LEVEL should be its level in the stack (or -1 for level not defined). | |
58 | This prints the level, the function executing, the arguments, | |
59 | and the file name and line number. | |
60 | If the pc is not at the beginning of the source line, | |
61 | the actual pc is printed at the beginning. | |
62 | ||
63 | If SOURCE is 1, print the source line as well. | |
64 | If SOURCE is -1, print ONLY the source line. */ | |
65 | ||
cadbb07a | 66 | void |
bd5635a1 RP |
67 | print_stack_frame (frame, level, source) |
68 | FRAME frame; | |
69 | int level; | |
70 | int source; | |
71 | { | |
72 | struct frame_info *fi; | |
73 | ||
74 | fi = get_frame_info (frame); | |
75 | ||
76 | print_frame_info (fi, level, source, 1); | |
77 | } | |
78 | ||
79 | void | |
80 | print_frame_info (fi, level, source, args) | |
81 | struct frame_info *fi; | |
82 | register int level; | |
83 | int source; | |
84 | int args; | |
85 | { | |
86 | struct symtab_and_line sal; | |
87 | struct symbol *func; | |
88 | register char *funname = 0; | |
89 | int numargs; | |
90 | ||
91 | #if 0 /* Symbol reading is fast enough now */ | |
92 | struct partial_symtab *pst; | |
93 | ||
94 | /* Don't give very much information if we haven't readin the | |
95 | symbol table yet. */ | |
96 | pst = find_pc_psymtab (fi->pc); | |
97 | if (pst && !pst->readin) | |
98 | { | |
99 | /* Abbreviated information. */ | |
100 | char *fname; | |
101 | ||
102 | if (!find_pc_partial_function (fi->pc, &fname, 0)) | |
103 | fname = "??"; | |
104 | ||
105 | printf_filtered ("#%-2d ", level); | |
106 | if (addressprint) | |
89e0bbcd | 107 | printf_filtered ("%s in ", local_hex_string(fi->pc)); |
bd5635a1 RP |
108 | |
109 | fputs_demangled (fname, stdout, -1); | |
110 | fputs_filtered (" (...)\n", stdout); | |
111 | ||
112 | return; | |
113 | } | |
114 | #endif | |
115 | ||
116 | sal = find_pc_line (fi->pc, fi->next_frame); | |
117 | func = find_pc_function (fi->pc); | |
118 | if (func) | |
119 | { | |
120 | /* In certain pathological cases, the symtabs give the wrong | |
121 | function (when we are in the first function in a file which | |
122 | is compiled without debugging symbols, the previous function | |
123 | is compiled with debugging symbols, and the "foo.o" symbol | |
124 | that is supposed to tell us where the file with debugging symbols | |
125 | ends has been truncated by ar because it is longer than 15 | |
126 | characters). | |
127 | ||
128 | So look in the misc_function_vector as well, and if it comes | |
129 | up with a larger address for the function use that instead. | |
130 | I don't think this can ever cause any problems; | |
131 | there shouldn't be any | |
132 | misc_function_vector symbols in the middle of a function. */ | |
133 | int misc_index = find_pc_misc_function (fi->pc); | |
134 | if (misc_index >= 0 | |
135 | && (misc_function_vector[misc_index].address | |
136 | > BLOCK_START (SYMBOL_BLOCK_VALUE (func)))) | |
137 | { | |
138 | /* In this case we have no way of knowing the source file | |
139 | and line number, so don't print them. */ | |
140 | sal.symtab = 0; | |
141 | /* We also don't know anything about the function besides | |
142 | its address and name. */ | |
143 | func = 0; | |
144 | funname = misc_function_vector[misc_index].name; | |
145 | } | |
146 | else | |
147 | funname = SYMBOL_NAME (func); | |
148 | } | |
149 | else | |
150 | { | |
151 | register int misc_index = find_pc_misc_function (fi->pc); | |
152 | if (misc_index >= 0) | |
153 | funname = misc_function_vector[misc_index].name; | |
154 | } | |
155 | ||
156 | if (source >= 0 || !sal.symtab) | |
157 | { | |
158 | if (level >= 0) | |
159 | printf_filtered ("#%-2d ", level); | |
160 | if (addressprint) | |
161 | if (fi->pc != sal.pc || !sal.symtab) | |
89e0bbcd | 162 | printf_filtered ("%s in ", local_hex_string(fi->pc)); |
bd5635a1 RP |
163 | fputs_demangled (funname ? funname : "??", stdout, -1); |
164 | wrap_here (" "); | |
165 | fputs_filtered (" (", stdout); | |
166 | if (args) | |
167 | { | |
168 | FRAME_NUM_ARGS (numargs, fi); | |
169 | print_frame_args (func, fi, numargs, stdout); | |
170 | } | |
171 | printf_filtered (")"); | |
172 | if (sal.symtab && sal.symtab->filename) | |
173 | { | |
174 | wrap_here (" "); | |
175 | printf_filtered (" at %s:%d", sal.symtab->filename, sal.line); | |
176 | } | |
177 | printf_filtered ("\n"); | |
178 | } | |
179 | ||
180 | if ((source != 0) && sal.symtab) | |
181 | { | |
182 | int done = 0; | |
183 | int mid_statement = source < 0 && fi->pc != sal.pc; | |
184 | if (frame_file_full_name) | |
185 | done = identify_source_line (sal.symtab, sal.line, mid_statement); | |
186 | if (!done) | |
187 | { | |
188 | if (addressprint && mid_statement) | |
89e0bbcd | 189 | printf_filtered ("%s\t", local_hex_string(fi->pc)); |
bd5635a1 RP |
190 | print_source_lines (sal.symtab, sal.line, sal.line + 1, 0); |
191 | } | |
cadbb07a | 192 | current_source_line = max (sal.line - lines_to_list/2, 1); |
bd5635a1 RP |
193 | } |
194 | if (source != 0) | |
195 | set_default_breakpoint (1, fi->pc, sal.symtab, sal.line); | |
196 | ||
197 | fflush (stdout); | |
198 | } | |
199 | ||
bd5635a1 RP |
200 | void flush_cached_frames (); |
201 | ||
202 | #ifdef FRAME_SPECIFICATION_DYADIC | |
203 | extern FRAME setup_arbitrary_frame (); | |
204 | #endif | |
205 | ||
206 | /* | |
207 | * Read a frame specification in whatever the appropriate format is. | |
777bef06 JK |
208 | * Call error() if the specification is in any way invalid (i.e. |
209 | * this function never returns NULL). | |
bd5635a1 RP |
210 | */ |
211 | static FRAME | |
212 | parse_frame_specification (frame_exp) | |
213 | char *frame_exp; | |
214 | { | |
215 | int numargs = 0; | |
216 | int arg1, arg2; | |
217 | ||
218 | if (frame_exp) | |
219 | { | |
220 | char *addr_string, *p; | |
221 | struct cleanup *tmp_cleanup; | |
222 | ||
223 | while (*frame_exp == ' ') frame_exp++; | |
224 | for (p = frame_exp; *p && *p != ' '; p++) | |
225 | ; | |
226 | ||
227 | if (*frame_exp) | |
228 | { | |
229 | numargs = 1; | |
230 | addr_string = savestring(frame_exp, p - frame_exp); | |
231 | ||
232 | { | |
233 | tmp_cleanup = make_cleanup (free, addr_string); | |
234 | arg1 = parse_and_eval_address (addr_string); | |
235 | do_cleanups (tmp_cleanup); | |
236 | } | |
237 | ||
238 | while (*p == ' ') p++; | |
239 | ||
240 | if (*p) | |
241 | { | |
242 | numargs = 2; | |
243 | arg2 = parse_and_eval_address (p); | |
244 | } | |
245 | } | |
246 | } | |
247 | ||
248 | switch (numargs) | |
249 | { | |
250 | case 0: | |
777bef06 JK |
251 | if (selected_frame == NULL) |
252 | error ("No selected frame."); | |
bd5635a1 RP |
253 | return selected_frame; |
254 | /* NOTREACHED */ | |
255 | case 1: | |
256 | { | |
257 | int level = arg1; | |
258 | FRAME fid = find_relative_frame (get_current_frame (), &level); | |
259 | FRAME tfid; | |
260 | ||
261 | if (level == 0) | |
262 | /* find_relative_frame was successful */ | |
263 | return fid; | |
264 | ||
265 | /* If (s)he specifies the frame with an address, he deserves what | |
266 | (s)he gets. Still, give the highest one that matches. */ | |
267 | ||
268 | for (fid = get_current_frame (); | |
269 | fid && FRAME_FP (fid) != arg1; | |
270 | fid = get_prev_frame (fid)) | |
271 | ; | |
272 | ||
273 | if (fid) | |
274 | while ((tfid = get_prev_frame (fid)) && | |
275 | (FRAME_FP (tfid) == arg1)) | |
276 | fid = tfid; | |
277 | ||
278 | #ifdef FRAME_SPECIFICATION_DYADIC | |
279 | if (!fid) | |
280 | error ("Incorrect number of args in frame specification"); | |
281 | ||
282 | return fid; | |
283 | #else | |
284 | return create_new_frame (arg1, 0); | |
285 | #endif | |
286 | } | |
287 | /* NOTREACHED */ | |
288 | case 2: | |
289 | /* Must be addresses */ | |
290 | #ifndef FRAME_SPECIFICATION_DYADIC | |
291 | error ("Incorrect number of args in frame specification"); | |
292 | #else | |
293 | return setup_arbitrary_frame (arg1, arg2); | |
294 | #endif | |
295 | /* NOTREACHED */ | |
296 | } | |
297 | fatal ("Internal: Error in parsing in parse_frame_specification"); | |
298 | /* NOTREACHED */ | |
299 | } | |
300 | ||
301 | /* FRAME_ARGS_ADDRESS_CORRECT is just like FRAME_ARGS_ADDRESS except | |
302 | that if it is unsure about the answer, it returns 0 | |
303 | instead of guessing (this happens on the VAX and i960, for example). | |
304 | ||
305 | On most machines, we never have to guess about the args address, | |
306 | so FRAME_ARGS_ADDRESS{,_CORRECT} are the same. */ | |
307 | #if !defined (FRAME_ARGS_ADDRESS_CORRECT) | |
308 | #define FRAME_ARGS_ADDRESS_CORRECT FRAME_ARGS_ADDRESS | |
309 | #endif | |
310 | ||
311 | /* Print verbosely the selected frame or the frame at address ADDR. | |
312 | This means absolutely all information in the frame is printed. */ | |
313 | ||
314 | static void | |
315 | frame_info (addr_exp) | |
316 | char *addr_exp; | |
317 | { | |
318 | FRAME frame; | |
319 | struct frame_info *fi; | |
320 | struct frame_saved_regs fsr; | |
321 | struct symtab_and_line sal; | |
322 | struct symbol *func; | |
89e0bbcd | 323 | struct symtab *s; |
bd5635a1 RP |
324 | FRAME calling_frame; |
325 | int i, count; | |
326 | char *funname = 0; | |
327 | ||
328 | if (!target_has_stack) | |
329 | error ("No inferior or core file."); | |
330 | ||
331 | frame = parse_frame_specification (addr_exp); | |
332 | if (!frame) | |
333 | error ("Invalid frame specified."); | |
334 | ||
335 | fi = get_frame_info (frame); | |
336 | sal = find_pc_line (fi->pc, fi->next_frame); | |
337 | func = get_frame_function (frame); | |
89e0bbcd | 338 | s = find_pc_symtab(fi->pc); |
bd5635a1 RP |
339 | if (func) |
340 | funname = SYMBOL_NAME (func); | |
341 | else | |
342 | { | |
343 | register int misc_index = find_pc_misc_function (fi->pc); | |
344 | if (misc_index >= 0) | |
345 | funname = misc_function_vector[misc_index].name; | |
346 | } | |
347 | calling_frame = get_prev_frame (frame); | |
348 | ||
89e0bbcd JG |
349 | if (!addr_exp && selected_frame_level >= 0) { |
350 | printf_filtered ("Stack level %d, frame at %s:\n", | |
351 | selected_frame_level, | |
352 | local_hex_string(FRAME_FP(frame))); | |
353 | } else { | |
354 | printf_filtered ("Stack frame at %s:\n", | |
355 | local_hex_string(FRAME_FP(frame))); | |
356 | } | |
357 | printf_filtered (" %s = %s", | |
358 | reg_names[PC_REGNUM], | |
359 | local_hex_string(fi->pc)); | |
bd5635a1 RP |
360 | |
361 | wrap_here (" "); | |
362 | if (funname) | |
363 | printf_filtered (" in %s", funname); | |
364 | wrap_here (" "); | |
365 | if (sal.symtab) | |
366 | printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line); | |
367 | puts_filtered ("; "); | |
368 | wrap_here (" "); | |
89e0bbcd JG |
369 | printf_filtered ("saved %s %s\n", reg_names[PC_REGNUM], |
370 | local_hex_string(FRAME_SAVED_PC (frame))); | |
bd5635a1 | 371 | if (calling_frame) |
89e0bbcd JG |
372 | printf_filtered (" called by frame at %s", |
373 | local_hex_string(FRAME_FP (calling_frame))); | |
bd5635a1 RP |
374 | if (fi->next_frame && calling_frame) |
375 | puts_filtered (","); | |
376 | wrap_here (" "); | |
377 | if (fi->next_frame) | |
89e0bbcd | 378 | printf_filtered (" caller of frame at %s", local_hex_string(fi->next_frame)); |
bd5635a1 RP |
379 | if (fi->next_frame || calling_frame) |
380 | puts_filtered ("\n"); | |
89e0bbcd JG |
381 | if (s) |
382 | printf_filtered(" source language %s.\n", language_str(s->language)); | |
bd5635a1 RP |
383 | |
384 | { | |
385 | /* Address of the argument list for this frame, or 0. */ | |
386 | CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi); | |
387 | /* Number of args for this frame, or -1 if unknown. */ | |
388 | int numargs; | |
389 | ||
390 | if (arg_list == 0) | |
391 | printf_filtered (" Arglist at unknown address.\n"); | |
392 | else | |
393 | { | |
89e0bbcd | 394 | printf_filtered (" Arglist at %s,", local_hex_string(arg_list)); |
bd5635a1 RP |
395 | |
396 | FRAME_NUM_ARGS (numargs, fi); | |
397 | if (numargs < 0) | |
398 | puts_filtered (" args: "); | |
399 | else if (numargs == 0) | |
400 | puts_filtered (" no args."); | |
401 | else if (numargs == 1) | |
402 | puts_filtered (" 1 arg: "); | |
403 | else | |
404 | printf_filtered (" %d args: ", numargs); | |
405 | print_frame_args (func, fi, numargs, stdout); | |
406 | puts_filtered ("\n"); | |
407 | } | |
408 | } | |
409 | ||
410 | #if defined (FRAME_FIND_SAVED_REGS) | |
411 | get_frame_saved_regs (fi, &fsr); | |
412 | /* The sp is special; what's returned isn't the save address, but | |
413 | actually the value of the previous frame's sp. */ | |
89e0bbcd JG |
414 | printf_filtered (" Previous frame's sp is %s\n", |
415 | local_hex_string(fsr.regs[SP_REGNUM])); | |
bd5635a1 RP |
416 | count = 0; |
417 | for (i = 0; i < NUM_REGS; i++) | |
418 | if (fsr.regs[i] && i != SP_REGNUM) | |
419 | { | |
420 | if (count == 0) | |
421 | puts_filtered (" Saved registers:\n "); | |
422 | else | |
423 | puts_filtered (","); | |
424 | wrap_here (" "); | |
89e0bbcd JG |
425 | printf_filtered (" %s at %s", reg_names[i], |
426 | local_hex_string(fsr.regs[i])); | |
bd5635a1 RP |
427 | count++; |
428 | } | |
429 | if (count) | |
430 | puts_filtered ("\n"); | |
431 | #endif /* Have FRAME_FIND_SAVED_REGS. */ | |
432 | } | |
433 | ||
434 | #if 0 | |
435 | /* Set a limit on the number of frames printed by default in a | |
436 | backtrace. */ | |
437 | ||
438 | static int backtrace_limit; | |
439 | ||
440 | static void | |
441 | set_backtrace_limit_command (count_exp, from_tty) | |
442 | char *count_exp; | |
443 | int from_tty; | |
444 | { | |
445 | int count = parse_and_eval_address (count_exp); | |
446 | ||
447 | if (count < 0) | |
448 | error ("Negative argument not meaningful as backtrace limit."); | |
449 | ||
450 | backtrace_limit = count; | |
451 | } | |
452 | ||
453 | static void | |
454 | backtrace_limit_info (arg, from_tty) | |
455 | char *arg; | |
456 | int from_tty; | |
457 | { | |
458 | if (arg) | |
459 | error ("\"Info backtrace-limit\" takes no arguments."); | |
460 | ||
461 | printf ("Backtrace limit: %d.\n", backtrace_limit); | |
462 | } | |
463 | #endif | |
464 | ||
465 | /* Print briefly all stack frames or just the innermost COUNT frames. */ | |
466 | ||
467 | static void | |
468 | backtrace_command (count_exp, from_tty) | |
469 | char *count_exp; | |
470 | int from_tty; | |
471 | { | |
472 | struct frame_info *fi; | |
473 | register int count; | |
474 | register FRAME frame; | |
475 | register int i; | |
476 | register FRAME trailing; | |
477 | register int trailing_level; | |
478 | ||
777bef06 JK |
479 | if (!target_has_stack) |
480 | error ("No stack."); | |
481 | ||
bd5635a1 RP |
482 | /* The following code must do two things. First, it must |
483 | set the variable TRAILING to the frame from which we should start | |
484 | printing. Second, it must set the variable count to the number | |
485 | of frames which we should print, or -1 if all of them. */ | |
486 | trailing = get_current_frame (); | |
487 | trailing_level = 0; | |
488 | if (count_exp) | |
489 | { | |
490 | count = parse_and_eval_address (count_exp); | |
491 | if (count < 0) | |
492 | { | |
493 | FRAME current; | |
494 | ||
495 | count = -count; | |
496 | ||
497 | current = trailing; | |
498 | while (current && count--) | |
499 | { | |
500 | QUIT; | |
501 | current = get_prev_frame (current); | |
502 | } | |
503 | ||
504 | /* Will stop when CURRENT reaches the top of the stack. TRAILING | |
505 | will be COUNT below it. */ | |
506 | while (current) | |
507 | { | |
508 | QUIT; | |
509 | trailing = get_prev_frame (trailing); | |
510 | current = get_prev_frame (current); | |
511 | trailing_level++; | |
512 | } | |
513 | ||
514 | count = -1; | |
515 | } | |
516 | } | |
517 | else | |
518 | count = -1; | |
519 | ||
520 | if (info_verbose) | |
521 | { | |
522 | struct partial_symtab *ps; | |
523 | ||
524 | /* Read in symbols for all of the frames. Need to do this in | |
525 | a separate pass so that "Reading in symbols for xxx" messages | |
526 | don't screw up the appearance of the backtrace. Also | |
527 | if people have strong opinions against reading symbols for | |
528 | backtrace this may have to be an option. */ | |
529 | i = count; | |
530 | for (frame = trailing; | |
531 | frame != NULL && i--; | |
532 | frame = get_prev_frame (frame)) | |
533 | { | |
534 | QUIT; | |
535 | fi = get_frame_info (frame); | |
536 | ps = find_pc_psymtab (fi->pc); | |
537 | if (ps) | |
538 | (void) PSYMTAB_TO_SYMTAB (ps); /* Force syms to come in */ | |
539 | } | |
540 | } | |
541 | ||
542 | for (i = 0, frame = trailing; | |
543 | frame && count--; | |
544 | i++, frame = get_prev_frame (frame)) | |
545 | { | |
546 | QUIT; | |
547 | fi = get_frame_info (frame); | |
548 | print_frame_info (fi, trailing_level + i, 0, 1); | |
549 | } | |
550 | ||
551 | /* If we've stopped before the end, mention that. */ | |
552 | if (frame && from_tty) | |
553 | printf_filtered ("(More stack frames follow...)\n"); | |
554 | } | |
555 | \f | |
556 | /* Print the local variables of a block B active in FRAME. | |
557 | Return 1 if any variables were printed; 0 otherwise. */ | |
558 | ||
559 | static int | |
560 | print_block_frame_locals (b, frame, stream) | |
561 | struct block *b; | |
562 | register FRAME frame; | |
563 | register FILE *stream; | |
564 | { | |
565 | int nsyms; | |
566 | register int i; | |
567 | register struct symbol *sym; | |
568 | register int values_printed = 0; | |
569 | ||
570 | nsyms = BLOCK_NSYMS (b); | |
571 | ||
572 | for (i = 0; i < nsyms; i++) | |
573 | { | |
574 | sym = BLOCK_SYM (b, i); | |
575 | if (SYMBOL_CLASS (sym) == LOC_LOCAL | |
576 | || SYMBOL_CLASS (sym) == LOC_REGISTER | |
577 | || SYMBOL_CLASS (sym) == LOC_STATIC) | |
578 | { | |
579 | values_printed = 1; | |
580 | fprint_symbol (stream, SYMBOL_NAME (sym)); | |
581 | fputs_filtered (" = ", stream); | |
582 | print_variable_value (sym, frame, stream); | |
583 | fprintf_filtered (stream, "\n"); | |
584 | fflush (stream); | |
585 | } | |
586 | } | |
587 | return values_printed; | |
588 | } | |
589 | ||
cadbb07a | 590 | /* Same, but print labels. */ |
bd5635a1 RP |
591 | |
592 | static int | |
cadbb07a | 593 | print_block_frame_labels (b, have_default, stream) |
bd5635a1 | 594 | struct block *b; |
bd5635a1 RP |
595 | int *have_default; |
596 | register FILE *stream; | |
597 | { | |
598 | int nsyms; | |
599 | register int i; | |
600 | register struct symbol *sym; | |
601 | register int values_printed = 0; | |
602 | ||
603 | nsyms = BLOCK_NSYMS (b); | |
604 | ||
605 | for (i = 0; i < nsyms; i++) | |
606 | { | |
607 | sym = BLOCK_SYM (b, i); | |
608 | if (! strcmp (SYMBOL_NAME (sym), "default")) | |
609 | { | |
610 | if (*have_default) | |
611 | continue; | |
612 | *have_default = 1; | |
613 | } | |
614 | if (SYMBOL_CLASS (sym) == LOC_LABEL) | |
615 | { | |
616 | struct symtab_and_line sal; | |
617 | sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0); | |
618 | values_printed = 1; | |
619 | fputs_demangled (SYMBOL_NAME (sym), stream, 1); | |
620 | if (addressprint) | |
89e0bbcd JG |
621 | fprintf_filtered (stream, " %s", |
622 | local_hex_string(SYMBOL_VALUE_ADDRESS (sym))); | |
bd5635a1 RP |
623 | fprintf_filtered (stream, " in file %s, line %d\n", |
624 | sal.symtab->filename, sal.line); | |
625 | fflush (stream); | |
626 | } | |
627 | } | |
628 | return values_printed; | |
629 | } | |
630 | ||
631 | /* Print on STREAM all the local variables in frame FRAME, | |
632 | including all the blocks active in that frame | |
633 | at its current pc. | |
634 | ||
635 | Returns 1 if the job was done, | |
636 | or 0 if nothing was printed because we have no info | |
637 | on the function running in FRAME. */ | |
638 | ||
639 | static int | |
640 | print_frame_local_vars (frame, stream) | |
641 | register FRAME frame; | |
642 | register FILE *stream; | |
643 | { | |
644 | register struct block *block = get_frame_block (frame); | |
645 | register int values_printed = 0; | |
646 | ||
647 | if (block == 0) | |
648 | { | |
649 | fprintf_filtered (stream, "No symbol table info available.\n"); | |
650 | fflush (stream); | |
651 | return 0; | |
652 | } | |
653 | ||
654 | while (block != 0) | |
655 | { | |
656 | if (print_block_frame_locals (block, frame, stream)) | |
657 | values_printed = 1; | |
658 | /* After handling the function's top-level block, stop. | |
659 | Don't continue to its superblock, the block of | |
660 | per-file symbols. */ | |
661 | if (BLOCK_FUNCTION (block)) | |
662 | break; | |
663 | block = BLOCK_SUPERBLOCK (block); | |
664 | } | |
665 | ||
666 | if (!values_printed) | |
667 | { | |
668 | fprintf_filtered (stream, "No locals.\n"); | |
669 | fflush (stream); | |
670 | } | |
671 | ||
672 | return 1; | |
673 | } | |
674 | ||
675 | /* Same, but print labels. */ | |
676 | ||
677 | static int | |
678 | print_frame_label_vars (frame, this_level_only, stream) | |
679 | register FRAME frame; | |
680 | int this_level_only; | |
681 | register FILE *stream; | |
682 | { | |
683 | extern struct blockvector *blockvector_for_pc (); | |
684 | register struct blockvector *bl; | |
685 | register struct block *block = get_frame_block (frame); | |
686 | register int values_printed = 0; | |
687 | int index, have_default = 0; | |
688 | char *blocks_printed; | |
689 | struct frame_info *fi = get_frame_info (frame); | |
690 | CORE_ADDR pc = fi->pc; | |
691 | ||
692 | if (block == 0) | |
693 | { | |
694 | fprintf_filtered (stream, "No symbol table info available.\n"); | |
695 | fflush (stream); | |
696 | return 0; | |
697 | } | |
698 | ||
699 | bl = blockvector_for_pc (BLOCK_END (block) - 4, &index); | |
700 | blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char)); | |
701 | bzero (blocks_printed, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char)); | |
702 | ||
703 | while (block != 0) | |
704 | { | |
705 | CORE_ADDR end = BLOCK_END (block) - 4; | |
706 | int last_index; | |
707 | ||
708 | if (bl != blockvector_for_pc (end, &index)) | |
709 | error ("blockvector blotch"); | |
710 | if (BLOCKVECTOR_BLOCK (bl, index) != block) | |
711 | error ("blockvector botch"); | |
712 | last_index = BLOCKVECTOR_NBLOCKS (bl); | |
713 | index += 1; | |
714 | ||
715 | /* Don't print out blocks that have gone by. */ | |
716 | while (index < last_index | |
717 | && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc) | |
718 | index++; | |
719 | ||
720 | while (index < last_index | |
721 | && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end) | |
722 | { | |
723 | if (blocks_printed[index] == 0) | |
724 | { | |
cadbb07a | 725 | if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream)) |
bd5635a1 RP |
726 | values_printed = 1; |
727 | blocks_printed[index] = 1; | |
728 | } | |
729 | index++; | |
730 | } | |
731 | if (have_default) | |
732 | return 1; | |
733 | if (values_printed && this_level_only) | |
734 | return 1; | |
735 | ||
736 | /* After handling the function's top-level block, stop. | |
737 | Don't continue to its superblock, the block of | |
738 | per-file symbols. */ | |
739 | if (BLOCK_FUNCTION (block)) | |
740 | break; | |
741 | block = BLOCK_SUPERBLOCK (block); | |
742 | } | |
743 | ||
744 | if (!values_printed && !this_level_only) | |
745 | { | |
746 | fprintf_filtered (stream, "No catches.\n"); | |
747 | fflush (stream); | |
748 | } | |
749 | ||
750 | return values_printed; | |
751 | } | |
752 | ||
e1ce8aa5 | 753 | /* ARGSUSED */ |
bd5635a1 RP |
754 | static void |
755 | locals_info (args, from_tty) | |
756 | char *args; | |
757 | int from_tty; | |
758 | { | |
cadbb07a JG |
759 | if (!selected_frame) |
760 | error ("No frame selected."); | |
bd5635a1 RP |
761 | print_frame_local_vars (selected_frame, stdout); |
762 | } | |
763 | ||
764 | static void | |
765 | catch_info () | |
766 | { | |
cadbb07a JG |
767 | if (!selected_frame) |
768 | error ("No frame selected."); | |
bd5635a1 RP |
769 | print_frame_label_vars (selected_frame, 0, stdout); |
770 | } | |
771 | ||
772 | static int | |
773 | print_frame_arg_vars (frame, stream) | |
774 | register FRAME frame; | |
775 | register FILE *stream; | |
776 | { | |
777 | struct symbol *func = get_frame_function (frame); | |
778 | register struct block *b; | |
779 | int nsyms; | |
780 | register int i; | |
781 | register struct symbol *sym, *sym2; | |
782 | register int values_printed = 0; | |
783 | ||
784 | if (func == 0) | |
785 | { | |
786 | fprintf_filtered (stream, "No symbol table info available.\n"); | |
787 | fflush (stream); | |
788 | return 0; | |
789 | } | |
790 | ||
791 | b = SYMBOL_BLOCK_VALUE (func); | |
792 | nsyms = BLOCK_NSYMS (b); | |
793 | ||
794 | for (i = 0; i < nsyms; i++) | |
795 | { | |
796 | sym = BLOCK_SYM (b, i); | |
797 | if (SYMBOL_CLASS (sym) == LOC_ARG | |
798 | || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG | |
799 | || SYMBOL_CLASS (sym) == LOC_REF_ARG | |
800 | || SYMBOL_CLASS (sym) == LOC_REGPARM) | |
801 | { | |
802 | values_printed = 1; | |
803 | fprint_symbol (stream, SYMBOL_NAME (sym)); | |
804 | fputs_filtered (" = ", stream); | |
805 | /* We have to look up the symbol because arguments often have | |
806 | two entries (one a parameter, one a register) and the one | |
807 | we want is the register, which lookup_symbol will find for | |
808 | us. */ | |
809 | sym2 = lookup_symbol (SYMBOL_NAME (sym), | |
810 | b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL); | |
811 | print_variable_value (sym2, frame, stream); | |
812 | fprintf_filtered (stream, "\n"); | |
813 | fflush (stream); | |
814 | } | |
815 | } | |
816 | ||
817 | if (!values_printed) | |
818 | { | |
819 | fprintf_filtered (stream, "No arguments.\n"); | |
820 | fflush (stream); | |
821 | } | |
822 | ||
823 | return 1; | |
824 | } | |
825 | ||
826 | static void | |
827 | args_info () | |
828 | { | |
cadbb07a JG |
829 | if (!selected_frame) |
830 | error ("No frame selected."); | |
bd5635a1 RP |
831 | print_frame_arg_vars (selected_frame, stdout); |
832 | } | |
833 | \f | |
834 | /* Select frame FRAME, and note that its stack level is LEVEL. | |
835 | LEVEL may be -1 if an actual level number is not known. */ | |
836 | ||
837 | void | |
838 | select_frame (frame, level) | |
839 | FRAME frame; | |
840 | int level; | |
841 | { | |
89e0bbcd JG |
842 | register struct symtab *s; |
843 | ||
bd5635a1 RP |
844 | selected_frame = frame; |
845 | selected_frame_level = level; | |
89e0bbcd JG |
846 | |
847 | /* Ensure that symbols for this frame are read in. Also, determine the | |
848 | source language of this frame, and switch to it if desired. */ | |
bd5635a1 | 849 | if (frame) |
89e0bbcd JG |
850 | { |
851 | s = find_pc_symtab (get_frame_info (frame)->pc); | |
852 | if (s | |
91ec58ee | 853 | && s->language != current_language->la_language |
89e0bbcd JG |
854 | && s->language != language_unknown |
855 | && language_mode == language_mode_auto) { | |
856 | set_language(s->language); | |
857 | } | |
858 | } | |
bd5635a1 RP |
859 | } |
860 | ||
777bef06 JK |
861 | /* Store the selected frame and its level into *FRAMEP and *LEVELP. |
862 | If there is no selected frame, *FRAMEP is set to NULL. */ | |
bd5635a1 RP |
863 | |
864 | void | |
865 | record_selected_frame (frameaddrp, levelp) | |
866 | FRAME_ADDR *frameaddrp; | |
867 | int *levelp; | |
868 | { | |
777bef06 | 869 | *frameaddrp = selected_frame ? FRAME_FP (selected_frame) : NULL; |
bd5635a1 RP |
870 | *levelp = selected_frame_level; |
871 | } | |
872 | ||
873 | /* Return the symbol-block in which the selected frame is executing. | |
874 | Can return zero under various legitimate circumstances. */ | |
875 | ||
876 | struct block * | |
877 | get_selected_block () | |
878 | { | |
879 | if (!target_has_stack) | |
880 | return 0; | |
881 | ||
882 | if (!selected_frame) | |
883 | return get_current_block (); | |
884 | return get_frame_block (selected_frame); | |
885 | } | |
886 | ||
887 | /* Find a frame a certain number of levels away from FRAME. | |
888 | LEVEL_OFFSET_PTR points to an int containing the number of levels. | |
889 | Positive means go to earlier frames (up); negative, the reverse. | |
890 | The int that contains the number of levels is counted toward | |
891 | zero as the frames for those levels are found. | |
892 | If the top or bottom frame is reached, that frame is returned, | |
893 | but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates | |
894 | how much farther the original request asked to go. */ | |
895 | ||
896 | FRAME | |
897 | find_relative_frame (frame, level_offset_ptr) | |
898 | register FRAME frame; | |
899 | register int* level_offset_ptr; | |
900 | { | |
901 | register FRAME prev; | |
d8ce1326 | 902 | register FRAME frame1; |
bd5635a1 RP |
903 | |
904 | /* Going up is simple: just do get_prev_frame enough times | |
905 | or until initial frame is reached. */ | |
906 | while (*level_offset_ptr > 0) | |
907 | { | |
908 | prev = get_prev_frame (frame); | |
909 | if (prev == 0) | |
910 | break; | |
911 | (*level_offset_ptr)--; | |
912 | frame = prev; | |
913 | } | |
d8ce1326 | 914 | /* Going down is just as simple. */ |
bd5635a1 RP |
915 | if (*level_offset_ptr < 0) |
916 | { | |
cadbb07a JG |
917 | while (*level_offset_ptr < 0) { |
918 | frame1 = get_next_frame (frame); | |
919 | if (!frame1) | |
920 | break; | |
921 | frame = frame1; | |
922 | (*level_offset_ptr)++; | |
923 | } | |
bd5635a1 RP |
924 | } |
925 | return frame; | |
926 | } | |
927 | ||
928 | /* The "frame" command. With no arg, print selected frame briefly. | |
929 | With arg LEVEL_EXP, select the frame at level LEVEL if it is a | |
930 | valid level. Otherwise, treat level_exp as an address expression | |
931 | and print it. See parse_frame_specification for more info on proper | |
932 | frame expressions. */ | |
933 | ||
934 | static void | |
935 | frame_command (level_exp, from_tty) | |
936 | char *level_exp; | |
937 | int from_tty; | |
938 | { | |
939 | register FRAME frame, frame1; | |
940 | unsigned int level = 0; | |
941 | ||
942 | if (!target_has_stack) | |
943 | error ("No stack."); | |
944 | ||
945 | frame = parse_frame_specification (level_exp); | |
946 | ||
cadbb07a JG |
947 | /* Try to figure out what level this frame is. But if there is |
948 | no current stack, don't error out -- let the user set one. */ | |
949 | frame1 = 0; | |
950 | if (get_current_frame()) { | |
951 | for (frame1 = get_prev_frame (0); | |
952 | frame1 && frame1 != frame; | |
953 | frame1 = get_prev_frame (frame1)) | |
954 | level++; | |
955 | } | |
bd5635a1 RP |
956 | |
957 | if (!frame1) | |
958 | level = 0; | |
959 | ||
960 | select_frame (frame, level); | |
961 | ||
962 | if (!from_tty) | |
963 | return; | |
964 | ||
965 | print_stack_frame (selected_frame, selected_frame_level, 1); | |
966 | } | |
967 | ||
968 | /* Select the frame up one or COUNT stack levels | |
969 | from the previously selected frame, and print it briefly. */ | |
970 | ||
e1ce8aa5 | 971 | /* ARGSUSED */ |
bd5635a1 RP |
972 | static void |
973 | up_silently_command (count_exp, from_tty) | |
974 | char *count_exp; | |
975 | int from_tty; | |
976 | { | |
977 | register FRAME frame; | |
978 | int count = 1, count1; | |
979 | if (count_exp) | |
980 | count = parse_and_eval_address (count_exp); | |
981 | count1 = count; | |
982 | ||
983 | if (!target_has_stack) | |
984 | error ("No stack."); | |
985 | ||
986 | frame = find_relative_frame (selected_frame, &count1); | |
987 | if (count1 != 0 && count_exp == 0) | |
988 | error ("Initial frame selected; you cannot go up."); | |
989 | select_frame (frame, selected_frame_level + count - count1); | |
990 | } | |
991 | ||
992 | static void | |
993 | up_command (count_exp, from_tty) | |
994 | char *count_exp; | |
995 | int from_tty; | |
996 | { | |
997 | up_silently_command (count_exp, from_tty); | |
998 | print_stack_frame (selected_frame, selected_frame_level, 1); | |
999 | } | |
1000 | ||
1001 | /* Select the frame down one or COUNT stack levels | |
1002 | from the previously selected frame, and print it briefly. */ | |
1003 | ||
e1ce8aa5 | 1004 | /* ARGSUSED */ |
bd5635a1 RP |
1005 | static void |
1006 | down_silently_command (count_exp, from_tty) | |
1007 | char *count_exp; | |
1008 | int from_tty; | |
1009 | { | |
1010 | register FRAME frame; | |
1011 | int count = -1, count1; | |
1012 | if (count_exp) | |
1013 | count = - parse_and_eval_address (count_exp); | |
1014 | count1 = count; | |
1015 | ||
89e0bbcd JG |
1016 | if (!target_has_stack) |
1017 | error ("No stack."); | |
1018 | ||
bd5635a1 RP |
1019 | frame = find_relative_frame (selected_frame, &count1); |
1020 | if (count1 != 0 && count_exp == 0) | |
1021 | error ("Bottom (i.e., innermost) frame selected; you cannot go down."); | |
1022 | select_frame (frame, selected_frame_level + count - count1); | |
1023 | } | |
1024 | ||
1025 | ||
1026 | static void | |
1027 | down_command (count_exp, from_tty) | |
1028 | char *count_exp; | |
1029 | int from_tty; | |
1030 | { | |
1031 | down_silently_command (count_exp, from_tty); | |
1032 | print_stack_frame (selected_frame, selected_frame_level, 1); | |
1033 | } | |
1034 | \f | |
1035 | static void | |
1036 | return_command (retval_exp, from_tty) | |
1037 | char *retval_exp; | |
1038 | int from_tty; | |
1039 | { | |
777bef06 JK |
1040 | struct symbol *thisfun; |
1041 | FRAME_ADDR selected_frame_addr; | |
1042 | CORE_ADDR selected_frame_pc; | |
bd5635a1 RP |
1043 | FRAME frame; |
1044 | ||
777bef06 JK |
1045 | if (selected_frame == NULL) |
1046 | error ("No selected frame."); | |
1047 | thisfun = get_frame_function (selected_frame); | |
1048 | selected_frame_addr = FRAME_FP (selected_frame); | |
1049 | selected_frame_pc = (get_frame_info (selected_frame))->pc; | |
1050 | ||
bd5635a1 RP |
1051 | /* If interactive, require confirmation. */ |
1052 | ||
1053 | if (from_tty) | |
1054 | { | |
1055 | if (thisfun != 0) | |
1056 | { | |
1057 | if (!query ("Make %s return now? ", SYMBOL_NAME (thisfun))) | |
1058 | error ("Not confirmed."); | |
1059 | } | |
1060 | else | |
1061 | if (!query ("Make selected stack frame return now? ")) | |
1062 | error ("Not confirmed."); | |
1063 | } | |
1064 | ||
1065 | /* Do the real work. Pop until the specified frame is current. We | |
1066 | use this method because the selected_frame is not valid after | |
1067 | a POP_FRAME. The pc comparison makes this work even if the | |
1068 | selected frame shares its fp with another frame. */ | |
1069 | ||
1070 | while ( selected_frame_addr != FRAME_FP (frame = get_current_frame()) | |
1071 | || selected_frame_pc != (get_frame_info (frame))->pc ) | |
1072 | POP_FRAME; | |
1073 | ||
1074 | /* Then pop that frame. */ | |
1075 | ||
1076 | POP_FRAME; | |
1077 | ||
1078 | /* Compute the return value (if any) and store in the place | |
1079 | for return values. */ | |
1080 | ||
1081 | if (retval_exp) | |
1082 | set_return_value (parse_and_eval (retval_exp)); | |
1083 | ||
1084 | /* If interactive, print the frame that is now current. */ | |
1085 | ||
1086 | if (from_tty) | |
1087 | frame_command ("0", 1); | |
1088 | } | |
89e0bbcd JG |
1089 | |
1090 | /* Gets the language of the current frame. */ | |
1091 | enum language | |
1092 | get_frame_language() | |
1093 | { | |
1094 | register struct symtab *s; | |
1095 | FRAME fr; | |
1096 | enum language flang; /* The language of the current frame */ | |
1097 | ||
1098 | fr = get_frame_info(selected_frame); | |
1099 | if(fr) | |
1100 | { | |
1101 | s = find_pc_symtab(fr->pc); | |
1102 | if(s) | |
1103 | flang = s->language; | |
1104 | else | |
1105 | flang = language_unknown; | |
1106 | } | |
1107 | else | |
1108 | flang = language_unknown; | |
1109 | ||
1110 | return flang; | |
1111 | } | |
bd5635a1 RP |
1112 | \f |
1113 | void | |
1114 | _initialize_stack () | |
1115 | { | |
1116 | #if 0 | |
1117 | backtrace_limit = 30; | |
1118 | #endif | |
1119 | ||
1120 | add_com ("return", class_stack, return_command, | |
1121 | "Make selected stack frame return to its caller.\n\ | |
1122 | Control remains in the debugger, but when you continue\n\ | |
1123 | execution will resume in the frame above the one now selected.\n\ | |
1124 | If an argument is given, it is an expression for the value to return."); | |
1125 | ||
1126 | add_com ("up", class_stack, up_command, | |
1127 | "Select and print stack frame that called this one.\n\ | |
1128 | An argument says how many frames up to go."); | |
1129 | add_com ("up-silently", class_support, up_silently_command, | |
1130 | "Same as the `up' command, but does not print anything.\n\ | |
1131 | This is useful in command scripts."); | |
1132 | ||
1133 | add_com ("down", class_stack, down_command, | |
1134 | "Select and print stack frame called by this one.\n\ | |
1135 | An argument says how many frames down to go."); | |
1136 | add_com_alias ("do", "down", class_stack, 1); | |
1137 | add_com ("down-silently", class_support, down_silently_command, | |
1138 | "Same as the `down' command, but does not print anything.\n\ | |
1139 | This is useful in command scripts."); | |
1140 | ||
1141 | add_com ("frame", class_stack, frame_command, | |
1142 | "Select and print a stack frame.\n\ | |
1143 | With no argument, print the selected stack frame. (See also \"info frame\").\n\ | |
1144 | An argument specifies the frame to select.\n\ | |
1145 | It can be a stack frame number or the address of the frame.\n\ | |
1146 | With argument, nothing is printed if input is coming from\n\ | |
1147 | a command file or a user-defined command."); | |
1148 | ||
1149 | add_com_alias ("f", "frame", class_stack, 1); | |
1150 | ||
1151 | add_com ("backtrace", class_stack, backtrace_command, | |
1152 | "Print backtrace of all stack frames, or innermost COUNT frames.\n\ | |
1153 | With a negative argument, print outermost -COUNT frames."); | |
1154 | add_com_alias ("bt", "backtrace", class_stack, 0); | |
1155 | add_com_alias ("where", "backtrace", class_alias, 0); | |
1156 | add_info ("stack", backtrace_command, | |
1157 | "Backtrace of the stack, or innermost COUNT frames."); | |
1158 | add_info_alias ("s", "stack", 1); | |
1159 | add_info ("frame", frame_info, | |
1160 | "All about selected stack frame, or frame at ADDR."); | |
1161 | add_info_alias ("f", "frame", 1); | |
1162 | add_info ("locals", locals_info, | |
1163 | "Local variables of current stack frame."); | |
1164 | add_info ("args", args_info, | |
1165 | "Argument variables of current stack frame."); | |
1166 | add_info ("catch", catch_info, | |
1167 | "Exceptions that can be caught in the current stack frame."); | |
1168 | ||
1169 | #if 0 | |
1170 | add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, | |
1171 | "Specify maximum number of frames for \"backtrace\" to print by default.", | |
1172 | &setlist); | |
1173 | add_info ("backtrace-limit", backtrace_limit_info, | |
1174 | "The maximum number of frames for \"backtrace\" to print by default."); | |
1175 | #endif | |
1176 | } |