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