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