jdk/test/com/sun/tools/attach/RedefineAgent.java
author duke
Wed, 05 Jul 2017 16:38:40 +0200
changeset 782 f2e6591ff695
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2006 Sun Microsystems, Inc.  All Rights Reserved.
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.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
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
 * @bug 6439234 6446941
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * Test attach used in concert with transformers and redefineClasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * 6439234 java.lang.instrument: 8 JCK JPLIS tests fail when running in Live phase (no transform events)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * 6446941 java.lang.instrument: multiple agent attach fails (first agent chooses capabilities)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.Socket;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.net.InetSocketAddress;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.lang.instrument.ClassFileTransformer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.lang.instrument.Instrumentation;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.lang.instrument.ClassDefinition;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public class RedefineAgent implements ClassFileTransformer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static byte[] classfilebytes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static final String targetName = "java.math.BigInteger";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static final String targetNameSlashes = "java/math/BigInteger";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static boolean gotRedefineTransform = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // test transform and capture class bytes for redefine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public byte[] transform(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                            String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                            Class<?> classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                            ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                            byte[] classfileBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (className.equals(targetNameSlashes)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            if (classBeingRedefined == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                System.out.println("Got bytes for: " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                classfilebytes = Arrays.copyOf(classfileBuffer, classfileBuffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                gotRedefineTransform = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // test transform and redefine for an attached agent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static void testRedefine(Instrumentation inst) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Class[] classes = inst.getAllLoadedClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        for (Class k : classes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (k.getName().equals(targetName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                throw new Exception("RedefineAgent Test error: class " + targetName + " has already been loaded.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        inst.addTransformer(new RedefineAgent());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        ClassLoader.getSystemClassLoader().loadClass(targetName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        classes = inst.getAllLoadedClasses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        Class targetClass = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        for (Class k : classes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            if (k.getName().equals(targetName)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                targetClass = k;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        if (targetClass == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            throw new Exception("RedefineAgent Test error: class " + targetName + " not loaded.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (classfilebytes == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            throw new Exception("RedefineAgent Error(6439234): no transform call for class " + targetName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        ClassDefinition cd = new ClassDefinition(targetClass, classfilebytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        inst.redefineClasses(cd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        System.out.println("RedefineAgent did redefine.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (gotRedefineTransform) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            System.out.println("RedefineAgent got redefine transform.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            throw new Exception("RedefineAgent Error(6439234): no transform call for redefine " + targetName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public static void agentmain(String args, Instrumentation inst) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println("RedefineAgent running...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        System.out.println("RedefineAgent redefine supported: " + inst.isRedefineClassesSupported());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int port = Integer.parseInt(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        System.out.println("RedefineAgent connecting back to Tool....");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        Socket s = new Socket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        s.connect( new InetSocketAddress(port) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        System.out.println("RedefineAgent connected to Tool.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        testRedefine(inst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
}