src/hotspot/share/prims/jvmtiEnvBase.cpp
author lfoltan
Mon, 21 Oct 2019 13:13:16 -0400
changeset 58722 cba8afa5cfed
parent 58488 165b193b30dd
child 58901 2700c409ff10
permissions -rw-r--r--
8231844: Enhance type signature characters in classfile_constants.h and improve the JVM to use type signature characters more consistently Summary: Increase the use of type signature constants instead of hard coded characters within the JVM. Reviewed-by: coleenp, dholmes, fparain Contributed-by: lois.foltan@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
53582
881c5fbeb849 8218041: Assorted wrong/missing includes
rehn
parents: 52877
diff changeset
     2
 * Copyright (c) 2003, 2019, 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: 4573
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 4573
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: 4573
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: 7106
diff changeset
    25
#include "precompiled.hpp"
51959
db0c3952de52 8209645: Split ClassLoaderData and ClassLoaderDataGraph into separate files
coleenp
parents: 51608
diff changeset
    26
#include "classfile/classLoaderDataGraph.hpp"
54347
235883996bc7 8221698: Remove redundant includes from popular header files
iklam
parents: 53582
diff changeset
    27
#include "classfile/moduleEntry.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    28
#include "classfile/systemDictionary.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    29
#include "jvmtifiles/jvmtiEnv.hpp"
37248
11a660dbbb8e 8132524: Missing includes to resourceArea.hpp
jprovino
parents: 36508
diff changeset
    30
#include "memory/resourceArea.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    31
#include "oops/objArrayKlass.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    32
#include "oops/objArrayOop.hpp"
29081
c61eb4914428 8072911: Remove includes of oop.inline.hpp from .hpp files
stefank
parents: 27165
diff changeset
    33
#include "oops/oop.inline.hpp"
49816
a3e79f97e86b 8200555: OopHandle should use Access API
coleenp
parents: 49480
diff changeset
    34
#include "oops/oopHandle.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    35
#include "prims/jvmtiEnvBase.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    36
#include "prims/jvmtiEventController.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    37
#include "prims/jvmtiExtensions.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    38
#include "prims/jvmtiImpl.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    39
#include "prims/jvmtiManageCapabilities.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    40
#include "prims/jvmtiTagMap.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    41
#include "prims/jvmtiThreadState.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    42
#include "runtime/biasedLocking.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    43
#include "runtime/deoptimization.hpp"
49480
d7df2dd501ce 8199809: Don't include frame.inline.hpp and other.inline.hpp from .hpp files
coleenp
parents: 49449
diff changeset
    44
#include "runtime/frame.inline.hpp"
53582
881c5fbeb849 8218041: Assorted wrong/missing includes
rehn
parents: 52877
diff changeset
    45
#include "runtime/handles.inline.hpp"
49449
ef5d5d343e2a 8199263: Split interfaceSupport.hpp to not require including .inline.hpp files
coleenp
parents: 49192
diff changeset
    46
#include "runtime/interfaceSupport.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    47
#include "runtime/jfieldIDWorkaround.hpp"
49192
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
    48
#include "runtime/jniHandles.inline.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    49
#include "runtime/objectMonitor.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    50
#include "runtime/objectMonitor.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    51
#include "runtime/signature.hpp"
24351
61b33cc6d3cf 8042195: Introduce umbrella header orderAccess.inline.hpp.
goetz
parents: 23518
diff changeset
    52
#include "runtime/thread.inline.hpp"
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
    53
#include "runtime/threadSMR.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    54
#include "runtime/vframe.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    55
#include "runtime/vframe_hp.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 7106
diff changeset
    56
#include "runtime/vmThread.hpp"
52877
9e041366c764 8214850: Rename vm_operations.?pp files to vmOperations.?pp files
tschatzl
parents: 51959
diff changeset
    57
#include "runtime/vmOperations.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
///////////////////////////////////////////////////////////////
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// JvmtiEnvBase
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
JvmtiEnvBase* JvmtiEnvBase::_head_environment = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
bool JvmtiEnvBase::_globally_initialized = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
volatile bool JvmtiEnvBase::_needs_clean_up = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
jvmtiPhase JvmtiEnvBase::_phase = JVMTI_PHASE_PRIMORDIAL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
volatile int JvmtiEnvBase::_dying_thread_env_iteration_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
extern jvmtiInterface_1_ jvmti_Interface;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
extern jvmtiInterface_1_ jvmtiTrace_Interface;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
// perform initializations that must occur before any JVMTI environments
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
// are released but which should only be initialized once (no matter
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
// how many environments are created).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
JvmtiEnvBase::globally_initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  assert(_globally_initialized == false, "bad call");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  JvmtiManageCapabilities::initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  // register extension functions and events
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  JvmtiExtensions::register_extensions();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
#ifdef JVMTI_TRACE
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  JvmtiTrace::initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  _globally_initialized = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
JvmtiEnvBase::initialize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  // Add this environment to the end of the environment list (order is important)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    // This block of code must not contain any safepoints, as list deallocation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
    // (which occurs at a safepoint) cannot occur simultaneously with this list
35492
c8c0273e6b91 8146690: Make all classes in GC follow the naming convention.
david
parents: 33589
diff changeset
   106
    // addition.  Note: NoSafepointVerifier cannot, currently, be used before
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
    // threads exist.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    JvmtiEnvIterator it;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    JvmtiEnvBase *previous_env = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
    for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
      previous_env = env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
    if (previous_env == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
      _head_environment = this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
      previous_env->set_next_environment(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  if (_globally_initialized == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    globally_initialize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   125
jvmtiPhase
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   126
JvmtiEnvBase::phase() {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   127
  // For the JVMTI environments possessed the can_generate_early_vmstart:
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   128
  //   replace JVMTI_PHASE_PRIMORDIAL with JVMTI_PHASE_START
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   129
  if (_phase == JVMTI_PHASE_PRIMORDIAL &&
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   130
      JvmtiExport::early_vmstart_recorded() &&
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   131
      early_vmstart_env()) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   132
    return JVMTI_PHASE_START;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   133
  }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   134
  return _phase; // Normal case
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
   135
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
2137
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   137
bool
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   138
JvmtiEnvBase::is_valid() {
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   139
  jint value = 0;
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   140
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   141
  // This object might not be a JvmtiEnvBase so we can't assume
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   142
  // the _magic field is properly aligned. Get the value in a safe
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   143
  // way and then check against JVMTI_MAGIC.
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   144
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   145
  switch (sizeof(_magic)) {
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   146
  case 2:
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   147
    value = Bytes::get_native_u2((address)&_magic);
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   148
    break;
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   149
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   150
  case 4:
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   151
    value = Bytes::get_native_u4((address)&_magic);
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   152
    break;
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   153
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   154
  case 8:
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   155
    value = Bytes::get_native_u8((address)&_magic);
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   156
    break;
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   157
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   158
  default:
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   159
    guarantee(false, "_magic field is an unexpected size");
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   160
  }
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   161
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   162
  return value == JVMTI_MAGIC;
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   163
}
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   164
b03f1da1a3d3 6567360: 3/4 SIGBUS in jvmti RawMonitor magic check for unaligned bad monitor pointer
dcubed
parents: 2135
diff changeset
   165
4492
dcubed
parents: 3261 4491
diff changeset
   166
bool
4491
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   167
JvmtiEnvBase::use_version_1_0_semantics() {
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   168
  int major, minor, micro;
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   169
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   170
  JvmtiExport::decode_version_values(_version, &major, &minor, &micro);
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   171
  return major == 1 && minor == 0;  // micro version doesn't matter here
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   172
}
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   173
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   174
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   175
bool
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   176
JvmtiEnvBase::use_version_1_1_semantics() {
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   177
  int major, minor, micro;
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   178
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   179
  JvmtiExport::decode_version_values(_version, &major, &minor, &micro);
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   180
  return major == 1 && minor == 1;  // micro version doesn't matter here
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   181
}
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   182
7413
59b978f8fbdf 7003782: Update JVMTI version to 1.2 for jdk7
kamg
parents: 7397
diff changeset
   183
bool
59b978f8fbdf 7003782: Update JVMTI version to 1.2 for jdk7
kamg
parents: 7397
diff changeset
   184
JvmtiEnvBase::use_version_1_2_semantics() {
59b978f8fbdf 7003782: Update JVMTI version to 1.2 for jdk7
kamg
parents: 7397
diff changeset
   185
  int major, minor, micro;
59b978f8fbdf 7003782: Update JVMTI version to 1.2 for jdk7
kamg
parents: 7397
diff changeset
   186
59b978f8fbdf 7003782: Update JVMTI version to 1.2 for jdk7
kamg
parents: 7397
diff changeset
   187
  JvmtiExport::decode_version_values(_version, &major, &minor, &micro);
59b978f8fbdf 7003782: Update JVMTI version to 1.2 for jdk7
kamg
parents: 7397
diff changeset
   188
  return major == 1 && minor == 2;  // micro version doesn't matter here
59b978f8fbdf 7003782: Update JVMTI version to 1.2 for jdk7
kamg
parents: 7397
diff changeset
   189
}
59b978f8fbdf 7003782: Update JVMTI version to 1.2 for jdk7
kamg
parents: 7397
diff changeset
   190
4491
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   191
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   192
JvmtiEnvBase::JvmtiEnvBase(jint version) : _env_event_enable() {
212bd48525d4 6849968: 3/2 JVMTI tests fails on jdk5.0 with hs14
dcubed
parents: 1623
diff changeset
   193
  _version = version;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  _env_local_storage = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  _tag_map = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  _native_method_prefix_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
  _native_method_prefixes = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  _next = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  _class_file_load_hook_ever_enabled = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  // Moot since ClassFileLoadHook not yet enabled.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  // But "true" will give a more predictable ClassFileLoadHook behavior
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  // for environment creation during ClassFileLoadHook.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  _is_retransformable = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  // all callbacks initially NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  memset(&_event_callbacks,0,sizeof(jvmtiEventCallbacks));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  // all capabilities initially off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
  memset(&_current_capabilities, 0, sizeof(_current_capabilities));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  // all prohibited capabilities initially off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  memset(&_prohibited_capabilities, 0, sizeof(_prohibited_capabilities));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
  _magic = JVMTI_MAGIC;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  JvmtiEventController::env_initialize((JvmtiEnv*)this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
#ifdef JVMTI_TRACE
1403
3be05c51cf44 6753795: HotSpot crash in strlen() when JVMTI is used
never
parents: 1
diff changeset
   220
  _jvmti_external.functions = TraceJVMTI != NULL ? &jvmtiTrace_Interface : &jvmti_Interface;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
  _jvmti_external.functions = &jvmti_Interface;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
JvmtiEnvBase::dispose() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
#ifdef JVMTI_TRACE
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  JvmtiTrace::shutdown();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
  // Dispose of event info and let the event controller call us back
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  // in a locked state (env_dispose, below)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  JvmtiEventController::env_dispose(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
JvmtiEnvBase::env_dispose() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  // We have been entered with all events disabled on this environment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  // A race to re-enable events (by setting callbacks) is prevented by
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  // checking for a valid environment when setting callbacks (while
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  // holding the JvmtiThreadState_lock).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  // Mark as invalid.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
  _magic = DISPOSED_MAGIC;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  // Relinquish all capabilities.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  jvmtiCapabilities *caps = get_capabilities();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  JvmtiManageCapabilities::relinquish_capabilities(caps, caps, caps);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  // Same situation as with events (see above)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  set_native_method_prefixes(0, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
  JvmtiTagMap* tag_map_to_deallocate = _tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  set_tag_map(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
  // A tag map can be big, deallocate it now
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  if (tag_map_to_deallocate != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
    delete tag_map_to_deallocate;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  _needs_clean_up = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
JvmtiEnvBase::~JvmtiEnvBase() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  // There is a small window of time during which the tag map of a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  // disposed environment could have been reallocated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  // Make sure it is gone.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  JvmtiTagMap* tag_map_to_deallocate = _tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
  set_tag_map(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  // A tag map can be big, deallocate it now
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  if (tag_map_to_deallocate != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
    delete tag_map_to_deallocate;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  _magic = BAD_MAGIC;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
JvmtiEnvBase::periodic_clean_up() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  // JvmtiEnvBase reference is saved in JvmtiEnvThreadState. So
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  // clean up JvmtiThreadState before deleting JvmtiEnv pointer.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  JvmtiThreadState::periodic_clean_up();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  // Unlink all invalid environments from the list of environments
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  // and deallocate them
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  JvmtiEnvIterator it;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  JvmtiEnvBase* previous_env = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  JvmtiEnvBase* env = it.first();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  while (env != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
    if (env->is_valid()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
      previous_env = env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
      env = it.next(env);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
      // This one isn't valid, remove it from the list and deallocate it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
      JvmtiEnvBase* defunct_env = env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
      env = it.next(env);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
      if (previous_env == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
        _head_environment = env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
        previous_env->set_next_environment(env);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
      delete defunct_env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
JvmtiEnvBase::check_for_periodic_clean_up() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  assert(SafepointSynchronize::is_at_safepoint(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
  class ThreadInsideIterationClosure: public ThreadClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
   private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    bool _inside;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
   public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    ThreadInsideIterationClosure() : _inside(false) {};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    void do_thread(Thread* thread) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
      _inside |= thread->is_inside_jvmti_env_iteration();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    bool is_inside_jvmti_env_iteration() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
      return _inside;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  if (_needs_clean_up) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    // Check if we are currently iterating environment,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
    // deallocation should not occur if we are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    ThreadInsideIterationClosure tiic;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
    Threads::threads_do(&tiic);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    if (!tiic.is_inside_jvmti_env_iteration() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
             !is_inside_dying_thread_env_iteration()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
      _needs_clean_up = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
      JvmtiEnvBase::periodic_clean_up();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
JvmtiEnvBase::record_first_time_class_file_load_hook_enabled() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
         "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  if (!_class_file_load_hook_ever_enabled) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    _class_file_load_hook_ever_enabled = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    if (get_capabilities()->can_retransform_classes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
      _is_retransformable = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
      _is_retransformable = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
      // cannot add retransform capability after ClassFileLoadHook has been enabled
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
      get_prohibited_capabilities()->can_retransform_classes = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
JvmtiEnvBase::record_class_file_load_hook_enabled() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  if (!_class_file_load_hook_ever_enabled) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    if (Threads::number_of_threads() == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
      record_first_time_class_file_load_hook_enabled();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
      MutexLocker mu(JvmtiThreadState_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
      record_first_time_class_file_load_hook_enabled();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
JvmtiEnvBase::set_native_method_prefixes(jint prefix_count, char** prefixes) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
         "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
  int old_prefix_count = get_native_method_prefix_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
  char **old_prefixes = get_native_method_prefixes();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
  // allocate and install the new prefixex
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
  if (prefix_count == 0 || !is_valid()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
    _native_method_prefix_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
    _native_method_prefixes = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
    // there are prefixes, allocate an array to hold them, and fill it
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10546
diff changeset
   399
    char** new_prefixes = (char**)os::malloc((prefix_count) * sizeof(char*), mtInternal);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
    if (new_prefixes == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
      return JVMTI_ERROR_OUT_OF_MEMORY;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
    for (int i = 0; i < prefix_count; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
      char* prefix = prefixes[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
      if (prefix == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
        for (int j = 0; j < (i-1); j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
          os::free(new_prefixes[j]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
        os::free(new_prefixes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
        return JVMTI_ERROR_NULL_POINTER;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
      prefix = os::strdup(prefixes[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
      if (prefix == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
        for (int j = 0; j < (i-1); j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
          os::free(new_prefixes[j]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
        os::free(new_prefixes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
        return JVMTI_ERROR_OUT_OF_MEMORY;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
      new_prefixes[i] = prefix;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
    _native_method_prefix_count = prefix_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
    _native_method_prefixes = new_prefixes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  // now that we know the new prefixes have been successfully installed we can
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
  // safely remove the old ones
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  if (old_prefix_count != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
    for (int i = 0; i < old_prefix_count; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
      os::free(old_prefixes[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
    os::free(old_prefixes);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
// Collect all the prefixes which have been set in any JVM TI environments
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
// by the SetNativeMethodPrefix(es) functions.  Be sure to maintain the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
// order of environments and the order of prefixes within each environment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
// Return in a resource allocated array.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
char**
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
JvmtiEnvBase::get_all_native_method_prefixes(int* count_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  assert(Threads::number_of_threads() == 0 ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
         SafepointSynchronize::is_at_safepoint() ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
         JvmtiThreadState_lock->is_locked(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
         "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  int total_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
  GrowableArray<char*>* prefix_array =new GrowableArray<char*>(5);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  JvmtiEnvIterator it;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
  for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
    int prefix_count = env->get_native_method_prefix_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
    char** prefixes = env->get_native_method_prefixes();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
    for (int j = 0; j < prefix_count; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
      // retrieve a prefix and so that it is safe against asynchronous changes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
      // copy it into the resource area
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
      char* prefix = prefixes[j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
      char* prefix_copy = NEW_RESOURCE_ARRAY(char, strlen(prefix)+1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
      strcpy(prefix_copy, prefix);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
      prefix_array->at_put_grow(total_count++, prefix_copy);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
  char** all_prefixes = NEW_RESOURCE_ARRAY(char*, total_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
  char** p = all_prefixes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
  for (int i = 0; i < total_count; ++i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
    *p++ = prefix_array->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
  *count_ptr = total_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
  return all_prefixes;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
JvmtiEnvBase::set_event_callbacks(const jvmtiEventCallbacks* callbacks,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
                                               jint size_of_callbacks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
  assert(Threads::number_of_threads() == 0 || JvmtiThreadState_lock->is_locked(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  size_t byte_cnt = sizeof(jvmtiEventCallbacks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  // clear in either case to be sure we got any gap between sizes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  memset(&_event_callbacks, 0, byte_cnt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
  // Now that JvmtiThreadState_lock is held, prevent a possible race condition where events
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
  // are re-enabled by a call to set event callbacks where the DisposeEnvironment
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
  // occurs after the boiler-plate environment check and before the lock is acquired.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  if (callbacks != NULL && is_valid()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
    if (size_of_callbacks < (jint)byte_cnt) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
      byte_cnt = size_of_callbacks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
    memcpy(&_event_callbacks, callbacks, byte_cnt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
// In the fullness of time, all users of the method should instead
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
// directly use allocate, besides being cleaner and faster, this will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
// mean much better out of memory handling
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
unsigned char *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
JvmtiEnvBase::jvmtiMalloc(jlong size) {
33589
7cbd1b2c139b 8139040: Fix initializations before ShouldNotReachHere() etc. and enable -Wuninitialized on linux.
goetz
parents: 31024
diff changeset
   503
  unsigned char* mem = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
  jvmtiError result = allocate(size, &mem);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
  assert(result == JVMTI_ERROR_NONE, "Allocate failed");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  return mem;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
49192
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   510
// Handle management
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   511
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   512
jobject JvmtiEnvBase::jni_reference(Handle hndl) {
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   513
  return JNIHandles::make_local(hndl());
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   514
}
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   515
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   516
jobject JvmtiEnvBase::jni_reference(JavaThread *thread, Handle hndl) {
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   517
  return JNIHandles::make_local(thread, hndl());
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   518
}
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   519
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   520
void JvmtiEnvBase::destroy_jni_reference(jobject jobj) {
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   521
  JNIHandles::destroy_local(jobj);
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   522
}
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   523
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   524
void JvmtiEnvBase::destroy_jni_reference(JavaThread *thread, jobject jobj) {
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   525
  JNIHandles::destroy_local(jobj); // thread is unused.
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   526
}
6734eeef4283 8198474: Move JNIHandles::resolve into jniHandles.inline.hpp
kbarrett
parents: 48823
diff changeset
   527
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
// Threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
jobject *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
JvmtiEnvBase::new_jobjectArray(int length, Handle *handles) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
  if (length == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
  jobject *objArray = (jobject *) jvmtiMalloc(sizeof(jobject) * length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
  NULL_CHECK(objArray, NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
  for (int i=0; i<length; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
    objArray[i] = jni_reference(handles[i]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
  return objArray;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
jthread *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
JvmtiEnvBase::new_jthreadArray(int length, Handle *handles) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
  return (jthread *) new_jobjectArray(length,handles);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
jthreadGroup *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
JvmtiEnvBase::new_jthreadGroupArray(int length, Handle *handles) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
  return (jthreadGroup *) new_jobjectArray(length,handles);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
// return the vframe on the specified thread and depth, NULL if no such frame
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
vframe*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
JvmtiEnvBase::vframeFor(JavaThread* java_thread, jint depth) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
  if (!java_thread->has_last_Java_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
    return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
  RegisterMap reg_map(java_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
  vframe *vf = java_thread->last_java_vframe(&reg_map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
  int d = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
  while ((vf != NULL) && (d < depth)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
    vf = vf->java_sender();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
    d++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
  return vf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
// utilities: JNI objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
jclass
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   580
JvmtiEnvBase::get_jni_class_non_null(Klass* k) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
  assert(k != NULL, "k != NULL");
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
   582
  Thread *thread = Thread::current();
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
   583
  return (jclass)jni_reference(Handle(thread, k->java_mirror()));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
// Field Information
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
bool
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   591
JvmtiEnvBase::get_field_descriptor(Klass* k, jfieldID field, fieldDescriptor* fd) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
  if (!jfieldIDWorkaround::is_valid_jfieldID(k, field)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  bool found = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
  if (jfieldIDWorkaround::is_static_jfieldID(field)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
    JNIid* id = jfieldIDWorkaround::from_static_jfieldID(field);
8725
8c1e3dd5fe1b 7017732: move static fields into Class to prepare for perm gen removal
never
parents: 8076
diff changeset
   598
    found = id->find_local_field(fd);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
    // Non-static field. The fieldID is really the offset of the field within the object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
    int offset = jfieldIDWorkaround::from_instance_jfieldID(k, field);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   602
    found = InstanceKlass::cast(k)->find_field_from_offset(offset, false, fd);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
  return found;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
// Object Monitor Information
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
// Count the number of objects for a lightweight monitor. The hobj
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
// parameter is object that owns the monitor so this routine will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
// count the number of times the same object was locked by frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
// in java_thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
jint
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
JvmtiEnvBase::count_locked_objects(JavaThread *java_thread, Handle hobj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
  jint ret = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
  if (!java_thread->has_last_Java_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
    return ret;  // no Java frames so no monitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
  HandleMark   hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
  RegisterMap  reg_map(java_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
  for(javaVFrame *jvf=java_thread->last_java_vframe(&reg_map); jvf != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
                                                 jvf = jvf->java_sender()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
    GrowableArray<MonitorInfo*>* mons = jvf->monitors();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
    if (!mons->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
      for (int i = 0; i < mons->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
        MonitorInfo *mi = mons->at(i);
3171
aa289b22b577 6837472: com/sun/jdi/MonitorFrameInfo.java fails with AggressiveOpts in 6u14
kvn
parents: 2137
diff changeset
   634
        if (mi->owner_is_scalar_replaced()) continue;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
        // see if owner of the monitor is our object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
        if (mi->owner() != NULL && mi->owner() == hobj()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
          ret++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
  return ret;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
JvmtiEnvBase::get_current_contended_monitor(JavaThread *calling_thread, JavaThread *java_thread, jobject *monitor_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
  uint32_t debug_bits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
  assert((SafepointSynchronize::is_at_safepoint() ||
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
   654
          java_thread->is_thread_fully_suspended(false, &debug_bits)),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
         "at safepoint or target thread is suspended");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
  oop obj = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
  ObjectMonitor *mon = java_thread->current_waiting_monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
  if (mon == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
    // thread is not doing an Object.wait() call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
    mon = java_thread->current_pending_monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
    if (mon != NULL) {
58488
165b193b30dd 8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents: 57893
diff changeset
   662
      // The thread is trying to enter() an ObjectMonitor.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
      obj = (oop)mon->object();
58488
165b193b30dd 8231289: Disentangle JvmtiRawMonitor from ObjectMonitor and clean it up
dholmes
parents: 57893
diff changeset
   664
      assert(obj != NULL, "ObjectMonitor should have a valid object!");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
    // implied else: no contended ObjectMonitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
    // thread is doing an Object.wait() call
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
    obj = (oop)mon->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
    assert(obj != NULL, "Object.wait() should have an object");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
  if (obj == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
    *monitor_ptr = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
    HandleMark hm;
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
   677
    Handle     hobj(Thread::current(), obj);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
    *monitor_ptr = jni_reference(calling_thread, hobj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
JvmtiEnvBase::get_owned_monitors(JavaThread *calling_thread, JavaThread* java_thread,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
                                 GrowableArray<jvmtiMonitorStackDepthInfo*> *owned_monitors_list) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
  jvmtiError err = JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
  uint32_t debug_bits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
  assert((SafepointSynchronize::is_at_safepoint() ||
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
   692
          java_thread->is_thread_fully_suspended(false, &debug_bits)),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
         "at safepoint or target thread is suspended");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
  if (java_thread->has_last_Java_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
    ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
    HandleMark   hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
    RegisterMap  reg_map(java_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
    int depth = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
    for (javaVFrame *jvf = java_thread->last_java_vframe(&reg_map); jvf != NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
         jvf = jvf->java_sender()) {
48823
99c973b56994 7179701: MaxJavaStackTraceDepth of zero is not handled correctly/consistently in the VM
coleenp
parents: 48105
diff changeset
   703
      if (MaxJavaStackTraceDepth == 0 || depth++ < MaxJavaStackTraceDepth) {  // check for stack too deep
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
        // add locked objects for this frame into list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
        err = get_locked_objects_in_frame(calling_thread, java_thread, jvf, owned_monitors_list, depth-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
        if (err != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
          return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
  // Get off stack monitors. (e.g. acquired via jni MonitorEnter).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
  JvmtiMonitorClosure jmc(java_thread, calling_thread, owned_monitors_list, this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
  ObjectSynchronizer::monitors_iterate(&jmc);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
  err = jmc.error();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
// Save JNI local handles for any objects that this frame owns.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
JvmtiEnvBase::get_locked_objects_in_frame(JavaThread* calling_thread, JavaThread* java_thread,
47765
b7c7428eaab9 8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents: 47216
diff changeset
   724
                                 javaVFrame *jvf, GrowableArray<jvmtiMonitorStackDepthInfo*>* owned_monitors_list, jint stack_depth) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
  jvmtiError err = JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
  GrowableArray<MonitorInfo*>* mons = jvf->monitors();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
  if (mons->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
    return err;  // this javaVFrame holds no monitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
  HandleMark hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
  oop wait_obj = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
    // save object of current wait() call (if any) for later comparison
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
    ObjectMonitor *mon = java_thread->current_waiting_monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
    if (mon != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
      wait_obj = (oop)mon->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
  oop pending_obj = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
    // save object of current enter() call (if any) for later comparison
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
    ObjectMonitor *mon = java_thread->current_pending_monitor();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
    if (mon != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
      pending_obj = (oop)mon->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
  for (int i = 0; i < mons->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
    MonitorInfo *mi = mons->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
3171
aa289b22b577 6837472: com/sun/jdi/MonitorFrameInfo.java fails with AggressiveOpts in 6u14
kvn
parents: 2137
diff changeset
   754
    if (mi->owner_is_scalar_replaced()) continue;
aa289b22b577 6837472: com/sun/jdi/MonitorFrameInfo.java fails with AggressiveOpts in 6u14
kvn
parents: 2137
diff changeset
   755
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
    oop obj = mi->owner();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
    if (obj == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
      // this monitor doesn't have an owning object so skip it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
    if (wait_obj == obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
      // the thread is waiting on this monitor so it isn't really owned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
    if (pending_obj == obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
      // the thread is pending on this monitor so it isn't really owned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
    if (owned_monitors_list->length() > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
      // Our list has at least one object on it so we have to check
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
      // for recursive object locking
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
      bool found = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
      for (int j = 0; j < owned_monitors_list->length(); j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
        jobject jobj = ((jvmtiMonitorStackDepthInfo*)owned_monitors_list->at(j))->monitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
        oop check = JNIHandles::resolve(jobj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
        if (check == obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
          found = true;  // we found the object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
      if (found) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
        // already have this object so don't include it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
        continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
    // add the owning object to our list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
    jvmtiMonitorStackDepthInfo *jmsdi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
    err = allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
    if (err != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
        return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
    }
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
   797
    Handle hobj(Thread::current(), obj);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
    jmsdi->monitor = jni_reference(calling_thread, hobj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
    jmsdi->stack_depth = stack_depth;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
    owned_monitors_list->append(jmsdi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
JvmtiEnvBase::get_stack_trace(JavaThread *java_thread,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
                              jint start_depth, jint max_count,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
                              jvmtiFrameInfo* frame_buffer, jint* count_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
  uint32_t debug_bits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
  assert((SafepointSynchronize::is_at_safepoint() ||
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
   814
          java_thread->is_thread_fully_suspended(false, &debug_bits)),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
         "at safepoint or target thread is suspended");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
  int count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
  if (java_thread->has_last_Java_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
    RegisterMap reg_map(java_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
    Thread* current_thread = Thread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
    ResourceMark rm(current_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
    javaVFrame *jvf = java_thread->last_java_vframe(&reg_map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
    HandleMark hm(current_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
    if (start_depth != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
      if (start_depth > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
        for (int j = 0; j < start_depth && jvf != NULL; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
          jvf = jvf->java_sender();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
        if (jvf == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
          // start_depth is deeper than the stack depth
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
          return JVMTI_ERROR_ILLEGAL_ARGUMENT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
      } else { // start_depth < 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
        // we are referencing the starting depth based on the oldest
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
        // part of the stack.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
        // optimize to limit the number of times that java_sender() is called
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
        javaVFrame *jvf_cursor = jvf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
        javaVFrame *jvf_prev = NULL;
31024
b619cc1d6ea6 8080928: Uninitialised variable in hotspot/src/share/vm/prims/jvmtiEnvBase.cpp
sla
parents: 29081
diff changeset
   838
        javaVFrame *jvf_prev_prev = NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
        int j = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
        while (jvf_cursor != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
          jvf_prev_prev = jvf_prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
          jvf_prev = jvf_cursor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
          for (j = 0; j > start_depth && jvf_cursor != NULL; j--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
            jvf_cursor = jvf_cursor->java_sender();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
        if (j == start_depth) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
          // previous pointer is exactly where we want to start
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
          jvf = jvf_prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
          // we need to back up further to get to the right place
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
          if (jvf_prev_prev == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
            // the -start_depth is greater than the stack depth
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
            return JVMTI_ERROR_ILLEGAL_ARGUMENT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
          // j now is the number of frames on the stack starting with
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
          // jvf_prev, we start from jvf_prev_prev and move older on
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
          // the stack that many, the result is -start_depth frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
          // remaining.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
          jvf = jvf_prev_prev;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
          for (; j < 0; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
            jvf = jvf->java_sender();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
    for (; count < max_count && jvf != NULL; count++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
      frame_buffer[count].method = jvf->method()->jmethod_id();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
      frame_buffer[count].location = (jvf->method()->is_native() ? -1 : jvf->bci());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
      jvf = jvf->java_sender();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
    if (start_depth != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
      // no frames and there is a starting depth
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
      return JVMTI_ERROR_ILLEGAL_ARGUMENT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
  *count_ptr = count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
JvmtiEnvBase::get_frame_count(JvmtiThreadState *state, jint *count_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
  assert((state != NULL),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
         "JavaThread should create JvmtiThreadState before calling this method");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
  *count_ptr = state->count_frames();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
JvmtiEnvBase::get_frame_location(JavaThread *java_thread, jint depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
                                 jmethodID* method_ptr, jlocation* location_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
  uint32_t debug_bits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
  assert((SafepointSynchronize::is_at_safepoint() ||
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
   897
          java_thread->is_thread_fully_suspended(false, &debug_bits)),
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
         "at safepoint or target thread is suspended");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
  Thread* current_thread = Thread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
  ResourceMark rm(current_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
  vframe *vf = vframeFor(java_thread, depth);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
  if (vf == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
    return JVMTI_ERROR_NO_MORE_FRAMES;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
  // vframeFor should return a java frame. If it doesn't
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
  // it means we've got an internal error and we return the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
  // error in product mode. In debug mode we will instead
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
  // attempt to cast the vframe to a javaVFrame and will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
  // cause an assertion/crash to allow further diagnosis.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
#ifdef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
  if (!vf->is_java_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
    return JVMTI_ERROR_INTERNAL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
  HandleMark hm(current_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
  javaVFrame *jvf = javaVFrame::cast(vf);
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 13195
diff changeset
   920
  Method* method = jvf->method();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
  if (method->is_native()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
    *location_ptr = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
    *location_ptr = jvf->bci();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
  *method_ptr = method->jmethod_id();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
JvmtiEnvBase::get_object_monitor_usage(JavaThread* calling_thread, jobject object, jvmtiMonitorUsage* info_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
  HandleMark hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
  Handle hobj;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
   937
  Thread* current_thread = Thread::current();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
  bool at_safepoint = SafepointSynchronize::is_at_safepoint();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
  // Check arguments
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
    oop mirror = JNIHandles::resolve_external_guard(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
    NULL_CHECK(mirror, JVMTI_ERROR_INVALID_OBJECT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
    NULL_CHECK(info_ptr, JVMTI_ERROR_NULL_POINTER);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
   946
    hobj = Handle(current_thread, mirror);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
  JavaThread *owning_thread = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
  ObjectMonitor *mon = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
  jvmtiMonitorUsage ret = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
      NULL, 0, 0, NULL, 0, NULL
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
  uint32_t debug_bits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
  // first derive the object's owner and entry_count (if any)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
    // Revoke any biases before querying the mark word
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
   959
    if (at_safepoint) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
      BiasedLocking::revoke_at_safepoint(hobj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
    } else {
57893
49fea19f0726 8229844: Remove attempt_rebias parameter from revoke_and_rebias()
pchilanomate
parents: 57777
diff changeset
   962
      BiasedLocking::revoke(hobj, calling_thread);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
    address owner = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
    {
57777
90ead0febf56 8229258: Rework markOop and markOopDesc into a simpler mark word value carrier
stefank
parents: 55416
diff changeset
   967
      markWord mark = hobj()->mark();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
57777
90ead0febf56 8229258: Rework markOop and markOopDesc into a simpler mark word value carrier
stefank
parents: 55416
diff changeset
   969
      if (!mark.has_monitor()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
        // this object has a lightweight monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
57777
90ead0febf56 8229258: Rework markOop and markOopDesc into a simpler mark word value carrier
stefank
parents: 55416
diff changeset
   972
        if (mark.has_locker()) {
90ead0febf56 8229258: Rework markOop and markOopDesc into a simpler mark word value carrier
stefank
parents: 55416
diff changeset
   973
          owner = (address)mark.locker(); // save the address of the Lock word
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
        // implied else: no owner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
        // this object has a heavyweight monitor
57777
90ead0febf56 8229258: Rework markOop and markOopDesc into a simpler mark word value carrier
stefank
parents: 55416
diff changeset
   978
        mon = mark.monitor();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
        // The owner field of a heavyweight monitor may be NULL for no
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
        // owner, a JavaThread * or it may still be the address of the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
        // Lock word in a JavaThread's stack. A monitor can be inflated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
        // by a non-owning JavaThread, but only the owning JavaThread
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
        // can change the owner field from the Lock word to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
        // JavaThread * and it may not have done that yet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
        owner = (address)mon->owner();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
    if (owner != NULL) {
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
   991
      // Use current thread since function can be called from a
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
   992
      // JavaThread or the VMThread.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
   993
      ThreadsListHandle tlh;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
      // This monitor is owned so we have to find the owning JavaThread.
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
   995
      owning_thread = Threads::owning_thread_from_monitor_owner(tlh.list(), owner);
15853
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 15432
diff changeset
   996
      // Cannot assume (owning_thread != NULL) here because this function
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 15432
diff changeset
   997
      // may not have been called at a safepoint and the owning_thread
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 15432
diff changeset
   998
      // might not be suspended.
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 15432
diff changeset
   999
      if (owning_thread != NULL) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
        // The monitor's owner either has to be the current thread, at safepoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
        // or it has to be suspended. Any of these conditions will prevent both
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
        // contending and waiting threads from modifying the state of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
        // the monitor.
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1004
        if (!at_safepoint && !owning_thread->is_thread_fully_suspended(true, &debug_bits)) {
15853
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 15432
diff changeset
  1005
          // Don't worry! This return of JVMTI_ERROR_THREAD_NOT_SUSPENDED
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 15432
diff changeset
  1006
          // will not make it back to the JVM/TI agent. The error code will
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 15432
diff changeset
  1007
          // get intercepted in JvmtiEnv::GetObjectMonitorUsage() which
1c4e16950e96 8007476: assert(the_owner != NULL) failed: Did not find owning Java thread for lock word address
dcubed
parents: 15432
diff changeset
  1008
          // will retry the call via a VM_GetObjectMonitorUsage VM op.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
          return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
        HandleMark hm;
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
  1012
        Handle     th(current_thread, owning_thread->threadObj());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
        ret.owner = (jthread)jni_reference(calling_thread, th);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
      // implied else: no owner
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1016
    } // ThreadsListHandle is destroyed here.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
    if (owning_thread != NULL) {  // monitor is owned
23518
00028482ef09 8036666: VMTI GetObjectMonitorUsage does not return correct recursion count
asiebenborn
parents: 15853
diff changeset
  1019
      // The recursions field of a monitor does not reflect recursions
00028482ef09 8036666: VMTI GetObjectMonitorUsage does not return correct recursion count
asiebenborn
parents: 15853
diff changeset
  1020
      // as lightweight locks before inflating the monitor are not included.
00028482ef09 8036666: VMTI GetObjectMonitorUsage does not return correct recursion count
asiebenborn
parents: 15853
diff changeset
  1021
      // We have to count the number of recursive monitor entries the hard way.
00028482ef09 8036666: VMTI GetObjectMonitorUsage does not return correct recursion count
asiebenborn
parents: 15853
diff changeset
  1022
      // We pass a handle to survive any GCs along the way.
00028482ef09 8036666: VMTI GetObjectMonitorUsage does not return correct recursion count
asiebenborn
parents: 15853
diff changeset
  1023
      ResourceMark rm;
00028482ef09 8036666: VMTI GetObjectMonitorUsage does not return correct recursion count
asiebenborn
parents: 15853
diff changeset
  1024
      ret.entry_count = count_locked_objects(owning_thread, hobj);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
    // implied else: entry_count == 0
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
33589
7cbd1b2c139b 8139040: Fix initializations before ShouldNotReachHere() etc. and enable -Wuninitialized on linux.
goetz
parents: 31024
diff changeset
  1029
  jint nWant = 0, nWait = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
  if (mon != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
    // this object has a heavyweight monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
    nWant = mon->contentions(); // # of threads contending for monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
    nWait = mon->waiters();     // # of threads in Object.wait()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
    ret.waiter_count = nWant + nWait;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1035
    ret.notify_waiter_count = nWait;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1036
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1037
    // this object has a lightweight monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1038
    ret.waiter_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
    ret.notify_waiter_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1040
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1041
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1042
  // Allocate memory for heavyweight and lightweight monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1043
  jvmtiError err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
  err = allocate(ret.waiter_count * sizeof(jthread *), (unsigned char**)&ret.waiters);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
  if (err != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
    return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
  err = allocate(ret.notify_waiter_count * sizeof(jthread *),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
                 (unsigned char**)&ret.notify_waiters);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
  if (err != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1051
    deallocate((unsigned char*)ret.waiters);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1052
    return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1053
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1054
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1055
  // now derive the rest of the fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1056
  if (mon != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1057
    // this object has a heavyweight monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1058
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1059
    // Number of waiters may actually be less than the waiter count.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1060
    // So NULL out memory so that unused memory will be NULL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1061
    memset(ret.waiters, 0, ret.waiter_count * sizeof(jthread *));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1062
    memset(ret.notify_waiters, 0, ret.notify_waiter_count * sizeof(jthread *));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1063
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1064
    if (ret.waiter_count > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1065
      // we have contending and/or waiting threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1066
      HandleMark hm;
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1067
      // Use current thread since function can be called from a
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1068
      // JavaThread or the VMThread.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1069
      ThreadsListHandle tlh;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1070
      if (nWant > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1071
        // we have contending threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1072
        ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1073
        // get_pending_threads returns only java thread so we do not need to
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1074
        // check for non java threads.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1075
        GrowableArray<JavaThread*>* wantList = Threads::get_pending_threads(tlh.list(), nWant, (address)mon);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1076
        if (wantList->length() < nWant) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1077
          // robustness: the pending list has gotten smaller
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1078
          nWant = wantList->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1079
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1080
        for (int i = 0; i < nWant; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1081
          JavaThread *pending_thread = wantList->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1082
          // If the monitor has no owner, then a non-suspended contending
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1083
          // thread could potentially change the state of the monitor by
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1084
          // entering it. The JVM/TI spec doesn't allow this.
55416
09ee0bd26bda 8217348: assert(thread->is_Java_thread()) failed: just checking
dtitov
parents: 54347
diff changeset
  1085
          if (owning_thread == NULL && !at_safepoint &&
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1086
              !pending_thread->is_thread_fully_suspended(true, &debug_bits)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1087
            if (ret.owner != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1088
              destroy_jni_reference(calling_thread, ret.owner);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1089
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1090
            for (int j = 0; j < i; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1091
              destroy_jni_reference(calling_thread, ret.waiters[j]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1092
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1093
            deallocate((unsigned char*)ret.waiters);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1094
            deallocate((unsigned char*)ret.notify_waiters);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1095
            return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1096
          }
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
  1097
          Handle th(current_thread, pending_thread->threadObj());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1098
          ret.waiters[i] = (jthread)jni_reference(calling_thread, th);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1099
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1100
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1101
      if (nWait > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1102
        // we have threads in Object.wait()
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
        int offset = nWant;  // add after any contending threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1104
        ObjectWaiter *waiter = mon->first_waiter();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1105
        for (int i = 0, j = 0; i < nWait; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1106
          if (waiter == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1107
            // robustness: the waiting list has gotten smaller
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1108
            nWait = j;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1109
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1110
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1111
          Thread *t = mon->thread_of_waiter(waiter);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1112
          if (t != NULL && t->is_Java_thread()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1113
            JavaThread *wjava_thread = (JavaThread *)t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1114
            // If the thread was found on the ObjectWaiter list, then
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1115
            // it has not been notified. This thread can't change the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1116
            // state of the monitor so it doesn't need to be suspended.
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
  1117
            Handle th(current_thread, wjava_thread->threadObj());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1118
            ret.waiters[offset + j] = (jthread)jni_reference(calling_thread, th);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1119
            ret.notify_waiters[j++] = (jthread)jni_reference(calling_thread, th);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1120
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1121
          waiter = mon->next_waiter(waiter);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1122
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1123
      }
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1124
    } // ThreadsListHandle is destroyed here.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1125
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1126
    // Adjust count. nWant and nWait count values may be less than original.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1127
    ret.waiter_count = nWant + nWait;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
    ret.notify_waiter_count = nWait;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1129
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1130
    // this object has a lightweight monitor and we have nothing more
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1131
    // to do here because the defaults are just fine.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1132
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1133
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1134
  // we don't update return parameter unless everything worked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1135
  *info_ptr = ret;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1136
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1137
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1138
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1139
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1140
ResourceTracker::ResourceTracker(JvmtiEnv* env) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1141
  _env = env;
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 10546
diff changeset
  1142
  _allocations = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<unsigned char*>(20, true);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1143
  _failed = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1144
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1145
ResourceTracker::~ResourceTracker() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1146
  if (_failed) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1147
    for (int i=0; i<_allocations->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1148
      _env->deallocate(_allocations->at(i));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1149
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1150
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1151
  delete _allocations;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1152
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1153
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1154
jvmtiError ResourceTracker::allocate(jlong size, unsigned char** mem_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1155
  unsigned char *ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1156
  jvmtiError err = _env->allocate(size, &ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1157
  if (err == JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1158
    _allocations->append(ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1159
    *mem_ptr = ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1160
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1161
    *mem_ptr = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1162
    _failed = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1163
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1164
  return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1165
 }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1166
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1167
unsigned char* ResourceTracker::allocate(jlong size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1168
  unsigned char* ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1169
  allocate(size, &ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1170
  return ptr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1171
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1173
char* ResourceTracker::strdup(const char* str) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1174
  char *dup_str = (char*)allocate(strlen(str)+1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
  if (dup_str != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
    strcpy(dup_str, str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1178
  return dup_str;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1179
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1180
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
struct StackInfoNode {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1182
  struct StackInfoNode *next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1183
  jvmtiStackInfo info;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1184
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
// Create a jvmtiStackInfo inside a linked list node and create a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1187
// buffer for the frame information, both allocated as resource objects.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
// Fill in both the jvmtiStackInfo and the jvmtiFrameInfo.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
// Note that either or both of thr and thread_oop
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1190
// may be null if the thread is new or has exited.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
VM_GetMultipleStackTraces::fill_frames(jthread jt, JavaThread *thr, oop thread_oop) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1195
  jint state = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
  struct StackInfoNode *node = NEW_RESOURCE_OBJ(struct StackInfoNode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1197
  jvmtiStackInfo *infop = &(node->info);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
  node->next = head();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
  set_head(node);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
  infop->frame_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
  infop->thread = jt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1202
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1203
  if (thread_oop != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1204
    // get most state bits
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1205
    state = (jint)java_lang_Thread::get_thread_status(thread_oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1207
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1208
  if (thr != NULL) {    // add more state bits if there is a JavaThead to query
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1209
    // same as is_being_ext_suspended() but without locking
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1210
    if (thr->is_ext_suspended() || thr->is_external_suspend()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
      state |= JVMTI_THREAD_STATE_SUSPENDED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1212
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1213
    JavaThreadState jts = thr->thread_state();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1214
    if (jts == _thread_in_native) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1215
      state |= JVMTI_THREAD_STATE_IN_NATIVE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1216
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1217
    OSThread* osThread = thr->osthread();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
    if (osThread != NULL && osThread->interrupted()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
      state |= JVMTI_THREAD_STATE_INTERRUPTED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1222
  infop->state = state;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1223
51070
2f4c3cac8556 8206977: Minor improvements of runtime code.
goetz
parents: 49816
diff changeset
  1224
  if (thr != NULL && (state & JVMTI_THREAD_STATE_ALIVE) != 0) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
    infop->frame_buffer = NEW_RESOURCE_ARRAY(jvmtiFrameInfo, max_frame_count());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1226
    env()->get_stack_trace(thr, 0, max_frame_count(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1227
                           infop->frame_buffer, &(infop->frame_count));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
    infop->frame_buffer = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
    infop->frame_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1231
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1232
  _frame_count_total += infop->frame_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1233
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1234
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
// Based on the stack information in the linked list, allocate memory
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
// block to return and fill it from the info in the linked list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
VM_GetMultipleStackTraces::allocate_and_fill_stacks(jint thread_count) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
  // do I need to worry about alignment issues?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
  jlong alloc_size =  thread_count       * sizeof(jvmtiStackInfo)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
                    + _frame_count_total * sizeof(jvmtiFrameInfo);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
  env()->allocate(alloc_size, (unsigned char **)&_stack_info);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
  // pointers to move through the newly allocated space as it is filled in
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
  jvmtiStackInfo *si = _stack_info + thread_count;      // bottom of stack info
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
  jvmtiFrameInfo *fi = (jvmtiFrameInfo *)si;            // is the top of frame info
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
  // copy information in resource area into allocated buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
  // insert stack info backwards since linked list is backwards
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
  // insert frame info forwards
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
  // walk the StackInfoNodes
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1252
  for (struct StackInfoNode *sin = head(); sin != NULL; sin = sin->next) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1253
    jint frame_count = sin->info.frame_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1254
    size_t frames_size = frame_count * sizeof(jvmtiFrameInfo);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
    --si;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1256
    memcpy(si, &(sin->info), sizeof(jvmtiStackInfo));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1257
    if (frames_size == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1258
      si->frame_buffer = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1260
      memcpy(fi, sin->info.frame_buffer, frames_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
      si->frame_buffer = fi;  // point to the new allocated copy of the frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
      fi += frame_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
  assert(si == _stack_info, "the last copied stack info must be the first record");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1266
  assert((unsigned char *)fi == ((unsigned char *)_stack_info) + alloc_size,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1267
         "the last copied frame info must be the last record");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1268
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1269
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1270
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1271
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
VM_GetThreadListStackTraces::doit() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
  ResourceMark rm;
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1276
  ThreadsListHandle tlh;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
  for (int i = 0; i < _thread_count; ++i) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
    jthread jt = _thread_list[i];
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1279
    JavaThread* java_thread = NULL;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1280
    oop thread_oop = NULL;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1281
    jvmtiError err = JvmtiExport::cv_external_thread_to_JavaThread(tlh.list(), jt, &java_thread, &thread_oop);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1282
    if (err != JVMTI_ERROR_NONE) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1283
      // We got an error code so we don't have a JavaThread *, but
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1284
      // only return an error from here if we didn't get a valid
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1285
      // thread_oop.
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1286
      if (thread_oop == NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1287
        set_result(err);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1288
        return;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1289
      }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1290
      // We have a valid thread_oop.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1291
    }
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1292
    fill_frames(jt, java_thread, thread_oop);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1293
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1294
  allocate_and_fill_stacks(_thread_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1295
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1297
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1298
VM_GetAllStackTraces::doit() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1299
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1300
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1301
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1302
  _final_thread_count = 0;
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1303
  for (JavaThreadIteratorWithHandle jtiwh; JavaThread *jt = jtiwh.next(); ) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1304
    oop thread_oop = jt->threadObj();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1305
    if (thread_oop != NULL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1306
        !jt->is_exiting() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1307
        java_lang_Thread::is_alive(thread_oop) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1308
        !jt->is_hidden_from_external_view()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1309
      ++_final_thread_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1310
      // Handle block of the calling thread is used to create local refs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1311
      fill_frames((jthread)JNIHandles::make_local(_calling_thread, thread_oop),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1312
                  jt, thread_oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1313
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1314
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1315
  allocate_and_fill_stacks(_final_thread_count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1316
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1317
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1318
// Verifies that the top frame is a java frame in an expected state.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1319
// Deoptimizes frame if needed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1320
// Checks that the frame method signature matches the return type (tos).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1321
// HandleMark must be defined in the caller only.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
// It is to keep a ret_ob_h handle alive after return to the caller.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1324
JvmtiEnvBase::check_top_frame(JavaThread* current_thread, JavaThread* java_thread,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1325
                              jvalue value, TosState tos, Handle* ret_ob_h) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
  ResourceMark rm(current_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1327
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
  vframe *vf = vframeFor(java_thread, 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
  NULL_CHECK(vf, JVMTI_ERROR_NO_MORE_FRAMES);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1330
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1331
  javaVFrame *jvf = (javaVFrame*) vf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1332
  if (!vf->is_java_frame() || jvf->method()->is_native()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1333
    return JVMTI_ERROR_OPAQUE_FRAME;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1334
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1335
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1336
  // If the frame is a compiled one, need to deoptimize it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1337
  if (vf->is_compiled_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1338
    if (!vf->fr().can_be_deoptimized()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1339
      return JVMTI_ERROR_OPAQUE_FRAME;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1340
    }
7106
867c9d296c6b 6968367: can_post_on_exceptions is still using VM_DeoptimizeFrame in some places
never
parents: 5547
diff changeset
  1341
    Deoptimization::deoptimize_frame(java_thread, jvf->fr().id());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1342
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1343
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1344
  // Get information about method return type
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 7413
diff changeset
  1345
  Symbol* signature = jvf->method()->signature();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1346
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1347
  ResultTypeFinder rtf(signature);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1348
  TosState fr_tos = as_TosState(rtf.type());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1349
  if (fr_tos != tos) {
37480
291ee208fb72 8132051: Better byte behavior
coleenp
parents: 37248
diff changeset
  1350
    if (tos != itos || (fr_tos != btos && fr_tos != ztos && fr_tos != ctos && fr_tos != stos)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1351
      return JVMTI_ERROR_TYPE_MISMATCH;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1352
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1353
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1354
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1355
  // Check that the jobject class matches the return type signature.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1356
  jobject jobj = value.l;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1357
  if (tos == atos && jobj != NULL) { // NULL reference is allowed
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
  1358
    Handle ob_h(current_thread, JNIHandles::resolve_external_guard(jobj));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1359
    NULL_CHECK(ob_h, JVMTI_ERROR_INVALID_OBJECT);
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 46271
diff changeset
  1360
    Klass* ob_k = ob_h()->klass();
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 46271
diff changeset
  1361
    NULL_CHECK(ob_k, JVMTI_ERROR_INVALID_OBJECT);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1362
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1363
    // Method return type signature.
58722
cba8afa5cfed 8231844: Enhance type signature characters in classfile_constants.h and improve the JVM to use type signature characters more consistently
lfoltan
parents: 58488
diff changeset
  1364
    char* ty_sign = 1 + strchr(signature->as_C_string(), JVM_SIGNATURE_ENDFUNC);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1365
46329
53ccc37bda19 8155672: Remove instanceKlassHandles and KlassHandles
coleenp
parents: 46271
diff changeset
  1366
    if (!VM_GetOrSetLocal::is_assignable(ty_sign, ob_k, current_thread)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1367
      return JVMTI_ERROR_TYPE_MISMATCH;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1368
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1369
    *ret_ob_h = ob_h;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1370
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1371
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1372
} /* end check_top_frame */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1373
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1374
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1375
// ForceEarlyReturn<type> follows the PopFrame approach in many aspects.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1376
// Main difference is on the last stage in the interpreter.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1377
// The PopFrame stops method execution to continue execution
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1378
// from the same method call instruction.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1379
// The ForceEarlyReturn forces return from method so the execution
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1380
// continues at the bytecode following the method call.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1381
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1382
// Threads_lock NOT held, java_thread not protected by lock
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1383
// java_thread - pre-checked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1384
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1385
jvmtiError
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1386
JvmtiEnvBase::force_early_return(JavaThread* java_thread, jvalue value, TosState tos) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1387
  JavaThread* current_thread = JavaThread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1388
  HandleMark   hm(current_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1389
  uint32_t debug_bits = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1390
2135
f82c3012ec86 6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents: 1623
diff changeset
  1391
  // retrieve or create the state
f82c3012ec86 6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents: 1623
diff changeset
  1392
  JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);
f82c3012ec86 6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents: 1623
diff changeset
  1393
  if (state == NULL) {
f82c3012ec86 6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents: 1623
diff changeset
  1394
    return JVMTI_ERROR_THREAD_NOT_ALIVE;
f82c3012ec86 6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents: 1623
diff changeset
  1395
  }
f82c3012ec86 6800721: 3/4 JavaThread::jvmti_thread_state() and JvmtiThreadState::state_for() robustness
dcubed
parents: 1623
diff changeset
  1396
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1397
  // Check if java_thread is fully suspended
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1398
  if (!java_thread->is_thread_fully_suspended(true /* wait for suspend completion */, &debug_bits)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1399
    return JVMTI_ERROR_THREAD_NOT_SUSPENDED;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1400
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1401
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1402
  // Check to see if a ForceEarlyReturn was already in progress
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1403
  if (state->is_earlyret_pending()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1404
    // Probably possible for JVMTI clients to trigger this, but the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1405
    // JPDA backend shouldn't allow this to happen
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1406
    return JVMTI_ERROR_INTERNAL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1407
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1408
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1409
    // The same as for PopFrame. Workaround bug:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1410
    //  4812902: popFrame hangs if the method is waiting at a synchronize
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1411
    // Catch this condition and return an error to avoid hanging.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1412
    // Now JVMTI spec allows an implementation to bail out with an opaque
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1413
    // frame error.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1414
    OSThread* osThread = java_thread->osthread();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1415
    if (osThread->get_state() == MONITOR_WAIT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1416
      return JVMTI_ERROR_OPAQUE_FRAME;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1417
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1418
  }
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
  1419
  Handle ret_ob_h;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1420
  jvmtiError err = check_top_frame(current_thread, java_thread, value, tos, &ret_ob_h);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1421
  if (err != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1422
    return err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1423
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1424
  assert(tos != atos || value.l == NULL || ret_ob_h() != NULL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1425
         "return object oop must not be NULL if jobject is not NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1426
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1427
  // Update the thread state to reflect that the top frame must be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1428
  // forced to return.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1429
  // The current frame will be returned later when the suspended
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1430
  // thread is resumed and right before returning from VM to Java.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1431
  // (see call_VM_base() in assembler_<cpu>.cpp).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1432
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1433
  state->set_earlyret_pending();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1434
  state->set_earlyret_oop(ret_ob_h());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1435
  state->set_earlyret_value(value, tos);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1436
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1437
  // Set pending step flag for this early return.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1438
  // It is cleared when next step event is posted.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1439
  state->set_pending_step_for_earlyret();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1440
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1441
  return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1442
} /* end force_early_return */
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1443
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1444
void
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1445
JvmtiMonitorClosure::do_monitor(ObjectMonitor* mon) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1446
  if ( _error != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1447
    // Error occurred in previous iteration so no need to add
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1448
    // to the list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1449
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1450
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1451
  if (mon->owner() == _java_thread ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1452
    // Filter out on stack monitors collected during stack walk.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1453
    oop obj = (oop)mon->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1454
    bool found = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1455
    for (int j = 0; j < _owned_monitors_list->length(); j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1456
      jobject jobj = ((jvmtiMonitorStackDepthInfo*)_owned_monitors_list->at(j))->monitor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1457
      oop check = JNIHandles::resolve(jobj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1458
      if (check == obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1459
        // On stack monitor already collected during the stack walk.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1460
        found = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1461
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1462
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1463
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1464
    if (found == false) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1465
      // This is off stack monitor (e.g. acquired via jni MonitorEnter).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1466
      jvmtiError err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1467
      jvmtiMonitorStackDepthInfo *jmsdi;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1468
      err = _env->allocate(sizeof(jvmtiMonitorStackDepthInfo), (unsigned char **)&jmsdi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1469
      if (err != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1470
        _error = err;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1471
        return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1472
      }
46271
979ebd346ecf 8169881: Remove implicit Handle conversions oop->Handle
coleenp
parents: 37480
diff changeset
  1473
      Handle hobj(Thread::current(), obj);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1474
      jmsdi->monitor = _env->jni_reference(_calling_thread, hobj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1475
      // stack depth is unknown for this monitor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1476
      jmsdi->stack_depth = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1477
      _owned_monitors_list->append(jmsdi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1478
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1479
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1480
}
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1481
46961
c9094b1e5f87 8186088: ConstantPoolCache::_resolved_references is not a JNIHandle
coleenp
parents: 46329
diff changeset
  1482
GrowableArray<OopHandle>* JvmtiModuleClosure::_tbl = NULL;
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1483
49816
a3e79f97e86b 8200555: OopHandle should use Access API
coleenp
parents: 49480
diff changeset
  1484
void JvmtiModuleClosure::do_module(ModuleEntry* entry) {
a3e79f97e86b 8200555: OopHandle should use Access API
coleenp
parents: 49480
diff changeset
  1485
  assert_locked_or_safepoint(Module_lock);
a3e79f97e86b 8200555: OopHandle should use Access API
coleenp
parents: 49480
diff changeset
  1486
  OopHandle module = entry->module_handle();
a3e79f97e86b 8200555: OopHandle should use Access API
coleenp
parents: 49480
diff changeset
  1487
  guarantee(module.resolve() != NULL, "module object is NULL");
a3e79f97e86b 8200555: OopHandle should use Access API
coleenp
parents: 49480
diff changeset
  1488
  _tbl->push(module);
a3e79f97e86b 8200555: OopHandle should use Access API
coleenp
parents: 49480
diff changeset
  1489
}
a3e79f97e86b 8200555: OopHandle should use Access API
coleenp
parents: 49480
diff changeset
  1490
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1491
jvmtiError
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1492
JvmtiModuleClosure::get_all_modules(JvmtiEnv* env, jint* module_count_ptr, jobject** modules_ptr) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1493
  ResourceMark rm;
51608
625a5bdde0c5 8210155: Lock ClassLoaderDataGraph
coleenp
parents: 51070
diff changeset
  1494
  MutexLocker mcld(ClassLoaderDataGraph_lock);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1495
  MutexLocker ml(Module_lock);
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1496
46961
c9094b1e5f87 8186088: ConstantPoolCache::_resolved_references is not a JNIHandle
coleenp
parents: 46329
diff changeset
  1497
  _tbl = new GrowableArray<OopHandle>(77);
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1498
  if (_tbl == NULL) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1499
    return JVMTI_ERROR_OUT_OF_MEMORY;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1500
  }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1501
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1502
  // Iterate over all the modules loaded to the system.
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1503
  ClassLoaderDataGraph::modules_do(&do_module);
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1504
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1505
  jint len = _tbl->length();
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1506
  guarantee(len > 0, "at least one module must be present");
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1507
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1508
  jobject* array = (jobject*)env->jvmtiMalloc((jlong)(len * sizeof(jobject)));
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1509
  if (array == NULL) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1510
    return JVMTI_ERROR_OUT_OF_MEMORY;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1511
  }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1512
  for (jint idx = 0; idx < len; idx++) {
46961
c9094b1e5f87 8186088: ConstantPoolCache::_resolved_references is not a JNIHandle
coleenp
parents: 46329
diff changeset
  1513
    array[idx] = JNIHandles::make_local(Thread::current(), _tbl->at(idx).resolve());
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1514
  }
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1515
  _tbl = NULL;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1516
  *modules_ptr = array;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1517
  *module_count_ptr = len;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1518
  return JVMTI_ERROR_NONE;
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1519
}
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 35492
diff changeset
  1520
48105
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1521
void
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1522
VM_UpdateForPopTopFrame::doit() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1523
  JavaThread* jt = _state->get_thread();
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1524
  ThreadsListHandle tlh;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1525
  if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1526
    _state->update_for_pop_top_frame();
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1527
  } else {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1528
    _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1529
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1530
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1531
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1532
void
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1533
VM_SetFramePop::doit() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1534
  JavaThread* jt = _state->get_thread();
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1535
  ThreadsListHandle tlh;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1536
  if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1537
    int frame_number = _state->count_frames() - _depth;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1538
    _state->env_thread_state((JvmtiEnvBase*)_env)->set_frame_pop(frame_number);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1539
  } else {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1540
    _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1541
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1542
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1543
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1544
void
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1545
VM_GetOwnedMonitorInfo::doit() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1546
  _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1547
  ThreadsListHandle tlh;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1548
  if (_java_thread != NULL && tlh.includes(_java_thread)
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1549
      && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1550
    _result = ((JvmtiEnvBase *)_env)->get_owned_monitors(_calling_thread, _java_thread,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1551
                                                          _owned_monitors_list);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1552
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1553
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1554
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1555
void
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1556
VM_GetCurrentContendedMonitor::doit() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1557
  _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1558
  ThreadsListHandle tlh;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1559
  if (_java_thread != NULL && tlh.includes(_java_thread)
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1560
      && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1561
    _result = ((JvmtiEnvBase *)_env)->get_current_contended_monitor(_calling_thread,_java_thread,_owned_monitor_ptr);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1562
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1563
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1564
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1565
void
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1566
VM_GetStackTrace::doit() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1567
  _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1568
  ThreadsListHandle tlh;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1569
  if (_java_thread != NULL && tlh.includes(_java_thread)
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1570
      && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1571
    _result = ((JvmtiEnvBase *)_env)->get_stack_trace(_java_thread,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1572
                                                      _start_depth, _max_count,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1573
                                                      _frame_buffer, _count_ptr);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1574
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1575
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1576
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1577
void
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1578
VM_GetFrameCount::doit() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1579
  _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1580
  JavaThread* jt = _state->get_thread();
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1581
  ThreadsListHandle tlh;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1582
  if (jt != NULL && tlh.includes(jt) && !jt->is_exiting() && jt->threadObj() != NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1583
    _result = ((JvmtiEnvBase*)_env)->get_frame_count(_state, _count_ptr);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1584
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1585
}
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1586
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1587
void
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1588
VM_GetFrameLocation::doit() {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1589
  _result = JVMTI_ERROR_THREAD_NOT_ALIVE;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1590
  ThreadsListHandle tlh;
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1591
  if (_java_thread != NULL && tlh.includes(_java_thread)
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1592
      && !_java_thread->is_exiting() && _java_thread->threadObj() != NULL) {
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1593
    _result = ((JvmtiEnvBase*)_env)->get_frame_location(_java_thread, _depth,
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1594
                                                        _method_ptr, _location_ptr);
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1595
  }
8d15b1369c7a 8167108: inconsistent handling of SR_lock can lead to crashes
dcubed
parents: 47765
diff changeset
  1596
}