Path: blob/master/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/debugger/DebuggerUtilities.java
41161 views
/*1* Copyright (c) 2001, 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.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324package sun.jvm.hotspot.debugger;2526/** Common routines for data conversion */2728public class DebuggerUtilities {29protected long addressSize;30protected boolean isBigEndian;3132public DebuggerUtilities(long addressSize, boolean isBigEndian) {33this.addressSize = addressSize;34this.isBigEndian = isBigEndian;35}3637public String addressValueToString(long address) {38StringBuilder buf = new StringBuilder();39buf.append("0x");40String val;41// Make negative addresses have the correct size42if (addressSize == 8) {43val = Long.toHexString(address);44} else {45val = Integer.toHexString((int) address);46}47for (int i = 0; i < ((2 * addressSize) - val.length()); i++) {48buf.append('0');49}50buf.append(val);51return buf.toString();52}5354public void checkAlignment(long address, long alignment) {55if (address % alignment != 0) {56throw new UnalignedAddressException("Trying to read at address: " +57addressValueToString(address) +58" with alignment: " + alignment,59address);60}61}6263public long scanAddress(String addrStr) throws NumberFormatException {64String s = addrStr.trim();65if (!s.startsWith("0x")) {66throw new NumberFormatException(addrStr);67}68long l = 0;69for (int i = 2; i < s.length(); ++i) {70int val = charToNibble(s.charAt(i));71l <<= 4;72l |= val;73}74return l;75}7677public int charToNibble(char ascii) throws NumberFormatException {78if (ascii >= '0' && ascii <= '9') {79return ascii - '0';80} else if (ascii >= 'A' && ascii <= 'F') {81return 10 + ascii - 'A';82} else if (ascii >= 'a' && ascii <= 'f') {83return 10 + ascii - 'a';84}85throw new NumberFormatException(Character.toString(ascii));86}8788public boolean dataToJBoolean(byte[] data, long jbooleanSize) {89checkDataContents(data, jbooleanSize);9091return (data[0] != 0);92}9394public byte dataToJByte(byte[] data, long jbyteSize) {95checkDataContents(data, jbyteSize);9697return data[0];98}99100public char dataToJChar(byte[] data, long jcharSize) {101checkDataContents(data, jcharSize);102103if (!isBigEndian) {104byteSwap(data);105}106107return (char) (((data[0] & 0xFF) << 8) | (data[1] & 0xFF));108}109110public double dataToJDouble(byte[] data, long jdoubleSize) {111long longBits = dataToJLong(data, jdoubleSize);112113return Double.longBitsToDouble(longBits);114}115116public float dataToJFloat(byte[] data, long jfloatSize) {117int intBits = dataToJInt(data, jfloatSize);118119return Float.intBitsToFloat(intBits);120}121122public int dataToJInt(byte[] data, long jintSize) {123checkDataContents(data, jintSize);124125if (!isBigEndian) {126byteSwap(data);127}128129return (((data[0] & 0xFF) << 24) | ((data[1] & 0xFF) << 16) | ((data[2] & 0xFF) << 8) | (data[3] & 0xFF));130}131132public long dataToJLong(byte[] data, long jlongSize) {133checkDataContents(data, jlongSize);134135if (!isBigEndian) {136byteSwap(data);137}138139return rawDataToJLong(data);140}141142public short dataToJShort(byte[] data, long jshortSize) {143checkDataContents(data, jshortSize);144145if (!isBigEndian) {146byteSwap(data);147}148149return (short) (((data[0] & 0xFF) << 8) | (data[1] & 0xFF));150}151152public long dataToCInteger(byte[] data, boolean isUnsigned) {153checkDataContents(data, data.length);154155if (!isBigEndian) {156byteSwap(data);157}158159// By default we'll be zero-extending.160// Therefore we need to check to see whether isUnsigned is false and161// also the high bit of the data is set.162if ((data.length < 8) &&163(isUnsigned == false) &&164((data[0] & 0x80) != 0)) {165// Must sign-extend and right-align the data166byte[] newData = new byte[8];167for (int i = 0; i < 8; ++i) {168if ((7 - i) < data.length) {169newData[i] = data[i + data.length - 8];170} else {171newData[i] = (byte) 0xFF;172}173}174data = newData;175}176177// Now just do the usual loop178return rawDataToJLong(data);179}180181public long dataToAddressValue(byte[] data) {182checkDataContents(data, addressSize);183184if (!isBigEndian) {185byteSwap(data);186}187188return rawDataToJLong(data);189}190191public byte[] jbooleanToData(boolean value) {192byte[] res = new byte[1];193res[0] = (byte) (value ? 1 : 0);194return res;195}196197public byte[] jbyteToData(byte value) {198byte[] res = new byte[1];199res[0] = value;200return res;201}202203public byte[] jcharToData(char value) {204byte[] res = new byte[2];205res[0] = (byte) ((value >> 8) & 0xFF);206res[1] = (byte) value;207if (!isBigEndian) {208byteSwap(res);209}210return res;211}212213public byte[] jdoubleToData(double value) {214return jlongToData(Double.doubleToLongBits(value));215}216217public byte[] jfloatToData(float value) {218return jintToData(Float.floatToIntBits(value));219}220221public byte[] jintToData(int value) {222byte[] res = new byte[4];223for (int i = 0; i < 4; i++) {224res[3 - i] = (byte) (value & 0xFF);225value >>>= 8;226}227if (!isBigEndian) {228byteSwap(res);229}230return res;231}232233public byte[] jlongToData(long value) {234byte[] res = new byte[8];235for (int i = 0; i < 8; i++) {236res[7 - i] = (byte) (value & 0xFF);237value >>>= 8;238}239if (!isBigEndian) {240byteSwap(res);241}242return res;243}244245public byte[] jshortToData(short value) {246byte[] res = new byte[2];247res[0] = (byte) ((value >> 8) & 0xFF);248res[1] = (byte) value;249if (!isBigEndian) {250byteSwap(res);251}252return res;253}254255public byte[] cIntegerToData(long longNumBytes, long value) {256int numBytes = (int) longNumBytes;257byte[] res = new byte[numBytes];258for (int i = 0; i < numBytes; i++) {259res[numBytes - i - 1] = (byte) (value & 0xFF);260value >>>= 8;261}262if (!isBigEndian) {263byteSwap(res);264}265return res;266}267268//--------------------------------------------------------------------------------269// Internals only below this point270//271272private void checkDataContents(byte[] data, long len) {273if (data.length != (int) len) {274throw new InternalError("Bug in Win32Debugger");275}276}277278private void byteSwap(byte[] data) {279for (int i = 0; i < (data.length / 2); ++i) {280int altIndex = data.length - i - 1;281byte t = data[altIndex];282data[altIndex] = data[i];283data[i] = t;284}285}286287private long rawDataToJLong(byte[] data) {288long addr = 0;289for (int i = 0; i < data.length; ++i) {290addr <<= 8;291addr |= data[i] & 0xFF;292}293294return addr;295}296}297298299