frame.cpp revision 6683:08a2164660fb
1254885Sdumbbell/*
2254885Sdumbbell * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3254885Sdumbbell * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254885Sdumbbell *
5254885Sdumbbell * This code is free software; you can redistribute it and/or modify it
6254885Sdumbbell * under the terms of the GNU General Public License version 2 only, as
7254885Sdumbbell * published by the Free Software Foundation.
8254885Sdumbbell *
9254885Sdumbbell * This code is distributed in the hope that it will be useful, but WITHOUT
10254885Sdumbbell * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11254885Sdumbbell * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12254885Sdumbbell * version 2 for more details (a copy is included in the LICENSE file that
13254885Sdumbbell * accompanied this code).
14254885Sdumbbell *
15254885Sdumbbell * You should have received a copy of the GNU General Public License version
16254885Sdumbbell * 2 along with this work; if not, write to the Free Software Foundation,
17254885Sdumbbell * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18254885Sdumbbell *
19254885Sdumbbell * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20254885Sdumbbell * or visit www.oracle.com if you need additional information or have any
21254885Sdumbbell * questions.
22254885Sdumbbell *
23254885Sdumbbell */
24254885Sdumbbell
25254885Sdumbbell#include "precompiled.hpp"
26254885Sdumbbell#include "compiler/abstractCompiler.hpp"
27254885Sdumbbell#include "compiler/disassembler.hpp"
28254885Sdumbbell#include "gc_interface/collectedHeap.inline.hpp"
29254885Sdumbbell#include "interpreter/interpreter.hpp"
30254885Sdumbbell#include "interpreter/oopMapCache.hpp"
31254885Sdumbbell#include "memory/resourceArea.hpp"
32254885Sdumbbell#include "memory/universe.inline.hpp"
33254885Sdumbbell#include "oops/markOop.hpp"
34254885Sdumbbell#include "oops/methodData.hpp"
35254885Sdumbbell#include "oops/method.hpp"
36254885Sdumbbell#include "oops/oop.inline.hpp"
37254885Sdumbbell#include "oops/oop.inline2.hpp"
38254885Sdumbbell#include "prims/methodHandles.hpp"
39254885Sdumbbell#include "runtime/frame.inline.hpp"
40254885Sdumbbell#include "runtime/handles.inline.hpp"
41254885Sdumbbell#include "runtime/javaCalls.hpp"
42254885Sdumbbell#include "runtime/monitorChunk.hpp"
43254885Sdumbbell#include "runtime/os.hpp"
44254885Sdumbbell#include "runtime/sharedRuntime.hpp"
45254885Sdumbbell#include "runtime/signature.hpp"
46254885Sdumbbell#include "runtime/stubCodeGenerator.hpp"
47254885Sdumbbell#include "runtime/stubRoutines.hpp"
48254885Sdumbbell#include "runtime/thread.inline.hpp"
49254885Sdumbbell#include "utilities/decoder.hpp"
50254885Sdumbbell
51254885Sdumbbell#ifdef TARGET_ARCH_x86
52254885Sdumbbell# include "nativeInst_x86.hpp"
53254885Sdumbbell#endif
54254885Sdumbbell#ifdef TARGET_ARCH_sparc
55254885Sdumbbell# include "nativeInst_sparc.hpp"
56254885Sdumbbell#endif
57254885Sdumbbell#ifdef TARGET_ARCH_zero
58254885Sdumbbell# include "nativeInst_zero.hpp"
59254885Sdumbbell#endif
60254885Sdumbbell#ifdef TARGET_ARCH_arm
61254885Sdumbbell# include "nativeInst_arm.hpp"
62254885Sdumbbell#endif
63254885Sdumbbell#ifdef TARGET_ARCH_ppc
64254885Sdumbbell# include "nativeInst_ppc.hpp"
65254885Sdumbbell#endif
66254885Sdumbbell
67254885SdumbbellPRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
68254885Sdumbbell
69254885SdumbbellRegisterMap::RegisterMap(JavaThread *thread, bool update_map) {
70254885Sdumbbell  _thread         = thread;
71254885Sdumbbell  _update_map     = update_map;
72254885Sdumbbell  clear();
73254885Sdumbbell  debug_only(_update_for_id = NULL;)
74254885Sdumbbell#ifndef PRODUCT
75254885Sdumbbell  for (int i = 0; i < reg_count ; i++ ) _location[i] = NULL;
76254885Sdumbbell#endif /* PRODUCT */
77254885Sdumbbell}
78254885Sdumbbell
79254885SdumbbellRegisterMap::RegisterMap(const RegisterMap* map) {
80254885Sdumbbell  assert(map != this, "bad initialization parameter");
81254885Sdumbbell  assert(map != NULL, "RegisterMap must be present");
82254885Sdumbbell  _thread                = map->thread();
83254885Sdumbbell  _update_map            = map->update_map();
84254885Sdumbbell  _include_argument_oops = map->include_argument_oops();
85254885Sdumbbell  debug_only(_update_for_id = map->_update_for_id;)
86254885Sdumbbell  pd_initialize_from(map);
87254885Sdumbbell  if (update_map()) {
88254885Sdumbbell    for(int i = 0; i < location_valid_size; i++) {
89254885Sdumbbell      LocationValidType bits = !update_map() ? 0 : map->_location_valid[i];
90254885Sdumbbell      _location_valid[i] = bits;
91254885Sdumbbell      // for whichever bits are set, pull in the corresponding map->_location
92254885Sdumbbell      int j = i*location_valid_type_size;
93254885Sdumbbell      while (bits != 0) {
94254885Sdumbbell        if ((bits & 1) != 0) {
95254885Sdumbbell          assert(0 <= j && j < reg_count, "range check");
96254885Sdumbbell          _location[j] = map->_location[j];
97254885Sdumbbell        }
98254885Sdumbbell        bits >>= 1;
99254885Sdumbbell        j += 1;
100254885Sdumbbell      }
101254885Sdumbbell    }
102254885Sdumbbell  }
103254885Sdumbbell}
104254885Sdumbbell
105254885Sdumbbellvoid RegisterMap::clear() {
106254885Sdumbbell  set_include_argument_oops(true);
107254885Sdumbbell  if (_update_map) {
108254885Sdumbbell    for(int i = 0; i < location_valid_size; i++) {
109254885Sdumbbell      _location_valid[i] = 0;
110254885Sdumbbell    }
111254885Sdumbbell    pd_clear();
112254885Sdumbbell  } else {
113254885Sdumbbell    pd_initialize();
114254885Sdumbbell  }
115254885Sdumbbell}
116254885Sdumbbell
117254885Sdumbbell#ifndef PRODUCT
118254885Sdumbbell
119254885Sdumbbellvoid RegisterMap::print_on(outputStream* st) const {
120254885Sdumbbell  st->print_cr("Register map");
121254885Sdumbbell  for(int i = 0; i < reg_count; i++) {
122254885Sdumbbell
123254885Sdumbbell    VMReg r = VMRegImpl::as_VMReg(i);
124254885Sdumbbell    intptr_t* src = (intptr_t*) location(r);
125254885Sdumbbell    if (src != NULL) {
126254885Sdumbbell
127254885Sdumbbell      r->print_on(st);
128254885Sdumbbell      st->print(" [" INTPTR_FORMAT "] = ", src);
129254885Sdumbbell      if (((uintptr_t)src & (sizeof(*src)-1)) != 0) {
130254885Sdumbbell        st->print_cr("<misaligned>");
131254885Sdumbbell      } else {
132254885Sdumbbell        st->print_cr(INTPTR_FORMAT, *src);
133254885Sdumbbell      }
134254885Sdumbbell    }
135254885Sdumbbell  }
136254885Sdumbbell}
137254885Sdumbbell
138254885Sdumbbellvoid RegisterMap::print() const {
139254885Sdumbbell  print_on(tty);
140254885Sdumbbell}
141254885Sdumbbell
142254885Sdumbbell#endif
143254885Sdumbbell// This returns the pc that if you were in the debugger you'd see. Not
144254885Sdumbbell// the idealized value in the frame object. This undoes the magic conversion
145254885Sdumbbell// that happens for deoptimized frames. In addition it makes the value the
146254885Sdumbbell// hardware would want to see in the native frame. The only user (at this point)
147254885Sdumbbell// is deoptimization. It likely no one else should ever use it.
148254885Sdumbbell
149254885Sdumbbelladdress frame::raw_pc() const {
150254885Sdumbbell  if (is_deoptimized_frame()) {
151254885Sdumbbell    nmethod* nm = cb()->as_nmethod_or_null();
152254885Sdumbbell    if (nm->is_method_handle_return(pc()))
153254885Sdumbbell      return nm->deopt_mh_handler_begin() - pc_return_offset;
154254885Sdumbbell    else
155254885Sdumbbell      return nm->deopt_handler_begin() - pc_return_offset;
156254885Sdumbbell  } else {
157254885Sdumbbell    return (pc() - pc_return_offset);
158254885Sdumbbell  }
159254885Sdumbbell}
160254885Sdumbbell
161254885Sdumbbell// Change the pc in a frame object. This does not change the actual pc in
162254885Sdumbbell// actual frame. To do that use patch_pc.
163254885Sdumbbell//
164254885Sdumbbellvoid frame::set_pc(address   newpc ) {
165254885Sdumbbell#ifdef ASSERT
166254885Sdumbbell  if (_cb != NULL && _cb->is_nmethod()) {
167254885Sdumbbell    assert(!((nmethod*)_cb)->is_deopt_pc(_pc), "invariant violation");
168254885Sdumbbell  }
169254885Sdumbbell#endif // ASSERT
170254885Sdumbbell
171254885Sdumbbell  // Unsafe to use the is_deoptimzed tester after changing pc
172254885Sdumbbell  _deopt_state = unknown;
173254885Sdumbbell  _pc = newpc;
174254885Sdumbbell  _cb = CodeCache::find_blob_unsafe(_pc);
175254885Sdumbbell
176254885Sdumbbell}
177254885Sdumbbell
178254885Sdumbbell// type testers
179254885Sdumbbellbool frame::is_ignored_frame() const {
180254885Sdumbbell  return false;  // FIXME: some LambdaForm frames should be ignored
181254885Sdumbbell}
182254885Sdumbbellbool frame::is_deoptimized_frame() const {
183254885Sdumbbell  assert(_deopt_state != unknown, "not answerable");
184254885Sdumbbell  return _deopt_state == is_deoptimized;
185254885Sdumbbell}
186254885Sdumbbell
187254885Sdumbbellbool frame::is_native_frame() const {
188254885Sdumbbell  return (_cb != NULL &&
189254885Sdumbbell          _cb->is_nmethod() &&
190254885Sdumbbell          ((nmethod*)_cb)->is_native_method());
191254885Sdumbbell}
192254885Sdumbbell
193254885Sdumbbellbool frame::is_java_frame() const {
194254885Sdumbbell  if (is_interpreted_frame()) return true;
195254885Sdumbbell  if (is_compiled_frame())    return true;
196254885Sdumbbell  return false;
197254885Sdumbbell}
198254885Sdumbbell
199254885Sdumbbell
200254885Sdumbbellbool frame::is_compiled_frame() const {
201254885Sdumbbell  if (_cb != NULL &&
202254885Sdumbbell      _cb->is_nmethod() &&
203254885Sdumbbell      ((nmethod*)_cb)->is_java_method()) {
204254885Sdumbbell    return true;
205254885Sdumbbell  }
206254885Sdumbbell  return false;
207254885Sdumbbell}
208254885Sdumbbell
209254885Sdumbbell
210254885Sdumbbellbool frame::is_runtime_frame() const {
211254885Sdumbbell  return (_cb != NULL && _cb->is_runtime_stub());
212254885Sdumbbell}
213254885Sdumbbell
214254885Sdumbbellbool frame::is_safepoint_blob_frame() const {
215254885Sdumbbell  return (_cb != NULL && _cb->is_safepoint_stub());
216254885Sdumbbell}
217254885Sdumbbell
218254885Sdumbbell// testers
219254885Sdumbbell
220254885Sdumbbellbool frame::is_first_java_frame() const {
221254885Sdumbbell  RegisterMap map(JavaThread::current(), false); // No update
222254885Sdumbbell  frame s;
223254885Sdumbbell  for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map));
224254885Sdumbbell  return s.is_first_frame();
225254885Sdumbbell}
226254885Sdumbbell
227254885Sdumbbell
228254885Sdumbbellbool frame::entry_frame_is_first() const {
229254885Sdumbbell  return entry_frame_call_wrapper()->is_first_frame();
230254885Sdumbbell}
231254885Sdumbbell
232254885SdumbbellJavaCallWrapper* frame::entry_frame_call_wrapper_if_safe(JavaThread* thread) const {
233254885Sdumbbell  JavaCallWrapper** jcw = entry_frame_call_wrapper_addr();
234254885Sdumbbell  address addr = (address) jcw;
235254885Sdumbbell
236254885Sdumbbell  // addr must be within the usable part of the stack
237254885Sdumbbell  if (thread->is_in_usable_stack(addr)) {
238254885Sdumbbell    return *jcw;
239254885Sdumbbell  }
240254885Sdumbbell
241254885Sdumbbell  return NULL;
242254885Sdumbbell}
243254885Sdumbbell
244254885Sdumbbellbool frame::should_be_deoptimized() const {
245254885Sdumbbell  if (_deopt_state == is_deoptimized ||
246254885Sdumbbell      !is_compiled_frame() ) return false;
247254885Sdumbbell  assert(_cb != NULL && _cb->is_nmethod(), "must be an nmethod");
248254885Sdumbbell  nmethod* nm = (nmethod *)_cb;
249254885Sdumbbell  if (TraceDependencies) {
250254885Sdumbbell    tty->print("checking (%s) ", nm->is_marked_for_deoptimization() ? "true" : "false");
251254885Sdumbbell    nm->print_value_on(tty);
252254885Sdumbbell    tty->cr();
253254885Sdumbbell  }
254254885Sdumbbell
255254885Sdumbbell  if( !nm->is_marked_for_deoptimization() )
256254885Sdumbbell    return false;
257254885Sdumbbell
258254885Sdumbbell  // If at the return point, then the frame has already been popped, and
259254885Sdumbbell  // only the return needs to be executed. Don't deoptimize here.
260254885Sdumbbell  return !nm->is_at_poll_return(pc());
261254885Sdumbbell}
262254885Sdumbbell
263254885Sdumbbellbool frame::can_be_deoptimized() const {
264254885Sdumbbell  if (!is_compiled_frame()) return false;
265254885Sdumbbell  nmethod* nm = (nmethod*)_cb;
266254885Sdumbbell
267254885Sdumbbell  if( !nm->can_be_deoptimized() )
268254885Sdumbbell    return false;
269254885Sdumbbell
270254885Sdumbbell  return !nm->is_at_poll_return(pc());
271254885Sdumbbell}
272254885Sdumbbell
273254885Sdumbbellvoid frame::deoptimize(JavaThread* thread) {
274254885Sdumbbell  // Schedule deoptimization of an nmethod activation with this frame.
275254885Sdumbbell  assert(_cb != NULL && _cb->is_nmethod(), "must be");
276254885Sdumbbell  nmethod* nm = (nmethod*)_cb;
277254885Sdumbbell
278254885Sdumbbell  // This is a fix for register window patching race
279254885Sdumbbell  if (NeedsDeoptSuspend && Thread::current() != thread) {
280254885Sdumbbell    assert(SafepointSynchronize::is_at_safepoint(),
281254885Sdumbbell           "patching other threads for deopt may only occur at a safepoint");
282254885Sdumbbell
283254885Sdumbbell    // It is possible especially with DeoptimizeALot/DeoptimizeRandom that
284254885Sdumbbell    // we could see the frame again and ask for it to be deoptimized since
285254885Sdumbbell    // it might move for a long time. That is harmless and we just ignore it.
286254885Sdumbbell    if (id() == thread->must_deopt_id()) {
287254885Sdumbbell      assert(thread->is_deopt_suspend(), "lost suspension");
288254885Sdumbbell      return;
289254885Sdumbbell    }
290254885Sdumbbell
291254885Sdumbbell    // We are at a safepoint so the target thread can only be
292254885Sdumbbell    // in 4 states:
293254885Sdumbbell    //     blocked - no problem
294254885Sdumbbell    //     blocked_trans - no problem (i.e. could have woken up from blocked
295254885Sdumbbell    //                                 during a safepoint).
296254885Sdumbbell    //     native - register window pc patching race
297254885Sdumbbell    //     native_trans - momentary state
298254885Sdumbbell    //
299254885Sdumbbell    // We could just wait out a thread in native_trans to block.
300254885Sdumbbell    // Then we'd have all the issues that the safepoint code has as to
301254885Sdumbbell    // whether to spin or block. It isn't worth it. Just treat it like
302254885Sdumbbell    // native and be done with it.
303254885Sdumbbell    //
304254885Sdumbbell    // Examine the state of the thread at the start of safepoint since
305254885Sdumbbell    // threads that were in native at the start of the safepoint could
306254885Sdumbbell    // come to a halt during the safepoint, changing the current value
307254885Sdumbbell    // of the safepoint_state.
308254885Sdumbbell    JavaThreadState state = thread->safepoint_state()->orig_thread_state();
309254885Sdumbbell    if (state == _thread_in_native || state == _thread_in_native_trans) {
310254885Sdumbbell      // Since we are at a safepoint the target thread will stop itself
311254885Sdumbbell      // before it can return to java as long as we remain at the safepoint.
312254885Sdumbbell      // Therefore we can put an additional request for the thread to stop
313254885Sdumbbell      // no matter what no (like a suspend). This will cause the thread
314254885Sdumbbell      // to notice it needs to do the deopt on its own once it leaves native.
315254885Sdumbbell      //
316254885Sdumbbell      // The only reason we must do this is because on machine with register
317254885Sdumbbell      // windows we have a race with patching the return address and the
318254885Sdumbbell      // window coming live as the thread returns to the Java code (but still
319254885Sdumbbell      // in native mode) and then blocks. It is only this top most frame
320254885Sdumbbell      // that is at risk. So in truth we could add an additional check to
321254885Sdumbbell      // see if this frame is one that is at risk.
322254885Sdumbbell      RegisterMap map(thread, false);
323254885Sdumbbell      frame at_risk =  thread->last_frame().sender(&map);
324254885Sdumbbell      if (id() == at_risk.id()) {
325254885Sdumbbell        thread->set_must_deopt_id(id());
326254885Sdumbbell        thread->set_deopt_suspend();
327254885Sdumbbell        return;
328254885Sdumbbell      }
329254885Sdumbbell    }
330254885Sdumbbell  } // NeedsDeoptSuspend
331254885Sdumbbell
332254885Sdumbbell
333254885Sdumbbell  // If the call site is a MethodHandle call site use the MH deopt
334254885Sdumbbell  // handler.
335254885Sdumbbell  address deopt = nm->is_method_handle_return(pc()) ?
336254885Sdumbbell    nm->deopt_mh_handler_begin() :
337254885Sdumbbell    nm->deopt_handler_begin();
338254885Sdumbbell
339254885Sdumbbell  // Save the original pc before we patch in the new one
340254885Sdumbbell  nm->set_original_pc(this, pc());
341254885Sdumbbell  patch_pc(thread, deopt);
342254885Sdumbbell
343254885Sdumbbell#ifdef ASSERT
344254885Sdumbbell  {
345254885Sdumbbell    RegisterMap map(thread, false);
346254885Sdumbbell    frame check = thread->last_frame();
347254885Sdumbbell    while (id() != check.id()) {
348254885Sdumbbell      check = check.sender(&map);
349254885Sdumbbell    }
350254885Sdumbbell    assert(check.is_deoptimized_frame(), "missed deopt");
351254885Sdumbbell  }
352254885Sdumbbell#endif // ASSERT
353254885Sdumbbell}
354254885Sdumbbell
355254885Sdumbbellframe frame::java_sender() const {
356254885Sdumbbell  RegisterMap map(JavaThread::current(), false);
357254885Sdumbbell  frame s;
358254885Sdumbbell  for (s = sender(&map); !(s.is_java_frame() || s.is_first_frame()); s = s.sender(&map)) ;
359254885Sdumbbell  guarantee(s.is_java_frame(), "tried to get caller of first java frame");
360254885Sdumbbell  return s;
361254885Sdumbbell}
362254885Sdumbbell
363254885Sdumbbellframe frame::real_sender(RegisterMap* map) const {
364254885Sdumbbell  frame result = sender(map);
365254885Sdumbbell  while (result.is_runtime_frame() ||
366254885Sdumbbell         result.is_ignored_frame()) {
367254885Sdumbbell    result = result.sender(map);
368254885Sdumbbell  }
369254885Sdumbbell  return result;
370254885Sdumbbell}
371254885Sdumbbell
372254885Sdumbbell// Note: called by profiler - NOT for current thread
373254885Sdumbbellframe frame::profile_find_Java_sender_frame(JavaThread *thread) {
374254885Sdumbbell// If we don't recognize this frame, walk back up the stack until we do
375254885Sdumbbell  RegisterMap map(thread, false);
376254885Sdumbbell  frame first_java_frame = frame();
377254885Sdumbbell
378254885Sdumbbell  // Find the first Java frame on the stack starting with input frame
379254885Sdumbbell  if (is_java_frame()) {
380254885Sdumbbell    // top frame is compiled frame or deoptimized frame
381254885Sdumbbell    first_java_frame = *this;
382254885Sdumbbell  } else if (safe_for_sender(thread)) {
383254885Sdumbbell    for (frame sender_frame = sender(&map);
384254885Sdumbbell      sender_frame.safe_for_sender(thread) && !sender_frame.is_first_frame();
385254885Sdumbbell      sender_frame = sender_frame.sender(&map)) {
386254885Sdumbbell      if (sender_frame.is_java_frame()) {
387254885Sdumbbell        first_java_frame = sender_frame;
388254885Sdumbbell        break;
389254885Sdumbbell      }
390254885Sdumbbell    }
391254885Sdumbbell  }
392254885Sdumbbell  return first_java_frame;
393254885Sdumbbell}
394254885Sdumbbell
395254885Sdumbbell// Interpreter frames
396254885Sdumbbell
397254885Sdumbbell
398254885Sdumbbellvoid frame::interpreter_frame_set_locals(intptr_t* locs)  {
399254885Sdumbbell  assert(is_interpreted_frame(), "Not an interpreted frame");
400254885Sdumbbell  *interpreter_frame_locals_addr() = locs;
401254885Sdumbbell}
402254885Sdumbbell
403254885SdumbbellMethod* frame::interpreter_frame_method() const {
404254885Sdumbbell  assert(is_interpreted_frame(), "interpreted frame expected");
405254885Sdumbbell  Method* m = *interpreter_frame_method_addr();
406254885Sdumbbell  assert(m->is_method(), "not a Method*");
407254885Sdumbbell  return m;
408254885Sdumbbell}
409254885Sdumbbell
410254885Sdumbbellvoid frame::interpreter_frame_set_method(Method* method) {
411254885Sdumbbell  assert(is_interpreted_frame(), "interpreted frame expected");
412254885Sdumbbell  *interpreter_frame_method_addr() = method;
413254885Sdumbbell}
414254885Sdumbbell
415254885Sdumbbellvoid frame::interpreter_frame_set_bcx(intptr_t bcx) {
416254885Sdumbbell  assert(is_interpreted_frame(), "Not an interpreted frame");
417254885Sdumbbell  if (ProfileInterpreter) {
418254885Sdumbbell    bool formerly_bci = is_bci(interpreter_frame_bcx());
419254885Sdumbbell    bool is_now_bci = is_bci(bcx);
420254885Sdumbbell    *interpreter_frame_bcx_addr() = bcx;
421254885Sdumbbell
422254885Sdumbbell    intptr_t mdx = interpreter_frame_mdx();
423254885Sdumbbell
424254885Sdumbbell    if (mdx != 0) {
425254885Sdumbbell      if (formerly_bci) {
426254885Sdumbbell        if (!is_now_bci) {
427254885Sdumbbell          // The bcx was just converted from bci to bcp.
428254885Sdumbbell          // Convert the mdx in parallel.
429254885Sdumbbell          MethodData* mdo = interpreter_frame_method()->method_data();
430254885Sdumbbell          assert(mdo != NULL, "");
431254885Sdumbbell          int mdi = mdx - 1; // We distinguish valid mdi from zero by adding one.
432254885Sdumbbell          address mdp = mdo->di_to_dp(mdi);
433254885Sdumbbell          interpreter_frame_set_mdx((intptr_t)mdp);
434254885Sdumbbell        }
435254885Sdumbbell      } else {
436254885Sdumbbell        if (is_now_bci) {
437254885Sdumbbell          // The bcx was just converted from bcp to bci.
438254885Sdumbbell          // Convert the mdx in parallel.
439254885Sdumbbell          MethodData* mdo = interpreter_frame_method()->method_data();
440254885Sdumbbell          assert(mdo != NULL, "");
441254885Sdumbbell          int mdi = mdo->dp_to_di((address)mdx);
442254885Sdumbbell          interpreter_frame_set_mdx((intptr_t)mdi + 1); // distinguish valid from 0.
443254885Sdumbbell        }
444254885Sdumbbell      }
445254885Sdumbbell    }
446254885Sdumbbell  } else {
447254885Sdumbbell    *interpreter_frame_bcx_addr() = bcx;
448254885Sdumbbell  }
449254885Sdumbbell}
450254885Sdumbbell
451254885Sdumbbelljint frame::interpreter_frame_bci() const {
452254885Sdumbbell  assert(is_interpreted_frame(), "interpreted frame expected");
453254885Sdumbbell  intptr_t bcx = interpreter_frame_bcx();
454254885Sdumbbell  return is_bci(bcx) ? bcx : interpreter_frame_method()->bci_from((address)bcx);
455254885Sdumbbell}
456254885Sdumbbell
457254885Sdumbbellvoid frame::interpreter_frame_set_bci(jint bci) {
458254885Sdumbbell  assert(is_interpreted_frame(), "interpreted frame expected");
459254885Sdumbbell  assert(!is_bci(interpreter_frame_bcx()), "should not set bci during GC");
460254885Sdumbbell  interpreter_frame_set_bcx((intptr_t)interpreter_frame_method()->bcp_from(bci));
461254885Sdumbbell}
462254885Sdumbbell
463254885Sdumbbelladdress frame::interpreter_frame_bcp() const {
464254885Sdumbbell  assert(is_interpreted_frame(), "interpreted frame expected");
465254885Sdumbbell  intptr_t bcx = interpreter_frame_bcx();
466254885Sdumbbell  return is_bci(bcx) ? interpreter_frame_method()->bcp_from(bcx) : (address)bcx;
467254885Sdumbbell}
468254885Sdumbbell
469254885Sdumbbellvoid frame::interpreter_frame_set_bcp(address bcp) {
470254885Sdumbbell  assert(is_interpreted_frame(), "interpreted frame expected");
471254885Sdumbbell  assert(!is_bci(interpreter_frame_bcx()), "should not set bcp during GC");
472254885Sdumbbell  interpreter_frame_set_bcx((intptr_t)bcp);
473254885Sdumbbell}
474254885Sdumbbell
475254885Sdumbbellvoid frame::interpreter_frame_set_mdx(intptr_t mdx) {
476254885Sdumbbell  assert(is_interpreted_frame(), "Not an interpreted frame");
477254885Sdumbbell  assert(ProfileInterpreter, "must be profiling interpreter");
478254885Sdumbbell  *interpreter_frame_mdx_addr() = mdx;
479254885Sdumbbell}
480254885Sdumbbell
481254885Sdumbbelladdress frame::interpreter_frame_mdp() const {
482254885Sdumbbell  assert(ProfileInterpreter, "must be profiling interpreter");
483254885Sdumbbell  assert(is_interpreted_frame(), "interpreted frame expected");
484254885Sdumbbell  intptr_t bcx = interpreter_frame_bcx();
485254885Sdumbbell  intptr_t mdx = interpreter_frame_mdx();
486254885Sdumbbell
487254885Sdumbbell  assert(!is_bci(bcx), "should not access mdp during GC");
488254885Sdumbbell  return (address)mdx;
489254885Sdumbbell}
490254885Sdumbbell
491254885Sdumbbellvoid frame::interpreter_frame_set_mdp(address mdp) {
492254885Sdumbbell  assert(is_interpreted_frame(), "interpreted frame expected");
493254885Sdumbbell  if (mdp == NULL) {
494254885Sdumbbell    // Always allow the mdp to be cleared.
495254885Sdumbbell    interpreter_frame_set_mdx((intptr_t)mdp);
496254885Sdumbbell  }
497254885Sdumbbell  intptr_t bcx = interpreter_frame_bcx();
498254885Sdumbbell  assert(!is_bci(bcx), "should not set mdp during GC");
499254885Sdumbbell  interpreter_frame_set_mdx((intptr_t)mdp);
500254885Sdumbbell}
501254885Sdumbbell
502254885SdumbbellBasicObjectLock* frame::next_monitor_in_interpreter_frame(BasicObjectLock* current) const {
503254885Sdumbbell  assert(is_interpreted_frame(), "Not an interpreted frame");
504254885Sdumbbell#ifdef ASSERT
505254885Sdumbbell  interpreter_frame_verify_monitor(current);
506254885Sdumbbell#endif
507254885Sdumbbell  BasicObjectLock* next = (BasicObjectLock*) (((intptr_t*) current) + interpreter_frame_monitor_size());
508254885Sdumbbell  return next;
509254885Sdumbbell}
510254885Sdumbbell
511254885SdumbbellBasicObjectLock* frame::previous_monitor_in_interpreter_frame(BasicObjectLock* current) const {
512254885Sdumbbell  assert(is_interpreted_frame(), "Not an interpreted frame");
513254885Sdumbbell#ifdef ASSERT
514254885Sdumbbell//   // This verification needs to be checked before being enabled
515254885Sdumbbell//   interpreter_frame_verify_monitor(current);
516254885Sdumbbell#endif
517254885Sdumbbell  BasicObjectLock* previous = (BasicObjectLock*) (((intptr_t*) current) - interpreter_frame_monitor_size());
518254885Sdumbbell  return previous;
519254885Sdumbbell}
520254885Sdumbbell
521254885Sdumbbell// Interpreter locals and expression stack locations.
522254885Sdumbbell
523254885Sdumbbellintptr_t* frame::interpreter_frame_local_at(int index) const {
524254885Sdumbbell  const int n = Interpreter::local_offset_in_bytes(index)/wordSize;
525254885Sdumbbell  return &((*interpreter_frame_locals_addr())[n]);
526254885Sdumbbell}
527254885Sdumbbell
528254885Sdumbbellintptr_t* frame::interpreter_frame_expression_stack_at(jint offset) const {
529254885Sdumbbell  const int i = offset * interpreter_frame_expression_stack_direction();
530254885Sdumbbell  const int n = i * Interpreter::stackElementWords;
531254885Sdumbbell  return &(interpreter_frame_expression_stack()[n]);
532254885Sdumbbell}
533254885Sdumbbell
534254885Sdumbbelljint frame::interpreter_frame_expression_stack_size() const {
535254885Sdumbbell  // Number of elements on the interpreter expression stack
536254885Sdumbbell  // Callers should span by stackElementWords
537254885Sdumbbell  int element_size = Interpreter::stackElementWords;
538254885Sdumbbell  size_t stack_size = 0;
539254885Sdumbbell  if (frame::interpreter_frame_expression_stack_direction() < 0) {
540254885Sdumbbell    stack_size = (interpreter_frame_expression_stack() -
541254885Sdumbbell                  interpreter_frame_tos_address() + 1)/element_size;
542254885Sdumbbell  } else {
543254885Sdumbbell    stack_size = (interpreter_frame_tos_address() -
544254885Sdumbbell                  interpreter_frame_expression_stack() + 1)/element_size;
545254885Sdumbbell  }
546254885Sdumbbell  assert( stack_size <= (size_t)max_jint, "stack size too big");
547254885Sdumbbell  return ((jint)stack_size);
548254885Sdumbbell}
549254885Sdumbbell
550254885Sdumbbell
551254885Sdumbbell// (frame::interpreter_frame_sender_sp accessor is in frame_<arch>.cpp)
552254885Sdumbbell
553254885Sdumbbellconst char* frame::print_name() const {
554254885Sdumbbell  if (is_native_frame())      return "Native";
555254885Sdumbbell  if (is_interpreted_frame()) return "Interpreted";
556254885Sdumbbell  if (is_compiled_frame()) {
557254885Sdumbbell    if (is_deoptimized_frame()) return "Deoptimized";
558254885Sdumbbell    return "Compiled";
559254885Sdumbbell  }
560254885Sdumbbell  if (sp() == NULL)            return "Empty";
561254885Sdumbbell  return "C";
562254885Sdumbbell}
563254885Sdumbbell
564254885Sdumbbellvoid frame::print_value_on(outputStream* st, JavaThread *thread) const {
565254885Sdumbbell  NOT_PRODUCT(address begin = pc()-40;)
566254885Sdumbbell  NOT_PRODUCT(address end   = NULL;)
567254885Sdumbbell
568254885Sdumbbell  st->print("%s frame (sp=" INTPTR_FORMAT " unextended sp=" INTPTR_FORMAT, print_name(), sp(), unextended_sp());
569254885Sdumbbell  if (sp() != NULL)
570254885Sdumbbell    st->print(", fp=" INTPTR_FORMAT ", real_fp=" INTPTR_FORMAT ", pc=" INTPTR_FORMAT, fp(), real_fp(), pc());
571254885Sdumbbell
572254885Sdumbbell  if (StubRoutines::contains(pc())) {
573254885Sdumbbell    st->print_cr(")");
574254885Sdumbbell    st->print("(");
575254885Sdumbbell    StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
576254885Sdumbbell    st->print("~Stub::%s", desc->name());
577254885Sdumbbell    NOT_PRODUCT(begin = desc->begin(); end = desc->end();)
578254885Sdumbbell  } else if (Interpreter::contains(pc())) {
579254885Sdumbbell    st->print_cr(")");
580254885Sdumbbell    st->print("(");
581254885Sdumbbell    InterpreterCodelet* desc = Interpreter::codelet_containing(pc());
582254885Sdumbbell    if (desc != NULL) {
583254885Sdumbbell      st->print("~");
584254885Sdumbbell      desc->print_on(st);
585254885Sdumbbell      NOT_PRODUCT(begin = desc->code_begin(); end = desc->code_end();)
586254885Sdumbbell    } else {
587254885Sdumbbell      st->print("~interpreter");
588254885Sdumbbell    }
589254885Sdumbbell  }
590254885Sdumbbell  st->print_cr(")");
591254885Sdumbbell
592254885Sdumbbell  if (_cb != NULL) {
593254885Sdumbbell    st->print("     ");
594254885Sdumbbell    _cb->print_value_on(st);
595254885Sdumbbell    st->cr();
596254885Sdumbbell#ifndef PRODUCT
597254885Sdumbbell    if (end == NULL) {
598254885Sdumbbell      begin = _cb->code_begin();
599254885Sdumbbell      end   = _cb->code_end();
600254885Sdumbbell    }
601254885Sdumbbell#endif
602254885Sdumbbell  }
603254885Sdumbbell  NOT_PRODUCT(if (WizardMode && Verbose) Disassembler::decode(begin, end);)
604254885Sdumbbell}
605254885Sdumbbell
606254885Sdumbbell
607254885Sdumbbellvoid frame::print_on(outputStream* st) const {
608254885Sdumbbell  print_value_on(st,NULL);
609254885Sdumbbell  if (is_interpreted_frame()) {
610254885Sdumbbell    interpreter_frame_print_on(st);
611254885Sdumbbell  }
612254885Sdumbbell}
613254885Sdumbbell
614254885Sdumbbell
615254885Sdumbbellvoid frame::interpreter_frame_print_on(outputStream* st) const {
616254885Sdumbbell#ifndef PRODUCT
617254885Sdumbbell  assert(is_interpreted_frame(), "Not an interpreted frame");
618254885Sdumbbell  jint i;
619254885Sdumbbell  for (i = 0; i < interpreter_frame_method()->max_locals(); i++ ) {
620254885Sdumbbell    intptr_t x = *interpreter_frame_local_at(i);
621254885Sdumbbell    st->print(" - local  [" INTPTR_FORMAT "]", x);
622254885Sdumbbell    st->fill_to(23);
623254885Sdumbbell    st->print_cr("; #%d", i);
624254885Sdumbbell  }
625254885Sdumbbell  for (i = interpreter_frame_expression_stack_size() - 1; i >= 0; --i ) {
626254885Sdumbbell    intptr_t x = *interpreter_frame_expression_stack_at(i);
627254885Sdumbbell    st->print(" - stack  [" INTPTR_FORMAT "]", x);
628254885Sdumbbell    st->fill_to(23);
629254885Sdumbbell    st->print_cr("; #%d", i);
630254885Sdumbbell  }
631254885Sdumbbell  // locks for synchronization
632254885Sdumbbell  for (BasicObjectLock* current = interpreter_frame_monitor_end();
633254885Sdumbbell       current < interpreter_frame_monitor_begin();
634254885Sdumbbell       current = next_monitor_in_interpreter_frame(current)) {
635254885Sdumbbell    st->print(" - obj    [");
636254885Sdumbbell    current->obj()->print_value_on(st);
637254885Sdumbbell    st->print_cr("]");
638254885Sdumbbell    st->print(" - lock   [");
639254885Sdumbbell    current->lock()->print_on(st);
640254885Sdumbbell    st->print_cr("]");
641254885Sdumbbell  }
642254885Sdumbbell  // monitor
643254885Sdumbbell  st->print_cr(" - monitor[" INTPTR_FORMAT "]", interpreter_frame_monitor_begin());
644254885Sdumbbell  // bcp
645254885Sdumbbell  st->print(" - bcp    [" INTPTR_FORMAT "]", interpreter_frame_bcp());
646254885Sdumbbell  st->fill_to(23);
647254885Sdumbbell  st->print_cr("; @%d", interpreter_frame_bci());
648254885Sdumbbell  // locals
649254885Sdumbbell  st->print_cr(" - locals [" INTPTR_FORMAT "]", interpreter_frame_local_at(0));
650254885Sdumbbell  // method
651254885Sdumbbell  st->print(" - method [" INTPTR_FORMAT "]", (address)interpreter_frame_method());
652254885Sdumbbell  st->fill_to(23);
653254885Sdumbbell  st->print("; ");
654254885Sdumbbell  interpreter_frame_method()->print_name(st);
655254885Sdumbbell  st->cr();
656254885Sdumbbell#endif
657254885Sdumbbell}
658254885Sdumbbell
659254885Sdumbbell// Print whether the frame is in the VM or OS indicating a HotSpot problem.
660254885Sdumbbell// Otherwise, it's likely a bug in the native library that the Java code calls,
661254885Sdumbbell// hopefully indicating where to submit bugs.
662254885Sdumbbellvoid frame::print_C_frame(outputStream* st, char* buf, int buflen, address pc) {
663254885Sdumbbell  // C/C++ frame
664254885Sdumbbell  bool in_vm = os::address_is_in_vm(pc);
665254885Sdumbbell  st->print(in_vm ? "V" : "C");
666254885Sdumbbell
667254885Sdumbbell  int offset;
668254885Sdumbbell  bool found;
669254885Sdumbbell
670254885Sdumbbell  // libname
671254885Sdumbbell  found = os::dll_address_to_library_name(pc, buf, buflen, &offset);
672254885Sdumbbell  if (found) {
673254885Sdumbbell    // skip directory names
674254885Sdumbbell    const char *p1, *p2;
675254885Sdumbbell    p1 = buf;
676254885Sdumbbell    int len = (int)strlen(os::file_separator());
677254885Sdumbbell    while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
678254885Sdumbbell    st->print("  [%s+0x%x]", p1, offset);
679254885Sdumbbell  } else {
680254885Sdumbbell    st->print("  " PTR_FORMAT, pc);
681254885Sdumbbell  }
682254885Sdumbbell
683254885Sdumbbell  // function name - os::dll_address_to_function_name() may return confusing
684254885Sdumbbell  // names if pc is within jvm.dll or libjvm.so, because JVM only has
685254885Sdumbbell  // JVM_xxxx and a few other symbols in the dynamic symbol table. Do this
686254885Sdumbbell  // only for native libraries.
687254885Sdumbbell  if (!in_vm || Decoder::can_decode_C_frame_in_vm()) {
688254885Sdumbbell    found = os::dll_address_to_function_name(pc, buf, buflen, &offset);
689254885Sdumbbell
690254885Sdumbbell    if (found) {
691254885Sdumbbell      st->print("  %s+0x%x", buf, offset);
692254885Sdumbbell    }
693254885Sdumbbell  }
694254885Sdumbbell}
695254885Sdumbbell
696254885Sdumbbell// frame::print_on_error() is called by fatal error handler. Notice that we may
697254885Sdumbbell// crash inside this function if stack frame is corrupted. The fatal error
698254885Sdumbbell// handler can catch and handle the crash. Here we assume the frame is valid.
699254885Sdumbbell//
700254885Sdumbbell// First letter indicates type of the frame:
701254885Sdumbbell//    J: Java frame (compiled)
702254885Sdumbbell//    j: Java frame (interpreted)
703254885Sdumbbell//    V: VM frame (C/C++)
704254885Sdumbbell//    v: Other frames running VM generated code (e.g. stubs, adapters, etc.)
705254885Sdumbbell//    C: C/C++ frame
706254885Sdumbbell//
707254885Sdumbbell// We don't need detailed frame type as that in frame::print_name(). "C"
708254885Sdumbbell// suggests the problem is in user lib; everything else is likely a VM bug.
709254885Sdumbbell
710254885Sdumbbellvoid frame::print_on_error(outputStream* st, char* buf, int buflen, bool verbose) const {
711254885Sdumbbell  if (_cb != NULL) {
712254885Sdumbbell    if (Interpreter::contains(pc())) {
713254885Sdumbbell      Method* m = this->interpreter_frame_method();
714254885Sdumbbell      if (m != NULL) {
715254885Sdumbbell        m->name_and_sig_as_C_string(buf, buflen);
716254885Sdumbbell        st->print("j  %s", buf);
717254885Sdumbbell        st->print("+%d", this->interpreter_frame_bci());
718254885Sdumbbell      } else {
719254885Sdumbbell        st->print("j  " PTR_FORMAT, pc());
720254885Sdumbbell      }
721254885Sdumbbell    } else if (StubRoutines::contains(pc())) {
722254885Sdumbbell      StubCodeDesc* desc = StubCodeDesc::desc_for(pc());
723254885Sdumbbell      if (desc != NULL) {
724254885Sdumbbell        st->print("v  ~StubRoutines::%s", desc->name());
725254885Sdumbbell      } else {
726254885Sdumbbell        st->print("v  ~StubRoutines::" PTR_FORMAT, pc());
727254885Sdumbbell      }
728254885Sdumbbell    } else if (_cb->is_buffer_blob()) {
729254885Sdumbbell      st->print("v  ~BufferBlob::%s", ((BufferBlob *)_cb)->name());
730254885Sdumbbell    } else if (_cb->is_nmethod()) {
731254885Sdumbbell      nmethod* nm = (nmethod*)_cb;
732254885Sdumbbell      Method* m = nm->method();
733254885Sdumbbell      if (m != NULL) {
734254885Sdumbbell        m->name_and_sig_as_C_string(buf, buflen);
735254885Sdumbbell        st->print("J %d%s %s %s (%d bytes) @ " PTR_FORMAT " [" PTR_FORMAT "+0x%x]",
736254885Sdumbbell                  nm->compile_id(), (nm->is_osr_method() ? "%" : ""),
737254885Sdumbbell                  ((nm->compiler() != NULL) ? nm->compiler()->name() : ""),
738254885Sdumbbell                  buf, m->code_size(), _pc, _cb->code_begin(), _pc - _cb->code_begin());
739254885Sdumbbell      } else {
740254885Sdumbbell        st->print("J  " PTR_FORMAT, pc());
741254885Sdumbbell      }
742254885Sdumbbell    } else if (_cb->is_runtime_stub()) {
743254885Sdumbbell      st->print("v  ~RuntimeStub::%s", ((RuntimeStub *)_cb)->name());
744254885Sdumbbell    } else if (_cb->is_deoptimization_stub()) {
745254885Sdumbbell      st->print("v  ~DeoptimizationBlob");
746254885Sdumbbell    } else if (_cb->is_exception_stub()) {
747254885Sdumbbell      st->print("v  ~ExceptionBlob");
748254885Sdumbbell    } else if (_cb->is_safepoint_stub()) {
749254885Sdumbbell      st->print("v  ~SafepointBlob");
750254885Sdumbbell    } else {
751254885Sdumbbell      st->print("v  blob " PTR_FORMAT, pc());
752254885Sdumbbell    }
753254885Sdumbbell  } else {
754254885Sdumbbell    print_C_frame(st, buf, buflen, pc());
755254885Sdumbbell  }
756254885Sdumbbell}
757254885Sdumbbell
758254885Sdumbbell
759254885Sdumbbell/*
760254885Sdumbbell  The interpreter_frame_expression_stack_at method in the case of SPARC needs the
761254885Sdumbbell  max_stack value of the method in order to compute the expression stack address.
762254885Sdumbbell  It uses the Method* in order to get the max_stack value but during GC this
763254885Sdumbbell  Method* value saved on the frame is changed by reverse_and_push and hence cannot
764254885Sdumbbell  be used. So we save the max_stack value in the FrameClosure object and pass it
765254885Sdumbbell  down to the interpreter_frame_expression_stack_at method
766254885Sdumbbell*/
767254885Sdumbbellclass InterpreterFrameClosure : public OffsetClosure {
768254885Sdumbbell private:
769254885Sdumbbell  frame* _fr;
770254885Sdumbbell  OopClosure* _f;
771254885Sdumbbell  int    _max_locals;
772254885Sdumbbell  int    _max_stack;
773254885Sdumbbell
774254885Sdumbbell public:
775254885Sdumbbell  InterpreterFrameClosure(frame* fr, int max_locals, int max_stack,
776254885Sdumbbell                          OopClosure* f) {
777254885Sdumbbell    _fr         = fr;
778254885Sdumbbell    _max_locals = max_locals;
779254885Sdumbbell    _max_stack  = max_stack;
780254885Sdumbbell    _f          = f;
781254885Sdumbbell  }
782254885Sdumbbell
783254885Sdumbbell  void offset_do(int offset) {
784254885Sdumbbell    oop* addr;
785254885Sdumbbell    if (offset < _max_locals) {
786254885Sdumbbell      addr = (oop*) _fr->interpreter_frame_local_at(offset);
787254885Sdumbbell      assert((intptr_t*)addr >= _fr->sp(), "must be inside the frame");
788254885Sdumbbell      _f->do_oop(addr);
789254885Sdumbbell    } else {
790254885Sdumbbell      addr = (oop*) _fr->interpreter_frame_expression_stack_at((offset - _max_locals));
791254885Sdumbbell      // In case of exceptions, the expression stack is invalid and the esp will be reset to express
792254885Sdumbbell      // this condition. Therefore, we call f only if addr is 'inside' the stack (i.e., addr >= esp for Intel).
793254885Sdumbbell      bool in_stack;
794254885Sdumbbell      if (frame::interpreter_frame_expression_stack_direction() > 0) {
795254885Sdumbbell        in_stack = (intptr_t*)addr <= _fr->interpreter_frame_tos_address();
796254885Sdumbbell      } else {
797254885Sdumbbell        in_stack = (intptr_t*)addr >= _fr->interpreter_frame_tos_address();
798254885Sdumbbell      }
799254885Sdumbbell      if (in_stack) {
800254885Sdumbbell        _f->do_oop(addr);
801254885Sdumbbell      }
802254885Sdumbbell    }
803254885Sdumbbell  }
804254885Sdumbbell
805254885Sdumbbell  int max_locals()  { return _max_locals; }
806254885Sdumbbell  frame* fr()       { return _fr; }
807254885Sdumbbell};
808254885Sdumbbell
809254885Sdumbbell
810254885Sdumbbellclass InterpretedArgumentOopFinder: public SignatureInfo {
811254885Sdumbbell private:
812254885Sdumbbell  OopClosure* _f;        // Closure to invoke
813254885Sdumbbell  int    _offset;        // TOS-relative offset, decremented with each argument
814254885Sdumbbell  bool   _has_receiver;  // true if the callee has a receiver
815254885Sdumbbell  frame* _fr;
816254885Sdumbbell
817254885Sdumbbell  void set(int size, BasicType type) {
818254885Sdumbbell    _offset -= size;
819254885Sdumbbell    if (type == T_OBJECT || type == T_ARRAY) oop_offset_do();
820254885Sdumbbell  }
821254885Sdumbbell
822254885Sdumbbell  void oop_offset_do() {
823254885Sdumbbell    oop* addr;
824254885Sdumbbell    addr = (oop*)_fr->interpreter_frame_tos_at(_offset);
825254885Sdumbbell    _f->do_oop(addr);
826254885Sdumbbell  }
827254885Sdumbbell
828254885Sdumbbell public:
829254885Sdumbbell  InterpretedArgumentOopFinder(Symbol* signature, bool has_receiver, frame* fr, OopClosure* f) : SignatureInfo(signature), _has_receiver(has_receiver) {
830254885Sdumbbell    // compute size of arguments
831254885Sdumbbell    int args_size = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0);
832254885Sdumbbell    assert(!fr->is_interpreted_frame() ||
833254885Sdumbbell           args_size <= fr->interpreter_frame_expression_stack_size(),
834254885Sdumbbell            "args cannot be on stack anymore");
835254885Sdumbbell    // initialize InterpretedArgumentOopFinder
836254885Sdumbbell    _f         = f;
837254885Sdumbbell    _fr        = fr;
838254885Sdumbbell    _offset    = args_size;
839254885Sdumbbell  }
840254885Sdumbbell
841254885Sdumbbell  void oops_do() {
842254885Sdumbbell    if (_has_receiver) {
843254885Sdumbbell      --_offset;
844254885Sdumbbell      oop_offset_do();
845254885Sdumbbell    }
846254885Sdumbbell    iterate_parameters();
847254885Sdumbbell  }
848254885Sdumbbell};
849254885Sdumbbell
850254885Sdumbbell
851254885Sdumbbell// Entry frame has following form (n arguments)
852254885Sdumbbell//         +-----------+
853254885Sdumbbell//   sp -> |  last arg |
854254885Sdumbbell//         +-----------+
855254885Sdumbbell//         :    :::    :
856254885Sdumbbell//         +-----------+
857254885Sdumbbell// (sp+n)->|  first arg|
858254885Sdumbbell//         +-----------+
859254885Sdumbbell
860254885Sdumbbell
861254885Sdumbbell
862254885Sdumbbell// visits and GC's all the arguments in entry frame
863254885Sdumbbellclass EntryFrameOopFinder: public SignatureInfo {
864254885Sdumbbell private:
865254885Sdumbbell  bool   _is_static;
866254885Sdumbbell  int    _offset;
867254885Sdumbbell  frame* _fr;
868254885Sdumbbell  OopClosure* _f;
869254885Sdumbbell
870254885Sdumbbell  void set(int size, BasicType type) {
871254885Sdumbbell    assert (_offset >= 0, "illegal offset");
872254885Sdumbbell    if (type == T_OBJECT || type == T_ARRAY) oop_at_offset_do(_offset);
873254885Sdumbbell    _offset -= size;
874254885Sdumbbell  }
875254885Sdumbbell
876254885Sdumbbell  void oop_at_offset_do(int offset) {
877254885Sdumbbell    assert (offset >= 0, "illegal offset");
878254885Sdumbbell    oop* addr = (oop*) _fr->entry_frame_argument_at(offset);
879254885Sdumbbell    _f->do_oop(addr);
880254885Sdumbbell  }
881254885Sdumbbell
882254885Sdumbbell public:
883254885Sdumbbell   EntryFrameOopFinder(frame* frame, Symbol* signature, bool is_static) : SignatureInfo(signature) {
884254885Sdumbbell     _f = NULL; // will be set later
885254885Sdumbbell     _fr = frame;
886254885Sdumbbell     _is_static = is_static;
887254885Sdumbbell     _offset = ArgumentSizeComputer(signature).size() - 1; // last parameter is at index 0
888254885Sdumbbell   }
889254885Sdumbbell
890254885Sdumbbell  void arguments_do(OopClosure* f) {
891254885Sdumbbell    _f = f;
892254885Sdumbbell    if (!_is_static) oop_at_offset_do(_offset+1); // do the receiver
893254885Sdumbbell    iterate_parameters();
894254885Sdumbbell  }
895254885Sdumbbell
896254885Sdumbbell};
897254885Sdumbbell
898254885Sdumbbelloop* frame::interpreter_callee_receiver_addr(Symbol* signature) {
899254885Sdumbbell  ArgumentSizeComputer asc(signature);
900254885Sdumbbell  int size = asc.size();
901254885Sdumbbell  return (oop *)interpreter_frame_tos_at(size);
902254885Sdumbbell}
903254885Sdumbbell
904254885Sdumbbell
905254885Sdumbbellvoid frame::oops_interpreted_do(OopClosure* f, CLDClosure* cld_f,
906254885Sdumbbell    const RegisterMap* map, bool query_oop_map_cache) {
907254885Sdumbbell  assert(is_interpreted_frame(), "Not an interpreted frame");
908254885Sdumbbell  assert(map != NULL, "map must be set");
909254885Sdumbbell  Thread *thread = Thread::current();
910254885Sdumbbell  methodHandle m (thread, interpreter_frame_method());
911254885Sdumbbell  jint      bci = interpreter_frame_bci();
912254885Sdumbbell
913254885Sdumbbell  assert(!Universe::heap()->is_in(m()),
914254885Sdumbbell          "must be valid oop");
915254885Sdumbbell  assert(m->is_method(), "checking frame value");
916254885Sdumbbell  assert((m->is_native() && bci == 0)  ||
917254885Sdumbbell         (!m->is_native() && bci >= 0 && bci < m->code_size()),
918254885Sdumbbell         "invalid bci value");
919254885Sdumbbell
920254885Sdumbbell  // Handle the monitor elements in the activation
921254885Sdumbbell  for (
922254885Sdumbbell    BasicObjectLock* current = interpreter_frame_monitor_end();
923254885Sdumbbell    current < interpreter_frame_monitor_begin();
924254885Sdumbbell    current = next_monitor_in_interpreter_frame(current)
925254885Sdumbbell  ) {
926254885Sdumbbell#ifdef ASSERT
927254885Sdumbbell    interpreter_frame_verify_monitor(current);
928254885Sdumbbell#endif
929254885Sdumbbell    current->oops_do(f);
930254885Sdumbbell  }
931254885Sdumbbell
932254885Sdumbbell  // process fixed part
933254885Sdumbbell  if (cld_f != NULL) {
934254885Sdumbbell    // The method pointer in the frame might be the only path to the method's
935254885Sdumbbell    // klass, and the klass needs to be kept alive while executing. The GCs
936254885Sdumbbell    // don't trace through method pointers, so typically in similar situations
937254885Sdumbbell    // the mirror or the class loader of the klass are installed as a GC root.
938254885Sdumbbell    // To minimize the overhead of doing that here, we ask the GC to pass down a
939254885Sdumbbell    // closure that knows how to keep klasses alive given a ClassLoaderData.
940254885Sdumbbell    cld_f->do_cld(m->method_holder()->class_loader_data());
941254885Sdumbbell  }
942254885Sdumbbell
943254885Sdumbbell  if (m->is_native() PPC32_ONLY(&& m->is_static())) {
944254885Sdumbbell    f->do_oop(interpreter_frame_temp_oop_addr());
945254885Sdumbbell  }
946254885Sdumbbell
947254885Sdumbbell  int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
948254885Sdumbbell
949254885Sdumbbell  Symbol* signature = NULL;
950254885Sdumbbell  bool has_receiver = false;
951254885Sdumbbell
952254885Sdumbbell  // Process a callee's arguments if we are at a call site
953254885Sdumbbell  // (i.e., if we are at an invoke bytecode)
954254885Sdumbbell  // This is used sometimes for calling into the VM, not for another
955254885Sdumbbell  // interpreted or compiled frame.
956254885Sdumbbell  if (!m->is_native()) {
957254885Sdumbbell    Bytecode_invoke call = Bytecode_invoke_check(m, bci);
958254885Sdumbbell    if (call.is_valid()) {
959254885Sdumbbell      signature = call.signature();
960254885Sdumbbell      has_receiver = call.has_receiver();
961254885Sdumbbell      if (map->include_argument_oops() &&
962254885Sdumbbell          interpreter_frame_expression_stack_size() > 0) {
963254885Sdumbbell        ResourceMark rm(thread);  // is this right ???
964254885Sdumbbell        // we are at a call site & the expression stack is not empty
965254885Sdumbbell        // => process callee's arguments
966254885Sdumbbell        //
967254885Sdumbbell        // Note: The expression stack can be empty if an exception
968254885Sdumbbell        //       occurred during method resolution/execution. In all
969254885Sdumbbell        //       cases we empty the expression stack completely be-
970254885Sdumbbell        //       fore handling the exception (the exception handling
971254885Sdumbbell        //       code in the interpreter calls a blocking runtime
972254885Sdumbbell        //       routine which can cause this code to be executed).
973254885Sdumbbell        //       (was bug gri 7/27/98)
974254885Sdumbbell        oops_interpreted_arguments_do(signature, has_receiver, f);
975254885Sdumbbell      }
976254885Sdumbbell    }
977254885Sdumbbell  }
978254885Sdumbbell
979254885Sdumbbell  InterpreterFrameClosure blk(this, max_locals, m->max_stack(), f);
980254885Sdumbbell
981254885Sdumbbell  // process locals & expression stack
982254885Sdumbbell  InterpreterOopMap mask;
983254885Sdumbbell  if (query_oop_map_cache) {
984254885Sdumbbell    m->mask_for(bci, &mask);
985254885Sdumbbell  } else {
986254885Sdumbbell    OopMapCache::compute_one_oop_map(m, bci, &mask);
987254885Sdumbbell  }
988254885Sdumbbell  mask.iterate_oop(&blk);
989254885Sdumbbell}
990254885Sdumbbell
991254885Sdumbbell
992254885Sdumbbellvoid frame::oops_interpreted_arguments_do(Symbol* signature, bool has_receiver, OopClosure* f) {
993254885Sdumbbell  InterpretedArgumentOopFinder finder(signature, has_receiver, this, f);
994254885Sdumbbell  finder.oops_do();
995254885Sdumbbell}
996254885Sdumbbell
997254885Sdumbbellvoid frame::oops_code_blob_do(OopClosure* f, CodeBlobClosure* cf, const RegisterMap* reg_map) {
998254885Sdumbbell  assert(_cb != NULL, "sanity check");
999254885Sdumbbell  if (_cb->oop_maps() != NULL) {
1000254885Sdumbbell    OopMapSet::oops_do(this, reg_map, f);
1001254885Sdumbbell
1002254885Sdumbbell    // Preserve potential arguments for a callee. We handle this by dispatching
1003254885Sdumbbell    // on the codeblob. For c2i, we do
1004254885Sdumbbell    if (reg_map->include_argument_oops()) {
1005254885Sdumbbell      _cb->preserve_callee_argument_oops(*this, reg_map, f);
1006254885Sdumbbell    }
1007254885Sdumbbell  }
1008254885Sdumbbell  // In cases where perm gen is collected, GC will want to mark
1009254885Sdumbbell  // oops referenced from nmethods active on thread stacks so as to
1010254885Sdumbbell  // prevent them from being collected. However, this visit should be
1011254885Sdumbbell  // restricted to certain phases of the collection only. The
1012254885Sdumbbell  // closure decides how it wants nmethods to be traced.
1013254885Sdumbbell  if (cf != NULL)
1014254885Sdumbbell    cf->do_code_blob(_cb);
1015254885Sdumbbell}
1016254885Sdumbbell
1017254885Sdumbbellclass CompiledArgumentOopFinder: public SignatureInfo {
1018254885Sdumbbell protected:
1019254885Sdumbbell  OopClosure*     _f;
1020254885Sdumbbell  int             _offset;        // the current offset, incremented with each argument
1021254885Sdumbbell  bool            _has_receiver;  // true if the callee has a receiver
1022254885Sdumbbell  bool            _has_appendix;  // true if the call has an appendix
1023254885Sdumbbell  frame           _fr;
1024254885Sdumbbell  RegisterMap*    _reg_map;
1025254885Sdumbbell  int             _arg_size;
1026254885Sdumbbell  VMRegPair*      _regs;        // VMReg list of arguments
1027254885Sdumbbell
1028254885Sdumbbell  void set(int size, BasicType type) {
1029254885Sdumbbell    if (type == T_OBJECT || type == T_ARRAY) handle_oop_offset();
1030254885Sdumbbell    _offset += size;
1031254885Sdumbbell  }
1032254885Sdumbbell
1033254885Sdumbbell  virtual void handle_oop_offset() {
1034254885Sdumbbell    // Extract low order register number from register array.
1035254885Sdumbbell    // In LP64-land, the high-order bits are valid but unhelpful.
1036254885Sdumbbell    VMReg reg = _regs[_offset].first();
1037254885Sdumbbell    oop *loc = _fr.oopmapreg_to_location(reg, _reg_map);
1038254885Sdumbbell    _f->do_oop(loc);
1039254885Sdumbbell  }
1040254885Sdumbbell
1041254885Sdumbbell public:
1042254885Sdumbbell  CompiledArgumentOopFinder(Symbol* signature, bool has_receiver, bool has_appendix, OopClosure* f, frame fr,  const RegisterMap* reg_map)
1043254885Sdumbbell    : SignatureInfo(signature) {
1044254885Sdumbbell
1045254885Sdumbbell    // initialize CompiledArgumentOopFinder
1046254885Sdumbbell    _f         = f;
1047254885Sdumbbell    _offset    = 0;
1048254885Sdumbbell    _has_receiver = has_receiver;
1049254885Sdumbbell    _has_appendix = has_appendix;
1050254885Sdumbbell    _fr        = fr;
1051254885Sdumbbell    _reg_map   = (RegisterMap*)reg_map;
1052254885Sdumbbell    _arg_size  = ArgumentSizeComputer(signature).size() + (has_receiver ? 1 : 0) + (has_appendix ? 1 : 0);
1053254885Sdumbbell
1054254885Sdumbbell    int arg_size;
1055254885Sdumbbell    _regs = SharedRuntime::find_callee_arguments(signature, has_receiver, has_appendix, &arg_size);
1056254885Sdumbbell    assert(arg_size == _arg_size, "wrong arg size");
1057254885Sdumbbell  }
1058254885Sdumbbell
1059254885Sdumbbell  void oops_do() {
1060254885Sdumbbell    if (_has_receiver) {
1061254885Sdumbbell      handle_oop_offset();
1062254885Sdumbbell      _offset++;
1063254885Sdumbbell    }
1064254885Sdumbbell    iterate_parameters();
1065254885Sdumbbell    if (_has_appendix) {
1066254885Sdumbbell      handle_oop_offset();
1067254885Sdumbbell      _offset++;
1068254885Sdumbbell    }
1069254885Sdumbbell  }
1070254885Sdumbbell};
1071254885Sdumbbell
1072254885Sdumbbellvoid frame::oops_compiled_arguments_do(Symbol* signature, bool has_receiver, bool has_appendix, const RegisterMap* reg_map, OopClosure* f) {
1073254885Sdumbbell  ResourceMark rm;
1074254885Sdumbbell  CompiledArgumentOopFinder finder(signature, has_receiver, has_appendix, f, *this, reg_map);
1075254885Sdumbbell  finder.oops_do();
1076254885Sdumbbell}
1077254885Sdumbbell
1078254885Sdumbbell
1079254885Sdumbbell// Get receiver out of callers frame, i.e. find parameter 0 in callers
1080254885Sdumbbell// frame.  Consult ADLC for where parameter 0 is to be found.  Then
1081254885Sdumbbell// check local reg_map for it being a callee-save register or argument
1082254885Sdumbbell// register, both of which are saved in the local frame.  If not found
1083254885Sdumbbell// there, it must be an in-stack argument of the caller.
1084254885Sdumbbell// Note: caller.sp() points to callee-arguments
1085254885Sdumbbelloop frame::retrieve_receiver(RegisterMap* reg_map) {
1086254885Sdumbbell  frame caller = *this;
1087254885Sdumbbell
1088254885Sdumbbell  // First consult the ADLC on where it puts parameter 0 for this signature.
1089254885Sdumbbell  VMReg reg = SharedRuntime::name_for_receiver();
1090254885Sdumbbell  oop* oop_adr = caller.oopmapreg_to_location(reg, reg_map);
1091254885Sdumbbell  if (oop_adr == NULL) {
1092254885Sdumbbell    guarantee(oop_adr != NULL, "bad register save location");
1093254885Sdumbbell    return NULL;
1094254885Sdumbbell  }
1095254885Sdumbbell  oop r = *oop_adr;
1096254885Sdumbbell  assert(Universe::heap()->is_in_or_null(r), err_msg("bad receiver: " INTPTR_FORMAT " (" INTX_FORMAT ")", (void *) r, (void *) r));
1097254885Sdumbbell  return r;
1098254885Sdumbbell}
1099254885Sdumbbell
1100254885Sdumbbell
1101254885Sdumbbelloop* frame::oopmapreg_to_location(VMReg reg, const RegisterMap* reg_map) const {
1102254885Sdumbbell  if(reg->is_reg()) {
1103254885Sdumbbell    // If it is passed in a register, it got spilled in the stub frame.
1104254885Sdumbbell    return (oop *)reg_map->location(reg);
1105254885Sdumbbell  } else {
1106254885Sdumbbell    int sp_offset_in_bytes = reg->reg2stack() * VMRegImpl::stack_slot_size;
1107254885Sdumbbell    return (oop*)(((address)unextended_sp()) + sp_offset_in_bytes);
1108254885Sdumbbell  }
1109254885Sdumbbell}
1110254885Sdumbbell
1111254885SdumbbellBasicLock* frame::get_native_monitor() {
1112254885Sdumbbell  nmethod* nm = (nmethod*)_cb;
1113254885Sdumbbell  assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1114254885Sdumbbell         "Should not call this unless it's a native nmethod");
1115254885Sdumbbell  int byte_offset = in_bytes(nm->native_basic_lock_sp_offset());
1116254885Sdumbbell  assert(byte_offset >= 0, "should not see invalid offset");
1117254885Sdumbbell  return (BasicLock*) &sp()[byte_offset / wordSize];
1118254885Sdumbbell}
1119254885Sdumbbell
1120254885Sdumbbelloop frame::get_native_receiver() {
1121254885Sdumbbell  nmethod* nm = (nmethod*)_cb;
1122254885Sdumbbell  assert(_cb != NULL && _cb->is_nmethod() && nm->method()->is_native(),
1123254885Sdumbbell         "Should not call this unless it's a native nmethod");
1124254885Sdumbbell  int byte_offset = in_bytes(nm->native_receiver_sp_offset());
1125254885Sdumbbell  assert(byte_offset >= 0, "should not see invalid offset");
1126254885Sdumbbell  oop owner = ((oop*) sp())[byte_offset / wordSize];
1127254885Sdumbbell  assert( Universe::heap()->is_in(owner), "bad receiver" );
1128254885Sdumbbell  return owner;
1129254885Sdumbbell}
1130254885Sdumbbell
1131254885Sdumbbellvoid frame::oops_entry_do(OopClosure* f, const RegisterMap* map) {
1132254885Sdumbbell  assert(map != NULL, "map must be set");
1133254885Sdumbbell  if (map->include_argument_oops()) {
1134254885Sdumbbell    // must collect argument oops, as nobody else is doing it
1135254885Sdumbbell    Thread *thread = Thread::current();
1136254885Sdumbbell    methodHandle m (thread, entry_frame_call_wrapper()->callee_method());
1137254885Sdumbbell    EntryFrameOopFinder finder(this, m->signature(), m->is_static());
1138254885Sdumbbell    finder.arguments_do(f);
1139254885Sdumbbell  }
1140254885Sdumbbell  // Traverse the Handle Block saved in the entry frame
1141254885Sdumbbell  entry_frame_call_wrapper()->oops_do(f);
1142254885Sdumbbell}
1143254885Sdumbbell
1144254885Sdumbbell
1145254885Sdumbbellvoid frame::oops_do_internal(OopClosure* f, CLDClosure* cld_f, CodeBlobClosure* cf, RegisterMap* map, bool use_interpreter_oop_map_cache) {
1146254885Sdumbbell#ifndef PRODUCT
1147254885Sdumbbell  // simulate GC crash here to dump java thread in error report
1148254885Sdumbbell  if (CrashGCForDumpingJavaThread) {
1149254885Sdumbbell    char *t = NULL;
1150254885Sdumbbell    *t = 'c';
1151254885Sdumbbell  }
1152254885Sdumbbell#endif
1153254885Sdumbbell  if (is_interpreted_frame()) {
1154254885Sdumbbell    oops_interpreted_do(f, cld_f, map, use_interpreter_oop_map_cache);
1155254885Sdumbbell  } else if (is_entry_frame()) {
1156254885Sdumbbell    oops_entry_do(f, map);
1157254885Sdumbbell  } else if (CodeCache::contains(pc())) {
1158254885Sdumbbell    oops_code_blob_do(f, cf, map);
1159254885Sdumbbell#ifdef SHARK
1160254885Sdumbbell  } else if (is_fake_stub_frame()) {
1161254885Sdumbbell    // nothing to do
1162254885Sdumbbell#endif // SHARK
1163254885Sdumbbell  } else {
1164254885Sdumbbell    ShouldNotReachHere();
1165254885Sdumbbell  }
1166254885Sdumbbell}
1167254885Sdumbbell
1168254885Sdumbbellvoid frame::nmethods_do(CodeBlobClosure* cf) {
1169254885Sdumbbell  if (_cb != NULL && _cb->is_nmethod()) {
1170254885Sdumbbell    cf->do_code_blob(_cb);
1171254885Sdumbbell  }
1172254885Sdumbbell}
1173254885Sdumbbell
1174254885Sdumbbell
1175254885Sdumbbell// call f() on the interpreted Method*s in the stack.
1176254885Sdumbbell// Have to walk the entire code cache for the compiled frames Yuck.
1177254885Sdumbbellvoid frame::metadata_do(void f(Metadata*)) {
1178254885Sdumbbell  if (_cb != NULL && Interpreter::contains(pc())) {
1179254885Sdumbbell    Method* m = this->interpreter_frame_method();
1180254885Sdumbbell    assert(m != NULL, "huh?");
1181254885Sdumbbell    f(m);
1182254885Sdumbbell  }
1183254885Sdumbbell}
1184254885Sdumbbell
1185254885Sdumbbellvoid frame::gc_prologue() {
1186254885Sdumbbell  if (is_interpreted_frame()) {
1187254885Sdumbbell    // set bcx to bci to become Method* position independent during GC
1188254885Sdumbbell    interpreter_frame_set_bcx(interpreter_frame_bci());
1189254885Sdumbbell  }
1190254885Sdumbbell}
1191254885Sdumbbell
1192254885Sdumbbell
1193254885Sdumbbellvoid frame::gc_epilogue() {
1194254885Sdumbbell  if (is_interpreted_frame()) {
1195254885Sdumbbell    // set bcx back to bcp for interpreter
1196254885Sdumbbell    interpreter_frame_set_bcx((intptr_t)interpreter_frame_bcp());
1197254885Sdumbbell  }
1198254885Sdumbbell  // call processor specific epilog function
1199254885Sdumbbell  pd_gc_epilog();
1200254885Sdumbbell}
1201254885Sdumbbell
1202254885Sdumbbell
1203254885Sdumbbell# ifdef ENABLE_ZAP_DEAD_LOCALS
1204254885Sdumbbell
1205254885Sdumbbellvoid frame::CheckValueClosure::do_oop(oop* p) {
1206254885Sdumbbell  if (CheckOopishValues && Universe::heap()->is_in_reserved(*p)) {
1207254885Sdumbbell    warning("value @ " INTPTR_FORMAT " looks oopish (" INTPTR_FORMAT ") (thread = " INTPTR_FORMAT ")", p, (address)*p, Thread::current());
1208254885Sdumbbell  }
1209254885Sdumbbell}
1210254885Sdumbbellframe::CheckValueClosure frame::_check_value;
1211254885Sdumbbell
1212254885Sdumbbell
1213254885Sdumbbellvoid frame::CheckOopClosure::do_oop(oop* p) {
1214254885Sdumbbell  if (*p != NULL && !(*p)->is_oop()) {
1215254885Sdumbbell    warning("value @ " INTPTR_FORMAT " should be an oop (" INTPTR_FORMAT ") (thread = " INTPTR_FORMAT ")", p, (address)*p, Thread::current());
1216254885Sdumbbell }
1217254885Sdumbbell}
1218254885Sdumbbellframe::CheckOopClosure frame::_check_oop;
1219254885Sdumbbell
1220254885Sdumbbellvoid frame::check_derived_oop(oop* base, oop* derived) {
1221254885Sdumbbell  _check_oop.do_oop(base);
1222254885Sdumbbell}
1223254885Sdumbbell
1224254885Sdumbbell
1225254885Sdumbbellvoid frame::ZapDeadClosure::do_oop(oop* p) {
1226254885Sdumbbell  if (TraceZapDeadLocals) tty->print_cr("zapping @ " INTPTR_FORMAT " containing " INTPTR_FORMAT, p, (address)*p);
1227254885Sdumbbell  *p = cast_to_oop<intptr_t>(0xbabebabe);
1228254885Sdumbbell}
1229254885Sdumbbellframe::ZapDeadClosure frame::_zap_dead;
1230254885Sdumbbell
1231254885Sdumbbellvoid frame::zap_dead_locals(JavaThread* thread, const RegisterMap* map) {
1232254885Sdumbbell  assert(thread == Thread::current(), "need to synchronize to do this to another thread");
1233254885Sdumbbell  // Tracing - part 1
1234254885Sdumbbell  if (TraceZapDeadLocals) {
1235254885Sdumbbell    ResourceMark rm(thread);
1236254885Sdumbbell    tty->print_cr("--------------------------------------------------------------------------------");
1237254885Sdumbbell    tty->print("Zapping dead locals in ");
1238254885Sdumbbell    print_on(tty);
1239254885Sdumbbell    tty->cr();
1240254885Sdumbbell  }
1241254885Sdumbbell  // Zapping
1242254885Sdumbbell       if (is_entry_frame      ()) zap_dead_entry_locals      (thread, map);
1243254885Sdumbbell  else if (is_interpreted_frame()) zap_dead_interpreted_locals(thread, map);
1244254885Sdumbbell  else if (is_compiled_frame()) zap_dead_compiled_locals   (thread, map);
1245254885Sdumbbell
1246254885Sdumbbell  else
1247254885Sdumbbell    // could be is_runtime_frame
1248254885Sdumbbell    // so remove error: ShouldNotReachHere();
1249254885Sdumbbell    ;
1250254885Sdumbbell  // Tracing - part 2
1251254885Sdumbbell  if (TraceZapDeadLocals) {
1252254885Sdumbbell    tty->cr();
1253254885Sdumbbell  }
1254254885Sdumbbell}
1255254885Sdumbbell
1256254885Sdumbbell
1257254885Sdumbbellvoid frame::zap_dead_interpreted_locals(JavaThread *thread, const RegisterMap* map) {
1258254885Sdumbbell  // get current interpreter 'pc'
1259254885Sdumbbell  assert(is_interpreted_frame(), "Not an interpreted frame");
1260254885Sdumbbell  Method* m   = interpreter_frame_method();
1261254885Sdumbbell  int       bci = interpreter_frame_bci();
1262254885Sdumbbell
1263254885Sdumbbell  int max_locals = m->is_native() ? m->size_of_parameters() : m->max_locals();
1264254885Sdumbbell
1265254885Sdumbbell  // process dynamic part
1266254885Sdumbbell  InterpreterFrameClosure value_blk(this, max_locals, m->max_stack(),
1267254885Sdumbbell                                    &_check_value);
1268254885Sdumbbell  InterpreterFrameClosure   oop_blk(this, max_locals, m->max_stack(),
1269254885Sdumbbell                                    &_check_oop  );
1270254885Sdumbbell  InterpreterFrameClosure  dead_blk(this, max_locals, m->max_stack(),
1271254885Sdumbbell                                    &_zap_dead   );
1272254885Sdumbbell
1273254885Sdumbbell  // get frame map
1274254885Sdumbbell  InterpreterOopMap mask;
1275254885Sdumbbell  m->mask_for(bci, &mask);
1276254885Sdumbbell  mask.iterate_all( &oop_blk, &value_blk, &dead_blk);
1277254885Sdumbbell}
1278254885Sdumbbell
1279254885Sdumbbell
1280254885Sdumbbellvoid frame::zap_dead_compiled_locals(JavaThread* thread, const RegisterMap* reg_map) {
1281254885Sdumbbell
1282254885Sdumbbell  ResourceMark rm(thread);
1283254885Sdumbbell  assert(_cb != NULL, "sanity check");
1284254885Sdumbbell  if (_cb->oop_maps() != NULL) {
1285254885Sdumbbell    OopMapSet::all_do(this, reg_map, &_check_oop, check_derived_oop, &_check_value);
1286254885Sdumbbell  }
1287254885Sdumbbell}
1288254885Sdumbbell
1289254885Sdumbbell
1290254885Sdumbbellvoid frame::zap_dead_entry_locals(JavaThread*, const RegisterMap*) {
1291254885Sdumbbell  if (TraceZapDeadLocals) warning("frame::zap_dead_entry_locals unimplemented");
1292254885Sdumbbell}
1293254885Sdumbbell
1294254885Sdumbbell
1295254885Sdumbbellvoid frame::zap_dead_deoptimized_locals(JavaThread*, const RegisterMap*) {
1296254885Sdumbbell  if (TraceZapDeadLocals) warning("frame::zap_dead_deoptimized_locals unimplemented");
1297254885Sdumbbell}
1298254885Sdumbbell
1299254885Sdumbbell# endif // ENABLE_ZAP_DEAD_LOCALS
1300254885Sdumbbell
1301254885Sdumbbellvoid frame::verify(const RegisterMap* map) {
1302254885Sdumbbell  // for now make sure receiver type is correct
1303254885Sdumbbell  if (is_interpreted_frame()) {
1304254885Sdumbbell    Method* method = interpreter_frame_method();
1305254885Sdumbbell    guarantee(method->is_method(), "method is wrong in frame::verify");
1306254885Sdumbbell    if (!method->is_static()) {
1307254885Sdumbbell      // fetch the receiver
1308254885Sdumbbell      oop* p = (oop*) interpreter_frame_local_at(0);
1309254885Sdumbbell      // make sure we have the right receiver type
1310254885Sdumbbell    }
1311254885Sdumbbell  }
1312254885Sdumbbell  COMPILER2_PRESENT(assert(DerivedPointerTable::is_empty(), "must be empty before verify");)
1313254885Sdumbbell  oops_do_internal(&VerifyOopClosure::verify_oop, NULL, NULL, (RegisterMap*)map, false);
1314254885Sdumbbell}
1315254885Sdumbbell
1316254885Sdumbbell
1317254885Sdumbbell#ifdef ASSERT
1318254885Sdumbbellbool frame::verify_return_pc(address x) {
1319254885Sdumbbell  if (StubRoutines::returns_to_call_stub(x)) {
1320254885Sdumbbell    return true;
1321254885Sdumbbell  }
1322254885Sdumbbell  if (CodeCache::contains(x)) {
1323254885Sdumbbell    return true;
1324254885Sdumbbell  }
1325254885Sdumbbell  if (Interpreter::contains(x)) {
1326254885Sdumbbell    return true;
1327254885Sdumbbell  }
1328254885Sdumbbell  return false;
1329254885Sdumbbell}
1330254885Sdumbbell#endif
1331254885Sdumbbell
1332254885Sdumbbell#ifdef ASSERT
1333254885Sdumbbellvoid frame::interpreter_frame_verify_monitor(BasicObjectLock* value) const {
1334254885Sdumbbell  assert(is_interpreted_frame(), "Not an interpreted frame");
1335254885Sdumbbell  // verify that the value is in the right part of the frame
1336254885Sdumbbell  address low_mark  = (address) interpreter_frame_monitor_end();
1337254885Sdumbbell  address high_mark = (address) interpreter_frame_monitor_begin();
1338254885Sdumbbell  address current   = (address) value;
1339254885Sdumbbell
1340254885Sdumbbell  const int monitor_size = frame::interpreter_frame_monitor_size();
1341254885Sdumbbell  guarantee((high_mark - current) % monitor_size  ==  0         , "Misaligned top of BasicObjectLock*");
1342254885Sdumbbell  guarantee( high_mark > current                                , "Current BasicObjectLock* higher than high_mark");
1343254885Sdumbbell
1344254885Sdumbbell  guarantee((current - low_mark) % monitor_size  ==  0         , "Misaligned bottom of BasicObjectLock*");
1345254885Sdumbbell  guarantee( current >= low_mark                               , "Current BasicObjectLock* below than low_mark");
1346254885Sdumbbell}
1347254885Sdumbbell#endif
1348254885Sdumbbell
1349254885Sdumbbell#ifndef PRODUCT
1350254885Sdumbbellvoid frame::describe(FrameValues& values, int frame_no) {
1351254885Sdumbbell  // boundaries: sp and the 'real' frame pointer
1352254885Sdumbbell  values.describe(-1, sp(), err_msg("sp for #%d", frame_no), 1);
1353254885Sdumbbell  intptr_t* frame_pointer = real_fp(); // Note: may differ from fp()
1354254885Sdumbbell
1355254885Sdumbbell  // print frame info at the highest boundary
1356254885Sdumbbell  intptr_t* info_address = MAX2(sp(), frame_pointer);
1357254885Sdumbbell
1358254885Sdumbbell  if (info_address != frame_pointer) {
1359254885Sdumbbell    // print frame_pointer explicitly if not marked by the frame info
1360254885Sdumbbell    values.describe(-1, frame_pointer, err_msg("frame pointer for #%d", frame_no), 1);
1361254885Sdumbbell  }
1362254885Sdumbbell
1363254885Sdumbbell  if (is_entry_frame() || is_compiled_frame() || is_interpreted_frame() || is_native_frame()) {
1364254885Sdumbbell    // Label values common to most frames
1365254885Sdumbbell    values.describe(-1, unextended_sp(), err_msg("unextended_sp for #%d", frame_no));
1366254885Sdumbbell  }
1367254885Sdumbbell
1368254885Sdumbbell  if (is_interpreted_frame()) {
1369254885Sdumbbell    Method* m = interpreter_frame_method();
1370254885Sdumbbell    int bci = interpreter_frame_bci();
1371254885Sdumbbell
1372254885Sdumbbell    // Label the method and current bci
1373254885Sdumbbell    values.describe(-1, info_address,
1374254885Sdumbbell                    FormatBuffer<1024>("#%d method %s @ %d", frame_no, m->name_and_sig_as_C_string(), bci), 2);
1375254885Sdumbbell    values.describe(-1, info_address,
1376254885Sdumbbell                    err_msg("- %d locals %d max stack", m->max_locals(), m->max_stack()), 1);
1377254885Sdumbbell    if (m->max_locals() > 0) {
1378254885Sdumbbell      intptr_t* l0 = interpreter_frame_local_at(0);
1379254885Sdumbbell      intptr_t* ln = interpreter_frame_local_at(m->max_locals() - 1);
1380254885Sdumbbell      values.describe(-1, MAX2(l0, ln), err_msg("locals for #%d", frame_no), 1);
1381254885Sdumbbell      // Report each local and mark as owned by this frame
1382254885Sdumbbell      for (int l = 0; l < m->max_locals(); l++) {
1383254885Sdumbbell        intptr_t* l0 = interpreter_frame_local_at(l);
1384254885Sdumbbell        values.describe(frame_no, l0, err_msg("local %d", l));
1385254885Sdumbbell      }
1386254885Sdumbbell    }
1387254885Sdumbbell
1388254885Sdumbbell    // Compute the actual expression stack size
1389254885Sdumbbell    InterpreterOopMap mask;
1390254885Sdumbbell    OopMapCache::compute_one_oop_map(m, bci, &mask);
1391254885Sdumbbell    intptr_t* tos = NULL;
1392254885Sdumbbell    // Report each stack element and mark as owned by this frame
1393254885Sdumbbell    for (int e = 0; e < mask.expression_stack_size(); e++) {
1394254885Sdumbbell      tos = MAX2(tos, interpreter_frame_expression_stack_at(e));
1395254885Sdumbbell      values.describe(frame_no, interpreter_frame_expression_stack_at(e),
1396254885Sdumbbell                      err_msg("stack %d", e));
1397254885Sdumbbell    }
1398254885Sdumbbell    if (tos != NULL) {
1399254885Sdumbbell      values.describe(-1, tos, err_msg("expression stack for #%d", frame_no), 1);
1400254885Sdumbbell    }
1401254885Sdumbbell    if (interpreter_frame_monitor_begin() != interpreter_frame_monitor_end()) {
1402254885Sdumbbell      values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_begin(), "monitors begin");
1403254885Sdumbbell      values.describe(frame_no, (intptr_t*)interpreter_frame_monitor_end(), "monitors end");
1404254885Sdumbbell    }
1405254885Sdumbbell  } else if (is_entry_frame()) {
1406254885Sdumbbell    // For now just label the frame
1407254885Sdumbbell    values.describe(-1, info_address, err_msg("#%d entry frame", frame_no), 2);
1408254885Sdumbbell  } else if (is_compiled_frame()) {
1409254885Sdumbbell    // For now just label the frame
1410254885Sdumbbell    nmethod* nm = cb()->as_nmethod_or_null();
1411254885Sdumbbell    values.describe(-1, info_address,
1412254885Sdumbbell                    FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for method %s%s", frame_no,
1413254885Sdumbbell                                       nm, nm->method()->name_and_sig_as_C_string(),
1414254885Sdumbbell                                       (_deopt_state == is_deoptimized) ?
1415254885Sdumbbell                                       " (deoptimized)" :
1416254885Sdumbbell                                       ((_deopt_state == unknown) ? " (state unknown)" : "")),
1417254885Sdumbbell                    2);
1418254885Sdumbbell  } else if (is_native_frame()) {
1419254885Sdumbbell    // For now just label the frame
1420254885Sdumbbell    nmethod* nm = cb()->as_nmethod_or_null();
1421254885Sdumbbell    values.describe(-1, info_address,
1422254885Sdumbbell                    FormatBuffer<1024>("#%d nmethod " INTPTR_FORMAT " for native method %s", frame_no,
1423254885Sdumbbell                                       nm, nm->method()->name_and_sig_as_C_string()), 2);
1424254885Sdumbbell  } else {
1425254885Sdumbbell    // provide default info if not handled before
1426254885Sdumbbell    char *info = (char *) "special frame";
1427254885Sdumbbell    if ((_cb != NULL) &&
1428254885Sdumbbell        (_cb->name() != NULL)) {
1429254885Sdumbbell      info = (char *)_cb->name();
1430254885Sdumbbell    }
1431254885Sdumbbell    values.describe(-1, info_address, err_msg("#%d <%s>", frame_no, info), 2);
1432254885Sdumbbell  }
1433254885Sdumbbell
1434254885Sdumbbell  // platform dependent additional data
1435254885Sdumbbell  describe_pd(values, frame_no);
1436254885Sdumbbell}
1437254885Sdumbbell
1438254885Sdumbbell#endif
1439254885Sdumbbell
1440254885Sdumbbell
1441254885Sdumbbell//-----------------------------------------------------------------------------------
1442254885Sdumbbell// StackFrameStream implementation
1443254885Sdumbbell
1444254885SdumbbellStackFrameStream::StackFrameStream(JavaThread *thread, bool update) : _reg_map(thread, update) {
1445254885Sdumbbell  assert(thread->has_last_Java_frame(), "sanity check");
1446254885Sdumbbell  _fr = thread->last_frame();
1447254885Sdumbbell  _is_done = false;
1448254885Sdumbbell}
1449254885Sdumbbell
1450254885Sdumbbell
1451254885Sdumbbell#ifndef PRODUCT
1452254885Sdumbbell
1453254885Sdumbbellvoid FrameValues::describe(int owner, intptr_t* location, const char* description, int priority) {
1454254885Sdumbbell  FrameValue fv;
1455254885Sdumbbell  fv.location = location;
1456254885Sdumbbell  fv.owner = owner;
1457254885Sdumbbell  fv.priority = priority;
1458254885Sdumbbell  fv.description = NEW_RESOURCE_ARRAY(char, strlen(description) + 1);
1459254885Sdumbbell  strcpy(fv.description, description);
1460254885Sdumbbell  _values.append(fv);
1461254885Sdumbbell}
1462254885Sdumbbell
1463254885Sdumbbell
1464254885Sdumbbell#ifdef ASSERT
1465254885Sdumbbellvoid FrameValues::validate() {
1466254885Sdumbbell  _values.sort(compare);
1467254885Sdumbbell  bool error = false;
1468254885Sdumbbell  FrameValue prev;
1469254885Sdumbbell  prev.owner = -1;
1470254885Sdumbbell  for (int i = _values.length() - 1; i >= 0; i--) {
1471254885Sdumbbell    FrameValue fv = _values.at(i);
1472254885Sdumbbell    if (fv.owner == -1) continue;
1473254885Sdumbbell    if (prev.owner == -1) {
1474254885Sdumbbell      prev = fv;
1475254885Sdumbbell      continue;
1476254885Sdumbbell    }
1477254885Sdumbbell    if (prev.location == fv.location) {
1478254885Sdumbbell      if (fv.owner != prev.owner) {
1479254885Sdumbbell        tty->print_cr("overlapping storage");
1480254885Sdumbbell        tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", prev.location, *prev.location, prev.description);
1481254885Sdumbbell        tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", fv.location, *fv.location, fv.description);
1482254885Sdumbbell        error = true;
1483254885Sdumbbell      }
1484254885Sdumbbell    } else {
1485254885Sdumbbell      prev = fv;
1486254885Sdumbbell    }
1487254885Sdumbbell  }
1488254885Sdumbbell  assert(!error, "invalid layout");
1489254885Sdumbbell}
1490254885Sdumbbell#endif // ASSERT
1491254885Sdumbbell
1492254885Sdumbbellvoid FrameValues::print(JavaThread* thread) {
1493254885Sdumbbell  _values.sort(compare);
1494254885Sdumbbell
1495254885Sdumbbell  // Sometimes values like the fp can be invalid values if the
1496254885Sdumbbell  // register map wasn't updated during the walk.  Trim out values
1497254885Sdumbbell  // that aren't actually in the stack of the thread.
1498254885Sdumbbell  int min_index = 0;
1499254885Sdumbbell  int max_index = _values.length() - 1;
1500254885Sdumbbell  intptr_t* v0 = _values.at(min_index).location;
1501254885Sdumbbell  intptr_t* v1 = _values.at(max_index).location;
1502254885Sdumbbell
1503254885Sdumbbell  if (thread == Thread::current()) {
1504254885Sdumbbell    while (!thread->is_in_stack((address)v0)) {
1505254885Sdumbbell      v0 = _values.at(++min_index).location;
1506254885Sdumbbell    }
1507254885Sdumbbell    while (!thread->is_in_stack((address)v1)) {
1508254885Sdumbbell      v1 = _values.at(--max_index).location;
1509254885Sdumbbell    }
1510254885Sdumbbell  } else {
1511254885Sdumbbell    while (!thread->on_local_stack((address)v0)) {
1512254885Sdumbbell      v0 = _values.at(++min_index).location;
1513254885Sdumbbell    }
1514254885Sdumbbell    while (!thread->on_local_stack((address)v1)) {
1515254885Sdumbbell      v1 = _values.at(--max_index).location;
1516254885Sdumbbell    }
1517254885Sdumbbell  }
1518254885Sdumbbell  intptr_t* min = MIN2(v0, v1);
1519254885Sdumbbell  intptr_t* max = MAX2(v0, v1);
1520254885Sdumbbell  intptr_t* cur = max;
1521254885Sdumbbell  intptr_t* last = NULL;
1522254885Sdumbbell  for (int i = max_index; i >= min_index; i--) {
1523254885Sdumbbell    FrameValue fv = _values.at(i);
1524254885Sdumbbell    while (cur > fv.location) {
1525254885Sdumbbell      tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT, cur, *cur);
1526254885Sdumbbell      cur--;
1527254885Sdumbbell    }
1528254885Sdumbbell    if (last == fv.location) {
1529254885Sdumbbell      const char* spacer = "          " LP64_ONLY("        ");
1530254885Sdumbbell      tty->print_cr(" %s  %s %s", spacer, spacer, fv.description);
1531254885Sdumbbell    } else {
1532254885Sdumbbell      tty->print_cr(" " INTPTR_FORMAT ": " INTPTR_FORMAT " %s", fv.location, *fv.location, fv.description);
1533254885Sdumbbell      last = fv.location;
1534254885Sdumbbell      cur--;
1535254885Sdumbbell    }
1536254885Sdumbbell  }
1537254885Sdumbbell}
1538254885Sdumbbell
1539254885Sdumbbell#endif // ndef PRODUCT
1540254885Sdumbbell