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