1 /*
  2  * Copyright (c) 2022, 2026, 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/arguments.hpp"
 27 #include "runtime/interfaceSupport.inline.hpp"
 28 #include "runtime/javaThread.hpp"
 29 #include "utilities/ostream.hpp"
 30 #include "unittest.hpp"
 31 
 32 using testing::ContainsRegex;
 33 using testing::HasSubstr;
 34 
 35 TEST_VM(ClassPrinter, print_classes) {
 36   JavaThread* THREAD = JavaThread::current();
 37   ThreadInVMfromNative invm(THREAD);
 38   ResourceMark rm;
 39 
 40   stringStream s1;
 41   ClassPrinter::print_classes("java/lang/Object", 0x03, &s1);
 42   const char* o1 = s1.freeze();
 43 
 44   ASSERT_THAT(o1, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 45   ASSERT_THAT(o1, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait";
 46   ASSERT_THAT(o1, HasSubstr("method finalize : ()V\n   0 return")) << "must find java/lang/Object::finalize and disasm";
 47 
 48   // "." should also work as separator in class name
 49   stringStream s2;
 50   ClassPrinter::print_classes("java.lang.Object", 0x03, &s2);
 51   const char* o2 = s2.freeze();
 52   ASSERT_THAT(o2, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 53 
 54   // 0x20 is PRINT_CLASS_DETAILS
 55   stringStream s3;
 56   ClassPrinter::print_classes("java.lang.Integer", 0x20, &s3);
 57   const char* o3 = s3.freeze();
 58   ASSERT_THAT(o3, HasSubstr("class: java/lang/Integer mirror:")) << "must find java/lang/Integer";
 59   Klass::KlassKind kind = Arguments::is_valhalla_enabled()
 60                               ? Klass::InlineKlassKind
 61                               : Klass::InstanceKlassKind;
 62   stringStream headerExpected;
 63   headerExpected.print("InstanceKlass (kind=%d): java.lang.Integer {0x", kind);
 64   ASSERT_THAT(o3, HasSubstr(headerExpected.freeze())) << "must print InstanceKlass";
 65   ASSERT_THAT(o3, HasSubstr("Java mirror oop for java/lang/Integer:")) << "must print mirror oop";
 66 #if GTEST_USES_POSIX_RE
 67   // Complex regex not available on Windows
 68   const char* re = Arguments::is_valhalla_enabled()
 69                        ? "public static final value 'MIN_VALUE' [(]fields 0x00000008[)] 'I'.* -2147483648 [(]0x80000000[)]"
 70                        : "public static final 'MIN_VALUE' [(]fields 0x00000008[)] 'I'.* -2147483648 [(]0x80000000[)]";
 71 
 72   ASSERT_THAT(o3, ContainsRegex(re)) << "must print static fields";
 73 #endif
 74 }
 75 
 76 TEST_VM(ClassPrinter, print_methods) {
 77   JavaThread* THREAD = JavaThread::current();
 78   ThreadInVMfromNative invm(THREAD);
 79   ResourceMark rm;
 80 
 81   stringStream s1;
 82   ClassPrinter::print_methods("*ang/Object*", "wait", 0x1, &s1);
 83   const char* o1 = s1.freeze();
 84   ASSERT_THAT(o1, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 85   ASSERT_THAT(o1, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait(long)";
 86   ASSERT_THAT(o1, HasSubstr("method wait : ()V")) << "must find java/lang/Object::wait()";
 87   ASSERT_THAT(o1, Not(HasSubstr("method finalize : ()V"))) << "must not find java/lang/Object::finalize";
 88 
 89   stringStream s2;
 90   ClassPrinter::print_methods("j*ang/Object*", "wait:(*J*)V", 0x1, &s2);
 91   const char* o2 = s2.freeze();
 92   ASSERT_THAT(o2, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 93   ASSERT_THAT(o2, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait(long)";
 94   ASSERT_THAT(o2, HasSubstr("method wait : (JI)V")) << "must find java/lang/Object::wait(long,int)";
 95   ASSERT_THAT(o2, Not(HasSubstr("method wait : ()V"))) << "must not find java/lang/Object::wait()";
 96 
 97   // 0x02 is PRINT_BYTECODE
 98   // 0x04 is PRINT_BYTECODE_ADDRESS
 99   // 0x40 is PRINT_METHOD_DETAILS
100   stringStream s3;
101   ClassPrinter::print_methods("java.lang.Object", "wait:()V", 0x46, &s3);
102   const char* o3 = s3.freeze();
103   ASSERT_THAT(o3, HasSubstr("method wait : ()V")) << "must find java/lang/Object::wait()";
104 
105 #ifndef PRODUCT
106   // PRINT_METHOD_DETAILS -- available only in debug builds
107   ASSERT_THAT(o3, HasSubstr("{method}")) << "must print Method metadata";
108 #if GTEST_USES_POSIX_RE
109   // Complex regex not available on Windows
110   ASSERT_THAT(o3, ContainsRegex("method holder:.*'java/lang/Object'")) << "must print Method metadata details";
111   ASSERT_THAT(o3, ContainsRegex("name: *'wait'")) << "must print Method metadata details";
112 #endif
113 #endif
114 
115 #if GTEST_USES_POSIX_RE
116   // Bytecodes: we should have at least one 'return' bytecide for Object.wait()
117   // The print out should look like this:
118   // 0x000000004adf73ad    5 return
119   ASSERT_THAT(o3, ContainsRegex("0x[0-9a-f]+ +[0-9]+ +return")) << "must print return bytecode";
120 #endif
121 
122 }