jdk/src/demo/share/jvmti/waiters/waiters.cpp
author bobv
Mon, 19 Oct 2015 13:41:09 -0400
changeset 33653 c1ee09fe3274
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136556: Add the ability to perform static builds of MacOSX x64 binaries Reviewed-by: ihse, bdelsart, gadams, lfoltan, rriggs, hseigel, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
     2
 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/* Example of using JVMTI events:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *      JVMTI_EVENT_VM_INIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *      JVMTI_EVENT_VM_DEATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *      JVMTI_EVENT_THREAD_START
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *      JVMTI_EVENT_THREAD_END
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *      JVMTI_EVENT_MONITOR_CONTENDED_ENTER
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *      JVMTI_EVENT_MONITOR_WAIT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *      JVMTI_EVENT_MONITOR_WAITED
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *      JVMTI_EVENT_OBJECT_FREE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#include <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
#include "jvmti.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
#include "agent_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
#include "Monitor.hpp"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
#include "Thread.hpp"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
#include "Agent.hpp"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
static jrawMonitorID vm_death_lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
static jboolean      vm_death_active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
/* Given a jvmtiEnv*, return the C++ Agent class instance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
static Agent *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
get_agent(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    Agent     *agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    agent = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    err = jvmti->GetEnvironmentLocalStorage((void**)&agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    check_jvmti_error(jvmti, err, "get env local storage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    if ( agent == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        /* This should never happen, but we should check */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        fatal_error("ERROR: GetEnvironmentLocalStorage() returned NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    return agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
/* Enter raw monitor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
menter(jvmtiEnv *jvmti, jrawMonitorID rmon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    err = jvmti->RawMonitorEnter(rmon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    check_jvmti_error(jvmti, err, "raw monitor enter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
/* Exit raw monitor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
mexit(jvmtiEnv *jvmti, jrawMonitorID rmon)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    err = jvmti->RawMonitorExit(rmon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    check_jvmti_error(jvmti, err, "raw monitor exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/* All callbacks need to be extern "C" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
extern "C" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    vm_init(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        Agent     *agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        /* Create raw monitor to protect against threads running after death */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        err = jvmti->CreateRawMonitor("Waiters vm_death lock", &vm_death_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        check_jvmti_error(jvmti, err, "create raw monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        vm_death_active = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /* Create an Agent instance, set JVMTI Local Storage */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        agent = new Agent(jvmti, env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        err = jvmti->SetEnvironmentLocalStorage((const void*)agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        check_jvmti_error(jvmti, err, "set env local storage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        /* Enable all other events we want */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                        JVMTI_EVENT_VM_DEATH, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        JVMTI_EVENT_THREAD_START, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                        JVMTI_EVENT_THREAD_END, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                        JVMTI_EVENT_MONITOR_CONTENDED_ENTER, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                        JVMTI_EVENT_MONITOR_WAIT, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                        JVMTI_EVENT_MONITOR_WAITED, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                        JVMTI_EVENT_OBJECT_FREE, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    vm_death(jvmtiEnv *jvmti, JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        Agent     *agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        /* Block all callbacks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        menter(jvmti, vm_death_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            /* Set flag for other callbacks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            vm_death_active = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            /* Inform Agent instance of VM_DEATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            agent = get_agent(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            agent->vm_death(jvmti, env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            /* Reclaim space of Agent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            err = jvmti->SetEnvironmentLocalStorage((const void*)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            check_jvmti_error(jvmti, err, "set env local storage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            delete agent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        } mexit(jvmti, vm_death_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    thread_start(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        menter(jvmti, vm_death_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            if ( !vm_death_active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                get_agent(jvmti)->thread_start(jvmti, env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        } mexit(jvmti, vm_death_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    thread_end(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        menter(jvmti, vm_death_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            if ( !vm_death_active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                get_agent(jvmti)->thread_end(jvmti, env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } mexit(jvmti, vm_death_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    monitor_contended_enter(jvmtiEnv* jvmti, JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                 jthread thread, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        menter(jvmti, vm_death_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            if ( !vm_death_active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                get_agent(jvmti)->monitor_contended_enter(jvmti, env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                                                          thread, object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } mexit(jvmti, vm_death_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    monitor_contended_entered(jvmtiEnv* jvmti, JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                   jthread thread, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        menter(jvmti, vm_death_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            if ( !vm_death_active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                get_agent(jvmti)->monitor_contended_entered(jvmti, env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                                            thread, object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } mexit(jvmti, vm_death_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    monitor_wait(jvmtiEnv* jvmti, JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                 jthread thread, jobject object, jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        menter(jvmti, vm_death_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if ( !vm_death_active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                get_agent(jvmti)->monitor_wait(jvmti, env, thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                                               object, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        } mexit(jvmti, vm_death_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    monitor_waited(jvmtiEnv* jvmti, JNIEnv *env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                   jthread thread, jobject object, jboolean timed_out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        menter(jvmti, vm_death_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            if ( !vm_death_active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                get_agent(jvmti)->monitor_waited(jvmti, env, thread,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                                 object, timed_out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } mexit(jvmti, vm_death_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    object_free(jvmtiEnv* jvmti, jlong tag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        menter(jvmti, vm_death_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            if ( !vm_death_active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                get_agent(jvmti)->object_free(jvmti, tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        } mexit(jvmti, vm_death_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    JNIEXPORT jint JNICALL
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
   246
    DEF_Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        jvmtiEnv           *jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        jint                rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        jvmtiError          err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        jvmtiCapabilities   capabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        jvmtiEventCallbacks callbacks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        /* Get JVMTI environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        rc = vm->GetEnv((void **)&jvmti, JVMTI_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        if (rc != JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            fatal_error("ERROR: Unable to create jvmtiEnv, GetEnv failed, error=%d\n", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        /* Get/Add JVMTI capabilities */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        (void)memset(&capabilities, 0, sizeof(capabilities));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        capabilities.can_generate_monitor_events        = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        capabilities.can_get_monitor_info               = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        capabilities.can_tag_objects                    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        capabilities.can_generate_object_free_events    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        err = jvmti->AddCapabilities(&capabilities);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        check_jvmti_error(jvmti, err, "add capabilities");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        /* Set all callbacks and enable VM_INIT event notification */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        memset(&callbacks, 0, sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        callbacks.VMInit                  = &vm_init;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        callbacks.VMDeath                 = &vm_death;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        callbacks.ThreadStart             = &thread_start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        callbacks.ThreadEnd               = &thread_end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        callbacks.MonitorContendedEnter   = &monitor_contended_enter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        callbacks.MonitorContendedEntered = &monitor_contended_entered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        callbacks.MonitorWait             = &monitor_wait;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        callbacks.MonitorWaited           = &monitor_waited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        callbacks.ObjectFree              = &object_free;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        err = jvmti->SetEventCallbacks(&callbacks, (jint)sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        check_jvmti_error(jvmti, err, "set event callbacks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                        JVMTI_EVENT_VM_INIT, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        check_jvmti_error(jvmti, err, "set event notify");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    /* Agent_OnUnload() is called last */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    JNIEXPORT void JNICALL
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
   291
    DEF_Agent_OnUnload(JavaVM *vm)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
} /* of extern "C" */