mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-28 07:14:30 +02:00
8325945: Error reporting should limit the number of String characters printed
Reviewed-by: thartmann, stuefe
This commit is contained in:
parent
f5871df25c
commit
10fcad70b3
6 changed files with 166 additions and 3 deletions
|
@ -751,8 +751,16 @@ bool java_lang_String::equals(oop str1, oop str2) {
|
||||||
return value_equals(value1, value2);
|
return value_equals(value1, value2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void java_lang_String::print(oop java_string, outputStream* st) {
|
// Print the given string to the given outputStream, limiting the output to
|
||||||
|
// at most max_length of the string's characters. If the length exceeds the
|
||||||
|
// limit we print an abridged version of the string with the "middle" elided
|
||||||
|
// and replaced by " ... (N characters ommitted) ... ". If max_length is odd
|
||||||
|
// it is treated as max_length-1.
|
||||||
|
void java_lang_String::print(oop java_string, outputStream* st, int max_length) {
|
||||||
assert(java_string->klass() == vmClasses::String_klass(), "must be java_string");
|
assert(java_string->klass() == vmClasses::String_klass(), "must be java_string");
|
||||||
|
// We need at least two characters to print A ... B
|
||||||
|
assert(max_length > 1, "invalid max_length: %d", max_length);
|
||||||
|
|
||||||
typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
|
typeArrayOop value = java_lang_String::value_no_keepalive(java_string);
|
||||||
|
|
||||||
if (value == nullptr) {
|
if (value == nullptr) {
|
||||||
|
@ -765,8 +773,17 @@ void java_lang_String::print(oop java_string, outputStream* st) {
|
||||||
int length = java_lang_String::length(java_string, value);
|
int length = java_lang_String::length(java_string, value);
|
||||||
bool is_latin1 = java_lang_String::is_latin1(java_string);
|
bool is_latin1 = java_lang_String::is_latin1(java_string);
|
||||||
|
|
||||||
|
bool abridge = length > max_length;
|
||||||
|
|
||||||
st->print("\"");
|
st->print("\"");
|
||||||
for (int index = 0; index < length; index++) {
|
for (int index = 0; index < length; index++) {
|
||||||
|
// If we need to abridge and we've printed half the allowed characters
|
||||||
|
// then jump to the tail of the string.
|
||||||
|
if (abridge && index >= max_length / 2) {
|
||||||
|
st->print(" ... (%d characters ommitted) ... ", length - 2 * (max_length / 2));
|
||||||
|
index = length - (max_length / 2);
|
||||||
|
abridge = false; // only do this once
|
||||||
|
}
|
||||||
jchar c = (!is_latin1) ? value->char_at(index) :
|
jchar c = (!is_latin1) ? value->char_at(index) :
|
||||||
((jchar) value->byte_at(index)) & 0xff;
|
((jchar) value->byte_at(index)) & 0xff;
|
||||||
if (c < ' ') {
|
if (c < ' ') {
|
||||||
|
@ -776,6 +793,10 @@ void java_lang_String::print(oop java_string, outputStream* st) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
st->print("\"");
|
st->print("\"");
|
||||||
|
|
||||||
|
if (length > max_length) {
|
||||||
|
st->print(" (abridged) ");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// java_lang_Class
|
// java_lang_Class
|
||||||
|
|
|
@ -194,7 +194,7 @@ class java_lang_String : AllStatic {
|
||||||
static inline bool is_instance(oop obj);
|
static inline bool is_instance(oop obj);
|
||||||
|
|
||||||
// Debugging
|
// Debugging
|
||||||
static void print(oop java_string, outputStream* st);
|
static void print(oop java_string, outputStream* st, int max_length = MaxStringPrintSize);
|
||||||
friend class JavaClasses;
|
friend class JavaClasses;
|
||||||
friend class StringTable;
|
friend class StringTable;
|
||||||
};
|
};
|
||||||
|
|
|
@ -179,6 +179,14 @@ WB_ENTRY(jlong, WB_GetVMLargePageSize(JNIEnv* env, jobject o))
|
||||||
return os::large_page_size();
|
return os::large_page_size();
|
||||||
WB_END
|
WB_END
|
||||||
|
|
||||||
|
WB_ENTRY(jstring, WB_PrintString(JNIEnv* env, jobject wb, jstring str, jint max_length))
|
||||||
|
ResourceMark rm(THREAD);
|
||||||
|
stringStream sb;
|
||||||
|
java_lang_String::print(JNIHandles::resolve(str), &sb, max_length);
|
||||||
|
oop result = java_lang_String::create_oop_from_str(sb.as_string(), THREAD);
|
||||||
|
return (jstring) JNIHandles::make_local(THREAD, result);
|
||||||
|
WB_END
|
||||||
|
|
||||||
class WBIsKlassAliveClosure : public LockedClassesDo {
|
class WBIsKlassAliveClosure : public LockedClassesDo {
|
||||||
Symbol* _name;
|
Symbol* _name;
|
||||||
int _count;
|
int _count;
|
||||||
|
@ -2959,6 +2967,7 @@ static JNINativeMethod methods[] = {
|
||||||
{CC"preTouchMemory", CC"(JJ)V", (void*)&WB_PreTouchMemory},
|
{CC"preTouchMemory", CC"(JJ)V", (void*)&WB_PreTouchMemory},
|
||||||
{CC"cleanMetaspaces", CC"()V", (void*)&WB_CleanMetaspaces},
|
{CC"cleanMetaspaces", CC"()V", (void*)&WB_CleanMetaspaces},
|
||||||
{CC"rss", CC"()J", (void*)&WB_Rss},
|
{CC"rss", CC"()J", (void*)&WB_Rss},
|
||||||
|
{CC"printString", CC"(Ljava/lang/String;I)Ljava/lang/String;", (void*)&WB_PrintString},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1304,6 +1304,12 @@ const int ObjectAlignmentInBytes = 8;
|
||||||
develop(int, MaxElementPrintSize, 256, \
|
develop(int, MaxElementPrintSize, 256, \
|
||||||
"maximum number of elements to print") \
|
"maximum number of elements to print") \
|
||||||
\
|
\
|
||||||
|
develop(int, MaxStringPrintSize, 256, \
|
||||||
|
"maximum number of characters to print for a java.lang.String " \
|
||||||
|
"in the VM. If exceeded, an abridged version of the string is " \
|
||||||
|
"printed with the middle of the string elided.") \
|
||||||
|
range(2, O_BUFLEN) \
|
||||||
|
\
|
||||||
develop(intx, MaxSubklassPrintSize, 4, \
|
develop(intx, MaxSubklassPrintSize, 4, \
|
||||||
"maximum number of subklasses to print when printing klass") \
|
"maximum number of subklasses to print when printing klass") \
|
||||||
\
|
\
|
||||||
|
|
122
test/hotspot/jtreg/runtime/PrintingTests/StringPrinting.java
Normal file
122
test/hotspot/jtreg/runtime/PrintingTests/StringPrinting.java
Normal file
|
@ -0,0 +1,122 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
|
||||||
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||||
|
*
|
||||||
|
* This code is free software; you can redistribute it and/or modify it
|
||||||
|
* under the terms of the GNU General Public License version 2 only, as
|
||||||
|
* published by the Free Software Foundation.
|
||||||
|
*
|
||||||
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||||
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||||
|
* version 2 for more details (a copy is included in the LICENSE file that
|
||||||
|
* accompanied this code).
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License version
|
||||||
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||||
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||||
|
*
|
||||||
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||||
|
* or visit www.oracle.com if you need additional information or have any
|
||||||
|
* questions.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @test
|
||||||
|
* @bug 8325945
|
||||||
|
* @summary Test abridged VM String printing
|
||||||
|
* @library /test/lib
|
||||||
|
* @build jdk.test.whitebox.WhiteBox
|
||||||
|
* @run driver jdk.test.lib.helpers.ClassFileInstaller jdk.test.whitebox.WhiteBox
|
||||||
|
* @run main/othervm -Xbootclasspath/a:. -XX:+WhiteBoxAPI StringPrinting
|
||||||
|
*/
|
||||||
|
|
||||||
|
import jdk.test.whitebox.WhiteBox;
|
||||||
|
|
||||||
|
public class StringPrinting {
|
||||||
|
|
||||||
|
private static final WhiteBox WB = WhiteBox.getWhiteBox();
|
||||||
|
|
||||||
|
static void checkEqual(String s1, String s2) {
|
||||||
|
if (!s1.equals(s2)) {
|
||||||
|
throw new RuntimeException("Different strings: " + s1 + " vs " + s2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void checkEqual(int len1, int len2) {
|
||||||
|
if (len1 != len2) {
|
||||||
|
throw new RuntimeException("Different lengths: " + len1 + " vs " + len2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
// Modified string format is "xxx ... (N characters ommitted) ... xxx" (abridged)
|
||||||
|
final String elipse = " ... ";
|
||||||
|
final String text = " characters ommitted)";
|
||||||
|
final String abridged = "\" (abridged) ";
|
||||||
|
|
||||||
|
// Define a set of maxLengths for ease of inspection in the test outout
|
||||||
|
// Note: maxLength must be >= 2
|
||||||
|
int[] maxLengths = new int[] { 2, 3, 16, 256, 512 };
|
||||||
|
for (int maxLength : maxLengths) {
|
||||||
|
// Test string lengths around maxLength and "much" bigger
|
||||||
|
// than maxLength
|
||||||
|
int[] strLengths = new int[] { maxLength - 1,
|
||||||
|
maxLength,
|
||||||
|
maxLength + 1,
|
||||||
|
2 * maxLength
|
||||||
|
};
|
||||||
|
for (int length : strLengths) {
|
||||||
|
System.out.println("Testing string length " + length + " with maxLength " + maxLength);
|
||||||
|
String s = "x".repeat(length);
|
||||||
|
String r = WB.printString(s, maxLength);
|
||||||
|
if (length <= maxLength) {
|
||||||
|
// Strip off the double-quotes that surround the string
|
||||||
|
if (r.charAt(0) == '\"' && r.charAt(r.length() - 1) == '\"') {
|
||||||
|
r = r.substring(1, r.length() - 1);
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("String was not quoted as expected: " + r);
|
||||||
|
}
|
||||||
|
checkEqual(s, r);
|
||||||
|
} else {
|
||||||
|
// Strip off leading double-quote
|
||||||
|
if (r.charAt(0) == '\"') {
|
||||||
|
r = r.substring(1, r.length());
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("String was not quoted as expected: " + r);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Strip off abridged
|
||||||
|
if (r.endsWith(abridged)) {
|
||||||
|
r = r.substring(0, r.length() - abridged.length());
|
||||||
|
} else {
|
||||||
|
throw new RuntimeException("String was not marked abridged as expected: " + r);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now extract the two "halves"
|
||||||
|
int elipseStart = r.indexOf(elipse);
|
||||||
|
String firstHalf = r.substring(0, elipseStart);
|
||||||
|
int secondElipseStart = r.lastIndexOf(elipse);
|
||||||
|
String secondHalf = r.substring(secondElipseStart + elipse.length(), r.length());
|
||||||
|
|
||||||
|
System.out.println("S1: >" + firstHalf + "<");
|
||||||
|
System.out.println("S2: >" + secondHalf + "<");
|
||||||
|
|
||||||
|
checkEqual(firstHalf.length(), maxLength / 2);
|
||||||
|
checkEqual(secondHalf.length(), maxLength /2);
|
||||||
|
|
||||||
|
// Now check number of characters ommitted
|
||||||
|
String tail = r.substring(r.indexOf("("), r.length());
|
||||||
|
int numberEnd = tail.indexOf(" ");
|
||||||
|
String nChars = tail.substring(1, numberEnd);
|
||||||
|
System.out.println("N: >" + nChars + "<");
|
||||||
|
|
||||||
|
// Now add all the bits back together to get the expected full length
|
||||||
|
int fullLength = maxLength / 2 + elipse.length() + 1 /* for ( */
|
||||||
|
+ nChars.length() + text.length() + elipse.length() + maxLength / 2;
|
||||||
|
checkEqual(r.length(), fullLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -99,8 +99,13 @@ public class WhiteBox {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runtime
|
// Runtime
|
||||||
// Make sure class name is in the correct format
|
|
||||||
|
// Returns the potentially abridged form of `str` as it would be
|
||||||
|
// printed by the VM.
|
||||||
|
public native String printString(String str, int maxLength);
|
||||||
|
|
||||||
public int countAliveClasses(String name) {
|
public int countAliveClasses(String name) {
|
||||||
|
// Make sure class name is in the correct format
|
||||||
return countAliveClasses0(name.replace('.', '/'));
|
return countAliveClasses0(name.replace('.', '/'));
|
||||||
}
|
}
|
||||||
private native int countAliveClasses0(String name);
|
private native int countAliveClasses0(String name);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue