hotspot/test/testlibrary/jvmti/TransformerAgent.java
author jiangli
Sun, 18 Sep 2016 21:10:48 -0400
changeset 41182 dbd59c1da636
permissions -rw-r--r--
8078644: CDS needs to support JVMTI CFLH Summary: Support posting CLFH for shared classes. Tests are contributed by Misha Seledtsov. Reviewed-by: iklam, coleenp, acorn, dcubed, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41182
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
     1
/*
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
     4
 *
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
     8
 *
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    13
 * accompanied this code).
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    14
 *
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    18
 *
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    21
 * questions.
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    22
 */
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    23
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    24
import java.lang.instrument.ClassFileTransformer;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    25
import java.lang.instrument.IllegalClassFormatException;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    26
import java.lang.instrument.Instrumentation;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    27
import java.security.ProtectionDomain;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    28
import java.util.HashMap;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    29
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    30
// This is a test utility class used to transform
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    31
// specified classes via initial transformation (ClassFileLoadHook).
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    32
// Names of classes to be transformed are supplied as arguments,
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    33
// the phrase to be transformed is a hard-coded predefined
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    34
// fairly unique phrase.
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    35
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    36
public class TransformerAgent {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    37
    private static String[] classesToTransform;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    38
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    39
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    40
    private static void log(String msg) {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    41
        System.out.println("TransformerAgent: " + msg);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    42
    }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    43
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    44
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    45
    // arguments are comma-separated list of classes to transform
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    46
    public static void premain(String agentArguments, Instrumentation instrumentation) {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    47
        log("premain() is called, arguments = " + agentArguments);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    48
        classesToTransform = agentArguments.split(",");
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    49
        instrumentation.addTransformer(new SimpleTransformer(), /*canRetransform=*/true);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    50
    }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    51
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    52
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    53
    public static void agentmain(String args, Instrumentation inst) throws Exception {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    54
        log("agentmain() is called");
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    55
        premain(args, inst);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    56
    }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    57
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    58
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    59
    static class SimpleTransformer implements ClassFileTransformer {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    60
       public byte[] transform(ClassLoader loader, String name, Class<?> classBeingRedefined,
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    61
                            ProtectionDomain pd, byte[] buffer) throws IllegalClassFormatException {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    62
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    63
            log("SimpleTransformer called for: " + name + "@" + incrCounter(name));
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    64
            if (!shouldTransform(name))
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    65
                return null;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    66
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    67
            log("transforming: class name = " + name);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    68
            int nrOfReplacements = TransformUtil.replace(buffer, TransformUtil.BeforePattern,
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    69
                                                         TransformUtil.AfterPattern);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    70
            log("replaced the string, nrOfReplacements = " + nrOfReplacements);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    71
            return buffer;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    72
        }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    73
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    74
        // Check class name pattern, since test should only transform certain classes
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    75
        private static boolean shouldTransform(String name) {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    76
            for (String match : classesToTransform) {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    77
                if (name.matches(match)) {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    78
                    log("shouldTransform: match-found, match = " + match);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    79
                    return true;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    80
                }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    81
            }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    82
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    83
            return false;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    84
        }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    85
    }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    86
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    87
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    88
    static HashMap<String, Integer> counterMap = new HashMap<>();
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    89
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    90
    static Integer incrCounter(String className) {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    91
        Integer i = counterMap.get(className);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    92
        if (i == null) {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    93
            i = new Integer(1);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    94
        } else {
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    95
            i = new Integer(i.intValue() + 1);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    96
        }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    97
        counterMap.put(className, i);
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    98
        return i;
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
    99
    }
dbd59c1da636 8078644: CDS needs to support JVMTI CFLH
jiangli
parents:
diff changeset
   100
}