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