duke@435: /* trims@1907: * Copyright (c) 2003, 2009, 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: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_jvmtiCodeBlobEvents.cpp.incl" 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. duke@435: address addr = cb->instructions_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: // we must name the CodeBlob - some CodeBlobs already have names :- duke@435: // - stubs used by compiled code to call a (static) C++ runtime routine duke@435: // - non-relocatable machine code such as the interpreter, stubroutines, etc. duke@435: // - various singleton blobs duke@435: // duke@435: // others are unnamed so we create a name :- duke@435: // - OSR adapter (interpreter frame that has been on-stack replaced) duke@435: // - I2C and C2I adapters duke@435: const char* name = NULL; duke@435: if (cb->is_runtime_stub()) { duke@435: name = ((RuntimeStub*)cb)->name(); duke@435: } duke@435: if (cb->is_buffer_blob()) { duke@435: name = ((BufferBlob*)cb)->name(); duke@435: } duke@435: if (cb->is_deoptimization_stub() || cb->is_safepoint_stub()) { duke@435: name = ((SingletonBlob*)cb)->name(); duke@435: } duke@435: if (cb->is_uncommon_trap_stub() || cb->is_exception_stub()) { duke@435: name = ((SingletonBlob*)cb)->name(); duke@435: } duke@435: duke@435: // record the CodeBlob details as a JvmtiCodeBlobDesc duke@435: JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(name, cb->instructions_begin(), duke@435: cb->instructions_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 duke@435: _global_code_blobs = new (ResourceObj::C_HEAP) 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: duke@435: // first collect all the code blobs 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: // Support class to describe a nmethod in the CodeCache duke@435: duke@435: class nmethodDesc: public CHeapObj { duke@435: private: duke@435: methodHandle _method; duke@435: address _code_begin; duke@435: address _code_end; duke@435: jvmtiAddrLocationMap* _map; duke@435: jint _map_length; duke@435: public: duke@435: nmethodDesc(methodHandle method, address code_begin, address code_end, duke@435: jvmtiAddrLocationMap* map, jint map_length) { duke@435: _method = method; duke@435: _code_begin = code_begin; duke@435: _code_end = code_end; duke@435: _map = map; duke@435: _map_length = map_length; duke@435: } duke@435: methodHandle method() const { return _method; } duke@435: address code_begin() const { return _code_begin; } duke@435: address code_end() const { return _code_end; } duke@435: jvmtiAddrLocationMap* map() const { return _map; } duke@435: jint map_length() const { return _map_length; } duke@435: }; duke@435: duke@435: duke@435: // Support class to collect a list of the nmethod CodeBlobs in duke@435: // the CodeCache. duke@435: // duke@435: // Usage :- duke@435: // duke@435: // nmethodCollector 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: class nmethodCollector : StackObj { duke@435: private: duke@435: GrowableArray* _nmethods; // collect nmethods duke@435: int _pos; // iteration support duke@435: duke@435: // used during a collection duke@435: static GrowableArray* _global_nmethods; duke@435: static void do_nmethod(nmethod* nm); duke@435: public: duke@435: nmethodCollector() { duke@435: _nmethods = NULL; duke@435: _pos = -1; duke@435: } duke@435: ~nmethodCollector() { duke@435: if (_nmethods != NULL) { duke@435: for (int i=0; i<_nmethods->length(); i++) { duke@435: nmethodDesc* blob = _nmethods->at(i); duke@435: if (blob->map()!= NULL) { duke@435: FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, blob->map()); duke@435: } duke@435: } duke@435: delete _nmethods; duke@435: } duke@435: } duke@435: duke@435: // collect list of nmethods in the cache duke@435: void collect(); duke@435: duke@435: // iteration support - return first code blob duke@435: nmethodDesc* first() { duke@435: assert(_nmethods != NULL, "not collected"); duke@435: if (_nmethods->length() == 0) { duke@435: return NULL; duke@435: } duke@435: _pos = 0; duke@435: return _nmethods->at(0); duke@435: } duke@435: duke@435: // iteration support - return next code blob duke@435: nmethodDesc* next() { duke@435: assert(_pos >= 0, "iteration not started"); duke@435: if (_pos+1 >= _nmethods->length()) { duke@435: return NULL; duke@435: } duke@435: return _nmethods->at(++_pos); duke@435: } duke@435: }; duke@435: duke@435: // used during collection duke@435: GrowableArray* nmethodCollector::_global_nmethods; duke@435: duke@435: duke@435: // called for each nmethod in the CodeCache duke@435: // duke@435: // This function simply adds a descriptor for each nmethod to the global list. duke@435: duke@435: void nmethodCollector::do_nmethod(nmethod* nm) { duke@435: // ignore zombies duke@435: if (!nm->is_alive()) { duke@435: return; duke@435: } duke@435: duke@435: assert(nm->method() != NULL, "checking"); duke@435: duke@435: // create the location map for the nmethod. duke@435: jvmtiAddrLocationMap* map; duke@435: jint map_length; duke@435: JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &map, &map_length); duke@435: duke@435: // record the nmethod details duke@435: methodHandle mh(nm->method()); duke@435: nmethodDesc* snm = new nmethodDesc(mh, duke@435: nm->code_begin(), duke@435: nm->code_end(), duke@435: map, duke@435: map_length); duke@435: _global_nmethods->append(snm); duke@435: } duke@435: duke@435: // collects a list of nmethod in the CodeCache. duke@435: // duke@435: // The created list is growable array of nmethodDesc - each one describes duke@435: // a nmethod and includs its JVMTI address location map. duke@435: duke@435: void nmethodCollector::collect() { duke@435: assert_locked_or_safepoint(CodeCache_lock); duke@435: assert(_global_nmethods == NULL, "checking"); duke@435: duke@435: // create the list duke@435: _global_nmethods = new (ResourceObj::C_HEAP) GrowableArray(100,true); duke@435: duke@435: // any a descriptor for each nmethod to the list. duke@435: CodeCache::nmethods_do(do_nmethod); duke@435: duke@435: // make the list the instance list duke@435: _nmethods = _global_nmethods; duke@435: _global_nmethods = NULL; duke@435: } duke@435: duke@435: // Generate a COMPILED_METHOD_LOAD event for each nnmethod duke@435: duke@435: jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) { duke@435: HandleMark hm; duke@435: nmethodCollector collector; duke@435: duke@435: // first collect all nmethods 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 list and post an event for each nmethod duke@435: nmethodDesc* nm_desc = collector.first(); duke@435: while (nm_desc != NULL) { duke@435: methodOop method = nm_desc->method()(); duke@435: jmethodID mid = method->jmethod_id(); duke@435: assert(mid != NULL, "checking"); duke@435: JvmtiExport::post_compiled_method_load(env, mid, duke@435: (jint)(nm_desc->code_end() - nm_desc->code_begin()), duke@435: nm_desc->code_begin(), nm_desc->map_length(), duke@435: nm_desc->map()); duke@435: nm_desc = collector.next(); 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()); duke@435: map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method); 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: }