jdk/src/demo/share/jvmti/minst/minst.c
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) 2006, 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
#include "stdlib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "minst.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "java_crw_demo.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/* Some constant maximum sizes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define MAX_TOKEN_LENGTH        80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#define MAX_METHOD_NAME_LENGTH  256
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
/* Some constant names that tie to Java class/method names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *    We assume the Java class whose static methods we will be calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *    looks like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * public class Minst {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *     private static int engaged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     private static native void _method_entry(Object thr, int cnum, int mnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     public static void method_entry(int cnum, int mnum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *         ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
#define MINST_class        Minst            /* Name of class we are using */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
#define MINST_entry        method_entry    /* Name of java entry method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
#define MINST_engaged      engaged         /* Name of java static field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
/* C macros to create strings from tokens */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#define _STRING(s) #s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
#define STRING(s) _STRING(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
/* Global agent data structure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /* JVMTI Environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    jvmtiEnv      *jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    jboolean       vm_is_dead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    jboolean       vm_is_started;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /* Data access Lock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    jrawMonitorID  lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /* Options */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    char           *include;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    char           *exclude;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /* Class Count/ID */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    jint            ccount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
} GlobalAgentData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
static GlobalAgentData *gdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
/* Enter a critical section by doing a JVMTI Raw Monitor Enter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
enter_critical_section(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    error = (*jvmti)->RawMonitorEnter(jvmti, gdata->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    check_jvmti_error(jvmti, error, "Cannot enter with raw monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
/* Exit a critical section by doing a JVMTI Raw Monitor Exit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
exit_critical_section(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    error = (*jvmti)->RawMonitorExit(jvmti, gdata->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    check_jvmti_error(jvmti, error, "Cannot exit with raw monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
/* Callback for JVMTI_EVENT_VM_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
cbVMStart(jvmtiEnv *jvmti, JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        /* Indicate VM has started */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        gdata->vm_is_started = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
/* Callback for JVMTI_EVENT_VM_INIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
cbVMInit(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        jclass   klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        jfieldID field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        /* Register Natives for class whose methods we use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        klass = (*env)->FindClass(env, STRING(MINST_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        if ( klass == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            fatal_error("ERROR: JNI: Cannot find %s with FindClass\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        STRING(MINST_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        /* Engage calls. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        field = (*env)->GetStaticFieldID(env, klass, STRING(MINST_engaged), "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if ( field == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            fatal_error("ERROR: JNI: Cannot get field from %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                        STRING(MINST_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        (*env)->SetStaticIntField(env, klass, field, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
/* Callback for JVMTI_EVENT_VM_DEATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
cbVMDeath(jvmtiEnv *jvmti, JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        jclass   klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        jfieldID field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        /* The VM has died. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        stdout_message("VMDeath\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        /* Disengage calls in MINST_class. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        klass = (*env)->FindClass(env, STRING(MINST_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if ( klass == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            fatal_error("ERROR: JNI: Cannot find %s with FindClass\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                        STRING(MINST_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        field = (*env)->GetStaticFieldID(env, klass, STRING(MINST_engaged), "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if ( field == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            fatal_error("ERROR: JNI: Cannot get field from %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        STRING(MINST_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        (*env)->SetStaticIntField(env, klass, field, -1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        /* The critical section here is important to hold back the VM death
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         *    until all other callbacks have completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        /* Since this critical section could be holding up other threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
         *   in other event callbacks, we need to indicate that the VM is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
         *   dead so that the other callbacks can short circuit their work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         *   We don't expect any further events after VmDeath but we do need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
         *   to be careful that existing threads might be in our own agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
         *   callback code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        gdata->vm_is_dead = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
/* Callback for JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
cbClassFileLoadHook(jvmtiEnv *jvmti, JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                jclass class_being_redefined, jobject loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                const char* name, jobject protection_domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                jint class_data_len, const unsigned char* class_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                jint* new_class_data_len, unsigned char** new_class_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        /* It's possible we get here right after VmDeath event, be careful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if ( !gdata->vm_is_dead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            const char *classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            /* Name could be NULL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            if ( name == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                classname = java_crw_demo_classname(class_data, class_data_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                        NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                if ( classname == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                    fatal_error("ERROR: No classname inside classfile\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                classname = strdup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                if ( classname == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            *new_class_data_len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            *new_class_data     = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            /* The tracker class itself? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            if ( interested((char*)classname, "", gdata->include, gdata->exclude)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                  &&  strcmp(classname, STRING(MINST_class)) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                jint           cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                int            system_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                unsigned char *new_image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                long           new_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                /* Get unique number for every class file image loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                cnum = gdata->ccount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                /* Is it a system class? If the class load is before VmStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                 *   then we will consider it a system class that should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                 *   be treated carefully. (See java_crw_demo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                system_class = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                if ( !gdata->vm_is_started ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                    system_class = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                new_image = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                new_length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                /* Call the class file reader/write demo code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                java_crw_demo(cnum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                    classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    class_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    class_data_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    system_class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    STRING(MINST_class), "L" STRING(MINST_class) ";",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                    STRING(MINST_entry), "(II)V",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    NULL, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    NULL, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    NULL, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    &new_image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    &new_length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                /* If we got back a new class image, return it back as "the"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                 *   new class image. This must be JVMTI Allocate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                if ( new_length > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    unsigned char *jvmti_space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    jvmti_space = (unsigned char *)allocate(jvmti, (jint)new_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                    (void)memcpy((void*)jvmti_space, (void*)new_image, (int)new_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                    *new_class_data_len = (jint)new_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                    *new_class_data     = jvmti_space; /* VM will deallocate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                /* Always free up the space we get from java_crw_demo() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                if ( new_image != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    (void)free((void*)new_image); /* Free malloc() space with free() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            (void)free((void*)classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
/* Parse the options for this minst agent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
parse_agent_options(char *options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    char token[MAX_TOKEN_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    char *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /* Parse options and set flags in gdata */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    if ( options==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    /* Get the first token from the options string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    next = get_token(options, ",=", token, sizeof(token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /* While not at the end of the options string, process this option. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    while ( next != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        if ( strcmp(token,"help")==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            stdout_message("The minst JVMTI demo agent\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            stdout_message("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            stdout_message(" java -agent:minst[=options] ...\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            stdout_message("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            stdout_message("The options are comma separated:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            stdout_message("\t help\t\t\t Print help information\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            stdout_message("\t include=item\t\t Only these classes/methods\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
            stdout_message("\t exclude=item\t\t Exclude these classes/methods\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            stdout_message("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            stdout_message("item\t Qualified class and/or method names\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            stdout_message("\t\t e.g. (*.<init>;Foobar.method;sun.*)\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            stdout_message("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        } else if ( strcmp(token,"include")==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            int   used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            int   maxlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            maxlen = MAX_METHOD_NAME_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            if ( gdata->include == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                gdata->include = (char*)calloc(maxlen+1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                used = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                used  = (int)strlen(gdata->include);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                gdata->include[used++] = ',';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                gdata->include[used] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                gdata->include = (char*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                             realloc((void*)gdata->include, used+maxlen+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if ( gdata->include == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            /* Add this item to the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            next = get_token(next, ",=", gdata->include+used, maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            /* Check for token scan error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            if ( next==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                fatal_error("ERROR: include option error\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        } else if ( strcmp(token,"exclude")==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            int   used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            int   maxlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            maxlen = MAX_METHOD_NAME_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            if ( gdata->exclude == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                gdata->exclude = (char*)calloc(maxlen+1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                used = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                used  = (int)strlen(gdata->exclude);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                gdata->exclude[used++] = ',';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                gdata->exclude[used] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                gdata->exclude = (char*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                             realloc((void*)gdata->exclude, used+maxlen+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            if ( gdata->exclude == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
            /* Add this item to the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            next = get_token(next, ",=", gdata->exclude+used, maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            /* Check for token scan error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            if ( next==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                fatal_error("ERROR: exclude option error\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        } else if ( token[0]!=0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            /* We got a non-empty token and we don't know what it is. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            fatal_error("ERROR: Unknown option: %s\n", token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        /* Get the next token (returns NULL if there are no more) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        next = get_token(next, ",=", token, sizeof(token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
/* Agent_OnLoad: This is called immediately after the shared library is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
 *   loaded. This is the first code executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
JNIEXPORT jint JNICALL
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
   376
DEF_Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    static GlobalAgentData data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    jvmtiEnv              *jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    jvmtiError             error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    jint                   res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    jvmtiCapabilities      capabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    jvmtiEventCallbacks    callbacks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /* Setup initial global agent data area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *   Use of static/extern data should be handled carefully here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *   We need to make sure that we are able to cleanup after ourselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *     so anything allocated in this library needs to be freed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *     the Agent_OnUnload() function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    (void)memset((void*)&data, 0, sizeof(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    gdata = &data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
    /* First thing we need to do is get the jvmtiEnv* or JVMTI environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    res = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    if (res != JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        /* This means that the VM was unable to obtain this version of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
         *   JVMTI interface, this is a fatal error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        fatal_error("ERROR: Unable to access JVMTI Version 1 (0x%x),"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
                " is your JDK a 5.0 or newer version?"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                " JNIEnv's GetEnv() returned %d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
               JVMTI_VERSION_1, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /* Here we save the jvmtiEnv* for Agent_OnUnload(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    gdata->jvmti = jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /* Parse any options supplied on java command line */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    parse_agent_options(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    /* Immediately after getting the jvmtiEnv* we need to ask for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     *   capabilities this agent will need. In this case we need to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     *   sure that we can get all class load hooks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    (void)memset(&capabilities,0, sizeof(capabilities));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
    capabilities.can_generate_all_class_hook_events  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    error = (*jvmti)->AddCapabilities(jvmti, &capabilities);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    check_jvmti_error(jvmti, error, "Unable to get necessary JVMTI capabilities.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    /* Next we need to provide the pointers to the callback functions to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *   to this jvmtiEnv*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
    (void)memset(&callbacks,0, sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    /* JVMTI_EVENT_VM_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    callbacks.VMStart           = &cbVMStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /* JVMTI_EVENT_VM_INIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    callbacks.VMInit           = &cbVMInit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /* JVMTI_EVENT_VM_DEATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    callbacks.VMDeath           = &cbVMDeath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /* JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    callbacks.ClassFileLoadHook = &cbClassFileLoadHook;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    error = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, (jint)sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    check_jvmti_error(jvmti, error, "Cannot set jvmti callbacks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    /* At first the only initial events we are interested in are VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *   initialization, VM death, and Class File Loads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *   Once the VM is initialized we will request more events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
    error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                          JVMTI_EVENT_VM_START, (jthread)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    check_jvmti_error(jvmti, error, "Cannot set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                          JVMTI_EVENT_VM_INIT, (jthread)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    check_jvmti_error(jvmti, error, "Cannot set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                          JVMTI_EVENT_VM_DEATH, (jthread)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
    check_jvmti_error(jvmti, error, "Cannot set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                          JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, (jthread)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    check_jvmti_error(jvmti, error, "Cannot set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    /* Here we create a raw monitor for our use in this agent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *   protect critical sections of code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    error = (*jvmti)->CreateRawMonitor(jvmti, "agent data", &(gdata->lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    check_jvmti_error(jvmti, error, "Cannot create raw monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    /* Add demo jar file to boot classpath */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    add_demo_jar_to_bootclasspath(jvmti, "minst");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    /* We return JNI_OK to signify success */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    return JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
/* Agent_OnUnload: This is called immediately before the shared library is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
 *   unloaded. This is the last code executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
JNIEXPORT void JNICALL
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
   470
DEF_Agent_OnUnload(JavaVM *vm)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    /* Make sure all malloc/calloc/strdup space is freed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    if ( gdata->include != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        (void)free((void*)gdata->include);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        gdata->include = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    if ( gdata->exclude != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        (void)free((void*)gdata->exclude);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        gdata->exclude = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
}