1 /* A stack holds a frame chain
2 * Copyright (C) 1996,1997,2000,2001, 2006, 2007, 2008, 2009 Free Software Foundation
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public License
6 * as published by the Free Software Foundation; either version 3 of
7 * the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include "libguile/_scm.h"
27 #include "libguile/eval.h"
28 #include "libguile/debug.h"
29 #include "libguile/continuations.h"
30 #include "libguile/struct.h"
31 #include "libguile/macros.h"
32 #include "libguile/procprop.h"
33 #include "libguile/modules.h"
34 #include "libguile/root.h"
35 #include "libguile/strings.h"
36 #include "libguile/vm.h" /* to capture vm stacks */
37 #include "libguile/frames.h" /* vm frames */
39 #include "libguile/validate.h"
40 #include "libguile/stacks.h"
41 #include "libguile/private-options.h"
47 * The stack is represented as a struct that holds a frame. The frame itself is
48 * linked to the next frame, or #f.
62 static SCM
stack_id_with_fp (SCM frame
, SCM
**fp
);
64 /* Count number of debug info frames on a stack, beginning with FRAME.
67 stack_depth (SCM frame
, SCM
*fp
)
70 /* count frames, skipping boot frames */
71 for (; scm_is_true (frame
) && SCM_VM_FRAME_FP (frame
) > fp
;
72 frame
= scm_c_frame_prev (frame
))
73 if (!SCM_PROGRAM_IS_BOOT (scm_frame_procedure (frame
)))
78 /* Narrow STACK by cutting away stackframes (mutatingly).
80 * Inner frames (most recent) are cut by advancing the frames pointer.
81 * Outer frames are cut by decreasing the recorded length.
83 * Cut maximally INNER inner frames and OUTER outer frames using
84 * the keys INNER_KEY and OUTER_KEY.
86 * Frames are cut away starting at the end points and moving towards
87 * the center of the stack. The key is normally compared to the
88 * operator in application frames. Frames up to and including the key
91 * If INNER_KEY is #t a different scheme is used for inner frames:
93 * Frames up to but excluding the first source frame originating from
94 * a user module are cut, except for possible application frames
95 * between the user frame and the last system frame previously
100 narrow_stack (SCM stack
, long inner
, SCM inner_key
, long outer
, SCM outer_key
)
102 unsigned long int len
;
105 len
= SCM_STACK_LENGTH (stack
);
106 frame
= SCM_STACK_FRAME (stack
);
108 /* Cut inner part. */
109 if (scm_is_eq (inner_key
, SCM_BOOL_T
))
111 /* Cut specified number of frames. */
112 for (; inner
&& len
; --inner
)
115 frame
= scm_c_frame_prev (frame
);
120 /* Cut until the given procedure is seen. */
121 for (; inner
&& len
; --inner
)
123 SCM proc
= scm_frame_procedure (frame
);
125 frame
= scm_c_frame_prev (frame
);
126 if (scm_is_eq (proc
, inner_key
))
131 SCM_SET_STACK_LENGTH (stack
, len
);
132 SCM_SET_STACK_FRAME (stack
, frame
);
134 /* Cut outer part. */
135 for (; outer
&& len
; --outer
)
137 frame
= scm_stack_ref (stack
, scm_from_long (len
- 1));
139 if (scm_is_eq (scm_frame_procedure (frame
), outer_key
))
143 SCM_SET_STACK_LENGTH (stack
, len
);
153 SCM_DEFINE (scm_stack_p
, "stack?", 1, 0, 0,
155 "Return @code{#t} if @var{obj} is a calling stack.")
156 #define FUNC_NAME s_scm_stack_p
158 return scm_from_bool(SCM_STACKP (obj
));
162 SCM_DEFINE (scm_make_stack
, "make-stack", 1, 0, 1,
164 "Create a new stack. If @var{obj} is @code{#t}, the current\n"
165 "evaluation stack is used for creating the stack frames,\n"
166 "otherwise the frames are taken from @var{obj} (which must be\n"
167 "either a debug object or a continuation).\n\n"
168 "@var{args} should be a list containing any combination of\n"
169 "integer, procedure and @code{#t} values.\n\n"
170 "These values specify various ways of cutting away uninteresting\n"
171 "stack frames from the top and bottom of the stack that\n"
172 "@code{make-stack} returns. They come in pairs like this:\n"
173 "@code{(@var{inner_cut_1} @var{outer_cut_1} @var{inner_cut_2}\n"
174 "@var{outer_cut_2} @dots{})}.\n\n"
175 "Each @var{inner_cut_N} can be @code{#t}, an integer, or a\n"
176 "procedure. @code{#t} means to cut away all frames up to but\n"
177 "excluding the first user module frame. An integer means to cut\n"
178 "away exactly that number of frames. A procedure means to cut\n"
179 "away all frames up to but excluding the application frame whose\n"
180 "procedure matches the specified one.\n\n"
181 "Each @var{outer_cut_N} can be an integer or a procedure. An\n"
182 "integer means to cut away that number of frames. A procedure\n"
183 "means to cut away frames down to but excluding the application\n"
184 "frame whose procedure matches the specified one.\n\n"
185 "If the @var{outer_cut_N} of the last pair is missing, it is\n"
187 #define FUNC_NAME s_scm_make_stack
194 SCM inner_cut
, outer_cut
;
196 /* Extract a pointer to the innermost frame of whatever object
197 scm_make_stack was given. */
198 if (scm_is_eq (obj
, SCM_BOOL_T
))
201 struct scm_vm_cont
*c
;
203 cont
= scm_cdar (scm_vm_capture_continuations ());
204 c
= SCM_VM_CONT_DATA (cont
);
206 frame
= scm_c_make_frame (cont
, c
->fp
+ c
->reloc
,
207 c
->sp
+ c
->reloc
, c
->ip
,
210 else if (SCM_VM_FRAME_P (obj
))
212 else if (SCM_CONTINUATIONP (obj
))
214 scm_t_contregs
*cont
= SCM_CONTREGS (obj
);
215 if (!scm_is_null (cont
->vm_conts
))
217 struct scm_vm_cont
*data
;
218 vm_cont
= scm_cdr (scm_car (cont
->vm_conts
));
219 data
= SCM_VM_CONT_DATA (vm_cont
);
220 frame
= scm_c_make_frame (vm_cont
,
221 data
->fp
+ data
->reloc
,
222 data
->sp
+ data
->reloc
,
230 SCM_WRONG_TYPE_ARG (SCM_ARG1
, obj
);
234 if (scm_is_false (frame
))
237 /* Get ID of the stack corresponding to the given frame. */
238 id
= stack_id_with_fp (frame
, &id_fp
);
240 /* Count number of frames. Also get stack id tag and check whether
241 there are more stackframes than we want to record
242 (SCM_BACKTRACE_MAXDEPTH). */
245 n
= stack_depth (frame
, id_fp
);
247 /* Make the stack object. */
248 stack
= scm_make_struct (scm_stack_type
, SCM_INUM0
, SCM_EOL
);
249 SCM_SET_STACK_LENGTH (stack
, n
);
250 SCM_SET_STACK_ID (stack
, id
);
251 SCM_SET_STACK_FRAME (stack
, frame
);
253 /* Narrow the stack according to the arguments given to scm_make_stack. */
254 SCM_VALIDATE_REST_ARGUMENT (args
);
255 while (n
> 0 && !scm_is_null (args
))
257 inner_cut
= SCM_CAR (args
);
258 args
= SCM_CDR (args
);
259 if (scm_is_null (args
))
261 outer_cut
= SCM_INUM0
;
265 outer_cut
= SCM_CAR (args
);
266 args
= SCM_CDR (args
);
270 scm_is_integer (inner_cut
) ? scm_to_int (inner_cut
) : n
,
271 scm_is_integer (inner_cut
) ? 0 : inner_cut
,
272 scm_is_integer (outer_cut
) ? scm_to_int (outer_cut
) : n
,
273 scm_is_integer (outer_cut
) ? 0 : outer_cut
);
275 n
= SCM_STACK_LENGTH (stack
);
285 SCM_DEFINE (scm_stack_id
, "stack-id", 1, 0, 0,
287 "Return the identifier given to @var{stack} by @code{start-stack}.")
288 #define FUNC_NAME s_scm_stack_id
292 if (scm_is_eq (stack
, SCM_BOOL_T
))
294 struct scm_vm
*vp
= SCM_VM_DATA (scm_the_vm ());
295 frame
= scm_c_make_frame (scm_the_vm (), vp
->fp
, vp
->sp
, vp
->ip
, 0);
297 else if (SCM_VM_FRAME_P (stack
))
299 else if (SCM_CONTINUATIONP (stack
))
301 scm_t_contregs
*cont
= SCM_CONTREGS (stack
);
302 if (!scm_is_null (cont
->vm_conts
))
304 struct scm_vm_cont
*data
;
305 vm_cont
= scm_cdr (scm_car (cont
->vm_conts
));
306 data
= SCM_VM_CONT_DATA (vm_cont
);
307 frame
= scm_c_make_frame (vm_cont
,
308 data
->fp
+ data
->reloc
,
309 data
->sp
+ data
->reloc
,
317 SCM_WRONG_TYPE_ARG (SCM_ARG1
, stack
);
321 return stack_id_with_fp (frame
, &id_fp
);
326 stack_id_with_fp (SCM frame
, SCM
**fp
)
328 SCM holder
= SCM_VM_FRAME_STACK_HOLDER (frame
);
330 if (SCM_VM_CONT_P (holder
))
342 SCM_DEFINE (scm_stack_ref
, "stack-ref", 2, 0, 0,
343 (SCM stack
, SCM index
),
344 "Return the @var{index}'th frame from @var{stack}.")
345 #define FUNC_NAME s_scm_stack_ref
347 unsigned long int c_index
;
350 SCM_VALIDATE_STACK (1, stack
);
351 c_index
= scm_to_unsigned_integer (index
, 0, SCM_STACK_LENGTH(stack
)-1);
352 frame
= SCM_STACK_FRAME (stack
);
355 frame
= scm_c_frame_prev (frame
);
356 while (SCM_PROGRAM_IS_BOOT (scm_frame_procedure (frame
)))
357 frame
= scm_c_frame_prev (frame
);
363 SCM_DEFINE (scm_stack_length
, "stack-length", 1, 0, 0,
365 "Return the length of @var{stack}.")
366 #define FUNC_NAME s_scm_stack_length
368 SCM_VALIDATE_STACK (1, stack
);
369 return scm_from_long (SCM_STACK_LENGTH (stack
));
378 scm_stack_type
= scm_make_vtable (scm_from_locale_string (SCM_STACK_LAYOUT
),
380 scm_set_struct_vtable_name_x (scm_stack_type
,
381 scm_from_locale_symbol ("stack"));
382 #include "libguile/stacks.x"