duke@435: /* zgu@4492: * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_PRIMS_JVMTIIMPL_HPP stefank@2314: #define SHARE_VM_PRIMS_JVMTIIMPL_HPP stefank@2314: stefank@2314: #include "classfile/systemDictionary.hpp" stefank@2314: #include "jvmtifiles/jvmti.h" stefank@2314: #include "oops/objArrayOop.hpp" stefank@2314: #include "prims/jvmtiEnvThreadState.hpp" stefank@2314: #include "prims/jvmtiEventController.hpp" stefank@2314: #include "prims/jvmtiTrace.hpp" stefank@2314: #include "prims/jvmtiUtil.hpp" stefank@2314: #include "runtime/stackValueCollection.hpp" stefank@2314: #include "runtime/vm_operations.hpp" stefank@2314: duke@435: // duke@435: // Forward Declarations duke@435: // duke@435: duke@435: class JvmtiBreakpoint; duke@435: class JvmtiBreakpoints; duke@435: duke@435: duke@435: /////////////////////////////////////////////////////////////// duke@435: // duke@435: // class GrowableCache, GrowableElement duke@435: // Used by : JvmtiBreakpointCache duke@435: // Used by JVMTI methods: none directly. duke@435: // duke@435: // GrowableCache is a permanent CHeap growable array of duke@435: // duke@435: // In addition, the GrowableCache maintains a NULL terminated cache array of type address duke@435: // that's created from the element array using the function: duke@435: // address GrowableElement::getCacheValue(). duke@435: // duke@435: // Whenever the GrowableArray changes size, the cache array gets recomputed into a new C_HEAP allocated duke@435: // block of memory. Additionally, every time the cache changes its position in memory, the duke@435: // void (*_listener_fun)(void *this_obj, address* cache) duke@435: // gets called with the cache's new address. This gives the user of the GrowableCache a callback duke@435: // to update its pointer to the address cache. duke@435: // duke@435: zgu@3900: class GrowableElement : public CHeapObj { duke@435: public: duke@435: virtual address getCacheValue() =0; duke@435: virtual bool equals(GrowableElement* e) =0; duke@435: virtual bool lessThan(GrowableElement *e)=0; duke@435: virtual GrowableElement *clone() =0; duke@435: virtual void oops_do(OopClosure* f) =0; coleenp@6063: virtual void metadata_do(void f(Metadata*)) =0; duke@435: }; duke@435: duke@435: class GrowableCache VALUE_OBJ_CLASS_SPEC { duke@435: duke@435: private: duke@435: // Object pointer passed into cache & listener functions. duke@435: void *_this_obj; duke@435: duke@435: // Array of elements in the collection duke@435: GrowableArray *_elements; duke@435: duke@435: // Parallel array of cached values duke@435: address *_cache; duke@435: duke@435: // Listener for changes to the _cache field. duke@435: // Called whenever the _cache field has it's value changed duke@435: // (but NOT when cached elements are recomputed). duke@435: void (*_listener_fun)(void *, address*); duke@435: duke@435: static bool equals(void *, GrowableElement *); duke@435: duke@435: // recache all elements after size change, notify listener duke@435: void recache(); duke@435: duke@435: public: duke@435: GrowableCache(); duke@435: ~GrowableCache(); duke@435: duke@435: void initialize(void *this_obj, void listener_fun(void *, address*) ); duke@435: duke@435: // number of elements in the collection duke@435: int length(); duke@435: // get the value of the index element in the collection duke@435: GrowableElement* at(int index); duke@435: // find the index of the element, -1 if it doesn't exist duke@435: int find(GrowableElement* e); duke@435: // append a copy of the element to the end of the collection, notify listener duke@435: void append(GrowableElement* e); duke@435: // insert a copy of the element using lessthan(), notify listener duke@435: void insert(GrowableElement* e); duke@435: // remove the element at index, notify listener duke@435: void remove (int index); duke@435: // clear out all elements and release all heap space, notify listener duke@435: void clear(); duke@435: // apply f to every element and update the cache duke@435: void oops_do(OopClosure* f); coleenp@6063: // walk metadata to preserve for RedefineClasses coleenp@6063: void metadata_do(void f(Metadata*)); kamg@2467: // update the cache after a full gc kamg@2467: void gc_epilogue(); duke@435: }; duke@435: duke@435: duke@435: /////////////////////////////////////////////////////////////// duke@435: // duke@435: // class JvmtiBreakpointCache duke@435: // Used by : JvmtiBreakpoints duke@435: // Used by JVMTI methods: none directly. duke@435: // Note : typesafe wrapper for GrowableCache of JvmtiBreakpoint duke@435: // duke@435: zgu@3900: class JvmtiBreakpointCache : public CHeapObj { duke@435: duke@435: private: duke@435: GrowableCache _cache; duke@435: duke@435: public: duke@435: JvmtiBreakpointCache() {} duke@435: ~JvmtiBreakpointCache() {} duke@435: duke@435: void initialize(void *this_obj, void listener_fun(void *, address*) ) { duke@435: _cache.initialize(this_obj,listener_fun); duke@435: } duke@435: duke@435: int length() { return _cache.length(); } duke@435: JvmtiBreakpoint& at(int index) { return (JvmtiBreakpoint&) *(_cache.at(index)); } duke@435: int find(JvmtiBreakpoint& e) { return _cache.find((GrowableElement *) &e); } duke@435: void append(JvmtiBreakpoint& e) { _cache.append((GrowableElement *) &e); } duke@435: void remove (int index) { _cache.remove(index); } duke@435: void clear() { _cache.clear(); } duke@435: void oops_do(OopClosure* f) { _cache.oops_do(f); } coleenp@6063: void metadata_do(void f(Metadata*)) { _cache.metadata_do(f); } kamg@2467: void gc_epilogue() { _cache.gc_epilogue(); } duke@435: }; duke@435: duke@435: duke@435: /////////////////////////////////////////////////////////////// duke@435: // duke@435: // class JvmtiBreakpoint duke@435: // Used by : JvmtiBreakpoints duke@435: // Used by JVMTI methods: SetBreakpoint, ClearBreakpoint, ClearAllBreakpoints duke@435: // Note: Extends GrowableElement for use in a GrowableCache duke@435: // duke@435: // A JvmtiBreakpoint describes a location (class, method, bci) to break at. duke@435: // duke@435: coleenp@4037: typedef void (Method::*method_action)(int _bci); duke@435: duke@435: class JvmtiBreakpoint : public GrowableElement { duke@435: private: coleenp@4037: Method* _method; duke@435: int _bci; duke@435: Bytecodes::Code _orig_bytecode; coleenp@6063: oop _class_holder; // keeps _method memory from being deallocated duke@435: duke@435: public: duke@435: JvmtiBreakpoint(); coleenp@4037: JvmtiBreakpoint(Method* m_method, jlocation location); duke@435: bool equals(JvmtiBreakpoint& bp); duke@435: bool lessThan(JvmtiBreakpoint &bp); duke@435: void copy(JvmtiBreakpoint& bp); duke@435: bool is_valid(); duke@435: address getBcp(); duke@435: void each_method_version_do(method_action meth_act); duke@435: void set(); duke@435: void clear(); duke@435: void print(); duke@435: coleenp@4037: Method* method() { return _method; } duke@435: duke@435: // GrowableElement implementation duke@435: address getCacheValue() { return getBcp(); } duke@435: bool lessThan(GrowableElement* e) { Unimplemented(); return false; } duke@435: bool equals(GrowableElement* e) { return equals((JvmtiBreakpoint&) *e); } coleenp@4037: void oops_do(OopClosure* f) { coleenp@6063: // Mark the method loader as live so the Method* class loader doesn't get coleenp@6063: // unloaded and Method* memory reclaimed. coleenp@6063: f->do_oop(&_class_holder); coleenp@4037: } coleenp@6063: void metadata_do(void f(Metadata*)) { coleenp@6063: // walk metadata to preserve for RedefineClasses coleenp@6063: f(_method); coleenp@6063: } coleenp@6063: duke@435: GrowableElement *clone() { duke@435: JvmtiBreakpoint *bp = new JvmtiBreakpoint(); duke@435: bp->copy(*this); duke@435: return bp; duke@435: } duke@435: }; duke@435: duke@435: duke@435: /////////////////////////////////////////////////////////////// duke@435: // duke@435: // class JvmtiBreakpoints duke@435: // Used by : JvmtiCurrentBreakpoints duke@435: // Used by JVMTI methods: none directly duke@435: // Note: A Helper class duke@435: // duke@435: // JvmtiBreakpoints is a GrowableCache of JvmtiBreakpoint. duke@435: // All changes to the GrowableCache occur at a safepoint using VM_ChangeBreakpoints. duke@435: // duke@435: // Because _bps is only modified at safepoints, its possible to always use the duke@435: // cached byte code pointers from _bps without doing any synchronization (see JvmtiCurrentBreakpoints). duke@435: // duke@435: // It would be possible to make JvmtiBreakpoints a static class, but I've made it duke@435: // CHeap allocated to emphasize its similarity to JvmtiFramePops. duke@435: // duke@435: zgu@3900: class JvmtiBreakpoints : public CHeapObj { duke@435: private: duke@435: duke@435: JvmtiBreakpointCache _bps; duke@435: duke@435: // These should only be used by VM_ChangeBreakpoints duke@435: // to insure they only occur at safepoints. duke@435: // Todo: add checks for safepoint duke@435: friend class VM_ChangeBreakpoints; duke@435: void set_at_safepoint(JvmtiBreakpoint& bp); duke@435: void clear_at_safepoint(JvmtiBreakpoint& bp); duke@435: duke@435: static void do_element(GrowableElement *e); duke@435: duke@435: public: duke@435: JvmtiBreakpoints(void listener_fun(void *, address *)); duke@435: ~JvmtiBreakpoints(); duke@435: duke@435: int length(); duke@435: void oops_do(OopClosure* f); coleenp@6063: void metadata_do(void f(Metadata*)); duke@435: void print(); duke@435: duke@435: int set(JvmtiBreakpoint& bp); duke@435: int clear(JvmtiBreakpoint& bp); coleenp@4037: void clearall_in_class_at_safepoint(Klass* klass); kamg@2467: void gc_epilogue(); duke@435: }; duke@435: duke@435: duke@435: /////////////////////////////////////////////////////////////// duke@435: // duke@435: // class JvmtiCurrentBreakpoints duke@435: // duke@435: // A static wrapper class for the JvmtiBreakpoints that provides: duke@435: // 1. a fast inlined function to check if a byte code pointer is a breakpoint (is_breakpoint). duke@435: // 2. a function for lazily creating the JvmtiBreakpoints class (this is not strictly necessary, duke@435: // but I'm copying the code from JvmtiThreadState which needs to lazily initialize duke@435: // JvmtiFramePops). duke@435: // 3. An oops_do entry point for GC'ing the breakpoint array. duke@435: // duke@435: duke@435: class JvmtiCurrentBreakpoints : public AllStatic { duke@435: duke@435: private: duke@435: duke@435: // Current breakpoints, lazily initialized by get_jvmti_breakpoints(); duke@435: static JvmtiBreakpoints *_jvmti_breakpoints; duke@435: duke@435: // NULL terminated cache of byte-code pointers corresponding to current breakpoints. duke@435: // Updated only at safepoints (with listener_fun) when the cache is moved. duke@435: // It exists only to make is_breakpoint fast. duke@435: static address *_breakpoint_list; duke@435: static inline void set_breakpoint_list(address *breakpoint_list) { _breakpoint_list = breakpoint_list; } duke@435: static inline address *get_breakpoint_list() { return _breakpoint_list; } duke@435: duke@435: // Listener for the GrowableCache in _jvmti_breakpoints, updates _breakpoint_list. duke@435: static void listener_fun(void *this_obj, address *cache); duke@435: duke@435: public: duke@435: static void initialize(); duke@435: static void destroy(); duke@435: duke@435: // lazily create _jvmti_breakpoints and _breakpoint_list duke@435: static JvmtiBreakpoints& get_jvmti_breakpoints(); duke@435: duke@435: // quickly test whether the bcp matches a cached breakpoint in the list duke@435: static inline bool is_breakpoint(address bcp); duke@435: duke@435: static void oops_do(OopClosure* f); jprovino@6140: static void metadata_do(void f(Metadata*)) NOT_JVMTI_RETURN; kamg@2467: static void gc_epilogue(); duke@435: }; duke@435: duke@435: // quickly test whether the bcp matches a cached breakpoint in the list duke@435: bool JvmtiCurrentBreakpoints::is_breakpoint(address bcp) { duke@435: address *bps = get_breakpoint_list(); duke@435: if (bps == NULL) return false; duke@435: for ( ; (*bps) != NULL; bps++) { duke@435: if ((*bps) == bcp) return true; duke@435: } duke@435: return false; duke@435: } duke@435: sla@5237: sla@5237: /////////////////////////////////////////////////////////////// sla@5237: // sla@5237: // class VM_ChangeBreakpoints sla@5237: // Used by : JvmtiBreakpoints sla@5237: // Used by JVMTI methods: none directly. sla@5237: // Note: A Helper class. sla@5237: // sla@5237: // VM_ChangeBreakpoints implements a VM_Operation for ALL modifications to the JvmtiBreakpoints class. sla@5237: // sla@5237: sla@5237: class VM_ChangeBreakpoints : public VM_Operation { sla@5237: private: sla@5237: JvmtiBreakpoints* _breakpoints; sla@5237: int _operation; sla@5237: JvmtiBreakpoint* _bp; sla@5237: sla@5237: public: sla@5237: enum { SET_BREAKPOINT=0, CLEAR_BREAKPOINT=1 }; sla@5237: sla@5237: VM_ChangeBreakpoints(int operation, JvmtiBreakpoint *bp) { sla@5237: JvmtiBreakpoints& current_bps = JvmtiCurrentBreakpoints::get_jvmti_breakpoints(); sla@5237: _breakpoints = ¤t_bps; sla@5237: _bp = bp; sla@5237: _operation = operation; sla@5237: assert(bp != NULL, "bp != NULL"); sla@5237: } sla@5237: sla@5237: VMOp_Type type() const { return VMOp_ChangeBreakpoints; } sla@5237: void doit(); sla@5237: void oops_do(OopClosure* f); coleenp@6063: void metadata_do(void f(Metadata*)); sla@5237: }; sla@5237: sla@5237: duke@435: /////////////////////////////////////////////////////////////// duke@435: // The get/set local operations must only be done by the VM thread duke@435: // because the interpreter version needs to access oop maps, which can duke@435: // only safely be done by the VM thread duke@435: // duke@435: // I'm told that in 1.5 oop maps are now protected by a lock and duke@435: // we could get rid of the VM op duke@435: // However if the VM op is removed then the target thread must duke@435: // be suspended AND a lock will be needed to prevent concurrent duke@435: // setting of locals to the same java thread. This lock is needed duke@435: // to prevent compiledVFrames from trying to add deferred updates duke@435: // to the thread simultaneously. duke@435: // duke@435: class VM_GetOrSetLocal : public VM_Operation { kamg@2361: protected: duke@435: JavaThread* _thread; duke@435: JavaThread* _calling_thread; duke@435: jint _depth; duke@435: jint _index; duke@435: BasicType _type; duke@435: jvalue _value; duke@435: javaVFrame* _jvf; duke@435: bool _set; duke@435: kamg@2361: // It is possible to get the receiver out of a non-static native wrapper kamg@2361: // frame. Use VM_GetReceiver to do this. kamg@2361: virtual bool getting_receiver() const { return false; } kamg@2361: duke@435: jvmtiError _result; duke@435: duke@435: vframe* get_vframe(); duke@435: javaVFrame* get_java_vframe(); duke@435: bool check_slot_type(javaVFrame* vf); duke@435: duke@435: public: duke@435: // Constructor for non-object getter duke@435: VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type); duke@435: duke@435: // Constructor for object or non-object setter duke@435: VM_GetOrSetLocal(JavaThread* thread, jint depth, jint index, BasicType type, jvalue value); duke@435: duke@435: // Constructor for object getter duke@435: VM_GetOrSetLocal(JavaThread* thread, JavaThread* calling_thread, jint depth, duke@435: int index); duke@435: duke@435: VMOp_Type type() const { return VMOp_GetOrSetLocal; } duke@435: jvalue value() { return _value; } duke@435: jvmtiError result() { return _result; } duke@435: duke@435: bool doit_prologue(); duke@435: void doit(); duke@435: bool allow_nested_vm_operations() const; duke@435: const char* name() const { return "get/set locals"; } duke@435: duke@435: // Check that the klass is assignable to a type with the given signature. duke@435: static bool is_assignable(const char* ty_sign, Klass* klass, Thread* thread); duke@435: }; duke@435: kamg@2361: class VM_GetReceiver : public VM_GetOrSetLocal { kamg@2361: protected: kamg@2361: virtual bool getting_receiver() const { return true; } kamg@2361: kamg@2361: public: kamg@2361: VM_GetReceiver(JavaThread* thread, JavaThread* calling_thread, jint depth); kamg@2361: const char* name() const { return "get receiver"; } kamg@2361: }; kamg@2361: duke@435: duke@435: /////////////////////////////////////////////////////////////// duke@435: // duke@435: // class JvmtiSuspendControl duke@435: // duke@435: // Convenience routines for suspending and resuming threads. duke@435: // duke@435: // All attempts by JVMTI to suspend and resume threads must go through the duke@435: // JvmtiSuspendControl interface. duke@435: // duke@435: // methods return true if successful duke@435: // duke@435: class JvmtiSuspendControl : public AllStatic { duke@435: public: duke@435: // suspend the thread, taking it to a safepoint duke@435: static bool suspend(JavaThread *java_thread); duke@435: // resume the thread duke@435: static bool resume(JavaThread *java_thread); duke@435: duke@435: static void print(); duke@435: }; duke@435: stefank@2325: kamg@2511: /** kamg@2511: * When a thread (such as the compiler thread or VM thread) cannot post a kamg@2511: * JVMTI event itself because the event needs to be posted from a Java kamg@2511: * thread, then it can defer the event to the Service thread for posting. kamg@2511: * The information needed to post the event is encapsulated into this class kamg@2511: * and then enqueued onto the JvmtiDeferredEventQueue, where the Service kamg@2511: * thread will pick it up and post it. kamg@2511: * kamg@2511: * This is currently only used for posting compiled-method-load and unload kamg@2511: * events, which we don't want posted from the compiler thread. kamg@2511: */ kamg@2511: class JvmtiDeferredEvent VALUE_OBJ_CLASS_SPEC { kamg@2511: friend class JvmtiDeferredEventQueue; kamg@2511: private: kamg@2511: typedef enum { kamg@2511: TYPE_NONE, kamg@2511: TYPE_COMPILED_METHOD_LOAD, kamg@2511: TYPE_COMPILED_METHOD_UNLOAD, kamg@2583: TYPE_DYNAMIC_CODE_GENERATED kamg@2511: } Type; kamg@2511: kamg@2511: Type _type; kamg@2511: union { kamg@2511: nmethod* compiled_method_load; kamg@2511: struct { dcubed@2624: nmethod* nm; kamg@2511: jmethodID method_id; kamg@2511: const void* code_begin; kamg@2511: } compiled_method_unload; kamg@2583: struct { kamg@2583: const char* name; kamg@2583: const void* code_begin; kamg@2583: const void* code_end; kamg@2583: } dynamic_code_generated; kamg@2511: } _event_data; kamg@2511: kamg@2511: JvmtiDeferredEvent(Type t) : _type(t) {} kamg@2511: kamg@2511: public: kamg@2511: kamg@2511: JvmtiDeferredEvent() : _type(TYPE_NONE) {} kamg@2511: kamg@2511: // Factory methods kamg@2511: static JvmtiDeferredEvent compiled_method_load_event(nmethod* nm) jprovino@4165: NOT_JVMTI_RETURN_(JvmtiDeferredEvent()); dcubed@2624: static JvmtiDeferredEvent compiled_method_unload_event(nmethod* nm, jprovino@4165: jmethodID id, const void* code) NOT_JVMTI_RETURN_(JvmtiDeferredEvent()); kamg@2583: static JvmtiDeferredEvent dynamic_code_generated_event( kamg@2583: const char* name, const void* begin, const void* end) jprovino@4165: NOT_JVMTI_RETURN_(JvmtiDeferredEvent()); kamg@2511: kamg@2511: // Actually posts the event. jprovino@4165: void post() NOT_JVMTI_RETURN; kamg@2511: }; kamg@2511: kamg@2511: /** kamg@2511: * Events enqueued on this queue wake up the Service thread which dequeues kamg@2511: * and posts the events. The Service_lock is required to be held kamg@2511: * when operating on the queue (except for the "pending" events). kamg@2511: */ kamg@2511: class JvmtiDeferredEventQueue : AllStatic { kamg@2511: friend class JvmtiDeferredEvent; kamg@2511: private: zgu@3900: class QueueNode : public CHeapObj { kamg@2511: private: kamg@2511: JvmtiDeferredEvent _event; kamg@2511: QueueNode* _next; kamg@2511: kamg@2511: public: kamg@2511: QueueNode(const JvmtiDeferredEvent& event) kamg@2511: : _event(event), _next(NULL) {} kamg@2511: kamg@2511: const JvmtiDeferredEvent& event() const { return _event; } kamg@2511: QueueNode* next() const { return _next; } kamg@2511: kamg@2511: void set_next(QueueNode* next) { _next = next; } kamg@2511: }; kamg@2511: kamg@2511: static QueueNode* _queue_head; // Hold Service_lock to access kamg@2511: static QueueNode* _queue_tail; // Hold Service_lock to access kamg@2511: static volatile QueueNode* _pending_list; // Uses CAS for read/update kamg@2511: kamg@2511: // Transfers events from the _pending_list to the _queue. jprovino@4165: static void process_pending_events() NOT_JVMTI_RETURN; kamg@2511: kamg@2511: public: kamg@2511: // Must be holding Service_lock when calling these jprovino@4165: static bool has_events() NOT_JVMTI_RETURN_(false); jprovino@4165: static void enqueue(const JvmtiDeferredEvent& event) NOT_JVMTI_RETURN; jprovino@4165: static JvmtiDeferredEvent dequeue() NOT_JVMTI_RETURN_(JvmtiDeferredEvent()); kamg@2511: kamg@2511: // Used to enqueue events without using a lock, for times (such as during kamg@2511: // safepoint) when we can't or don't want to lock the Service_lock. kamg@2511: // kamg@2511: // Events will be held off to the side until there's a call to kamg@2511: // dequeue(), enqueue(), or process_pending_events() (all of which require kamg@2511: // the holding of the Service_lock), and will be enqueued at that time. jprovino@4165: static void add_pending_event(const JvmtiDeferredEvent&) NOT_JVMTI_RETURN; kamg@2511: }; kamg@2511: duke@435: // Utility macro that checks for NULL pointers: duke@435: #define NULL_CHECK(X, Y) if ((X) == NULL) { return (Y); } stefank@2314: stefank@2314: #endif // SHARE_VM_PRIMS_JVMTIIMPL_HPP