jdk/src/share/instrument/InvocationAdapter.c
author sla
Fri, 07 Mar 2014 09:18:15 +0100
changeset 23235 d1b1878b2eaa
parent 22962 309eaab8c853
child 23566 760a74c1b589
permissions -rw-r--r--
8034025: Remove JPLIS agent dependency on canonicalize_md.c Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     2
 * Copyright (c) 2003, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * Copyright 2003 Wily Technology, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
#include    <string.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#include    <stdlib.h>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
#include    "jni.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
#include    "Utilities.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
#include    "JPLISAssert.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
#include    "JPLISAgent.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#include    "JavaExceptions.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
#include    "EncodingSupport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
#include    "FileSystemSupport.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
#include    "JarFacade.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
#include    "PathCharsValidator.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This module contains the direct interface points with the JVMTI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The OnLoad handler is here, along with the various event handlers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
appendClassPath(JPLISAgent* agent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                const char* jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
appendBootClassPath(JPLISAgent* agent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                    const char* jarfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                    const char* pathList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * Parse -javaagent tail, of the form name[=options], into name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * and options. Returned values are heap allocated and options maybe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * NULL. Returns 0 if parse succeeds, -1 if allocation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
parseArgumentTail(char* tail, char** name, char** options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    char* pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    pos = strchr(tail, '=');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    len = (pos == NULL) ? (int)strlen(tail) : (int)(pos - tail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    *name = (char*)malloc(len+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    if (*name == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    memcpy(*name, tail, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    (*name)[len] = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if (pos == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        *options = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        char * str = (char*)malloc( (int)strlen(pos + 1) + 1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (str == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            free(*name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        strcpy(str, pos +1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        *options = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    return 0;
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
 * Get the value of an attribute in an attribute list. Returns NULL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * if attribute not found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
jboolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
getBooleanAttribute(const jarAttribute* attributes, const char* name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    char* attributeValue = getAttribute(attributes, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    return attributeValue != NULL && strcasecmp(attributeValue, "true") == 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * Parse any capability settings in the JAR manifest and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 * convert them to JVM TI capabilities.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
convertCapabilityAtrributes(const jarAttribute* attributes, JPLISAgent* agent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /* set redefineClasses capability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    if (getBooleanAttribute(attributes, "Can-Redefine-Classes")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        addRedefineClassesCapability(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    /* create an environment which has the retransformClasses capability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    if (getBooleanAttribute(attributes, "Can-Retransform-Classes")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        retransformableEnvironment(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    /* set setNativeMethodPrefix capability */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    if (getBooleanAttribute(attributes, "Can-Set-Native-Method-Prefix")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        addNativeMethodPrefixCapability(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /* for retransformClasses testing, set capability to use original method order */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    if (getBooleanAttribute(attributes, "Can-Maintain-Original-Method-Order")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        addOriginalMethodOrderCapability(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *  This will be called once for every -javaagent on the command line.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *  Each call to Agent_OnLoad will create its own agent and agent data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 *  The argument tail string provided to Agent_OnLoad will be of form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 *  <jarfile>[=<options>]. The tail string is split into the jarfile and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *  options components. The jarfile manifest is parsed and the value of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
 *  Premain-Class attribute will become the agent's premain class. The jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *  file is then added to the system class path, and if the Boot-Class-Path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *  attribute is present then all relative URLs in the value are processed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 *  to create boot class path segments to append to the boot class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
Agent_OnLoad(JavaVM *vm, char *tail, void * reserved) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    JPLISInitializationError initerror  = JPLIS_INIT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    jint                     result     = JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    JPLISAgent *             agent      = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    initerror = createNewJPLISAgent(vm, &agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    if ( initerror == JPLIS_INIT_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        int             oldLen, newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        char *          jarfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        char *          options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        jarAttribute*   attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        char *          premainClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        char *          bootClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
         * Parse <jarfile>[=options] into jarfile and options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        if (parseArgumentTail(tail, &jarfile, &options) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            fprintf(stderr, "-javaagent: memory allocation failure.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
         * Agent_OnLoad is specified to provide the agent options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
         * argument tail in modified UTF8. However for 1.5.0 this is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
         * actually in the platform encoding - see 5049313.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         * Open zip/jar file and parse archive. If can't be opened or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
         * not a zip file return error. Also if Premain-Class attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
         * isn't present we return an error.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        attributes = readAttributes(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        if (attributes == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            fprintf(stderr, "Error opening zip file or JAR manifest missing : %s\n", jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            free(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            if (options != NULL) free(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        premainClass = getAttribute(attributes, "Premain-Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (premainClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            fprintf(stderr, "Failed to find Premain-Class manifest attribute in %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            free(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (options != NULL) free(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            freeAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
         * Add to the jarfile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        appendClassPath(agent, jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
         * The value of the Premain-Class attribute becomes the agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
         * class name. The manifest is in UTF8 so need to convert to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
         * modified UTF8 (see JNI spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        oldLen = (int)strlen(premainClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        newLen = modifiedUtf8LengthOfUtf8(premainClass, oldLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (newLen == oldLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            premainClass = strdup(premainClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            char* str = (char*)malloc( newLen+1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (str != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                convertUtf8ToModifiedUtf8(premainClass, oldLen, str, newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            premainClass = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (premainClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            fprintf(stderr, "-javaagent: memory allocation failed\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            free(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (options != NULL) free(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            freeAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            return JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
         * If the Boot-Class-Path attribute is specified then we process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
         * each relative URL and add it to the bootclasspath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        bootClassPath = getAttribute(attributes, "Boot-Class-Path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (bootClassPath != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            appendBootClassPath(agent, jarfile, bootClassPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
         * Convert JAR attributes into agent capabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        convertCapabilityAtrributes(attributes, agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
         * Track (record) the agent class name and options data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        initerror = recordCommandLineData(agent, premainClass, options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
         * Clean-up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        free(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        if (options != NULL) free(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        freeAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        free(premainClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    switch (initerror) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    case JPLIS_INIT_ERROR_NONE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
      result = JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    case JPLIS_INIT_ERROR_CANNOT_CREATE_NATIVE_AGENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
      result = JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
      fprintf(stderr, "java.lang.instrument/-javaagent: cannot create native agent.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    case JPLIS_INIT_ERROR_FAILURE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
      result = JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
      fprintf(stderr, "java.lang.instrument/-javaagent: initialization of native agent failed.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    case JPLIS_INIT_ERROR_ALLOCATION_FAILURE:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
      result = JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
      fprintf(stderr, "java.lang.instrument/-javaagent: allocation failure.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    case JPLIS_INIT_ERROR_AGENT_CLASS_NOT_SPECIFIED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
      result = JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
      fprintf(stderr, "-javaagent: agent class not specified.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
      result = JNI_ERR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
      fprintf(stderr, "java.lang.instrument/-javaagent: unknown error\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
      break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 * Agent_OnAttach returns a jint. 0/JNI_OK indicates success and non-0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 * indicates an error. To allow the attach mechanism throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 * AgentInitializationException with a reasonable exception message we define
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 * a few specific errors here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
#define AGENT_ERROR_BADJAR    ((jint)100)  /* Agent JAR not found or no Agent-Class attribute */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
#define AGENT_ERROR_NOTONCP   ((jint)101)  /* Unable to add JAR file to system class path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
#define AGENT_ERROR_STARTFAIL ((jint)102)  /* No agentmain method or agentmain failed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 *  This will be called once each time a tool attaches to the VM and loads
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *  the JPLIS library.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
JNIEXPORT jint JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
Agent_OnAttach(JavaVM* vm, char *args, void * reserved) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    JPLISInitializationError initerror  = JPLIS_INIT_ERROR_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    jint                     result     = JNI_OK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
    JPLISAgent *             agent      = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    JNIEnv *                 jni_env    = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * Need JNIEnv - guaranteed to be called from thread that is already
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * attached to VM
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    result = (*vm)->GetEnv(vm, (void**)&jni_env, JNI_VERSION_1_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    jplis_assert(result==JNI_OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    initerror = createNewJPLISAgent(vm, &agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    if ( initerror == JPLIS_INIT_ERROR_NONE ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        int             oldLen, newLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        char *          jarfile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        char *          options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        jarAttribute*   attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        char *          agentClass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        char *          bootClassPath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        jboolean        success;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
         * Parse <jarfile>[=options] into jarfile and options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        if (parseArgumentTail(args, &jarfile, &options) != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            return JNI_ENOMEM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
         * Open the JAR file and parse the manifest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        attributes = readAttributes( jarfile );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        if (attributes == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            fprintf(stderr, "Error opening zip file or JAR manifest missing: %s\n", jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            free(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            if (options != NULL) free(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            return AGENT_ERROR_BADJAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        agentClass = getAttribute(attributes, "Agent-Class");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (agentClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
            fprintf(stderr, "Failed to find Agent-Class manifest attribute from %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            free(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            if (options != NULL) free(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            freeAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            return AGENT_ERROR_BADJAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
         * Add the jarfile to the system class path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (appendClassPath(agent, jarfile)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            fprintf(stderr, "Unable to add %s to system class path "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                "- not supported by system class loader or configuration error!\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            free(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            if (options != NULL) free(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            freeAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            return AGENT_ERROR_NOTONCP;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
         * The value of the Agent-Class attribute becomes the agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
         * class name. The manifest is in UTF8 so need to convert to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
         * modified UTF8 (see JNI spec).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        oldLen = strlen(agentClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        newLen = modifiedUtf8LengthOfUtf8(agentClass, oldLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        if (newLen == oldLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            agentClass = strdup(agentClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            char* str = (char*)malloc( newLen+1 );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            if (str != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                convertUtf8ToModifiedUtf8(agentClass, oldLen, str, newLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            agentClass = str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (agentClass == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            free(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
            if (options != NULL) free(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            freeAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            return JNI_ENOMEM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
         * If the Boot-Class-Path attribute is specified then we process
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
         * each URL - in the live phase only JAR files will be added.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        bootClassPath = getAttribute(attributes, "Boot-Class-Path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (bootClassPath != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            appendBootClassPath(agent, jarfile, bootClassPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
         * Convert JAR attributes into agent capabilities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        convertCapabilityAtrributes(attributes, agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
         * Create the java.lang.instrument.Instrumentation instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        success = createInstrumentationImpl(jni_env, agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        jplis_assert(success);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
         *  Turn on the ClassFileLoadHook.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            success = setLivePhaseEventHandlers(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            jplis_assert(success);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
         * Start the agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        if (success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            success = startJavaAgent(agent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                                     jni_env,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                                     agentClass,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                                     options,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                                     agent->mAgentmainCaller);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        if (!success) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            fprintf(stderr, "Agent failed to start!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            result = AGENT_ERROR_STARTFAIL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
         * Clean-up
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        free(jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        if (options != NULL) free(options);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        free(agentClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        freeAttributes(attributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
JNIEXPORT void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
Agent_OnUnload(JavaVM *vm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
}
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
 *  JVMTI callback support
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 *  We have two "stages" of callback support.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 *  At OnLoad time, we install a VMInit handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 *  When the VMInit handler runs, we remove the VMInit handler and install a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
 *  ClassFileLoadHook handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
eventHandlerVMInit( jvmtiEnv *      jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    JNIEnv *        jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                    jthread         thread) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    JPLISEnvironment * environment  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    jboolean           success      = JNI_FALSE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    environment = getJPLISEnvironment(jvmtienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /* process the premain calls on the all the JPL agents */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    if ( environment != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        jthrowable outstandingException = preserveThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        success = processJavaStart( environment->mAgent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
                                    jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        restoreThrowable(jnienv, outstandingException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
    /* if we fail to start cleanly, bring down the JVM */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    if ( !success ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
        abortJVM(jnienv, JPLIS_ERRORMESSAGE_CANNOTSTART);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
void JNICALL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
eventHandlerClassFileLoadHook(  jvmtiEnv *              jvmtienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                JNIEnv *                jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                jclass                  class_being_redefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                                jobject                 loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
                                const char*             name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
                                jobject                 protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
                                jint                    class_data_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
                                const unsigned char*    class_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                                jint*                   new_class_data_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                                unsigned char**         new_class_data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    JPLISEnvironment * environment  = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    environment = getJPLISEnvironment(jvmtienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    /* if something is internally inconsistent (no agent), just silently return without touching the buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    if ( environment != NULL ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        jthrowable outstandingException = preserveThrowable(jnienv);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        transformClassFile( environment->mAgent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                            jnienv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
                            loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                            name,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                            class_being_redefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                            protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                            class_data_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                            class_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                            new_class_data_len,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                            new_class_data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                            environment->mIsRetransformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        restoreThrowable(jnienv, outstandingException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 * URLs in Boot-Class-Path attributes are separated by one or more spaces.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
 * This function splits the attribute value into a list of path segments.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
 * The attribute value is in UTF8 but cannot contain NUL. Also non US-ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
 * characters must be escaped (URI syntax) so safe to iterate through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
 * value as a C string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
splitPathList(const char* str, int* pathCount, char*** paths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    int count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    char** segments = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    char* c = (char*) str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
    while (*c != '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        while (*c == ' ') c++;          /* skip leading spaces */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (*c == '\0') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (segments == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            segments = (char**)malloc( sizeof(char**) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            segments = (char**)realloc( segments, (count+1)*sizeof(char**) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        jplis_assert(segments != (char**)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        segments[count++] = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        c = strchr(c, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
        if (c == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        *c = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        c++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    *pathCount = count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    *paths = segments;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
/* URI path decoding - ported from src/share/classes/java/net/URI.java */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
decodeNibble(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
    if ((c >= '0') && (c <= '9'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        return c - '0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    if ((c >= 'a') && (c <= 'f'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        return c - 'a' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    if ((c >= 'A') && (c <= 'F'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        return c - 'A' + 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
decodeByte(char c1, char c2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
    return (((decodeNibble(c1) & 0xf) << 4) | ((decodeNibble(c2) & 0xf) << 0));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
 * Evaluates all escapes in s.  Assumes that escapes are well-formed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
 * syntactically, i.e., of the form %XX.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
 * If the path does not require decoding the the original path is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
 * returned. Otherwise the decoded path (heap allocated) is returned,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
 * along with the length of the decoded path. Note that the return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
 * string will not be null terminated after decoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
static
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
char *decodePath(const char *s, int* decodedLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
    int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    char *result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
    char *resultp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    int c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    n = (int)strlen(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
    if (n == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
        *decodedLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        return (char*)s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    if (strchr(s, '%') == NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        *decodedLen = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
        return (char*)s; /* no escapes, we are done */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    resultp = result = calloc(n+1, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    c = s[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    for (i = 0; i < n;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        if (c != '%') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
            *resultp++ = c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            if (++i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            c = s[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            char b1 = s[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            char b2 = s[++i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            int decoded = decodeByte(b1, b2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            *resultp++ = decoded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
            if (++i >= n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            c = s[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            if (c != '%')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    *decodedLen = (int)(resultp - result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    return result; // not null terminated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
 * Append the given jar file to the system class path. This should succeed in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
 * onload phase but may fail in the live phase if the system class loader doesn't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
 * support appending to the class path.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
static int
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
appendClassPath( JPLISAgent* agent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                 const char* jarfile ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    jvmtiEnv* jvmtienv = jvmti(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    jvmtiError jvmtierr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    jvmtierr = (*jvmtienv)->AddToSystemClassLoaderSearch(jvmtienv, jarfile);
282
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
   628
    check_phase_ret_1(jvmtierr);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    if (jvmtierr == JVMTI_ERROR_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        jvmtiPhase phase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        jvmtiError err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
        err = (*jvmtienv)->GetPhase(jvmtienv, &phase);
282
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
   637
        /* can be called from any phase */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
        jplis_assert(err == JVMTI_ERROR_NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        if (phase == JVMTI_PHASE_LIVE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            switch (jvmtierr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                case JVMTI_ERROR_CLASS_LOADER_UNSUPPORTED :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    fprintf(stderr, "System class loader does not support adding "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                        "JAR file to system class path during the live phase!\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    fprintf(stderr, "Unexpected error (%d) returned by "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                        "AddToSystemClassLoaderSearch\n", jvmtierr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        jplis_assert(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    return -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
 * res = func, free'ing the previous value of 'res' if function
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
 * returns a new result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
#define TRANSFORM(res,func) {    \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
    char* tmp = func;            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    if (tmp != res) {            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        free(res);               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        res = tmp;               \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
    }                            \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    jplis_assert((void*)res != (void*)NULL);     \
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
23235
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22962
diff changeset
   672
/**
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22962
diff changeset
   673
 * Convert a pathname to canonical form.
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22962
diff changeset
   674
 * This method is exported from libjava.
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22962
diff changeset
   675
 */
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22962
diff changeset
   676
extern int
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22962
diff changeset
   677
Canonicalize(JNIEnv *unused, char *orig, char *out, int len);
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22962
diff changeset
   678
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
 * This function takes the value of the Boot-Class-Path attribute,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
 * splits it into the individual path segments, and then combines it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
 * with the path to the jar file to create the path to be added
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
 * to the bootclasspath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
 * Each individual path segment starts out as a UTF8 string. Additionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
 * as the path is specified to use URI path syntax all non US-ASCII
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
 * characters are escaped. Once the URI path is decoded we get a UTF8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
 * string which must then be converted to the platform encoding (as it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
 * will be combined with the platform path of the jar file). Once
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
 * converted it is then normalized (remove duplicate slashes, etc.).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
 * If the resulting path is an absolute path (starts with a slash for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
 * example) then the path will be added to the bootclasspath. Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
 * if it's not absolute then we get the canoncial path of the agent jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
 * file and then resolve the path in the context of the base path of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
 * the agent jar.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
appendBootClassPath( JPLISAgent* agent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
                     const char* jarfile,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                     const char* pathList ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    char canonicalPath[MAXPATHLEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
    char *parent = NULL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
    int haveBasePath = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
    int count, i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    char **paths;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
    jvmtiEnv* jvmtienv = jvmti(agent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    jvmtiError jvmtierr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     * Split the attribute value into the individual path segments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * and process each in sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    splitPathList(pathList, &count, &paths);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    for (i=0; i<count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        char* path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        char* pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
         * The path segment at this point is a pointer into the attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
         * value. As it will go through a number of transformation (tossing away
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
         * the previous results as we go along) it make it easier if the path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
         * starts out as a heap allocated string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
        path = strdup(paths[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        jplis_assert(path != (char*)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
         * The attribute is specified to be a list of relative URIs so in theory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
         * there could be a query component - if so, get rid of it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        pos = strchr(path, '?');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        if (pos != NULL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            *pos = '\0';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
         * Check for characters that are not allowed in the path component of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
         * a URI.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (validatePathChars(path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
            fprintf(stderr, "WARNING: illegal character in Boot-Class-Path value: %s\n",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
               path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            free(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
         * Next decode any escaped characters. The result is a UTF8 string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
        TRANSFORM(path, decodePath(path,&len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
         * Convert to the platform encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            char platform[MAXPATHLEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
            int new_len = convertUft8ToPlatformString(path, len, platform, MAXPATHLEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            free(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
            if (new_len  < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                /* bogus value - exceeds maximum path size or unable to convert */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
            path = strdup(platform);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            jplis_assert(path != (char*)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
         * Post-process the URI path - needed on Windows to transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
         * /c:/foo to c:/foo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        TRANSFORM(path, fromURIPath(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
         * Normalize the path - no duplicate slashes (except UNCs on Windows), trailing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
         * slash removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
        TRANSFORM(path, normalize(path));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
         * If the path is an absolute path then add to the bootclassloader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
         * search path. Otherwise we get the canonical path of the agent jar
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
         * and then use its base path (directory) to resolve the given path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
         * segment.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
         * NOTE: JVMTI is specified to use modified UTF8 strings (like JNI).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
         * In 1.5.0 the AddToBootstrapClassLoaderSearch takes a platform string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
         * - see 5049313.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (isAbsolute(path)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            jvmtierr = (*jvmtienv)->AddToBootstrapClassLoaderSearch(jvmtienv, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            char* resolved;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            if (!haveBasePath) {
23235
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22962
diff changeset
   800
                /* Use NULL as the JNIEnv since we know that Canonicalize does not use it. */
d1b1878b2eaa 8034025: Remove JPLIS agent dependency on canonicalize_md.c
sla
parents: 22962
diff changeset
   801
                if (Canonicalize(NULL, (char*)jarfile, canonicalPath, sizeof(canonicalPath)) != 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                    fprintf(stderr, "WARNING: unable to canonicalize %s\n", jarfile);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                    free(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                parent = basePath(canonicalPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                jplis_assert(parent != (char*)NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                haveBasePath = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            resolved = resolve(parent, path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            jvmtierr = (*jvmtienv)->AddToBootstrapClassLoaderSearch(jvmtienv, resolved);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        /* print warning if boot class path not updated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        if (jvmtierr != JVMTI_ERROR_NONE) {
282
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
   817
            check_phase_blob_ret(jvmtierr, free(path));
bca3e5a71df1 6572160: 3/3 Instrumentation.getObjectSize triggers JVM crash in JPLISAssert in shutdown
dcubed
parents: 2
diff changeset
   818
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            fprintf(stderr, "WARNING: %s not added to bootstrap class loader search: ", path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            switch (jvmtierr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                case JVMTI_ERROR_ILLEGAL_ARGUMENT :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                    fprintf(stderr, "Illegal argument or not JAR file\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
                default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    fprintf(stderr, "Unexpected error: %d\n", jvmtierr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        /* finished with the path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
        free(path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    /* clean-up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    if (haveBasePath && parent != canonicalPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        free(parent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
}