Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/aod/AgentsAttacher.java
41161 views
/*1* Copyright (c) 2008, 2018, 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*/22package nsk.share.aod;2324import nsk.share.*;25import java.io.IOException;26import java.util.*;27import com.sun.tools.attach.*;2829/*30* This class loads java and native agents in the running VM using Attach API31* (API from package com.sun.tools.attach).32*/33public class AgentsAttacher {3435protected String targetVMId;3637protected List<AgentInformation> agents;3839protected Log log;4041public AgentsAttacher(String targetVMId, List<AgentInformation> agents, Log log) {42this.targetVMId = targetVMId;43this.agents = agents;44this.log = log;45}4647public void attachAgents() {48VirtualMachine vm = null;4950try {51log.display("Trying to get VirtualMachine object");52vm = VirtualMachine.attach(targetVMId);53} catch (AttachNotSupportedException e) {54log.complain("Unexpected AttachNotSupportedException during VirtualMachine.attach: " + e);55e.printStackTrace(log.getOutStream());56} catch (IOException e) {57log.complain("Unexpected IOException during VirtualMachine.attach: " + e);58e.printStackTrace(log.getOutStream());59}6061if (vm == null) {62failed("Unable to create VirtualMachine object");63}6465log.display("VirtualMachine was created: " + vm);6667try {68for (AgentInformation agentInfo : agents) {69tryToLoadAgent(vm, agentInfo.pathToAgent, agentInfo.agentOptions, agentInfo.jarAgent);70}71} finally {72try {73log.display("Detaching from the VM '" + vm + "'");74vm.detach();75} catch (IOException e) {76failed("Unexpected IOException during detaching: " + e, e);77}78}79}8081protected void tryToLoadAgent(VirtualMachine vm, String agent, String agentOptions, boolean jarAgent) {82boolean agentLoaded = false;8384Throwable failureCause = null;8586try {87if (jarAgent) {88log.display("Trying to load jar agent: '" + agent + "' (agent options: '" + agentOptions + "')");89vm.loadAgent(agent, agentOptions);90} else {91log.display("Trying to load native agent: '" + agent + "' (agent options: '" + agentOptions + "')");92vm.loadAgentLibrary(agent, agentOptions);93}94log.display("Agent was loaded");95agentLoaded = true;96} catch (AgentLoadException e) {97failureCause = e;98log.complain("Unexpected AgentLoadException during agent loading: " + e);99if (jarAgent) {100log.complain("(probably the agent does not exist, or cannot be started in the manner specified in "101+ "the java.lang.instrument specification)");102} else {103log.complain("(probably agent library does not exist, or cannot be loaded for another reason)");104}105e.printStackTrace(log.getOutStream());106} catch (AgentInitializationException e) {107failureCause = e;108log.complain("Unexpected AgentInitializationException during agent loading: " + e);109if (jarAgent) {110log.complain("(agentmain have thrown an exception)");111} else {112log.complain("Agent_OnAttach function returned an error: " + e.returnValue());113}114e.printStackTrace(log.getOutStream());115} catch (IOException e) {116failureCause = e;117log.complain("Unexpected IOException during agent loading: " + e);118e.printStackTrace(log.getOutStream());119}120121if (!agentLoaded) {122if (failureCause != null)123failed("Couldn't attach agent to the target application", failureCause);124else125failed("Couldn't attach agent to the target application");126}127}128129private void failed(String errorMessage) {130throw new Failure(errorMessage);131}132133private void failed(String errorMessage, Throwable t) {134throw new Failure(errorMessage, t);135}136}137138139