File: Debugee.java

package info (click to toggle)
openjdk-11 11.0.16%2B8-1~deb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 784,576 kB
  • sloc: java: 5,128,021; xml: 1,192,224; cpp: 1,124,021; ansic: 422,433; javascript: 155,577; sh: 17,084; objc: 13,327; python: 4,522; asm: 3,570; makefile: 2,858; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (1659 lines) | stat: -rw-r--r-- 64,854 bytes parent folder | download | duplicates (10)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
/*
 * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package nsk.share.jdwp;

import nsk.share.*;
import nsk.share.jpda.*;

import java.util.*;
import java.io.*;

/**
 * This class is used to interact with debugee VM using JDWP features.
 * <p>
 * This class is an mirror of debugee VM that is constructed by
 * <code>Binder</code> and uses <code>Transport</code> object
 * to interact with debugee VM.
 * <p>
 * In addition to the general abities to control of debugee VM process,
 * provided by the base class <code>DebugeeProcess</code>, this class
 * adds some service methods that uses JDWP protocol to simplify interaction
 * with debugee VM (such as finding classes, setting breakpoints,
 * handling events, and so on.).
 *
 * @see Binder
 * @see Transport
 * @see DebugeeProcess
 */
abstract public class Debugee extends DebugeeProcess {

    /** Binder that creates this debugee. */
    protected Binder binder = null;

    protected LinkedList<EventPacket> eventQueue = new LinkedList<EventPacket>();

    protected Transport transport = null;

    /** Make new <code>Debugee</code> object for the given binder. */
    protected Debugee (Binder binder) {
        super(binder);
        this.argumentHandler = binder.getArgumentHandler();
        this.binder = binder;
        prefix = "Debugee> ";
    }

    /** Return <code>Binder</code> of the debugee object. */
    public Binder getBinder() {
        return binder;
    }

    /** Return <code>Transport</code> of the debugee object. */
    public Transport getTransport() {
        return transport;
    }

    /**
     * Prepare transport object for establishing connection.
     * This may change connection options in <code>argumentHandler</code>.
     *
     * @return specific address string if listening has started or null otherwise
     */
    public String prepareTransport(ArgumentHandler argumentHandler) {
        String address = null;
        try {
            if (argumentHandler.isSocketTransport()) {
                SocketTransport socket_transport = new SocketTransport(log);
                if (argumentHandler.isListeningConnector()) {
                    int port = 0;
                    if (argumentHandler.isTransportAddressDynamic()) {
                        port = socket_transport.bind(0);
//                        argumentHandler.setTransportPortNumber(port);
                    } else {
                        port = argumentHandler.getTransportPortNumber();
                        socket_transport.bind(port);
                    }
                    address = argumentHandler.getTestHost() + ":" + port;
                }
                transport = socket_transport;
/*
            } else if (argumentHandler.isShmemTransport()) {
                ShmemTransport shmem_transport = new ShmemTransport(log);
                if (argumentHandler.isListeningConnector()) {
                    String sharedName = agrHandler.getTransportSharedName();
                    shmem_transport.bind(sharedName);
                    address = sharedName;
                }
                transport = shmem_transport;
 */
            } else {
               throw new TestBug("Unexpected transport type: "
                               + argumentHandler.getTransportType());
            }

        } catch (IOException e) {
            e.printStackTrace(log.getOutStream());
            throw new Failure("Caught IOException while preparing for JDWP transport connection:\n\t"
                    + e);
        }

        return address;
    }

    /**
     * Establish connection to debugee VM.
     */
    public Transport connect() {
        if (transport == null) {
            throw new Failure("Attemt to establish JDWP connection for not prepared transport");
        }

        try {
            if (argumentHandler.isSocketTransport()) {
                display("Establishing JDWP socket connection");
                SocketTransport socket_transport = (SocketTransport)transport;
                int transportPort = argumentHandler.getTransportPortNumber();
                if (argumentHandler.isAttachingConnector()) {
                    String debugeeHost = argumentHandler.getDebugeeHost();
                    display("Attaching to debugee: " + debugeeHost + ":" + transportPort);
                    socket_transport.attach(debugeeHost, transportPort);
                } else if (argumentHandler.isListeningConnector()) {
                    display("Listening from debugee");
                    socket_transport.accept();
                } else {
                    throw new TestBug("Unexpected connector type: "
                                    + argumentHandler.getConnectorType());
                }
/*
            } else if (argumentHandler.isShmemTransport()) {
                display("Establishing JDWP shared-memory connection");
                ShmemTransport shmem_transport = (ShmemTransport)transport;
                String sharedName = argumentHandler.getTransportSharedName();
                if (argumentHandler.isAttachingConnector()) {
                    display("Attaching to debugee: " + sharedName);
                    shmem_transport.attach(sharedName);
                } else if (argumentHandler.isListeningConnector()) {
                    display("Listening from debugee");
                    shmem_transport.accept();
                } else {
                    throw new TestBug("Unexpected connector type: "
                                    + argumentHandler.getConnectorType());
                }
 */
            } else {
               throw new TestBug("Unexpected transport type: "
                               + argumentHandler.getTransportType());
            }

            transport.handshake();

        } catch (IOException e) {
            e.printStackTrace(log.getOutStream());
            throw new Failure("Caught IOException while establishing JDWP transport connection:\n\t"
                    + e);
        }
        return transport;
    }

    // --------------------------------------------------- //

   /**
    * Waits for VM_INIT event from debugee VM.
    */
    public void waitForVMInit() {
        String eventName = "VirtualMachine.VM_START";
        EventPacket packet = receiveEventFor(JDWP.EventKind.VM_START, eventName);
//        String versionInfo = getVersionInfo();
//        display("Target VM started:\n" + versionInfo);
    }

   /**
    * Waits for VM_DEATH event from debugee VM.
    */
    public void waitForVMDeath() {
        String eventName = "VirtualMachine.VM_DEATH";
        EventPacket packet = receiveEventFor(JDWP.EventKind.VM_DEATH, eventName);
    }

    /**
     * Wait for class loaded on debugee start up and return its classID.
     * Debuggee should be initially suspended and it will also left suspended
     * by the CLASS_PREPARE event request.
     */
    public long waitForClassLoaded(String className, byte suspendPolicy) {
        // make request for CLASS_PREPARE_EVENT for this class name
        int requestID = requestClassPrepareEvent(className, suspendPolicy);
        // resume initially suspended debugee
        resume();
        // wait for CLASS_PREPARE_EVENT
        return waitForClassPrepareEvent(requestID, className);
    }

    /**
     * Wait for classes loaded on debugee start up and return their classIDs.
     * Debuggee should be initially suspended and it will also left suspended
     * by the CLASS_PREPARE event request.
     */
    public long[] waitForClassesLoaded(String classNames[], byte suspendPolicy) {
        int count = classNames.length;

        // make requests for CLASS_PREPARE_EVENT for these class names
        int[] requestIDs = new int[count];
        for (int i = 0; i < count; i++) {
            requestIDs[i] = requestClassPrepareEvent(classNames[i], suspendPolicy);
        }

        // resume initially suspended debugee
        resume();

        return waitForClassPrepareEvents(requestIDs, classNames);
    }

    /**
     * Wait for breakpoint reached and return threadIDs.
     * Debuggee should be initially suspended and it will also left suspended
     * by the BREAKPOINT event request.
     */
    public long waitForBreakpointReached(long classID, String methodName,
                                                int line, byte suspendPolicy) {
        // query debuggee for methodID
        long methodID = getMethodID(classID, methodName, true);
        // create BREAKPOINT event request
        int requestID = requestBreakpointEvent(JDWP.TypeTag.CLASS, classID, methodID,
                                                                line, suspendPolicy);
        // resume initially suspended debugee
        resume();
        // wait for BREAKPOINT event
        return waitForBreakpointEvent(requestID);
    }

    // --------------------------------------------------- //

    /**
     * Wait for CLASS_PREPARE event made by given request received
     * and return classID.
     * Debuggee will be left suspended by the CLASS_PREPARE event.
     */
    public long waitForClassPrepareEvent(int requestID, String className) {
        String error = "Error occured while waiting for CLASS_PREPARE event for class:\n\t"
                        + className;

        String signature = "L" + className.replace('.', '/') + ";";
        long classID = 0;

        // wait for CLASS_PREPARE event
        for(;;) {
            EventPacket event = receiveEvent();
            byte eventSuspendPolicy = 0;
            long eventThreadID = 0;
            try {
                eventSuspendPolicy = event.getByte();
                int events = event.getInt();
                for (int i = 0; i < events; i++) {
                    // check event kind
                    byte eventKind = event.getByte();
                    if (eventKind == JDWP.EventKind.VM_DEATH) {
                        complain("Unexpected VM_DEATH event received: " + eventKind
                                + " (expected: " + JDWP.EventKind.CLASS_PREPARE +")");
                        throw new Failure(error);
                    } else if (eventKind != JDWP.EventKind.CLASS_PREPARE) {
                        complain("Unexpected event kind received: " + eventKind
                                + " (expected: " + JDWP.EventKind.CLASS_PREPARE +")");
                        throw new Failure(error);
                    }

                    // extract CLASS_PREPARE event specific data
                    int eventRequestID = event.getInt();
                    eventThreadID = event.getObjectID();
                    byte eventRefTypeTag = event.getByte();
                    long eventClassID = event.getReferenceTypeID();
                    String eventClassSignature = event.getString();
                    int eventClassStatus = event.getInt();

                    // check if event was single
                    if (events > 1) {
                        complain("Not single CLASS_PREPARE event received for class:\n\t"
                                    + eventClassSignature);
                        throw new Failure(error);
                    }

                    // check if event is for expected class
                    if (eventClassSignature.equals(signature)) {

                        // check if event is because of expected request
                        if (eventRequestID != requestID) {
                            complain("CLASS_PREPARE event with unexpected requestID ("
                                    + eventRequestID + ") received for class:\n\t"
                                    + eventClassSignature);
                            throw new Failure(error);
                        }

                        // remove event request
                        clearEventRequest(JDWP.EventKind.CLASS_PREPARE, requestID);

                        return eventClassID;
                    } else {
                        complain("Unexpected CLASS_PREPARE event received with class signature:\n"
                               + "  " + eventClassSignature);
                    }

                }

            } catch (BoundException e) {
                complain("Unable to extract data from event packet while waiting for CLASS_PREPARE event:\n\t"
                        + e.getMessage() + "\n" + event);
                throw new Failure(error);
            }

            // resume debuggee according to event suspend policy
            resumeEvent(eventSuspendPolicy, eventThreadID);
        }
    }

    /**
     * Wait for CLASS_PREPARE events made by given requests received
     * and return classIDs.
     * Debuggee will be left suspended by the CLASS_PREPARE event.
     */
    public long[] waitForClassPrepareEvents(int requestIDs[], String classNames[]) {
        int count = classNames.length;
        String error = "Error occured while waiting for " + count + " CLASS_PREPARE events";

        // prepare expected class signatures
        String[] signatures = new String[count];
        for (int i = 0; i < count; i++) {
            signatures[i] = "L" + classNames[i].replace('.', '/') + ";";
        }

        // clear list of classIDs
        long[] classIDs = new long[count];
        for (int i = 0; i < count; i++) {
            classIDs[i] = 0;
        }

        // wait for all expected CLASS_PREPARE events
        int received = 0;
        for(;;) {
            EventPacket event = receiveEvent();
            byte eventSuspendPolicy = 0;
            long eventThreadID = 0;
            try {
                eventSuspendPolicy = event.getByte();
                int events = event.getInt();
                for (int i = 0; i < events; i++) {
                    // check event kind
                    byte eventKind = event.getByte();
                    if (eventKind == JDWP.EventKind.VM_DEATH) {
                        complain("Unexpected VM_DEATH event received: " + eventKind
                                + " (expected: " + JDWP.EventKind.CLASS_PREPARE +")");
                        throw new Failure(error);
                    } else if (eventKind != JDWP.EventKind.CLASS_PREPARE) {
                        complain("Unexpected event kind received: " + eventKind
                                + " (expected: " + JDWP.EventKind.CLASS_PREPARE +")");
                        throw new Failure(error);
                    }

                    // extracy CLASS_PREPARE event specific data
                    int eventRequestID = event.getInt();
                    eventThreadID = event.getObjectID();
                    byte eventRefTypeTag = event.getByte();
                    long eventClassID = event.getReferenceTypeID();
                    String eventClassSignature = event.getString();
                    int eventClassStatus = event.getInt();

                    // check if event was single
                    if (events > 1) {
                        complain("Not single CLASS_PREPARE event received for class:\n\t"
                                + eventClassSignature);
                    }

                    // find appropriate class by signature
                    boolean found = false;
                    for (int j = 0; j < count; j++) {
                        if (eventClassSignature.equals(signatures[j])) {
                            found = true;

                            // check if event is not duplicated
                            if (classIDs[j] != 0) {
                                complain("Extra CLASS_PREPARE event recieved for class:\n\t"
                                        + eventClassSignature);
                            } else {
                                classIDs[j] = eventClassID;
                                received ++;
                            }

                            // check if event is because of expected request
                            if (eventRequestID != requestIDs[j]) {
                                complain("CLASS_PREPARE event with unexpected requestID ("
                                        + requestIDs[j] + ") received for class:\n\t"
                                        + eventClassSignature);
                            } else {
                                clearEventRequest(JDWP.EventKind.CLASS_PREPARE, requestIDs[j]);
                            }
                        }
                    }
                    if (!found) {
                        log.complain("Unexpected CLASS_PREPARE event received with class signature:\n"
                                   + "  " + eventClassSignature);
                    }
                }
            } catch (BoundException e) {
                complain("Unable to extract data from event packet while waiting for CLASS_PREPARE event:\n\t"
                            + e.getMessage() + "\n" + event);
                throw new Failure(error);
            }

            // if all events received return without resuming
            if (received >= count)
                return classIDs;

            // resume debuggee according to events suspend policy
            resumeEvent(eventSuspendPolicy, eventThreadID);
        }
    }

    /**
     * Wait for BREAKPOINT event made by the given request and return threadID.
     * Debuggee will be left suspended by the BREAKPOINT event.
     */
    public long waitForBreakpointEvent(int requestID) {
        String error = "Error occured while waiting for BREAKPOINT event for ";

        for(;;) {
            EventPacket event = receiveEvent();
            byte eventSuspendPolicy = 0;
            long eventThreadID = 0;
            try {
                eventSuspendPolicy = event.getByte();
                int events = event.getInt();
                for (int i = 0; i < events; i++) {
                    // check event kind
                    byte eventKind = event.getByte();
                    if (eventKind == JDWP.EventKind.VM_DEATH) {
                        complain("Unexpected VM_DEATH event received: " + eventKind
                                + " (expected: " + JDWP.EventKind.BREAKPOINT +")");
                        throw new Failure(error);
                    } else if (eventKind != JDWP.EventKind.BREAKPOINT) {
                        complain("Unexpected event kind received: " + eventKind
                                + " (expected: " + JDWP.EventKind.BREAKPOINT +")");
                        throw new Failure(error);
                    }

                    // extrack specific BREAKPOINT event data
                    int eventRequestID = event.getInt();
                    eventThreadID = event.getObjectID();
                    JDWP.Location eventLocation = event.getLocation();

                    if (eventRequestID == requestID) {
                        clearEventRequest(JDWP.EventKind.BREAKPOINT, requestID);
                        return eventThreadID;
                    } else {
                        complain("Unexpected BREAKPOINT event received with requestID: "
                                + eventRequestID + " (expected: " + requestID + ")");
                    }
                }
            } catch (BoundException e) {
                complain("Unable to extract data from event packet while waiting for BREAKPOINT event:\n\t"
                            + e.getMessage() + "\n" + event);
                throw new Failure(error);
            }

            resumeEvent(eventSuspendPolicy, eventThreadID);
        }
    }

    /**
     * Resume debuggee according given event suspend policy.
     */
    public void resumeEvent(byte suspendPolicy, long threadID) {
        if (suspendPolicy == JDWP.SuspendPolicy.NONE) {
            // do nothing
        } else if (suspendPolicy == JDWP.SuspendPolicy.EVENT_THREAD) {
            resumeThread(threadID);
        } else if (suspendPolicy == JDWP.SuspendPolicy.ALL) {
            resume();
        } else {
            throw new Failure("Unexpected event suspend policy while resuming debuggee: "
                            + suspendPolicy);
        }
    }

    // --------------------------------------------------- //

    /**
     * Query target VM for version info.
     */
    public String getVersionInfo() {
        String commandName = "VirtualMachine.Version";
        CommandPacket command =
            new CommandPacket(JDWP.Command.VirtualMachine.Version);
        ReplyPacket reply = receiveReplyFor(command, commandName);

        try {
            String description = reply.getString();
            int jdwpMajor = reply.getInt();
            int jdwpMinor = reply.getInt();
            String vmVersion = reply.getString();
            String vmName = reply.getString();
            return description;
        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting JDWP and VM version info");
        }
    }

   /**
    * Query target VM about VM dependent ID sizes.
    */
    public void queryForIDSizes() {
        String commandName = "VirtualMachine.IDSizes";
        CommandPacket command = new CommandPacket(JDWP.Command.VirtualMachine.IDSizes);
        ReplyPacket reply = receiveReplyFor(command);
        try {
            reply.resetPosition();
            JDWP.TypeSize.FIELD_ID = reply.getInt();
            JDWP.TypeSize.METHOD_ID = reply.getInt();
            JDWP.TypeSize.OBJECT_ID = reply.getInt();
            JDWP.TypeSize.REFERENCE_TYPE_ID = reply.getInt();
            JDWP.TypeSize.FRAME_ID = reply.getInt();
        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting VM dependent ID sizes");
        }
        JDWP.TypeSize.CalculateSizes();
    }

    // --------------------------------------------------- //

    /**
     * Suspend the debugee VM by sending VirtualMachine.Suspend command.
     */
    public void suspend() {
        String commandName = "VirtualMachine.Suspend";
        CommandPacket command = new CommandPacket(JDWP.Command.VirtualMachine.Suspend);
        ReplyPacket reply = receiveReplyFor(command, commandName);
    }

    /**
     * Resume the debugee VM by sending VirtualMachine.Resume command.
     */
    public void resume() {
        String commandName = "VirtualMachine.Resume";
        CommandPacket command = new CommandPacket(JDWP.Command.VirtualMachine.Resume);
        ReplyPacket reply = receiveReplyFor(command, commandName);
    }

    /**
     * Dispose the debugee VM by sending VirtualMachine.Dispose command.
     */
    public void dispose() {
        String commandName = "VirtualMachine.Dispose";
        CommandPacket command = new CommandPacket(JDWP.Command.VirtualMachine.Dispose);
        ReplyPacket reply = receiveReplyFor(command, commandName);
    }

    // --------------------------------------------------- //

    /**
     * Sends JDWP command packet.
     */
    public void sendCommand(CommandPacket packet, String commandName) {
        try {
            transport.write(packet);
        } catch (IOException e) {
            e.printStackTrace(log.getOutStream());
            complain("Caught IOException while sending command packet for "
                    + commandName + ":\n\t" + e);
            display("Command packet:\n" + packet);
            throw new Failure("Error occured while sending command: " + commandName);
        }
    }

    /**
     * Receive next JDWP packet.
     */
/*
    public Packet receivePacket() {
        try {
            ReplyPacket packet = new ReplyPacket();
            transport.read(packet);
            return packet;
        } catch (IOException e) {
            e.printStackTrace(log.getOutStream());
            throw new Failure("Caught IOException while receiving reply packet:\n\t" + e);
        }
    }
 */
    /**
     * Receive next JDWP reply packet.
     */
    public ReplyPacket receiveReply() {
        try {
            for (;;) {
                Packet packet = new Packet();
                transport.read(packet);

                if (packet.getFlags() == JDWP.Flag.REPLY_PACKET) {
                    ReplyPacket reply = new ReplyPacket(packet);
                    return reply;
                }

                EventPacket event = new EventPacket(packet);
                display("Placing received event packet into queue");
                eventQueue.add(event);
            }
        } catch (IOException e) {
            e.printStackTrace(log.getOutStream());
            throw new Failure("Caught IOException while receiving reply packet:\n\t" + e);
        }
    }

    /**
     * Get next JDWP event packet by reading from transport or getting stored
     * event in the event queue.
     */
    public EventPacket getEventPacket() throws IOException {
        // check events queue first
        if (!eventQueue.isEmpty()) {
            EventPacket event = (EventPacket)(eventQueue.removeFirst());
            return event;
        }

        // read from transport
        Packet packet = new Packet();
        transport.read(packet);

        EventPacket event = new EventPacket(packet);
        return event;
    }

    /**
     * Get next JDWP event packet by reading from transport for specified timeout
     * or getting stored event in the event queue.
     */
    public EventPacket getEventPacket(long timeout) throws IOException {
        transport.setReadTimeout(timeout);
        return getEventPacket();
    }

    /**
     * Receive next JDWP event packet.
     */
    public EventPacket receiveEvent() {
        EventPacket packet = null;
        try {
            packet = getEventPacket();
        } catch (IOException e) {
            e.printStackTrace(log.getOutStream());
            throw new Failure("Caught IOException while receiving event packet:\n\t" + e);
        }

        if (packet.getFlags() == JDWP.Flag.REPLY_PACKET) {
            ReplyPacket reply = new ReplyPacket(packet);
            log.complain("Unexpected reply packet received with id: "
                        + reply.getPacketID());
            log.display("Reply packet:\n" + reply);
            throw new Failure("Unexpected reply packet received instead of event packet");
        }

        return packet;
    }

    /**
     * Send specified command packet, receive and check reply packet.
     *
     * @throws Failure if exception caught in sending and reading packets
     */
    public ReplyPacket receiveReplyFor(CommandPacket command) {
        return receiveReplyFor(command, Packet.toHexString(command.getCommand(), 4));
    }

    /**
     * Send specified command packet, receive and check reply packet.
     *
     * @throws Failure if exception caught in sending and reading packets
     */
    public ReplyPacket receiveReplyFor(CommandPacket command, String commandName) {
        ReplyPacket reply = null;
        sendCommand(command, commandName);
        reply = receiveReply();
        try {
            reply.checkHeader(command.getPacketID());
        } catch (BoundException e) {
            complain("Wrong header of reply packet for command "+ commandName + ":\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Wrong reply packet received for command: " + commandName);
        }
        return reply;
    }

    /**
     * Receive and check event packet for specified event kind.
     *
     * @throws Failure if exception caught in sending and reading packets
     */
    public EventPacket receiveEventFor(int eventKind, String eventName) {
        EventPacket event = null;
        event = receiveEvent();
        try {
            event.checkHeader(eventKind);
        } catch (BoundException e) {
            complain("Wrong header of event packet for expected "+ eventName + " event:\n\t"
                    + e.getMessage());
            display("Event packet:\n" + event);
            throw new Failure("Wrong event packet received for expected event: " + eventName);
        }
        return event;
    }

    // --------------------------------------------------- //

    /**
     * Check common VM capability.
     */
    public boolean getCapability(int capability, String name) {
        String commandName = "VirtualMachine.Capabilities";

        int count = JDWP.Capability.CAN_GET_MONITOR_INFO + 1;
        if (capability < 0 || capability >= count) {
            throw new TestBug("Illegal capability number (" + capability
                            + ") while checking for VM capability: " + name);
        }

        CommandPacket command =
            new CommandPacket(JDWP.Command.VirtualMachine.Capabilities);
        ReplyPacket reply = receiveReplyFor(command, commandName);

        try {
            reply.resetPosition();

            for (int i = 0; i < count; i++) {
                byte value = reply.getByte();
                if (i == capability) {
                    return (value != 0);
                }
            }

        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting VM capability: "
                                + name);
        }

        throw new TestBug("Illegal capability number (" + capability
                        + ") while checking for VM capability: " + name);
    }

    /**
     * Check new VM capability (since JDWP version 1.4).
     */
    public boolean getNewCapability(int capability, String name) {
        String commandName = "VirtualMachine.CapabilitiesNew";
        int count = JDWP.Capability.CAN_SET_DEFAULT_STRATUM + 1;

        if (capability < 0 || capability >= count) {
            throw new TestBug("Illegal capability number (" + capability
            + ") while checking for VM new capability: " + name);
        }

        CommandPacket command =
            new CommandPacket(JDWP.Command.VirtualMachine.CapabilitiesNew);
        ReplyPacket reply = receiveReplyFor(command, commandName);

        try {
            reply.resetPosition();

            for (int i = 0; i < count; i++) {
                byte value = reply.getByte();
                if (i == capability) {
                    return (value != 0);
                }
            }
        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting VM new capability: "
                                + name);
        }

        throw new TestBug("Illegal capability number (" + capability
                        + ") while checking for VM new capability: " + name);
    }

    // --------------------------------------------------- //

    /**
     * Return ReferenceTypeID for requested class by given signature.
     */
    public long getReferenceTypeID(String classSignature) {
        String commandName = "VirtualMachine.ClassesBySignature";
        CommandPacket command =
            new CommandPacket(JDWP.Command.VirtualMachine.ClassesBySignature);
        command.addString(classSignature);
        command.setLength();
        ReplyPacket reply = receiveReplyFor(command, commandName);

        long typeID = 0;

        try {
            reply.resetPosition();

            int classes = reply.getInt();
            for (int i = 0; i < classes; i++) {
                byte refTypeTag = reply.getByte();
                typeID = reply.getReferenceTypeID();
                int status = reply.getInt();
            }

            if (classes < 0) {
                throw new Failure("Negative number (" + classes
                                + ") of referenceTypeIDs received for signature: "
                                + classSignature);
            }

            if (classes == 0) {
                throw new Failure("No any referenceTypeID received for signature: "
                                + classSignature);
            }

            if (classes > 1) {
                throw new Failure("Too many (" + classes
                                + ") referenceTypeIDs received for signature: "
                                + classSignature);
            }

        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting referenceTypeID for signature: "
                                + classSignature);
        }

        return typeID;
    }

    // --------------------------------------------------- //


    /**
     * Get list of IDs of supertypes (interfaces and classes) for given class.
     */
    public long[] getSupertypes(long classID, boolean declared) {
        Vector<Long> vector = new Vector<Long>();
        addSupertypes(classID, vector, null, null, false, declared);
        return makeListOfLongValues(vector);
    }

    /**
     * Get list of IDs of superclasses for given class.
     */
    public long[] getSuperclasses(long classID, boolean declared) {
        Vector<Long> vector = new Vector<Long>();
        addSupertypes(classID, null, null, vector, false, declared);
        return makeListOfLongValues(vector);
    }

    /**
     * Get list of IDs of implemented interfaces for given class.
     */
    public long[] getImplementedInterfaces(long classID, boolean declared) {
        Vector<Long> vector = new Vector<Long>();
        addSupertypes(classID, null, vector, null, false, declared);
        return makeListOfLongValues(vector);
    }

    /**
     * Get list of IDs of superinterfaces for given interface.
     */
    public long[] getSuperinterfaces(long interfaceID, boolean declared) {
        Vector<Long> vector = new Vector<Long>();
        addSupertypes(interfaceID, null, vector, null, true, declared);
        return makeListOfLongValues(vector);
    }

    // --------------------------------------------------- //

    /**
     * Get list of IDs of methods of given class.
     */
    public long[] getMethodIDs(long classID, boolean declared) {
        Vector<Long> list = new Vector<Long>();
        addMethods(classID, list, null, null, null, false, declared);
        return makeListOfLongValues(list);
    }

    /**
     * Get list of names of methods of given class.
     */
    public String[] getMethodNames(long classID, boolean declared) {
        Vector<String> list = new Vector<String>();
        addMethods(classID, null, list, null, null, false, declared);
        return makeListOfStringValues(list);
    }

    /**
     * Get list of signatures of methods of given class.
     */
    public String[] getMethodSignatures(long classID, boolean declared) {
        Vector<String> list = new Vector<String>();
        addMethods(classID, null, null, list, null, false, declared);
        return makeListOfStringValues(list);
    }

    /**
     * Get ID of a method of given class by name.
     */
    public long getMethodID(long classID, String name, boolean declared) {
        Vector<Long> IDs = new Vector<Long>();
        Vector<String> names = new Vector<String>();
        addMethods(classID, IDs, names, null, null, false, declared);
        int count = names.size();
        for (int i = 0; i < count; i++) {
            if (name.equals(names.elementAt(i))) {
                return (IDs.elementAt(i)).longValue();
            }
        }
        throw new Failure("Method \"" + name + "\" not found for classID: " + classID);
    }

    // --------------------------------------------------- //

    /**
     * Get list of IDs of static fields of given class.
     */
    public long[] getClassFieldIDs(long classID, boolean declared) {
        Vector<Long> list = new Vector<Long>();
        addFields(classID, list, null, null, null, false, declared);
        return makeListOfLongValues(list);
    }

    /**
     * Get list of names of static fields of given class.
     */
    public String[] getClassFieldNames(long classID, boolean declared) {
        Vector<String> list = new Vector<String>();
        addFields(classID, null, list, null, null, false, declared);
        return makeListOfStringValues(list);
    }

    /**
     * Get list of signatures of static fields of given class.
     */
    public String[] getClassFieldSignatures(long classID, boolean declared) {
        Vector<String> list = new Vector<String>();
        addFields(classID, null, null, list, null, false, declared);
        return makeListOfStringValues(list);
    }

    /**
     * Get ID of a static field of given class by name.
     */
    public long getClassFieldID(long classID, String name, boolean declared) {
        Vector<Long> IDs = new Vector<Long>();
        Vector<String> names = new Vector<String>();
        addFields(classID, IDs, names, null, null, false, declared);
        int count = names.size();
        for (int i = 0; i < count; i++) {
            if (name.equals((String)names.elementAt(i))) {
                return ((Long)IDs.elementAt(i)).longValue();
            }
        }
        throw new Failure("Static field \"" + name + "\" not found for classID: " + classID);
    }

    // --------------------------------------------------- //

    /**
     * Get value of a static field of given class.
     */
    public JDWP.Value getStaticFieldValue(long typeID, long fieldID) {
        String commandName = "ReferenceType.GetValues";
        CommandPacket command =
            new CommandPacket(JDWP.Command.ReferenceType.GetValues);
        command.addReferenceTypeID(typeID);
        command.addInt(1);
        command.addFieldID(fieldID);

        ReplyPacket reply = receiveReplyFor(command, commandName);
        JDWP.Value value = null;

        try {
            reply.resetPosition();

            int count = reply.getInt();
            if (count < 1) {
                throw new Failure("No values returned for static fieldID: " + fieldID);
            }
            value = reply.getValue();

        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName +" command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting value of static field: " +
                                + fieldID);
        }
        return value;
    }

    /**
     * Get value of particular type from a static field of given class.
     */
    public JDWP.Value getStaticFieldValue(long typeID, String fieldName, byte tag) {
        long fieldID = getClassFieldID(typeID, fieldName, true);
        JDWP.Value value = getStaticFieldValue(typeID, fieldID);

        if (value.getTag() != tag) {
            complain("unexpedted value tag returned from debuggee: " + value.getTag()
                        + " (expected: " + tag + ")");
            throw new Failure("Error occured while getting value from static field: "
                            + fieldName);
        }
        return value;
    }

    /**
     * Set value of a static field of given class.
     */
    public void setStaticFieldValue(long typeID, long fieldID, JDWP.Value value) {
        String commandName = "ClassType.SetValues";
        CommandPacket command =
            new CommandPacket(JDWP.Command.ClassType.SetValues);
        command.addReferenceTypeID(typeID);
        command.addInt(1);
        command.addFieldID(fieldID);
        command.addUntaggedValue(value, value.getTag());

        ReplyPacket reply = receiveReplyFor(command, commandName);
    }

    /**
     * Get value of a field of given object.
     */
    public JDWP.Value getObjectFieldValue(long objectID, long fieldID) {
        String commandName = "ObjectReference.GetValues";
        CommandPacket command =
            new CommandPacket(JDWP.Command.ObjectReference.GetValues);
        command.addObjectID(objectID);
        command.addInt(1);
        command.addFieldID(fieldID);

        ReplyPacket reply = receiveReplyFor(command, commandName);
        JDWP.Value value = null;

        try {
            reply.resetPosition();

            int count = reply.getInt();
            if (count < 1) {
                throw new Failure("No values returned for object fieldID: " + fieldID);
            }
            value = reply.getValue();

        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting value of object field: " +
                                + fieldID);
        }
        return value;
    }

    /**
     * Set value of a static field of given class.
     */
    public void setObjectFieldValue(long objectID, long fieldID, JDWP.Value value) {
        String commandName = "ObjectReference.SetValues";
        CommandPacket command =
            new CommandPacket(JDWP.Command.ObjectReference.SetValues);
        command.addObjectID(objectID);
        command.addInt(1);
        command.addFieldID(fieldID);
        command.addUntaggedValue(value, value.getTag());

        ReplyPacket reply = receiveReplyFor(command, commandName);
    }

    // --------------------------------------------------- //

    /**
     * Find threadID for given thread name among all active threads.
     */
    public long getThreadID(String name) {
        // request list of all threadIDs
        int threads = 0;
        long threadIDs[] = null;
        {
            String commandName = "VirtualMachine.AllThreads";
            CommandPacket command = new CommandPacket(JDWP.Command.VirtualMachine.AllThreads);
            ReplyPacket reply = receiveReplyFor(command, commandName);
            reply.resetPosition();
            try {
                threads = reply.getInt();
                threadIDs = new long[threads];

                for (int i = 0; i < threads; i++) {
                    threadIDs[i] = reply.getObjectID();
                }
            } catch (BoundException e) {
                complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                        + e.getMessage());
                display("Reply packet:\n" + reply);
                throw new Failure("Error occured while getting threadID for thread name: "
                                    + name);
            }
        }

        // request name for each threadID
        for (int i = 0; i < threads; i++) {
            String commandName = "ThreadReference.Name";
            CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Name);
            command.addObjectID(threadIDs[i]);
            ReplyPacket reply = receiveReplyFor(command, commandName);
            try {
                reply.resetPosition();
                String threadName = reply.getString();
                if (threadName.equals(name)) {
                    return threadIDs[i];
                }
            } catch (BoundException e) {
                complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                        + e.getMessage());
                display("Reply packet:\n" + reply);
                throw new Failure("Error occured while getting name for threadID: "
                                + threadIDs[i]);
            }
        }

        throw new Failure("No threadID found for thread name: " + name);
    }

    /**
     * Return thread name for the given threadID.
     */
    public String getThreadName(long threadID) {
        String commandName = "ThreadReference.Name";
        CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Name);
        command.addObjectID(threadID);
        ReplyPacket reply = receiveReplyFor(command, commandName);
        try {
            reply.resetPosition();
            String threadName = reply.getString();
            return threadName;
        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting name for threadID: "
                            + threadID);
        }
    }

    /**
     * Suspend thread for the given threadID.
     */
    public void suspendThread(long threadID) {
        String commandName = "ThreadReference.Suspend";
        CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Suspend);
        command.addObjectID(threadID);

        ReplyPacket reply = receiveReplyFor(command, commandName);
    }

    /**
     * Resume thread for the given threadID.
     */
    public void resumeThread(long threadID) {
        String commandName = "ThreadReference.resume";
        CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Resume);
        command.addObjectID(threadID);

        ReplyPacket reply = receiveReplyFor(command, commandName);
    }

    /**
     * Return frameID for the current frame of the thread.
     * Thread must be suspended.
     */
    public long getCurrentFrameID(long threadID) {
        String commandName = "ThreadReference.Frames";
        CommandPacket command = new CommandPacket(JDWP.Command.ThreadReference.Frames);
        command.addObjectID(threadID);     // threadID
        command.addInt(0);                 // startFrame
        command.addInt(1);                 // length

        ReplyPacket reply = receiveReplyFor(command, commandName);
        try {
            reply.resetPosition();
            int frames = reply.getInt();
            if (frames != 1) {
                throw new Failure("Not only one current frame returned for threadID "
                                + threadID + ": " + frames);
            }
            long frameID = reply.getFrameID();
            JDWP.Location location = reply.getLocation();
            return frameID;
        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting current frame for threadID: "
                            + threadID);
        }
    }

    // --------------------------------------------------- //

    /**
     * Find line number for the given location from the method line table.
     * If <code>approximate</code> is <it>true</i> the found line should begin
     * with code index of the given location. Otherwise, the found line will
     * just cover code index of the given location.
     */
    public int getLineNumber(JDWP.Location location, boolean approximate) {
        String commandName = "Method.LineTable";
        CommandPacket command = new CommandPacket(JDWP.Command.Method.LineTable);
        command.addReferenceTypeID(location.getClassID());
        command.addMethodID(location.getMethodID());
        long index = location.getIndex();
        ReplyPacket reply = receiveReplyFor(command, commandName);
        String msg = "Error occured while getting line number for location: " + location;
        try {
            reply.resetPosition();
            long start = reply.getLong();
            if (index < start) {
                complain("Location index (" + index
                        + ") is less than start method index (" + start);
                throw new Failure(msg);
            }
            long end = reply.getLong();
            if (index > end) {
                complain("Location index (" + index
                        + ") is greater than end method index (" + end);
                throw new Failure(msg);
            }
            int lines = reply.getInt();
            if (!approximate) {
                for (int i = 0; i < lines; i++) {
                    long lineCodeIndex = reply.getLong();
                    int lineNumber = reply.getInt();
                    if (lineCodeIndex == index) {
                        return lineNumber;
                    }
                }
                throw new Failure("No exact line number exactly for location: " + location);
            } else {
                int prevLine = -1;
                for (int i = 0; i < lines; i++) {
                    long lineCodeIndex = reply.getLong();
                    int lineNumber = reply.getInt();
                    if (lineCodeIndex == index) {
                        return lineNumber;
                    } else if (lineCodeIndex > index) {
                        break;
                    }
                    prevLine = lineNumber;
                }
                if (prevLine < 0)
                    throw new Failure("No approximate line number found for location: " + location);
                return prevLine;
            }
        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure(msg);
        }
    }

    /**
     * Find line index for the given line number from the method line table.
     */
    public long getCodeIndex(long classID, long methodID, int lineNumber) {
        String commandName = "Method.LineTable";
        CommandPacket command = new CommandPacket(JDWP.Command.Method.LineTable);
        command.addReferenceTypeID(classID);
        command.addMethodID(methodID);
        ReplyPacket reply = receiveReplyFor(command, commandName);
        String msg = "Error occured while getting code index for line number: " + lineNumber;
        try {
            reply.resetPosition();
            long start = reply.getLong();
            long end = reply.getLong();
            int lines = reply.getInt();
            for (int i = 0; i < lines; i++) {
                long lineCodeIndex = reply.getLong();
                int line = reply.getInt();
                if (lineNumber == line) {
                    return lineCodeIndex;
                }
            }
        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure(msg);
        }

        throw new Failure("No code index found for line number: " + lineNumber);
    }

    // --------------------------------------------------- //

    /**
     * Make the specified event request into debuggee.
     */
    public int requestEvent(CommandPacket requestCommand, String name) {
        String commandName = "EventRequest.Set";
        ReplyPacket reply = receiveReplyFor(requestCommand, name);

        try {
            reply.resetPosition();

            int requestID = reply.getInt();
            return requestID;

        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Request command packet:\n" + requestCommand);
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while making event request: " + name);
        }
    }

    /**
     * Remove existing event request from debuggee.
     */
    public void clearEventRequest(byte eventKind, int requestID) {
        String commandName = "EventRequest.Clear";
        CommandPacket command = new CommandPacket(JDWP.Command.EventRequest.Clear);
        command.addByte(eventKind);
        command.addInt(requestID);
        ReplyPacket reply = receiveReplyFor(command, commandName);
    }

    /**
     * Make request for CLASS_PREPARE event for specified class into debuggee.
     */
    public int requestClassPrepareEvent(String className, byte suspendPolicy) {
        CommandPacket command = new CommandPacket(JDWP.Command.EventRequest.Set);
        command.addByte(JDWP.EventKind.CLASS_PREPARE);
        command.addByte(suspendPolicy);
        command.addInt(1);
            command.addByte(JDWP.EventModifierKind.CLASS_MATCH);
                command.addString(className);

        return requestEvent(command, "CLASS_PREPARE");
    }

    /**
     * Make request for BREAKPOINT event for the specified location into debuggee.
     */
    public int requestBreakpointEvent(JDWP.Location location, byte suspendPolicy) {
        CommandPacket command = new CommandPacket(JDWP.Command.EventRequest.Set);
        command.addByte(JDWP.EventKind.BREAKPOINT);
        command.addByte(suspendPolicy);
        command.addInt(1);
            command.addByte(JDWP.EventModifierKind.LOCATION_ONLY);
                command.addLocation(location);
        return requestEvent(command, "BREAKPOINT");
    }

    /**
     * Make request for BREAKPOINT event for the specified line of the given method.
     */
    public int requestBreakpointEvent(byte typeTag, long classID, long methodID,
                                            int lineNumber, byte suspendPolicy) {
        long codeIndex = getCodeIndex(classID, methodID, lineNumber);
        JDWP.Location location = new JDWP.Location(typeTag, classID, methodID, codeIndex);
        return requestBreakpointEvent(location, suspendPolicy);
    }

    /**
     * Remove all existing BREAKPOINT event requests from debuggee.
     */
    public void clearAllBreakpoints() {
        String commandName = "EventRequest.ClearAllBreakpoints";
        CommandPacket command = new CommandPacket(JDWP.Command.EventRequest.ClearAllBreakpoints);
        ReplyPacket reply = receiveReplyFor(command, commandName);
    }

    // --------------------------------------------------- //

    /**
     * Add IDs of supertypes (interfaces and classes) for given class to the lists.
     */
    private void addSupertypes(long referenceTypeID, Vector<Long> supertypes,
                                Vector<Long> interfaces, Vector<Long> superclasses,
                                boolean interfaceOnly, boolean declared) {

        if (supertypes != null || interfaces != null) {

            // obtain list of direct implemented interfaces
            String commandName = "ReferenceType.Interfaces";
            CommandPacket command = new CommandPacket(JDWP.Command.ReferenceType.Interfaces);
            command.addReferenceTypeID(referenceTypeID);
            ReplyPacket reply = receiveReplyFor(command, commandName);

            try {
                reply.resetPosition();

                int count = reply.getInt();
                if (count < 0) {
                    throw new Failure("Negative number (" + count
                                    + ") of declared interfaces received for referenceTypeID: "
                                    + referenceTypeID);
                }

                for (int i = 0; i < count; i++) {
                    long typeID = reply.getReferenceTypeID();
                    if (!declared) {
                        addSupertypes(typeID, supertypes, interfaces, superclasses,
                                        true, declared);
                    }
                    Long value = new Long(typeID);
                    if (supertypes != null) {
                        supertypes.add(value);
                    }
                    if (interfaces != null) {
                        interfaces.add(value);
                    }
                }

            } catch (BoundException e) {
                complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                        + e.getMessage());
                display("Reply packet:\n" + reply);
                throw new Failure("Error occured while getting interfeceIDs for referenceTypeID: " +
                                    + referenceTypeID);
            }

        }

        if (!interfaceOnly) {

            String commandName = "ClassType.Superclasses";
            CommandPacket command = new CommandPacket(JDWP.Command.ClassType.Superclass);
            command.addReferenceTypeID(referenceTypeID);
            ReplyPacket reply = receiveReplyFor(command, commandName);

            try {
                reply.resetPosition();

                long typeID = reply.getReferenceTypeID();
                if (typeID != 0) {
                    if (!declared) {
                        addSupertypes(typeID, supertypes, interfaces, superclasses,
                                    false, declared);
                    }
                    Long value = new Long(typeID);
                    if (supertypes != null) {
                        supertypes.add(value);
                    }
                    if (superclasses != null) {
                        superclasses.add(value);
                    }
                }
            } catch (BoundException e) {
                complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                        + e.getMessage());
                display("Reply packet:\n" + reply);
                throw new Failure("Error occured while getting superclass ID for classID: " +
                                    + referenceTypeID);
            }

        }
    }

    /**
     * Add attributes of fields of given class to the lists.
     */
    private void addFields(long referenceTypeID, Vector<Long> IDs, Vector<String> names,
                                Vector<String> signatures, Vector<Integer> modifiers,
                                boolean interfaceOnly, boolean declared) {

        if (!declared) {
            Vector<Long> supertypes = new Vector<Long>();
            addSupertypes(referenceTypeID, supertypes, null, null, interfaceOnly, declared);
            int count = supertypes.size();
            for (int i = 0; i < count; i++) {
                long typeID = (supertypes.elementAt(i)).longValue();
                addFields(typeID, IDs, names, signatures, modifiers,
                            interfaceOnly, declared);
            }
        }

        String commandName = "ReferenceType.Fields";
        CommandPacket command = new CommandPacket(JDWP.Command.ReferenceType.Fields);
        command.addReferenceTypeID(referenceTypeID);
        ReplyPacket reply = receiveReplyFor(command, commandName);

        try {
            reply.resetPosition();

            int count = reply.getInt();
            if (count < 0) {
                throw new Failure("Negative number (" + count
                                + ") of declared fields received for referenceTypeID: "
                                + referenceTypeID);
            }

            for (int i = 0; i < count; i++) {
                long id = reply.getFieldID();
                String name = reply.getString();
                String signature = reply.getString();
                int modBits = reply.getInt();

                if (IDs != null)
                    IDs.add(new Long(id));
                if (names != null)
                    names.add(name);
                if (signatures != null)
                    signatures.add(signature);
                if (modifiers != null)
                    modifiers.add(new Integer(modBits));
            }

        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting fieldIDs for referenceTypeID: " +
                                + referenceTypeID);
        }
    }

    /**
     * Add attributes of methods of given class to the lists.
     */
    private void addMethods(long referenceTypeID, Vector<Long> IDs, Vector<String> names,
                                Vector<String> signatures, Vector<Integer> modifiers,
                                boolean interfaceOnly, boolean declared) {

        if (!declared) {
            Vector<Long> supertypes = new Vector<Long>();
            addSupertypes(referenceTypeID, supertypes, null, null, interfaceOnly, declared);
            int count = supertypes.size();
            for (int i = 0; i < count; i++) {
                long typeID = (supertypes.elementAt(i)).longValue();
                addMethods(typeID, IDs, names, signatures, modifiers,
                            interfaceOnly, declared);
            }
        }

        String commandName = "ReferenceType.Methods";
        CommandPacket command = new CommandPacket(JDWP.Command.ReferenceType.Methods);
        command.addReferenceTypeID(referenceTypeID);
        ReplyPacket reply = receiveReplyFor(command, commandName);

        try {
            reply.resetPosition();

            int count = reply.getInt();
            if (count < 0) {
                throw new Failure("Negative number (" + count
                                + ") of declared fields received for referenceTypeID: "
                                + referenceTypeID);
            }

            for (int i = 0; i < count; i++) {
                long id = reply.getMethodID();
                String name = reply.getString();
                String signature = reply.getString();
                int modBits = reply.getInt();

                if (IDs != null)
                    IDs.add(new Long(id));
                if (names != null)
                    names.add(name);
                if (signatures != null)
                    signatures.add(signature);
                if (modifiers != null)
                    modifiers.add(new Integer(modBits));
            }

        } catch (BoundException e) {
            complain("Unable to parse reply packet for " + commandName + " command:\n\t"
                    + e.getMessage());
            display("Reply packet:\n" + reply);
            throw new Failure("Error occured while getting methodIDs for referenceTypeID: " +
                                + referenceTypeID);
        }
    }

    // --------------------------------------------------- //

    private static long[] makeListOfLongValues(Vector<Long> vector) {
        int count = vector.size();
        long[] list = new long[count];
        for (int i = 0; i < count; i++) {
            list[i] = (vector.elementAt(i)).longValue();
        }
        return list;
    }

    private static String[] makeListOfStringValues(Vector<String> vector) {
        int count = vector.size();
        String[] list = new String[count];
        for (int i = 0; i < count; i++) {
            list[i] = vector.elementAt(i);
        }
        return list;
    }

    // --------------------------------------------------- //

    /**
     * Force debugge VM to exit using JDWP connection if possible.
     */
    protected void killDebugee() {
        // ignore
    }

    /**
     * Close transport channel and kill the debugee VM if it is not terminated yet.
     */
    public void close() {
        if (transport != null) {
            try {
                transport.close();
            } catch (IOException e) {
                log.display("WARNING: Caught IOException while closing JDWP connection:\n\t"
                            + e);
            }
        }
        super.close();
    }

}