* Replaced a lot of calls to SCM_C[AD]R with more appropriate macros.
[bpt/guile.git] / libguile / debug.c
CommitLineData
68baa7e7
DH
1/* Debugging extensions for Guile
2 * Copyright (C) 1995,1996,1997,1998,1999,2000,2001 Free Software Foundation
ee340120
MD
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
82892bed
JB
16 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307 USA
ee340120
MD
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
82892bed 44 * Mikael Djurfeldt, SANS/NADA KTH, 10044 STOCKHOLM, SWEDEN */
f0e9217a 45
1bbd0b84
GB
46/* Software engineering face-lift by Greg J. Badros, 11-Dec-1999,
47 gjb@cs.washington.edu, http://www.cs.washington.edu/homes/gjb */
48
49
a0599745
MD
50#include "libguile/_scm.h"
51#include "libguile/eval.h"
52#include "libguile/stackchk.h"
53#include "libguile/throw.h"
54#include "libguile/macros.h"
55#include "libguile/smob.h"
56#include "libguile/procprop.h"
57#include "libguile/srcprop.h"
58#include "libguile/alist.h"
59#include "libguile/continuations.h"
60#include "libguile/strports.h"
61#include "libguile/read.h"
62#include "libguile/feature.h"
63#include "libguile/dynwind.h"
64#include "libguile/modules.h"
65#include "libguile/ports.h"
66#include "libguile/root.h"
b06a8b87 67#include "libguile/fluids.h"
a0599745
MD
68
69#include "libguile/validate.h"
70#include "libguile/debug.h"
f0e9217a
MD
71\f
72
73/* {Run time control of the debugging evaluator}
74 */
75
a1ec6916 76SCM_DEFINE (scm_debug_options, "debug-options-interface", 0, 1, 0,
1bbd0b84 77 (SCM setting),
ba94f79e
MG
78 "Option interface for the debug options. Instead of using\n"
79 "this procedure directly, use the procedures @code{debug-enable},\n"
80 "@code{debug-disable}, @code{debug-set!} and @var{debug-options}.")
1bbd0b84 81#define FUNC_NAME s_scm_debug_options
f0e9217a
MD
82{
83 SCM ans;
84 SCM_DEFER_INTS;
5e8d7fd4
MD
85 ans = scm_options (setting,
86 scm_debug_opts,
87 SCM_N_DEBUG_OPTIONS,
1bbd0b84 88 FUNC_NAME);
f0e9217a 89#ifndef SCM_RECKLESS
5e8d7fd4 90 if (!(1 <= SCM_N_FRAMES && SCM_N_FRAMES <= SCM_MAX_FRAME_SIZE))
f0e9217a 91 {
1bbd0b84 92 scm_options (ans, scm_debug_opts, SCM_N_DEBUG_OPTIONS, FUNC_NAME);
1e76143f 93 SCM_OUT_OF_RANGE (1, setting);
f0e9217a
MD
94 }
95#endif
5e8d7fd4 96 SCM_RESET_DEBUG_MODE;
a6e350dd 97 scm_stack_checking_enabled_p = SCM_STACK_CHECKING_P;
5e8d7fd4 98 scm_debug_eframe_size = 2 * SCM_N_FRAMES;
bfc69694 99 SCM_ALLOW_INTS;
f0e9217a
MD
100 return ans;
101}
1bbd0b84 102#undef FUNC_NAME
260b1416
MD
103
104static void
105with_traps_before (void *data)
106{
107 int *trap_flag = data;
108 *trap_flag = SCM_TRAPS_P;
109 SCM_TRAPS_P = 1;
110}
111
112static void
113with_traps_after (void *data)
114{
115 int *trap_flag = data;
116 SCM_TRAPS_P = *trap_flag;
117}
118
119static SCM
120with_traps_inner (void *data)
121{
451e591c 122 SCM thunk = SCM_PACK (data);
260b1416
MD
123 return scm_apply (thunk, SCM_EOL, SCM_EOL);
124}
1cc91f1b 125
a1ec6916 126SCM_DEFINE (scm_with_traps, "with-traps", 1, 0, 0,
1bbd0b84 127 (SCM thunk),
ba94f79e 128 "Call @var{thunk} with traps enabled.")
1bbd0b84 129#define FUNC_NAME s_scm_with_traps
f0e9217a 130{
260b1416 131 int trap_flag;
3b3b36dd 132 SCM_VALIDATE_THUNK (1,thunk);
260b1416
MD
133 return scm_internal_dynamic_wind (with_traps_before,
134 with_traps_inner,
135 with_traps_after,
451e591c 136 (void *) SCM_UNPACK (thunk),
260b1416 137 &trap_flag);
f0e9217a 138}
1bbd0b84 139#undef FUNC_NAME
f0e9217a
MD
140
141\f
85db4a2c
DH
142
143SCM_SYMBOL (scm_sym_procname, "procname");
144SCM_SYMBOL (scm_sym_dots, "...");
145SCM_SYMBOL (scm_sym_source, "source");
f0e9217a
MD
146
147/* {Memoized Source}
148 */
149
e841c3e0 150scm_bits_t scm_tc16_memoized;
1cc91f1b 151
f0e9217a 152static int
e841c3e0 153memoized_print (SCM obj, SCM port, scm_print_state *pstate)
f0e9217a 154{
9882ea19 155 int writingp = SCM_WRITINGP (pstate);
b7f3516f 156 scm_puts ("#<memoized ", port);
9882ea19 157 SCM_SET_WRITINGP (pstate, 1);
33b97402
MD
158#ifdef GUILE_DEBUG
159 scm_iprin1 (SCM_MEMOIZED_EXP (obj), port, pstate);
160#else
9882ea19 161 scm_iprin1 (scm_unmemoize (obj), port, pstate);
33b97402 162#endif
9882ea19 163 SCM_SET_WRITINGP (pstate, writingp);
b7f3516f 164 scm_putc ('>', port);
f0e9217a
MD
165 return 1;
166}
167
a1ec6916 168SCM_DEFINE (scm_memoized_p, "memoized?", 1, 0, 0,
1bbd0b84 169 (SCM obj),
ba94f79e 170 "Return @code{#t} if @var{obj} is memoized.")
1bbd0b84 171#define FUNC_NAME s_scm_memoized_p
f0e9217a 172{
0c95b57d 173 return SCM_BOOL(SCM_MEMOIZEDP (obj));
f0e9217a 174}
1bbd0b84 175#undef FUNC_NAME
f0e9217a 176
f0e9217a 177SCM
1bbd0b84 178scm_make_memoized (SCM exp, SCM env)
f0e9217a 179{
6c179711 180 /* *fixme* Check that env is a valid environment. */
f0e9217a 181 register SCM z, ans;
f83e2737 182 SCM_ENTER_A_SECTION;
cffcab30
DH
183 SCM_NEWSMOB (z, SCM_UNPACK (exp), SCM_UNPACK (env));
184 SCM_NEWSMOB (ans, scm_tc16_memoized, SCM_UNPACK (z));
f83e2737 185 SCM_EXIT_A_SECTION;
f0e9217a
MD
186 return ans;
187}
188
33b97402
MD
189#ifdef GUILE_DEBUG
190/*
191 * Some primitives for construction of memoized code
192 *
193 * - procedure: memcons CAR CDR [ENV]
194 *
195 * Construct a pair, encapsulated in a memoized object.
196 *
197 * The CAR and CDR can be either normal or memoized. If ENV isn't
198 * specified, the top-level environment of the current module will
199 * be assumed. All environments must match.
200 *
201 * - procedure: make-gloc VARIABLE [ENV]
202 *
203 * Return a gloc, encapsulated in a memoized object.
204 *
205 * (Glocs can't exist in normal list structures, since they will
206 * be mistaken for structs.)
207 *
208 * - procedure: gloc? OBJECT
209 *
210 * Return #t if OBJECT is a memoized gloc.
211 *
212 * - procedure: make-iloc FRAME BINDING CDRP
213 *
214 * Return an iloc referring to frame no. FRAME, binding
215 * no. BINDING. If CDRP is non-#f, the iloc is referring to a
216 * frame consisting of a single pair, with the value stored in the
217 * CDR.
218 *
219 * - procedure: iloc? OBJECT
220 *
221 * Return #t if OBJECT is an iloc.
222 *
223 * - procedure: mem->proc MEMOIZED
224 *
225 * Construct a closure from the memoized lambda expression MEMOIZED
226 *
227 * WARNING! The code is not copied!
228 *
229 * - procedure: proc->mem CLOSURE
230 *
231 * Turn the closure CLOSURE into a memoized object.
232 *
233 * WARNING! The code is not copied!
234 *
235 * - constant: SCM_IM_AND
236 * - constant: SCM_IM_BEGIN
237 * - constant: SCM_IM_CASE
238 * - constant: SCM_IM_COND
239 * - constant: SCM_IM_DO
240 * - constant: SCM_IM_IF
241 * - constant: SCM_IM_LAMBDA
242 * - constant: SCM_IM_LET
243 * - constant: SCM_IM_LETSTAR
244 * - constant: SCM_IM_LETREC
245 * - constant: SCM_IM_OR
246 * - constant: SCM_IM_QUOTE
247 * - constant: SCM_IM_SET
248 * - constant: SCM_IM_DEFINE
249 * - constant: SCM_IM_APPLY
250 * - constant: SCM_IM_CONT
bbab09f6 251 * - constant: SCM_IM_DISPATCH
33b97402
MD
252 */
253
a0599745
MD
254#include "libguile/variable.h"
255#include "libguile/procs.h"
33b97402 256
a1ec6916 257SCM_DEFINE (scm_make_gloc, "make-gloc", 1, 1, 0,
1bbd0b84 258 (SCM var, SCM env),
ba94f79e
MG
259 "Create a gloc for variable @var{var} in the environment\n"
260 "@var{env}.")
1bbd0b84 261#define FUNC_NAME s_scm_make_gloc
33b97402
MD
262{
263#if 1 /* Unsafe */
0c95b57d 264 if (SCM_CONSP (var))
33b97402
MD
265 var = scm_cons (SCM_BOOL_F, var);
266 else
267#endif
3b3b36dd 268 SCM_VALIDATE_VARIABLE (1,var);
33b97402 269 if (SCM_UNBNDP (env))
7e73eaee 270 env = scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE);
33b97402 271 else
3b3b36dd 272 SCM_VALIDATE_NULLORCONS (2,env);
33b97402
MD
273 return scm_make_memoized (SCM_VARVCELL (var) + 1, env);
274}
1bbd0b84 275#undef FUNC_NAME
33b97402 276
a1ec6916 277SCM_DEFINE (scm_gloc_p, "gloc?", 1, 0, 0,
1bbd0b84 278 (SCM obj),
ba94f79e 279 "Return @code{#t} if @var{obj} is a gloc.")
1bbd0b84 280#define FUNC_NAME s_scm_gloc_p
33b97402 281{
cabe682c 282 return SCM_BOOL((SCM_MEMOIZEDP (obj)
f1267706 283 && (SCM_UNPACK(SCM_MEMOIZED_EXP (obj)) & 7) == 1));
33b97402 284}
1bbd0b84 285#undef FUNC_NAME
33b97402 286
a1ec6916 287SCM_DEFINE (scm_make_iloc, "make-iloc", 3, 0, 0,
1bbd0b84 288 (SCM frame, SCM binding, SCM cdrp),
ba94f79e
MG
289 "Return a new iloc with frame offset @var{frame}, binding\n"
290 "offset @var{binding} and the cdr flag @var{cdrp}.")
1bbd0b84 291#define FUNC_NAME s_scm_make_iloc
33b97402 292{
3b3b36dd
GB
293 SCM_VALIDATE_INUM (1,frame);
294 SCM_VALIDATE_INUM (2,binding);
33b97402
MD
295 return (SCM_ILOC00
296 + SCM_IFRINC * SCM_INUM (frame)
297 + (SCM_NFALSEP (cdrp) ? SCM_ICDR : 0)
298 + SCM_IDINC * SCM_INUM (binding));
299}
1bbd0b84 300#undef FUNC_NAME
33b97402 301
a1ec6916 302SCM_DEFINE (scm_iloc_p, "iloc?", 1, 0, 0,
1bbd0b84 303 (SCM obj),
ba94f79e 304 "Return @code{#t} if @var{obj} is an iloc.")
0f981281 305#define FUNC_NAME s_scm_iloc_p
33b97402 306{
1bbd0b84 307 return SCM_BOOL(SCM_ILOCP (obj));
33b97402 308}
1bbd0b84 309#undef FUNC_NAME
33b97402 310
a1ec6916 311SCM_DEFINE (scm_memcons, "memcons", 2, 1, 0,
1bbd0b84 312 (SCM car, SCM cdr, SCM env),
ba94f79e
MG
313 "Return a new memoized cons cell with @var{car} and @var{cdr}\n"
314 "as members and @var{env} as the environment.")
1bbd0b84 315#define FUNC_NAME s_scm_memcons
33b97402 316{
0c95b57d 317 if (SCM_MEMOIZEDP (car))
33b97402
MD
318 {
319 /*fixme* environments may be two different but equal top-level envs */
320 if (!SCM_UNBNDP (env) && SCM_MEMOIZED_ENV (car) != env)
e8e9b690 321 SCM_MISC_ERROR ("environment mismatch arg1 <-> arg3",
33b97402
MD
322 scm_cons2 (car, env, SCM_EOL));
323 else
324 env = SCM_MEMOIZED_ENV (car);
325 car = SCM_MEMOIZED_EXP (car);
326 }
0c95b57d 327 if (SCM_MEMOIZEDP (cdr))
33b97402
MD
328 {
329 if (!SCM_UNBNDP (env) && SCM_MEMOIZED_ENV (cdr) != env)
e8e9b690 330 SCM_MISC_ERROR ("environment mismatch arg2 <-> arg3",
33b97402
MD
331 scm_cons2 (cdr, env, SCM_EOL));
332 else
333 env = SCM_MEMOIZED_ENV (cdr);
334 cdr = SCM_MEMOIZED_EXP (cdr);
335 }
336 if (SCM_UNBNDP (env))
7e73eaee 337 env = scm_top_level_env (SCM_TOP_LEVEL_LOOKUP_CLOSURE);
33b97402 338 else
3b3b36dd 339 SCM_VALIDATE_NULLORCONS (3,env);
33b97402
MD
340 return scm_make_memoized (scm_cons (car, cdr), env);
341}
1bbd0b84 342#undef FUNC_NAME
33b97402 343
a1ec6916 344SCM_DEFINE (scm_mem_to_proc, "mem->proc", 1, 0, 0,
1bbd0b84 345 (SCM obj),
ba94f79e
MG
346 "Convert a memoized object (which must be a lambda expression)\n"
347 "to a procedure.")
1bbd0b84 348#define FUNC_NAME s_scm_mem_to_proc
33b97402
MD
349{
350 SCM env;
3b3b36dd 351 SCM_VALIDATE_MEMOIZED (1,obj);
33b97402
MD
352 env = SCM_MEMOIZED_ENV (obj);
353 obj = SCM_MEMOIZED_EXP (obj);
354 if (!(SCM_NIMP (obj) && SCM_CAR (obj) == SCM_IM_LAMBDA))
e8e9b690 355 SCM_MISC_ERROR ("expected lambda expression",
33b97402
MD
356 scm_cons (obj, SCM_EOL));
357 return scm_closure (SCM_CDR (obj), env);
358}
1bbd0b84 359#undef FUNC_NAME
33b97402 360
a1ec6916 361SCM_DEFINE (scm_proc_to_mem, "proc->mem", 1, 0, 0,
1bbd0b84 362 (SCM obj),
ba94f79e 363 "Convert a procedure to a memoized object.")
1bbd0b84 364#define FUNC_NAME s_scm_proc_to_mem
33b97402 365{
5623a9b4 366 SCM_VALIDATE_CLOSURE (1, obj);
33b97402
MD
367 return scm_make_memoized (scm_cons (SCM_IM_LAMBDA, SCM_CODE (obj)),
368 SCM_ENV (obj));
369}
1bbd0b84 370#undef FUNC_NAME
33b97402
MD
371
372#endif /* GUILE_DEBUG */
373
a1ec6916 374SCM_DEFINE (scm_unmemoize, "unmemoize", 1, 0, 0,
1bbd0b84 375 (SCM m),
ba94f79e 376 "Unmemoize the memoized expression @var{m},")
1bbd0b84 377#define FUNC_NAME s_scm_unmemoize
f0e9217a 378{
3b3b36dd 379 SCM_VALIDATE_MEMOIZED (1,m);
bfe3154c 380 return scm_unmemocopy (SCM_MEMOIZED_EXP (m), SCM_MEMOIZED_ENV (m));
f0e9217a 381}
1bbd0b84 382#undef FUNC_NAME
f0e9217a 383
a1ec6916 384SCM_DEFINE (scm_memoized_environment, "memoized-environment", 1, 0, 0,
1bbd0b84 385 (SCM m),
ba94f79e 386 "Return the environment of the memoized expression @var{m}.")
1bbd0b84 387#define FUNC_NAME s_scm_memoized_environment
f0e9217a 388{
3b3b36dd 389 SCM_VALIDATE_MEMOIZED (1,m);
bfe3154c 390 return SCM_MEMOIZED_ENV (m);
f0e9217a 391}
1bbd0b84 392#undef FUNC_NAME
f0e9217a 393
a1ec6916 394SCM_DEFINE (scm_procedure_name, "procedure-name", 1, 0, 0,
1bbd0b84 395 (SCM proc),
ba94f79e 396 "Return the name of the procedure @var{proc}")
1bbd0b84 397#define FUNC_NAME s_scm_procedure_name
f0e9217a 398{
3b3b36dd 399 SCM_VALIDATE_PROC (1,proc);
f0e9217a 400 switch (SCM_TYP7 (proc)) {
a726dd9d
MD
401 case scm_tcs_subrs:
402 return SCM_SNAME (proc);
403 default:
f0e9217a 404 {
63c51b9a 405 SCM name = scm_procedure_property (proc, scm_sym_name);
f0e9217a 406#if 0
63c51b9a
MD
407 /* Source property scm_sym_procname not implemented yet... */
408 SCM name = scm_source_property (SCM_CAR (SCM_CDR (SCM_CODE (proc))), scm_sym_procname);
f0e9217a 409 if (SCM_FALSEP (name))
63c51b9a 410 name = scm_procedure_property (proc, scm_sym_name);
f0e9217a 411#endif
c75512d6
MD
412 if (SCM_FALSEP (name) && SCM_CLOSUREP (proc))
413 name = scm_reverse_lookup (SCM_ENV (proc), proc);
f0e9217a
MD
414 return name;
415 }
f0e9217a
MD
416 }
417}
1bbd0b84 418#undef FUNC_NAME
f0e9217a 419
a1ec6916 420SCM_DEFINE (scm_procedure_source, "procedure-source", 1, 0, 0,
1bbd0b84 421 (SCM proc),
ba94f79e 422 "Return the source of the procedure @var{proc}.")
1bbd0b84 423#define FUNC_NAME s_scm_procedure_source
f0e9217a 424{
6b5a304f 425 SCM_VALIDATE_NIM (1,proc);
f0e9217a
MD
426 switch (SCM_TYP7 (proc)) {
427 case scm_tcs_closures:
428 {
429 SCM src;
63c51b9a 430 src = scm_source_property (SCM_CDR (SCM_CODE (proc)), scm_sym_copy);
cffcab30 431 if (! SCM_FALSEP (src))
63c51b9a 432 return scm_cons2 (scm_sym_lambda, SCM_CAR (SCM_CODE (proc)), src);
f0e9217a 433 src = SCM_CODE (proc);
63c51b9a 434 return scm_cons (scm_sym_lambda,
f0e9217a 435 scm_unmemocopy (src,
e2806c10 436 SCM_EXTEND_ENV (SCM_CAR (src),
f0e9217a
MD
437 SCM_EOL,
438 SCM_ENV (proc))));
439 }
f0e9217a
MD
440 case scm_tcs_subrs:
441#ifdef CCLO
442 case scm_tc7_cclo:
443#endif
444 /* It would indeed be a nice thing if we supplied source even for
445 built in procedures! */
63c51b9a 446 return scm_procedure_property (proc, scm_sym_source);
f0e9217a 447 default:
276dd677
DH
448 SCM_WRONG_TYPE_ARG (1, proc);
449 /* not reached */
f0e9217a
MD
450 }
451}
1bbd0b84 452#undef FUNC_NAME
f0e9217a 453
a1ec6916 454SCM_DEFINE (scm_procedure_environment, "procedure-environment", 1, 0, 0,
1bbd0b84 455 (SCM proc),
ba94f79e 456 "Return the environment of the procedure @var{proc}.")
1bbd0b84 457#define FUNC_NAME s_scm_procedure_environment
f0e9217a 458{
6b5a304f 459 SCM_VALIDATE_NIM (1,proc);
f0e9217a
MD
460 switch (SCM_TYP7 (proc)) {
461 case scm_tcs_closures:
462 return SCM_ENV (proc);
f0e9217a
MD
463 case scm_tcs_subrs:
464#ifdef CCLO
465 case scm_tc7_cclo:
466#endif
467 return SCM_EOL;
468 default:
276dd677
DH
469 SCM_WRONG_TYPE_ARG (1, proc);
470 /* not reached */
f0e9217a
MD
471 }
472}
1bbd0b84 473#undef FUNC_NAME
f0e9217a 474
bfe3154c 475\f
f0e9217a
MD
476
477/* Eval in a local environment. We would like to have the ability to
e38ecb05
MD
478 * evaluate in a specified local environment, but due to the
479 * memoization this isn't normally possible. We solve it by copying
480 * the code before evaluating. One solution would be to have eval.c
481 * generate yet another evaluator. They are not very big actually.
f0e9217a 482 */
a1ec6916 483SCM_DEFINE (scm_local_eval, "local-eval", 1, 1, 0,
1bbd0b84 484 (SCM exp, SCM env),
b380b885
MD
485 "Evaluate @var{exp} in its environment. If @var{env} is supplied,\n"
486 "it is the environment in which to evaluate @var{exp}. Otherwise,\n"
487 "@var{exp} must be a memoized code object (in which case, its environment\n"
488 "is implicit).")
1bbd0b84 489#define FUNC_NAME s_scm_local_eval
f0e9217a 490{
6c179711
MD
491 if (SCM_UNBNDP (env))
492 {
82b3290d
MD
493 SCM_VALIDATE_MEMOIZED (1, exp);
494 return scm_i_eval_x (SCM_MEMOIZED_EXP (exp), SCM_MEMOIZED_ENV (exp));
6c179711 495 }
82b3290d 496 return scm_i_eval (exp, env);
f0e9217a 497}
1bbd0b84 498#undef FUNC_NAME
f0e9217a 499
c75512d6 500#if 0
1bbd0b84 501SCM_REGISTER_PROC (s_reverse_lookup, "reverse-lookup", 2, 0, 0, scm_reverse_lookup);
c75512d6
MD
502#endif
503
504SCM
505scm_reverse_lookup (SCM env, SCM data)
506{
22a52da1 507 while (SCM_CONSP (env) && SCM_CONSP (SCM_CAR (env)))
c75512d6 508 {
22a52da1
DH
509 SCM names = SCM_CAAR (env);
510 SCM values = SCM_CDAR (env);
0c95b57d 511 while (SCM_CONSP (names))
c75512d6 512 {
cffcab30 513 if (SCM_EQ_P (SCM_CAR (values), data))
c75512d6
MD
514 return SCM_CAR (names);
515 names = SCM_CDR (names);
516 values = SCM_CDR (values);
517 }
22a52da1 518 if (!SCM_NULLP (names) && SCM_EQ_P (values, data))
c75512d6
MD
519 return names;
520 env = SCM_CDR (env);
521 }
522 return SCM_BOOL_F;
523}
524
bfe3154c 525SCM
6e8d25a6 526scm_start_stack (SCM id, SCM exp, SCM env)
bfe3154c
MD
527{
528 SCM answer;
c6b8a41a 529 scm_debug_frame vframe;
c0ab1b8d 530 scm_debug_info vframe_vect_body;
9fa2c7b1
MD
531 vframe.prev = scm_last_debug_frame;
532 vframe.status = SCM_VOIDFRAME;
533 vframe.vect = &vframe_vect_body;
534 vframe.vect[0].id = id;
535 scm_last_debug_frame = &vframe;
82b3290d 536 answer = scm_i_eval (exp, env);
9fa2c7b1
MD
537 scm_last_debug_frame = vframe.prev;
538 return answer;
539}
540
b8229a3b
MS
541SCM_SYNTAX(s_start_stack, "start-stack", scm_makacro, scm_m_start_stack);
542
8a04c1a2 543static SCM
6e8d25a6 544scm_m_start_stack (SCM exp, SCM env)
68baa7e7 545#define FUNC_NAME s_start_stack
9fa2c7b1 546{
bfe3154c 547 exp = SCM_CDR (exp);
68baa7e7
DH
548 if (!SCM_ECONSP (exp)
549 || !SCM_ECONSP (SCM_CDR (exp))
550 || !SCM_NULLP (SCM_CDDR (exp)))
551 SCM_WRONG_NUM_ARGS ();
9fa2c7b1 552 return scm_start_stack (scm_eval_car (exp, env), SCM_CADR (exp), env);
bfe3154c 553}
68baa7e7
DH
554#undef FUNC_NAME
555
f0e9217a
MD
556
557/* {Debug Objects}
558 *
559 * The debugging evaluator throws these on frame traps.
560 */
561
e841c3e0 562scm_bits_t scm_tc16_debugobj;
f0e9217a 563
f0e9217a 564static int
e841c3e0 565debugobj_print (SCM obj, SCM port, scm_print_state *pstate)
f0e9217a 566{
b7f3516f 567 scm_puts ("#<debug-object ", port);
c209c88e 568 scm_intprint ((int) SCM_DEBUGOBJ_FRAME (obj), 16, port);
b7f3516f 569 scm_putc ('>', port);
f0e9217a
MD
570 return 1;
571}
572
a1ec6916 573SCM_DEFINE (scm_debug_object_p, "debug-object?", 1, 0, 0,
1bbd0b84 574 (SCM obj),
ba94f79e 575 "Return @code{#t} if @var{obj} is a debug object.")
1bbd0b84 576#define FUNC_NAME s_scm_debug_object_p
f0e9217a 577{
0c95b57d 578 return SCM_BOOL(SCM_DEBUGOBJP (obj));
f0e9217a 579}
1bbd0b84 580#undef FUNC_NAME
f0e9217a 581
1cc91f1b 582
f0e9217a 583SCM
1bbd0b84 584scm_make_debugobj (scm_debug_frame *frame)
f0e9217a
MD
585{
586 register SCM z;
f0e9217a 587 SCM_NEWCELL (z);
f83e2737 588 SCM_ENTER_A_SECTION;
cffcab30
DH
589 SCM_SET_DEBUGOBJ_FRAME (z, frame);
590 SCM_SET_CELL_TYPE (z, scm_tc16_debugobj);
f83e2737 591 SCM_EXIT_A_SECTION;
f0e9217a
MD
592 return z;
593}
594
f0e9217a
MD
595\f
596
fe57f652
MD
597/* Undocumented debugging procedure */
598#ifdef GUILE_DEBUG
a1ec6916 599SCM_DEFINE (scm_debug_hang, "debug-hang", 0, 1, 0,
1bbd0b84 600 (SCM obj),
ba94f79e
MG
601 "Go into an endless loop, which can be only terminated with\n"
602 "a debugger.")
1bbd0b84 603#define FUNC_NAME s_scm_debug_hang
e38ecb05
MD
604{
605 int go = 0;
606 while (!go) ;
607 return SCM_UNSPECIFIED;
608}
1bbd0b84 609#undef FUNC_NAME
fe57f652 610#endif
e38ecb05
MD
611
612\f
613
f0e9217a
MD
614void
615scm_init_debug ()
616{
5e8d7fd4 617 scm_init_opts (scm_debug_options, scm_debug_opts, SCM_N_DEBUG_OPTIONS);
ee340120 618
e841c3e0
KN
619 scm_tc16_memoized = scm_make_smob_type ("memoized", 0);
620 scm_set_smob_mark (scm_tc16_memoized, scm_markcdr);
621 scm_set_smob_print (scm_tc16_memoized, memoized_print);
23a62151 622
e841c3e0
KN
623 scm_tc16_debugobj = scm_make_smob_type ("debug-object", 0);
624 scm_set_smob_print (scm_tc16_debugobj, debugobj_print);
f0e9217a 625
33b97402
MD
626#ifdef GUILE_DEBUG
627 scm_sysintern ("SCM_IM_AND", SCM_IM_AND);
628 scm_sysintern ("SCM_IM_BEGIN", SCM_IM_BEGIN);
629 scm_sysintern ("SCM_IM_CASE", SCM_IM_CASE);
630 scm_sysintern ("SCM_IM_COND", SCM_IM_COND);
631 scm_sysintern ("SCM_IM_DO", SCM_IM_DO);
632 scm_sysintern ("SCM_IM_IF", SCM_IM_IF);
633 scm_sysintern ("SCM_IM_LAMBDA", SCM_IM_LAMBDA);
634 scm_sysintern ("SCM_IM_LET", SCM_IM_LET);
635 scm_sysintern ("SCM_IM_LETSTAR", SCM_IM_LETSTAR);
636 scm_sysintern ("SCM_IM_LETREC", SCM_IM_LETREC);
637 scm_sysintern ("SCM_IM_OR", SCM_IM_OR);
638 scm_sysintern ("SCM_IM_QUOTE", SCM_IM_QUOTE);
bbab09f6 639 scm_sysintern ("SCM_IM_SET_X", SCM_IM_SET_X);
33b97402
MD
640 scm_sysintern ("SCM_IM_DEFINE", SCM_IM_DEFINE);
641 scm_sysintern ("SCM_IM_APPLY", SCM_IM_APPLY);
642 scm_sysintern ("SCM_IM_CONT", SCM_IM_CONT);
bbab09f6 643 scm_sysintern ("SCM_IM_DISPATCH", SCM_IM_DISPATCH);
33b97402 644#endif
f0e9217a
MD
645 scm_add_feature ("debug-extensions");
646
8dc9439f 647#ifndef SCM_MAGIC_SNARFER
a0599745 648#include "libguile/debug.x"
8dc9439f 649#endif
f0e9217a 650}
89e00824
ML
651
652/*
653 Local Variables:
654 c-file-style: "gnu"
655 End:
656*/