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