1 /*
   2  * Copyright (c) 2015, 2019, 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 
  26 #ifndef SHARE_PRIMS_STACKWALK_HPP
  27 #define SHARE_PRIMS_STACKWALK_HPP
  28 
  29 #include "oops/oop.hpp"
  30 #include "runtime/continuation.hpp"
  31 #include "runtime/vframe.hpp"
  32 
  33 // BaseFrameStream is an abstract base class for encapsulating the VM-side
  34 // implementation of the StackWalker API.  There are two concrete subclasses:
  35 // - JavaFrameStream:
  36 //     -based on vframeStream; used in most instances
  37 // - LiveFrameStream:
  38 //     -based on javaVFrame; used for retrieving locals/monitors/operands for
  39 //      LiveStackFrame
  40 class BaseFrameStream : public StackObj {
  41 private:
  42   enum {
  43     magic_pos = 0
  44   };
  45 
  46   JavaThread*           _thread;
  47   Handle                _continuation;
  48   jlong                 _anchor;
  49 
  50 protected:
  51   void fill_stackframe(Handle stackFrame, const methodHandle& method, TRAPS);
  52 public:
  53   BaseFrameStream(JavaThread* thread, Handle continuation);
  54 
  55   virtual void    next()=0;
  56   virtual bool    at_end()=0;
  57 
  58   virtual Method* method()=0;
  59   virtual int     bci()=0;
  60   virtual oop     cont()=0; // returns the current continuation (even when walking a thread)
  61 
  62   virtual void    fill_frame(int index, objArrayHandle  frames_array,
  63                              const methodHandle& method, TRAPS)=0;
  64   
  65   Handle continuation() { return _continuation; }
  66   virtual void set_continuation(Handle cont);
  67 
  68   void setup_magic_on_entry(objArrayHandle frames_array);
  69   bool check_magic(objArrayHandle frames_array);
  70   bool cleanup_magic_on_exit(objArrayHandle frames_array);
  71 
  72   bool is_valid_in(Thread* thread, objArrayHandle frames_array) {
  73     return (_thread == thread && check_magic(frames_array));
  74   }
  75 
  76   jlong address_value() {
  77     return (jlong) castable_address(this);
  78   }
  79 
  80   static BaseFrameStream* from_current(JavaThread* thread, jlong magic, objArrayHandle frames_array);
  81 };
  82 
  83 class JavaFrameStream : public BaseFrameStream {
  84 private:
  85   vframeStream          _vfst;
  86   bool                  _need_method_info;
  87 
  88 public:
  89   JavaFrameStream(JavaThread* thread, int mode, Handle cont_scope, Handle cont);
  90 
  91   void next();
  92   bool at_end()    { return _vfst.at_end(); }
  93 
  94   Method* method() { return _vfst.method(); }
  95   int bci()        { return _vfst.bci(); }
  96   oop cont()       { return _vfst.continuation(); }
  97 
  98   void fill_frame(int index, objArrayHandle  frames_array,
  99                   const methodHandle& method, TRAPS);
 100 
 101   void set_continuation(Handle cont);
 102 };
 103 
 104 class LiveFrameStream : public BaseFrameStream {
 105 private:
 106   enum {
 107     MODE_INTERPRETED = 0x01,
 108     MODE_COMPILED    = 0x02
 109   };
 110 
 111   Handle                _cont_scope;  // the delimitation of this walk
 112 
 113   RegisterMap*          _map;
 114   javaVFrame*           _jvf;
 115   Handle                _cont; // the current continuation
 116 
 117   void fill_live_stackframe(Handle stackFrame, const methodHandle& method, TRAPS);
 118   static oop create_primitive_slot_instance(StackValueCollection* values,
 119                                             int i, BasicType type, TRAPS);
 120   static objArrayHandle monitors_to_object_array(GrowableArray<MonitorInfo*>* monitors,
 121                                                  TRAPS);
 122   static objArrayHandle values_to_object_array(StackValueCollection* values, TRAPS);
 123 public:
 124   LiveFrameStream(JavaThread* thread, RegisterMap* rm, Handle cont_scope, Handle cont);
 125 
 126   void next();
 127   bool at_end()    { return _jvf == NULL; }
 128 
 129   Method* method() { return _jvf->method(); }
 130   int bci()        { return _jvf->bci(); }
 131   oop cont()       { return _jvf->continuation(); }
 132 
 133   void fill_frame(int index, objArrayHandle  frames_array,
 134                   const methodHandle& method, TRAPS);
 135 
 136   void set_continuation(Handle cont);
 137 };
 138 
 139 class StackWalk : public AllStatic {
 140 private:
 141   static int fill_in_frames(jlong mode, BaseFrameStream& stream,
 142                             int max_nframes, int start_index,
 143                             objArrayHandle frames_array,
 144                             int& end_index, TRAPS);
 145 
 146   static inline bool get_caller_class(int mode) {
 147     return (mode & JVM_STACKWALK_GET_CALLER_CLASS) != 0;
 148   }
 149   static inline bool skip_hidden_frames(int mode) {
 150     return (mode & JVM_STACKWALK_SHOW_HIDDEN_FRAMES) == 0;
 151   }
 152   static inline bool live_frame_info(int mode) {
 153     return (mode & JVM_STACKWALK_FILL_LIVE_STACK_FRAMES) != 0;
 154   }
 155 
 156 public:
 157   static inline bool need_method_info(int mode) {
 158     return (mode & JVM_STACKWALK_FILL_CLASS_REFS_ONLY) == 0;
 159   }
 160   static inline bool use_frames_array(int mode) {
 161     return (mode & JVM_STACKWALK_FILL_CLASS_REFS_ONLY) == 0;
 162   }
 163   static oop walk(Handle stackStream, jlong mode, int skip_frames, Handle cont_scope, Handle cont,
 164                   int frame_count, int start_index, objArrayHandle frames_array,
 165                   TRAPS);
 166 
 167   static oop fetchFirstBatch(BaseFrameStream& stream, Handle stackStream,
 168                              jlong mode, int skip_frames, int frame_count,
 169                              int start_index, objArrayHandle frames_array, TRAPS);
 170 
 171   static jint fetchNextBatch(Handle stackStream, jlong mode, jlong magic,
 172                              int frame_count, int start_index,
 173                              objArrayHandle frames_array, TRAPS);
 174 
 175   static void setContinuation(Handle stackStream, jlong magic, objArrayHandle frames_array, 
 176                               Handle cont, TRAPS);
 177 };
 178 #endif // SHARE_PRIMS_STACKWALK_HPP