annotate src/share/vm/code/debugInfoRec.cpp @ 900:9987d9d5eb0e

6833129: specjvm98 fails with NullPointerException in the compiler with -XX:DeoptimizeALot Summary: developed a reexecute logic for the interpreter to reexecute the bytecode when deopt happens Reviewed-by: kvn, never, jrose, twisti
author cfang
date Fri, 31 Jul 2009 17:12:33 -0700
parents a61af66fc99e
children 72088be4b386
rev   line source
duke@0 1 /*
duke@0 2 * Copyright 1998-2006 Sun Microsystems, Inc. All Rights Reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
duke@0 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@0 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@0 21 * have any questions.
duke@0 22 *
duke@0 23 */
duke@0 24
duke@0 25 # include "incls/_precompiled.incl"
duke@0 26 # include "incls/_debugInfoRec.cpp.incl"
duke@0 27
duke@0 28 // Private definition.
duke@0 29 // There is one DIR_Chunk for each scope and values array.
duke@0 30 // A chunk can potentially be used more than once.
duke@0 31 // We keep track of these chunks in order to detect
duke@0 32 // repetition and enable sharing.
duke@0 33 class DIR_Chunk {
duke@0 34 friend class DebugInformationRecorder;
duke@0 35 int _offset; // location in the stream of this scope
duke@0 36 int _length; // number of bytes in the stream
duke@0 37 int _hash; // hash of stream bytes (for quicker reuse)
duke@0 38
duke@0 39 void* operator new(size_t ignore, DebugInformationRecorder* dir) {
duke@0 40 assert(ignore == sizeof(DIR_Chunk), "");
duke@0 41 if (dir->_next_chunk >= dir->_next_chunk_limit) {
duke@0 42 const int CHUNK = 100;
duke@0 43 dir->_next_chunk = NEW_RESOURCE_ARRAY(DIR_Chunk, CHUNK);
duke@0 44 dir->_next_chunk_limit = dir->_next_chunk + CHUNK;
duke@0 45 }
duke@0 46 return dir->_next_chunk++;
duke@0 47 }
duke@0 48
duke@0 49 DIR_Chunk(int offset, int length, DebugInformationRecorder* dir) {
duke@0 50 _offset = offset;
duke@0 51 _length = length;
duke@0 52 unsigned int hash = 0;
duke@0 53 address p = dir->stream()->buffer() + _offset;
duke@0 54 for (int i = 0; i < length; i++) {
duke@0 55 if (i == 6) break;
duke@0 56 hash *= 127;
duke@0 57 hash += p[i];
duke@0 58 }
duke@0 59 _hash = hash;
duke@0 60 }
duke@0 61
duke@0 62 DIR_Chunk* find_match(GrowableArray<DIR_Chunk*>* arr,
duke@0 63 int start_index,
duke@0 64 DebugInformationRecorder* dir) {
duke@0 65 int end_index = arr->length();
duke@0 66 int hash = this->_hash, length = this->_length;
duke@0 67 address buf = dir->stream()->buffer();
duke@0 68 for (int i = end_index; --i >= start_index; ) {
duke@0 69 DIR_Chunk* that = arr->at(i);
duke@0 70 if (hash == that->_hash &&
duke@0 71 length == that->_length &&
duke@0 72 0 == memcmp(buf + this->_offset, buf + that->_offset, length)) {
duke@0 73 return that;
duke@0 74 }
duke@0 75 }
duke@0 76 return NULL;
duke@0 77 }
duke@0 78 };
duke@0 79
duke@0 80 static inline bool compute_recording_non_safepoints() {
duke@0 81 if (JvmtiExport::should_post_compiled_method_load()
duke@0 82 && FLAG_IS_DEFAULT(DebugNonSafepoints)) {
duke@0 83 // The default value of this flag is taken to be true,
duke@0 84 // if JVMTI is looking at nmethod codes.
duke@0 85 // We anticipate that JVMTI may wish to participate in profiling.
duke@0 86 return true;
duke@0 87 }
duke@0 88
duke@0 89 // If the flag is set manually, use it, whether true or false.
duke@0 90 // Otherwise, if JVMTI is not in the picture, use the default setting.
duke@0 91 // (This is true in debug, just for the exercise, false in product mode.)
duke@0 92 return DebugNonSafepoints;
duke@0 93 }
duke@0 94
duke@0 95 DebugInformationRecorder::DebugInformationRecorder(OopRecorder* oop_recorder)
duke@0 96 : _recording_non_safepoints(compute_recording_non_safepoints())
duke@0 97 {
duke@0 98 _pcs_size = 100;
duke@0 99 _pcs = NEW_RESOURCE_ARRAY(PcDesc, _pcs_size);
duke@0 100 _pcs_length = 0;
duke@0 101
duke@0 102 _prev_safepoint_pc = PcDesc::lower_offset_limit;
duke@0 103
duke@0 104 _stream = new DebugInfoWriteStream(this, 10 * K);
duke@0 105 // make sure that there is no stream_decode_offset that is zero
duke@0 106 _stream->write_byte((jbyte)0xFF);
duke@0 107
duke@0 108 // make sure that we can distinguish the value "serialized_null" from offsets
duke@0 109 assert(_stream->position() > serialized_null, "sanity");
duke@0 110
duke@0 111 _oop_recorder = oop_recorder;
duke@0 112
duke@0 113 _all_chunks = new GrowableArray<DIR_Chunk*>(300);
duke@0 114 _shared_chunks = new GrowableArray<DIR_Chunk*>(30);
duke@0 115 _next_chunk = _next_chunk_limit = NULL;
duke@0 116
duke@0 117 add_new_pc_offset(PcDesc::lower_offset_limit); // sentinel record
duke@0 118
duke@0 119 debug_only(_recording_state = rs_null);
duke@0 120 }
duke@0 121
duke@0 122
duke@0 123 void DebugInformationRecorder::add_oopmap(int pc_offset, OopMap* map) {
duke@0 124 // !!!!! Preserve old style handling of oopmaps for now
duke@0 125 _oopmaps->add_gc_map(pc_offset, map);
duke@0 126 }
duke@0 127
duke@0 128 void DebugInformationRecorder::add_safepoint(int pc_offset, OopMap* map) {
duke@0 129 assert(!_oop_recorder->is_complete(), "not frozen yet");
duke@0 130 // Store the new safepoint
duke@0 131
duke@0 132 // Add the oop map
duke@0 133 add_oopmap(pc_offset, map);
duke@0 134
duke@0 135 add_new_pc_offset(pc_offset);
duke@0 136
duke@0 137 assert(_recording_state == rs_null, "nesting of recording calls");
duke@0 138 debug_only(_recording_state = rs_safepoint);
duke@0 139 }
duke@0 140
duke@0 141 void DebugInformationRecorder::add_non_safepoint(int pc_offset) {
duke@0 142 assert(!_oop_recorder->is_complete(), "not frozen yet");
duke@0 143 assert(_recording_non_safepoints, "must be recording non-safepoints");
duke@0 144
duke@0 145 add_new_pc_offset(pc_offset);
duke@0 146
duke@0 147 assert(_recording_state == rs_null, "nesting of recording calls");
duke@0 148 debug_only(_recording_state = rs_non_safepoint);
duke@0 149 }
duke@0 150
duke@0 151 void DebugInformationRecorder::add_new_pc_offset(int pc_offset) {
duke@0 152 assert(_pcs_length == 0 || last_pc()->pc_offset() < pc_offset,
duke@0 153 "must specify a new, larger pc offset");
duke@0 154
duke@0 155 // add the pcdesc
duke@0 156 if (_pcs_length == _pcs_size) {
duke@0 157 // Expand
duke@0 158 int new_pcs_size = _pcs_size * 2;
duke@0 159 PcDesc* new_pcs = NEW_RESOURCE_ARRAY(PcDesc, new_pcs_size);
duke@0 160 for (int index = 0; index < _pcs_length; index++) {
duke@0 161 new_pcs[index] = _pcs[index];
duke@0 162 }
duke@0 163 _pcs_size = new_pcs_size;
duke@0 164 _pcs = new_pcs;
duke@0 165 }
duke@0 166 assert(_pcs_size > _pcs_length, "There must be room for after expanding");
duke@0 167
duke@0 168 _pcs[_pcs_length++] = PcDesc(pc_offset, DebugInformationRecorder::serialized_null,
duke@0 169 DebugInformationRecorder::serialized_null);
duke@0 170 }
duke@0 171
duke@0 172
duke@0 173 int DebugInformationRecorder::serialize_monitor_values(GrowableArray<MonitorValue*>* monitors) {
duke@0 174 if (monitors == NULL || monitors->is_empty()) return DebugInformationRecorder::serialized_null;
duke@0 175 assert(_recording_state == rs_safepoint, "must be recording a safepoint");
duke@0 176 int result = stream()->position();
duke@0 177 stream()->write_int(monitors->length());
duke@0 178 for (int index = 0; index < monitors->length(); index++) {
duke@0 179 monitors->at(index)->write_on(stream());
duke@0 180 }
duke@0 181 assert(result != serialized_null, "sanity");
duke@0 182
duke@0 183 // (See comment below on DebugInformationRecorder::describe_scope.)
duke@0 184 int shared_result = find_sharable_decode_offset(result);
duke@0 185 if (shared_result != serialized_null) {
duke@0 186 stream()->set_position(result);
duke@0 187 result = shared_result;
duke@0 188 }
duke@0 189
duke@0 190 return result;
duke@0 191 }
duke@0 192
duke@0 193
duke@0 194 int DebugInformationRecorder::serialize_scope_values(GrowableArray<ScopeValue*>* values) {
duke@0 195 if (values == NULL || values->is_empty()) return DebugInformationRecorder::serialized_null;
duke@0 196 assert(_recording_state == rs_safepoint, "must be recording a safepoint");
duke@0 197 int result = stream()->position();
duke@0 198 assert(result != serialized_null, "sanity");
duke@0 199 stream()->write_int(values->length());
duke@0 200 for (int index = 0; index < values->length(); index++) {
duke@0 201 values->at(index)->write_on(stream());
duke@0 202 }
duke@0 203
duke@0 204 // (See comment below on DebugInformationRecorder::describe_scope.)
duke@0 205 int shared_result = find_sharable_decode_offset(result);
duke@0 206 if (shared_result != serialized_null) {
duke@0 207 stream()->set_position(result);
duke@0 208 result = shared_result;
duke@0 209 }
duke@0 210
duke@0 211 return result;
duke@0 212 }
duke@0 213
duke@0 214
duke@0 215 #ifndef PRODUCT
duke@0 216 // These variables are put into one block to reduce relocations
duke@0 217 // and make it simpler to print from the debugger.
duke@0 218 static
duke@0 219 struct dir_stats_struct {
duke@0 220 int chunks_queried;
duke@0 221 int chunks_shared;
duke@0 222 int chunks_reshared;
duke@0 223 int chunks_elided;
duke@0 224
duke@0 225 void print() {
duke@0 226 tty->print_cr("Debug Data Chunks: %d, shared %d+%d, non-SP's elided %d",
duke@0 227 chunks_queried,
duke@0 228 chunks_shared, chunks_reshared,
duke@0 229 chunks_elided);
duke@0 230 }
duke@0 231 } dir_stats;
duke@0 232 #endif //PRODUCT
duke@0 233
duke@0 234
duke@0 235 int DebugInformationRecorder::find_sharable_decode_offset(int stream_offset) {
duke@0 236 // Only pull this trick if non-safepoint recording
duke@0 237 // is enabled, for now.
duke@0 238 if (!recording_non_safepoints())
duke@0 239 return serialized_null;
duke@0 240
duke@0 241 NOT_PRODUCT(++dir_stats.chunks_queried);
duke@0 242 int stream_length = stream()->position() - stream_offset;
duke@0 243 assert(stream_offset != serialized_null, "should not be null");
duke@0 244 assert(stream_length != 0, "should not be empty");
duke@0 245
duke@0 246 DIR_Chunk* ns = new(this) DIR_Chunk(stream_offset, stream_length, this);
duke@0 247
duke@0 248 // Look in previously shared scopes first:
duke@0 249 DIR_Chunk* ms = ns->find_match(_shared_chunks, 0, this);
duke@0 250 if (ms != NULL) {
duke@0 251 NOT_PRODUCT(++dir_stats.chunks_reshared);
duke@0 252 assert(ns+1 == _next_chunk, "");
duke@0 253 _next_chunk = ns;
duke@0 254 return ms->_offset;
duke@0 255 }
duke@0 256
duke@0 257 // Look in recently encountered scopes next:
duke@0 258 const int MAX_RECENT = 50;
duke@0 259 int start_index = _all_chunks->length() - MAX_RECENT;
duke@0 260 if (start_index < 0) start_index = 0;
duke@0 261 ms = ns->find_match(_all_chunks, start_index, this);
duke@0 262 if (ms != NULL) {
duke@0 263 NOT_PRODUCT(++dir_stats.chunks_shared);
duke@0 264 // Searching in _all_chunks is limited to a window,
duke@0 265 // but searching in _shared_chunks is unlimited.
duke@0 266 _shared_chunks->append(ms);
duke@0 267 assert(ns+1 == _next_chunk, "");
duke@0 268 _next_chunk = ns;
duke@0 269 return ms->_offset;
duke@0 270 }
duke@0 271
duke@0 272 // No match. Add this guy to the list, in hopes of future shares.
duke@0 273 _all_chunks->append(ns);
duke@0 274 return serialized_null;
duke@0 275 }
duke@0 276
duke@0 277
duke@0 278 // must call add_safepoint before: it sets PcDesc and this routine uses
duke@0 279 // the last PcDesc set
duke@0 280 void DebugInformationRecorder::describe_scope(int pc_offset,
duke@0 281 ciMethod* method,
duke@0 282 int bci,
cfang@900 283 bool reexecute,
duke@0 284 DebugToken* locals,
duke@0 285 DebugToken* expressions,
duke@0 286 DebugToken* monitors) {
duke@0 287 assert(_recording_state != rs_null, "nesting of recording calls");
duke@0 288 PcDesc* last_pd = last_pc();
duke@0 289 assert(last_pd->pc_offset() == pc_offset, "must be last pc");
duke@0 290 int sender_stream_offset = last_pd->scope_decode_offset();
duke@0 291 // update the stream offset of current pc desc
duke@0 292 int stream_offset = stream()->position();
duke@0 293 last_pd->set_scope_decode_offset(stream_offset);
duke@0 294
duke@0 295 // serialize sender stream offest
duke@0 296 stream()->write_int(sender_stream_offset);
duke@0 297
duke@0 298 // serialize scope
duke@0 299 jobject method_enc = (method == NULL)? NULL: method->encoding();
duke@0 300 stream()->write_int(oop_recorder()->find_index(method_enc));
cfang@900 301 stream()->write_bci_and_reexecute(bci, reexecute);
duke@0 302 assert(method == NULL ||
duke@0 303 (method->is_native() && bci == 0) ||
duke@0 304 (!method->is_native() && 0 <= bci && bci < method->code_size()) ||
duke@0 305 bci == -1, "illegal bci");
duke@0 306
duke@0 307 // serialize the locals/expressions/monitors
duke@0 308 stream()->write_int((intptr_t) locals);
duke@0 309 stream()->write_int((intptr_t) expressions);
duke@0 310 stream()->write_int((intptr_t) monitors);
duke@0 311
duke@0 312 // Here's a tricky bit. We just wrote some bytes.
duke@0 313 // Wouldn't it be nice to find that we had already
duke@0 314 // written those same bytes somewhere else?
duke@0 315 // If we get lucky this way, reset the stream
duke@0 316 // and reuse the old bytes. By the way, this
duke@0 317 // trick not only shares parent scopes, but also
duke@0 318 // compresses equivalent non-safepoint PcDescs.
duke@0 319 int shared_stream_offset = find_sharable_decode_offset(stream_offset);
duke@0 320 if (shared_stream_offset != serialized_null) {
duke@0 321 stream()->set_position(stream_offset);
duke@0 322 last_pd->set_scope_decode_offset(shared_stream_offset);
duke@0 323 }
duke@0 324 }
duke@0 325
duke@0 326 void DebugInformationRecorder::dump_object_pool(GrowableArray<ScopeValue*>* objects) {
duke@0 327 guarantee( _pcs_length > 0, "safepoint must exist before describing scopes");
duke@0 328 PcDesc* last_pd = &_pcs[_pcs_length-1];
duke@0 329 if (objects != NULL) {
duke@0 330 for (int i = objects->length() - 1; i >= 0; i--) {
duke@0 331 ((ObjectValue*) objects->at(i))->set_visited(false);
duke@0 332 }
duke@0 333 }
duke@0 334 int offset = serialize_scope_values(objects);
duke@0 335 last_pd->set_obj_decode_offset(offset);
duke@0 336 }
duke@0 337
duke@0 338 void DebugInformationRecorder::end_scopes(int pc_offset, bool is_safepoint) {
duke@0 339 assert(_recording_state == (is_safepoint? rs_safepoint: rs_non_safepoint),
duke@0 340 "nesting of recording calls");
duke@0 341 debug_only(_recording_state = rs_null);
duke@0 342
duke@0 343 // Try to compress away an equivalent non-safepoint predecessor.
duke@0 344 // (This only works because we have previously recognized redundant
duke@0 345 // scope trees and made them use a common scope_decode_offset.)
duke@0 346 if (_pcs_length >= 2 && recording_non_safepoints()) {
duke@0 347 PcDesc* last = last_pc();
duke@0 348 PcDesc* prev = prev_pc();
duke@0 349 // If prev is (a) not a safepoint and (b) has the same
duke@0 350 // stream pointer, then it can be coalesced into the last.
duke@0 351 // This is valid because non-safepoints are only sought
duke@0 352 // with pc_desc_near, which (when it misses prev) will
duke@0 353 // search forward until it finds last.
duke@0 354 // In addition, it does not matter if the last PcDesc
duke@0 355 // is for a safepoint or not.
duke@0 356 if (_prev_safepoint_pc < prev->pc_offset() &&
duke@0 357 prev->scope_decode_offset() == last->scope_decode_offset()) {
duke@0 358 assert(prev == last-1, "sane");
duke@0 359 prev->set_pc_offset(pc_offset);
duke@0 360 _pcs_length -= 1;
duke@0 361 NOT_PRODUCT(++dir_stats.chunks_elided);
duke@0 362 }
duke@0 363 }
duke@0 364
duke@0 365 // We have just recorded this safepoint.
duke@0 366 // Remember it in case the previous paragraph needs to know.
duke@0 367 if (is_safepoint) {
duke@0 368 _prev_safepoint_pc = pc_offset;
duke@0 369 }
duke@0 370 }
duke@0 371
duke@0 372 DebugToken* DebugInformationRecorder::create_scope_values(GrowableArray<ScopeValue*>* values) {
duke@0 373 assert(!_oop_recorder->is_complete(), "not frozen yet");
duke@0 374 return (DebugToken*) (intptr_t) serialize_scope_values(values);
duke@0 375 }
duke@0 376
duke@0 377
duke@0 378 DebugToken* DebugInformationRecorder::create_monitor_values(GrowableArray<MonitorValue*>* monitors) {
duke@0 379 assert(!_oop_recorder->is_complete(), "not frozen yet");
duke@0 380 return (DebugToken*) (intptr_t) serialize_monitor_values(monitors);
duke@0 381 }
duke@0 382
duke@0 383
duke@0 384 int DebugInformationRecorder::data_size() {
duke@0 385 debug_only(_oop_recorder->oop_size()); // mark it "frozen" for asserts
duke@0 386 return _stream->position();
duke@0 387 }
duke@0 388
duke@0 389
duke@0 390 int DebugInformationRecorder::pcs_size() {
duke@0 391 debug_only(_oop_recorder->oop_size()); // mark it "frozen" for asserts
duke@0 392 if (last_pc()->pc_offset() != PcDesc::upper_offset_limit)
duke@0 393 add_new_pc_offset(PcDesc::upper_offset_limit);
duke@0 394 return _pcs_length * sizeof(PcDesc);
duke@0 395 }
duke@0 396
duke@0 397
duke@0 398 void DebugInformationRecorder::copy_to(nmethod* nm) {
duke@0 399 nm->copy_scopes_data(stream()->buffer(), stream()->position());
duke@0 400 nm->copy_scopes_pcs(_pcs, _pcs_length);
duke@0 401 }
duke@0 402
duke@0 403
duke@0 404 void DebugInformationRecorder::verify(const nmethod* code) {
duke@0 405 Unimplemented();
duke@0 406 }
duke@0 407
duke@0 408 #ifndef PRODUCT
duke@0 409 void DebugInformationRecorder::print_statistics() {
duke@0 410 dir_stats.print();
duke@0 411 }
duke@0 412 #endif //PRODUCT