Better backtraces from C, especially for optimized closures
[bpt/guile.git] / libguile / frames.c
1 /* Copyright (C) 2001, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
2 *
3 * This library is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU Lesser General Public License
5 * as published by the Free Software Foundation; either version 3 of
6 * the License, or (at your option) any later version.
7 *
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public
14 * License along with this library; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16 * 02110-1301 USA
17 */
18
19 #if HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include "_scm.h"
26 #include "frames.h"
27 #include "vm.h"
28 #include <verify.h>
29
30 /* Make sure assumptions on the layout of `struct scm_vm_frame' hold. */
31 verify (sizeof (SCM) == sizeof (SCM *));
32 verify (sizeof (struct scm_vm_frame) == 3 * sizeof (SCM));
33 verify (offsetof (struct scm_vm_frame, dynamic_link) == 0);
34
35 \f
36
37 SCM
38 scm_c_make_frame (enum scm_vm_frame_kind kind, const struct scm_frame *frame)
39 {
40 struct scm_frame *p = scm_gc_malloc (sizeof (struct scm_frame),
41 "vmframe");
42 p->stack_holder = frame->stack_holder;
43 p->fp_offset = frame->fp_offset;
44 p->sp_offset = frame->sp_offset;
45 p->ip = frame->ip;
46 return scm_cell (scm_tc7_frame | (kind << 8), (scm_t_bits)p);
47 }
48
49 void
50 scm_i_frame_print (SCM frame, SCM port, scm_print_state *pstate)
51 {
52 scm_puts_unlocked ("#<frame ", port);
53 scm_uintprint (SCM_UNPACK (frame), 16, port);
54 scm_putc_unlocked (' ', port);
55 scm_write (scm_frame_procedure (frame), port);
56 /* don't write args, they can get us into trouble. */
57 scm_puts_unlocked (">", port);
58 }
59
60 static SCM*
61 frame_stack_base (enum scm_vm_frame_kind kind, const struct scm_frame *frame)
62 {
63 switch (kind)
64 {
65 case SCM_VM_FRAME_KIND_CONT:
66 return ((struct scm_vm_cont *) frame->stack_holder)->stack_base;
67
68 case SCM_VM_FRAME_KIND_VM:
69 return ((struct scm_vm *) frame->stack_holder)->stack_base;
70
71 default:
72 abort ();
73 }
74 }
75
76 static scm_t_ptrdiff
77 frame_offset (enum scm_vm_frame_kind kind, const struct scm_frame *frame)
78 {
79 switch (kind)
80 {
81 case SCM_VM_FRAME_KIND_CONT:
82 return ((struct scm_vm_cont *) frame->stack_holder)->reloc;
83
84 case SCM_VM_FRAME_KIND_VM:
85 return 0;
86
87 default:
88 abort ();
89 }
90 }
91
92 SCM*
93 scm_i_frame_stack_base (SCM frame)
94 #define FUNC_NAME "frame-stack-base"
95 {
96 SCM_VALIDATE_VM_FRAME (1, frame);
97
98 return frame_stack_base (SCM_VM_FRAME_KIND (frame),
99 SCM_VM_FRAME_DATA (frame));
100 }
101 #undef FUNC_NAME
102
103 scm_t_ptrdiff
104 scm_i_frame_offset (SCM frame)
105 #define FUNC_NAME "frame-offset"
106 {
107 SCM_VALIDATE_VM_FRAME (1, frame);
108
109 return frame_offset (SCM_VM_FRAME_KIND (frame),
110 SCM_VM_FRAME_DATA (frame));
111
112 }
113 #undef FUNC_NAME
114
115 \f
116 /* Scheme interface */
117
118 SCM_DEFINE (scm_frame_p, "frame?", 1, 0, 0,
119 (SCM obj),
120 "")
121 #define FUNC_NAME s_scm_frame_p
122 {
123 return scm_from_bool (SCM_VM_FRAME_P (obj));
124 }
125 #undef FUNC_NAME
126
127 /* Retrieve the local in slot 0, which may or may not actually be a
128 procedure, and may or may not actually be the procedure being
129 applied. If you want the procedure, look it up from the IP. */
130 SCM
131 scm_c_frame_closure (enum scm_vm_frame_kind kind, const struct scm_frame *frame)
132 {
133 SCM *fp = frame_stack_base (kind, frame) + frame->fp_offset;
134
135 return SCM_FRAME_PROGRAM (fp);
136 }
137
138 SCM_DEFINE (scm_frame_procedure, "frame-procedure", 1, 0, 0,
139 (SCM frame),
140 "")
141 #define FUNC_NAME s_scm_frame_procedure
142 {
143 SCM_VALIDATE_VM_FRAME (1, frame);
144
145 /* FIXME: Retrieve procedure from address? */
146 return scm_c_frame_closure (SCM_VM_FRAME_KIND (frame),
147 SCM_VM_FRAME_DATA (frame));
148 }
149 #undef FUNC_NAME
150
151 static SCM frame_arguments_var;
152
153 static void
154 init_frame_arguments_var (void)
155 {
156 frame_arguments_var
157 = scm_c_private_lookup ("system vm frame", "frame-arguments");
158 }
159
160 SCM_DEFINE (scm_frame_arguments, "frame-arguments", 1, 0, 0,
161 (SCM frame),
162 "")
163 #define FUNC_NAME s_scm_frame_arguments
164 {
165 static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
166 scm_i_pthread_once (&once, init_frame_arguments_var);
167
168 SCM_VALIDATE_VM_FRAME (1, frame);
169
170 return scm_call_1 (scm_variable_ref (frame_arguments_var), frame);
171 }
172 #undef FUNC_NAME
173
174 static SCM frame_call_representation_var;
175
176 static void
177 init_frame_call_representation_var (void)
178 {
179 frame_call_representation_var
180 = scm_c_private_lookup ("system vm frame", "frame-call-representation");
181 }
182
183 SCM scm_frame_call_representation (SCM frame)
184 #define FUNC_NAME "frame-call-representation"
185 {
186 static scm_i_pthread_once_t once = SCM_I_PTHREAD_ONCE_INIT;
187 scm_i_pthread_once (&once, init_frame_call_representation_var);
188
189 SCM_VALIDATE_VM_FRAME (1, frame);
190
191 return scm_call_1 (scm_variable_ref (frame_call_representation_var), frame);
192 }
193 #undef FUNC_NAME
194
195 SCM_DEFINE (scm_frame_source, "frame-source", 1, 0, 0,
196 (SCM frame),
197 "")
198 #define FUNC_NAME s_scm_frame_source
199 {
200 SCM_VALIDATE_VM_FRAME (1, frame);
201
202 return scm_find_source_for_addr (scm_frame_instruction_pointer (frame));
203 }
204 #undef FUNC_NAME
205
206 SCM_DEFINE (scm_frame_num_locals, "frame-num-locals", 1, 0, 0,
207 (SCM frame),
208 "")
209 #define FUNC_NAME s_scm_frame_num_locals
210 {
211 SCM *fp, *sp;
212
213 SCM_VALIDATE_VM_FRAME (1, frame);
214
215 fp = SCM_VM_FRAME_FP (frame);
216 sp = SCM_VM_FRAME_SP (frame);
217
218 return scm_from_ptrdiff_t (SCM_FRAME_NUM_LOCALS (fp, sp));
219 }
220 #undef FUNC_NAME
221
222 SCM_DEFINE (scm_frame_local_ref, "frame-local-ref", 2, 0, 0,
223 (SCM frame, SCM index),
224 "")
225 #define FUNC_NAME s_scm_frame_local_ref
226 {
227 SCM *fp, *sp;
228 unsigned int i;
229
230 SCM_VALIDATE_VM_FRAME (1, frame);
231 SCM_VALIDATE_UINT_COPY (2, index, i);
232
233 fp = SCM_VM_FRAME_FP (frame);
234 sp = SCM_VM_FRAME_SP (frame);
235
236 if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
237 return SCM_FRAME_LOCAL (fp, i);
238
239 SCM_OUT_OF_RANGE (SCM_ARG2, index);
240 }
241 #undef FUNC_NAME
242
243 /* Need same not-yet-active frame logic here as in frame-num-locals */
244 SCM_DEFINE (scm_frame_local_set_x, "frame-local-set!", 3, 0, 0,
245 (SCM frame, SCM index, SCM val),
246 "")
247 #define FUNC_NAME s_scm_frame_local_set_x
248 {
249 SCM *fp, *sp;
250 unsigned int i;
251
252 SCM_VALIDATE_VM_FRAME (1, frame);
253 SCM_VALIDATE_UINT_COPY (2, index, i);
254
255 fp = SCM_VM_FRAME_FP (frame);
256 sp = SCM_VM_FRAME_SP (frame);
257
258 if (i < SCM_FRAME_NUM_LOCALS (fp, sp))
259 {
260 SCM_FRAME_LOCAL (fp, i) = val;
261 return SCM_UNSPECIFIED;
262 }
263
264 SCM_OUT_OF_RANGE (SCM_ARG2, index);
265 }
266 #undef FUNC_NAME
267
268 SCM_DEFINE (scm_frame_address, "frame-address", 1, 0, 0,
269 (SCM frame),
270 "Return the frame pointer for @var{frame}.")
271 #define FUNC_NAME s_scm_frame_address
272 {
273 SCM_VALIDATE_VM_FRAME (1, frame);
274 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_FP (frame));
275 }
276 #undef FUNC_NAME
277
278 SCM_DEFINE (scm_frame_stack_pointer, "frame-stack-pointer", 1, 0, 0,
279 (SCM frame),
280 "")
281 #define FUNC_NAME s_scm_frame_stack_pointer
282 {
283 SCM_VALIDATE_VM_FRAME (1, frame);
284
285 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_SP (frame));
286 }
287 #undef FUNC_NAME
288
289 SCM_DEFINE (scm_frame_instruction_pointer, "frame-instruction-pointer", 1, 0, 0,
290 (SCM frame),
291 "")
292 #define FUNC_NAME s_scm_frame_instruction_pointer
293 {
294 SCM_VALIDATE_VM_FRAME (1, frame);
295
296 return scm_from_uintptr_t ((scm_t_uintptr) SCM_VM_FRAME_IP (frame));
297 }
298 #undef FUNC_NAME
299
300 SCM_DEFINE (scm_frame_return_address, "frame-return-address", 1, 0, 0,
301 (SCM frame),
302 "")
303 #define FUNC_NAME s_scm_frame_return_address
304 {
305 SCM_VALIDATE_VM_FRAME (1, frame);
306 return scm_from_uintptr_t ((scm_t_uintptr) (SCM_FRAME_RETURN_ADDRESS
307 (SCM_VM_FRAME_FP (frame))));
308 }
309 #undef FUNC_NAME
310
311 #define RELOC(kind, frame, val) \
312 (((SCM *) (val)) + frame_offset (kind, frame))
313
314 SCM_DEFINE (scm_frame_dynamic_link, "frame-dynamic-link", 1, 0, 0,
315 (SCM frame),
316 "")
317 #define FUNC_NAME s_scm_frame_dynamic_link
318 {
319 SCM_VALIDATE_VM_FRAME (1, frame);
320 /* fixme: munge fp if holder is a continuation */
321 return scm_from_uintptr_t
322 ((scm_t_uintptr)
323 RELOC (SCM_VM_FRAME_KIND (frame), SCM_VM_FRAME_DATA (frame),
324 SCM_FRAME_DYNAMIC_LINK (SCM_VM_FRAME_FP (frame))));
325 }
326 #undef FUNC_NAME
327
328 int
329 scm_c_frame_previous (enum scm_vm_frame_kind kind, struct scm_frame *frame)
330 {
331 SCM *this_fp, *new_fp, *new_sp;
332 SCM proc;
333
334 again:
335 this_fp = frame->fp_offset + frame_stack_base (kind, frame);
336 new_fp = SCM_FRAME_DYNAMIC_LINK (this_fp);
337 if (new_fp)
338 {
339 SCM *stack_base = frame_stack_base (kind, frame);
340 new_fp = RELOC (kind, frame, new_fp);
341 new_sp = SCM_FRAME_PREVIOUS_SP (this_fp);
342 frame->fp_offset = new_fp - stack_base;
343 frame->sp_offset = new_sp - stack_base;
344 frame->ip = SCM_FRAME_RETURN_ADDRESS (this_fp);
345
346 proc = SCM_FRAME_PROGRAM (new_fp);
347
348 if (SCM_PROGRAM_P (proc) && SCM_PROGRAM_IS_BOOT (proc))
349 goto again;
350 else
351 return 1;
352 }
353 else
354 return 0;
355 }
356
357 SCM_DEFINE (scm_frame_previous, "frame-previous", 1, 0, 0,
358 (SCM frame),
359 "")
360 #define FUNC_NAME s_scm_frame_previous
361 {
362 enum scm_vm_frame_kind kind;
363 struct scm_frame tmp;
364
365 SCM_VALIDATE_VM_FRAME (1, frame);
366
367 kind = SCM_VM_FRAME_KIND (frame);
368 memcpy (&tmp, SCM_VM_FRAME_DATA (frame), sizeof tmp);
369
370 if (!scm_c_frame_previous (SCM_VM_FRAME_KIND (frame), &tmp))
371 return SCM_BOOL_F;
372
373 return scm_c_make_frame (kind, &tmp);
374 }
375 #undef FUNC_NAME
376
377 \f
378 void
379 scm_init_frames (void)
380 {
381 #ifndef SCM_MAGIC_SNARFER
382 #include "libguile/frames.x"
383 #endif
384 }
385
386 /*
387 Local Variables:
388 c-file-style: "gnu"
389 End:
390 */