1 /*
   2  * Copyright (c) 2003, 2012, 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/codeBlob.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "code/scopeDesc.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "oops/oop.inline.hpp"
  31 #include "prims/jvmtiCodeBlobEvents.hpp"
  32 #include "prims/jvmtiExport.hpp"
  33 #include "runtime/handles.hpp"
  34 #include "runtime/handles.inline.hpp"
  35 #include "runtime/vmThread.hpp"
  36 
  37 // Support class to collect a list of the non-nmethod CodeBlobs in
  38 // the CodeCache.
  39 //
  40 // This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc
  41 // describes a single CodeBlob in the CodeCache. Note that collection is
  42 // done to a static list - this is because CodeCache::blobs_do is defined
  43 // as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires
  44 // a C or static method.
  45 //
  46 // Usage :-
  47 //
  48 // CodeBlobCollector collector;
  49 //
  50 // collector.collect();
  51 // JvmtiCodeBlobDesc* blob = collector.first();
  52 // while (blob != NULL) {
  53 //   :
  54 //   blob = collector.next();
  55 // }
  56 //
  57 
  58 class CodeBlobCollector : StackObj {
  59  private:
  60   GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs;   // collected blobs
  61   int _pos;                                         // iterator position
  62 
  63   // used during a collection
  64   static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs;
  65   static void do_blob(CodeBlob* cb);
  66  public:
  67   CodeBlobCollector() {
  68     _code_blobs = NULL;
  69     _pos = -1;
  70   }
  71   ~CodeBlobCollector() {
  72     if (_code_blobs != NULL) {
  73       for (int i=0; i<_code_blobs->length(); i++) {
  74         FreeHeap(_code_blobs->at(i));
  75       }
  76       delete _code_blobs;
  77     }
  78   }
  79 
  80   // collect list of code blobs in the cache
  81   void collect();
  82 
  83   // iteration support - return first code blob
  84   JvmtiCodeBlobDesc* first() {
  85     assert(_code_blobs != NULL, "not collected");
  86     if (_code_blobs->length() == 0) {
  87       return NULL;
  88     }
  89     _pos = 0;
  90     return _code_blobs->at(0);
  91   }
  92 
  93   // iteration support - return next code blob
  94   JvmtiCodeBlobDesc* next() {
  95     assert(_pos >= 0, "iteration not started");
  96     if (_pos+1 >= _code_blobs->length()) {
  97       return NULL;
  98     }
  99     return _code_blobs->at(++_pos);
 100   }
 101 
 102 };
 103 
 104 // used during collection
 105 GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs;
 106 
 107 
 108 // called for each CodeBlob in the CodeCache
 109 //
 110 // This function filters out nmethods as it is only interested in
 111 // other CodeBlobs. This function also filters out CodeBlobs that have
 112 // a duplicate starting address as previous blobs. This is needed to
 113 // handle the case where multiple stubs are generated into a single
 114 // BufferBlob.
 115 
 116 void CodeBlobCollector::do_blob(CodeBlob* cb) {
 117 
 118   // ignore nmethods
 119   if (cb->is_nmethod()) {
 120     return;
 121   }
 122 
 123   // check if this starting address has been seen already - the
 124   // assumption is that stubs are inserted into the list before the
 125   // enclosing BufferBlobs.
 126   address addr = cb->code_begin();
 127   for (int i=0; i<_global_code_blobs->length(); i++) {
 128     JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i);
 129     if (addr == scb->code_begin()) {
 130       return;
 131     }
 132   }
 133 
 134   // record the CodeBlob details as a JvmtiCodeBlobDesc
 135   JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end());
 136   _global_code_blobs->append(scb);
 137 }
 138 
 139 
 140 // collects a list of CodeBlobs in the CodeCache.
 141 //
 142 // The created list is growable array of JvmtiCodeBlobDesc - each one describes
 143 // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do
 144 // requires a a C or static function so we can't use an instance function. This
 145 // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock
 146 // to iterate over the code cache.
 147 //
 148 // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may
 149 // contain multiple stubs. As a profiler is interested in the stubs rather than
 150 // the enclosing container we first iterate over the stub code descriptors so
 151 // that the stubs go into the list first. do_blob will then filter out the
 152 // enclosing blobs if the starting address of the enclosing blobs matches the
 153 // starting address of first stub generated in the enclosing blob.
 154 
 155 void CodeBlobCollector::collect() {
 156   assert_locked_or_safepoint(CodeCache_lock);
 157   assert(_global_code_blobs == NULL, "checking");
 158 
 159   // create the global list
 160   _global_code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(50,true);
 161 
 162   // iterate over the stub code descriptors and put them in the list first.
 163   int index = 0;
 164   StubCodeDesc* desc;
 165   while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) {
 166     _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
 167   }
 168 
 169   // next iterate over all the non-nmethod code blobs and add them to
 170   // the list - as noted above this will filter out duplicates and
 171   // enclosing blobs.
 172   CodeCache::blobs_do(do_blob);
 173 
 174   // make the global list the instance list so that it can be used
 175   // for other iterations.
 176   _code_blobs = _global_code_blobs;
 177   _global_code_blobs = NULL;
 178 }
 179 
 180 
 181 // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.
 182 
 183 jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {
 184   CodeBlobCollector collector;
 185 
 186   // First collect all the code blobs.  This has to be done in a
 187   // single pass over the code cache with CodeCache_lock held because
 188   // there isn't any safe way to iterate over regular CodeBlobs since
 189   // they can be freed at any point.
 190   {
 191     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 192     collector.collect();
 193   }
 194 
 195   // iterate over the collected list and post an event for each blob
 196   JvmtiCodeBlobDesc* blob = collector.first();
 197   while (blob != NULL) {
 198     JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());
 199     blob = collector.next();
 200   }
 201   return JVMTI_ERROR_NONE;
 202 }
 203 
 204 
 205 // Generate a COMPILED_METHOD_LOAD event for each nnmethod
 206 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {
 207   HandleMark hm;
 208 
 209   // Walk the CodeCache notifying for live nmethods.  The code cache
 210   // may be changing while this is happening which is ok since newly
 211   // created nmethod will notify normally and nmethods which are freed
 212   // can be safely skipped.
 213   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 214   // Iterate over non-profiled and profiled nmethods
 215   for (int code_blob_type = CodeBlobType::MethodNonProfiled; code_blob_type <= CodeBlobType::MethodProfiled; ++code_blob_type) {
 216     // Only notify for live nmethods
 217     nmethod* current = (nmethod*) CodeCache::first_alive_blob(code_blob_type);
 218     while (current != NULL) {
 219       // Lock the nmethod so it can't be freed
 220       nmethodLocker nml(current);
 221 
 222       // Don't hold the lock over the notify or jmethodID creation
 223       MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 224       current->get_and_cache_jmethod_id();
 225       JvmtiExport::post_compiled_method_load(current);
 226 
 227       current = (nmethod*) CodeCache::next_alive_blob(current, code_blob_type);
 228     }
 229   }
 230   return JVMTI_ERROR_NONE;
 231 }
 232 
 233 
 234 // create a C-heap allocated address location map for an nmethod
 235 void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,
 236                                                         jvmtiAddrLocationMap** map_ptr,
 237                                                         jint *map_length_ptr)
 238 {
 239   ResourceMark rm;
 240   jvmtiAddrLocationMap* map = NULL;
 241   jint map_length = 0;
 242 
 243 
 244   // Generate line numbers using PcDesc and ScopeDesc info
 245   methodHandle mh(nm->method());
 246 
 247   if (!mh->is_native()) {
 248     PcDesc *pcd;
 249     int pcds_in_method;
 250 
 251     pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());
 252     map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method, mtInternal);
 253 
 254     address scopes_data = nm->scopes_data_begin();
 255     for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {
 256       ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute(), pcd->return_oop());
 257       ScopeDesc *sd  = &sc0;
 258       while( !sd->is_top() ) { sd = sd->sender(); }
 259       int bci = sd->bci();
 260       if (bci != InvocationEntryBci) {
 261         assert(map_length < pcds_in_method, "checking");
 262         map[map_length].start_address = (const void*)pcd->real_pc(nm);
 263         map[map_length].location = bci;
 264         ++map_length;
 265       }
 266     }
 267   }
 268 
 269   *map_ptr = map;
 270   *map_length_ptr = map_length;
 271 }