1 /* 2 * Copyright (c) 2022, 2025, 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. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include "classfile/classPrinter.hpp" 25 #include "memory/resourceArea.hpp" 26 #include "runtime/interfaceSupport.inline.hpp" 27 #include "runtime/javaThread.hpp" 28 #include "utilities/ostream.hpp" 29 #include "unittest.hpp" 30 31 using testing::ContainsRegex; 32 using testing::HasSubstr; 33 34 TEST_VM(ClassPrinter, print_classes) { 35 JavaThread* THREAD = JavaThread::current(); 36 ThreadInVMfromNative invm(THREAD); 37 ResourceMark rm; 38 39 stringStream s1; 40 ClassPrinter::print_classes("java/lang/Object", 0x03, &s1); 41 const char* o1 = s1.freeze(); 42 43 ASSERT_THAT(o1, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object"; 44 ASSERT_THAT(o1, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait"; 45 ASSERT_THAT(o1, HasSubstr("method finalize : ()V\n 0 return")) << "must find java/lang/Object::finalize and disasm"; 46 47 // "." should also work as separator in class name 48 stringStream s2; 49 ClassPrinter::print_classes("java.lang.Object", 0x03, &s2); 50 const char* o2 = s2.freeze(); 51 ASSERT_THAT(o2, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object"; 52 53 // 0x20 is PRINT_CLASS_DETAILS 54 stringStream s3; 55 ClassPrinter::print_classes("java.lang.Integer", 0x20, &s3); 56 const char* o3 = s3.freeze(); 57 ASSERT_THAT(o3, HasSubstr("class: java/lang/Integer mirror:")) << "must find java/lang/Integer"; 58 ASSERT_THAT(o3, HasSubstr("InstanceKlass: java.lang.Integer {0x")) << "must print InstanceKlass"; 59 ASSERT_THAT(o3, HasSubstr("Java mirror oop for java/lang/Integer:")) << "must print mirror oop"; 60 #if GTEST_USES_POSIX_RE 61 // Complex regex not available on Windows 62 ASSERT_THAT(o3, ContainsRegex("public static final 'MIN_VALUE' 'I'.* -2147483648 [(]0x80000000[)]")) << "must print static fields"; 63 #endif 64 } 65 66 TEST_VM(ClassPrinter, print_methods) { 67 JavaThread* THREAD = JavaThread::current(); 68 ThreadInVMfromNative invm(THREAD); 69 ResourceMark rm; 70 71 stringStream s1; 72 ClassPrinter::print_methods("*ang/Object*", "wait", 0x1, &s1); 73 const char* o1 = s1.freeze(); 74 ASSERT_THAT(o1, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object"; 75 ASSERT_THAT(o1, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait(long)"; 76 ASSERT_THAT(o1, HasSubstr("method wait : ()V")) << "must find java/lang/Object::wait()"; 77 ASSERT_THAT(o1, Not(HasSubstr("method finalize : ()V"))) << "must not find java/lang/Object::finalize"; 78 79 stringStream s2; 80 ClassPrinter::print_methods("j*ang/Object*", "wait:(*J*)V", 0x1, &s2); 81 const char* o2 = s2.freeze(); 82 ASSERT_THAT(o2, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object"; 83 ASSERT_THAT(o2, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait(long)"; 84 ASSERT_THAT(o2, HasSubstr("method wait : (JI)V")) << "must find java/lang/Object::wait(long,int)"; 85 ASSERT_THAT(o2, Not(HasSubstr("method wait : ()V"))) << "must not find java/lang/Object::wait()"; 86 87 // 0x02 is PRINT_BYTECODE 88 // 0x04 is PRINT_BYTECODE_ADDRESS 89 // 0x40 is PRINT_METHOD_DETAILS 90 stringStream s3; 91 ClassPrinter::print_methods("java.lang.Object", "wait:()V", 0x46, &s3); 92 const char* o3 = s3.freeze(); 93 ASSERT_THAT(o3, HasSubstr("method wait : ()V")) << "must find java/lang/Object::wait()"; 94 95 #ifndef PRODUCT 96 // PRINT_METHOD_DETAILS -- available only in debug builds 97 ASSERT_THAT(o3, HasSubstr("{method}")) << "must print Method metadata"; 98 #if GTEST_USES_POSIX_RE 99 // Complex regex not available on Windows 100 ASSERT_THAT(o3, ContainsRegex("method holder:.*'java/lang/Object'")) << "must print Method metadata details"; 101 ASSERT_THAT(o3, ContainsRegex("name: *'wait'")) << "must print Method metadata details"; 102 #endif 103 #endif 104 105 #if GTEST_USES_POSIX_RE 106 // Bytecodes: we should have at least one 'return' bytecide for Object.wait() 107 // The print out should look like this: 108 // 0x000000004adf73ad 5 return 109 ASSERT_THAT(o3, ContainsRegex("0x[0-9a-f]+ +[0-9]+ +return")) << "must print return bytecode"; 110 #endif 111 112 } --- EOF ---