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 import java.nio.*; 34 import java.nio.charset.*; 35 36 /* 37 * Support class used by JVMTI and VM attach mechanism. 38 */ 39 public class VMSupport { 40 41 private static Properties agentProps = null; 42 /** 43 * Returns the agent properties. 44 */ 45 public static synchronized Properties getAgentProperties() { 46 if (agentProps == null) { 47 agentProps = new Properties(); 48 initAgentProperties(agentProps); 49 } 50 return agentProps; 51 } 52 private static native Properties initAgentProperties(Properties props); 53 54 private static String convertToBytesStr(String str, boolean escapeSpace) throws Exception { 55 if (escapeSpace) { 56 str = str.replace(" ", "\\ "); 57 } 58 59 var charBuf = CharBuffer.wrap(str); 60 var byteBuf = ByteBuffer.allocate(charBuf.length() * 5); // 1 unicode char might have 4 digits + 2 ('\u') 61 var encoder = StandardCharsets.ISO_8859_1 62 .newEncoder() 63 .onUnmappableCharacter(CodingErrorAction.REPORT); 64 CoderResult result; 65 do { 66 result = encoder.encode(charBuf, byteBuf, false); 67 if (result.isUnmappable()) { 68 byteBuf.put(String.format("\\u%04X", (int)charBuf.get()).getBytes()); 69 } else if (result.isError()) { 70 result.throwException(); 71 } 72 } while (result.isError()); 73 74 return new String(byteBuf.array()); 75 } 76 77 /** 78 * Write the given properties list to a byte array and return it. Properties with 79 * a key or value that is not a String is filtered out. The stream written to the byte 80 * array is ISO 8859-1 encoded. 81 */ 82 private static byte[] serializePropertiesToByteArray(Properties p) throws IOException { 83 var joiner = new StringJoiner("\n"); 84 joiner.add((new Date()).toString()); 85 for (var entry : p.entrySet()) { 86 var key = convertToBytes(entry.getKey().toString(), true); 87 var value = convertToBytes(entry.getValue().toString(), false); 88 joiner.add(key + "=" + value); 89 } 90 return joiner.toString().getBytes(); 91 } 92 93 public static byte[] serializePropertiesToByteArray() throws IOException { 94 return serializePropertiesToByteArray(System.getProperties()); 95 } 96 97 public static byte[] serializeAgentPropertiesToByteArray() throws IOException { 98 return serializePropertiesToByteArray(getAgentProperties()); 99 } 100 101 /* 102 * Returns true if the given JAR file has the Class-Path attribute in the 103 * main section of the JAR manifest. Throws RuntimeException if the given 104 * path is not a JAR file or some other error occurs. 105 */ 106 public static boolean isClassPathAttributePresent(String path) { 107 try { 108 Manifest man = (new JarFile(path)).getManifest(); 109 if (man != null) { 110 if (man.getMainAttributes().getValue(Attributes.Name.CLASS_PATH) != null) { 111 return true; 112 } 113 } 114 return false; 115 } catch (IOException ioe) { 116 throw new RuntimeException(ioe.getMessage()); 117 } 118 } 119 120 /* 121 * Return the temporary directory that the VM uses for the attach 122 * and perf data files. 123 * 124 * It is important that this directory is well-known and the 125 * same for all VM instances. It cannot be affected by configuration 126 * variables such as java.io.tmpdir. 127 */ 128 public static native String getVMTemporaryDirectory(); 129 }