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 0 // FIX: JDK-8374115
 61 #if GTEST_USES_POSIX_RE
 62   // Complex regex not available on Windows
 63   ASSERT_THAT(o3, ContainsRegex("public static final 'MIN_VALUE' 'I'.* -2147483648 [(]0x80000000[)]")) << "must print static fields";
 64 #endif
 65 #endif
 66 }
 67 
 68 TEST_VM(ClassPrinter, print_methods) {
 69   JavaThread* THREAD = JavaThread::current();
 70   ThreadInVMfromNative invm(THREAD);
 71   ResourceMark rm;
 72 
 73   stringStream s1;
 74   ClassPrinter::print_methods("*ang/Object*", "wait", 0x1, &s1);
 75   const char* o1 = s1.freeze();
 76   ASSERT_THAT(o1, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 77   ASSERT_THAT(o1, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait(long)";
 78   ASSERT_THAT(o1, HasSubstr("method wait : ()V")) << "must find java/lang/Object::wait()";
 79   ASSERT_THAT(o1, Not(HasSubstr("method finalize : ()V"))) << "must not find java/lang/Object::finalize";
 80 
 81   stringStream s2;
 82   ClassPrinter::print_methods("j*ang/Object*", "wait:(*J*)V", 0x1, &s2);
 83   const char* o2 = s2.freeze();
 84   ASSERT_THAT(o2, HasSubstr("class: java/lang/Object mirror:")) << "must find java/lang/Object";
 85   ASSERT_THAT(o2, HasSubstr("method wait : (J)V")) << "must find java/lang/Object::wait(long)";
 86   ASSERT_THAT(o2, HasSubstr("method wait : (JI)V")) << "must find java/lang/Object::wait(long,int)";
 87   ASSERT_THAT(o2, Not(HasSubstr("method wait : ()V"))) << "must not find java/lang/Object::wait()";
 88 
 89   // 0x02 is PRINT_BYTECODE
 90   // 0x04 is PRINT_BYTECODE_ADDRESS
 91   // 0x40 is PRINT_METHOD_DETAILS
 92   stringStream s3;
 93   ClassPrinter::print_methods("java.lang.Object", "wait:()V", 0x46, &s3);
 94   const char* o3 = s3.freeze();
 95   ASSERT_THAT(o3, HasSubstr("method wait : ()V")) << "must find java/lang/Object::wait()";
 96 
 97 #ifndef PRODUCT
 98   // PRINT_METHOD_DETAILS -- available only in debug builds
 99   ASSERT_THAT(o3, HasSubstr("{method}")) << "must print Method metadata";
100 #if GTEST_USES_POSIX_RE
101   // Complex regex not available on Windows
102   ASSERT_THAT(o3, ContainsRegex("method holder:.*'java/lang/Object'")) << "must print Method metadata details";
103   ASSERT_THAT(o3, ContainsRegex("name: *'wait'")) << "must print Method metadata details";
104 #endif
105 #endif
106 
107 #if GTEST_USES_POSIX_RE
108   // Bytecodes: we should have at least one 'return' bytecide for Object.wait()
109   // The print out should look like this:
110   // 0x000000004adf73ad    5 return
111   ASSERT_THAT(o3, ContainsRegex("0x[0-9a-f]+ +[0-9]+ +return")) << "must print return bytecode";
112 #endif
113 
114 }