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