src/share/vm/services/attachListener.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 7128
2545e461115b
parent 6876
710a3c8b516e
child 8856
ac27a9c85bea
permissions
-rw-r--r--

merge

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;
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;
aoqi@0 274 const char* arg1;
aoqi@0 275 if ((arg1 = op->arg(1)) != NULL) {
aoqi@0 276 int n = sscanf(arg1, UINTX_FORMAT, &value);
aoqi@0 277 if (n != 1) {
aoqi@0 278 out->print_cr("flag value must be an unsigned integer");
aoqi@0 279 return JNI_ERR;
aoqi@0 280 }
aoqi@0 281 }
aoqi@0 282
aoqi@0 283 if (strncmp(name, "MaxHeapFreeRatio", 17) == 0) {
aoqi@0 284 FormatBuffer<80> err_msg("%s", "");
aoqi@0 285 if (!Arguments::verify_MaxHeapFreeRatio(err_msg, value)) {
aoqi@0 286 out->print_cr("%s", err_msg.buffer());
aoqi@0 287 return JNI_ERR;
aoqi@0 288 }
aoqi@0 289 } else if (strncmp(name, "MinHeapFreeRatio", 17) == 0) {
aoqi@0 290 FormatBuffer<80> err_msg("%s", "");
aoqi@0 291 if (!Arguments::verify_MinHeapFreeRatio(err_msg, value)) {
aoqi@0 292 out->print_cr("%s", err_msg.buffer());
aoqi@0 293 return JNI_ERR;
aoqi@0 294 }
aoqi@0 295 }
aoqi@0 296 bool res = CommandLineFlags::uintxAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND);
aoqi@0 297 if (! res) {
aoqi@0 298 out->print_cr("setting flag %s failed", name);
aoqi@0 299 }
aoqi@0 300
aoqi@0 301 return res? JNI_OK : JNI_ERR;
aoqi@0 302 }
aoqi@0 303
aoqi@0 304 // set a uint64_t global flag using value from AttachOperation
aoqi@0 305 static jint set_uint64_t_flag(const char* name, AttachOperation* op, outputStream* out) {
aoqi@0 306 uint64_t value;
aoqi@0 307 const char* arg1;
aoqi@0 308 if ((arg1 = op->arg(1)) != NULL) {
aoqi@0 309 int n = sscanf(arg1, UINT64_FORMAT, &value);
aoqi@0 310 if (n != 1) {
aoqi@0 311 out->print_cr("flag value must be an unsigned 64-bit integer");
aoqi@0 312 return JNI_ERR;
aoqi@0 313 }
aoqi@0 314 }
aoqi@0 315 bool res = CommandLineFlags::uint64_tAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND);
aoqi@0 316 if (! res) {
aoqi@0 317 out->print_cr("setting flag %s failed", name);
aoqi@0 318 }
aoqi@0 319
aoqi@0 320 return res? JNI_OK : JNI_ERR;
aoqi@0 321 }
aoqi@0 322
aoqi@0 323 // set a string global flag using value from AttachOperation
aoqi@0 324 static jint set_ccstr_flag(const char* name, AttachOperation* op, outputStream* out) {
aoqi@0 325 const char* value;
aoqi@0 326 if ((value = op->arg(1)) == NULL) {
aoqi@0 327 out->print_cr("flag value must be a string");
aoqi@0 328 return JNI_ERR;
aoqi@0 329 }
aoqi@0 330 bool res = CommandLineFlags::ccstrAtPut((char*)name, &value, Flag::ATTACH_ON_DEMAND);
aoqi@0 331 if (res) {
aoqi@0 332 FREE_C_HEAP_ARRAY(char, value, mtInternal);
aoqi@0 333 } else {
aoqi@0 334 out->print_cr("setting flag %s failed", name);
aoqi@0 335 }
aoqi@0 336
aoqi@0 337 return res? JNI_OK : JNI_ERR;
aoqi@0 338 }
aoqi@0 339
aoqi@0 340 // Implementation of "setflag" command
aoqi@0 341 static jint set_flag(AttachOperation* op, outputStream* out) {
aoqi@0 342
aoqi@0 343 const char* name = NULL;
aoqi@0 344 if ((name = op->arg(0)) == NULL) {
aoqi@0 345 out->print_cr("flag name is missing");
aoqi@0 346 return JNI_ERR;
aoqi@0 347 }
aoqi@0 348
aoqi@0 349 Flag* f = Flag::find_flag((char*)name, strlen(name));
aoqi@0 350 if (f && f->is_external() && f->is_writeable()) {
aoqi@0 351 if (f->is_bool()) {
aoqi@0 352 return set_bool_flag(name, op, out);
aoqi@0 353 } else if (f->is_intx()) {
aoqi@0 354 return set_intx_flag(name, op, out);
aoqi@0 355 } else if (f->is_uintx()) {
aoqi@0 356 return set_uintx_flag(name, op, out);
aoqi@0 357 } else if (f->is_uint64_t()) {
aoqi@0 358 return set_uint64_t_flag(name, op, out);
aoqi@0 359 } else if (f->is_ccstr()) {
aoqi@0 360 return set_ccstr_flag(name, op, out);
aoqi@0 361 } else {
aoqi@0 362 ShouldNotReachHere();
aoqi@0 363 return JNI_ERR;
aoqi@0 364 }
aoqi@0 365 } else {
aoqi@0 366 return AttachListener::pd_set_flag(op, out);
aoqi@0 367 }
aoqi@0 368 }
aoqi@0 369
aoqi@0 370 // Implementation of "printflag" command
aoqi@0 371 // See also: PrintVMFlagsDCmd class
aoqi@0 372 static jint print_flag(AttachOperation* op, outputStream* out) {
aoqi@0 373 const char* name = NULL;
aoqi@0 374 if ((name = op->arg(0)) == NULL) {
aoqi@0 375 out->print_cr("flag name is missing");
aoqi@0 376 return JNI_ERR;
aoqi@0 377 }
aoqi@0 378 Flag* f = Flag::find_flag((char*)name, strlen(name));
aoqi@0 379 if (f) {
aoqi@0 380 f->print_as_flag(out);
aoqi@0 381 out->cr();
aoqi@0 382 } else {
aoqi@0 383 out->print_cr("no such flag '%s'", name);
aoqi@0 384 }
aoqi@0 385 return JNI_OK;
aoqi@0 386 }
aoqi@0 387
aoqi@0 388 // Table to map operation names to functions.
aoqi@0 389
aoqi@0 390 // names must be of length <= AttachOperation::name_length_max
aoqi@0 391 static AttachOperationFunctionInfo funcs[] = {
aoqi@0 392 { "agentProperties", get_agent_properties },
aoqi@0 393 { "datadump", data_dump },
aoqi@0 394 { "dumpheap", dump_heap },
aoqi@0 395 { "load", JvmtiExport::load_agent_library },
aoqi@0 396 { "properties", get_system_properties },
aoqi@0 397 { "threaddump", thread_dump },
aoqi@0 398 { "inspectheap", heap_inspection },
aoqi@0 399 { "setflag", set_flag },
aoqi@0 400 { "printflag", print_flag },
aoqi@0 401 { "jcmd", jcmd },
aoqi@0 402 { NULL, NULL }
aoqi@0 403 };
aoqi@0 404
aoqi@0 405
aoqi@0 406
aoqi@0 407 // The Attach Listener threads services a queue. It dequeues an operation
aoqi@0 408 // from the queue, examines the operation name (command), and dispatches
aoqi@0 409 // to the corresponding function to perform the operation.
aoqi@0 410
aoqi@0 411 static void attach_listener_thread_entry(JavaThread* thread, TRAPS) {
aoqi@0 412 os::set_priority(thread, NearMaxPriority);
aoqi@0 413
aoqi@0 414 thread->record_stack_base_and_size();
aoqi@0 415
aoqi@0 416 if (AttachListener::pd_init() != 0) {
aoqi@0 417 return;
aoqi@0 418 }
aoqi@0 419 AttachListener::set_initialized();
aoqi@0 420
aoqi@0 421 for (;;) {
aoqi@0 422 AttachOperation* op = AttachListener::dequeue();
aoqi@0 423 if (op == NULL) {
aoqi@0 424 return; // dequeue failed or shutdown
aoqi@0 425 }
aoqi@0 426
aoqi@0 427 ResourceMark rm;
aoqi@0 428 bufferedStream st;
aoqi@0 429 jint res = JNI_OK;
aoqi@0 430
aoqi@0 431 // handle special detachall operation
aoqi@0 432 if (strcmp(op->name(), AttachOperation::detachall_operation_name()) == 0) {
aoqi@0 433 AttachListener::detachall();
aoqi@0 434 } else {
aoqi@0 435 // find the function to dispatch too
aoqi@0 436 AttachOperationFunctionInfo* info = NULL;
aoqi@0 437 for (int i=0; funcs[i].name != NULL; i++) {
aoqi@0 438 const char* name = funcs[i].name;
aoqi@0 439 assert(strlen(name) <= AttachOperation::name_length_max, "operation <= name_length_max");
aoqi@0 440 if (strcmp(op->name(), name) == 0) {
aoqi@0 441 info = &(funcs[i]);
aoqi@0 442 break;
aoqi@0 443 }
aoqi@0 444 }
aoqi@0 445
aoqi@0 446 // check for platform dependent attach operation
aoqi@0 447 if (info == NULL) {
aoqi@0 448 info = AttachListener::pd_find_operation(op->name());
aoqi@0 449 }
aoqi@0 450
aoqi@0 451 if (info != NULL) {
aoqi@0 452 // dispatch to the function that implements this operation
aoqi@0 453 res = (info->func)(op, &st);
aoqi@0 454 } else {
aoqi@0 455 st.print("Operation %s not recognized!", op->name());
aoqi@0 456 res = JNI_ERR;
aoqi@0 457 }
aoqi@0 458 }
aoqi@0 459
aoqi@0 460 // operation complete - send result and output to client
aoqi@0 461 op->complete(res, &st);
aoqi@0 462 }
aoqi@0 463 }
aoqi@0 464
aoqi@0 465 // Starts the Attach Listener thread
aoqi@0 466 void AttachListener::init() {
aoqi@0 467 EXCEPTION_MARK;
aoqi@0 468 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK);
aoqi@0 469 instanceKlassHandle klass (THREAD, k);
aoqi@0 470 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);
aoqi@0 471
aoqi@0 472 const char thread_name[] = "Attach Listener";
aoqi@0 473 Handle string = java_lang_String::create_from_str(thread_name, CHECK);
aoqi@0 474
aoqi@0 475 // Initialize thread_oop to put it into the system threadGroup
aoqi@0 476 Handle thread_group (THREAD, Universe::system_thread_group());
aoqi@0 477 JavaValue result(T_VOID);
aoqi@0 478 JavaCalls::call_special(&result, thread_oop,
aoqi@0 479 klass,
aoqi@0 480 vmSymbols::object_initializer_name(),
aoqi@0 481 vmSymbols::threadgroup_string_void_signature(),
aoqi@0 482 thread_group,
aoqi@0 483 string,
aoqi@0 484 THREAD);
aoqi@0 485
aoqi@0 486 if (HAS_PENDING_EXCEPTION) {
aoqi@0 487 tty->print_cr("Exception in VM (AttachListener::init) : ");
aoqi@0 488 java_lang_Throwable::print(PENDING_EXCEPTION, tty);
aoqi@0 489 tty->cr();
aoqi@0 490
aoqi@0 491 CLEAR_PENDING_EXCEPTION;
aoqi@0 492
aoqi@0 493 return;
aoqi@0 494 }
aoqi@0 495
aoqi@0 496 KlassHandle group(THREAD, SystemDictionary::ThreadGroup_klass());
aoqi@0 497 JavaCalls::call_special(&result,
aoqi@0 498 thread_group,
aoqi@0 499 group,
aoqi@0 500 vmSymbols::add_method_name(),
aoqi@0 501 vmSymbols::thread_void_signature(),
aoqi@0 502 thread_oop, // ARG 1
aoqi@0 503 THREAD);
aoqi@0 504
aoqi@0 505 if (HAS_PENDING_EXCEPTION) {
aoqi@0 506 tty->print_cr("Exception in VM (AttachListener::init) : ");
aoqi@0 507 java_lang_Throwable::print(PENDING_EXCEPTION, tty);
aoqi@0 508 tty->cr();
aoqi@0 509
aoqi@0 510 CLEAR_PENDING_EXCEPTION;
aoqi@0 511
aoqi@0 512 return;
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 { MutexLocker mu(Threads_lock);
aoqi@0 516 JavaThread* listener_thread = new JavaThread(&attach_listener_thread_entry);
aoqi@0 517
aoqi@0 518 // Check that thread and osthread were created
aoqi@0 519 if (listener_thread == NULL || listener_thread->osthread() == NULL) {
aoqi@0 520 vm_exit_during_initialization("java.lang.OutOfMemoryError",
aoqi@0 521 "unable to create new native thread");
aoqi@0 522 }
aoqi@0 523
aoqi@0 524 java_lang_Thread::set_thread(thread_oop(), listener_thread);
aoqi@0 525 java_lang_Thread::set_daemon(thread_oop());
aoqi@0 526
aoqi@0 527 listener_thread->set_threadObj(thread_oop());
aoqi@0 528 Threads::add(listener_thread);
aoqi@0 529 Thread::start(listener_thread);
aoqi@0 530 }
aoqi@0 531 }
aoqi@0 532
aoqi@0 533 // Performs clean-up tasks on platforms where we can detect that the last
aoqi@0 534 // client has detached
aoqi@0 535 void AttachListener::detachall() {
aoqi@0 536 // call the platform dependent clean-up
aoqi@0 537 pd_detachall();
aoqi@0 538 }

mercurial