duke@435: /* coleenp@4037: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "code/debugInfoRec.hpp" stefank@2314: #include "code/scopeDesc.hpp" stefank@2314: #include "prims/jvmtiExport.hpp" duke@435: duke@435: // Private definition. duke@435: // There is one DIR_Chunk for each scope and values array. duke@435: // A chunk can potentially be used more than once. duke@435: // We keep track of these chunks in order to detect duke@435: // repetition and enable sharing. duke@435: class DIR_Chunk { duke@435: friend class DebugInformationRecorder; duke@435: int _offset; // location in the stream of this scope duke@435: int _length; // number of bytes in the stream duke@435: int _hash; // hash of stream bytes (for quicker reuse) duke@435: duke@435: void* operator new(size_t ignore, DebugInformationRecorder* dir) { duke@435: assert(ignore == sizeof(DIR_Chunk), ""); duke@435: if (dir->_next_chunk >= dir->_next_chunk_limit) { duke@435: const int CHUNK = 100; duke@435: dir->_next_chunk = NEW_RESOURCE_ARRAY(DIR_Chunk, CHUNK); duke@435: dir->_next_chunk_limit = dir->_next_chunk + CHUNK; duke@435: } duke@435: return dir->_next_chunk++; duke@435: } duke@435: duke@435: DIR_Chunk(int offset, int length, DebugInformationRecorder* dir) { duke@435: _offset = offset; duke@435: _length = length; duke@435: unsigned int hash = 0; duke@435: address p = dir->stream()->buffer() + _offset; duke@435: for (int i = 0; i < length; i++) { duke@435: if (i == 6) break; duke@435: hash *= 127; duke@435: hash += p[i]; duke@435: } duke@435: _hash = hash; duke@435: } duke@435: duke@435: DIR_Chunk* find_match(GrowableArray* arr, duke@435: int start_index, duke@435: DebugInformationRecorder* dir) { duke@435: int end_index = arr->length(); duke@435: int hash = this->_hash, length = this->_length; duke@435: address buf = dir->stream()->buffer(); duke@435: for (int i = end_index; --i >= start_index; ) { duke@435: DIR_Chunk* that = arr->at(i); duke@435: if (hash == that->_hash && duke@435: length == that->_length && duke@435: 0 == memcmp(buf + this->_offset, buf + that->_offset, length)) { duke@435: return that; duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: }; duke@435: duke@435: static inline bool compute_recording_non_safepoints() { duke@435: if (JvmtiExport::should_post_compiled_method_load() duke@435: && FLAG_IS_DEFAULT(DebugNonSafepoints)) { duke@435: // The default value of this flag is taken to be true, duke@435: // if JVMTI is looking at nmethod codes. duke@435: // We anticipate that JVMTI may wish to participate in profiling. duke@435: return true; duke@435: } duke@435: duke@435: // If the flag is set manually, use it, whether true or false. duke@435: // Otherwise, if JVMTI is not in the picture, use the default setting. duke@435: // (This is true in debug, just for the exercise, false in product mode.) duke@435: return DebugNonSafepoints; duke@435: } duke@435: duke@435: DebugInformationRecorder::DebugInformationRecorder(OopRecorder* oop_recorder) duke@435: : _recording_non_safepoints(compute_recording_non_safepoints()) duke@435: { duke@435: _pcs_size = 100; duke@435: _pcs = NEW_RESOURCE_ARRAY(PcDesc, _pcs_size); duke@435: _pcs_length = 0; duke@435: duke@435: _prev_safepoint_pc = PcDesc::lower_offset_limit; duke@435: duke@435: _stream = new DebugInfoWriteStream(this, 10 * K); duke@435: // make sure that there is no stream_decode_offset that is zero duke@435: _stream->write_byte((jbyte)0xFF); duke@435: duke@435: // make sure that we can distinguish the value "serialized_null" from offsets duke@435: assert(_stream->position() > serialized_null, "sanity"); duke@435: duke@435: _oop_recorder = oop_recorder; duke@435: duke@435: _all_chunks = new GrowableArray(300); duke@435: _shared_chunks = new GrowableArray(30); duke@435: _next_chunk = _next_chunk_limit = NULL; duke@435: duke@435: add_new_pc_offset(PcDesc::lower_offset_limit); // sentinel record duke@435: duke@435: debug_only(_recording_state = rs_null); duke@435: } duke@435: duke@435: duke@435: void DebugInformationRecorder::add_oopmap(int pc_offset, OopMap* map) { duke@435: // !!!!! Preserve old style handling of oopmaps for now duke@435: _oopmaps->add_gc_map(pc_offset, map); duke@435: } duke@435: duke@435: void DebugInformationRecorder::add_safepoint(int pc_offset, OopMap* map) { duke@435: assert(!_oop_recorder->is_complete(), "not frozen yet"); duke@435: // Store the new safepoint duke@435: duke@435: // Add the oop map duke@435: add_oopmap(pc_offset, map); duke@435: duke@435: add_new_pc_offset(pc_offset); duke@435: duke@435: assert(_recording_state == rs_null, "nesting of recording calls"); duke@435: debug_only(_recording_state = rs_safepoint); duke@435: } duke@435: duke@435: void DebugInformationRecorder::add_non_safepoint(int pc_offset) { duke@435: assert(!_oop_recorder->is_complete(), "not frozen yet"); duke@435: assert(_recording_non_safepoints, "must be recording non-safepoints"); duke@435: duke@435: add_new_pc_offset(pc_offset); duke@435: duke@435: assert(_recording_state == rs_null, "nesting of recording calls"); duke@435: debug_only(_recording_state = rs_non_safepoint); duke@435: } duke@435: duke@435: void DebugInformationRecorder::add_new_pc_offset(int pc_offset) { duke@435: assert(_pcs_length == 0 || last_pc()->pc_offset() < pc_offset, duke@435: "must specify a new, larger pc offset"); duke@435: duke@435: // add the pcdesc duke@435: if (_pcs_length == _pcs_size) { duke@435: // Expand duke@435: int new_pcs_size = _pcs_size * 2; duke@435: PcDesc* new_pcs = NEW_RESOURCE_ARRAY(PcDesc, new_pcs_size); duke@435: for (int index = 0; index < _pcs_length; index++) { duke@435: new_pcs[index] = _pcs[index]; duke@435: } duke@435: _pcs_size = new_pcs_size; duke@435: _pcs = new_pcs; duke@435: } duke@435: assert(_pcs_size > _pcs_length, "There must be room for after expanding"); duke@435: duke@435: _pcs[_pcs_length++] = PcDesc(pc_offset, DebugInformationRecorder::serialized_null, duke@435: DebugInformationRecorder::serialized_null); duke@435: } duke@435: duke@435: duke@435: int DebugInformationRecorder::serialize_monitor_values(GrowableArray* monitors) { duke@435: if (monitors == NULL || monitors->is_empty()) return DebugInformationRecorder::serialized_null; duke@435: assert(_recording_state == rs_safepoint, "must be recording a safepoint"); duke@435: int result = stream()->position(); duke@435: stream()->write_int(monitors->length()); duke@435: for (int index = 0; index < monitors->length(); index++) { duke@435: monitors->at(index)->write_on(stream()); duke@435: } duke@435: assert(result != serialized_null, "sanity"); duke@435: duke@435: // (See comment below on DebugInformationRecorder::describe_scope.) duke@435: int shared_result = find_sharable_decode_offset(result); duke@435: if (shared_result != serialized_null) { duke@435: stream()->set_position(result); duke@435: result = shared_result; duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: duke@435: int DebugInformationRecorder::serialize_scope_values(GrowableArray* values) { duke@435: if (values == NULL || values->is_empty()) return DebugInformationRecorder::serialized_null; duke@435: assert(_recording_state == rs_safepoint, "must be recording a safepoint"); duke@435: int result = stream()->position(); duke@435: assert(result != serialized_null, "sanity"); duke@435: stream()->write_int(values->length()); duke@435: for (int index = 0; index < values->length(); index++) { duke@435: values->at(index)->write_on(stream()); duke@435: } duke@435: duke@435: // (See comment below on DebugInformationRecorder::describe_scope.) duke@435: int shared_result = find_sharable_decode_offset(result); duke@435: if (shared_result != serialized_null) { duke@435: stream()->set_position(result); duke@435: result = shared_result; duke@435: } duke@435: duke@435: return result; duke@435: } duke@435: duke@435: duke@435: #ifndef PRODUCT duke@435: // These variables are put into one block to reduce relocations duke@435: // and make it simpler to print from the debugger. duke@435: static duke@435: struct dir_stats_struct { duke@435: int chunks_queried; duke@435: int chunks_shared; duke@435: int chunks_reshared; duke@435: int chunks_elided; duke@435: duke@435: void print() { duke@435: tty->print_cr("Debug Data Chunks: %d, shared %d+%d, non-SP's elided %d", duke@435: chunks_queried, duke@435: chunks_shared, chunks_reshared, duke@435: chunks_elided); duke@435: } duke@435: } dir_stats; duke@435: #endif //PRODUCT duke@435: duke@435: duke@435: int DebugInformationRecorder::find_sharable_decode_offset(int stream_offset) { duke@435: // Only pull this trick if non-safepoint recording duke@435: // is enabled, for now. duke@435: if (!recording_non_safepoints()) duke@435: return serialized_null; duke@435: duke@435: NOT_PRODUCT(++dir_stats.chunks_queried); duke@435: int stream_length = stream()->position() - stream_offset; duke@435: assert(stream_offset != serialized_null, "should not be null"); duke@435: assert(stream_length != 0, "should not be empty"); duke@435: duke@435: DIR_Chunk* ns = new(this) DIR_Chunk(stream_offset, stream_length, this); duke@435: duke@435: // Look in previously shared scopes first: duke@435: DIR_Chunk* ms = ns->find_match(_shared_chunks, 0, this); duke@435: if (ms != NULL) { duke@435: NOT_PRODUCT(++dir_stats.chunks_reshared); duke@435: assert(ns+1 == _next_chunk, ""); duke@435: _next_chunk = ns; duke@435: return ms->_offset; duke@435: } duke@435: duke@435: // Look in recently encountered scopes next: duke@435: const int MAX_RECENT = 50; duke@435: int start_index = _all_chunks->length() - MAX_RECENT; duke@435: if (start_index < 0) start_index = 0; duke@435: ms = ns->find_match(_all_chunks, start_index, this); duke@435: if (ms != NULL) { duke@435: NOT_PRODUCT(++dir_stats.chunks_shared); duke@435: // Searching in _all_chunks is limited to a window, duke@435: // but searching in _shared_chunks is unlimited. duke@435: _shared_chunks->append(ms); duke@435: assert(ns+1 == _next_chunk, ""); duke@435: _next_chunk = ns; duke@435: return ms->_offset; duke@435: } duke@435: duke@435: // No match. Add this guy to the list, in hopes of future shares. duke@435: _all_chunks->append(ns); duke@435: return serialized_null; duke@435: } duke@435: duke@435: duke@435: // must call add_safepoint before: it sets PcDesc and this routine uses duke@435: // the last PcDesc set duke@435: void DebugInformationRecorder::describe_scope(int pc_offset, duke@435: ciMethod* method, duke@435: int bci, cfang@1335: bool reexecute, twisti@1570: bool is_method_handle_invoke, kvn@1688: bool return_oop, duke@435: DebugToken* locals, duke@435: DebugToken* expressions, duke@435: DebugToken* monitors) { duke@435: assert(_recording_state != rs_null, "nesting of recording calls"); duke@435: PcDesc* last_pd = last_pc(); duke@435: assert(last_pd->pc_offset() == pc_offset, "must be last pc"); duke@435: int sender_stream_offset = last_pd->scope_decode_offset(); duke@435: // update the stream offset of current pc desc duke@435: int stream_offset = stream()->position(); duke@435: last_pd->set_scope_decode_offset(stream_offset); duke@435: twisti@1570: // Record flags into pcDesc. cfang@1366: last_pd->set_should_reexecute(reexecute); twisti@1570: last_pd->set_is_method_handle_invoke(is_method_handle_invoke); kvn@1688: last_pd->set_return_oop(return_oop); cfang@1366: duke@435: // serialize sender stream offest duke@435: stream()->write_int(sender_stream_offset); duke@435: duke@435: // serialize scope coleenp@4037: Metadata* method_enc = (method == NULL)? NULL: method->constant_encoding(); duke@435: stream()->write_int(oop_recorder()->find_index(method_enc)); cfang@1366: stream()->write_bci(bci); duke@435: assert(method == NULL || duke@435: (method->is_native() && bci == 0) || duke@435: (!method->is_native() && 0 <= bci && bci < method->code_size()) || twisti@3969: (method->is_compiled_lambda_form() && bci == -99) || // this might happen in C1 duke@435: bci == -1, "illegal bci"); duke@435: duke@435: // serialize the locals/expressions/monitors duke@435: stream()->write_int((intptr_t) locals); duke@435: stream()->write_int((intptr_t) expressions); duke@435: stream()->write_int((intptr_t) monitors); duke@435: duke@435: // Here's a tricky bit. We just wrote some bytes. duke@435: // Wouldn't it be nice to find that we had already duke@435: // written those same bytes somewhere else? duke@435: // If we get lucky this way, reset the stream duke@435: // and reuse the old bytes. By the way, this duke@435: // trick not only shares parent scopes, but also duke@435: // compresses equivalent non-safepoint PcDescs. duke@435: int shared_stream_offset = find_sharable_decode_offset(stream_offset); duke@435: if (shared_stream_offset != serialized_null) { duke@435: stream()->set_position(stream_offset); duke@435: last_pd->set_scope_decode_offset(shared_stream_offset); duke@435: } duke@435: } duke@435: duke@435: void DebugInformationRecorder::dump_object_pool(GrowableArray* objects) { duke@435: guarantee( _pcs_length > 0, "safepoint must exist before describing scopes"); duke@435: PcDesc* last_pd = &_pcs[_pcs_length-1]; duke@435: if (objects != NULL) { duke@435: for (int i = objects->length() - 1; i >= 0; i--) { duke@435: ((ObjectValue*) objects->at(i))->set_visited(false); duke@435: } duke@435: } duke@435: int offset = serialize_scope_values(objects); duke@435: last_pd->set_obj_decode_offset(offset); duke@435: } duke@435: duke@435: void DebugInformationRecorder::end_scopes(int pc_offset, bool is_safepoint) { duke@435: assert(_recording_state == (is_safepoint? rs_safepoint: rs_non_safepoint), duke@435: "nesting of recording calls"); duke@435: debug_only(_recording_state = rs_null); duke@435: duke@435: // Try to compress away an equivalent non-safepoint predecessor. duke@435: // (This only works because we have previously recognized redundant duke@435: // scope trees and made them use a common scope_decode_offset.) duke@435: if (_pcs_length >= 2 && recording_non_safepoints()) { duke@435: PcDesc* last = last_pc(); duke@435: PcDesc* prev = prev_pc(); duke@435: // If prev is (a) not a safepoint and (b) has the same duke@435: // stream pointer, then it can be coalesced into the last. duke@435: // This is valid because non-safepoints are only sought duke@435: // with pc_desc_near, which (when it misses prev) will duke@435: // search forward until it finds last. duke@435: // In addition, it does not matter if the last PcDesc duke@435: // is for a safepoint or not. never@1449: if (_prev_safepoint_pc < prev->pc_offset() && prev->is_same_info(last)) { duke@435: assert(prev == last-1, "sane"); duke@435: prev->set_pc_offset(pc_offset); duke@435: _pcs_length -= 1; duke@435: NOT_PRODUCT(++dir_stats.chunks_elided); duke@435: } duke@435: } duke@435: duke@435: // We have just recorded this safepoint. duke@435: // Remember it in case the previous paragraph needs to know. duke@435: if (is_safepoint) { duke@435: _prev_safepoint_pc = pc_offset; duke@435: } duke@435: } duke@435: coleenp@4037: #ifdef ASSERT coleenp@4037: bool DebugInformationRecorder::recorders_frozen() { coleenp@4037: return _oop_recorder->is_complete() || _oop_recorder->is_complete(); coleenp@4037: } coleenp@4037: coleenp@4037: void DebugInformationRecorder::mark_recorders_frozen() { coleenp@4037: _oop_recorder->freeze(); coleenp@4037: } coleenp@4037: #endif // PRODUCT coleenp@4037: duke@435: DebugToken* DebugInformationRecorder::create_scope_values(GrowableArray* values) { coleenp@4037: assert(!recorders_frozen(), "not frozen yet"); duke@435: return (DebugToken*) (intptr_t) serialize_scope_values(values); duke@435: } duke@435: duke@435: duke@435: DebugToken* DebugInformationRecorder::create_monitor_values(GrowableArray* monitors) { coleenp@4037: assert(!recorders_frozen(), "not frozen yet"); duke@435: return (DebugToken*) (intptr_t) serialize_monitor_values(monitors); duke@435: } duke@435: duke@435: duke@435: int DebugInformationRecorder::data_size() { coleenp@4037: debug_only(mark_recorders_frozen()); // mark it "frozen" for asserts duke@435: return _stream->position(); duke@435: } duke@435: duke@435: duke@435: int DebugInformationRecorder::pcs_size() { coleenp@4037: debug_only(mark_recorders_frozen()); // mark it "frozen" for asserts duke@435: if (last_pc()->pc_offset() != PcDesc::upper_offset_limit) duke@435: add_new_pc_offset(PcDesc::upper_offset_limit); duke@435: return _pcs_length * sizeof(PcDesc); duke@435: } duke@435: duke@435: duke@435: void DebugInformationRecorder::copy_to(nmethod* nm) { duke@435: nm->copy_scopes_data(stream()->buffer(), stream()->position()); duke@435: nm->copy_scopes_pcs(_pcs, _pcs_length); duke@435: } duke@435: duke@435: duke@435: void DebugInformationRecorder::verify(const nmethod* code) { duke@435: Unimplemented(); duke@435: } duke@435: duke@435: #ifndef PRODUCT duke@435: void DebugInformationRecorder::print_statistics() { duke@435: dir_stats.print(); duke@435: } duke@435: #endif //PRODUCT