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  * @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");
  84 
  85         manifest.getMainAttributes().put(
  86              new Attributes.Name("Agent-Class"), agentClass);
  87 
  88         JarOutputStream target = null;
  89 
  90         try {
  91             target = new
  92               JarOutputStream(new FileOutputStream(jarName), manifest);
  93             JarEntry entry = new JarEntry(agentClass + ".class");
  94             target.putNextEntry(entry);
  95             target.closeEntry();
  96         } finally {
  97             target.close();
  98         }
  99     }
 100 
 101     public void run(CommandExecutor executor)  {
 102         try{
 103 
 104             createJarFileForAgent();
 105 
 106             String libpath = getLibInstrumentPath();
 107             OutputAnalyzer output = null;
 108 
 109             // Test 1: Native agent, no arguments
 110             output = executor.execute("JVMTI.agent_load " +
 111                                           libpath + " agent.jar");
 112             output.stderrShouldBeEmpty();
 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 }