1 (define-module (ice-9 gds-client)
2 #:use-module (oop goops)
3 #:use-module (oop goops describe)
4 #:use-module (ice-9 debugging breakpoints)
5 #:use-module (ice-9 debugging trace)
6 #:use-module (ice-9 debugging traps)
7 #:use-module (ice-9 debugging trc)
8 #:use-module (ice-9 debugging steps)
9 #:use-module (ice-9 pretty-print)
10 #:use-module (ice-9 regex)
11 #:use-module (ice-9 session)
12 #:use-module (ice-9 string-fun)
13 #:export (gds-debug-trap
18 (cond ((string>=? (version) "1.7")
19 (use-modules (ice-9 debugger utils)))
21 (define the-ice-9-debugger-module (resolve-module '(ice-9 debugger)))
22 (module-export! the-ice-9-debugger-module
24 write-frame-short/application
25 write-frame-short/expression
29 (use-modules (ice-9 debugger))
33 ;; Return an integer that somehow identifies the current thread.
34 (define (get-thread-id)
35 (let ((root (dynamic-root)))
36 (cond ((integer? root)
39 (object-address root))
41 (error "Unexpected dynamic root:" root)))))
43 ;; gds-debug-read is a high-priority read. The (debug-thread-id ID)
44 ;; form causes the frontend to dismiss any reads from threads whose id
45 ;; is not ID, until it receives the (thread-id ...) form with the same
46 ;; id as ID. Dismissing the reads of any other threads (by sending a
47 ;; form that is otherwise ignored) causes those threads to release the
48 ;; read mutex, which allows the (gds-read) here to proceed.
49 (define (gds-debug-read)
50 (write-form `(debug-thread-id ,(get-thread-id)))
53 (define (gds-debug-trap trap-context)
54 "Invoke the GDS debugger to explore the stack at the specified trap."
56 (start-stack 'debugger
57 (let* ((stack (tc:stack trap-context))
58 (flags1 (let ((trap-type (tc:type trap-context)))
62 (tc:return-value trap-context)))
65 (flags (if (tc:continuation trap-context)
66 (cons #:continuable flags1)
68 (fired-traps (tc:fired-traps trap-context))
69 (special-index (and (= (length fired-traps) 1)
70 (is-a? (car fired-traps) <exit-trap>)
71 (eq? (tc:type trap-context) #:return)
72 (- (tc:depth trap-context)
73 (slot-ref (car fired-traps) 'depth)))))
74 ;; Write current stack to the frontend.
75 (write-form (list 'stack
77 (stack->emacs-readable stack)
78 (append (flags->emacs-readable flags)
79 (slot-ref trap-context
80 'handler-return-syms))))
81 ;; Now wait for instruction.
82 (let loop ((protocol (gds-debug-read)))
86 ;; Request to tweak the handler return value.
87 (let ((tweaking (catch #t
89 (list (with-input-from-string
92 (lambda ignored #f))))
94 (slot-set! trap-context
96 (cons 'instead (car tweaking)))))
97 (loop (gds-debug-read)))
99 ;; Continue (by exiting the debugger).
102 ;; Evaluate expression in specified frame.
103 (eval-in-frame stack (cadr protocol) (caddr protocol))
104 (loop (gds-debug-read)))
106 ;; Return frame info.
107 (let ((frame (stack-ref stack (cadr protocol))))
108 (write-form (list 'info-result
109 (with-output-to-string
111 (write-frame-long frame))))))
112 (loop (gds-debug-read)))
114 ;; Return frame args.
115 (let ((frame (stack-ref stack (cadr protocol))))
116 (write-form (list 'info-result
117 (with-output-to-string
119 (write-frame-args-long frame))))))
120 (loop (gds-debug-read)))
122 ;; Show source of application procedure.
123 (let* ((frame (stack-ref stack (cadr protocol)))
124 (proc (frame-procedure frame))
125 (source (and proc (procedure-source proc))))
126 (write-form (list 'info-result
128 (sans-surrounding-whitespace
129 (with-output-to-string
131 (pretty-print source))))
133 "This procedure is coded in C"
134 "This frame has no procedure")))))
135 (loop (gds-debug-read)))
137 ;; Show the traps that fired here.
138 (write-form (list 'info-result
139 (with-output-to-string
142 (tc:fired-traps trap-context))))))
143 (loop (gds-debug-read)))
145 ;; Set temporary breakpoint on next trap.
146 (at-step gds-debug-trap
149 (if (memq #:return flags)
151 (- (stack-length stack)
154 ;; Set temporary breakpoint on exit from
156 (at-exit (- (stack-length stack) (cadr protocol))
159 ;; Set temporary breakpoint on next trap in same
161 (at-step gds-debug-trap
163 (frame-file-name (stack-ref stack
165 (if (memq #:return flags)
167 (- (stack-length stack)
170 (safely-handle-nondebug-protocol protocol)
171 (loop (gds-debug-read))))))))
173 (define (connect-to-gds)
177 (or (let ((s (socket PF_INET SOCK_STREAM 0))
180 (setsockopt s SOL_TCP TCP_NODELAY 1)
183 (connect s AF_INET (inet-aton "127.0.0.1") 8333)
186 (let ((s (socket PF_UNIX SOCK_STREAM 0)))
189 (connect s AF_UNIX "/tmp/.gds_socket")
192 (error "Couldn't connect to GDS by TCP or Unix domain socket")))
193 (write-form (list 'name (getpid) (format #f "PID ~A" (getpid)))))))
195 (if (not (defined? 'make-mutex))
197 (define (make-mutex) #f)
198 (define lock-mutex noop)
199 (define unlock-mutex noop)))
201 (define write-mutex (make-mutex))
203 (define (write-form form)
204 ;; Write any form FORM to GDS.
205 (lock-mutex write-mutex)
206 (write form gds-port)
208 (force-output gds-port)
209 (unlock-mutex write-mutex))
211 (define (stack->emacs-readable stack)
212 ;; Return Emacs-readable representation of STACK.
214 (frame->emacs-readable (stack-ref stack index)))
215 (iota (min (stack-length stack)
216 (cadr (memq 'depth (debug-options)))))))
218 (define (frame->emacs-readable frame)
219 ;; Return Emacs-readable representation of FRAME.
220 (if (frame-procedure? frame)
222 (with-output-to-string
224 (display (if (frame-real? frame) " " "t "))
225 (write-frame-short/application frame)))
226 (source->emacs-readable frame))
228 (with-output-to-string
230 (display (if (frame-real? frame) " " "t "))
231 (write-frame-short/expression frame)))
232 (source->emacs-readable frame))))
234 (define (source->emacs-readable frame)
235 ;; Return Emacs-readable representation of the filename, line and
236 ;; column source properties of SOURCE.
237 (or (frame->source-position frame) 'nil))
239 (define (flags->emacs-readable flags)
240 ;; Return Emacs-readable representation of trap FLAGS.
243 (let ((erf (if (and (keyword? flag)
244 (not (eq? prev #:return)))
245 (keyword->symbol flag)
246 (format #f "~S" flag))))
251 (define (eval-in-frame stack index expr)
257 (local-eval (with-input-from-string expr read)
258 (memoized-environment
259 (frame-source (stack-ref stack
262 (cons 'ERROR args)))))))
264 (set! (behaviour-ordering gds-debug-trap) 100)
266 ;;; Code below here adds support for interaction between the GDS
267 ;;; client program and the Emacs frontend even when not stopped in the
270 ;; A mutex to control attempts by multiple threads to read protocol
271 ;; back from the frontend.
272 (define gds-read-mutex (make-mutex))
274 ;; Read a protocol instruction from the frontend.
276 ;; Acquire the read mutex.
277 (lock-mutex gds-read-mutex)
278 ;; Tell the front end something that identifies us as a thread.
279 (write-form `(thread-id ,(get-thread-id)))
280 ;; Now read, then release the mutex and return what was read.
282 (lambda () (read gds-port))
283 (lambda ignored the-eof-object))))
284 (unlock-mutex gds-read-mutex)
287 (define (gds-accept-input exit-on-continue)
288 ;; If reading from the GDS connection returns EOF, we will throw to
292 (let loop ((protocol (gds-read)))
293 (if (or (eof-object? protocol)
294 (and exit-on-continue
295 (eq? (car protocol) 'continue)))
297 (safely-handle-nondebug-protocol protocol)
299 (lambda ignored #f)))
301 (define (safely-handle-nondebug-protocol protocol)
302 ;; This catch covers any internal errors in the GDS code or
308 (handle-nondebug-protocol protocol))
309 save-lazy-trap-context-and-rethrow))
312 `(eval-results (error . ,(format #f "~s" protocol))
313 ,(if last-lazy-trap-context 't 'nil)
315 Please report this to <neil@ossau.uklinux.net>, ideally including:
316 - a description of the scenario in which this error occurred
317 - which versions of Guile and guile-debugging you are using
318 - the error stack, which you can get by clicking on the link below,
319 and then cut and paste into your report.
321 ,(list (with-output-to-string
328 ;; The key that is used to signal a read error changes from 1.6 to
329 ;; 1.8; here we cover all eventualities by discovering the key
331 (define read-error-key
334 (with-input-from-string "(+ 3 4" read))
338 (define (handle-nondebug-protocol protocol)
342 (set! last-lazy-trap-context #f)
343 (apply (lambda (correlator module port-name line column code)
344 (with-input-from-string code
346 (set-port-filename! (current-input-port) port-name)
347 (set-port-line! (current-input-port) line)
348 (set-port-column! (current-input-port) column)
349 (let ((m (and module (resolve-module-from-root module))))
350 (catch read-error-key
352 (let loop ((exprs '()) (x (read)))
354 ;; Expressions to be evaluated have all
355 ;; been read. Now evaluate them.
356 (let loop2 ((exprs (reverse! exprs))
360 (write-form `(eval-results ,correlator
361 ,(if last-lazy-trap-context 't 'nil)
364 (append results (gds-eval (car exprs) m
365 (if (and (null? (cdr exprs))
369 ;; Another complete expression read; add
372 (for-each-breakpoint setup-after-read x)
373 (loop (cons x exprs) (read))))))
375 (write-form `(eval-results
377 ,(if last-lazy-trap-context 't 'nil)
378 ,(with-output-to-string
380 (display ";;; Reading expressions")
381 (display " to evaluate\n")
382 (apply display-error #f
383 (current-output-port) args)))
384 ("error-in-read"))))))))
385 (if (string? port-name)
388 (for-each-breakpoint setup-after-eval port-name)))))
392 (let ((matches (apropos-internal
393 (string-append "^" (regexp-quote (cadr protocol))))))
394 (cond ((null? matches)
395 (write-form '(completion-result nil)))
397 ;;(write matches (current-error-port))
398 ;;(newline (current-error-port))
400 (let loop ((match (symbol->string (car matches)))
401 (matches (cdr matches)))
402 ;;(write match (current-error-port))
403 ;;(newline (current-error-port))
404 ;;(write matches (current-error-port))
405 ;;(newline (current-error-port))
408 (if (string-prefix=? match
409 (symbol->string (car matches)))
410 (loop match (cdr matches))
411 (loop (substring match 0
412 (- (string-length match) 1))
414 (if (string=? match (cadr protocol))
415 (write-form `(completion-result
416 ,(map symbol->string matches)))
417 (write-form `(completion-result
420 ((debug-lazy-trap-context)
421 (if last-lazy-trap-context
422 (gds-debug-trap last-lazy-trap-context)
423 (error "There is no stack available to show")))
426 ;; Create or update a breakpoint object according to the
427 ;; definition. If the target code is already loaded, note that
428 ;; this may immediately install a trap.
429 (let* ((num (cadr protocol))
430 (def (caddr protocol))
431 (behaviour (case (list-ref def 0)
432 ((debug) gds-debug-trap)
433 ((trace) gds-trace-trap)
434 ((trace-subtree) gds-trace-subtree)
435 (else (error "Unsupported behaviour:"
437 (bp (hash-ref breakpoints num)))
438 (trc 'existing-bp bp)
440 (update-breakpoint bp (list-ref def 3))
443 (case (list-ref def 1)
445 (break-in (string->symbol (list-ref def 3))
447 #:behaviour behaviour))
449 (break-at (list-ref def 2)
450 (car (list-ref def 3))
451 (cdr (list-ref def 3))
452 #:behaviour behaviour))
454 (error "Unsupported breakpoint type:"
456 ;; Install an observer that will tell the frontend about
457 ;; future changes in this breakpoint's status.
458 (slot-set! bp 'observer
460 (write-form `(breakpoint
462 ,@(map trap-description
463 (slot-ref bp 'traps))))))
464 ;; Add this to the breakpoint hash, and return the
465 ;; breakpoint number and status to the front end.
466 (hash-set! breakpoints num bp)))
467 ;; Call the breakpoint's observer now.
468 ((slot-ref bp 'observer))))
471 (let* ((num (cadr protocol))
472 (bp (hash-ref breakpoints num)))
475 (hash-remove! breakpoints num)
476 (delete-breakpoint bp)))))
478 ;;; ((describe-breakpoints)
479 ;;; ;; Describe all breakpoints.
481 ;;; (with-output-to-string
483 ;;; (hash-fold (lambda (num bp acc)
485 ;;; "Breakpoint ~a ~a (~a):\n"
486 ;;; (class-name (class-of bp))
488 ;;; (slot-ref bp 'status))
489 ;;; (for-each (lambda (trap)
490 ;;; (write (trap-description trap))
492 ;;; (slot-ref bp 'traps)))
495 ;;; (write-form (list 'info-result desc))))
498 (error "Unexpected protocol:" protocol))))
500 (define breakpoints (make-hash-table 11))
502 (define (resolve-module-from-root name)
503 (save-module-excursion
505 (set-current-module the-root-module)
506 (resolve-module name))))
508 (define (gds-eval x m part)
509 ;; Consumer to accept possibly multiple values and present them for
510 ;; Emacs as a list of strings.
511 (define (value-consumer . values)
512 (if (unspecified? (car values))
515 (with-output-to-string (lambda () (write value))))
517 ;; Now do evaluation.
518 (let ((intro (if part
519 (format #f ";;; Evaluating expression ~A" part)
522 (let* ((do-eval (if m
525 (display " in module ")
526 (write (module-name m))
529 (call-with-values (lambda ()
530 (start-stack 'gds-eval-stack
535 (display " in current module ")
536 (write (module-name (current-module)))
539 (call-with-values (lambda ()
540 (start-stack 'gds-eval-stack
544 (with-output-to-string
550 save-lazy-trap-context-and-rethrow))
553 ((misc-error signal unbound-variable numerical-overflow)
554 (apply display-error #f
555 (current-output-port) args)
556 (set! value '("error-in-evaluation")))
558 (display "EXCEPTION: ")
564 '("unhandled-exception-in-evaluation"))))))))))
565 (list output value))))
567 (define last-lazy-trap-context #f)
569 (define (save-lazy-trap-context-and-rethrow key . args)
570 (set! last-lazy-trap-context
571 (throw->trap-context key args save-lazy-trap-context-and-rethrow))
572 (apply throw key args))
574 (define (run-utility)
575 (set-gds-breakpoints)
579 (named-module-use! '(guile-user) '(ice-9 session))
580 (gds-accept-input #f))
582 (define (set-gds-breakpoints)
584 (write-form '(get-breakpoints))
585 (gds-accept-input #t))
587 (define-method (trap-description (trap <trap>))
588 (let loop ((description (list (class-name (class-of trap))))
592 (loop (if (slot-ref trap 'installed)
593 (cons 'installed description)
597 (loop (if (slot-ref trap 'condition)
598 (cons 'conditional description)
602 (loop (let ((skip-count (slot-ref trap 'skip-count)))
603 (if (zero? skip-count)
605 (cons* skip-count 'skip-count description)))
608 (loop (if (slot-ref trap 'single-shot)
609 (cons 'single-shot description)
613 (reverse! description)))))
615 (define-method (trap-description (trap <procedure-trap>))
616 (let ((description (next-method)))
617 (set-cdr! description
618 (cons (procedure-name (slot-ref trap 'procedure))
622 (define-method (trap-description (trap <source-trap>))
623 (let ((description (next-method)))
624 (set-cdr! description
625 (cons (format #f "~s" (slot-ref trap 'expression))
629 (define-method (trap-description (trap <location-trap>))
630 (let ((description (next-method)))
631 (set-cdr! description
632 (cons* (slot-ref trap 'file-regexp)
633 (slot-ref trap 'line)
634 (slot-ref trap 'column)
638 (define (gds-trace-trap trap-context)
640 (gds-do-trace trap-context)
641 (at-exit (tc:depth trap-context) gds-do-trace))
643 (define (gds-do-trace trap-context)
644 (write-form (list 'trace
647 (trace/stack-real-depth trap-context)
648 (trace/info trap-context)))))
650 (define (gds-trace-subtree trap-context)
652 (gds-do-trace trap-context)
653 (let ((step-trap (make <step-trap> #:behaviour gds-do-trace)))
654 (install-trap step-trap)
655 (at-exit (tc:depth trap-context)
656 (lambda (trap-context)
657 (uninstall-trap step-trap)))))
659 ;;; (ice-9 gds-client) ends here.