< prev index next >

src/jdk.attach/share/classes/sun/tools/attach/HotSpotVirtualMachine.java

Print this page




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.attach;
  27 
  28 import com.sun.tools.attach.AttachNotSupportedException;
  29 import com.sun.tools.attach.VirtualMachine;
  30 import com.sun.tools.attach.AgentLoadException;
  31 import com.sun.tools.attach.AgentInitializationException;
  32 import com.sun.tools.attach.spi.AttachProvider;
  33 import jdk.internal.misc.VM;
  34 
  35 import java.io.BufferedReader;
  36 import java.io.InputStream;
  37 import java.io.IOException;
  38 import java.io.InputStreamReader;
  39 import java.security.AccessController;
  40 import java.security.PrivilegedAction;
  41 import java.util.Properties;


  42 import java.util.stream.Collectors;
  43 
  44 /*
  45  * The HotSpot implementation of com.sun.tools.attach.VirtualMachine.
  46  */
  47 
  48 public abstract class HotSpotVirtualMachine extends VirtualMachine {
  49 
  50     private static final long CURRENT_PID;
  51     private static final boolean ALLOW_ATTACH_SELF;
  52     static {
  53         PrivilegedAction<ProcessHandle> pa = ProcessHandle::current;
  54         CURRENT_PID = AccessController.doPrivileged(pa).pid();
  55 
  56         String s = VM.getSavedProperty("jdk.attach.allowAttachSelf");
  57         ALLOW_ATTACH_SELF = "".equals(s) || Boolean.parseBoolean(s);
  58     }
  59 
  60     HotSpotVirtualMachine(AttachProvider provider, String id)
  61         throws AttachNotSupportedException, IOException


  69             throw new AttachNotSupportedException("Invalid process identifier");
  70         }
  71 
  72         // The tool should be a different VM to the target. This check will
  73         // eventually be enforced by the target VM.
  74         if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == CURRENT_PID)) {
  75             throw new IOException("Can not attach to current VM");
  76         }
  77     }
  78 
  79     /*
  80      * Load agent library
  81      * If isAbsolute is true then the agent library is the absolute path
  82      * to the library and thus will not be expanded in the target VM.
  83      * if isAbsolute is false then the agent library is just a library
  84      * name and it will be expended in the target VM.
  85      */
  86     private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
  87         throws AgentLoadException, AgentInitializationException, IOException
  88     {
  89         InputStream in = execute("load",
  90                                  agentLibrary,
  91                                  isAbsolute ? "true" : "false",
  92                                  options);
  93         try {
  94             int result = readInt(in);
  95             if (result != 0) {
  96                 throw new AgentInitializationException("Agent_OnAttach failed", result);











  97             }
  98         } finally {
  99             in.close();
 100 
 101         }
 102     }
 103 
 104     /*
 105      * Load agent library - library name will be expanded in target VM
 106      */
 107     public void loadAgentLibrary(String agentLibrary, String options)
 108         throws AgentLoadException, AgentInitializationException, IOException
 109     {
 110         loadAgentLibrary(agentLibrary, false, options);
 111     }
 112 
 113     /*
 114      * Load agent - absolute path of library provided to target VM
 115      */
 116     public void loadAgentPath(String agentLibrary, String options)
 117         throws AgentLoadException, AgentInitializationException, IOException
 118     {
 119         loadAgentLibrary(agentLibrary, true, options);
 120     }




  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.tools.attach;
  27 
  28 import com.sun.tools.attach.AttachNotSupportedException;
  29 import com.sun.tools.attach.VirtualMachine;
  30 import com.sun.tools.attach.AgentLoadException;
  31 import com.sun.tools.attach.AgentInitializationException;
  32 import com.sun.tools.attach.spi.AttachProvider;
  33 import jdk.internal.misc.VM;
  34 
  35 import java.io.BufferedReader;
  36 import java.io.InputStream;
  37 import java.io.IOException;
  38 import java.io.InputStreamReader;
  39 import java.security.AccessController;
  40 import java.security.PrivilegedAction;
  41 import java.util.Properties;
  42 import java.util.regex.Matcher;
  43 import java.util.regex.Pattern;
  44 import java.util.stream.Collectors;
  45 
  46 /*
  47  * The HotSpot implementation of com.sun.tools.attach.VirtualMachine.
  48  */
  49 
  50 public abstract class HotSpotVirtualMachine extends VirtualMachine {
  51 
  52     private static final long CURRENT_PID;
  53     private static final boolean ALLOW_ATTACH_SELF;
  54     static {
  55         PrivilegedAction<ProcessHandle> pa = ProcessHandle::current;
  56         CURRENT_PID = AccessController.doPrivileged(pa).pid();
  57 
  58         String s = VM.getSavedProperty("jdk.attach.allowAttachSelf");
  59         ALLOW_ATTACH_SELF = "".equals(s) || Boolean.parseBoolean(s);
  60     }
  61 
  62     HotSpotVirtualMachine(AttachProvider provider, String id)
  63         throws AttachNotSupportedException, IOException


  71             throw new AttachNotSupportedException("Invalid process identifier");
  72         }
  73 
  74         // The tool should be a different VM to the target. This check will
  75         // eventually be enforced by the target VM.
  76         if (!ALLOW_ATTACH_SELF && (pid == 0 || pid == CURRENT_PID)) {
  77             throw new IOException("Can not attach to current VM");
  78         }
  79     }
  80 
  81     /*
  82      * Load agent library
  83      * If isAbsolute is true then the agent library is the absolute path
  84      * to the library and thus will not be expanded in the target VM.
  85      * if isAbsolute is false then the agent library is just a library
  86      * name and it will be expended in the target VM.
  87      */
  88     private void loadAgentLibrary(String agentLibrary, boolean isAbsolute, String options)
  89         throws AgentLoadException, AgentInitializationException, IOException
  90     {
  91         try (BufferedReader reader = new BufferedReader(
  92                    new InputStreamReader(
  93                             execute("load", agentLibrary,
  94                                      Boolean.toString(isAbsolute), options)))) {
  95             String result = reader.readLine();
  96             if (result == null) {
  97                 throw new AgentLoadException("Target VM did not respond");
  98             } else {
  99                 Matcher matcher = Pattern.compile("^return code: (\\d+)$")
 100                                          .matcher(result);
 101                 if (matcher.matches()) {
 102                     int retCode = Integer.parseInt(matcher.group(1));
 103                     if (retCode != 0) {
 104                         throw new AgentInitializationException(
 105                                               "Agent_OnAttach failed", retCode);
 106                     }
 107                 } else {
 108                     throw new AgentLoadException(result);
 109                 }
 110             }



 111         }
 112     }
 113 
 114     /*
 115      * Load agent library - library name will be expanded in target VM
 116      */
 117     public void loadAgentLibrary(String agentLibrary, String options)
 118         throws AgentLoadException, AgentInitializationException, IOException
 119     {
 120         loadAgentLibrary(agentLibrary, false, options);
 121     }
 122 
 123     /*
 124      * Load agent - absolute path of library provided to target VM
 125      */
 126     public void loadAgentPath(String agentLibrary, String options)
 127         throws AgentLoadException, AgentInitializationException, IOException
 128     {
 129         loadAgentLibrary(agentLibrary, true, options);
 130     }


< prev index next >