jdk/src/jdk.hprof.agent/share/native/libhprof/hprof_init.c
author sla
Tue, 26 Aug 2014 07:55:08 +0200
changeset 26201 40a873d21081
parent 25859 jdk/src/demo/share/jvmti/hprof/hprof_init.c@3317bb8137f4
permissions -rw-r--r--
8043936: Drop HPROF as demo, keep as HPROF agent shipped with JDK Reviewed-by: erikj, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
24967
582420f5ab6c 8041498: Remove npt library
dsamersoff
parents: 23056
diff changeset
     2
 * Copyright (c) 2003, 2014, 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
/* Main source file, the basic JVMTI connection/startup code. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "hprof.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
#include "java_crw_demo.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * This file contains all the startup logic (Agent_Onload) and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *   connection to the JVMTI interface.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * All JVMTI Event callbacks are in this file.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * All setting of global data (gdata) is done here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * Options are parsed here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * Option help messages are here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * Termination handled here (VM_DEATH) and shutdown (Agent_OnUnload).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * Spawning of the cpu sample loop thread and listener thread is done here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * Use of private 'static' data has been limited, most shared static data
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *    should be found in the GlobalData structure pointed to by gdata
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *    (see hprof.h).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
/* The default output filenames. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
#define DEFAULT_TXT_SUFFIX      ".txt"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
#define DEFAULT_OUTPUTFILE      "java.hprof"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
#define DEFAULT_OUTPUTTEMP      "java.hprof.temp"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
/* The only global variable, defined by this library */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
GlobalData *gdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
/* Experimental options */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
#define EXPERIMENT_NO_EARLY_HOOK 0x1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
/* Default trace depth */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
#define DEFAULT_TRACE_DEPTH 4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
/* Default sample interval */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
#define DEFAULT_SAMPLE_INTERVAL 10
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
/* Default cutoff */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
#define DEFAULT_CUTOFF_POINT 0.0001
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
/* Stringize macros for help. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#define _TO_STR(a) #a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
#define TO_STR(a) _TO_STR(a)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
/* Macros to surround callback code (non-VM_DEATH callbacks).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *   Note that this just keeps a count of the non-VM_DEATH callbacks that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *   are currently active, it does not prevent these callbacks from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *   operating in parallel. It's the VM_DEATH callback that will wait
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 *   for all these callbacks to either complete and block, or just block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *   We need to hold back these threads so they don't die during the final
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *   VM_DEATH processing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *   If the VM_DEATH callback is active in the beginning, then this callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *   just blocks to prevent further execution of the thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *   If the VM_DEATH callback is active at the end, then this callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 *   will notify the VM_DEATH callback if it's the last one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *   In all cases, the last thing they do is Enter/Exit the monitor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *   gdata->callbackBlock, which will block this callback if VM_DEATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *   is running.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *   WARNING: No not 'return' or 'goto' out of the BEGIN_CALLBACK/END_CALLBACK
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *            block, this will mess up the count.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
#define BEGIN_CALLBACK()                                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
{ /* BEGIN OF CALLBACK */                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    jboolean bypass;                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    rawMonitorEnter(gdata->callbackLock);                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    if (gdata->vm_death_callback_active) {                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        /* VM_DEATH is active, we will bypass the CALLBACK CODE */  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        bypass = JNI_TRUE;                                          \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        rawMonitorExit(gdata->callbackLock);                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        /* Bypassed CALLBACKS block here until VM_DEATH done */     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        rawMonitorEnter(gdata->callbackBlock);                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        rawMonitorExit(gdata->callbackBlock);                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    } else {                                                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        /* We will be executing the CALLBACK CODE in this case */   \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        gdata->active_callbacks++;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        bypass = JNI_FALSE;                                         \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        rawMonitorExit(gdata->callbackLock);                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    if ( !bypass ) {                                                \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        /* BODY OF CALLBACK CODE (with no callback locks held) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
#define END_CALLBACK() /* Part of bypass if body */                 \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        rawMonitorEnter(gdata->callbackLock);                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        gdata->active_callbacks--;                                  \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        /* If VM_DEATH is active, and last one, send notify. */     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (gdata->vm_death_callback_active) {                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (gdata->active_callbacks == 0) {                     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                rawMonitorNotifyAll(gdata->callbackLock);           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            }                                                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }                                                           \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        rawMonitorExit(gdata->callbackLock);                        \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        /* Non-Bypassed CALLBACKS block here until VM_DEATH done */ \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        rawMonitorEnter(gdata->callbackBlock);                      \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        rawMonitorExit(gdata->callbackBlock);                       \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }                                                               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
} /* END OF CALLBACK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
/* Forward declarations */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
static void set_callbacks(jboolean on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
/* Global data initialization */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
/* Get initialized global data area */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
static GlobalData *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
get_gdata(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    static GlobalData data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    /* Create initial default values */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    (void)memset(&data, 0, sizeof(GlobalData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    data.fd                             = -1; /* Non-zero file or socket. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    data.heap_fd                        = -1; /* For heap=dump, see hprof_io */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    data.check_fd                       = -1; /* For heap=dump, see hprof_io */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    data.max_trace_depth                = DEFAULT_TRACE_DEPTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    data.prof_trace_depth               = DEFAULT_TRACE_DEPTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    data.sample_interval                = DEFAULT_SAMPLE_INTERVAL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    data.lineno_in_traces               = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    data.output_format                  = 'a';      /* 'b' for binary */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    data.cutoff_point                   = DEFAULT_CUTOFF_POINT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    data.dump_on_exit                   = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    data.gc_start_time                  = -1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    data.debug                          = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    data.coredump                       = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    data.micro_state_accounting         = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    data.force_output                   = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    data.verbose                        = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    data.primfields                     = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    data.primarrays                     = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    data.table_serial_number_start    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    data.class_serial_number_start    = 100000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    data.thread_serial_number_start   = 200000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    data.trace_serial_number_start    = 300000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    data.object_serial_number_start   = 400000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    data.frame_serial_number_start    = 500000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    data.gref_serial_number_start     = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    data.table_serial_number_counter  = data.table_serial_number_start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    data.class_serial_number_counter  = data.class_serial_number_start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    data.thread_serial_number_counter = data.thread_serial_number_start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    data.trace_serial_number_counter  = data.trace_serial_number_start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    data.object_serial_number_counter = data.object_serial_number_start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    data.frame_serial_number_counter  = data.frame_serial_number_start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    data.gref_serial_number_counter   = data.gref_serial_number_start;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    data.unknown_thread_serial_num    = data.thread_serial_number_counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    return &data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
/* Error handler callback for the java_crw_demo (classfile read write) functions. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
my_crw_fatal_error_handler(const char * msg, const char *file, int line)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    char errmsg[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    (void)md_snprintf(errmsg, sizeof(errmsg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                "%s [%s:%d]", msg, file, line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    errmsg[sizeof(errmsg)-1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    HPROF_ERROR(JNI_TRUE, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
list_all_tables(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    string_list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    class_list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    frame_list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    site_list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    object_list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    trace_list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    monitor_list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    tls_list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    loader_list();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
/* Option Parsing support */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * Socket connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 * Return a socket  connect()ed to a "hostname" that is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * accept()ing heap profile data on "port." Return a value <= 0 if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 * such a connection can't be made.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
static int
23056
af6eda86177e 8036675: Compiler warnings in serviceability code
sla
parents: 23010
diff changeset
   240
connect_to_socket(char *hostname, int port)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    if (port == 0 || port > 65535) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        HPROF_ERROR(JNI_FALSE, "invalid port number");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    if (hostname == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        HPROF_ERROR(JNI_FALSE, "hostname is NULL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /* create a socket */
23056
af6eda86177e 8036675: Compiler warnings in serviceability code
sla
parents: 23010
diff changeset
   254
    fd = md_connect(hostname, (unsigned short)port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    return fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
/* Accept a filename, and adjust the name so that it is unique for this PID */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
make_unique_filename(char **filename)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /* Find a file that doesn't exist */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    fd = md_open(*filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    if ( fd >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int   pid;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        char *new_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        char *old_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        char *prefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        char  suffix[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        int   new_len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        /* Close the file. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        md_close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        /* Make filename name.PID[.txt] */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        pid = md_getpid();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        old_name = *filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        new_len = (int)strlen(old_name)+64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        new_name = HPROF_MALLOC(new_len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        prefix = old_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        suffix[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        /* Look for .txt suffix if not binary output */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        if (gdata->output_format != 'b') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            char *dot;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            char *format_suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            format_suffix = DEFAULT_TXT_SUFFIX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            (void)strcpy(suffix, format_suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            dot = strrchr(old_name, '.');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            if ( dot != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                int slen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                int match;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                slen = (int)strlen(format_suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                match = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                for ( i = 0; i < slen; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    if ( dot[i]==0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                         tolower(format_suffix[i]) != tolower(dot[i]) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                        match = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                if ( match ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    (void)strcpy(suffix, dot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    *dot = 0; /* truncates prefix and old_name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        /* Construct the name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        (void)md_snprintf(new_name, new_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                   "%s.%d%s", prefix, pid, suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        *filename = new_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        HPROF_FREE(old_name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        /* Odds are with Windows, this file may not be so unique. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        (void)remove(gdata->output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
get_tok(char **src, char *buf, int buflen, int sep)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    char *p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    buf[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    if ( **src == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    p = strchr(*src, sep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    if ( p==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        len = (int)strlen(*src);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        p = (*src) + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        /*LINTED*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        len = (int)(p - (*src));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    if ( (len+1) > buflen ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    (void)memcpy(buf, *src, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    buf[len] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    if ( *p != 0 && *p == sep ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        (*src) = p+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        (*src) = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    return len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
static jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
setBinarySwitch(char **src, jboolean *ptr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    char buf[80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    if (!get_tok(src, buf, (int)sizeof(buf), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    if (strcmp(buf, "y") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        *ptr = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    } else if (strcmp(buf, "n") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        *ptr = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        return JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    return JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
print_usage(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    (void)fprintf(stdout,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
"     HPROF: Heap and CPU Profiling Agent (JVMTI Demonstration Code)\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
AGENTNAME " usage: java " AGENTLIB "=[help]|[<option>=<value>, ...]\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
"Option Name and Value  Description                    Default\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
"---------------------  -----------                    -------\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
"heap=dump|sites|all    heap profiling                 all\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
"cpu=samples|times|old  CPU usage                      off\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
"monitor=y|n            monitor contention             n\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
"format=a|b             text(txt) or binary output     a\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
"file=<file>            write data to file             " DEFAULT_OUTPUTFILE "[{" DEFAULT_TXT_SUFFIX "}]\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
"net=<host>:<port>      send data over a socket        off\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
"depth=<size>           stack trace depth              " TO_STR(DEFAULT_TRACE_DEPTH) "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
"interval=<ms>          sample interval in ms          " TO_STR(DEFAULT_SAMPLE_INTERVAL) "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
"cutoff=<value>         output cutoff point            " TO_STR(DEFAULT_CUTOFF_POINT) "\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
"lineno=y|n             line number in traces?         y\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
"thread=y|n             thread in traces?              n\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
"doe=y|n                dump on exit?                  y\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
"msa=y|n                Solaris micro state accounting n\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
"force=y|n              force output to <file>         y\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
"verbose=y|n            print messages about dumps     y\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
"Obsolete Options\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
"----------------\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
"gc_okay=y|n\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
"DEBUG Option           Description                    Default\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
"------------           -----------                    -------\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
"primfields=y|n         include primitive field values y\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
"primarrays=y|n         include primitive array values y\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
"debugflags=MASK        Various debug flags            0\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
"                        0x01   Report refs in and of unprepared classes\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
"logflags=MASK          Logging to stderr              0\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
"                        " TO_STR(LOG_DUMP_MISC)    " Misc logging\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
"                        " TO_STR(LOG_DUMP_LISTS)   " Dump out the tables\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
"                        " TO_STR(LOG_CHECK_BINARY) " Verify & dump format=b\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
"coredump=y|n           Core dump on fatal             n\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
"errorexit=y|n          Exit on any error              n\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
"pause=y|n              Pause on onload & echo PID     n\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
"debug=y|n              Turn on all debug checking     n\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
"X=MASK                 Internal use only              0\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
"Environment Variables\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
"---------------------\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
"_JAVA_HPROF_OPTIONS\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
"    Options can be added externally via this environment variable.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
"    Anything contained in it will get a comma prepended to it (if needed),\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
"    then it will be added to the end of the options supplied via the\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
"    " XRUN " or " AGENTLIB " command line option.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
"Examples\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
"--------\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
"  - Get sample cpu information every 20 millisec, with a stack depth of 3:\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
"      java " AGENTLIB "=cpu=samples,interval=20,depth=3 classname\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
"  - Get heap usage information based on the allocation sites:\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
"      java " AGENTLIB "=heap=sites classname\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
"  - Using the external option addition with csh, log details on all runs:\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
"      setenv _JAVA_HPROF_OPTIONS \"logflags=0xC\"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
"      java " AGENTLIB "=cpu=samples classname\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
"    is the same as:\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
"      java " AGENTLIB "=cpu=samples,logflags=0xC classname\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
"Notes\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
"-----\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
"  - The option format=b cannot be used with monitor=y.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
"  - The option format=b cannot be used with cpu=old|times.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
"  - Use of the " XRUN " interface can still be used, e.g.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
"       java " XRUN ":[help]|[<option>=<value>, ...]\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
"    will behave exactly the same as:\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
"       java " AGENTLIB "=[help]|[<option>=<value>, ...]\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
"  - The debug options and environment variables are available with both java\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
"    and java_g versions.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
"\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
"Warnings\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
"--------\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
"  - This is demonstration code for the JVMTI interface and use of BCI,\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
"    it is not an official product or formal part of the JDK.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
"  - The " XRUN " interface will be removed in a future release.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
"  - The option format=b is considered experimental, this format may change\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
"    in a future release.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
#ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
"  - The obsolete options may be completely removed in a future release.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
"  - The debug options and environment variables are not considered public\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
"    interfaces and can change or be removed with any type of update of\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
"    " AGENTNAME ", including patches.\n"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
#endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
option_error(char *description)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    char errmsg[FILENAME_MAX+80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    (void)md_snprintf(errmsg, sizeof(errmsg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
           "%s option error: %s (%s)", AGENTNAME, description, gdata->options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    errmsg[sizeof(errmsg)-1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    HPROF_ERROR(JNI_FALSE, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    error_exit_process(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
parse_options(char *command_line_options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    int file_or_net_option_seen = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
    char *all_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    char *extra_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
    char *options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    char *default_filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    int   ulen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
    if (command_line_options == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        command_line_options = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
    if ((strcmp(command_line_options, "help")) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
        print_usage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        error_exit_process(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    extra_options = getenv("_JAVA_HPROF_OPTIONS");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    if ( extra_options == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        extra_options = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    all_options = HPROF_MALLOC((int)strlen(command_line_options) +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                                (int)strlen(extra_options) + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    gdata->options = all_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    (void)strcpy(all_options, command_line_options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    if ( extra_options[0] != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if ( all_options[0] != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            (void)strcat(all_options, ",");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        (void)strcat(all_options, extra_options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
    options = all_options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    LOG2("parse_options()", all_options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    while (*options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        char option[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        char suboption[FILENAME_MAX+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        char *endptr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        if (!get_tok(&options, option, (int)sizeof(option), '=')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            option_error("general syntax error parsing options");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
        if (strcmp(option, "file") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            if ( file_or_net_option_seen  ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                option_error("file or net options should only appear once");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                option_error("syntax error parsing file=filename");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            gdata->utf8_output_filename = HPROF_MALLOC((int)strlen(suboption)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            (void)strcpy(gdata->utf8_output_filename, suboption);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            file_or_net_option_seen = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        } else if (strcmp(option, "net") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            char port_number[16];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
            if (file_or_net_option_seen ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                option_error("file or net options should only appear once");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ':')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                option_error("net option missing ':'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
            if (!get_tok(&options, port_number, (int)sizeof(port_number), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                option_error("net option missing port");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            gdata->net_hostname = HPROF_MALLOC((int)strlen(suboption)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
            (void)strcpy(gdata->net_hostname, suboption);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            gdata->net_port = (int)strtol(port_number, NULL, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            file_or_net_option_seen = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        } else if (strcmp(option, "format") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                option_error("syntax error parsing format=a|b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            if (strcmp(suboption, "a") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                gdata->output_format = 'a';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            } else if (strcmp(suboption, "b") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                gdata->output_format = 'b';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                option_error("format option value must be a|b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        } else if (strcmp(option, "depth") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                option_error("syntax error parsing depth=DECIMAL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            gdata->max_trace_depth = (int)strtol(suboption, &endptr, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            if ((endptr != NULL && *endptr != 0) || gdata->max_trace_depth < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                option_error("depth option value must be decimal and >= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            gdata->prof_trace_depth = gdata->max_trace_depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
        } else if (strcmp(option, "interval") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                option_error("syntax error parsing interval=DECIMAL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
            gdata->sample_interval = (int)strtol(suboption, &endptr, 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            if ((endptr != NULL && *endptr != 0) || gdata->sample_interval <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                option_error("interval option value must be decimal and > 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        } else if (strcmp(option, "cutoff") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                option_error("syntax error parsing cutoff=DOUBLE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            gdata->cutoff_point = strtod(suboption, &endptr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            if ((endptr != NULL && *endptr != 0) || gdata->cutoff_point < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
                option_error("cutoff option value must be floating point and >= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        } else if (strcmp(option, "cpu") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                option_error("syntax error parsing cpu=y|samples|times|old");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if ((strcmp(suboption, "samples") == 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                (strcmp(suboption, "y") == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                gdata->cpu_sampling = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            } else if (strcmp(suboption, "times") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                gdata->cpu_timing = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                gdata->old_timing_format = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            } else if (strcmp(suboption, "old") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                gdata->cpu_timing = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
                gdata->old_timing_format = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                option_error("cpu option value must be y|samples|times|old");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        } else if (strcmp(option, "heap") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                option_error("syntax error parsing heap=dump|sites|all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            if (strcmp(suboption, "dump") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                gdata->heap_dump = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            } else if (strcmp(suboption, "sites") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                gdata->alloc_sites = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            } else if (strcmp(suboption, "all") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                gdata->heap_dump = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                gdata->alloc_sites = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                option_error("heap option value must be dump|sites|all");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
        } else if( strcmp(option,"lineno") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            if ( !setBinarySwitch(&options, &(gdata->lineno_in_traces)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                option_error("lineno option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } else if( strcmp(option,"thread") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            if ( !setBinarySwitch(&options, &(gdata->thread_in_traces)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                option_error("thread option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        } else if( strcmp(option,"doe") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if ( !setBinarySwitch(&options, &(gdata->dump_on_exit)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                option_error("doe option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        } else if( strcmp(option,"msa") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            if ( !setBinarySwitch(&options, &(gdata->micro_state_accounting)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                option_error("msa option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        } else if( strcmp(option,"force") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            if ( !setBinarySwitch(&options, &(gdata->force_output)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
                option_error("force option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        } else if( strcmp(option,"verbose") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            if ( !setBinarySwitch(&options, &(gdata->verbose)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                option_error("verbose option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
        } else if( strcmp(option,"primfields") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            if ( !setBinarySwitch(&options, &(gdata->primfields)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                option_error("primfields option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        } else if( strcmp(option,"primarrays") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            if ( !setBinarySwitch(&options, &(gdata->primarrays)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                option_error("primarrays option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        } else if( strcmp(option,"monitor") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if ( !setBinarySwitch(&options, &(gdata->monitor_tracing)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                option_error("monitor option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        } else if( strcmp(option,"gc_okay") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            if ( !setBinarySwitch(&options, &(gdata->gc_okay)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                option_error("gc_okay option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
        } else if (strcmp(option, "logflags") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                option_error("logflags option value must be numeric");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            gdata->logflags = (int)strtol(suboption, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
        } else if (strcmp(option, "debugflags") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                option_error("debugflags option value must be numeric");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            gdata->debugflags = (int)strtol(suboption, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        } else if (strcmp(option, "coredump") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if ( !setBinarySwitch(&options, &(gdata->coredump)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                option_error("coredump option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        } else if (strcmp(option, "exitpause") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            option_error("The exitpause option was removed, use -XX:OnError='cmd %%p'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        } else if (strcmp(option, "errorexit") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            if ( !setBinarySwitch(&options, &(gdata->errorexit)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                option_error("errorexit option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        } else if (strcmp(option, "pause") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            if ( !setBinarySwitch(&options, &(gdata->pause)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                option_error("pause option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        } else if (strcmp(option, "debug") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            if ( !setBinarySwitch(&options, &(gdata->debug)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                option_error("debug option value must be y|n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        } else if (strcmp(option, "precrash") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            option_error("The precrash option was removed, use -XX:OnError='precrash -p %%p'");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        } else if (strcmp(option, "X") == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            if (!get_tok(&options, suboption, (int)sizeof(suboption), ',')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                option_error("X option value must be numeric");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            gdata->experiment = (int)strtol(suboption, NULL, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            char errmsg[80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
            (void)strcpy(errmsg, "Unknown option: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
            (void)strcat(errmsg, option);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            option_error(errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    if (gdata->output_format == 'b') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        if (gdata->cpu_timing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
            option_error("cpu=times|old is not supported with format=b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        if (gdata->monitor_tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            option_error("monitor=y is not supported with format=b");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    if (gdata->old_timing_format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        gdata->prof_trace_depth = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    if (gdata->output_format == 'b') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        default_filename = DEFAULT_OUTPUTFILE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        default_filename = DEFAULT_OUTPUTFILE DEFAULT_TXT_SUFFIX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    if (!file_or_net_option_seen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        gdata->utf8_output_filename = HPROF_MALLOC((int)strlen(default_filename)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
        (void)strcpy(gdata->utf8_output_filename, default_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    if ( gdata->utf8_output_filename != NULL ) {
24967
582420f5ab6c 8041498: Remove npt library
dsamersoff
parents: 23056
diff changeset
   743
        // Don't attempt to convert output filename.
582420f5ab6c 8041498: Remove npt library
dsamersoff
parents: 23056
diff changeset
   744
        // If fileystem uses the same encoding as the rest of the OS it will work as is.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        ulen = (int)strlen(gdata->utf8_output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        gdata->output_filename = (char*)HPROF_MALLOC(ulen*3+3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        (void)strcpy(gdata->output_filename, gdata->utf8_output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    /* By default we turn on gdata->alloc_sites and gdata->heap_dump */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    if (     !gdata->cpu_timing &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
             !gdata->cpu_sampling &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
             !gdata->monitor_tracing &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
             !gdata->alloc_sites &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
             !gdata->heap_dump) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
        gdata->heap_dump = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        gdata->alloc_sites = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    if ( gdata->alloc_sites || gdata->heap_dump ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
        gdata->obj_watch = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    if ( gdata->obj_watch || gdata->cpu_timing ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
        gdata->bci = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
    /* Create files & sockets needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    if (gdata->heap_dump) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        char *base;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        int   len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        /* Get a fast tempfile for the heap information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
        base = gdata->output_filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
        if ( base==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            base = default_filename;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        len = (int)strlen(base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        gdata->heapfilename = HPROF_MALLOC(len + 5);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        (void)strcpy(gdata->heapfilename, base);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        (void)strcat(gdata->heapfilename, ".TMP");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        make_unique_filename(&(gdata->heapfilename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        (void)remove(gdata->heapfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
        if (gdata->output_format == 'b') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            if ( gdata->logflags & LOG_CHECK_BINARY ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                char * check_suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                check_suffix = ".check" DEFAULT_TXT_SUFFIX;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                gdata->checkfilename =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
                    HPROF_MALLOC((int)strlen(default_filename)+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                                (int)strlen(check_suffix)+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                (void)strcpy(gdata->checkfilename, default_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
                (void)strcat(gdata->checkfilename, check_suffix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                (void)remove(gdata->checkfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                gdata->check_fd = md_creat(gdata->checkfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            if ( gdata->debug ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                gdata->logflags |= LOG_CHECK_BINARY;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            gdata->heap_fd = md_creat_binary(gdata->heapfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            gdata->heap_fd = md_creat(gdata->heapfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        if ( gdata->heap_fd < 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            char errmsg[FILENAME_MAX+80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            (void)md_snprintf(errmsg, sizeof(errmsg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                     "can't create temp heap file: %s", gdata->heapfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                    errmsg[sizeof(errmsg)-1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            HPROF_ERROR(JNI_TRUE, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    if ( gdata->net_port > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        LOG2("Agent_OnLoad", "Connecting to socket");
23056
af6eda86177e 8036675: Compiler warnings in serviceability code
sla
parents: 23010
diff changeset
   815
        gdata->fd = connect_to_socket(gdata->net_hostname, gdata->net_port);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        if (gdata->fd <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            char errmsg[120];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            (void)md_snprintf(errmsg, sizeof(errmsg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                "can't connect to %s:%u", gdata->net_hostname, gdata->net_port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            errmsg[sizeof(errmsg)-1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
            HPROF_ERROR(JNI_FALSE, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
            error_exit_process(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
        gdata->socket = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        /* If going out to a file, obey the force=y|n option */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        if ( !gdata->force_output ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            make_unique_filename(&(gdata->output_filename));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        /* Make doubly sure this file does NOT exist */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        (void)remove(gdata->output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
        /* Create the file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
        if (gdata->output_format == 'b') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
            gdata->fd = md_creat_binary(gdata->output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            gdata->fd = md_creat(gdata->output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
        if (gdata->fd < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
            char errmsg[FILENAME_MAX+80];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            (void)md_snprintf(errmsg, sizeof(errmsg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                "can't create profile file: %s", gdata->output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            errmsg[sizeof(errmsg)-1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            HPROF_ERROR(JNI_FALSE, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            error_exit_process(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
/* Data reset and dump functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
reset_all_data(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    if (gdata->cpu_sampling || gdata->cpu_timing || gdata->monitor_tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        rawMonitorEnter(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    if (gdata->cpu_sampling || gdata->cpu_timing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        trace_clear_cost();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    if (gdata->monitor_tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        monitor_clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    if (gdata->cpu_sampling || gdata->cpu_timing || gdata->monitor_tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
static void reset_class_load_status(JNIEnv *env, jthread thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
dump_all_data(JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    verbose_message("Dumping");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
    if (gdata->monitor_tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        verbose_message(" contended monitor usage ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        tls_dump_monitor_state(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        monitor_write_contended_time(env, gdata->cutoff_point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    if (gdata->heap_dump) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        verbose_message(" Java heap ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        /* Update the class table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        reset_class_load_status(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        site_heapdump(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    if (gdata->alloc_sites) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        verbose_message(" allocation sites ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        site_write(env, 0, gdata->cutoff_point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    if (gdata->cpu_sampling) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        verbose_message(" CPU usage by sampling running threads ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        trace_output_cost(env, gdata->cutoff_point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
    if (gdata->cpu_timing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
        if (!gdata->old_timing_format) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            verbose_message(" CPU usage by timing methods ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
            trace_output_cost(env, gdata->cutoff_point);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            verbose_message(" CPU usage in old prof format ...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
            trace_output_cost_in_prof_format(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
    reset_all_data();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
    io_flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
    verbose_message(" done.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
/* Dealing with class load and unload status */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
reset_class_load_status(JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
    WITH_LOCAL_REFS(env, 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        jint    class_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        jclass *classes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        jint    i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        /* Get all classes from JVMTI, make sure they are in the class table. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        getLoadedClasses(&classes, &class_count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        /* We don't know if the class list has changed really, so we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
         *    guess by the class count changing. Don't want to do
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
         *    a bunch of work on classes when it's unnecessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
         *    I assume that even though we have global references on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
         *    jclass object that the class is still considered unloaded.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
         *    (e.g. GC of jclass isn't required for it to be included
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
         *    in the unloaded list, or not in the load list)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
         *    [Note: Use of Weak references was a performance problem.]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
        if ( class_count != gdata->class_count ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
            rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
                /* Unmark the classes in the load list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
                class_all_status_remove(CLASS_IN_LOAD_LIST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                /* Pretend like it was a class load event */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                for ( i = 0 ; i < class_count ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                    jobject loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                    loader = getClassLoader(classes[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                    event_class_load(env, thread, classes[i], loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
                /* Process the classes that have been unloaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
                class_do_unloads(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
            } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        /* Free the space and save the count. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        jvmtiDeallocate(classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
        gdata->class_count = class_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
    } END_WITH_LOCAL_REFS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
/* A GC or Death event has happened, so do some cleanup */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
object_free_cleanup(JNIEnv *env, jboolean force_class_table_reset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
    Stack *stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
    /* Then we process the ObjectFreeStack */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
    rawMonitorEnter(gdata->object_free_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        stack = gdata->object_free_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        gdata->object_free_stack = NULL; /* Will trigger new stack */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
    } rawMonitorExit(gdata->object_free_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
    /* Notice we just grabbed the stack of freed objects so
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
     *    any object free events will create a new stack.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    if ( stack != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        count = stack_depth(stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
        /* If we saw something freed in this GC */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
        if ( count > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
            for ( i = 0 ; i < count ; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                ObjectIndex object_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                jlong tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                tag = *(jlong*)stack_element(stack,i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
                    object_index = tag_extract(tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                (void)object_free(object_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            /* We reset the class load status (only do this once) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            reset_class_load_status(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            force_class_table_reset = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        /* Just terminate this stack object */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
        stack_term(stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
    /* We reset the class load status if we haven't and need to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
    if ( force_class_table_reset ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
        reset_class_load_status(env, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
/* Main function for thread that watches for GC finish events */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
gc_finish_watcher(jvmtiEnv *jvmti, JNIEnv *env, void *p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
    jboolean active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
    active = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
    /* Indicate the watcher thread is active */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
    rawMonitorEnter(gdata->gc_finish_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
        gdata->gc_finish_active = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
    } rawMonitorExit(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    /* Loop while active */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
    while ( active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
        jboolean do_cleanup;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
        do_cleanup = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
        rawMonitorEnter(gdata->gc_finish_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
            /* Don't wait if VM_DEATH wants us to quit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
            if ( gdata->gc_finish_stop_request ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                /* Time to terminate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                active = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                /* Wait for notification to do cleanup, or terminate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                rawMonitorWait(gdata->gc_finish_lock, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                /* After wait, check to see if VM_DEATH wants us to quit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                if ( gdata->gc_finish_stop_request ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    /* Time to terminate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                    active = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
            if ( active && gdata->gc_finish > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                /* Time to cleanup, reset count and prepare for cleanup */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                gdata->gc_finish = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                do_cleanup = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
        } rawMonitorExit(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
        /* Do the cleanup if requested outside gc_finish_lock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
        if ( do_cleanup ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
            /* Free up all freed objects, don't force class table reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
             *   We cannot let the VM_DEATH complete while we are doing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
             *   this cleanup. So if during this, VM_DEATH happens,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
             *   the VM_DEATH callback should block waiting for this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
             *   loop to terminate, and send a notification to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
             *   VM_DEATH thread.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
            object_free_cleanup(env, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            /* Cleanup the tls table where the Thread objects were GC'd */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
            tls_garbage_collect(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
    /* Falling out means VM_DEATH is happening, we need to notify VM_DEATH
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *    that we are done doing the cleanup. VM_DEATH is waiting on this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     *    notify.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    rawMonitorEnter(gdata->gc_finish_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
        gdata->gc_finish_active = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        rawMonitorNotifyAll(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
    } rawMonitorExit(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
/* JVMTI Event callback functions */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
setup_event_mode(jboolean onload_set_only, jvmtiEventMode state)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
    if ( onload_set_only ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                        JVMTI_EVENT_VM_INIT,                   NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
        setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                        JVMTI_EVENT_VM_DEATH,                  NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        if (gdata->bci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
            setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                        JVMTI_EVENT_CLASS_FILE_LOAD_HOOK,      NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        /* Enable all other JVMTI events of interest now. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
        setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                        JVMTI_EVENT_THREAD_START,              NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
        setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                        JVMTI_EVENT_THREAD_END,                NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                        JVMTI_EVENT_CLASS_LOAD,                NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                        JVMTI_EVENT_CLASS_PREPARE,             NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
        setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                        JVMTI_EVENT_DATA_DUMP_REQUEST,         NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
        if (gdata->cpu_timing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
            setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                        JVMTI_EVENT_EXCEPTION_CATCH,           NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
        if (gdata->monitor_tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
            setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                        JVMTI_EVENT_MONITOR_WAIT,              NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
                        JVMTI_EVENT_MONITOR_WAITED,            NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
            setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
                        JVMTI_EVENT_MONITOR_CONTENDED_ENTER,   NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
            setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                        JVMTI_EVENT_MONITOR_CONTENDED_ENTERED, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        if (gdata->obj_watch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
            setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                        JVMTI_EVENT_OBJECT_FREE,               NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
        setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                        JVMTI_EVENT_GARBAGE_COLLECTION_START,  NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
        setEventNotificationMode(state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                        JVMTI_EVENT_GARBAGE_COLLECTION_FINISH, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
/* JVMTI_EVENT_VM_INIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
cbVMInit(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
    rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        LoaderIndex loader_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
        ClassIndex  cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
        TlsIndex    tls_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
        gdata->jvm_initializing = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
        /* Header to use in heap dumps */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        gdata->header    = "JAVA PROFILE 1.0.1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
        gdata->segmented = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        if (gdata->output_format == 'b') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
            /* We need JNI here to call in and get the current maximum memory */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            gdata->maxMemory      = getMaxMemory(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
            gdata->maxHeapSegment = (jlong)2000000000;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            /* More than 2Gig triggers segments and 1.0.2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            if ( gdata->maxMemory >= gdata->maxHeapSegment ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
                gdata->header    = "JAVA PROFILE 1.0.2";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
                gdata->segmented = JNI_TRUE; /* 1.0.2 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        /* We write the initial header after the VM initializes now
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
         *    because we needed to use JNI to get maxMemory and determine if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
         *    a 1.0.1 or a 1.0.2 header will be used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
         *    This used to be done in Agent_OnLoad.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        io_write_file_header();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        LOG("cbVMInit begin");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
        /* Create a system loader entry first */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        loader_index            = loader_find_or_create(NULL,NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        /* Find the thread jclass (does JNI calls) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
        gdata->thread_cnum = class_find_or_create("Ljava/lang/Thread;",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
                        loader_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
        class_add_status(gdata->thread_cnum, CLASS_SYSTEM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        /* Issue fake system thread start */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        tls_index = tls_find_or_create(env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        /* Setup the Tracker class (should be first class in table) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        tracker_setup_class();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        /* Find selected system classes to keep track of */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        gdata->system_class_size = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
        cnum = class_find_or_create("Ljava/lang/Object;", loader_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
        gdata->system_trace_index = tls_get_trace(tls_index, env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                                gdata->max_trace_depth, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
        gdata->system_object_site_index = site_find_or_create(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
                    cnum, gdata->system_trace_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        /* Used to ID HPROF generated items */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
        gdata->hprof_trace_index = tls_get_trace(tls_index, env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                                gdata->max_trace_depth, JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
        gdata->hprof_site_index = site_find_or_create(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    cnum, gdata->hprof_trace_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        if ( gdata->logflags & LOG_DUMP_LISTS ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
            list_all_tables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
        /* Prime the class table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
        reset_class_load_status(env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
        /* Find the tracker jclass and jmethodID's (does JNI calls) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        if ( gdata->bci ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
            tracker_setup_methods(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        /* Start any agent threads (does JNI, JVMTI, and Java calls) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        /* Thread to watch for gc_finish events */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        rawMonitorEnter(gdata->gc_finish_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            createAgentThread(env, "HPROF gc_finish watcher",
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                              &gc_finish_watcher);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        } rawMonitorExit(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        /* Start up listener thread if we need it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        if ( gdata->socket ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            listener_init(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
        /* Start up cpu sampling thread if we need it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        if ( gdata->cpu_sampling ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            /* Note: this could also get started later (see cpu) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            cpu_sample_init(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
        /* Setup event modes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        setup_event_mode(JNI_FALSE, JVMTI_ENABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
        /* Engage tracking (sets Java Tracker field so injections call into
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
         *     agent library).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
        if ( gdata->bci ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            tracker_engage(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
        /* Indicate the VM is initialized now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        gdata->jvm_initialized = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
        gdata->jvm_initializing = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        LOG("cbVMInit end");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
    } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
/* JVMTI_EVENT_VM_DEATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
cbVMDeath(jvmtiEnv *jvmti, JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
     * Use local flag to minimize gdata->dump_lock hold time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
    jboolean need_to_dump = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
    LOG("cbVMDeath");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
    /* Shutdown thread watching gc_finish, outside CALLBACK locks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
     *   We need to make sure the watcher thread is done doing any cleanup
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
     *   work before we continue here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
    rawMonitorEnter(gdata->gc_finish_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        /* Notify watcher thread to finish up, it will send
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
         *   another notify when done. If the watcher thread is busy
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
         *   cleaning up, it will detect gc_finish_stop_request when it's done.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
         *   Then it sets gc_finish_active to JNI_FALSE and will notify us.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
         *   If the watcher thread is waiting to be notified, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
         *   notification wakes it up.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
         *   We do not want to do the VM_DEATH while the gc_finish
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
         *   watcher thread is in the middle of a cleanup.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        gdata->gc_finish_stop_request = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        rawMonitorNotifyAll(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        /* Wait for the gc_finish watcher thread to notify us it's done */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        while ( gdata->gc_finish_active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
            rawMonitorWait(gdata->gc_finish_lock,0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    } rawMonitorExit(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
    /* The gc_finish watcher thread should be done now, or done shortly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
    /* BEGIN_CALLBACK/END_CALLBACK handling. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
    /* The callbackBlock prevents any active callbacks from returning
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
     *   back to the VM, and also blocks all new callbacks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
     *   We want to prevent any threads from premature death, so
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
     *   that we don't have worry about that during thread queries
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
     *   in this final dump process.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
    rawMonitorEnter(gdata->callbackBlock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
        /* We need to wait for all callbacks actively executing to block
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
         *   on exit, and new ones will block on entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
         *   The BEGIN_CALLBACK/END_CALLBACK macros keep track of callbacks
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
         *   that are active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
         *   Once the last active callback is done, it will notify this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
         *   thread and block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
        rawMonitorEnter(gdata->callbackLock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
            /* Turn off native calls */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            if ( gdata->bci ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
                tracker_disengage(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
            gdata->vm_death_callback_active = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
            while (gdata->active_callbacks > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
                rawMonitorWait(gdata->callbackLock, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        } rawMonitorExit(gdata->callbackLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        /* Now we know that no threads will die on us, being blocked
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
         *   on some event callback, at a minimum ThreadEnd.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
        /* Make some basic checks. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
        rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
            if ( gdata->jvm_initializing ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
                HPROF_ERROR(JNI_TRUE, "VM Death during VM Init");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
            if ( !gdata->jvm_initialized ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
                HPROF_ERROR(JNI_TRUE, "VM Death before VM Init");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
            if (gdata->jvm_shut_down) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
                HPROF_ERROR(JNI_TRUE, "VM Death more than once?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
        /* Shutdown the cpu loop thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
        if ( gdata->cpu_sampling ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
            cpu_sample_term(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        /* Time to dump the final data */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
        rawMonitorEnter(gdata->dump_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
            gdata->jvm_shut_down = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
            if (!gdata->dump_in_process) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
                need_to_dump    = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
                gdata->dump_in_process = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
                 * Setting gdata->dump_in_process will cause cpu sampling to pause
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
                 * (if we are sampling). We don't resume sampling after the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
                 * dump_all_data() call below because the VM is shutting
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
                 * down.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        } rawMonitorExit(gdata->dump_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        /* Dump everything if we need to */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        if (gdata->dump_on_exit && need_to_dump) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
            dump_all_data(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        /* Disable all events and callbacks now, all of them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
         *   NOTE: It's important that this be done after the dump
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1366
         *         it prevents other threads from messing up the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
         *         because they will block on ThreadStart and ThreadEnd
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
         *         events due to the CALLBACK block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
        set_callbacks(JNI_FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        setup_event_mode(JNI_FALSE, JVMTI_DISABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
        setup_event_mode(JNI_TRUE, JVMTI_DISABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        /* Write tail of file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
        io_write_file_footer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
    } rawMonitorExit(gdata->callbackBlock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
    /* Shutdown the listener thread and socket, or flush I/O buffers */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
    if (gdata->socket) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
        listener_term(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        io_flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
    /* Close the file descriptors down */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
    if ( gdata->fd  >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
        (void)md_close(gdata->fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
        gdata->fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
        if ( gdata->logflags & LOG_CHECK_BINARY ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            if (gdata->output_format == 'b' && gdata->output_filename != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                check_binary_file(gdata->output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
    if ( gdata->heap_fd  >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        (void)md_close(gdata->heap_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        gdata->heap_fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
    if ( gdata->check_fd  >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
        (void)md_close(gdata->check_fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        gdata->check_fd = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    /* Remove the temporary heap file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
    if (gdata->heap_dump) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
        (void)remove(gdata->heapfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
    /* If logging, dump the tables */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
    if ( gdata->logflags & LOG_DUMP_LISTS ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
        list_all_tables();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
    /* Make sure all global references are deleted */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
    class_delete_global_references(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
    loader_delete_global_references(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
    tls_delete_global_references(env);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
/* JVMTI_EVENT_THREAD_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
cbThreadStart(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
    LOG3("cbThreadStart", "thread is", (int)(long)(ptrdiff_t)thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
        event_thread_start(env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
/* JVMTI_EVENT_THREAD_END */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
cbThreadEnd(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
    LOG3("cbThreadEnd", "thread is", (int)(long)(ptrdiff_t)thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        event_thread_end(env, thread);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
/* JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
cbClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
                jclass class_being_redefined, jobject loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
                const char* name, jobject protection_domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
                jint class_data_len, const unsigned char* class_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
                jint* new_class_data_len, unsigned char** new_class_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
    /* WARNING: This will be called before VM_INIT. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
    LOG2("cbClassFileLoadHook:",(name==NULL?"Unknown":name));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
    if (!gdata->bci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
        rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
            const char *classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
            if ( gdata->bci_counter == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
                /* Prime the system classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
                class_prime_system_classes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1471
            gdata->bci_counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1473
            *new_class_data_len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            *new_class_data     = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
            /* Name could be NULL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
            if ( name == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
                classname = ((JavaCrwDemoClassname)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
                             (gdata->java_crw_demo_classname_function))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
                    (class_data, class_data_len, &my_crw_fatal_error_handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
                if ( classname == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
                    HPROF_ERROR(JNI_TRUE, "No classname in classfile");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
                classname = strdup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
                if ( classname == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
                    HPROF_ERROR(JNI_TRUE, "Ran out of malloc() space");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
            /* The tracker class itself? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            if ( strcmp(classname, TRACKER_CLASS_NAME) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                ClassIndex            cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
                int                   system_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
                unsigned char *       new_image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
                long                  new_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
                int                   len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
                char                 *signature;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                LoaderIndex           loader_index;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
                LOG2("cbClassFileLoadHook injecting class" , classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
                /* Define a unique class number for this class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
                len              = (int)strlen(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
                signature        = HPROF_MALLOC(len+3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
                signature[0]     = JVM_SIGNATURE_CLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
                (void)memcpy(signature+1, classname, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
                signature[len+1] = JVM_SIGNATURE_ENDCLASS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
                signature[len+2] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
                loader_index = loader_find_or_create(env,loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
                if ( class_being_redefined != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                    cnum  = class_find_or_create(signature, loader_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
                    cnum  = class_create(signature, loader_index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
                HPROF_FREE(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
                signature        = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
                /* Make sure class doesn't get unloaded by accident */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
                class_add_status(cnum, CLASS_IN_LOAD_LIST);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
                /* Is it a system class? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
                system_class = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
                if (    (!gdata->jvm_initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
                     && (!gdata->jvm_initializing)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
                     && ( ( class_get_status(cnum) & CLASS_SYSTEM) != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
                            || gdata->bci_counter < 8 ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
                    system_class = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
                    LOG2(classname, " is a system class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
                new_image = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
                new_length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
                /* Call the class file reader/write demo code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
                ((JavaCrwDemo)(gdata->java_crw_demo_function))(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
                    cnum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
                    classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
                    class_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
                    class_data_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
                    system_class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
                    TRACKER_CLASS_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
                    TRACKER_CLASS_SIG,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
                    (gdata->cpu_timing)?TRACKER_CALL_NAME:NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
                    (gdata->cpu_timing)?TRACKER_CALL_SIG:NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
                    (gdata->cpu_timing)?TRACKER_RETURN_NAME:NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                    (gdata->cpu_timing)?TRACKER_RETURN_SIG:NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
                    (gdata->obj_watch)?TRACKER_OBJECT_INIT_NAME:NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                    (gdata->obj_watch)?TRACKER_OBJECT_INIT_SIG:NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
                    (gdata->obj_watch)?TRACKER_NEWARRAY_NAME:NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                    (gdata->obj_watch)?TRACKER_NEWARRAY_SIG:NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
                    &new_image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
                    &new_length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
                    &my_crw_fatal_error_handler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
                    &class_set_methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
                if ( new_length > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
                    unsigned char *jvmti_space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
                    LOG2("cbClassFileLoadHook DID inject this class", classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
                    jvmti_space = (unsigned char *)jvmtiAllocate((jint)new_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
                    (void)memcpy((void*)jvmti_space, (void*)new_image, (int)new_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
                    *new_class_data_len = (jint)new_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
                    *new_class_data     = jvmti_space; /* VM will deallocate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
                    LOG2("cbClassFileLoadHook DID NOT inject this class", classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
                    *new_class_data_len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
                    *new_class_data     = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
                if ( new_image != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
                    (void)free((void*)new_image); /* Free malloc() space with free() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            (void)free((void*)classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
        } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
/* JVMTI_EVENT_CLASS_LOAD */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
cbClassLoad(jvmtiEnv *jvmti, JNIEnv *env, jthread thread, jclass klass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    /* WARNING: This MAY be called before VM_INIT. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    LOG("cbClassLoad");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
            WITH_LOCAL_REFS(env, 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
                jobject loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
                loader = getClassLoader(klass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
                event_class_load(env, thread, klass, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
            } END_WITH_LOCAL_REFS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
        } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
/* JVMTI_EVENT_CLASS_PREPARE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
cbClassPrepare(jvmtiEnv *jvmti, JNIEnv *env, jthread thread, jclass klass)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
    /* WARNING: This will be called before VM_INIT. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    LOG("cbClassPrepare");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
        rawMonitorEnter(gdata->data_access_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
            WITH_LOCAL_REFS(env, 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
                jobject loader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1617
                loader = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
                loader = getClassLoader(klass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
                event_class_prepare(env, thread, klass, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
            } END_WITH_LOCAL_REFS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        } rawMonitorExit(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
/* JVMTI_EVENT_DATA_DUMP_REQUEST */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
cbDataDumpRequest(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
    jboolean need_to_dump;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    LOG("cbDataDumpRequest");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
        need_to_dump = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        rawMonitorEnter(gdata->dump_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            if (!gdata->dump_in_process) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
                need_to_dump    = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
                gdata->dump_in_process = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
        } rawMonitorExit(gdata->dump_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
        if (need_to_dump) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
            dump_all_data(getEnv());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            rawMonitorEnter(gdata->dump_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
                gdata->dump_in_process = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            } rawMonitorExit(gdata->dump_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            if (gdata->cpu_sampling && !gdata->jvm_shut_down) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
                cpu_sample_on(NULL, 0); /* resume sampling */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1657
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1658
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1659
/* JVMTI_EVENT_EXCEPTION_CATCH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1660
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1661
cbExceptionCatch(jvmtiEnv *jvmti, JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1662
                jthread thread, jmethodID method, jlocation location,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1663
                jobject exception)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1664
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1665
    LOG("cbExceptionCatch");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1666
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1667
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1668
        event_exception_catch(env, thread, method, location, exception);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1669
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1670
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
/* JVMTI_EVENT_MONITOR_WAIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
cbMonitorWait(jvmtiEnv *jvmti, JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
                jthread thread, jobject object, jlong timeout)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
    LOG("cbMonitorWait");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1678
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1679
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1680
        monitor_wait_event(env, thread, object, timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1681
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1682
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1683
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1684
/* JVMTI_EVENT_MONITOR_WAITED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1685
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1686
cbMonitorWaited(jvmtiEnv *jvmti, JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1687
                jthread thread, jobject object, jboolean timed_out)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1688
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1689
    LOG("cbMonitorWaited");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1690
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1691
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1692
        monitor_waited_event(env, thread, object, timed_out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1693
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1694
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
/* JVMTI_EVENT_MONITOR_CONTENDED_ENTER */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
cbMonitorContendedEnter(jvmtiEnv *jvmti, JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
                jthread thread, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
    LOG("cbMonitorContendedEnter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
        monitor_contended_enter_event(env, thread, object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
/* JVMTI_EVENT_MONITOR_CONTENDED_ENTERED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1709
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
cbMonitorContendedEntered(jvmtiEnv *jvmti, JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
                jthread thread, jobject object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1712
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
    LOG("cbMonitorContendedEntered");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1715
    BEGIN_CALLBACK() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1716
        monitor_contended_entered_event(env, thread, object);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1717
    } END_CALLBACK();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1718
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1719
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1720
/* JVMTI_EVENT_GARBAGE_COLLECTION_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1721
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1722
cbGarbageCollectionStart(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1723
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1724
    LOG("cbGarbageCollectionStart");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1725
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1726
    /* Only calls to Allocate, Deallocate, RawMonitorEnter & RawMonitorExit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1727
     *   are allowed here (see the JVMTI Spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1730
    gdata->gc_start_time = md_get_timemillis();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
/* JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
cbGarbageCollectionFinish(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
    LOG("cbGarbageCollectionFinish");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
    /* Only calls to Allocate, Deallocate, RawMonitorEnter & RawMonitorExit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
     *   are allowed here (see the JVMTI Spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1741
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
    if ( gdata->gc_start_time != -1L ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
        gdata->time_in_gc += (md_get_timemillis() - gdata->gc_start_time);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
        gdata->gc_start_time = -1L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
    /* Increment gc_finish counter, notify watcher thread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
    rawMonitorEnter(gdata->gc_finish_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
        /* If VM_DEATH is trying to shut it down, don't do anything at all.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
         *    Never send notify if VM_DEATH wants the watcher thread to quit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
        if ( gdata->gc_finish_active ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
            gdata->gc_finish++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
            rawMonitorNotifyAll(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
    } rawMonitorExit(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
/* JVMTI_EVENT_OBJECT_FREE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
cbObjectFree(jvmtiEnv *jvmti, jlong tag)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1764
    LOG3("cbObjectFree", "tag", (int)tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1765
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1766
    /* Only calls to Allocate, Deallocate, RawMonitorEnter & RawMonitorExit
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1767
     *   are allowed here (see the JVMTI Spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1768
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1769
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1770
    HPROF_ASSERT(tag!=(jlong)0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1771
    rawMonitorEnter(gdata->object_free_lock); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1772
        if ( !gdata->jvm_shut_down ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1773
            Stack *stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1774
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1775
            stack = gdata->object_free_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
            if ( stack == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                gdata->object_free_stack = stack_init(512, 512, sizeof(jlong));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                stack = gdata->object_free_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
            stack_push(stack, (void*)&tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
    } rawMonitorExit(gdata->object_free_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1783
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1784
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1785
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
set_callbacks(jboolean on)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
    jvmtiEventCallbacks callbacks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
    (void)memset(&callbacks,0,sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
    if ( ! on ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
        setEventCallbacks(&callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
    /* JVMTI_EVENT_VM_INIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
    callbacks.VMInit                     = &cbVMInit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
    /* JVMTI_EVENT_VM_DEATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
    callbacks.VMDeath                    = &cbVMDeath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
    /* JVMTI_EVENT_THREAD_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
    callbacks.ThreadStart                = &cbThreadStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
    /* JVMTI_EVENT_THREAD_END */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
    callbacks.ThreadEnd                  = &cbThreadEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
    /* JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
    callbacks.ClassFileLoadHook          = &cbClassFileLoadHook;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
    /* JVMTI_EVENT_CLASS_LOAD */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
    callbacks.ClassLoad                  = &cbClassLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
    /* JVMTI_EVENT_CLASS_PREPARE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
    callbacks.ClassPrepare               = &cbClassPrepare;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
    /* JVMTI_EVENT_DATA_DUMP_REQUEST */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    callbacks.DataDumpRequest            = &cbDataDumpRequest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
    /* JVMTI_EVENT_EXCEPTION_CATCH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    callbacks.ExceptionCatch             = &cbExceptionCatch;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
    /* JVMTI_EVENT_MONITOR_WAIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1815
    callbacks.MonitorWait                = &cbMonitorWait;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
    /* JVMTI_EVENT_MONITOR_WAITED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
    callbacks.MonitorWaited              = &cbMonitorWaited;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
    /* JVMTI_EVENT_MONITOR_CONTENDED_ENTER */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
    callbacks.MonitorContendedEnter      = &cbMonitorContendedEnter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    /* JVMTI_EVENT_MONITOR_CONTENDED_ENTERED */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
    callbacks.MonitorContendedEntered    = &cbMonitorContendedEntered;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
    /* JVMTI_EVENT_GARBAGE_COLLECTION_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
    callbacks.GarbageCollectionStart     = &cbGarbageCollectionStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1824
    /* JVMTI_EVENT_GARBAGE_COLLECTION_FINISH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
    callbacks.GarbageCollectionFinish    = &cbGarbageCollectionFinish;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
    /* JVMTI_EVENT_OBJECT_FREE */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1827
    callbacks.ObjectFree                 = &cbObjectFree;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1828
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1829
    setEventCallbacks(&callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1830
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1831
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1832
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1833
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1834
getCapabilities(void)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1835
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1836
    jvmtiCapabilities needed_capabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1837
    jvmtiCapabilities potential_capabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1839
    /* Fill in ones that we must have */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1840
    (void)memset(&needed_capabilities,0,sizeof(needed_capabilities));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1841
    needed_capabilities.can_generate_garbage_collection_events   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1842
    needed_capabilities.can_tag_objects                          = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1843
    if (gdata->bci) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1844
        needed_capabilities.can_generate_all_class_hook_events   = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1845
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1846
    if (gdata->obj_watch) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1847
        needed_capabilities.can_generate_object_free_events      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1849
    if (gdata->cpu_timing || gdata->cpu_sampling) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1850
        #if 0 /* Not needed until we call JVMTI for CpuTime */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1851
        needed_capabilities.can_get_thread_cpu_time              = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1852
        needed_capabilities.can_get_current_thread_cpu_time      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1853
        #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1854
        needed_capabilities.can_generate_exception_events        = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1855
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
    if (gdata->monitor_tracing) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1857
        #if 0 /* Not needed until we call JVMTI for CpuTime */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1858
        needed_capabilities.can_get_thread_cpu_time              = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1859
        needed_capabilities.can_get_current_thread_cpu_time      = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1860
        #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1861
        needed_capabilities.can_get_owned_monitor_info           = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1862
        needed_capabilities.can_get_current_contended_monitor    = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1863
        needed_capabilities.can_get_monitor_info                 = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1864
        needed_capabilities.can_generate_monitor_events          = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1866
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1867
    /* Get potential capabilities */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1868
    getPotentialCapabilities(&potential_capabilities);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1869
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1870
    /* Some capabilities would be nicer to have */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1871
    needed_capabilities.can_get_source_file_name        =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1872
        potential_capabilities.can_get_source_file_name;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1873
    needed_capabilities.can_get_line_numbers    =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1874
        potential_capabilities.can_get_line_numbers;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1875
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1876
    /* Add the capabilities */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1877
    addCapabilities(&needed_capabilities);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1878
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1879
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1880
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1881
/* Dynamic library loading */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1882
static void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1883
load_library(char *name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1884
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1885
    char  lname[FILENAME_MAX+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1886
    char  err_buf[256+FILENAME_MAX+1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
    char *boot_path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
    void *handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1890
    handle = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1891
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
    /* The library may be located in different ways, try both, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
     *   if it comes from outside the SDK/jre it isn't ours.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
    getSystemProperty("sun.boot.library.path", &boot_path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
    md_build_library_name(lname, FILENAME_MAX, boot_path, name);
16057
16a81953a291 8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket
sla
parents: 14698
diff changeset
  1897
    if ( strlen(lname) == 0 ) {
16a81953a291 8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket
sla
parents: 14698
diff changeset
  1898
        HPROF_ERROR(JNI_TRUE, "Could not find library");
16a81953a291 8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket
sla
parents: 14698
diff changeset
  1899
    }
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
  1900
    jvmtiDeallocate(boot_path);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
    handle = md_load_library(lname, err_buf, (int)sizeof(err_buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    if ( handle == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
        /* This may be necessary on Windows. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
        md_build_library_name(lname, FILENAME_MAX, "", name);
16057
16a81953a291 8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket
sla
parents: 14698
diff changeset
  1905
        if ( strlen(lname) == 0 ) {
16a81953a291 8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket
sla
parents: 14698
diff changeset
  1906
            HPROF_ERROR(JNI_TRUE, "Could not find library");
16a81953a291 8009397: test/com/sun/jdi/PrivateTransportTest.sh: ERROR: transport library missing onLoad entry: private_dt_socket
sla
parents: 14698
diff changeset
  1907
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
        handle = md_load_library(lname, err_buf, (int)sizeof(err_buf));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
        if ( handle == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
            HPROF_ERROR(JNI_TRUE, err_buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
    return handle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
/* Lookup dynamic function pointer in shared library */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
static void *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
lookup_library_symbol(void *library, char **symbols, int nsymbols)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
    void *addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1921
    int   i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
    addr = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
    for( i = 0 ; i < nsymbols; i++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        addr = md_find_library_entry(library, symbols[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        if ( addr != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
    if ( addr == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
        char errmsg[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
        (void)md_snprintf(errmsg, sizeof(errmsg),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
                    "Cannot find library symbol '%s'", symbols[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1935
        HPROF_ERROR(JNI_TRUE, errmsg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1936
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1937
    return addr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1938
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1939
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1941
/* The OnLoad interface */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1942
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
{
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
  1946
    char *boot_path = NULL;
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
  1947
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
    /* See if it's already loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
    if ( gdata!=NULL && gdata->isLoaded==JNI_TRUE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        HPROF_ERROR(JNI_TRUE, "Cannot load this JVM TI agent twice, check your java command line for duplicate hprof options.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
    gdata = get_gdata();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
    gdata->isLoaded = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
    error_setup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    LOG2("Agent_OnLoad", "gdata setup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    gdata->jvm = vm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
14698
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
  1964
    /* Get the JVMTI environment */
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
  1965
    getJvmti();
9294fcf94c46 7200297: agent code does not handle multiple boot library path elements correctly
dholmes
parents: 14342
diff changeset
  1966
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
    /* Lock needed to protect debug_malloc() code, which is not MT safe */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
    #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
        gdata->debug_malloc_lock = createRawMonitor("HPROF debug_malloc lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
    #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
    parse_options(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
    LOG2("Agent_OnLoad", "Has jvmtiEnv and options parsed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
    /* Initialize machine dependent code (micro state accounting) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
    md_init();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    string_init();      /* Table index values look like: 0x10000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    class_init();       /* Table index values look like: 0x20000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
    tls_init();         /* Table index values look like: 0x30000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
    trace_init();       /* Table index values look like: 0x40000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
    object_init();      /* Table index values look like: 0x50000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
    site_init();        /* Table index values look like: 0x60000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
    frame_init();       /* Table index values look like: 0x70000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
    monitor_init();     /* Table index values look like: 0x80000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
    loader_init();      /* Table index values look like: 0x90000000 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
    LOG2("Agent_OnLoad", "Tables initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
    if ( gdata->pause ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        error_do_pause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
    getCapabilities();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
    /* Set the JVMTI callback functions  (do this only once)*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
    set_callbacks(JNI_TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
    /* Create basic locks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
    gdata->dump_lock          = createRawMonitor("HPROF dump lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
    gdata->data_access_lock   = createRawMonitor("HPROF data access lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
    gdata->callbackLock       = createRawMonitor("HPROF callback lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
    gdata->callbackBlock      = createRawMonitor("HPROF callback block");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
    gdata->object_free_lock   = createRawMonitor("HPROF object free lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
    gdata->gc_finish_lock     = createRawMonitor("HPROF gc_finish lock");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2010
    /* Set Onload events mode. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
    setup_event_mode(JNI_TRUE, JVMTI_ENABLE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
    LOG2("Agent_OnLoad", "JVMTI capabilities, callbacks and initial notifications setup");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
    /* Used in VM_DEATH to wait for callbacks to complete */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
    gdata->jvm_initializing             = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
    gdata->jvm_initialized              = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
    gdata->vm_death_callback_active     = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
    gdata->active_callbacks             = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
    /* Write the header information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2022
    io_setup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2023
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
    /* We sample the start time now so that the time increments can be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
     *    placed in the various heap dump segments in micro seconds.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
    gdata->micro_sec_ticks = md_get_microsecs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
    /* Load java_crw_demo library and find function "java_crw_demo" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
    if ( gdata->bci ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
        /* Load the library or get the handle to it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
        gdata->java_crw_demo_library = load_library("java_crw_demo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
        { /* "java_crw_demo" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2036
            static char *symbols[]  = JAVA_CRW_DEMO_SYMBOLS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2037
            gdata->java_crw_demo_function =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2038
                   lookup_library_symbol(gdata->java_crw_demo_library,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2039
                              symbols, (int)(sizeof(symbols)/sizeof(char*)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2040
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2041
        { /* "java_crw_demo_classname" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2042
            static char *symbols[] = JAVA_CRW_DEMO_CLASSNAME_SYMBOLS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2043
            gdata->java_crw_demo_classname_function =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                   lookup_library_symbol(gdata->java_crw_demo_library,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2045
                              symbols, (int)(sizeof(symbols)/sizeof(char*)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2046
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2047
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2048
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2049
    return JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2050
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2052
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
Agent_OnUnload(JavaVM *vm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
    Stack *stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
    LOG("Agent_OnUnload");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
    gdata->isLoaded = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
    stack = gdata->object_free_stack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
    gdata->object_free_stack = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
    if ( stack != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
        stack_term(stack);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2066
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
    io_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
    loader_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
    tls_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
    monitor_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2071
    trace_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
    site_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2073
    object_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2074
    frame_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2075
    class_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2076
    string_cleanup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2078
    /* Deallocate any memory in gdata */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2079
    if ( gdata->net_hostname != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2080
        HPROF_FREE(gdata->net_hostname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2081
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2082
    if ( gdata->utf8_output_filename != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2083
        HPROF_FREE(gdata->utf8_output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2084
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2085
    if ( gdata->output_filename != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2086
        HPROF_FREE(gdata->output_filename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2087
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2088
    if ( gdata->heapfilename != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2089
        HPROF_FREE(gdata->heapfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2090
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2091
    if ( gdata->checkfilename != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2092
        HPROF_FREE(gdata->checkfilename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2093
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2094
    if ( gdata->options != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2095
        HPROF_FREE(gdata->options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2096
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2098
    /* Verify all allocated memory has been taken care of. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2099
    malloc_police();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2100
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2101
    /* Cleanup is hard to do when other threads might still be running
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2102
     *  so we skip destroying some raw monitors which still might be in use
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2103
     *  and we skip disposal of the jvmtiEnv* which might still be needed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2104
     *  Only raw monitors that could be held by other threads are left
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2105
     *  alone. So we explicitly do NOT do this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2106
     *      destroyRawMonitor(gdata->callbackLock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2107
     *      destroyRawMonitor(gdata->callbackBlock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2108
     *      destroyRawMonitor(gdata->gc_finish_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2109
     *      destroyRawMonitor(gdata->object_free_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2110
     *      destroyRawMonitor(gdata->listener_loop_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
     *      destroyRawMonitor(gdata->cpu_loop_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
     *      disposeEnvironment();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
     *      gdata->jvmti = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
    /* Destroy basic locks */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
    destroyRawMonitor(gdata->dump_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
    gdata->dump_lock = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
    destroyRawMonitor(gdata->data_access_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2120
    gdata->data_access_lock = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2121
    if ( gdata->cpu_sample_lock != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2122
        destroyRawMonitor(gdata->cpu_sample_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2123
        gdata->cpu_sample_lock = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2125
    #ifdef DEBUG
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2126
        destroyRawMonitor(gdata->debug_malloc_lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2127
        gdata->debug_malloc_lock = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2128
    #endif
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2129
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2130
    /* Unload java_crw_demo library */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2131
    if ( gdata->bci && gdata->java_crw_demo_library != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2132
        md_unload_library(gdata->java_crw_demo_library);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2133
        gdata->java_crw_demo_library = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
    /* You would think you could clear out gdata and set it to NULL, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
     *   turns out that isn't a good idea.  Some of the threads could be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2138
     *   blocked inside the CALLBACK*() macros, where they got blocked up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2139
     *   waiting for the VM_DEATH callback to complete. They only have
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2140
     *   some raw monitor actions to do, but they need access to gdata to do it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2141
     *   So do not do this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2142
     *       (void)memset(gdata, 0, sizeof(GlobalData));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2143
     *       gdata = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2144
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
}