src/share/vm/services/attachListener.cpp

Thu, 05 Jun 2008 15:57:56 -0700

author
ysr
date
Thu, 05 Jun 2008 15:57:56 -0700
changeset 777
37f87013dfd8
parent 435
a61af66fc99e
child 1050
c6c601a0f2d6
permissions
-rw-r--r--

6711316: Open source the Garbage-First garbage collector
Summary: First mercurial integration of the code for the Garbage-First garbage collector.
Reviewed-by: apetrusenko, iveresov, jmasa, sgoldman, tonyp, ysr

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

mercurial