Path: blob/master/src/java.base/share/classes/jdk/internal/vm/VMSupport.java
41159 views
/*1* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24package jdk.internal.vm;2526import java.io.ByteArrayOutputStream;27import java.io.IOException;28import java.util.Properties;29import java.util.Set;30import java.util.jar.JarFile;31import java.util.jar.Manifest;32import java.util.jar.Attributes;3334/*35* Support class used by JVMTI and VM attach mechanism.36*/37public class VMSupport {3839private static Properties agentProps = null;40/**41* Returns the agent properties.42*/43public static synchronized Properties getAgentProperties() {44if (agentProps == null) {45agentProps = new Properties();46initAgentProperties(agentProps);47}48return agentProps;49}50private static native Properties initAgentProperties(Properties props);5152/**53* Write the given properties list to a byte array and return it. Properties with54* a key or value that is not a String is filtered out. The stream written to the byte55* array is ISO 8859-1 encoded.56*/57private static byte[] serializePropertiesToByteArray(Properties p) throws IOException {58ByteArrayOutputStream out = new ByteArrayOutputStream(4096);5960Properties props = new Properties();6162// stringPropertyNames() returns a snapshot of the property keys63Set<String> keyset = p.stringPropertyNames();64for (String key : keyset) {65String value = p.getProperty(key);66props.put(key, value);67}6869props.store(out, null);70return out.toByteArray();71}7273public static byte[] serializePropertiesToByteArray() throws IOException {74return serializePropertiesToByteArray(System.getProperties());75}7677public static byte[] serializeAgentPropertiesToByteArray() throws IOException {78return serializePropertiesToByteArray(getAgentProperties());79}8081/*82* Return the temporary directory that the VM uses for the attach83* and perf data files.84*85* It is important that this directory is well-known and the86* same for all VM instances. It cannot be affected by configuration87* variables such as java.io.tmpdir.88*/89public static native String getVMTemporaryDirectory();90}919293