-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjdibug-ui.el
executable file
·2324 lines (1996 loc) · 95.2 KB
/
jdibug-ui.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
;;; jdibug.el --- Fully elisp java debugger
;; Copyright (C) 2008 Phuah Yee Keat
;; Author: Phuah Yee Keat <[email protected]>
;; Maintainer: Troy Daniels <[email protected]>
;; Created: 20 May 2008
;; Keywords: lisp tools
;; This file is NOT part of GNU Emacs.
;; 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/'.
(defgroup jdibug nil
"JDIbug options"
:prefix "jdibug"
:group 'languages)
(defcustom jdibug-connect-hosts '("localhost:6001")
"Host:Port of the debuggees to connect to."
:group 'jdibug
:type '(repeat (string :tag "Host:Port")))
(defcustom jdibug-source-paths nil
"Paths of the source codes. This will be ignored if
`jdibug-use-jdee-source-paths' is t. This must be a list like '(\"~/src\") not astring like \"~/src\""
:group 'jdibug
:type 'list)
(defgroup jdibug-ui nil
"JDIbug user interface options."
:prefix "jdibug"
:group 'jdibug
:package-version '(jdibug . "0.7"))
(defcustom jdibug-refresh-delay 0.5
"The delay in seconds between when a breakpoint/step was hit
and the frames/locals buffer are refreshed. Set to 0 for instant update.
Set to more for speeding up quick multi step when the frames/locals buffer
need not be refreshed."
:group 'jdibug-ui
:type 'float)
(defcustom jdibug-locals-max-array-size 20
"The maximum number of entries shown when expanding an array.
Expanding large numbers of arrays is slow. Setting this to a
reasonably small number allows the arrays to be expanded quickly."
:group 'jdibug-ui
:type 'integer)
(defcustom jdibug-locals-min-sub-array-size 10
"The minium number of entries shown when expanding an
sub-array. This prevents the subarrays from being exceedingly
short."
:group 'jdibug-ui
:type 'integer)
(defcustom jdibug-use-jdee-source-paths t
"Set to t to use the jdee-sourcepath as the source paths.
`jdibug-source-paths' will be ignored if this is set to t."
:group 'jdibug
:type 'boolean)
(defcustom jdibug-threads-buffer-name "*JDIbug-Threads*"
"Name of the buffer to display the tree of threads."
:group 'jdibug-ui
:type 'string)
(defcustom jdibug-locals-buffer-name "*JDIbug-Locals*"
"Name of the buffer to display the tree of local variables."
:group 'jdibug-ui
:type 'string)
(defcustom jdibug-frames-buffer-name "*JDIbug-Frames*"
"Name of the buffer to display the list of active frames."
:group 'jdibug-ui
:type 'string)
(defcustom jdibug-breakpoints-buffer-name "*JDIbug-Breakpoints*"
"Name of the buffer to display the list of breakpoints."
:group 'jdibug-ui
:type 'string)
(defcustom jdibug-watchpoints-buffer-name "*JDIbug-Watchpoints*"
"Name of the buffer to display the list of watchpoints."
:group 'jdibug-ui
:type 'string)
(defcustom jdibug-break-on-class-regexp "^public\\s-+class"
"The regexp that will be used to identify whether you want to break on all the methods in the class."
:group 'jdibug
:type 'string)
(defcustom jdibug-step-over-classes
'("^java\\." "^javax\\.")
"List of regular expressions. Stepping into a class that matches any of
the regular expression will result in automatically stepping out of the method."
:group 'jdibug-ui
:type '(repeat string)
:package-version '("jdibug" . "0.7"))
(defvar jdibug-connected-hook nil
"Hook to run when we are connected to the debuggee.")
(defvar jdibug-breakpoint-hit-hook nil
"Hook to run when a breakpoint is hit.")
(defvar jdibug-detached-hook nil
"Hook to run when debuggee is detached, either from jdibug-disconnect or from vm-death event.")
(defvar jdibug-resumed-hook nil
"Hook to run when debuggee is resumed.")
(defcustom jdibug-locals-buffer-field-filters nil
"Filters to hide fields in the locals buffer. The filter is
passed a `jdi-field'. If any filter returns t, the field is not
shown. Useful values include `jdi-field-static-p',
`jdi-field-final-p' and `jdi-field-static-final-p'. Note that
`jdi-field-static-p' will filter static fields and static final
fields."
:group 'jdibug-ui
:type '(repeat function)
:package-version '("jdibug" . "0.7"))
(require 'bindat)
(require 'elog)
(require 'jdwp)
(require 'jdi)
(require 'jdibug-expr)
(require 'jdibug-util)
(require 'tree-mode)
(require 'eieio)
(elog-make-logger jdibug)
(defvar jdibug-virtual-machines nil
"list of jdi-virtual-machine")
(defvar jdibug-active-frame nil
"the jdi-frame that
1. we are looking at in the locals browser
2. we have selected in the frames buffer
3. our resume, step commands will go to")
(defvar jdibug-active-thread nil
"the thread that is suspended")
(defvar jdibug-others-suspended nil
"When a breakpoint event happens and we are already on a breakpoint. The (thread location request-id) will be pushed into this list.
When the user resumes, we will switch to this thread and location.")
(defvar jdibug-current-line-overlay nil)
(defvar jdibug-threads-tree nil)
(defvar jdibug-threads-buffer nil)
(defvar jdibug-breakpoints-buffer nil)
(defvar jdibug-watchpoints nil
"List of watchpoints")
(defvar jdibug-locals-buffer nil
"buffer the shows a tree of the local variables")
(defvar jdibug-watchpoints-buffer nil
"buffer the shows a tree of the local variables")
(defvar jdibug-locals-tree nil
"the variable that hold the tree-widget of the locals buffer")
(defvar jdibug-frames-buffer nil)
(defvar jdibug-frames-tree nil)
(defvar jdibug-refresh-threads-buffer-timer nil)
(defvar jdibug-refresh-frames-buffer-timer nil)
(defvar jdibug-refresh-locals-buffer-timer nil)
(defvar jdibug-refresh-watchpoints-buffer-timer nil)
(defvar jdibug-breakpoints-mode-map nil
"Local keymap for breakpoints buffer.")
(setq jdibug-breakpoints-mode-map (make-keymap))
(suppress-keymap jdibug-breakpoints-mode-map t)
(define-key jdibug-breakpoints-mode-map "e" 'jdibug-breakpoints-toggle)
(define-key jdibug-breakpoints-mode-map "d" 'jdibug-breakpoints-delete)
(define-key jdibug-breakpoints-mode-map "c" 'jdibug-breakpoints-add-condition)
(define-key jdibug-breakpoints-mode-map "\C-m" 'jdibug-breakpoints-goto)
(define-key jdibug-breakpoints-mode-map [mouse-1] 'jdibug-breakpoints-goto)
;;; Customized display and expanders:
(defvar jdibug-object-custom-set-strings nil
"a list of (class setter) where
class is a string which hold the class name of the object to be matched.
The matching will be done using something like instance of.
setter is a function that is passed (jdi jdi-value) and is expected
to populate the jdi-value-string of the jdi-value. If setter
is a string, it will be the method that will be invoked on the java object
and the value that is returned is shown.")
(setq jdibug-object-custom-set-strings
'(("java.lang.Boolean" "toString")
("java.lang.Number" "toString")
("java.lang.StringBuffer" "toString")
("java.util.Date" "toString")
("java.util.Collection" jdibug-object-custom-set-string-with-size)
("java.util.Map" jdibug-object-custom-set-string-with-size)))
(defvar jdibug-object-custom-expanders nil
"a list of (instance expander-func) where
instance is a string that is matched with jdi-object-instance-of-p with the
value
expander-func is a function that is passed (jdi jdi-value) and is expected
to populate the jdi-value-values of the jdi-value.")
(setq jdibug-object-custom-expanders
'(("java.util.Collection" jdibug-value-custom-expand-collection)
("java.util.Map" jdibug-value-custom-expand-map)))
(defclass jdibug-breakpoint nil
((status :initform unresolved :type symbol :accessor jdibug-breakpoint-status)
(event-requests :initform nil :type list ;; :allow-nil-initform t (probably need this)
:accessor jdibug-breakpoint-event-requests
:documentation
"List jdi-event-request. This is a list
because we might be installing 2 breakpoints for a single vm
because it have two class loaders, or we have two different vm!")
(condition :type list :initform nil
:accessor jdibug-breakpoint-condition
:documentation
"The condition for the breakpoint. If non-nil,
only stop if the condition evaluates to true. Any
other value (or an error) causes the breakpoint to
be ignored.")
(condition-text :type string :initform ""
:accessor jdibug-breakpoint-condition-text
:documentation "Unparsed text of the condition. Useful for display."))
:abstract t
:documentation "Underlying infrastructure for all types of conditions that can
suspend the JVM (even those that are not considered breakpoints
by JDWP).
Subclasses must define a static method `breakpoint-list' that
specifies the list that holds breakpoints after they are
created.")
;; Exception based breakpoint
(defclass jdibug-breakpoint-exception (jdibug-breakpoint)
;; Fields
((name :type string :initarg :name :accessor jdibug-breakpoint-name
:documentation
"Name of the exception in java format (e.g., java.lang.Exception)")
(caught :type boolean :initarg :caught :accessor jdibug-breakpoint-caught)
(uncaught :type boolean :initarg :uncaught :accessor jdibug-breakpoint-uncaught)
(list :type list :initform nil :accessor breakpoint-list :allocation :class))
:documentation "Breakpoint based on an exception being thrown")
(defclass jdibug-breakpoint-location (jdibug-breakpoint)
((source-file :type string :initarg :source-file :accessor jdibug-breakpoint-source-file)
(line-number :type integer :initarg :line-number :accessor jdibug-breakpoint-line-number)
(overlay :type (or overlay null) :initform nil ; :allow-nil-initform t
:accessor jdibug-breakpoint-overlay
:documentation
"Overlay within the file to indicate the status of the breakpoint.")
(list :type list :initform nil :accessor breakpoint-list :allocation :class))
:documentation "Traditional breakpoint based on file and line number.")
(defmethod pretty-print ((bp jdibug-breakpoint))
"Convert to a string for developers to understand what is in the object"
(concat (symbol-name (class-of bp)) " is "
(symbol-name (jdibug-breakpoint-status bp))
" with event requests of ("
(mapconcat (lambda (er)
(number-to-string (jdi-event-request-id er)))
(jdibug-breakpoint-event-requests bp)
", ")
")"
(if (jdibug-breakpoint-condition bp)
(format "\n\twhen %s" (jdibug-breakpoint-condition-text bp))
"")
"\n\t"
(pretty-print-extra bp)))
(defmethod pretty-print-extra ((bp jdibug-breakpoint-location))
"Extra information when pretty-printing a breakpoint"
(concat "at line " (number-to-string (jdibug-breakpoint-line-number bp))
" in " (jdibug-breakpoint-source-file bp)))
(defmethod pretty-print-extra ((bp jdibug-breakpoint-exception))
"Extra information when pretty-printing a breakpoint"
(concat "when " (jdibug-breakpoint-name bp)
" is thrown"
(if (jdibug-breakpoint-caught bp)
(if (jdibug-breakpoint-uncaught bp)
;; always caught
""
" and caught")
(if (jdibug-breakpoint-uncaught bp)
" but not caught"
"but neither caught nor not caught?!?"))))
(defun jdibug-list-breakpoints (&rest ignore)
"List all of the current breakpoints and provide a reasonably
legible description of the breakpoint. The resulting buffer has
static contents, but `revert-buffer' in the resulting buffer will
update the list of breakpoints based on the current breakpoints."
(interactive "d")
(switch-to-buffer "*JDIbug Breakpoint List*")
(erase-buffer)
(mapc (lambda (bp)
(insert (pretty-print bp))
(newline))
(jdibug-all-breakpoints))
(setq revert-buffer-function 'jdibug-list-breakpoints))
(defmethod cleanup-on-disconnect ((bp jdibug-breakpoint))
"Do any cleanup related to this BP when the jvm disconnects"
(setf (jdibug-breakpoint-status bp) 'unresolved))
(defmethod cleanup-on-disconnect ((bp jdibug-breakpoint-location))
"Do any cleanup related to this BP when the jvm disconnects"
(call-next-method)
(if (jdibug-breakpoint-overlay bp)
(delete-overlay (jdibug-breakpoint-overlay bp))))
(defun jdibug-all-breakpoints nil
"Return all instances of jdibug breakpoints"
(loop for class in (class-children jdibug-breakpoint)
append (breakpoint-list class)))
(defstruct jdibug-watchpoint
;; Basic expression to evaluate
expression
;; Parsed form of the expression
parse)
(defmethod short-source-file ((bp jdibug-breakpoint-location))
(let ((buf (jdibug-breakpoint-source-file bp)))
(setf buf (replace-regexp-in-string ".*/" "" buf))
buf))
(defvar jdibug-current-message "")
(defun jdibug-message (message &optional append)
(if append
(setf jdibug-current-message (concat jdibug-current-message message))
(setf jdibug-current-message message))
(jdibug-info jdibug-current-message)
(message jdibug-current-message))
(add-hook 'jdi-breakpoint-hooks 'jdibug-handle-breakpoint)
;; Currently, no difference in breakpoint and exception handling,
;; since exception events occur when we want to break on the exception
;; being thrown
(add-hook 'jdi-exception-hooks 'jdibug-handle-breakpoint)
(add-hook 'jdi-step-hooks 'jdibug-handle-step)
(add-hook 'jdi-detached-hooks 'jdibug-handle-detach)
(add-hook 'jdi-class-prepare-hooks 'jdibug-handle-class-prepare)
(add-hook 'jdi-thread-start-hooks 'jdibug-handle-thread-start)
(add-hook 'jdi-thread-end-hooks 'jdibug-handle-thread-end)
(defun jdibug-connect ()
"Create a new debugger and connect to a running process. See
`jdibug-connect-hosts' for where it will look for the debuggee
processes"
(interactive)
;; (unless (byte-code-function-p (symbol-function 'jdibug-connect))
;; (error "You must byte-compile jdibug before using it."))
(jdibug-debug "jdibug-connect")
(if (jdibug-connected-p)
(message "JDIbug already connected")
(jdwp-uninterruptibly
(jdibug-message "JDIbug connecting... ")
(jdibug-debug "setting jdibug-active-thread to nil in jdibug-connect")
(setq jdibug-threads-buffer (get-buffer-create jdibug-threads-buffer-name)
jdibug-locals-buffer (get-buffer-create jdibug-locals-buffer-name)
jdibug-watchpoints-buffer (get-buffer-create jdibug-watchpoints-buffer-name)
jdibug-frames-buffer (get-buffer-create jdibug-frames-buffer-name)
jdibug-breakpoints-buffer (get-buffer-create jdibug-breakpoints-buffer-name)
jdibug-active-frame nil
jdibug-active-thread nil
jdibug-others-suspended nil)
(with-current-buffer jdibug-breakpoints-buffer
(use-local-map jdibug-breakpoints-mode-map)
(toggle-read-only 1))
(setq jdibug-virtual-machines
(remove nil
(mapcar (lambda (connect-host-and-port)
(let* ((host (nth 0 (split-string connect-host-and-port ":")))
(port (string-to-number (nth 1 (split-string connect-host-and-port ":"))))
(jdwp (make-jdwp))
(vm (make-jdi-virtual-machine :host host :port port :jdwp jdwp)))
(jdibug-message (format "%s:%s" host port) t)
(condition-case err
(progn
(jdi-virtual-machine-connect vm)
(jdibug-message "(connected)" t)
vm)
(file-error (jdibug-message "(failed)" t) nil))))
jdibug-connect-hosts)))
(when (find-if 'identity jdibug-virtual-machines)
(mapc 'set-breakpoint (jdibug-all-breakpoints))
(run-hooks 'jdibug-connected-hook)
(jdibug-refresh-frames-buffer)
(jdibug-refresh-threads-buffer)))))
(defun jdibug-disconnect ()
(interactive)
(jdwp-uninterruptibly
(jdibug-trace "jdibug-disconnect")
(jdibug-cleanup-on-disconnect)
(jdibug-message "JDIbug disconnecting... ")
(mapc (lambda (vm)
(jdibug-message (format "%s:%s"
(jdi-virtual-machine-host vm)
(jdi-virtual-machine-port vm)) t)
(jdi-virtual-machine-disconnect vm)
(jdibug-message "(disconnected) " t))
jdibug-virtual-machines)
(setq jdibug-virtual-machines nil)
(run-hooks 'jdibug-detached-hook)))
(defun jdibug-cleanup-on-disconnect nil
(if jdibug-current-line-overlay
(delete-overlay jdibug-current-line-overlay))
(mapc (lambda (bp)
(cleanup-on-disconnect bp))
(jdibug-all-breakpoints))
(and (timerp jdibug-refresh-locals-buffer-timer)
(cancel-timer jdibug-refresh-locals-buffer-timer))
(and (timerp jdibug-refresh-watchpoints-buffer-timer)
(cancel-timer jdibug-refresh-watchpoints-buffer-timer))
(and (timerp jdibug-refresh-frames-buffer-timer)
(cancel-timer jdibug-refresh-frames-buffer-timer))
(and (timerp jdibug-refresh-threads-buffer-timer)
(cancel-timer jdibug-refresh-threads-buffer-timer))
(mapc (lambda (buf)
(when buf (kill-buffer buf)))
(list jdibug-locals-buffer jdibug-watchpoints-buffer
jdibug-frames-buffer jdibug-threads-buffer))
(setq jdibug-locals-tree nil
jdibug-locals-buffer nil
jdibug-frames-tree nil
jdibug-frames-buffer nil
jdibug-threads-buffer nil
jdibug-threads-tree nil
jdibug-watchpoints-buffer nil
jdibug-active-thread nil
jdibug-active-frame nil))
(defun jdibug-exit-jvm ()
"End the debugging session and kill all attached JVMs."
(interactive)
(jdwp-uninterruptibly
(jdibug-trace "jdibug-exit-jvm")
(jdibug-cleanup-on-disconnect)
(jdibug-message "JDIbug disconnecting and killing JVM... ")
(mapc (lambda (vm)
(jdibug-message (format "%s:%s"
(jdi-virtual-machine-host vm)
(jdi-virtual-machine-port vm)) t)
(jdi-virtual-machine-exit vm -1)
(jdibug-message " (killed) " t))
jdibug-virtual-machines)
(setq jdibug-virtual-machines nil)
(run-hooks 'jdibug-detached-hook)))
(defun jdibug-have-class-source-p (class)
(jdibug-debug "jdibug-have-class-source-p")
(let ((signature (jdi-class-get-signature class)))
(file-exists-p (jdibug-class-signature-to-source-file signature))))
(defun jdibug-find-buffer (file-name)
"Return the buffer that is viewing this file."
(catch 'found
(dolist (buf (buffer-list))
(if (equal file-name (buffer-file-name buf))
(throw 'found buf)))))
(defun jdibug-raise-window (window)
"Raise the frame that is showing this window."
(jdibug-debug "jdibug-raise-window")
(let ((frame (window-frame window)))
(make-frame-visible frame)
(raise-frame frame)
(select-frame frame)
(select-window window)))
(defun jdibug-show-file-and-line-number (file-name line-number &optional highlight)
"Show the buffer containing the file, or open the file if there are no open buffer for this file.
And position the point at the line number."
(jdibug-debug "jdibug-show-file-and-line-number:%s:%s" file-name line-number)
(let* ((buffer-name (jdibug-find-buffer file-name))
(win (if buffer-name (get-buffer-window buffer-name t) nil))
(frame (if win (window-frame win) nil)))
(jdibug-debug "buffer-name=%s win=%s frame=%s" buffer-name win frame)
(if win
;; file is already open in a buffer, just show it
(progn
(jdibug-raise-window win)
(goto-char (point-min))
(forward-line (1- line-number))
(if highlight
(with-current-buffer buffer-name
(jdibug-highlight-current-line line-number))))
;; file is not open, try to find it
(if (file-exists-p file-name)
(if line-number
(let ((win (catch 'done
(walk-windows (lambda (win)
(if (not (window-dedicated-p win))
(throw 'done win)))))))
(select-window win)
(find-file file-name)
(jdibug-raise-window win)
(goto-char (point-min))
(forward-line (1- line-number))
(if highlight
(with-current-buffer (window-buffer win)
(jdibug-highlight-current-line line-number))))
(message "JDIbug file %s does not have line number information" file-name))
(message "JDIbug file %s not in source path" file-name)))))
(defun jdibug-goto-location (location)
(jdibug-debug "jdibug-goto-location")
(let ((signature (jdi-class-get-signature (jdi-location-class location))))
(let ((line-number (jdi-location-get-line-number location)))
(jdibug-show-file-and-line-number (jdibug-class-signature-to-source-file signature)
line-number
t))))
(defun jdibug-handle-breakpoint (thread location request-id)
(jdibug-debug "jdibug-handle-breakpoint: active-frames is %s"
(and jdibug-active-frame (jdi-frame-id jdibug-active-frame)))
;; Check if this is a conditional breakpoint with an unsatisfied condition
(if (jdibug-breakpoint-condition-p thread location request-id)
(progn
(if (null jdibug-active-frame)
(progn
(jdibug-goto-location location)
(setq jdibug-active-frame (car (jdi-thread-get-frames thread)))
(jdibug-refresh-locals-buffer)
(jdibug-refresh-watchpoints-buffer)
;; (jdibug-debug "setting jdibug-active-thread to %s in jdibug-handle-breakpoint"
;; thread)
(setq jdibug-active-thread thread))
(setq jdibug-others-suspended (append jdibug-others-suspended (list `(,thread ,location ,request-id)))))
(jdibug-refresh-frames-buffer)
(jdibug-refresh-threads-buffer)
(run-hooks 'jdibug-breakpoint-hit-hook))
;; Condition not satisfied, so continue
(jdibug-trace "Condition not satisfied so resuming thread")
(jdi-thread-resume thread)))
(defun jdibug-breakpoint-condition-p (thread location request-id)
"Check if the breakpoint corresponding to REQUEST-ID that has
been hit in THREAD at LOCATION either has no condition or that
the condition is satisfied."
(let ((bp (find-if (lambda (bp)
(find-if (lambda (er)
(= (jdi-event-request-id er) request-id))
(jdibug-breakpoint-event-requests bp)))
(jdibug-all-breakpoints)))
condition)
(if (not bp)
(progn
(jdibug-error "Unable to find breakpoint with request id %d" request-id)
nil)
(setq condition (jdibug-breakpoint-condition bp))
(if condition
(let* ((frame (car (jdi-thread-get-frames thread)))
(jdwp (jdi-virtual-machine-jdwp (jdi-frame-virtual-machine frame)))
(result (catch jdibug-expr-bad-eval (jdibug-expr-eval-expr jdwp condition frame))))
;; We pass only if the result is a boolean that is true
(when (jdi-value-p result)
(if (= (jdi-value-type result) jdwp-tag-boolean)
(not (equal (jdi-primitive-value-value result) 0))
(jdibug-warn "Breakpoint condition is not a boolean but a %s" (jdwp-type-name (jdi-value-type result)))
nil)))
t))))
(defun jdibug-handle-step (thread location)
(jdibug-debug "jdibug-handle-step")
;; (jdibug-debug "setting jdibug-active-thread to %s in jdibug-handle-step"
;; thread)
(setq jdibug-active-thread thread)
(jdibug-goto-location location)
;; Check if we are in a class that we want to step out of
(if (jdibug--auto-step-out-class-p location)
(jdibug-step-out)
;; We want to stop here. Update the displays
(jdibug-refresh-frames-buffer)
(jdibug-refresh-threads-buffer)
(unless jdibug-active-frame
(setq jdibug-active-frame (car (jdi-thread-get-frames
jdibug-active-thread)))
(jdibug-refresh-locals-buffer)
(jdibug-refresh-watchpoints-buffer))))
(defun jdibug--auto-step-out-class-p (location)
"Check if LOCATION is part of a class that we want
to automatically step out of"
(let ((class-name (car (jdi-jni-to-print
(jdi-class-get-signature
(jdi-location-class location))))))
(cl-find class-name jdibug-step-over-classes
:test (lambda (string regexp) (string-match regexp string)))))
(defun jdibug-handle-change-frame (frame)
(jdibug-debug "jdibug-handle-change-frame")
(jdibug-goto-location (jdi-frame-location frame))
(jdibug-refresh-frames-buffer)
(jdibug-refresh-watchpoints-buffer)
(jdibug-refresh-locals-buffer))
(defun jdibug-handle-class-prepare (class thread)
(jdibug-debug "jdibug-handle-class-prepare")
;; TODO: refactor this to call an overridden method?
(dolist (bp (breakpoint-list jdibug-breakpoint-location))
(jdibug-handle-class-prepare-breakpoint class thread bp))
(dolist (exc (breakpoint-list jdibug-breakpoint-exception))
(jdibug-handle-class-prepare-exception class thread exc))
(jdibug-debug "jdibug-handle-class-prepare finishing, resuming thread, thread status is %s" (jdi-thread-get-status thread))
(jdi-thread-resume thread))
(defun jdibug-handle-class-prepare-exception (class thread exc)
"Check loaded class against exception breaks."
(jdibug-debug "jdibug-handle-class-prepare-exception: class=%s, exc=%s" (jdi-class-get-signature class) (jdibug-breakpoint-name exc))
(when (jdibug-signature-matches-name-with-wildcards
(jdi-class-get-signature class)
(jdibug-breakpoint-name exc))
(jdibug-debug "Found exception matching %s" (jdibug-breakpoint-name exc))
(if (jdi-class-instance-of-p
class
(jdi-class-name-to-class-signature "java.lang.Throwable"))
(progn
(let* ((vm (jdi-mirror-virtual-machine class)))
(set-breakpoint exc (list vm))
(jdibug-refresh-breakpoints-buffer)))
(jdibug-warn "%s is not a subclass of Throwable. Not setting break when thrown."
(jdi-jni-to-print (jdi-class-get-signature class))))))
(defun jdibug-signature-matches-name-with-wildcards (signature name)
"Checks if the JNI SIGNATURE matches NAME, which might contain * as a wildcard"
(jdibug-debug "jdibug-signature-matches-name-with-wildcards %s %s"
signature name)
(let ((sig-as-name (car (jdi-jni-to-print signature)))
(name-regexp (mapconcat 'regexp-quote
(split-string name "*")
".*")))
(jdibug-debug "Checking if %s matches %s" sig-as-name name-regexp)
(string-match (concat "^" name-regexp "$") sig-as-name)))
(defun jdibug-handle-class-prepare-breakpoint (class thread bp)
"Check loaded class against breakpoints"
(when (equal (jdibug-breakpoint-source-file bp)
(jdibug-class-signature-to-source-file (jdi-class-get-signature class)))
(jdibug-debug "Found breakpoint in %s" (jdibug-breakpoint-source-file bp))
(let (found)
(dolist (location (jdi-class-get-locations-of-line class (jdibug-breakpoint-line-number bp)))
(setq found t)
(let ((vm (jdi-mirror-virtual-machine class)))
(set-breakpoint bp (list vm) class)
(jdibug-refresh-breakpoints-buffer))))))
(defun jdibug-handle-detach (vm)
(if jdibug-current-line-overlay
(delete-overlay jdibug-current-line-overlay))
(mapc (lambda (bp) (handle-detach bp))
(jdibug-all-breakpoints))
(message "JDIbug %s:%s vm detached" (jdi-virtual-machine-host vm) (jdi-virtual-machine-port vm))
(unless (jdibug-connected-p)
(run-hooks 'jdibug-detached-hook)))
(defmethod handle-detach ((bp jdibug-breakpoint))
"Handle detaching from a running JVM. The default implementation does nothing."
nil)
(defmethod handle-detach ((bp jdibug-breakpoint-location))
"Clear the overlay if it exists"
(if (jdibug-breakpoint-overlay bp)
(delete-overlay (jdibug-breakpoint-overlay bp))))
(defun jdibug-handle-thread-start (thread)
;; We don't request that the thread is suspended in the event
;; request, so don't resume it. Doing so can cause problems with other
;; events, like class-prepare, which do suspend it and cause the
;; thread to be resumed before a breakpoint can be set.
;; (jdi-thread-resume thread)
(jdibug-refresh-frames-buffer)
(jdibug-refresh-threads-buffer))
(defun jdibug-handle-thread-end (thread)
(jdibug-refresh-frames-buffer)
(jdibug-refresh-threads-buffer))
(defun jdibug-highlight-current-line (line-number)
(jdibug-debug "jdibug-highlight-current-line:%d:current-buffer=%s" line-number (current-buffer))
(goto-char (point-min))
(forward-line (1- line-number))
(beginning-of-line)
(if jdibug-current-line-overlay
(delete-overlay jdibug-current-line-overlay))
(setq jdibug-current-line-overlay (make-overlay (point) (1+ (line-end-position))))
(overlay-put jdibug-current-line-overlay 'face 'jdibug-current-line)
(overlay-put jdibug-current-line-overlay 'priority 10))
;;(custom-set-faces '(jdibug-current-line ((t (:foreground "navajo white" :background "DodgerBlue"))) t))
(defface jdibug-current-line
`((t (:foreground "navajo white" :background "DodgerBlue")))
"Face for current executing line"
:group 'jdibug-ui)
;;(custom-set-faces '(jdibug-breakpoint-enabled ((t (:foreground "navajo white" :background "indian red"))) t))
(defface jdibug-breakpoint-enabled
`((t (:foreground "navajo white" :background "indian red")))
"Face for enabled breakpoint"
:group 'jdibug-ui)
;;(custom-set-faces '(jdibug-breakpoint-disabled ((t (:foreground "navajo white" :background "SpringGreen4"))) t))
(defface jdibug-breakpoint-disabled
`((t (:foreground "navajo white" :background "SpringGreen4")))
"Face for disabled breakpoint"
:group 'jdibug-ui)
;;(custom-set-faces '(jdibug-breakpoint-unresolved ((t (:foreground "navajo white" :background "gold4"))) t))
(defface jdibug-breakpoint-unresolved
`((t (:foreground "navajo white" :background "gold4")))
"Face for unresolved breakpoint"
:group 'jdibug-ui)
(defface jdibug-current-frame
`((t (:foreground "navajo white" :background "DodgerBlue")))
"Face for current frame"
:group 'jdibug-ui)
(defun jdibug-expand-method-node (tree)
(jdwp-uninterruptibly
(jdibug-debug "jdibug-expand-method-node")
(let* ((value (widget-get tree :jdi-value))
(method (widget-get tree :jdi-method))
(result-value (jdi-value-invoke-method value jdibug-active-thread method nil nil))
(result-string (jdibug-value-get-string result-value)))
(jdibug-debug "running method %s returned %s" (jdi-method-name method) result-string)
(list (jdibug-make-tree-from-value "result" result-value result-string)))))
(defun jdibug-make-method-node (value method)
(let ((display (format "%s: %s" (jdi-method-name method) (jdi-method-signature method))))
(if (string= "()" (substring (jdi-method-signature method) 0 2))
`(tree-widget
:node (push-button
:tag ,display
:format "%[%t%]\n")
:open nil
:jdi-value ,value
:jdi-method ,method
:dynargs jdibug-expand-method-node
:expander jdibug-expand-method-node)
`(item
:value
,display))))
(defun jdibug-expand-methods (tree)
(jdwp-uninterruptibly
(let ((value (widget-get tree :jdi-value)))
(let ((class (jdi-value-get-reference-type value)))
(let ((methods (jdi-class-get-all-methods class)))
(mapcar (lambda (method)
(jdibug-make-method-node value method))
(sort (remove-duplicates methods
:test (lambda (obj1 obj2)
(and (equal (jdi-method-name obj1)
(jdi-method-name obj2))
(equal (jdi-method-signature obj1)
(jdi-method-signature obj2))))
:from-end t)
(lambda (obj1 obj2)
(string< (jdi-method-name obj1)
(jdi-method-name obj2))))))))))
(defun jdibug-make-methods-node (value)
`(tree-widget
:node (push-button
:tag "methods"
:format "%[%t%]\n")
:open nil
:jdi-value ,value
:dynargs jdibug-expand-methods
:expander jdibug-expand-methods))
(defun jdibug-expand-locals (tree)
(jdibug-debug "jdibug-expand-locals")
(let ((values (widget-get tree :jdi-values))
(variables (widget-get tree :jdi-variables)))
(let ((strings (mapcar 'jdibug-value-get-string values)))
(loop for variable in variables
for value in values
for string in strings
collect (jdibug-make-tree-from-variable-value variable value string)))))
(defun jdibug-make-locals-tree (variables values)
`(tree-widget
:node (push-button
:tag "Locals"
:format "%[%t%]\n")
:open t
:jdi-variables ,variables
:jdi-values ,values
:dynargs jdibug-expand-locals
:expander jdibug-expand-locals))
(defun jdibug-tree-mode-find-child-path (tree path)
(when path
(if (equal (widget-get (tree-widget-node tree) :tag) (car path))
path
(find-if (lambda (path)
(jdibug-tree-mode-find-child-path tree path))
(cdr path)))))
(defun jdibug-tree-mode-reflesh-tree (tree)
"Redraw TREE.
If tree has attribute :dynargs, generate new :args from that function.
Otherwise use :old-args which saved by `tree-mode-backup-args'."
(eval `(jdwp-uninterruptibly (jdibug-tree-mode-reflesh-tree-1 ,tree))))
(defun jdibug-tree-mode-reflesh-tree-1 (tree)
(let ((path (or (jdibug-tree-mode-find-child-path tree (widget-get tree :jdibug-opened-path))
(tree-mode-opened-tree tree))))
(jdibug-debug "jdibug-tree-mode-reflesh-tree:tag=%s:path=%s" (widget-get (tree-widget-node tree) :tag) path)
(jdibug-debug "jdibug-tree-mode-reflesh-tree:opened-path=%s" (tree-mode-opened-tree tree))
(if (widget-get tree :dynargs)
(widget-put tree :args nil)
(if (widget-get tree :old-args)
(widget-put tree :args (widget-get tree :old-args))))
(jdibug-debug "before widget-value-set")
(widget-value-set tree (widget-value tree))
(jdibug-debug "before tree-mode-open-tree")
(tree-mode-open-tree tree path)))
(defun jdibug-value-expander (tree)
(jdibug-make-expansion-list 'jdibug-value-expander-1 'jdibug-tree-mode-reflesh-tree tree))
(defun jdibug-value-expander-1 (tree)
(let ((value (widget-get tree :jdi-value)))
(jdibug-debug "jdibug-value-expander:type=%s" (jdi-value-type value))
;; call the custom expander here, so that the custom expander can
;; make a ArrayList look like an array by changing the value-type
(let* ((expander (find-if (lambda (custom)
(jdi-object-instance-of-p value (jdi-class-name-to-class-signature (car custom))))
jdibug-object-custom-expanders))
(expander2 (if expander (cadr expander))))
(cond (expander2
(funcall expander2 value))
((= (jdi-value-type value) jdwp-tag-object)
(jdibug-value-expander-object value))
((= (jdi-value-type value) jdwp-tag-array)
(jdibug-value-expander-array value))))))
(defun jdibug-value-expander-object (value)
(let* ((reference-type (jdi-value-get-reference-type value))
(fields (sort (copy-sequence (jdi-reference-type-get-all-fields reference-type)) 'jdibug-field-sorter))
(values (jdi-value-get-values value fields)))
(jdibug-debug "jdibug-value-expander got %s values" (length values))
(append (loop for v in values
for f in fields
when (jdibug-make-tree-from-field-value f v)
collect it)
(list (jdibug-make-methods-node value)))))
(defun jdibug-value-expander-array (value &optional first last)
(let* ((first (or first 0))
(last (or last (jdi-array-get-array-length value)))
(num-display (- last first)))
(if (<= num-display jdibug-locals-max-array-size)
;; Short array, so display all of the values
(loop with values = (jdi-array-get-values value first last)
with strings = (mapcar 'jdibug-value-get-string values)
for v in values
for s in strings
for i from first by 1
collect (jdibug-make-tree-from-value (format "[%s]" i) v s))
;; Long array, so create pseudo nodes for subarrays
(loop with step = (jdibug-locals-step-size num-display)
for start from first below last by step
for end = (min last (+ start step))
collect (jdibug-make-array-subtree value start end)))))
(defun jdibug-locals-step-size (num-display)
"Determine the number of subnodes to create when NUM-DISPLAY entries are present. This respects `jdibug-locals-max-array-size' and `jdibug-locals-min-sub-array-size' and attempts to find a round value to return."
(let ((step (max (ceiling num-display jdibug-locals-max-array-size) jdibug-locals-min-sub-array-size))
(scale 1))
(if (> step jdibug-locals-min-sub-array-size)
;; Round up to the nearest power of 10.
(progn
;; First, find the largest power of 10 less than the current step
(while (< (* scale 10) step)
(setq scale (* scale 10)))
;; Now round up to that value
(* scale (ceiling step scale)))
;; We are at the minimum value (which the user can set) so just use that
step)))
(defun jdibug-make-map-array-subtree (value start end label expander)
"Create a pseudo node representing a subset of the array VALUE
from START (inclusive) to END (exclusive). Use LABEL to format
the text for the button. It must be a format string that takes
the two parameters (the start and end indices). EXPANDER is used
to expand the node."
(let ((display (format label start (1- end))))
`(tree-widget
:node (push-button
:tag ,display
:format "%[%t%]\n")
:open nil
:args nil
:jdi-value ,value
:jdi-start ,start
:jdi-end ,end
:expander ,expander
:dynargs ,expander)))
(defun jdibug-make-array-subtree (value start end)
"Create a pseudo node representing a subset of the array VALUE
from START (inclusive) to END (exclusive)"
(jdibug-make-map-array-subtree value start end "[%d-%d]" 'jdibug-array-subtree-expander))
(defun jdibug-make-map-subtree (value start end)
"Create a pseudo node representing a subset of the array VALUE