1 /*
   2  * Copyright (c) 2003, 2012, 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_PRIMS_JVMTIENVTHREADSTATE_HPP
  26 #define SHARE_VM_PRIMS_JVMTIENVTHREADSTATE_HPP
  27 
  28 #include "jvmtifiles/jvmti.h"
  29 #include "memory/allocation.hpp"
  30 #include "oops/instanceKlass.hpp"
  31 #include "prims/jvmtiEventController.hpp"
  32 #include "utilities/globalDefinitions.hpp"
  33 #include "utilities/growableArray.hpp"
  34 
  35 class JvmtiEnv;
  36 
  37 ///////////////////////////////////////////////////////////////
  38 //
  39 // class JvmtiFramePop
  40 // Used by              : JvmtiFramePops
  41 // Used by JVMTI methods: none directly.
  42 //
  43 // Wrapper class for FramePop, used in the JvmtiFramePops class.
  44 //
  45 // Two problems: 1) this isn't being used as a ValueObj class, in
  46 // several places there are constructors for it. 2) It seems like
  47 // overkill as a means to get an assert and name the geater than
  48 // operator.  I'm trying to to rewrite everything.
  49 
  50 class JvmtiFramePop VALUE_OBJ_CLASS_SPEC {
  51  private:
  52   // Frame number counting from BOTTOM (oldest) frame;
  53   // bottom frame == #0
  54   int _frame_number;
  55  public:
  56   JvmtiFramePop() {}
  57   JvmtiFramePop(int frame_number) {
  58     assert(frame_number >= 0, "invalid frame number");
  59     _frame_number = frame_number;
  60   }
  61 
  62   int frame_number() { return _frame_number; }
  63   int above_on_stack(JvmtiFramePop& other) { return _frame_number > other._frame_number; }
  64   void print() PRODUCT_RETURN;
  65 };
  66 
  67 
  68 ///////////////////////////////////////////////////////////////
  69 //
  70 // class JvmtiFramePops
  71 // Used by              : JvmtiThreadState
  72 // Used by JVMTI methods: none directly.
  73 //
  74 // A collection of JvmtiFramePop.
  75 // It records what frames on a threads stack should post frame_pop events when they're exited.
  76 //
  77 
  78 class JvmtiFramePops : public CHeapObj<mtInternal> {
  79  private:
  80   GrowableArray<int>* _pops;
  81 
  82   // should only be used by JvmtiEventControllerPrivate
  83   // to insure they only occur at safepoints.
  84   // Todo: add checks for safepoint
  85   friend class JvmtiEventControllerPrivate;
  86   void set(JvmtiFramePop& fp);
  87   void clear(JvmtiFramePop& fp);
  88   int clear_to(JvmtiFramePop& fp);
  89 
  90  public:
  91   JvmtiFramePops();
  92   ~JvmtiFramePops();
  93 
  94   bool contains(JvmtiFramePop& fp) { return _pops->contains(fp.frame_number()); }
  95   int length() { return _pops->length(); }
  96   void print() PRODUCT_RETURN;
  97 };
  98 
  99 
 100 ///////////////////////////////////////////////////////////////
 101 //
 102 // class JvmtiEnvThreadState
 103 //
 104 // 2. Cache of pending frame_pop_events, created by NotifyFramePop
 105 //    and lazily initialized.
 106 // 3: Location of last executed instruction, used to filter out duplicate
 107 //    events due to instruction rewriting.
 108 
 109 class JvmtiEnvThreadState : public CHeapObj<mtInternal> {
 110 private:
 111   friend class JvmtiEnv;
 112   JavaThread        *_thread;
 113   JvmtiEnv          *_env;
 114   JvmtiEnvThreadState *_next;
 115   jmethodID         _current_method_id;
 116   int               _current_bci;
 117   bool              _breakpoint_posted;
 118   bool              _single_stepping_posted;
 119   JvmtiEnvThreadEventEnable _event_enable;
 120   void              *_agent_thread_local_storage_data; // per env and per thread agent allocated data.
 121 
 122   // Class used to store pending framepops.
 123   // lazily initialized by get_frame_pops();
 124   JvmtiFramePops *_frame_pops;
 125 
 126   inline void set_current_location(jmethodID method_id, int bci) {
 127     _current_method_id = method_id;
 128     _current_bci  = bci;
 129   }
 130 
 131   friend class JvmtiEnvThreadStateIterator;
 132   JvmtiEnvThreadState* next() { return _next; }
 133 
 134   friend class JvmtiThreadState;
 135   void set_next(JvmtiEnvThreadState* link) { _next = link; }
 136 
 137 public:
 138   JvmtiEnvThreadState(JavaThread *thread, JvmtiEnvBase *env);
 139   ~JvmtiEnvThreadState();
 140 
 141   bool is_enabled(jvmtiEvent event_type) { return _event_enable.is_enabled(event_type); }
 142 
 143   JvmtiEnvThreadEventEnable *event_enable() { return &_event_enable; }
 144   void *get_agent_thread_local_storage_data() { return _agent_thread_local_storage_data; }
 145   void set_agent_thread_local_storage_data (void *data) { _agent_thread_local_storage_data = data; }
 146 
 147 
 148   // If the thread is in the given method at the given
 149   // location just return.  Otherwise, reset the current location
 150   // and reset _breakpoint_posted and _single_stepping_posted.
 151   // _breakpoint_posted and _single_stepping_posted are only cleared
 152   // here.
 153   void compare_and_set_current_location(Method* method, address location, jvmtiEvent event);
 154 
 155   void clear_current_location() { set_current_location((jmethodID)NULL, 0); }
 156 
 157   void reset_current_location(jvmtiEvent event, bool enabled);
 158 
 159   inline void set_breakpoint_posted()  { _breakpoint_posted = true; }
 160   inline void set_single_stepping_posted() {
 161     _single_stepping_posted = true;
 162   }
 163   inline bool breakpoint_posted() { return _breakpoint_posted; }
 164   inline bool single_stepping_posted() {
 165     return _single_stepping_posted;
 166   }
 167 
 168   inline JavaThread *get_thread() { return _thread; }
 169   inline JvmtiEnv *get_env() { return _env; }
 170 
 171   // lazily initialize _frame_pops
 172   JvmtiFramePops* get_frame_pops();
 173 
 174   bool has_frame_pops();
 175 
 176   // quickly test whether we should deliver a frame pop event on return from sp
 177   bool is_frame_pop(int cur_stack_depth);
 178 
 179   void set_frame_pop(int frame_number);
 180   void clear_frame_pop(int frame_number);
 181   void clear_to_frame_pop(int frame_number);
 182 
 183 };
 184 
 185 #endif // SHARE_VM_PRIMS_JVMTIENVTHREADSTATE_HPP