jdk/src/share/classes/sun/instrument/InstrumentationImpl.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 274 d0ca9ee56273
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 274
diff changeset
     2
 * Copyright 2003-2008 Sun Microsystems, Inc.  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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
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
package sun.instrument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.lang.reflect.AccessibleObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.instrument.ClassFileTransformer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.instrument.ClassDefinition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.instrument.Instrumentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.util.jar.JarFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * Copyright 2003 Wily Technology, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * The Java side of the JPLIS implementation. Works in concert with a native JVMTI agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * to implement the JPLIS API set. Provides both the Java API implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * the Instrumentation interface and utility Java routines to support the native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * Keeps a pointer to the native data structure in a scalar field to allow native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * processing behind native methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class InstrumentationImpl implements Instrumentation {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private final     TransformerManager      mTransformerManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private           TransformerManager      mRetransfomableTransformerManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // needs to store a native pointer, so use 64 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private final     long                    mNativeAgent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private final     boolean                 mEnvironmentSupportsRedefineClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private volatile  boolean                 mEnvironmentSupportsRetransformClassesKnown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private volatile  boolean                 mEnvironmentSupportsRetransformClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private final     boolean                 mEnvironmentSupportsNativeMethodPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    InstrumentationImpl(long    nativeAgent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                        boolean environmentSupportsRedefineClasses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                        boolean environmentSupportsNativeMethodPrefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        mTransformerManager                    = new TransformerManager(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        mRetransfomableTransformerManager      = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        mNativeAgent                           = nativeAgent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        mEnvironmentSupportsRedefineClasses    = environmentSupportsRedefineClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        mEnvironmentSupportsRetransformClassesKnown = false; // false = need to ask
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        mEnvironmentSupportsRetransformClasses = false;      // don't know yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        mEnvironmentSupportsNativeMethodPrefix = environmentSupportsNativeMethodPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    addTransformer(ClassFileTransformer transformer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        addTransformer(transformer, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    addTransformer(ClassFileTransformer transformer, boolean canRetransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (transformer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            throw new NullPointerException("null passed as 'transformer' in addTransformer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (canRetransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            if (!isRetransformClassesSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                  "adding retransformable transformers is not supported in this environment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (mRetransfomableTransformerManager == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                mRetransfomableTransformerManager = new TransformerManager(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            mRetransfomableTransformerManager.addTransformer(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (mRetransfomableTransformerManager.getTransformerCount() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                setHasRetransformableTransformers(mNativeAgent, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            mTransformerManager.addTransformer(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    public synchronized boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    removeTransformer(ClassFileTransformer transformer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (transformer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new NullPointerException("null passed as 'transformer' in removeTransformer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        TransformerManager mgr = findTransformerManager(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if (mgr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            mgr.removeTransformer(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            if (mgr.isRetransformable() && mgr.getTransformerCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                setHasRetransformableTransformers(mNativeAgent, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    isModifiableClass(Class<?> theClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (theClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                         "null passed as 'theClass' in isModifiableClass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return isModifiableClass0(mNativeAgent, theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    isRetransformClassesSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        // ask lazily since there is some overhead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (!mEnvironmentSupportsRetransformClassesKnown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            mEnvironmentSupportsRetransformClasses = isRetransformClassesSupported0(mNativeAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            mEnvironmentSupportsRetransformClassesKnown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return mEnvironmentSupportsRetransformClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    retransformClasses(Class<?>[] classes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (!isRetransformClassesSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
              "retransformClasses is not supported in this environment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        retransformClasses0(mNativeAgent, classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    public boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    isRedefineClassesSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        return mEnvironmentSupportsRedefineClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    redefineClasses(ClassDefinition[]   definitions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throws  ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if (!isRedefineClassesSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throw new UnsupportedOperationException("redefineClasses is not supported in this environment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (definitions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            throw new NullPointerException("null passed as 'definitions' in redefineClasses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        for (int i = 0; i < definitions.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (definitions[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                throw new NullPointerException("element of 'definitions' is null in redefineClasses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        if (definitions.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return; // short-circuit if there are no changes requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        redefineClasses0(mNativeAgent, definitions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public Class[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    getAllLoadedClasses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return getAllLoadedClasses0(mNativeAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public Class[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    getInitiatedClasses(ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        return getInitiatedClasses0(mNativeAgent, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    public long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    getObjectSize(Object objectToSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (objectToSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            throw new NullPointerException("null passed as 'objectToSize' in getObjectSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        return getObjectSize0(mNativeAgent, objectToSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    appendToBootstrapClassLoaderSearch(JarFile jarfile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        appendToClassLoaderSearch0(mNativeAgent, jarfile.getName(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    appendToSystemClassLoaderSearch(JarFile jarfile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        appendToClassLoaderSearch0(mNativeAgent, jarfile.getName(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    isNativeMethodPrefixSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return mEnvironmentSupportsNativeMethodPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    public synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (!isNativeMethodPrefixSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                   "setNativeMethodPrefix is not supported in this environment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        if (transformer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                       "null passed as 'transformer' in setNativeMethodPrefix");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        TransformerManager mgr = findTransformerManager(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (mgr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                       "transformer not registered in setNativeMethodPrefix");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        mgr.setNativeMethodPrefix(transformer, prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        String[] prefixes = mgr.getNativeMethodPrefixes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        setNativeMethodPrefixes(mNativeAgent, prefixes, mgr.isRetransformable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    private TransformerManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    findTransformerManager(ClassFileTransformer transformer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (mTransformerManager.includesTransformer(transformer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            return mTransformerManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (mRetransfomableTransformerManager != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                mRetransfomableTransformerManager.includesTransformer(transformer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            return mRetransfomableTransformerManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *  Natives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    private native boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    isModifiableClass0(long nativeAgent, Class<?> theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    private native boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    isRetransformClassesSupported0(long nativeAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    setHasRetransformableTransformers(long nativeAgent, boolean has);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    retransformClasses0(long nativeAgent, Class<?>[] classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    redefineClasses0(long nativeAgent, ClassDefinition[]  definitions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        throws  ClassNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    private native Class[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    getAllLoadedClasses0(long nativeAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private native Class[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    getInitiatedClasses0(long nativeAgent, ClassLoader loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private native long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    getObjectSize0(long nativeAgent, Object objectToSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    appendToClassLoaderSearch0(long nativeAgent, String jarfile, boolean bootLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    setNativeMethodPrefixes(long nativeAgent, String[] prefixes, boolean isRetransformable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        System.loadLibrary("instrument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     *  Internals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    // Enable or disable Java programming language access checks on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    // reflected object (for example, a method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    private static void setAccessible(final AccessibleObject ao, final boolean accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    ao.setAccessible(accessible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // Attempt to load and start an agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    loadClassAndStartAgent( String  classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                            String  methodname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                            String  optionsString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        ClassLoader mainAppLoader   = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        Class<?>    javaAgentClass  = mainAppLoader.loadClass(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        Method m = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        NoSuchMethodException firstExc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        boolean twoArgAgent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   306
        // The agent class must have a premain or agentmain method that
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   307
        // has 1 or 2 arguments. We check in the following order:
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   308
        //
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   309
        // 1) declared with a signature of (String, Instrumentation)
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   310
        // 2) declared with a signature of (String)
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   311
        // 3) inherited with a signature of (String, Instrumentation)
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   312
        // 4) inherited with a signature of (String)
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   313
        //
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   314
        // So the declared version of either 1-arg or 2-arg always takes
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   315
        // primary precedence over an inherited version. After that, the
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   316
        // 2-arg version takes precedence over the 1-arg version.
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   317
        //
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   318
        // If no method is found then we throw the NoSuchMethodException
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   319
        // from the first attempt so that the exception text indicates
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   320
        // the lookup failed for the 2-arg method (same as JDK5.0).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        try {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   323
            m = javaAgentClass.getDeclaredMethod( methodname,
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   324
                                 new Class[] {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   325
                                     String.class,
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   326
                                     java.lang.instrument.Instrumentation.class
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   327
                                 }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   328
                               );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            twoArgAgent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            // remember the NoSuchMethodException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            firstExc = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        if (m == null) {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   336
            // now try the declared 1-arg method
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   337
            try {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   338
                m = javaAgentClass.getDeclaredMethod(methodname,
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   339
                                                 new Class[] { String.class });
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   340
            } catch (NoSuchMethodException x) {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   341
                // ignore this exception because we'll try
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   342
                // two arg inheritance next
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   343
            }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   344
        }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   345
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   346
        if (m == null) {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   347
            // now try the inherited 2-arg method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            try {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   349
                m = javaAgentClass.getMethod( methodname,
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   350
                                 new Class[] {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   351
                                     String.class,
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   352
                                     java.lang.instrument.Instrumentation.class
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   353
                                 }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   354
                               );
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   355
                twoArgAgent = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            } catch (NoSuchMethodException x) {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   357
                // ignore this exception because we'll try
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   358
                // one arg inheritance next
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   359
            }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   360
        }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   361
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   362
        if (m == null) {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   363
            // finally try the inherited 1-arg method
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   364
            try {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   365
                m = javaAgentClass.getMethod(methodname,
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   366
                                             new Class[] { String.class });
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   367
            } catch (NoSuchMethodException x) {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   368
                // none of the methods exists so we throw the
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   369
                // first NoSuchMethodException as per 5.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                throw firstExc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        // the premain method should not be required to be public,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        // make it accessible so we can call it
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   376
        // Note: The spec says the following:
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   377
        //     The agent class must implement a public static premain method...
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        setAccessible(m, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        // invoke the 1 or 2-arg method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        if (twoArgAgent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            m.invoke(null, new Object[] { optionsString, this });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            m.invoke(null, new Object[] { optionsString });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // don't let others access a non-public premain method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        setAccessible(m, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    // WARNING: the native code knows the name & signature of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    loadClassAndCallPremain(    String  classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                                String  optionsString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        loadClassAndStartAgent( classname, "premain", optionsString );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    // WARNING: the native code knows the name & signature of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    loadClassAndCallAgentmain(  String  classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                                String  optionsString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
            throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        loadClassAndStartAgent( classname, "agentmain", optionsString );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    // WARNING: the native code knows the name & signature of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    transform(  ClassLoader         loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                String              classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                Class               classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                byte[]              classfileBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                boolean             isRetransformer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        TransformerManager mgr = isRetransformer?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                                        mRetransfomableTransformerManager :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                                        mTransformerManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        if (mgr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return null; // no manager, no transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return mgr.transform(   loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                                    classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                                    classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                                    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                                    classfileBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
}