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 jdk.test.lib.*;
  26 import jdk.test.lib.dcmd.*;
  27 import org.testng.annotations.Test;
  28 
  29 /*
  30  * Test to attach JVMTI java agent.
  31  *
  32  * @test
  33  * @bug 8147388
  34  * @library /testlibrary
  35  * @modules java.base/sun.misc
  36  *          java.compiler
  37  *          java.instrument
  38  *          java.management
  39  *          jdk.jvmstat/sun.jvmstat.monitor
  40  * @build ClassFileInstaller jdk.test.lib.* SimpleJvmtiAgent
  41  * @run main ClassFileInstaller SimpleJvmtiAgent
  42  * @run testng LoadAgentDcmdTest
  43  */
  44 public class LoadAgentDcmdTest {
  45 
  46     public String getLibInstrumentPath() throws FileNotFoundException {
  47         String jdkPath = System.getProperty("test.jdk");
  48 
  49         if (jdkPath == null) {
  50             throw new RuntimeException(
  51                     "System property 'test.jdk' not set. " +
  52                     "This property is normally set by jtreg. " +
  53                     "When running test separately, set this property using " +
  54                     "'-Dtest.jdk=/path/to/jdk'.");
  55         }
  56 
  57         String libname = Platform.isWindows() ? "instrument.dll"
  58                                               : "libinstrument.so";
  59         Path libpath = Paths.get(jdkPath, "lib", Platform.getOsArch(), libname);
  60         if (!libpath.toFile().exists()) {
  61             throw new FileNotFoundException(
  62                                   "Could not find " + libpath.toAbsolutePath());
  63         }
  64 
  65         return libpath.toAbsolutePath().toString();
  66     }
  67 
  68     public void run(CommandExecutor executor)  {
  69         try{
  70             PrintWriter pw = new PrintWriter("MANIFEST.MF");
  71             pw.println("Agent-Class: SimpleJvmtiAgent");
  72             pw.close();
  73 
  74             ProcessBuilder pb = new ProcessBuilder();
  75             pb.command(new String[] { JDKToolFinder.getJDKTool("jar"),
  76                                       "cmf",
  77                                       "MANIFEST.MF",
  78                                       "agent.jar",
  79                                       "SimpleJvmtiAgent.class"});
  80             pb.start().waitFor();
  81 
  82             String libpath = getLibInstrumentPath();
  83 
  84             // Test 1: No argument
  85             OutputAnalyzer output = executor.execute(
  86                                   "JVMTI.agent_load " + libpath + " agent.jar");
  87             output.stderrShouldBeEmpty();
  88 
  89             // Test 2: With argument
  90             output = executor.execute(
  91                       "JVMTI.agent_load " + libpath + " \"agent.jar=foo=bar\"");
  92             output.stderrShouldBeEmpty();
  93         } catch (Exception e) {
  94             throw new RuntimeException(e);
  95         }
  96     }
  97 
  98     @Test
  99     public void jmx() throws Throwable {
 100         run(new JMXExecutor());
 101     }
 102 
 103     @Test
 104     public void cli() throws Throwable {
 105         run(new PidJcmdExecutor());
 106     }
 107 }