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