1 /*
2 * Copyright (c) 2003, 2020, 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 */
24
25 #include "precompiled.hpp"
26 #include "code/codeBlob.hpp"
27 #include "code/codeCache.hpp"
28 #include "code/scopeDesc.hpp"
29 #include "code/vtableStubs.hpp"
30 #include "memory/allocation.inline.hpp"
31 #include "memory/resourceArea.hpp"
32 #include "oops/oop.inline.hpp"
33 #include "prims/jvmtiCodeBlobEvents.hpp"
34 #include "prims/jvmtiExport.hpp"
35 #include "prims/jvmtiThreadState.inline.hpp"
36 #include "runtime/handles.inline.hpp"
37 #include "runtime/safepointVerifiers.hpp"
38 #include "runtime/vmThread.hpp"
39
40 // Support class to collect a list of the non-nmethod CodeBlobs in
41 // the CodeCache.
42 //
43 // This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc
44 // describes a single CodeBlob in the CodeCache. Note that collection is
45 // done to a static list - this is because CodeCache::blobs_do is defined
46 // as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires
47 // a C or static method.
48 //
49 // Usage :-
50 //
51 // CodeBlobCollector collector;
52 //
53 // collector.collect();
54 // JvmtiCodeBlobDesc* blob = collector.first();
55 // while (blob != NULL) {
56 // :
57 // blob = collector.next();
58 // }
59 //
60
61 class CodeBlobCollector : StackObj {
62 private:
63 GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected blobs
64 int _pos; // iterator position
65
66 // used during a collection
67 static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs;
68 static void do_blob(CodeBlob* cb);
69 static void do_vtable_stub(VtableStub* vs);
70 public:
CodeBlobCollector()71 CodeBlobCollector() {
72 _code_blobs = NULL;
73 _pos = -1;
74 }
~CodeBlobCollector()75 ~CodeBlobCollector() {
76 if (_code_blobs != NULL) {
77 for (int i=0; i<_code_blobs->length(); i++) {
78 FreeHeap(_code_blobs->at(i));
79 }
80 delete _code_blobs;
81 }
82 }
83
84 // collect list of code blobs in the cache
85 void collect();
86
87 // iteration support - return first code blob
first()88 JvmtiCodeBlobDesc* first() {
89 assert(_code_blobs != NULL, "not collected");
90 if (_code_blobs->length() == 0) {
91 return NULL;
92 }
93 _pos = 0;
94 return _code_blobs->at(0);
95 }
96
97 // iteration support - return next code blob
next()98 JvmtiCodeBlobDesc* next() {
99 assert(_pos >= 0, "iteration not started");
100 if (_pos+1 >= _code_blobs->length()) {
101 return NULL;
102 }
103 return _code_blobs->at(++_pos);
104 }
105
106 };
107
108 // used during collection
109 GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs;
110
111
112 // called for each CodeBlob in the CodeCache
113 //
114 // This function filters out nmethods as it is only interested in
115 // other CodeBlobs. This function also filters out CodeBlobs that have
116 // a duplicate starting address as previous blobs. This is needed to
117 // handle the case where multiple stubs are generated into a single
118 // BufferBlob.
119
do_blob(CodeBlob * cb)120 void CodeBlobCollector::do_blob(CodeBlob* cb) {
121
122 // ignore nmethods
123 if (cb->is_nmethod()) {
124 return;
125 }
126 // exclude VtableStubs, which are processed separately
127 if (cb->is_buffer_blob() && strcmp(cb->name(), "vtable chunks") == 0) {
128 return;
129 }
130
131 // check if this starting address has been seen already - the
132 // assumption is that stubs are inserted into the list before the
133 // enclosing BufferBlobs.
134 address addr = cb->code_begin();
135 for (int i=0; i<_global_code_blobs->length(); i++) {
136 JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i);
137 if (addr == scb->code_begin()) {
138 return;
139 }
140 }
141
142 // record the CodeBlob details as a JvmtiCodeBlobDesc
143 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end());
144 _global_code_blobs->append(scb);
145 }
146
147 // called for each VtableStub in VtableStubs
148
do_vtable_stub(VtableStub * vs)149 void CodeBlobCollector::do_vtable_stub(VtableStub* vs) {
150 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(vs->is_vtable_stub() ? "vtable stub" : "itable stub",
151 vs->code_begin(), vs->code_end());
152 _global_code_blobs->append(scb);
153 }
154
155 // collects a list of CodeBlobs in the CodeCache.
156 //
157 // The created list is growable array of JvmtiCodeBlobDesc - each one describes
158 // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do
159 // requires a a C or static function so we can't use an instance function. This
160 // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock
161 // to iterate over the code cache.
162 //
163 // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may
164 // contain multiple stubs. As a profiler is interested in the stubs rather than
165 // the enclosing container we first iterate over the stub code descriptors so
166 // that the stubs go into the list first. do_blob will then filter out the
167 // enclosing blobs if the starting address of the enclosing blobs matches the
168 // starting address of first stub generated in the enclosing blob.
169
collect()170 void CodeBlobCollector::collect() {
171 assert_locked_or_safepoint(CodeCache_lock);
172 assert(_global_code_blobs == NULL, "checking");
173
174 // create the global list
175 _global_code_blobs = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<JvmtiCodeBlobDesc*>(50, mtServiceability);
176
177 // iterate over the stub code descriptors and put them in the list first.
178 for (StubCodeDesc* desc = StubCodeDesc::first(); desc != NULL; desc = StubCodeDesc::next(desc)) {
179 _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
180 }
181
182 // Vtable stubs are not described with StubCodeDesc,
183 // process them separately
184 VtableStubs::vtable_stub_do(do_vtable_stub);
185
186 // next iterate over all the non-nmethod code blobs and add them to
187 // the list - as noted above this will filter out duplicates and
188 // enclosing blobs.
189 CodeCache::blobs_do(do_blob);
190
191 // make the global list the instance list so that it can be used
192 // for other iterations.
193 _code_blobs = _global_code_blobs;
194 _global_code_blobs = NULL;
195 }
196
197
198 // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.
199
generate_dynamic_code_events(JvmtiEnv * env)200 jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {
201 CodeBlobCollector collector;
202
203 // First collect all the code blobs. This has to be done in a
204 // single pass over the code cache with CodeCache_lock held because
205 // there isn't any safe way to iterate over regular CodeBlobs since
206 // they can be freed at any point.
207 {
208 MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
209 collector.collect();
210 }
211
212 // iterate over the collected list and post an event for each blob
213 JvmtiCodeBlobDesc* blob = collector.first();
214 while (blob != NULL) {
215 JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());
216 blob = collector.next();
217 }
218 return JVMTI_ERROR_NONE;
219 }
220
221
222 // Generate a COMPILED_METHOD_LOAD event for each nnmethod
generate_compiled_method_load_events(JvmtiEnv * env)223 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {
224 JavaThread* java_thread = JavaThread::current();
225 JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
226 {
227 NoSafepointVerifier nsv; // safepoints are not safe while collecting methods to post.
228 {
229 // Walk the CodeCache notifying for live nmethods, don't release the CodeCache_lock
230 // because the sweeper may be running concurrently.
231 // Save events to the queue for posting outside the CodeCache_lock.
232 MutexLocker mu(java_thread, CodeCache_lock, Mutex::_no_safepoint_check_flag);
233 // Iterate over non-profiled and profiled nmethods
234 NMethodIterator iter(NMethodIterator::only_alive_and_not_unloading);
235 while(iter.next()) {
236 nmethod* current = iter.method();
237 current->post_compiled_method_load_event(state);
238 }
239 }
240
241 // Enter nmethod barrier code if present outside CodeCache_lock
242 state->run_nmethod_entry_barriers();
243 }
244
245 // Now post all the events outside the CodeCache_lock.
246 // If there's a safepoint, the queued events will be kept alive.
247 // Adding these events to the service thread to post is something that
248 // should work, but the service thread doesn't keep up in stress scenarios and
249 // the os eventually kills the process with OOM.
250 // We want this thread to wait until the events are all posted.
251 state->post_events(env);
252 return JVMTI_ERROR_NONE;
253 }
254
255
256 // create a C-heap allocated address location map for an nmethod
build_jvmti_addr_location_map(nmethod * nm,jvmtiAddrLocationMap ** map_ptr,jint * map_length_ptr)257 void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,
258 jvmtiAddrLocationMap** map_ptr,
259 jint *map_length_ptr)
260 {
261 ResourceMark rm;
262 jvmtiAddrLocationMap* map = NULL;
263 jint map_length = 0;
264
265
266 // Generate line numbers using PcDesc and ScopeDesc info
267 methodHandle mh(Thread::current(), nm->method());
268
269 if (!mh->is_native()) {
270 PcDesc *pcd;
271 int pcds_in_method;
272
273 pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());
274 map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method, mtInternal);
275
276 address scopes_data = nm->scopes_data_begin();
277 for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {
278 ScopeDesc sc0(nm, pcd, true);
279 ScopeDesc *sd = &sc0;
280 while( !sd->is_top() ) { sd = sd->sender(); }
281 int bci = sd->bci();
282 if (bci >= 0) {
283 assert(map_length < pcds_in_method, "checking");
284 map[map_length].start_address = (const void*)pcd->real_pc(nm);
285 map[map_length].location = bci;
286 ++map_length;
287 }
288 }
289 }
290
291 *map_ptr = map;
292 *map_length_ptr = map_length;
293 }
294