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