src/share/vm/prims/jvmtiCodeBlobEvents.cpp

Mon, 03 Feb 2014 15:24:20 +0100

author
sla
date
Mon, 03 Feb 2014 15:24:20 +0100
changeset 6327
fd07a7e4222b
parent 6310
22b3b2f888bc
child 6876
710a3c8b516e
permissions
-rw-r--r--

8033126: Can't call default methods from JNI
Reviewed-by: dholmes, acorn, kamg

duke@435 1 /*
mikael@4153 2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "code/codeBlob.hpp"
stefank@2314 27 #include "code/codeCache.hpp"
stefank@2314 28 #include "code/scopeDesc.hpp"
sspitsyn@6310 29 #include "code/vtableStubs.hpp"
stefank@2314 30 #include "memory/resourceArea.hpp"
stefank@2314 31 #include "oops/oop.inline.hpp"
stefank@2314 32 #include "prims/jvmtiCodeBlobEvents.hpp"
stefank@2314 33 #include "prims/jvmtiExport.hpp"
stefank@2314 34 #include "runtime/handles.hpp"
stefank@2314 35 #include "runtime/handles.inline.hpp"
stefank@2314 36 #include "runtime/vmThread.hpp"
duke@435 37
duke@435 38 // Support class to collect a list of the non-nmethod CodeBlobs in
duke@435 39 // the CodeCache.
duke@435 40 //
duke@435 41 // This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc
duke@435 42 // describes a single CodeBlob in the CodeCache. Note that collection is
duke@435 43 // done to a static list - this is because CodeCache::blobs_do is defined
duke@435 44 // as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires
duke@435 45 // a C or static method.
duke@435 46 //
duke@435 47 // Usage :-
duke@435 48 //
duke@435 49 // CodeBlobCollector collector;
duke@435 50 //
duke@435 51 // collector.collect();
duke@435 52 // JvmtiCodeBlobDesc* blob = collector.first();
duke@435 53 // while (blob != NULL) {
duke@435 54 // :
duke@435 55 // blob = collector.next();
duke@435 56 // }
duke@435 57 //
duke@435 58
duke@435 59 class CodeBlobCollector : StackObj {
duke@435 60 private:
duke@435 61 GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected blobs
duke@435 62 int _pos; // iterator position
duke@435 63
duke@435 64 // used during a collection
duke@435 65 static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs;
duke@435 66 static void do_blob(CodeBlob* cb);
sspitsyn@6310 67 static void do_vtable_stub(VtableStub* vs);
duke@435 68 public:
duke@435 69 CodeBlobCollector() {
duke@435 70 _code_blobs = NULL;
duke@435 71 _pos = -1;
duke@435 72 }
duke@435 73 ~CodeBlobCollector() {
duke@435 74 if (_code_blobs != NULL) {
duke@435 75 for (int i=0; i<_code_blobs->length(); i++) {
duke@435 76 FreeHeap(_code_blobs->at(i));
duke@435 77 }
duke@435 78 delete _code_blobs;
duke@435 79 }
duke@435 80 }
duke@435 81
duke@435 82 // collect list of code blobs in the cache
duke@435 83 void collect();
duke@435 84
duke@435 85 // iteration support - return first code blob
duke@435 86 JvmtiCodeBlobDesc* first() {
duke@435 87 assert(_code_blobs != NULL, "not collected");
duke@435 88 if (_code_blobs->length() == 0) {
duke@435 89 return NULL;
duke@435 90 }
duke@435 91 _pos = 0;
duke@435 92 return _code_blobs->at(0);
duke@435 93 }
duke@435 94
duke@435 95 // iteration support - return next code blob
duke@435 96 JvmtiCodeBlobDesc* next() {
duke@435 97 assert(_pos >= 0, "iteration not started");
duke@435 98 if (_pos+1 >= _code_blobs->length()) {
duke@435 99 return NULL;
duke@435 100 }
duke@435 101 return _code_blobs->at(++_pos);
duke@435 102 }
duke@435 103
duke@435 104 };
duke@435 105
duke@435 106 // used during collection
duke@435 107 GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs;
duke@435 108
duke@435 109
duke@435 110 // called for each CodeBlob in the CodeCache
duke@435 111 //
duke@435 112 // This function filters out nmethods as it is only interested in
duke@435 113 // other CodeBlobs. This function also filters out CodeBlobs that have
duke@435 114 // a duplicate starting address as previous blobs. This is needed to
duke@435 115 // handle the case where multiple stubs are generated into a single
duke@435 116 // BufferBlob.
duke@435 117
duke@435 118 void CodeBlobCollector::do_blob(CodeBlob* cb) {
duke@435 119
duke@435 120 // ignore nmethods
duke@435 121 if (cb->is_nmethod()) {
duke@435 122 return;
duke@435 123 }
sspitsyn@6310 124 // exclude VtableStubs, which are processed separately
sspitsyn@6310 125 if (cb->is_buffer_blob() && strcmp(cb->name(), "vtable chunks") == 0) {
sspitsyn@6310 126 return;
sspitsyn@6310 127 }
duke@435 128
duke@435 129 // check if this starting address has been seen already - the
duke@435 130 // assumption is that stubs are inserted into the list before the
duke@435 131 // enclosing BufferBlobs.
twisti@2103 132 address addr = cb->code_begin();
duke@435 133 for (int i=0; i<_global_code_blobs->length(); i++) {
duke@435 134 JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i);
duke@435 135 if (addr == scb->code_begin()) {
duke@435 136 return;
duke@435 137 }
duke@435 138 }
duke@435 139
duke@435 140 // record the CodeBlob details as a JvmtiCodeBlobDesc
twisti@2103 141 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end());
duke@435 142 _global_code_blobs->append(scb);
duke@435 143 }
duke@435 144
sspitsyn@6310 145 // called for each VtableStub in VtableStubs
sspitsyn@6310 146
sspitsyn@6310 147 void CodeBlobCollector::do_vtable_stub(VtableStub* vs) {
sspitsyn@6310 148 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(vs->is_vtable_stub() ? "vtable stub" : "itable stub",
sspitsyn@6310 149 vs->code_begin(), vs->code_end());
sspitsyn@6310 150 _global_code_blobs->append(scb);
sspitsyn@6310 151 }
duke@435 152
duke@435 153 // collects a list of CodeBlobs in the CodeCache.
duke@435 154 //
duke@435 155 // The created list is growable array of JvmtiCodeBlobDesc - each one describes
duke@435 156 // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do
duke@435 157 // requires a a C or static function so we can't use an instance function. This
duke@435 158 // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock
duke@435 159 // to iterate over the code cache.
duke@435 160 //
duke@435 161 // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may
duke@435 162 // contain multiple stubs. As a profiler is interested in the stubs rather than
duke@435 163 // the enclosing container we first iterate over the stub code descriptors so
duke@435 164 // that the stubs go into the list first. do_blob will then filter out the
duke@435 165 // enclosing blobs if the starting address of the enclosing blobs matches the
duke@435 166 // starting address of first stub generated in the enclosing blob.
duke@435 167
duke@435 168 void CodeBlobCollector::collect() {
duke@435 169 assert_locked_or_safepoint(CodeCache_lock);
duke@435 170 assert(_global_code_blobs == NULL, "checking");
duke@435 171
duke@435 172 // create the global list
zgu@3900 173 _global_code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(50,true);
duke@435 174
duke@435 175 // iterate over the stub code descriptors and put them in the list first.
duke@435 176 int index = 0;
duke@435 177 StubCodeDesc* desc;
duke@435 178 while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) {
duke@435 179 _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
duke@435 180 }
duke@435 181
sspitsyn@6310 182 // Vtable stubs are not described with StubCodeDesc,
sspitsyn@6310 183 // process them separately
sspitsyn@6310 184 VtableStubs::vtable_stub_do(do_vtable_stub);
sspitsyn@6310 185
duke@435 186 // next iterate over all the non-nmethod code blobs and add them to
duke@435 187 // the list - as noted above this will filter out duplicates and
duke@435 188 // enclosing blobs.
duke@435 189 CodeCache::blobs_do(do_blob);
duke@435 190
duke@435 191 // make the global list the instance list so that it can be used
duke@435 192 // for other iterations.
duke@435 193 _code_blobs = _global_code_blobs;
duke@435 194 _global_code_blobs = NULL;
duke@435 195 }
duke@435 196
duke@435 197
duke@435 198 // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.
duke@435 199
duke@435 200 jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {
duke@435 201 CodeBlobCollector collector;
duke@435 202
never@1988 203 // First collect all the code blobs. This has to be done in a
never@1988 204 // single pass over the code cache with CodeCache_lock held because
never@1988 205 // there isn't any safe way to iterate over regular CodeBlobs since
never@1988 206 // they can be freed at any point.
duke@435 207 {
duke@435 208 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
duke@435 209 collector.collect();
duke@435 210 }
duke@435 211
duke@435 212 // iterate over the collected list and post an event for each blob
duke@435 213 JvmtiCodeBlobDesc* blob = collector.first();
duke@435 214 while (blob != NULL) {
duke@435 215 JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());
duke@435 216 blob = collector.next();
duke@435 217 }
duke@435 218 return JVMTI_ERROR_NONE;
duke@435 219 }
duke@435 220
duke@435 221
duke@435 222 // Generate a COMPILED_METHOD_LOAD event for each nnmethod
duke@435 223 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {
duke@435 224 HandleMark hm;
duke@435 225
never@1988 226 // Walk the CodeCache notifying for live nmethods. The code cache
never@1988 227 // may be changing while this is happening which is ok since newly
never@1988 228 // created nmethod will notify normally and nmethods which are freed
never@1988 229 // can be safely skipped.
never@1988 230 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1988 231 nmethod* current = CodeCache::first_nmethod();
never@1988 232 while (current != NULL) {
never@1988 233 // Only notify for live nmethods
never@1988 234 if (current->is_alive()) {
never@2004 235 // Lock the nmethod so it can't be freed
never@2004 236 nmethodLocker nml(current);
never@2004 237
never@1988 238 // Don't hold the lock over the notify or jmethodID creation
never@1988 239 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1988 240 current->get_and_cache_jmethod_id();
never@1988 241 JvmtiExport::post_compiled_method_load(current);
never@1988 242 }
never@1988 243 current = CodeCache::next_nmethod(current);
duke@435 244 }
duke@435 245 return JVMTI_ERROR_NONE;
duke@435 246 }
duke@435 247
duke@435 248
duke@435 249 // create a C-heap allocated address location map for an nmethod
duke@435 250 void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,
duke@435 251 jvmtiAddrLocationMap** map_ptr,
duke@435 252 jint *map_length_ptr)
duke@435 253 {
duke@435 254 ResourceMark rm;
duke@435 255 jvmtiAddrLocationMap* map = NULL;
duke@435 256 jint map_length = 0;
duke@435 257
duke@435 258
duke@435 259 // Generate line numbers using PcDesc and ScopeDesc info
duke@435 260 methodHandle mh(nm->method());
duke@435 261
duke@435 262 if (!mh->is_native()) {
duke@435 263 PcDesc *pcd;
duke@435 264 int pcds_in_method;
duke@435 265
duke@435 266 pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());
zgu@3900 267 map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method, mtInternal);
duke@435 268
duke@435 269 address scopes_data = nm->scopes_data_begin();
duke@435 270 for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {
kvn@1688 271 ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute(), pcd->return_oop());
duke@435 272 ScopeDesc *sd = &sc0;
duke@435 273 while( !sd->is_top() ) { sd = sd->sender(); }
duke@435 274 int bci = sd->bci();
duke@435 275 if (bci != InvocationEntryBci) {
duke@435 276 assert(map_length < pcds_in_method, "checking");
duke@435 277 map[map_length].start_address = (const void*)pcd->real_pc(nm);
duke@435 278 map[map_length].location = bci;
duke@435 279 ++map_length;
duke@435 280 }
duke@435 281 }
duke@435 282 }
duke@435 283
duke@435 284 *map_ptr = map;
duke@435 285 *map_length_ptr = map_length;
duke@435 286 }

mercurial