1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   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  * @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, jdkLibPath(), 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");
  83 
  84         manifest.getMainAttributes().put(
  85              new Attributes.Name("Agent-Class"), agentClass);
  86 
  87         JarOutputStream target = null;
  88 
  89         try {
  90             target = new
  91               JarOutputStream(new FileOutputStream(jarName), manifest);
  92             JarEntry entry = new JarEntry(agentClass + ".class");
  93             target.putNextEntry(entry);
  94             target.closeEntry();
  95         } finally {
  96             target.close();
  97         }
  98     }
  99 
 100     public void run(CommandExecutor executor)  {
 101         try{
 102 
 103             createJarFileForAgent();
 104 
 105             String libpath = getLibInstrumentPath();
 106             OutputAnalyzer output = null;
 107 
 108             // Test 1: Native agent, no arguments
 109             output = executor.execute("JVMTI.agent_load " +
 110                                           libpath + " agent.jar");
 111             output.stderrShouldBeEmpty();
 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      * return path to library inside jdk tree
 134      */
 135     public static String jdkLibPath() {
 136         if (Platform.isWindows()) {
 137             return "bin";
 138         }
 139         if (Platform.isOSX()) {
 140             return "lib";
 141         }
 142 
 143         return "lib/" + Platform.getOsArch();
 144     }
 145 
 146     /**
 147      * Build name of shared object according to platform rules
 148      */
 149     public static String sharedObjectName(String name) {
 150         if (Platform.isWindows()) {
 151             return name + ".dll";
 152         }
 153         if (Platform.isOSX()) {
 154             return "lib" + name + ".dylib";
 155         }
 156         return "lib" + name + ".so";
 157     }
 158 
 159     @Test
 160     public void jmx() throws Throwable {
 161         run(new JMXExecutor());
 162     }
 163 
 164     @Test
 165     public void cli() throws Throwable {
 166         run(new PidJcmdExecutor());
 167     }
 168 }