< prev index next >

test/serviceability/dcmd/jvmti/LoadAgentDcmdTest.java

Print this page




   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.io.*;
  24 import java.nio.file.*;
  25 import java.util.jar.Attributes;
  26 import java.util.jar.JarEntry;
  27 import java.util.jar.JarOutputStream;
  28 import java.util.jar.Manifest;
  29 import jdk.test.lib.*;

  30 import jdk.test.lib.dcmd.*;
  31 import org.testng.annotations.Test;
  32 
  33 /*
  34  * Test to attach JVMTI java agent.
  35  *
  36  * @test
  37  * @bug 8147388
  38  * @library /testlibrary
  39  * @modules java.base/jdk.internal.misc
  40  *          java.compiler
  41  *          java.instrument
  42  *          java.management
  43  *          jdk.jvmstat/sun.jvmstat.monitor
  44  * @build ClassFileInstaller jdk.test.lib.* SimpleJvmtiAgent
  45  * @ignore 8150318
  46  * @run main ClassFileInstaller SimpleJvmtiAgent
  47  * @run testng LoadAgentDcmdTest
  48  */
  49 public class LoadAgentDcmdTest {
  50 
  51     public String getLibInstrumentPath() throws FileNotFoundException {
  52         String jdkPath = System.getProperty("test.jdk");
  53 
  54         if (jdkPath == null) {
  55             throw new RuntimeException(
  56                       "System property 'test.jdk' not set. " +
  57                       "This property is normally set by jtreg. " +
  58                       "When running test separately, set this property using " +
  59                       "'-Dtest.jdk=/path/to/jdk'.");
  60         }
  61 
  62         Path libpath = Paths.get(jdkPath, Platform.jdkLibPath(), Platform.sharedObjectName("instrument"));
  63 
  64         if (!libpath.toFile().exists()) {
  65             throw new FileNotFoundException(
  66                       "Could not find " + libpath.toAbsolutePath());
  67         }
  68 
  69         return libpath.toAbsolutePath().toString();
  70     }
  71 
  72 
  73     public void createJarFileForAgent()
  74       throws IOException {
  75 
  76         final String jarName = "agent.jar";
  77         final String agentClass = "SimpleJvmtiAgent";
  78 
  79         Manifest manifest = new Manifest();
  80 
  81         manifest.getMainAttributes().put(
  82              Attributes.Name.MANIFEST_VERSION, "1.0");


 112 
 113             // Test 2: Native agent, with arguments
 114             output = executor.execute("JVMTI.agent_load " +
 115                                           libpath + " \"agent.jar=foo=bar\"");
 116             output.stderrShouldBeEmpty();
 117 
 118             // Test 3: Java agent, no arguments
 119             output = executor.execute("JVMTI.agent_load " +
 120                                           "agent.jar");
 121             output.stderrShouldBeEmpty();
 122 
 123             // Test 4: Java agent, with arguments
 124             output = executor.execute("JVMTI.agent_load " +
 125                                           "\"agent.jar=foo=bar\"");
 126             output.stderrShouldBeEmpty();
 127 
 128         } catch (Exception e) {
 129             throw new RuntimeException(e);
 130         }
 131     }



























 132 
 133     @Test
 134     public void jmx() throws Throwable {
 135         run(new JMXExecutor());
 136     }
 137 
 138     @Test
 139     public void cli() throws Throwable {
 140         run(new PidJcmdExecutor());
 141     }
 142 }


   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.io.*;
  24 import java.nio.file.*;
  25 import java.util.jar.Attributes;
  26 import java.util.jar.JarEntry;
  27 import java.util.jar.JarOutputStream;
  28 import java.util.jar.Manifest;
  29 import jdk.test.lib.Platform;
  30 import jdk.test.lib.process.OutputAnalyzer;
  31 import jdk.test.lib.dcmd.*;
  32 import org.testng.annotations.Test;
  33 
  34 /*
  35  * Test to attach JVMTI java agent.
  36  *
  37  * @test
  38  * @bug 8147388
  39  * @library /test/lib
  40  * @modules java.base/jdk.internal.misc
  41  *          java.compiler
  42  *          java.instrument
  43  *          java.management
  44  *          jdk.jvmstat/sun.jvmstat.monitor
  45  * @build SimpleJvmtiAgent
  46  * @ignore 8150318
  47  * @run main ClassFileInstaller SimpleJvmtiAgent
  48  * @run testng LoadAgentDcmdTest
  49  */
  50 public class LoadAgentDcmdTest {
  51 
  52     public String getLibInstrumentPath() throws FileNotFoundException {
  53         String jdkPath = System.getProperty("test.jdk");
  54 
  55         if (jdkPath == null) {
  56             throw new RuntimeException(
  57                       "System property 'test.jdk' not set. " +
  58                       "This property is normally set by jtreg. " +
  59                       "When running test separately, set this property using " +
  60                       "'-Dtest.jdk=/path/to/jdk'.");
  61         }
  62 
  63         Path libpath = Paths.get(jdkPath, jdkLibPath(), sharedObjectName("instrument"));
  64 
  65         if (!libpath.toFile().exists()) {
  66             throw new FileNotFoundException(
  67                       "Could not find " + libpath.toAbsolutePath());
  68         }
  69 
  70         return libpath.toAbsolutePath().toString();
  71     }
  72 
  73 
  74     public void createJarFileForAgent()
  75       throws IOException {
  76 
  77         final String jarName = "agent.jar";
  78         final String agentClass = "SimpleJvmtiAgent";
  79 
  80         Manifest manifest = new Manifest();
  81 
  82         manifest.getMainAttributes().put(
  83              Attributes.Name.MANIFEST_VERSION, "1.0");


 113 
 114             // Test 2: Native agent, with arguments
 115             output = executor.execute("JVMTI.agent_load " +
 116                                           libpath + " \"agent.jar=foo=bar\"");
 117             output.stderrShouldBeEmpty();
 118 
 119             // Test 3: Java agent, no arguments
 120             output = executor.execute("JVMTI.agent_load " +
 121                                           "agent.jar");
 122             output.stderrShouldBeEmpty();
 123 
 124             // Test 4: Java agent, with arguments
 125             output = executor.execute("JVMTI.agent_load " +
 126                                           "\"agent.jar=foo=bar\"");
 127             output.stderrShouldBeEmpty();
 128 
 129         } catch (Exception e) {
 130             throw new RuntimeException(e);
 131         }
 132     }
 133     /**
 134      * return path to library inside jdk tree
 135      */
 136     public static String jdkLibPath() {
 137         if (Platform.isWindows()) {
 138             return "bin";
 139         }
 140         if (Platform.isOSX()) {
 141             return "lib";
 142         }
 143 
 144         return "lib/" + Platform.getOsArch();
 145     }
 146 
 147     /**
 148      * Build name of shared object according to platform rules
 149      */
 150     public static String sharedObjectName(String name) {
 151         if (Platform.isWindows()) {
 152             return name + ".dll";
 153         }
 154         if (Platform.isOSX()) {
 155             return "lib" + name + ".dylib";
 156         }
 157         return "lib" + name + ".so";
 158     }
 159 }
 160 
 161     @Test
 162     public void jmx() throws Throwable {
 163         run(new JMXExecutor());
 164     }
 165 
 166     @Test
 167     public void cli() throws Throwable {
 168         run(new PidJcmdExecutor());
 169     }
 170 }
< prev index next >