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