1 /* 2 * Copyright (c) 1998, 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 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 private: 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 public: 45 int offset() { return _offset; } 46 47 void* operator new(size_t ignore, DebugInformationRecorder* dir) throw() { 48 assert(ignore == sizeof(DIR_Chunk), ""); 49 if (dir->_next_chunk >= dir->_next_chunk_limit) { 50 const int CHUNK = 100; 51 dir->_next_chunk = NEW_RESOURCE_ARRAY(DIR_Chunk, CHUNK); 52 dir->_next_chunk_limit = dir->_next_chunk + CHUNK; 53 } 54 return dir->_next_chunk++; 55 } 56 57 DIR_Chunk(int offset, int length, DebugInformationRecorder* dir) { 58 _offset = offset; 59 _length = length; 60 #if INCLUDE_JVMCI 61 _DIR = dir; 62 #endif 63 unsigned int hash = 0; 64 address p = dir->stream()->buffer() + _offset; 65 for (int i = 0; i < length; i++) { 66 if (i == 6) break; 67 hash *= 127; 68 hash += p[i]; 69 } 70 _hash = hash; 71 } 72 73 DIR_Chunk* find_match(GrowableArray<DIR_Chunk*>* arr, 74 int start_index, 75 DebugInformationRecorder* dir) { 76 int end_index = arr->length(); 77 int hash = this->_hash, length = this->_length; 78 address buf = dir->stream()->buffer(); 79 for (int i = end_index; --i >= start_index; ) { 80 DIR_Chunk* that = arr->at(i); 81 if (hash == that->_hash && 82 length == that->_length && 83 0 == memcmp(buf + this->_offset, buf + that->_offset, length)) { 84 return that; 85 } 86 } 87 return NULL; 88 } 89 90 #if INCLUDE_JVMCI 91 static int compare(DIR_Chunk* const & a, DIR_Chunk* const & b) { 92 if (b->_hash > a->_hash) { 93 return 1; 94 } 95 if (b->_hash < a->_hash) { 96 return -1; 97 } 98 if (b->_length > a->_length) { 99 return 1; 100 } 101 if (b->_length < a->_length) { 102 return -1; 103 } 104 address buf = a->_DIR->stream()->buffer(); 105 return memcmp(buf + b->_offset, buf + a->_offset, a->_length); 106 } 107 #endif 108 }; 109 110 static inline bool compute_recording_non_safepoints() { 111 if (JvmtiExport::should_post_compiled_method_load() 112 && FLAG_IS_DEFAULT(DebugNonSafepoints)) { 113 // The default value of this flag is taken to be true, 114 // if JVMTI is looking at nmethod codes. 115 // We anticipate that JVMTI may wish to participate in profiling. 116 return true; 117 } 118 119 // If the flag is set manually, use it, whether true or false. 120 // Otherwise, if JVMTI is not in the picture, use the default setting. 121 // (This is true in debug, just for the exercise, false in product mode.) 122 return DebugNonSafepoints; 123 } 124 125 DebugInformationRecorder::DebugInformationRecorder(OopRecorder* oop_recorder) 126 : _recording_non_safepoints(compute_recording_non_safepoints()) 127 { 128 _pcs_size = 100; 129 _pcs = NEW_RESOURCE_ARRAY(PcDesc, _pcs_size); 130 _pcs_length = 0; 131 132 _prev_safepoint_pc = PcDesc::lower_offset_limit; 133 134 _stream = new DebugInfoWriteStream(this, 10 * K); 135 // make sure that there is no stream_decode_offset that is zero 136 _stream->write_byte((jbyte)0xFF); 137 138 // make sure that we can distinguish the value "serialized_null" from offsets 139 assert(_stream->position() > serialized_null, "sanity"); 140 141 _oop_recorder = oop_recorder; 142 143 _all_chunks = new GrowableArray<DIR_Chunk*>(300); 144 #if !INCLUDE_JVMCI 145 _shared_chunks = new GrowableArray<DIR_Chunk*>(30); 146 #endif 147 _next_chunk = _next_chunk_limit = NULL; 148 149 add_new_pc_offset(PcDesc::lower_offset_limit); // sentinel record 150 151 debug_only(_recording_state = rs_null); 152 } 153 154 155 void DebugInformationRecorder::add_oopmap(int pc_offset, OopMap* map) { 156 // !!!!! Preserve old style handling of oopmaps for now 157 _oopmaps->add_gc_map(pc_offset, map); 158 } 159 160 void DebugInformationRecorder::add_safepoint(int pc_offset, OopMap* map) { 161 assert(!_oop_recorder->is_complete(), "not frozen yet"); 162 // Store the new safepoint 163 164 // Add the oop map 165 add_oopmap(pc_offset, map); 166 167 add_new_pc_offset(pc_offset); 168 169 assert(_recording_state == rs_null, "nesting of recording calls"); 170 debug_only(_recording_state = rs_safepoint); 171 } 172 173 void DebugInformationRecorder::add_non_safepoint(int pc_offset) { 174 assert(!_oop_recorder->is_complete(), "not frozen yet"); 175 assert(_recording_non_safepoints, "must be recording non-safepoints"); 176 177 add_new_pc_offset(pc_offset); 178 179 assert(_recording_state == rs_null, "nesting of recording calls"); 180 debug_only(_recording_state = rs_non_safepoint); 181 } 182 183 void DebugInformationRecorder::add_new_pc_offset(int pc_offset) { 184 assert(_pcs_length == 0 || last_pc()->pc_offset() < pc_offset, 185 "must specify a new, larger pc offset"); 186 187 // add the pcdesc 188 if (_pcs_length == _pcs_size) { 189 // Expand 190 int new_pcs_size = _pcs_size * 2; 191 PcDesc* new_pcs = NEW_RESOURCE_ARRAY(PcDesc, new_pcs_size); 192 for (int index = 0; index < _pcs_length; index++) { 193 new_pcs[index] = _pcs[index]; 194 } 195 _pcs_size = new_pcs_size; 196 _pcs = new_pcs; 197 } 198 assert(_pcs_size > _pcs_length, "There must be room for after expanding"); 199 200 _pcs[_pcs_length++] = PcDesc(pc_offset, DebugInformationRecorder::serialized_null, 201 DebugInformationRecorder::serialized_null); 202 } 203 204 205 int DebugInformationRecorder::serialize_monitor_values(GrowableArray<MonitorValue*>* monitors) { 206 if (monitors == NULL || monitors->is_empty()) return DebugInformationRecorder::serialized_null; 207 assert(_recording_state == rs_safepoint, "must be recording a safepoint"); 208 int result = stream()->position(); 209 stream()->write_int(monitors->length()); 210 for (int index = 0; index < monitors->length(); index++) { 211 monitors->at(index)->write_on(stream()); 212 } 213 assert(result != serialized_null, "sanity"); 214 215 // (See comment below on DebugInformationRecorder::describe_scope.) 216 int shared_result = find_sharable_decode_offset(result); 217 if (shared_result != serialized_null) { 218 stream()->set_position(result); 219 result = shared_result; 220 } 221 222 return result; 223 } 224 225 226 int DebugInformationRecorder::serialize_scope_values(GrowableArray<ScopeValue*>* values) { 227 if (values == NULL || values->is_empty()) return DebugInformationRecorder::serialized_null; 228 assert(_recording_state == rs_safepoint, "must be recording a safepoint"); 229 int result = stream()->position(); 230 assert(result != serialized_null, "sanity"); 231 stream()->write_int(values->length()); 232 for (int index = 0; index < values->length(); index++) { 233 values->at(index)->write_on(stream()); 234 } 235 236 // (See comment below on DebugInformationRecorder::describe_scope.) 237 int shared_result = find_sharable_decode_offset(result); 238 if (shared_result != serialized_null) { 239 stream()->set_position(result); 240 result = shared_result; 241 } 242 243 return result; 244 } 245 246 247 #ifndef PRODUCT 248 // These variables are put into one block to reduce relocations 249 // and make it simpler to print from the debugger. 250 static 251 struct dir_stats_struct { 252 int chunks_queried; 253 int chunks_shared; 254 int chunks_reshared; 255 int chunks_elided; 256 257 void print() { 258 tty->print_cr("Debug Data Chunks: %d, shared %d+%d, non-SP's elided %d", 259 chunks_queried, 260 chunks_shared, chunks_reshared, 261 chunks_elided); 262 } 263 } dir_stats; 264 #endif //PRODUCT 265 266 267 int DebugInformationRecorder::find_sharable_decode_offset(int stream_offset) { 268 #if !INCLUDE_JVMCI 269 // Only pull this trick if non-safepoint recording 270 // is enabled, for now. 271 if (!recording_non_safepoints()) { 272 return serialized_null; 273 } 274 #endif // INCLUDE_JVMCI 275 276 NOT_PRODUCT(++dir_stats.chunks_queried); 277 int stream_length = stream()->position() - stream_offset; 278 assert(stream_offset != serialized_null, "should not be null"); 279 assert(stream_length != 0, "should not be empty"); 280 281 DIR_Chunk* ns = new(this) DIR_Chunk(stream_offset, stream_length, this); 282 283 #if INCLUDE_JVMCI 284 DIR_Chunk* match = _all_chunks->insert_sorted<DIR_Chunk::compare>(ns); 285 if (match != ns) { 286 // Found an existing chunk 287 NOT_PRODUCT(++dir_stats.chunks_shared); 288 assert(ns+1 == _next_chunk, ""); 289 _next_chunk = ns; 290 return match->offset(); 291 } else { 292 // Inserted this chunk, so nothing to do 293 return serialized_null; 294 } 295 #else // INCLUDE_JVMCI 296 // Look in previously shared scopes first: 297 DIR_Chunk* ms = ns->find_match(_shared_chunks, 0, this); 298 if (ms != NULL) { 299 NOT_PRODUCT(++dir_stats.chunks_reshared); 300 assert(ns+1 == _next_chunk, ""); 301 _next_chunk = ns; 302 return ms->offset(); 303 } 304 305 // Look in recently encountered scopes next: 306 const int MAX_RECENT = 50; 307 int start_index = _all_chunks->length() - MAX_RECENT; 308 if (start_index < 0) start_index = 0; 309 ms = ns->find_match(_all_chunks, start_index, this); 310 if (ms != NULL) { 311 NOT_PRODUCT(++dir_stats.chunks_shared); 312 // Searching in _all_chunks is limited to a window, 313 // but searching in _shared_chunks is unlimited. 314 _shared_chunks->append(ms); 315 assert(ns+1 == _next_chunk, ""); 316 _next_chunk = ns; 317 return ms->offset(); 318 } 319 320 // No match. Add this guy to the list, in hopes of future shares. 321 _all_chunks->append(ns); 322 return serialized_null; 323 #endif // INCLUDE_JVMCI 324 } 325 326 327 // must call add_safepoint before: it sets PcDesc and this routine uses 328 // the last PcDesc set 329 void DebugInformationRecorder::describe_scope(int pc_offset, 330 const methodHandle& methodH, 331 ciMethod* method, 332 int bci, 333 bool reexecute, 334 bool rethrow_exception, 335 bool is_method_handle_invoke, 336 bool return_oop, 337 DebugToken* locals, 338 DebugToken* expressions, 339 DebugToken* monitors) { 340 assert(_recording_state != rs_null, "nesting of recording calls"); 341 PcDesc* last_pd = last_pc(); 342 assert(last_pd->pc_offset() == pc_offset, "must be last pc"); 343 int sender_stream_offset = last_pd->scope_decode_offset(); 344 // update the stream offset of current pc desc 345 int stream_offset = stream()->position(); 346 last_pd->set_scope_decode_offset(stream_offset); 347 348 // Record flags into pcDesc. 349 last_pd->set_should_reexecute(reexecute); 350 last_pd->set_rethrow_exception(rethrow_exception); 351 last_pd->set_is_method_handle_invoke(is_method_handle_invoke); 352 last_pd->set_return_oop(return_oop); 353 354 // serialize sender stream offest 355 stream()->write_int(sender_stream_offset); 356 357 // serialize scope 358 Metadata* method_enc; 359 if (method != NULL) { 360 method_enc = method->constant_encoding(); 361 } else if (methodH.not_null()) { 362 method_enc = methodH(); 363 } else { 364 method_enc = NULL; 365 } 366 int method_enc_index = oop_recorder()->find_index(method_enc); 367 stream()->write_int(method_enc_index); 368 stream()->write_bci(bci); 369 assert(method == NULL || 370 (method->is_native() && bci == 0) || 371 (!method->is_native() && 0 <= bci && bci < method->code_size()) || 372 bci == -1, "illegal bci"); 373 374 // serialize the locals/expressions/monitors 375 stream()->write_int((intptr_t) locals); 376 stream()->write_int((intptr_t) expressions); 377 stream()->write_int((intptr_t) monitors); 378 379 // Here's a tricky bit. We just wrote some bytes. 380 // Wouldn't it be nice to find that we had already 381 // written those same bytes somewhere else? 382 // If we get lucky this way, reset the stream 383 // and reuse the old bytes. By the way, this 384 // trick not only shares parent scopes, but also 385 // compresses equivalent non-safepoint PcDescs. 386 int shared_stream_offset = find_sharable_decode_offset(stream_offset); 387 if (shared_stream_offset != serialized_null) { 388 stream()->set_position(stream_offset); 389 last_pd->set_scope_decode_offset(shared_stream_offset); 390 } 391 } 392 393 void DebugInformationRecorder::dump_object_pool(GrowableArray<ScopeValue*>* objects) { 394 guarantee( _pcs_length > 0, "safepoint must exist before describing scopes"); 395 PcDesc* last_pd = &_pcs[_pcs_length-1]; 396 if (objects != NULL) { 397 for (int i = objects->length() - 1; i >= 0; i--) { 398 objects->at(i)->as_ObjectValue()->set_visited(false); 399 } 400 } 401 int offset = serialize_scope_values(objects); 402 last_pd->set_obj_decode_offset(offset); 403 } 404 405 void DebugInformationRecorder::end_scopes(int pc_offset, bool is_safepoint) { 406 assert(_recording_state == (is_safepoint? rs_safepoint: rs_non_safepoint), 407 "nesting of recording calls"); 408 debug_only(_recording_state = rs_null); 409 410 // Try to compress away an equivalent non-safepoint predecessor. 411 // (This only works because we have previously recognized redundant 412 // scope trees and made them use a common scope_decode_offset.) 413 if (_pcs_length >= 2 && recording_non_safepoints()) { 414 PcDesc* last = last_pc(); 415 PcDesc* prev = prev_pc(); 416 // If prev is (a) not a safepoint and (b) has the same 417 // stream pointer, then it can be coalesced into the last. 418 // This is valid because non-safepoints are only sought 419 // with pc_desc_near, which (when it misses prev) will 420 // search forward until it finds last. 421 // In addition, it does not matter if the last PcDesc 422 // is for a safepoint or not. 423 if (_prev_safepoint_pc < prev->pc_offset() && prev->is_same_info(last)) { 424 assert(prev == last-1, "sane"); 425 prev->set_pc_offset(pc_offset); 426 _pcs_length -= 1; 427 NOT_PRODUCT(++dir_stats.chunks_elided); 428 } 429 } 430 431 // We have just recorded this safepoint. 432 // Remember it in case the previous paragraph needs to know. 433 if (is_safepoint) { 434 _prev_safepoint_pc = pc_offset; 435 } 436 } 437 438 #ifdef ASSERT 439 bool DebugInformationRecorder::recorders_frozen() { 440 return _oop_recorder->is_complete() || _oop_recorder->is_complete(); 441 } 442 443 void DebugInformationRecorder::mark_recorders_frozen() { 444 _oop_recorder->freeze(); 445 } 446 #endif // PRODUCT 447 448 DebugToken* DebugInformationRecorder::create_scope_values(GrowableArray<ScopeValue*>* values) { 449 assert(!recorders_frozen(), "not frozen yet"); 450 return (DebugToken*) (intptr_t) serialize_scope_values(values); 451 } 452 453 454 DebugToken* DebugInformationRecorder::create_monitor_values(GrowableArray<MonitorValue*>* monitors) { 455 assert(!recorders_frozen(), "not frozen yet"); 456 return (DebugToken*) (intptr_t) serialize_monitor_values(monitors); 457 } 458 459 460 int DebugInformationRecorder::data_size() { 461 debug_only(mark_recorders_frozen()); // mark it "frozen" for asserts 462 return _stream->position(); 463 } 464 465 466 int DebugInformationRecorder::pcs_size() { 467 debug_only(mark_recorders_frozen()); // mark it "frozen" for asserts 468 if (last_pc()->pc_offset() != PcDesc::upper_offset_limit) 469 add_new_pc_offset(PcDesc::upper_offset_limit); 470 return _pcs_length * sizeof(PcDesc); 471 } 472 473 474 void DebugInformationRecorder::copy_to(nmethod* nm) { 475 nm->copy_scopes_data(stream()->buffer(), stream()->position()); 476 nm->copy_scopes_pcs(_pcs, _pcs_length); 477 } 478 479 480 void DebugInformationRecorder::verify(const nmethod* code) { 481 Unimplemented(); 482 } 483 484 #ifndef PRODUCT 485 void DebugInformationRecorder::print_statistics() { 486 dir_stats.print(); 487 } 488 #endif //PRODUCT