File: JVMTIagent.cpp

package info (click to toggle)
openjdk-11-jre-dcevm 11.0.15%2B1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 221,420 kB
  • sloc: java: 1,363,561; cpp: 967,919; ansic: 69,480; xml: 62,475; sh: 8,106; asm: 3,475; python: 1,667; javascript: 943; makefile: 397; sed: 172; perl: 114
file content (1207 lines) | stat: -rw-r--r-- 43,108 bytes parent folder | download | duplicates (6)
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
/*
 * Copyright (c) 2004, 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.
 */

/*
 *
 * JVMTI agent used for run every test from the testbase in a special
 * debug mode. This mode is intended to be part of serviceability
 * reliability testing.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <jvmti.h>

#include "nsk_tools.h"
#include "jni_tools.h"
#include "JVMTITools.h"
#include "jvmti_tools.h"

extern "C" {

static jvmtiEnv *jvmti = NULL; /* JVMTI env */
static jvmtiEventCallbacks callbacks;
static jrawMonitorID eventLock; /* raw monitor used for exclusive ownership of HotSwap function */

static volatile int debug_mode = 0; /* 0 - verbose mode off;
                                       1 - verbose mode on;
                                       2 - verbose mode on including all JVMTI events reporting,
                                           produces a huge number of messages */

/* stress level */
static volatile int stress_lev = 0; /* 0 - default mode: generation of all events except
                                                ExceptionCatch,
                                                MethodEntry/Exit, SingleStep;
                                       1 - generation of all events except
                                                MethodEntry/Exit,
                                                SingleStep;
                                       2 - generation of all events except
                                                SingleStep;
                                       3 - generation of all events, including
                                                ExceptionCatch,
                                                MethodEntry/Exit,
                                                SingleStep
                                     */

#define TRUE 1
#define FALSE 0

/**** the following is used for "postVM_DEATH" events watching ****/
static volatile int vm_death_occured = FALSE;
/************************************************/

/**** the following is used for HotSwap mode ****/

/* HotSwap modes:
 HOTSWAP_OFF                - default mode: HotSwap off;
 HOTSWAP_EVERY_METHOD_ENTRY - HotSwap tested class in every method entry event
                              of running test
 HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS - HotSwap tested class in every
                              method entry event of every class
 HOTSWAP_EVERY_SINGLE_STEP  - HotSwap tested class in every single step event
                              of running test
 HOTSWAP_EVERY_EXCEPTION    - HotSwap tested class in every exception event
                              of running test
 HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS - HotSwap tested class in every
                              exception event of every class
 */

#define HOTSWAP_OFF 0
#define HOTSWAP_EVERY_METHOD_ENTRY 2
#define HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS 20
#define HOTSWAP_EVERY_SINGLE_STEP 3
#define HOTSWAP_EVERY_EXCEPTION 4
#define HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS 40

static int hotswap = HOTSWAP_OFF;

typedef struct {   /* test class info */
    char *clazzsig;  /* class signature */
    jclass cls;      /* a class to be redefined */
    jint bCount;     /* number of bytes defining the class */
    jbyte *clsBytes; /* bytes defining the class */
    struct class_info *next;
} class_info;


static const char *shortTestName = NULL; /* name of the test without package prefix */
static jclass rasCls; /* reference to the auxiliary class RASagent used for HotSwap */
static class_info *clsInfo = NULL, *clsInfoFst = NULL;

static void lock(JNIEnv*);
static void unlock(JNIEnv*);
static jint allocClsInfo(JNIEnv*, char*, jclass);
static void deallocClsInfo(JNIEnv*);
static int findAndHotSwap(JNIEnv*, jclass);
static int doHotSwap(JNIEnv*, jclass, jint, jbyte*);
static void display(int, const char format[], ...);
static void clearJavaException(JNIEnv*);
static int enableEventsCaps();
static int addStressEvents();
static void getVerdict(JNIEnv*, const char *);
/************************************************/

/** callback functions **/
void JNICALL
Breakpoint(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thr, jmethodID method,
        jlocation loc) {

    display(1, "#### JVMTIagent: Breakpoint occurred ####\n");

    getVerdict(jni_env, "Breakpoint");
}

void JNICALL
ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
        jclass class_beeing_redefined,
        jobject loader, const char* name, jobject protection_domain,
        jint class_data_len, const unsigned char* class_data,
        jint *new_class_data_len, unsigned char** new_class_data) {

    display(1, "#### JVMTIagent: ClassFileLoadHook occurred ####\n");

    getVerdict(jni_env, "ClassFileLoadHook");
}

void JNICALL
ClassLoad(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread, jclass klass) {
    char *cls_sig;
    jint clsByteCount;

    display((hotswap != HOTSWAP_OFF)?0:1,
        "#### JVMTIagent: ClassLoad occurred ####\n");

    getVerdict(jni_env, "ClassLoad");

    if (hotswap != HOTSWAP_OFF) {
        /* enter into a raw monitor for exclusive work with redefined class */
        lock(jni_env);
        display(0, "#### JVMTIagent: ClassLoad: >>>>>>>> entered the raw monitor \"eventLock\" ####\n");

        if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(klass, &cls_sig, /*&generic*/NULL)))
            jni_env->FatalError("JVMTIagent: failed to get class signature\n");
        else {
            if (shortTestName != NULL) {
                if (strstr((const char*) cls_sig, shortTestName) != NULL) {
                    display(0,
                            "#### JVMTIagent: found test class matched with \"%s\"\n"
                            "<JVMTIagent>\tsignature=%s\n",
                            shortTestName, cls_sig);
                    clsByteCount = allocClsInfo(jni_env, cls_sig, klass);
                    display(0, "#### JVMTIagent: %d bytes defining the class have been successfully loaded\n",
                            clsByteCount);
                }
            }
        }

        /* exit from the raw monitor */
        unlock(jni_env);
        display(0, "#### JVMTIagent: ClassLoad: <<<<<<<< exited from the raw monitor \"eventLock\" ####\n\n");
    }
}

void JNICALL
ClassPrepare(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
        jthread thr, jclass cls) {

    display(1, "#### JVMTIagent: ClassPrepare occurred ####\n");

    getVerdict(jni_env, "ClassPrepare");
}

void JNICALL
CompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,
        const void* code_addr,  jint map_length,
        const jvmtiAddrLocationMap* map, const void* compile_info) {

    display(1, "#### JVMTIagent: CompiledMethodLoad occurred ####\n");

    getVerdict(NULL, "CompiledMethodLoad");
}

void JNICALL
CompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method,
        const void* code_addr) {

    display(1, "#### JVMTIagent: CompiledMethodUnload occurred ####\n");

    getVerdict(NULL, "CompiledMethodUnload");
}

void JNICALL
DataDumpRequest(jvmtiEnv *jvmti_env) {

    display(1, "#### JVMTIagent: DataDumpRequest occurred ####\n");

    getVerdict(NULL, "DataDumpRequest");
}

void JNICALL
DynamicCodeGenerated(jvmtiEnv *jvmti_env,
        const char* name,
        const void* address,
        jint length) {

    display(1, "#### JVMTIagent: DynamicCodeGenerated occurred ####\n");

    getVerdict(NULL, "DynamicCodeGenerated");
}

void JNICALL
Exception(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thr,
        jmethodID method, jlocation location, jobject exception,
        jmethodID catch_method, jlocation catch_location) {
    jclass decl_clazz;

    display((hotswap == HOTSWAP_EVERY_EXCEPTION ||
            hotswap == HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS)?0:1,
        "#### JVMTIagent: Exception occurred ####\n");

    getVerdict(jni_env, "Exception");

    if (hotswap == HOTSWAP_EVERY_EXCEPTION ||
            hotswap == HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS) {
        if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(method, &decl_clazz)))
            jni_env->FatalError("JVMTIagent: failed to get method declaring class\n");

        if (findAndHotSwap(jni_env, decl_clazz) != 0)
            jni_env->FatalError("JVMTIagent: failed to hotswap class\n");
    }
}

void JNICALL
FieldAccess(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
        jthread thr, jmethodID method,
        jlocation location, jclass field_klass, jobject obj, jfieldID field) {

    display(1, "#### JVMTIagent: FieldAccess occurred ####\n");

    getVerdict(jni_env, "FieldAccess");
}

void JNICALL
FieldModification(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
        jthread thr, jmethodID method, jlocation location,
        jclass field_klass, jobject obj,
        jfieldID field, char sig, jvalue new_value) {

    display(1, "#### JVMTIagent: FieldModification occurred ####\n");

    getVerdict(jni_env, "FieldModification");
}

void JNICALL
FramePop(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
        jthread thr, jmethodID method, jboolean wasPopedByException) {

    display(1, "#### JVMTIagent: FramePop occurred ####\n");

    getVerdict(jni_env, "FramePop");
}

void JNICALL
GarbageCollectionFinish(jvmtiEnv *jvmti_env) {

    display(1, "#### JVMTIagent: GarbageCollectionFinish occurred ####\n");

    getVerdict(NULL, "GarbageCollectionFinish");
}

void JNICALL
GarbageCollectionStart(jvmtiEnv *jvmti_env) {

    display(1, "#### JVMTIagent: GarbageCollectionStart occurred ####\n");

    getVerdict(NULL, "GarbageCollectionStart");
}

void JNICALL
MonitorContendedEnter(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thr,
        jobject obj) {

    display(1, "#### JVMTIagent: MonitorContendedEnter occurred ####\n");

    getVerdict(jni_env, "MonitorContendedEnter");
}

void JNICALL
MonitorContendedEntered(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thr,
        jobject obj) {

    display(1, "#### JVMTIagent: MonitorContendedEntered occurred ####\n");

    getVerdict(jni_env, "MonitorContendedEntered");
}

void JNICALL
MonitorWait(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thr, jobject obj,
        jlong tout) {

    display(1, "#### JVMTIagent: MonitorWait occurred ####\n");

    getVerdict(jni_env, "MonitorWait");
}

void JNICALL
MonitorWaited(jvmtiEnv *jvmti_env, JNIEnv* jni_env,
        jthread thr, jobject obj, jboolean timed_out) {

    display(1, "#### JVMTIagent: MonitorWaited occurred ####\n");

    getVerdict(jni_env, "MonitorWaited");
}

void JNICALL
NativeMethodBind(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
        jmethodID method, void *addr, void **new_addr) {

    display(1, "#### JVMTIagent: NativeMethodBind occurred ####\n");

    getVerdict(jni_env, "NativeMethodBind");
}

void JNICALL
ObjectFree(jvmtiEnv *jvmti_env, jlong tag) {

    display(1, "#### JVMTIagent: ObjectFree occurred ####\n");

    getVerdict(NULL, "ObjectFree");
}

void JNICALL
ThreadEnd(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread) {

    display(1, "#### JVMTIagent: ThreadEnd occurred ####\n");

    getVerdict(jni_env, "ThreadEnd");
}

void JNICALL
ThreadStart(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thread) {

    display(1, "#### JVMTIagent: ThreadStart occurred ####\n");

    getVerdict(jni_env, "ThreadStart");
}

void JNICALL
VMDeath(jvmtiEnv *jvmti_env, JNIEnv *jni_env) {
    vm_death_occured = TRUE;

    display(0, "#### JVMTIagent: VMDeath occurred ####\n");

    if (hotswap != HOTSWAP_OFF) {
        deallocClsInfo(jni_env);
        display(0, "#### JVMTIagent: allocated memory was successfully freed ####\n");
    }
}

void JNICALL
VMInit(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thr) {

    display(0, "#### JVMTIagent: VMInit occurred ####\n");

    getVerdict(jni_env, "VMInit");
}

void JNICALL
VMStart(jvmtiEnv *jvmti_env, JNIEnv* jni_env) {

    display(0, "#### JVMTIagent: VMStart occurred ####\n");

    getVerdict(jni_env, "VMStart");
}

JNIEXPORT void JNICALL
VMObjectAlloc(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
        jobject object, jclass object_klass, jlong size) {

    display(1, "#### JVMTIagent: VMObjectAlloc occurred ####\n");

    getVerdict(jni_env, "VMObjectAlloc");
}

void JNICALL
SingleStep(jvmtiEnv *jvmti_env, JNIEnv* jni_env, jthread thread,
        jmethodID method, jlocation location) {
    jclass decl_clazz;

    display((hotswap == HOTSWAP_EVERY_SINGLE_STEP)?0:1,
        "#### JVMTIagent: SingleStep occurred ####\n");

    getVerdict(jni_env, "SingleStep");

    if (hotswap == HOTSWAP_EVERY_SINGLE_STEP) {
        if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(method, &decl_clazz)))
            jni_env->FatalError("JVMTIagent: failed to get method declaring class\n");

        if (findAndHotSwap(jni_env, decl_clazz) != 0)
            jni_env->FatalError("JVMTIagent: failed to hotswap class\n");
    }
}

void JNICALL
MethodEntry(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
        jthread thr, jmethodID method) {
    jclass decl_clazz;

    display((hotswap == HOTSWAP_EVERY_METHOD_ENTRY ||
            hotswap == HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS)?0:1,
        "#### JVMTIagent: MethodEntry occurred ####\n");

    getVerdict(jni_env, "MethodEntry");

    if (hotswap == HOTSWAP_EVERY_METHOD_ENTRY ||
            hotswap == HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS) {
        if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(method, &decl_clazz)))
            jni_env->FatalError("JVMTIagent: failed to get method declaring class\n");

        if (findAndHotSwap(jni_env, decl_clazz) != 0)
            jni_env->FatalError("JVMTIagent: failed to hotswap class\n");
    }
}

void JNICALL
MethodExit(jvmtiEnv *jvmti_env, JNIEnv *jni_env,
        jthread thr, jmethodID method,
        jboolean was_poped_by_exc, jvalue return_value) {

    display(1, "#### JVMTIagent: MethodExit occurred ####\n");

    getVerdict(jni_env, "MethodExit");
}

void JNICALL
ExceptionCatch(jvmtiEnv *jvmti_env, JNIEnv *jni_env, jthread thr,
        jmethodID method, jlocation location, jobject exception) {
    jclass decl_clazz;

    display((hotswap == HOTSWAP_EVERY_EXCEPTION ||
            hotswap == HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS)?0:1,
        "#### JVMTIagent: ExceptionCatch occurred ####\n");

    getVerdict(jni_env, "ExceptionCatch");

    if (hotswap == HOTSWAP_EVERY_EXCEPTION ||
            hotswap == HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS) {
        if (!NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(method, &decl_clazz)))
            jni_env->FatalError("JVMTIagent: failed to get method declaring class\n");

        if (findAndHotSwap(jni_env, decl_clazz) != 0)
            jni_env->FatalError("JVMTIagent: failed to hotswap class\n");
    }
}
/************************/

static void lock(JNIEnv *jni_env) {
    if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorEnter(eventLock)))
        jni_env->FatalError("JVMTIagent: failed to enter a raw monitor\n");
}

static void unlock(JNIEnv *jni_env) {
    if (!NSK_JVMTI_VERIFY(jvmti->RawMonitorExit(eventLock)))
        jni_env->FatalError("JVMTIagent: failed to exit a raw monitor\n");
}

JNIEXPORT jint JNICALL
Java_nsk_share_RASagent_setHotSwapMode(JNIEnv *jni_env, jclass cls,
        jboolean vrb, jint level, jstring shortName) {
    jvmtiCapabilities capabil;
    jmethodID mid = NULL;

    if (jvmti == NULL) {
        printf("ERROR(%s,%d): JVMTIagent was not properly loaded: JVMTI env = NULL\n",
               __FILE__, __LINE__);
        return 1;
    }

    /* get supported JVMTI capabilities */
    if (!NSK_JVMTI_VERIFY(jvmti->GetCapabilities(&capabil)))
        jni_env->FatalError("JVMTIagent: failed to get capabilities\n");
    if (capabil.can_redefine_classes != 1) { /* ???????????? */
        printf("ERROR: JVMTIagent: Class File Redefinition (HotSwap) is not implemented in this VM\n");
        return 1;
    }

    if (vrb == JNI_TRUE && debug_mode == 0)
        debug_mode = 1;

    hotswap = level;
    switch (hotswap) {
        case HOTSWAP_OFF:
            display(0, "#### JVMTIagent: hotswap mode off ####\n");
            return 0;
        case HOTSWAP_EVERY_METHOD_ENTRY:
            stress_lev = 2;
            display(0,
                    "#### JVMTIagent: hotswapping class in every method entry event enabled ####\n"
                    "<JVMTIagent>\tHotSwap stress level: %d\n",
                    stress_lev);
            break;
        case HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS:
            stress_lev = 2;
            display(0,
                    "#### JVMTIagent: hotswapping class in every method entry event for every class enabled ####\n"
                    "<JVMTIagent>\tHotSwap stress level: %d\n",
                    stress_lev);
            break;
        case HOTSWAP_EVERY_SINGLE_STEP:
            stress_lev = 3;
            display(0,
                    "#### JVMTIagent: hotswapping class in every single step event enabled ####\n"
                    "<JVMTIagent>\tHotSwap stress level: %d\n",
                    stress_lev);
            break;
        case HOTSWAP_EVERY_EXCEPTION:
            stress_lev = 4;
            display(0,
                    "#### JVMTIagent: hotswapping class in every exception event enabled ####\n"
                    "<JVMTIagent>\tHotSwap stress level: %d\n",
                    stress_lev);
            break;
        case HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS:
            stress_lev = 40;
            display(0,
                    "#### JVMTIagent: hotswapping class in every exception event for every class enabled ####\n"
                    "<JVMTIagent>\tHotSwap stress level: %d\n",
                    stress_lev);
            break;
        default:
            printf("ERROR(%s,%d): JVMTIagent: unknown value of HotSwap stress level: \"%d\"\n",
                __FILE__,__LINE__,hotswap);
            return 1;
    }

    if (!NSK_JNI_VERIFY(jni_env, (shortTestName = jni_env->GetStringUTFChars(shortName, NULL)) != NULL)) {
        printf("ERROR: JVMTIagent: unable to get UTF-8 characters of the string\n");
        return 1;
    }
    display(0, "#### JVMTIagent: short name of current test is \"%s\"\n",
        shortTestName);

    if (!NSK_JNI_VERIFY(jni_env, (rasCls = jni_env->NewGlobalRef(cls)) != NULL)) {
        printf("ERROR JVMTIagent: unable to create a new global reference of the class \"RASagent\"\n");
        return 1;
    }

    if (addStressEvents() != 0) {
        printf("ERROR(%s,%d): JVMTIagent terminated abnormally! ####\n",
            __FILE__,__LINE__);
        return 1;
    }

    return 0;
}

static jint allocClsInfo(JNIEnv *jni_env, char *cls_sig, jclass clazz) {
    class_info *_clsInfo = NULL;
    jmethodID mid = NULL;
    jbyteArray classBytes;
    jboolean isCopy;

    if ((_clsInfo = (class_info*)
            malloc(sizeof(class_info))) == NULL)
        jni_env->FatalError("JVMTIagent: cannot allocate memory for class_info\n");

    /* fill the structure class_info */
    _clsInfo->clazzsig = cls_sig;

    if (!NSK_JNI_VERIFY(jni_env, ((*_clsInfo).cls = jni_env->NewGlobalRef(clazz)) != NULL)) {
        printf("ERROR: JVMTIagent: unable to create a new global reference of class \"%s\"\n",
            _clsInfo->clazzsig);
        free(_clsInfo);
        deallocClsInfo(jni_env);
        jni_env->FatalError("JVMTIagent: unable to create a new global reference of class\n");
    }

    if (!NSK_JNI_VERIFY(jni_env, (mid =
        jni_env->GetStaticMethodID(rasCls, "loadFromClassFile", "(Ljava/lang/String;)[B")) != NULL))
        jni_env->FatalError("JVMTIagent: unable to get ID of the method \"loadFromClassFile\"\n");

    classBytes = (jbyteArray) jni_env->CallStaticObjectMethod(rasCls, mid, jni_env->NewStringUTF(cls_sig));

    clearJavaException(jni_env);

    (*_clsInfo).bCount = jni_env->GetArrayLength(classBytes);

    (*_clsInfo).clsBytes =
        jni_env->GetByteArrayElements(classBytes, &isCopy);

    _clsInfo->next = NULL;

    if (clsInfo != NULL) {
        clsInfo->next = (struct class_info*) _clsInfo;
    }
    else {
        clsInfoFst = _clsInfo;
    }
    clsInfo = _clsInfo;

    return (*_clsInfo).bCount;
}

static void deallocClsInfo(JNIEnv *jni_env) {
    class_info *clsInfoCurr = clsInfoFst;

    NSK_TRACE(jni_env->DeleteGlobalRef(rasCls));

    while(clsInfoCurr != NULL) {
        class_info *_clsInfo = clsInfoCurr;

        if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*) clsInfoCurr->clazzsig)))
            jni_env->FatalError("JVMTIagent: failed to deallocate memory for clazzsig\n");

        NSK_TRACE(jni_env->DeleteGlobalRef(clsInfoCurr->cls));

        clsInfoCurr = (class_info*) clsInfoCurr->next;

        free(_clsInfo);
    }
    /* fix for 4756585: indicate that stucture class_info is empty now */
    clsInfoFst = NULL;
}

static int findAndHotSwap(JNIEnv *jni_env, jclass clazz) {
    int ret_code = 0;
    char *clazzsig = NULL;
    class_info *clsInfoCurr = clsInfoFst;

    display(1, "\n#### JVMTIagent: findAndHotSwap: obtaining class signature of class to be hotswap ...\n");
    if (!NSK_JVMTI_VERIFY(jvmti->GetClassSignature(clazz, &clazzsig, /*&generic*/NULL)))
        jni_env->FatalError("JVMTIagent: findAndHotSwap: failed to get class signature\n");
    else {
        display(1, "#### JVMTIagent: findAndHotSwap: ... class signature obtained: \"%s\"\n",
            clazzsig);

        /* enter into a raw monitor for exclusive work with redefined class */
        lock(jni_env);
        display(0, "#### JVMTIagent: findAndHotSwap: >>>>>>>> entered the raw monitor \"eventLock\" ####\n");

        while(clsInfoCurr != NULL) {
            if (hotswap == HOTSWAP_EVERY_METHOD_ENTRY_FOR_EVERY_CLASS ||
                    hotswap == HOTSWAP_EVERY_EXCEPTION_FOR_EVERY_CLASS) {
                display(1, "\n#### JVMTIagent: findAndHotSwap: going to hotswap tested class \"%s\" during execution of class \"%s\" ...\n",
                    clsInfoCurr->clazzsig, clazzsig);
                if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*) clazzsig)))
                    jni_env->FatalError("JVMTIagent: findAndHotSwap: failed to deallocate memory for clazzsig\n");

                if (doHotSwap(jni_env, clsInfoCurr->cls,
                        clsInfoCurr->bCount, clsInfoCurr->clsBytes) != 0) {
                    ret_code = 1;
                    break;
                }
            }
            else {
                if (strcmp(clazzsig, clsInfoCurr->clazzsig) == 0) {
                    display(0, "\n#### JVMTIagent: findAndHotSwap: tested class found \"%s\" ...\n",
                        clazzsig);

                    if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*) clazzsig)))
                        jni_env->FatalError("JVMTIagent: findAndHotSwap: failed to deallocate memory for clazzsig\n");

                    display(0, "\n#### JVMTIagent: findAndHotSwap: going to hotswap tested class \"%s\" ...\n",
                        clsInfoCurr->clazzsig);
                    if (doHotSwap(jni_env, clsInfoCurr->cls,
                            clsInfoCurr->bCount, clsInfoCurr->clsBytes) != 0) {
                        ret_code = 1;
                        break;
                    }
                }
            }

            clsInfoCurr = (class_info*) clsInfoCurr->next;
        }

        /* exit raw monitor */
        unlock(jni_env);
        display(0, "#### JVMTIagent: findAndHotSwap: <<<<<<<< exited from the raw monitor \"eventLock\" ####\n\n");
    }

    return ret_code;
}

static int doHotSwap(JNIEnv *jni_env, jclass redefCls, jint bCount,
        jbyte *classBytes) {
    jvmtiClassDefinition classDef;

    /* fill the structure jvmtiClassDefinition */
    classDef.klass = redefCls;
    classDef.class_byte_count = bCount;
    classDef.class_bytes = (unsigned char*) classBytes;

    display(0,
            "#### JVMTIagent: >>>>>>>> Invoke RedefineClasses():\n"
            "<JVMTIagent>\tnew class byte count=%d\n",
            classDef.class_byte_count);
    if (!NSK_JVMTI_VERIFY(jvmti->RedefineClasses(1, &classDef)))
        return 1;

    display(0, "#### JVMTIagent: <<<<<<<< RedefineClasses() is successfully done ####\n");

    return 0;
}

static int addStressEvents() {
    static int stepEventSet = JNI_FALSE;
    static int methodsEventSet = JNI_FALSE;
    static int excCatchEventSet = JNI_FALSE;

    if (stress_lev >= 3) {
        /* SingleStep events */
        if (stepEventSet == JNI_FALSE) { /* don't set the event twice */
            display(0, "#### JVMTIagent: setting SingleStep events ...\n");

            callbacks.SingleStep = &SingleStep;

            if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_SINGLE_STEP, NULL)))
                return JNI_ERR;

            stepEventSet = JNI_TRUE;

            display(0, "#### JVMTIagent: ... setting SingleStep events done\n");
        }
    }

    if (stress_lev >= 2) {
        /* MethodEntry/Exit events */
        if (methodsEventSet == JNI_FALSE) { /* don't set the event twice */
            display(0, "#### JVMTIagent: setting MethodEntry events ...\n");

            callbacks.MethodEntry = &MethodEntry;

            if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_ENTRY, NULL)))
                return JNI_ERR;

            display(0, "#### JVMTIagent: ... setting MethodEntry events done\n");

            /* MethodExit events */
            display(0, "#### JVMTIagent: setting MethodExit events ...\n");

            callbacks.MethodExit = &MethodExit;

            if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_METHOD_EXIT, NULL)))
                return JNI_ERR;

            display(0, "#### JVMTIagent: ... setting MethodExit events done\n");

            methodsEventSet = JNI_TRUE;
        }
    }

    if (stress_lev >= 1) {
        /* ExceptionCatch events */
        if (excCatchEventSet == JNI_FALSE) { /* don't set the event twice */
            display(0, "#### JVMTIagent: setting ExceptionCatch events ...\n");

            callbacks.ExceptionCatch = &ExceptionCatch;

            if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION_CATCH, NULL)))
                return JNI_ERR;

            excCatchEventSet = JNI_TRUE;

            display(0, "#### JVMTIagent: ... setting ExceptionCatch events done\n");
        }
    }

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
        return JNI_ERR;
    else
        return 0;
}

static int enableEventsCaps() {
    jvmtiCapabilities caps;

    memset(&caps, 0, sizeof(jvmtiCapabilities));

    /* add all capabilities */
    caps.can_redefine_classes = 1;
    caps.can_generate_breakpoint_events = 1;
    caps.can_generate_all_class_hook_events = 1;
    caps.can_generate_single_step_events = 1;
    caps.can_generate_method_entry_events = 1;
    caps.can_generate_method_exit_events = 1;
    caps.can_generate_exception_events = 1;
    caps.can_generate_compiled_method_load_events = 1;
    caps.can_generate_field_access_events = 1;
    caps.can_generate_field_modification_events = 1;
    caps.can_generate_frame_pop_events = 1;
    caps.can_generate_garbage_collection_events = 1;
    caps.can_generate_monitor_events = 1;
    caps.can_generate_native_method_bind_events = 1;
    caps.can_generate_object_free_events = 1;
    caps.can_generate_vm_object_alloc_events = 1;
    if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps)))
        return JNI_ERR;

    /* Breakpoint events */
    display(0, "#### JVMTIagent: setting Breakpoint events ...\n");

    callbacks.Breakpoint = &Breakpoint;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_BREAKPOINT, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting Breakpoint events done\n");

    /* ClassFileLoadHook events */
    display(0, "#### JVMTIagent: setting ClassFileLoadHook events ...\n");

    callbacks.ClassFileLoadHook = &ClassFileLoadHook;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting ClassFileLoadHook events done\n");

    /* ClassLoad events */
    display(0, "#### JVMTIagent: setting ClassLoad events ...\n");

    callbacks.ClassLoad = &ClassLoad;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting ClassLoad events done\n");

    /* ClassPrepare events */
    display(0, "#### JVMTIagent: setting ClassPrepare events ...\n");

    callbacks.ClassPrepare = &ClassPrepare;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting ClassPrepare events done\n");

    /* CompiledMethodLoad events */
    display(0, "#### JVMTIagent: setting CompiledMethodLoad events ...\n");

    callbacks.CompiledMethodLoad = &CompiledMethodLoad;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting CompiledMethodLoad events done\n");

    /* CompiledMethodUnload events */
    display(0, "#### JVMTIagent: setting CompiledMethodUnload events ...\n");

    callbacks.CompiledMethodUnload = &CompiledMethodUnload;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting CompiledMethodUnload events done\n");

    /* DataDumpRequest events */
    display(0, "#### JVMTIagent: setting DataDumpRequest events ...\n");

    callbacks.DataDumpRequest = &DataDumpRequest;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_DATA_DUMP_REQUEST, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting DataDumpRequest events done\n");

    /* DynamicCodeGenerated events */
    display(0, "#### JVMTIagent: setting DynamicCodeGenerated events ...\n");

    callbacks.DynamicCodeGenerated = &DynamicCodeGenerated;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_DYNAMIC_CODE_GENERATED, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting DynamicCodeGenerated events done\n");

    /* Exception events */
    display(0, "#### JVMTIagent: setting Exception events ...\n");

    callbacks.Exception = &Exception;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_EXCEPTION, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting Exception events done\n");

    /* FieldAccess events */
    display(0, "#### JVMTIagent: setting FieldAccess events ...\n");

    callbacks.FieldAccess = &FieldAccess;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FIELD_ACCESS, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting FieldAccess events done\n");

    /* FieldModification events */
    display(0, "#### JVMTIagent: setting FieldModification events ...\n");

    callbacks.FieldModification = &FieldModification;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FIELD_MODIFICATION, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting FieldModification events done\n");

    /* FramePop events */
    display(0, "#### JVMTIagent: setting FramePop events ...\n");

    callbacks.FramePop = &FramePop;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_FRAME_POP, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting FramePop events done\n");

    /* GarbageCollectionFinish events */
    display(0, "#### JVMTIagent: setting GarbageCollectionFinish events ...\n");

    callbacks.GarbageCollectionFinish = &GarbageCollectionFinish;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting GarbageCollectionFinish events done\n");

    /* GarbageCollectionStart events */
    display(0, "#### JVMTIagent: setting GarbageCollectionStart events ...\n");

    callbacks.GarbageCollectionStart = &GarbageCollectionStart;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_GARBAGE_COLLECTION_START, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting GarbageCollectionStart events done\n");

    /* MonitorContendedEnter events */
    display(0, "#### JVMTIagent: setting MonitorContendedEnter events ...\n");

    callbacks.MonitorContendedEnter = &MonitorContendedEnter;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTER, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting MonitorContendedEnter events done\n");

    /* MonitorContendedEntered events */
    display(0, "#### JVMTIagent: setting MonitorContendedEntered events ...\n");

    callbacks.MonitorContendedEntered = &MonitorContendedEntered;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting MonitorContendedEntered events done\n");

    /* MonitorWait events */
    display(0, "#### JVMTIagent: setting MonitorWait events ...\n");

    callbacks.MonitorWait = &MonitorWait;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAIT, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting MonitorWait events done\n");

    /* MonitorWaited events */
    display(0, "#### JVMTIagent: setting MonitorWaited events ...\n");

    callbacks.MonitorWaited = &MonitorWaited;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_MONITOR_WAITED, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting MonitorWaited events done\n");

    /* NativeMethodBind events */
    display(0, "#### JVMTIagent: setting NativeMethodBind events ...\n");

    callbacks.NativeMethodBind = &NativeMethodBind;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_NATIVE_METHOD_BIND, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting NativeMethodBind events done\n");

    /* ObjectFree events */
    display(0, "#### JVMTIagent: setting ObjectFree events ...\n");

    callbacks.ObjectFree = &ObjectFree;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_OBJECT_FREE, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting ObjectFree events done\n");

    /* ThreadEnd events */
    display(0, "#### JVMTIagent: setting ThreadEnd events ...\n");

    callbacks.ThreadEnd = &ThreadEnd;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_THREAD_END, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting ThreadEnd events done\n");

    /* ThreadStart events */
    display(0, "#### JVMTIagent: setting ThreadStart events ...\n");

    callbacks.ThreadStart = &ThreadStart;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_THREAD_START, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting ThreadStart events done\n");

    /* VMDeath events */
    display(0, "#### JVMTIagent: setting VMDeath events ...\n");

    callbacks.VMDeath = &VMDeath;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_DEATH, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting VMDeath events done\n");

    /* VMInit events */
    display(0, "#### JVMTIagent: setting VMInit events ...\n");

    callbacks.VMInit = &VMInit;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting VMInit events done\n");

    /* VMStart events */
    display(0, "#### JVMTIagent: setting VMStart events ...\n");

    callbacks.VMStart = &VMStart;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_START, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting VMStart events done\n");

    /* VMObjectAlloc events */
    display(0, "#### JVMTIagent: setting VMObjectAlloc events ...\n");

    callbacks.VMObjectAlloc = &VMObjectAlloc;

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_VM_OBJECT_ALLOC, NULL)))
        return JNI_ERR;
    display(0, "#### JVMTIagent: ... setting VMObjectAlloc events done\n");

    if (!NSK_JVMTI_VERIFY(jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks))))
        return JNI_ERR;

    return 0;
}

static void clearJavaException(JNIEnv* jni_env) {
    if (jni_env->ExceptionOccurred()) {

        jni_env->ExceptionDescribe();
        jni_env->ExceptionClear();

        jni_env->FatalError("JVMTIagent: exception occurred in java code, aborting\n");
    }
}

static int get_tok(char **src, char *buf, int buflen, char sep) {
    int i;
    char *p = *src;
    for (i = 0; i < buflen; i++) {
        if (p[i] == 0 || p[i] == sep) {
            buf[i] = 0;
            if (p[i] == sep) {
                i++;
            }
            *src += i;
            return i;
        }
        buf[i] = p[i];
    }
    /* overflow */
    return 0;
}

static void doSetup(char *str) {
    if (str == 0)
        str = (char*) "";

    if ((strcmp(str, "help")) == 0) {
        printf("#### JVMTIagent usage: -agentlib:JVMTIagent[=[help]|[=[verbose]|[verbose2],[stress0|stress1|stress2|stress3]]]\n");
        printf("####      where: help\tprint this message\n");
        printf("####             verbose\tturn verbose mode on\n");
        printf("####             verbose2\tturn extended verbose mode on (including reporting JVMTI events)\n");
        printf("####             stress0, or empty value\tturn stress level 0 on (default mode):\n");
        printf("####                   enable event generation except ExceptionCatch, MethodEntry/Exit, SingleStep\n");
        printf("####             stress1\tturn stress level 1 on:\n");
        printf("####                   enable generation of ExceptionCatch events\n");
        printf("####             stress2\tturn stress level 2 on:\n");
        printf("####                   enable generation of ExceptionCatch,\n");
        printf("####                                        MethodEntry/Exit events\n");
        printf("####             stress3\tturn stress level 3 on:\n");
        printf("####                   enable generation of ExceptionCatch,\n");
        printf("####                                        MethodEntry/Exit,\n");
        printf("####                                        SingleStep events\n");
        exit(1);
    }

    while (*str) {
        char buf[1000];

        if (!get_tok(&str, buf, sizeof(buf), ',')) {
            printf("ERROR: JVMTIagent: bad option: \"%s\"!\n", str);
            exit(1);
        }
        if ((strcmp(buf, "verbose")) == 0) {
            printf("#### JVMTIagent: turned verbose mode on ####\n");
            debug_mode = 1;
        }
        if ((strcmp(buf, "verbose2")) == 0) {
            printf("#### JVMTIagent: turned extended verbose mode on ####\n");
            debug_mode = 2;
        }
        if ((strcmp(buf, "stress0")) == 0) {
            if (debug_mode > 0)
                printf("#### JVMTIagent: turned stress level 0 on ####\n");
            stress_lev = 0;
        }
        if ((strcmp(buf, "stress1")) == 0) {
            if (debug_mode > 0)
                printf("#### JVMTIagent: turned stress level 1 on ####\n");
            stress_lev = 1;
        }
        if ((strcmp(buf, "stress2")) == 0) {
            if (debug_mode > 0)
                printf("#### JVMTIagent: turned stress level 2 on ####\n");
            stress_lev = 2;
        }
        if ((strcmp(buf, "stress3")) == 0) {
            if (debug_mode > 0)
                printf("#### JVMTIagent: turned stress level 3 on ####\n");
            stress_lev = 3;
        }
    }
}

static void getVerdict(JNIEnv *jni_env, const char *evnt) {
    char error_msg[80];

    if (vm_death_occured == TRUE) {
        sprintf(error_msg, "JVMTIagent: getVerdict: %s event occured after VMDeath",
            evnt);

        if (jni_env==NULL) { /* some event callbacks have no pointer to jni */
            printf("ERROR: %s\n", error_msg);
            exit(97);
        }
        else
            jni_env->FatalError(error_msg);
    }
}

static void display(int level, const char format[], ...) {
    va_list ar;

    if (debug_mode > level) {
        va_start(ar, format);
        vprintf(format, ar);
        va_end(ar);
    }
}

/* agent procedure */
static void JNICALL
agentProc(jvmtiEnv* jvmti_env, JNIEnv* jni_env, void* arg) {
}

JNIEXPORT jint JNICALL
Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
    /* create JVMTI environment */
    if (!NSK_VERIFY((jvmti =
            nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
        return JNI_ERR;

    doSetup(options);

    if (!NSK_JVMTI_VERIFY(jvmti->CreateRawMonitor("_event_lock", &eventLock)))
        return JNI_ERR;

    if (enableEventsCaps() == 0 && addStressEvents() == 0) {
        display(0, "#### JVMTIagent: all events were successfully enabled and capabilities/events callbacks set ####\n\n");
    } else {
        printf("ERROR(%s,%d): JVMTIagent terminated abnormally! ####\n",
            __FILE__,__LINE__);
        return JNI_ERR;
    }

    /* register agent proc and arg */
    if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
        return JNI_ERR;

    return JNI_OK;
}

}