-
Notifications
You must be signed in to change notification settings - Fork 10
/
geben.el
3910 lines (3451 loc) · 158 KB
/
geben.el
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; geben.el --- DBGp protocol frontend, a script debugger
;; Copyright (C) 2005-2010 reedom <fujinaka.tohru@gmail.com>
;; Copyright (C) 2016 Matthew Carter
;; Filename: geben.el
;; Author: Matthew Carter <m@ahungry.com>
;; Author: Johannes Goslar <jogo@kronberger-spiele.de>
;; Code derived from Original Author: reedom <fujinaka.tohru@gmail.com>
;; Maintainer: Matthew Carter <m@ahungry.com>
;; URL: https://github.com/ahungry/geben
;; Version: 1.1.1
;; Keywords: c, comm, tools
;; Compatibility: Emacs 24+
;; Package-Requires: ((emacs "24.3") (cl-lib "0.5"))
;; This file is not part of GNU Emacs
;;; License:
;; 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 3 of the License, 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 program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; GEBEN is a software package that interfaces Emacs to DBGp protocol
;; with which you can debug running scripts interactively. At this
;; present time, DBGp protocols are supported in several script
;; languages with help of custom extensions.
;;; Usage
;; 1. Insert autoload hooks into your .Emacs file.
;; -> (autoload 'geben "geben" "DBGp protocol frontend, a script debugger" t)
;; 2. Start GEBEN. By default, M-x geben will start it.
;; GEBEN starts to listening to DBGp protocol session connection.
;; 3. Run debuggee script.
;; When the connection is established, GEBEN loads the entry script
;; file in geben-mode.
;; 4. Start debugging. To see geben-mode key bindings, type ?.
;;; Requirements:
;; [Server side]
;; - PHP with Xdebug 2.0.3
;; http://xdebug.org/
;; - Perl, Python, Ruby, Tcl with Komodo Debugger Extension
;; http://aspn.activestate.com/ASPN/Downloads/Komodo/RemoteDebugging
;; [Client side]
;; - Emacs 24 and later
;;; Code:
(eval-when-compile
(when (or (not (boundp 'emacs-version))
(string< emacs-version "24"))
(error (concat "geben.el: This package requires Emacs 24 or later.")))
(require 'cl))
(eval-and-compile
(require 'cl-lib)
(require 'xml)
(require 'tree-widget)
(require 'dbgp))
(defvar geben-version "1.1.1")
;;--------------------------------------------------------------
;; customization
;;--------------------------------------------------------------
;; customize group
(defgroup geben nil
"A PHP Debugging environment."
:group 'debug)
(defgroup geben-highlighting-faces nil
"Faces for GEBEN."
:group 'geben
:group 'font-lock-highlighting-faces)
;; display window behavior
(defvar geben-dynamic-property-buffer-p nil)
(defcustom geben-display-window-function 'pop-to-buffer
"*Function to display a debuggee script's content.
Typically `pop-to-buffer' or `switch-to-buffer'."
:group 'geben
:type 'function)
(defcustom geben-show-redirect-buffers t
"Shall stdout/stderr buffers be shown automatically."
:group 'geben
:type 'boolean)
(defsubst geben-dbgp-dynamic-property-bufferp (buf)
(with-current-buffer buf
(symbol-value 'geben-dynamic-property-buffer-p)))
(defun geben-dbgp-display-window (buf)
"Display a buffer BUF anywhere in a window, depends on the circumstance."
(cond
(geben-full-frame-first-buffer
(setq geben-full-frame-first-buffer nil)
(switch-to-buffer buf))
((get-buffer-window buf)
(select-window (get-buffer-window buf))
(switch-to-buffer buf))
((or (eq 1 (count-windows))
(not (geben-dbgp-dynamic-property-buffer-visiblep)))
(funcall geben-display-window-function buf))
(t
(let ((candidates (make-vector 3 nil))
(dynamic-p (geben-dbgp-dynamic-property-bufferp buf)))
(cl-block finder
(walk-windows (lambda (window)
(if (geben-dbgp-dynamic-property-bufferp (window-buffer window))
(if dynamic-p
(unless (aref candidates 1)
(aset candidates 1 window)))
(if (eq (selected-window) window)
(aset candidates 2 window)
(aset candidates 0 window)
(cl-return-from finder))))))
(select-window (or (aref candidates 0)
(aref candidates 1)
(aref candidates 2)
(selected-window)))
(switch-to-buffer buf))))
buf)
;; (when (buffer-live-p buf)
;; (or (eq buf (get-buffer geben-context-buffer-name))
;; (eq buf (get-buffer (geben-dbgp-redirect-buffer-name session :stdout)))
;; (eq buf (get-buffer (geben-dbgp-redirect-buffer-name session :stderr))))))
(defun geben-dbgp-dynamic-property-buffer-visiblep ()
"Check whether any window displays any property buffer."
(cl-block walk-loop
(walk-windows (lambda (window)
(if (geben-dbgp-dynamic-property-bufferp (window-buffer window))
(cl-return-from walk-loop t))))))
;;==============================================================
;; utilities
;;==============================================================
(defun geben-rec (x acc)
"Helper function for recursively flattening a list, where X is the list and ACC is the accumulator."
(cond ((null x) acc)
((atom x) (cons x acc))
(t (geben-rec (car x) (geben-rec (cdr x) acc)))))
(defsubst geben-flatten (x)
"Make cons X to a flat list."
(geben-rec x nil))
(defsubst geben-what-line (&optional pos)
"Get the number of the line in which POS is located.
If POS is omitted, then the current position is used."
(save-restriction
(widen)
(save-excursion
(if pos (goto-char pos))
(beginning-of-line)
(1+ (count-lines 1 (point))))))
(defmacro geben-plist-push (plist prop value)
`(let* ((plist ,plist)
(l (plist-get plist ,prop)))
(cond
((consp l)
(plist-put plist ,prop
(cons ,value (plist-get plist ,prop))))
((null l)
(plist-put plist ,prop (list ,value)))
(t
(error "geben-plist-push: cannot add value; type of prop `%s' is not `list' but `%s'."
,prop (type-of ,value))))))
(defmacro geben-plist-append (plist prop value)
`(let* ((plist ,plist)
(l (plist-get plist ,prop)))
(cond
((consp l)
(nconc l (list ,value)))
((null l)
(plist-put plist ,prop (list ,value)))
(t
(error "geben-plist-add: cannot add value; type of prop `%s' is not `list' but `%s'."
,prop (type-of ,value))))))
(defmacro geben-lexical-bind (bindings &rest body)
(declare (indent 1)
(debug (sexp &rest form)))
(macroexpand-all
(nconc
(list 'lexical-let (mapcar (lambda (arg)
(list arg arg))
bindings))
body)))
(defun geben-remove-directory-tree (basedir)
(ignore-errors
(mapc (lambda (path)
(cond
((or (file-symlink-p path)
(file-regular-p path))
(delete-file path))
((file-directory-p path)
(let ((name (file-name-nondirectory path)))
(or (equal "." name)
(equal ".." name)
(geben-remove-directory-tree path))))))
(directory-files basedir t nil t))
(delete-directory basedir)))
(defun geben-remote-p (ip)
"Test whether IP refers a remote system."
(not (or (equal ip "127.0.0.1")
(and (fboundp 'network-interface-list)
(member ip (mapcar (lambda (addr)
(format-network-address (cdr addr) t))
(network-interface-list)))))))
;;--------------------------------------------------------------
;; cross emacs overlay definitions
;;--------------------------------------------------------------
(eval-and-compile
(and (featurep 'xemacs)
(require 'overlay))
(or (fboundp 'overlay-livep)
(defalias 'overlay-livep 'overlay-buffer)))
(defun geben-overlay-make-line (lineno &optional buf)
"Create a whole line overlay on LINENO line.
Optionally, in buffer BUF."
(with-current-buffer (or buf (current-buffer))
(save-excursion
(widen)
(goto-line lineno)
(beginning-of-line)
(make-overlay (point)
(save-excursion
(forward-line) (point))
nil t nil))))
;;==============================================================
;; DBGp related utilities
;;==============================================================
(cl-defmacro geben-dbgp-sequence (cmd &rest callback)
(declare (indent 1)
(debug (form &rest form)))
(list 'progn
(list 'geben-plist-append cmd
:callback (car callback))))
(cl-defmacro geben-dbgp-sequence-bind (bindings cmd callback)
(declare (indent 1)
(debug (sexp form lambda-expr)))
(macroexpand-all
(list 'progn
(list 'geben-plist-append cmd
:callback (if bindings
(list 'geben-lexical-bind bindings callback)
callback)))))
(defun geben-dbgp-decode-string (string data-encoding coding-system)
"Decode encoded STRING.
Use the DATA-ENCODING appropriate to the CODING-SYSTEM."
(when string
(let ((s string))
(when (consp s)
(setq s (car s)))
(when (stringp s)
(setq s (cond
((equal "base64" data-encoding)
(base64-decode-string s))
(t s)))
(if coding-system
(decode-coding-string s coding-system)
s)))))
(defcustom geben-temporary-file-directory (expand-file-name "geben" user-emacs-directory)
"*Base directory path where GEBEN creates temporary files and directories."
:group 'geben
:type 'directory)
(defvar geben-storages nil)
(defvar geben-storage-loaded nil)
(defun geben-storage-load ()
(let ((storage-path (expand-file-name ".storage"
geben-temporary-file-directory)))
(when (file-exists-p storage-path)
(ignore-errors
(with-temp-buffer
(insert-file-contents storage-path)
(setq geben-storages (read (buffer-string))))))))
(defun geben-storage-save ()
(let ((storage-path (expand-file-name ".storage"
geben-temporary-file-directory)))
(with-temp-buffer
(pp geben-storages (current-buffer))
(with-temp-message ""
(write-region (point-min) (point-max) storage-path)))))
;;==============================================================
;; session
;;==============================================================
;;--------------------------------------------------------------
;; constants
;;--------------------------------------------------------------
(defconst geben-process-buffer-name "*GEBEN<%s> process*"
"Name for DBGp client process console buffer.")
(defconst geben-backtrace-buffer-name "*GEBEN<%s> backtrace*"
"Name for backtrace buffer.")
(defconst geben-breakpoint-list-buffer-name "*GEBEN<%s> breakpoint list*"
"Name for breakpoint list buffer.")
(defconst geben-context-buffer-name "*GEBEN<%s> context*"
"Name for context buffer.")
(defvar geben-sessions nil)
(defvar geben-current-session nil)
;; geben session start/finish hooks
(defcustom geben-session-enter-hook nil
"*Hook running at when the geben debugging session is starting.
Each function is invoked with one argument, SESSION"
:group 'geben
:type 'hook)
(defcustom geben-session-exit-hook nil
"*Hook running at when the geben debugging session is finished."
:group 'geben
:type 'hook)
(defcustom geben-pause-at-entry-line t
"*Specify whether debuggee script should be paused at the entry line.
If the value is t, GEBEN will automatically pause the starting program
at the entry line of the script."
:group 'geben
:type 'boolean)
(cl-defstruct (geben-session
(:constructor nil)
(:constructor geben-session-make))
"Represent a DBGp protocol connection session."
storage
process
(tid 30000)
(state :created)
initmsg
xdebug-p
language
feature
redirect
breakpoint
cmd
sending-p
source
stack
context
(cursor (list :overlay nil :position nil))
tempdir
)
(defmacro geben-with-current-session (binding &rest body)
(declare (indent 1)
(debug (symbolp &rest form)))
(macroexpand-all
`(let ((,binding geben-current-session))
(when ,binding
,@body))))
;; initialize
(defsubst geben-session-init (session init-msg)
"Initialize a SESSION of a process PROC, with the INIT-MSG."
(geben-session-tempdir-setup session)
(setf (geben-session-initmsg session) init-msg)
(setf (geben-session-xdebug-p session)
(equal "Xdebug" (car (xml-node-children
(car (xml-get-children init-msg 'engine))))))
(setf (geben-session-language session)
(let ((lang (xml-get-attribute-or-nil init-msg 'language)))
(and lang
(intern (concat ":" (downcase lang))))))
(setf (geben-session-storage session) (or (geben-session-storage-find session)
(geben-session-storage-create session)))
(run-hook-with-args 'geben-session-enter-hook session))
(defun geben-session-storage-create (session)
"Create the necessary storage for the SESSION."
(let* ((initmsg (geben-session-initmsg session))
(process (geben-session-process session))
(listener (dbgp-plist-get process :listener))
(storage (if (dbgp-proxy-p process)
(list :proxy t
:addr (xml-get-attribute initmsg 'hostname)
:idekey (xml-get-attribute initmsg 'idekey))
(list :proxy nil
:port (cl-second (process-contact listener))))))
(nconc storage (list :language (geben-session-language session)
:fileuri (xml-get-attribute initmsg 'fileuri)))
(add-to-list 'geben-storages storage)
storage))
(defun geben-session-storage-find (session)
(unless geben-storage-loaded
(geben-storage-load)
(setq geben-storage-loaded t))
(let* ((initmsg (geben-session-initmsg session))
(addr (xml-get-attribute initmsg 'hostname))
(fileuri (xml-get-attribute initmsg 'fileuri))
(idekey (xml-get-attribute initmsg 'idekey))
(process (geben-session-process session))
(listener (dbgp-plist-get process :listener))
(proxy-p (dbgp-proxy-p listener))
(port (cl-second (process-contact listener))))
(cl-find-if (lambda (storage)
(and (eq (not proxy-p)
(not (plist-get storage :proxy)))
(eq (geben-session-language session)
(plist-get storage :language))
(equal fileuri (plist-get storage :fileuri))
(if proxy-p
(and (equal addr (plist-get storage :addr))
(equal idekey (plist-get storage :idekey)))
(eq port (plist-get storage :port)))))
geben-storages)))
(defsubst geben-session-release (session)
"Initialize a SESSION of a process PROC."
(setf (geben-session-process session) nil)
(setf (geben-session-cursor session) nil)
(geben-session-tempdir-remove session)
(geben-storage-save)
(run-hook-with-args 'geben-session-exit-hook session))
(defsubst geben-session-active-p (session)
"Evaluate the active state of SESSION. If active, will return t, otherwise nil."
(let ((proc (geben-session-process session)))
(and (processp proc)
(eq 'open (process-status proc)))))
;; tid
(defsubst geben-session-next-tid (session)
"Get transaction id for next command in SESSION."
(prog1
(geben-session-tid session)
(cl-incf (geben-session-tid session))))
;; buffer
(defsubst geben-session-buffer-name (session format-string)
"Return the buffer name for SESSION, formatted according to FORMAT-STRING."
(let* ((proc (geben-session-process session))
(idekey (plist-get (dbgp-proxy-get proc) :idekey)))
(format format-string
(concat (if idekey
(format "%s:" idekey)
"")
(format "%s:%s"
(dbgp-ip-get proc)
(dbgp-port-get (dbgp-listener-get proc)))))))
(defsubst geben-session-buffer (session format-string)
(get-buffer-create (geben-session-buffer-name session format-string)))
(defsubst geben-session-buffer-get (session format-string)
(get-buffer (geben-session-buffer-name session format-string)))
(defsubst geben-session-buffer-live-p (session format-string)
(buffer-live-p (get-buffer (geben-session-buffer-name session format-string))))
(defsubst geben-session-buffer-visible-p (session format-string)
(let ((buf (get-buffer (geben-session-buffer-name session format-string))))
(and buf
(buffer-live-p buf)
(get-buffer-window buf))))
(defun geben-kill-buffers (&rest args)
"Kills all buffers whose name start with *GEBEN.
ARGS is so it can be used for `geben-session-exit-hook'.
TODO: Standard buffer names so we don't have to do a string match."
(mapc (lambda(buffer)
(if (string-prefix-p "*GEBEN" (buffer-name buffer))
(kill-buffer buffer)))
(buffer-list)))
;; temporary directory
(defun geben-session-tempdir-setup (session)
"Setup temporary directory for SESSION."
(let* ((proc (geben-session-process session))
(gebendir (file-truename geben-temporary-file-directory))
(leafdir (format "%d" (cl-second (process-contact proc))))
(tempdir (expand-file-name leafdir gebendir)))
(unless (file-directory-p gebendir)
(make-directory gebendir t)
(set-file-modes gebendir #o1777))
(setf (geben-session-tempdir session) tempdir)))
(defun geben-session-tempdir-remove (session)
"Remove temporary directory for SESSION."
(let ((tempdir (geben-session-tempdir session)))
(when (file-directory-p tempdir)
(geben-remove-directory-tree tempdir))))
;; misc
(defsubst geben-session-ip-get (session)
"Get ip address of the host server in SESSION."
(let* ((proc (geben-session-process session))
(listener (dbgp-listener-get proc)))
(format-network-address (dbgp-ip-get proc) t)))
(defun geben-session-remote-p (session)
"Get ip address of the host server IN SESSION."
(geben-remote-p (geben-session-ip-get session)))
;;==============================================================
;; cmd hash
;;==============================================================
(defmacro geben-cmd-param-for (key)
`(plist-get '(:depth "-d"
:context-id "-c"
:max-data-size "-m"
:type "-t"
:page "-p"
:key "k"
:address "-a"
:name "-n"
:fileuri "-f"
:lineno "-n"
:class "-a"
:function "-m"
:state "-s"
:exception "-x"
:hit-value "-h"
:hit-condition "-o"
:run-once "-r"
:expression "--")
,key))
(defsubst geben-cmd-param-get (cmd flag)
"For CMD, get FLAG's parameter used.
For a DBGp command \`stack_get -i 1 -d 2\',
`(geben-cmd-param-get cmd \"-d\")\' gets \"2\"."
(cdr-safe (assoc flag (plist-get cmd :param))))
(defun geben-cmd-expand (cmd)
"Build a send command CMD string for DBGp protocol."
(mapconcat #'(lambda (x)
(cond ((stringp x) x)
((integerp x) (int-to-string x))
((atom (format "%S" x)))
((null x) "")
(t x)))
(geben-flatten (list (plist-get cmd :operand)
"-i"
(plist-get cmd :tid)
(plist-get cmd :param)))
" "))
(defsubst geben-session-cmd-make (session operand params)
"Create a new command object for SESSION.
Assign OPERAND to :operand, and PARAMS to :param in the plist."
(list :session session
:tid (geben-session-next-tid session)
:operand operand
:param params))
(defsubst geben-session-cmd-append (session cmd)
(let ((cmds (geben-session-cmd session)))
(if cmds
(nconc cmds (list cmd))
(setf (geben-session-cmd session) (list cmd)))))
(defun geben-session-cmd-remove (session tid)
"Get a command object from the command hash table for SESSION specified by TID."
(let ((cmds (geben-session-cmd session)))
(if (eq tid (plist-get (car cmds) :tid))
(prog1
(car cmds)
(setf (geben-session-cmd session) (cdr cmds)))
(let (match-cmd)
(setf (geben-session-cmd session)
(cl-remove-if (lambda (cmd)
(and (eq tid (plist-get cmd :tid))
(setq match-cmd cmd)))
cmds))
match-cmd))))
;;==============================================================
;; DBGp protocol handler
;;==============================================================
(defsubst geben-dbgp-tid-read (msg)
"Get a transaction id of MSG."
(let ((tid (xml-get-attribute-or-nil msg 'transaction_id)))
(and tid
(string-to-number tid))))
(defun geben-dbgp-entry (session msg)
"Within SESSION, analyze MSG and dispatch to a specific handler."
;; remain session status ('connect, 'init, 'break, 'stopping, 'stopped)
(let ((handler (intern-soft (concat "geben-dbgp-handle-"
(symbol-name (xml-node-name msg)))))
(status (xml-get-attribute-or-nil msg 'status)))
(and status
(setf (geben-session-state session) (intern (concat ":" status))))
(and (functionp handler)
(funcall handler session msg))))
(defvar geben-dbgp-init-hook nil)
(defun geben-dbgp-handle-init (session msg)
"Within SESSION, handle a init message MSG."
(geben-session-init session msg)
(run-hook-with-args 'geben-dbgp-init-hook session))
(defun geben-dbgp-handle-response (session msg)
"Within SESSION, handle a response message MSG."
(let* ((tid (geben-dbgp-tid-read msg))
(cmd (geben-session-cmd-remove session tid))
(err (dbgp-xml-get-error-node msg)))
(geben-dbgp-handle-status session msg)
(geben-dbgp-process-command-queue session)
(cond
(err
(message "Command error: %s"
(dbgp-xml-get-error-message msg)))
(cmd
(let* ((operand (replace-regexp-in-string
"_" "-" (xml-get-attribute msg 'command)))
(func-name (concat "geben-dbgp-response-" operand))
(func (intern-soft func-name)))
(and (functionp func)
(funcall func session cmd msg)))))
(mapc (lambda (callback)
(funcall callback session cmd msg err))
(plist-get cmd :callback))))
(defun geben-dbgp-handle-status (session msg)
"Within SESSION, handle status code in a response message MSG."
(let ((status (xml-get-attribute msg 'status)))
(cond
((equal status "stopping")
(accept-process-output)
(and (geben-session-active-p session)
(geben-dbgp-command-stop session))))))
;;; command sending
(defun geben-dbgp-send-string (session string)
(and (string< "" string)
(geben-session-active-p session)
(dbgp-session-send-string (geben-session-process session) string t)))
(defun geben-send-raw-command (session fmt &rest arg)
"Send a command string to a debugger engine for SESSION.
The command string will be built up with FMT and ARG with a help of
the string formatter function `format'."
(let ((cmd (apply #'format fmt arg)))
(geben-dbgp-send-string session cmd)))
(defun geben-dbgp-send-command (session operand &rest params)
"Send a command to a debugger engine for SESSION.
OPERAND and PARAMS will be passed along to 'geben-session-cmd-make.
Return a cmd list."
(if (geben-session-active-p session)
(let ((cmd (geben-session-cmd-make session operand params)))
(geben-session-cmd-append session cmd)
(unless (geben-session-sending-p session)
(setf (geben-session-sending-p session) t)
(geben-dbgp-process-command-queue session))
cmd)))
(defun geben-dbgp-process-command-queue (session)
(let ((cmd (car (geben-session-cmd session))))
(if cmd
(geben-dbgp-send-string session (geben-cmd-expand cmd))
(setf (geben-session-sending-p session) nil))))
(defvar geben-dbgp-continuous-command-hook nil)
;;--------------------------------------------------------------
;; continuous commands
;;--------------------------------------------------------------
;; step_into
(defun geben-dbgp-command-step-into (session)
"Send \`step_into\' command to the SESSION."
(geben-dbgp-send-command session "step_into"))
(defun geben-dbgp-response-step-into (session cmd msg)
"For SESSION, send a CMD response MSG handler for \`step_into\'."
(run-hook-with-args 'geben-dbgp-continuous-command-hook session))
;; step_over
(defun geben-dbgp-command-step-over (session)
"Send \`step_over\' command to the SESSION."
(geben-dbgp-send-command session "step_over"))
(defun geben-dbgp-response-step-over (session cmd msg)
"For SESSION, send a CMD response MSG handler for \`step_over\'."
(run-hook-with-args 'geben-dbgp-continuous-command-hook session))
;; step_out
(defun geben-dbgp-command-step-out (session)
"Send \`step_out\' command to the SESSION."
(geben-dbgp-send-command session "step_out"))
(defun geben-dbgp-response-step-out (session cmd msg)
"A response message handler for \`step_out\' command."
(run-hook-with-args 'geben-dbgp-continuous-command-hook session))
;; run
(defun geben-dbgp-command-run (session)
"Send \`run\' command."
(geben-dbgp-send-command session "run"))
(defun geben-dbgp-response-run (session cmd msg)
"A response message handler for \`run\' command."
(run-hook-with-args 'geben-dbgp-continuous-command-hook session))
;;; stop
(defun geben-dbgp-command-stop (session)
"Send \`stop\' command."
(geben-dbgp-send-command session "stop"))
;;; eval
(defun geben-dbgp-command-eval (session exp)
"Send \`eval\' command."
(geben-dbgp-send-command
session
"eval"
(format "-- {%s}" (base64-encode-string exp))))
(defun geben-dbgp-response-eval (session cmd msg)
"A response message handler for \`eval\' command."
(message "result: %S"
(geben-dbgp-decode-value (car-safe (xml-get-children msg 'property)))))
(defun geben-dbgp-decode-value (prop)
"Decode a VALUE passed by debugger engine."
(let ((type (xml-get-attribute prop 'type))
result)
(setq result
(cond
((or (string= "array" type)
(string= "object" type))
(mapcar (lambda (value)
(geben-dbgp-decode-value value))
(xml-get-children prop 'property)))
((string= "null" type)
nil)
(t
(let ((value (car (last prop))))
(cl-assert (stringp value))
(when (string= "base64" (xml-get-attribute prop 'encoding))
(setq value (base64-decode-string value)))
(if (string= "string" type)
(decode-coding-string value 'utf-8)
(string-to-number value))))))
(let ((name (xml-get-attribute-or-nil prop 'name)))
(if name
(cons name result)
result))))
(eval-when-compile
(require 'tramp))
;;==============================================================
;; source
;;==============================================================
;; file hooks
(defcustom geben-source-visit-hook nil
"*Hook running at when GEBEN visits a debuggee script file.
Each function is invoked with one argument, BUFFER."
:group 'geben
:type 'hook)
(defcustom geben-close-mirror-file-after-finish t
"*Specify whether GEBEN should close fetched files from remote site after debugging.
Since the remote files is stored temporary that you can confuse
they were editable if they were left after a debugging session.
If the value is non-nil, GEBEN closes temporary files when
debugging is finished.
If the value is nil, the files left in buffers."
:group 'geben
:type 'boolean)
(defun geben-source-find-file-handler ()
(let* ((local-path (buffer-file-name))
(session (and local-path (geben-source-find-session local-path))))
(if session
(run-hook-with-args 'geben-source-visit-hook session (current-buffer)))))
(add-hook 'find-file-hook #'geben-source-find-file-handler)
;;--------------------------------------------------------------
;; source hash
;;--------------------------------------------------------------
(defcustom geben-source-coding-system 'utf-8
"Coding system for source code retrieving remotely via the debugger engine."
:group 'geben
:type 'coding-system)
(defmacro geben-source-make (fileuri local-path)
"Create a new source object.
A source object forms a property list with three properties
:fileuri, :remotep and :local-path."
`(list :fileuri ,fileuri :local-path ,local-path))
(defvar geben-source-release-hook nil)
(defun geben-source-release (source)
"Release a SOURCE object."
(let ((buf (find-buffer-visiting (or (plist-get source :local-path) ""))))
(when buf
(with-current-buffer buf
(when (bound-and-true-p geben-mode)
(run-hooks 'geben-source-release-hook))
(when geben-close-mirror-file-after-finish
(set-buffer-modified-p nil)
(kill-buffer buf))))))
(defsubst geben-source-fileuri-regularize (fileuri)
;; for bug of Xdebug 2.0.3 and below:
(replace-regexp-in-string "%28[0-9]+%29%20:%20runtime-created%20function$" ""
fileuri))
(defun geben-source-fileuri (session local-path)
"Guess a file uri string which counters to LOCAL-PATH."
(let* ((tempdir (geben-session-tempdir session))
(templen (length tempdir))
(tramp-spec (plist-get (geben-session-storage session) :tramp))
(tramp-spec-len (and tramp-spec (length tramp-spec))))
(concat "file://"
(cond
((and (< templen (length local-path))
(string= tempdir (substring local-path 0 templen)))
(substring local-path
(- templen
(if (string< "" (file-name-nondirectory tempdir)) 0 1))))
((and tramp-spec
(< tramp-spec-len (length local-path))
(string= tramp-spec (substring local-path 0 tramp-spec-len)))
(substring local-path tramp-spec-len))
(t
local-path)))))
(defun geben-source-local-path (session fileuri)
"Generate path string from FILEURI to store temporarily."
(let ((local-path (geben-source-local-path-in-server session fileuri)))
(when local-path
(expand-file-name (substring local-path (if (string-match "^[A-Z]:" local-path) 3 1))
(geben-session-tempdir session)))))
(defun geben-source-local-path-in-server (session fileuri &optional disable-completion)
"Make a path string correspond to FILEURI."
(when (string-match "^\\(file\\|https?\\):/+" fileuri)
(let ((path (substring fileuri (1- (match-end 0)))))
(require 'url-util)
(setq path (url-unhex-string path))
(when (string-match "^/[A-Z]:" path) ;; for HTTP server on Windows
(setq path (substring path 1)))
(if (and (not disable-completion)
(string= "" (file-name-nondirectory path)))
(expand-file-name (geben-source-default-file-name session)
path)
path))))
(defun geben-source-default-file-name (session)
(cl-case (geben-session-language session)
(:php "index.php")
(:python "index.py")
(:perl "index.pl")
(:ruby "index.rb")
(t "index.html")))
(defun geben-source-find-session (temp-path)
"Find a session which may have a file at TEMP-PATH in its temporary directory tree."
(cl-find-if (lambda (session)
(let ((tempdir (geben-session-tempdir session)))
(ignore-errors
(string= tempdir (substring temp-path 0 (length tempdir))))))
geben-sessions))
(defun geben-source-visit (local-path)
"Visit to a local source code file."
(let ((buf (or (find-buffer-visiting local-path)
(if (file-exists-p local-path)
(let* ((session (geben-source-find-session local-path))
(storage (and session
(geben-session-storage session)))
(coding-system (or (plist-get storage :source-coding-system)
geben-source-coding-system)))
(if coding-system
(let ((coding-system-for-read coding-system)
(coding-system-for-write coding-system))
(find-file-noselect local-path))
(find-file-noselect local-path)))))))
(when buf
(geben-dbgp-display-window buf)
buf)))
;; session storage
(defun geben-session-source-storage-add (session fileuri)
(let* ((storage (geben-session-storage session))
(list (plist-get storage :source)))
(if (and (string-match "^file:/" fileuri)
(not (cl-find list fileuri :test #'equal)))
(if list
(nconc list (list fileuri))
(plist-put storage :source (list fileuri))))))
;; session
(defun geben-session-source-init (session)
"Initialize a source hash table of the SESSION."
(setf (geben-session-source session) (make-hash-table :test 'equal)))
(add-hook 'geben-session-enter-hook #'geben-session-source-init)
(defun geben-session-source-add (session fileuri local-path content)
"Add a source object to SESSION."
(let ((tempdir (geben-session-tempdir session)))
(unless (file-directory-p tempdir)
(make-directory tempdir t)
(set-file-modes tempdir #o0700)))
(geben-session-source-write-file session local-path content)
(puthash fileuri (geben-source-make fileuri local-path) (geben-session-source session))
(geben-session-source-storage-add session fileuri))
(defun geben-session-source-release (session)
"Release source objects."
(maphash (lambda (fileuri source)
(geben-source-release source))
(geben-session-source session)))
(add-hook 'geben-session-exit-hook #'geben-session-source-release)
(add-hook 'geben-session-exit-hook #'geben-kill-buffers)
(defsubst geben-session-source-get (session fileuri)
(gethash fileuri (geben-session-source session)))
(defsubst geben-session-source-append (session fileuri local-path)
(puthash fileuri (list :fileuri fileuri :local-path local-path)
(geben-session-source session)))
(defsubst geben-session-source-local-path (session fileuri)
"Find a known local-path that counters to FILEURI."
(plist-get (gethash fileuri (geben-session-source session))
:local-path))
(defsubst geben-session-source-fileuri (session local-path)
"Find a known fileuri that counters to LOCAL-PATH."
(cl-block geben-session-souce-fileuri