1 /* 2 * Copyright (c) 2005, 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 #ifndef SHARE_SERVICES_ATTACHLISTENER_HPP 26 #define SHARE_SERVICES_ATTACHLISTENER_HPP 27 28 #include "memory/allocation.hpp" 29 #include "metaprogramming/isRegisteredEnum.hpp" 30 #include "runtime/atomic.hpp" 31 #include "utilities/debug.hpp" 32 #include "utilities/globalDefinitions.hpp" 33 #include "utilities/macros.hpp" 34 #include "utilities/ostream.hpp" 35 36 // The AttachListener thread services a queue of operations that are enqueued 37 // by client tools. Each operation is identified by a name and has up to 3 38 // arguments. The operation name is mapped to a function which performs the 39 // operation. The function is called with an outputStream which is can use to 40 // write any result data (for examples the properties command serializes 41 // properties names and values to the output stream). When the function 42 // complets the result value and any result data is returned to the client 43 // tool. 44 45 class AttachOperation; 46 47 typedef jint (*AttachOperationFunction)(AttachOperation* op, outputStream* out); 48 49 struct AttachOperationFunctionInfo { 50 const char* name; 51 AttachOperationFunction func; 52 }; 53 54 enum AttachListenerState { 55 AL_NOT_INITIALIZED, 56 AL_INITIALIZING, 57 AL_INITIALIZED 58 }; 59 60 template<> struct IsRegisteredEnum<AttachListenerState> : public TrueType {}; 61 62 class AttachListener: AllStatic { 63 public: 64 static void vm_start() NOT_SERVICES_RETURN; 65 static void init() NOT_SERVICES_RETURN; 66 static void abort() NOT_SERVICES_RETURN; 67 68 // invoke to perform clean-up tasks when all clients detach 69 static void detachall() NOT_SERVICES_RETURN; 70 71 // indicates if the Attach Listener needs to be created at startup 72 static bool init_at_startup() NOT_SERVICES_RETURN_(false); 73 74 // indicates if we have a trigger to start the Attach Listener 75 static bool is_init_trigger() NOT_SERVICES_RETURN_(false); 76 77 #if !INCLUDE_SERVICES 78 static bool is_attach_supported() { return false; } 79 #else 80 81 private: 82 static volatile AttachListenerState _state; 83 84 public: 85 static void set_state(AttachListenerState new_state) { 86 Atomic::store(new_state, &_state); 87 } 88 89 static AttachListenerState get_state() { 90 return Atomic::load(&_state); 91 } 92 93 static AttachListenerState transit_state(AttachListenerState new_state, 94 AttachListenerState cmp_state) { 95 return Atomic::cmpxchg(new_state, &_state, cmp_state); 96 } 97 98 static bool is_initialized() { 99 return Atomic::load(&_state) == AL_INITIALIZED; 100 } 101 102 static void set_initialized() { 103 Atomic::store(AL_INITIALIZED, &_state); 104 } 105 106 // indicates if this VM supports attach-on-demand 107 static bool is_attach_supported() { return !DisableAttachMechanism; } 108 109 // platform specific initialization 110 static int pd_init(); 111 112 // platform specific operation 113 static AttachOperationFunctionInfo* pd_find_operation(const char* name); 114 115 // platform specific flag change 116 static jint pd_set_flag(AttachOperation* op, outputStream* out); 117 118 // platform specific detachall 119 static void pd_detachall(); 120 121 // platform specific data dump 122 static void pd_data_dump(); 123 124 // dequeue the next operation 125 static AttachOperation* dequeue(); 126 #endif // !INCLUDE_SERVICES 127 128 private: 129 static bool has_init_error(TRAPS); 130 }; 131 132 #if INCLUDE_SERVICES 133 class AttachOperation: public CHeapObj<mtInternal> { 134 public: 135 enum { 136 name_length_max = 16, // maximum length of name 137 arg_length_max = 1024, // maximum length of argument 138 arg_count_max = 3 // maximum number of arguments 139 }; 140 141 // name of special operation that can be enqueued when all 142 // clients detach 143 static char* detachall_operation_name() { return (char*)"detachall"; } 144 145 private: 146 char _name[name_length_max+1]; 147 char _arg[arg_count_max][arg_length_max+1]; 148 149 public: 150 const char* name() const { return _name; } 151 152 // set the operation name 153 void set_name(const char* name) { 154 assert(strlen(name) <= name_length_max, "exceeds maximum name length"); 155 size_t len = MIN2(strlen(name), (size_t)name_length_max); 156 memcpy(_name, name, len); 157 _name[len] = '\0'; 158 } 159 160 // get an argument value 161 const char* arg(int i) const { 162 assert(i>=0 && i<arg_count_max, "invalid argument index"); 163 return _arg[i]; 164 } 165 166 // set an argument value 167 void set_arg(int i, char* arg) { 168 assert(i>=0 && i<arg_count_max, "invalid argument index"); 169 if (arg == NULL) { 170 _arg[i][0] = '\0'; 171 } else { 172 assert(strlen(arg) <= arg_length_max, "exceeds maximum argument length"); 173 size_t len = MIN2(strlen(arg), (size_t)arg_length_max); 174 memcpy(_arg[i], arg, len); 175 _arg[i][len] = '\0'; 176 } 177 } 178 179 // create an operation of a given name 180 AttachOperation(const char* name) { 181 set_name(name); 182 for (int i=0; i<arg_count_max; i++) { 183 set_arg(i, NULL); 184 } 185 } 186 187 // complete operation by sending result code and any result data to the client 188 virtual void complete(jint result, bufferedStream* result_stream) = 0; 189 }; 190 #endif // INCLUDE_SERVICES 191 192 #endif // SHARE_SERVICES_ATTACHLISTENER_HPP