1616
1717package scouter .agent .proxy ;
1818
19+ import scouter .agent .JavaAgent ;
20+ import scouter .agent .Logger ;
21+ import scouter .agent .util .ModuleUtil ;
1922import scouter .lang .pack .MapPack ;
2023import scouter .lang .pack .Pack ;
2124import scouter .lang .value .ListValue ;
2730
2831public class ToolsMainFactory {
2932 private static final String TOOLS_MAIN = "scouter.xtra.tools.ToolsMain" ;
33+ private static boolean wasGrantedAccessToHSM = false ;
3034
3135 public static MapPack heaphisto (Pack param ) throws Throwable {
3236
@@ -50,6 +54,7 @@ public static MapPack heaphisto(Pack param) throws Throwable {
5054 try {
5155 Class c = Class .forName (TOOLS_MAIN , true , loader );
5256 IToolsMain toolsMain = (IToolsMain ) c .newInstance ();
57+ checkGrantAccess (loader );
5358 List <String > out = toolsMain .heaphisto (0 , 100000 , "all" );
5459 ListValue lv = m .newList ("heaphisto" );
5560 for (int i = 0 ; i < out .size (); i ++) {
@@ -76,6 +81,7 @@ public static void heaphisto(PrintWriter out) throws Throwable {
7681 try {
7782 Class c = Class .forName (TOOLS_MAIN , true , loader );
7883 IToolsMain toolsMain = (IToolsMain ) c .newInstance ();
84+ checkGrantAccess (loader );
7985 toolsMain .heaphisto (out );
8086 } catch (Exception e ) {
8187 e .printStackTrace ();
@@ -115,6 +121,7 @@ public static Pack threadDump(Pack param) throws Throwable {
115121 try {
116122 Class c = Class .forName (TOOLS_MAIN , true , loader );
117123 IToolsMain toolsMain = (IToolsMain ) c .newInstance ();
124+ checkGrantAccess (loader );
118125 List <String > out = (List <String >) toolsMain .threadDump (0 , 100000 );
119126 ListValue lv = m .newList ("threadDump" );
120127 for (int i = 0 ; i < out .size (); i ++) {
@@ -143,9 +150,23 @@ public static synchronized void threadDump(PrintWriter out) throws Throwable {
143150 try {
144151 Class c = Class .forName (TOOLS_MAIN , true , loader );
145152 IToolsMain toolsMain = (IToolsMain ) c .newInstance ();
153+ checkGrantAccess (loader );
146154 toolsMain .threadDump (out );
147155 } catch (Exception e ) {
148156 e .printStackTrace ();
149157 }
150158 }
159+
160+ private static void checkGrantAccess (ClassLoader loader ) {
161+ if (JavaAgent .isJava9plus () && !wasGrantedAccessToHSM ) {
162+ try {
163+ ModuleUtil .grantAccess (JavaAgent .getInstrumentation (), TOOLS_MAIN , loader ,
164+ "sun.tools.attach.HotSpotVirtualMachine" , loader );
165+ } catch (Throwable th ) {
166+ Logger .println ("TOOLS-5" , th .getMessage (), th );
167+ } finally {
168+ wasGrantedAccessToHSM = true ;
169+ }
170+ }
171+ }
151172}
0 commit comments