Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/aod/AgentsAttacher.java
41161 views
1
/*
2
* Copyright (c) 2008, 2018, 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
package nsk.share.aod;
24
25
import nsk.share.*;
26
import java.io.IOException;
27
import java.util.*;
28
import com.sun.tools.attach.*;
29
30
/*
31
* This class loads java and native agents in the running VM using Attach API
32
* (API from package com.sun.tools.attach).
33
*/
34
public class AgentsAttacher {
35
36
protected String targetVMId;
37
38
protected List<AgentInformation> agents;
39
40
protected Log log;
41
42
public AgentsAttacher(String targetVMId, List<AgentInformation> agents, Log log) {
43
this.targetVMId = targetVMId;
44
this.agents = agents;
45
this.log = log;
46
}
47
48
public void attachAgents() {
49
VirtualMachine vm = null;
50
51
try {
52
log.display("Trying to get VirtualMachine object");
53
vm = VirtualMachine.attach(targetVMId);
54
} catch (AttachNotSupportedException e) {
55
log.complain("Unexpected AttachNotSupportedException during VirtualMachine.attach: " + e);
56
e.printStackTrace(log.getOutStream());
57
} catch (IOException e) {
58
log.complain("Unexpected IOException during VirtualMachine.attach: " + e);
59
e.printStackTrace(log.getOutStream());
60
}
61
62
if (vm == null) {
63
failed("Unable to create VirtualMachine object");
64
}
65
66
log.display("VirtualMachine was created: " + vm);
67
68
try {
69
for (AgentInformation agentInfo : agents) {
70
tryToLoadAgent(vm, agentInfo.pathToAgent, agentInfo.agentOptions, agentInfo.jarAgent);
71
}
72
} finally {
73
try {
74
log.display("Detaching from the VM '" + vm + "'");
75
vm.detach();
76
} catch (IOException e) {
77
failed("Unexpected IOException during detaching: " + e, e);
78
}
79
}
80
}
81
82
protected void tryToLoadAgent(VirtualMachine vm, String agent, String agentOptions, boolean jarAgent) {
83
boolean agentLoaded = false;
84
85
Throwable failureCause = null;
86
87
try {
88
if (jarAgent) {
89
log.display("Trying to load jar agent: '" + agent + "' (agent options: '" + agentOptions + "')");
90
vm.loadAgent(agent, agentOptions);
91
} else {
92
log.display("Trying to load native agent: '" + agent + "' (agent options: '" + agentOptions + "')");
93
vm.loadAgentLibrary(agent, agentOptions);
94
}
95
log.display("Agent was loaded");
96
agentLoaded = true;
97
} catch (AgentLoadException e) {
98
failureCause = e;
99
log.complain("Unexpected AgentLoadException during agent loading: " + e);
100
if (jarAgent) {
101
log.complain("(probably the agent does not exist, or cannot be started in the manner specified in "
102
+ "the java.lang.instrument specification)");
103
} else {
104
log.complain("(probably agent library does not exist, or cannot be loaded for another reason)");
105
}
106
e.printStackTrace(log.getOutStream());
107
} catch (AgentInitializationException e) {
108
failureCause = e;
109
log.complain("Unexpected AgentInitializationException during agent loading: " + e);
110
if (jarAgent) {
111
log.complain("(agentmain have thrown an exception)");
112
} else {
113
log.complain("Agent_OnAttach function returned an error: " + e.returnValue());
114
}
115
e.printStackTrace(log.getOutStream());
116
} catch (IOException e) {
117
failureCause = e;
118
log.complain("Unexpected IOException during agent loading: " + e);
119
e.printStackTrace(log.getOutStream());
120
}
121
122
if (!agentLoaded) {
123
if (failureCause != null)
124
failed("Couldn't attach agent to the target application", failureCause);
125
else
126
failed("Couldn't attach agent to the target application");
127
}
128
}
129
130
private void failed(String errorMessage) {
131
throw new Failure(errorMessage);
132
}
133
134
private void failed(String errorMessage, Throwable t) {
135
throw new Failure(errorMessage, t);
136
}
137
}
138
139