Path: blob/master/test/hotspot/jtreg/serviceability/dcmd/jvmti/LoadAgentDcmdTest.java
41153 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22import java.io.*;23import java.nio.file.*;24import java.util.Arrays;25import java.util.jar.Attributes;26import java.util.jar.JarEntry;27import java.util.jar.JarOutputStream;28import java.util.jar.Manifest;29import java.util.List;30import java.util.regex.Matcher;31import java.util.regex.Pattern;32import jdk.test.lib.Platform;33import jdk.test.lib.process.OutputAnalyzer;34import jdk.test.lib.dcmd.*;35import org.testng.annotations.Test;3637/*38* Test to attach JVMTI java agent.39*40* @test41* @bug 814738842* @library /test/lib43* @modules java.base/jdk.internal.misc44* java.compiler45* java.instrument46* java.management47* jdk.internal.jvmstat/sun.jvmstat.monitor48* @build SimpleJvmtiAgent49* @run driver jdk.test.lib.helpers.ClassFileInstaller SimpleJvmtiAgent50* @run testng/othervm LoadAgentDcmdTest51*/52public class LoadAgentDcmdTest {5354public String getLibInstrumentPath() throws FileNotFoundException {55String jdkPath = System.getProperty("test.jdk");5657if (jdkPath == null) {58throw new RuntimeException(59"System property 'test.jdk' not set. " +60"This property is normally set by jtreg. " +61"When running test separately, set this property using " +62"'-Dtest.jdk=/path/to/jdk'.");63}6465Path libpath = Paths.get(jdkPath, jdkLibPath(), sharedObjectName("instrument"));6667if (!libpath.toFile().exists()) {68throw new FileNotFoundException(69"Could not find " + libpath.toAbsolutePath());70}7172return libpath.toAbsolutePath().toString();73}747576public void createJarFileForAgent()77throws IOException {7879final String jarName = "agent.jar";80final String agentClass = "SimpleJvmtiAgent";8182Manifest manifest = new Manifest();8384manifest.getMainAttributes().put(85Attributes.Name.MANIFEST_VERSION, "1.0");8687manifest.getMainAttributes().put(88new Attributes.Name("Agent-Class"), agentClass);8990JarOutputStream target = null;9192try {93target = new94JarOutputStream(new FileOutputStream(jarName), manifest);95JarEntry entry = new JarEntry(agentClass + ".class");96target.putNextEntry(entry);97target.closeEntry();98} finally {99target.close();100}101}102103static void checkWarningsOnly(OutputAnalyzer out) {104// stderr should be empty except for VM warnings.105if (!out.getStderr().isEmpty()) {106List<String> lines = Arrays.asList(out.getStderr().split("(\\r\\n|\\n|\\r)"));107Pattern p = Pattern.compile(".*VM warning.*");108for (String line : lines) {109Matcher m = p.matcher(line);110if (!m.matches()) {111throw new RuntimeException("Stderr has output other than VM warnings");112}113}114}115}116117public void run(CommandExecutor executor) {118try{119120createJarFileForAgent();121122String libpath = getLibInstrumentPath();123OutputAnalyzer output = null;124125// Test 1: Native agent, no arguments126output = executor.execute("JVMTI.agent_load " +127libpath + " agent.jar");128checkWarningsOnly(output);129130// Test 2: Native agent, with arguments131output = executor.execute("JVMTI.agent_load " +132libpath + " \"agent.jar=foo=bar\"");133checkWarningsOnly(output);134135// Test 3: Java agent, no arguments136output = executor.execute("JVMTI.agent_load " +137"agent.jar");138checkWarningsOnly(output);139140// Test 4: Java agent, with arguments141output = executor.execute("JVMTI.agent_load " +142"\"agent.jar=foo=bar\"");143checkWarningsOnly(output);144145} catch (Exception e) {146throw new RuntimeException(e);147}148}149/**150* return path to library inside jdk tree151*/152public static String jdkLibPath() {153if (Platform.isWindows()) {154return "bin";155}156return "lib";157}158159/**160* Build name of shared object according to platform rules161*/162public static String sharedObjectName(String name) {163if (Platform.isWindows()) {164return name + ".dll";165}166if (Platform.isOSX()) {167return "lib" + name + ".dylib";168}169return "lib" + name + ".so";170}171172@Test173public void jmx() throws Throwable {174run(new JMXExecutor());175}176177@Test178public void cli() throws Throwable {179run(new PidJcmdExecutor());180}181}182183184