gdb-2.4+.aux.coff
[deliverable/binutils-gdb.git] / gdb / stack.c
1 /* Print and select stack frames for GDB, the GNU debugger.
2 Copyright (C) 1986, 1987 Free Software Foundation, Inc.
3
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
9
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
16
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
19 */
20
21 #include <stdio.h>
22
23 #include "defs.h"
24 #include "initialize.h"
25 #include "param.h"
26 #include "symtab.h"
27 #include "frame.h"
28
29 START_FILE
30
31 /* Thie "selected" stack frame is used by default for local and arg access.
32 May be zero, for no selected frame. */
33
34 FRAME selected_frame;
35
36 /* Level of the selected frame:
37 0 for innermost, 1 for its caller, ...
38 or -1 for frame specified by address with no defined level. */
39
40 int selected_frame_level;
41
42 static void select_calling_frame ();
43
44 void print_frame_info ();
45 \f
46 /* Print a stack frame briefly. FRAME should be the frame address
47 and LEVEL should be its level in the stack (or -1 for level not defined).
48 This prints the level, the function executing, the arguments,
49 and the file name and line number.
50 If the pc is not at the beginning of the source line,
51 the actual pc is printed at the beginning.
52
53 If SOURCE is 1, print the source line as well.
54 If SOURCE is -1, print ONLY the source line. */
55
56 static void
57 print_stack_frame (frame, level, source)
58 FRAME frame;
59 int level;
60 int source;
61 {
62 struct frame_info fi;
63
64 fi = get_frame_info (frame);
65
66 print_frame_info (&fi, level, source, 1);
67 }
68
69 void
70 print_frame_info (fi, level, source, args)
71 struct frame_info *fi;
72 register int level;
73 int source;
74 int args;
75 {
76 register FRAME frame = fi->frame;
77 struct symtab_and_line sal;
78 struct symbol *func;
79 register char *funname = 0;
80 int numargs;
81
82 sal = find_pc_line (fi->pc, fi->next_frame);
83 func = get_frame_function (frame);
84 if (func)
85 funname = SYMBOL_NAME (func);
86 else
87 {
88 register int misc_index = find_pc_misc_function (fi->pc);
89 if (misc_index >= 0)
90 funname = misc_function_vector[misc_index].name;
91 }
92
93 if (source >= 0 || !sal.symtab)
94 {
95 /* This avoids a bug in cc on the sun. */
96 struct frame_info tem;
97 tem = *fi;
98
99 if (level >= 0)
100 printf ("#%-2d ", level);
101 if (fi->pc != sal.pc || !sal.symtab)
102 printf ("0x%x in ", fi->pc);
103 printf ("%s (", funname ? funname : "??");
104 if (args)
105 {
106 FRAME_NUM_ARGS (numargs, tem);
107 print_frame_args (func, FRAME_ARGS_ADDRESS (tem), numargs, stdout);
108 }
109 printf (")");
110 if (sal.symtab)
111 printf (" (%s line %d)", sal.symtab->filename, sal.line);
112 printf ("\n");
113 }
114
115 if (source != 0 && sal.symtab)
116 {
117 if (source < 0 && fi->pc != sal.pc)
118 printf ("0x%x\t", fi->pc);
119 print_source_lines (sal.symtab, sal.line, sal.line + 1);
120 current_source_line = max (sal.line - 5, 1);
121 }
122 if (source != 0)
123 set_default_breakpoint (1, fi->pc, sal.symtab, sal.line);
124
125 fflush (stdout);
126 }
127
128 /* Call here to print info on selected frame, after a trap. */
129
130 void
131 print_sel_frame (just_source)
132 int just_source;
133 {
134 print_stack_frame (selected_frame, -1, just_source ? -1 : 1);
135 }
136
137 /* Print info on the selected frame, including level number
138 but not source. */
139
140 print_selected_frame ()
141 {
142 print_stack_frame (selected_frame, selected_frame_level, 0);
143 }
144
145 /* Print verbosely the selected frame or the frame at address ADDR.
146 This means absolutely all information in the frame is printed. */
147
148 static void
149 frame_info (addr_exp)
150 char *addr_exp;
151 {
152 FRAME frame = addr_exp ? parse_and_eval_address (addr_exp) : selected_frame;
153 struct frame_info fi;
154 struct frame_saved_regs fsr;
155 struct symtab_and_line sal;
156 struct symbol *func;
157 FRAME calling_frame;
158 int i, count;
159 char *funname = 0;
160 int numargs;
161
162 fi = get_frame_info (frame);
163 get_frame_saved_regs (&fi, &fsr);
164 sal = find_pc_line (fi.pc, fi.next_frame);
165 func = get_frame_function (frame);
166 if (func)
167 funname = SYMBOL_NAME (func);
168 else
169 {
170 register int misc_index = find_pc_misc_function (fi.pc);
171 if (misc_index >= 0)
172 funname = misc_function_vector[misc_index].name;
173 }
174 calling_frame = get_prev_frame (frame);
175
176 if (!addr_exp && selected_frame_level >= 0)
177 printf ("Stack level %d, frame at 0x%x:\n pc = 0x%x",
178 selected_frame_level, frame, fi.pc);
179 else
180 printf ("Stack frame at 0x%x:\n pc = 0x%x",
181 frame, fi.pc);
182
183 if (funname)
184 printf (" in %s", funname);
185 if (sal.symtab)
186 printf (" (%s line %d)", sal.symtab->filename, sal.line);
187 printf ("; saved pc 0x%x\n", FRAME_SAVED_PC (frame));
188 if (calling_frame)
189 printf (" called by frame at 0x%x", calling_frame);
190 if (fi.next_frame && calling_frame)
191 printf (",");
192 if (fi.next_frame)
193 printf (" caller of frame at 0x%x", fi.next_frame);
194 if (fi.next_frame || calling_frame)
195 printf ("\n");
196 printf (" Arglist at 0x%x,", FRAME_ARGS_ADDRESS (fi));
197 FRAME_NUM_ARGS (i, fi);
198 if (i < 0)
199 printf (" args: ");
200 else if (i == 0)
201 printf (" no args.");
202 else if (i == 1)
203 printf (" 1 arg: ");
204 else
205 printf (" %d args: ", i);
206
207 FRAME_NUM_ARGS (numargs, fi);
208 print_frame_args (func, FRAME_ARGS_ADDRESS (fi), numargs, stdout);
209 printf ("\n");
210 count = 0;
211 for (i = 0; i < NUM_REGS; i++)
212 if (fsr.regs[i])
213 {
214 if (count % 4 != 0)
215 printf (", ");
216 else
217 {
218 if (count == 0)
219 printf (" Saved registers:");
220 printf ("\n ");
221 }
222 printf ("%s at 0x%x", reg_names[i], fsr.regs[i]);
223 count++;
224 }
225 if (count)
226 printf ("\n");
227 }
228
229 /* Print briefly all stack frames or just the innermost COUNT frames. */
230
231 static void
232 backtrace_command (count_exp)
233 char *count_exp;
234 {
235 struct frame_info fi;
236 register int count;
237 register FRAME frame;
238 register int i;
239
240 if (count_exp)
241 count = parse_and_eval_address (count_exp);
242 else
243 count = -1;
244
245 for (i = 0, frame = get_current_frame (), fi = get_frame_info (frame);
246 frame && count--;
247 i++, fi = get_prev_frame_info (fi.frame), frame = fi.frame)
248 {
249 QUIT;
250 print_frame_info (&fi, i, 0, 1);
251 }
252 }
253 \f
254 /* Print the local variables of a block B active in FRAME. */
255
256 static void
257 print_block_frame_locals (b, frame, stream)
258 struct block *b;
259 register FRAME frame;
260 register FILE *stream;
261 {
262 int nsyms;
263 register int i;
264 register struct symbol *sym;
265
266 nsyms = BLOCK_NSYMS (b);
267
268 for (i = 0; i < nsyms; i++)
269 {
270 sym = BLOCK_SYM (b, i);
271 if (SYMBOL_CLASS (sym) == LOC_LOCAL
272 || SYMBOL_CLASS (sym) == LOC_REGISTER)
273 {
274 fprintf (stream, "%s = ", SYMBOL_NAME (sym));
275 print_variable_value (sym, frame, stream);
276 fprintf (stream, "\n");
277 fflush (stream);
278 }
279 }
280 }
281
282 /* Print on STREAM all the local variables in frame FRAME,
283 including all the blocks active in that frame
284 at its current pc.
285
286 Returns 1 if the job was done,
287 or 0 if nothing was printed because we have no info
288 on the function running in FRAME. */
289
290 static int
291 print_frame_local_vars (frame, stream)
292 register FRAME frame;
293 register FILE *stream;
294 {
295 register struct block *block = get_frame_block (frame);
296 if (block == 0)
297 return 0;
298 while (block != 0)
299 {
300 print_block_frame_locals (block, frame, stream);
301 /* After handling the function's top-level block, stop.
302 Don't continue to its superblock, the block of
303 per-file symbols. */
304 if (BLOCK_FUNCTION (block))
305 break;
306 block = BLOCK_SUPERBLOCK (block);
307 }
308 return 1;
309 }
310
311 static void
312 locals_info ()
313 {
314 print_frame_local_vars (selected_frame, stdout);
315 }
316
317 static int
318 print_frame_arg_vars (frame, stream)
319 register FRAME frame;
320 register FILE *stream;
321 {
322 struct symbol *func = get_frame_function (frame);
323 register struct block *b;
324 int nsyms;
325 register int i;
326 register struct symbol *sym;
327
328 if (func == 0)
329 return 0;
330
331 b = SYMBOL_BLOCK_VALUE (func);
332 nsyms = BLOCK_NSYMS (b);
333
334 for (i = 0; i < nsyms; i++)
335 {
336 sym = BLOCK_SYM (b, i);
337 if (SYMBOL_CLASS (sym) == LOC_ARG)
338 {
339 fprintf (stream, "%s = ", SYMBOL_NAME (sym));
340 print_variable_value (sym, frame, stream);
341 fprintf (stream, "\n");
342 fflush (stream);
343 }
344 }
345
346 return 1;
347 }
348
349 static void
350 args_info ()
351 {
352 print_frame_arg_vars (selected_frame, stdout);
353 }
354 \f
355 /* Select frame FRAME, and note that its stack level is LEVEL.
356 LEVEL may be -1 if an actual level number is not known. */
357
358 void
359 select_frame (frame, level)
360 FRAME frame;
361 int level;
362 {
363 selected_frame = frame;
364 selected_frame_level = level;
365 }
366
367 /* Store the selected frame and its level into *FRAMEP and *LEVELP. */
368
369 void
370 record_selected_frame (framep, levelp)
371 FRAME *framep;
372 int *levelp;
373 {
374 *framep = selected_frame;
375 *levelp = selected_frame_level;
376 }
377
378 /* Return the symbol-block in which the selected frame is executing.
379 Can return zero under various legitimate circumstances. */
380
381 struct block *
382 get_selected_block ()
383 {
384 if (!have_inferior_p () && !have_core_file_p ())
385 return 0;
386
387 if (!selected_frame)
388 return get_current_block ();
389 return get_frame_block (selected_frame);
390 }
391
392 /* Find a frame a certain number of levels away from FRAME.
393 LEVEL_OFFSET_PTR points to an int containing the number of levels.
394 Positive means go to earlier frames (up); negative, the reverse.
395 The int that contains the number of levels is counted toward
396 zero as the frames for those levels are found.
397 If the top or bottom frame is reached, that frame is returned,
398 but the final value of *LEVEL_OFFSET_PTR is nonzero and indicates
399 how much farther the original request asked to go. */
400
401 FRAME
402 find_relative_frame (frame, level_offset_ptr)
403 register FRAME frame;
404 register int* level_offset_ptr;
405 {
406 register FRAME prev;
407 struct frame_info fi;
408 register FRAME frame1, frame2;
409
410 /* Going up is simple: just do get_prev_frame enough times
411 or until initial frame is reached. */
412 while (*level_offset_ptr > 0)
413 {
414 prev = get_prev_frame (frame);
415 if (prev == 0)
416 break;
417 (*level_offset_ptr)--;
418 frame = prev;
419 }
420 /* Going down could be done by iterating get_frame_info to
421 find the next frame, but that would be quadratic
422 since get_frame_info must scan all the way from the current frame.
423 The following algotithm is linear. */
424 if (*level_offset_ptr < 0)
425 {
426 /* First put frame1 at innermost frame
427 and frame2 N levels up from there. */
428 frame1 = get_current_frame ();
429 frame2 = frame1;
430 while (*level_offset_ptr < 0 && frame2 != frame)
431 {
432 frame2 = get_prev_frame (frame2);
433 (*level_offset_ptr) ++;
434 }
435 /* Then slide frame1 and frame2 up in synchrony
436 and when frame2 reaches our starting point
437 frame1 must be N levels down from there. */
438 while (frame2 != frame)
439 {
440 frame1 = get_prev_frame (frame1);
441 frame2 = get_prev_frame (frame2);
442 }
443 return frame1;
444 }
445 return frame;
446 }
447
448 /* The "frame" command. With no arg, print selected frame briefly.
449 With arg LEVEL, select the frame at level LEVEL and print it.
450 With arg larger than 100000, use it as address of frame to select.
451 If from command file or user-defined command, don't print anything
452 if we have an argument. */
453
454 static void
455 frame_command (level_exp, from_tty)
456 char *level_exp;
457 int from_tty;
458 {
459 register int i;
460 register FRAME frame;
461 unsigned int level, level1;
462
463 if (level_exp)
464 {
465 level1 = level = parse_and_eval_address (level_exp);
466 if (level > 100000)
467 {
468 select_frame (level, -1);
469 frame_info (0);
470 return;
471 }
472
473 frame = find_relative_frame (get_current_frame (), &level1);
474 if (level1 != 0)
475 error ("Stack level %d is out of range.", level);
476 select_frame (frame, level);
477 if (! from_tty)
478 return;
479 }
480
481 print_stack_frame (selected_frame, selected_frame_level, 1);
482 }
483
484 /* Select the frame up one or COUNT stack levels
485 from the previously selected frame, and print it briefly. */
486
487 static void
488 up_command (count_exp)
489 char *count_exp;
490 {
491 register FRAME frame;
492 int count = 1, count1;
493 if (count_exp)
494 count = parse_and_eval_address (count_exp);
495 count1 = count;
496
497 frame = find_relative_frame (selected_frame, &count1);
498 if (count1 != 0 && count_exp == 0)
499 error ("Initial frame selected; you cannot go up.");
500 select_frame (frame, selected_frame_level + count - count1);
501
502 print_stack_frame (selected_frame, selected_frame_level, 1);
503 }
504
505 /* Select the frame down one or COUNT stack levels
506 from the previously selected frame, and print it briefly. */
507
508 static void
509 down_command (count_exp)
510 char *count_exp;
511 {
512 register FRAME frame;
513 int count = -1, count1;
514 if (count_exp)
515 count = - parse_and_eval_address (count_exp);
516 count1 = count;
517
518 frame = find_relative_frame (selected_frame, &count1);
519 if (count1 != 0 && count_exp == 0)
520 error ("Bottom (i.e., innermost) frame selected; you cannot go down.");
521 select_frame (frame, selected_frame_level + count - count1);
522
523 print_stack_frame (selected_frame, selected_frame_level, 1);
524 }
525 \f
526 static void
527 return_command (retval_exp, from_tty)
528 char *retval_exp;
529 int from_tty;
530 {
531 struct symbol *thisfun = get_frame_function (selected_frame);
532
533 /* If interactive, require confirmation. */
534
535 if (from_tty)
536 {
537 if (thisfun != 0)
538 {
539 if (!query ("Make %s return now? ", SYMBOL_NAME (thisfun)))
540 error ("Not confirmed.");
541 }
542 else
543 if (!query ("Make selected stack frame return now? "))
544 error ("Not confirmed.");
545 }
546
547 /* Do the real work. Pop until the specified frame is current. */
548
549 while (selected_frame != get_current_frame ())
550 POP_FRAME;
551
552 /* Then pop that frame. */
553
554 POP_FRAME;
555
556 /* Compute the return value (if any) and store in the place
557 for return values. */
558
559 if (retval_exp)
560 set_return_value (parse_and_eval (retval_exp));
561
562 /* If interactive, print the frame that is now current. */
563
564 if (from_tty)
565 frame_command ("0", 1);
566 }
567 \f
568 static
569 initialize ()
570 {
571 add_com ("return", class_stack, return_command,
572 "Make selected stack frame return to its caller.\n\
573 Control remains in the debugger, but when you continue\n\
574 execution will resume in the frame above the one now selected.\n\
575 If an argument is given, it is an expression for the value to return.");
576
577 add_com ("up", class_stack, up_command,
578 "Select and print stack frame that called this one.\n\
579 An argument says how many frames up to go.");
580
581 add_com ("down", class_stack, down_command,
582 "Select and print stack frame called by this one.\n\
583 An argument says how many frames down to go.");
584 add_com_alias ("do", "down", class_stack, 1);
585
586 add_com ("frame", class_stack, frame_command,
587 "Select and print a stack frame.\n\
588 With no argument, print the selected stack frame. (See also \"info frame\").\n\
589 An argument specifies the frame to select.\n\
590 It can be a stack frame number or the address of the frame.\n\
591 With argument, nothing is printed if input is coming from\n\
592 a command file or a user-defined command.");
593
594 add_com_alias ("f", "frame", class_stack, 1);
595
596 add_com ("backtrace", class_stack, backtrace_command,
597 "Print backtrace of all stack frames, or innermost COUNT frames.");
598 add_com_alias ("bt", "backtrace", class_stack, 0);
599 add_com_alias ("where", "backtrace", class_alias, 0);
600 add_info ("stack", backtrace_command,
601 "Backtrace of the stack, or innermost COUNT frames.");
602 add_info_alias ("s", "stack", 1);
603 add_info ("frame", frame_info,
604 "All about selected stack frame, or frame at ADDR.");
605 add_info_alias ("f", "frame", 1);
606 add_info ("locals", locals_info,
607 "Local variables of current stack frame.");
608 add_info ("args", args_info,
609 "Argument variables of current stack frame.");
610 }
611
612 END_FILE
This page took 0.04634 seconds and 5 git commands to generate.