Merge commit '752be95a475132506c35922d284884cf776149d0' into vm-check
[bpt/guile.git] / libguile / debug.c
1 /* Debugging extensions for Guile
2 * Copyright (C) 1995,1996,1997,1998,1999,2000,2001, 2002, 2003, 2006, 2008 Free Software Foundation
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Lesser General Public License for more details.
13 *
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 02110-1301 USA
17 */
18
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include "libguile/_scm.h"
25 #include "libguile/async.h"
26 #include "libguile/eval.h"
27 #include "libguile/list.h"
28 #include "libguile/stackchk.h"
29 #include "libguile/throw.h"
30 #include "libguile/macros.h"
31 #include "libguile/smob.h"
32 #include "libguile/procprop.h"
33 #include "libguile/srcprop.h"
34 #include "libguile/alist.h"
35 #include "libguile/continuations.h"
36 #include "libguile/strports.h"
37 #include "libguile/read.h"
38 #include "libguile/feature.h"
39 #include "libguile/dynwind.h"
40 #include "libguile/modules.h"
41 #include "libguile/ports.h"
42 #include "libguile/root.h"
43 #include "libguile/fluids.h"
44 #include "libguile/objects.h"
45 #include "libguile/programs.h"
46
47 #include "libguile/validate.h"
48 #include "libguile/debug.h"
49
50 #include "libguile/private-options.h"
51 \f
52
53
54 /* {Run time control of the debugging evaluator}
55 */
56
57 SCM_DEFINE (scm_debug_options, "debug-options-interface", 0, 1, 0,
58 (SCM setting),
59 "Option interface for the debug options. Instead of using\n"
60 "this procedure directly, use the procedures @code{debug-enable},\n"
61 "@code{debug-disable}, @code{debug-set!} and @code{debug-options}.")
62 #define FUNC_NAME s_scm_debug_options
63 {
64 SCM ans;
65
66 scm_dynwind_begin (0);
67 scm_dynwind_critical_section (SCM_BOOL_F);
68
69 ans = scm_options (setting, scm_debug_opts, FUNC_NAME);
70 if (!(1 <= SCM_N_FRAMES && SCM_N_FRAMES <= SCM_MAX_FRAME_SIZE))
71 {
72 scm_options (ans, scm_debug_opts, FUNC_NAME);
73 SCM_OUT_OF_RANGE (1, setting);
74 }
75 SCM_RESET_DEBUG_MODE;
76 #ifdef STACK_CHECKING
77 scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
78 #endif
79 scm_debug_eframe_size = 2 * SCM_N_FRAMES;
80
81 scm_dynwind_end ();
82 return ans;
83 }
84 #undef FUNC_NAME
85
86
87 static void
88 with_traps_before (void *data)
89 {
90 int *trap_flag = data;
91 *trap_flag = SCM_TRAPS_P;
92 SCM_TRAPS_P = 1;
93 }
94
95 static void
96 with_traps_after (void *data)
97 {
98 int *trap_flag = data;
99 SCM_TRAPS_P = *trap_flag;
100 }
101
102 static SCM
103 with_traps_inner (void *data)
104 {
105 SCM thunk = SCM_PACK ((scm_t_bits) data);
106 return scm_call_0 (thunk);
107 }
108
109 SCM_DEFINE (scm_with_traps, "with-traps", 1, 0, 0,
110 (SCM thunk),
111 "Call @var{thunk} with traps enabled.")
112 #define FUNC_NAME s_scm_with_traps
113 {
114 int trap_flag;
115 SCM_VALIDATE_THUNK (1, thunk);
116 return scm_internal_dynamic_wind (with_traps_before,
117 with_traps_inner,
118 with_traps_after,
119 (void *) SCM_UNPACK (thunk),
120 &trap_flag);
121 }
122 #undef FUNC_NAME
123
124 \f
125 SCM_SYMBOL (scm_sym_procname, "procname");
126 SCM_SYMBOL (scm_sym_dots, "...");
127 SCM_SYMBOL (scm_sym_source, "source");
128
129 /* {Memoized Source}
130 */
131
132 scm_t_bits scm_tc16_memoized;
133
134 static int
135 memoized_print (SCM obj, SCM port, scm_print_state *pstate)
136 {
137 int writingp = SCM_WRITINGP (pstate);
138 scm_puts ("#<memoized ", port);
139 SCM_SET_WRITINGP (pstate, 1);
140 scm_iprin1 (SCM_MEMOIZED_EXP (obj), port, pstate);
141 SCM_SET_WRITINGP (pstate, writingp);
142 scm_putc ('>', port);
143 return 1;
144 }
145
146 SCM_DEFINE (scm_memoized_p, "memoized?", 1, 0, 0,
147 (SCM obj),
148 "Return @code{#t} if @var{obj} is memoized.")
149 #define FUNC_NAME s_scm_memoized_p
150 {
151 return scm_from_bool(SCM_MEMOIZEDP (obj));
152 }
153 #undef FUNC_NAME
154
155 SCM
156 scm_make_memoized (SCM exp, SCM env)
157 {
158 /* *fixme* Check that env is a valid environment. */
159 SCM_RETURN_NEWSMOB (scm_tc16_memoized, SCM_UNPACK (scm_cons (exp, env)));
160 }
161
162 #ifdef GUILE_DEBUG
163 /*
164 * Some primitives for construction of memoized code
165 *
166 * - procedure: memcons CAR CDR [ENV]
167 *
168 * Construct a pair, encapsulated in a memoized object.
169 *
170 * The CAR and CDR can be either normal or memoized. If ENV isn't
171 * specified, the top-level environment of the current module will
172 * be assumed. All environments must match.
173 *
174 * - procedure: make-iloc FRAME BINDING CDRP
175 *
176 * Return an iloc referring to frame no. FRAME, binding
177 * no. BINDING. If CDRP is non-#f, the iloc is referring to a
178 * frame consisting of a single pair, with the value stored in the
179 * CDR.
180 *
181 * - procedure: iloc? OBJECT
182 *
183 * Return #t if OBJECT is an iloc.
184 *
185 * - procedure: mem->proc MEMOIZED
186 *
187 * Construct a closure from the memoized lambda expression MEMOIZED
188 *
189 * WARNING! The code is not copied!
190 *
191 * - procedure: proc->mem CLOSURE
192 *
193 * Turn the closure CLOSURE into a memoized object.
194 *
195 * WARNING! The code is not copied!
196 *
197 * - constant: SCM_IM_AND
198 * - constant: SCM_IM_BEGIN
199 * - constant: SCM_IM_CASE
200 * - constant: SCM_IM_COND
201 * - constant: SCM_IM_DO
202 * - constant: SCM_IM_IF
203 * - constant: SCM_IM_LAMBDA
204 * - constant: SCM_IM_LET
205 * - constant: SCM_IM_LETSTAR
206 * - constant: SCM_IM_LETREC
207 * - constant: SCM_IM_OR
208 * - constant: SCM_IM_QUOTE
209 * - constant: SCM_IM_SET
210 * - constant: SCM_IM_DEFINE
211 * - constant: SCM_IM_APPLY
212 * - constant: SCM_IM_CONT
213 * - constant: SCM_IM_DISPATCH
214 */
215
216 #include "libguile/variable.h"
217 #include "libguile/procs.h"
218
219 SCM_DEFINE (scm_memcons, "memcons", 2, 1, 0,
220 (SCM car, SCM cdr, SCM env),
221 "Return a new memoized cons cell with @var{car} and @var{cdr}\n"
222 "as members and @var{env} as the environment.")
223 #define FUNC_NAME s_scm_memcons
224 {
225 if (SCM_MEMOIZEDP (car))
226 {
227 /*fixme* environments may be two different but equal top-level envs */
228 if (!SCM_UNBNDP (env) && SCM_MEMOIZED_ENV (car) != env)
229 SCM_MISC_ERROR ("environment mismatch arg1 <-> arg3",
230 scm_list_2 (car, env));
231 else
232 env = SCM_MEMOIZED_ENV (car);
233 car = SCM_MEMOIZED_EXP (car);
234 }
235 if (SCM_MEMOIZEDP (cdr))
236 {
237 if (!SCM_UNBNDP (env) && SCM_MEMOIZED_ENV (cdr) != env)
238 SCM_MISC_ERROR ("environment mismatch arg2 <-> arg3",
239 scm_list_2 (cdr, env));
240 else
241 env = SCM_MEMOIZED_ENV (cdr);
242 cdr = SCM_MEMOIZED_EXP (cdr);
243 }
244 if (SCM_UNBNDP (env))
245 env = scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE);
246 else
247 SCM_VALIDATE_NULLORCONS (3, env);
248 return scm_make_memoized (scm_cons (car, cdr), env);
249 }
250 #undef FUNC_NAME
251
252 SCM_DEFINE (scm_mem_to_proc, "mem->proc", 1, 0, 0,
253 (SCM obj),
254 "Convert a memoized object (which must represent a body)\n"
255 "to a procedure.")
256 #define FUNC_NAME s_scm_mem_to_proc
257 {
258 SCM env;
259 SCM_VALIDATE_MEMOIZED (1, obj);
260 env = SCM_MEMOIZED_ENV (obj);
261 obj = SCM_MEMOIZED_EXP (obj);
262 return scm_closure (obj, env);
263 }
264 #undef FUNC_NAME
265
266 SCM_DEFINE (scm_proc_to_mem, "proc->mem", 1, 0, 0,
267 (SCM obj),
268 "Convert a procedure to a memoized object.")
269 #define FUNC_NAME s_scm_proc_to_mem
270 {
271 SCM_VALIDATE_CLOSURE (1, obj);
272 return scm_make_memoized (SCM_CODE (obj), SCM_ENV (obj));
273 }
274 #undef FUNC_NAME
275
276 #endif /* GUILE_DEBUG */
277
278 SCM_DEFINE (scm_i_unmemoize_expr, "unmemoize-expr", 1, 0, 0,
279 (SCM m),
280 "Unmemoize the memoized expression @var{m},")
281 #define FUNC_NAME s_scm_i_unmemoize_expr
282 {
283 SCM_VALIDATE_MEMOIZED (1, m);
284 return scm_i_unmemocopy_expr (SCM_MEMOIZED_EXP (m), SCM_MEMOIZED_ENV (m));
285 }
286 #undef FUNC_NAME
287
288 SCM_DEFINE (scm_memoized_environment, "memoized-environment", 1, 0, 0,
289 (SCM m),
290 "Return the environment of the memoized expression @var{m}.")
291 #define FUNC_NAME s_scm_memoized_environment
292 {
293 SCM_VALIDATE_MEMOIZED (1, m);
294 return SCM_MEMOIZED_ENV (m);
295 }
296 #undef FUNC_NAME
297
298 SCM_DEFINE (scm_procedure_name, "procedure-name", 1, 0, 0,
299 (SCM proc),
300 "Return the name of the procedure @var{proc}")
301 #define FUNC_NAME s_scm_procedure_name
302 {
303 SCM_VALIDATE_PROC (1, proc);
304 switch (SCM_TYP7 (proc)) {
305 case scm_tcs_subrs:
306 return SCM_SNAME (proc);
307 default:
308 {
309 SCM name = scm_procedure_property (proc, scm_sym_name);
310 #if 0
311 /* Source property scm_sym_procname not implemented yet... */
312 SCM name = scm_source_property (SCM_CAR (SCM_CLOSURE_BODY (proc)), scm_sym_procname);
313 if (scm_is_false (name))
314 name = scm_procedure_property (proc, scm_sym_name);
315 #endif
316 if (scm_is_false (name) && SCM_CLOSUREP (proc))
317 name = scm_reverse_lookup (SCM_ENV (proc), proc);
318 if (scm_is_false (name) && SCM_PROGRAM_P (proc))
319 name = scm_program_name (proc);
320 return name;
321 }
322 }
323 }
324 #undef FUNC_NAME
325
326 SCM_DEFINE (scm_procedure_source, "procedure-source", 1, 0, 0,
327 (SCM proc),
328 "Return the source of the procedure @var{proc}.")
329 #define FUNC_NAME s_scm_procedure_source
330 {
331 SCM_VALIDATE_NIM (1, proc);
332 again:
333 switch (SCM_TYP7 (proc)) {
334 case scm_tcs_closures:
335 {
336 const SCM formals = SCM_CLOSURE_FORMALS (proc);
337 const SCM body = SCM_CLOSURE_BODY (proc);
338 const SCM src = scm_source_property (body, scm_sym_copy);
339
340 if (scm_is_true (src))
341 {
342 return scm_cons2 (scm_sym_lambda, formals, src);
343 }
344 else
345 {
346 const SCM env = SCM_EXTEND_ENV (formals, SCM_EOL, SCM_ENV (proc));
347 return scm_cons2 (scm_sym_lambda,
348 scm_i_finite_list_copy (formals),
349 scm_i_unmemocopy_body (body, env));
350 }
351 }
352 case scm_tcs_struct:
353 if (!SCM_I_OPERATORP (proc))
354 break;
355 goto procprop;
356 case scm_tc7_smob:
357 if (!SCM_SMOB_DESCRIPTOR (proc).apply)
358 break;
359 case scm_tcs_subrs:
360 #ifdef CCLO
361 case scm_tc7_cclo:
362 #endif
363 procprop:
364 /* It would indeed be a nice thing if we supplied source even for
365 built in procedures! */
366 return scm_procedure_property (proc, scm_sym_source);
367 case scm_tc7_pws:
368 {
369 SCM src = scm_procedure_property (proc, scm_sym_source);
370 if (scm_is_true (src))
371 return src;
372 proc = SCM_PROCEDURE (proc);
373 goto again;
374 }
375 default:
376 ;
377 }
378 SCM_WRONG_TYPE_ARG (1, proc);
379 return SCM_BOOL_F; /* not reached */
380 }
381 #undef FUNC_NAME
382
383 SCM_DEFINE (scm_procedure_environment, "procedure-environment", 1, 0, 0,
384 (SCM proc),
385 "Return the environment of the procedure @var{proc}.")
386 #define FUNC_NAME s_scm_procedure_environment
387 {
388 SCM_VALIDATE_NIM (1, proc);
389 switch (SCM_TYP7 (proc)) {
390 case scm_tcs_closures:
391 return SCM_ENV (proc);
392 case scm_tcs_subrs:
393 #ifdef CCLO
394 case scm_tc7_cclo:
395 #endif
396 return SCM_EOL;
397 default:
398 SCM_WRONG_TYPE_ARG (1, proc);
399 /* not reached */
400 }
401 }
402 #undef FUNC_NAME
403
404 \f
405
406 /* Eval in a local environment. We would like to have the ability to
407 * evaluate in a specified local environment, but due to the
408 * memoization this isn't normally possible. We solve it by copying
409 * the code before evaluating. One solution would be to have eval.c
410 * generate yet another evaluator. They are not very big actually.
411 */
412 SCM_DEFINE (scm_local_eval, "local-eval", 1, 1, 0,
413 (SCM exp, SCM env),
414 "Evaluate @var{exp} in its environment. If @var{env} is supplied,\n"
415 "it is the environment in which to evaluate @var{exp}. Otherwise,\n"
416 "@var{exp} must be a memoized code object (in which case, its environment\n"
417 "is implicit).")
418 #define FUNC_NAME s_scm_local_eval
419 {
420 if (SCM_UNBNDP (env))
421 {
422 SCM_VALIDATE_MEMOIZED (1, exp);
423 return scm_i_eval_x (SCM_MEMOIZED_EXP (exp), SCM_MEMOIZED_ENV (exp));
424 }
425 return scm_i_eval (exp, env);
426 }
427 #undef FUNC_NAME
428
429 #if 0
430 SCM_REGISTER_PROC (s_reverse_lookup, "reverse-lookup", 2, 0, 0, scm_reverse_lookup);
431 #endif
432
433 SCM
434 scm_reverse_lookup (SCM env, SCM data)
435 {
436 while (scm_is_pair (env) && scm_is_pair (SCM_CAR (env)))
437 {
438 SCM names = SCM_CAAR (env);
439 SCM values = SCM_CDAR (env);
440 while (scm_is_pair (names))
441 {
442 if (scm_is_eq (SCM_CAR (values), data))
443 return SCM_CAR (names);
444 names = SCM_CDR (names);
445 values = SCM_CDR (values);
446 }
447 if (!scm_is_null (names) && scm_is_eq (values, data))
448 return names;
449 env = SCM_CDR (env);
450 }
451 return SCM_BOOL_F;
452 }
453
454 SCM_DEFINE (scm_sys_start_stack, "%start-stack", 2, 0, 0,
455 (SCM id, SCM thunk),
456 "Call @var{thunk} on an evaluator stack tagged with @var{id}.")
457 #define FUNC_NAME s_scm_sys_start_stack
458 {
459 SCM answer;
460 scm_t_debug_frame vframe;
461 scm_t_debug_info vframe_vect_body;
462 vframe.prev = scm_i_last_debug_frame ();
463 vframe.status = SCM_VOIDFRAME;
464 vframe.vect = &vframe_vect_body;
465 vframe.vect[0].id = id;
466 scm_i_set_last_debug_frame (&vframe);
467 answer = scm_call_0 (thunk);
468 scm_i_set_last_debug_frame (vframe.prev);
469 return answer;
470 }
471 #undef FUNC_NAME
472
473 /* {Debug Objects}
474 *
475 * The debugging evaluator throws these on frame traps.
476 */
477
478 scm_t_bits scm_tc16_debugobj;
479
480 static int
481 debugobj_print (SCM obj, SCM port, scm_print_state *pstate SCM_UNUSED)
482 {
483 scm_puts ("#<debug-object ", port);
484 scm_intprint ((long) SCM_DEBUGOBJ_FRAME (obj), 16, port);
485 scm_putc ('>', port);
486 return 1;
487 }
488
489 SCM_DEFINE (scm_debug_object_p, "debug-object?", 1, 0, 0,
490 (SCM obj),
491 "Return @code{#t} if @var{obj} is a debug object.")
492 #define FUNC_NAME s_scm_debug_object_p
493 {
494 return scm_from_bool(SCM_DEBUGOBJP (obj));
495 }
496 #undef FUNC_NAME
497
498
499 SCM
500 scm_make_debugobj (scm_t_debug_frame *frame)
501 {
502 return scm_cell (scm_tc16_debugobj, (scm_t_bits) frame);
503 }
504
505 \f
506
507 /* Undocumented debugging procedure */
508 #ifdef GUILE_DEBUG
509 SCM_DEFINE (scm_debug_hang, "debug-hang", 0, 1, 0,
510 (SCM obj),
511 "Go into an endless loop, which can be only terminated with\n"
512 "a debugger.")
513 #define FUNC_NAME s_scm_debug_hang
514 {
515 int go = 0;
516 while (!go) ;
517 return SCM_UNSPECIFIED;
518 }
519 #undef FUNC_NAME
520 #endif
521
522 \f
523
524 void
525 scm_init_debug ()
526 {
527 scm_init_opts (scm_debug_options, scm_debug_opts);
528
529 scm_tc16_memoized = scm_make_smob_type ("memoized", 0);
530 scm_set_smob_mark (scm_tc16_memoized, scm_markcdr);
531 scm_set_smob_print (scm_tc16_memoized, memoized_print);
532
533 scm_tc16_debugobj = scm_make_smob_type ("debug-object", 0);
534 scm_set_smob_print (scm_tc16_debugobj, debugobj_print);
535
536 #ifdef GUILE_DEBUG
537 scm_c_define ("SCM_IM_AND", SCM_IM_AND);
538 scm_c_define ("SCM_IM_BEGIN", SCM_IM_BEGIN);
539 scm_c_define ("SCM_IM_CASE", SCM_IM_CASE);
540 scm_c_define ("SCM_IM_COND", SCM_IM_COND);
541 scm_c_define ("SCM_IM_DO", SCM_IM_DO);
542 scm_c_define ("SCM_IM_IF", SCM_IM_IF);
543 scm_c_define ("SCM_IM_LAMBDA", SCM_IM_LAMBDA);
544 scm_c_define ("SCM_IM_LET", SCM_IM_LET);
545 scm_c_define ("SCM_IM_LETSTAR", SCM_IM_LETSTAR);
546 scm_c_define ("SCM_IM_LETREC", SCM_IM_LETREC);
547 scm_c_define ("SCM_IM_OR", SCM_IM_OR);
548 scm_c_define ("SCM_IM_QUOTE", SCM_IM_QUOTE);
549 scm_c_define ("SCM_IM_SET_X", SCM_IM_SET_X);
550 scm_c_define ("SCM_IM_DEFINE", SCM_IM_DEFINE);
551 scm_c_define ("SCM_IM_APPLY", SCM_IM_APPLY);
552 scm_c_define ("SCM_IM_CONT", SCM_IM_CONT);
553 scm_c_define ("SCM_IM_DISPATCH", SCM_IM_DISPATCH);
554 #endif
555 scm_add_feature ("debug-extensions");
556
557 #include "libguile/debug.x"
558 }
559
560 /*
561 Local Variables:
562 c-file-style: "gnu"
563 End:
564 */