src/share/vm/code/debugInfoRec.cpp

Fri, 29 Jan 2010 08:33:24 -0800

author
twisti
date
Fri, 29 Jan 2010 08:33:24 -0800
changeset 1636
24128c2ffa87
parent 1570
e66fd840cb6b
child 1688
f70b0d9ab095
permissions
-rw-r--r--

6921339: backout 6917766
Reviewed-by: mr

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

mercurial