src/share/vm/services/attachListener.cpp

Wed, 03 Jul 2019 20:42:37 +0800

author
aoqi
date
Wed, 03 Jul 2019 20:42:37 +0800
changeset 9637
eef07cd490d4
parent 9448
73d689add964
permissions
-rw-r--r--

Merge

aoqi@0 1 /*
dsamersoff@8658 2 * Copyright (c) 2005, 2016, 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;
sla@7128 165 return JNI_ERR;
aoqi@0 166 }
aoqi@0 167 return JNI_OK;
aoqi@0 168 }
aoqi@0 169
aoqi@0 170 // Implementation of "dumpheap" command.
aoqi@0 171 // See also: HeapDumpDCmd class
aoqi@0 172 //
aoqi@0 173 // Input arguments :-
aoqi@0 174 // arg0: Name of the dump file
aoqi@0 175 // arg1: "-live" or "-all"
aoqi@0 176 jint dump_heap(AttachOperation* op, outputStream* out) {
aoqi@0 177 const char* path = op->arg(0);
aoqi@0 178 if (path == NULL || path[0] == '\0') {
aoqi@0 179 out->print_cr("No dump file specified");
aoqi@0 180 } else {
aoqi@0 181 bool live_objects_only = true; // default is true to retain the behavior before this change is made
aoqi@0 182 const char* arg1 = op->arg(1);
aoqi@0 183 if (arg1 != NULL && (strlen(arg1) > 0)) {
aoqi@0 184 if (strcmp(arg1, "-all") != 0 && strcmp(arg1, "-live") != 0) {
aoqi@0 185 out->print_cr("Invalid argument to dumpheap operation: %s", arg1);
aoqi@0 186 return JNI_ERR;
aoqi@0 187 }
aoqi@0 188 live_objects_only = strcmp(arg1, "-live") == 0;
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 // Request a full GC before heap dump if live_objects_only = true
aoqi@0 192 // This helps reduces the amount of unreachable objects in the dump
aoqi@0 193 // and makes it easier to browse.
aoqi@0 194 HeapDumper dumper(live_objects_only /* request GC */);
aoqi@0 195 int res = dumper.dump(op->arg(0));
aoqi@0 196 if (res == 0) {
aoqi@0 197 out->print_cr("Heap dump file created");
aoqi@0 198 } else {
aoqi@0 199 // heap dump failed
aoqi@0 200 ResourceMark rm;
aoqi@0 201 char* error = dumper.error_as_C_string();
aoqi@0 202 if (error == NULL) {
aoqi@0 203 out->print_cr("Dump failed - reason unknown");
aoqi@0 204 } else {
aoqi@0 205 out->print_cr("%s", error);
aoqi@0 206 }
aoqi@0 207 }
aoqi@0 208 }
aoqi@0 209 return JNI_OK;
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 // Implementation of "inspectheap" command
aoqi@0 213 // See also: ClassHistogramDCmd class
aoqi@0 214 //
aoqi@0 215 // Input arguments :-
aoqi@0 216 // arg0: "-live" or "-all"
aoqi@0 217 static jint heap_inspection(AttachOperation* op, outputStream* out) {
aoqi@0 218 bool live_objects_only = true; // default is true to retain the behavior before this change is made
aoqi@0 219 const char* arg0 = op->arg(0);
aoqi@0 220 if (arg0 != NULL && (strlen(arg0) > 0)) {
aoqi@0 221 if (strcmp(arg0, "-all") != 0 && strcmp(arg0, "-live") != 0) {
aoqi@0 222 out->print_cr("Invalid argument to inspectheap operation: %s", arg0);
aoqi@0 223 return JNI_ERR;
aoqi@0 224 }
aoqi@0 225 live_objects_only = strcmp(arg0, "-live") == 0;
aoqi@0 226 }
aoqi@0 227 VM_GC_HeapInspection heapop(out, live_objects_only /* request full gc */);
aoqi@0 228 VMThread::execute(&heapop);
aoqi@0 229 return JNI_OK;
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 // set a boolean global flag using value from AttachOperation
aoqi@0 233 static jint set_bool_flag(const char* name, AttachOperation* op, outputStream* out) {
aoqi@0 234 bool value = true;
aoqi@0 235 const char* arg1;
aoqi@0 236 if ((arg1 = op->arg(1)) != NULL) {
aoqi@0 237 int tmp;
aoqi@0 238 int n = sscanf(arg1, "%d", &tmp);
aoqi@0 239 if (n != 1) {
aoqi@0 240 out->print_cr("flag value must be a boolean (1 or 0)");
aoqi@0 241 return JNI_ERR;
aoqi@0 242 }
aoqi@0 243 value = (tmp != 0);
aoqi@0 244 }
aoqi@0 245 bool res = CommandLineFlags::boolAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND);
aoqi@0 246 if (! res) {
aoqi@0 247 out->print_cr("setting flag %s failed", name);
aoqi@0 248 }
aoqi@0 249 return res? JNI_OK : JNI_ERR;
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 // set a intx global flag using value from AttachOperation
aoqi@0 253 static jint set_intx_flag(const char* name, AttachOperation* op, outputStream* out) {
aoqi@0 254 intx value;
aoqi@0 255 const char* arg1;
aoqi@0 256 if ((arg1 = op->arg(1)) != NULL) {
aoqi@0 257 int n = sscanf(arg1, INTX_FORMAT, &value);
aoqi@0 258 if (n != 1) {
aoqi@0 259 out->print_cr("flag value must be an integer");
aoqi@0 260 return JNI_ERR;
aoqi@0 261 }
aoqi@0 262 }
aoqi@0 263 bool res = CommandLineFlags::intxAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND);
aoqi@0 264 if (! res) {
aoqi@0 265 out->print_cr("setting flag %s failed", name);
aoqi@0 266 }
aoqi@0 267
aoqi@0 268 return res? JNI_OK : JNI_ERR;
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 // set a uintx global flag using value from AttachOperation
aoqi@0 272 static jint set_uintx_flag(const char* name, AttachOperation* op, outputStream* out) {
aoqi@0 273 uintx value;
dsamersoff@8658 274
dsamersoff@8658 275 const char* arg1 = op->arg(1);
dsamersoff@8658 276 if (arg1 == NULL) {
dsamersoff@8658 277 out->print_cr("flag value must be specified");
dsamersoff@8658 278 return JNI_ERR;
dsamersoff@8658 279 }
dsamersoff@8658 280
dsamersoff@8658 281 int n = sscanf(arg1, UINTX_FORMAT, &value);
dsamersoff@8658 282 if (n != 1) {
dsamersoff@8658 283 out->print_cr("flag value must be an unsigned integer");
dsamersoff@8658 284 return JNI_ERR;
aoqi@0 285 }
aoqi@0 286
aoqi@0 287 if (strncmp(name, "MaxHeapFreeRatio", 17) == 0) {
aoqi@0 288 FormatBuffer<80> err_msg("%s", "");
aoqi@0 289 if (!Arguments::verify_MaxHeapFreeRatio(err_msg, value)) {
aoqi@0 290 out->print_cr("%s", err_msg.buffer());
aoqi@0 291 return JNI_ERR;
aoqi@0 292 }
aoqi@0 293 } else if (strncmp(name, "MinHeapFreeRatio", 17) == 0) {
aoqi@0 294 FormatBuffer<80> err_msg("%s", "");
aoqi@0 295 if (!Arguments::verify_MinHeapFreeRatio(err_msg, value)) {
aoqi@0 296 out->print_cr("%s", err_msg.buffer());
aoqi@0 297 return JNI_ERR;
aoqi@0 298 }
aoqi@0 299 }
aoqi@0 300 bool res = CommandLineFlags::uintxAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND);
aoqi@0 301 if (! res) {
aoqi@0 302 out->print_cr("setting flag %s failed", name);
aoqi@0 303 }
aoqi@0 304
aoqi@0 305 return res? JNI_OK : JNI_ERR;
aoqi@0 306 }
aoqi@0 307
aoqi@0 308 // set a uint64_t global flag using value from AttachOperation
aoqi@0 309 static jint set_uint64_t_flag(const char* name, AttachOperation* op, outputStream* out) {
aoqi@0 310 uint64_t value;
aoqi@0 311 const char* arg1;
aoqi@0 312 if ((arg1 = op->arg(1)) != NULL) {
aoqi@0 313 int n = sscanf(arg1, UINT64_FORMAT, &value);
aoqi@0 314 if (n != 1) {
aoqi@0 315 out->print_cr("flag value must be an unsigned 64-bit integer");
aoqi@0 316 return JNI_ERR;
aoqi@0 317 }
aoqi@0 318 }
aoqi@0 319 bool res = CommandLineFlags::uint64_tAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND);
aoqi@0 320 if (! res) {
aoqi@0 321 out->print_cr("setting flag %s failed", name);
aoqi@0 322 }
aoqi@0 323
aoqi@0 324 return res? JNI_OK : JNI_ERR;
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 // set a string global flag using value from AttachOperation
aoqi@0 328 static jint set_ccstr_flag(const char* name, AttachOperation* op, outputStream* out) {
aoqi@0 329 const char* value;
aoqi@0 330 if ((value = op->arg(1)) == NULL) {
aoqi@0 331 out->print_cr("flag value must be a string");
aoqi@0 332 return JNI_ERR;
aoqi@0 333 }
aoqi@0 334 bool res = CommandLineFlags::ccstrAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND);
aoqi@0 335 if (res) {
aoqi@0 336 FREE_C_HEAP_ARRAY(char, value, mtInternal);
aoqi@0 337 } else {
aoqi@0 338 out->print_cr("setting flag %s failed", name);
aoqi@0 339 }
aoqi@0 340
aoqi@0 341 return res? JNI_OK : JNI_ERR;
aoqi@0 342 }
aoqi@0 343
aoqi@0 344 // Implementation of "setflag" command
aoqi@0 345 static jint set_flag(AttachOperation* op, outputStream* out) {
aoqi@0 346
aoqi@0 347 const char* name = NULL;
aoqi@0 348 if ((name = op->arg(0)) == NULL) {
aoqi@0 349 out->print_cr("flag name is missing");
aoqi@0 350 return JNI_ERR;
aoqi@0 351 }
aoqi@0 352
aoqi@0 353 Flag* f = Flag::find_flag((char*)name, strlen(name));
aoqi@0 354 if (f && f->is_external() && f->is_writeable()) {
aoqi@0 355 if (f->is_bool()) {
aoqi@0 356 return set_bool_flag(name, op, out);
aoqi@0 357 } else if (f->is_intx()) {
aoqi@0 358 return set_intx_flag(name, op, out);
aoqi@0 359 } else if (f->is_uintx()) {
aoqi@0 360 return set_uintx_flag(name, op, out);
aoqi@0 361 } else if (f->is_uint64_t()) {
aoqi@0 362 return set_uint64_t_flag(name, op, out);
aoqi@0 363 } else if (f->is_ccstr()) {
aoqi@0 364 return set_ccstr_flag(name, op, out);
aoqi@0 365 } else {
aoqi@0 366 ShouldNotReachHere();
aoqi@0 367 return JNI_ERR;
aoqi@0 368 }
aoqi@0 369 } else {
aoqi@0 370 return AttachListener::pd_set_flag(op, out);
aoqi@0 371 }
aoqi@0 372 }
aoqi@0 373
aoqi@0 374 // Implementation of "printflag" command
aoqi@0 375 // See also: PrintVMFlagsDCmd class
aoqi@0 376 static jint print_flag(AttachOperation* op, outputStream* out) {
aoqi@0 377 const char* name = NULL;
aoqi@0 378 if ((name = op->arg(0)) == NULL) {
aoqi@0 379 out->print_cr("flag name is missing");
aoqi@0 380 return JNI_ERR;
aoqi@0 381 }
aoqi@0 382 Flag* f = Flag::find_flag((char*)name, strlen(name));
aoqi@0 383 if (f) {
aoqi@0 384 f->print_as_flag(out);
aoqi@0 385 out->cr();
aoqi@0 386 } else {
aoqi@0 387 out->print_cr("no such flag '%s'", name);
aoqi@0 388 }
aoqi@0 389 return JNI_OK;
aoqi@0 390 }
aoqi@0 391
aoqi@0 392 // Table to map operation names to functions.
aoqi@0 393
aoqi@0 394 // names must be of length <= AttachOperation::name_length_max
aoqi@0 395 static AttachOperationFunctionInfo funcs[] = {
aoqi@0 396 { "agentProperties", get_agent_properties },
aoqi@0 397 { "datadump", data_dump },
aoqi@0 398 { "dumpheap", dump_heap },
aoqi@0 399 { "load", JvmtiExport::load_agent_library },
aoqi@0 400 { "properties", get_system_properties },
aoqi@0 401 { "threaddump", thread_dump },
aoqi@0 402 { "inspectheap", heap_inspection },
aoqi@0 403 { "setflag", set_flag },
aoqi@0 404 { "printflag", print_flag },
aoqi@0 405 { "jcmd", jcmd },
aoqi@0 406 { NULL, NULL }
aoqi@0 407 };
aoqi@0 408
aoqi@0 409
aoqi@0 410
aoqi@0 411 // The Attach Listener threads services a queue. It dequeues an operation
aoqi@0 412 // from the queue, examines the operation name (command), and dispatches
aoqi@0 413 // to the corresponding function to perform the operation.
aoqi@0 414
aoqi@0 415 static void attach_listener_thread_entry(JavaThread* thread, TRAPS) {
aoqi@0 416 os::set_priority(thread, NearMaxPriority);
aoqi@0 417
aoqi@0 418 thread->record_stack_base_and_size();
aoqi@0 419
aoqi@0 420 if (AttachListener::pd_init() != 0) {
aoqi@0 421 return;
aoqi@0 422 }
aoqi@0 423 AttachListener::set_initialized();
aoqi@0 424
aoqi@0 425 for (;;) {
aoqi@0 426 AttachOperation* op = AttachListener::dequeue();
aoqi@0 427 if (op == NULL) {
aoqi@0 428 return; // dequeue failed or shutdown
aoqi@0 429 }
aoqi@0 430
aoqi@0 431 ResourceMark rm;
aoqi@0 432 bufferedStream st;
aoqi@0 433 jint res = JNI_OK;
aoqi@0 434
aoqi@0 435 // handle special detachall operation
aoqi@0 436 if (strcmp(op->name(), AttachOperation::detachall_operation_name()) == 0) {
aoqi@0 437 AttachListener::detachall();
aoqi@0 438 } else {
aoqi@0 439 // find the function to dispatch too
aoqi@0 440 AttachOperationFunctionInfo* info = NULL;
aoqi@0 441 for (int i=0; funcs[i].name != NULL; i++) {
aoqi@0 442 const char* name = funcs[i].name;
aoqi@0 443 assert(strlen(name) <= AttachOperation::name_length_max, "operation <= name_length_max");
aoqi@0 444 if (strcmp(op->name(), name) == 0) {
aoqi@0 445 info = &(funcs[i]);
aoqi@0 446 break;
aoqi@0 447 }
aoqi@0 448 }
aoqi@0 449
aoqi@0 450 // check for platform dependent attach operation
aoqi@0 451 if (info == NULL) {
aoqi@0 452 info = AttachListener::pd_find_operation(op->name());
aoqi@0 453 }
aoqi@0 454
aoqi@0 455 if (info != NULL) {
aoqi@0 456 // dispatch to the function that implements this operation
aoqi@0 457 res = (info->func)(op, &st);
aoqi@0 458 } else {
aoqi@0 459 st.print("Operation %s not recognized!", op->name());
aoqi@0 460 res = JNI_ERR;
aoqi@0 461 }
aoqi@0 462 }
aoqi@0 463
aoqi@0 464 // operation complete - send result and output to client
aoqi@0 465 op->complete(res, &st);
aoqi@0 466 }
aoqi@0 467 }
aoqi@0 468
zgu@9313 469 bool AttachListener::has_init_error(TRAPS) {
zgu@9313 470 if (HAS_PENDING_EXCEPTION) {
zgu@9313 471 tty->print_cr("Exception in VM (AttachListener::init) : ");
zgu@9313 472 java_lang_Throwable::print(PENDING_EXCEPTION, tty);
zgu@9313 473 tty->cr();
zgu@9313 474
zgu@9313 475 CLEAR_PENDING_EXCEPTION;
zgu@9313 476
zgu@9313 477 return true;
zgu@9313 478 } else {
zgu@9313 479 return false;
zgu@9313 480 }
zgu@9313 481 }
zgu@9313 482
aoqi@0 483 // Starts the Attach Listener thread
aoqi@0 484 void AttachListener::init() {
aoqi@0 485 EXCEPTION_MARK;
zgu@9313 486 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, THREAD);
zgu@9313 487 if (has_init_error(THREAD)) {
zgu@9313 488 return;
zgu@9313 489 }
zgu@9313 490
aoqi@0 491 instanceKlassHandle klass (THREAD, k);
zgu@9313 492 instanceHandle thread_oop = klass->allocate_instance_handle(THREAD);
zgu@9313 493 if (has_init_error(THREAD)) {
zgu@9313 494 return;
zgu@9313 495 }
aoqi@0 496
aoqi@0 497 const char thread_name[] = "Attach Listener";
zgu@9313 498 Handle string = java_lang_String::create_from_str(thread_name, THREAD);
zgu@9313 499 if (has_init_error(THREAD)) {
zgu@9313 500 return;
zgu@9313 501 }
aoqi@0 502
aoqi@0 503 // Initialize thread_oop to put it into the system threadGroup
aoqi@0 504 Handle thread_group (THREAD, Universe::system_thread_group());
aoqi@0 505 JavaValue result(T_VOID);
aoqi@0 506 JavaCalls::call_special(&result, thread_oop,
aoqi@0 507 klass,
aoqi@0 508 vmSymbols::object_initializer_name(),
aoqi@0 509 vmSymbols::threadgroup_string_void_signature(),
aoqi@0 510 thread_group,
aoqi@0 511 string,
aoqi@0 512 THREAD);
aoqi@0 513
zgu@9313 514 if (has_init_error(THREAD)) {
aoqi@0 515 return;
aoqi@0 516 }
aoqi@0 517
aoqi@0 518 KlassHandle group(THREAD, SystemDictionary::ThreadGroup_klass());
aoqi@0 519 JavaCalls::call_special(&result,
aoqi@0 520 thread_group,
aoqi@0 521 group,
aoqi@0 522 vmSymbols::add_method_name(),
aoqi@0 523 vmSymbols::thread_void_signature(),
aoqi@0 524 thread_oop, // ARG 1
aoqi@0 525 THREAD);
zgu@9313 526 if (has_init_error(THREAD)) {
aoqi@0 527 return;
aoqi@0 528 }
aoqi@0 529
aoqi@0 530 { MutexLocker mu(Threads_lock);
aoqi@0 531 JavaThread* listener_thread = new JavaThread(&attach_listener_thread_entry);
aoqi@0 532
aoqi@0 533 // Check that thread and osthread were created
aoqi@0 534 if (listener_thread == NULL || listener_thread->osthread() == NULL) {
aoqi@0 535 vm_exit_during_initialization("java.lang.OutOfMemoryError",
aoqi@0 536 "unable to create new native thread");
aoqi@0 537 }
aoqi@0 538
aoqi@0 539 java_lang_Thread::set_thread(thread_oop(), listener_thread);
aoqi@0 540 java_lang_Thread::set_daemon(thread_oop());
aoqi@0 541
aoqi@0 542 listener_thread->set_threadObj(thread_oop());
aoqi@0 543 Threads::add(listener_thread);
aoqi@0 544 Thread::start(listener_thread);
aoqi@0 545 }
aoqi@0 546 }
aoqi@0 547
aoqi@0 548 // Performs clean-up tasks on platforms where we can detect that the last
aoqi@0 549 // client has detached
aoqi@0 550 void AttachListener::detachall() {
aoqi@0 551 // call the platform dependent clean-up
aoqi@0 552 pd_detachall();
aoqi@0 553 }

mercurial