1 /* Representation of stack frame debug information
2 * Copyright (C) 1996,1997 Free Software Foundation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this software; see the file COPYING. If not, write to
16 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307 USA
19 * As a special exception, the Free Software Foundation gives permission
20 * for additional uses of the text contained in its release of GUILE.
22 * The exception is that, if you link the GUILE library with other files
23 * to produce an executable, this does not by itself cause the
24 * resulting executable to be covered by the GNU General Public License.
25 * Your use of that executable is in no way restricted on account of
26 * linking the GUILE library code into it.
28 * This exception does not however invalidate any other reasons why
29 * the executable file might be covered by the GNU General Public License.
31 * This exception applies only to the code released by the
32 * Free Software Foundation under the name GUILE. If you copy
33 * code from other Free Software Foundation releases into a copy of
34 * GUILE, as the General Public License permits, the exception does
35 * not apply to the code that you add in this way. To avoid misleading
36 * anyone as to the status of such modified files, you must delete
37 * this exception notice from them.
39 * If you write modifications of your own for GUILE, it is your choice
40 * whether to permit this exception to apply to your modifications.
41 * If you do not wish that, delete this exception notice.
43 * The author can be reached at djurfeldt@nada.kth.se
44 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
46 /* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
55 #include "continuations.h"
67 /* {Frames and stacks}
69 * The debugging evaluator creates debug frames on the stack. These
70 * are linked from the innermost frame and outwards. The last frame
71 * created can always be accessed as SCM_LAST_DEBUG_FRAME.
72 * Continuations contain a pointer to the innermost debug frame on the
75 * Each debug frame contains a set of flags and information about one
76 * or more stack frames. The case of multiple frames occurs due to
77 * tail recursion. The maximal number of stack frames which can be
78 * recorded in one debug frame can be set dynamically with the debug
81 * Stack frame information is of two types: eval information (the
82 * expression being evaluated and its environment) and apply
83 * information (the procedure being applied and its arguments). A
84 * stack frame normally corresponds to an eval/apply pair, but macros
85 * and special forms (which are implemented as macros in Guile) only
86 * have eval information and apply calls leads to apply only frames.
88 * Since we want to record the total stack information and later
89 * manipulate this data at the scheme level in the debugger, we need
90 * to transform it into a new representation. In the following code
91 * section you'll find the functions implementing this data type.
95 * The stack is represented as a struct with an id slot and a tail
96 * array of scm_info_frame structs.
98 * A frame is represented as a pair where the car contains a stack and
99 * the cdr an inum. The inum is an index to the first SCM value of
100 * the scm_info_frame struct.
124 * frame-evaluating-args?
129 /* Some auxiliary functions for reading debug frames off the stack.
132 /* Stacks often contain pointers to other items on the stack; for
133 example, each scm_debug_frame structure contains a pointer to the
134 next frame out. When we capture a continuation, we copy the stack
135 into the heap, and just leave all the pointers unchanged. This
136 makes it simple to restore the continuation --- just copy the stack
137 back! However, if we retrieve a pointer from the heap copy to
138 another item that was originally on the stack, we have to add an
139 offset to the pointer to discover the new referent.
141 If PTR is a pointer retrieved from a continuation, whose original
142 target was on the stack, and OFFSET is the appropriate offset from
143 the original stack to the continuation, then RELOC_MUMBLE (PTR,
144 OFFSET) is a pointer to the copy in the continuation of the
145 original referent, cast to an scm_debug_MUMBLE *. */
146 #define RELOC_INFO(ptr, offset) \
147 ((scm_debug_info *) ((SCM_STACKITEM *) (ptr) + (offset)))
148 #define RELOC_FRAME(ptr, offset) \
149 ((scm_debug_frame *) ((SCM_STACKITEM *) (ptr) + (offset)))
152 /* Count number of debug info frames on a stack, beginning with
153 * DFRAME. OFFSET is used for relocation of pointers when the stack
154 * is read from a continuation.
157 stack_depth (scm_debug_frame
*dframe
,long offset
,SCM
*id
,int *maxp
)
160 int max_depth
= SCM_BACKTRACE_MAXDEPTH
;
161 scm_debug_info
*info
;
163 dframe
&& !SCM_VOIDFRAMEP (*dframe
) && n
< max_depth
;
164 dframe
= RELOC_FRAME (dframe
->prev
, offset
))
166 if (SCM_EVALFRAMEP (*dframe
))
168 size
= dframe
->status
& SCM_MAX_FRAME_SIZE
;
169 info
= RELOC_INFO (dframe
->info
, offset
);
170 n
+= (info
- dframe
->vect
) / 2 + 1;
171 /* Data in the apply part of an eval info frame comes from previous
172 stack frame if the scm_debug_info vector is overflowed. */
173 if ((((info
- dframe
->vect
) & 1) == 0)
174 && SCM_OVERFLOWP (*dframe
)
175 && !SCM_UNBNDP (info
[1].a
.proc
))
181 if (dframe
&& SCM_VOIDFRAMEP (*dframe
))
182 *id
= dframe
->vect
[0].id
;
188 /* Read debug info from DFRAME into IFRAME.
191 read_frame (scm_debug_frame
*dframe
,long offset
,scm_info_frame
*iframe
)
193 scm_bits_t flags
= SCM_UNPACK (SCM_INUM0
); /* UGh. */
195 scm_debug_info
*info
;
196 if (SCM_EVALFRAMEP (*dframe
))
198 size
= dframe
->status
& SCM_MAX_FRAME_SIZE
;
199 info
= RELOC_INFO (dframe
->info
, offset
);
200 if ((info
- dframe
->vect
) & 1)
202 /* Debug.vect ends with apply info. */
204 if (info
[1].a
.proc
!= SCM_UNDEFINED
)
206 flags
|= SCM_FRAMEF_PROC
;
207 iframe
->proc
= info
[1].a
.proc
;
208 iframe
->args
= info
[1].a
.args
;
209 if (!SCM_ARGS_READY_P (*dframe
))
210 flags
|= SCM_FRAMEF_EVAL_ARGS
;
213 iframe
->source
= scm_make_memoized (info
[0].e
.exp
, info
[0].e
.env
);
217 flags
|= SCM_FRAMEF_PROC
;
218 iframe
->proc
= dframe
->vect
[0].a
.proc
;
219 iframe
->args
= dframe
->vect
[0].a
.args
;
221 iframe
->flags
= flags
;
224 /* Look up the first body form of the apply closure. We'll use this
225 below to prevent it from being displayed.
230 SCM proc
= SCM_CDR (scm_sym2vcell (scm_sym_apply
, SCM_BOOL_F
, SCM_BOOL_F
));
231 if (SCM_CLOSUREP (proc
))
232 return SCM_CADR (SCM_CODE (proc
));
234 return SCM_UNDEFINED
;
237 #define NEXT_FRAME(iframe, n, quit) \
239 if (SCM_NIMP (iframe->source) \
240 && SCM_MEMOIZED_EXP (iframe->source) == applybody) \
242 iframe->source = SCM_BOOL_F; \
243 if (SCM_FALSEP (iframe->proc)) \
255 /* Fill the scm_info_frame vector IFRAME with data from N stack frames
256 * starting with the first stack frame represented by debug frame
261 read_frames (scm_debug_frame
*dframe
,long offset
,int n
,scm_info_frame
*iframes
)
264 scm_info_frame
*iframe
= iframes
;
265 scm_debug_info
*info
;
266 static SCM applybody
= SCM_UNDEFINED
;
268 /* The value of applybody has to be setup after r4rs.scm has executed. */
269 if (SCM_UNBNDP (applybody
))
270 applybody
= get_applybody ();
272 dframe
&& !SCM_VOIDFRAMEP (*dframe
) && n
> 0;
273 dframe
= RELOC_FRAME (dframe
->prev
, offset
))
275 read_frame (dframe
, offset
, iframe
);
276 if (SCM_EVALFRAMEP (*dframe
))
278 /* If current frame is a macro during expansion, we should
279 skip the previously recorded macro transformer
280 application frame. */
281 if (SCM_MACROEXPP (*dframe
) && iframe
> iframes
)
283 *(iframe
- 1) = *iframe
;
286 size
= dframe
->status
& SCM_MAX_FRAME_SIZE
;
287 info
= RELOC_INFO (dframe
->info
, offset
);
288 if ((info
- dframe
->vect
) & 1)
290 /* Data in the apply part of an eval info frame comes from
291 previous stack frame if the scm_debug_info vector is overflowed. */
292 else if (SCM_OVERFLOWP (*dframe
)
293 && !SCM_UNBNDP (info
[1].a
.proc
))
295 NEXT_FRAME (iframe
, n
, quit
);
296 iframe
->flags
= SCM_UNPACK(SCM_INUM0
) | SCM_FRAMEF_PROC
;
297 iframe
->proc
= info
[1].a
.proc
;
298 iframe
->args
= info
[1].a
.args
;
300 if (SCM_OVERFLOWP (*dframe
))
301 iframe
->flags
|= SCM_FRAMEF_OVERFLOW
;
303 NEXT_FRAME (iframe
, n
, quit
);
304 while (info
>= dframe
->vect
)
306 if (!SCM_UNBNDP (info
[1].a
.proc
))
308 iframe
->flags
= SCM_UNPACK(SCM_INUM0
) | SCM_FRAMEF_PROC
;
309 iframe
->proc
= info
[1].a
.proc
;
310 iframe
->args
= info
[1].a
.args
;
313 iframe
->flags
= SCM_UNPACK (SCM_INUM0
);
314 iframe
->source
= scm_make_memoized (info
[0].e
.exp
,
317 NEXT_FRAME (iframe
, n
, quit
);
320 else if (iframe
->proc
== scm_f_gsubr_apply
)
321 /* Skip gsubr apply frames. */
325 NEXT_FRAME (iframe
, n
, quit
);
328 if (iframe
> iframes
)
329 (iframe
- 1) -> flags
|= SCM_FRAMEF_REAL
;
331 return iframe
- iframes
; /* Number of frames actually read */
334 /* Narrow STACK by cutting away stackframes (mutatingly).
336 * Inner frames (most recent) are cut by advancing the frames pointer.
337 * Outer frames are cut by decreasing the recorded length.
339 * Cut maximally INNER inner frames and OUTER outer frames using
340 * the keys INNER_KEY and OUTER_KEY.
342 * Frames are cut away starting at the end points and moving towards
343 * the center of the stack. The key is normally compared to the
344 * operator in application frames. Frames up to and including the key
347 * If INNER_KEY is #t a different scheme is used for inner frames:
349 * Frames up to but excluding the first source frame originating from
350 * a user module are cut, except for possible application frames
351 * between the user frame and the last system frame previously
356 narrow_stack (SCM stack
,int inner
,SCM inner_key
,int outer
,SCM outer_key
)
358 scm_stack
*s
= SCM_STACK (stack
);
362 /* Cut inner part. */
363 if (inner_key
== SCM_BOOL_T
)
364 /* Cut all frames up to user module code */
366 for (i
= 0; inner
; ++i
, --inner
)
368 SCM m
= s
->frames
[i
].source
;
369 if ( SCM_MEMOIZEDP (m
)
370 && SCM_NIMP (SCM_MEMOIZED_ENV (m
))
371 && SCM_FALSEP (scm_system_module_env_p (SCM_MEMOIZED_ENV (m
))))
373 /* Back up in order to include any non-source frames */
375 && !((m
= s
->frames
[i
- 1].source
, SCM_MEMOIZEDP (m
))
376 || (SCM_NIMP (m
= s
->frames
[i
- 1].proc
)
377 && SCM_NFALSEP (scm_procedure_p (m
))
378 && SCM_NFALSEP (scm_procedure_property
379 (m
, scm_sym_system_procedure
)))))
389 /* Use standard cutting procedure. */
391 for (i
= 0; inner
; --inner
)
392 if (s
->frames
[i
++].proc
== inner_key
)
395 s
->frames
= &s
->frames
[i
];
398 /* Cut outer part. */
399 for (; n
&& outer
; --outer
)
400 if (s
->frames
[--n
].proc
== outer_key
)
413 SCM_DEFINE (scm_stack_p
, "stack?", 1, 0, 0,
415 "Return @code{#t} if @var{obj} is a calling stack.")
416 #define FUNC_NAME s_scm_stack_p
418 return SCM_BOOL(SCM_STACKP (obj
));
422 SCM_DEFINE (scm_make_stack
, "make-stack", 0, 0, 1,
425 #define FUNC_NAME s_scm_make_stack
428 scm_debug_frame
*dframe
= scm_last_debug_frame
;
429 scm_info_frame
*iframe
;
432 SCM obj
, inner_cut
, outer_cut
;
434 SCM_ASSERT (SCM_CONSP (args
),
435 SCM_FUNC_NAME
, SCM_WNA
, NULL
);
436 obj
= SCM_CAR (args
);
437 args
= SCM_CDR (args
);
439 /* Extract a pointer to the innermost frame of whatever object
440 scm_make_stack was given. */
441 /* just use dframe == scm_last_debug_frame
442 (from initialization of dframe, above) if obj is #t */
443 if (obj
!= SCM_BOOL_T
)
445 SCM_ASSERT (SCM_NIMP (obj
), obj
, SCM_ARG1
, FUNC_NAME
);
446 if (SCM_DEBUGOBJP (obj
))
447 dframe
= (scm_debug_frame
*) SCM_DEBUGOBJ_FRAME (obj
);
448 else if (scm_tc7_contin
== SCM_TYP7 (obj
))
450 offset
= ((SCM_STACKITEM
*) (SCM_CHARS (obj
) + sizeof (scm_contregs
))
452 #ifndef STACK_GROWS_UP
453 offset
+= SCM_LENGTH (obj
);
455 dframe
= RELOC_FRAME (SCM_DFRAME (obj
), offset
);
459 SCM_WTA (SCM_ARG1
, obj
);
464 /* Count number of frames. Also get stack id tag and check whether
465 there are more stackframes than we want to record
466 (SCM_BACKTRACE_MAXDEPTH). */
469 n
= stack_depth (dframe
, offset
, &id
, &maxp
);
470 size
= n
* SCM_FRAME_N_SLOTS
;
472 /* Make the stack object. */
473 stack
= scm_make_struct (scm_stack_type
, SCM_MAKINUM (size
), SCM_EOL
);
474 SCM_STACK (stack
) -> id
= id
;
475 iframe
= &SCM_STACK (stack
) -> tail
[0];
476 SCM_STACK (stack
) -> frames
= iframe
;
478 /* Translate the current chain of stack frames into debugging information. */
479 n
= read_frames (RELOC_FRAME (dframe
, offset
), offset
, n
, iframe
);
480 SCM_STACK (stack
) -> length
= n
;
482 /* Narrow the stack according to the arguments given to scm_make_stack. */
483 while (n
> 0 && SCM_CONSP (args
))
485 inner_cut
= SCM_CAR (args
);
486 args
= SCM_CDR (args
);
487 if (SCM_CONSP (args
))
489 outer_cut
= SCM_CAR (args
);
490 args
= SCM_CDR (args
);
493 outer_cut
= SCM_INUM0
;
496 SCM_INUMP (inner_cut
) ? SCM_INUM (inner_cut
) : n
,
497 SCM_INUMP (inner_cut
) ? 0 : inner_cut
,
498 SCM_INUMP (outer_cut
) ? SCM_INUM (outer_cut
) : n
,
499 SCM_INUMP (outer_cut
) ? 0 : outer_cut
);
501 n
= SCM_STACK (stack
) -> length
;
507 iframe
[n
- 1].flags
|= SCM_FRAMEF_OVERFLOW
;
515 SCM_DEFINE (scm_stack_id
, "stack-id", 1, 0, 0,
517 "Return the identifier given to @var{stack} by @code{start-stack}.")
518 #define FUNC_NAME s_scm_stack_id
520 scm_debug_frame
*dframe
;
522 if (stack
== SCM_BOOL_T
)
523 dframe
= scm_last_debug_frame
;
526 SCM_VALIDATE_NIM (1,stack
);
527 if (SCM_DEBUGOBJP (stack
))
528 dframe
= (scm_debug_frame
*) SCM_DEBUGOBJ_FRAME (stack
);
529 else if (scm_tc7_contin
== SCM_TYP7 (stack
))
531 offset
= ((SCM_STACKITEM
*) (SCM_CHARS (stack
) + sizeof (scm_contregs
))
533 #ifndef STACK_GROWS_UP
534 offset
+= SCM_LENGTH (stack
);
536 dframe
= RELOC_FRAME (SCM_DFRAME (stack
), offset
);
538 else if (SCM_STACKP (stack
))
539 return SCM_STACK (stack
) -> id
;
541 SCM_WRONG_TYPE_ARG (1, stack
);
543 while (dframe
&& !SCM_VOIDFRAMEP (*dframe
))
544 dframe
= RELOC_FRAME (dframe
->prev
, offset
);
545 if (dframe
&& SCM_VOIDFRAMEP (*dframe
))
546 return dframe
->vect
[0].id
;
551 SCM_DEFINE (scm_stack_ref
, "stack-ref", 2, 0, 0,
554 #define FUNC_NAME s_scm_stack_ref
556 SCM_VALIDATE_STACK (1,stack
);
557 SCM_VALIDATE_INUM (2,i
);
558 SCM_ASSERT_RANGE (1,i
,
560 SCM_INUM (i
) < SCM_STACK_LENGTH (stack
));
561 return scm_cons (stack
, i
);
565 SCM_DEFINE (scm_stack_length
, "stack-length", 1, 0, 0,
568 #define FUNC_NAME s_scm_stack_length
570 SCM_VALIDATE_STACK (1,stack
);
571 return SCM_MAKINUM (SCM_STACK_LENGTH (stack
));
578 SCM_DEFINE (scm_frame_p
, "frame?", 1, 0, 0,
581 #define FUNC_NAME s_scm_frame_p
583 return SCM_BOOL(SCM_FRAMEP (obj
));
587 SCM_DEFINE (scm_last_stack_frame
, "last-stack-frame", 1, 0, 0,
590 #define FUNC_NAME s_scm_last_stack_frame
592 scm_debug_frame
*dframe
;
596 SCM_VALIDATE_NIM (1,obj
);
597 if (SCM_DEBUGOBJP (obj
))
598 dframe
= (scm_debug_frame
*) SCM_DEBUGOBJ_FRAME (obj
);
599 else if (scm_tc7_contin
== SCM_TYP7 (obj
))
601 offset
= ((SCM_STACKITEM
*) (SCM_CHARS (obj
) + sizeof (scm_contregs
))
603 #ifndef STACK_GROWS_UP
604 offset
+= SCM_LENGTH (obj
);
606 dframe
= RELOC_FRAME (SCM_DFRAME (obj
), offset
);
614 if (!dframe
|| SCM_VOIDFRAMEP (*dframe
))
617 stack
= scm_make_struct (scm_stack_type
, SCM_MAKINUM (SCM_FRAME_N_SLOTS
),
619 SCM_STACK (stack
) -> length
= 1;
620 SCM_STACK (stack
) -> frames
= &SCM_STACK (stack
) -> tail
[0];
621 read_frame (dframe
, offset
,
622 (scm_info_frame
*) &SCM_STACK (stack
) -> frames
[0]);
624 return scm_cons (stack
, SCM_INUM0
);;
628 SCM_DEFINE (scm_frame_number
, "frame-number", 1, 0, 0,
631 #define FUNC_NAME s_scm_frame_number
633 SCM_VALIDATE_FRAME (1,frame
);
634 return SCM_MAKINUM (SCM_FRAME_NUMBER (frame
));
638 SCM_DEFINE (scm_frame_source
, "frame-source", 1, 0, 0,
641 #define FUNC_NAME s_scm_frame_source
643 SCM_VALIDATE_FRAME (1,frame
);
644 return SCM_FRAME_SOURCE (frame
);
648 SCM_DEFINE (scm_frame_procedure
, "frame-procedure", 1, 0, 0,
651 #define FUNC_NAME s_scm_frame_procedure
653 SCM_VALIDATE_FRAME (1,frame
);
654 return (SCM_FRAME_PROC_P (frame
)
655 ? SCM_FRAME_PROC (frame
)
660 SCM_DEFINE (scm_frame_arguments
, "frame-arguments", 1, 0, 0,
663 #define FUNC_NAME s_scm_frame_arguments
665 SCM_VALIDATE_FRAME (1,frame
);
666 return SCM_FRAME_ARGS (frame
);
670 SCM_DEFINE (scm_frame_previous
, "frame-previous", 1, 0, 0,
673 #define FUNC_NAME s_scm_frame_previous
676 SCM_VALIDATE_FRAME (1,frame
);
677 n
= SCM_INUM (SCM_CDR (frame
)) + 1;
678 if (n
>= SCM_STACK_LENGTH (SCM_CAR (frame
)))
681 return scm_cons (SCM_CAR (frame
), SCM_MAKINUM (n
));
685 SCM_DEFINE (scm_frame_next
, "frame-next", 1, 0, 0,
688 #define FUNC_NAME s_scm_frame_next
691 SCM_VALIDATE_FRAME (1,frame
);
692 n
= SCM_INUM (SCM_CDR (frame
)) - 1;
696 return scm_cons (SCM_CAR (frame
), SCM_MAKINUM (n
));
700 SCM_DEFINE (scm_frame_real_p
, "frame-real?", 1, 0, 0,
703 #define FUNC_NAME s_scm_frame_real_p
705 SCM_VALIDATE_FRAME (1,frame
);
706 return SCM_BOOL(SCM_FRAME_REAL_P (frame
));
710 SCM_DEFINE (scm_frame_procedure_p
, "frame-procedure?", 1, 0, 0,
713 #define FUNC_NAME s_scm_frame_procedure_p
715 SCM_VALIDATE_FRAME (1,frame
);
716 return SCM_BOOL(SCM_FRAME_PROC_P (frame
));
720 SCM_DEFINE (scm_frame_evaluating_args_p
, "frame-evaluating-args?", 1, 0, 0,
723 #define FUNC_NAME s_scm_frame_evaluating_args_p
725 SCM_VALIDATE_FRAME (1,frame
);
726 return SCM_BOOL(SCM_FRAME_EVAL_ARGS_P (frame
));
730 SCM_DEFINE (scm_frame_overflow_p
, "frame-overflow?", 1, 0, 0,
733 #define FUNC_NAME s_scm_frame_overflow_p
735 SCM_VALIDATE_FRAME (1,frame
);
736 return SCM_BOOL(SCM_FRAME_OVERFLOW_P (frame
));
746 SCM vtable_layout
= scm_make_struct_layout (scm_nullstr
);
748 = scm_make_struct_layout (scm_makfrom0str (SCM_STACK_LAYOUT
));
749 vtable
= scm_make_vtable_vtable (vtable_layout
, SCM_INUM0
, SCM_EOL
);
751 = scm_permanent_object (scm_make_struct (vtable
, SCM_INUM0
,
752 scm_cons (stack_layout
,
754 scm_set_struct_vtable_name_x (scm_stack_type
,
755 SCM_CAR (scm_intern0 ("stack")));