lambda* in make-mutable-parameter
[bpt/guile.git] / module / ice-9 / gds-client.scm
1 (define-module (ice-9 gds-client)
2 #:use-module (oop goops)
3 #:use-module (oop goops describe)
4 #:use-module (ice-9 debugging trace)
5 #:use-module (ice-9 debugging traps)
6 #:use-module (ice-9 debugging trc)
7 #:use-module (ice-9 debugging steps)
8 #:use-module (ice-9 pretty-print)
9 #:use-module (ice-9 regex)
10 #:use-module (ice-9 session)
11 #:use-module (ice-9 string-fun)
12 #:export (gds-debug-trap
13 run-utility
14 gds-accept-input))
15
16 (use-modules (ice-9 debugger utils))
17
18 (use-modules (ice-9 debugger))
19
20 (define gds-port #f)
21
22 ;; Return an integer that somehow identifies the current thread.
23 (define (get-thread-id)
24 (let ((root (dynamic-root)))
25 (cond ((integer? root)
26 root)
27 ((pair? root)
28 (object-address root))
29 (else
30 (error "Unexpected dynamic root:" root)))))
31
32 ;; gds-debug-read is a high-priority read. The (debug-thread-id ID)
33 ;; form causes the frontend to dismiss any reads from threads whose id
34 ;; is not ID, until it receives the (thread-id ...) form with the same
35 ;; id as ID. Dismissing the reads of any other threads (by sending a
36 ;; form that is otherwise ignored) causes those threads to release the
37 ;; read mutex, which allows the (gds-read) here to proceed.
38 (define (gds-debug-read)
39 (write-form `(debug-thread-id ,(get-thread-id)))
40 (gds-read))
41
42 (define (gds-debug-trap trap-context)
43 "Invoke the GDS debugger to explore the stack at the specified trap."
44 (connect-to-gds)
45 (start-stack 'debugger
46 (let* ((stack (tc:stack trap-context))
47 (flags1 (let ((trap-type (tc:type trap-context)))
48 (case trap-type
49 ((#:return #:error)
50 (list trap-type
51 (tc:return-value trap-context)))
52 (else
53 (list trap-type)))))
54 (flags (if (tc:continuation trap-context)
55 (cons #:continuable flags1)
56 flags1))
57 (fired-traps (tc:fired-traps trap-context))
58 (special-index (and (= (length fired-traps) 1)
59 (is-a? (car fired-traps) <exit-trap>)
60 (eq? (tc:type trap-context) #:return)
61 (- (tc:depth trap-context)
62 (slot-ref (car fired-traps) 'depth)))))
63 ;; Write current stack to the frontend.
64 (write-form (list 'stack
65 (if (and special-index (> special-index 0))
66 special-index
67 0)
68 (stack->emacs-readable stack)
69 (append (flags->emacs-readable flags)
70 (slot-ref trap-context
71 'handler-return-syms))))
72 ;; Now wait for instruction.
73 (let loop ((protocol (gds-debug-read)))
74 ;; Act on it.
75 (case (car protocol)
76 ((tweak)
77 ;; Request to tweak the handler return value.
78 (let ((tweaking (catch #t
79 (lambda ()
80 (list (with-input-from-string
81 (cadr protocol)
82 read)))
83 (lambda ignored #f))))
84 (if tweaking
85 (slot-set! trap-context
86 'handler-return-value
87 (cons 'instead (car tweaking)))))
88 (loop (gds-debug-read)))
89 ((continue)
90 ;; Continue (by exiting the debugger).
91 *unspecified*)
92 ((evaluate)
93 ;; Evaluate expression in specified frame.
94 (eval-in-frame stack (cadr protocol) (caddr protocol))
95 (loop (gds-debug-read)))
96 ((info-frame)
97 ;; Return frame info.
98 (let ((frame (stack-ref stack (cadr protocol))))
99 (write-form (list 'info-result
100 (with-output-to-string
101 (lambda ()
102 (write-frame-long frame))))))
103 (loop (gds-debug-read)))
104 ((info-args)
105 ;; Return frame args.
106 (let ((frame (stack-ref stack (cadr protocol))))
107 (write-form (list 'info-result
108 (with-output-to-string
109 (lambda ()
110 (write-frame-args-long frame))))))
111 (loop (gds-debug-read)))
112 ((proc-source)
113 ;; Show source of application procedure.
114 (let* ((frame (stack-ref stack (cadr protocol)))
115 (proc (frame-procedure frame))
116 (source (and proc (procedure-source proc))))
117 (write-form (list 'info-result
118 (if source
119 (sans-surrounding-whitespace
120 (with-output-to-string
121 (lambda ()
122 (pretty-print source))))
123 (if proc
124 "This procedure is coded in C"
125 "This frame has no procedure")))))
126 (loop (gds-debug-read)))
127 ((traps-here)
128 ;; Show the traps that fired here.
129 (write-form (list 'info-result
130 (with-output-to-string
131 (lambda ()
132 (for-each describe
133 (tc:fired-traps trap-context))))))
134 (loop (gds-debug-read)))
135 ((step-into)
136 ;; Set temporary breakpoint on next trap.
137 (at-step gds-debug-trap
138 1
139 #f
140 (if (memq #:return flags)
141 #f
142 (- (stack-length stack)
143 (cadr protocol)))))
144 ((step-over)
145 ;; Set temporary breakpoint on exit from
146 ;; specified frame.
147 (at-exit (- (stack-length stack) (cadr protocol))
148 gds-debug-trap))
149 ((step-file)
150 ;; Set temporary breakpoint on next trap in same
151 ;; source file.
152 (at-step gds-debug-trap
153 1
154 (frame-file-name (stack-ref stack
155 (cadr protocol)))
156 (if (memq #:return flags)
157 #f
158 (- (stack-length stack)
159 (cadr protocol)))))
160 (else
161 (safely-handle-nondebug-protocol protocol)
162 (loop (gds-debug-read))))))))
163
164 (define (connect-to-gds . application-name)
165 (or gds-port
166 (let ((gds-unix-socket-name (getenv "GDS_UNIX_SOCKET_NAME")))
167 (set! gds-port
168 (or (and gds-unix-socket-name
169 (false-if-exception
170 (let ((s (socket PF_UNIX SOCK_STREAM 0)))
171 (connect s AF_UNIX gds-unix-socket-name)
172 s)))
173 (false-if-exception
174 (let ((s (socket PF_INET SOCK_STREAM 0))
175 (SOL_TCP 6)
176 (TCP_NODELAY 1))
177 (setsockopt s SOL_TCP TCP_NODELAY 1)
178 (connect s AF_INET (inet-aton "127.0.0.1") 8333)
179 s))
180 (error "Couldn't connect to GDS by TCP or Unix domain socket")))
181 (write-form (list 'name (getpid) (apply client-name application-name))))))
182
183 (define (client-name . application-name)
184 (let loop ((args (append application-name (program-arguments))))
185 (if (null? args)
186 (format #f "PID ~A" (getpid))
187 (let ((arg (car args)))
188 (cond ((string-match "^(.*[/\\])?guile(\\..*)?$" arg)
189 (loop (cdr args)))
190 ((string-match "^-" arg)
191 (loop (cdr args)))
192 (else
193 (format #f "~A (PID ~A)" arg (getpid))))))))
194
195 ;;(if (not (defined? 'make-mutex))
196 ;; (begin
197 ;; (define (make-mutex) #f)
198 ;; (define lock-mutex noop)
199 ;; (define unlock-mutex noop)))
200
201 (define write-mutex (make-mutex))
202
203 (define (write-form form)
204 ;; Write any form FORM to GDS.
205 (lock-mutex write-mutex)
206 (write form gds-port)
207 (newline gds-port)
208 (force-output gds-port)
209 (unlock-mutex write-mutex))
210
211 (define (stack->emacs-readable stack)
212 ;; Return Emacs-readable representation of STACK.
213 (map (lambda (index)
214 (frame->emacs-readable (stack-ref stack index)))
215 (iota (min (stack-length stack)
216 (cadr (memq 'depth (debug-options)))))))
217
218 (define (frame->emacs-readable frame)
219 ;; Return Emacs-readable representation of FRAME.
220 (if (frame-procedure? frame)
221 (list 'application
222 (with-output-to-string
223 (lambda ()
224 (display (if (frame-real? frame) " " "t "))
225 (write-frame-short/application frame)))
226 (source->emacs-readable frame))
227 (list 'evaluation
228 (with-output-to-string
229 (lambda ()
230 (display (if (frame-real? frame) " " "t "))
231 (write-frame-short/expression frame)))
232 (source->emacs-readable frame))))
233
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))
238
239 (define (flags->emacs-readable flags)
240 ;; Return Emacs-readable representation of trap FLAGS.
241 (let ((prev #f))
242 (map (lambda (flag)
243 (let ((erf (if (and (keyword? flag)
244 (not (eq? prev #:return)))
245 (keyword->symbol flag)
246 (format #f "~S" flag))))
247 (set! prev flag)
248 erf))
249 flags)))
250
251 ;; FIXME: the new evaluator breaks this, by removing local-eval. Need to
252 ;; figure out our story in this regard.
253 (define (eval-in-frame stack index expr)
254 (write-form
255 (list 'eval-result
256 (format #f "~S"
257 (catch #t
258 (lambda ()
259 (local-eval (with-input-from-string expr read)
260 (memoized-environment
261 (frame-source (stack-ref stack
262 index)))))
263 (lambda args
264 (cons 'ERROR args)))))))
265
266 (set! (behaviour-ordering gds-debug-trap) 100)
267
268 ;;; Code below here adds support for interaction between the GDS
269 ;;; client program and the Emacs frontend even when not stopped in the
270 ;;; debugger.
271
272 ;; A mutex to control attempts by multiple threads to read protocol
273 ;; back from the frontend.
274 (define gds-read-mutex (make-mutex))
275
276 ;; Read a protocol instruction from the frontend.
277 (define (gds-read)
278 ;; Acquire the read mutex.
279 (lock-mutex gds-read-mutex)
280 ;; Tell the front end something that identifies us as a thread.
281 (write-form `(thread-id ,(get-thread-id)))
282 ;; Now read, then release the mutex and return what was read.
283 (let ((x (catch #t
284 (lambda () (read gds-port))
285 (lambda ignored the-eof-object))))
286 (unlock-mutex gds-read-mutex)
287 x))
288
289 (define (gds-accept-input exit-on-continue)
290 ;; If reading from the GDS connection returns EOF, we will throw to
291 ;; this catch.
292 (catch 'server-eof
293 (lambda ()
294 (let loop ((protocol (gds-read)))
295 (if (or (eof-object? protocol)
296 (and exit-on-continue
297 (eq? (car protocol) 'continue)))
298 (throw 'server-eof))
299 (safely-handle-nondebug-protocol protocol)
300 (loop (gds-read))))
301 (lambda ignored #f)))
302
303 (define (safely-handle-nondebug-protocol protocol)
304 ;; This catch covers any internal errors in the GDS code or
305 ;; protocol.
306 (catch #t
307 (lambda ()
308 (lazy-catch #t
309 (lambda ()
310 (handle-nondebug-protocol protocol))
311 save-lazy-trap-context-and-rethrow))
312 (lambda (key . args)
313 (write-form
314 `(eval-results (error . ,(format #f "~s" protocol))
315 ,(if last-lazy-trap-context 't 'nil)
316 "GDS Internal Error
317 Please report this to <neil@ossau.uklinux.net>, ideally including:
318 - a description of the scenario in which this error occurred
319 - which versions of Guile and guile-debugging you are using
320 - the error stack, which you can get by clicking on the link below,
321 and then cut and paste into your report.
322 Thanks!\n\n"
323 ,(list (with-output-to-string
324 (lambda ()
325 (write key)
326 (display ": ")
327 (write args)
328 (newline)))))))))
329
330 ;; The key that is used to signal a read error changes from 1.6 to
331 ;; 1.8; here we cover all eventualities by discovering the key
332 ;; dynamically.
333 (define read-error-key
334 (catch #t
335 (lambda ()
336 (with-input-from-string "(+ 3 4" read))
337 (lambda (key . args)
338 key)))
339
340 (define (handle-nondebug-protocol protocol)
341 (case (car protocol)
342
343 ((eval)
344 (set! last-lazy-trap-context #f)
345 (apply (lambda (correlator module port-name line column code flags)
346 (with-input-from-string code
347 (lambda ()
348 (set-port-filename! (current-input-port) port-name)
349 (set-port-line! (current-input-port) line)
350 (set-port-column! (current-input-port) column)
351 (let ((m (and module (resolve-module-from-root module))))
352 (catch read-error-key
353 (lambda ()
354 (let loop ((exprs '()) (x (read)))
355 (if (eof-object? x)
356 ;; Expressions to be evaluated have all
357 ;; been read. Now evaluate them.
358 (let loop2 ((exprs (reverse! exprs))
359 (results '())
360 (n 1))
361 (if (null? exprs)
362 (write-form `(eval-results ,correlator
363 ,(if last-lazy-trap-context 't 'nil)
364 ,@results))
365 (loop2 (cdr exprs)
366 (append results (gds-eval (car exprs) m
367 (if (and (null? (cdr exprs))
368 (= n 1))
369 #f n)))
370 (+ n 1))))
371 ;; Another complete expression read; add
372 ;; it to the list.
373 (begin
374 (if (and (pair? x)
375 (memq 'debug flags))
376 (install-trap (make <source-trap>
377 #:expression x
378 #:behaviour gds-debug-trap)))
379 (loop (cons x exprs) (read))))))
380 (lambda (key . args)
381 (write-form `(eval-results
382 ,correlator
383 ,(if last-lazy-trap-context 't 'nil)
384 ,(with-output-to-string
385 (lambda ()
386 (display ";;; Reading expressions")
387 (display " to evaluate\n")
388 (apply display-error #f
389 (current-output-port) args)))
390 ("error-in-read")))))))))
391 (cdr protocol)))
392
393 ((complete)
394 (let ((matches (apropos-internal
395 (string-append "^" (regexp-quote (cadr protocol))))))
396 (cond ((null? matches)
397 (write-form '(completion-result nil)))
398 (else
399 ;;(write matches (current-error-port))
400 ;;(newline (current-error-port))
401 (let ((match
402 (let loop ((match (symbol->string (car matches)))
403 (matches (cdr matches)))
404 ;;(write match (current-error-port))
405 ;;(newline (current-error-port))
406 ;;(write matches (current-error-port))
407 ;;(newline (current-error-port))
408 (if (null? matches)
409 match
410 (if (string-prefix=? match
411 (symbol->string (car matches)))
412 (loop match (cdr matches))
413 (loop (substring match 0
414 (- (string-length match) 1))
415 matches))))))
416 (if (string=? match (cadr protocol))
417 (write-form `(completion-result
418 ,(map symbol->string matches)))
419 (write-form `(completion-result
420 ,match))))))))
421
422 ((debug-lazy-trap-context)
423 (if last-lazy-trap-context
424 (gds-debug-trap last-lazy-trap-context)
425 (error "There is no stack available to show")))
426
427 (else
428 (error "Unexpected protocol:" protocol))))
429
430 (define (resolve-module-from-root name)
431 (save-module-excursion
432 (lambda ()
433 (set-current-module the-root-module)
434 (resolve-module name))))
435
436 (define (gds-eval x m part)
437 ;; Consumer to accept possibly multiple values and present them for
438 ;; Emacs as a list of strings.
439 (define (value-consumer . values)
440 (if (unspecified? (car values))
441 '()
442 (map (lambda (value)
443 (with-output-to-string (lambda () (write value))))
444 values)))
445 ;; Now do evaluation.
446 (let ((intro (if part
447 (format #f ";;; Evaluating expression ~A" part)
448 ";;; Evaluating"))
449 (value #f))
450 (let* ((do-eval (if m
451 (lambda ()
452 (display intro)
453 (display " in module ")
454 (write (module-name m))
455 (newline)
456 (set! value
457 (call-with-values (lambda ()
458 (start-stack 'gds-eval-stack
459 (eval x m)))
460 value-consumer)))
461 (lambda ()
462 (display intro)
463 (display " in current module ")
464 (write (module-name (current-module)))
465 (newline)
466 (set! value
467 (call-with-values (lambda ()
468 (start-stack 'gds-eval-stack
469 (primitive-eval x)))
470 value-consumer)))))
471 (output
472 (with-output-to-string
473 (lambda ()
474 (catch #t
475 (lambda ()
476 (lazy-catch #t
477 do-eval
478 save-lazy-trap-context-and-rethrow))
479 (lambda (key . args)
480 (case key
481 ((misc-error signal unbound-variable numerical-overflow)
482 (apply display-error #f
483 (current-output-port) args)
484 (set! value '("error-in-evaluation")))
485 (else
486 (display "EXCEPTION: ")
487 (display key)
488 (display " ")
489 (write args)
490 (newline)
491 (set! value
492 '("unhandled-exception-in-evaluation"))))))))))
493 (list output value))))
494
495 (define last-lazy-trap-context #f)
496
497 (define (save-lazy-trap-context-and-rethrow key . args)
498 (set! last-lazy-trap-context
499 (throw->trap-context key args save-lazy-trap-context-and-rethrow))
500 (apply throw key args))
501
502 (define (run-utility)
503 (connect-to-gds)
504 (write (getpid))
505 (newline)
506 (force-output)
507 (named-module-use! '(guile-user) '(ice-9 session))
508 (gds-accept-input #f))
509
510 (define-method (trap-description (trap <trap>))
511 (let loop ((description (list (class-name (class-of trap))))
512 (next 'installed?))
513 (case next
514 ((installed?)
515 (loop (if (slot-ref trap 'installed)
516 (cons 'installed description)
517 description)
518 'conditional?))
519 ((conditional?)
520 (loop (if (slot-ref trap 'condition)
521 (cons 'conditional description)
522 description)
523 'skip-count))
524 ((skip-count)
525 (loop (let ((skip-count (slot-ref trap 'skip-count)))
526 (if (zero? skip-count)
527 description
528 (cons* skip-count 'skip-count description)))
529 'single-shot?))
530 ((single-shot?)
531 (loop (if (slot-ref trap 'single-shot)
532 (cons 'single-shot description)
533 description)
534 'done))
535 (else
536 (reverse! description)))))
537
538 (define-method (trap-description (trap <procedure-trap>))
539 (let ((description (next-method)))
540 (set-cdr! description
541 (cons (procedure-name (slot-ref trap 'procedure))
542 (cdr description)))
543 description))
544
545 (define-method (trap-description (trap <source-trap>))
546 (let ((description (next-method)))
547 (set-cdr! description
548 (cons (format #f "~s" (slot-ref trap 'expression))
549 (cdr description)))
550 description))
551
552 (define-method (trap-description (trap <location-trap>))
553 (let ((description (next-method)))
554 (set-cdr! description
555 (cons* (slot-ref trap 'file-regexp)
556 (slot-ref trap 'line)
557 (slot-ref trap 'column)
558 (cdr description)))
559 description))
560
561 (define (gds-trace-trap trap-context)
562 (connect-to-gds)
563 (gds-do-trace trap-context)
564 (at-exit (tc:depth trap-context) gds-do-trace))
565
566 (define (gds-do-trace trap-context)
567 (write-form (list 'trace
568 (format #f
569 "~3@a: ~a"
570 (trace/stack-real-depth trap-context)
571 (trace/info trap-context)))))
572
573 (define (gds-trace-subtree trap-context)
574 (connect-to-gds)
575 (gds-do-trace trap-context)
576 (let ((step-trap (make <step-trap> #:behaviour gds-do-trace)))
577 (install-trap step-trap)
578 (at-exit (tc:depth trap-context)
579 (lambda (trap-context)
580 (uninstall-trap step-trap)))))
581
582 ;;; (ice-9 gds-client) ends here.