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 "code/debugInfoRec.hpp" 27 #include "code/scopeDesc.hpp" 28 #include "prims/jvmtiExport.hpp" 29 30 // Private definition. 31 // There is one DIR_Chunk for each scope and values array. 32 // A chunk can potentially be used more than once. 33 // We keep track of these chunks in order to detect 34 // repetition and enable sharing. 35 class DIR_Chunk { 36 friend class DebugInformationRecorder; 37 int _offset; // location in the stream of this scope 38 int _length; // number of bytes in the stream 39 int _hash; // hash of stream bytes (for quicker reuse) 40 41 void* operator new(size_t ignore, DebugInformationRecorder* dir) throw() { 42 assert(ignore == sizeof(DIR_Chunk), ""); 43 if (dir->_next_chunk >= dir->_next_chunk_limit) { 44 const int CHUNK = 100; 45 dir->_next_chunk = NEW_RESOURCE_ARRAY(DIR_Chunk, CHUNK); 46 dir->_next_chunk_limit = dir->_next_chunk + CHUNK; 47 } 48 return dir->_next_chunk++; 49 } 50 51 DIR_Chunk(int offset, int length, DebugInformationRecorder* dir) { 52 _offset = offset; 53 _length = length; 54 unsigned int hash = 0; 55 address p = dir->stream()->buffer() + _offset; 56 for (int i = 0; i < length; i++) { 57 if (i == 6) break; 58 hash *= 127; 59 hash += p[i]; 60 } 61 _hash = hash; 62 } 63 64 DIR_Chunk* find_match(GrowableArray<DIR_Chunk*>* arr, 65 int start_index, 66 DebugInformationRecorder* dir) { 67 int end_index = arr->length(); 68 int hash = this->_hash, length = this->_length; 69 address buf = dir->stream()->buffer(); 70 for (int i = end_index; --i >= start_index; ) { 71 DIR_Chunk* that = arr->at(i); 72 if (hash == that->_hash && 73 length == that->_length && 74 0 == memcmp(buf + this->_offset, buf + that->_offset, length)) { 75 return that; 76 } 77 } 78 return NULL; 79 } 80 }; 81 82 static inline bool compute_recording_non_safepoints() { 83 if (JvmtiExport::should_post_compiled_method_load() 84 && FLAG_IS_DEFAULT(DebugNonSafepoints)) { 85 // The default value of this flag is taken to be true, 86 // if JVMTI is looking at nmethod codes. 87 // We anticipate that JVMTI may wish to participate in profiling. 88 return true; 89 } 90 91 // If the flag is set manually, use it, whether true or false. 92 // Otherwise, if JVMTI is not in the picture, use the default setting. 93 // (This is true in debug, just for the exercise, false in product mode.) 94 return DebugNonSafepoints; 95 } 96 97 DebugInformationRecorder::DebugInformationRecorder(OopRecorder* oop_recorder) 98 : _recording_non_safepoints(compute_recording_non_safepoints()) 99 { 100 _pcs_size = 100; 101 _pcs = NEW_RESOURCE_ARRAY(PcDesc, _pcs_size); 102 _pcs_length = 0; 103 104 _prev_safepoint_pc = PcDesc::lower_offset_limit; 105 106 _stream = new DebugInfoWriteStream(this, 10 * K); 107 // make sure that there is no stream_decode_offset that is zero 108 _stream->write_byte((jbyte)0xFF); 109 110 // make sure that we can distinguish the value "serialized_null" from offsets 111 assert(_stream->position() > serialized_null, "sanity"); 112 113 _oop_recorder = oop_recorder; 114 115 _all_chunks = new GrowableArray<DIR_Chunk*>(300); 116 _shared_chunks = new GrowableArray<DIR_Chunk*>(30); 117 _next_chunk = _next_chunk_limit = NULL; 118 119 add_new_pc_offset(PcDesc::lower_offset_limit); // sentinel record 120 121 debug_only(_recording_state = rs_null); 122 } 123 124 125 void DebugInformationRecorder::add_oopmap(int pc_offset, OopMap* map) { 126 // !!!!! Preserve old style handling of oopmaps for now 127 _oopmaps->add_gc_map(pc_offset, map); 128 } 129 130 void DebugInformationRecorder::add_safepoint(int pc_offset, OopMap* map) { 131 assert(!_oop_recorder->is_complete(), "not frozen yet"); 132 // Store the new safepoint 133 134 // Add the oop map 135 add_oopmap(pc_offset, map); 136 218 // These variables are put into one block to reduce relocations 219 // and make it simpler to print from the debugger. 220 static 221 struct dir_stats_struct { 222 int chunks_queried; 223 int chunks_shared; 224 int chunks_reshared; 225 int chunks_elided; 226 227 void print() { 228 tty->print_cr("Debug Data Chunks: %d, shared %d+%d, non-SP's elided %d", 229 chunks_queried, 230 chunks_shared, chunks_reshared, 231 chunks_elided); 232 } 233 } dir_stats; 234 #endif //PRODUCT 235 236 237 int DebugInformationRecorder::find_sharable_decode_offset(int stream_offset) { 238 // Only pull this trick if non-safepoint recording 239 // is enabled, for now. 240 if (!recording_non_safepoints()) 241 return serialized_null; 242 243 NOT_PRODUCT(++dir_stats.chunks_queried); 244 int stream_length = stream()->position() - stream_offset; 245 assert(stream_offset != serialized_null, "should not be null"); 246 assert(stream_length != 0, "should not be empty"); 247 248 DIR_Chunk* ns = new(this) DIR_Chunk(stream_offset, stream_length, this); 249 250 // Look in previously shared scopes first: 251 DIR_Chunk* ms = ns->find_match(_shared_chunks, 0, this); 252 if (ms != NULL) { 253 NOT_PRODUCT(++dir_stats.chunks_reshared); 254 assert(ns+1 == _next_chunk, ""); 255 _next_chunk = ns; 256 return ms->_offset; 257 } 258 259 // Look in recently encountered scopes next: 260 const int MAX_RECENT = 50; 261 int start_index = _all_chunks->length() - MAX_RECENT; 262 if (start_index < 0) start_index = 0; 263 ms = ns->find_match(_all_chunks, start_index, this); 264 if (ms != NULL) { 265 NOT_PRODUCT(++dir_stats.chunks_shared); 266 // Searching in _all_chunks is limited to a window, 267 // but searching in _shared_chunks is unlimited. 268 _shared_chunks->append(ms); 269 assert(ns+1 == _next_chunk, ""); 270 _next_chunk = ns; 271 return ms->_offset; 272 } 273 274 // No match. Add this guy to the list, in hopes of future shares. 275 _all_chunks->append(ns); 276 return serialized_null; 277 } 278 279 280 // must call add_safepoint before: it sets PcDesc and this routine uses 281 // the last PcDesc set 282 void DebugInformationRecorder::describe_scope(int pc_offset, 283 ciMethod* method, 284 int bci, 285 bool reexecute, 286 bool is_method_handle_invoke, 287 bool return_oop, 288 DebugToken* locals, 289 DebugToken* expressions, 290 DebugToken* monitors) { 291 assert(_recording_state != rs_null, "nesting of recording calls"); 292 PcDesc* last_pd = last_pc(); 293 assert(last_pd->pc_offset() == pc_offset, "must be last pc"); 294 int sender_stream_offset = last_pd->scope_decode_offset(); 295 // update the stream offset of current pc desc 296 int stream_offset = stream()->position(); 297 last_pd->set_scope_decode_offset(stream_offset); 298 299 // Record flags into pcDesc. 300 last_pd->set_should_reexecute(reexecute); 301 last_pd->set_is_method_handle_invoke(is_method_handle_invoke); 302 last_pd->set_return_oop(return_oop); 303 304 // serialize sender stream offest 305 stream()->write_int(sender_stream_offset); 306 307 // serialize scope 308 Metadata* method_enc = (method == NULL)? NULL: method->constant_encoding(); 309 stream()->write_int(oop_recorder()->find_index(method_enc)); 310 stream()->write_bci(bci); 311 assert(method == NULL || 312 (method->is_native() && bci == 0) || 313 (!method->is_native() && 0 <= bci && bci < method->code_size()) || 314 (method->is_compiled_lambda_form() && bci == -99) || // this might happen in C1 315 bci == -1, "illegal bci"); 316 317 // serialize the locals/expressions/monitors 318 stream()->write_int((intptr_t) locals); 319 stream()->write_int((intptr_t) expressions); 320 stream()->write_int((intptr_t) monitors); 321 322 // Here's a tricky bit. We just wrote some bytes. 323 // Wouldn't it be nice to find that we had already 324 // written those same bytes somewhere else? 325 // If we get lucky this way, reset the stream 326 // and reuse the old bytes. By the way, this 327 // trick not only shares parent scopes, but also 328 // compresses equivalent non-safepoint PcDescs. 329 int shared_stream_offset = find_sharable_decode_offset(stream_offset); 330 if (shared_stream_offset != serialized_null) { 331 stream()->set_position(stream_offset); 332 last_pd->set_scope_decode_offset(shared_stream_offset); 333 } 334 } 335 336 void DebugInformationRecorder::dump_object_pool(GrowableArray<ScopeValue*>* objects) { 337 guarantee( _pcs_length > 0, "safepoint must exist before describing scopes"); 338 PcDesc* last_pd = &_pcs[_pcs_length-1]; 339 if (objects != NULL) { 340 for (int i = objects->length() - 1; i >= 0; i--) { 341 ((ObjectValue*) objects->at(i))->set_visited(false); 342 } 343 } 344 int offset = serialize_scope_values(objects); 345 last_pd->set_obj_decode_offset(offset); 346 } 347 348 void DebugInformationRecorder::end_scopes(int pc_offset, bool is_safepoint) { 349 assert(_recording_state == (is_safepoint? rs_safepoint: rs_non_safepoint), 350 "nesting of recording calls"); 351 debug_only(_recording_state = rs_null); 352 353 // Try to compress away an equivalent non-safepoint predecessor. 354 // (This only works because we have previously recognized redundant 355 // scope trees and made them use a common scope_decode_offset.) 356 if (_pcs_length >= 2 && recording_non_safepoints()) { 357 PcDesc* last = last_pc(); 358 PcDesc* prev = prev_pc(); 359 // If prev is (a) not a safepoint and (b) has the same 360 // stream pointer, then it can be coalesced into the last. 361 // This is valid because non-safepoints are only sought | 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 "code/debugInfoRec.hpp" 27 #include "code/scopeDesc.hpp" 28 #include "prims/jvmtiExport.hpp" 29 30 // Private definition. 31 // There is one DIR_Chunk for each scope and values array. 32 // A chunk can potentially be used more than once. 33 // We keep track of these chunks in order to detect 34 // repetition and enable sharing. 35 class DIR_Chunk { 36 friend class DebugInformationRecorder; 37 int _offset; // location in the stream of this scope 38 int _length; // number of bytes in the stream 39 int _hash; // hash of stream bytes (for quicker reuse) 40 #if INCLUDE_JVMCI 41 DebugInformationRecorder* _DIR; 42 #endif 43 44 void* operator new(size_t ignore, DebugInformationRecorder* dir) throw() { 45 assert(ignore == sizeof(DIR_Chunk), ""); 46 if (dir->_next_chunk >= dir->_next_chunk_limit) { 47 const int CHUNK = 100; 48 dir->_next_chunk = NEW_RESOURCE_ARRAY(DIR_Chunk, CHUNK); 49 dir->_next_chunk_limit = dir->_next_chunk + CHUNK; 50 } 51 return dir->_next_chunk++; 52 } 53 54 DIR_Chunk(int offset, int length, DebugInformationRecorder* dir) { 55 _offset = offset; 56 _length = length; 57 #if INCLUDE_JVMCI 58 _DIR = dir; 59 #endif 60 unsigned int hash = 0; 61 address p = dir->stream()->buffer() + _offset; 62 for (int i = 0; i < length; i++) { 63 if (i == 6) break; 64 hash *= 127; 65 hash += p[i]; 66 } 67 _hash = hash; 68 } 69 70 DIR_Chunk* find_match(GrowableArray<DIR_Chunk*>* arr, 71 int start_index, 72 DebugInformationRecorder* dir) { 73 int end_index = arr->length(); 74 int hash = this->_hash, length = this->_length; 75 address buf = dir->stream()->buffer(); 76 for (int i = end_index; --i >= start_index; ) { 77 DIR_Chunk* that = arr->at(i); 78 if (hash == that->_hash && 79 length == that->_length && 80 0 == memcmp(buf + this->_offset, buf + that->_offset, length)) { 81 return that; 82 } 83 } 84 return NULL; 85 } 86 87 #if INCLUDE_JVMCI 88 static int compare(DIR_Chunk* const & a, DIR_Chunk* const & b) { 89 if (b->_hash > a->_hash) { 90 return 1; 91 } 92 if (b->_hash < a->_hash) { 93 return -1; 94 } 95 if (b->_length > a->_length) { 96 return 1; 97 } 98 if (b->_length < a->_length) { 99 return -1; 100 } 101 address buf = a->_DIR->stream()->buffer(); 102 return memcmp(buf + b->_offset, buf + a->_offset, a->_length); 103 } 104 #endif 105 }; 106 107 static inline bool compute_recording_non_safepoints() { 108 if (JvmtiExport::should_post_compiled_method_load() 109 && FLAG_IS_DEFAULT(DebugNonSafepoints)) { 110 // The default value of this flag is taken to be true, 111 // if JVMTI is looking at nmethod codes. 112 // We anticipate that JVMTI may wish to participate in profiling. 113 return true; 114 } 115 116 // If the flag is set manually, use it, whether true or false. 117 // Otherwise, if JVMTI is not in the picture, use the default setting. 118 // (This is true in debug, just for the exercise, false in product mode.) 119 return DebugNonSafepoints; 120 } 121 122 DebugInformationRecorder::DebugInformationRecorder(OopRecorder* oop_recorder) 123 : _recording_non_safepoints(compute_recording_non_safepoints()) 124 { 125 _pcs_size = 100; 126 _pcs = NEW_RESOURCE_ARRAY(PcDesc, _pcs_size); 127 _pcs_length = 0; 128 129 _prev_safepoint_pc = PcDesc::lower_offset_limit; 130 131 _stream = new DebugInfoWriteStream(this, 10 * K); 132 // make sure that there is no stream_decode_offset that is zero 133 _stream->write_byte((jbyte)0xFF); 134 135 // make sure that we can distinguish the value "serialized_null" from offsets 136 assert(_stream->position() > serialized_null, "sanity"); 137 138 _oop_recorder = oop_recorder; 139 140 _all_chunks = new GrowableArray<DIR_Chunk*>(300); 141 #if !INCLUDE_JVMCI 142 _shared_chunks = new GrowableArray<DIR_Chunk*>(30); 143 #endif 144 _next_chunk = _next_chunk_limit = NULL; 145 146 add_new_pc_offset(PcDesc::lower_offset_limit); // sentinel record 147 148 debug_only(_recording_state = rs_null); 149 } 150 151 152 void DebugInformationRecorder::add_oopmap(int pc_offset, OopMap* map) { 153 // !!!!! Preserve old style handling of oopmaps for now 154 _oopmaps->add_gc_map(pc_offset, map); 155 } 156 157 void DebugInformationRecorder::add_safepoint(int pc_offset, OopMap* map) { 158 assert(!_oop_recorder->is_complete(), "not frozen yet"); 159 // Store the new safepoint 160 161 // Add the oop map 162 add_oopmap(pc_offset, map); 163 245 // These variables are put into one block to reduce relocations 246 // and make it simpler to print from the debugger. 247 static 248 struct dir_stats_struct { 249 int chunks_queried; 250 int chunks_shared; 251 int chunks_reshared; 252 int chunks_elided; 253 254 void print() { 255 tty->print_cr("Debug Data Chunks: %d, shared %d+%d, non-SP's elided %d", 256 chunks_queried, 257 chunks_shared, chunks_reshared, 258 chunks_elided); 259 } 260 } dir_stats; 261 #endif //PRODUCT 262 263 264 int DebugInformationRecorder::find_sharable_decode_offset(int stream_offset) { 265 #if !INCLUDE_JVMCI 266 // Only pull this trick if non-safepoint recording 267 // is enabled, for now. 268 if (!recording_non_safepoints()) { 269 return serialized_null; 270 } 271 #endif // INCLUDE_JVMCI 272 273 NOT_PRODUCT(++dir_stats.chunks_queried); 274 int stream_length = stream()->position() - stream_offset; 275 assert(stream_offset != serialized_null, "should not be null"); 276 assert(stream_length != 0, "should not be empty"); 277 278 DIR_Chunk* ns = new(this) DIR_Chunk(stream_offset, stream_length, this); 279 280 #if INCLUDE_JVMCI 281 DIR_Chunk* match = _all_chunks->insert_sorted<DIR_Chunk::compare>(ns); 282 if (match != ns) { 283 // Found an existing chunk 284 NOT_PRODUCT(++dir_stats.chunks_shared); 285 assert(ns+1 == _next_chunk, ""); 286 _next_chunk = ns; 287 return match->_offset; 288 } else { 289 // Inserted this chunk, so nothing to do 290 return serialized_null; 291 } 292 #else // INCLUDE_JVMCI 293 // Look in previously shared scopes first: 294 DIR_Chunk* ms = ns->find_match(_shared_chunks, 0, this); 295 if (ms != NULL) { 296 NOT_PRODUCT(++dir_stats.chunks_reshared); 297 assert(ns+1 == _next_chunk, ""); 298 _next_chunk = ns; 299 return ms->_offset; 300 } 301 302 // Look in recently encountered scopes next: 303 const int MAX_RECENT = 50; 304 int start_index = _all_chunks->length() - MAX_RECENT; 305 if (start_index < 0) start_index = 0; 306 ms = ns->find_match(_all_chunks, start_index, this); 307 if (ms != NULL) { 308 NOT_PRODUCT(++dir_stats.chunks_shared); 309 // Searching in _all_chunks is limited to a window, 310 // but searching in _shared_chunks is unlimited. 311 _shared_chunks->append(ms); 312 assert(ns+1 == _next_chunk, ""); 313 _next_chunk = ns; 314 return ms->_offset; 315 } 316 317 // No match. Add this guy to the list, in hopes of future shares. 318 _all_chunks->append(ns); 319 return serialized_null; 320 #endif // INCLUDE_JVMCI 321 } 322 323 324 // must call add_safepoint before: it sets PcDesc and this routine uses 325 // the last PcDesc set 326 void DebugInformationRecorder::describe_scope(int pc_offset, 327 methodHandle methodH, 328 ciMethod* method, 329 int bci, 330 bool reexecute, 331 bool rethrow_exception, 332 bool is_method_handle_invoke, 333 bool return_oop, 334 DebugToken* locals, 335 DebugToken* expressions, 336 DebugToken* monitors) { 337 assert(_recording_state != rs_null, "nesting of recording calls"); 338 PcDesc* last_pd = last_pc(); 339 assert(last_pd->pc_offset() == pc_offset, "must be last pc"); 340 int sender_stream_offset = last_pd->scope_decode_offset(); 341 // update the stream offset of current pc desc 342 int stream_offset = stream()->position(); 343 last_pd->set_scope_decode_offset(stream_offset); 344 345 // Record flags into pcDesc. 346 last_pd->set_should_reexecute(reexecute); 347 last_pd->set_rethrow_exception(rethrow_exception); 348 last_pd->set_is_method_handle_invoke(is_method_handle_invoke); 349 last_pd->set_return_oop(return_oop); 350 351 // serialize sender stream offest 352 stream()->write_int(sender_stream_offset); 353 354 // serialize scope 355 Metadata* method_enc; 356 if (method != NULL) { 357 method_enc = method->constant_encoding(); 358 } else if (methodH.not_null()) { 359 method_enc = methodH(); 360 } else { 361 method_enc = NULL; 362 } 363 int method_enc_index = oop_recorder()->find_index(method_enc); 364 stream()->write_int(method_enc_index); 365 stream()->write_bci(bci); 366 assert(method == NULL || 367 (method->is_native() && bci == 0) || 368 (!method->is_native() && 0 <= bci && bci < method->code_size()) || 369 (method->is_compiled_lambda_form() && bci == -99) || // this might happen in C1 370 bci == -1, "illegal bci"); 371 372 // serialize the locals/expressions/monitors 373 stream()->write_int((intptr_t) locals); 374 stream()->write_int((intptr_t) expressions); 375 stream()->write_int((intptr_t) monitors); 376 377 // Here's a tricky bit. We just wrote some bytes. 378 // Wouldn't it be nice to find that we had already 379 // written those same bytes somewhere else? 380 // If we get lucky this way, reset the stream 381 // and reuse the old bytes. By the way, this 382 // trick not only shares parent scopes, but also 383 // compresses equivalent non-safepoint PcDescs. 384 int shared_stream_offset = find_sharable_decode_offset(stream_offset); 385 if (shared_stream_offset != serialized_null) { 386 stream()->set_position(stream_offset); 387 last_pd->set_scope_decode_offset(shared_stream_offset); 388 } 389 } 390 391 void DebugInformationRecorder::dump_object_pool(GrowableArray<ScopeValue*>* objects) { 392 guarantee( _pcs_length > 0, "safepoint must exist before describing scopes"); 393 PcDesc* last_pd = &_pcs[_pcs_length-1]; 394 if (objects != NULL) { 395 for (int i = objects->length() - 1; i >= 0; i--) { 396 objects->at(i)->as_ObjectValue()->set_visited(false); 397 } 398 } 399 int offset = serialize_scope_values(objects); 400 last_pd->set_obj_decode_offset(offset); 401 } 402 403 void DebugInformationRecorder::end_scopes(int pc_offset, bool is_safepoint) { 404 assert(_recording_state == (is_safepoint? rs_safepoint: rs_non_safepoint), 405 "nesting of recording calls"); 406 debug_only(_recording_state = rs_null); 407 408 // Try to compress away an equivalent non-safepoint predecessor. 409 // (This only works because we have previously recognized redundant 410 // scope trees and made them use a common scope_decode_offset.) 411 if (_pcs_length >= 2 && recording_non_safepoints()) { 412 PcDesc* last = last_pc(); 413 PcDesc* prev = prev_pc(); 414 // If prev is (a) not a safepoint and (b) has the same 415 // stream pointer, then it can be coalesced into the last. 416 // This is valid because non-safepoints are only sought |