1 /*
   2  * Copyright (c) 1997, 2016, 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 #ifndef SHARE_VM_RUNTIME_VFRAME_HPP
  26 #define SHARE_VM_RUNTIME_VFRAME_HPP
  27 
  28 #include "code/debugInfo.hpp"
  29 #include "code/debugInfoRec.hpp"
  30 #include "code/location.hpp"
  31 #include "oops/oop.hpp"
  32 #include "runtime/frame.hpp"
  33 #include "runtime/frame.inline.hpp"
  34 #include "runtime/stackValue.hpp"
  35 #include "runtime/stackValueCollection.hpp"
  36 #include "utilities/growableArray.hpp"
  37 
  38 // vframes are virtual stack frames representing source level activations.
  39 // A single frame may hold several source level activations in the case of
  40 // optimized code. The debugging stored with the optimized code enables
  41 // us to unfold a frame as a stack of vframes.
  42 // A cVFrame represents an activation of a non-java method.
  43 
  44 // The vframe inheritance hierarchy:
  45 // - vframe
  46 //   - javaVFrame
  47 //     - interpretedVFrame
  48 //     - compiledVFrame     ; (used for both compiled Java methods and native stubs)
  49 //   - externalVFrame
  50 //     - entryVFrame        ; special frame created when calling Java from C
  51 
  52 // - BasicLock
  53 
  54 class vframe: public ResourceObj {
  55  protected:
  56   frame        _fr;      // Raw frame behind the virtual frame.
  57   RegisterMap  _reg_map; // Register map for the raw frame (used to handle callee-saved registers).
  58   JavaThread*  _thread;  // The thread owning the raw frame.
  59 
  60   vframe(const frame* fr, const RegisterMap* reg_map, JavaThread* thread);
  61   vframe(const frame* fr, JavaThread* thread);
  62  public:
  63   // Factory method for creating vframes
  64   static vframe* new_vframe(const frame* f, const RegisterMap *reg_map, JavaThread* thread);
  65 
  66   // Accessors
  67   frame              fr()           const { return _fr;       }
  68   CodeBlob*          cb()         const { return _fr.cb();  }
  69   CompiledMethod*   nm()         const {
  70       assert( cb() != NULL && cb()->is_compiled(), "usage");
  71       return (CompiledMethod*) cb();
  72   }
  73 
  74 // ???? Does this need to be a copy?
  75   frame*             frame_pointer() { return &_fr;       }
  76   const RegisterMap* register_map() const { return &_reg_map; }
  77   JavaThread*        thread()       const { return _thread;   }
  78 
  79   // Returns the sender vframe
  80   virtual vframe* sender() const;
  81 
  82   // Returns the next javaVFrame on the stack (skipping all other kinds of frame)
  83   javaVFrame *java_sender() const;
  84 
  85   // Answers if the this is the top vframe in the frame, i.e., if the sender vframe
  86   // is in the caller frame
  87   virtual bool is_top() const { return true; }
  88 
  89   // Returns top vframe within same frame (see is_top())
  90   virtual vframe* top() const;
  91 
  92   // Type testing operations
  93   virtual bool is_entry_frame()       const { return false; }
  94   virtual bool is_java_frame()        const { return false; }
  95   virtual bool is_interpreted_frame() const { return false; }
  96   virtual bool is_compiled_frame()    const { return false; }
  97 
  98 #ifndef PRODUCT
  99   // printing operations
 100   virtual void print_value() const;
 101   virtual void print();
 102 #endif
 103 };
 104 
 105 
 106 class javaVFrame: public vframe {
 107  public:
 108   // JVM state
 109   virtual Method*                      method()         const = 0;
 110   virtual int                          bci()            const = 0;
 111   virtual StackValueCollection*        locals()         const = 0;
 112   virtual StackValueCollection*        expressions()    const = 0;
 113   // the order returned by monitors() is from oldest -> youngest#4418568
 114   virtual GrowableArray<MonitorInfo*>* monitors()       const = 0;
 115 
 116   // Debugging support via JVMTI.
 117   // NOTE that this is not guaranteed to give correct results for compiled vframes.
 118   // Deoptimize first if necessary.
 119   virtual void set_locals(StackValueCollection* values) const = 0;
 120 
 121   // Test operation
 122   bool is_java_frame() const { return true; }
 123 
 124  protected:
 125   javaVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread) : vframe(fr, reg_map, thread) {}
 126   javaVFrame(const frame* fr, JavaThread* thread) : vframe(fr, thread) {}
 127 
 128  public:
 129   // casting
 130   static javaVFrame* cast(vframe* vf) {
 131     assert(vf == NULL || vf->is_java_frame(), "must be java frame");
 132     return (javaVFrame*) vf;
 133   }
 134 
 135   // Return an array of monitors locked by this frame in the youngest to oldest order
 136   GrowableArray<MonitorInfo*>* locked_monitors();
 137 
 138   // printing used during stack dumps and diagnostics
 139   static void print_locked_object_class_name(outputStream* st, Handle obj, const char* lock_state);
 140   void print_lock_info_on(outputStream* st, int frame_count);
 141   void print_lock_info(int frame_count) { print_lock_info_on(tty, frame_count); }
 142 
 143 #ifndef PRODUCT
 144  public:
 145   // printing operations
 146   void print();
 147   void print_value() const;
 148   void print_activation(int index) const;
 149 
 150   // verify operations
 151   virtual void verify() const;
 152 
 153   // Structural compare
 154   bool structural_compare(javaVFrame* other);
 155 #endif
 156   friend class vframe;
 157 };
 158 
 159 class interpretedVFrame: public javaVFrame {
 160  public:
 161   // JVM state
 162   Method*                      method()         const;
 163   int                          bci()            const;
 164   StackValueCollection*        locals()         const;
 165   StackValueCollection*        expressions()    const;
 166   GrowableArray<MonitorInfo*>* monitors()       const;
 167 
 168   void set_locals(StackValueCollection* values) const;
 169 
 170   // Test operation
 171   bool is_interpreted_frame() const { return true; }
 172 
 173  protected:
 174   interpretedVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread) : javaVFrame(fr, reg_map, thread) {};
 175 
 176  public:
 177   // Accessors for Byte Code Pointer
 178   u_char* bcp() const;
 179   void set_bcp(u_char* bcp);
 180 
 181   // casting
 182   static interpretedVFrame* cast(vframe* vf) {
 183     assert(vf == NULL || vf->is_interpreted_frame(), "must be interpreted frame");
 184     return (interpretedVFrame*) vf;
 185   }
 186 
 187  private:
 188   static const int bcp_offset;
 189   intptr_t* locals_addr_at(int offset) const;
 190   StackValueCollection* stack_data(bool expressions) const;
 191   // returns where the parameters starts relative to the frame pointer
 192   int start_of_parameters() const;
 193 
 194 #ifndef PRODUCT
 195  public:
 196   // verify operations
 197   void verify() const;
 198 #endif
 199   friend class vframe;
 200 };
 201 
 202 
 203 class externalVFrame: public vframe {
 204  protected:
 205   externalVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread) : vframe(fr, reg_map, thread) {}
 206 
 207 #ifndef PRODUCT
 208  public:
 209   // printing operations
 210   void print_value() const;
 211   void print();
 212 #endif
 213   friend class vframe;
 214 };
 215 
 216 class entryVFrame: public externalVFrame {
 217  public:
 218   bool is_entry_frame() const { return true; }
 219 
 220  protected:
 221   entryVFrame(const frame* fr, const RegisterMap* reg_map, JavaThread* thread);
 222 
 223  public:
 224   // casting
 225   static entryVFrame* cast(vframe* vf) {
 226     assert(vf == NULL || vf->is_entry_frame(), "must be entry frame");
 227     return (entryVFrame*) vf;
 228   }
 229 
 230 #ifndef PRODUCT
 231  public:
 232   // printing
 233   void print_value() const;
 234   void print();
 235 #endif
 236   friend class vframe;
 237 };
 238 
 239 
 240 // A MonitorInfo is a ResourceObject that describes a the pair:
 241 // 1) the owner of the monitor
 242 // 2) the monitor lock
 243 class MonitorInfo : public ResourceObj {
 244  private:
 245   oop        _owner; // the object owning the monitor
 246   BasicLock* _lock;
 247   oop        _owner_klass; // klass (mirror) if owner was scalar replaced
 248   bool       _eliminated;
 249   bool       _owner_is_scalar_replaced;
 250  public:
 251   // Constructor
 252   MonitorInfo(oop owner, BasicLock* lock, bool eliminated, bool owner_is_scalar_replaced) {
 253     if (!owner_is_scalar_replaced) {
 254       _owner = owner;
 255       _owner_klass = NULL;
 256     } else {
 257       assert(eliminated, "monitor should be eliminated for scalar replaced object");
 258       _owner = NULL;
 259       _owner_klass = owner;
 260     }
 261     _lock  = lock;
 262     _eliminated = eliminated;
 263     _owner_is_scalar_replaced = owner_is_scalar_replaced;
 264   }
 265   // Accessors
 266   oop        owner() const {
 267     assert(!_owner_is_scalar_replaced, "should not be called for scalar replaced object");
 268     return _owner;
 269   }
 270   oop   owner_klass() const {
 271     assert(_owner_is_scalar_replaced, "should not be called for not scalar replaced object");
 272     return _owner_klass;
 273   }
 274   BasicLock* lock()  const { return _lock;  }
 275   bool eliminated()  const { return _eliminated; }
 276   bool owner_is_scalar_replaced()  const { return _owner_is_scalar_replaced; }
 277 };
 278 
 279 class vframeStreamCommon : StackObj {
 280  protected:
 281   // common
 282   frame        _frame;
 283   JavaThread*  _thread;
 284   RegisterMap  _reg_map;
 285   enum { interpreted_mode, compiled_mode, at_end_mode } _mode;
 286 
 287   int _sender_decode_offset;
 288 
 289   // Cached information
 290   Method* _method;
 291   int       _bci;
 292 
 293   // Should VM activations be ignored or not
 294   bool _stop_at_java_call_stub;
 295 
 296   bool fill_in_compiled_inlined_sender();
 297   void fill_from_compiled_frame(int decode_offset);
 298   void fill_from_compiled_native_frame();
 299 
 300   void found_bad_method_frame();
 301 
 302   void fill_from_interpreter_frame();
 303   bool fill_from_frame();
 304 
 305   // Helper routine for security_get_caller_frame
 306   void skip_prefixed_method_and_wrappers();
 307 
 308  public:
 309   // Constructor
 310   vframeStreamCommon(JavaThread* thread) : _reg_map(thread, false) {
 311     _thread = thread;
 312   }
 313 
 314   // Accessors
 315   Method* method() const { return _method; }
 316   int bci() const { return _bci; }
 317   intptr_t* frame_id() const { return _frame.id(); }
 318   address frame_pc() const { return _frame.pc(); }
 319 
 320   javaVFrame* java_frame() {
 321     vframe* vf = vframe::new_vframe(&_frame, &_reg_map, _thread);
 322     if (vf->is_java_frame()) {
 323       return (javaVFrame*)vf;
 324     }
 325     return NULL;
 326   }
 327 
 328   CodeBlob*          cb()         const { return _frame.cb();  }
 329   CompiledMethod*   nm()         const {
 330       assert( cb() != NULL && cb()->is_compiled(), "usage");
 331       return (CompiledMethod*) cb();
 332   }
 333 
 334   // Frame type
 335   bool is_interpreted_frame() const { return _frame.is_interpreted_frame(); }
 336   bool is_entry_frame() const       { return _frame.is_entry_frame(); }
 337 
 338   // Iteration
 339   void next() {
 340     // handle frames with inlining
 341     if (_mode == compiled_mode    && fill_in_compiled_inlined_sender()) return;
 342 
 343     // handle general case
 344     do {
 345       _frame = _frame.sender(&_reg_map);
 346     } while (!fill_from_frame());
 347   }
 348   void security_next();
 349 
 350   bool at_end() const { return _mode == at_end_mode; }
 351 
 352   // Implements security traversal. Skips depth no. of frame including
 353   // special security frames and prefixed native methods
 354   void security_get_caller_frame(int depth);
 355 
 356   // Helper routine for JVM_LatestUserDefinedLoader -- needed for 1.4
 357   // reflection implementation
 358   void skip_reflection_related_frames();
 359 };
 360 
 361 class vframeStream : public vframeStreamCommon {
 362  public:
 363   // Constructors
 364   vframeStream(JavaThread* thread, bool stop_at_java_call_stub = false)
 365     : vframeStreamCommon(thread) {
 366     _stop_at_java_call_stub = stop_at_java_call_stub;
 367 
 368     if (!thread->has_last_Java_frame()) {
 369       _mode = at_end_mode;
 370       return;
 371     }
 372 
 373     _frame = _thread->last_frame();
 374     while (!fill_from_frame()) {
 375       _frame = _frame.sender(&_reg_map);
 376     }
 377   }
 378 
 379   // top_frame may not be at safepoint, start with sender
 380   vframeStream(JavaThread* thread, frame top_frame, bool stop_at_java_call_stub = false);
 381 };
 382 
 383 
 384 inline bool vframeStreamCommon::fill_in_compiled_inlined_sender() {
 385   if (_sender_decode_offset == DebugInformationRecorder::serialized_null) {
 386     return false;
 387   }
 388   fill_from_compiled_frame(_sender_decode_offset);
 389   return true;
 390 }
 391 
 392 
 393 inline void vframeStreamCommon::fill_from_compiled_frame(int decode_offset) {
 394   _mode = compiled_mode;
 395 
 396   // Range check to detect ridiculous offsets.
 397   if (decode_offset == DebugInformationRecorder::serialized_null ||
 398       decode_offset < 0 ||
 399       decode_offset >= nm()->scopes_data_size()) {
 400     // 6379830 AsyncGetCallTrace sometimes feeds us wild frames.
 401     // If we read nmethod::scopes_data at serialized_null (== 0)
 402     // or if read some at other invalid offset, invalid values will be decoded.
 403     // Based on these values, invalid heap locations could be referenced
 404     // that could lead to crashes in product mode.
 405     // Therefore, do not use the decode offset if invalid, but fill the frame
 406     // as it were a native compiled frame (no Java-level assumptions).
 407 #ifdef ASSERT
 408     if (WizardMode) {
 409       ttyLocker ttyl;
 410       tty->print_cr("Error in fill_from_frame: pc_desc for "
 411                     INTPTR_FORMAT " not found or invalid at %d",
 412                     p2i(_frame.pc()), decode_offset);
 413       nm()->print();
 414       nm()->method()->print_codes();
 415       nm()->print_code();
 416       nm()->print_pcs();
 417     }
 418 #endif
 419     // Provide a cheap fallback in product mode.  (See comment above.)
 420     found_bad_method_frame();
 421     fill_from_compiled_native_frame();
 422     return;
 423   }
 424 
 425   // Decode first part of scopeDesc
 426   DebugInfoReadStream buffer(nm(), decode_offset);
 427   _sender_decode_offset = buffer.read_int();
 428   _method               = buffer.read_method();
 429   _bci                  = buffer.read_bci();
 430 
 431   assert(_method->is_method(), "checking type of decoded method");
 432 }
 433 
 434 // The native frames are handled specially. We do not rely on ScopeDesc info
 435 // since the pc might not be exact due to the _last_native_pc trick.
 436 inline void vframeStreamCommon::fill_from_compiled_native_frame() {
 437   _mode = compiled_mode;
 438   _sender_decode_offset = DebugInformationRecorder::serialized_null;
 439   _method = nm()->method();
 440   _bci = 0;
 441 }
 442 
 443 inline bool vframeStreamCommon::fill_from_frame() {
 444   // Interpreted frame
 445   if (_frame.is_interpreted_frame()) {
 446     fill_from_interpreter_frame();
 447     return true;
 448   }
 449 
 450   // Compiled frame
 451 
 452   if (cb() != NULL && cb()->is_compiled()) {
 453     if (nm()->is_native_method()) {
 454       // Do not rely on scopeDesc since the pc might be unprecise due to the _last_native_pc trick.
 455       fill_from_compiled_native_frame();
 456     } else {
 457       PcDesc* pc_desc = nm()->pc_desc_at(_frame.pc());
 458       int decode_offset;
 459       if (pc_desc == NULL) {
 460         // Should not happen, but let fill_from_compiled_frame handle it.
 461 
 462         // If we are trying to walk the stack of a thread that is not
 463         // at a safepoint (like AsyncGetCallTrace would do) then this is an
 464         // acceptable result. [ This is assuming that safe_for_sender
 465         // is so bullet proof that we can trust the frames it produced. ]
 466         //
 467         // So if we see that the thread is not safepoint safe
 468         // then simply produce the method and a bci of zero
 469         // and skip the possibility of decoding any inlining that
 470         // may be present. That is far better than simply stopping (or
 471         // asserting. If however the thread is safepoint safe this
 472         // is the sign of a compiler bug  and we'll let
 473         // fill_from_compiled_frame handle it.
 474 
 475 
 476         JavaThreadState state = _thread->thread_state();
 477 
 478         // in_Java should be good enough to test safepoint safety
 479         // if state were say in_Java_trans then we'd expect that
 480         // the pc would have already been slightly adjusted to
 481         // one that would produce a pcDesc since the trans state
 482         // would be one that might in fact anticipate a safepoint
 483 
 484         if (state == _thread_in_Java ) {
 485           // This will get a method a zero bci and no inlining.
 486           // Might be nice to have a unique bci to signify this
 487           // particular case but for now zero will do.
 488 
 489           fill_from_compiled_native_frame();
 490 
 491           // There is something to be said for setting the mode to
 492           // at_end_mode to prevent trying to walk further up the
 493           // stack. There is evidence that if we walk any further
 494           // that we could produce a bad stack chain. However until
 495           // we see evidence that allowing this causes us to find
 496           // frames bad enough to cause segv's or assertion failures
 497           // we don't do it as while we may get a bad call chain the
 498           // probability is much higher (several magnitudes) that we
 499           // get good data.
 500 
 501           return true;
 502         }
 503         decode_offset = DebugInformationRecorder::serialized_null;
 504       } else {
 505         decode_offset = pc_desc->scope_decode_offset();
 506       }
 507       fill_from_compiled_frame(decode_offset);
 508     }
 509     return true;
 510   }
 511 
 512   // End of stack?
 513   if (_frame.is_first_frame() || (_stop_at_java_call_stub && _frame.is_entry_frame())) {
 514     _mode = at_end_mode;
 515     return true;
 516   }
 517 
 518   return false;
 519 }
 520 
 521 
 522 inline void vframeStreamCommon::fill_from_interpreter_frame() {
 523   Method* method = _frame.interpreter_frame_method();
 524   address   bcp    = _frame.interpreter_frame_bcp();
 525   int       bci    = method->validate_bci_from_bcp(bcp);
 526   // 6379830 AsyncGetCallTrace sometimes feeds us wild frames.
 527   // AsyncGetCallTrace interrupts the VM asynchronously. As a result
 528   // it is possible to access an interpreter frame for which
 529   // no Java-level information is yet available (e.g., becasue
 530   // the frame was being created when the VM interrupted it).
 531   // In this scenario, pretend that the interpreter is at the point
 532   // of entering the method.
 533   if (bci < 0) {
 534     found_bad_method_frame();
 535     bci = 0;
 536   }
 537   _mode   = interpreted_mode;
 538   _method = method;
 539   _bci    = bci;
 540 }
 541 
 542 #endif // SHARE_VM_RUNTIME_VFRAME_HPP