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
25 #include "classfile/classLoaderData.hpp"
26 #include "classfile/classLoaderDataGraph.hpp"
27 #include "classfile/classPrinter.hpp"
28 #include "memory/iterator.hpp"
29 #include "memory/resourceArea.hpp"
30 #include "oops/instanceKlass.hpp"
31 #include "oops/klass.inline.hpp"
32 #include "oops/method.hpp"
33 #include "oops/symbol.hpp"
34 #include "utilities/growableArray.hpp"
35 #include "utilities/ostream.hpp"
36
37 class ClassPrinter::KlassPrintClosure : public LockedClassesDo {
38 const char* _class_name_pattern;
39 const char* _method_name_pattern;
40 const char* _method_signature_pattern;
41 bool _always_print_class_name;
42 int _flags;
43 outputStream* _st;
44 int _num;
45 bool _has_printed_methods;
46 GrowableArray<InstanceKlass*> _klasses;
47
48 public:
49 KlassPrintClosure(const char* class_name_pattern,
50 const char* method_name_pattern,
51 const char* method_signature_pattern,
52 bool always_print_class_name,
53 int flags, outputStream* st)
54 : _always_print_class_name(always_print_class_name),
55 _flags(flags), _st(st), _num(0), _has_printed_methods(false)
56 {
57 _flags |= (PRINT_PROFILE);
58 if (has_mode(_flags, PRINT_METHOD_HANDLE)) {
59 _flags |= (PRINT_METHOD_NAME | PRINT_BYTECODE);
60 }
61 if (has_mode(_flags, PRINT_DYNAMIC)) {
62 _flags |= (PRINT_METHOD_NAME | PRINT_BYTECODE);
63 }
64 if (has_mode(_flags, PRINT_BYTECODE_ADDR)) {
65 _flags |= (PRINT_METHOD_NAME | PRINT_BYTECODE);
66 }
67 if (has_mode(_flags, PRINT_BYTECODE)) {
68 _flags |= (PRINT_METHOD_NAME);
69 }
70
71 if (has_mode(_flags, PRINT_CLASS_DETAILS)) {
72 _always_print_class_name = true;
73 }
74
75 _class_name_pattern = copy_pattern(class_name_pattern);
76 _method_name_pattern = copy_pattern(method_name_pattern);
77 _method_signature_pattern = copy_pattern(method_signature_pattern);
78 }
79
80 static const char* copy_pattern(const char* pattern) {
81 if (pattern == nullptr) {
82 return nullptr;
83 }
84 char* copy = ResourceArea::strdup(pattern);
85 for (char* p = copy; *p; p++) {
86 if (*p == '.') {
87 *p = '/';
88 }
89 }
90 return copy;
91 }
92
93 virtual void do_klass(Klass* k) {
94 if (!k->is_instance_klass()) {
95 return;
96 }
97 InstanceKlass* ik = InstanceKlass::cast(k);
98 if (ik->is_loaded() && ik->name()->is_star_match(_class_name_pattern)) {
99 _klasses.append(ik);
100 }
101 }
102
103 void print() {
104 _klasses.sort(compare_klasses_alphabetically);
105 for (int i = 0; i < _klasses.length(); i++) {
106 print_instance_klass(_klasses.at(i));
107 }
108 }
109
110
111 static bool match(const char* pattern, Symbol* sym) {
112 return (pattern == nullptr || sym->is_star_match(pattern));
113 }
114
115 static int compare_klasses_alphabetically(InstanceKlass** a, InstanceKlass** b) {
116 return compare_symbols_alphabetically((*a)->name(), (*b)->name());
117 }
118
119 static int compare_methods_alphabetically(const void* a, const void* b) {
120 Method* ma = *(Method**)a;
121 Method* mb = *(Method**)b;
122 int n = compare_symbols_alphabetically(ma->name(), mb->name());
123 if (n == 0) {
124 n = compare_symbols_alphabetically(ma->signature(), mb->signature());
125 }
126 return n;
127 }
128
129 static int compare_symbols_alphabetically(Symbol* a, Symbol *b) {
130 if (a == b) {
131 return 0;
132 }
133 if (a != nullptr && b == nullptr) {
134 return 1;
135 }
136 if (a == nullptr && b != nullptr) {
137 return -1;
138 }
139
140 return strcmp(a->as_C_string(), b->as_C_string());
141 }
142
143 void print_klass_name(InstanceKlass* ik) {
144 _st->print("[%3d] " INTPTR_FORMAT " class: %s mirror: " INTPTR_FORMAT " ", _num++,
145 p2i(ik), ik->name()->as_C_string(), p2i(ik->java_mirror()));
146 ik->class_loader_data()->print_value_on(_st);
147 _st->cr();
148 }
149
150 void print_instance_klass(InstanceKlass* ik) {
151 ResourceMark rm;
152 if (_has_printed_methods) {
153 // We have printed some methods in the previous class.
154 // Print a new line to separate the two classes
155 _st->cr();
156 }
157 _has_printed_methods = false;
158 if (_always_print_class_name) {
159 print_klass_name(ik);
160 }
161
162 if (has_mode(_flags, ClassPrinter::PRINT_CLASS_DETAILS)) {
163 _st->print("InstanceKlass: ");
164 ik->print_on(_st);
165 oop mirror = ik->java_mirror();
166 if (mirror != nullptr) {
167 _st->print("\nJava mirror oop for %s: ", ik->name()->as_C_string());
168 mirror->print_on(_st);
169 }
170 }
171
172 if (has_mode(_flags, ClassPrinter::PRINT_METHOD_NAME)) {
173 bool print_codes = has_mode(_flags, ClassPrinter::PRINT_BYTECODE);
174 int len = ik->methods()->length();
175 int num_methods_printed = 0;
176
177 Method** sorted_methods = NEW_RESOURCE_ARRAY(Method*, len);
178 for (int index = 0; index < len; index++) {
179 sorted_methods[index] = ik->methods()->at(index);
180 }
181
182 qsort(sorted_methods, len, sizeof(Method*), compare_methods_alphabetically);
183
184 for (int index = 0; index < len; index++) {
185 Method* m = sorted_methods[index];
186 if (match(_method_name_pattern, m->name()) &&
187 match(_method_signature_pattern, m->signature())) {
188 if (print_codes && num_methods_printed++ > 0) {
189 _st->cr();
190 }
191
192 if (_has_printed_methods == false) {
193 if (!_always_print_class_name) {
194 print_klass_name(ik);
195 }
196 _has_printed_methods = true;
197 }
198 print_method(m);
199 }
200 }
201 }
202 }
203
204 void print_method(Method* m) {
205 _st->print_cr(INTPTR_FORMAT " %smethod %s : %s", p2i(m),
206 m->is_static() ? "static " : "",
207 m->name()->as_C_string(), m->signature()->as_C_string());
208
209 if (has_mode(_flags, ClassPrinter::PRINT_METHOD_DETAILS)) {
210 m->print_on(_st);
211 }
212
213 if (has_mode(_flags, ClassPrinter::PRINT_BYTECODE)) {
214 m->print_codes_on(_st, _flags);
215 }
216 }
217 };
218
219 void ClassPrinter::print_flags_help(outputStream* os) {
220 os->print_cr("flags (bitmask):");
221 os->print_cr(" 0x%02x - print names of methods", PRINT_METHOD_NAME);
222 os->print_cr(" 0x%02x - print bytecodes", PRINT_BYTECODE);
223 os->print_cr(" 0x%02x - print the address of bytecodes", PRINT_BYTECODE_ADDR);
224 os->print_cr(" 0x%02x - print info for invokedynamic", PRINT_DYNAMIC);
225 os->print_cr(" 0x%02x - print info for invokehandle", PRINT_METHOD_HANDLE);
226 os->print_cr(" 0x%02x - print details of the C++ and Java objects that represent classes", PRINT_CLASS_DETAILS);
227 os->print_cr(" 0x%02x - print details of the C++ objects that represent methods", PRINT_METHOD_DETAILS);
228 os->cr();
229 }
230
231 void ClassPrinter::print_classes(const char* class_name_pattern, int flags, outputStream* os) {
232 ResourceMark rm;
233 KlassPrintClosure closure(class_name_pattern, nullptr, nullptr, true, flags, os);
234 ClassLoaderDataGraph::classes_do(&closure);
235 closure.print();
236 }
237
238 void ClassPrinter::print_methods(const char* class_name_pattern,
239 const char* method_pattern, int flags, outputStream* os) {
240 ResourceMark rm;
241 const char* method_name_pattern;
242 const char* method_signature_pattern;
243
244 const char* colon = strchr(method_pattern, ':');
245 if (colon == nullptr) {
246 method_name_pattern = method_pattern;
247 method_signature_pattern = nullptr;
248 } else {
249 ptrdiff_t name_pat_len = colon - method_pattern;
250 assert(name_pat_len >= 0, "sanity");
251 char* buf = NEW_RESOURCE_ARRAY(char, name_pat_len + 1);
252 strncpy(buf, method_pattern, name_pat_len);
253 buf[name_pat_len] = 0;
254
255 method_name_pattern = buf;
256 method_signature_pattern = colon + 1;
257 }
258
259 KlassPrintClosure closure(class_name_pattern, method_name_pattern, method_signature_pattern,
260 false, flags | PRINT_METHOD_NAME, os);
261 ClassLoaderDataGraph::classes_do(&closure);
262 closure.print();
263 }