Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/aod/VirtualMachine/VirtualMachine07/VirtualMachine07.java
41160 views
/*1* Copyright (c) 2008, 2020, 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*/2223/*24* @test25*26* @summary converted from VM Testbase nsk/aod/VirtualMachine/VirtualMachine07.27* VM Testbase keywords: [feature_282, jdk]28* VM Testbase readme:29* Description :30* Test checks work of Attach API (com.sun.tools.attach).31* Test is based on the nsk.share.aod framework.32* This test checks methods VirtualMachine.loadAgentLibrary(String agent) and33* VirtualMachine.loadAgentLibrary(String agent, String options).34* Test checks following cases:35* - it is possible to pass options to agent using loadAgentLibrary(String agent, String options)36* - it is possible to specify null options (in this case zero-length is passed to the Agent_OnAttach)37* - if Agent_OnAttach returns error code loadAgentLibrary throws AgentInitializationException and38* AgentInitializationException.returnValue() returns this error code39*40* @library /vmTestbase /test/hotspot/jtreg/vmTestbase41* /test/lib42* @build nsk.share.aod.TargetApplicationWaitingAgents43* @run main/othervm/native44* -XX:+UsePerfData45* nsk.aod.VirtualMachine.VirtualMachine07.VirtualMachine0746* -jdk ${test.jdk}47* -javaOpts="-XX:+UsePerfData ${test.vm.opts} ${test.java.opts}"48* -target nsk.share.aod.TargetApplicationWaitingAgents49* -na VirtualMachine07agent00,VirtualMachine07agent01,VirtualMachine07agent02,VirtualMachine07agent0350* -testedMethod loadAgentLibrary51*/5253package nsk.aod.VirtualMachine.VirtualMachine07;5455import com.sun.tools.attach.AgentInitializationException;56import com.sun.tools.attach.VirtualMachine;57import nsk.share.TestBug;58import nsk.share.aod.AODRunnerArgParser;59import nsk.share.aod.AODTestRunner;60import nsk.share.aod.AgentInformation;61import nsk.share.test.TestUtils;6263import java.util.List;6465/*66* Test is written to test following methods:67* - VirtualMachine.loadAgentLibrary68* - VirtualMachine.loadAgentPath69*70*(method to test is specified via command line parameter 'testedMethod')71*/72public class VirtualMachine07 extends AODTestRunner {73private static class ArgParser extends AODRunnerArgParser {74private static final String TESTED_METHOD_OPT = "testedMethod";7576ArgParser(String[] args) {77super(args);78}7980protected boolean checkOption(String option, String value) {81if (super.checkOption(option, value))82return true;8384if (option.equals(TESTED_METHOD_OPT)) {85if ("loadAgentLibrary".equals(value) || "loadAgentPath".equals(value)) {86return true;87} else {88throw new TestBug("Unexpected value of '" + TESTED_METHOD_OPT + "': " + value);89}90}9192return false;93}9495protected void checkOptions() {96super.checkOptions();9798// if test loadAgentPath parameter arch is needed99if (!testLoadAgentLibrary()) {100if (!options.containsKey("arch")) {101throw new TestBug("Option 'arch' wasn't specified");102}103}104}105106boolean testLoadAgentLibrary() {107return options.getProperty(TESTED_METHOD_OPT).equals("loadAgentLibrary");108}109}110111public VirtualMachine07(String[] args) {112super(args);113}114115/*116* When test method loadAgentPath platform specific agent name should be117* created (lib<agent>.so for unix, lib<agent>.dylib for macosx and118* <agent>.dll for windows)119*/120protected String expandAgentPath(String path) {121int index = path.lastIndexOf('/');122if (index < 0)123throw new TestBug("Unexpected agent library name format");124125String dir = path.substring(0, index);126String libName = path.substring(index + 1);127128if (argParser.getArch().startsWith("windows")) {129return dir + "/" + libName + ".dll";130} else if (argParser.getArch().startsWith("mac")) {131return dir + "/" + "lib" + libName + ".dylib";132} else {133return dir + "/" + "lib" + libName + ".so";134}135}136137protected AODRunnerArgParser createArgParser(String[] args) {138return new ArgParser(args);139}140141protected void loadAgent(VirtualMachine vm, String agent) throws Throwable {142boolean testLoadAgentLibrary = ((ArgParser) argParser).testLoadAgentLibrary();143144if (testLoadAgentLibrary) {145log.display("Test method VirtualMachine.loadAgentLibrary");146} else {147log.display("Test method VirtualMachine.loadAgentPath");148}149150if (testLoadAgentLibrary) {151log.display("Loading '" + agent + "'");152vm.loadAgentLibrary(agent);153} else {154String expandedName = (agent == null ? null : expandAgentPath(agent));155log.display("Loading '" + expandedName + "'");156vm.loadAgentPath(expandedName);157}158}159160protected void loadAgent(VirtualMachine vm, String agent, String options) throws Throwable {161boolean testLoadAgentLibrary = ((ArgParser) argParser).testLoadAgentLibrary();162163if (testLoadAgentLibrary) {164log.display("Test method VirtualMachine.loadAgentLibrary");165} else {166log.display("Test method VirtualMachine.loadAgentPath");167}168169if (testLoadAgentLibrary) {170log.display("Loading '" + agent + "'");171vm.loadAgentLibrary(agent, options);172} else {173String expandedName = (agent == null ? null : expandAgentPath(agent));174log.display("Loading '" + expandedName + "'");175vm.loadAgentPath(expandedName, options);176}177}178179public void doTestActions(String targetVMId) throws Throwable {180// check that all required parameters were passed to the test181List<AgentInformation> agents = argParser.getAgents();182if (agents.size() != 4) {183throw new TestBug("Test requires 4 agents, actually " + agents.size() + " were specified");184}185for (AgentInformation agent : agents) {186if (agent.jarAgent) {187throw new TestBug("Non native agent was specified");188}189}190191VirtualMachine vm = VirtualMachine.attach(targetVMId);192193try {194AgentInformation agent;195196agent = agents.get(0);197loadAgent(vm, agent.pathToAgent);198199agent = agents.get(1);200loadAgent(vm, agent.pathToAgent, null);201202agent = agents.get(2);203loadAgent(vm, agent.pathToAgent, "VirtualMachine_TestOptions");204205agent = agents.get(3);206log.display("Loading '" + agent.pathToAgent + "' (this agent fails to initialize)");207try {208loadAgent(vm, agent.pathToAgent);209210TestUtils.testFailed("Expected AgentInitializationException wasn't thrown");211} catch (AgentInitializationException e) {212log.display("Expected AgentInitializationException was caught");213log.display("AgentInitializationException.returnValue(): " + e.returnValue());214TestUtils.assertEquals(e.returnValue(), 10,215"AgentInitializationException.returnValue() returns unexpected value: " + e.returnValue() + ", expected value is 10");216}217} finally {218vm.detach();219}220}221222public static void main(String[] args) {223new VirtualMachine07(args).runTest();224}225}226227228