src/share/vm/services/attachListener.cpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 1907
c18cbe5936b8
child 2497
3582bf76420e
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

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

mercurial