test/jdk/com/sun/tools/attach/RedefineAgent.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21968
diff changeset
     2
 * Copyright (c) 2006, 2013, 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
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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;
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    46
    static final String targetName = "RedefineDummy";
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    47
    static final String targetNameSlashes = "RedefineDummy";
2
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,
21968
619035df6988 6461635: [TESTBUG] BasicTests.sh test fails intermittently
sla
parents: 5506
diff changeset
    54
                            ProtectionDomain  protectionDomain,
2
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
}