1 /*
2  * Copyright (c) 1997, 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 "classfile/moduleEntry.hpp"
27 #include "code/codeCache.hpp"
28 #include "code/vmreg.inline.hpp"
29 #include "compiler/abstractCompiler.hpp"
30 #include "compiler/disassembler.hpp"
31 #include "gc/shared/collectedHeap.inline.hpp"
32 #include "interpreter/interpreter.hpp"
33 #include "interpreter/oopMapCache.hpp"
34 #include "memory/resourceArea.hpp"
35 #include "memory/universe.hpp"
36 #include "oops/markWord.hpp"
37 #include "oops/method.hpp"
38 #include "oops/methodData.hpp"
39 #include "oops/oop.inline.hpp"
40 #include "oops/verifyOopClosure.hpp"
41 #include "prims/methodHandles.hpp"
42 #include "runtime/frame.inline.hpp"
43 #include "runtime/handles.inline.hpp"
44 #include "runtime/javaCalls.hpp"
45 #include "runtime/monitorChunk.hpp"
46 #include "runtime/os.hpp"
47 #include "runtime/sharedRuntime.hpp"
48 #include "runtime/signature.hpp"
49 #include "runtime/stubCodeGenerator.hpp"
50 #include "runtime/stubRoutines.hpp"
51 #include "runtime/thread.inline.hpp"
52 #include "utilities/debug.hpp"
53 #include "utilities/decoder.hpp"
54 #include "utilities/formatBuffer.hpp"
55 
RegisterMap(JavaThread * thread,bool update_map,bool process_frames)56 RegisterMap::RegisterMap(JavaThread *thread, bool update_map, bool process_frames) {
57   _thread         = thread;
58   _update_map     = update_map;
59   _process_frames = process_frames;
60   clear();
61   debug_only(_update_for_id = NULL;)
62 #ifndef PRODUCT
63   for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;
64 #endif /* PRODUCT */
65 }
66 
RegisterMap(const RegisterMap * map)67 RegisterMap::RegisterMap(const RegisterMap* map) {
68   assert(map != this, "bad initialization parameter");
69   assert(map != NULL, "RegisterMap must be present");
70   _thread                = map->thread();
71   _update_map            = map->update_map();
72   _process_frames        = map->process_frames();
73   _include_argument_oops = map->include_argument_oops();
74   debug_only(_update_for_id = map->_update_for_id;)
75   pd_initialize_from(map);
76   if (update_map()) {
77     for(int i = 0; i < location_valid_size; i++) {
78       LocationValidType bits = !update_map() ? 0 : map->_location_valid[i];
79       _location_valid[i] = bits;
80       // for whichever bits are set, pull in the corresponding map->_location
81       int j = i*location_valid_type_size;
82       while (bits != 0) {
83         if ((bits & 1) != 0) {
84           assert(0 <= j && j < reg_count, "range check");
85           _location[j] = map->_location[j];
86         }
87         bits >>= 1;
88         j += 1;
89       }
90     }
91   }
92 }
93 
clear()94 void RegisterMap::clear() {
95   set_include_argument_oops(true);
96   if (_update_map) {
97     for(int i = 0; i < location_valid_size; i++) {
98       _location_valid[i] = 0;
99     }
100     pd_clear();
101   } else {
102     pd_initialize();
103   }
104 }
105 
106 #ifndef PRODUCT
107 
print_on(outputStream * st) const108 void RegisterMap::print_on(outputStream* st) const {
109   st->print_cr("Register map");
110   for(int i = 0; i < reg_count; i++) {
111 
112     VMReg r = VMRegImpl::as_VMReg(i);
113     intptr_t* src = (intptr_t*) location(r);
114     if (src != NULL) {
115 
116       r->print_on(st);
117       st->print(" [" INTPTR_FORMAT "] = ", p2i(src));
118       if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
119         st->print_cr("<misaligned>");
120       } else {
121         st->print_cr(INTPTR_FORMAT, *src);
122       }
123     }
124   }
125 }
126 
print() const127 void RegisterMap::print() const {
128   print_on(tty);
129 }
130 
131 #endif
132 // This returns the pc that if you were in the debugger you'd see. Not
133 // the idealized value in the frame object. This undoes the magic conversion
134 // that happens for deoptimized frames. In addition it makes the value the
135 // hardware would want to see in the native frame. The only user (at this point)
136 // is deoptimization. It likely no one else should ever use it.
137 
raw_pc() const138 address frame::raw_pc() const {
139   if (is_deoptimized_frame()) {
140     CompiledMethod* cm = cb()->as_compiled_method_or_null();
141     if (cm->is_method_handle_return(pc()))
142       return cm->deopt_mh_handler_begin() - pc_return_offset;
143     else
144       return cm->deopt_handler_begin() - pc_return_offset;
145   } else {
146     return (pc() - pc_return_offset);
147   }
148 }
149 
150 // Change the pc in a frame object. This does not change the actual pc in
151 // actual frame. To do that use patch_pc.
152 //
set_pc(address newpc)153 void frame::set_pc(address   newpc ) {
154 #ifdef ASSERT
155   if (_cb != NULL && _cb->is_nmethod()) {
156     assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
157   }
158 #endif // ASSERT
159 
160   // Unsafe to use the is_deoptimzed tester after changing pc
161   _deopt_state = unknown;
162   _pc = newpc;
163   _cb = CodeCache::find_blob_unsafe(_pc);
164 
165 }
166 
167 // type testers
is_ignored_frame() const168 bool frame::is_ignored_frame() const {
169   return false;  // FIXME: some LambdaForm frames should be ignored
170 }
is_deoptimized_frame() const171 bool frame::is_deoptimized_frame() const {
172   assert(_deopt_state != unknown, "not answerable");
173   return _deopt_state == is_deoptimized;
174 }
175 
is_native_frame() const176 bool frame::is_native_frame() const {
177   return (_cb != NULL &&
178           _cb->is_nmethod() &&
179           ((nmethod*)_cb)->is_native_method());
180 }
181 
is_java_frame() const182 bool frame::is_java_frame() const {
183   if (is_interpreted_frame()) return true;
184   if (is_compiled_frame())    return true;
185   return false;
186 }
187 
188 
is_compiled_frame() const189 bool frame::is_compiled_frame() const {
190   if (_cb != NULL &&
191       _cb->is_compiled() &&
192       ((CompiledMethod*)_cb)->is_java_method()) {
193     return true;
194   }
195   return false;
196 }
197 
198 
is_runtime_frame() const199 bool frame::is_runtime_frame() const {
200   return (_cb != NULL && _cb->is_runtime_stub());
201 }
202 
is_safepoint_blob_frame() const203 bool frame::is_safepoint_blob_frame() const {
204   return (_cb != NULL && _cb->is_safepoint_stub());
205 }
206 
207 // testers
208 
is_first_java_frame() const209 bool frame::is_first_java_frame() const {
210   RegisterMap map(JavaThread::current(), false); // No update
211   frame s;
212   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map));
213   return s.is_first_frame();
214 }
215 
216 
entry_frame_is_first() const217 bool frame::entry_frame_is_first() const {
218   return entry_frame_call_wrapper()->is_first_frame();
219 }
220 
entry_frame_call_wrapper_if_safe(JavaThread * thread) const221 JavaCallWrapper* frame::entry_frame_call_wrapper_if_safe(JavaThread* thread) const {
222   JavaCallWrapper** jcw = entry_frame_call_wrapper_addr();
223   address addr = (address) jcw;
224 
225   // addr must be within the usable part of the stack
226   if (thread->is_in_usable_stack(addr)) {
227     return *jcw;
228   }
229 
230   return NULL;
231 }
232 
is_entry_frame_valid(JavaThread * thread) const233 bool frame::is_entry_frame_valid(JavaThread* thread) const {
234   // Validate the JavaCallWrapper an entry frame must have
235   address jcw = (address)entry_frame_call_wrapper();
236   if (!thread->is_in_stack_range_excl(jcw, (address)fp())) {
237     return false;
238   }
239 
240   // Validate sp saved in the java frame anchor
241   JavaFrameAnchor* jfa = entry_frame_call_wrapper()->anchor();
242   return (jfa->last_Java_sp() > sp());
243 }
244 
should_be_deoptimized() const245 bool frame::should_be_deoptimized() const {
246   if (_deopt_state == is_deoptimized ||
247       !is_compiled_frame() ) return false;
248   assert(_cb != NULL && _cb->is_compiled(), "must be an nmethod");
249   CompiledMethod* nm = (CompiledMethod *)_cb;
250   if (TraceDependencies) {
251     tty->print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");
252     nm->print_value_on(tty);
253     tty->cr();
254   }
255 
256   if( !nm->is_marked_for_deoptimization() )
257     return false;
258 
259   // If at the return point, then the frame has already been popped, and
260   // only the return needs to be executed. Don't deoptimize here.
261   return !nm->is_at_poll_return(pc());
262 }
263 
can_be_deoptimized() const264 bool frame::can_be_deoptimized() const {
265   if (!is_compiled_frame()) return false;
266   CompiledMethod* nm = (CompiledMethod*)_cb;
267 
268   if( !nm->can_be_deoptimized() )
269     return false;
270 
271   return !nm->is_at_poll_return(pc());
272 }
273 
deoptimize(JavaThread * thread)274 void frame::deoptimize(JavaThread* thread) {
275   assert(thread->frame_anchor()->has_last_Java_frame() &&
276          thread->frame_anchor()->walkable(), "must be");
277   // Schedule deoptimization of an nmethod activation with this frame.
278   assert(_cb != NULL && _cb->is_compiled(), "must be");
279 
280   // If the call site is a MethodHandle call site use the MH deopt
281   // handler.
282   CompiledMethod* cm = (CompiledMethod*) _cb;
283   address deopt = cm->is_method_handle_return(pc()) ?
284                         cm->deopt_mh_handler_begin() :
285                         cm->deopt_handler_begin();
286 
287   // Save the original pc before we patch in the new one
288   cm->set_original_pc(this, pc());
289   patch_pc(thread, deopt);
290 
291 #ifdef ASSERT
292   {
293     RegisterMap map(thread, false);
294     frame check = thread->last_frame();
295     while (id() != check.id()) {
296       check = check.sender(&map);
297     }
298     assert(check.is_deoptimized_frame(), "missed deopt");
299   }
300 #endif // ASSERT
301 }
302 
java_sender() const303 frame frame::java_sender() const {
304   RegisterMap map(JavaThread::current(), false);
305   frame s;
306   for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
307   guarantee(s.is_java_frame(), "tried to get caller of first java frame");
308   return s;
309 }
310 
real_sender(RegisterMap * map) const311 frame frame::real_sender(RegisterMap* map) const {
312   frame result = sender(map);
313   while (result.is_runtime_frame() ||
314          result.is_ignored_frame()) {
315     result = result.sender(map);
316   }
317   return result;
318 }
319 
320 // Interpreter frames
321 
322 
interpreter_frame_set_locals(intptr_t * locs)323 void frame::interpreter_frame_set_locals(intptr_t* locs)  {
324   assert(is_interpreted_frame(), "Not an interpreted frame");
325   *interpreter_frame_locals_addr() = locs;
326 }
327 
interpreter_frame_method() const328 Method* frame::interpreter_frame_method() const {
329   assert(is_interpreted_frame(), "interpreted frame expected");
330   Method* m = *interpreter_frame_method_addr();
331   assert(m->is_method(), "not a Method*");
332   return m;
333 }
334 
interpreter_frame_set_method(Method * method)335 void frame::interpreter_frame_set_method(Method* method) {
336   assert(is_interpreted_frame(), "interpreted frame expected");
337   *interpreter_frame_method_addr() = method;
338 }
339 
interpreter_frame_set_mirror(oop mirror)340 void frame::interpreter_frame_set_mirror(oop mirror) {
341   assert(is_interpreted_frame(), "interpreted frame expected");
342   *interpreter_frame_mirror_addr() = mirror;
343 }
344 
interpreter_frame_bci() const345 jint frame::interpreter_frame_bci() const {
346   assert(is_interpreted_frame(), "interpreted frame expected");
347   address bcp = interpreter_frame_bcp();
348   return interpreter_frame_method()->bci_from(bcp);
349 }
350 
interpreter_frame_bcp() const351 address frame::interpreter_frame_bcp() const {
352   assert(is_interpreted_frame(), "interpreted frame expected");
353   address bcp = (address)*interpreter_frame_bcp_addr();
354   return interpreter_frame_method()->bcp_from(bcp);
355 }
356 
interpreter_frame_set_bcp(address bcp)357 void frame::interpreter_frame_set_bcp(address bcp) {
358   assert(is_interpreted_frame(), "interpreted frame expected");
359   *interpreter_frame_bcp_addr() = (intptr_t)bcp;
360 }
361 
interpreter_frame_mdp() const362 address frame::interpreter_frame_mdp() const {
363   assert(ProfileInterpreter, "must be profiling interpreter");
364   assert(is_interpreted_frame(), "interpreted frame expected");
365   return (address)*interpreter_frame_mdp_addr();
366 }
367 
interpreter_frame_set_mdp(address mdp)368 void frame::interpreter_frame_set_mdp(address mdp) {
369   assert(is_interpreted_frame(), "interpreted frame expected");
370   assert(ProfileInterpreter, "must be profiling interpreter");
371   *interpreter_frame_mdp_addr() = (intptr_t)mdp;
372 }
373 
next_monitor_in_interpreter_frame(BasicObjectLock * current) const374 BasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {
375   assert(is_interpreted_frame(), "Not an interpreted frame");
376 #ifdef ASSERT
377   interpreter_frame_verify_monitor(current);
378 #endif
379   BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());
380   return next;
381 }
382 
previous_monitor_in_interpreter_frame(BasicObjectLock * current) const383 BasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {
384   assert(is_interpreted_frame(), "Not an interpreted frame");
385 #ifdef ASSERT
386 //   // This verification needs to be checked before being enabled
387 //   interpreter_frame_verify_monitor(current);
388 #endif
389   BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());
390   return previous;
391 }
392 
393 // Interpreter locals and expression stack locations.
394 
interpreter_frame_local_at(int index) const395 intptr_t* frame::interpreter_frame_local_at(int index) const {
396   const int n = Interpreter::local_offset_in_bytes(index)/wordSize;
397   return &((*interpreter_frame_locals_addr())[n]);
398 }
399 
interpreter_frame_expression_stack_at(jint offset) const400 intptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {
401   const int i = offset * interpreter_frame_expression_stack_direction();
402   const int n = i * Interpreter::stackElementWords;
403   return &(interpreter_frame_expression_stack()[n]);
404 }
405 
interpreter_frame_expression_stack_size() const406 jint frame::interpreter_frame_expression_stack_size() const {
407   // Number of elements on the interpreter expression stack
408   // Callers should span by stackElementWords
409   int element_size = Interpreter::stackElementWords;
410   size_t stack_size = 0;
411   if (frame::interpreter_frame_expression_stack_direction() < 0) {
412     stack_size = (interpreter_frame_expression_stack() -
413                   interpreter_frame_tos_address() + 1)/element_size;
414   } else {
415     stack_size = (interpreter_frame_tos_address() -
416                   interpreter_frame_expression_stack() + 1)/element_size;
417   }
418   assert( stack_size <= (size_t)max_jint, "stack size too big");
419   return ((jint)stack_size);
420 }
421 
422 
423 // (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)
424 
print_name() const425 const char* frame::print_name() const {
426   if (is_native_frame())      return "Native";
427   if (is_interpreted_frame()) return "Interpreted";
428   if (is_compiled_frame()) {
429     if (is_deoptimized_frame()) return "Deoptimized";
430     return "Compiled";
431   }
432   if (sp() == NULL)            return "Empty";
433   return "C";
434 }
435 
print_value_on(outputStream * st,JavaThread * thread) const436 void frame::print_value_on(outputStream* st, JavaThread *thread) const {
437   NOT_PRODUCT(address begin = pc()-40;)
438   NOT_PRODUCT(address end   = NULL;)
439 
440   st->print("%s frame (sp=" INTPTR_FORMAT " unextended sp=" INTPTR_FORMAT, print_name(), p2i(sp()), p2i(unextended_sp()));
441   if (sp() != NULL)
442     st->print(", fp=" INTPTR_FORMAT ", real_fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT,
443               p2i(fp()), p2i(real_fp()), p2i(pc()));
444 
445   if (StubRoutines::contains(pc())) {
446     st->print_cr(")");
447     st->print("(");
448     StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
449     st->print("~Stub::%s", desc->name());
450     NOT_PRODUCT(begin = desc->begin(); end = desc->end();)
451   } else if (Interpreter::contains(pc())) {
452     st->print_cr(")");
453     st->print("(");
454     InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
455     if (desc != NULL) {
456       st->print("~");
457       desc->print_on(st);
458       NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();)
459     } else {
460       st->print("~interpreter");
461     }
462   }
463   st->print_cr(")");
464 
465   if (_cb != NULL) {
466     st->print("     ");
467     _cb->print_value_on(st);
468     st->cr();
469 #ifndef PRODUCT
470     if (end == NULL) {
471       begin = _cb->code_begin();
472       end   = _cb->code_end();
473     }
474 #endif
475   }
476   NOT_PRODUCT(if (WizardMode && Verbose) Disassembler::decode(begin, end);)
477 }
478 
479 
print_on(outputStream * st) const480 void frame::print_on(outputStream* st) const {
481   print_value_on(st,NULL);
482   if (is_interpreted_frame()) {
483     interpreter_frame_print_on(st);
484   }
485 }
486 
487 
interpreter_frame_print_on(outputStream * st) const488 void frame::interpreter_frame_print_on(outputStream* st) const {
489 #ifndef PRODUCT
490   assert(is_interpreted_frame(), "Not an interpreted frame");
491   jint i;
492   for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
493     intptr_t x = *interpreter_frame_local_at(i);
494     st->print(" - local  [" INTPTR_FORMAT "]", x);
495     st->fill_to(23);
496     st->print_cr("; #%d", i);
497   }
498   for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {
499     intptr_t x = *interpreter_frame_expression_stack_at(i);
500     st->print(" - stack  [" INTPTR_FORMAT "]", x);
501     st->fill_to(23);
502     st->print_cr("; #%d", i);
503   }
504   // locks for synchronization
505   for (BasicObjectLock* current = interpreter_frame_monitor_end();
506        current < interpreter_frame_monitor_begin();
507        current = next_monitor_in_interpreter_frame(current)) {
508     st->print(" - obj    [");
509     current->obj()->print_value_on(st);
510     st->print_cr("]");
511     st->print(" - lock   [");
512     current->lock()->print_on(st, current->obj());
513     st->print_cr("]");
514   }
515   // monitor
516   st->print_cr(" - monitor[" INTPTR_FORMAT "]", p2i(interpreter_frame_monitor_begin()));
517   // bcp
518   st->print(" - bcp    [" INTPTR_FORMAT "]", p2i(interpreter_frame_bcp()));
519   st->fill_to(23);
520   st->print_cr("; @%d", interpreter_frame_bci());
521   // locals
522   st->print_cr(" - locals [" INTPTR_FORMAT "]", p2i(interpreter_frame_local_at(0)));
523   // method
524   st->print(" - method [" INTPTR_FORMAT "]", p2i(interpreter_frame_method()));
525   st->fill_to(23);
526   st->print("; ");
527   interpreter_frame_method()->print_name(st);
528   st->cr();
529 #endif
530 }
531 
532 // Print whether the frame is in the VM or OS indicating a HotSpot problem.
533 // Otherwise, it's likely a bug in the native library that the Java code calls,
534 // hopefully indicating where to submit bugs.
print_C_frame(outputStream * st,char * buf,int buflen,address pc)535 void frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
536   // C/C++ frame
537   bool in_vm = os::address_is_in_vm(pc);
538   st->print(in_vm ? "V" : "C");
539 
540   int offset;
541   bool found;
542 
543   // libname
544   found = os::dll_address_to_library_name(pc, buf, buflen, &offset);
545   if (found) {
546     // skip directory names
547     const char *p1, *p2;
548     p1 = buf;
549     int len = (int)strlen(os::file_separator());
550     while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
551     st->print("  [%s+0x%x]", p1, offset);
552   } else {
553     st->print("  " PTR_FORMAT, p2i(pc));
554   }
555 
556   found = os::dll_address_to_function_name(pc, buf, buflen, &offset);
557   if (found) {
558     st->print("  %s+0x%x", buf, offset);
559   }
560 }
561 
562 // frame::print_on_error() is called by fatal error handler. Notice that we may
563 // crash inside this function if stack frame is corrupted. The fatal error
564 // handler can catch and handle the crash. Here we assume the frame is valid.
565 //
566 // First letter indicates type of the frame:
567 //    J: Java frame (compiled)
568 //    A: Java frame (aot compiled)
569 //    j: Java frame (interpreted)
570 //    V: VM frame (C/C++)
571 //    v: Other frames running VM generated code (e.g. stubs, adapters, etc.)
572 //    C: C/C++ frame
573 //
574 // We don't need detailed frame type as that in frame::print_name(). "C"
575 // suggests the problem is in user lib; everything else is likely a VM bug.
576 
print_on_error(outputStream * st,char * buf,int buflen,bool verbose) const577 void frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const {
578   if (_cb != NULL) {
579     if (Interpreter::contains(pc())) {
580       Method* m = this->interpreter_frame_method();
581       if (m != NULL) {
582         m->name_and_sig_as_C_string(buf, buflen);
583         st->print("j  %s", buf);
584         st->print("+%d", this->interpreter_frame_bci());
585         ModuleEntry* module = m->method_holder()->module();
586         if (module->is_named()) {
587           module->name()->as_C_string(buf, buflen);
588           st->print(" %s", buf);
589           if (module->version() != NULL) {
590             module->version()->as_C_string(buf, buflen);
591             st->print("@%s", buf);
592           }
593         }
594       } else {
595         st->print("j  " PTR_FORMAT, p2i(pc()));
596       }
597     } else if (StubRoutines::contains(pc())) {
598       StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
599       if (desc != NULL) {
600         st->print("v  ~StubRoutines::%s", desc->name());
601       } else {
602         st->print("v  ~StubRoutines::" PTR_FORMAT, p2i(pc()));
603       }
604     } else if (_cb->is_buffer_blob()) {
605       st->print("v  ~BufferBlob::%s", ((BufferBlob *)_cb)->name());
606     } else if (_cb->is_compiled()) {
607       CompiledMethod* cm = (CompiledMethod*)_cb;
608       Method* m = cm->method();
609       if (m != NULL) {
610         if (cm->is_aot()) {
611           st->print("A %d ", cm->compile_id());
612         } else if (cm->is_nmethod()) {
613           nmethod* nm = cm->as_nmethod();
614           st->print("J %d%s", nm->compile_id(), (nm->is_osr_method() ? "%" : ""));
615           st->print(" %s", nm->compiler_name());
616         }
617         m->name_and_sig_as_C_string(buf, buflen);
618         st->print(" %s", buf);
619         ModuleEntry* module = m->method_holder()->module();
620         if (module->is_named()) {
621           module->name()->as_C_string(buf, buflen);
622           st->print(" %s", buf);
623           if (module->version() != NULL) {
624             module->version()->as_C_string(buf, buflen);
625             st->print("@%s", buf);
626           }
627         }
628         st->print(" (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+" INTPTR_FORMAT "]",
629                   m->code_size(), p2i(_pc), p2i(_cb->code_begin()), _pc - _cb->code_begin());
630 #if INCLUDE_JVMCI
631         if (cm->is_nmethod()) {
632           nmethod* nm = cm->as_nmethod();
633           const char* jvmciName = nm->jvmci_name();
634           if (jvmciName != NULL) {
635             st->print(" (%s)", jvmciName);
636           }
637         }
638 #endif
639       } else {
640         st->print("J  " PTR_FORMAT, p2i(pc()));
641       }
642     } else if (_cb->is_runtime_stub()) {
643       st->print("v  ~RuntimeStub::%s", ((RuntimeStub *)_cb)->name());
644     } else if (_cb->is_deoptimization_stub()) {
645       st->print("v  ~DeoptimizationBlob");
646     } else if (_cb->is_exception_stub()) {
647       st->print("v  ~ExceptionBlob");
648     } else if (_cb->is_safepoint_stub()) {
649       st->print("v  ~SafepointBlob");
650     } else if (_cb->is_adapter_blob()) {
651       st->print("v  ~AdapterBlob");
652     } else if (_cb->is_vtable_blob()) {
653       st->print("v  ~VtableBlob");
654     } else if (_cb->is_method_handles_adapter_blob()) {
655       st->print("v  ~MethodHandlesAdapterBlob");
656     } else if (_cb->is_uncommon_trap_stub()) {
657       st->print("v  ~UncommonTrapBlob");
658     } else {
659       st->print("v  blob " PTR_FORMAT, p2i(pc()));
660     }
661   } else {
662     print_C_frame(st, buf, buflen, pc());
663   }
664 }
665 
666 
667 /*
668   The interpreter_frame_expression_stack_at method in the case of SPARC needs the
669   max_stack value of the method in order to compute the expression stack address.
670   It uses the Method* in order to get the max_stack value but during GC this
671   Method* value saved on the frame is changed by reverse_and_push and hence cannot
672   be used. So we save the max_stack value in the FrameClosure object and pass it
673   down to the interpreter_frame_expression_stack_at method
674 */
675 class InterpreterFrameClosure : public OffsetClosure {
676  private:
677   const frame* _fr;
678   OopClosure*  _f;
679   int          _max_locals;
680   int          _max_stack;
681 
682  public:
InterpreterFrameClosure(const frame * fr,int max_locals,int max_stack,OopClosure * f)683   InterpreterFrameClosure(const frame* fr, int max_locals, int max_stack,
684                           OopClosure* f) {
685     _fr         = fr;
686     _max_locals = max_locals;
687     _max_stack  = max_stack;
688     _f          = f;
689   }
690 
offset_do(int offset)691   void offset_do(int offset) {
692     oop* addr;
693     if (offset < _max_locals) {
694       addr = (oop*) _fr->interpreter_frame_local_at(offset);
695       assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");
696       _f->do_oop(addr);
697     } else {
698       addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));
699       // In case of exceptions, the expression stack is invalid and the esp will be reset to express
700       // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
701       bool in_stack;
702       if (frame::interpreter_frame_expression_stack_direction() > 0) {
703         in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();
704       } else {
705         in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();
706       }
707       if (in_stack) {
708         _f->do_oop(addr);
709       }
710     }
711   }
712 
max_locals()713   int max_locals()  { return _max_locals; }
714 };
715 
716 
717 class InterpretedArgumentOopFinder: public SignatureIterator {
718  private:
719   OopClosure*  _f;             // Closure to invoke
720   int          _offset;        // TOS-relative offset, decremented with each argument
721   bool         _has_receiver;  // true if the callee has a receiver
722   const frame* _fr;
723 
724   friend class SignatureIterator;  // so do_parameters_on can call do_type
do_type(BasicType type)725   void do_type(BasicType type) {
726     _offset -= parameter_type_word_count(type);
727     if (is_reference_type(type)) oop_offset_do();
728    }
729 
oop_offset_do()730   void oop_offset_do() {
731     oop* addr;
732     addr = (oop*)_fr->interpreter_frame_tos_at(_offset);
733     _f->do_oop(addr);
734   }
735 
736  public:
InterpretedArgumentOopFinder(Symbol * signature,bool has_receiver,const frame * fr,OopClosure * f)737   InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, const frame* fr, OopClosure* f) : SignatureIterator(signature), _has_receiver(has_receiver) {
738     // compute size of arguments
739     int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0);
740     assert(!fr->is_interpreted_frame() ||
741            args_size <= fr->interpreter_frame_expression_stack_size(),
742             "args cannot be on stack anymore");
743     // initialize InterpretedArgumentOopFinder
744     _f         = f;
745     _fr        = fr;
746     _offset    = args_size;
747   }
748 
oops_do()749   void oops_do() {
750     if (_has_receiver) {
751       --_offset;
752       oop_offset_do();
753     }
754     do_parameters_on(this);
755   }
756 };
757 
758 
759 // Entry frame has following form (n arguments)
760 //         +-----------+
761 //   sp -> |  last arg |
762 //         +-----------+
763 //         :    :::    :
764 //         +-----------+
765 // (sp+n)->|  first arg|
766 //         +-----------+
767 
768 
769 
770 // visits and GC's all the arguments in entry frame
771 class EntryFrameOopFinder: public SignatureIterator {
772  private:
773   bool         _is_static;
774   int          _offset;
775   const frame* _fr;
776   OopClosure*  _f;
777 
778   friend class SignatureIterator;  // so do_parameters_on can call do_type
do_type(BasicType type)779   void do_type(BasicType type) {
780     // decrement offset before processing the type
781     _offset -= parameter_type_word_count(type);
782     assert (_offset >= 0, "illegal offset");
783     if (is_reference_type(type))  oop_at_offset_do(_offset);
784  }
785 
oop_at_offset_do(int offset)786   void oop_at_offset_do(int offset) {
787     assert (offset >= 0, "illegal offset");
788     oop* addr = (oop*) _fr->entry_frame_argument_at(offset);
789     _f->do_oop(addr);
790   }
791 
792  public:
EntryFrameOopFinder(const frame * frame,Symbol * signature,bool is_static)793   EntryFrameOopFinder(const frame* frame, Symbol* signature, bool is_static) : SignatureIterator(signature) {
794     _f = NULL; // will be set later
795     _fr = frame;
796     _is_static = is_static;
797     _offset = ArgumentSizeComputer(signature).size();  // pre-decremented down to zero
798   }
799 
arguments_do(OopClosure * f)800   void arguments_do(OopClosure* f) {
801     _f = f;
802     if (!_is_static)  oop_at_offset_do(_offset); // do the receiver
803     do_parameters_on(this);
804   }
805 
806 };
807 
interpreter_callee_receiver_addr(Symbol * signature)808 oop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
809   ArgumentSizeComputer asc(signature);
810   int size = asc.size();
811   return (oop *)interpreter_frame_tos_at(size);
812 }
813 
814 
oops_interpreted_do(OopClosure * f,const RegisterMap * map,bool query_oop_map_cache) const815 void frame::oops_interpreted_do(OopClosure* f, const RegisterMap* map, bool query_oop_map_cache) const {
816   assert(is_interpreted_frame(), "Not an interpreted frame");
817   assert(map != NULL, "map must be set");
818   Thread *thread = Thread::current();
819   methodHandle m (thread, interpreter_frame_method());
820   jint      bci = interpreter_frame_bci();
821 
822   assert(!Universe::heap()->is_in(m()),
823           "must be valid oop");
824   assert(m->is_method(), "checking frame value");
825   assert((m->is_native() && bci == 0)  ||
826          (!m->is_native() && bci >= 0 && bci < m->code_size()),
827          "invalid bci value");
828 
829   // Handle the monitor elements in the activation
830   for (
831     BasicObjectLock* current = interpreter_frame_monitor_end();
832     current < interpreter_frame_monitor_begin();
833     current = next_monitor_in_interpreter_frame(current)
834   ) {
835 #ifdef ASSERT
836     interpreter_frame_verify_monitor(current);
837 #endif
838     current->oops_do(f);
839   }
840 
841   if (m->is_native()) {
842     f->do_oop(interpreter_frame_temp_oop_addr());
843   }
844 
845   // The method pointer in the frame might be the only path to the method's
846   // klass, and the klass needs to be kept alive while executing. The GCs
847   // don't trace through method pointers, so the mirror of the method's klass
848   // is installed as a GC root.
849   f->do_oop(interpreter_frame_mirror_addr());
850 
851   int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
852 
853   Symbol* signature = NULL;
854   bool has_receiver = false;
855 
856   // Process a callee's arguments if we are at a call site
857   // (i.e., if we are at an invoke bytecode)
858   // This is used sometimes for calling into the VM, not for another
859   // interpreted or compiled frame.
860   if (!m->is_native()) {
861     Bytecode_invoke call = Bytecode_invoke_check(m, bci);
862     if (call.is_valid()) {
863       signature = call.signature();
864       has_receiver = call.has_receiver();
865       if (map->include_argument_oops() &&
866           interpreter_frame_expression_stack_size() > 0) {
867         ResourceMark rm(thread);  // is this right ???
868         // we are at a call site & the expression stack is not empty
869         // => process callee's arguments
870         //
871         // Note: The expression stack can be empty if an exception
872         //       occurred during method resolution/execution. In all
873         //       cases we empty the expression stack completely be-
874         //       fore handling the exception (the exception handling
875         //       code in the interpreter calls a blocking runtime
876         //       routine which can cause this code to be executed).
877         //       (was bug gri 7/27/98)
878         oops_interpreted_arguments_do(signature, has_receiver, f);
879       }
880     }
881   }
882 
883   InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);
884 
885   // process locals & expression stack
886   InterpreterOopMap mask;
887   if (query_oop_map_cache) {
888     m->mask_for(bci, &mask);
889   } else {
890     OopMapCache::compute_one_oop_map(m, bci, &mask);
891   }
892   mask.iterate_oop(&blk);
893 }
894 
895 
oops_interpreted_arguments_do(Symbol * signature,bool has_receiver,OopClosure * f) const896 void frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) const {
897   InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
898   finder.oops_do();
899 }
900 
oops_code_blob_do(OopClosure * f,CodeBlobClosure * cf,const RegisterMap * reg_map,DerivedPointerIterationMode derived_mode) const901 void frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* reg_map,
902                               DerivedPointerIterationMode derived_mode) const {
903   assert(_cb != NULL, "sanity check");
904   if (_cb->oop_maps() != NULL) {
905     OopMapSet::oops_do(this, reg_map, f, derived_mode);
906 
907     // Preserve potential arguments for a callee. We handle this by dispatching
908     // on the codeblob. For c2i, we do
909     if (reg_map->include_argument_oops()) {
910       _cb->preserve_callee_argument_oops(*this, reg_map, f);
911     }
912   }
913   // In cases where perm gen is collected, GC will want to mark
914   // oops referenced from nmethods active on thread stacks so as to
915   // prevent them from being collected. However, this visit should be
916   // restricted to certain phases of the collection only. The
917   // closure decides how it wants nmethods to be traced.
918   if (cf != NULL)
919     cf->do_code_blob(_cb);
920 }
921 
922 class CompiledArgumentOopFinder: public SignatureIterator {
923  protected:
924   OopClosure*     _f;
925   int             _offset;        // the current offset, incremented with each argument
926   bool            _has_receiver;  // true if the callee has a receiver
927   bool            _has_appendix;  // true if the call has an appendix
928   frame           _fr;
929   RegisterMap*    _reg_map;
930   int             _arg_size;
931   VMRegPair*      _regs;        // VMReg list of arguments
932 
933   friend class SignatureIterator;  // so do_parameters_on can call do_type
do_type(BasicType type)934   void do_type(BasicType type) {
935     if (is_reference_type(type))  handle_oop_offset();
936     _offset += parameter_type_word_count(type);
937   }
938 
handle_oop_offset()939   virtual void handle_oop_offset() {
940     // Extract low order register number from register array.
941     // In LP64-land, the high-order bits are valid but unhelpful.
942     VMReg reg = _regs[_offset].first();
943     oop *loc = _fr.oopmapreg_to_location(reg, _reg_map);
944     assert(loc != NULL, "missing register map entry");
945     _f->do_oop(loc);
946   }
947 
948  public:
CompiledArgumentOopFinder(Symbol * signature,bool has_receiver,bool has_appendix,OopClosure * f,frame fr,const RegisterMap * reg_map)949   CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr, const RegisterMap* reg_map)
950     : SignatureIterator(signature) {
951 
952     // initialize CompiledArgumentOopFinder
953     _f         = f;
954     _offset    = 0;
955     _has_receiver = has_receiver;
956     _has_appendix = has_appendix;
957     _fr        = fr;
958     _reg_map   = (RegisterMap*)reg_map;
959     _arg_size  = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
960 
961     int arg_size;
962     _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
963     assert(arg_size == _arg_size, "wrong arg size");
964   }
965 
oops_do()966   void oops_do() {
967     if (_has_receiver) {
968       handle_oop_offset();
969       _offset++;
970     }
971     do_parameters_on(this);
972     if (_has_appendix) {
973       handle_oop_offset();
974       _offset++;
975     }
976   }
977 };
978 
oops_compiled_arguments_do(Symbol * signature,bool has_receiver,bool has_appendix,const RegisterMap * reg_map,OopClosure * f) const979 void frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix,
980                                        const RegisterMap* reg_map, OopClosure* f) const {
981   ResourceMark rm;
982   CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
983   finder.oops_do();
984 }
985 
986 
987 // Get receiver out of callers frame, i.e. find parameter 0 in callers
988 // frame.  Consult ADLC for where parameter 0 is to be found.  Then
989 // check local reg_map for it being a callee-save register or argument
990 // register, both of which are saved in the local frame.  If not found
991 // there, it must be an in-stack argument of the caller.
992 // Note: caller.sp() points to callee-arguments
retrieve_receiver(RegisterMap * reg_map)993 oop frame::retrieve_receiver(RegisterMap* reg_map) {
994   frame caller = *this;
995 
996   // First consult the ADLC on where it puts parameter 0 for this signature.
997   VMReg reg = SharedRuntime::name_for_receiver();
998   oop* oop_adr = caller.oopmapreg_to_location(reg, reg_map);
999   if (oop_adr == NULL) {
1000     guarantee(oop_adr != NULL, "bad register save location");
1001     return NULL;
1002   }
1003   oop r = *oop_adr;
1004   assert(Universe::heap()->is_in_or_null(r), "bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", p2i(r), p2i(r));
1005   return r;
1006 }
1007 
1008 
get_native_monitor()1009 BasicLock* frame::get_native_monitor() {
1010   nmethod* nm = (nmethod*)_cb;
1011   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1012          "Should not call this unless it's a native nmethod");
1013   int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1014   assert(byte_offset >= 0, "should not see invalid offset");
1015   return (BasicLock*) &sp()[byte_offset / wordSize];
1016 }
1017 
get_native_receiver()1018 oop frame::get_native_receiver() {
1019   nmethod* nm = (nmethod*)_cb;
1020   assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1021          "Should not call this unless it's a native nmethod");
1022   int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1023   assert(byte_offset >= 0, "should not see invalid offset");
1024   oop owner = ((oop*) sp())[byte_offset / wordSize];
1025   assert( Universe::heap()->is_in(owner), "bad receiver" );
1026   return owner;
1027 }
1028 
oops_entry_do(OopClosure * f,const RegisterMap * map) const1029 void frame::oops_entry_do(OopClosure* f, const RegisterMap* map) const {
1030   assert(map != NULL, "map must be set");
1031   if (map->include_argument_oops()) {
1032     // must collect argument oops, as nobody else is doing it
1033     Thread *thread = Thread::current();
1034     methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1035     EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1036     finder.arguments_do(f);
1037   }
1038   // Traverse the Handle Block saved in the entry frame
1039   entry_frame_call_wrapper()->oops_do(f);
1040 }
1041 
oops_do(OopClosure * f,CodeBlobClosure * cf,const RegisterMap * map,DerivedPointerIterationMode derived_mode) const1042 void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
1043                     DerivedPointerIterationMode derived_mode) const {
1044   oops_do_internal(f, cf, map, true, derived_mode);
1045 }
1046 
oops_do(OopClosure * f,CodeBlobClosure * cf,const RegisterMap * map) const1047 void frame::oops_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map) const {
1048 #if COMPILER2_OR_JVMCI
1049   oops_do_internal(f, cf, map, true, DerivedPointerTable::is_active() ?
1050                                      DerivedPointerIterationMode::_with_table :
1051                                      DerivedPointerIterationMode::_ignore);
1052 #else
1053   oops_do_internal(f, cf, map, true, DerivedPointerIterationMode::_ignore);
1054 #endif
1055 }
1056 
oops_do_internal(OopClosure * f,CodeBlobClosure * cf,const RegisterMap * map,bool use_interpreter_oop_map_cache,DerivedPointerIterationMode derived_mode) const1057 void frame::oops_do_internal(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* map,
1058                              bool use_interpreter_oop_map_cache, DerivedPointerIterationMode derived_mode) const {
1059 #ifndef PRODUCT
1060   // simulate GC crash here to dump java thread in error report
1061   if (CrashGCForDumpingJavaThread) {
1062     char *t = NULL;
1063     *t = 'c';
1064   }
1065 #endif
1066   if (is_interpreted_frame()) {
1067     oops_interpreted_do(f, map, use_interpreter_oop_map_cache);
1068   } else if (is_entry_frame()) {
1069     oops_entry_do(f, map);
1070   } else if (CodeCache::contains(pc())) {
1071     oops_code_blob_do(f, cf, map, derived_mode);
1072   } else {
1073     ShouldNotReachHere();
1074   }
1075 }
1076 
nmethods_do(CodeBlobClosure * cf) const1077 void frame::nmethods_do(CodeBlobClosure* cf) const {
1078   if (_cb != NULL && _cb->is_nmethod()) {
1079     cf->do_code_blob(_cb);
1080   }
1081 }
1082 
1083 
1084 // Call f closure on the interpreted Method*s in the stack.
metadata_do(MetadataClosure * f) const1085 void frame::metadata_do(MetadataClosure* f) const {
1086   ResourceMark rm;
1087   if (is_interpreted_frame()) {
1088     Method* m = this->interpreter_frame_method();
1089     assert(m != NULL, "expecting a method in this frame");
1090     f->do_metadata(m);
1091   }
1092 }
1093 
verify(const RegisterMap * map) const1094 void frame::verify(const RegisterMap* map) const {
1095   // for now make sure receiver type is correct
1096   if (is_interpreted_frame()) {
1097     Method* method = interpreter_frame_method();
1098     guarantee(method->is_method(), "method is wrong in frame::verify");
1099     if (!method->is_static()) {
1100       // fetch the receiver
1101       oop* p = (oop*) interpreter_frame_local_at(0);
1102       // make sure we have the right receiver type
1103     }
1104   }
1105 #if COMPILER2_OR_JVMCI
1106   assert(DerivedPointerTable::is_empty(), "must be empty before verify");
1107 #endif
1108   oops_do_internal(&VerifyOopClosure::verify_oop, NULL, map, false, DerivedPointerIterationMode::_ignore);
1109 }
1110 
1111 
1112 #ifdef ASSERT
verify_return_pc(address x)1113 bool frame::verify_return_pc(address x) {
1114   if (StubRoutines::returns_to_call_stub(x)) {
1115     return true;
1116   }
1117   if (CodeCache::contains(x)) {
1118     return true;
1119   }
1120   if (Interpreter::contains(x)) {
1121     return true;
1122   }
1123   return false;
1124 }
1125 #endif
1126 
1127 #ifdef ASSERT
interpreter_frame_verify_monitor(BasicObjectLock * value) const1128 void frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1129   assert(is_interpreted_frame(), "Not an interpreted frame");
1130   // verify that the value is in the right part of the frame
1131   address low_mark  = (address) interpreter_frame_monitor_end();
1132   address high_mark = (address) interpreter_frame_monitor_begin();
1133   address current   = (address) value;
1134 
1135   const int monitor_size = frame::interpreter_frame_monitor_size();
1136   guarantee((high_mark - current) % monitor_size  ==  0         , "Misaligned top of BasicObjectLock*");
1137   guarantee( high_mark > current                                , "Current BasicObjectLock* higher than high_mark");
1138 
1139   guarantee((current - low_mark) % monitor_size  ==  0         , "Misaligned bottom of BasicObjectLock*");
1140   guarantee( current >= low_mark                               , "Current BasicObjectLock* below than low_mark");
1141 }
1142 #endif
1143 
1144 #ifndef PRODUCT
1145 // callers need a ResourceMark because of name_and_sig_as_C_string() usage,
1146 // RA allocated string is returned to the caller
describe(FrameValues & values,int frame_no)1147 void frame::describe(FrameValues& values, int frame_no) {
1148   // boundaries: sp and the 'real' frame pointer
1149   values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 1);
1150   intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1151 
1152   // print frame info at the highest boundary
1153   intptr_t* info_address = MAX2(sp(), frame_pointer);
1154 
1155   if (info_address != frame_pointer) {
1156     // print frame_pointer explicitly if not marked by the frame info
1157     values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1158   }
1159 
1160   if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1161     // Label values common to most frames
1162     values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no));
1163   }
1164 
1165   if (is_interpreted_frame()) {
1166     Method* m = interpreter_frame_method();
1167     int bci = interpreter_frame_bci();
1168 
1169     // Label the method and current bci
1170     values.describe(-1, info_address,
1171                     FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 2);
1172     values.describe(-1, info_address,
1173                     err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 1);
1174     if (m->max_locals() > 0) {
1175       intptr_t* l0 = interpreter_frame_local_at(0);
1176       intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1177       values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 1);
1178       // Report each local and mark as owned by this frame
1179       for (int l = 0; l < m->max_locals(); l++) {
1180         intptr_t* l0 = interpreter_frame_local_at(l);
1181         values.describe(frame_no, l0, err_msg("local %d", l));
1182       }
1183     }
1184 
1185     // Compute the actual expression stack size
1186     InterpreterOopMap mask;
1187     OopMapCache::compute_one_oop_map(methodHandle(Thread::current(), m), bci, &mask);
1188     intptr_t* tos = NULL;
1189     // Report each stack element and mark as owned by this frame
1190     for (int e = 0; e < mask.expression_stack_size(); e++) {
1191       tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1192       values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1193                       err_msg("stack %d", e));
1194     }
1195     if (tos != NULL) {
1196       values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 1);
1197     }
1198     if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1199       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1200       values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");
1201     }
1202   } else if (is_entry_frame()) {
1203     // For now just label the frame
1204     values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1205   } else if (is_compiled_frame()) {
1206     // For now just label the frame
1207     CompiledMethod* cm = (CompiledMethod*)cb();
1208     values.describe(-1, info_address,
1209                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method %s%s%s", frame_no,
1210                                        p2i(cm),
1211                                        (cm->is_aot() ? "A ": "J "),
1212                                        cm->method()->name_and_sig_as_C_string(),
1213                                        (_deopt_state == is_deoptimized) ?
1214                                        " (deoptimized)" :
1215                                        ((_deopt_state == unknown) ? " (state unknown)" : "")),
1216                     2);
1217   } else if (is_native_frame()) {
1218     // For now just label the frame
1219     nmethod* nm = cb()->as_nmethod_or_null();
1220     values.describe(-1, info_address,
1221                     FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1222                                        p2i(nm), nm->method()->name_and_sig_as_C_string()), 2);
1223   } else {
1224     // provide default info if not handled before
1225     char *info = (char *) "special frame";
1226     if ((_cb != NULL) &&
1227         (_cb->name() != NULL)) {
1228       info = (char *)_cb->name();
1229     }
1230     values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1231   }
1232 
1233   // platform dependent additional data
1234   describe_pd(values, frame_no);
1235 }
1236 
1237 #endif
1238 
1239 
1240 //-----------------------------------------------------------------------------------
1241 // StackFrameStream implementation
1242 
StackFrameStream(JavaThread * thread,bool update,bool process_frames)1243 StackFrameStream::StackFrameStream(JavaThread *thread, bool update, bool process_frames) : _reg_map(thread, update, process_frames) {
1244   assert(thread->has_last_Java_frame(), "sanity check");
1245   _fr = thread->last_frame();
1246   _is_done = false;
1247 }
1248 
1249 
1250 #ifndef PRODUCT
1251 
describe(int owner,intptr_t * location,const char * description,int priority)1252 void FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1253   FrameValue fv;
1254   fv.location = location;
1255   fv.owner = owner;
1256   fv.priority = priority;
1257   fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1258   strcpy(fv.description, description);
1259   _values.append(fv);
1260 }
1261 
1262 
1263 #ifdef ASSERT
validate()1264 void FrameValues::validate() {
1265   _values.sort(compare);
1266   bool error = false;
1267   FrameValue prev;
1268   prev.owner = -1;
1269   for (int i = _values.length() - 1; i >= 0; i--) {
1270     FrameValue fv = _values.at(i);
1271     if (fv.owner == -1) continue;
1272     if (prev.owner == -1) {
1273       prev = fv;
1274       continue;
1275     }
1276     if (prev.location == fv.location) {
1277       if (fv.owner != prev.owner) {
1278         tty->print_cr("overlapping storage");
1279         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(prev.location), *prev.location, prev.description);
1280         tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1281         error = true;
1282       }
1283     } else {
1284       prev = fv;
1285     }
1286   }
1287   assert(!error, "invalid layout");
1288 }
1289 #endif // ASSERT
1290 
print_on(JavaThread * thread,outputStream * st)1291 void FrameValues::print_on(JavaThread* thread, outputStream* st) {
1292   _values.sort(compare);
1293 
1294   // Sometimes values like the fp can be invalid values if the
1295   // register map wasn't updated during the walk.  Trim out values
1296   // that aren't actually in the stack of the thread.
1297   int min_index = 0;
1298   int max_index = _values.length() - 1;
1299   intptr_t* v0 = _values.at(min_index).location;
1300   intptr_t* v1 = _values.at(max_index).location;
1301 
1302   if (thread == Thread::current()) {
1303     while (!thread->is_in_live_stack((address)v0)) {
1304       v0 = _values.at(++min_index).location;
1305     }
1306     while (!thread->is_in_live_stack((address)v1)) {
1307       v1 = _values.at(--max_index).location;
1308     }
1309   } else {
1310     while (!thread->is_in_full_stack((address)v0)) {
1311       v0 = _values.at(++min_index).location;
1312     }
1313     while (!thread->is_in_full_stack((address)v1)) {
1314       v1 = _values.at(--max_index).location;
1315     }
1316   }
1317   intptr_t* min = MIN2(v0, v1);
1318   intptr_t* max = MAX2(v0, v1);
1319   intptr_t* cur = max;
1320   intptr_t* last = NULL;
1321   for (int i = max_index; i >= min_index; i--) {
1322     FrameValue fv = _values.at(i);
1323     while (cur > fv.location) {
1324       st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, p2i(cur), *cur);
1325       cur--;
1326     }
1327     if (last == fv.location) {
1328       const char* spacer = "          " LP64_ONLY("        ");
1329       st->print_cr(" %s  %s %s", spacer, spacer, fv.description);
1330     } else {
1331       st->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", p2i(fv.location), *fv.location, fv.description);
1332       last = fv.location;
1333       cur--;
1334     }
1335   }
1336 }
1337 
1338 #endif // ndef PRODUCT
1339