diff --git a/src/hotspot/share/classfile/classPrinter.hpp b/src/hotspot/share/classfile/classPrinter.hpp index e90dd44b579..0fff372c302 100644 --- a/src/hotspot/share/classfile/classPrinter.hpp +++ b/src/hotspot/share/classfile/classPrinter.hpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2022, 2023, 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 @@ -59,10 +59,19 @@ public: static void print_flags_help(outputStream* os); - // flags must be OR'ed from ClassPrinter::Mode for the these two functions + // Parameters for print_classes() and print_methods(): + // + // - The patterns are matched by StringUtils::is_star_match() + // - class_name_pattern matches Klass::external_name(). E.g., "java/lang/Object" or "*ang/Object" + // - method_pattern may optionally include the signature. E.g., "wait", "wait:()V" or "*ai*t:(*)V" + // - flags must be OR'ed from ClassPrinter::Mode + // + // print_classes("java/lang/Object", 0x3, os) -> find j.l.Object and disasm all of its methods + // print_methods("*ang/Object*", "wait", 0xff, os) -> detailed disasm of all "wait" methods in j.l.Object + // print_methods("*ang/Object*", "wait:(*J*)V", 0x1, os) -> list all "wait" methods in j.l.Object that have a long parameter static void print_classes(const char* class_name_pattern, int flags, outputStream* os); static void print_methods(const char* class_name_pattern, - const char* method_name_pattern, int flags, outputStream* os); + const char* method_pattern, int flags, outputStream* os); }; #endif // SHARE_CLASSFILE_CLASSPRINTER_HPP diff --git a/src/hotspot/share/interpreter/bytecodeTracer.cpp b/src/hotspot/share/interpreter/bytecodeTracer.cpp index cd51849bb1f..333c1e9488e 100644 --- a/src/hotspot/share/interpreter/bytecodeTracer.cpp +++ b/src/hotspot/share/interpreter/bytecodeTracer.cpp @@ -303,10 +303,16 @@ bool BytecodePrinter::check_obj_index(int i, int& cp_index, outputStream* st) { bool BytecodePrinter::check_invokedynamic_index(int i, int& cp_index, outputStream* st) { - assert(ConstantPool::is_invokedynamic_index(i), "not secondary index?"); - i = ConstantPool::decode_invokedynamic_index(i) + ConstantPool::CPCACHE_INDEX_TAG; - - return check_cp_cache_index(i, cp_index, st); + ConstantPool* constants = _current_method->constants(); + if (constants->cache() == nullptr) { + cp_index = i; // TODO: This is wrong on little-endian. See JDK-8309811. + } else { + assert(ConstantPool::is_invokedynamic_index(i), "must be"); + int indy_index = ConstantPool::decode_invokedynamic_index(i); + ResolvedIndyEntry* indy_entry = constants->resolved_indy_entry_at(indy_index); + cp_index = indy_entry->constant_pool_index(); + } + return true; } void BytecodePrinter::print_constant(int i, outputStream* st) { diff --git a/src/hotspot/share/prims/whitebox.cpp b/src/hotspot/share/prims/whitebox.cpp index e03856cbab6..accd9ceaf64 100644 --- a/src/hotspot/share/prims/whitebox.cpp +++ b/src/hotspot/share/prims/whitebox.cpp @@ -31,6 +31,7 @@ #include "cds/metaspaceShared.hpp" #include "classfile/classLoaderDataGraph.hpp" #include "classfile/classLoaderStats.hpp" +#include "classfile/classPrinter.hpp" #include "classfile/javaClasses.inline.hpp" #include "classfile/modules.hpp" #include "classfile/protectionDomainCache.hpp" @@ -1897,6 +1898,35 @@ WB_ENTRY(jint, WB_getIndyCPIndex(JNIEnv* env, jobject wb, jclass klass, jint ind return cp->resolved_indy_entry_at(index)->constant_pool_index(); WB_END +WB_ENTRY(jobject, WB_printClasses(JNIEnv* env, jobject wb, jstring class_name_pattern, jint flags)) + ThreadToNativeFromVM ttnfv(thread); + const char* c = env->GetStringUTFChars(class_name_pattern, nullptr); + ResourceMark rm; + stringStream st; + { + ThreadInVMfromNative ttvfn(thread); // back to VM + ClassPrinter::print_classes(c, flags, &st); + } + jstring result = env->NewStringUTF(st.freeze()); + CHECK_JNI_EXCEPTION_(env, nullptr); + return result; +WB_END + +WB_ENTRY(jobject, WB_printMethods(JNIEnv* env, jobject wb, jstring class_name_pattern, jstring method_pattern, jint flags)) + ThreadToNativeFromVM ttnfv(thread); + const char* c = env->GetStringUTFChars(class_name_pattern, nullptr); + const char* m = env->GetStringUTFChars(method_pattern, nullptr); + ResourceMark rm; + stringStream st; + { + ThreadInVMfromNative ttvfn(thread); // back to VM + ClassPrinter::print_methods(c, m, flags, &st); + } + jstring result = env->NewStringUTF(st.freeze()); + CHECK_JNI_EXCEPTION_(env, nullptr); + return result; +WB_END + WB_ENTRY(void, WB_ClearInlineCaches(JNIEnv* env, jobject wb, jboolean preserve_static_stubs)) VM_ClearICs clear_ics(preserve_static_stubs == JNI_TRUE); VMThread::execute(&clear_ics); @@ -2737,6 +2767,8 @@ static JNINativeMethod methods[] = { CC"(I)I", (void*)&WB_ConstantPoolEncodeIndyIndex}, {CC"getIndyInfoLength0", CC"(Ljava/lang/Class;)I", (void*)&WB_getIndyInfoLength}, {CC"getIndyCPIndex0", CC"(Ljava/lang/Class;I)I", (void*)&WB_getIndyCPIndex}, + {CC"printClasses0", CC"(Ljava/lang/String;I)Ljava/lang/String;", (void*)&WB_printClasses}, + {CC"printMethods0", CC"(Ljava/lang/String;Ljava/lang/String;I)Ljava/lang/String;", (void*)&WB_printMethods}, {CC"getMethodBooleanOption", CC"(Ljava/lang/reflect/Executable;Ljava/lang/String;)Ljava/lang/Boolean;", (void*)&WB_GetMethodBooleaneOption}, diff --git a/src/hotspot/share/utilities/debug.cpp b/src/hotspot/share/utilities/debug.cpp index f0047396512..c918d8d04b3 100644 --- a/src/hotspot/share/utilities/debug.cpp +++ b/src/hotspot/share/utilities/debug.cpp @@ -534,10 +534,7 @@ extern "C" JNIEXPORT void findpc(intptr_t x) { } // For findmethod() and findclass(): -// - The patterns are matched by StringUtils::is_star_match() -// - class_name_pattern matches Klass::external_name(). E.g., "java/lang/Object" or "*ang/Object" -// - method_pattern may optionally the signature. E.g., "wait", "wait:()V" or "*ai*t:(*)V" -// - flags must be OR'ed from ClassPrinter::Mode for findclass/findmethod +// See comments in classPrinter.hpp about the meanings of class_name_pattern, method_pattern and flags. // Examples (in gdb): // call findclass("java/lang/Object", 0x3) -> find j.l.Object and disasm all of its methods // call findmethod("*ang/Object*", "wait", 0xff) -> detailed disasm of all "wait" methods in j.l.Object diff --git a/test/hotspot/jtreg/runtime/interpreter/BytecodeTracerTest.java b/test/hotspot/jtreg/runtime/interpreter/BytecodeTracerTest.java new file mode 100644 index 00000000000..4e76e9aca14 --- /dev/null +++ b/test/hotspot/jtreg/runtime/interpreter/BytecodeTracerTest.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2023, 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 8309808 8309811 + * @summary Test the output of the HotSpot BytecodeTracer and ClassPrinter classes. + * @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:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI BytecodeTracerTest + */ + +import java.io.BufferedWriter; +import java.io.FileWriter; +import java.io.Serializable; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import jdk.test.lib.Asserts; +import jdk.test.whitebox.WhiteBox; + +public class BytecodeTracerTest { + public static class Linked { + public static void doit(String args[]) { + System.out.println("num args = " + args.length); + } + } + + public static class Unlinked implements Serializable { + public String toString() { + return "Unlinked" + this.hashCode(); + } + } + + static String output; + static int testCount = 0; + + static String nextCase(String testName) { + ++ testCount; + return "======================================================================\nTest case " + + testCount + ": " + testName + "\n "; + } + + static void logOutput() throws Exception { + String logFileName = "log-" + testCount + ".txt"; + System.out.println("Output saved in " + logFileName); + BufferedWriter writer = new BufferedWriter(new FileWriter(logFileName)); + writer.write(output); + writer.close(); + } + + static void printClasses(String testName, String classNamePattern, int flags) throws Exception { + System.out.println(nextCase(testName) + "printClasses(\"" + classNamePattern + "\", " + flags + ")"); + output = WhiteBox.getWhiteBox().printClasses(classNamePattern, flags); + logOutput(); + } + + static void printMethods(String testName, String classNamePattern, String methodPattern, int flags) throws Exception { + System.out.println(nextCase(testName) + "printMethods(\"" + classNamePattern + "\", \"" + methodPattern + "\", " + flags + ")"); + output = WhiteBox.getWhiteBox().printMethods(classNamePattern, methodPattern, flags); + logOutput(); + } + + static void mustMatch(String pattern) { + Pattern p = Pattern.compile(pattern, Pattern.MULTILINE); + Matcher m = p.matcher(output); + boolean found = m.find(); + if (!found) { + System.err.println("********* output ********"); + System.err.println(output); + System.err.println("*************************"); + } + Asserts.assertTrue(found, + "Missing pattern: \"" + pattern + "\""); + System.out.println("Found pattern: " + pattern); + System.out.println(" ==>: " + m.group()); + } + + public static void main(String args[]) throws Exception { + Linked.doit(args); // Force "Linked" class to be linked (and rewritten); + + // ====== + printClasses("invokedynamic in linked class", + "BytecodeTracerTest$Linked", 0xff); + mustMatch("invokedynamic bsm=[0-9]+ [0-9]+ "); + mustMatch("BSM: REF_invokeStatic [0-9]+ "); + mustMatch("BSM: REF_invokeStatic [0-9]+