duke@435: /* mikael@4153: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #include "precompiled.hpp" stefank@2314: #include "code/codeBlob.hpp" stefank@2314: #include "code/codeCache.hpp" stefank@2314: #include "code/scopeDesc.hpp" stefank@2314: #include "memory/resourceArea.hpp" stefank@2314: #include "oops/oop.inline.hpp" stefank@2314: #include "prims/jvmtiCodeBlobEvents.hpp" stefank@2314: #include "prims/jvmtiExport.hpp" stefank@2314: #include "runtime/handles.hpp" stefank@2314: #include "runtime/handles.inline.hpp" stefank@2314: #include "runtime/vmThread.hpp" duke@435: duke@435: // Support class to collect a list of the non-nmethod CodeBlobs in duke@435: // the CodeCache. duke@435: // duke@435: // This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc duke@435: // describes a single CodeBlob in the CodeCache. Note that collection is duke@435: // done to a static list - this is because CodeCache::blobs_do is defined duke@435: // as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires duke@435: // a C or static method. duke@435: // duke@435: // Usage :- duke@435: // duke@435: // CodeBlobCollector collector; duke@435: // duke@435: // collector.collect(); duke@435: // JvmtiCodeBlobDesc* blob = collector.first(); duke@435: // while (blob != NULL) { duke@435: // : duke@435: // blob = collector.next(); duke@435: // } duke@435: // duke@435: duke@435: class CodeBlobCollector : StackObj { duke@435: private: duke@435: GrowableArray* _code_blobs; // collected blobs duke@435: int _pos; // iterator position duke@435: duke@435: // used during a collection duke@435: static GrowableArray* _global_code_blobs; duke@435: static void do_blob(CodeBlob* cb); duke@435: public: duke@435: CodeBlobCollector() { duke@435: _code_blobs = NULL; duke@435: _pos = -1; duke@435: } duke@435: ~CodeBlobCollector() { duke@435: if (_code_blobs != NULL) { duke@435: for (int i=0; i<_code_blobs->length(); i++) { duke@435: FreeHeap(_code_blobs->at(i)); duke@435: } duke@435: delete _code_blobs; duke@435: } duke@435: } duke@435: duke@435: // collect list of code blobs in the cache duke@435: void collect(); duke@435: duke@435: // iteration support - return first code blob duke@435: JvmtiCodeBlobDesc* first() { duke@435: assert(_code_blobs != NULL, "not collected"); duke@435: if (_code_blobs->length() == 0) { duke@435: return NULL; duke@435: } duke@435: _pos = 0; duke@435: return _code_blobs->at(0); duke@435: } duke@435: duke@435: // iteration support - return next code blob duke@435: JvmtiCodeBlobDesc* next() { duke@435: assert(_pos >= 0, "iteration not started"); duke@435: if (_pos+1 >= _code_blobs->length()) { duke@435: return NULL; duke@435: } duke@435: return _code_blobs->at(++_pos); duke@435: } duke@435: duke@435: }; duke@435: duke@435: // used during collection duke@435: GrowableArray* CodeBlobCollector::_global_code_blobs; duke@435: duke@435: duke@435: // called for each CodeBlob in the CodeCache duke@435: // duke@435: // This function filters out nmethods as it is only interested in duke@435: // other CodeBlobs. This function also filters out CodeBlobs that have duke@435: // a duplicate starting address as previous blobs. This is needed to duke@435: // handle the case where multiple stubs are generated into a single duke@435: // BufferBlob. duke@435: duke@435: void CodeBlobCollector::do_blob(CodeBlob* cb) { duke@435: duke@435: // ignore nmethods duke@435: if (cb->is_nmethod()) { duke@435: return; duke@435: } duke@435: duke@435: // check if this starting address has been seen already - the duke@435: // assumption is that stubs are inserted into the list before the duke@435: // enclosing BufferBlobs. twisti@2103: address addr = cb->code_begin(); duke@435: for (int i=0; i<_global_code_blobs->length(); i++) { duke@435: JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i); duke@435: if (addr == scb->code_begin()) { duke@435: return; duke@435: } duke@435: } duke@435: duke@435: // record the CodeBlob details as a JvmtiCodeBlobDesc twisti@2103: JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end()); duke@435: _global_code_blobs->append(scb); duke@435: } duke@435: duke@435: duke@435: // collects a list of CodeBlobs in the CodeCache. duke@435: // duke@435: // The created list is growable array of JvmtiCodeBlobDesc - each one describes duke@435: // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do duke@435: // requires a a C or static function so we can't use an instance function. This duke@435: // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock duke@435: // to iterate over the code cache. duke@435: // duke@435: // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may duke@435: // contain multiple stubs. As a profiler is interested in the stubs rather than duke@435: // the enclosing container we first iterate over the stub code descriptors so duke@435: // that the stubs go into the list first. do_blob will then filter out the duke@435: // enclosing blobs if the starting address of the enclosing blobs matches the duke@435: // starting address of first stub generated in the enclosing blob. duke@435: duke@435: void CodeBlobCollector::collect() { duke@435: assert_locked_or_safepoint(CodeCache_lock); duke@435: assert(_global_code_blobs == NULL, "checking"); duke@435: duke@435: // create the global list zgu@3900: _global_code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(50,true); duke@435: duke@435: // iterate over the stub code descriptors and put them in the list first. duke@435: int index = 0; duke@435: StubCodeDesc* desc; duke@435: while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) { duke@435: _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end())); duke@435: } duke@435: duke@435: // next iterate over all the non-nmethod code blobs and add them to duke@435: // the list - as noted above this will filter out duplicates and duke@435: // enclosing blobs. duke@435: CodeCache::blobs_do(do_blob); duke@435: duke@435: // make the global list the instance list so that it can be used duke@435: // for other iterations. duke@435: _code_blobs = _global_code_blobs; duke@435: _global_code_blobs = NULL; duke@435: } duke@435: duke@435: duke@435: // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob. duke@435: duke@435: jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) { duke@435: CodeBlobCollector collector; duke@435: never@1988: // First collect all the code blobs. This has to be done in a never@1988: // single pass over the code cache with CodeCache_lock held because never@1988: // there isn't any safe way to iterate over regular CodeBlobs since never@1988: // they can be freed at any point. duke@435: { duke@435: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); duke@435: collector.collect(); duke@435: } duke@435: duke@435: // iterate over the collected list and post an event for each blob duke@435: JvmtiCodeBlobDesc* blob = collector.first(); duke@435: while (blob != NULL) { duke@435: JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end()); duke@435: blob = collector.next(); duke@435: } duke@435: return JVMTI_ERROR_NONE; duke@435: } duke@435: duke@435: duke@435: // Generate a COMPILED_METHOD_LOAD event for each nnmethod duke@435: jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) { duke@435: HandleMark hm; duke@435: never@1988: // Walk the CodeCache notifying for live nmethods. The code cache never@1988: // may be changing while this is happening which is ok since newly never@1988: // created nmethod will notify normally and nmethods which are freed never@1988: // can be safely skipped. never@1988: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); never@1988: nmethod* current = CodeCache::first_nmethod(); never@1988: while (current != NULL) { never@1988: // Only notify for live nmethods never@1988: if (current->is_alive()) { never@2004: // Lock the nmethod so it can't be freed never@2004: nmethodLocker nml(current); never@2004: never@1988: // Don't hold the lock over the notify or jmethodID creation never@1988: MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); never@1988: current->get_and_cache_jmethod_id(); never@1988: JvmtiExport::post_compiled_method_load(current); never@1988: } never@1988: current = CodeCache::next_nmethod(current); duke@435: } duke@435: return JVMTI_ERROR_NONE; duke@435: } duke@435: duke@435: duke@435: // create a C-heap allocated address location map for an nmethod duke@435: void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm, duke@435: jvmtiAddrLocationMap** map_ptr, duke@435: jint *map_length_ptr) duke@435: { duke@435: ResourceMark rm; duke@435: jvmtiAddrLocationMap* map = NULL; duke@435: jint map_length = 0; duke@435: duke@435: duke@435: // Generate line numbers using PcDesc and ScopeDesc info duke@435: methodHandle mh(nm->method()); duke@435: duke@435: if (!mh->is_native()) { duke@435: PcDesc *pcd; duke@435: int pcds_in_method; duke@435: duke@435: pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin()); zgu@3900: map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method, mtInternal); duke@435: duke@435: address scopes_data = nm->scopes_data_begin(); duke@435: for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) { kvn@1688: ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute(), pcd->return_oop()); duke@435: ScopeDesc *sd = &sc0; duke@435: while( !sd->is_top() ) { sd = sd->sender(); } duke@435: int bci = sd->bci(); duke@435: if (bci != InvocationEntryBci) { duke@435: assert(map_length < pcds_in_method, "checking"); duke@435: map[map_length].start_address = (const void*)pcd->real_pc(nm); duke@435: map[map_length].location = bci; duke@435: ++map_length; duke@435: } duke@435: } duke@435: } duke@435: duke@435: *map_ptr = map; duke@435: *map_length_ptr = map_length; duke@435: }