summaryrefslogtreecommitdiffstats
path: root/debug.c
blob: 22c387b9137f07cc91416339bfb5f220a8a6385b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
/* Copyright (C) 2001 Free Software Foundation, Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 * As a special exception, the Free Software Foundation gives permission
 * for additional uses of the text contained in its release of SCM.
 *
 * The exception is that, if you link the SCM library with other files
 * to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * Your use of that executable is in no way restricted on account of
 * linking the SCM library code into it.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU General Public License.
 *
 * This exception applies only to the code released by the
 * Free Software Foundation under the name SCM.  If you copy
 * code from other Free Software Foundation releases into a copy of
 * SCM, as the General Public License permits, the exception does
 * not apply to the code that you add in this way.  To avoid misleading
 * anyone as to the status of such modified files, you must delete
 * this exception notice from them.
 *
 * If you write modifications of your own for SCM, it is your choice
 * whether to permit this exception to apply to your modifications.
 * If you do not wish that, delete this exception notice.
 */

/* "debug.c" procedures for displaying and debugging code.
   Authors: Radey Shouman & Aubrey Jaffer. */

#include "scm.h"
#include "setjump.h"

long tc16_codeptr;		/*  Type code for non-executable code
				    wrappers */

static SCM prinhead P((SCM x, SCM port, int writing));
static void prinbindings P((SCM names, SCM inits, SCM init_env,
			    SCM steps, SCM step_env, SCM port, int writing));

SCM scm_env_rlookup(addr, stenv, what)
     SCM addr, stenv;
     char *what;
{
  SCM env, fr;
  int icdrp;
  unsigned int idist, iframe;
  if (IMP(addr)) {
    if (!ILOCP(addr)) return BOOL_F;
    iframe = IFRAME(addr);
    idist = IDIST(addr);
    icdrp = ICDRP(addr) && 1;
  }
  else {
    if (!ISYMP(CAR(addr))) return BOOL_F;
    icdrp = 0;
    switch (ISYMNUM(CAR(addr))) {
    default: return BOOL_F;
    case (ISYMNUM(IM_FARLOC_CDR)):
      icdrp = 1;
    case (ISYMNUM(IM_FARLOC_CAR)):
      fr = CDR(addr);
      iframe = INUM(CAR(fr));
      idist = INUM(CDR(fr));
      break;
    }
  }
  for (env = stenv; NIMP(env); env = CDR(env)) {
    fr = CAR(env);
    if (INUMP(fr)) {
      ASSERT(NIMP(env) && CONSP(env), stenv, s_badenv, what);
      env = CDR(env);
      continue;
    }
    if (SCM_LINUMP(fr)) continue;
    if (NIMP(fr) && CONSP(fr) && IMP(CAR(fr))) continue;
    if (0==iframe--) {
      while (idist--) {
	if (IMP(fr) || NCONSP(fr)) return BOOL_F;
	fr = CDR(fr);
      }
      if (!icdrp) {
	if (NIMP(fr) && CONSP(fr))
	  fr = CAR(fr);
	else
	  fr = BOOL_F;
      }
      if (NIMP(fr) && IDENTP(fr)) return fr;
      return BOOL_F;
    }
  }
  return BOOL_F;
}

SCM scm_env_addprop(prop, val, env)
     SCM prop, val, env;
{
  if (UNBNDP(prop)) return env;
  return cons2(prop, val, env);
}

SCM scm_env_getprop(prop, env)
     SCM prop, env;
{
  SCM e = env;
  if (!UNBNDP(prop)) {
    while (NIMP(e)) {
      if (INUMP(CAR(e))) {
	if (CAR(e)==prop) return CDR(e);
	e = CDR(e);
	ASSERT(NIMP(e), env, s_badenv, "env_getprop");
      }
      e = CDR(e);
    }
  }
  return BOOL_F;
}

static SCM prinhead(x, port, writing)
     SCM x, port;
     int writing;
{
  lputc('(', port);
  iprin1(CAR(x), port, writing);
  lputc(' ', port);
  return CDR(x);
}

static void prinbindings(names, inits, init_env,
			 steps, step_env, port, writing)
     SCM names, inits, init_env, steps, step_env, port;
     int writing;
{
  lputc('(', port);
  while (NIMP(names) && NIMP(inits)) {
    lputc('(', port);
    iprin1(CAR(names), port, writing);
    lputc(' ', port);
    scm_princode(CAR(inits), init_env, port, writing);
    if (NIMP(steps)) {
      lputc(' ', port);
      scm_princode(CAR(steps), step_env, port, writing);
      steps = CDR(steps);
    }
    lputc(')', port);
    names = CDR(names);
    inits = CDR(inits);
    if (NIMP(names)) lputc(' ', port);
  }
  lputs(") ", port);
}

void scm_princode(code, env, port, writing)
     SCM code, env, port;
     int writing;
{
  SCM oenv = env, name, init, bdgs, x = code;
  if (UNBNDP(env)) {
    iprin1(code, port, writing);
    return;
  }
 tail:
  if (IMP(x)) {
    if (ILOCP(x)) {
    local:
      name = scm_env_rlookup(x, env, "princode");
      if (FALSEP(name)) goto gencase;
      lputs("#@", port);
      iprin1(name, port, writing);
      return;
    }
    else
      goto gencase;
  }
  if (SCM_GLOCP(x)) {
    iprin1(x, port, writing);
    return;
  }
  switch (TYP7(x)) {
  default: gencase:
    iprin1(x, port, writing);
    return;
  gencode:
  case tcs_cons_gloc:
  case tcs_cons_nimcar:
  case tcs_cons_iloc:
  case (127 & IM_OR): case (127 & IM_AND): case (127 & IM_BEGIN):
  case (127 & IM_SET): case (127 & IM_COND): case (127 & IM_CASE):
  case (127 & IM_IF):
    lputc('(', port);
    scm_princode(CAR(x), env, port, writing);
  body:
    x = CDR(x);
  no_cdr:
    for (; NNULLP(x); x = CDR(x)) {
      if (IMP(x) || NECONSP(x)) {
	lputs(" . ", port);
	iprin1(x, port, writing);
	break;
      }
      lputc(' ', port);
      scm_princode(CAR(x), env, port, writing);
    }
    lputc(')', port);
    return;
  case (127 & IM_LAMBDA):
    x = prinhead(x, port, writing);
    env = CAR(x);
    bdgs = SCM_ENV_FORMALS(env);
    if (IMP(bdgs) || NECONSP(bdgs))
      iprin1(bdgs, port, writing);
    else {
      lputc('(', port);
      while (!0) {
	if (NECONSP(bdgs)) break;
	iprin1(CAR(bdgs), port, writing);
	if (NIMP(bdgs = CDR(bdgs)))
	  lputc(' ', port);
	else break;
      }
      if (NIMP(bdgs)) {
	lputs(". ", port);
	iprin1(bdgs, port, writing);
      }
      lputc(')', port);
    }
    goto body;
  case (127 & IM_LETREC):
  case (127 & IM_LET):
    x = prinhead(x, port, writing);
    env = CAR(x);
    prinbindings(SCM_ENV_FORMALS(env),
		 CAR(CDR(x)), (TYP7(x)==(127 & IM_LET) ? oenv: env),
		 UNDEFINED, UNDEFINED, port, writing);
    x = CDR(x);
    goto body;
  case (127 & IM_LETSTAR):
    x = prinhead(x, port, writing);
    lputc('(', port);
    if (NIMP(bdgs = CAR(x))) {
      oenv = CAR(bdgs);
      bdgs = CDR(bdgs);
      while (!0) {
	init = CAR(bdgs);
	bdgs = CDR(bdgs);
	env = CAR(bdgs);
	lputc('(', port);
	iprin1(SCM_ENV_FORMALS(env), port, writing);
	lputc(' ', port);
	scm_princode(init, oenv, port, writing);
	oenv = env;
	lputc(')', port);
	if (IMP(bdgs = CDR(bdgs)))
	  break;
	lputc(' ', port);
      }
    }
    lputs(") ", port);
    goto body;
  case (127 & IM_DO):
    {
      /* (#@do (env (init ...) (test ...) (body ...) step ...)) */
      SCM test, steps;
      x = prinhead(x, port, writing);
      env = CAR(x);
      x = CDR(x);
      init = CAR(x);
      x = CDR(x);
      test = CAR(x);
      x = CDR(x);
      steps = CDR(x);
      x = CAR(x);
      prinbindings(SCM_ENV_FORMALS(env), init, oenv, steps, env,
		   port, writing);
      scm_princode(test, env, port, writing);
      lputc(' ', port);
      goto no_cdr;
    }
  case (127 & IM_FUNCALL):
    lputc('(', port);
    x = CDR(x);
    scm_princode(CAR(x), env, port, writing);
    goto body;
  case (127 & MAKISYM(0)):
    if (!ISYMP(CAR(x))) goto gencode;
    switch (ISYMNUM(CAR(x))) {
    default:
      goto gencode;
    case ISYMNUM(IM_LINUM):
      x = CDR(x);
      goto tail;
    case ISYMNUM(IM_FARLOC_CAR):
    case ISYMNUM(IM_FARLOC_CDR):
      goto local;
    }
  }
}

void scm_princlosure(proc, port, writing)
     SCM proc, port;
     int writing;
{
  SCM env, linum = UNDEFINED;
  proc = CODE(proc);
  lputs("#<CLOSURE ", port);
  env = CAR(proc);
#ifdef CAUTIOUS
  if (NIMP(env=scm_env_getprop(SCM_ENV_PROCNAME, env))) {
    iprin1(CAR(env), port, 1);
    lputc(' ', port);
    env = CDR(env);
    if (NIMP(env) && SCM_LINUMP(CAR(env)))
      linum = CAR(env);
  }
  else
    lputs("<anon> ", port);
  env = CAR(proc);
  if (NIMP(env=scm_env_getprop(SCM_ENV_FILENAME, env)))
    scm_line_msg(CAR(env), linum, port);
#endif
  env = CAR(proc);
  iprin1(SCM_ENV_FORMALS(env), port, writing);
  if (writing) {
    for(proc = CDR(proc); NIMP(proc); proc = CDR(proc)) {
      lputc(' ', port);
      scm_princode(CAR(proc), env, port, writing);
    }
  }
  lputc('>', port);
}

static char s_int2linum[] = "integer->line-number";
SCM scm_int2linum(n)
     SCM n;
{
  int i = INUM(n);
  ASSERT(INUMP(n) && i >= 0, n, ARG1, s_int2linum);
  return SCM_MAKE_LINUM(i);
}

static char s_linum2int[] = "line-number->integer";
SCM scm_linum2int(linum)
     SCM linum;
{
  ASSERT(SCM_LINUMP(linum), linum, ARG1, s_linum2int);
  return MAKINUM(SCM_LINUM(linum));
}

SCM scm_linump(obj)
     SCM obj;
{
  return SCM_LINUMP(obj) ? BOOL_T : BOOL_F;
}

static char s_remove_linums[] = "remove-line-numbers!";
SCM scm_remove_linums(x)
     SCM x;
{
  SCM ret = x;
  SCM *px = &ret;
 tail:
  x = *px;
  if (IMP(x)) return ret;
  if (CONSP(x)) {
    if (SCM_LINUMP(CAR(x))) {
      *px = CDR(x);
      px = &CDR(x);
      goto tail;
    }
    if (NIMP(CAR(x)))
      CAR(x) = scm_remove_linums(CAR(x));
    px = &CDR(x);
    goto tail;
  }
  else if (VECTORP(x)) {
    SCM *ve = VELTS(x);
    sizet i = LENGTH(x);
    while (i--) {
      if (NIMP(ve[i]))
	ve[i] = scm_remove_linums(ve[i]);
    }
    return ret;
  }
  else
    return ret;
}

#ifdef CAUTIOUS
long num_frames(estk, i)
     SCM estk;
     int i;
{
  long n = 0;
  while NIMP(estk) {
    n += (i - SCM_ESTK_BASE)/SCM_ESTK_FRLEN;
    i = INUM(SCM_ESTK_PARENT_INDEX(estk));
    estk = SCM_ESTK_PARENT(estk);
  }
  return n;
}

SCM *estk_frame(estk, i, nf)
     SCM estk;
     int i, nf;
{
  int n;
  /* Make this 1-based, because continuations have an extra frame at
     the top of the estk. */
  nf -= 1;
  while NIMP(estk) {
    n = (i - SCM_ESTK_BASE)/SCM_ESTK_FRLEN;
    if (nf <= n) return &(VELTS(estk)[i - nf*SCM_ESTK_FRLEN]);
    nf -= n;
    i = INUM(SCM_ESTK_PARENT_INDEX(estk));
    estk = SCM_ESTK_PARENT(estk);
  }
  return (SCM *)0;
}

SCM stacktrace1(estk, i)
     SCM estk;
     int i;
{
  SCM *frame, env, ste, lste = UNDEFINED;
  int n, nf = num_frames(estk, i);
  int nbrk1 = 7, nbrk2 = nf - 6;
  if (nf <= 0) return BOOL_F;
  lputs("\n;STACK TRACE", cur_errp);
  for (n = 1; n <= nf; n++) {
    if ((0 <= nbrk1--) || n >= nbrk2) {
      if (!(frame = estk_frame(estk, i, n))) continue;
      if (BOOL_F==(ste = frame[3])) continue;
      env = frame[2];
      if (ste != lste) {
	lste = ste;
	if (reset_safeport(sys_safep, 65, cur_errp)) {
	  /* The usual C setjmp, not SCM's setjump. */
	  if (0==setjmp(SAFEP_JMPBUF(sys_safep))) {
	    lputc('\n', cur_errp);
	    intprint((long)n, -10, sys_safep);
	    lputs("; ", sys_safep);
	    scm_princode(ste, env, sys_safep, 1);
	  }
	}
      }
      else {
	lputs("\n...", cur_errp);
	break;
      }
    }
  }
  lputc('\n', cur_errp);
  return BOOL_T;
}

SCM *cont_frame(contin, nf)
     SCM contin;
     int nf;
{
  CONTINUATION *cont = CONT(contin);
  SCM estk = cont->other.estk;
  int i = LENGTH(estk) - SCM_ESTK_FRLEN;
  if (0 == nf) return cont->other.stkframe;
  return estk_frame(estk, i, nf);
}

static char s_stack_trace[] = "stack-trace";
SCM scm_stack_trace(contin)
     SCM contin;
{
  SCM estk;
  int i;
  if (UNBNDP(contin)) {
    estk = scm_estk;
    i = (scm_estk_ptr - VELTS(scm_estk));
  }
  else {
    CONTINUATION *cont;
    ASSERT(NIMP(contin) && (tc7_contin==TYP7(contin)), contin, ARG1,
	   s_stack_trace);
    cont = CONT(contin);
    estk = cont->other.estk;
    i = LENGTH(estk) - SCM_ESTK_FRLEN;
  }
  return stacktrace1(estk, i);
}

static char s_frame_trace[] = "frame-trace";
SCM scm_frame_trace(contin, nf)
     SCM contin, nf;
{
    SCM *stkframe, code, env;
    ASSERT(NIMP(contin) && tc7_contin==TYP7(contin), contin, ARG1,
	   s_frame_trace);
    ASSERT(INUMP(nf) && INUM(nf) >= 0, nf, ARG2, s_frame_trace);
    if (!(stkframe = cont_frame(contin, INUM(nf))))
      return BOOL_F;
    env = stkframe[2];
    code = stkframe[3];
    scm_princode(code, env, cur_errp, 1);
    scm_scope_trace(env);
    return UNSPECIFIED;
}

static char s_frame2env[] = "frame->environment";
SCM scm_frame2env(contin, nf)
     SCM contin, nf;
{
    SCM *stkframe;
    ASSERT(NIMP(contin) && tc7_contin==TYP7(contin), contin, ARG1,
	   s_frame2env);
    ASSERT(INUMP(nf) && INUM(nf) >= 0, nf, ARG2, s_frame2env);
    if (!(stkframe = cont_frame(contin, INUM(nf))))
      return BOOL_F;
    return stkframe[2];
}

static char s_frame_eval[] = "frame-eval";
SCM scm_frame_eval(contin, nf, expr)
     SCM contin, nf, expr;
{
    SCM res, env, *stkframe;
    ASSERT(NIMP(contin) && tc7_contin==TYP7(contin), contin, ARG1,
	   s_frame_eval);
    ASSERT(INUMP(nf) && INUM(nf) >= 0, nf, ARG2, s_frame_eval);
    if (!(stkframe = cont_frame(contin, INUM(nf))))
      return BOOL_F;
    env = stkframe[2];
    if (IMP(expr)) return expr;
    DEFER_INTS_EGC;
    res = ceval(expr, env, stkframe[0]);
    ALLOW_INTS_EGC;
    return res;
}

#endif

static char s_scope_trace[] = "scope-trace";
SCM scm_scope_trace(env)
     SCM env;
{
  SCM ef, file = UNDEFINED;
  int fprinted = 0;
  if (UNBNDP(env))
    env = scm_current_env();
  else if (NIMP(env) && CLOSUREP(env))
    env = CAR(CODE(env));
  if (scm_nullenv_p(env))
    lputs("\n; in top level environment.", cur_errp);
  else
    lputs("\n; in scope:", cur_errp);
#ifdef CAUTIOUS
  if (NIMP(ef=scm_env_getprop(SCM_ENV_FILENAME, env))) {
    file = CAR(ef);
  }
#endif
  for (; NIMP(env); env = CDR(env)) {
    if (NCONSP(env)) {
    badenv:
      lputs("\n; corrupted environment ", cur_errp);
      iprin1(env, cur_errp, 1);
      return UNSPECIFIED;
    }
    ef = CAR(env);
    if (SCM_LINUMP(ef)) {
      lputs("\n;   ", cur_errp);
      scm_line_msg(file, ef, cur_errp);
      fprinted++;
    }
    else if (INUMP(ef)) {
      ASRTGO(NIMP(env) && CONSP(env), badenv);
      env = CDR(env);
#ifdef CAUTIOUS
      switch (ef) {
      default: break;
      case SCM_ENV_PROCNAME:
	lputs("  procedure ", cur_errp);
	iprin1(CAR(env), cur_errp, 1);
	break;
      }
#endif
    }
    else if (NIMP(ef) && CONSP(ef) && NIMP(CAR(ef)) && CONSP(CAR(ef))) {
      lputs("\n;   ", cur_errp);
      iprin1(CAR(ef), cur_errp, 1);
      lputs("  syntax bindings", cur_errp);
    }
    else {
      lputs("\n;   ", cur_errp);
      iprin1(ef, cur_errp, 1);
    }
  }
#ifdef CAUTIOUS
  if (NIMP(file) && !fprinted) {
    lputs("\n; defined by ", cur_errp);
    if (NIMP(file) && STRINGP(file))
      lputs("load: ", cur_errp);
    iprin1(file, cur_errp, 1);
    lputc('\n', cur_errp);
  }
#endif
  return UNSPECIFIED;
}

static char s_env_annotation[] = "environment-annotation";
SCM scm_env_annotation(var, stenv)
     SCM var, stenv;
{
  SCM s, frame, env = stenv;
#ifdef MACRO
  SCM mark = IDENT_ENV(var);
  if (NIMP(mark)) mark = CAR(mark);
#endif
  for (; NIMP(env); env = CDR(env)) {
    frame = CAR(env);
#ifdef MACRO
    if (frame==mark) {
      var = IDENT_PARENT(var);
      mark = IDENT_ENV(var);
      if (NIMP(mark)) mark = CAR(mark);
    }
#endif
    if (IMP(frame)) {
      if (INUMP(frame)) {
#ifndef RECKLESS
	if (!(NIMP(env) && CONSP(env))) {
	badenv: wta(stenv, s_badenv, s_env_annotation);
	}
#endif
	env = CDR(env);
      }
      continue;
    }
#ifdef MACRO
    if (NIMP(frame) && CONSP(frame) && BOOL_F==CAR(frame)) {
      /* syntax binding */
      s = assq(var, CDR(frame));
      if (NIMP(s)) goto local_out;
      continue;
    }
#endif
    for (; NIMP(frame); frame = CDR(frame)) {
      if (NCONSP(frame)) {
	if (var==frame)
	  goto local_out;
	break;
      }
      if (CAR(frame)==var) {
      local_out:
	env = CDR(env);
	if (IMP(env)) return BOOL_T;
	if (SCM_ENV_ANNOTATION != CAR(env)) return BOOL_T;
	env = CDR(env);
	ASRTGO(NIMP(env), badenv);
	s = assq(var, CAR(env));
	if (NIMP(s)) return s;
	return BOOL_T;
      }
      ASRTGO(CONSP(frame), badenv);
    }
  }
  ASRTGO(NULLP(env), badenv);
  return BOOL_F;
}

/* This is to be used for code backpointers to go into environments,
   allowing run-time reporting of error line numbers. */
SCM scm_wrapcode(code, env)
     SCM code, env;
{
  SCM z, x = cons(env, code);
  NEWCELL(z);
  CDR(z) = x;
  CAR(z) = tc16_codeptr;
  return z;
}

static int princodeptr(exp, port, writing)
     SCM exp;
     SCM port;
     int writing;
{
  SCM env = CAR(CDR(exp));
  lputs("#<code ", port);
  exp = CDR(CDR(exp));
  if (IMP(exp) || NCONSP(exp))
    scm_princode(exp, env, port, writing);
  else {
    while (!0) {
      if (IMP(exp) || NCONSP(exp)) break;
      scm_princode(CAR(exp), env, port, writing);
      lputc(' ', port);
      exp = CDR(exp);
    }
    if (NNULLP(exp)) {
      lputs(" . ", port);
      scm_princode(exp, env, port, writing);
    }
  }
  lputc('>', port);
  return !0;
}

static smobfuns codesmob = {markcdr, free0, princodeptr};

static iproc subr1os[] = {
  {s_scope_trace, scm_scope_trace},
#ifdef CAUTIOUS
  {s_stack_trace, scm_stack_trace},
#endif
	{0, 0}};

static iproc subr1s[] = {
  {s_int2linum, scm_int2linum},
  {"line-number?", scm_linump},
  {s_linum2int, scm_linum2int},
  {s_remove_linums, scm_remove_linums},
  {0, 0}};

static iproc subr2s[] = {
  {s_env_annotation, scm_env_annotation},
#ifdef CAUTIOUS
  {s_frame_trace, scm_frame_trace},
  {s_frame2env, scm_frame2env},
#endif
  {0, 0}};

void init_debug()
{
  tc16_codeptr = newsmob(&codesmob);
  init_iprocs(subr1os, tc7_subr_1o);
  init_iprocs(subr1s, tc7_subr_1);
  init_iprocs(subr2s, tc7_subr_2);
#ifdef CAUTIOUS
  make_subr(s_frame_eval, tc7_subr_3, scm_frame_eval);
#endif
}