Wed, 13 Jan 2010 23:05:52 -0800
6912065: final fields in objects need to support inlining optimizations for JSR 292
Reviewed-by: twisti, kvn
1 /*
2 * Copyright 1997-2010 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/_vframe.cpp.incl"
28 vframe::vframe(const frame* fr, const RegisterMap* reg_map, JavaThread* thread)
29 : _reg_map(reg_map), _thread(thread) {
30 assert(fr != NULL, "must have frame");
31 _fr = *fr;
32 }
34 vframe::vframe(const frame* fr, JavaThread* thread)
35 : _reg_map(thread), _thread(thread) {
36 assert(fr != NULL, "must have frame");
37 _fr = *fr;
38 }
40 vframe* vframe::new_vframe(const frame* f, const RegisterMap* reg_map, JavaThread* thread) {
41 // Interpreter frame
42 if (f->is_interpreted_frame()) {
43 return new interpretedVFrame(f, reg_map, thread);
44 }
46 // Compiled frame
47 CodeBlob* cb = f->cb();
48 if (cb != NULL) {
49 if (cb->is_nmethod()) {
50 nmethod* nm = (nmethod*)cb;
51 return new compiledVFrame(f, reg_map, thread, nm);
52 }
54 if (f->is_runtime_frame()) {
55 // Skip this frame and try again.
56 RegisterMap temp_map = *reg_map;
57 frame s = f->sender(&temp_map);
58 return new_vframe(&s, &temp_map, thread);
59 }
60 }
62 // External frame
63 return new externalVFrame(f, reg_map, thread);
64 }
66 vframe* vframe::sender() const {
67 RegisterMap temp_map = *register_map();
68 assert(is_top(), "just checking");
69 if (_fr.is_entry_frame() && _fr.is_first_frame()) return NULL;
70 frame s = _fr.real_sender(&temp_map);
71 if (s.is_first_frame()) return NULL;
72 return vframe::new_vframe(&s, &temp_map, thread());
73 }
75 vframe* vframe::top() const {
76 vframe* vf = (vframe*) this;
77 while (!vf->is_top()) vf = vf->sender();
78 return vf;
79 }
82 javaVFrame* vframe::java_sender() const {
83 vframe* f = sender();
84 while (f != NULL) {
85 if (f->is_java_frame()) return javaVFrame::cast(f);
86 f = f->sender();
87 }
88 return NULL;
89 }
91 // ------------- javaVFrame --------------
93 GrowableArray<MonitorInfo*>* javaVFrame::locked_monitors() {
94 assert(SafepointSynchronize::is_at_safepoint() || JavaThread::current() == thread(),
95 "must be at safepoint or it's a java frame of the current thread");
97 GrowableArray<MonitorInfo*>* mons = monitors();
98 GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(mons->length());
99 if (mons->is_empty()) return result;
101 bool found_first_monitor = false;
102 ObjectMonitor *pending_monitor = thread()->current_pending_monitor();
103 ObjectMonitor *waiting_monitor = thread()->current_waiting_monitor();
104 oop pending_obj = (pending_monitor != NULL ? (oop) pending_monitor->object() : NULL);
105 oop waiting_obj = (waiting_monitor != NULL ? (oop) waiting_monitor->object() : NULL);
107 for (int index = (mons->length()-1); index >= 0; index--) {
108 MonitorInfo* monitor = mons->at(index);
109 if (monitor->eliminated() && is_compiled_frame()) continue; // skip eliminated monitor
110 oop obj = monitor->owner();
111 if (obj == NULL) continue; // skip unowned monitor
112 //
113 // Skip the monitor that the thread is blocked to enter or waiting on
114 //
115 if (!found_first_monitor && (obj == pending_obj || obj == waiting_obj)) {
116 continue;
117 }
118 found_first_monitor = true;
119 result->append(monitor);
120 }
121 return result;
122 }
124 static void print_locked_object_class_name(outputStream* st, Handle obj, const char* lock_state) {
125 if (obj.not_null()) {
126 st->print("\t- %s <" INTPTR_FORMAT "> ", lock_state, (address)obj());
127 if (obj->klass() == SystemDictionary::Class_klass()) {
128 klassOop target_klass = java_lang_Class::as_klassOop(obj());
129 st->print_cr("(a java.lang.Class for %s)", instanceKlass::cast(target_klass)->external_name());
130 } else {
131 Klass* k = Klass::cast(obj->klass());
132 st->print_cr("(a %s)", k->external_name());
133 }
134 }
135 }
137 void javaVFrame::print_lock_info_on(outputStream* st, int frame_count) {
138 ResourceMark rm;
140 // If this is the first frame, and java.lang.Object.wait(...) then print out the receiver.
141 if (frame_count == 0) {
142 if (method()->name() == vmSymbols::wait_name() &&
143 instanceKlass::cast(method()->method_holder())->name() == vmSymbols::java_lang_Object()) {
144 StackValueCollection* locs = locals();
145 if (!locs->is_empty()) {
146 StackValue* sv = locs->at(0);
147 if (sv->type() == T_OBJECT) {
148 Handle o = locs->at(0)->get_obj();
149 print_locked_object_class_name(st, o, "waiting on");
150 }
151 }
152 } else if (thread()->current_park_blocker() != NULL) {
153 oop obj = thread()->current_park_blocker();
154 Klass* k = Klass::cast(obj->klass());
155 st->print_cr("\t- %s <" INTPTR_FORMAT "> (a %s)", "parking to wait for ", (address)obj, k->external_name());
156 }
157 }
160 // Print out all monitors that we have locked or are trying to lock
161 GrowableArray<MonitorInfo*>* mons = monitors();
162 if (!mons->is_empty()) {
163 bool found_first_monitor = false;
164 for (int index = (mons->length()-1); index >= 0; index--) {
165 MonitorInfo* monitor = mons->at(index);
166 if (monitor->eliminated() && is_compiled_frame()) { // Eliminated in compiled code
167 if (monitor->owner_is_scalar_replaced()) {
168 Klass* k = Klass::cast(monitor->owner_klass());
169 st->print("\t- eliminated <owner is scalar replaced> (a %s)", k->external_name());
170 } else {
171 oop obj = monitor->owner();
172 if (obj != NULL) {
173 print_locked_object_class_name(st, obj, "eliminated");
174 }
175 }
176 continue;
177 }
178 if (monitor->owner() != NULL) {
180 // First, assume we have the monitor locked. If we haven't found an
181 // owned monitor before and this is the first frame, then we need to
182 // see if we have completed the lock or we are blocked trying to
183 // acquire it - we can only be blocked if the monitor is inflated
185 const char *lock_state = "locked"; // assume we have the monitor locked
186 if (!found_first_monitor && frame_count == 0) {
187 markOop mark = monitor->owner()->mark();
188 if (mark->has_monitor() &&
189 mark->monitor() == thread()->current_pending_monitor()) {
190 lock_state = "waiting to lock";
191 }
192 }
194 found_first_monitor = true;
195 print_locked_object_class_name(st, monitor->owner(), lock_state);
196 }
197 }
198 }
199 }
201 // ------------- interpretedVFrame --------------
203 u_char* interpretedVFrame::bcp() const {
204 return fr().interpreter_frame_bcp();
205 }
207 void interpretedVFrame::set_bcp(u_char* bcp) {
208 fr().interpreter_frame_set_bcp(bcp);
209 }
211 intptr_t* interpretedVFrame::locals_addr_at(int offset) const {
212 assert(fr().is_interpreted_frame(), "frame should be an interpreted frame");
213 return fr().interpreter_frame_local_at(offset);
214 }
217 GrowableArray<MonitorInfo*>* interpretedVFrame::monitors() const {
218 GrowableArray<MonitorInfo*>* result = new GrowableArray<MonitorInfo*>(5);
219 for (BasicObjectLock* current = (fr().previous_monitor_in_interpreter_frame(fr().interpreter_frame_monitor_begin()));
220 current >= fr().interpreter_frame_monitor_end();
221 current = fr().previous_monitor_in_interpreter_frame(current)) {
222 result->push(new MonitorInfo(current->obj(), current->lock(), false, false));
223 }
224 return result;
225 }
227 int interpretedVFrame::bci() const {
228 return method()->bci_from(bcp());
229 }
231 methodOop interpretedVFrame::method() const {
232 return fr().interpreter_frame_method();
233 }
235 StackValueCollection* interpretedVFrame::locals() const {
236 int length = method()->max_locals();
238 if (method()->is_native()) {
239 // If the method is native, max_locals is not telling the truth.
240 // maxlocals then equals the size of parameters
241 length = method()->size_of_parameters();
242 }
244 StackValueCollection* result = new StackValueCollection(length);
246 // Get oopmap describing oops and int for current bci
247 if (TaggedStackInterpreter) {
248 for(int i=0; i < length; i++) {
249 // Find stack location
250 intptr_t *addr = locals_addr_at(i);
252 // Depending on oop/int put it in the right package
253 StackValue *sv;
254 frame::Tag tag = fr().interpreter_frame_local_tag(i);
255 if (tag == frame::TagReference) {
256 // oop value
257 Handle h(*(oop *)addr);
258 sv = new StackValue(h);
259 } else {
260 // integer
261 sv = new StackValue(*addr);
262 }
263 assert(sv != NULL, "sanity check");
264 result->add(sv);
265 }
266 } else {
267 InterpreterOopMap oop_mask;
268 if (TraceDeoptimization && Verbose) {
269 methodHandle m_h(thread(), method());
270 OopMapCache::compute_one_oop_map(m_h, bci(), &oop_mask);
271 } else {
272 method()->mask_for(bci(), &oop_mask);
273 }
274 // handle locals
275 for(int i=0; i < length; i++) {
276 // Find stack location
277 intptr_t *addr = locals_addr_at(i);
279 // Depending on oop/int put it in the right package
280 StackValue *sv;
281 if (oop_mask.is_oop(i)) {
282 // oop value
283 Handle h(*(oop *)addr);
284 sv = new StackValue(h);
285 } else {
286 // integer
287 sv = new StackValue(*addr);
288 }
289 assert(sv != NULL, "sanity check");
290 result->add(sv);
291 }
292 }
293 return result;
294 }
296 void interpretedVFrame::set_locals(StackValueCollection* values) const {
297 if (values == NULL || values->size() == 0) return;
299 int length = method()->max_locals();
300 if (method()->is_native()) {
301 // If the method is native, max_locals is not telling the truth.
302 // maxlocals then equals the size of parameters
303 length = method()->size_of_parameters();
304 }
306 assert(length == values->size(), "Mismatch between actual stack format and supplied data");
308 // handle locals
309 for (int i = 0; i < length; i++) {
310 // Find stack location
311 intptr_t *addr = locals_addr_at(i);
313 // Depending on oop/int put it in the right package
314 StackValue *sv = values->at(i);
315 assert(sv != NULL, "sanity check");
316 if (sv->type() == T_OBJECT) {
317 *(oop *) addr = (sv->get_obj())();
318 } else { // integer
319 *addr = sv->get_int();
320 }
321 }
322 }
324 StackValueCollection* interpretedVFrame::expressions() const {
325 int length = fr().interpreter_frame_expression_stack_size();
326 if (method()->is_native()) {
327 // If the method is native, there is no expression stack
328 length = 0;
329 }
331 int nof_locals = method()->max_locals();
332 StackValueCollection* result = new StackValueCollection(length);
334 if (TaggedStackInterpreter) {
335 // handle expressions
336 for(int i=0; i < length; i++) {
337 // Find stack location
338 intptr_t *addr = fr().interpreter_frame_expression_stack_at(i);
339 frame::Tag tag = fr().interpreter_frame_expression_stack_tag(i);
341 // Depending on oop/int put it in the right package
342 StackValue *sv;
343 if (tag == frame::TagReference) {
344 // oop value
345 Handle h(*(oop *)addr);
346 sv = new StackValue(h);
347 } else {
348 // otherwise
349 sv = new StackValue(*addr);
350 }
351 assert(sv != NULL, "sanity check");
352 result->add(sv);
353 }
354 } else {
355 InterpreterOopMap oop_mask;
356 // Get oopmap describing oops and int for current bci
357 if (TraceDeoptimization && Verbose) {
358 methodHandle m_h(method());
359 OopMapCache::compute_one_oop_map(m_h, bci(), &oop_mask);
360 } else {
361 method()->mask_for(bci(), &oop_mask);
362 }
363 // handle expressions
364 for(int i=0; i < length; i++) {
365 // Find stack location
366 intptr_t *addr = fr().interpreter_frame_expression_stack_at(i);
368 // Depending on oop/int put it in the right package
369 StackValue *sv;
370 if (oop_mask.is_oop(i + nof_locals)) {
371 // oop value
372 Handle h(*(oop *)addr);
373 sv = new StackValue(h);
374 } else {
375 // integer
376 sv = new StackValue(*addr);
377 }
378 assert(sv != NULL, "sanity check");
379 result->add(sv);
380 }
381 }
382 return result;
383 }
386 // ------------- cChunk --------------
388 entryVFrame::entryVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread)
389 : externalVFrame(fr, reg_map, thread) {}
392 void vframeStreamCommon::found_bad_method_frame() {
393 // 6379830 Cut point for an assertion that occasionally fires when
394 // we are using the performance analyzer.
395 // Disable this assert when testing the analyzer with fastdebug.
396 // -XX:SuppressErrorAt=vframe.cpp:XXX (XXX=following line number)
397 assert(false, "invalid bci or invalid scope desc");
398 }
400 // top-frame will be skipped
401 vframeStream::vframeStream(JavaThread* thread, frame top_frame,
402 bool stop_at_java_call_stub) : vframeStreamCommon(thread) {
403 _stop_at_java_call_stub = stop_at_java_call_stub;
405 // skip top frame, as it may not be at safepoint
406 _frame = top_frame.sender(&_reg_map);
407 while (!fill_from_frame()) {
408 _frame = _frame.sender(&_reg_map);
409 }
410 }
413 // Step back n frames, skip any pseudo frames in between.
414 // This function is used in Class.forName, Class.newInstance, Method.Invoke,
415 // AccessController.doPrivileged.
416 //
417 // NOTE that in JDK 1.4 this has been exposed to Java as
418 // sun.reflect.Reflection.getCallerClass(), which can be inlined.
419 // Inlined versions must match this routine's logic.
420 // Native method prefixing logic does not need to match since
421 // the method names don't match and inlining will not occur.
422 // See, for example,
423 // Parse::inline_native_Reflection_getCallerClass in
424 // opto/library_call.cpp.
425 void vframeStreamCommon::security_get_caller_frame(int depth) {
426 bool use_new_reflection = JDK_Version::is_gte_jdk14x_version() && UseNewReflection;
428 while (!at_end()) {
429 if (Universe::reflect_invoke_cache()->is_same_method(method())) {
430 // This is Method.invoke() -- skip it
431 } else if (use_new_reflection &&
432 Klass::cast(method()->method_holder())
433 ->is_subclass_of(SystemDictionary::reflect_MethodAccessorImpl_klass())) {
434 // This is an auxilary frame -- skip it
435 } else if (method()->is_method_handle_adapter()) {
436 // This is an internal adapter frame from the MethodHandleCompiler -- skip it
437 } else {
438 // This is non-excluded frame, we need to count it against the depth
439 if (depth-- <= 0) {
440 // we have reached the desired depth, we are done
441 break;
442 }
443 }
444 if (method()->is_prefixed_native()) {
445 skip_prefixed_method_and_wrappers();
446 } else {
447 next();
448 }
449 }
450 }
453 void vframeStreamCommon::skip_prefixed_method_and_wrappers() {
454 ResourceMark rm;
455 HandleMark hm;
457 int method_prefix_count = 0;
458 char** method_prefixes = JvmtiExport::get_all_native_method_prefixes(&method_prefix_count);
459 KlassHandle prefixed_klass(method()->method_holder());
460 const char* prefixed_name = method()->name()->as_C_string();
461 size_t prefixed_name_len = strlen(prefixed_name);
462 int prefix_index = method_prefix_count-1;
464 while (!at_end()) {
465 next();
466 if (method()->method_holder() != prefixed_klass()) {
467 break; // classes don't match, can't be a wrapper
468 }
469 const char* name = method()->name()->as_C_string();
470 size_t name_len = strlen(name);
471 size_t prefix_len = prefixed_name_len - name_len;
472 if (prefix_len <= 0 || strcmp(name, prefixed_name + prefix_len) != 0) {
473 break; // prefixed name isn't prefixed version of method name, can't be a wrapper
474 }
475 for (; prefix_index >= 0; --prefix_index) {
476 const char* possible_prefix = method_prefixes[prefix_index];
477 size_t possible_prefix_len = strlen(possible_prefix);
478 if (possible_prefix_len == prefix_len &&
479 strncmp(possible_prefix, prefixed_name, prefix_len) == 0) {
480 break; // matching prefix found
481 }
482 }
483 if (prefix_index < 0) {
484 break; // didn't find the prefix, can't be a wrapper
485 }
486 prefixed_name = name;
487 prefixed_name_len = name_len;
488 }
489 }
492 void vframeStreamCommon::skip_reflection_related_frames() {
493 while (!at_end() &&
494 (JDK_Version::is_gte_jdk14x_version() && UseNewReflection &&
495 (Klass::cast(method()->method_holder())->is_subclass_of(SystemDictionary::reflect_MethodAccessorImpl_klass()) ||
496 Klass::cast(method()->method_holder())->is_subclass_of(SystemDictionary::reflect_ConstructorAccessorImpl_klass())))) {
497 next();
498 }
499 }
502 #ifndef PRODUCT
503 void vframe::print() {
504 if (WizardMode) _fr.print_value_on(tty,NULL);
505 }
508 void vframe::print_value() const {
509 ((vframe*)this)->print();
510 }
513 void entryVFrame::print_value() const {
514 ((entryVFrame*)this)->print();
515 }
517 void entryVFrame::print() {
518 vframe::print();
519 tty->print_cr("C Chunk inbetween Java");
520 tty->print_cr("C link " INTPTR_FORMAT, _fr.link());
521 }
524 // ------------- javaVFrame --------------
526 static void print_stack_values(const char* title, StackValueCollection* values) {
527 if (values->is_empty()) return;
528 tty->print_cr("\t%s:", title);
529 values->print();
530 }
533 void javaVFrame::print() {
534 ResourceMark rm;
535 vframe::print();
536 tty->print("\t");
537 method()->print_value();
538 tty->cr();
539 tty->print_cr("\tbci: %d", bci());
541 print_stack_values("locals", locals());
542 print_stack_values("expressions", expressions());
544 GrowableArray<MonitorInfo*>* list = monitors();
545 if (list->is_empty()) return;
546 tty->print_cr("\tmonitor list:");
547 for (int index = (list->length()-1); index >= 0; index--) {
548 MonitorInfo* monitor = list->at(index);
549 tty->print("\t obj\t");
550 if (monitor->owner_is_scalar_replaced()) {
551 Klass* k = Klass::cast(monitor->owner_klass());
552 tty->print("( is scalar replaced %s)", k->external_name());
553 } else if (monitor->owner() == NULL) {
554 tty->print("( null )");
555 } else {
556 monitor->owner()->print_value();
557 tty->print("(" INTPTR_FORMAT ")", (address)monitor->owner());
558 }
559 if (monitor->eliminated() && is_compiled_frame())
560 tty->print(" ( lock is eliminated )");
561 tty->cr();
562 tty->print("\t ");
563 monitor->lock()->print_on(tty);
564 tty->cr();
565 }
566 }
569 void javaVFrame::print_value() const {
570 methodOop m = method();
571 klassOop k = m->method_holder();
572 tty->print_cr("frame( sp=" INTPTR_FORMAT ", unextended_sp=" INTPTR_FORMAT ", fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT ")",
573 _fr.sp(), _fr.unextended_sp(), _fr.fp(), _fr.pc());
574 tty->print("%s.%s", Klass::cast(k)->internal_name(), m->name()->as_C_string());
576 if (!m->is_native()) {
577 symbolOop source_name = instanceKlass::cast(k)->source_file_name();
578 int line_number = m->line_number_from_bci(bci());
579 if (source_name != NULL && (line_number != -1)) {
580 tty->print("(%s:%d)", source_name->as_C_string(), line_number);
581 }
582 } else {
583 tty->print("(Native Method)");
584 }
585 // Check frame size and print warning if it looks suspiciously large
586 if (fr().sp() != NULL) {
587 RegisterMap map = *register_map();
588 uint size = fr().frame_size(&map);
589 #ifdef _LP64
590 if (size > 8*K) warning("SUSPICIOUSLY LARGE FRAME (%d)", size);
591 #else
592 if (size > 4*K) warning("SUSPICIOUSLY LARGE FRAME (%d)", size);
593 #endif
594 }
595 }
598 bool javaVFrame::structural_compare(javaVFrame* other) {
599 // Check static part
600 if (method() != other->method()) return false;
601 if (bci() != other->bci()) return false;
603 // Check locals
604 StackValueCollection *locs = locals();
605 StackValueCollection *other_locs = other->locals();
606 assert(locs->size() == other_locs->size(), "sanity check");
607 int i;
608 for(i = 0; i < locs->size(); i++) {
609 // it might happen the compiler reports a conflict and
610 // the interpreter reports a bogus int.
611 if ( is_compiled_frame() && locs->at(i)->type() == T_CONFLICT) continue;
612 if (other->is_compiled_frame() && other_locs->at(i)->type() == T_CONFLICT) continue;
614 if (!locs->at(i)->equal(other_locs->at(i)))
615 return false;
616 }
618 // Check expressions
619 StackValueCollection* exprs = expressions();
620 StackValueCollection* other_exprs = other->expressions();
621 assert(exprs->size() == other_exprs->size(), "sanity check");
622 for(i = 0; i < exprs->size(); i++) {
623 if (!exprs->at(i)->equal(other_exprs->at(i)))
624 return false;
625 }
627 return true;
628 }
631 void javaVFrame::print_activation(int index) const {
632 // frame number and method
633 tty->print("%2d - ", index);
634 ((vframe*)this)->print_value();
635 tty->cr();
637 if (WizardMode) {
638 ((vframe*)this)->print();
639 tty->cr();
640 }
641 }
644 void javaVFrame::verify() const {
645 }
648 void interpretedVFrame::verify() const {
649 }
652 // ------------- externalVFrame --------------
654 void externalVFrame::print() {
655 _fr.print_value_on(tty,NULL);
656 }
659 void externalVFrame::print_value() const {
660 ((vframe*)this)->print();
661 }
662 #endif // PRODUCT