jdk/src/java.instrument/share/classes/sun/instrument/InstrumentationImpl.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 42338 a60f280f803c
child 45004 ea3137042a61
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
     2
 * Copyright (c) 2003, 2016, 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
package sun.instrument;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
    28
import java.lang.instrument.UnmodifiableModuleException;
2
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
import java.lang.instrument.ClassFileTransformer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.lang.instrument.ClassDefinition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.lang.instrument.Instrumentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.ProtectionDomain;
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
    37
import java.util.Collections;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
    38
import java.util.ArrayList;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
    39
import java.util.HashMap;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
    40
import java.util.HashSet;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
    41
import java.util.List;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
    42
import java.util.Map;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
    43
import java.util.Set;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
    44
import java.util.jar.JarFile;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
    46
import jdk.internal.module.Modules;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Copyright 2003 Wily Technology, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * The Java side of the JPLIS implementation. Works in concert with a native JVMTI agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * to implement the JPLIS API set. Provides both the Java API implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * the Instrumentation interface and utility Java routines to support the native code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Keeps a pointer to the native data structure in a scalar field to allow native
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * processing behind native methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
public class InstrumentationImpl implements Instrumentation {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private final     TransformerManager      mTransformerManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private           TransformerManager      mRetransfomableTransformerManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // needs to store a native pointer, so use 64 bits
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private final     long                    mNativeAgent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private final     boolean                 mEnvironmentSupportsRedefineClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private volatile  boolean                 mEnvironmentSupportsRetransformClassesKnown;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private volatile  boolean                 mEnvironmentSupportsRetransformClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private final     boolean                 mEnvironmentSupportsNativeMethodPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    private
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    InstrumentationImpl(long    nativeAgent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                        boolean environmentSupportsRedefineClasses,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                        boolean environmentSupportsNativeMethodPrefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        mTransformerManager                    = new TransformerManager(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        mRetransfomableTransformerManager      = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        mNativeAgent                           = nativeAgent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        mEnvironmentSupportsRedefineClasses    = environmentSupportsRedefineClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        mEnvironmentSupportsRetransformClassesKnown = false; // false = need to ask
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        mEnvironmentSupportsRetransformClasses = false;      // don't know yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        mEnvironmentSupportsNativeMethodPrefix = environmentSupportsNativeMethodPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    addTransformer(ClassFileTransformer transformer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        addTransformer(transformer, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    addTransformer(ClassFileTransformer transformer, boolean canRetransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        if (transformer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new NullPointerException("null passed as 'transformer' in addTransformer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (canRetransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            if (!isRetransformClassesSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                  "adding retransformable transformers is not supported in this environment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            if (mRetransfomableTransformerManager == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                mRetransfomableTransformerManager = new TransformerManager(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            mRetransfomableTransformerManager.addTransformer(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (mRetransfomableTransformerManager.getTransformerCount() == 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                setHasRetransformableTransformers(mNativeAgent, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            mTransformerManager.addTransformer(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public synchronized boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    removeTransformer(ClassFileTransformer transformer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (transformer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            throw new NullPointerException("null passed as 'transformer' in removeTransformer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        TransformerManager mgr = findTransformerManager(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (mgr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            mgr.removeTransformer(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (mgr.isRetransformable() && mgr.getTransformerCount() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                setHasRetransformableTransformers(mNativeAgent, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    isModifiableClass(Class<?> theClass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if (theClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                         "null passed as 'theClass' in isModifiableClass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        return isModifiableClass0(mNativeAgent, theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   134
    public boolean isModifiableModule(Module module) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   135
        if (module == null) {
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   136
            throw new NullPointerException("'module' is null");
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   137
        }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   138
        return true;
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   139
    }
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   140
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    public boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    isRetransformClassesSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        // ask lazily since there is some overhead
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (!mEnvironmentSupportsRetransformClassesKnown) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            mEnvironmentSupportsRetransformClasses = isRetransformClassesSupported0(mNativeAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            mEnvironmentSupportsRetransformClassesKnown = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        return mEnvironmentSupportsRetransformClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    public void
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   152
    retransformClasses(Class<?>... classes) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        if (!isRetransformClassesSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
              "retransformClasses is not supported in this environment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        retransformClasses0(mNativeAgent, classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    isRedefineClassesSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return mEnvironmentSupportsRedefineClasses;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public void
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   166
    redefineClasses(ClassDefinition...  definitions)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throws  ClassNotFoundException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (!isRedefineClassesSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            throw new UnsupportedOperationException("redefineClasses is not supported in this environment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (definitions == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            throw new NullPointerException("null passed as 'definitions' in redefineClasses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        for (int i = 0; i < definitions.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            if (definitions[i] == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                throw new NullPointerException("element of 'definitions' is null in redefineClasses");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (definitions.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            return; // short-circuit if there are no changes requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        redefineClasses0(mNativeAgent, definitions);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
20754
3d7b2fafc34b 8025967: addition of -Werror broke the old build
valeriep
parents: 14342
diff changeset
   186
    @SuppressWarnings("rawtypes")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public Class[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    getAllLoadedClasses() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return getAllLoadedClasses0(mNativeAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
20754
3d7b2fafc34b 8025967: addition of -Werror broke the old build
valeriep
parents: 14342
diff changeset
   192
    @SuppressWarnings("rawtypes")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    public Class[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    getInitiatedClasses(ClassLoader loader) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return getInitiatedClasses0(mNativeAgent, loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    public long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    getObjectSize(Object objectToSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        if (objectToSize == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            throw new NullPointerException("null passed as 'objectToSize' in getObjectSize");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        return getObjectSize0(mNativeAgent, objectToSize);
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 void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    appendToBootstrapClassLoaderSearch(JarFile jarfile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        appendToClassLoaderSearch0(mNativeAgent, jarfile.getName(), true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    appendToSystemClassLoaderSearch(JarFile jarfile) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        appendToClassLoaderSearch0(mNativeAgent, jarfile.getName(), false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    isNativeMethodPrefixSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return mEnvironmentSupportsNativeMethodPrefix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public synchronized void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    setNativeMethodPrefix(ClassFileTransformer transformer, String prefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        if (!isNativeMethodPrefixSupported()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                   "setNativeMethodPrefix is not supported in this environment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        if (transformer == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            throw new NullPointerException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                       "null passed as 'transformer' in setNativeMethodPrefix");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        TransformerManager mgr = findTransformerManager(transformer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (mgr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                       "transformer not registered in setNativeMethodPrefix");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        mgr.setNativeMethodPrefix(transformer, prefix);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        String[] prefixes = mgr.getNativeMethodPrefixes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        setNativeMethodPrefixes(mNativeAgent, prefixes, mgr.isRetransformable());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   241
    @Override
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   242
    public void redefineModule(Module module,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   243
                               Set<Module> extraReads,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   244
                               Map<String, Set<Module>> extraExports,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   245
                               Map<String, Set<Module>> extraOpens,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   246
                               Set<Class<?>> extraUses,
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   247
                               Map<Class<?>, List<Class<?>>> extraProvides)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   248
    {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   249
        if (!module.isNamed())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   250
            return;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   251
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   252
        if (!isModifiableModule(module))
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   253
            throw new UnmodifiableModuleException(module.getName());
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   254
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   255
        // copy and check reads
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   256
        extraReads = new HashSet<>(extraReads);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   257
        if (extraReads.contains(null))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   258
            throw new NullPointerException("'extraReads' contains null");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   259
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   260
        // copy and check exports and opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   261
        extraExports = cloneAndCheckMap(module, extraExports);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   262
        extraOpens = cloneAndCheckMap(module, extraOpens);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   263
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   264
        // copy and check uses
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   265
        extraUses = new HashSet<>(extraUses);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   266
        if (extraUses.contains(null))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   267
            throw new NullPointerException("'extraUses' contains null");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   268
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   269
        // copy and check provides
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   270
        Map<Class<?>, List<Class<?>>> tmpProvides = new HashMap<>();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   271
        for (Map.Entry<Class<?>, List<Class<?>>> e : extraProvides.entrySet()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   272
            Class<?> service = e.getKey();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   273
            if (service == null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   274
                throw new NullPointerException("'extraProvides' contains null");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   275
            List<Class<?>> providers = new ArrayList<>(e.getValue());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   276
            if (providers.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   277
                throw new IllegalArgumentException("list of providers is empty");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   278
            providers.forEach(p -> {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   279
                if (p.getModule() != module)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   280
                    throw new IllegalArgumentException(p + " not in " + module);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   281
                if (!service.isAssignableFrom(p))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   282
                    throw new IllegalArgumentException(p + " is not a " + service);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   283
            });
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   284
            tmpProvides.put(service, providers);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   285
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   286
        extraProvides = tmpProvides;
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   287
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   288
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   289
        // update reads
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   290
        extraReads.forEach(m -> Modules.addReads(module, m));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   291
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   292
        // update exports
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   293
        for (Map.Entry<String, Set<Module>> e : extraExports.entrySet()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   294
            String pkg = e.getKey();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   295
            Set<Module> targets = e.getValue();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   296
            targets.forEach(m -> Modules.addExports(module, pkg, m));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   297
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   298
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   299
        // update opens
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   300
        for (Map.Entry<String, Set<Module>> e : extraOpens.entrySet()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   301
            String pkg = e.getKey();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   302
            Set<Module> targets = e.getValue();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   303
            targets.forEach(m -> Modules.addOpens(module, pkg, m));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   304
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   305
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   306
        // update uses
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   307
        extraUses.forEach(service -> Modules.addUses(module, service));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   308
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   309
        // update provides
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   310
        for (Map.Entry<Class<?>, List<Class<?>>> e : extraProvides.entrySet()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   311
            Class<?> service = e.getKey();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   312
            List<Class<?>> providers = e.getValue();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   313
            providers.forEach(p -> Modules.addProvides(module, service, p));
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   314
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   315
    }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   316
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   317
    private Map<String, Set<Module>>
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   318
        cloneAndCheckMap(Module module, Map<String, Set<Module>> map)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   319
    {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   320
        if (map.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   321
            return Collections.emptyMap();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   322
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   323
        Map<String, Set<Module>> result = new HashMap<>();
44545
83b611b88ac8 8177530: Module system implementation refresh (4/2017)
alanb
parents: 42338
diff changeset
   324
        Set<String> packages = module.getPackages();
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   325
        for (Map.Entry<String, Set<Module>> e : map.entrySet()) {
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   326
            String pkg = e.getKey();
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   327
            if (pkg == null)
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   328
                throw new NullPointerException("package cannot be null");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   329
            if (!packages.contains(pkg))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   330
                throw new IllegalArgumentException(pkg + " not in module");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   331
            Set<Module> targets = new HashSet<>(e.getValue());
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   332
            if (targets.isEmpty())
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   333
                throw new IllegalArgumentException("set of targets is empty");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   334
            if (targets.contains(null))
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   335
                throw new NullPointerException("set of targets cannot include null");
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   336
            result.put(pkg, targets);
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   337
        }
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 39739
diff changeset
   338
        return result;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   339
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   340
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   341
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    private TransformerManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    findTransformerManager(ClassFileTransformer transformer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        if (mTransformerManager.includesTransformer(transformer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            return mTransformerManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        if (mRetransfomableTransformerManager != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                mRetransfomableTransformerManager.includesTransformer(transformer)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            return mRetransfomableTransformerManager;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     *  Natives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private native boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
    isModifiableClass0(long nativeAgent, Class<?> theClass);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    private native boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    isRetransformClassesSupported0(long nativeAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    setHasRetransformableTransformers(long nativeAgent, boolean has);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    retransformClasses0(long nativeAgent, Class<?>[] classes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    redefineClasses0(long nativeAgent, ClassDefinition[]  definitions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        throws  ClassNotFoundException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
20754
3d7b2fafc34b 8025967: addition of -Werror broke the old build
valeriep
parents: 14342
diff changeset
   374
    @SuppressWarnings("rawtypes")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    private native Class[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    getAllLoadedClasses0(long nativeAgent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
20754
3d7b2fafc34b 8025967: addition of -Werror broke the old build
valeriep
parents: 14342
diff changeset
   378
    @SuppressWarnings("rawtypes")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    private native Class[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    getInitiatedClasses0(long nativeAgent, ClassLoader loader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
    private native long
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    getObjectSize0(long nativeAgent, Object objectToSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    appendToClassLoaderSearch0(long nativeAgent, String jarfile, boolean bootLoader);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    private native void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    setNativeMethodPrefixes(long nativeAgent, String[] prefixes, boolean isRetransformable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        System.loadLibrary("instrument");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *  Internals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    // Enable or disable Java programming language access checks on a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
    // reflected object (for example, a method)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    private static void setAccessible(final AccessibleObject ao, final boolean accessible) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        AccessController.doPrivileged(new PrivilegedAction<Object>() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                public Object run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    ao.setAccessible(accessible);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                }});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    // Attempt to load and start an agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    loadClassAndStartAgent( String  classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                            String  methodname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                            String  optionsString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        ClassLoader mainAppLoader   = ClassLoader.getSystemClassLoader();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        Class<?>    javaAgentClass  = mainAppLoader.loadClass(classname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        Method m = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        NoSuchMethodException firstExc = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        boolean twoArgAgent = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   424
        // 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
   425
        // 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
   426
        //
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   427
        // 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
   428
        // 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
   429
        // 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
   430
        // 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
   431
        //
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   432
        // 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
   433
        // 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
   434
        // 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
   435
        //
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   436
        // 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
   437
        // 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
   438
        // the lookup failed for the 2-arg method (same as JDK5.0).
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        try {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   441
            m = javaAgentClass.getDeclaredMethod( methodname,
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   442
                                 new Class<?>[] {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   443
                                     String.class,
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   444
                                     java.lang.instrument.Instrumentation.class
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   445
                                 }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   446
                               );
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            twoArgAgent = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        } catch (NoSuchMethodException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
            // remember the NoSuchMethodException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            firstExc = x;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (m == null) {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   454
            // 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
   455
            try {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   456
                m = javaAgentClass.getDeclaredMethod(methodname,
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   457
                                                 new Class<?>[] { String.class });
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   458
            } catch (NoSuchMethodException x) {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   459
                // 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
   460
                // two arg inheritance next
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   461
            }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   462
        }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   463
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   464
        if (m == null) {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   465
            // now try the inherited 2-arg method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            try {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   467
                m = javaAgentClass.getMethod( methodname,
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   468
                                 new Class<?>[] {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   469
                                     String.class,
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   470
                                     java.lang.instrument.Instrumentation.class
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   471
                                 }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   472
                               );
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   473
                twoArgAgent = true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            } catch (NoSuchMethodException x) {
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   475
                // 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
   476
                // one arg inheritance next
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   477
            }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   478
        }
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   479
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   480
        if (m == null) {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   481
            // 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
   482
            try {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   483
                m = javaAgentClass.getMethod(methodname,
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   484
                                             new Class<?>[] { String.class });
274
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   485
            } catch (NoSuchMethodException x) {
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   486
                // 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
   487
                // first NoSuchMethodException as per 5.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                throw firstExc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        // the premain method should not be required to be public,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        // 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
   494
        // Note: The spec says the following:
d0ca9ee56273 6289149: 4/4 Java Agent will pick wrong execution path while attaching
dcubed
parents: 2
diff changeset
   495
        //     The agent class must implement a public static premain method...
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        setAccessible(m, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        // invoke the 1 or 2-arg method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (twoArgAgent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            m.invoke(null, new Object[] { optionsString, this });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            m.invoke(null, new Object[] { optionsString });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
    // WARNING: the native code knows the name & signature of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    loadClassAndCallPremain(    String  classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                                String  optionsString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        loadClassAndStartAgent( classname, "premain", optionsString );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    // WARNING: the native code knows the name & signature of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    loadClassAndCallAgentmain(  String  classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                                String  optionsString)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        loadClassAndStartAgent( classname, "agentmain", optionsString );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    // WARNING: the native code knows the name & signature of this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
    private byte[]
39739
5d058d6bd245 8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents: 37807
diff changeset
   527
    transform(  Module              module,
5d058d6bd245 8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents: 37807
diff changeset
   528
                ClassLoader         loader,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                String              classname,
11125
99b115114fa3 7117357: Warnings in sun.instrument, tools and other sun.* classes
alanb
parents: 5506
diff changeset
   530
                Class<?>            classBeingRedefined,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
                ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                byte[]              classfileBuffer,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                boolean             isRetransformer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
        TransformerManager mgr = isRetransformer?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                                        mRetransfomableTransformerManager :
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                                        mTransformerManager;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   537
        // module is null when not a class load or when loading a class in an
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   538
        // unnamed module and this is the first type to be loaded in the package.
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   539
        if (module == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   540
            if (classBeingRedefined != null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   541
                module = classBeingRedefined.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   542
            } else {
37807
78e5cb21ca13 8156147: NPE in InstrumentationImpl.transform when loading classes from -Xbootclasspath/a
sspitsyn
parents: 36511
diff changeset
   543
                module = (loader == null) ? jdk.internal.loader.BootLoader.getUnnamedModule()
78e5cb21ca13 8156147: NPE in InstrumentationImpl.transform when loading classes from -Xbootclasspath/a
sspitsyn
parents: 36511
diff changeset
   544
                                          : loader.getUnnamedModule();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   545
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   546
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
        if (mgr == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
            return null; // no manager, no transform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } else {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents: 25859
diff changeset
   550
            return mgr.transform(   module,
39739
5d058d6bd245 8159147: Add ClassLoader parameter to new ClassFileTransformer transform method
sspitsyn
parents: 37807
diff changeset
   551
                                    loader,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                                    classname,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                                    classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                                    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
                                    classfileBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
}