File: debug.scm

package info (click to toggle)
scsh 0.5.1-2
  • links: PTS
  • area: non-free
  • in suites: potato, slink
  • size: 6,540 kB
  • ctags: 8,656
  • sloc: lisp: 39,346; ansic: 13,466; sh: 1,669; makefile: 624
file content (575 lines) | stat: -rw-r--r-- 15,756 bytes parent folder | download
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
; Copyright (c) 1993, 1994 Richard Kelsey and Jonathan Rees.  See file COPYING.


; Commands for debugging.

; translate

(define-command-syntax 'translate "<from> <to>"
  "establish file name translation"
  '(filename filename))

(define translate set-translation!)

; preview  -- show continuations

(define (preview)
  (display-preview (continuation-preview (command-continuation))
		   (command-output)))

(define (display-preview preview port)
  (for-each (lambda (info+pc)
	      (if (not (fluid-let-continuation-info? (car info+pc)))
		  (display-template-names (car info+pc) port)))
	    preview))

(define (display-template-names info port)
  (let ((names (debug-data-names info)))
    (display "  " port)
    (if (null? names)
	(begin (display "unnamed " port)
	       (write `(id ,(if (debug-data? info)
				(debug-data-uid info)
				info))
		      port))
	(let loop ((names names))
	  (if (car names)
	      (write (car names) port)
	      (display "unnamed" port))
	  (if (and (not (null? (cdr names)))
		   (cadr names))
	      (begin (display " in " port)
		     (loop (cdr names))))))
    (newline port)))

(define fluid-let-continuation-info?	;Incestuous!
  (let ((id (let-fluid (make-fluid #f) #f
	      (lambda ()
		(primitive-catch (lambda (k)
				   (template-id
				    (continuation-template k))))))))
    (lambda (info) 
      (eqv? (if (debug-data? info)
		(debug-data-uid info)
		info)
	    id))))


(define-command-syntax 'preview ""
  "show pending continuations (stack trace)"
  '())

; Proceed

(define (really-proceed vals)
  (let* ((level (command-level))
	 (condition (command-level-condition level)))
    (if (ok-to-proceed? condition)
	(throw-to-command-level
	     level
	     (lambda ()
	       (let ((interrupts (enabled-interrupts))
		     (new-interrupts (command-level-interrupts level)))
		 (if (not (= new-interrupts interrupts))
		     (begin (if (not (and (interrupt? condition)
					  (= (caddr condition) interrupts)))
				(write-line "(Disabling interrupts)"
					    (command-output)))
			    (set-enabled-interrupts! new-interrupts))))
	       (apply values vals)))
	(write-line "No way to proceed from here." (command-output)))))

(define-command-syntax 'proceed "<exp>" "proceed after an interrupt or error"
  '(&rest expression))

(define (proceed . exps)
  (really-proceed (map (lambda (exp)
			 (evaluate exp (environment-for-commands)))
		       exps)))

; Scrutinize the condition to ensure that it's safe to return from the
; call to RAISE.

(define (ok-to-proceed? condition)
  (and condition
       (if (error? condition)
	   (and (exception? condition)
		(let ((opcode (exception-opcode condition)))
		  (or (= opcode op/global)
		      (= opcode op/local0)
		      (= opcode op/set-global!)
		      (>= opcode op/eq?))))
	   #t)))

(define op/global (enum op global))
(define op/local0 (enum op local0))
(define op/set-global! (enum op set-global!))
(define op/eq? (enum op eq?))


(define (breakpoint . rest)
  (command-loop unspecific
		(make-condition 'breakpoint rest)))

(define-condition-type 'breakpoint '())
(define breakpoint? (condition-predicate 'breakpoint))


; push

(define-command-syntax 'push "" "push command level" '())

(define (push)
  (command-loop list
		(if (command-level? (focus-object))
		    (command-level-condition (focus-object))
		    #f)))

; reset

(define (reset)
  (abort-to-command-level (top-command-level)))

(define-command-syntax 'reset "" "top level" '())

(define (go-to-level n)
  (let ((levels (reverse (fluid $command-levels))))
    (if (and (integer? n)
	     (>= n 0)
	     (< n (length levels)))
	(abort-to-command-level (list-ref levels n))
	(write-line "invalid command level" (command-output)))))

(define-command-syntax 'level "<number>" "go to specific command level"
  '(expression))

(define level go-to-level)

(define-command-syntax 'condition ""
  "select an object that describes the current error condition"
  '())

(define (condition)
  (let ((c (command-level-condition (command-level))))
    (if c
	(set-focus-object! c)
	(write-line "no condition" (command-output)))))

; Commands that toggle various flags.

(define (toggle-command get set description)
  (lambda maybe-option
    (let ((b (if (null? maybe-option)
		 (not (get))
		 (case (car maybe-option)
		   ((off) #f)
		   ((on) #t)
		   ((?) (get))
		   (else (error "invalid setting (should be on or off or ?)"
				(car maybe-option))))))
	  (out (command-output)))
      (set b)
      (display (if b "Will " "Won't ") out)
      (display description out)
      (newline out))))

(define syntax-for-toggle '(&opt name))


; Batch mode

(define-command-syntax 'batch "[on | off]"
  "enable/disable batch mode (no prompt, errors exit)"
  syntax-for-toggle)

(define batch
  (toggle-command batch-mode?
		  set-batch-mode?!
		  "exit on errors"))

; Benchmark mode (i.e., inline primitives)

(define-command-syntax 'bench "[on | off]"
  "enable/disable benchmark mode (integrate primitives)"
  syntax-for-toggle)

(define bench
  (toggle-command (lambda ()
		    (package-integrate? (environment-for-commands)))
		  (lambda (b)
		    (set-package-integrate?! (environment-for-commands) b))
		  "compile some calls in line"))

; Break on warnings

(define-command-syntax 'break-on-warnings "[on | off]"
  "treat warnings as errors"
  syntax-for-toggle)

(define break-on-warnings
  (toggle-command break-on-warnings?
		  set-break-on-warnings?!
		  "enter breakpoint on warnings"))


(define-command-syntax 'form-preferred "[on | off]"
  "enable/disable form-preferred command processor mode"
  syntax-for-toggle)

(define form-preferred
  (toggle-command (user-context-accessor 'form-preferred? (lambda () #t))
		  (user-context-modifier 'form-preferred?)
		  "prefer forms to commands"))

(define-command-syntax 'levels "[on | off]"
  "disable/enable command levels"
  syntax-for-toggle)

(define levels
  (toggle-command (user-context-accessor 'push-command-levels (lambda () #t))
		  (user-context-modifier 'push-command-levels)
		  "push command level on errors"))


; Flush debug data base

(define-command-syntax 'flush "[<kind> ...]"
  "start forgetting debug information
Kind should be one of: names maps files source tabulate
 location-names file-packages"
  '(&rest name))

(define (flush . kinds)
  (cond ((null? kinds)
	 (write-line "Flushing location names and tabulated debug info"
		     (command-output))
	 (flush-location-names)
	 ((debug-flag-modifier 'table) (make-table)))
	(else
	 (for-each (lambda (kind)
		     (cond ((memq kind debug-flag-names)
			    ((debug-flag-modifier kind)
			     (if (eq? kind 'table) (make-table) #f)))
			   ((eq? kind 'location-names)
			    (flush-location-names))
			   ((eq? kind 'file-packages)
			    (forget-file-environments))
			   (else
			    (write-line "Unrecognized debug flag"
					(command-output)))))
		   kinds))))

; Control retention of debugging information

(define-command-syntax 'keep "[<kind> ...]"
  "start remembering debug information
Kind should be one of: names maps files source tabulate"
  '(&rest name))

(define (keep . kinds)
  (let ((port (command-output)))
    (if (null? kinds)
	(for-each (lambda (kind)
		    (if (not (eq? kind 'table))
			(begin
			  (display (if ((debug-flag-accessor kind))
				       "+ " "- ")
				   port)
			  (display kind port)
			  (newline port))))
		  debug-flag-names)
	(for-each (lambda (kind)
		    (if (and (memq kind debug-flag-names)
			     (not (eq? kind 'table)))
			((debug-flag-modifier kind) #t)
			(write-line "Unrecognized debug flag"
				    port)))
		  kinds))))


; Collect some garbage

(define (collect)
  (let ((port (command-output))
	(before (memory-status memory-status-option/available #f)))
    ((structure-ref primitives collect))
    (let ((after (memory-status memory-status-option/available #f)))
      (display "Before: " port)
      (write before port)
      (display " words free in semispace")
      (newline)
      (display "After:  " port)
      (write after port)
      (display " words free in semispace")
      (newline))))

(define memory-status-option/available (enum memory-status-option available))

(define-command-syntax 'collect "" "invoke the garbage collector" '())

; Undefined (this is sort of pointless now that NOTING-UNDEFINED-VARIABLES
; exists)
;
;(define (show-undefined-variables)
;  (let ((out (command-output))
;        (undef (undefined-variables (environment-for-commands))))
;    (if (not (null? undef))
;        (begin (display "Undefined: " out)
;               (write undef out)
;               (newline out)))))
;
;(define-command-syntax 'undefined "" "list undefined variables"
;  '() show-undefined-variables)



; Trace and untrace

(define traced-procedures
  (user-context-accessor 'traced (lambda () '())))
(define set-traced-procedures!
  (user-context-modifier 'traced))

(define (trace . names)
  (if (null? names)
      (let ((port (command-output)))
	(write (map car (traced-procedures)) port)
	(newline port))
      (for-each trace-1 names)))

(define-command-syntax 'trace "<name> ..."
  "trace calls to given procedure(s)"
  '(&rest name))

(define (untrace . names)
  (if (null? names)
      (for-each untrace-1 (map car (traced-procedures)))
      (for-each untrace-1 names)))

(define-command-syntax 'untrace "<name> ..." "stop tracing calls"
  '(&rest name))

; Trace internals

(define (trace-1 name)
  (let* ((env (environment-for-commands))
	 (proc (environment-ref env name))
	 (traced (make-traced proc name)))
    (set-traced-procedures!
		(cons (list name traced proc env)
		      (traced-procedures)))
    (environment-define! env name traced))) ;was environment-set!
	   
; Should be doing clookup's here -- avoid creating new locations

(define (untrace-1 name)
  (let ((probe (assq name (traced-procedures))))
    (if probe
	(let* ((traced (cadr probe))
	       (proc (caddr probe))
	       (env (cadddr probe)))
	  (if (eq? (environment-ref env name) traced)
	      (environment-set! env name proc)
	      (let ((out (command-output)))
		(display "Value of " out)
		(write name out)
		(display " changed since ,trace; not restoring it." out)
		(newline out)))
	  (set-traced-procedures!
		      (filter (lambda (x)
				(not (eq? (car x) name)))
			      (traced-procedures))))
	(write-line "?" (command-output)))))

(define (make-traced proc name)
  (lambda args
    (apply-traced proc name args)))

(define *trace-depth* 8)
(define (apply-traced proc name args)
  (let ((port (command-output)))
    (dynamic-wind
     (lambda ()
       (display "[" port))
     (lambda ()
       (let-fluids $write-length *trace-depth*
	 $write-depth *trace-depth*
	 (lambda ()
	   (display "Enter " port)
	   (write-carefully (error-form name args) port)
	   (newline port)))
       (call-with-values (lambda ()
			   (apply proc args))
	 (lambda results
	   (let-fluids $write-length *trace-depth*
	     $write-depth (- *trace-depth* 1)
	     (lambda ()
	       (display " Leave " port)
	       (write-carefully name port)
	       (for-each (lambda (result)
			   (display " " port)
			   (write-carefully (value->expression result) port))
			 results)))
	   (apply values results))))
     (lambda ()
       (display "]" port)
       (newline port)))))

; Timer stuff.

(define ptime (structure-ref primitives time))

(define (time command)
  (let* ((thunk (if (eq? (car command) 'run)
		    (evaluate `(lambda () ,(cadr command))
			      (environment-for-commands))
		    (lambda () (execute-command command))))
	 (start-time (ptime time-option/run-time #f)))
    (call-with-values thunk
      (lambda results
	(let* ((stop-time (ptime time-option/run-time #f))
	       (dt (- stop-time start-time))
	       (units-per-second (ptime time-option/ticks-per-second #f))
	       (delta (quotient (* dt 100) units-per-second))
	       (port (command-output)))
	  (display "Run time: " port)
	  (write-hundredths delta port)
	  (display " seconds" port)
	  (newline port)
	  (set-focus-values! results))))))

(define time-option/run-time (enum time-option run-time))
(define time-option/ticks-per-second (enum time-option ticks-per-second))

(define (write-hundredths n port)
  (write (quotient n 100) port)
  (write-char #\. port)
  (let ((r (remainder n 100)))
    (if (< r 10)
	(write-char #\0 port))
    (write r port)))

(define-command-syntax 'time "<command>" "measure execution time"
  '(command))

; Support for stuffing things from Emacs.

(define-command-syntax 'from-file #f #f	;"<filename>" "editor support"
  '(&opt filename))

(define-command-syntax 'end #f #f
  '())

(define (from-file . maybe-filename)
  (let* ((filename (if (null? maybe-filename) #f (car maybe-filename)))
	 (env (let ((probe (if filename
			       (get-file-environment filename)
			       #f))
		    (c (environment-for-commands)))
		(if (and probe (not (eq? probe c)))
		    (let ((port (command-output)))
		      (newline port)
		      (display filename port)
		      (display " => " port)
		      (write probe port)
		      (display " " port) ;dots follow
		      probe)
		    c)))
	 (in (command-input)))
    (let ((forms (let recur ()
		   (let ((command (read-command #f #t in)))
		     (if (eof-object? command)
			 '()
			 (case (car command)
			   ((end) '())
			   ((#f run) (cons (cadr command) (recur)))
			   (else
			    (error "unusual command in ,from-file ... ,end"
				   command))))))))
      (if (package? env)
	  (with-interaction-environment env
	    (lambda ()
	      (noting-undefined-variables env
		(lambda ()
		  (eval-from-file forms env (if (null? maybe-filename)
						#f
						(car maybe-filename)))))))
	  (for-each (lambda (form) (eval form env)) ;Foo
		    env)))))


; Filename -> environment map.

(define file-environments
  (user-context-accessor 'file-environments (lambda () '())))

(define set-file-environments!
  (user-context-modifier 'file-environments))

(define (forget-file-environments)
  (set-file-environments! '()))

(define (note-file-environment! filename env)
  (if (user-context)
      (let* ((translated ((structure-ref filenames translate) filename))
	     (envs (file-environments))
	     (probe (or (assoc filename envs) ;What to do?
			(assoc translated envs))))
	(if probe
	    (if (not (eq? env (weak-pointer-ref (cdr probe))))
		(let ((port (command-output)))
		  (newline port)
		  (display "Changing default package for file " port)
		  (display filename port)
		  (display " from" port)
		  (newline port)
		  (write (weak-pointer-ref (cdr probe)) port)
		  (display " to " port)
		  (write env port)
		  (newline port)
		  (set-cdr! probe (make-weak-pointer env))))
	    (set-file-environments!
	     (cons (cons filename (make-weak-pointer env))
		   envs))))))

(define (get-file-environment filename)
  (let ((probe (assoc filename (file-environments)))) ;translate ?
    (if probe
	(weak-pointer-ref (cdr probe))
	#f)))

(set-fluid! $note-file-package note-file-environment!)

(define-command-syntax 'forget "<filename>"
  "forget file/package association"
  '(filename))

(define (forget filename)
  (note-file-environment! filename #f))

; ,bound? <name>

(define-command-syntax 'bound? "<name>"
  "display binding of name, if any"
  '(name))

(define (bound? name)
  (let ((port (command-output))
	(probe (package-lookup (environment-for-commands) name)))
    (if probe
	(begin (display "Bound to " port)
	       (write probe)
	       (newline port))
	(write-line "Not bound" port))))

; ,expand <form>

(define-command-syntax 'expand "[<form>]"
  "macro-expand a form"
  '(&opt expression))

(define (expand . maybe-exp)
  (let ((exp (if (null? maybe-exp) (focus-object) (car maybe-exp)))
	(env (package->environment (environment-for-commands))))
    (set-focus-object!
     (schemify (classify exp env) env))))