src/share/vm/prims/jvmtiImpl.hpp

Wed, 19 Nov 2014 19:31:13 -0800

author
iklam
date
Wed, 19 Nov 2014 19:31:13 -0800
changeset 7382
c80ddae00f51
parent 6140
77b028ba548c
child 6876
710a3c8b516e
permissions
-rw-r--r--

8065346: WB_AddToBootstrapClassLoaderSearch calls JvmtiEnv::create_a_jvmti when not in _thread_in_vm state
Summary: Removed ThreadToNativeFromVM and use java_lang_String::as_utf8_string instead
Reviewed-by: dholmes, minqi

     1 /*
     2  * Copyright (c) 1999, 2013, 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  */
    25 #ifndef SHARE_VM_PRIMS_JVMTIIMPL_HPP
    26 #define SHARE_VM_PRIMS_JVMTIIMPL_HPP
    28 #include "classfile/systemDictionary.hpp"
    29 #include "jvmtifiles/jvmti.h"
    30 #include "oops/objArrayOop.hpp"
    31 #include "prims/jvmtiEnvThreadState.hpp"
    32 #include "prims/jvmtiEventController.hpp"
    33 #include "prims/jvmtiTrace.hpp"
    34 #include "prims/jvmtiUtil.hpp"
    35 #include "runtime/stackValueCollection.hpp"
    36 #include "runtime/vm_operations.hpp"
    38 //
    39 // Forward Declarations
    40 //
    42 class JvmtiBreakpoint;
    43 class JvmtiBreakpoints;
    46 ///////////////////////////////////////////////////////////////
    47 //
    48 // class GrowableCache, GrowableElement
    49 // Used by              : JvmtiBreakpointCache
    50 // Used by JVMTI methods: none directly.
    51 //
    52 // GrowableCache is a permanent CHeap growable array of <GrowableElement *>
    53 //
    54 // In addition, the GrowableCache maintains a NULL terminated cache array of type address
    55 // that's created from the element array using the function:
    56 //     address GrowableElement::getCacheValue().
    57 //
    58 // Whenever the GrowableArray changes size, the cache array gets recomputed into a new C_HEAP allocated
    59 // block of memory. Additionally, every time the cache changes its position in memory, the
    60 //    void (*_listener_fun)(void *this_obj, address* cache)
    61 // gets called with the cache's new address. This gives the user of the GrowableCache a callback
    62 // to update its pointer to the address cache.
    63 //
    65 class GrowableElement : public CHeapObj<mtInternal> {
    66 public:
    67   virtual address getCacheValue()          =0;
    68   virtual bool equals(GrowableElement* e)  =0;
    69   virtual bool lessThan(GrowableElement *e)=0;
    70   virtual GrowableElement *clone()         =0;
    71   virtual void oops_do(OopClosure* f)      =0;
    72   virtual void metadata_do(void f(Metadata*)) =0;
    73 };
    75 class GrowableCache VALUE_OBJ_CLASS_SPEC {
    77 private:
    78   // Object pointer passed into cache & listener functions.
    79   void *_this_obj;
    81   // Array of elements in the collection
    82   GrowableArray<GrowableElement *> *_elements;
    84   // Parallel array of cached values
    85   address *_cache;
    87   // Listener for changes to the _cache field.
    88   // Called whenever the _cache field has it's value changed
    89   // (but NOT when cached elements are recomputed).
    90   void (*_listener_fun)(void *, address*);
    92   static bool equals(void *, GrowableElement *);
    94   // recache all elements after size change, notify listener
    95   void recache();
    97 public:
    98    GrowableCache();
    99    ~GrowableCache();
   101   void initialize(void *this_obj, void listener_fun(void *, address*) );
   103   // number of elements in the collection
   104   int length();
   105   // get the value of the index element in the collection
   106   GrowableElement* at(int index);
   107   // find the index of the element, -1 if it doesn't exist
   108   int find(GrowableElement* e);
   109   // append a copy of the element to the end of the collection, notify listener
   110   void append(GrowableElement* e);
   111   // insert a copy of the element using lessthan(), notify listener
   112   void insert(GrowableElement* e);
   113   // remove the element at index, notify listener
   114   void remove (int index);
   115   // clear out all elements and release all heap space, notify listener
   116   void clear();
   117   // apply f to every element and update the cache
   118   void oops_do(OopClosure* f);
   119   // walk metadata to preserve for RedefineClasses
   120   void metadata_do(void f(Metadata*));
   121   // update the cache after a full gc
   122   void gc_epilogue();
   123 };
   126 ///////////////////////////////////////////////////////////////
   127 //
   128 // class JvmtiBreakpointCache
   129 // Used by              : JvmtiBreakpoints
   130 // Used by JVMTI methods: none directly.
   131 // Note   : typesafe wrapper for GrowableCache of JvmtiBreakpoint
   132 //
   134 class JvmtiBreakpointCache : public CHeapObj<mtInternal> {
   136 private:
   137   GrowableCache _cache;
   139 public:
   140   JvmtiBreakpointCache()  {}
   141   ~JvmtiBreakpointCache() {}
   143   void initialize(void *this_obj, void listener_fun(void *, address*) ) {
   144     _cache.initialize(this_obj,listener_fun);
   145   }
   147   int length()                          { return _cache.length(); }
   148   JvmtiBreakpoint& at(int index)        { return (JvmtiBreakpoint&) *(_cache.at(index)); }
   149   int find(JvmtiBreakpoint& e)          { return _cache.find((GrowableElement *) &e); }
   150   void append(JvmtiBreakpoint& e)       { _cache.append((GrowableElement *) &e); }
   151   void remove (int index)               { _cache.remove(index); }
   152   void clear()                          { _cache.clear(); }
   153   void oops_do(OopClosure* f)           { _cache.oops_do(f); }
   154   void metadata_do(void f(Metadata*))   { _cache.metadata_do(f); }
   155   void gc_epilogue()                    { _cache.gc_epilogue(); }
   156 };
   159 ///////////////////////////////////////////////////////////////
   160 //
   161 // class JvmtiBreakpoint
   162 // Used by              : JvmtiBreakpoints
   163 // Used by JVMTI methods: SetBreakpoint, ClearBreakpoint, ClearAllBreakpoints
   164 // Note: Extends GrowableElement for use in a GrowableCache
   165 //
   166 // A JvmtiBreakpoint describes a location (class, method, bci) to break at.
   167 //
   169 typedef void (Method::*method_action)(int _bci);
   171 class JvmtiBreakpoint : public GrowableElement {
   172 private:
   173   Method*               _method;
   174   int                   _bci;
   175   Bytecodes::Code       _orig_bytecode;
   176   oop                   _class_holder;  // keeps _method memory from being deallocated
   178 public:
   179   JvmtiBreakpoint();
   180   JvmtiBreakpoint(Method* m_method, jlocation location);
   181   bool equals(JvmtiBreakpoint& bp);
   182   bool lessThan(JvmtiBreakpoint &bp);
   183   void copy(JvmtiBreakpoint& bp);
   184   bool is_valid();
   185   address getBcp();
   186   void each_method_version_do(method_action meth_act);
   187   void set();
   188   void clear();
   189   void print();
   191   Method* method() { return _method; }
   193   // GrowableElement implementation
   194   address getCacheValue()         { return getBcp(); }
   195   bool lessThan(GrowableElement* e) { Unimplemented(); return false; }
   196   bool equals(GrowableElement* e) { return equals((JvmtiBreakpoint&) *e); }
   197   void oops_do(OopClosure* f)     {
   198     // Mark the method loader as live so the Method* class loader doesn't get
   199     // unloaded and Method* memory reclaimed.
   200     f->do_oop(&_class_holder);
   201   }
   202   void metadata_do(void f(Metadata*)) {
   203     // walk metadata to preserve for RedefineClasses
   204     f(_method);
   205   }
   207   GrowableElement *clone()        {
   208     JvmtiBreakpoint *bp = new JvmtiBreakpoint();
   209     bp->copy(*this);
   210     return bp;
   211   }
   212 };
   215 ///////////////////////////////////////////////////////////////
   216 //
   217 // class JvmtiBreakpoints
   218 // Used by              : JvmtiCurrentBreakpoints
   219 // Used by JVMTI methods: none directly
   220 // Note: A Helper class
   221 //
   222 // JvmtiBreakpoints is a GrowableCache of JvmtiBreakpoint.
   223 // All changes to the GrowableCache occur at a safepoint using VM_ChangeBreakpoints.
   224 //
   225 // Because _bps is only modified at safepoints, its possible to always use the
   226 // cached byte code pointers from _bps without doing any synchronization (see JvmtiCurrentBreakpoints).
   227 //
   228 // It would be possible to make JvmtiBreakpoints a static class, but I've made it
   229 // CHeap allocated to emphasize its similarity to JvmtiFramePops.
   230 //
   232 class JvmtiBreakpoints : public CHeapObj<mtInternal> {
   233 private:
   235   JvmtiBreakpointCache _bps;
   237   // These should only be used by VM_ChangeBreakpoints
   238   // to insure they only occur at safepoints.
   239   // Todo: add checks for safepoint
   240   friend class VM_ChangeBreakpoints;
   241   void set_at_safepoint(JvmtiBreakpoint& bp);
   242   void clear_at_safepoint(JvmtiBreakpoint& bp);
   244   static void do_element(GrowableElement *e);
   246 public:
   247   JvmtiBreakpoints(void listener_fun(void *, address *));
   248   ~JvmtiBreakpoints();
   250   int length();
   251   void oops_do(OopClosure* f);
   252   void metadata_do(void f(Metadata*));
   253   void print();
   255   int  set(JvmtiBreakpoint& bp);
   256   int  clear(JvmtiBreakpoint& bp);
   257   void clearall_in_class_at_safepoint(Klass* klass);
   258   void gc_epilogue();
   259 };
   262 ///////////////////////////////////////////////////////////////
   263 //
   264 // class JvmtiCurrentBreakpoints
   265 //
   266 // A static wrapper class for the JvmtiBreakpoints that provides:
   267 // 1. a fast inlined function to check if a byte code pointer is a breakpoint (is_breakpoint).
   268 // 2. a function for lazily creating the JvmtiBreakpoints class (this is not strictly necessary,
   269 //    but I'm copying the code from JvmtiThreadState which needs to lazily initialize
   270 //    JvmtiFramePops).
   271 // 3. An oops_do entry point for GC'ing the breakpoint array.
   272 //
   274 class JvmtiCurrentBreakpoints : public AllStatic {
   276 private:
   278   // Current breakpoints, lazily initialized by get_jvmti_breakpoints();
   279   static JvmtiBreakpoints *_jvmti_breakpoints;
   281   // NULL terminated cache of byte-code pointers corresponding to current breakpoints.
   282   // Updated only at safepoints (with listener_fun) when the cache is moved.
   283   // It exists only to make is_breakpoint fast.
   284   static address          *_breakpoint_list;
   285   static inline void set_breakpoint_list(address *breakpoint_list) { _breakpoint_list = breakpoint_list; }
   286   static inline address *get_breakpoint_list()                     { return _breakpoint_list; }
   288   // Listener for the GrowableCache in _jvmti_breakpoints, updates _breakpoint_list.
   289   static void listener_fun(void *this_obj, address *cache);
   291 public:
   292   static void initialize();
   293   static void destroy();
   295   // lazily create _jvmti_breakpoints and _breakpoint_list
   296   static JvmtiBreakpoints& get_jvmti_breakpoints();
   298   // quickly test whether the bcp matches a cached breakpoint in the list
   299   static inline bool is_breakpoint(address bcp);
   301   static void oops_do(OopClosure* f);
   302   static void metadata_do(void f(Metadata*)) NOT_JVMTI_RETURN;
   303   static void gc_epilogue();
   304 };
   306 // quickly test whether the bcp matches a cached breakpoint in the list
   307 bool JvmtiCurrentBreakpoints::is_breakpoint(address bcp) {
   308     address *bps = get_breakpoint_list();
   309     if (bps == NULL) return false;
   310     for ( ; (*bps) != NULL; bps++) {
   311       if ((*bps) == bcp) return true;
   312     }
   313     return false;
   314 }
   317 ///////////////////////////////////////////////////////////////
   318 //
   319 // class VM_ChangeBreakpoints
   320 // Used by              : JvmtiBreakpoints
   321 // Used by JVMTI methods: none directly.
   322 // Note: A Helper class.
   323 //
   324 // VM_ChangeBreakpoints implements a VM_Operation for ALL modifications to the JvmtiBreakpoints class.
   325 //
   327 class VM_ChangeBreakpoints : public VM_Operation {
   328 private:
   329   JvmtiBreakpoints* _breakpoints;
   330   int               _operation;
   331   JvmtiBreakpoint*  _bp;
   333 public:
   334   enum { SET_BREAKPOINT=0, CLEAR_BREAKPOINT=1 };
   336   VM_ChangeBreakpoints(int operation, JvmtiBreakpoint *bp) {
   337     JvmtiBreakpoints& current_bps = JvmtiCurrentBreakpoints::get_jvmti_breakpoints();
   338     _breakpoints = &current_bps;
   339     _bp = bp;
   340     _operation = operation;
   341     assert(bp != NULL, "bp != NULL");
   342   }
   344   VMOp_Type type() const { return VMOp_ChangeBreakpoints; }
   345   void doit();
   346   void oops_do(OopClosure* f);
   347   void metadata_do(void f(Metadata*));
   348 };
   351 ///////////////////////////////////////////////////////////////
   352 // The get/set local operations must only be done by the VM thread
   353 // because the interpreter version needs to access oop maps, which can
   354 // only safely be done by the VM thread
   355 //
   356 // I'm told that in 1.5 oop maps are now protected by a lock and
   357 // we could get rid of the VM op
   358 // However if the VM op is removed then the target thread must
   359 // be suspended AND a lock will be needed to prevent concurrent
   360 // setting of locals to the same java thread. This lock is needed
   361 // to prevent compiledVFrames from trying to add deferred updates
   362 // to the thread simultaneously.
   363 //
   364 class VM_GetOrSetLocal : public VM_Operation {
   365  protected:
   366   JavaThread* _thread;
   367   JavaThread* _calling_thread;
   368   jint        _depth;
   369   jint        _index;
   370   BasicType   _type;
   371   jvalue      _value;
   372   javaVFrame* _jvf;
   373   bool        _set;
   375   // It is possible to get the receiver out of a non-static native wrapper
   376   // frame.  Use VM_GetReceiver to do this.
   377   virtual bool getting_receiver() const { return false; }
   379   jvmtiError  _result;
   381   vframe* get_vframe();
   382   javaVFrame* get_java_vframe();
   383   bool check_slot_type(javaVFrame* vf);
   385 public:
   386   // Constructor for non-object getter
   387   VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type);
   389   // Constructor for object or non-object setter
   390   VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, jvalue value);
   392   // Constructor for object getter
   393   VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth,
   394                    int index);
   396   VMOp_Type type() const { return VMOp_GetOrSetLocal; }
   397   jvalue value()         { return _value; }
   398   jvmtiError result()    { return _result; }
   400   bool doit_prologue();
   401   void doit();
   402   bool allow_nested_vm_operations() const;
   403   const char* name() const                       { return "get/set locals"; }
   405   // Check that the klass is assignable to a type with the given signature.
   406   static bool is_assignable(const char* ty_sign, Klass* klass, Thread* thread);
   407 };
   409 class VM_GetReceiver : public VM_GetOrSetLocal {
   410  protected:
   411   virtual bool getting_receiver() const { return true; }
   413  public:
   414   VM_GetReceiver(JavaThread* thread, JavaThread* calling_thread, jint depth);
   415   const char* name() const                       { return "get receiver"; }
   416 };
   419 ///////////////////////////////////////////////////////////////
   420 //
   421 // class JvmtiSuspendControl
   422 //
   423 // Convenience routines for suspending and resuming threads.
   424 //
   425 // All attempts by JVMTI to suspend and resume threads must go through the
   426 // JvmtiSuspendControl interface.
   427 //
   428 // methods return true if successful
   429 //
   430 class JvmtiSuspendControl : public AllStatic {
   431 public:
   432   // suspend the thread, taking it to a safepoint
   433   static bool suspend(JavaThread *java_thread);
   434   // resume the thread
   435   static bool resume(JavaThread *java_thread);
   437   static void print();
   438 };
   441 /**
   442  * When a thread (such as the compiler thread or VM thread) cannot post a
   443  * JVMTI event itself because the event needs to be posted from a Java
   444  * thread, then it can defer the event to the Service thread for posting.
   445  * The information needed to post the event is encapsulated into this class
   446  * and then enqueued onto the JvmtiDeferredEventQueue, where the Service
   447  * thread will pick it up and post it.
   448  *
   449  * This is currently only used for posting compiled-method-load and unload
   450  * events, which we don't want posted from the compiler thread.
   451  */
   452 class JvmtiDeferredEvent VALUE_OBJ_CLASS_SPEC {
   453   friend class JvmtiDeferredEventQueue;
   454  private:
   455   typedef enum {
   456     TYPE_NONE,
   457     TYPE_COMPILED_METHOD_LOAD,
   458     TYPE_COMPILED_METHOD_UNLOAD,
   459     TYPE_DYNAMIC_CODE_GENERATED
   460   } Type;
   462   Type _type;
   463   union {
   464     nmethod* compiled_method_load;
   465     struct {
   466       nmethod* nm;
   467       jmethodID method_id;
   468       const void* code_begin;
   469     } compiled_method_unload;
   470     struct {
   471       const char* name;
   472       const void* code_begin;
   473       const void* code_end;
   474     } dynamic_code_generated;
   475   } _event_data;
   477   JvmtiDeferredEvent(Type t) : _type(t) {}
   479  public:
   481   JvmtiDeferredEvent() : _type(TYPE_NONE) {}
   483   // Factory methods
   484   static JvmtiDeferredEvent compiled_method_load_event(nmethod* nm)
   485     NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
   486   static JvmtiDeferredEvent compiled_method_unload_event(nmethod* nm,
   487       jmethodID id, const void* code) NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
   488   static JvmtiDeferredEvent dynamic_code_generated_event(
   489       const char* name, const void* begin, const void* end)
   490           NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
   492   // Actually posts the event.
   493   void post() NOT_JVMTI_RETURN;
   494 };
   496 /**
   497  * Events enqueued on this queue wake up the Service thread which dequeues
   498  * and posts the events.  The Service_lock is required to be held
   499  * when operating on the queue (except for the "pending" events).
   500  */
   501 class JvmtiDeferredEventQueue : AllStatic {
   502   friend class JvmtiDeferredEvent;
   503  private:
   504   class QueueNode : public CHeapObj<mtInternal> {
   505    private:
   506     JvmtiDeferredEvent _event;
   507     QueueNode* _next;
   509    public:
   510     QueueNode(const JvmtiDeferredEvent& event)
   511       : _event(event), _next(NULL) {}
   513     const JvmtiDeferredEvent& event() const { return _event; }
   514     QueueNode* next() const { return _next; }
   516     void set_next(QueueNode* next) { _next = next; }
   517   };
   519   static QueueNode* _queue_head;             // Hold Service_lock to access
   520   static QueueNode* _queue_tail;             // Hold Service_lock to access
   521   static volatile QueueNode* _pending_list;  // Uses CAS for read/update
   523   // Transfers events from the _pending_list to the _queue.
   524   static void process_pending_events() NOT_JVMTI_RETURN;
   526  public:
   527   // Must be holding Service_lock when calling these
   528   static bool has_events() NOT_JVMTI_RETURN_(false);
   529   static void enqueue(const JvmtiDeferredEvent& event) NOT_JVMTI_RETURN;
   530   static JvmtiDeferredEvent dequeue() NOT_JVMTI_RETURN_(JvmtiDeferredEvent());
   532   // Used to enqueue events without using a lock, for times (such as during
   533   // safepoint) when we can't or don't want to lock the Service_lock.
   534   //
   535   // Events will be held off to the side until there's a call to
   536   // dequeue(), enqueue(), or process_pending_events() (all of which require
   537   // the holding of the Service_lock), and will be enqueued at that time.
   538   static void add_pending_event(const JvmtiDeferredEvent&) NOT_JVMTI_RETURN;
   539 };
   541 // Utility macro that checks for NULL pointers:
   542 #define NULL_CHECK(X, Y) if ((X) == NULL) { return (Y); }
   544 #endif // SHARE_VM_PRIMS_JVMTIIMPL_HPP

mercurial