Wed, 27 Apr 2016 01:25:04 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #include "precompiled.hpp" |
aoqi@0 | 26 | #include "classfile/javaClasses.hpp" |
aoqi@0 | 27 | #include "classfile/systemDictionary.hpp" |
aoqi@0 | 28 | #include "gc_implementation/shared/vmGCOperations.hpp" |
aoqi@0 | 29 | #include "memory/resourceArea.hpp" |
aoqi@0 | 30 | #include "prims/jvmtiExport.hpp" |
aoqi@0 | 31 | #include "runtime/arguments.hpp" |
aoqi@0 | 32 | #include "runtime/globals.hpp" |
aoqi@0 | 33 | #include "runtime/java.hpp" |
aoqi@0 | 34 | #include "runtime/javaCalls.hpp" |
aoqi@0 | 35 | #include "runtime/os.hpp" |
aoqi@0 | 36 | #include "services/attachListener.hpp" |
aoqi@0 | 37 | #include "services/diagnosticCommand.hpp" |
aoqi@0 | 38 | #include "services/heapDumper.hpp" |
aoqi@0 | 39 | |
aoqi@0 | 40 | volatile bool AttachListener::_initialized; |
aoqi@0 | 41 | |
aoqi@0 | 42 | // Implementation of "properties" command. |
aoqi@0 | 43 | // |
aoqi@0 | 44 | // Invokes sun.misc.VMSupport.serializePropertiesToByteArray to serialize |
aoqi@0 | 45 | // the system properties into a byte array. |
aoqi@0 | 46 | |
aoqi@0 | 47 | static Klass* load_and_initialize_klass(Symbol* sh, TRAPS) { |
aoqi@0 | 48 | Klass* k = SystemDictionary::resolve_or_fail(sh, true, CHECK_NULL); |
aoqi@0 | 49 | instanceKlassHandle ik (THREAD, k); |
aoqi@0 | 50 | if (ik->should_be_initialized()) { |
aoqi@0 | 51 | ik->initialize(CHECK_NULL); |
aoqi@0 | 52 | } |
aoqi@0 | 53 | return ik(); |
aoqi@0 | 54 | } |
aoqi@0 | 55 | |
aoqi@0 | 56 | static jint get_properties(AttachOperation* op, outputStream* out, Symbol* serializePropertiesMethod) { |
aoqi@0 | 57 | Thread* THREAD = Thread::current(); |
aoqi@0 | 58 | HandleMark hm; |
aoqi@0 | 59 | |
aoqi@0 | 60 | // load sun.misc.VMSupport |
aoqi@0 | 61 | Symbol* klass = vmSymbols::sun_misc_VMSupport(); |
aoqi@0 | 62 | Klass* k = load_and_initialize_klass(klass, THREAD); |
aoqi@0 | 63 | if (HAS_PENDING_EXCEPTION) { |
aoqi@0 | 64 | java_lang_Throwable::print(PENDING_EXCEPTION, out); |
aoqi@0 | 65 | CLEAR_PENDING_EXCEPTION; |
aoqi@0 | 66 | return JNI_ERR; |
aoqi@0 | 67 | } |
aoqi@0 | 68 | instanceKlassHandle ik(THREAD, k); |
aoqi@0 | 69 | |
aoqi@0 | 70 | // invoke the serializePropertiesToByteArray method |
aoqi@0 | 71 | JavaValue result(T_OBJECT); |
aoqi@0 | 72 | JavaCallArguments args; |
aoqi@0 | 73 | |
aoqi@0 | 74 | |
aoqi@0 | 75 | Symbol* signature = vmSymbols::serializePropertiesToByteArray_signature(); |
aoqi@0 | 76 | JavaCalls::call_static(&result, |
aoqi@0 | 77 | ik, |
aoqi@0 | 78 | serializePropertiesMethod, |
aoqi@0 | 79 | signature, |
aoqi@0 | 80 | &args, |
aoqi@0 | 81 | THREAD); |
aoqi@0 | 82 | if (HAS_PENDING_EXCEPTION) { |
aoqi@0 | 83 | java_lang_Throwable::print(PENDING_EXCEPTION, out); |
aoqi@0 | 84 | CLEAR_PENDING_EXCEPTION; |
aoqi@0 | 85 | return JNI_ERR; |
aoqi@0 | 86 | } |
aoqi@0 | 87 | |
aoqi@0 | 88 | // The result should be a [B |
aoqi@0 | 89 | oop res = (oop)result.get_jobject(); |
aoqi@0 | 90 | assert(res->is_typeArray(), "just checking"); |
aoqi@0 | 91 | assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking"); |
aoqi@0 | 92 | |
aoqi@0 | 93 | // copy the bytes to the output stream |
aoqi@0 | 94 | typeArrayOop ba = typeArrayOop(res); |
aoqi@0 | 95 | jbyte* addr = typeArrayOop(res)->byte_at_addr(0); |
aoqi@0 | 96 | out->print_raw((const char*)addr, ba->length()); |
aoqi@0 | 97 | |
aoqi@0 | 98 | return JNI_OK; |
aoqi@0 | 99 | } |
aoqi@0 | 100 | |
aoqi@0 | 101 | // Implementation of "properties" command. |
aoqi@0 | 102 | // See also: PrintSystemPropertiesDCmd class |
aoqi@0 | 103 | static jint get_system_properties(AttachOperation* op, outputStream* out) { |
aoqi@0 | 104 | return get_properties(op, out, vmSymbols::serializePropertiesToByteArray_name()); |
aoqi@0 | 105 | } |
aoqi@0 | 106 | |
aoqi@0 | 107 | // Implementation of "agent_properties" command. |
aoqi@0 | 108 | static jint get_agent_properties(AttachOperation* op, outputStream* out) { |
aoqi@0 | 109 | return get_properties(op, out, vmSymbols::serializeAgentPropertiesToByteArray_name()); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | // Implementation of "datadump" command. |
aoqi@0 | 113 | // |
aoqi@0 | 114 | // Raises a SIGBREAK signal so that VM dump threads, does deadlock detection, |
aoqi@0 | 115 | // etc. In theory this command should only post a DataDumpRequest to any |
aoqi@0 | 116 | // JVMTI environment that has enabled this event. However it's useful to |
aoqi@0 | 117 | // trigger the SIGBREAK handler. |
aoqi@0 | 118 | |
aoqi@0 | 119 | static jint data_dump(AttachOperation* op, outputStream* out) { |
aoqi@0 | 120 | if (!ReduceSignalUsage) { |
aoqi@0 | 121 | AttachListener::pd_data_dump(); |
aoqi@0 | 122 | } else { |
aoqi@0 | 123 | if (JvmtiExport::should_post_data_dump()) { |
aoqi@0 | 124 | JvmtiExport::post_data_dump(); |
aoqi@0 | 125 | } |
aoqi@0 | 126 | } |
aoqi@0 | 127 | return JNI_OK; |
aoqi@0 | 128 | } |
aoqi@0 | 129 | |
aoqi@0 | 130 | // Implementation of "threaddump" command - essentially a remote ctrl-break |
aoqi@0 | 131 | // See also: ThreadDumpDCmd class |
aoqi@0 | 132 | // |
aoqi@0 | 133 | static jint thread_dump(AttachOperation* op, outputStream* out) { |
aoqi@0 | 134 | bool print_concurrent_locks = false; |
aoqi@0 | 135 | if (op->arg(0) != NULL && strcmp(op->arg(0), "-l") == 0) { |
aoqi@0 | 136 | print_concurrent_locks = true; |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | // thread stacks |
aoqi@0 | 140 | VM_PrintThreads op1(out, print_concurrent_locks); |
aoqi@0 | 141 | VMThread::execute(&op1); |
aoqi@0 | 142 | |
aoqi@0 | 143 | // JNI global handles |
aoqi@0 | 144 | VM_PrintJNI op2(out); |
aoqi@0 | 145 | VMThread::execute(&op2); |
aoqi@0 | 146 | |
aoqi@0 | 147 | // Deadlock detection |
aoqi@0 | 148 | VM_FindDeadlocks op3(out); |
aoqi@0 | 149 | VMThread::execute(&op3); |
aoqi@0 | 150 | |
aoqi@0 | 151 | return JNI_OK; |
aoqi@0 | 152 | } |
aoqi@0 | 153 | |
aoqi@0 | 154 | // A jcmd attach operation request was received, which will now |
aoqi@0 | 155 | // dispatch to the diagnostic commands used for serviceability functions. |
aoqi@0 | 156 | static jint jcmd(AttachOperation* op, outputStream* out) { |
aoqi@0 | 157 | Thread* THREAD = Thread::current(); |
aoqi@0 | 158 | // All the supplied jcmd arguments are stored as a single |
aoqi@0 | 159 | // string (op->arg(0)). This is parsed by the Dcmd framework. |
aoqi@0 | 160 | DCmd::parse_and_execute(DCmd_Source_AttachAPI, out, op->arg(0), ' ', THREAD); |
aoqi@0 | 161 | if (HAS_PENDING_EXCEPTION) { |
aoqi@0 | 162 | java_lang_Throwable::print(PENDING_EXCEPTION, out); |
aoqi@0 | 163 | out->cr(); |
aoqi@0 | 164 | CLEAR_PENDING_EXCEPTION; |
aoqi@0 | 165 | // The exception has been printed on the output stream |
aoqi@0 | 166 | // If the JVM returns JNI_ERR, the attachAPI throws a generic I/O |
aoqi@0 | 167 | // exception and the content of the output stream is not processed. |
aoqi@0 | 168 | // By returning JNI_OK, the exception will be displayed on the client side |
aoqi@0 | 169 | } |
aoqi@0 | 170 | return JNI_OK; |
aoqi@0 | 171 | } |
aoqi@0 | 172 | |
aoqi@0 | 173 | // Implementation of "dumpheap" command. |
aoqi@0 | 174 | // See also: HeapDumpDCmd class |
aoqi@0 | 175 | // |
aoqi@0 | 176 | // Input arguments :- |
aoqi@0 | 177 | // arg0: Name of the dump file |
aoqi@0 | 178 | // arg1: "-live" or "-all" |
aoqi@0 | 179 | jint dump_heap(AttachOperation* op, outputStream* out) { |
aoqi@0 | 180 | const char* path = op->arg(0); |
aoqi@0 | 181 | if (path == NULL || path[0] == '\0') { |
aoqi@0 | 182 | out->print_cr("No dump file specified"); |
aoqi@0 | 183 | } else { |
aoqi@0 | 184 | bool live_objects_only = true; // default is true to retain the behavior before this change is made |
aoqi@0 | 185 | const char* arg1 = op->arg(1); |
aoqi@0 | 186 | if (arg1 != NULL && (strlen(arg1) > 0)) { |
aoqi@0 | 187 | if (strcmp(arg1, "-all") != 0 && strcmp(arg1, "-live") != 0) { |
aoqi@0 | 188 | out->print_cr("Invalid argument to dumpheap operation: %s", arg1); |
aoqi@0 | 189 | return JNI_ERR; |
aoqi@0 | 190 | } |
aoqi@0 | 191 | live_objects_only = strcmp(arg1, "-live") == 0; |
aoqi@0 | 192 | } |
aoqi@0 | 193 | |
aoqi@0 | 194 | // Request a full GC before heap dump if live_objects_only = true |
aoqi@0 | 195 | // This helps reduces the amount of unreachable objects in the dump |
aoqi@0 | 196 | // and makes it easier to browse. |
aoqi@0 | 197 | HeapDumper dumper(live_objects_only /* request GC */); |
aoqi@0 | 198 | int res = dumper.dump(op->arg(0)); |
aoqi@0 | 199 | if (res == 0) { |
aoqi@0 | 200 | out->print_cr("Heap dump file created"); |
aoqi@0 | 201 | } else { |
aoqi@0 | 202 | // heap dump failed |
aoqi@0 | 203 | ResourceMark rm; |
aoqi@0 | 204 | char* error = dumper.error_as_C_string(); |
aoqi@0 | 205 | if (error == NULL) { |
aoqi@0 | 206 | out->print_cr("Dump failed - reason unknown"); |
aoqi@0 | 207 | } else { |
aoqi@0 | 208 | out->print_cr("%s", error); |
aoqi@0 | 209 | } |
aoqi@0 | 210 | } |
aoqi@0 | 211 | } |
aoqi@0 | 212 | return JNI_OK; |
aoqi@0 | 213 | } |
aoqi@0 | 214 | |
aoqi@0 | 215 | // Implementation of "inspectheap" command |
aoqi@0 | 216 | // See also: ClassHistogramDCmd class |
aoqi@0 | 217 | // |
aoqi@0 | 218 | // Input arguments :- |
aoqi@0 | 219 | // arg0: "-live" or "-all" |
aoqi@0 | 220 | static jint heap_inspection(AttachOperation* op, outputStream* out) { |
aoqi@0 | 221 | bool live_objects_only = true; // default is true to retain the behavior before this change is made |
aoqi@0 | 222 | const char* arg0 = op->arg(0); |
aoqi@0 | 223 | if (arg0 != NULL && (strlen(arg0) > 0)) { |
aoqi@0 | 224 | if (strcmp(arg0, "-all") != 0 && strcmp(arg0, "-live") != 0) { |
aoqi@0 | 225 | out->print_cr("Invalid argument to inspectheap operation: %s", arg0); |
aoqi@0 | 226 | return JNI_ERR; |
aoqi@0 | 227 | } |
aoqi@0 | 228 | live_objects_only = strcmp(arg0, "-live") == 0; |
aoqi@0 | 229 | } |
aoqi@0 | 230 | VM_GC_HeapInspection heapop(out, live_objects_only /* request full gc */); |
aoqi@0 | 231 | VMThread::execute(&heapop); |
aoqi@0 | 232 | return JNI_OK; |
aoqi@0 | 233 | } |
aoqi@0 | 234 | |
aoqi@0 | 235 | // set a boolean global flag using value from AttachOperation |
aoqi@0 | 236 | static jint set_bool_flag(const char* name, AttachOperation* op, outputStream* out) { |
aoqi@0 | 237 | bool value = true; |
aoqi@0 | 238 | const char* arg1; |
aoqi@0 | 239 | if ((arg1 = op->arg(1)) != NULL) { |
aoqi@0 | 240 | int tmp; |
aoqi@0 | 241 | int n = sscanf(arg1, "%d", &tmp); |
aoqi@0 | 242 | if (n != 1) { |
aoqi@0 | 243 | out->print_cr("flag value must be a boolean (1 or 0)"); |
aoqi@0 | 244 | return JNI_ERR; |
aoqi@0 | 245 | } |
aoqi@0 | 246 | value = (tmp != 0); |
aoqi@0 | 247 | } |
aoqi@0 | 248 | bool res = CommandLineFlags::boolAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND); |
aoqi@0 | 249 | if (! res) { |
aoqi@0 | 250 | out->print_cr("setting flag %s failed", name); |
aoqi@0 | 251 | } |
aoqi@0 | 252 | return res? JNI_OK : JNI_ERR; |
aoqi@0 | 253 | } |
aoqi@0 | 254 | |
aoqi@0 | 255 | // set a intx global flag using value from AttachOperation |
aoqi@0 | 256 | static jint set_intx_flag(const char* name, AttachOperation* op, outputStream* out) { |
aoqi@0 | 257 | intx value; |
aoqi@0 | 258 | const char* arg1; |
aoqi@0 | 259 | if ((arg1 = op->arg(1)) != NULL) { |
aoqi@0 | 260 | int n = sscanf(arg1, INTX_FORMAT, &value); |
aoqi@0 | 261 | if (n != 1) { |
aoqi@0 | 262 | out->print_cr("flag value must be an integer"); |
aoqi@0 | 263 | return JNI_ERR; |
aoqi@0 | 264 | } |
aoqi@0 | 265 | } |
aoqi@0 | 266 | bool res = CommandLineFlags::intxAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND); |
aoqi@0 | 267 | if (! res) { |
aoqi@0 | 268 | out->print_cr("setting flag %s failed", name); |
aoqi@0 | 269 | } |
aoqi@0 | 270 | |
aoqi@0 | 271 | return res? JNI_OK : JNI_ERR; |
aoqi@0 | 272 | } |
aoqi@0 | 273 | |
aoqi@0 | 274 | // set a uintx global flag using value from AttachOperation |
aoqi@0 | 275 | static jint set_uintx_flag(const char* name, AttachOperation* op, outputStream* out) { |
aoqi@0 | 276 | uintx value; |
aoqi@0 | 277 | const char* arg1; |
aoqi@0 | 278 | if ((arg1 = op->arg(1)) != NULL) { |
aoqi@0 | 279 | int n = sscanf(arg1, UINTX_FORMAT, &value); |
aoqi@0 | 280 | if (n != 1) { |
aoqi@0 | 281 | out->print_cr("flag value must be an unsigned integer"); |
aoqi@0 | 282 | return JNI_ERR; |
aoqi@0 | 283 | } |
aoqi@0 | 284 | } |
aoqi@0 | 285 | |
aoqi@0 | 286 | if (strncmp(name, "MaxHeapFreeRatio", 17) == 0) { |
aoqi@0 | 287 | FormatBuffer<80> err_msg("%s", ""); |
aoqi@0 | 288 | if (!Arguments::verify_MaxHeapFreeRatio(err_msg, value)) { |
aoqi@0 | 289 | out->print_cr("%s", err_msg.buffer()); |
aoqi@0 | 290 | return JNI_ERR; |
aoqi@0 | 291 | } |
aoqi@0 | 292 | } else if (strncmp(name, "MinHeapFreeRatio", 17) == 0) { |
aoqi@0 | 293 | FormatBuffer<80> err_msg("%s", ""); |
aoqi@0 | 294 | if (!Arguments::verify_MinHeapFreeRatio(err_msg, value)) { |
aoqi@0 | 295 | out->print_cr("%s", err_msg.buffer()); |
aoqi@0 | 296 | return JNI_ERR; |
aoqi@0 | 297 | } |
aoqi@0 | 298 | } |
aoqi@0 | 299 | bool res = CommandLineFlags::uintxAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND); |
aoqi@0 | 300 | if (! res) { |
aoqi@0 | 301 | out->print_cr("setting flag %s failed", name); |
aoqi@0 | 302 | } |
aoqi@0 | 303 | |
aoqi@0 | 304 | return res? JNI_OK : JNI_ERR; |
aoqi@0 | 305 | } |
aoqi@0 | 306 | |
aoqi@0 | 307 | // set a uint64_t global flag using value from AttachOperation |
aoqi@0 | 308 | static jint set_uint64_t_flag(const char* name, AttachOperation* op, outputStream* out) { |
aoqi@0 | 309 | uint64_t value; |
aoqi@0 | 310 | const char* arg1; |
aoqi@0 | 311 | if ((arg1 = op->arg(1)) != NULL) { |
aoqi@0 | 312 | int n = sscanf(arg1, UINT64_FORMAT, &value); |
aoqi@0 | 313 | if (n != 1) { |
aoqi@0 | 314 | out->print_cr("flag value must be an unsigned 64-bit integer"); |
aoqi@0 | 315 | return JNI_ERR; |
aoqi@0 | 316 | } |
aoqi@0 | 317 | } |
aoqi@0 | 318 | bool res = CommandLineFlags::uint64_tAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND); |
aoqi@0 | 319 | if (! res) { |
aoqi@0 | 320 | out->print_cr("setting flag %s failed", name); |
aoqi@0 | 321 | } |
aoqi@0 | 322 | |
aoqi@0 | 323 | return res? JNI_OK : JNI_ERR; |
aoqi@0 | 324 | } |
aoqi@0 | 325 | |
aoqi@0 | 326 | // set a string global flag using value from AttachOperation |
aoqi@0 | 327 | static jint set_ccstr_flag(const char* name, AttachOperation* op, outputStream* out) { |
aoqi@0 | 328 | const char* value; |
aoqi@0 | 329 | if ((value = op->arg(1)) == NULL) { |
aoqi@0 | 330 | out->print_cr("flag value must be a string"); |
aoqi@0 | 331 | return JNI_ERR; |
aoqi@0 | 332 | } |
aoqi@0 | 333 | bool res = CommandLineFlags::ccstrAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND); |
aoqi@0 | 334 | if (res) { |
aoqi@0 | 335 | FREE_C_HEAP_ARRAY(char, value, mtInternal); |
aoqi@0 | 336 | } else { |
aoqi@0 | 337 | out->print_cr("setting flag %s failed", name); |
aoqi@0 | 338 | } |
aoqi@0 | 339 | |
aoqi@0 | 340 | return res? JNI_OK : JNI_ERR; |
aoqi@0 | 341 | } |
aoqi@0 | 342 | |
aoqi@0 | 343 | // Implementation of "setflag" command |
aoqi@0 | 344 | static jint set_flag(AttachOperation* op, outputStream* out) { |
aoqi@0 | 345 | |
aoqi@0 | 346 | const char* name = NULL; |
aoqi@0 | 347 | if ((name = op->arg(0)) == NULL) { |
aoqi@0 | 348 | out->print_cr("flag name is missing"); |
aoqi@0 | 349 | return JNI_ERR; |
aoqi@0 | 350 | } |
aoqi@0 | 351 | |
aoqi@0 | 352 | Flag* f = Flag::find_flag((char*)name, strlen(name)); |
aoqi@0 | 353 | if (f && f->is_external() && f->is_writeable()) { |
aoqi@0 | 354 | if (f->is_bool()) { |
aoqi@0 | 355 | return set_bool_flag(name, op, out); |
aoqi@0 | 356 | } else if (f->is_intx()) { |
aoqi@0 | 357 | return set_intx_flag(name, op, out); |
aoqi@0 | 358 | } else if (f->is_uintx()) { |
aoqi@0 | 359 | return set_uintx_flag(name, op, out); |
aoqi@0 | 360 | } else if (f->is_uint64_t()) { |
aoqi@0 | 361 | return set_uint64_t_flag(name, op, out); |
aoqi@0 | 362 | } else if (f->is_ccstr()) { |
aoqi@0 | 363 | return set_ccstr_flag(name, op, out); |
aoqi@0 | 364 | } else { |
aoqi@0 | 365 | ShouldNotReachHere(); |
aoqi@0 | 366 | return JNI_ERR; |
aoqi@0 | 367 | } |
aoqi@0 | 368 | } else { |
aoqi@0 | 369 | return AttachListener::pd_set_flag(op, out); |
aoqi@0 | 370 | } |
aoqi@0 | 371 | } |
aoqi@0 | 372 | |
aoqi@0 | 373 | // Implementation of "printflag" command |
aoqi@0 | 374 | // See also: PrintVMFlagsDCmd class |
aoqi@0 | 375 | static jint print_flag(AttachOperation* op, outputStream* out) { |
aoqi@0 | 376 | const char* name = NULL; |
aoqi@0 | 377 | if ((name = op->arg(0)) == NULL) { |
aoqi@0 | 378 | out->print_cr("flag name is missing"); |
aoqi@0 | 379 | return JNI_ERR; |
aoqi@0 | 380 | } |
aoqi@0 | 381 | Flag* f = Flag::find_flag((char*)name, strlen(name)); |
aoqi@0 | 382 | if (f) { |
aoqi@0 | 383 | f->print_as_flag(out); |
aoqi@0 | 384 | out->cr(); |
aoqi@0 | 385 | } else { |
aoqi@0 | 386 | out->print_cr("no such flag '%s'", name); |
aoqi@0 | 387 | } |
aoqi@0 | 388 | return JNI_OK; |
aoqi@0 | 389 | } |
aoqi@0 | 390 | |
aoqi@0 | 391 | // Table to map operation names to functions. |
aoqi@0 | 392 | |
aoqi@0 | 393 | // names must be of length <= AttachOperation::name_length_max |
aoqi@0 | 394 | static AttachOperationFunctionInfo funcs[] = { |
aoqi@0 | 395 | { "agentProperties", get_agent_properties }, |
aoqi@0 | 396 | { "datadump", data_dump }, |
aoqi@0 | 397 | { "dumpheap", dump_heap }, |
aoqi@0 | 398 | { "load", JvmtiExport::load_agent_library }, |
aoqi@0 | 399 | { "properties", get_system_properties }, |
aoqi@0 | 400 | { "threaddump", thread_dump }, |
aoqi@0 | 401 | { "inspectheap", heap_inspection }, |
aoqi@0 | 402 | { "setflag", set_flag }, |
aoqi@0 | 403 | { "printflag", print_flag }, |
aoqi@0 | 404 | { "jcmd", jcmd }, |
aoqi@0 | 405 | { NULL, NULL } |
aoqi@0 | 406 | }; |
aoqi@0 | 407 | |
aoqi@0 | 408 | |
aoqi@0 | 409 | |
aoqi@0 | 410 | // The Attach Listener threads services a queue. It dequeues an operation |
aoqi@0 | 411 | // from the queue, examines the operation name (command), and dispatches |
aoqi@0 | 412 | // to the corresponding function to perform the operation. |
aoqi@0 | 413 | |
aoqi@0 | 414 | static void attach_listener_thread_entry(JavaThread* thread, TRAPS) { |
aoqi@0 | 415 | os::set_priority(thread, NearMaxPriority); |
aoqi@0 | 416 | |
aoqi@0 | 417 | thread->record_stack_base_and_size(); |
aoqi@0 | 418 | |
aoqi@0 | 419 | if (AttachListener::pd_init() != 0) { |
aoqi@0 | 420 | return; |
aoqi@0 | 421 | } |
aoqi@0 | 422 | AttachListener::set_initialized(); |
aoqi@0 | 423 | |
aoqi@0 | 424 | for (;;) { |
aoqi@0 | 425 | AttachOperation* op = AttachListener::dequeue(); |
aoqi@0 | 426 | if (op == NULL) { |
aoqi@0 | 427 | return; // dequeue failed or shutdown |
aoqi@0 | 428 | } |
aoqi@0 | 429 | |
aoqi@0 | 430 | ResourceMark rm; |
aoqi@0 | 431 | bufferedStream st; |
aoqi@0 | 432 | jint res = JNI_OK; |
aoqi@0 | 433 | |
aoqi@0 | 434 | // handle special detachall operation |
aoqi@0 | 435 | if (strcmp(op->name(), AttachOperation::detachall_operation_name()) == 0) { |
aoqi@0 | 436 | AttachListener::detachall(); |
aoqi@0 | 437 | } else { |
aoqi@0 | 438 | // find the function to dispatch too |
aoqi@0 | 439 | AttachOperationFunctionInfo* info = NULL; |
aoqi@0 | 440 | for (int i=0; funcs[i].name != NULL; i++) { |
aoqi@0 | 441 | const char* name = funcs[i].name; |
aoqi@0 | 442 | assert(strlen(name) <= AttachOperation::name_length_max, "operation <= name_length_max"); |
aoqi@0 | 443 | if (strcmp(op->name(), name) == 0) { |
aoqi@0 | 444 | info = &(funcs[i]); |
aoqi@0 | 445 | break; |
aoqi@0 | 446 | } |
aoqi@0 | 447 | } |
aoqi@0 | 448 | |
aoqi@0 | 449 | // check for platform dependent attach operation |
aoqi@0 | 450 | if (info == NULL) { |
aoqi@0 | 451 | info = AttachListener::pd_find_operation(op->name()); |
aoqi@0 | 452 | } |
aoqi@0 | 453 | |
aoqi@0 | 454 | if (info != NULL) { |
aoqi@0 | 455 | // dispatch to the function that implements this operation |
aoqi@0 | 456 | res = (info->func)(op, &st); |
aoqi@0 | 457 | } else { |
aoqi@0 | 458 | st.print("Operation %s not recognized!", op->name()); |
aoqi@0 | 459 | res = JNI_ERR; |
aoqi@0 | 460 | } |
aoqi@0 | 461 | } |
aoqi@0 | 462 | |
aoqi@0 | 463 | // operation complete - send result and output to client |
aoqi@0 | 464 | op->complete(res, &st); |
aoqi@0 | 465 | } |
aoqi@0 | 466 | } |
aoqi@0 | 467 | |
aoqi@0 | 468 | // Starts the Attach Listener thread |
aoqi@0 | 469 | void AttachListener::init() { |
aoqi@0 | 470 | EXCEPTION_MARK; |
aoqi@0 | 471 | Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK); |
aoqi@0 | 472 | instanceKlassHandle klass (THREAD, k); |
aoqi@0 | 473 | instanceHandle thread_oop = klass->allocate_instance_handle(CHECK); |
aoqi@0 | 474 | |
aoqi@0 | 475 | const char thread_name[] = "Attach Listener"; |
aoqi@0 | 476 | Handle string = java_lang_String::create_from_str(thread_name, CHECK); |
aoqi@0 | 477 | |
aoqi@0 | 478 | // Initialize thread_oop to put it into the system threadGroup |
aoqi@0 | 479 | Handle thread_group (THREAD, Universe::system_thread_group()); |
aoqi@0 | 480 | JavaValue result(T_VOID); |
aoqi@0 | 481 | JavaCalls::call_special(&result, thread_oop, |
aoqi@0 | 482 | klass, |
aoqi@0 | 483 | vmSymbols::object_initializer_name(), |
aoqi@0 | 484 | vmSymbols::threadgroup_string_void_signature(), |
aoqi@0 | 485 | thread_group, |
aoqi@0 | 486 | string, |
aoqi@0 | 487 | THREAD); |
aoqi@0 | 488 | |
aoqi@0 | 489 | if (HAS_PENDING_EXCEPTION) { |
aoqi@0 | 490 | tty->print_cr("Exception in VM (AttachListener::init) : "); |
aoqi@0 | 491 | java_lang_Throwable::print(PENDING_EXCEPTION, tty); |
aoqi@0 | 492 | tty->cr(); |
aoqi@0 | 493 | |
aoqi@0 | 494 | CLEAR_PENDING_EXCEPTION; |
aoqi@0 | 495 | |
aoqi@0 | 496 | return; |
aoqi@0 | 497 | } |
aoqi@0 | 498 | |
aoqi@0 | 499 | KlassHandle group(THREAD, SystemDictionary::ThreadGroup_klass()); |
aoqi@0 | 500 | JavaCalls::call_special(&result, |
aoqi@0 | 501 | thread_group, |
aoqi@0 | 502 | group, |
aoqi@0 | 503 | vmSymbols::add_method_name(), |
aoqi@0 | 504 | vmSymbols::thread_void_signature(), |
aoqi@0 | 505 | thread_oop, // ARG 1 |
aoqi@0 | 506 | THREAD); |
aoqi@0 | 507 | |
aoqi@0 | 508 | if (HAS_PENDING_EXCEPTION) { |
aoqi@0 | 509 | tty->print_cr("Exception in VM (AttachListener::init) : "); |
aoqi@0 | 510 | java_lang_Throwable::print(PENDING_EXCEPTION, tty); |
aoqi@0 | 511 | tty->cr(); |
aoqi@0 | 512 | |
aoqi@0 | 513 | CLEAR_PENDING_EXCEPTION; |
aoqi@0 | 514 | |
aoqi@0 | 515 | return; |
aoqi@0 | 516 | } |
aoqi@0 | 517 | |
aoqi@0 | 518 | { MutexLocker mu(Threads_lock); |
aoqi@0 | 519 | JavaThread* listener_thread = new JavaThread(&attach_listener_thread_entry); |
aoqi@0 | 520 | |
aoqi@0 | 521 | // Check that thread and osthread were created |
aoqi@0 | 522 | if (listener_thread == NULL || listener_thread->osthread() == NULL) { |
aoqi@0 | 523 | vm_exit_during_initialization("java.lang.OutOfMemoryError", |
aoqi@0 | 524 | "unable to create new native thread"); |
aoqi@0 | 525 | } |
aoqi@0 | 526 | |
aoqi@0 | 527 | java_lang_Thread::set_thread(thread_oop(), listener_thread); |
aoqi@0 | 528 | java_lang_Thread::set_daemon(thread_oop()); |
aoqi@0 | 529 | |
aoqi@0 | 530 | listener_thread->set_threadObj(thread_oop()); |
aoqi@0 | 531 | Threads::add(listener_thread); |
aoqi@0 | 532 | Thread::start(listener_thread); |
aoqi@0 | 533 | } |
aoqi@0 | 534 | } |
aoqi@0 | 535 | |
aoqi@0 | 536 | // Performs clean-up tasks on platforms where we can detect that the last |
aoqi@0 | 537 | // client has detached |
aoqi@0 | 538 | void AttachListener::detachall() { |
aoqi@0 | 539 | // call the platform dependent clean-up |
aoqi@0 | 540 | pd_detachall(); |
aoqi@0 | 541 | } |