File: agent_tools.cpp

package info (click to toggle)
openjdk-25 25.0.1%2B8-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 825,408 kB
  • sloc: java: 5,585,680; cpp: 1,333,948; xml: 1,321,242; ansic: 488,034; asm: 404,003; objc: 21,088; sh: 15,106; javascript: 13,265; python: 8,319; makefile: 2,518; perl: 357; awk: 351; pascal: 103; exp: 83; sed: 72; jsp: 24
file content (785 lines) | stat: -rw-r--r-- 25,712 bytes parent folder | download | duplicates (4)
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
/*
 * Copyright (c) 2003, 2024, 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.
 */

#include <stdlib.h>
#include <string.h>

#include "native_thread.hpp"
#include "jni_tools.hpp"
#include "jvmti_tools.hpp"

extern "C" {

/* ============================================================================= */

/* Be careful: do not build shared library which will be linked with different
 * agent libs while global variables are used
 * Now the same source is used to build different agent libs, so these
 * variables are not shared between agents */

static jthread agentThread = nullptr;
static jvmtiStartFunction agentThreadProc = nullptr;
static void* agentThreadArg = nullptr;


typedef enum { NEW, RUNNABLE, WAITING, SUSPENDED, TERMINATED } thread_state_t;

typedef struct agent_data_t {
    volatile thread_state_t thread_state;
    int last_debuggee_status;
    jrawMonitorID monitor;
} agent_data_t;

static agent_data_t agent_data;

static jvmtiEnv* jvmti_env = nullptr;
static JavaVM* jvm = nullptr;
static JNIEnv* jni_env = nullptr;

static volatile int currentAgentStatus = NSK_STATUS_PASSED;

/* ============================================================================= */

void nsk_jvmti_setFailStatus() {
    currentAgentStatus = NSK_STATUS_FAILED;
}

int nsk_jvmti_isFailStatus() {
    return (nsk_jvmti_getStatus() != NSK_STATUS_PASSED);
}

jint nsk_jvmti_getStatus() {
    return currentAgentStatus;
}

/* ============================================================================= */
static jvmtiError init_agent_data(jvmtiEnv *jvmti_env, agent_data_t *data) {
    data->thread_state = NEW;
    data->last_debuggee_status = NSK_STATUS_PASSED;

    return jvmti_env->CreateRawMonitor("agent_data_monitor", &data->monitor);
}

/** Reset agent data to prepare for another run. */
void nsk_jvmti_resetAgentData() {
    rawMonitorEnter(jvmti_env, agent_data.monitor);
    /* wait for agentThreadWrapper() to finish */
    while (agent_data.thread_state != TERMINATED) {
        rawMonitorWait(jvmti_env, agent_data.monitor, 10);
    }
    agent_data.thread_state = NEW;
    agent_data.last_debuggee_status = NSK_STATUS_PASSED;
    rawMonitorExit(jvmti_env, agent_data.monitor);
}

static jvmtiError free_agent_data(jvmtiEnv *jvmti_env, agent_data_t *data) {
    return jvmti_env->DestroyRawMonitor(data->monitor);
}

/** Create JVMTI environment. */
jvmtiEnv* nsk_jvmti_createJVMTIEnv(JavaVM* javaVM, void* reserved) {
    jvm = javaVM;
    if (!NSK_VERIFY(javaVM->GetEnv((void **)&jvmti_env, JVMTI_VERSION_1_1) == JNI_OK)) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    if (!NSK_JVMTI_VERIFY(init_agent_data(jvmti_env, &agent_data))) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    return jvmti_env;
}

/** Dispose JVMTI environment */
static int nsk_jvmti_disposeJVMTIEnv(jvmtiEnv* jvmti_env) {
    if (jvmti_env != nullptr) {
        if (!NSK_JVMTI_VERIFY(jvmti_env->DisposeEnvironment())) {
            nsk_jvmti_setFailStatus();
            return NSK_FALSE;
        }

        if (!NSK_JVMTI_VERIFY(free_agent_data(jvmti_env, &agent_data))) {
            nsk_jvmti_setFailStatus();
            return NSK_FALSE;
        }
    }
    return NSK_TRUE;
}

/** Get JNI environment for agent thread. */
JNIEnv* nsk_jvmti_getAgentJNIEnv() {
    return jni_env;
}

/** Get JVMTI environment for agent */
jvmtiEnv* nsk_jvmti_getAgentJVMTIEnv() {
    return jvmti_env;
}

/** Wrapper for user agent thread. */
static void JNICALL
agentThreadWrapper(jvmtiEnv* jvmti_env, JNIEnv* agentJNI, void* arg) {
    jni_env = agentJNI;

    rawMonitorEnter(jvmti_env, agent_data.monitor);
    agent_data.thread_state = RUNNABLE;
    rawMonitorNotify(jvmti_env, agent_data.monitor);
    rawMonitorExit(jvmti_env, agent_data.monitor);

    NSK_TRACE((*agentThreadProc)(jvmti_env, agentJNI, agentThreadArg));

    rawMonitorEnter(jvmti_env, agent_data.monitor);
    agent_data.thread_state = TERMINATED;
    agentJNI->DeleteGlobalRef(agentThread);
    agentThread = nullptr;
    rawMonitorNotify(jvmti_env, agent_data.monitor);
    rawMonitorExit(jvmti_env, agent_data.monitor);

}

/** Start wrapper for user agent thread. */
static jthread startAgentThreadWrapper(JNIEnv *jni_env, jvmtiEnv* jvmti_env) {
    const jint  THREAD_PRIORITY = JVMTI_THREAD_MAX_PRIORITY;
    const char* THREAD_NAME = "JVMTI agent thread";
    const char* THREAD_CLASS_NAME = "java/lang/Thread";
    const char* THREAD_CTOR_NAME = "<init>";
    const char* THREAD_CTOR_SIGNATURE = "(Ljava/lang/String;)V";

    jobject threadName = nullptr;
    jclass threadClass = nullptr;
    jmethodID threadCtor = nullptr;
    jobject threadObject = nullptr;
    jobject threadGlobalRef = nullptr;

    if (!NSK_JNI_VERIFY(jni_env, (threadClass = jni_env->FindClass(THREAD_CLASS_NAME)) != nullptr)) {
        return nullptr;
    }

    if (!NSK_JNI_VERIFY(jni_env, (threadCtor =
            jni_env->GetMethodID(threadClass, THREAD_CTOR_NAME, THREAD_CTOR_SIGNATURE)) != nullptr))
        return nullptr;

    if (!NSK_JNI_VERIFY(jni_env, (threadName = jni_env->NewStringUTF(THREAD_NAME)) != nullptr))
        return nullptr;

    if (!NSK_JNI_VERIFY(jni_env, (threadObject =
            jni_env->NewObject(threadClass, threadCtor, threadName)) != nullptr))
        return nullptr;

    if (!NSK_JNI_VERIFY(jni_env, (threadGlobalRef =
            jni_env->NewGlobalRef(threadObject)) != nullptr)) {
        jni_env->DeleteLocalRef(threadObject);
        return nullptr;
    }
    agentThread = (jthread)threadGlobalRef;

    if (!NSK_JVMTI_VERIFY(
            jvmti_env->RunAgentThread(agentThread, &agentThreadWrapper, agentThreadArg, THREAD_PRIORITY))) {
        jni_env->DeleteGlobalRef(threadGlobalRef);
        jni_env->DeleteLocalRef(threadObject);
        return nullptr;
    }
    return agentThread;
}

/** Register user agent thread with arg. */
int nsk_jvmti_setAgentProc(jvmtiStartFunction proc, void* arg) {
    agentThreadProc = proc;
    agentThreadArg = arg;
    return NSK_TRUE;
}

/** Get agent thread ref. */
jthread nsk_jvmti_getAgentThread() {
    return agentThread;
}

/** Run registered user agent thread via wrapper. */
static jthread nsk_jvmti_runAgentThread(JNIEnv *jni_env, jvmtiEnv* jvmti_env) {
    /* start agent thread wrapper */
    jthread thread = startAgentThreadWrapper(jni_env, jvmti_env);
    if (thread == nullptr) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    return thread;
}

/* ============================================================================= */

/** Sleep current thread. */
void nsk_jvmti_sleep(jlong timeout) {
    int seconds = (int)((timeout + 999) / 1000);
    THREAD_sleep(seconds);
}

/** Sync point called from Java code. */
static jint syncDebuggeeStatus(JNIEnv* jni_env, jvmtiEnv* jvmti_env, jint debuggeeStatus) {
    jint result = NSK_STATUS_FAILED;

    rawMonitorEnter(jvmti_env, agent_data.monitor);

    /* save last debugee status */
    agent_data.last_debuggee_status = debuggeeStatus;

    /* we don't enter if-stmt in second call */
    if (agent_data.thread_state == NEW) {
        if (nsk_jvmti_runAgentThread(jni_env, jvmti_env) == nullptr)
            goto monitor_exit_and_return;

        /* SP2.2-w - wait for agent thread */
        while (agent_data.thread_state == NEW) {
            rawMonitorWait(jvmti_env, agent_data.monitor, 0);
        }
    }

    /* wait for sync permit */
    /* we don't enter loop in first call */
    while (agent_data.thread_state != WAITING && agent_data.thread_state != TERMINATED) {
        /* SP4.2-w - second wait for agent thread */
        rawMonitorWait(jvmti_env, agent_data.monitor, 0);
    }

    if (agent_data.thread_state != TERMINATED) {
        agent_data.thread_state = SUSPENDED;
        /* SP3.2-n - notify to start test */
        /* SP6.2-n - notify to end test */
        rawMonitorNotify(jvmti_env, agent_data.monitor);
    }
    else {
        NSK_COMPLAIN0("Debuggee status sync aborted because agent thread has finished\n");
        goto monitor_exit_and_return;
    }

    /* update status from debuggee */
    if (debuggeeStatus != NSK_STATUS_PASSED) {
        nsk_jvmti_setFailStatus();
    }

    while (agent_data.thread_state == SUSPENDED) {
        /* SP5.2-w - wait while testing */
        /* SP7.2 - wait for agent end */
        rawMonitorWait(jvmti_env, agent_data.monitor, 0);
    }

    agent_data.last_debuggee_status = nsk_jvmti_getStatus();
    result = agent_data.last_debuggee_status;

monitor_exit_and_return:
    rawMonitorExit(jvmti_env, agent_data.monitor);
    return result;
}

/** Wait for sync point with Java code. */
int nsk_jvmti_waitForSync(jlong timeout) {
    static const int inc_timeout = 1000;

    jlong t = 0;
    int result = NSK_TRUE;

    rawMonitorEnter(jvmti_env, agent_data.monitor);

    agent_data.thread_state = WAITING;

    /* SP2.2-n - notify agent is waiting and wait */
    /* SP4.1-n - notify agent is waiting and wait */
    rawMonitorNotify(jvmti_env, agent_data.monitor);

    while (agent_data.thread_state == WAITING) {
        /* SP3.2-w - wait to start test */
        /* SP6.2-w - wait to end test */
        rawMonitorWait(jvmti_env, agent_data.monitor, inc_timeout);

        if (timeout == 0) continue;

        t += inc_timeout;

        if (t >= timeout) break;
    }

    if (agent_data.thread_state == WAITING) {
        NSK_COMPLAIN1("No status sync occured for timeout: %" LL "d ms\n", timeout);
        nsk_jvmti_setFailStatus();
        result = NSK_FALSE;
    }

    rawMonitorExit(jvmti_env, agent_data.monitor);

    return result;
}

/** Resume java code suspended on sync point. */
int nsk_jvmti_resumeSync() {
    int result;
    rawMonitorEnter(jvmti_env, agent_data.monitor);

    if (agent_data.thread_state == SUSPENDED) {
        result = NSK_TRUE;
        agent_data.thread_state = RUNNABLE;
        /* SP5.2-n - notify suspend done */
        /* SP7.2-n - notify agent end */
        rawMonitorNotify(jvmti_env, agent_data.monitor);
    }
    else {
        NSK_COMPLAIN0("Debuggee was not suspended on status sync\n");
        nsk_jvmti_setFailStatus();
        result = NSK_FALSE;
    }

    rawMonitorExit(jvmti_env, agent_data.monitor);
    return NSK_TRUE;
}

/** Native function for Java code to provide sync point. */
JNIEXPORT jint JNICALL
Java_nsk_share_jvmti_DebugeeClass_checkStatus(JNIEnv* jni_env, jclass cls, jint debuggeeStatus) {
    jint status;
    NSK_TRACE(status = syncDebuggeeStatus(jni_env, jvmti_env, debuggeeStatus));
    return status;
}

/** Native function for Java code to reset agent data. */
JNIEXPORT void JNICALL
Java_nsk_share_jvmti_DebugeeClass_resetAgentData(JNIEnv* jni_env, jclass cls) {
    NSK_TRACE(nsk_jvmti_resetAgentData());
}

/* ============================================================================= */

/** Find loaded class by signature. */
jclass nsk_jvmti_classBySignature(const char signature[]) {
    jclass* classes = nullptr;
    jint count = 0;
    jclass foundClass = nullptr;
    int i;

    if (!NSK_VERIFY(signature != nullptr)) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    if (!NSK_JVMTI_VERIFY(jvmti_env->GetLoadedClasses(&count, &classes))) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    for (i = 0; i < count; i++) {
        char* sig = nullptr;
        char* generic = nullptr;

        if (!NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(classes[i], &sig, &generic))) {
            nsk_jvmti_setFailStatus();
            break;
        }

        if (sig != nullptr && strcmp(signature, sig) == 0) {
            foundClass = classes[i];
        }

        if (!(NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)sig))
                && NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)generic)))) {
            nsk_jvmti_setFailStatus();
            break;
        }

        if (foundClass != nullptr)
            break;
    }

    if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)classes))) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    if (!NSK_JNI_VERIFY(jni_env, (foundClass = (jclass)
                jni_env->NewGlobalRef(foundClass)) != nullptr)) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    return foundClass;
}

/** Find alive thread by name. */
jthread nsk_jvmti_threadByName(const char name[]) {
    jthread* threads = nullptr;
    jint count = 0;
    jthread foundThread = nullptr;
    int i;

    if (!NSK_VERIFY(name != nullptr)) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    if (!NSK_JVMTI_VERIFY(jvmti_env->GetAllThreads(&count, &threads))) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    for (i = 0; i < count; i++) {
        jvmtiThreadInfo info;

        if (!NSK_JVMTI_VERIFY(jvmti_env->GetThreadInfo(threads[i], &info))) {
            nsk_jvmti_setFailStatus();
            break;
        }

        if (info.name != nullptr && strcmp(name, info.name) == 0) {
            foundThread = threads[i];
        }
        if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)info.name))) {
            nsk_jvmti_setFailStatus();
            return nullptr;
        }
        if (foundThread != nullptr) {
            break;
        }
    }

    if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)threads))) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    if (!NSK_JNI_VERIFY(jni_env, (foundThread = (jthread)
                jni_env->NewGlobalRef(foundThread)) != nullptr)) {
        nsk_jvmti_setFailStatus();
        return nullptr;
    }

    return foundThread;
}


/* ============================================================================= */

/** Add all capabilities for finding line locations. */
int nsk_jvmti_addLocationCapabilities() {
    jvmtiCapabilities caps;

    memset(&caps, 0, sizeof(caps));
    caps.can_get_line_numbers = 1;
    if (!NSK_JVMTI_VERIFY(jvmti_env->AddCapabilities(&caps)))
        return NSK_FALSE;

    return NSK_TRUE;
}

/** Add all capabilities for using breakpoints. */
int nsk_jvmti_addBreakpointCapabilities() {
    jvmtiCapabilities caps;

    if (!nsk_jvmti_addLocationCapabilities())
        return NSK_FALSE;

    memset(&caps, 0, sizeof(caps));
    caps.can_generate_breakpoint_events = 1;
    if (!NSK_JVMTI_VERIFY(jvmti_env->AddCapabilities(&caps)))
        return NSK_FALSE;

    return NSK_TRUE;
}

/** Find line location. */
jlocation nsk_jvmti_getLineLocation(jclass cls, jmethodID method, int line) {
    jint count = 0;
    jvmtiLineNumberEntry* table = nullptr;
    jlocation location = NSK_JVMTI_INVALID_JLOCATION;
    int i;

    if (!NSK_JVMTI_VERIFY(jvmti_env->GetLineNumberTable(method, &count, &table)))
        return NSK_JVMTI_INVALID_JLOCATION;

    for (i = 0; i < count; i++) {
        if (table[i].line_number == line) {
            location = table[i].start_location;
            break;
        }
    }

    if (!NSK_JVMTI_VERIFY(jvmti_env->Deallocate((unsigned char*)table)))
        return NSK_JVMTI_INVALID_JLOCATION;

    return location;
}

/** Set breakpoint to a line. */
jlocation nsk_jvmti_setLineBreakpoint(jclass cls, jmethodID method, int line) {
    jlocation location = NSK_JVMTI_INVALID_JLOCATION;

    if (!NSK_VERIFY((location =
            nsk_jvmti_getLineLocation(cls, method, line)) != NSK_JVMTI_INVALID_JLOCATION))
        return NSK_JVMTI_INVALID_JLOCATION;

    if (!NSK_JVMTI_VERIFY(jvmti_env->SetBreakpoint(method, location)))
        return NSK_JVMTI_INVALID_JLOCATION;

    return location;
}

/** Remove breakpoint from a line. */
jlocation nsk_jvmti_clearLineBreakpoint(jclass cls, jmethodID method, int line) {
    jlocation location = NSK_JVMTI_INVALID_JLOCATION;

    if (!NSK_VERIFY((location =
            nsk_jvmti_getLineLocation(cls, method, line)) != NSK_JVMTI_INVALID_JLOCATION))
        return NSK_JVMTI_INVALID_JLOCATION;

    if (!NSK_JVMTI_VERIFY(jvmti_env->ClearBreakpoint(method, location)))
        return NSK_JVMTI_INVALID_JLOCATION;

    return location;
}

/* ============================================================================= */

/** Enable or disable given events. */
int nsk_jvmti_enableEvents(jvmtiEventMode enable, int size, jvmtiEvent list[], jthread thread) {
    int i;

    for (i = 0; i < size; i++) {
        if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventNotificationMode(enable, list[i], thread))) {
            nsk_jvmti_setFailStatus();
            return NSK_FALSE;
        }
    }
    return NSK_TRUE;
}

/* ============================================================================= */

typedef jint (JNICALL *checkStatus_type)(JNIEnv* jni_env, jclass cls, jint debuggeeStatus);

static checkStatus_type checkStatus_func = nullptr;

/**
 * Proxy function to gain sequential access to checkStatus of each agent
 */
JNIEXPORT jint JNICALL
MA_checkStatus(JNIEnv* jni_env, jclass cls, jint debuggeeStatus) {
    jint status;

    NSK_TRACE(status = syncDebuggeeStatus(jni_env, jvmti_env, debuggeeStatus));
    return (*checkStatus_func)(jni_env, cls, status);
}

/**
 * nativeMethodBind callback:
 *      if needed, redirects checkStatus native method call
 */
static void JNICALL nativeMethodBind(jvmtiEnv* jvmti_env, JNIEnv *jni_env,
                              jthread thread, jmethodID mid,
                              void* address, void** new_address_ptr) {
    const char* BIND_CLASS_NAME = "Lnsk/share/jvmti/DebugeeClass;";
    const char* BIND_METHOD_NAME = "checkStatus";
    const char* BIND_METHOD_SIGNATURE = "(I)I";

    jvmtiPhase phase;
    jclass cls;
    char *class_sig = nullptr;
    char *name = nullptr;
    char *sig = nullptr;

    if (!NSK_JVMTI_VERIFY(jvmti_env->GetPhase(&phase))) {
        nsk_jvmti_setFailStatus();
        return;
    }

    if (phase != JVMTI_PHASE_START && phase != JVMTI_PHASE_LIVE)
        return;

    if (NSK_JVMTI_VERIFY(jvmti_env->GetMethodName(mid, &name, &sig, nullptr))) {
        if (strcmp(name, BIND_METHOD_NAME) == 0 &&
                strcmp(sig, BIND_METHOD_SIGNATURE) == 0) {

            if (NSK_JVMTI_VERIFY(jvmti_env->GetMethodDeclaringClass(mid, &cls))
             && NSK_JVMTI_VERIFY(jvmti_env->GetClassSignature(cls, &class_sig, nullptr))
             && strcmp(class_sig, BIND_CLASS_NAME) == 0
             && address != (void*)Java_nsk_share_jvmti_DebugeeClass_checkStatus) {
                checkStatus_func = (checkStatus_type)address;
                NSK_TRACE(*new_address_ptr = (void*)MA_checkStatus);
            }
        }
    }

    if (name != nullptr)
        jvmti_env->Deallocate((unsigned char*)name);

    if (sig != nullptr)
        jvmti_env->Deallocate((unsigned char*)sig);

    if (class_sig != nullptr)
        jvmti_env->Deallocate((unsigned char*)class_sig);
}

/**
 * Initialize multiple agent:
 *      establish processing of nativeMethodBind events
 */
int nsk_jvmti_init_MA(jvmtiEventCallbacks* callbacks) {

    if (callbacks == nullptr) {
        NSK_COMPLAIN0("callbacks should not be null\n");
        nsk_jvmti_setFailStatus();
        return NSK_FALSE;
    }

    if (callbacks->NativeMethodBind != nullptr) {
        NSK_COMPLAIN0("callbacks.NativeMethodBind should be null\n");
        nsk_jvmti_setFailStatus();
        return NSK_FALSE;
    }

    {
        jvmtiCapabilities caps;
        memset(&caps, 0, sizeof(caps));
        caps.can_generate_native_method_bind_events = 1;
        if (!NSK_JVMTI_VERIFY(jvmti_env->AddCapabilities(&caps)))
            return NSK_FALSE;
    }

    callbacks->NativeMethodBind = nativeMethodBind;
    if (!NSK_JVMTI_VERIFY(jvmti_env->SetEventCallbacks(callbacks, sizeof(jvmtiEventCallbacks))))
        return NSK_FALSE;

    if (!NSK_JVMTI_VERIFY(
            jvmti_env->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_NATIVE_METHOD_BIND, nullptr)))
        return NSK_FALSE;

    return NSK_TRUE;
}

/* ============================================================================= */

int nsk_jvmti_isOptionalEvent(jvmtiEvent event) {

    return (event == JVMTI_EVENT_EXCEPTION)
        || (event == JVMTI_EVENT_EXCEPTION_CATCH)
        || (event == JVMTI_EVENT_SINGLE_STEP)
        || (event == JVMTI_EVENT_FRAME_POP)
        || (event == JVMTI_EVENT_BREAKPOINT)
        || (event == JVMTI_EVENT_FIELD_ACCESS)
        || (event == JVMTI_EVENT_FIELD_MODIFICATION)
        || (event == JVMTI_EVENT_METHOD_ENTRY)
        || (event == JVMTI_EVENT_METHOD_EXIT)
        || (event == JVMTI_EVENT_NATIVE_METHOD_BIND)
        || (event == JVMTI_EVENT_COMPILED_METHOD_LOAD)
        || (event == JVMTI_EVENT_COMPILED_METHOD_UNLOAD)
        || (event == JVMTI_EVENT_MONITOR_WAIT)
        || (event == JVMTI_EVENT_MONITOR_WAITED)
        || (event == JVMTI_EVENT_MONITOR_CONTENDED_ENTER)
        || (event == JVMTI_EVENT_MONITOR_CONTENDED_ENTERED)
        || (event == JVMTI_EVENT_GARBAGE_COLLECTION_START)
        || (event == JVMTI_EVENT_GARBAGE_COLLECTION_FINISH)
        || (event == JVMTI_EVENT_OBJECT_FREE)
        || (event == JVMTI_EVENT_VM_OBJECT_ALLOC);
}

/* ============================================================================= */

void nsk_jvmti_showPossessedCapabilities(jvmtiEnv *jvmti_env) {

    jvmtiCapabilities caps;

    if (!NSK_JVMTI_VERIFY(jvmti_env->GetCapabilities(&caps))) {
        return;
    }

    NSK_DISPLAY0("\n");
    NSK_DISPLAY0("Possessed capabilities:\n");
    NSK_DISPLAY0("-----------------------\n");
    if (caps.can_tag_objects)
        NSK_DISPLAY0("\tcan_tag_objects\n");
    if (caps.can_generate_field_modification_events)
        NSK_DISPLAY0("\tcan_generate_field_modification_events\n");
    if (caps.can_generate_field_access_events)
        NSK_DISPLAY0("\tcan_generate_field_access_events\n");
    if (caps.can_get_bytecodes)
        NSK_DISPLAY0("\tcan_get_bytecodes\n");
    if (caps.can_get_synthetic_attribute)
        NSK_DISPLAY0("\tcan_get_synthetic_attribute\n");
    if (caps.can_get_owned_monitor_info)
        NSK_DISPLAY0("\tcan_get_owned_monitor_info\n");
    if (caps.can_get_current_contended_monitor)
        NSK_DISPLAY0("\tcan_get_current_contended_monitor\n");
    if (caps.can_get_monitor_info)
        NSK_DISPLAY0("\tcan_get_monitor_info\n");
    if (caps.can_pop_frame)
        NSK_DISPLAY0("\tcan_pop_frame\n");
    if (caps.can_redefine_classes)
        NSK_DISPLAY0("\tcan_redefine_classes\n");
    if (caps.can_signal_thread)
        NSK_DISPLAY0("\tcan_signal_thread\n");
    if (caps.can_get_source_file_name)
        NSK_DISPLAY0("\tcan_get_source_file_name\n");
    if (caps.can_get_line_numbers)
        NSK_DISPLAY0("\tcan_get_line_numbers\n");
    if (caps.can_get_source_debug_extension)
        NSK_DISPLAY0("\tcan_get_source_debug_extension\n");
    if (caps.can_access_local_variables)
        NSK_DISPLAY0("\tcan_access_local_variables\n");
    if (caps.can_maintain_original_method_order)
        NSK_DISPLAY0("\tcan_maintain_original_method_order\n");
    if (caps.can_generate_single_step_events)
        NSK_DISPLAY0("\tcan_generate_single_step_events\n");
    if (caps.can_generate_exception_events)
        NSK_DISPLAY0("\tcan_generate_exception_events\n");
    if (caps.can_generate_frame_pop_events)
        NSK_DISPLAY0("\tcan_generate_frame_pop_events\n");
    if (caps.can_generate_breakpoint_events)
        NSK_DISPLAY0("\tcan_generate_breakpoint_events\n");
    if (caps.can_suspend)
        NSK_DISPLAY0("\tcan_suspend\n");
    if (caps.can_get_current_thread_cpu_time)
        NSK_DISPLAY0("\tcan_get_current_thread_cpu_time\n");
    if (caps.can_get_thread_cpu_time)
        NSK_DISPLAY0("\tcan_get_thread_cpu_time\n");
    if (caps.can_generate_method_entry_events)
        NSK_DISPLAY0("\tcan_generate_method_entry_events\n");
    if (caps.can_generate_method_exit_events)
        NSK_DISPLAY0("\tcan_generate_method_exit_events\n");
    if (caps.can_generate_all_class_hook_events)
        NSK_DISPLAY0("\tcan_generate_all_class_hook_events\n");
    if (caps.can_generate_compiled_method_load_events)
        NSK_DISPLAY0("\tcan_generate_compiled_method_load_events\n");
    if (caps.can_generate_monitor_events)
        NSK_DISPLAY0("\tcan_generate_monitor_events\n");
    if (caps.can_generate_vm_object_alloc_events)
        NSK_DISPLAY0("\tcan_generate_vm_object_alloc_events\n");
    if (caps.can_generate_native_method_bind_events)
        NSK_DISPLAY0("\tcan_generate_native_method_bind_events\n");
    if (caps.can_generate_garbage_collection_events)
        NSK_DISPLAY0("\tcan_generate_garbage_collection_events\n");
    if (caps.can_generate_object_free_events)
        NSK_DISPLAY0("\tcan_generate_object_free_events\n");

    NSK_DISPLAY0("\n");
}

/* ============================================================================= */

}