Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/code/debugInfoRec.cpp
32285 views
/*1* Copyright (c) 1998, 2013, 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 "prims/jvmtiExport.hpp"2829// Private definition.30// There is one DIR_Chunk for each scope and values array.31// A chunk can potentially be used more than once.32// We keep track of these chunks in order to detect33// repetition and enable sharing.34class DIR_Chunk {35friend class DebugInformationRecorder;36int _offset; // location in the stream of this scope37int _length; // number of bytes in the stream38int _hash; // hash of stream bytes (for quicker reuse)3940void* operator new(size_t ignore, DebugInformationRecorder* dir) throw() {41assert(ignore == sizeof(DIR_Chunk), "");42if (dir->_next_chunk >= dir->_next_chunk_limit) {43const int CHUNK = 100;44dir->_next_chunk = NEW_RESOURCE_ARRAY(DIR_Chunk, CHUNK);45dir->_next_chunk_limit = dir->_next_chunk + CHUNK;46}47return dir->_next_chunk++;48}4950DIR_Chunk(int offset, int length, DebugInformationRecorder* dir) {51_offset = offset;52_length = length;53unsigned int hash = 0;54address p = dir->stream()->buffer() + _offset;55for (int i = 0; i < length; i++) {56if (i == 6) break;57hash *= 127;58hash += p[i];59}60_hash = hash;61}6263DIR_Chunk* find_match(GrowableArray<DIR_Chunk*>* arr,64int start_index,65DebugInformationRecorder* dir) {66int end_index = arr->length();67int hash = this->_hash, length = this->_length;68address buf = dir->stream()->buffer();69for (int i = end_index; --i >= start_index; ) {70DIR_Chunk* that = arr->at(i);71if (hash == that->_hash &&72length == that->_length &&730 == memcmp(buf + this->_offset, buf + that->_offset, length)) {74return that;75}76}77return NULL;78}79};8081static inline bool compute_recording_non_safepoints() {82if (JvmtiExport::should_post_compiled_method_load()83&& FLAG_IS_DEFAULT(DebugNonSafepoints)) {84// The default value of this flag is taken to be true,85// if JVMTI is looking at nmethod codes.86// We anticipate that JVMTI may wish to participate in profiling.87return true;88}8990// If the flag is set manually, use it, whether true or false.91// Otherwise, if JVMTI is not in the picture, use the default setting.92// (This is true in debug, just for the exercise, false in product mode.)93return DebugNonSafepoints;94}9596DebugInformationRecorder::DebugInformationRecorder(OopRecorder* oop_recorder)97: _recording_non_safepoints(compute_recording_non_safepoints())98{99_pcs_size = 100;100_pcs = NEW_RESOURCE_ARRAY(PcDesc, _pcs_size);101_pcs_length = 0;102103_prev_safepoint_pc = PcDesc::lower_offset_limit;104105_stream = new DebugInfoWriteStream(this, 10 * K);106// make sure that there is no stream_decode_offset that is zero107_stream->write_byte((jbyte)0xFF);108109// make sure that we can distinguish the value "serialized_null" from offsets110assert(_stream->position() > serialized_null, "sanity");111112_oop_recorder = oop_recorder;113114_all_chunks = new GrowableArray<DIR_Chunk*>(300);115_shared_chunks = new GrowableArray<DIR_Chunk*>(30);116_next_chunk = _next_chunk_limit = NULL;117118add_new_pc_offset(PcDesc::lower_offset_limit); // sentinel record119120debug_only(_recording_state = rs_null);121}122123124void DebugInformationRecorder::add_oopmap(int pc_offset, OopMap* map) {125// !!!!! Preserve old style handling of oopmaps for now126_oopmaps->add_gc_map(pc_offset, map);127}128129void DebugInformationRecorder::add_safepoint(int pc_offset, OopMap* map) {130assert(!_oop_recorder->is_complete(), "not frozen yet");131// Store the new safepoint132133// Add the oop map134add_oopmap(pc_offset, map);135136add_new_pc_offset(pc_offset);137138assert(_recording_state == rs_null, "nesting of recording calls");139debug_only(_recording_state = rs_safepoint);140}141142void DebugInformationRecorder::add_non_safepoint(int pc_offset) {143assert(!_oop_recorder->is_complete(), "not frozen yet");144assert(_recording_non_safepoints, "must be recording non-safepoints");145146add_new_pc_offset(pc_offset);147148assert(_recording_state == rs_null, "nesting of recording calls");149debug_only(_recording_state = rs_non_safepoint);150}151152void DebugInformationRecorder::add_new_pc_offset(int pc_offset) {153assert(_pcs_length == 0 || last_pc()->pc_offset() < pc_offset,154"must specify a new, larger pc offset");155156// add the pcdesc157if (_pcs_length == _pcs_size) {158// Expand159int new_pcs_size = _pcs_size * 2;160PcDesc* new_pcs = NEW_RESOURCE_ARRAY(PcDesc, new_pcs_size);161for (int index = 0; index < _pcs_length; index++) {162new_pcs[index] = _pcs[index];163}164_pcs_size = new_pcs_size;165_pcs = new_pcs;166}167assert(_pcs_size > _pcs_length, "There must be room for after expanding");168169_pcs[_pcs_length++] = PcDesc(pc_offset, DebugInformationRecorder::serialized_null,170DebugInformationRecorder::serialized_null);171}172173174int DebugInformationRecorder::serialize_monitor_values(GrowableArray<MonitorValue*>* monitors) {175if (monitors == NULL || monitors->is_empty()) return DebugInformationRecorder::serialized_null;176assert(_recording_state == rs_safepoint, "must be recording a safepoint");177int result = stream()->position();178stream()->write_int(monitors->length());179for (int index = 0; index < monitors->length(); index++) {180monitors->at(index)->write_on(stream());181}182assert(result != serialized_null, "sanity");183184// (See comment below on DebugInformationRecorder::describe_scope.)185int shared_result = find_sharable_decode_offset(result);186if (shared_result != serialized_null) {187stream()->set_position(result);188result = shared_result;189}190191return result;192}193194195int DebugInformationRecorder::serialize_scope_values(GrowableArray<ScopeValue*>* values) {196if (values == NULL || values->is_empty()) return DebugInformationRecorder::serialized_null;197assert(_recording_state == rs_safepoint, "must be recording a safepoint");198int result = stream()->position();199assert(result != serialized_null, "sanity");200stream()->write_int(values->length());201for (int index = 0; index < values->length(); index++) {202values->at(index)->write_on(stream());203}204205// (See comment below on DebugInformationRecorder::describe_scope.)206int shared_result = find_sharable_decode_offset(result);207if (shared_result != serialized_null) {208stream()->set_position(result);209result = shared_result;210}211212return result;213}214215216#ifndef PRODUCT217// These variables are put into one block to reduce relocations218// and make it simpler to print from the debugger.219static220struct dir_stats_struct {221int chunks_queried;222int chunks_shared;223int chunks_reshared;224int chunks_elided;225226void print() {227tty->print_cr("Debug Data Chunks: %d, shared %d+%d, non-SP's elided %d",228chunks_queried,229chunks_shared, chunks_reshared,230chunks_elided);231}232} dir_stats;233#endif //PRODUCT234235236int DebugInformationRecorder::find_sharable_decode_offset(int stream_offset) {237// Only pull this trick if non-safepoint recording238// is enabled, for now.239if (!recording_non_safepoints())240return serialized_null;241242NOT_PRODUCT(++dir_stats.chunks_queried);243int stream_length = stream()->position() - stream_offset;244assert(stream_offset != serialized_null, "should not be null");245assert(stream_length != 0, "should not be empty");246247DIR_Chunk* ns = new(this) DIR_Chunk(stream_offset, stream_length, this);248249// Look in previously shared scopes first:250DIR_Chunk* ms = ns->find_match(_shared_chunks, 0, this);251if (ms != NULL) {252NOT_PRODUCT(++dir_stats.chunks_reshared);253assert(ns+1 == _next_chunk, "");254_next_chunk = ns;255return ms->_offset;256}257258// Look in recently encountered scopes next:259const int MAX_RECENT = 50;260int start_index = _all_chunks->length() - MAX_RECENT;261if (start_index < 0) start_index = 0;262ms = ns->find_match(_all_chunks, start_index, this);263if (ms != NULL) {264NOT_PRODUCT(++dir_stats.chunks_shared);265// Searching in _all_chunks is limited to a window,266// but searching in _shared_chunks is unlimited.267_shared_chunks->append(ms);268assert(ns+1 == _next_chunk, "");269_next_chunk = ns;270return ms->_offset;271}272273// No match. Add this guy to the list, in hopes of future shares.274_all_chunks->append(ns);275return serialized_null;276}277278279// must call add_safepoint before: it sets PcDesc and this routine uses280// the last PcDesc set281void DebugInformationRecorder::describe_scope(int pc_offset,282ciMethod* method,283int bci,284bool reexecute,285bool is_method_handle_invoke,286bool return_oop,287DebugToken* locals,288DebugToken* expressions,289DebugToken* monitors) {290assert(_recording_state != rs_null, "nesting of recording calls");291PcDesc* last_pd = last_pc();292assert(last_pd->pc_offset() == pc_offset, "must be last pc");293int sender_stream_offset = last_pd->scope_decode_offset();294// update the stream offset of current pc desc295int stream_offset = stream()->position();296last_pd->set_scope_decode_offset(stream_offset);297298// Record flags into pcDesc.299last_pd->set_should_reexecute(reexecute);300last_pd->set_is_method_handle_invoke(is_method_handle_invoke);301last_pd->set_return_oop(return_oop);302303// serialize sender stream offest304stream()->write_int(sender_stream_offset);305306// serialize scope307Metadata* method_enc = (method == NULL)? NULL: method->constant_encoding();308stream()->write_int(oop_recorder()->find_index(method_enc));309stream()->write_bci(bci);310assert(method == NULL ||311(method->is_native() && bci == 0) ||312(!method->is_native() && 0 <= bci && bci < method->code_size()) ||313(method->is_compiled_lambda_form() && bci == -99) || // this might happen in C1314bci == -1, "illegal bci");315316// serialize the locals/expressions/monitors317stream()->write_int((intptr_t) locals);318stream()->write_int((intptr_t) expressions);319stream()->write_int((intptr_t) monitors);320321// Here's a tricky bit. We just wrote some bytes.322// Wouldn't it be nice to find that we had already323// written those same bytes somewhere else?324// If we get lucky this way, reset the stream325// and reuse the old bytes. By the way, this326// trick not only shares parent scopes, but also327// compresses equivalent non-safepoint PcDescs.328int shared_stream_offset = find_sharable_decode_offset(stream_offset);329if (shared_stream_offset != serialized_null) {330stream()->set_position(stream_offset);331last_pd->set_scope_decode_offset(shared_stream_offset);332}333}334335void DebugInformationRecorder::dump_object_pool(GrowableArray<ScopeValue*>* objects) {336guarantee( _pcs_length > 0, "safepoint must exist before describing scopes");337PcDesc* last_pd = &_pcs[_pcs_length-1];338if (objects != NULL) {339for (int i = objects->length() - 1; i >= 0; i--) {340((ObjectValue*) objects->at(i))->set_visited(false);341}342}343int offset = serialize_scope_values(objects);344last_pd->set_obj_decode_offset(offset);345}346347void DebugInformationRecorder::end_scopes(int pc_offset, bool is_safepoint) {348assert(_recording_state == (is_safepoint? rs_safepoint: rs_non_safepoint),349"nesting of recording calls");350debug_only(_recording_state = rs_null);351352// Try to compress away an equivalent non-safepoint predecessor.353// (This only works because we have previously recognized redundant354// scope trees and made them use a common scope_decode_offset.)355if (_pcs_length >= 2 && recording_non_safepoints()) {356PcDesc* last = last_pc();357PcDesc* prev = prev_pc();358// If prev is (a) not a safepoint and (b) has the same359// stream pointer, then it can be coalesced into the last.360// This is valid because non-safepoints are only sought361// with pc_desc_near, which (when it misses prev) will362// search forward until it finds last.363// In addition, it does not matter if the last PcDesc364// is for a safepoint or not.365if (_prev_safepoint_pc < prev->pc_offset() && prev->is_same_info(last)) {366assert(prev == last-1, "sane");367prev->set_pc_offset(pc_offset);368_pcs_length -= 1;369NOT_PRODUCT(++dir_stats.chunks_elided);370}371}372373// We have just recorded this safepoint.374// Remember it in case the previous paragraph needs to know.375if (is_safepoint) {376_prev_safepoint_pc = pc_offset;377}378}379380#ifdef ASSERT381bool DebugInformationRecorder::recorders_frozen() {382return _oop_recorder->is_complete() || _oop_recorder->is_complete();383}384385void DebugInformationRecorder::mark_recorders_frozen() {386_oop_recorder->freeze();387}388#endif // PRODUCT389390DebugToken* DebugInformationRecorder::create_scope_values(GrowableArray<ScopeValue*>* values) {391assert(!recorders_frozen(), "not frozen yet");392return (DebugToken*) (intptr_t) serialize_scope_values(values);393}394395396DebugToken* DebugInformationRecorder::create_monitor_values(GrowableArray<MonitorValue*>* monitors) {397assert(!recorders_frozen(), "not frozen yet");398return (DebugToken*) (intptr_t) serialize_monitor_values(monitors);399}400401402int DebugInformationRecorder::data_size() {403debug_only(mark_recorders_frozen()); // mark it "frozen" for asserts404return _stream->position();405}406407408int DebugInformationRecorder::pcs_size() {409debug_only(mark_recorders_frozen()); // mark it "frozen" for asserts410if (last_pc()->pc_offset() != PcDesc::upper_offset_limit)411add_new_pc_offset(PcDesc::upper_offset_limit);412return _pcs_length * sizeof(PcDesc);413}414415416void DebugInformationRecorder::copy_to(nmethod* nm) {417nm->copy_scopes_data(stream()->buffer(), stream()->position());418nm->copy_scopes_pcs(_pcs, _pcs_length);419}420421422void DebugInformationRecorder::verify(const nmethod* code) {423Unimplemented();424}425426#ifndef PRODUCT427void DebugInformationRecorder::print_statistics() {428dir_stats.print();429}430#endif //PRODUCT431432433