src/hotspot/share/services/attachListener.cpp
author gziemski
Thu, 26 Apr 2018 11:19:05 -0500
changeset 49902 3661f31c6df4
parent 49860 ca5216a2a2cc
child 50217 843fc56f4686
permissions -rw-r--r--
8202150: [REDO] Split globals.hpp to factor out the Flag class Summary: Factored out Flag out go globals, renamed to JVMFlag Reviewed-by: coleenp, dholmes, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
49041
44122f767467 8198286: Direct memory accessors in typeArrayOop.hpp should use Access API
eosterlund
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4571
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4571
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4571
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "classfile/javaClasses.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "classfile/systemDictionary.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 29083
diff changeset
    28
#include "gc/shared/vmGCOperations.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "memory/resourceArea.hpp"
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 27880
diff changeset
    30
#include "oops/oop.inline.hpp"
49041
44122f767467 8198286: Direct memory accessors in typeArrayOop.hpp should use Access API
eosterlund
parents: 47216
diff changeset
    31
#include "oops/typeArrayOop.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
#include "prims/jvmtiExport.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    33
#include "runtime/arguments.hpp"
49902
3661f31c6df4 8202150: [REDO] Split globals.hpp to factor out the Flag class
gziemski
parents: 49860
diff changeset
    34
#include "runtime/flags/jvmFlag.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    35
#include "runtime/globals.hpp"
49361
1956d0ec092a 8199319: Remove handles.inline.hpp include from reflectionUtils.hpp
stefank
parents: 49041
diff changeset
    36
#include "runtime/handles.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    37
#include "runtime/java.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    38
#include "runtime/javaCalls.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    39
#include "runtime/os.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    40
#include "services/attachListener.hpp"
11209
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
    41
#include "services/diagnosticCommand.hpp"
30764
fec48bf5a827 8079792: GC directory structure cleanup
pliden
parents: 29083
diff changeset
    42
#include "services/heapDumper.hpp"
28949
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
    43
#include "services/writeableFlags.hpp"
46560
388aa8d67c80 8181449: Fix debug.hpp / globalDefinitions.hpp dependency inversion
kbarrett
parents: 46458
diff changeset
    44
#include "utilities/debug.hpp"
388aa8d67c80 8181449: Fix debug.hpp / globalDefinitions.hpp dependency inversion
kbarrett
parents: 46458
diff changeset
    45
#include "utilities/formatBuffer.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
volatile bool AttachListener::_initialized;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
// Implementation of "properties" command.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
//
36795
bfd8c074c8ec 8153181: Move sun.misc.VMSupport to an internal package
chegar
parents: 31371
diff changeset
    51
// Invokes VMSupport.serializePropertiesToByteArray to serialize
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
// the system properties into a byte array.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
    54
static InstanceKlass* load_and_initialize_klass(Symbol* sh, TRAPS) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
    55
  Klass* k = SystemDictionary::resolve_or_fail(sh, true, CHECK_NULL);
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
    56
  InstanceKlass* ik = InstanceKlass::cast(k);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  if (ik->should_be_initialized()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    ik->initialize(CHECK_NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  }
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
    60
  return ik;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
    63
static jint get_properties(AttachOperation* op, outputStream* out, Symbol* serializePropertiesMethod) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  Thread* THREAD = Thread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  HandleMark hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
36795
bfd8c074c8ec 8153181: Move sun.misc.VMSupport to an internal package
chegar
parents: 31371
diff changeset
    67
  // load VMSupport
bfd8c074c8ec 8153181: Move sun.misc.VMSupport to an internal package
chegar
parents: 31371
diff changeset
    68
  Symbol* klass = vmSymbols::jdk_internal_vm_VMSupport();
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
    69
  InstanceKlass* k = load_and_initialize_klass(klass, THREAD);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  if (HAS_PENDING_EXCEPTION) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
    java_lang_Throwable::print(PENDING_EXCEPTION, out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
    CLEAR_PENDING_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
    return JNI_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  // invoke the serializePropertiesToByteArray method
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  JavaValue result(T_OBJECT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  JavaCallArguments args;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
    81
  Symbol* signature = vmSymbols::serializePropertiesToByteArray_signature();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  JavaCalls::call_static(&result,
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
    83
                         k,
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
    84
                         serializePropertiesMethod,
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
    85
                         signature,
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
    86
                         &args,
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
    87
                         THREAD);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  if (HAS_PENDING_EXCEPTION) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    java_lang_Throwable::print(PENDING_EXCEPTION, out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
    CLEAR_PENDING_EXCEPTION;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    return JNI_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  // The result should be a [B
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  oop res = (oop)result.get_jobject();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  assert(res->is_typeArray(), "just checking");
13952
e3cf184080bc 8000213: NPG: Should have renamed arrayKlass and typeArrayKlass
coleenp
parents: 13728
diff changeset
    97
  assert(TypeArrayKlass::cast(res->klass())->element_type() == T_BYTE, "just checking");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  // copy the bytes to the output stream
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  typeArrayOop ba = typeArrayOop(res);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  jbyte* addr = typeArrayOop(res)->byte_at_addr(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  out->print_raw((const char*)addr, ba->length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  return JNI_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
44993
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   107
// Implementation of "load" command.
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   108
static jint load_agent(AttachOperation* op, outputStream* out) {
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   109
  // get agent name and options
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   110
  const char* agent = op->arg(0);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   111
  const char* absParam = op->arg(1);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   112
  const char* options = op->arg(2);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   113
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   114
  // If loading a java agent then need to ensure that the java.instrument module is loaded
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   115
  if (strcmp(agent, "instrument") == 0) {
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   116
    Thread* THREAD = Thread::current();
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   117
    ResourceMark rm(THREAD);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   118
    HandleMark hm(THREAD);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   119
    JavaValue result(T_OBJECT);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   120
    Handle h_module_name = java_lang_String::create_from_str("java.instrument", THREAD);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   121
    JavaCalls::call_static(&result,
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   122
                           SystemDictionary::module_Modules_klass(),
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   123
                           vmSymbols::loadModule_name(),
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   124
                           vmSymbols::loadModule_signature(),
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   125
                           h_module_name,
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   126
                           THREAD);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   127
    if (HAS_PENDING_EXCEPTION) {
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   128
      java_lang_Throwable::print(PENDING_EXCEPTION, out);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   129
      CLEAR_PENDING_EXCEPTION;
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   130
      return JNI_ERR;
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   131
    }
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   132
  }
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   133
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   134
  return JvmtiExport::load_agent_library(agent, absParam, options, out);
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   135
}
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   136
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
// Implementation of "properties" command.
11441
a89f443814cd 7120511: Add diagnostic commands
fparain
parents: 11209
diff changeset
   138
// See also: PrintSystemPropertiesDCmd class
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
static jint get_system_properties(AttachOperation* op, outputStream* out) {
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   140
  return get_properties(op, out, vmSymbols::serializePropertiesToByteArray_name());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
// Implementation of "agent_properties" command.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
static jint get_agent_properties(AttachOperation* op, outputStream* out) {
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   145
  return get_properties(op, out, vmSymbols::serializeAgentPropertiesToByteArray_name());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
// Implementation of "datadump" command.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
// Raises a SIGBREAK signal so that VM dump threads, does deadlock detection,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
// etc. In theory this command should only post a DataDumpRequest to any
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
// JVMTI environment that has enabled this event. However it's useful to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
// trigger the SIGBREAK handler.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
static jint data_dump(AttachOperation* op, outputStream* out) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  if (!ReduceSignalUsage) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    AttachListener::pd_data_dump();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    if (JvmtiExport::should_post_data_dump()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
      JvmtiExport::post_data_dump();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  return JNI_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
// Implementation of "threaddump" command - essentially a remote ctrl-break
11441
a89f443814cd 7120511: Add diagnostic commands
fparain
parents: 11209
diff changeset
   167
// See also: ThreadDumpDCmd class
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
static jint thread_dump(AttachOperation* op, outputStream* out) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  bool print_concurrent_locks = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  if (op->arg(0) != NULL && strcmp(op->arg(0), "-l") == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
    print_concurrent_locks = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
  // thread stacks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
  VM_PrintThreads op1(out, print_concurrent_locks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  VMThread::execute(&op1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  // JNI global handles
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  VM_PrintJNI op2(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  VMThread::execute(&op2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  // Deadlock detection
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  VM_FindDeadlocks op3(out);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  VMThread::execute(&op3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
  return JNI_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
11209
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   190
// A jcmd attach operation request was received, which will now
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   191
// dispatch to the diagnostic commands used for serviceability functions.
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   192
static jint jcmd(AttachOperation* op, outputStream* out) {
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   193
  Thread* THREAD = Thread::current();
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   194
  // All the supplied jcmd arguments are stored as a single
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   195
  // string (op->arg(0)). This is parsed by the Dcmd framework.
17296
68557efd8583 8004095: Add support for JMX interface to Diagnostic Framework and Commands
fparain
parents: 15432
diff changeset
   196
  DCmd::parse_and_execute(DCmd_Source_AttachAPI, out, op->arg(0), ' ', THREAD);
11209
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   197
  if (HAS_PENDING_EXCEPTION) {
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   198
    java_lang_Throwable::print(PENDING_EXCEPTION, out);
11441
a89f443814cd 7120511: Add diagnostic commands
fparain
parents: 11209
diff changeset
   199
    out->cr();
11209
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   200
    CLEAR_PENDING_EXCEPTION;
24833
c55a7c2888f6 8044398: Attach code should propagate errors in Diagnostic Commands as errors
sla
parents: 24424
diff changeset
   201
    return JNI_ERR;
11209
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   202
  }
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   203
  return JNI_OK;
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   204
}
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   205
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
// Implementation of "dumpheap" command.
11441
a89f443814cd 7120511: Add diagnostic commands
fparain
parents: 11209
diff changeset
   207
// See also: HeapDumpDCmd class
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
// Input arguments :-
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
//   arg0: Name of the dump file
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
//   arg1: "-live" or "-all"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
jint dump_heap(AttachOperation* op, outputStream* out) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  const char* path = op->arg(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  if (path == NULL || path[0] == '\0') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    out->print_cr("No dump file specified");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
    bool live_objects_only = true;   // default is true to retain the behavior before this change is made
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    const char* arg1 = op->arg(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
    if (arg1 != NULL && (strlen(arg1) > 0)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
      if (strcmp(arg1, "-all") != 0 && strcmp(arg1, "-live") != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
        out->print_cr("Invalid argument to dumpheap operation: %s", arg1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
        return JNI_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
      live_objects_only = strcmp(arg1, "-live") == 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
    // Request a full GC before heap dump if live_objects_only = true
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    // This helps reduces the amount of unreachable objects in the dump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    // and makes it easier to browse.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
    HeapDumper dumper(live_objects_only /* request GC */);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    int res = dumper.dump(op->arg(0));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    if (res == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      out->print_cr("Heap dump file created");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
      // heap dump failed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
      ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
      char* error = dumper.error_as_C_string();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      if (error == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
        out->print_cr("Dump failed - reason unknown");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
        out->print_cr("%s", error);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  return JNI_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
// Implementation of "inspectheap" command
11441
a89f443814cd 7120511: Add diagnostic commands
fparain
parents: 11209
diff changeset
   249
// See also: ClassHistogramDCmd class
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
// Input arguments :-
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
//   arg0: "-live" or "-all"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
static jint heap_inspection(AttachOperation* op, outputStream* out) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  bool live_objects_only = true;   // default is true to retain the behavior before this change is made
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  const char* arg0 = op->arg(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  if (arg0 != NULL && (strlen(arg0) > 0)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    if (strcmp(arg0, "-all") != 0 && strcmp(arg0, "-live") != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
      out->print_cr("Invalid argument to inspectheap operation: %s", arg0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
      return JNI_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
    live_objects_only = strcmp(arg0, "-live") == 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  }
18025
b7bcf7497f93 8005849: JEP 167: Event-Based JVM Tracing
sla
parents: 17296
diff changeset
   263
  VM_GC_HeapInspection heapop(out, live_objects_only /* request full gc */);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  VMThread::execute(&heapop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  return JNI_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
// Implementation of "setflag" command
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
static jint set_flag(AttachOperation* op, outputStream* out) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  const char* name = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  if ((name = op->arg(0)) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
    out->print_cr("flag name is missing");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
    return JNI_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
28949
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
   277
  FormatBuffer<80> err_msg("%s", "");
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
   278
49902
3661f31c6df4 8202150: [REDO] Split globals.hpp to factor out the Flag class
gziemski
parents: 49860
diff changeset
   279
  int ret = WriteableFlags::set_flag(op->arg(0), op->arg(1), JVMFlag::ATTACH_ON_DEMAND, err_msg);
3661f31c6df4 8202150: [REDO] Split globals.hpp to factor out the Flag class
gziemski
parents: 49860
diff changeset
   280
  if (ret != JVMFlag::SUCCESS) {
3661f31c6df4 8202150: [REDO] Split globals.hpp to factor out the Flag class
gziemski
parents: 49860
diff changeset
   281
    if (ret == JVMFlag::NON_WRITABLE) {
28949
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
   282
      // if the flag is not manageable try to change it through
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
   283
      // the platform dependent implementation
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
   284
      return AttachListener::pd_set_flag(op, out);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    } else {
28949
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
   286
      out->print_cr("%s", err_msg.buffer());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
    }
28949
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
   288
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
   289
    return JNI_ERR;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  }
28949
c6f50d62ecef 8067447: Factor out the shared implementation of the VM flags manipulation code
jbachorik
parents: 27880
diff changeset
   291
  return JNI_OK;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
// Implementation of "printflag" command
11441
a89f443814cd 7120511: Add diagnostic commands
fparain
parents: 11209
diff changeset
   295
// See also: PrintVMFlagsDCmd class
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
static jint print_flag(AttachOperation* op, outputStream* out) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  const char* name = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  if ((name = op->arg(0)) == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    out->print_cr("flag name is missing");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    return JNI_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  }
49902
3661f31c6df4 8202150: [REDO] Split globals.hpp to factor out the Flag class
gziemski
parents: 49860
diff changeset
   302
  JVMFlag* f = JVMFlag::find_flag((char*)name, strlen(name));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  if (f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    f->print_as_flag(out);
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 22758
diff changeset
   305
    out->cr();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    out->print_cr("no such flag '%s'", name);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  return JNI_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
// Table to map operation names to functions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
// names must be of length <= AttachOperation::name_length_max
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
static AttachOperationFunctionInfo funcs[] = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  { "agentProperties",  get_agent_properties },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  { "datadump",         data_dump },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  { "dumpheap",         dump_heap },
44993
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   319
  { "load",             load_agent },
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  { "properties",       get_system_properties },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  { "threaddump",       thread_dump },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  { "inspectheap",      heap_inspection },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  { "setflag",          set_flag },
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  { "printflag",        print_flag },
11209
110525ac80b0 7104647: Adding a diagnostic command framework
fparain
parents: 8921
diff changeset
   325
  { "jcmd",             jcmd },
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  { NULL,               NULL }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
// The Attach Listener threads services a queue. It dequeues an operation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
// from the queue, examines the operation name (command), and dispatches
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
// to the corresponding function to perform the operation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
static void attach_listener_thread_entry(JavaThread* thread, TRAPS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
  os::set_priority(thread, NearMaxPriority);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
49737
01960eec4570 8201321: NMT: Unnecessary re-recording thread stack and size when attaching listener to JavaThread
zgu
parents: 49361
diff changeset
   338
  assert(thread == Thread::current(), "Must be");
01960eec4570 8201321: NMT: Unnecessary re-recording thread stack and size when attaching listener to JavaThread
zgu
parents: 49361
diff changeset
   339
  assert(thread->stack_base() != NULL && thread->stack_size() > 0,
01960eec4570 8201321: NMT: Unnecessary re-recording thread stack and size when attaching listener to JavaThread
zgu
parents: 49361
diff changeset
   340
         "Should already be setup");
14120
7d298141c258 7199092: NMT: NMT needs to deal overlapped virtual memory ranges
zgu
parents: 13952
diff changeset
   341
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
  if (AttachListener::pd_init() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  AttachListener::set_initialized();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  for (;;) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    AttachOperation* op = AttachListener::dequeue();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    if (op == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
      return;   // dequeue failed or shutdown
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
    bufferedStream st;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
    jint res = JNI_OK;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
    // handle special detachall operation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    if (strcmp(op->name(), AttachOperation::detachall_operation_name()) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
      AttachListener::detachall();
44993
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   360
    } else if (!EnableDynamicAgentLoading && strcmp(op->name(), "load") == 0) {
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   361
      st.print("Dynamic agent loading is not enabled. "
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   362
               "Use -XX:+EnableDynamicAgentLoading to launch target VM.");
f61bcd80ec1f 8178380: Module system implementation refresh (5/2017)
alanb
parents: 36795
diff changeset
   363
      res = JNI_ERR;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
      // find the function to dispatch too
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
      AttachOperationFunctionInfo* info = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
      for (int i=0; funcs[i].name != NULL; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
        const char* name = funcs[i].name;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
        assert(strlen(name) <= AttachOperation::name_length_max, "operation <= name_length_max");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
        if (strcmp(op->name(), name) == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
          info = &(funcs[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
      // check for platform dependent attach operation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
      if (info == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
        info = AttachListener::pd_find_operation(op->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
      if (info != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
        // dispatch to the function that implements this operation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
        res = (info->func)(op, &st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
        st.print("Operation %s not recognized!", op->name());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
        res = JNI_ERR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    // operation complete - send result and output to client
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    op->complete(res, &st);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
22197
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   395
bool AttachListener::has_init_error(TRAPS) {
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   396
  if (HAS_PENDING_EXCEPTION) {
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   397
    tty->print_cr("Exception in VM (AttachListener::init) : ");
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   398
    java_lang_Throwable::print(PENDING_EXCEPTION, tty);
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   399
    tty->cr();
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   400
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   401
    CLEAR_PENDING_EXCEPTION;
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   402
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   403
    return true;
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   404
  } else {
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   405
    return false;
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   406
  }
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   407
}
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   408
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
// Starts the Attach Listener thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
void AttachListener::init() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
  EXCEPTION_MARK;
22197
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   412
  Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, THREAD);
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   413
  if (has_init_error(THREAD)) {
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   414
    return;
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   415
  }
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   416
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
   417
  InstanceKlass* klass = InstanceKlass::cast(k);
22197
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   418
  instanceHandle thread_oop = klass->allocate_instance_handle(THREAD);
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   419
  if (has_init_error(THREAD)) {
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   420
    return;
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   421
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
  const char thread_name[] = "Attach Listener";
22197
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   424
  Handle string = java_lang_String::create_from_str(thread_name, THREAD);
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   425
  if (has_init_error(THREAD)) {
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   426
    return;
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   427
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
  // Initialize thread_oop to put it into the system threadGroup
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  Handle thread_group (THREAD, Universe::system_thread_group());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
  JavaValue result(T_VOID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
  JavaCalls::call_special(&result, thread_oop,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
                       klass,
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   434
                       vmSymbols::object_initializer_name(),
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   435
                       vmSymbols::threadgroup_string_void_signature(),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
                       thread_group,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
                       string,
20067
e0337f5f9f5a 6989981: jstack causes "fatal error: ExceptionMark destructor expects no pending exceptions"
sla
parents: 18025
diff changeset
   438
                       THREAD);
e0337f5f9f5a 6989981: jstack causes "fatal error: ExceptionMark destructor expects no pending exceptions"
sla
parents: 18025
diff changeset
   439
22197
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   440
  if (has_init_error(THREAD)) {
20067
e0337f5f9f5a 6989981: jstack causes "fatal error: ExceptionMark destructor expects no pending exceptions"
sla
parents: 18025
diff changeset
   441
    return;
e0337f5f9f5a 6989981: jstack causes "fatal error: ExceptionMark destructor expects no pending exceptions"
sla
parents: 18025
diff changeset
   442
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 36795
diff changeset
   444
  Klass* group = SystemDictionary::ThreadGroup_klass();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  JavaCalls::call_special(&result,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
                        thread_group,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
                        group,
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   448
                        vmSymbols::add_method_name(),
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7397
diff changeset
   449
                        vmSymbols::thread_void_signature(),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
                        thread_oop,             // ARG 1
20067
e0337f5f9f5a 6989981: jstack causes "fatal error: ExceptionMark destructor expects no pending exceptions"
sla
parents: 18025
diff changeset
   451
                        THREAD);
22197
41ea552a447d 6730115: Fastdebug VM crashes with "ExceptionMark destructor expects no pending exceptions" error
zgu
parents: 20291
diff changeset
   452
  if (has_init_error(THREAD)) {
20067
e0337f5f9f5a 6989981: jstack causes "fatal error: ExceptionMark destructor expects no pending exceptions"
sla
parents: 18025
diff changeset
   453
    return;
e0337f5f9f5a 6989981: jstack causes "fatal error: ExceptionMark destructor expects no pending exceptions"
sla
parents: 18025
diff changeset
   454
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
  { MutexLocker mu(Threads_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
    JavaThread* listener_thread = new JavaThread(&attach_listener_thread_entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
    // Check that thread and osthread were created
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
    if (listener_thread == NULL || listener_thread->osthread() == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
      vm_exit_during_initialization("java.lang.OutOfMemoryError",
22758
c6b6abb73544 7182040: volano29 limited by os resource on Linux - need better diagnostic message
iklam
parents: 22555
diff changeset
   462
                                    os::native_thread_creation_failed_msg());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
    java_lang_Thread::set_thread(thread_oop(), listener_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
    java_lang_Thread::set_daemon(thread_oop());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
    listener_thread->set_threadObj(thread_oop());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
    Threads::add(listener_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
    Thread::start(listener_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
// Performs clean-up tasks on platforms where we can detect that the last
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
// client has detached
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
void AttachListener::detachall() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
  // call the platform dependent clean-up
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
  pd_detachall();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
}