Mon, 04 Jan 2010 18:38:08 +0100
6893081: method handle & invokedynamic code needs additional cleanup (post 6815692, 6858164)
Summary: During the work for 6829187 we have fixed a number of basic bugs which are logically grouped with 6815692 and 6858164 but which must be reviewed and pushed separately.
Reviewed-by: kvn, never
1 /*
2 * Copyright 1998-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 # include "incls/_precompiled.incl"
26 # include "incls/_debugInfoRec.cpp.incl"
28 // Private definition.
29 // There is one DIR_Chunk for each scope and values array.
30 // A chunk can potentially be used more than once.
31 // We keep track of these chunks in order to detect
32 // repetition and enable sharing.
33 class DIR_Chunk {
34 friend class DebugInformationRecorder;
35 int _offset; // location in the stream of this scope
36 int _length; // number of bytes in the stream
37 int _hash; // hash of stream bytes (for quicker reuse)
39 void* operator new(size_t ignore, DebugInformationRecorder* dir) {
40 assert(ignore == sizeof(DIR_Chunk), "");
41 if (dir->_next_chunk >= dir->_next_chunk_limit) {
42 const int CHUNK = 100;
43 dir->_next_chunk = NEW_RESOURCE_ARRAY(DIR_Chunk, CHUNK);
44 dir->_next_chunk_limit = dir->_next_chunk + CHUNK;
45 }
46 return dir->_next_chunk++;
47 }
49 DIR_Chunk(int offset, int length, DebugInformationRecorder* dir) {
50 _offset = offset;
51 _length = length;
52 unsigned int hash = 0;
53 address p = dir->stream()->buffer() + _offset;
54 for (int i = 0; i < length; i++) {
55 if (i == 6) break;
56 hash *= 127;
57 hash += p[i];
58 }
59 _hash = hash;
60 }
62 DIR_Chunk* find_match(GrowableArray<DIR_Chunk*>* arr,
63 int start_index,
64 DebugInformationRecorder* dir) {
65 int end_index = arr->length();
66 int hash = this->_hash, length = this->_length;
67 address buf = dir->stream()->buffer();
68 for (int i = end_index; --i >= start_index; ) {
69 DIR_Chunk* that = arr->at(i);
70 if (hash == that->_hash &&
71 length == that->_length &&
72 0 == memcmp(buf + this->_offset, buf + that->_offset, length)) {
73 return that;
74 }
75 }
76 return NULL;
77 }
78 };
80 static inline bool compute_recording_non_safepoints() {
81 if (JvmtiExport::should_post_compiled_method_load()
82 && FLAG_IS_DEFAULT(DebugNonSafepoints)) {
83 // The default value of this flag is taken to be true,
84 // if JVMTI is looking at nmethod codes.
85 // We anticipate that JVMTI may wish to participate in profiling.
86 return true;
87 }
89 // If the flag is set manually, use it, whether true or false.
90 // Otherwise, if JVMTI is not in the picture, use the default setting.
91 // (This is true in debug, just for the exercise, false in product mode.)
92 return DebugNonSafepoints;
93 }
95 DebugInformationRecorder::DebugInformationRecorder(OopRecorder* oop_recorder)
96 : _recording_non_safepoints(compute_recording_non_safepoints())
97 {
98 _pcs_size = 100;
99 _pcs = NEW_RESOURCE_ARRAY(PcDesc, _pcs_size);
100 _pcs_length = 0;
102 _prev_safepoint_pc = PcDesc::lower_offset_limit;
104 _stream = new DebugInfoWriteStream(this, 10 * K);
105 // make sure that there is no stream_decode_offset that is zero
106 _stream->write_byte((jbyte)0xFF);
108 // make sure that we can distinguish the value "serialized_null" from offsets
109 assert(_stream->position() > serialized_null, "sanity");
111 _oop_recorder = oop_recorder;
113 _all_chunks = new GrowableArray<DIR_Chunk*>(300);
114 _shared_chunks = new GrowableArray<DIR_Chunk*>(30);
115 _next_chunk = _next_chunk_limit = NULL;
117 add_new_pc_offset(PcDesc::lower_offset_limit); // sentinel record
119 debug_only(_recording_state = rs_null);
120 }
123 void DebugInformationRecorder::add_oopmap(int pc_offset, OopMap* map) {
124 // !!!!! Preserve old style handling of oopmaps for now
125 _oopmaps->add_gc_map(pc_offset, map);
126 }
128 void DebugInformationRecorder::add_safepoint(int pc_offset, OopMap* map) {
129 assert(!_oop_recorder->is_complete(), "not frozen yet");
130 // Store the new safepoint
132 // Add the oop map
133 add_oopmap(pc_offset, map);
135 add_new_pc_offset(pc_offset);
137 assert(_recording_state == rs_null, "nesting of recording calls");
138 debug_only(_recording_state = rs_safepoint);
139 }
141 void DebugInformationRecorder::add_non_safepoint(int pc_offset) {
142 assert(!_oop_recorder->is_complete(), "not frozen yet");
143 assert(_recording_non_safepoints, "must be recording non-safepoints");
145 add_new_pc_offset(pc_offset);
147 assert(_recording_state == rs_null, "nesting of recording calls");
148 debug_only(_recording_state = rs_non_safepoint);
149 }
151 void DebugInformationRecorder::add_new_pc_offset(int pc_offset) {
152 assert(_pcs_length == 0 || last_pc()->pc_offset() < pc_offset,
153 "must specify a new, larger pc offset");
155 // add the pcdesc
156 if (_pcs_length == _pcs_size) {
157 // Expand
158 int new_pcs_size = _pcs_size * 2;
159 PcDesc* new_pcs = NEW_RESOURCE_ARRAY(PcDesc, new_pcs_size);
160 for (int index = 0; index < _pcs_length; index++) {
161 new_pcs[index] = _pcs[index];
162 }
163 _pcs_size = new_pcs_size;
164 _pcs = new_pcs;
165 }
166 assert(_pcs_size > _pcs_length, "There must be room for after expanding");
168 _pcs[_pcs_length++] = PcDesc(pc_offset, DebugInformationRecorder::serialized_null,
169 DebugInformationRecorder::serialized_null);
170 }
173 int DebugInformationRecorder::serialize_monitor_values(GrowableArray<MonitorValue*>* monitors) {
174 if (monitors == NULL || monitors->is_empty()) return DebugInformationRecorder::serialized_null;
175 assert(_recording_state == rs_safepoint, "must be recording a safepoint");
176 int result = stream()->position();
177 stream()->write_int(monitors->length());
178 for (int index = 0; index < monitors->length(); index++) {
179 monitors->at(index)->write_on(stream());
180 }
181 assert(result != serialized_null, "sanity");
183 // (See comment below on DebugInformationRecorder::describe_scope.)
184 int shared_result = find_sharable_decode_offset(result);
185 if (shared_result != serialized_null) {
186 stream()->set_position(result);
187 result = shared_result;
188 }
190 return result;
191 }
194 int DebugInformationRecorder::serialize_scope_values(GrowableArray<ScopeValue*>* values) {
195 if (values == NULL || values->is_empty()) return DebugInformationRecorder::serialized_null;
196 assert(_recording_state == rs_safepoint, "must be recording a safepoint");
197 int result = stream()->position();
198 assert(result != serialized_null, "sanity");
199 stream()->write_int(values->length());
200 for (int index = 0; index < values->length(); index++) {
201 values->at(index)->write_on(stream());
202 }
204 // (See comment below on DebugInformationRecorder::describe_scope.)
205 int shared_result = find_sharable_decode_offset(result);
206 if (shared_result != serialized_null) {
207 stream()->set_position(result);
208 result = shared_result;
209 }
211 return result;
212 }
215 #ifndef PRODUCT
216 // These variables are put into one block to reduce relocations
217 // and make it simpler to print from the debugger.
218 static
219 struct dir_stats_struct {
220 int chunks_queried;
221 int chunks_shared;
222 int chunks_reshared;
223 int chunks_elided;
225 void print() {
226 tty->print_cr("Debug Data Chunks: %d, shared %d+%d, non-SP's elided %d",
227 chunks_queried,
228 chunks_shared, chunks_reshared,
229 chunks_elided);
230 }
231 } dir_stats;
232 #endif //PRODUCT
235 int DebugInformationRecorder::find_sharable_decode_offset(int stream_offset) {
236 // Only pull this trick if non-safepoint recording
237 // is enabled, for now.
238 if (!recording_non_safepoints())
239 return serialized_null;
241 NOT_PRODUCT(++dir_stats.chunks_queried);
242 int stream_length = stream()->position() - stream_offset;
243 assert(stream_offset != serialized_null, "should not be null");
244 assert(stream_length != 0, "should not be empty");
246 DIR_Chunk* ns = new(this) DIR_Chunk(stream_offset, stream_length, this);
248 // Look in previously shared scopes first:
249 DIR_Chunk* ms = ns->find_match(_shared_chunks, 0, this);
250 if (ms != NULL) {
251 NOT_PRODUCT(++dir_stats.chunks_reshared);
252 assert(ns+1 == _next_chunk, "");
253 _next_chunk = ns;
254 return ms->_offset;
255 }
257 // Look in recently encountered scopes next:
258 const int MAX_RECENT = 50;
259 int start_index = _all_chunks->length() - MAX_RECENT;
260 if (start_index < 0) start_index = 0;
261 ms = ns->find_match(_all_chunks, start_index, this);
262 if (ms != NULL) {
263 NOT_PRODUCT(++dir_stats.chunks_shared);
264 // Searching in _all_chunks is limited to a window,
265 // but searching in _shared_chunks is unlimited.
266 _shared_chunks->append(ms);
267 assert(ns+1 == _next_chunk, "");
268 _next_chunk = ns;
269 return ms->_offset;
270 }
272 // No match. Add this guy to the list, in hopes of future shares.
273 _all_chunks->append(ns);
274 return serialized_null;
275 }
278 // must call add_safepoint before: it sets PcDesc and this routine uses
279 // the last PcDesc set
280 void DebugInformationRecorder::describe_scope(int pc_offset,
281 ciMethod* method,
282 int bci,
283 bool reexecute,
284 bool is_method_handle_invoke,
285 DebugToken* locals,
286 DebugToken* expressions,
287 DebugToken* monitors) {
288 assert(_recording_state != rs_null, "nesting of recording calls");
289 PcDesc* last_pd = last_pc();
290 assert(last_pd->pc_offset() == pc_offset, "must be last pc");
291 int sender_stream_offset = last_pd->scope_decode_offset();
292 // update the stream offset of current pc desc
293 int stream_offset = stream()->position();
294 last_pd->set_scope_decode_offset(stream_offset);
296 // Record flags into pcDesc.
297 last_pd->set_should_reexecute(reexecute);
298 last_pd->set_is_method_handle_invoke(is_method_handle_invoke);
300 // serialize sender stream offest
301 stream()->write_int(sender_stream_offset);
303 // serialize scope
304 jobject method_enc = (method == NULL)? NULL: method->constant_encoding();
305 stream()->write_int(oop_recorder()->find_index(method_enc));
306 stream()->write_bci(bci);
307 assert(method == NULL ||
308 (method->is_native() && bci == 0) ||
309 (!method->is_native() && 0 <= bci && bci < method->code_size()) ||
310 bci == -1, "illegal bci");
312 // serialize the locals/expressions/monitors
313 stream()->write_int((intptr_t) locals);
314 stream()->write_int((intptr_t) expressions);
315 stream()->write_int((intptr_t) monitors);
317 // Here's a tricky bit. We just wrote some bytes.
318 // Wouldn't it be nice to find that we had already
319 // written those same bytes somewhere else?
320 // If we get lucky this way, reset the stream
321 // and reuse the old bytes. By the way, this
322 // trick not only shares parent scopes, but also
323 // compresses equivalent non-safepoint PcDescs.
324 int shared_stream_offset = find_sharable_decode_offset(stream_offset);
325 if (shared_stream_offset != serialized_null) {
326 stream()->set_position(stream_offset);
327 last_pd->set_scope_decode_offset(shared_stream_offset);
328 }
329 }
331 void DebugInformationRecorder::dump_object_pool(GrowableArray<ScopeValue*>* objects) {
332 guarantee( _pcs_length > 0, "safepoint must exist before describing scopes");
333 PcDesc* last_pd = &_pcs[_pcs_length-1];
334 if (objects != NULL) {
335 for (int i = objects->length() - 1; i >= 0; i--) {
336 ((ObjectValue*) objects->at(i))->set_visited(false);
337 }
338 }
339 int offset = serialize_scope_values(objects);
340 last_pd->set_obj_decode_offset(offset);
341 }
343 void DebugInformationRecorder::end_scopes(int pc_offset, bool is_safepoint) {
344 assert(_recording_state == (is_safepoint? rs_safepoint: rs_non_safepoint),
345 "nesting of recording calls");
346 debug_only(_recording_state = rs_null);
348 // Try to compress away an equivalent non-safepoint predecessor.
349 // (This only works because we have previously recognized redundant
350 // scope trees and made them use a common scope_decode_offset.)
351 if (_pcs_length >= 2 && recording_non_safepoints()) {
352 PcDesc* last = last_pc();
353 PcDesc* prev = prev_pc();
354 // If prev is (a) not a safepoint and (b) has the same
355 // stream pointer, then it can be coalesced into the last.
356 // This is valid because non-safepoints are only sought
357 // with pc_desc_near, which (when it misses prev) will
358 // search forward until it finds last.
359 // In addition, it does not matter if the last PcDesc
360 // is for a safepoint or not.
361 if (_prev_safepoint_pc < prev->pc_offset() && prev->is_same_info(last)) {
362 assert(prev == last-1, "sane");
363 prev->set_pc_offset(pc_offset);
364 _pcs_length -= 1;
365 NOT_PRODUCT(++dir_stats.chunks_elided);
366 }
367 }
369 // We have just recorded this safepoint.
370 // Remember it in case the previous paragraph needs to know.
371 if (is_safepoint) {
372 _prev_safepoint_pc = pc_offset;
373 }
374 }
376 DebugToken* DebugInformationRecorder::create_scope_values(GrowableArray<ScopeValue*>* values) {
377 assert(!_oop_recorder->is_complete(), "not frozen yet");
378 return (DebugToken*) (intptr_t) serialize_scope_values(values);
379 }
382 DebugToken* DebugInformationRecorder::create_monitor_values(GrowableArray<MonitorValue*>* monitors) {
383 assert(!_oop_recorder->is_complete(), "not frozen yet");
384 return (DebugToken*) (intptr_t) serialize_monitor_values(monitors);
385 }
388 int DebugInformationRecorder::data_size() {
389 debug_only(_oop_recorder->oop_size()); // mark it "frozen" for asserts
390 return _stream->position();
391 }
394 int DebugInformationRecorder::pcs_size() {
395 debug_only(_oop_recorder->oop_size()); // mark it "frozen" for asserts
396 if (last_pc()->pc_offset() != PcDesc::upper_offset_limit)
397 add_new_pc_offset(PcDesc::upper_offset_limit);
398 return _pcs_length * sizeof(PcDesc);
399 }
402 void DebugInformationRecorder::copy_to(nmethod* nm) {
403 nm->copy_scopes_data(stream()->buffer(), stream()->position());
404 nm->copy_scopes_pcs(_pcs, _pcs_length);
405 }
408 void DebugInformationRecorder::verify(const nmethod* code) {
409 Unimplemented();
410 }
412 #ifndef PRODUCT
413 void DebugInformationRecorder::print_statistics() {
414 dir_stats.print();
415 }
416 #endif //PRODUCT