-
Notifications
You must be signed in to change notification settings - Fork 4
/
jdi.el
executable file
·1750 lines (1511 loc) · 72.5 KB
/
jdi.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
;;; jdi.el --- library that provides the Java(tm) Debug Interface
;; 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/'.
;;; Commentary:
;; This module tries to implement everything documented here:
;; http://java.sun.com/j2se/1.4.2/docs/guide/jpda/jdi/
;; Look near the end of the file for customizing how special
;; objects (ArrayList, HashMap) are displayed in the locals buffer
;; This module requires bindat.el, elog.el and jdwp.el
;;; Code:
(require 'bindat)
(require 'elog)
(require 'jdwp)
(elog-make-logger jdi)
(defvar jdi-unit-testing nil)
(eval-when-compile
(setq jdi-unit-testing t)
(defvar jdibug-active-thread))
(defstruct jdi-mirror
virtual-machine)
(defstruct jdi-virtual-machine
jdwp
description
jdwp-major
jdwp-minor
version
name
(event-request-manager (make-jdi-event-request-manager))
host
port
(classes (make-hash-table :test 'equal)) ;; hash table where key=class-id value=jdi-class
classes-by-signature ;; hash table where key=signature value=jdi-class
suspended-thread-id
;; list of jdi-frame that is suspended (by breakpoint/step events)
suspended-frames
(objects (make-hash-table :test 'equal)) ;; hash table where key=object-id value=jdi-object
;; Exception breakpoint workaround data
(break-on-exception-data (make-hash-table))
)
(defstruct (jdi-reference-type (:include jdi-mirror))
id
fields-cache)
(defstruct (jdi-class (:include jdi-reference-type))
;; jni-style
signature
generic-signature
ref-type-tag
status
;; list of jdi-field
fields
;; parent jdi-class
super
;; list of interfaces implemented by this class
interfaces
interfaces-count ;; we use this to see whether the interfaces field have been resolved or not
;; list of jdi-method
methods
;; list of nested classes. This is a cached value and might be nil.
;; Use jdi-class-get-nested-classes instead.
nested-classes)
(defstruct (jdi-interface (:include jdi-reference-type)))
(defstruct (jdi-array-type (:include jdi-reference-type)))
(defstruct (jdi-method (:include jdi-mirror))
id
name
signature
mod-bits
;; list of jdi-location
locations
;; list of jdi-variable
variables
;; link back to our containing jdi-class
class
)
(defstruct (jdi-location (:include jdi-mirror))
;; this might have to be resolved
line-number
line-code-index
type
;; jdi-class
class
;; jdi-method
method
;; String to be displayed in the frames buffer
string)
(defstruct jdi-event-request-manager
breakpoint-requests
)
(defstruct (jdi-event-request (:include jdi-mirror))
id
data
)
;; com.sun.jdi.Value
(defstruct (jdi-value (:include jdi-mirror))
type)
(defstruct (jdi-primitive-value (:include jdi-value))
value)
(defstruct (jdi-object (:include jdi-value)) ;; com.sun.jdi.ObjectReference
id)
(defstruct (jdi-string (:include jdi-object)))
(defstruct (jdi-array (:include jdi-object)))
(defstruct (jdi-class-object (:include jdi-object)))
(defstruct (jdi-class-loader (:include jdi-object)))
(defstruct (jdi-thread (:include jdi-object))
(running-p t)
thread-group-cache
daemon-p-cache
system-thread-p-cache
)
(defstruct (jdi-thread-group (:include jdi-object))
name
parent
child-threads
child-groups)
(defstruct (jdi-frame (:include jdi-mirror)) ;; this is actually StackFrame in JDI
id
values
thread
location)
(defstruct (jdi-variable (:include jdi-mirror)) ;; this is actually LocalVariable in JDI
code-index
name
signature
length
slot)
(defstruct (jdi-field (:include jdi-mirror))
id
name
signature
generic-signature
mod-bits)
;;; Constants:
(defconst jdi-access-public #x0001)
(defconst jdi-access-private #x0002)
(defconst jdi-access-protected #x0004)
(defconst jdi-access-static #x0008)
(defconst jdi-access-final #x0010)
(defconst jdi-access-synchronized #x0020)
(defconst jdi-access-volatile #x0040)
(defconst jdi-access-transient #x0080)
(defconst jdi-access-native #x0100)
(defconst jdi-access-abstract #x0400)
(defun jdi-mirror-jdwp (mirror)
(jdi-virtual-machine-jdwp (jdi-mirror-virtual-machine mirror)))
(defun jdi-event-request-manager-create-breakpoint (erm location)
(let* ((location-data `((:type . 1)
(:class-id . ,(jdi-class-id (jdi-location-class location)))
(:method-id . ,(jdi-method-id (jdi-location-method location)))
(:index . ,(jdi-location-line-code-index location))))
(data `((:event-kind . ,jdwp-event-breakpoint)
(:suspend-policy . ,jdwp-suspend-policy-event-thread)
(:modifiers . 1)
(:modifier ((:mod-kind . ,jdwp-mod-kind-location-only)
(:location . ,location-data))))))
(make-jdi-event-request :virtual-machine (jdi-mirror-virtual-machine location) :data data)))
(defun jdi-mappend (fn &rest lsts)
(apply 'append (apply 'mapcar fn lsts)))
(defun jdi-event-request-manager-create-step (erm thread depth)
(let ((data `((:event-kind . ,jdwp-event-single-step)
(:suspend-policy . ,jdwp-suspend-policy-event-thread)
(:modifiers . 2)
(:modifier
((:mod-kind . ,jdwp-mod-kind-case-step)
(:thread . ,(jdi-thread-id thread))
(:size . 1)
(:depth . ,depth))
((:mod-kind . ,jdwp-mod-kind-case-count)
(:count . 1))))))
(make-jdi-event-request :virtual-machine (jdi-mirror-virtual-machine thread) :data data)))
(defun jdi-event-request-manager-create-class-prepare (erm vm signature)
(jdi-debug "jdi-event-request-manager-create-class-prepare %s" signature)
(let ((data `((:event-kind . ,jdwp-event-class-prepare)
(:suspend-policy . ,jdwp-suspend-policy-event-thread)
(:modifiers . 1)
(:modifier
((:mod-kind . ,jdwp-mod-kind-class-match)
(:class-pattern . ((:length . ,(length signature))
(:string . ,signature))))))))
(make-jdi-event-request :virtual-machine vm :data data)))
(defun jdi-event-request-manager-create-break-on-exception (erm vm class-id caught uncaught)
(let ((data `((:event-kind . ,jdwp-event-exception)
(:suspend-policy . ,jdwp-suspend-policy-event-thread)
(:modifiers . 1)
(:modifier
((:mod-kind . ,jdwp-mod-kind-exception-only)
(:class-pattern . (
(:exception . ,class-id)
(:caught . ,caught)
(:uncaught . ,uncaught)))))
)))
(make-jdi-event-request :virtual-machine vm :data data)))
(defun jdi-event-request-manager-create-break-on-exception-workaround (erm vm class-id caught uncaught)
"It appears that the exception-only modifier does not work, as
the event is never sent. This method generates an event request
that breaks for all exceptions. The handling code must then
check the caught/uncaught properties and if the exception is the
correct class."
(let ((data `((:event-kind . ,jdwp-event-exception)
(:suspend-policy . ,jdwp-suspend-policy-event-thread)
(:modifiers . 0)
)))
(make-jdi-event-request :virtual-machine vm :data data)))
(defstruct jdi-break-on-exception-workaround-data
class-id caught uncaught)
(defun jdi-event-request-enable (er)
(let ((reply (jdwp-send-command (jdi-mirror-jdwp er) "set" (jdi-event-request-data er))))
(jdi-trace "received requestid:%s" (bindat-get-field reply :request-id))
(setf (jdi-event-request-id er) (bindat-get-field reply :request-id))))
(defun jdi-event-request-disable (er)
(jdwp-send-command (jdi-mirror-jdwp er) "clear" `((:event . ,jdwp-event-breakpoint) (:request-id . ,(jdi-event-request-id er))))
(jdi-trace "cleared event request"))
(defun jdi-virtual-machine-set-standard-events (vm)
(mapcar (lambda (event)
(jdwp-send-command (jdi-virtual-machine-jdwp vm) "set"
`((:event-kind . ,event)
(:suspend-policy . ,jdwp-suspend-policy-none)
(:modifiers . 0))))
(list jdwp-event-class-unload
jdwp-event-thread-start
jdwp-event-thread-end
jdwp-event-vm-death)))
(defun jdi-virtual-machine-connect (vm)
"[ASYNC] returns t if success, nil on failure"
(when (jdwp-connect (jdi-virtual-machine-jdwp vm) (jdi-virtual-machine-host vm) (jdi-virtual-machine-port vm))
(jdwp-put (jdi-virtual-machine-jdwp vm) 'jdi-virtual-machine vm)
(let ((reply (jdwp-send-command (jdi-virtual-machine-jdwp vm) "version" nil)))
(setf (jdi-virtual-machine-description vm) (jdwp-get-string reply :description)
(jdi-virtual-machine-jdwp-major vm) (bindat-get-field reply :jdwp-major)
(jdi-virtual-machine-jdwp-minor vm) (bindat-get-field reply :jdwp-minor)
(jdi-virtual-machine-version vm) (jdwp-get-string reply :vm-version)
(jdi-virtual-machine-name vm) (jdwp-get-string reply :vm-name))
(jdi-trace "description: \n%s" (jdwp-get-string reply :description))
(jdi-trace "major : %s" (bindat-get-field reply :jdwp-major))
(jdi-trace "minor : %s" (bindat-get-field reply :jdwp-minor))
(jdi-trace "version : %s" (jdwp-get-string reply :vm-version))
(jdi-trace "name : %s" (jdwp-get-string reply :vm-name))
(let ((reply (jdwp-send-command (jdi-virtual-machine-jdwp vm) "capabilities-new" nil)))
(jdi-trace "capabilities-new:%s" reply)
(jdi-virtual-machine-set-standard-events vm)
t))))
(defun jdi-virtual-machine-get-threads (vm)
(jdi-debug "jdi-virtual-machine-get-threads")
(let ((reply (jdwp-send-command (jdi-virtual-machine-jdwp vm) "all-threads" nil)))
(jdi-debug "number of threads:%s" (bindat-get-field reply :threads))
(loop for thread in (bindat-get-field reply :thread)
collect (jdi-virtual-machine-get-object-create
vm
(make-jdi-thread :id (bindat-get-field thread :id))))))
(defun jdi-virtual-machine-get-top-level-thread-groups (vm)
(jdi-debug "jdi-virtual-machine-get-top-level-thread-groups")
(let ((reply (jdwp-send-command (jdi-virtual-machine-jdwp vm) "top-level-thread-groups" nil)))
(jdi-debug "jdi-virtual-machine-get-top-level-thread-groups:number = %s" (bindat-get-field reply :groups))
(loop for group in (bindat-get-field reply :group)
collect (jdi-virtual-machine-get-object-create vm (make-jdi-thread-group :id (bindat-get-field group :id))))))
(defun jdi-virtual-machine-get-all-thread-groups (vm)
(jdi-debug "jdi-virtual-machine-get-all-thread-groups")
(jdi-virtual-machine-get-top-level-thread-groups vm))
(defun jdi-virtual-machine-get-classes-by-signature (vm signature)
(jdi-debug "jdi-virtual-machine-get-classes-by-signature:%s" signature)
(let ((reply (jdwp-send-command (jdi-virtual-machine-jdwp vm)
"classes-by-signature"
`((:signature . ((:length . ,(length signature))
(:string . ,signature)))))))
(jdi-debug "number of classes matched:%s" (bindat-get-field reply :classes))
(loop for class in (bindat-get-field reply :class)
for type-id = (bindat-get-field class :type-id)
for ref-type-tag = (bindat-get-field class :ref-type-tag)
for status = (bindat-get-field class :status)
for newclass = (jdi-virtual-machine-get-class-create vm type-id
:signature signature
:ref-type-tag ref-type-tag
:status status)
collect newclass)))
(defun jdi-virtual-machine-get-suspended-threads (vm)
(jdi-debug "jdi-virtual-machine-suspended-threads")
(let* ((threads (jdi-virtual-machine-get-threads vm))
(suspendeds (mapcar 'jdi-thread-get-suspended-p threads)))
(loop for suspended in suspendeds
for thread in threads
if suspended collect thread)))
(defun* jdi-virtual-machine-get-class-create (vm id &key signature ref-type-tag status)
(let ((found (gethash id (jdi-virtual-machine-classes vm))))
(jdi-debug "jdi-virtual-machine-get-class-create id=%s: %s" id (if found "found" "not found"))
(if found
(progn
(if signature (setf (jdi-class-signature found) signature))
(if ref-type-tag (setf (jdi-class-ref-type-tag found) ref-type-tag))
(if status (setf (jdi-class-status found)) status))
(puthash id (setq found (make-jdi-class :virtual-machine vm
:id id
:signature signature
:ref-type-tag ref-type-tag
:status status))
(jdi-virtual-machine-classes vm)))
found))
(defun jdi-virtual-machine-get-object-create (vm newobj)
(jdi-debug "jdi-virtual-machine-get-object-create:%s" (jdi-object-id newobj))
(let ((found (gethash (jdi-object-id newobj) (jdi-virtual-machine-objects vm))))
(jdi-debug "jdi-virtual-machine-get-object-create:%s" (if found "found" "not found"))
(unless found
(setf (jdi-mirror-virtual-machine newobj) vm)
(puthash (jdi-object-id newobj) (setq found newobj) (jdi-virtual-machine-objects vm)))
found))
(defun jdi-thread-get-name (thread)
(jdi-debug "jdi-thread-get-name")
(jdwp-get-string (jdwp-send-command (jdi-mirror-jdwp thread) "thread-name" `((:thread . ,(jdi-thread-id thread)))) :thread-name))
(defun jdi-thread-update-status (thread)
"Query the virtual machine to get an up to date status on the thread since the cached value can get stale"
(jdi-debug "jdi-thread-update-status")
(multiple-value-bind (status suspend-status) (jdi-thread-get-status thread)
(jdi-debug "thread status=%s suspend-status=%s jdwp-susp=%s " status suspend-status jdwp-suspend-status-suspended)
(setf (jdi-thread-running-p thread) (not (= suspend-status jdwp-suspend-status-suspended)))))
(defun jdi-thread-get-status (thread)
(jdi-debug "jdi-thread-get-status")
(let ((reply (jdwp-send-command (jdi-mirror-jdwp thread) "thread-status" `((:thread . ,(jdi-thread-id thread))))))
(jdi-trace "reply=%s" reply)
(values (bindat-get-field reply :thread-status)
(bindat-get-field reply :suspend-status))))
;; a symbol that we use to mean that we have resolved
;; the value and the result is nil
(defvar jdi-cache-nil nil)
(defmacro jdi-with-cache (acc obj form)
`(cond ((null (,acc ,obj))
(setf (,acc ,obj)
,form)
(if (,acc ,obj)
(,acc ,obj)
(setf (,acc ,obj) 'jdi-cache-nil)
nil))
((equal (,acc ,obj) 'jdi-cache-nil)
nil)
(t (,acc ,obj))))
(defun jdi-thread-get-thread-group (thread)
(jdi-debug "jdi-thread-get-thread-group:%s" (jdi-thread-id thread))
(jdi-with-cache jdi-thread-thread-group-cache thread
(let ((reply (jdwp-send-command (jdi-mirror-jdwp thread) "thread-group" `((:thread . ,(jdi-thread-id thread))))))
(jdi-debug "jdi-thread-get-thread-group:group=%s" (bindat-get-field reply :group))
(jdi-virtual-machine-get-object-create
(jdi-mirror-virtual-machine thread)
(make-jdi-thread-group :id (bindat-get-field reply :group))))))
(defun jdi-thread-get-system-thread-p (thread)
(jdi-debug "jdi-thread-get-system-thread-p:%s" (jdi-thread-id thread))
(jdi-with-cache jdi-thread-system-thread-p-cache thread
(let ((group (jdi-thread-get-thread-group thread)))
(jdi-thread-group-get-system-thread-p group))))
(defun jdi-thread-get-daemon-p (thread)
(jdi-debug "jdi-thread-get-daemon-p:%s" (jdi-thread-id thread))
(jdi-with-cache jdi-thread-daemon-p-cache thread
(let* ((reference-type (jdi-object-get-reference-type thread))
(field (jdi-reference-type-get-field-by-name reference-type "daemon")))
(if (null field)
(setq field (jdi-reference-type-get-field-by-name reference-type "isDaemon")))
(if field
(if (string= (car (jdi-jni-to-print (jdi-field-signature field))) "boolean")
(not (= 0 (jdi-primitive-value-value (jdi-object-get-value thread field)))))))))
(defun jdi-thread-group-get-system-thread-p (thread-group)
(jdi-debug "jdi-thread-group-get-system-thread-p:%s" (jdi-thread-group-id thread-group))
(let ((name (jdi-thread-group-get-name thread-group)))
(let ((parent (jdi-thread-group-get-parent thread-group)))
(if (string= name "main")
nil
(if parent
(jdi-thread-group-get-system-thread-p parent)
t)))))
(defun jdi-thread-group-get-name (thread-group)
(jdi-debug "jdi-thread-group-get-name:%s" (jdi-thread-group-id thread-group))
(if (jdi-thread-group-name thread-group)
(jdi-thread-group-name thread-group)
(let ((reply (jdwp-send-command (jdi-mirror-jdwp thread-group) "thread-group-name" `((:group . ,(jdi-thread-group-id thread-group))))))
(setf (jdi-thread-group-name thread-group) (jdwp-get-string reply :group-name)))))
(defun jdi-thread-group-get-parent (thread-group)
(jdi-debug "jdi-thread-group-get-parent:%s" (jdi-thread-group-id thread-group))
(if (jdi-thread-group-parent thread-group)
(if (equal (jdi-thread-group-parent thread-group) t)
nil
(jdi-thread-group-parent thread-group))
(let ((reply (jdwp-send-command (jdi-mirror-jdwp thread-group) "thread-group-parent" `((:group . ,(jdi-thread-group-id thread-group))))))
(let ((group-id (bindat-get-field reply :parent-group)))
(if (equal group-id [0 0 0 0 0 0 0 0])
(progn
(setf (jdi-thread-group-parent thread-group) t)
nil)
(setf (jdi-thread-group-parent thread-group) (jdi-virtual-machine-get-object-create
(jdi-mirror-virtual-machine thread-group)
(make-jdi-thread-group :id (bindat-get-field reply :parent-group)))))))))
(defun jdi-thread-group-get-children (thread-group)
(jdi-debug "jdi-thread-group-get-children:%s" (jdi-thread-group-id thread-group))
(let ((reply (jdwp-send-command (jdi-mirror-jdwp thread-group) "thread-group-children" `((:group . ,(jdi-thread-group-id thread-group))))))
(setf (jdi-thread-group-child-threads thread-group)
(loop for thread in (bindat-get-field reply :child-thread)
collect (jdi-virtual-machine-get-object-create (jdi-mirror-virtual-machine thread-group)
(make-jdi-thread :id (bindat-get-field thread :child-thread)))))
(setf (jdi-thread-group-child-groups thread-group)
(loop for group in (bindat-get-field reply :child-group)
collect (jdi-virtual-machine-get-object-create (jdi-mirror-virtual-machine thread-group)
(make-jdi-thread-group :id (bindat-get-field group :child-group)))))
(values (jdi-thread-group-child-threads thread-group) (jdi-thread-group-child-groups thread-group))))
(defun jdi-thread-group-get-thread-groups (thread-group)
(jdi-debug "jdi-thread-group-get-thread-groups:%s" (jdi-thread-group-id thread-group))
(multiple-value-bind (child-threads child-groups) (jdi-thread-group-get-children thread-group)
child-groups))
(defun jdi-thread-group-get-threads (thread-group)
(jdi-debug "jdi-thread-group-get-threads:%s" (jdi-thread-group-id thread-group))
(multiple-value-bind (child-threads child-groups) (jdi-thread-group-get-children thread-group)
child-groups))
(defun jdi-virtual-machine-disconnect (vm)
(jdwp-disconnect (jdi-virtual-machine-jdwp vm)))
(defun jdi-virtual-machine-exit (vm exit-code)
(jdwp-exit (jdi-virtual-machine-jdwp vm) `((:exit-code . ,exit-code))))
(defun jdi-class-get-all-methods (class)
(jdi-debug "jdi-class-get-all-methods")
(let ((supers (jdi-class-get-all-super class)))
(jdi-mappend 'jdi-class-get-methods (cons class supers))))
(defun jdi-class-get-methods (class)
"returns a list of jdi-method in the class"
(jdi-debug "jdi-class-get-methods:%s" (jdi-class-id class))
(if (jdi-class-methods class)
(jdi-class-methods class)
(let ((reply (jdwp-send-command (jdi-mirror-jdwp class)
(jdi-get-methods-command-name class)
`((:ref-type . ,(jdi-class-id class))))))
(jdi-debug "number of methods:%s" (bindat-get-field reply :methods))
(setf (jdi-class-methods class)
(loop for method in (bindat-get-field reply :method)
collect (make-jdi-method :virtual-machine (jdi-mirror-virtual-machine class)
:class class
:id (bindat-get-field method :method-id)
:name (jdwp-get-string method :name)
:signature (jdwp-get-string method :signature)
:mod-bits (bindat-get-field method :mod-bits))
do (jdi-debug "jdi-class-get-methods:name=%s:signature=%s" (jdwp-get-string method :name) (jdwp-get-string method :signature))))
(jdi-class-methods class))))
(defun jdi-class-get-signature (class)
(jdi-debug "jdi-class-get-signature")
(if (jdi-class-signature class)
(jdi-class-signature class)
(let ((reply (jdwp-send-command (jdi-mirror-jdwp class)
(if (jdi-virtual-machine-has-generic-p
(jdi-mirror-virtual-machine class))
"signature-with-generic"
"signature")
`((:ref-type . ,(jdi-class-id class))))))
(setf (jdi-class-generic-signature class) (jdwp-get-string reply :generic-signature)
(jdi-class-signature class) (jdwp-get-string reply :signature)))))
(defun jdi-class-get-signature-by-id (vm class-id)
(let ((class (jdi-virtual-machine-get-class-create vm class-id)))
(jdi-class-get-signature class)))
(defun jdi-class-get-generic-signature (class)
(jdi-debug "jdi-class-get-generic-signature")
(when (jdi-virtual-machine-has-generic-p (jdi-mirror-virtual-machine class))
(if (jdi-class-generic-signature class)
(jdi-class-generic-signature class)
(let ((reply (jdwp-send-command (jdi-mirror-jdwp class) "signature-with-generic" `((:ref-type . ,(jdi-class-id class))))))
(setf (jdi-class-signature class) (jdwp-get-string reply :signature)
(jdi-class-generic-signature class) (jdwp-get-string reply :generic-signature))))))
(defcustom jdi-class-nested-classes-workaround t
"The default 1.6 JVM will not return anonymous nested classes.
Among other things, this can break setting breakpoints in such
classes. If this is true, an alternate implementation is used
that works around this problem. It is possible for the
workaround to return incorrect values is classes are created that
have $ in the class name but are not inner classes."
:type 'boolean
:group 'jdibug)
(defun jdi-class-get-nested-classes (class)
"Return the (possibly cached) list of nested classes for this class. Note: this is not implemented in the default 1.6 JVM for anonymous classes. As a workaround, if `jdi-class-nested-classes-workaround' is non-nil, we use a workaround to query for all classes and match against the names to determine the direct nested classes."
(jdi-debug "jdi-class-get-nested-classes")
(or (jdi-class-nested-classes class)
(setf (jdi-class-nested-classes class)
(if jdi-class-nested-classes-workaround
(jdi-fetch-nested-classes-workaround class)
(jdi-fetch-nested-classes class)))
;; TODO: create a class-prepare event to update the list of
;; nested classes if additional nested classes are loaded.
))
(defun jdi-fetch-nested-classes-workaround (class)
"Get the nested classes of CLASS by fetching all the classes,
then filter out any that do not match the signature of a nested
class of CLASS. This works around the bug in JDWP that does not
return anonymous nested classes when asking for nested classes.
It is probably very slow."
(let* ((vm (jdi-mirror-virtual-machine class))
(candidate-classes (jdi-get-all-classes vm))
(signature (jdi-class-get-signature class))
(sig-less-1 (substring signature 0 (1- (length signature))))
(inner-sig-re (concat "^"
(regexp-quote sig-less-1)
"\\$[^$]+;$")))
(jdi-debug "Accepting classes of the form %s" inner-sig-re)
(loop for nested in candidate-classes
for nested-sig = (jdi-class-get-signature nested)
if (string-match inner-sig-re nested-sig)
collect nested)))
(defun jdi-get-all-classes (vm)
"Get all classes from a VM. This is probably very slow and should not be used."
(jdi-debug "jdi-get-all-classes")
(let* ((jdwp (jdi-virtual-machine-jdwp vm))
(reply (jdwp-send-command jdwp "all-classes" nil)))
(jdi-debug "number of classes matched:%s" (bindat-get-field reply :classes))
(loop for class in (bindat-get-field reply :class)
for type-id = (bindat-get-field class :type-id)
for ref-type-tag = (bindat-get-field class :ref-type-tag)
for status = (bindat-get-field class :status)
for signature = (jdwp-get-string class :signature)
for newclass = (jdi-virtual-machine-get-class-create vm type-id
:signature signature
:ref-type-tag ref-type-tag
:status status)
collect newclass)))
(defun jdi-fetch-nested-classes (class)
"Get the nested classes of CLASS by fetching the classes using
the nested-classes command."
(let* ((jdwp (jdi-mirror-jdwp class))
(reply (jdwp-send-command jdwp "nested-types"
`((:ref-type . ,(jdi-class-id class))))))
(loop with vm = (jdi-class-virtual-machine class)
for nested in (bindat-get-field reply :nested)
for id = (bindat-get-field nested :id)
for new-class = (jdi-virtual-machine-get-class-create vm id)
collect new-class
do (jdi-debug "jdi-class-get-nested-classes:id=%s" id))))
(defun jdi-method-get-signature (method)
(jdi-debug "jdi-method-get-signature")
(if (jdi-method-signature method)
(jdi-method-signature method)
(setf (jdi-method-signature method)
(jdi-method-signature (find-if (lambda (obj)
(equal (jdi-method-id method) (jdi-method-id obj)))
(jdi-class-get-methods (jdi-method-class method)))))))
(defun jdi-method-get-locations (method)
(jdi-debug "jdi-method-get-locations:class=%s method=%s" (jdi-class-signature (jdi-method-class method)) (jdi-method-name method))
(if (or (jdi-method-locations method)
(jdi-method-native-p method))
(jdi-method-locations method)
(let ((reply (jdwp-send-command
(jdi-mirror-jdwp method) "line-table" `((:ref-type . ,(jdi-class-id (jdi-method-class method)))
(:method-id . ,(jdi-method-id method))))))
(jdi-trace "start=%s:end=%s:lines=%s"
(jdwp-string-to-hex (bindat-get-field reply :start))
(jdwp-string-to-hex (bindat-get-field reply :end))
(bindat-get-field reply :lines))
(setf (jdi-method-locations method)
(loop for line in (bindat-get-field reply :line)
collect (make-jdi-location :virtual-machine (jdi-mirror-virtual-machine method)
:class (jdi-method-class method)
:method method
:line-code-index (bindat-get-field line :line-code-index)
:line-number (bindat-get-field line :line-number))))
(dolist (location (jdi-method-locations method))
(jdi-trace "line-number:%s line-code-index:%s" (jdi-location-line-number location) (jdi-location-line-code-index location)))
(jdi-method-locations method))))
(defun jdi-method-get-name (method)
(jdi-debug "jdi-method-get-signature")
(let ((methods (jdi-class-get-methods (jdi-method-class method))))
(jdi-method-name (find-if (lambda (obj)
(equal (jdi-method-id obj)
(jdi-method-id method)))
methods))))
(defun jdi-virtual-machine-set-breakpoint (vm signature line)
"Set breakpoint and return a list of jdi-event-request"
(jdi-debug "jdi-virtual-machine-set-breakpoint:signature=%s:line=%s" signature line)
(let* ((classes (jdi-virtual-machine-get-classes-by-signature vm signature))
(result (jdi-virtual-machine-set-breakpoint-1 vm classes line))
(request-manager (jdi-virtual-machine-event-request-manager vm))
(class-name (car (jdi-jni-to-print signature)))
;; the class might be loaded again by another class loader
;; so we install the class prepare event anyway.
(er (jdi-event-request-manager-create-class-prepare
request-manager vm class-name)))
(jdi-event-request-enable er)
;; Install a handler for inner classes if we didn't set the
;; breakpoint, since that might be what the problem was
(unless result
(setq er (jdi-event-request-manager-create-class-prepare
request-manager vm (concat class-name "$*")))
(jdi-event-request-enable er))
result))
(defun jdi-virtual-machine-set-breakpoint-1 (vm classes line)
"Attempt to set a breakpoint in CLASSES and all inner classes at LINE"
(if classes
(let (found result)
(dolist (class classes)
;; If we found the breakpoint in one class, we can skip the
;; rest of the iterations (which should be over other inner
;; classes)
(jdi-debug "jdi-virtual-machine-set-breakpoint-1 checking %s" (jdi-class-signature class))
(unless found
(dolist (location (jdi-class-get-locations-of-line class line))
(setq found t)
(let ((er (jdi-event-request-manager-create-breakpoint
(jdi-virtual-machine-event-request-manager vm)
location)))
(jdi-event-request-enable er)
(push er result)))
;; If we did not find the breakpoint, look in inner classes.
(unless found
(setq result (append
(jdi-virtual-machine-set-breakpoint-1
vm
(jdi-class-get-nested-classes class)
line)
result)))))
result)))
(defcustom jdi-break-on-exception-workaround t
"Apply a workaround to an apparent bug which causes the JVM to
not report exceptions if they are restricted to a specific
class."
:type 'boolean
:group 'jdibug)
(defun jdi-virtual-machine-set-break-on-exception (vm signature caught uncaught)
"Set break for when an exception is thrown and return a list of
jdi-event-request. SIGNATURE should be a JNI format
signature (e.g., Ljava/lang/RuntimeException not
java.lang.RuntimeException.
Returns a list of event requests sent to the VM. This may be nil
if no class matching SIGNATURE is found."
(jdi-debug "jdi-virtual-machine-set-break-on-exception:signature=%s:caught=%s:uncaught=%s"
signature caught uncaught)
(let (result
(classes (jdi-virtual-machine-get-classes-by-signature vm signature)))
(dolist (class classes)
(if (jdi-class-instance-of-p
class
(jdi-class-name-to-class-signature "java.lang.Throwable"))
(progn
;; Create the event request and enable it.
(let* ((class-id (jdi-class-id class))
(erm (jdi-virtual-machine-event-request-manager vm))
;; Form of the event request depends on if the
;; workaround is enabled.
(er (funcall (if jdi-break-on-exception-workaround
'jdi-event-request-manager-create-break-on-exception-workaround
'jdi-event-request-manager-create-break-on-exception)
erm vm class-id caught uncaught))
(request-id (jdi-event-request-enable er)))
;; Store the data for the exception workaround. Always
;; store it in case the user turns on the workaround
;; later.
(puthash request-id (make-jdi-break-on-exception-workaround-data
:class-id class-id
:caught caught
:uncaught uncaught)
(jdi-virtual-machine-break-on-exception-data vm))
(push er result)))
(jdi-warn "%s is not a subclass of Throwable. Not setting break when thrown."
(jdi-jni-to-print (jdi-class-get-signature class)))))
;; the class might be loaded again by another class loader
;; so we install the class prepare event anyway
(let ((er (jdi-event-request-manager-create-class-prepare
(jdi-virtual-machine-event-request-manager vm)
vm
(car (jdi-jni-to-print signature)))))
(jdi-event-request-enable er))
;; Return event requests.
result))
(defun jdi-event-request-manager-create-inner-class-prepare (class)
"Request prepare events for the inner classes of CLASS"
(jdi-debug "jdi-event-request-manager-create-inner-class-prepare %s" (jdi-class-signature class))
(let* ((vm (jdi-mirror-virtual-machine class))
(signature (jdi-class-signature class))
(er (jdi-event-request-manager-create-class-prepare
(jdi-virtual-machine-event-request-manager vm)
vm
(concat (car (jdi-jni-to-print signature)) "$*"))))
er))
(defun jdi-virtual-machine-has-generic-p (vm)
(>= (jdi-virtual-machine-jdwp-minor vm) 5))
(defun jdi-method-location-by-line-code-index (method line-code-index)
(jdi-trace "jdi-method-location-by-line-code-index:%s:%s:%s" (jdi-method-name method) line-code-index (length (jdi-method-locations method)))
(if jdi-trace-flag
(loop for loc in (jdi-method-locations method)
do
(jdi-trace "line-code-index:%s line-number:%s" (jdi-location-line-code-index loc) (jdi-location-line-number loc))))
(let ((result))
(loop for loc in (jdi-method-locations method)
while (>= (jdwp-vec-to-int line-code-index)
(jdwp-vec-to-int (jdi-location-line-code-index loc)))
do (setq result loc))
(if (null result)
(setq result (car (last (jdi-method-locations method)))))
(if result
(jdi-trace "found at line-number:%s" (jdi-location-line-number result)))
result))
(defun jdi-class-get-locations-of-line (class line-number)
(jdi-debug "jdi-class-get-locations-of-line")
(let ((result))
(dolist (method (jdi-class-get-methods class))
(dolist (location (jdi-method-get-locations method))
(if (equal line-number (jdi-location-line-number location))
(push location result))))
result))
(defun jdi-class-find-location (class method-id line-code-index)
(jdi-debug "jdi-class-find-location:number of methods=%s" (length (jdi-class-methods class)))
(let* ((method (find-if (lambda (method)
(equal (jdi-method-id method) method-id))
(jdi-class-methods class)))
(location (if method (jdi-method-location-by-line-code-index method line-code-index))))
(if location
location
(jdi-error "failed to look line-code-index %s in class %s" line-code-index (jdi-class-name class)))))
(defun jdi-location-get-line-number (location)
(jdi-debug "jdi-location-get-line-number:wanted-line-code-index=%s" (jdi-location-line-code-index location))
(let ((methods (jdi-class-get-methods (jdi-location-class location))))
(jdi-debug "jdi-location-get-line-number: methods=%s" (length methods))
(setf (jdi-location-method location)
(find-if (lambda (obj)
(equal (jdi-method-id obj)
(jdi-method-id (jdi-location-method location))))
methods))
(let ((locations (jdi-method-get-locations (jdi-location-method location))))
(jdi-debug "jdi-location-get-line-number: locations=%s" (length locations))
(let ((found))
(loop for loc in locations
while (>= (jdwp-vec-to-int (jdi-location-line-code-index location))
(jdwp-vec-to-int (jdi-location-line-code-index loc)))
do (setq found loc))
(jdi-debug "jdi-location-get-line-number:found=%s" (if found "yes" "no"))
(if found (setf (jdi-location-line-number location) (jdi-location-line-number found)))
(jdi-location-line-number location)))))
(defun jdi-thread-get-frames (thread)
(jdi-debug "jdi-thread-get-frames")
(let ((reply (jdwp-send-command (jdi-mirror-jdwp thread) "frame-count" `((:thread . ,(jdi-thread-id thread))))))
(jdi-debug "number of frames:%s" (bindat-get-field reply :frame-count))
(let ((reply (jdwp-send-command (jdi-mirror-jdwp thread) "frames" `((:thread . ,(jdi-thread-id thread))
(:start-frame . 0)
(:length . ,(bindat-get-field reply :frame-count))))))
(loop for frame in (bindat-get-field reply :frame)
for class-id = (bindat-get-field frame :location :class-id)
for method-id = (bindat-get-field frame :location :method-id)
for type = (bindat-get-field frame :location :type)
for line-code-index = (bindat-get-field frame :location :index)
for class = (jdi-virtual-machine-get-class-create (jdi-mirror-virtual-machine thread) class-id)
for method = (make-jdi-method :virtual-machine (jdi-mirror-virtual-machine thread)
:id method-id
:class class)
for location = (make-jdi-location :virtual-machine (jdi-mirror-virtual-machine thread)
:class class
:method method
:type type
:line-code-index line-code-index)
collect (make-jdi-frame :virtual-machine (jdi-mirror-virtual-machine thread)
:thread thread
:location location
:id (bindat-get-field frame :id))))))
(defun jdi-thread-get-suspended-p (thread)
(jdi-debug "jdi-thread-get-suspended-p")
(multiple-value-bind (status suspend-status) (jdi-thread-get-status thread)
(= suspend-status jdwp-suspend-status-suspended)))
(defun jdi-thread-get-suspend-count (thread)
(jdi-trace "jdi-thread-get-suspend-count")
(let ((reply (jdwp-send-command (jdi-mirror-jdwp thread)
"suspend-count"
`((:thread . ,(jdi-thread-id thread))))))
(bindat-get-field reply :suspend-count)))
(defun jdi-thread-resume (thread)
(jdi-debug "jdi-thread-resume")
(setf (jdi-thread-running-p thread) t)
(jdwp-send-command (jdi-mirror-jdwp thread) "thread-resume" `((:thread . ,(jdi-thread-id thread)))))
(defun jdi-thread-suspend (thread)
(jdi-debug "jdi-thread-suspend")
(setf (jdi-thread-running-p thread) nil)
(jdwp-send-command (jdi-mirror-jdwp thread) "thread-suspend" `((:thread . ,(jdi-thread-id thread)))))
(defun jdi-resume (vm)
(jdi-debug "jdi-resume")
;; TODO: We should update jdi-thread-running-p, but I don't think we have
;; a good way of getting all thread threads in the vm
(jdwp-send-command (jdi-virtual-machine-jdwp vm) "resume" nil))
(defun jdi-thread-send-step (thread depth)
(jdi-debug "jdi-thread-send-step:depth=%s" depth)
(setf (jdi-thread-running-p thread) t)
(let* ((vm (jdi-mirror-virtual-machine thread))
(erm (jdi-virtual-machine-event-request-manager vm))
(er (jdi-event-request-manager-create-step erm thread depth)))
(jdi-event-request-enable er)
(jdi-thread-resume thread)))
(defun jdi-variable-sigbyte (variable)
(string-to-char (jdi-variable-signature variable)))
(defun jdi-method-get-variables (method)
(jdi-debug "jdi-method-get-variables")
(if (jdi-method-variables method)
(jdi-method-variables method)
(let ((reply (jdwp-send-command (jdi-mirror-jdwp method)
(jdi-get-variable-table-command-name method)
`((:ref-type . ,(jdi-class-id (jdi-method-class method)))
(:method-id . ,(jdi-method-id method))))))
(jdi-trace "variable-table arg-count:%s slots:%s" (bindat-get-field reply :arg-cnt) (bindat-get-field reply :slots))
(setf (jdi-method-variables method)
(loop for slot in (bindat-get-field reply :slot)
for code-index = (jdwp-get-int slot :code-index)
for name = (jdwp-get-string slot :name)
for signature = (jdwp-get-string slot :signature)
for length = (bindat-get-field slot :length)
for slot2 = (bindat-get-field slot :slot)
do (jdi-trace "slot:%s code-index:%s length:%s name:%s signature:%s generic-signature:%s"
slot2 code-index length name signature nil)
collect (make-jdi-variable :virtual-machine (jdi-mirror-virtual-machine method)
:code-index code-index
:name name
:signature signature
:length length
:slot slot2)))
(jdi-method-variables method))))
(defun jdi-frame-get-visible-variables (frame)
(jdi-debug "jdi-frame-get-visible-variables")
;; we don't cache the variables as the location within the same frame might change
(let ((variables (jdi-method-get-variables (jdi-location-method (jdi-frame-location frame)))))
(loop for variable in variables
for line-code = (jdwp-vec-to-int (jdi-location-line-code-index (jdi-frame-location frame)))
if (and (>= line-code (jdi-variable-code-index variable))
(< line-code (+ (jdi-variable-code-index variable) (jdi-variable-length variable))))
collect variable)))
(defun jdi-frame-get-values (frame variables)
(jdi-debug "jdi-frame-get-values")
(let ((data `((:thread . ,(jdi-thread-id (jdi-frame-thread frame)))
(:frame . ,(jdi-frame-id frame))
(:slots . ,(length variables))
,(nconc (list :slot)
(mapcar (lambda (variable)
`((:slot . ,(jdi-variable-slot variable))
(:sigbyte . ,(jdi-variable-sigbyte variable))))
variables)))))
(let ((reply (jdwp-send-command
(jdi-mirror-jdwp frame)
"stack-frame-get-values"
data)))
(loop for variable in variables
for value in (bindat-get-field reply :value)
collect (jdi-virtual-machine-get-value-create (jdi-mirror-virtual-machine frame)
(bindat-get-field value :slot-value :type)
(bindat-get-field value :slot-value :u :value))))))
(defun jdi-frame-get-this-object (frame)
(jdi-debug "jdi-frame-get-this-object")
(let ((reply (jdwp-send-command (jdi-mirror-jdwp frame) "stack-frame-this-object" `((:thread . ,(jdi-thread-id (jdi-frame-thread frame)))
(:frame . ,(jdi-frame-id frame))))))
(jdi-virtual-machine-get-value-create (jdi-mirror-virtual-machine frame)
(bindat-get-field reply :object-this :type)
(bindat-get-field reply :object-this :u :value))))
(defun jdi-virtual-machine-get-value-create (vm type value)
(cond ((member type (list jdwp-tag-boolean
jdwp-tag-byte