jdk/src/demo/share/jvmti/mtrace/mtrace.c
author bobv
Mon, 19 Oct 2015 13:41:09 -0400
changeset 33653 c1ee09fe3274
parent 25859 3317bb8137f4
permissions -rw-r--r--
8136556: Add the ability to perform static builds of MacOSX x64 binaries Reviewed-by: ihse, bdelsart, gadams, lfoltan, rriggs, hseigel, twisti
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
     2
 * Copyright (c) 2004, 2015, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * are met:
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
10292
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    32
/*
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    33
 * This source code is provided to illustrate the usage of a given feature
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    34
 * or technique and has been deliberately simplified. Additional steps
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    35
 * required for a production-quality application, such as security checks,
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    36
 * input validation and proper error handling, might not be present in
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    37
 * this sample code.
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    38
 */
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    39
ed7db6a12c2a 7067811: Update demo/sample code to state it should not be used for production
nloodin
parents: 5506
diff changeset
    40
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include "stdlib.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include "mtrace.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
#include "java_crw_demo.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/* Some constant maximum sizes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
#define MAX_TOKEN_LENGTH        16
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
#define MAX_THREAD_NAME_LENGTH  512
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
#define MAX_METHOD_NAME_LENGTH  1024
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
/* Some constant names that tie to Java class/method names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *    We assume the Java class whose static methods we will be calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *    looks like:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * public class Mtrace {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *     private static int engaged;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *     private static native void _method_entry(Object thr, int cnum, int mnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *     public static void method_entry(int cnum, int mnum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *         if ( engaged != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *             _method_entry(Thread.currentThread(), cnum, mnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *     private static native void _method_exit(Object thr, int cnum, int mnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *     public static void method_exit(int cnum, int mnum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *         if ( engaged != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *             _method_exit(Thread.currentThread(), cnum, mnum);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *    The engaged field allows us to inject all classes (even system classes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *    and delay the actual calls to the native code until the VM has reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *    a safe time to call native methods (Past the JVMTI VM_START event).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
#define MTRACE_class        Mtrace          /* Name of class we are using */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
#define MTRACE_entry        method_entry    /* Name of java entry method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
#define MTRACE_exit         method_exit     /* Name of java exit method */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
#define MTRACE_native_entry _method_entry   /* Name of java entry native */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
#define MTRACE_native_exit  _method_exit    /* Name of java exit native */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
#define MTRACE_engaged      engaged         /* Name of java static field */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
/* C macros to create strings from tokens */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
#define _STRING(s) #s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
#define STRING(s) _STRING(s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
/* ------------------------------------------------------------------- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
/* Data structure to hold method and class information in agent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
typedef struct MethodInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    const char *name;          /* Method name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    const char *signature;     /* Method signature */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    int         calls;         /* Method call count */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    int         returns;       /* Method return count */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
} MethodInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
typedef struct ClassInfo {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    const char *name;          /* Class name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    int         mcount;        /* Method count */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    MethodInfo *methods;       /* Method information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    int         calls;         /* Method call count for this class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
} ClassInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
/* Global agent data structure */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
typedef struct {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    /* JVMTI Environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    jvmtiEnv      *jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    jboolean       vm_is_dead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    jboolean       vm_is_started;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /* Data access Lock */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    jrawMonitorID  lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /* Options */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    char           *include;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    char           *exclude;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    int             max_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /* ClassInfo Table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    ClassInfo      *classes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    jint            ccount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
} GlobalAgentData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
static GlobalAgentData *gdata;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
/* Enter a critical section by doing a JVMTI Raw Monitor Enter */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
enter_critical_section(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    error = (*jvmti)->RawMonitorEnter(jvmti, gdata->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    check_jvmti_error(jvmti, error, "Cannot enter with raw monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
/* Exit a critical section by doing a JVMTI Raw Monitor Exit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
exit_critical_section(jvmtiEnv *jvmti)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    error = (*jvmti)->RawMonitorExit(jvmti, gdata->lock);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    check_jvmti_error(jvmti, error, "Cannot exit with raw monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
/* Get a name for a jthread */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
get_thread_name(jvmtiEnv *jvmti, jthread thread, char *tname, int maxlen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    jvmtiThreadInfo info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    jvmtiError      error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    /* Make sure the stack variables are garbage free */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    (void)memset(&info,0, sizeof(info));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    /* Assume the name is unknown for now */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    (void)strcpy(tname, "Unknown");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /* Get the thread information, which includes the name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    error = (*jvmti)->GetThreadInfo(jvmti, thread, &info);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    check_jvmti_error(jvmti, error, "Cannot get thread info");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /* The thread might not have a name, be careful here. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    if ( info.name != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        /* Copy the thread name into tname if it will fit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        len = (int)strlen(info.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if ( len < maxlen ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            (void)strcpy(tname, info.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        /* Every string allocated by JVMTI needs to be freed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        deallocate(jvmti, (void*)info.name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
/* Qsort class compare routine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
class_compar(const void *e1, const void *e2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    ClassInfo *c1 = (ClassInfo*)e1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    ClassInfo *c2 = (ClassInfo*)e2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    if ( c1->calls > c2->calls ) return  1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    if ( c1->calls < c2->calls ) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
/* Qsort method compare routine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
method_compar(const void *e1, const void *e2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    MethodInfo *m1 = (MethodInfo*)e1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    MethodInfo *m2 = (MethodInfo*)e2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    if ( m1->calls > m2->calls ) return  1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    if ( m1->calls < m2->calls ) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
/* Callback from java_crw_demo() that gives us mnum mappings */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
mnum_callbacks(unsigned cnum, const char **names, const char**sigs, int mcount)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    ClassInfo  *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    int         mnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    if ( cnum >= (unsigned)gdata->ccount ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        fatal_error("ERROR: Class number out of range\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    if ( mcount == 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    cp           = gdata->classes + (int)cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    cp->calls    = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    cp->mcount   = mcount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    cp->methods  = (MethodInfo*)calloc(mcount, sizeof(MethodInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    if ( cp->methods == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    for ( mnum = 0 ; mnum < mcount ; mnum++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        MethodInfo *mp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        mp            = cp->methods + mnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        mp->name      = (const char *)strdup(names[mnum]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if ( mp->name == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        mp->signature = (const char *)strdup(sigs[mnum]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if ( mp->signature == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
/* Java Native Method for entry */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
MTRACE_native_entry(JNIEnv *env, jclass klass, jobject thread, jint cnum, jint mnum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    enter_critical_section(gdata->jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        /* It's possible we get here right after VmDeath event, be careful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        if ( !gdata->vm_is_dead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            ClassInfo  *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            MethodInfo *mp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if ( cnum >= gdata->ccount ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                fatal_error("ERROR: Class number out of range\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            cp = gdata->classes + cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            if ( mnum >= cp->mcount ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                fatal_error("ERROR: Method number out of range\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            mp = cp->methods + mnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            if ( interested((char*)cp->name, (char*)mp->name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                            gdata->include, gdata->exclude)  ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                mp->calls++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                cp->calls++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    } exit_critical_section(gdata->jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
/* Java Native Method for exit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
MTRACE_native_exit(JNIEnv *env, jclass klass, jobject thread, jint cnum, jint mnum)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    enter_critical_section(gdata->jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        /* It's possible we get here right after VmDeath event, be careful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        if ( !gdata->vm_is_dead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            ClassInfo  *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            MethodInfo *mp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if ( cnum >= gdata->ccount ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                fatal_error("ERROR: Class number out of range\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            cp = gdata->classes + cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            if ( mnum >= cp->mcount ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                fatal_error("ERROR: Method number out of range\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            mp = cp->methods + mnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if ( interested((char*)cp->name, (char*)mp->name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                            gdata->include, gdata->exclude)  ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                mp->returns++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    } exit_critical_section(gdata->jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
/* Callback for JVMTI_EVENT_VM_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
cbVMStart(jvmtiEnv *jvmti, JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        jclass   klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        jfieldID field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        int      rc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        /* Java Native Methods for class */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        static JNINativeMethod registry[2] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            {STRING(MTRACE_native_entry), "(Ljava/lang/Object;II)V",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                (void*)&MTRACE_native_entry},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            {STRING(MTRACE_native_exit),  "(Ljava/lang/Object;II)V",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                (void*)&MTRACE_native_exit}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        /* The VM has started. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        stdout_message("VMStart\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        /* Register Natives for class whose methods we use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        klass = (*env)->FindClass(env, STRING(MTRACE_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        if ( klass == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            fatal_error("ERROR: JNI: Cannot find %s with FindClass\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                        STRING(MTRACE_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        rc = (*env)->RegisterNatives(env, klass, registry, 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if ( rc != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            fatal_error("ERROR: JNI: Cannot register native methods for %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                        STRING(MTRACE_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        /* Engage calls. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        field = (*env)->GetStaticFieldID(env, klass, STRING(MTRACE_engaged), "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        if ( field == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            fatal_error("ERROR: JNI: Cannot get field from %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                        STRING(MTRACE_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        (*env)->SetStaticIntField(env, klass, field, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        /* Indicate VM has started */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        gdata->vm_is_started = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
/* Callback for JVMTI_EVENT_VM_INIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
cbVMInit(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        char  tname[MAX_THREAD_NAME_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        static jvmtiEvent events[] =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                { JVMTI_EVENT_THREAD_START, JVMTI_EVENT_THREAD_END };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        int        i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        /* The VM has started. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        get_thread_name(jvmti, thread, tname, sizeof(tname));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        stdout_message("VMInit %s\n", tname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        /* The VM is now initialized, at this time we make our requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
         *   for additional events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        for( i=0; i < (int)(sizeof(events)/sizeof(jvmtiEvent)); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            jvmtiError error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            /* Setup event  notification modes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                                  events[i], (jthread)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            check_jvmti_error(jvmti, error, "Cannot set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
/* Callback for JVMTI_EVENT_VM_DEATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
cbVMDeath(jvmtiEnv *jvmti, JNIEnv *env)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        jclass   klass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        jfieldID field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        /* The VM has died. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        stdout_message("VMDeath\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        /* Disengage calls in MTRACE_class. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        klass = (*env)->FindClass(env, STRING(MTRACE_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if ( klass == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            fatal_error("ERROR: JNI: Cannot find %s with FindClass\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                        STRING(MTRACE_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        field = (*env)->GetStaticFieldID(env, klass, STRING(MTRACE_engaged), "I");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        if ( field == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            fatal_error("ERROR: JNI: Cannot get field from %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                        STRING(MTRACE_class));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        (*env)->SetStaticIntField(env, klass, field, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        /* The critical section here is important to hold back the VM death
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         *    until all other callbacks have completed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        /* Since this critical section could be holding up other threads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
         *   in other event callbacks, we need to indicate that the VM is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         *   dead so that the other callbacks can short circuit their work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         *   We don't expect any further events after VmDeath but we do need
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
         *   to be careful that existing threads might be in our own agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
         *   callback code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        gdata->vm_is_dead = JNI_TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        /* Dump out stats */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        stdout_message("Begin Class Stats\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if ( gdata->ccount > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
            int cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
            /* Sort table (in place) by number of method calls into class. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            /*  Note: Do not use this table after this qsort! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            qsort(gdata->classes, gdata->ccount, sizeof(ClassInfo),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                        &class_compar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            /* Dump out gdata->max_count most called classes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            for ( cnum=gdata->ccount-1 ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                  cnum >= 0 && cnum >= gdata->ccount - gdata->max_count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                  cnum-- ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                ClassInfo *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                int        mnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                cp = gdata->classes + cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                stdout_message("Class %s %d calls\n", cp->name, cp->calls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                if ( cp->calls==0 ) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                /* Sort method table (in place) by number of method calls. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                /*  Note: Do not use this table after this qsort! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                qsort(cp->methods, cp->mcount, sizeof(MethodInfo),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                            &method_compar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                for ( mnum=cp->mcount-1 ; mnum >= 0 ; mnum-- ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    MethodInfo *mp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                    mp = cp->methods + mnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                    if ( mp->calls==0 ) continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                    stdout_message("\tMethod %s %s %d calls %d returns\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                        mp->name, mp->signature, mp->calls, mp->returns);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        stdout_message("End Class Stats\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        (void)fflush(stdout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
/* Callback for JVMTI_EVENT_THREAD_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
cbThreadStart(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        /* It's possible we get here right after VmDeath event, be careful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
        if ( !gdata->vm_is_dead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
            char  tname[MAX_THREAD_NAME_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            get_thread_name(jvmti, thread, tname, sizeof(tname));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            stdout_message("ThreadStart %s\n", tname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
/* Callback for JVMTI_EVENT_THREAD_END */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
cbThreadEnd(jvmtiEnv *jvmti, JNIEnv *env, jthread thread)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        /* It's possible we get here right after VmDeath event, be careful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        if ( !gdata->vm_is_dead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            char  tname[MAX_THREAD_NAME_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            get_thread_name(jvmti, thread, tname, sizeof(tname));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            stdout_message("ThreadEnd %s\n", tname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
/* Callback for JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
static void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
cbClassFileLoadHook(jvmtiEnv *jvmti, JNIEnv* env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                jclass class_being_redefined, jobject loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                const char* name, jobject protection_domain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                jint class_data_len, const unsigned char* class_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                jint* new_class_data_len, unsigned char** new_class_data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    enter_critical_section(jvmti); {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        /* It's possible we get here right after VmDeath event, be careful */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        if ( !gdata->vm_is_dead ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            const char *classname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            /* Name could be NULL */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            if ( name == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                classname = java_crw_demo_classname(class_data, class_data_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                        NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                if ( classname == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    fatal_error("ERROR: No classname inside classfile\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                classname = strdup(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                if ( classname == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                    fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            *new_class_data_len = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            *new_class_data     = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            /* The tracker class itself? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            if ( interested((char*)classname, "", gdata->include, gdata->exclude)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                  &&  strcmp(classname, STRING(MTRACE_class)) != 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                jint           cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                int            system_class;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                unsigned char *new_image;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                long           new_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                ClassInfo     *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                /* Get unique number for every class file image loaded */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                cnum = gdata->ccount++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                /* Save away class information */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                if ( gdata->classes == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                    gdata->classes = (ClassInfo*)malloc(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                                gdata->ccount*sizeof(ClassInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    gdata->classes = (ClassInfo*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                                realloc((void*)gdata->classes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                                gdata->ccount*sizeof(ClassInfo));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                if ( gdata->classes == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                    fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                cp           = gdata->classes + cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                cp->name     = (const char *)strdup(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                if ( cp->name == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                    fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                cp->calls    = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                cp->mcount   = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                cp->methods  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                /* Is it a system class? If the class load is before VmStart
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                 *   then we will consider it a system class that should
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                 *   be treated carefully. (See java_crw_demo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                system_class = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                if ( !gdata->vm_is_started ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                    system_class = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                new_image = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                new_length = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                /* Call the class file reader/write demo code */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                java_crw_demo(cnum,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                    classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    class_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    class_data_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
                    system_class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    STRING(MTRACE_class), "L" STRING(MTRACE_class) ";",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    STRING(MTRACE_entry), "(II)V",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
                    STRING(MTRACE_exit), "(II)V",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    NULL, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    NULL, NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    &new_image,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
                    &new_length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    NULL,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    &mnum_callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                /* If we got back a new class image, return it back as "the"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                 *   new class image. This must be JVMTI Allocate space.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                if ( new_length > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    unsigned char *jvmti_space;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    jvmti_space = (unsigned char *)allocate(jvmti, (jint)new_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    (void)memcpy((void*)jvmti_space, (void*)new_image, (int)new_length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    *new_class_data_len = (jint)new_length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                    *new_class_data     = jvmti_space; /* VM will deallocate */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                /* Always free up the space we get from java_crw_demo() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                if ( new_image != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    (void)free((void*)new_image); /* Free malloc() space with free() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            (void)free((void*)classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    } exit_critical_section(jvmti);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
/* Parse the options for this mtrace agent */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
parse_agent_options(char *options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    char token[MAX_TOKEN_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
    char *next;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
    gdata->max_count = 10; /* Default max=n */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    /* Parse options and set flags in gdata */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
    if ( options==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    /* Get the first token from the options string. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    next = get_token(options, ",=", token, sizeof(token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    /* While not at the end of the options string, process this option. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    while ( next != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        if ( strcmp(token,"help")==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            stdout_message("The mtrace JVMTI demo agent\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
            stdout_message("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            stdout_message(" java -agent:mtrace[=options] ...\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            stdout_message("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            stdout_message("The options are comma separated:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            stdout_message("\t help\t\t\t Print help information\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            stdout_message("\t max=n\t\t Only list top n classes\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            stdout_message("\t include=item\t\t Only these classes/methods\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            stdout_message("\t exclude=item\t\t Exclude these classes/methods\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            stdout_message("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            stdout_message("item\t Qualified class and/or method names\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
            stdout_message("\t\t e.g. (*.<init>;Foobar.method;sun.*)\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
            stdout_message("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            exit(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
        } else if ( strcmp(token,"max")==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            char number[MAX_TOKEN_LENGTH];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
            /* Get the numeric option */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            next = get_token(next, ",=", number, (int)sizeof(number));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            /* Check for token scan error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if ( next==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                fatal_error("ERROR: max=n option error\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            /* Save numeric value */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            gdata->max_count = atoi(number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
        } else if ( strcmp(token,"include")==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            int   used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            int   maxlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
            maxlen = MAX_METHOD_NAME_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            if ( gdata->include == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                gdata->include = (char*)calloc(maxlen+1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                used = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                used  = (int)strlen(gdata->include);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                gdata->include[used++] = ',';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
                gdata->include[used] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
                gdata->include = (char*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                             realloc((void*)gdata->include, used+maxlen+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            if ( gdata->include == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
                fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            /* Add this item to the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            next = get_token(next, ",=", gdata->include+used, maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            /* Check for token scan error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            if ( next==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
                fatal_error("ERROR: include option error\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        } else if ( strcmp(token,"exclude")==0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            int   used;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            int   maxlen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            maxlen = MAX_METHOD_NAME_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            if ( gdata->exclude == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
                gdata->exclude = (char*)calloc(maxlen+1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
                used = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
                used  = (int)strlen(gdata->exclude);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                gdata->exclude[used++] = ',';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                gdata->exclude[used] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                gdata->exclude = (char*)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                             realloc((void*)gdata->exclude, used+maxlen+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            if ( gdata->exclude == NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
                fatal_error("ERROR: Out of malloc memory\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            /* Add this item to the list */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            next = get_token(next, ",=", gdata->exclude+used, maxlen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
            /* Check for token scan error */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            if ( next==NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                fatal_error("ERROR: exclude option error\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        } else if ( token[0]!=0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            /* We got a non-empty token and we don't know what it is. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            fatal_error("ERROR: Unknown option: %s\n", token);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        /* Get the next token (returns NULL if there are no more) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        next = get_token(next, ",=", token, sizeof(token));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
/* Agent_OnLoad: This is called immediately after the shared library is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
 *   loaded. This is the first code executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
JNIEXPORT jint JNICALL
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
   700
DEF_Agent_OnLoad(JavaVM *vm, char *options, void *reserved)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    static GlobalAgentData data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    jvmtiEnv              *jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    jvmtiError             error;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    jint                   res;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    jvmtiCapabilities      capabilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    jvmtiEventCallbacks    callbacks;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /* Setup initial global agent data area
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     *   Use of static/extern data should be handled carefully here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     *   We need to make sure that we are able to cleanup after ourselves
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *     so anything allocated in this library needs to be freed in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     *     the Agent_OnUnload() function.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    (void)memset((void*)&data, 0, sizeof(data));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
    gdata = &data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
    /* First thing we need to do is get the jvmtiEnv* or JVMTI environment */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    res = (*vm)->GetEnv(vm, (void **)&jvmti, JVMTI_VERSION_1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    if (res != JNI_OK) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        /* This means that the VM was unable to obtain this version of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
         *   JVMTI interface, this is a fatal error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        fatal_error("ERROR: Unable to access JVMTI Version 1 (0x%x),"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                " is your JDK a 5.0 or newer version?"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                " JNIEnv's GetEnv() returned %d\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
               JVMTI_VERSION_1, res);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /* Here we save the jvmtiEnv* for Agent_OnUnload(). */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    gdata->jvmti = jvmti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
    /* Parse any options supplied on java command line */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    parse_agent_options(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
    /* Immediately after getting the jvmtiEnv* we need to ask for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *   capabilities this agent will need. In this case we need to make
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     *   sure that we can get all class load hooks.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    (void)memset(&capabilities,0, sizeof(capabilities));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    capabilities.can_generate_all_class_hook_events  = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
    error = (*jvmti)->AddCapabilities(jvmti, &capabilities);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    check_jvmti_error(jvmti, error, "Unable to get necessary JVMTI capabilities.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    /* Next we need to provide the pointers to the callback functions to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
     *   to this jvmtiEnv*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    (void)memset(&callbacks,0, sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
    /* JVMTI_EVENT_VM_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
    callbacks.VMStart           = &cbVMStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
    /* JVMTI_EVENT_VM_INIT */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
    callbacks.VMInit            = &cbVMInit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    /* JVMTI_EVENT_VM_DEATH */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
    callbacks.VMDeath           = &cbVMDeath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
    /* JVMTI_EVENT_CLASS_FILE_LOAD_HOOK */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
    callbacks.ClassFileLoadHook = &cbClassFileLoadHook;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
    /* JVMTI_EVENT_THREAD_START */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
    callbacks.ThreadStart       = &cbThreadStart;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
    /* JVMTI_EVENT_THREAD_END */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    callbacks.ThreadEnd         = &cbThreadEnd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    error = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, (jint)sizeof(callbacks));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    check_jvmti_error(jvmti, error, "Cannot set jvmti callbacks");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    /* At first the only initial events we are interested in are VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     *   initialization, VM death, and Class File Loads.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     *   Once the VM is initialized we will request more events.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
    error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                          JVMTI_EVENT_VM_START, (jthread)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    check_jvmti_error(jvmti, error, "Cannot set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                          JVMTI_EVENT_VM_INIT, (jthread)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
    check_jvmti_error(jvmti, error, "Cannot set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
    error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                          JVMTI_EVENT_VM_DEATH, (jthread)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    check_jvmti_error(jvmti, error, "Cannot set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    error = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                          JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, (jthread)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    check_jvmti_error(jvmti, error, "Cannot set event notification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /* Here we create a raw monitor for our use in this agent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     *   protect critical sections of code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    error = (*jvmti)->CreateRawMonitor(jvmti, "agent data", &(gdata->lock));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    check_jvmti_error(jvmti, error, "Cannot create raw monitor");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /* Add demo jar file to boot classpath */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    add_demo_jar_to_bootclasspath(jvmti, "mtrace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /* We return JNI_OK to signify success */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    return JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
/* Agent_OnUnload: This is called immediately before the shared library is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
 *   unloaded. This is the last code executed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
JNIEXPORT void JNICALL
33653
c1ee09fe3274 8136556: Add the ability to perform static builds of MacOSX x64 binaries
bobv
parents: 25859
diff changeset
   798
DEF_Agent_OnUnload(JavaVM *vm)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    /* Make sure all malloc/calloc/strdup space is freed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    if ( gdata->include != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        (void)free((void*)gdata->include);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        gdata->include = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
    if ( gdata->exclude != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        (void)free((void*)gdata->exclude);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        gdata->exclude = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    if ( gdata->classes != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        int cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        for ( cnum = 0 ; cnum < gdata->ccount ; cnum++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            ClassInfo *cp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            cp = gdata->classes + cnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            (void)free((void*)cp->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            if ( cp->mcount > 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                int mnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                for ( mnum = 0 ; mnum < cp->mcount ; mnum++ ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                    MethodInfo *mp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    mp = cp->methods + mnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                    (void)free((void*)mp->name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    (void)free((void*)mp->signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                (void)free((void*)cp->methods);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        (void)free((void*)gdata->classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        gdata->classes = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
}