1 /*
   2  * Copyright (c) 2005, 2020, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package jdk.internal.vm;
  26 
  27 import java.io.IOException;
  28 import java.util.Properties;
  29 import java.util.jar.JarFile;
  30 import java.util.jar.Manifest;
  31 import java.util.jar.Attributes;
  32 import java.util.*;
  33 
  34 /*
  35  * Support class used by JVMTI and VM attach mechanism.
  36  */
  37 public class VMSupport {
  38 
  39     private static Properties agentProps = null;
  40     /**
  41      * Returns the agent properties.
  42      */
  43     public static synchronized Properties getAgentProperties() {
  44         if (agentProps == null) {
  45             agentProps = new Properties();
  46             initAgentProperties(agentProps);
  47         }
  48         return agentProps;
  49     }
  50     private static native Properties initAgentProperties(Properties props);
  51 
  52     private static String convertToBytesStr(String str, boolean escapeSpace) throws Exception {
  53         str = str.replace("\n", "\\n")
  54                  .replace("\r", "\\r")
  55                  .replace("\t", "\\t")
  56                  .replace("\f", "\\f");
  57         if (escapeSpace) {
  58             str = str.replace(" ", "\\ ");
  59         }
  60 
  61         String result = "";
  62         for (char ch : str.toCharArray()) {
  63             result += ((ch < 0x20) || (ch > 0x7e)) ? String.format("\\u%04X", (int)ch)
  64                                                    : ch;
  65         }
  66 
  67         return result;
  68     }
  69 
  70     /**
  71      * Write the given properties list to a byte array and return it. Properties with
  72      * a key or value that is not a String is filtered out.
  73      */
  74     private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {
  75         var joiner = new StringJoiner("\n");
  76         joiner.add((new Date()).toString());
  77         for (var entry : p.entrySet()) {
  78             var key = convertToBytes(entry.getKey().toString(), true);
  79             var value = convertToBytes(entry.getValue().toString(), false);
  80             joiner.add(key + "=" + value);
  81         }
  82         return joiner.toString().getBytes();
  83     }
  84 
  85     public static byte[] serializePropertiesToByteArray() throws IOException {
  86         return serializePropertiesToByteArray(System.getProperties());
  87     }
  88 
  89     public static byte[] serializeAgentPropertiesToByteArray() throws IOException {
  90         return serializePropertiesToByteArray(getAgentProperties());
  91     }
  92 
  93     /*
  94      * Returns true if the given JAR file has the Class-Path attribute in the
  95      * main section of the JAR manifest. Throws RuntimeException if the given
  96      * path is not a JAR file or some other error occurs.
  97      */
  98     public static boolean isClassPathAttributePresent(String path) {
  99         try {
 100             Manifest man = (new JarFile(path)).getManifest();
 101             if (man != null) {
 102                 if (man.getMainAttributes().getValue(Attributes.Name.CLASS_PATH) != null) {
 103                     return true;
 104                 }
 105             }
 106             return false;
 107         } catch (IOException ioe) {
 108             throw new RuntimeException(ioe.getMessage());
 109         }
 110     }
 111 
 112     /*
 113      * Return the temporary directory that the VM uses for the attach
 114      * and perf data files.
 115      *
 116      * It is important that this directory is well-known and the
 117      * same for all VM instances. It cannot be affected by configuration
 118      * variables such as java.io.tmpdir.
 119      */
 120     public static native String getVMTemporaryDirectory();
 121 }