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) | |
b0077123 JG |
363 | { |
364 | printf_filtered (" in "); | |
365 | fputs_demangled (funname, stdout, 1); | |
366 | } | |
bd5635a1 RP |
367 | wrap_here (" "); |
368 | if (sal.symtab) | |
369 | printf_filtered (" (%s:%d)", sal.symtab->filename, sal.line); | |
370 | puts_filtered ("; "); | |
371 | wrap_here (" "); | |
89e0bbcd JG |
372 | printf_filtered ("saved %s %s\n", reg_names[PC_REGNUM], |
373 | local_hex_string(FRAME_SAVED_PC (frame))); | |
bd5635a1 | 374 | if (calling_frame) |
89e0bbcd JG |
375 | printf_filtered (" called by frame at %s", |
376 | local_hex_string(FRAME_FP (calling_frame))); | |
bd5635a1 RP |
377 | if (fi->next_frame && calling_frame) |
378 | puts_filtered (","); | |
379 | wrap_here (" "); | |
380 | if (fi->next_frame) | |
89e0bbcd | 381 | printf_filtered (" caller of frame at %s", local_hex_string(fi->next_frame)); |
bd5635a1 RP |
382 | if (fi->next_frame || calling_frame) |
383 | puts_filtered ("\n"); | |
89e0bbcd JG |
384 | if (s) |
385 | printf_filtered(" source language %s.\n", language_str(s->language)); | |
bd5635a1 RP |
386 | |
387 | { | |
388 | /* Address of the argument list for this frame, or 0. */ | |
389 | CORE_ADDR arg_list = FRAME_ARGS_ADDRESS_CORRECT (fi); | |
390 | /* Number of args for this frame, or -1 if unknown. */ | |
391 | int numargs; | |
392 | ||
393 | if (arg_list == 0) | |
394 | printf_filtered (" Arglist at unknown address.\n"); | |
395 | else | |
396 | { | |
89e0bbcd | 397 | printf_filtered (" Arglist at %s,", local_hex_string(arg_list)); |
bd5635a1 RP |
398 | |
399 | FRAME_NUM_ARGS (numargs, fi); | |
400 | if (numargs < 0) | |
401 | puts_filtered (" args: "); | |
402 | else if (numargs == 0) | |
403 | puts_filtered (" no args."); | |
404 | else if (numargs == 1) | |
405 | puts_filtered (" 1 arg: "); | |
406 | else | |
407 | printf_filtered (" %d args: ", numargs); | |
408 | print_frame_args (func, fi, numargs, stdout); | |
409 | puts_filtered ("\n"); | |
410 | } | |
411 | } | |
412 | ||
413 | #if defined (FRAME_FIND_SAVED_REGS) | |
414 | get_frame_saved_regs (fi, &fsr); | |
415 | /* The sp is special; what's returned isn't the save address, but | |
416 | actually the value of the previous frame's sp. */ | |
89e0bbcd JG |
417 | printf_filtered (" Previous frame's sp is %s\n", |
418 | local_hex_string(fsr.regs[SP_REGNUM])); | |
bd5635a1 RP |
419 | count = 0; |
420 | for (i = 0; i < NUM_REGS; i++) | |
421 | if (fsr.regs[i] && i != SP_REGNUM) | |
422 | { | |
423 | if (count == 0) | |
424 | puts_filtered (" Saved registers:\n "); | |
425 | else | |
426 | puts_filtered (","); | |
427 | wrap_here (" "); | |
89e0bbcd JG |
428 | printf_filtered (" %s at %s", reg_names[i], |
429 | local_hex_string(fsr.regs[i])); | |
bd5635a1 RP |
430 | count++; |
431 | } | |
432 | if (count) | |
433 | puts_filtered ("\n"); | |
434 | #endif /* Have FRAME_FIND_SAVED_REGS. */ | |
435 | } | |
436 | ||
437 | #if 0 | |
438 | /* Set a limit on the number of frames printed by default in a | |
439 | backtrace. */ | |
440 | ||
441 | static int backtrace_limit; | |
442 | ||
443 | static void | |
444 | set_backtrace_limit_command (count_exp, from_tty) | |
445 | char *count_exp; | |
446 | int from_tty; | |
447 | { | |
448 | int count = parse_and_eval_address (count_exp); | |
449 | ||
450 | if (count < 0) | |
451 | error ("Negative argument not meaningful as backtrace limit."); | |
452 | ||
453 | backtrace_limit = count; | |
454 | } | |
455 | ||
456 | static void | |
457 | backtrace_limit_info (arg, from_tty) | |
458 | char *arg; | |
459 | int from_tty; | |
460 | { | |
461 | if (arg) | |
462 | error ("\"Info backtrace-limit\" takes no arguments."); | |
463 | ||
464 | printf ("Backtrace limit: %d.\n", backtrace_limit); | |
465 | } | |
466 | #endif | |
467 | ||
468 | /* Print briefly all stack frames or just the innermost COUNT frames. */ | |
469 | ||
470 | static void | |
471 | backtrace_command (count_exp, from_tty) | |
472 | char *count_exp; | |
473 | int from_tty; | |
474 | { | |
475 | struct frame_info *fi; | |
476 | register int count; | |
477 | register FRAME frame; | |
478 | register int i; | |
479 | register FRAME trailing; | |
480 | register int trailing_level; | |
481 | ||
777bef06 JK |
482 | if (!target_has_stack) |
483 | error ("No stack."); | |
484 | ||
bd5635a1 RP |
485 | /* The following code must do two things. First, it must |
486 | set the variable TRAILING to the frame from which we should start | |
487 | printing. Second, it must set the variable count to the number | |
488 | of frames which we should print, or -1 if all of them. */ | |
489 | trailing = get_current_frame (); | |
490 | trailing_level = 0; | |
491 | if (count_exp) | |
492 | { | |
493 | count = parse_and_eval_address (count_exp); | |
494 | if (count < 0) | |
495 | { | |
496 | FRAME current; | |
497 | ||
498 | count = -count; | |
499 | ||
500 | current = trailing; | |
501 | while (current && count--) | |
502 | { | |
503 | QUIT; | |
504 | current = get_prev_frame (current); | |
505 | } | |
506 | ||
507 | /* Will stop when CURRENT reaches the top of the stack. TRAILING | |
508 | will be COUNT below it. */ | |
509 | while (current) | |
510 | { | |
511 | QUIT; | |
512 | trailing = get_prev_frame (trailing); | |
513 | current = get_prev_frame (current); | |
514 | trailing_level++; | |
515 | } | |
516 | ||
517 | count = -1; | |
518 | } | |
519 | } | |
520 | else | |
521 | count = -1; | |
522 | ||
523 | if (info_verbose) | |
524 | { | |
525 | struct partial_symtab *ps; | |
526 | ||
527 | /* Read in symbols for all of the frames. Need to do this in | |
528 | a separate pass so that "Reading in symbols for xxx" messages | |
529 | don't screw up the appearance of the backtrace. Also | |
530 | if people have strong opinions against reading symbols for | |
531 | backtrace this may have to be an option. */ | |
532 | i = count; | |
533 | for (frame = trailing; | |
534 | frame != NULL && i--; | |
535 | frame = get_prev_frame (frame)) | |
536 | { | |
537 | QUIT; | |
538 | fi = get_frame_info (frame); | |
539 | ps = find_pc_psymtab (fi->pc); | |
540 | if (ps) | |
541 | (void) PSYMTAB_TO_SYMTAB (ps); /* Force syms to come in */ | |
542 | } | |
543 | } | |
544 | ||
545 | for (i = 0, frame = trailing; | |
546 | frame && count--; | |
547 | i++, frame = get_prev_frame (frame)) | |
548 | { | |
549 | QUIT; | |
550 | fi = get_frame_info (frame); | |
551 | print_frame_info (fi, trailing_level + i, 0, 1); | |
552 | } | |
553 | ||
554 | /* If we've stopped before the end, mention that. */ | |
555 | if (frame && from_tty) | |
556 | printf_filtered ("(More stack frames follow...)\n"); | |
557 | } | |
558 | \f | |
559 | /* Print the local variables of a block B active in FRAME. | |
560 | Return 1 if any variables were printed; 0 otherwise. */ | |
561 | ||
562 | static int | |
563 | print_block_frame_locals (b, frame, stream) | |
564 | struct block *b; | |
565 | register FRAME frame; | |
566 | register FILE *stream; | |
567 | { | |
568 | int nsyms; | |
569 | register int i; | |
570 | register struct symbol *sym; | |
571 | register int values_printed = 0; | |
572 | ||
573 | nsyms = BLOCK_NSYMS (b); | |
574 | ||
575 | for (i = 0; i < nsyms; i++) | |
576 | { | |
577 | sym = BLOCK_SYM (b, i); | |
578 | if (SYMBOL_CLASS (sym) == LOC_LOCAL | |
579 | || SYMBOL_CLASS (sym) == LOC_REGISTER | |
580 | || SYMBOL_CLASS (sym) == LOC_STATIC) | |
581 | { | |
582 | values_printed = 1; | |
583 | fprint_symbol (stream, SYMBOL_NAME (sym)); | |
584 | fputs_filtered (" = ", stream); | |
585 | print_variable_value (sym, frame, stream); | |
586 | fprintf_filtered (stream, "\n"); | |
587 | fflush (stream); | |
588 | } | |
589 | } | |
590 | return values_printed; | |
591 | } | |
592 | ||
cadbb07a | 593 | /* Same, but print labels. */ |
bd5635a1 RP |
594 | |
595 | static int | |
cadbb07a | 596 | print_block_frame_labels (b, have_default, stream) |
bd5635a1 | 597 | struct block *b; |
bd5635a1 RP |
598 | int *have_default; |
599 | register FILE *stream; | |
600 | { | |
601 | int nsyms; | |
602 | register int i; | |
603 | register struct symbol *sym; | |
604 | register int values_printed = 0; | |
605 | ||
606 | nsyms = BLOCK_NSYMS (b); | |
607 | ||
608 | for (i = 0; i < nsyms; i++) | |
609 | { | |
610 | sym = BLOCK_SYM (b, i); | |
611 | if (! strcmp (SYMBOL_NAME (sym), "default")) | |
612 | { | |
613 | if (*have_default) | |
614 | continue; | |
615 | *have_default = 1; | |
616 | } | |
617 | if (SYMBOL_CLASS (sym) == LOC_LABEL) | |
618 | { | |
619 | struct symtab_and_line sal; | |
620 | sal = find_pc_line (SYMBOL_VALUE_ADDRESS (sym), 0); | |
621 | values_printed = 1; | |
622 | fputs_demangled (SYMBOL_NAME (sym), stream, 1); | |
623 | if (addressprint) | |
89e0bbcd JG |
624 | fprintf_filtered (stream, " %s", |
625 | local_hex_string(SYMBOL_VALUE_ADDRESS (sym))); | |
bd5635a1 RP |
626 | fprintf_filtered (stream, " in file %s, line %d\n", |
627 | sal.symtab->filename, sal.line); | |
628 | fflush (stream); | |
629 | } | |
630 | } | |
631 | return values_printed; | |
632 | } | |
633 | ||
634 | /* Print on STREAM all the local variables in frame FRAME, | |
635 | including all the blocks active in that frame | |
636 | at its current pc. | |
637 | ||
638 | Returns 1 if the job was done, | |
639 | or 0 if nothing was printed because we have no info | |
640 | on the function running in FRAME. */ | |
641 | ||
642 | static int | |
643 | print_frame_local_vars (frame, stream) | |
644 | register FRAME frame; | |
645 | register FILE *stream; | |
646 | { | |
647 | register struct block *block = get_frame_block (frame); | |
648 | register int values_printed = 0; | |
649 | ||
650 | if (block == 0) | |
651 | { | |
652 | fprintf_filtered (stream, "No symbol table info available.\n"); | |
653 | fflush (stream); | |
654 | return 0; | |
655 | } | |
656 | ||
657 | while (block != 0) | |
658 | { | |
659 | if (print_block_frame_locals (block, frame, stream)) | |
660 | values_printed = 1; | |
661 | /* After handling the function's top-level block, stop. | |
662 | Don't continue to its superblock, the block of | |
663 | per-file symbols. */ | |
664 | if (BLOCK_FUNCTION (block)) | |
665 | break; | |
666 | block = BLOCK_SUPERBLOCK (block); | |
667 | } | |
668 | ||
669 | if (!values_printed) | |
670 | { | |
671 | fprintf_filtered (stream, "No locals.\n"); | |
672 | fflush (stream); | |
673 | } | |
674 | ||
675 | return 1; | |
676 | } | |
677 | ||
678 | /* Same, but print labels. */ | |
679 | ||
680 | static int | |
681 | print_frame_label_vars (frame, this_level_only, stream) | |
682 | register FRAME frame; | |
683 | int this_level_only; | |
684 | register FILE *stream; | |
685 | { | |
686 | extern struct blockvector *blockvector_for_pc (); | |
687 | register struct blockvector *bl; | |
688 | register struct block *block = get_frame_block (frame); | |
689 | register int values_printed = 0; | |
690 | int index, have_default = 0; | |
691 | char *blocks_printed; | |
692 | struct frame_info *fi = get_frame_info (frame); | |
693 | CORE_ADDR pc = fi->pc; | |
694 | ||
695 | if (block == 0) | |
696 | { | |
697 | fprintf_filtered (stream, "No symbol table info available.\n"); | |
698 | fflush (stream); | |
699 | return 0; | |
700 | } | |
701 | ||
702 | bl = blockvector_for_pc (BLOCK_END (block) - 4, &index); | |
703 | blocks_printed = (char *) alloca (BLOCKVECTOR_NBLOCKS (bl) * sizeof (char)); | |
704 | bzero (blocks_printed, BLOCKVECTOR_NBLOCKS (bl) * sizeof (char)); | |
705 | ||
706 | while (block != 0) | |
707 | { | |
708 | CORE_ADDR end = BLOCK_END (block) - 4; | |
709 | int last_index; | |
710 | ||
711 | if (bl != blockvector_for_pc (end, &index)) | |
712 | error ("blockvector blotch"); | |
713 | if (BLOCKVECTOR_BLOCK (bl, index) != block) | |
714 | error ("blockvector botch"); | |
715 | last_index = BLOCKVECTOR_NBLOCKS (bl); | |
716 | index += 1; | |
717 | ||
718 | /* Don't print out blocks that have gone by. */ | |
719 | while (index < last_index | |
720 | && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < pc) | |
721 | index++; | |
722 | ||
723 | while (index < last_index | |
724 | && BLOCK_END (BLOCKVECTOR_BLOCK (bl, index)) < end) | |
725 | { | |
726 | if (blocks_printed[index] == 0) | |
727 | { | |
cadbb07a | 728 | if (print_block_frame_labels (BLOCKVECTOR_BLOCK (bl, index), &have_default, stream)) |
bd5635a1 RP |
729 | values_printed = 1; |
730 | blocks_printed[index] = 1; | |
731 | } | |
732 | index++; | |
733 | } | |
734 | if (have_default) | |
735 | return 1; | |
736 | if (values_printed && this_level_only) | |
737 | return 1; | |
738 | ||
739 | /* After handling the function's top-level block, stop. | |
740 | Don't continue to its superblock, the block of | |
741 | per-file symbols. */ | |
742 | if (BLOCK_FUNCTION (block)) | |
743 | break; | |
744 | block = BLOCK_SUPERBLOCK (block); | |
745 | } | |
746 | ||
747 | if (!values_printed && !this_level_only) | |
748 | { | |
749 | fprintf_filtered (stream, "No catches.\n"); | |
750 | fflush (stream); | |
751 | } | |
752 | ||
753 | return values_printed; | |
754 | } | |
755 | ||
e1ce8aa5 | 756 | /* ARGSUSED */ |
bd5635a1 RP |
757 | static void |
758 | locals_info (args, from_tty) | |
759 | char *args; | |
760 | int from_tty; | |
761 | { | |
cadbb07a JG |
762 | if (!selected_frame) |
763 | error ("No frame selected."); | |
bd5635a1 RP |
764 | print_frame_local_vars (selected_frame, stdout); |
765 | } | |
766 | ||
767 | static void | |
768 | catch_info () | |
769 | { | |
cadbb07a JG |
770 | if (!selected_frame) |
771 | error ("No frame selected."); | |
bd5635a1 RP |
772 | print_frame_label_vars (selected_frame, 0, stdout); |
773 | } | |
774 | ||
775 | static int | |
776 | print_frame_arg_vars (frame, stream) | |
777 | register FRAME frame; | |
778 | register FILE *stream; | |
779 | { | |
780 | struct symbol *func = get_frame_function (frame); | |
781 | register struct block *b; | |
782 | int nsyms; | |
783 | register int i; | |
784 | register struct symbol *sym, *sym2; | |
785 | register int values_printed = 0; | |
786 | ||
787 | if (func == 0) | |
788 | { | |
789 | fprintf_filtered (stream, "No symbol table info available.\n"); | |
790 | fflush (stream); | |
791 | return 0; | |
792 | } | |
793 | ||
794 | b = SYMBOL_BLOCK_VALUE (func); | |
795 | nsyms = BLOCK_NSYMS (b); | |
796 | ||
797 | for (i = 0; i < nsyms; i++) | |
798 | { | |
799 | sym = BLOCK_SYM (b, i); | |
800 | if (SYMBOL_CLASS (sym) == LOC_ARG | |
801 | || SYMBOL_CLASS (sym) == LOC_LOCAL_ARG | |
802 | || SYMBOL_CLASS (sym) == LOC_REF_ARG | |
803 | || SYMBOL_CLASS (sym) == LOC_REGPARM) | |
804 | { | |
805 | values_printed = 1; | |
806 | fprint_symbol (stream, SYMBOL_NAME (sym)); | |
807 | fputs_filtered (" = ", stream); | |
808 | /* We have to look up the symbol because arguments often have | |
809 | two entries (one a parameter, one a register) and the one | |
810 | we want is the register, which lookup_symbol will find for | |
811 | us. */ | |
812 | sym2 = lookup_symbol (SYMBOL_NAME (sym), | |
813 | b, VAR_NAMESPACE, (int *)NULL, (struct symtab **)NULL); | |
814 | print_variable_value (sym2, frame, stream); | |
815 | fprintf_filtered (stream, "\n"); | |
816 | fflush (stream); | |
817 | } | |
818 | } | |
819 | ||
820 | if (!values_printed) | |
821 | { | |
822 | fprintf_filtered (stream, "No arguments.\n"); | |
823 | fflush (stream); | |
824 | } | |
825 | ||
826 | return 1; | |
827 | } | |
828 | ||
829 | static void | |
830 | args_info () | |
831 | { | |
cadbb07a JG |
832 | if (!selected_frame) |
833 | error ("No frame selected."); | |
bd5635a1 RP |
834 | print_frame_arg_vars (selected_frame, stdout); |
835 | } | |
836 | \f | |
837 | /* Select frame FRAME, and note that its stack level is LEVEL. | |
838 | LEVEL may be -1 if an actual level number is not known. */ | |
839 | ||
840 | void | |
841 | select_frame (frame, level) | |
842 | FRAME frame; | |
843 | int level; | |
844 | { | |
89e0bbcd JG |
845 | register struct symtab *s; |
846 | ||
bd5635a1 RP |
847 | selected_frame = frame; |
848 | selected_frame_level = level; | |
89e0bbcd JG |
849 | |
850 | /* Ensure that symbols for this frame are read in. Also, determine the | |
851 | source language of this frame, and switch to it if desired. */ | |
bd5635a1 | 852 | if (frame) |
89e0bbcd JG |
853 | { |
854 | s = find_pc_symtab (get_frame_info (frame)->pc); | |
855 | if (s | |
91ec58ee | 856 | && s->language != current_language->la_language |
89e0bbcd JG |
857 | && s->language != language_unknown |
858 | && language_mode == language_mode_auto) { | |
859 | set_language(s->language); | |
860 | } | |
861 | } | |
bd5635a1 RP |
862 | } |
863 | ||
777bef06 JK |
864 | /* Store the selected frame and its level into *FRAMEP and *LEVELP. |
865 | If there is no selected frame, *FRAMEP is set to NULL. */ | |
bd5635a1 RP |
866 | |
867 | void | |
868 | record_selected_frame (frameaddrp, levelp) | |
869 | FRAME_ADDR *frameaddrp; | |
870 | int *levelp; | |
871 | { | |
777bef06 | 872 | *frameaddrp = selected_frame ? FRAME_FP (selected_frame) : NULL; |
bd5635a1 RP |
873 | *levelp = selected_frame_level; |
874 | } | |
875 | ||
876 | /* Return the symbol-block in which the selected frame is executing. | |
877 | Can return zero under various legitimate circumstances. */ | |
878 | ||
879 | struct block * | |
880 | get_selected_block () | |
881 | { | |
882 | if (!target_has_stack) | |
883 | return 0; | |
884 | ||
885 | if (!selected_frame) | |
886 | return get_current_block (); | |
887 | return get_frame_block (selected_frame); | |
888 | } | |
889 | ||
890 | /* Find a frame a certain number of levels away from FRAME. | |
891 | LEVEL_OFFSET_PTR points to an int containing the number of levels. | |
892 | Positive means go to earlier frames (up); negative, the reverse. | |
893 | The int that contains the number of levels is counted toward | |
894 | zero as the frames for those levels are found. | |
895 | If the top or bottom frame is reached, that frame is returned, | |
896 | but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates | |
897 | how much farther the original request asked to go. */ | |
898 | ||
899 | FRAME | |
900 | find_relative_frame (frame, level_offset_ptr) | |
901 | register FRAME frame; | |
902 | register int* level_offset_ptr; | |
903 | { | |
904 | register FRAME prev; | |
d8ce1326 | 905 | register FRAME frame1; |
bd5635a1 RP |
906 | |
907 | /* Going up is simple: just do get_prev_frame enough times | |
908 | or until initial frame is reached. */ | |
909 | while (*level_offset_ptr > 0) | |
910 | { | |
911 | prev = get_prev_frame (frame); | |
912 | if (prev == 0) | |
913 | break; | |
914 | (*level_offset_ptr)--; | |
915 | frame = prev; | |
916 | } | |
d8ce1326 | 917 | /* Going down is just as simple. */ |
bd5635a1 RP |
918 | if (*level_offset_ptr < 0) |
919 | { | |
cadbb07a JG |
920 | while (*level_offset_ptr < 0) { |
921 | frame1 = get_next_frame (frame); | |
922 | if (!frame1) | |
923 | break; | |
924 | frame = frame1; | |
925 | (*level_offset_ptr)++; | |
926 | } | |
bd5635a1 RP |
927 | } |
928 | return frame; | |
929 | } | |
930 | ||
b0077123 | 931 | /* The "frame_select" command. With no arg, NOP. |
bd5635a1 RP |
932 | With arg LEVEL_EXP, select the frame at level LEVEL if it is a |
933 | valid level. Otherwise, treat level_exp as an address expression | |
b0077123 | 934 | and select it. See parse_frame_specification for more info on proper |
bd5635a1 RP |
935 | frame expressions. */ |
936 | ||
937 | static void | |
b0077123 | 938 | frame_select_command (level_exp, from_tty) |
bd5635a1 RP |
939 | char *level_exp; |
940 | int from_tty; | |
941 | { | |
942 | register FRAME frame, frame1; | |
943 | unsigned int level = 0; | |
944 | ||
945 | if (!target_has_stack) | |
946 | error ("No stack."); | |
947 | ||
948 | frame = parse_frame_specification (level_exp); | |
949 | ||
cadbb07a JG |
950 | /* Try to figure out what level this frame is. But if there is |
951 | no current stack, don't error out -- let the user set one. */ | |
952 | frame1 = 0; | |
953 | if (get_current_frame()) { | |
954 | for (frame1 = get_prev_frame (0); | |
955 | frame1 && frame1 != frame; | |
956 | frame1 = get_prev_frame (frame1)) | |
957 | level++; | |
958 | } | |
bd5635a1 RP |
959 | |
960 | if (!frame1) | |
961 | level = 0; | |
962 | ||
963 | select_frame (frame, level); | |
b0077123 | 964 | } |
bd5635a1 | 965 | |
b0077123 JG |
966 | /* The "frame" command. With no arg, print selected frame briefly. |
967 | With arg, behaves like frame_select and then prints the selected | |
968 | frame. */ | |
bd5635a1 | 969 | |
b0077123 JG |
970 | static void |
971 | frame_command (level_exp, from_tty) | |
972 | char *level_exp; | |
973 | int from_tty; | |
974 | { | |
975 | frame_select_command (level_exp, from_tty); | |
bd5635a1 RP |
976 | print_stack_frame (selected_frame, selected_frame_level, 1); |
977 | } | |
978 | ||
979 | /* Select the frame up one or COUNT stack levels | |
980 | from the previously selected frame, and print it briefly. */ | |
981 | ||
e1ce8aa5 | 982 | /* ARGSUSED */ |
bd5635a1 RP |
983 | static void |
984 | up_silently_command (count_exp, from_tty) | |
985 | char *count_exp; | |
986 | int from_tty; | |
987 | { | |
988 | register FRAME frame; | |
989 | int count = 1, count1; | |
990 | if (count_exp) | |
991 | count = parse_and_eval_address (count_exp); | |
992 | count1 = count; | |
993 | ||
994 | if (!target_has_stack) | |
995 | error ("No stack."); | |
996 | ||
997 | frame = find_relative_frame (selected_frame, &count1); | |
998 | if (count1 != 0 && count_exp == 0) | |
999 | error ("Initial frame selected; you cannot go up."); | |
1000 | select_frame (frame, selected_frame_level + count - count1); | |
1001 | } | |
1002 | ||
1003 | static void | |
1004 | up_command (count_exp, from_tty) | |
1005 | char *count_exp; | |
1006 | int from_tty; | |
1007 | { | |
1008 | up_silently_command (count_exp, from_tty); | |
1009 | print_stack_frame (selected_frame, selected_frame_level, 1); | |
1010 | } | |
1011 | ||
1012 | /* Select the frame down one or COUNT stack levels | |
1013 | from the previously selected frame, and print it briefly. */ | |
1014 | ||
e1ce8aa5 | 1015 | /* ARGSUSED */ |
bd5635a1 RP |
1016 | static void |
1017 | down_silently_command (count_exp, from_tty) | |
1018 | char *count_exp; | |
1019 | int from_tty; | |
1020 | { | |
1021 | register FRAME frame; | |
1022 | int count = -1, count1; | |
1023 | if (count_exp) | |
1024 | count = - parse_and_eval_address (count_exp); | |
1025 | count1 = count; | |
1026 | ||
89e0bbcd JG |
1027 | if (!target_has_stack) |
1028 | error ("No stack."); | |
1029 | ||
bd5635a1 RP |
1030 | frame = find_relative_frame (selected_frame, &count1); |
1031 | if (count1 != 0 && count_exp == 0) | |
1032 | error ("Bottom (i.e., innermost) frame selected; you cannot go down."); | |
1033 | select_frame (frame, selected_frame_level + count - count1); | |
1034 | } | |
1035 | ||
1036 | ||
1037 | static void | |
1038 | down_command (count_exp, from_tty) | |
1039 | char *count_exp; | |
1040 | int from_tty; | |
1041 | { | |
1042 | down_silently_command (count_exp, from_tty); | |
1043 | print_stack_frame (selected_frame, selected_frame_level, 1); | |
1044 | } | |
1045 | \f | |
1046 | static void | |
1047 | return_command (retval_exp, from_tty) | |
1048 | char *retval_exp; | |
1049 | int from_tty; | |
1050 | { | |
777bef06 JK |
1051 | struct symbol *thisfun; |
1052 | FRAME_ADDR selected_frame_addr; | |
1053 | CORE_ADDR selected_frame_pc; | |
bd5635a1 RP |
1054 | FRAME frame; |
1055 | ||
777bef06 JK |
1056 | if (selected_frame == NULL) |
1057 | error ("No selected frame."); | |
1058 | thisfun = get_frame_function (selected_frame); | |
1059 | selected_frame_addr = FRAME_FP (selected_frame); | |
1060 | selected_frame_pc = (get_frame_info (selected_frame))->pc; | |
1061 | ||
bd5635a1 RP |
1062 | /* If interactive, require confirmation. */ |
1063 | ||
1064 | if (from_tty) | |
1065 | { | |
1066 | if (thisfun != 0) | |
1067 | { | |
1068 | if (!query ("Make %s return now? ", SYMBOL_NAME (thisfun))) | |
1069 | error ("Not confirmed."); | |
1070 | } | |
1071 | else | |
1072 | if (!query ("Make selected stack frame return now? ")) | |
1073 | error ("Not confirmed."); | |
1074 | } | |
1075 | ||
1076 | /* Do the real work. Pop until the specified frame is current. We | |
1077 | use this method because the selected_frame is not valid after | |
1078 | a POP_FRAME. The pc comparison makes this work even if the | |
1079 | selected frame shares its fp with another frame. */ | |
1080 | ||
1081 | while ( selected_frame_addr != FRAME_FP (frame = get_current_frame()) | |
1082 | || selected_frame_pc != (get_frame_info (frame))->pc ) | |
1083 | POP_FRAME; | |
1084 | ||
1085 | /* Then pop that frame. */ | |
1086 | ||
1087 | POP_FRAME; | |
1088 | ||
1089 | /* Compute the return value (if any) and store in the place | |
1090 | for return values. */ | |
1091 | ||
1092 | if (retval_exp) | |
1093 | set_return_value (parse_and_eval (retval_exp)); | |
1094 | ||
1095 | /* If interactive, print the frame that is now current. */ | |
1096 | ||
1097 | if (from_tty) | |
1098 | frame_command ("0", 1); | |
1099 | } | |
89e0bbcd JG |
1100 | |
1101 | /* Gets the language of the current frame. */ | |
1102 | enum language | |
1103 | get_frame_language() | |
1104 | { | |
1105 | register struct symtab *s; | |
1106 | FRAME fr; | |
1107 | enum language flang; /* The language of the current frame */ | |
1108 | ||
1109 | fr = get_frame_info(selected_frame); | |
1110 | if(fr) | |
1111 | { | |
1112 | s = find_pc_symtab(fr->pc); | |
1113 | if(s) | |
1114 | flang = s->language; | |
1115 | else | |
1116 | flang = language_unknown; | |
1117 | } | |
1118 | else | |
1119 | flang = language_unknown; | |
1120 | ||
1121 | return flang; | |
1122 | } | |
bd5635a1 RP |
1123 | \f |
1124 | void | |
1125 | _initialize_stack () | |
1126 | { | |
1127 | #if 0 | |
1128 | backtrace_limit = 30; | |
1129 | #endif | |
1130 | ||
1131 | add_com ("return", class_stack, return_command, | |
1132 | "Make selected stack frame return to its caller.\n\ | |
1133 | Control remains in the debugger, but when you continue\n\ | |
1134 | execution will resume in the frame above the one now selected.\n\ | |
1135 | If an argument is given, it is an expression for the value to return."); | |
1136 | ||
1137 | add_com ("up", class_stack, up_command, | |
1138 | "Select and print stack frame that called this one.\n\ | |
1139 | An argument says how many frames up to go."); | |
1140 | add_com ("up-silently", class_support, up_silently_command, | |
1141 | "Same as the `up' command, but does not print anything.\n\ | |
1142 | This is useful in command scripts."); | |
1143 | ||
1144 | add_com ("down", class_stack, down_command, | |
1145 | "Select and print stack frame called by this one.\n\ | |
1146 | An argument says how many frames down to go."); | |
1147 | add_com_alias ("do", "down", class_stack, 1); | |
1148 | add_com ("down-silently", class_support, down_silently_command, | |
1149 | "Same as the `down' command, but does not print anything.\n\ | |
1150 | This is useful in command scripts."); | |
1151 | ||
1152 | add_com ("frame", class_stack, frame_command, | |
1153 | "Select and print a stack frame.\n\ | |
1154 | With no argument, print the selected stack frame. (See also \"info frame\").\n\ | |
1155 | An argument specifies the frame to select.\n\ | |
1156 | It can be a stack frame number or the address of the frame.\n\ | |
1157 | With argument, nothing is printed if input is coming from\n\ | |
1158 | a command file or a user-defined command."); | |
1159 | ||
1160 | add_com_alias ("f", "frame", class_stack, 1); | |
1161 | ||
b0077123 JG |
1162 | add_com ("frame-select", class_stack, frame_select_command, |
1163 | "Select a stack frame without printing anything.\n\ | |
1164 | An argument specifies the frame to select.\n\ | |
1165 | It can be a stack frame number or the address of the frame.\n"); | |
1166 | ||
bd5635a1 RP |
1167 | add_com ("backtrace", class_stack, backtrace_command, |
1168 | "Print backtrace of all stack frames, or innermost COUNT frames.\n\ | |
1169 | With a negative argument, print outermost -COUNT frames."); | |
1170 | add_com_alias ("bt", "backtrace", class_stack, 0); | |
1171 | add_com_alias ("where", "backtrace", class_alias, 0); | |
1172 | add_info ("stack", backtrace_command, | |
1173 | "Backtrace of the stack, or innermost COUNT frames."); | |
1174 | add_info_alias ("s", "stack", 1); | |
1175 | add_info ("frame", frame_info, | |
1176 | "All about selected stack frame, or frame at ADDR."); | |
1177 | add_info_alias ("f", "frame", 1); | |
1178 | add_info ("locals", locals_info, | |
1179 | "Local variables of current stack frame."); | |
1180 | add_info ("args", args_info, | |
1181 | "Argument variables of current stack frame."); | |
1182 | add_info ("catch", catch_info, | |
1183 | "Exceptions that can be caught in the current stack frame."); | |
1184 | ||
1185 | #if 0 | |
1186 | add_cmd ("backtrace-limit", class_stack, set_backtrace_limit_command, | |
1187 | "Specify maximum number of frames for \"backtrace\" to print by default.", | |
1188 | &setlist); | |
1189 | add_info ("backtrace-limit", backtrace_limit_info, | |
1190 | "The maximum number of frames for \"backtrace\" to print by default."); | |
1191 | #endif | |
1192 | } |