Skip to content

Commit

Permalink
HotSpotVirtualMachine 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kakao-bill-23 committed May 13, 2022
1 parent 3408613 commit 5f72471
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

package scouter.agent.proxy;

import scouter.agent.JavaAgent;
import scouter.agent.Logger;
import scouter.agent.util.ModuleUtil;
import scouter.lang.pack.MapPack;
import scouter.lang.pack.Pack;
import scouter.lang.value.ListValue;
Expand All @@ -27,6 +30,7 @@

public class ToolsMainFactory {
private static final String TOOLS_MAIN = "scouter.xtra.tools.ToolsMain";
private static boolean wasGrantedAccessToHSM = false;

public static MapPack heaphisto(Pack param) throws Throwable {

Expand All @@ -50,6 +54,7 @@ public static MapPack heaphisto(Pack param) throws Throwable {
try {
Class c = Class.forName(TOOLS_MAIN, true, loader);
IToolsMain toolsMain = (IToolsMain) c.newInstance();
checkGrantAccess(loader);
List<String> out = toolsMain.heaphisto(0, 100000, "all");
ListValue lv = m.newList("heaphisto");
for (int i = 0; i < out.size(); i++) {
Expand All @@ -76,6 +81,7 @@ public static void heaphisto(PrintWriter out) throws Throwable {
try {
Class c = Class.forName(TOOLS_MAIN, true, loader);
IToolsMain toolsMain = (IToolsMain) c.newInstance();
checkGrantAccess(loader);
toolsMain.heaphisto(out);
} catch (Exception e) {
e.printStackTrace();
Expand Down Expand Up @@ -115,6 +121,7 @@ public static Pack threadDump(Pack param) throws Throwable {
try {
Class c = Class.forName(TOOLS_MAIN, true, loader);
IToolsMain toolsMain = (IToolsMain) c.newInstance();
checkGrantAccess(loader);
List<String> out = (List<String>) toolsMain.threadDump(0, 100000);
ListValue lv = m.newList("threadDump");
for (int i = 0; i < out.size(); i++) {
Expand Down Expand Up @@ -143,9 +150,23 @@ public static synchronized void threadDump(PrintWriter out) throws Throwable {
try {
Class c = Class.forName(TOOLS_MAIN, true, loader);
IToolsMain toolsMain = (IToolsMain) c.newInstance();
checkGrantAccess(loader);
toolsMain.threadDump(out);
} catch (Exception e) {
e.printStackTrace();
}
}

private static void checkGrantAccess(ClassLoader loader) {
if (JavaAgent.isJava9plus() && !wasGrantedAccessToHSM) {
try {
ModuleUtil.grantAccess(JavaAgent.getInstrumentation(), TOOLS_MAIN, loader,
"sun.tools.attach.HotSpotVirtualMachine", loader);
} catch (Throwable th) {
Logger.println("TOOLS-5", th.getMessage(), th);
} finally {
wasGrantedAccessToHSM = true;
}
}
}
}

0 comments on commit 5f72471

Please sign in to comment.