jdk/test/java/lang/instrument/NativeMethodPrefixAgent.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 277 9d9c1747de00
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 277
diff changeset
     2
 * Copyright 2005-2008 Sun Microsystems, Inc.  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
 *
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 6263319
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary test setNativeMethodPrefix
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Robert Field, Sun Microsystems
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
277
9d9c1747de00 6528548: 4/4 NativeMethodPrefixAgent.java times out intermittently in nightly
dcubed
parents: 2
diff changeset
    30
 * @run shell/timeout=240 MakeJAR2.sh NativeMethodPrefixAgent NativeMethodPrefixApp 'Can-Retransform-Classes: true' 'Can-Set-Native-Method-Prefix: true'
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main/othervm -javaagent:NativeMethodPrefixAgent.jar NativeMethodPrefixApp
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 NativeMethodPrefixAgent {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static ClassFileTransformer t0, t1, t2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    static Instrumentation inst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static class Tr implements ClassFileTransformer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        final String trname;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        final int transformId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        Tr(int transformId) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            this.trname = "tr" + transformId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            this.transformId = transformId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        public byte[]
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        transform(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            ClassLoader loader,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            String className,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            Class<?> classBeingRedefined,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            ProtectionDomain    protectionDomain,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            byte[] classfileBuffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            boolean redef = classBeingRedefined != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.println(trname + ": " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                               (redef? "Retransforming " : "Loading ") + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if (className != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                Options opt = new Options();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                opt.shouldInstrumentNativeMethods = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                opt.trackerClassName = "bootreporter/StringIdCallbackReporter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                opt.wrappedTrackerMethodName = "tracker";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                opt.fixedIndex = transformId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                opt.wrappedPrefix = "wrapped_" + trname + "_";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    byte[] newcf =  Inject.instrumentation(opt, loader, className, classfileBuffer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                    return redef? null : newcf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                } catch (Throwable ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                    System.err.println("ERROR: Injection failure: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                    ex.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    System.err.println("Returning bad class file, to cause test failure");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                    return new byte[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    // for debugging
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    static void write_buffer(String fname, byte[]buffer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            FileOutputStream outStream = new FileOutputStream(fname);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            outStream.write(buffer, 0, buffer.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            outStream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.err.println("EXCEPTION in write_buffer: " + ex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    premain (String agentArgs, Instrumentation instArg)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        throws IOException, IllegalClassFormatException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        ClassNotFoundException, UnmodifiableClassException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        inst = instArg;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        System.out.println("Premain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        t1 = new Tr(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        t2 = new Tr(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        t0 = new Tr(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        inst.addTransformer(t1, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        inst.addTransformer(t2, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        inst.addTransformer(t0, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        instArg.setNativeMethodPrefix(t0, "wrapped_tr0_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        instArg.setNativeMethodPrefix(t1, "wrapped_tr1_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        instArg.setNativeMethodPrefix(t2, "wrapped_tr2_");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // warm up: cause load of transformer classes before used during class load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        instArg.retransformClasses(Runtime.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
}