Path: blob/master/src/hotspot/share/code/debugInfoRec.cpp
40931 views
/*1* Copyright (c) 1998, 2021, 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 "code/debugInfoRec.hpp"26#include "code/scopeDesc.hpp"27#include "compiler/oopMap.hpp"28#include "prims/jvmtiExport.hpp"29#include "runtime/globals_extension.hpp"3031// Private definition.32// There is one DIR_Chunk for each scope and values array.33// A chunk can potentially be used more than once.34// We keep track of these chunks in order to detect35// repetition and enable sharing.36class DIR_Chunk {37private:38int _offset; // location in the stream of this scope39int _length; // number of bytes in the stream40int _hash; // hash of stream bytes (for quicker reuse)41DebugInformationRecorder* _DIR;4243public:44int offset() { return _offset; }4546void* operator new(size_t ignore, DebugInformationRecorder* dir) throw() {47assert(ignore == sizeof(DIR_Chunk), "");48if (dir->_next_chunk >= dir->_next_chunk_limit) {49const int CHUNK = 100;50dir->_next_chunk = NEW_RESOURCE_ARRAY(DIR_Chunk, CHUNK);51dir->_next_chunk_limit = dir->_next_chunk + CHUNK;52}53return dir->_next_chunk++;54}5556DIR_Chunk(int offset, int length, DebugInformationRecorder* dir) {57_offset = offset;58_length = length;59_DIR = dir;60unsigned int hash = 0;61address p = dir->stream()->buffer() + _offset;62for (int i = 0; i < length; i++) {63if (i == 6) break;64hash *= 127;65hash += p[i];66}67_hash = hash;68}6970DIR_Chunk* find_match(GrowableArray<DIR_Chunk*>* arr,71int start_index,72DebugInformationRecorder* dir) {73int end_index = arr->length();74int hash = this->_hash, length = this->_length;75address buf = dir->stream()->buffer();76for (int i = end_index; --i >= start_index; ) {77DIR_Chunk* that = arr->at(i);78if (hash == that->_hash &&79length == that->_length &&800 == memcmp(buf + this->_offset, buf + that->_offset, length)) {81return that;82}83}84return NULL;85}8687static int compare(DIR_Chunk* const & a, DIR_Chunk* const & b) {88if (b->_hash > a->_hash) {89return 1;90}91if (b->_hash < a->_hash) {92return -1;93}94if (b->_length > a->_length) {95return 1;96}97if (b->_length < a->_length) {98return -1;99}100address buf = a->_DIR->stream()->buffer();101return memcmp(buf + b->_offset, buf + a->_offset, a->_length);102}103};104105static inline bool compute_recording_non_safepoints() {106if (JvmtiExport::should_post_compiled_method_load()107&& FLAG_IS_DEFAULT(DebugNonSafepoints)) {108// The default value of this flag is taken to be true,109// if JVMTI is looking at nmethod codes.110// We anticipate that JVMTI may wish to participate in profiling.111return true;112}113114// If the flag is set manually, use it, whether true or false.115// Otherwise, if JVMTI is not in the picture, use the default setting.116// (This is true in debug, just for the exercise, false in product mode.)117return DebugNonSafepoints;118}119120DebugInformationRecorder::DebugInformationRecorder(OopRecorder* oop_recorder)121: _recording_non_safepoints(compute_recording_non_safepoints())122{123_pcs_size = 100;124_pcs = NEW_RESOURCE_ARRAY(PcDesc, _pcs_size);125_pcs_length = 0;126127_prev_safepoint_pc = PcDesc::lower_offset_limit;128129_stream = new DebugInfoWriteStream(this, 10 * K);130// make sure that there is no stream_decode_offset that is zero131_stream->write_byte((jbyte)0xFF);132133// make sure that we can distinguish the value "serialized_null" from offsets134assert(_stream->position() > serialized_null, "sanity");135136_oop_recorder = oop_recorder;137138_all_chunks = new GrowableArray<DIR_Chunk*>(300);139_next_chunk = _next_chunk_limit = NULL;140141add_new_pc_offset(PcDesc::lower_offset_limit); // sentinel record142143debug_only(_recording_state = rs_null);144}145146147void DebugInformationRecorder::add_oopmap(int pc_offset, OopMap* map) {148// !!!!! Preserve old style handling of oopmaps for now149_oopmaps->add_gc_map(pc_offset, map);150}151152void DebugInformationRecorder::add_safepoint(int pc_offset, OopMap* map) {153assert(!_oop_recorder->is_complete(), "not frozen yet");154// Store the new safepoint155156// Add the oop map157add_oopmap(pc_offset, map);158159add_new_pc_offset(pc_offset);160161assert(_recording_state == rs_null, "nesting of recording calls");162debug_only(_recording_state = rs_safepoint);163}164165void DebugInformationRecorder::add_non_safepoint(int pc_offset) {166assert(!_oop_recorder->is_complete(), "not frozen yet");167assert(_recording_non_safepoints, "must be recording non-safepoints");168169add_new_pc_offset(pc_offset);170171assert(_recording_state == rs_null, "nesting of recording calls");172debug_only(_recording_state = rs_non_safepoint);173}174175void DebugInformationRecorder::add_new_pc_offset(int pc_offset) {176assert(_pcs_length == 0 || last_pc()->pc_offset() < pc_offset,177"must specify a new, larger pc offset");178179// add the pcdesc180if (_pcs_length == _pcs_size) {181// Expand182int new_pcs_size = _pcs_size * 2;183PcDesc* new_pcs = NEW_RESOURCE_ARRAY(PcDesc, new_pcs_size);184for (int index = 0; index < _pcs_length; index++) {185new_pcs[index] = _pcs[index];186}187_pcs_size = new_pcs_size;188_pcs = new_pcs;189}190assert(_pcs_size > _pcs_length, "There must be room for after expanding");191192_pcs[_pcs_length++] = PcDesc(pc_offset, DebugInformationRecorder::serialized_null,193DebugInformationRecorder::serialized_null);194}195196197int DebugInformationRecorder::serialize_monitor_values(GrowableArray<MonitorValue*>* monitors) {198if (monitors == NULL || monitors->is_empty()) return DebugInformationRecorder::serialized_null;199assert(_recording_state == rs_safepoint, "must be recording a safepoint");200int result = stream()->position();201stream()->write_int(monitors->length());202for (int index = 0; index < monitors->length(); index++) {203monitors->at(index)->write_on(stream());204}205assert(result != serialized_null, "sanity");206207// (See comment below on DebugInformationRecorder::describe_scope.)208int shared_result = find_sharable_decode_offset(result);209if (shared_result != serialized_null) {210stream()->set_position(result);211result = shared_result;212}213214return result;215}216217218int DebugInformationRecorder::serialize_scope_values(GrowableArray<ScopeValue*>* values) {219if (values == NULL || values->is_empty()) return DebugInformationRecorder::serialized_null;220assert(_recording_state == rs_safepoint, "must be recording a safepoint");221int result = stream()->position();222assert(result != serialized_null, "sanity");223stream()->write_int(values->length());224for (int index = 0; index < values->length(); index++) {225values->at(index)->write_on(stream());226}227228// (See comment below on DebugInformationRecorder::describe_scope.)229int shared_result = find_sharable_decode_offset(result);230if (shared_result != serialized_null) {231stream()->set_position(result);232result = shared_result;233}234235return result;236}237238239#ifndef PRODUCT240// These variables are put into one block to reduce relocations241// and make it simpler to print from the debugger.242static243struct dir_stats_struct {244int chunks_queried;245int chunks_shared;246int chunks_reshared;247int chunks_elided;248249void print() {250tty->print_cr("Debug Data Chunks: %d, shared %d+%d, non-SP's elided %d",251chunks_queried,252chunks_shared, chunks_reshared,253chunks_elided);254}255} dir_stats;256#endif //PRODUCT257258259int DebugInformationRecorder::find_sharable_decode_offset(int stream_offset) {260NOT_PRODUCT(++dir_stats.chunks_queried);261int stream_length = stream()->position() - stream_offset;262assert(stream_offset != serialized_null, "should not be null");263assert(stream_length != 0, "should not be empty");264265DIR_Chunk* ns = new(this) DIR_Chunk(stream_offset, stream_length, this);266267DIR_Chunk* match = _all_chunks->insert_sorted<DIR_Chunk::compare>(ns);268if (match != ns) {269// Found an existing chunk270NOT_PRODUCT(++dir_stats.chunks_shared);271assert(ns+1 == _next_chunk, "");272_next_chunk = ns;273return match->offset();274} else {275// Inserted this chunk, so nothing to do276return serialized_null;277}278}279280281// must call add_safepoint before: it sets PcDesc and this routine uses282// the last PcDesc set283void DebugInformationRecorder::describe_scope(int pc_offset,284const methodHandle& methodH,285ciMethod* method,286int bci,287bool reexecute,288bool rethrow_exception,289bool is_method_handle_invoke,290bool is_optimized_linkToNative,291bool return_oop,292bool has_ea_local_in_scope,293bool arg_escape,294DebugToken* locals,295DebugToken* expressions,296DebugToken* monitors) {297assert(_recording_state != rs_null, "nesting of recording calls");298PcDesc* last_pd = last_pc();299assert(last_pd->pc_offset() == pc_offset, "must be last pc");300int sender_stream_offset = last_pd->scope_decode_offset();301// update the stream offset of current pc desc302int stream_offset = stream()->position();303last_pd->set_scope_decode_offset(stream_offset);304305// Record flags into pcDesc.306last_pd->set_should_reexecute(reexecute);307last_pd->set_rethrow_exception(rethrow_exception);308last_pd->set_is_method_handle_invoke(is_method_handle_invoke);309last_pd->set_is_optimized_linkToNative(is_optimized_linkToNative);310last_pd->set_return_oop(return_oop);311last_pd->set_has_ea_local_in_scope(has_ea_local_in_scope);312last_pd->set_arg_escape(arg_escape);313314// serialize sender stream offest315stream()->write_int(sender_stream_offset);316317// serialize scope318Metadata* method_enc;319if (method != NULL) {320method_enc = method->constant_encoding();321} else if (methodH.not_null()) {322method_enc = methodH();323} else {324method_enc = NULL;325}326int method_enc_index = oop_recorder()->find_index(method_enc);327stream()->write_int(method_enc_index);328stream()->write_bci(bci);329assert(method == NULL ||330(method->is_native() && bci == 0) ||331(!method->is_native() && 0 <= bci && bci < method->code_size()) ||332bci == -1, "illegal bci");333334// serialize the locals/expressions/monitors335stream()->write_int((intptr_t) locals);336stream()->write_int((intptr_t) expressions);337stream()->write_int((intptr_t) monitors);338339// Here's a tricky bit. We just wrote some bytes.340// Wouldn't it be nice to find that we had already341// written those same bytes somewhere else?342// If we get lucky this way, reset the stream343// and reuse the old bytes. By the way, this344// trick not only shares parent scopes, but also345// compresses equivalent non-safepoint PcDescs.346int shared_stream_offset = find_sharable_decode_offset(stream_offset);347if (shared_stream_offset != serialized_null) {348stream()->set_position(stream_offset);349last_pd->set_scope_decode_offset(shared_stream_offset);350}351}352353void DebugInformationRecorder::dump_object_pool(GrowableArray<ScopeValue*>* objects) {354guarantee( _pcs_length > 0, "safepoint must exist before describing scopes");355PcDesc* last_pd = &_pcs[_pcs_length-1];356if (objects != NULL) {357for (int i = objects->length() - 1; i >= 0; i--) {358objects->at(i)->as_ObjectValue()->set_visited(false);359}360}361int offset = serialize_scope_values(objects);362last_pd->set_obj_decode_offset(offset);363}364365void DebugInformationRecorder::end_scopes(int pc_offset, bool is_safepoint) {366assert(_recording_state == (is_safepoint? rs_safepoint: rs_non_safepoint),367"nesting of recording calls");368debug_only(_recording_state = rs_null);369370// Try to compress away an equivalent non-safepoint predecessor.371// (This only works because we have previously recognized redundant372// scope trees and made them use a common scope_decode_offset.)373if (_pcs_length >= 2 && recording_non_safepoints()) {374PcDesc* last = last_pc();375PcDesc* prev = prev_pc();376// If prev is (a) not a safepoint and (b) has the same377// stream pointer, then it can be coalesced into the last.378// This is valid because non-safepoints are only sought379// with pc_desc_near, which (when it misses prev) will380// search forward until it finds last.381// In addition, it does not matter if the last PcDesc382// is for a safepoint or not.383if (_prev_safepoint_pc < prev->pc_offset() && prev->is_same_info(last)) {384assert(prev == last-1, "sane");385prev->set_pc_offset(pc_offset);386_pcs_length -= 1;387NOT_PRODUCT(++dir_stats.chunks_elided);388}389}390391// We have just recorded this safepoint.392// Remember it in case the previous paragraph needs to know.393if (is_safepoint) {394_prev_safepoint_pc = pc_offset;395}396}397398#ifdef ASSERT399bool DebugInformationRecorder::recorders_frozen() {400return _oop_recorder->is_complete();401}402403void DebugInformationRecorder::mark_recorders_frozen() {404_oop_recorder->freeze();405}406#endif // PRODUCT407408DebugToken* DebugInformationRecorder::create_scope_values(GrowableArray<ScopeValue*>* values) {409assert(!recorders_frozen(), "not frozen yet");410return (DebugToken*) (intptr_t) serialize_scope_values(values);411}412413414DebugToken* DebugInformationRecorder::create_monitor_values(GrowableArray<MonitorValue*>* monitors) {415assert(!recorders_frozen(), "not frozen yet");416return (DebugToken*) (intptr_t) serialize_monitor_values(monitors);417}418419420int DebugInformationRecorder::data_size() {421debug_only(mark_recorders_frozen()); // mark it "frozen" for asserts422return _stream->position();423}424425426int DebugInformationRecorder::pcs_size() {427debug_only(mark_recorders_frozen()); // mark it "frozen" for asserts428if (last_pc()->pc_offset() != PcDesc::upper_offset_limit)429add_new_pc_offset(PcDesc::upper_offset_limit);430return _pcs_length * sizeof(PcDesc);431}432433434void DebugInformationRecorder::copy_to(nmethod* nm) {435nm->copy_scopes_data(stream()->buffer(), stream()->position());436nm->copy_scopes_pcs(_pcs, _pcs_length);437}438439440void DebugInformationRecorder::verify(const nmethod* code) {441Unimplemented();442}443444#ifndef PRODUCT445void DebugInformationRecorder::print_statistics() {446dir_stats.print();447}448#endif //PRODUCT449450451