jdk/src/share/demo/jvmti/heapViewer/heapViewer.c
author jgish
Thu, 18 Apr 2013 16:33:11 -0400
changeset 17159 bb566a21b661
parent 14342 8435a30053c1
permissions -rw-r--r--
8012005: LogManager needs test to ensure stack trace is not being done to find bundle Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10292
diff changeset
     2
 * Copyright (c) 2004, 2011, 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 <stdio.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include <stddef.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
#include "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
#include "jvmti.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
#include "agent_util.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
/* Global static data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    jboolean      vmDeathCalled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    jboolean      dumpInProgress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    jrawMonitorID lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
} GlobalData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
static GlobalData globalData, *gdata = &globalData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
/* Typedef to hold class details */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    char *signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    int   count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    int   space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
} ClassDetails;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
/* Enter agent monitor protected section */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
enterAgentMonitor(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    err = (*jvmti)->RawMonitorEnter(jvmti, gdata->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    check_jvmti_error(jvmti, err, "raw monitor enter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
/* Exit agent monitor protected section */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
exitAgentMonitor(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    err = (*jvmti)->RawMonitorExit(jvmti, gdata->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    check_jvmti_error(jvmti, err, "raw monitor exit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
/* Heap object callback */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
static jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
cbHeapObject(jlong class_tag, jlong size, jlong* tag_ptr, jint length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
           void* user_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    if ( class_tag != (jlong)0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        ClassDetails *d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        d = (ClassDetails*)(void*)(ptrdiff_t)class_tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        (*((jint*)(user_data)))++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        d->count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        d->space += (int)size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    return JVMTI_VISIT_OBJECTS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
/* Compare two ClassDetails */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
compareDetails(const void *p1, const void *p2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    return ((ClassDetails*)p2)->space - ((ClassDetails*)p1)->space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
/* Callback for JVMTI_EVENT_DATA_DUMP_REQUEST (Ctrl-\ or at exit) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
dataDumpRequest(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    enterAgentMonitor(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if ( !gdata->vmDeathCalled && !gdata->dumpInProgress ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            jvmtiHeapCallbacks heapCallbacks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            ClassDetails      *details;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            jvmtiError         err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            jclass            *classes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            jint               totalCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            jint               count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            jint               i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            gdata->dumpInProgress = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            /* Get all the loaded classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            err = (*jvmti)->GetLoadedClasses(jvmti, &count, &classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            check_jvmti_error(jvmti, err, "get loaded classes");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            /* Setup an area to hold details about these classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            details = (ClassDetails*)calloc(sizeof(ClassDetails), count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if ( details == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                fatal_error("ERROR: Ran out of malloc space\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            for ( i = 0 ; i < count ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                char *sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                /* Get and save the class signature */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                err = (*jvmti)->GetClassSignature(jvmti, classes[i], &sig, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                check_jvmti_error(jvmti, err, "get class signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                if ( sig == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    fatal_error("ERROR: No class signature found\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                details[i].signature = strdup(sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                deallocate(jvmti, sig);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                /* Tag this jclass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                err = (*jvmti)->SetTag(jvmti, classes[i],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                                    (jlong)(ptrdiff_t)(void*)(&details[i]));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                check_jvmti_error(jvmti, err, "set object tag");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            /* Iterate through the heap and count up uses of jclass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            (void)memset(&heapCallbacks, 0, sizeof(heapCallbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            heapCallbacks.heap_iteration_callback = &cbHeapObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            totalCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            err = (*jvmti)->IterateThroughHeap(jvmti,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                       JVMTI_HEAP_FILTER_CLASS_UNTAGGED, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                       &heapCallbacks, (const void *)&totalCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            check_jvmti_error(jvmti, err, "iterate through heap");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            /* Remove tags */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            for ( i = 0 ; i < count ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                /* Un-Tag this jclass */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                err = (*jvmti)->SetTag(jvmti, classes[i], (jlong)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                check_jvmti_error(jvmti, err, "set object tag");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            /* Sort details by space used */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            qsort(details, count, sizeof(ClassDetails), &compareDetails);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            /* Print out sorted table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            stdout_message("Heap View, Total of %d objects found.\n\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                         totalCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            stdout_message("Space      Count      Class Signature\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            stdout_message("---------- ---------- ----------------------\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            for ( i = 0 ; i < count ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if ( details[i].space == 0 || i > 20 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                stdout_message("%10d %10d %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    details[i].space, details[i].count, details[i].signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            stdout_message("---------- ---------- ----------------------\n\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            /* Free up all allocated space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            deallocate(jvmti, classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            for ( i = 0 ; i < count ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                if ( details[i].signature != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    free(details[i].signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            free(details);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            gdata->dumpInProgress = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    } exitAgentMonitor(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
/* Callback for JVMTI_EVENT_VM_INIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
vmInit(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    enterAgentMonitor(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        jvmtiError          err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                            JVMTI_EVENT_DATA_DUMP_REQUEST, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        check_jvmti_error(jvmti, err, "set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    } exitAgentMonitor(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
/* Callback for JVMTI_EVENT_VM_DEATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
vmDeath(jvmtiEnv *jvmti, JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    jvmtiError          err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /* Make sure everything has been garbage collected */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    err = (*jvmti)->ForceGarbageCollection(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    check_jvmti_error(jvmti, err, "force garbage collection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /* Disable events and dump the heap information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    enterAgentMonitor(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_DISABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                            JVMTI_EVENT_DATA_DUMP_REQUEST, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        check_jvmti_error(jvmti, err, "set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        dataDumpRequest(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        gdata->vmDeathCalled = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
    } exitAgentMonitor(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
/* Agent_OnLoad() is called first, we prepare for a VM_INIT event here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    jint                rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    jvmtiError          err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    jvmtiCapabilities   capabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    jvmtiEventCallbacks callbacks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    jvmtiEnv           *jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /* Get JVMTI environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    jvmti = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    rc = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    if (rc != JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        fatal_error("ERROR: Unable to create jvmtiEnv, error=%d\n", rc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    if ( jvmti == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        fatal_error("ERROR: No jvmtiEnv* returned from GetEnv\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    /* Get/Add JVMTI capabilities */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    (void)memset(&capabilities, 0, sizeof(capabilities));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    capabilities.can_tag_objects = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    capabilities.can_generate_garbage_collection_events = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    err = (*jvmti)->AddCapabilities(jvmti, &capabilities);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    check_jvmti_error(jvmti, err, "add capabilities");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /* Create the raw monitor */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    err = (*jvmti)->CreateRawMonitor(jvmti, "agent lock", &(gdata->lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    check_jvmti_error(jvmti, err, "create raw monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    /* Set callbacks and enable event notifications */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    memset(&callbacks, 0, sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    callbacks.VMInit                  = &vmInit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    callbacks.VMDeath                 = &vmDeath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
    callbacks.DataDumpRequest         = &dataDumpRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    check_jvmti_error(jvmti, err, "set event callbacks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                        JVMTI_EVENT_VM_INIT, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    check_jvmti_error(jvmti, err, "set event notifications");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                        JVMTI_EVENT_VM_DEATH, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    check_jvmti_error(jvmti, err, "set event notifications");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
/* Agent_OnUnload() is called last */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
Agent_OnUnload(JavaVM *vm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
}