jdk/test/java/lang/instrument/RetransformAgent.java
author dcubed
Mon, 24 Mar 2008 15:14:31 -0700
changeset 273 3d51c181a2e5
parent 2 90ce3da70b43
child 277 9d9c1747de00
permissions -rw-r--r--
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names Summary: Attribute values should be extracted without leading or trailing whitespace. Reviewed-by: ohair, sspitsyn
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 2005 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
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6274264 6274241 5070281
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test retransformClasses
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Robert Field, Sun Microsystems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run shell MakeJAR2.sh RetransformAgent RetransformApp 'Can-Retransform-Classes: true'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main/othervm -javaagent:RetransformAgent.jar RetransformApp
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.lang.instrument.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.ProtectionDomain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import ilib.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
class RetransformAgent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static ClassFileTransformer t1, t2, t3, t4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static Instrumentation inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    static boolean succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static int markCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static int[] markGolden = {30, 40, 20, 30, 40, 20, 30, 40, 20, 30, 40, 20,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
                               11, 40, 20, 11, 40, 20, 11, 40, 20, 11, 40, 20};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static class Tr implements ClassFileTransformer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        final String trname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        final boolean onLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        final int loadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        final boolean onRedef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        final int redefIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        final String cname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        final String nname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        Tr(String trname, boolean onLoad, int loadIndex, boolean onRedef, int redefIndex,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
           String cname, String nname) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            this.trname = trname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            this.onLoad = onLoad;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            this.loadIndex = loadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            this.onRedef = onRedef;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            this.redefIndex = redefIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            this.cname = cname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            this.nname = nname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public byte[] transform(ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                                String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                Class<?> classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                                byte[] classfileBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            boolean redef = classBeingRedefined != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            // System.err.println("hook " + trname + ": " + className +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            //                    (redef? " REDEF" : " LOAD"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            if ((redef? onRedef : onLoad) && className != null && className.equals(cname)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                Options opt = new Options();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                opt.shouldInstrumentIndexed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                opt.shouldInstrumentCall = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                opt.targetMethod = nname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                opt.fixedIndex = redef? redefIndex : loadIndex;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                opt.trackerClassName = "RetransformAgent";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                    byte[] newcf =  Inject.instrumentation(opt, loader, className, classfileBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    /*** debugging ...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                         String fname = trname + (redef?"_redef" : "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                         write_buffer(fname + "_before.class", classfileBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                         write_buffer(fname + "_instr.class", newcf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    System.err.println(trname + ": " + className + " index: " + opt.fixedIndex +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                                       (redef? " REDEF" : " LOAD") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                                       " len before: " + classfileBuffer.length +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                                       " after: " + newcf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                    return newcf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                } catch (Throwable ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    System.err.println("Injection failure: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    static void write_buffer(String fname, byte[]buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            FileOutputStream outStream = new FileOutputStream(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            outStream.write(buffer, 0, buffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            outStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            System.err.println("EXCEPTION in write_buffer: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public static void premain (String agentArgs, Instrumentation instArg) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        inst = instArg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        System.err.println("Premain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        t1 = new Tr("TR1", false, 10, true, 11, "RetransformApp", "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        inst.addTransformer(t1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        t2 = new Tr("TN2", true,  20, true, 21, "RetransformApp", "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        inst.addTransformer(t2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        t3 = new Tr("TR3", true,  30, true, 31, "RetransformApp", "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        inst.addTransformer(t3, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        t4 = new Tr("TN4", true,  40, true, 41, "RetransformApp", "foo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        inst.addTransformer(t4, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public static void undo() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        inst.removeTransformer(t3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            System.err.println("RETRANSFORM");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            inst.retransformClasses(new RetransformApp().getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            System.err.println("EXCEPTION on undo: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static boolean succeeded() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return succeeded && markCount == markGolden.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    public static void callTracker(int mark) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        System.err.println("got mark " + mark);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        if (markCount >= markGolden.length || mark != markGolden[markCount++]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}