Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/jfr/leakprofiler/checkpoint/objectSampleDescription.cpp
38922 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/javaClasses.hpp"26#include "classfile/symbolTable.hpp"27#include "classfile/systemDictionary.hpp"28#include "jfr/leakprofiler/checkpoint/objectSampleDescription.hpp"29#include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"30#include "oops/oop.inline.hpp"31#include "runtime/thread.hpp"32#include "utilities/ostream.hpp"3334static Symbol* symbol_size = NULL;3536ObjectDescriptionBuilder::ObjectDescriptionBuilder() {37reset();38}3940void ObjectDescriptionBuilder::write_int(jint value) {41char buf[20];42jio_snprintf(buf, sizeof(buf), "%d", value);43write_text(buf);44}4546void ObjectDescriptionBuilder::write_text(const char* text) {47if (_index == sizeof(_buffer) - 2) {48return;49}50while (*text != '\0' && _index < sizeof(_buffer) - 2) {51_buffer[_index] = *text;52_index++;53text++;54}55assert(_index < sizeof(_buffer) - 1, "index should not exceed buffer size");56// add ellipsis if we reached end57if (_index == sizeof(_buffer) - 2) {58_buffer[_index-3] = '.';59_buffer[_index-2] = '.';60_buffer[_index-1] = '.';61}62// terminate string63_buffer[_index] = '\0';64}6566void ObjectDescriptionBuilder::reset() {67_index = 0;68_buffer[0] = '\0';69}7071void ObjectDescriptionBuilder::print_description(outputStream* out) {72out->print("%s", (const char*)_buffer);73}7475const char* ObjectDescriptionBuilder::description() {76if (_buffer[0] == '\0') {77return NULL;78}79const size_t len = strlen(_buffer);80char* copy = NEW_RESOURCE_ARRAY(char, len + 1);81assert(copy != NULL, "invariant");82strncpy(copy, _buffer, len + 1);83return copy;84}8586ObjectSampleDescription::ObjectSampleDescription(oop object) :87_object(object) {88}8990void ObjectSampleDescription::ensure_initialized() {91if (symbol_size == NULL) {92symbol_size = SymbolTable::new_permanent_symbol("size", Thread::current());93}94}9596void ObjectSampleDescription::print_description(outputStream* out) {97write_object_to_buffer();98_description.print_description(out);99}100101const char* ObjectSampleDescription::description() {102write_object_to_buffer();103return _description.description();104}105106void ObjectSampleDescription::write_text(const char* text) {107_description.write_text(text);108}109110void ObjectSampleDescription::write_int(jint value) {111_description.write_int(value);112}113114void ObjectSampleDescription::write_object_to_buffer() {115ensure_initialized();116_description.reset();117write_object_details();118}119120void ObjectSampleDescription::write_object_details() {121Klass* klass = _object->klass();122Symbol* class_name = klass->name();123jint size;124125if (_object->is_a(SystemDictionary::Class_klass())) {126write_class_name();127return;128}129130if (_object->is_a(SystemDictionary::Thread_klass())) {131write_thread_name();132return;133}134135if (_object->is_a(SystemDictionary::ThreadGroup_klass())) {136write_thread_group_name();137return;138}139140if (read_int_size(&size)) {141write_size(size);142return;143}144}145146void ObjectSampleDescription::write_class_name() {147assert(_object->is_a(SystemDictionary::Class_klass()), "invariant");148Klass* const k = java_lang_Class::as_Klass(_object);149if (k == NULL) {150// might represent a primitive151const Klass* const ak = java_lang_Class::array_klass(_object);152// If ak is NULL, this is most likely a mirror associated with a153// jvmti redefine/retransform scratch klass. We can't get any additional154// information from it.155if (ak != NULL) {156write_text(type2name(java_lang_Class::primitive_type(_object)));157}158return;159}160161if (k->oop_is_instance()) {162const InstanceKlass* ik = InstanceKlass::cast(k);163if (ik->is_anonymous()) {164return;165}166assert(!ik->is_anonymous(), "invariant");167const Symbol* name = ik->name();168if (name != NULL) {169write_text("Class Name: ");170write_text(name->as_klass_external_name());171}172}173}174175void ObjectSampleDescription::write_thread_group_name() {176assert(_object->is_a(SystemDictionary::ThreadGroup_klass()), "invariant");177typeArrayOop tg_name = java_lang_ThreadGroup::name(_object);178if (tg_name != NULL) {179write_text("Thread Group: ");180write_text(UNICODE::as_utf8((jchar*) tg_name->base(T_CHAR), tg_name->length()));181}182}183184void ObjectSampleDescription::write_thread_name() {185assert(_object->is_a(SystemDictionary::Thread_klass()), "invariant");186oop name = java_lang_Thread::name(_object);187if (name != NULL) {188char* p = java_lang_String::as_utf8_string(name);189if (p != NULL) {190write_text("Thread Name: ");191write_text(p);192}193}194}195196void ObjectSampleDescription::write_size(jint size) {197if (size >= 0) {198write_text("Size: ");199write_int(size);200}201}202203bool ObjectSampleDescription::read_int_size(jint* result_size) {204fieldDescriptor fd;205Klass* klass = _object->klass();206if (klass->oop_is_instance()) {207InstanceKlass* ik = InstanceKlass::cast(klass);208if (ik->find_field(symbol_size, vmSymbols::int_signature(), false, &fd) != NULL) {209jint size = _object->int_field(fd.offset());210*result_size = size;211return true;212}213}214return false;215}216217218