1/*
2 * Copyright (c) 2017, 2019, 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 "precompiled.hpp"
26#include "classfile/javaClasses.hpp"
27#include "classfile/symbolTable.hpp"
28#include "classfile/systemDictionary.hpp"
29#include "jfr/leakprofiler/checkpoint/objectSampleDescription.hpp"
30#include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
31#include "oops/oop.inline.hpp"
32#include "runtime/fieldDescriptor.inline.hpp"
33#include "runtime/thread.hpp"
34#include "utilities/ostream.hpp"
35
36static Symbol* symbol_size = NULL;
37
38ObjectDescriptionBuilder::ObjectDescriptionBuilder() {
39 reset();
40}
41
42void ObjectDescriptionBuilder::write_int(jint value) {
43 char buf[20];
44 jio_snprintf(buf, sizeof(buf), "%d", value);
45 write_text(buf);
46}
47
48void ObjectDescriptionBuilder::write_text(const char* text) {
49 if (_index == sizeof(_buffer) - 2) {
50 return;
51 }
52 while (*text != '\0' && _index < sizeof(_buffer) - 2) {
53 _buffer[_index] = *text;
54 _index++;
55 text++;
56 }
57 assert(_index < sizeof(_buffer) - 1, "index should not exceed buffer size");
58 // add ellipsis if we reached end
59 if (_index == sizeof(_buffer) - 2) {
60 _buffer[_index-3] = '.';
61 _buffer[_index-2] = '.';
62 _buffer[_index-1] = '.';
63 }
64 // terminate string
65 _buffer[_index] = '\0';
66}
67
68void ObjectDescriptionBuilder::reset() {
69 _index = 0;
70 _buffer[0] = '\0';
71}
72
73void ObjectDescriptionBuilder::print_description(outputStream* out) {
74 out->print("%s", (const char*)_buffer);
75}
76
77const char* ObjectDescriptionBuilder::description() {
78 if (_buffer[0] == '\0') {
79 return NULL;
80 }
81 const size_t len = strlen(_buffer);
82 char* copy = NEW_RESOURCE_ARRAY(char, len + 1);
83 assert(copy != NULL, "invariant");
84 strncpy(copy, _buffer, len + 1);
85 return copy;
86}
87
88ObjectSampleDescription::ObjectSampleDescription(oop object) :
89 _object(object) {
90}
91
92void ObjectSampleDescription::ensure_initialized() {
93 if (symbol_size == NULL) {
94 symbol_size = SymbolTable::new_permanent_symbol("size");
95 }
96}
97
98void ObjectSampleDescription::print_description(outputStream* out) {
99 write_object_to_buffer();
100 _description.print_description(out);
101}
102
103const char* ObjectSampleDescription::description() {
104 write_object_to_buffer();
105 return _description.description();
106}
107
108void ObjectSampleDescription::write_text(const char* text) {
109 _description.write_text(text);
110}
111
112void ObjectSampleDescription::write_int(jint value) {
113 _description.write_int(value);
114}
115
116void ObjectSampleDescription::write_object_to_buffer() {
117 ensure_initialized();
118 _description.reset();
119 write_object_details();
120}
121
122void ObjectSampleDescription::write_object_details() {
123 Klass* klass = _object->klass();
124 Symbol* class_name = klass->name();
125 jint size;
126
127 if (_object->is_a(SystemDictionary::Class_klass())) {
128 write_class_name();
129 return;
130 }
131
132 if (_object->is_a(SystemDictionary::Thread_klass())) {
133 write_thread_name();
134 return;
135 }
136
137 if (_object->is_a(SystemDictionary::ThreadGroup_klass())) {
138 write_thread_group_name();
139 return;
140 }
141
142 if (read_int_size(&size)) {
143 write_size(size);
144 return;
145 }
146}
147
148void ObjectSampleDescription::write_class_name() {
149 assert(_object->is_a(SystemDictionary::Class_klass()), "invariant");
150 const Klass* const k = java_lang_Class::as_Klass(_object);
151 if (k == NULL) {
152 // might represent a primitive
153 const Klass* const ak = java_lang_Class::array_klass_acquire(_object);
154 // If ak is NULL, this is most likely a mirror associated with a
155 // jvmti redefine/retransform scratch klass. We can't get any additional
156 // information from it.
157 if (ak != NULL) {
158 write_text(type2name(java_lang_Class::primitive_type(_object)));
159 }
160 return;
161 }
162
163 if (k->is_instance_klass()) {
164 const InstanceKlass* ik = InstanceKlass::cast(k);
165 if (ik->is_unsafe_anonymous()) {
166 return;
167 }
168 assert(!ik->is_unsafe_anonymous(), "invariant");
169 const Symbol* name = ik->name();
170 if (name != NULL) {
171 write_text("Class Name: ");
172 write_text(name->as_klass_external_name());
173 }
174 }
175}
176
177void ObjectSampleDescription::write_thread_group_name() {
178 assert(_object->is_a(SystemDictionary::ThreadGroup_klass()), "invariant");
179 const char* tg_name = java_lang_ThreadGroup::name(_object);
180 if (tg_name != NULL) {
181 write_text("Thread Group: ");
182 write_text(tg_name);
183 }
184}
185
186void ObjectSampleDescription::write_thread_name() {
187 assert(_object->is_a(SystemDictionary::Thread_klass()), "invariant");
188 oop name = java_lang_Thread::name(_object);
189 if (name != NULL) {
190 char* p = java_lang_String::as_utf8_string(name);
191 if (p != NULL) {
192 write_text("Thread Name: ");
193 write_text(p);
194 }
195 }
196}
197
198void ObjectSampleDescription::write_size(jint size) {
199 if (size >= 0) {
200 write_text("Size: ");
201 write_int(size);
202 }
203}
204
205bool ObjectSampleDescription::read_int_size(jint* result_size) {
206 fieldDescriptor fd;
207 Klass* klass = _object->klass();
208 if (klass->is_instance_klass()) {
209 InstanceKlass* ik = InstanceKlass::cast(klass);
210 if (ik->find_field(symbol_size, vmSymbols::int_signature(), false, &fd) != NULL) {
211 jint size = _object->int_field(fd.offset());
212 *result_size = size;
213 return true;
214 }
215 }
216 return false;
217}
218