hotspot/src/share/vm/prims/jvmtiTagMap.cpp
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 1 489c9b5090e2
child 360 21d113ecbf6a
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_jvmtiTagMap.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
// JvmtiTagHashmapEntry
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// Each entry encapsulates a JNI weak reference to the tagged object
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// and the tag value. In addition an entry includes a next pointer which
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// is used to chain entries together.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
class JvmtiTagHashmapEntry : public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  friend class JvmtiTagMap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  jweak _object;                        // JNI weak ref to tagged object
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  jlong _tag;                           // the tag
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  JvmtiTagHashmapEntry* _next;          // next on the list
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  inline void init(jweak object, jlong tag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
    _object = object;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
    _tag = tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
    _next = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  // constructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  JvmtiTagHashmapEntry(jweak object, jlong tag)         { init(object, tag); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  // accessor methods
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  inline jweak object() const                           { return _object; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  inline jlong tag() const                              { return _tag; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  inline void set_tag(jlong tag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    assert(tag != 0, "can't be zero");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    _tag = tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  inline JvmtiTagHashmapEntry* next() const             { return _next; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  inline void set_next(JvmtiTagHashmapEntry* next)      { _next = next; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
// JvmtiTagHashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
// A hashmap is essentially a table of pointers to entries. Entries
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
// are hashed to a location, or position in the table, and then
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
// chained from that location. The "key" for hashing is address of
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
// the object, or oop. The "value" is the JNI weak reference to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
// object and the tag value. Keys are not stored with the entry.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
// Instead the weak reference is resolved to obtain the key.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
// A hashmap maintains a count of the number entries in the hashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
// and resizes if the number of entries exceeds a given threshold.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
// The threshold is specified as a percentage of the size - for
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
// example a threshold of 0.75 will trigger the hashmap to resize
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
// if the number of entries is >75% of table size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
// A hashmap provides functions for adding, removing, and finding
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
// entries. It also provides a function to iterate over all entries
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
// in the hashmap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
class JvmtiTagHashmap : public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  friend class JvmtiTagMap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    small_trace_threshold  = 10000,                  // threshold for tracing
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    medium_trace_threshold = 100000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
    large_trace_threshold  = 1000000,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    initial_trace_threshold = small_trace_threshold
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  static int _sizes[];                  // array of possible hashmap sizes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  int _size;                            // actual size of the table
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  int _size_index;                      // index into size table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  int _entry_count;                     // number of entries in the hashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  float _load_factor;                   // load factor as a % of the size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  int _resize_threshold;                // computed threshold to trigger resizing.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  bool _resizing_enabled;               // indicates if hashmap can resize
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  int _trace_threshold;                 // threshold for trace messages
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  JvmtiTagHashmapEntry** _table;        // the table of entries.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  // private accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  int resize_threshold() const                  { return _resize_threshold; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  int trace_threshold() const                   { return _trace_threshold; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  // initialize the hashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  void init(int size_index=0, float load_factor=4.0f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    int initial_size =  _sizes[size_index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    _size_index = size_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    _size = initial_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    _entry_count = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
    if (TraceJVMTIObjectTagging) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
      _trace_threshold = initial_trace_threshold;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      _trace_threshold = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
    _load_factor = load_factor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    _resize_threshold = (int)(_load_factor * _size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    _resizing_enabled = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    size_t s = initial_size * sizeof(JvmtiTagHashmapEntry*);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    _table = (JvmtiTagHashmapEntry**)os::malloc(s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    if (_table == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
      vm_exit_out_of_memory(s, "unable to allocate initial hashtable for jvmti object tags");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    for (int i=0; i<initial_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
      _table[i] = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  // hash a given key (oop) with the specified size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  static unsigned int hash(oop key, int size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
    // shift right to get better distribution (as these bits will be zero
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
    // with aligned addresses)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    unsigned int addr = (unsigned int)((intptr_t)key);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
#ifdef _LP64
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    return (addr >> 3) % size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
#else
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
    return (addr >> 2) % size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  // hash a given key (oop)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  unsigned int hash(oop key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    return hash(key, _size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  // resize the hashmap - allocates a large table and re-hashes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
  // all entries into the new table.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  void resize() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    int new_size_index = _size_index+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    int new_size = _sizes[new_size_index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    if (new_size < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
      // hashmap already at maximum capacity
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
    // allocate new table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
    size_t s = new_size * sizeof(JvmtiTagHashmapEntry*);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    JvmtiTagHashmapEntry** new_table = (JvmtiTagHashmapEntry**)os::malloc(s);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
    if (new_table == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
      warning("unable to allocate larger hashtable for jvmti object tags");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
      set_resizing_enabled(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
    // initialize new table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
    for (i=0; i<new_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
      new_table[i] = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    // rehash all entries into the new table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    for (i=0; i<_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
      JvmtiTagHashmapEntry* entry = _table[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
      while (entry != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
        JvmtiTagHashmapEntry* next = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
        oop key = JNIHandles::resolve(entry->object());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
        assert(key != NULL, "jni weak reference cleared!!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
        unsigned int h = hash(key, new_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
        JvmtiTagHashmapEntry* anchor = new_table[h];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
        if (anchor == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
          new_table[h] = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
          entry->set_next(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
          entry->set_next(anchor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
          new_table[h] = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
        entry = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
    // free old table and update settings.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    os::free((void*)_table);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    _table = new_table;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
    _size_index = new_size_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
    _size = new_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
    // compute new resize threshold
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
    _resize_threshold = (int)(_load_factor * _size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
  // internal remove function - remove an entry at a given position in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
  // table.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  inline void remove(JvmtiTagHashmapEntry* prev, int pos, JvmtiTagHashmapEntry* entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
    assert(pos >= 0 && pos < _size, "out of range");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
    if (prev == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
      _table[pos] = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
      prev->set_next(entry->next());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    assert(_entry_count > 0, "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
    _entry_count--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  // resizing switch
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  bool is_resizing_enabled() const          { return _resizing_enabled; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
  void set_resizing_enabled(bool enable)    { _resizing_enabled = enable; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
  // debugging
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  void print_memory_usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  void compute_next_trace_threshold();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  // create a JvmtiTagHashmap of a preferred size and optionally a load factor.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  // The preferred size is rounded down to an actual size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  JvmtiTagHashmap(int size, float load_factor=0.0f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
    int i=0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
    while (_sizes[i] < size) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
      if (_sizes[i] < 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
        assert(i > 0, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
        i--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
      i++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
    // if a load factor is specified then use it, otherwise use default
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
    if (load_factor > 0.01f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
      init(i, load_factor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
      init(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  // create a JvmtiTagHashmap with default settings
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  JvmtiTagHashmap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
    init();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
  // release table when JvmtiTagHashmap destroyed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
  ~JvmtiTagHashmap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
    if (_table != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
      os::free((void*)_table);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
      _table = NULL;
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
  // accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  int size() const                              { return _size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  JvmtiTagHashmapEntry** table() const          { return _table; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  int entry_count() const                       { return _entry_count; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  // find an entry in the hashmap, returns NULL if not found.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
  inline JvmtiTagHashmapEntry* find(oop key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
    unsigned int h = hash(key);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
    JvmtiTagHashmapEntry* entry = _table[h];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
    while (entry != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
      oop orig_key = JNIHandles::resolve(entry->object());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
      assert(orig_key != NULL, "jni weak reference cleared!!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
      if (key == orig_key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
      entry = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
    return entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  // add a new entry to hashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  inline void add(oop key, JvmtiTagHashmapEntry* entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
    assert(key != NULL, "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
    assert(find(key) == NULL, "duplicate detected");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    unsigned int h = hash(key);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    JvmtiTagHashmapEntry* anchor = _table[h];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
    if (anchor == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
      _table[h] = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
      entry->set_next(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
      entry->set_next(anchor);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
      _table[h] = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    _entry_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    if (trace_threshold() > 0 && entry_count() >= trace_threshold()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
      assert(TraceJVMTIObjectTagging, "should only get here when tracing");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
      print_memory_usage();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
      compute_next_trace_threshold();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
    // if the number of entries exceed the threshold then resize
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    if (entry_count() > resize_threshold() && is_resizing_enabled()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
      resize();
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
  // remove an entry with the given key.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
  inline JvmtiTagHashmapEntry* remove(oop key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    unsigned int h = hash(key);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
    JvmtiTagHashmapEntry* entry = _table[h];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
    JvmtiTagHashmapEntry* prev = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
    while (entry != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
      oop orig_key = JNIHandles::resolve(entry->object());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
      assert(orig_key != NULL, "jni weak reference cleared!!");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
      if (key == orig_key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
        break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
      prev = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
      entry = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    if (entry != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
      remove(prev, h, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    return entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
  // iterate over all entries in the hashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
  void entry_iterate(JvmtiTagHashmapEntryClosure* closure);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
// possible hashmap sizes - odd primes that roughly double in size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
// To avoid excessive resizing the odd primes from 4801-76831 and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
// 76831-307261 have been removed. The list must be terminated by -1.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
int JvmtiTagHashmap::_sizes[] =  { 4801, 76831, 307261, 614563, 1228891,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    2457733, 4915219, 9830479, 19660831, 39321619, 78643219, -1 };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
// A supporting class for iterating over all entries in Hashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
class JvmtiTagHashmapEntryClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
  virtual void do_entry(JvmtiTagHashmapEntry* entry) = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
// iterate over all entries in the hashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
void JvmtiTagHashmap::entry_iterate(JvmtiTagHashmapEntryClosure* closure) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  for (int i=0; i<_size; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
    JvmtiTagHashmapEntry* entry = _table[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
    JvmtiTagHashmapEntry* prev = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
    while (entry != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
      // obtain the next entry before invoking do_entry - this is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
      // necessary because do_entry may remove the entry from the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
      // hashmap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
      JvmtiTagHashmapEntry* next = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
      closure->do_entry(entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
      entry = next;
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
// debugging
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
void JvmtiTagHashmap::print_memory_usage() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
  intptr_t p = (intptr_t)this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  tty->print("[JvmtiTagHashmap @ " INTPTR_FORMAT, p);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  // table + entries in KB
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  int hashmap_usage = (size()*sizeof(JvmtiTagHashmapEntry*) +
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    entry_count()*sizeof(JvmtiTagHashmapEntry))/K;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
  int weak_globals_usage = (int)(JNIHandles::weak_global_handle_memory_usage()/K);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
  tty->print_cr(", %d entries (%d KB) <JNI weak globals: %d KB>]",
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    entry_count(), hashmap_usage, weak_globals_usage);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
// compute threshold for the next trace message
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
void JvmtiTagHashmap::compute_next_trace_threshold() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  if (trace_threshold() < medium_trace_threshold) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    _trace_threshold += small_trace_threshold;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    if (trace_threshold() < large_trace_threshold) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
      _trace_threshold += medium_trace_threshold;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
      _trace_threshold += large_trace_threshold;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
// memory region for young generation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
MemRegion JvmtiTagMap::_young_gen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
// get the memory region used for the young generation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
void JvmtiTagMap::get_young_generation() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
  if (Universe::heap()->kind() == CollectedHeap::GenCollectedHeap) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
    GenCollectedHeap* gch = GenCollectedHeap::heap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
    _young_gen = gch->get_gen(0)->reserved();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
#ifndef SERIALGC
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
    ParallelScavengeHeap* psh = ParallelScavengeHeap::heap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
    _young_gen= psh->young_gen()->reserved();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
#else  // SERIALGC
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
    fatal("SerialGC only supported in this configuration.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
#endif // SERIALGC
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
// returns true if oop is in the young generation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
inline bool JvmtiTagMap::is_in_young(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
  assert(_young_gen.start() != NULL, "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
  void* p = (void*)o;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
  bool in_young = _young_gen.contains(p);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
  return in_young;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
// returns the appropriate hashmap for a given object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
inline JvmtiTagHashmap* JvmtiTagMap::hashmap_for(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
  if (is_in_young(o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
    return _hashmap[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
    return _hashmap[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
// create a JvmtiTagMap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
JvmtiTagMap::JvmtiTagMap(JvmtiEnv* env) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
  _env(env),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
  _lock(Mutex::nonleaf+2, "JvmtiTagMap._lock", false),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
  _free_entries(NULL),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
  _free_entries_count(0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
  assert(JvmtiThreadState_lock->is_locked(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
  assert(((JvmtiEnvBase *)env)->tag_map() == NULL, "tag map already exists for environment");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
  // create the hashmaps
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
  for (int i=0; i<n_hashmaps; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
    _hashmap[i] = new JvmtiTagHashmap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
  // get the memory region used by the young generation
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
  get_young_generation();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
  // finally add us to the environment
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
  ((JvmtiEnvBase *)env)->set_tag_map(this);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   454
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   455
489c9b5090e2 Initial load
duke
parents:
diff changeset
   456
489c9b5090e2 Initial load
duke
parents:
diff changeset
   457
// destroy a JvmtiTagMap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   458
JvmtiTagMap::~JvmtiTagMap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
  // no lock acquired as we assume the enclosing environment is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
  // also being destroryed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
  ((JvmtiEnvBase *)_env)->set_tag_map(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
  // iterate over the hashmaps and destroy each of the entries
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
  for (int i=0; i<n_hashmaps; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
    JvmtiTagHashmap* hashmap = _hashmap[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
    JvmtiTagHashmapEntry** table = hashmap->table();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
    for (int j=0; j<hashmap->size(); j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
      JvmtiTagHashmapEntry *entry = table[j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
      while (entry != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
        JvmtiTagHashmapEntry* next = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
        jweak ref = entry->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
        JNIHandles::destroy_weak_global(ref);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
        delete entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
        entry = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
    // finally destroy the hashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   480
    delete hashmap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   481
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
  // remove any entries on the free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
  JvmtiTagHashmapEntry* entry = _free_entries;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
  while (entry != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
    JvmtiTagHashmapEntry* next = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
    delete entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
    entry = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
// create a hashmap entry
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
// - if there's an entry on the (per-environment) free list then this
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
// is returned. Otherwise an new entry is allocated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
JvmtiTagHashmapEntry* JvmtiTagMap::create_entry(jweak ref, jlong tag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
  assert(Thread::current()->is_VM_thread() || is_locked(), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
  JvmtiTagHashmapEntry* entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
  if (_free_entries == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
    entry = new JvmtiTagHashmapEntry(ref, tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
    assert(_free_entries_count > 0, "mismatched _free_entries_count");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
    _free_entries_count--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
    entry = _free_entries;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
    _free_entries = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
    entry->init(ref, tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
  return entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
// destroy an entry by returning it to the free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
void JvmtiTagMap::destroy_entry(JvmtiTagHashmapEntry* entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
  assert(SafepointSynchronize::is_at_safepoint() || is_locked(), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
  // limit the size of the free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
  if (_free_entries_count >= max_free_entries) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
    delete entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
    entry->set_next(_free_entries);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
    _free_entries = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   519
    _free_entries_count++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   520
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
// returns the tag map for the given environments. If the tag map
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
// doesn't exist then it is created.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
JvmtiTagMap* JvmtiTagMap::tag_map_for(JvmtiEnv* env) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
  JvmtiTagMap* tag_map = ((JvmtiEnvBase *)env)->tag_map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
  if (tag_map == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
    MutexLocker mu(JvmtiThreadState_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
    tag_map = ((JvmtiEnvBase *)env)->tag_map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
    if (tag_map == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
      tag_map = new JvmtiTagMap(env);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
    CHECK_UNHANDLED_OOPS_ONLY(Thread::current()->clear_unhandled_oops());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
  return tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
// iterate over all entries in the tag map.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
void JvmtiTagMap::entry_iterate(JvmtiTagHashmapEntryClosure* closure) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
  for (int i=0; i<n_hashmaps; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
    JvmtiTagHashmap* hashmap = _hashmap[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
    hashmap->entry_iterate(closure);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
// returns true if the hashmaps are empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
bool JvmtiTagMap::is_empty() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
  assert(SafepointSynchronize::is_at_safepoint() || is_locked(), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
  assert(n_hashmaps == 2, "not implemented");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
  return ((_hashmap[0]->entry_count() == 0) && (_hashmap[1]->entry_count() == 0));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
// Return the tag value for an object, or 0 if the object is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
// not tagged
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
static inline jlong tag_for(JvmtiTagMap* tag_map, oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
  JvmtiTagHashmapEntry* entry = tag_map->hashmap_for(o)->find(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
  if (entry == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
    return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
    return entry->tag();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
// If the object is a java.lang.Class then return the klassOop,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
// otherwise return the original object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
static inline oop klassOop_if_java_lang_Class(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
  if (o->klass() == SystemDictionary::class_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
    if (!java_lang_Class::is_primitive(o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
      o = (oop)java_lang_Class::as_klassOop(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
      assert(o != NULL, "class for non-primitive mirror must exist");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
  return o;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   577
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
// A CallbackWrapper is a support class for querying and tagging an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
// around a callback to a profiler. The constructor does pre-callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
// work to get the tag value, klass tag value, ... and the destructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
// does the post-callback work of tagging or untagging the object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
// {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
//   CallbackWrapper wrapper(tag_map, o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
//   (*callback)(wrapper.klass_tag(), wrapper.obj_size(), wrapper.obj_tag_p(), ...)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
// } // wrapper goes out of scope here which results in the destructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
//      checking to see if the object has been tagged, untagged, or the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
//      tag value has changed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
class CallbackWrapper : public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  JvmtiTagMap* _tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
  JvmtiTagHashmap* _hashmap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
  JvmtiTagHashmapEntry* _entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
  oop _o;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
  jlong _obj_size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
  jlong _obj_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
  klassOop _klass;         // the object's class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
  jlong _klass_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
 protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
  JvmtiTagMap* tag_map() const      { return _tag_map; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
  // invoked post-callback to tag, untag, or update the tag of an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
  void inline post_callback_tag_update(oop o, JvmtiTagHashmap* hashmap,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
                                       JvmtiTagHashmapEntry* entry, jlong obj_tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
  CallbackWrapper(JvmtiTagMap* tag_map, oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
    assert(Thread::current()->is_VM_thread() || tag_map->is_locked(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
           "MT unsafe or must be VM thread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
    // for Classes the klassOop is tagged
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
    _o = klassOop_if_java_lang_Class(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
    // object size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
    _obj_size = _o->size() * wordSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
    // record the context
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
    _tag_map = tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
    _hashmap = tag_map->hashmap_for(_o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
    _entry = _hashmap->find(_o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
    // get object tag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
    _obj_tag = (_entry == NULL) ? 0 : _entry->tag();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
    // get the class and the class's tag value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
    if (_o == o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
      _klass = _o->klass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
      // if the object represents a runtime class then use the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
      // tag for java.lang.Class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
      _klass = SystemDictionary::class_klass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
    _klass_tag = tag_for(tag_map, _klass);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
  ~CallbackWrapper() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
    post_callback_tag_update(_o, _hashmap, _entry, _obj_tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
  inline jlong* obj_tag_p()                     { return &_obj_tag; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
  inline jlong obj_size() const                 { return _obj_size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
  inline jlong obj_tag() const                  { return _obj_tag; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
  inline klassOop klass() const                 { return _klass; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
  inline jlong klass_tag() const                { return _klass_tag; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
// callback post-callback to tag, untag, or update the tag of an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
void inline CallbackWrapper::post_callback_tag_update(oop o,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
                                                      JvmtiTagHashmap* hashmap,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
                                                      JvmtiTagHashmapEntry* entry,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
                                                      jlong obj_tag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
  if (entry == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
    if (obj_tag != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
      // callback has tagged the object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
      assert(Thread::current()->is_VM_thread(), "must be VMThread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
      HandleMark hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
      Handle h(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
      jweak ref = JNIHandles::make_weak_global(h);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
      entry = tag_map()->create_entry(ref, obj_tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
      hashmap->add(o, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
    // object was previously tagged - the callback may have untagged
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
    // the object or changed the tag value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
    if (obj_tag == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
      jweak ref = entry->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
      JvmtiTagHashmapEntry* entry_removed = hashmap->remove(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
      assert(entry_removed == entry, "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
      tag_map()->destroy_entry(entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
      JNIHandles::destroy_weak_global(ref);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
      if (obj_tag != entry->tag()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
         entry->set_tag(obj_tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
// An extended CallbackWrapper used when reporting an object reference
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
// to the agent.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
// {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
//   TwoOopCallbackWrapper wrapper(tag_map, referrer, o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
//   (*callback)(wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
//               wrapper.obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
//               wrapper.obj_tag_p()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
//               wrapper.referrer_tag_p(), ...)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
// } // wrapper goes out of scope here which results in the destructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
//      checking to see if the referrer object has been tagged, untagged,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
//      or the tag value has changed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
class TwoOopCallbackWrapper : public CallbackWrapper {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
  bool _is_reference_to_self;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
  JvmtiTagHashmap* _referrer_hashmap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
  JvmtiTagHashmapEntry* _referrer_entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
  oop _referrer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
  jlong _referrer_obj_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
  jlong _referrer_klass_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
  jlong* _referrer_tag_p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
  bool is_reference_to_self() const             { return _is_reference_to_self; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
  TwoOopCallbackWrapper(JvmtiTagMap* tag_map, oop referrer, oop o) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
    CallbackWrapper(tag_map, o)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
    // self reference needs to be handled in a special way
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
    _is_reference_to_self = (referrer == o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
    if (_is_reference_to_self) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
      _referrer_klass_tag = klass_tag();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
      _referrer_tag_p = obj_tag_p();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
      // for Classes the klassOop is tagged
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
      _referrer = klassOop_if_java_lang_Class(referrer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
      // record the context
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
      _referrer_hashmap = tag_map->hashmap_for(_referrer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
      _referrer_entry = _referrer_hashmap->find(_referrer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
      // get object tag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
      _referrer_obj_tag = (_referrer_entry == NULL) ? 0 : _referrer_entry->tag();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
      _referrer_tag_p = &_referrer_obj_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
      // get referrer class tag.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
      klassOop k = (_referrer == referrer) ?  // Check if referrer is a class...
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
          _referrer->klass()                  // No, just get its class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
         : SystemDictionary::class_klass();   // Yes, its class is Class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
      _referrer_klass_tag = tag_for(tag_map, k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
489c9b5090e2 Initial load
duke
parents:
diff changeset
   743
  ~TwoOopCallbackWrapper() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
    if (!is_reference_to_self()){
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
      post_callback_tag_update(_referrer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
                               _referrer_hashmap,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
                               _referrer_entry,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
                               _referrer_obj_tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   749
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
  // address of referrer tag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
  // (for a self reference this will return the same thing as obj_tag_p())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
  inline jlong* referrer_tag_p()        { return _referrer_tag_p; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
  // referrer's class tag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
  inline jlong referrer_klass_tag()     { return _referrer_klass_tag; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
// tag an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
// This function is performance critical. If many threads attempt to tag objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
// around the same time then it's possible that the Mutex associated with the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
// tag map will be a hot lock. Eliminating this lock will not eliminate the issue
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
// because creating a JNI weak reference requires acquiring a global lock also.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
void JvmtiTagMap::set_tag(jobject object, jlong tag) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
  MutexLocker ml(lock());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
  // resolve the object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
  oop o = JNIHandles::resolve_non_null(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
  // for Classes we tag the klassOop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
  o = klassOop_if_java_lang_Class(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
  // see if the object is already tagged
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
  JvmtiTagHashmap* hashmap = hashmap_for(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
  JvmtiTagHashmapEntry* entry = hashmap->find(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
  // if the object is not already tagged then we tag it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
  if (entry == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
    if (tag != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
      HandleMark hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
      Handle h(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
      jweak ref = JNIHandles::make_weak_global(h);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
      // the object may have moved because make_weak_global may
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
      // have blocked - thus it is necessary resolve the handle
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
      // and re-hash the object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
      o = h();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
      entry = create_entry(ref, tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
      hashmap_for(o)->add(o, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
      // no-op
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
    // if the object is already tagged then we either update
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
    // the tag (if a new tag value has been provided)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
    // or remove the object if the new tag value is 0.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
    // Removing the object requires that we also delete the JNI
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
    // weak ref to the object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
    if (tag == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
      jweak ref = entry->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
      hashmap->remove(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
      destroy_entry(entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
      JNIHandles::destroy_weak_global(ref);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
      entry->set_tag(tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   810
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   811
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
// get the tag for an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   813
jlong JvmtiTagMap::get_tag(jobject object) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   814
  MutexLocker ml(lock());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   815
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
  // resolve the object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
  oop o = JNIHandles::resolve_non_null(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
  // for Classes get the tag from the klassOop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
  return tag_for(this, klassOop_if_java_lang_Class(o));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
// Helper class used to describe the static or instance fields of a class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
// For each field it holds the field index (as defined by the JVMTI specification),
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
// the field type, and the offset.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
class ClassFieldDescriptor: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
  int _field_index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
  int _field_offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
  char _field_type;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
  ClassFieldDescriptor(int index, char type, int offset) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
    _field_index(index), _field_type(type), _field_offset(offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
  int field_index()  const  { return _field_index; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
  char field_type()  const  { return _field_type; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
  int field_offset() const  { return _field_offset; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
class ClassFieldMap: public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
    initial_field_count = 5
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
  // list of field descriptors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
  GrowableArray<ClassFieldDescriptor*>* _fields;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
  // constructor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
  ClassFieldMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
  // add a field
489c9b5090e2 Initial load
duke
parents:
diff changeset
   855
  void add(int index, char type, int offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
  // returns the field count for the given class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
  static int compute_field_count(instanceKlassHandle ikh);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
  ~ClassFieldMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
  // access
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
  int field_count()                     { return _fields->length(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
  ClassFieldDescriptor* field_at(int i) { return _fields->at(i); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
  // functions to create maps of static or instance fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
  static ClassFieldMap* create_map_of_static_fields(klassOop k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
  static ClassFieldMap* create_map_of_instance_fields(oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
489c9b5090e2 Initial load
duke
parents:
diff changeset
   872
ClassFieldMap::ClassFieldMap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
  _fields = new (ResourceObj::C_HEAP) GrowableArray<ClassFieldDescriptor*>(initial_field_count, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
ClassFieldMap::~ClassFieldMap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
  for (int i=0; i<_fields->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
    delete _fields->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
  delete _fields;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
void ClassFieldMap::add(int index, char type, int offset) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
  ClassFieldDescriptor* field = new ClassFieldDescriptor(index, type, offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
  _fields->append(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
// Returns a heap allocated ClassFieldMap to describe the static fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
// of the given class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
ClassFieldMap* ClassFieldMap::create_map_of_static_fields(klassOop k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
  HandleMark hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
  instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
  // create the field map
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
  ClassFieldMap* field_map = new ClassFieldMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
  FilteredFieldStream f(ikh, false, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
  int max_field_index = f.field_count()-1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
  int index = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
  for (FilteredFieldStream fld(ikh, true, true); !fld.eos(); fld.next(), index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
    // ignore instance fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   904
    if (!fld.access_flags().is_static()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   905
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   906
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   907
    field_map->add(max_field_index - index, fld.signature()->byte_at(0), fld.offset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
  return field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
// Returns a heap allocated ClassFieldMap to describe the instance fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
// of the given class. All instance fields are included (this means public
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
// and private fields declared in superclasses and superinterfaces too).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
ClassFieldMap* ClassFieldMap::create_map_of_instance_fields(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
  HandleMark hm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
  instanceKlassHandle ikh = instanceKlassHandle(Thread::current(), obj->klass());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
  // create the field map
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
  ClassFieldMap* field_map = new ClassFieldMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
  FilteredFieldStream f(ikh, false, false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
  int max_field_index = f.field_count()-1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
  int index = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
  for (FilteredFieldStream fld(ikh, false, false); !fld.eos(); fld.next(), index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
    // ignore static fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
    if (fld.access_flags().is_static()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
    field_map->add(max_field_index - index, fld.signature()->byte_at(0), fld.offset());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
  return field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
// Helper class used to cache a ClassFileMap for the instance fields of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
// a cache. A JvmtiCachedClassFieldMap can be cached by an instanceKlass during
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
// heap iteration and avoid creating a field map for each object in the heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
// (only need to create the map when the first instance of a class is encountered).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
class JvmtiCachedClassFieldMap : public CHeapObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
   enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
     initial_class_count = 200
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
   };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
  ClassFieldMap* _field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
  ClassFieldMap* field_map() const          { return _field_map; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
  JvmtiCachedClassFieldMap(ClassFieldMap* field_map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
  ~JvmtiCachedClassFieldMap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
  static GrowableArray<instanceKlass*>* _class_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
  static void add_to_class_list(instanceKlass* ik);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
  // returns the field map for a given object (returning map cached
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
  // by instanceKlass if possible
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
  static ClassFieldMap* get_map_of_instance_fields(oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
  // removes the field map from all instanceKlasses - should be
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
  // called before VM operation completes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
  static void clear_cache();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
  // returns the number of ClassFieldMap cached by instanceKlasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
  static int cached_field_map_count();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
GrowableArray<instanceKlass*>* JvmtiCachedClassFieldMap::_class_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
JvmtiCachedClassFieldMap::JvmtiCachedClassFieldMap(ClassFieldMap* field_map) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
  _field_map = field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
JvmtiCachedClassFieldMap::~JvmtiCachedClassFieldMap() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
  if (_field_map != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
    delete _field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
// Marker class to ensure that the class file map cache is only used in a defined
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
// scope.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
class ClassFieldMapCacheMark : public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
   static bool _is_active;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
   ClassFieldMapCacheMark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
     assert(Thread::current()->is_VM_thread(), "must be VMThread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
     assert(JvmtiCachedClassFieldMap::cached_field_map_count() == 0, "cache not empty");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
     assert(!_is_active, "ClassFieldMapCacheMark cannot be nested");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
     _is_active = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
   ~ClassFieldMapCacheMark() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
     JvmtiCachedClassFieldMap::clear_cache();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
     _is_active = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
   static bool is_active() { return _is_active; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
bool ClassFieldMapCacheMark::_is_active;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
// record that the given instanceKlass is caching a field map
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
void JvmtiCachedClassFieldMap::add_to_class_list(instanceKlass* ik) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
  if (_class_list == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
    _class_list = new (ResourceObj::C_HEAP) GrowableArray<instanceKlass*>(initial_class_count, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
  _class_list->push(ik);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
// returns the instance field map for the given object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
// (returns field map cached by the instanceKlass if possible)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1016
ClassFieldMap* JvmtiCachedClassFieldMap::get_map_of_instance_fields(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1017
  assert(Thread::current()->is_VM_thread(), "must be VMThread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1018
  assert(ClassFieldMapCacheMark::is_active(), "ClassFieldMapCacheMark not active");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
  klassOop k = obj->klass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1021
  instanceKlass* ik = instanceKlass::cast(k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1022
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
  // return cached map if possible
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1024
  JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
  if (cached_map != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
    assert(cached_map->field_map() != NULL, "missing field list");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
    return cached_map->field_map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
    ClassFieldMap* field_map = ClassFieldMap::create_map_of_instance_fields(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
    cached_map = new JvmtiCachedClassFieldMap(field_map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
    ik->set_jvmti_cached_class_field_map(cached_map);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1032
    add_to_class_list(ik);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
    return field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1034
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1035
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1036
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1037
// remove the fields maps cached from all instanceKlasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1038
void JvmtiCachedClassFieldMap::clear_cache() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
  assert(Thread::current()->is_VM_thread(), "must be VMThread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1040
  if (_class_list != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1041
    for (int i = 0; i < _class_list->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1042
      instanceKlass* ik = _class_list->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1043
      JvmtiCachedClassFieldMap* cached_map = ik->jvmti_cached_class_field_map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
      assert(cached_map != NULL, "should not be NULL");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
      ik->set_jvmti_cached_class_field_map(NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
      delete cached_map;  // deletes the encapsulated field map
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
    delete _class_list;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
    _class_list = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1051
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1052
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1053
// returns the number of ClassFieldMap cached by instanceKlasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1054
int JvmtiCachedClassFieldMap::cached_field_map_count() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1055
  return (_class_list == NULL) ? 0 : _class_list->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1056
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1057
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1058
// helper function to indicate if an object is filtered by its tag or class tag
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1059
static inline bool is_filtered_by_heap_filter(jlong obj_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1060
                                              jlong klass_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1061
                                              int heap_filter) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1062
  // apply the heap filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1063
  if (obj_tag != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1064
    // filter out tagged objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1065
    if (heap_filter & JVMTI_HEAP_FILTER_TAGGED) return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1066
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1067
    // filter out untagged objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1068
    if (heap_filter & JVMTI_HEAP_FILTER_UNTAGGED) return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1069
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1070
  if (klass_tag != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1071
    // filter out objects with tagged classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1072
    if (heap_filter & JVMTI_HEAP_FILTER_CLASS_TAGGED) return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1073
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1074
    // filter out objects with untagged classes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1075
    if (heap_filter & JVMTI_HEAP_FILTER_CLASS_UNTAGGED) return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1076
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1077
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1078
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1079
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1080
// helper function to indicate if an object is filtered by a klass filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1081
static inline bool is_filtered_by_klass_filter(oop obj, KlassHandle klass_filter) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1082
  if (!klass_filter.is_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1083
    if (obj->klass() != klass_filter()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1084
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1085
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1086
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1087
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1088
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1089
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1090
// helper function to tell if a field is a primitive field or not
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1091
static inline bool is_primitive_field_type(char type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1092
  return (type != 'L' && type != '[');
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1093
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1094
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1095
// helper function to copy the value from location addr to jvalue.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1096
static inline void copy_to_jvalue(jvalue *v, address addr, jvmtiPrimitiveType value_type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1097
  switch (value_type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1098
    case JVMTI_PRIMITIVE_TYPE_BOOLEAN : { v->z = *(jboolean*)addr; break; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1099
    case JVMTI_PRIMITIVE_TYPE_BYTE    : { v->b = *(jbyte*)addr;    break; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1100
    case JVMTI_PRIMITIVE_TYPE_CHAR    : { v->c = *(jchar*)addr;    break; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1101
    case JVMTI_PRIMITIVE_TYPE_SHORT   : { v->s = *(jshort*)addr;   break; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1102
    case JVMTI_PRIMITIVE_TYPE_INT     : { v->i = *(jint*)addr;     break; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1103
    case JVMTI_PRIMITIVE_TYPE_LONG    : { v->j = *(jlong*)addr;    break; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1104
    case JVMTI_PRIMITIVE_TYPE_FLOAT   : { v->f = *(jfloat*)addr;   break; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1105
    case JVMTI_PRIMITIVE_TYPE_DOUBLE  : { v->d = *(jdouble*)addr;  break; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1106
    default: ShouldNotReachHere();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1107
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1108
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1109
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1110
// helper function to invoke string primitive value callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1111
// returns visit control flags
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1112
static jint invoke_string_value_callback(jvmtiStringPrimitiveValueCallback cb,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1113
                                         CallbackWrapper* wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1114
                                         oop str,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1115
                                         void* user_data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1116
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1117
  assert(str->klass() == SystemDictionary::string_klass(), "not a string");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1118
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1119
  // get the string value and length
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1120
  // (string value may be offset from the base)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1121
  int s_len = java_lang_String::length(str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1122
  typeArrayOop s_value = java_lang_String::value(str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1123
  int s_offset = java_lang_String::offset(str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1124
  jchar* value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1125
  if (s_len > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1126
    value = s_value->char_at_addr(s_offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1127
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1128
    value = (jchar*) s_value->base(T_CHAR);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1129
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1130
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1131
  // invoke the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1132
  return (*cb)(wrapper->klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1133
               wrapper->obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1134
               wrapper->obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1135
               value,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1136
               (jint)s_len,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1137
               user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1138
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1139
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1140
// helper function to invoke string primitive value callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1141
// returns visit control flags
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1142
static jint invoke_array_primitive_value_callback(jvmtiArrayPrimitiveValueCallback cb,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1143
                                                  CallbackWrapper* wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1144
                                                  oop obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1145
                                                  void* user_data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1146
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1147
  assert(obj->is_typeArray(), "not a primitive array");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1148
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1149
  // get base address of first element
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1150
  typeArrayOop array = typeArrayOop(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1151
  BasicType type = typeArrayKlass::cast(array->klass())->element_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1152
  void* elements = array->base(type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1153
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1154
  // jvmtiPrimitiveType is defined so this mapping is always correct
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1155
  jvmtiPrimitiveType elem_type = (jvmtiPrimitiveType)type2char(type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1156
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1157
  return (*cb)(wrapper->klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1158
               wrapper->obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1159
               wrapper->obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1160
               (jint)array->length(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1161
               elem_type,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1162
               elements,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1163
               user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1164
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1165
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1166
// helper function to invoke the primitive field callback for all static fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1167
// of a given class
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1168
static jint invoke_primitive_field_callback_for_static_fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1169
  (CallbackWrapper* wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1170
   oop obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1171
   jvmtiPrimitiveFieldCallback cb,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1172
   void* user_data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1173
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1174
  // for static fields only the index will be set
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1175
  static jvmtiHeapReferenceInfo reference_info = { 0 };
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1176
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1177
  assert(obj->klass() == SystemDictionary::class_klass(), "not a class");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1178
  if (java_lang_Class::is_primitive(obj)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1179
    return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1180
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1181
  klassOop k = java_lang_Class::as_klassOop(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1182
  Klass* klass = k->klass_part();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1183
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1184
  // ignore classes for object and type arrays
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1185
  if (!klass->oop_is_instance()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1186
    return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1187
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1188
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1189
  // ignore classes which aren't linked yet
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1190
  instanceKlass* ik = instanceKlass::cast(k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1191
  if (!ik->is_linked()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1192
    return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1193
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1194
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1195
  // get the field map
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1196
  ClassFieldMap* field_map = ClassFieldMap::create_map_of_static_fields(k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1197
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1198
  // invoke the callback for each static primitive field
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1199
  for (int i=0; i<field_map->field_count(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1200
    ClassFieldDescriptor* field = field_map->field_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1201
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1202
    // ignore non-primitive fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1203
    char type = field->field_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1204
    if (!is_primitive_field_type(type)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1205
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1206
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1207
    // one-to-one mapping
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1208
    jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1209
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1210
    // get offset and field value
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1211
    int offset = field->field_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1212
    address addr = (address)k + offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1213
    jvalue value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1214
    copy_to_jvalue(&value, addr, value_type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1215
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1216
    // field index
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1217
    reference_info.field.index = field->field_index();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1218
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1219
    // invoke the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1220
    jint res = (*cb)(JVMTI_HEAP_REFERENCE_STATIC_FIELD,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1221
                     &reference_info,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1222
                     wrapper->klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1223
                     wrapper->obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1224
                     value,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1225
                     value_type,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1226
                     user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1227
    if (res & JVMTI_VISIT_ABORT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1228
      delete field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1229
      return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1230
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1231
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1232
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1233
  delete field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1234
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1235
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1236
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1237
// helper function to invoke the primitive field callback for all instance fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1238
// of a given object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1239
static jint invoke_primitive_field_callback_for_instance_fields(
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1240
  CallbackWrapper* wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1241
  oop obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1242
  jvmtiPrimitiveFieldCallback cb,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1243
  void* user_data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1244
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1245
  // for instance fields only the index will be set
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
  static jvmtiHeapReferenceInfo reference_info = { 0 };
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
  // get the map of the instance fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
  ClassFieldMap* fields = JvmtiCachedClassFieldMap::get_map_of_instance_fields(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
  // invoke the callback for each instance primitive field
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1252
  for (int i=0; i<fields->field_count(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1253
    ClassFieldDescriptor* field = fields->field_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1254
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1255
    // ignore non-primitive fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1256
    char type = field->field_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1257
    if (!is_primitive_field_type(type)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1258
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1259
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1260
    // one-to-one mapping
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
    jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
    // get offset and field value
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
    int offset = field->field_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
    address addr = (address)obj + offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1266
    jvalue value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1267
    copy_to_jvalue(&value, addr, value_type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1268
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1269
    // field index
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1270
    reference_info.field.index = field->field_index();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1271
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
    // invoke the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
    jint res = (*cb)(JVMTI_HEAP_REFERENCE_FIELD,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
                     &reference_info,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
                     wrapper->klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1276
                     wrapper->obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
                     value,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
                     value_type,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1279
                     user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1280
    if (res & JVMTI_VISIT_ABORT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1281
      return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1282
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1283
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1284
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1285
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1286
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1287
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1288
// VM operation to iterate over all objects in the heap (both reachable
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1289
// and unreachable)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1290
class VM_HeapIterateOperation: public VM_Operation {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1291
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1292
  ObjectClosure* _blk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1293
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1294
  VM_HeapIterateOperation(ObjectClosure* blk) { _blk = blk; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1295
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
  VMOp_Type type() const { return VMOp_HeapIterateOperation; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1297
  void doit() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1298
    // allows class files maps to be cached during iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1299
    ClassFieldMapCacheMark cm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1300
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1301
    // make sure that heap is parsable (fills TLABs with filler objects)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1302
    Universe::heap()->ensure_parsability(false);  // no need to retire TLABs
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1303
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1304
    // Verify heap before iteration - if the heap gets corrupted then
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1305
    // JVMTI's IterateOverHeap will crash.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1306
    if (VerifyBeforeIteration) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1307
      Universe::verify();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1308
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1309
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1310
    // do the iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1311
    Universe::heap()->object_iterate(_blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1312
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1313
    // when sharing is enabled we must iterate over the shared spaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1314
    if (UseSharedSpaces) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1315
      GenCollectedHeap* gch = GenCollectedHeap::heap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1316
      CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1317
      gen->ro_space()->object_iterate(_blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1318
      gen->rw_space()->object_iterate(_blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1319
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1320
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1321
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1324
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1325
// An ObjectClosure used to support the deprecated IterateOverHeap and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
// IterateOverInstancesOfClass functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1327
class IterateOverHeapObjectClosure: public ObjectClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
  JvmtiTagMap* _tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1330
  KlassHandle _klass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1331
  jvmtiHeapObjectFilter _object_filter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1332
  jvmtiHeapObjectCallback _heap_object_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1333
  const void* _user_data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1334
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1335
  // accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1336
  JvmtiTagMap* tag_map() const                    { return _tag_map; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1337
  jvmtiHeapObjectFilter object_filter() const     { return _object_filter; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1338
  jvmtiHeapObjectCallback object_callback() const { return _heap_object_callback; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1339
  KlassHandle klass() const                       { return _klass; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1340
  const void* user_data() const                   { return _user_data; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1341
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1342
  // indicates if iteration has been aborted
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1343
  bool _iteration_aborted;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1344
  bool is_iteration_aborted() const               { return _iteration_aborted; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1345
  void set_iteration_aborted(bool aborted)        { _iteration_aborted = aborted; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1346
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1347
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1348
  IterateOverHeapObjectClosure(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1349
                               KlassHandle klass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1350
                               jvmtiHeapObjectFilter object_filter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1351
                               jvmtiHeapObjectCallback heap_object_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1352
                               const void* user_data) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1353
    _tag_map(tag_map),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1354
    _klass(klass),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1355
    _object_filter(object_filter),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1356
    _heap_object_callback(heap_object_callback),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1357
    _user_data(user_data),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1358
    _iteration_aborted(false)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1359
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1360
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1361
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1362
  void do_object(oop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1363
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1364
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1365
// invoked for each object in the heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1366
void IterateOverHeapObjectClosure::do_object(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1367
  // check if iteration has been halted
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1368
  if (is_iteration_aborted()) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1369
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1370
  // ignore any objects that aren't visible to profiler
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1371
  if (!ServiceUtil::visible_oop(o)) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1372
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1373
  // instanceof check when filtering by klass
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1374
  if (!klass().is_null() && !o->is_a(klass()())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1375
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1376
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1377
  // prepare for the calllback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1378
  CallbackWrapper wrapper(tag_map(), o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1379
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1380
  // if the object is tagged and we're only interested in untagged objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1381
  // then don't invoke the callback. Similiarly, if the object is untagged
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1382
  // and we're only interested in tagged objects we skip the callback.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1383
  if (wrapper.obj_tag() != 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1384
    if (object_filter() == JVMTI_HEAP_OBJECT_UNTAGGED) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1385
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1386
    if (object_filter() == JVMTI_HEAP_OBJECT_TAGGED) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1387
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1388
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1389
  // invoke the agent's callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1390
  jvmtiIterationControl control = (*object_callback())(wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1391
                                                       wrapper.obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1392
                                                       wrapper.obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1393
                                                       (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1394
  if (control == JVMTI_ITERATION_ABORT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1395
    set_iteration_aborted(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1396
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1397
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1398
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1399
// An ObjectClosure used to support the IterateThroughHeap function
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1400
class IterateThroughHeapObjectClosure: public ObjectClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1401
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1402
  JvmtiTagMap* _tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1403
  KlassHandle _klass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1404
  int _heap_filter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1405
  const jvmtiHeapCallbacks* _callbacks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1406
  const void* _user_data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1407
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1408
  // accessor functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1409
  JvmtiTagMap* tag_map() const                     { return _tag_map; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1410
  int heap_filter() const                          { return _heap_filter; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1411
  const jvmtiHeapCallbacks* callbacks() const      { return _callbacks; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1412
  KlassHandle klass() const                        { return _klass; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1413
  const void* user_data() const                    { return _user_data; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1414
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1415
  // indicates if the iteration has been aborted
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1416
  bool _iteration_aborted;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1417
  bool is_iteration_aborted() const                { return _iteration_aborted; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1418
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1419
  // used to check the visit control flags. If the abort flag is set
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1420
  // then we set the iteration aborted flag so that the iteration completes
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1421
  // without processing any further objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1422
  bool check_flags_for_abort(jint flags) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1423
    bool is_abort = (flags & JVMTI_VISIT_ABORT) != 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1424
    if (is_abort) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1425
      _iteration_aborted = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1426
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1427
    return is_abort;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1428
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1429
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1430
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1431
  IterateThroughHeapObjectClosure(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1432
                                  KlassHandle klass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1433
                                  int heap_filter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1434
                                  const jvmtiHeapCallbacks* heap_callbacks,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1435
                                  const void* user_data) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1436
    _tag_map(tag_map),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1437
    _klass(klass),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1438
    _heap_filter(heap_filter),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1439
    _callbacks(heap_callbacks),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1440
    _user_data(user_data),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1441
    _iteration_aborted(false)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1442
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1443
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1444
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1445
  void do_object(oop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1446
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1447
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1448
// invoked for each object in the heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1449
void IterateThroughHeapObjectClosure::do_object(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1450
  // check if iteration has been halted
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1451
  if (is_iteration_aborted()) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1452
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1453
  // ignore any objects that aren't visible to profiler
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1454
  if (!ServiceUtil::visible_oop(obj)) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1455
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1456
  // apply class filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1457
  if (is_filtered_by_klass_filter(obj, klass())) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1458
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1459
  // prepare for callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1460
  CallbackWrapper wrapper(tag_map(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1461
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1462
  // check if filtered by the heap filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1463
  if (is_filtered_by_heap_filter(wrapper.obj_tag(), wrapper.klass_tag(), heap_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1464
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1465
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1466
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1467
  // for arrays we need the length, otherwise -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1468
  bool is_array = obj->is_array();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1469
  int len = is_array ? arrayOop(obj)->length() : -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1470
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1471
  // invoke the object callback (if callback is provided)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1472
  if (callbacks()->heap_iteration_callback != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1473
    jvmtiHeapIterationCallback cb = callbacks()->heap_iteration_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1474
    jint res = (*cb)(wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1475
                     wrapper.obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1476
                     wrapper.obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1477
                     (jint)len,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1478
                     (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1479
    if (check_flags_for_abort(res)) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1480
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1481
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1482
  // for objects and classes we report primitive fields if callback provided
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1483
  if (callbacks()->primitive_field_callback != NULL && obj->is_instance()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1484
    jint res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1485
    jvmtiPrimitiveFieldCallback cb = callbacks()->primitive_field_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1486
    if (obj->klass() == SystemDictionary::class_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1487
      res = invoke_primitive_field_callback_for_static_fields(&wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1488
                                                                    obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1489
                                                                    cb,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1490
                                                                    (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1491
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1492
      res = invoke_primitive_field_callback_for_instance_fields(&wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1493
                                                                      obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1494
                                                                      cb,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1495
                                                                      (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1496
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1497
    if (check_flags_for_abort(res)) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1498
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1499
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1500
  // string callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1501
  if (!is_array &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1502
      callbacks()->string_primitive_value_callback != NULL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1503
      obj->klass() == SystemDictionary::string_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1504
    jint res = invoke_string_value_callback(
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1505
                callbacks()->string_primitive_value_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1506
                &wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1507
                obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1508
                (void*)user_data() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1509
    if (check_flags_for_abort(res)) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1510
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1511
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1512
  // array callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1513
  if (is_array &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1514
      callbacks()->array_primitive_value_callback != NULL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1515
      obj->is_typeArray()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1516
    jint res = invoke_array_primitive_value_callback(
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1517
               callbacks()->array_primitive_value_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1518
               &wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1519
               obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1520
               (void*)user_data() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1521
    if (check_flags_for_abort(res)) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1522
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1523
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1524
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1525
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1526
// Deprecated function to iterate over all objects in the heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1527
void JvmtiTagMap::iterate_over_heap(jvmtiHeapObjectFilter object_filter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1528
                                    KlassHandle klass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1529
                                    jvmtiHeapObjectCallback heap_object_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1530
                                    const void* user_data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1531
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1532
  MutexLocker ml(Heap_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1533
  IterateOverHeapObjectClosure blk(this,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1534
                                   klass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1535
                                   object_filter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1536
                                   heap_object_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1537
                                   user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1538
  VM_HeapIterateOperation op(&blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1539
  VMThread::execute(&op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1540
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1541
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1542
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1543
// Iterates over all objects in the heap
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1544
void JvmtiTagMap::iterate_through_heap(jint heap_filter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1545
                                       KlassHandle klass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1546
                                       const jvmtiHeapCallbacks* callbacks,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1547
                                       const void* user_data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1548
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1549
  MutexLocker ml(Heap_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1550
  IterateThroughHeapObjectClosure blk(this,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1551
                                      klass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1552
                                      heap_filter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1553
                                      callbacks,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1554
                                      user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1555
  VM_HeapIterateOperation op(&blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1556
  VMThread::execute(&op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1557
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1558
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1559
// support class for get_objects_with_tags
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1560
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1561
class TagObjectCollector : public JvmtiTagHashmapEntryClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1562
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1563
  JvmtiEnv* _env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1564
  jlong* _tags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1565
  jint _tag_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1566
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1567
  GrowableArray<jobject>* _object_results;  // collected objects (JNI weak refs)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1568
  GrowableArray<uint64_t>* _tag_results;    // collected tags
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1569
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1570
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1571
  TagObjectCollector(JvmtiEnv* env, const jlong* tags, jint tag_count) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1572
    _env = env;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1573
    _tags = (jlong*)tags;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1574
    _tag_count = tag_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1575
    _object_results = new (ResourceObj::C_HEAP) GrowableArray<jobject>(1,true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1576
    _tag_results = new (ResourceObj::C_HEAP) GrowableArray<uint64_t>(1,true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1577
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1578
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1579
  ~TagObjectCollector() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1580
    delete _object_results;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1581
    delete _tag_results;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1582
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1583
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1584
  // for each tagged object check if the tag value matches
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1585
  // - if it matches then we create a JNI local reference to the object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1586
  // and record the reference and tag value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1587
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1588
  void do_entry(JvmtiTagHashmapEntry* entry) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1589
    for (int i=0; i<_tag_count; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1590
      if (_tags[i] == entry->tag()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1591
        oop o = JNIHandles::resolve(entry->object());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1592
        assert(o != NULL && o != JNIHandles::deleted_handle(), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1593
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1594
        // the mirror is tagged
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1595
        if (o->is_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1596
          klassOop k = (klassOop)o;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1597
          o = Klass::cast(k)->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1598
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1599
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1600
        jobject ref = JNIHandles::make_local(JavaThread::current(), o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1601
        _object_results->append(ref);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1602
        _tag_results->append((uint64_t)entry->tag());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1603
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1604
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1605
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1606
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1607
  // return the results from the collection
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1608
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1609
  jvmtiError result(jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1610
    jvmtiError error;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1611
    int count = _object_results->length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1612
    assert(count >= 0, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1613
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1614
    // if object_result_ptr is not NULL then allocate the result and copy
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1615
    // in the object references.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1616
    if (object_result_ptr != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1617
      error = _env->Allocate(count * sizeof(jobject), (unsigned char**)object_result_ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1618
      if (error != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1619
        return error;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1620
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1621
      for (int i=0; i<count; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1622
        (*object_result_ptr)[i] = _object_results->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1623
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1624
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1625
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1626
    // if tag_result_ptr is not NULL then allocate the result and copy
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1627
    // in the tag values.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1628
    if (tag_result_ptr != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1629
      error = _env->Allocate(count * sizeof(jlong), (unsigned char**)tag_result_ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1630
      if (error != JVMTI_ERROR_NONE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1631
        if (object_result_ptr != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1632
          _env->Deallocate((unsigned char*)object_result_ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1633
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1634
        return error;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1635
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1636
      for (int i=0; i<count; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1637
        (*tag_result_ptr)[i] = (jlong)_tag_results->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1638
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1639
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1640
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1641
    *count_ptr = count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1642
    return JVMTI_ERROR_NONE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1643
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1644
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1645
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1646
// return the list of objects with the specified tags
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1647
jvmtiError JvmtiTagMap::get_objects_with_tags(const jlong* tags,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1648
  jint count, jint* count_ptr, jobject** object_result_ptr, jlong** tag_result_ptr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1649
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1650
  TagObjectCollector collector(env(), tags, count);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1651
  {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1652
    // iterate over all tagged objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1653
    MutexLocker ml(lock());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1654
    entry_iterate(&collector);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1655
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1656
  return collector.result(count_ptr, object_result_ptr, tag_result_ptr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1657
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1658
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1659
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1660
// ObjectMarker is used to support the marking objects when walking the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1661
// heap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1662
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1663
// This implementation uses the existing mark bits in an object for
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1664
// marking. Objects that are marked must later have their headers restored.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1665
// As most objects are unlocked and don't have their identity hash computed
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1666
// we don't have to save their headers. Instead we save the headers that
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1667
// are "interesting". Later when the headers are restored this implementation
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1668
// restores all headers to their initial value and then restores the few
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1669
// objects that had interesting headers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1670
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1671
// Future work: This implementation currently uses growable arrays to save
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1672
// the oop and header of interesting objects. As an optimization we could
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1673
// use the same technique as the GC and make use of the unused area
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1674
// between top() and end().
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1675
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1676
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1677
// An ObjectClosure used to restore the mark bits of an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1678
class RestoreMarksClosure : public ObjectClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1679
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1680
  void do_object(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1681
    if (o != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1682
      markOop mark = o->mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1683
      if (mark->is_marked()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1684
        o->init_mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1685
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1686
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1687
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1688
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1689
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1690
// ObjectMarker provides the mark and visited functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1691
class ObjectMarker : AllStatic {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1692
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1693
  // saved headers
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1694
  static GrowableArray<oop>* _saved_oop_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1695
  static GrowableArray<markOop>* _saved_mark_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1696
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1697
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1698
  static void init();                       // initialize
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1699
  static void done();                       // clean-up
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1700
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1701
  static inline void mark(oop o);           // mark an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1702
  static inline bool visited(oop o);        // check if object has been visited
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1703
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1704
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1705
GrowableArray<oop>* ObjectMarker::_saved_oop_stack = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1706
GrowableArray<markOop>* ObjectMarker::_saved_mark_stack = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1707
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1708
// initialize ObjectMarker - prepares for object marking
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1709
void ObjectMarker::init() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1710
  assert(Thread::current()->is_VM_thread(), "must be VMThread");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1711
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1712
  // prepare heap for iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1713
  Universe::heap()->ensure_parsability(false);  // no need to retire TLABs
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1714
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1715
  // create stacks for interesting headers
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1716
  _saved_mark_stack = new (ResourceObj::C_HEAP) GrowableArray<markOop>(4000, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1717
  _saved_oop_stack = new (ResourceObj::C_HEAP) GrowableArray<oop>(4000, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1718
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1719
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1720
    BiasedLocking::preserve_marks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1721
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1722
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1723
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1724
// Object marking is done so restore object headers
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1725
void ObjectMarker::done() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1726
  // iterate over all objects and restore the mark bits to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1727
  // their initial value
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1728
  RestoreMarksClosure blk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1729
  Universe::heap()->object_iterate(&blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1730
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1731
  // When sharing is enabled we need to restore the headers of the objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1732
  // in the readwrite space too.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1733
  if (UseSharedSpaces) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1734
    GenCollectedHeap* gch = GenCollectedHeap::heap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1735
    CompactingPermGenGen* gen = (CompactingPermGenGen*)gch->perm_gen();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1736
    gen->rw_space()->object_iterate(&blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1737
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1738
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1739
  // now restore the interesting headers
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1740
  for (int i = 0; i < _saved_oop_stack->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1741
    oop o = _saved_oop_stack->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1742
    markOop mark = _saved_mark_stack->at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1743
    o->set_mark(mark);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1744
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1745
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1746
  if (UseBiasedLocking) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1747
    BiasedLocking::restore_marks();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1748
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1749
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1750
  // free the stacks
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1751
  delete _saved_oop_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1752
  delete _saved_mark_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1753
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1754
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1755
// mark an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1756
inline void ObjectMarker::mark(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1757
  assert(Universe::heap()->is_in(o), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1758
  assert(!o->mark()->is_marked(), "should only mark an object once");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1759
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1760
  // object's mark word
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1761
  markOop mark = o->mark();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1762
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1763
  if (mark->must_be_preserved(o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1764
    _saved_mark_stack->push(mark);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1765
    _saved_oop_stack->push(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1766
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1767
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1768
  // mark the object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1769
  o->set_mark(markOopDesc::prototype()->set_marked());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1770
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1771
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1772
// return true if object is marked
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1773
inline bool ObjectMarker::visited(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1774
  return o->mark()->is_marked();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1775
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1776
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1777
// Stack allocated class to help ensure that ObjectMarker is used
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1778
// correctly. Constructor initializes ObjectMarker, destructor calls
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1779
// ObjectMarker's done() function to restore object headers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1780
class ObjectMarkerController : public StackObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1781
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1782
  ObjectMarkerController() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1783
    ObjectMarker::init();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1784
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1785
  ~ObjectMarkerController() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1786
    ObjectMarker::done();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1787
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1788
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1789
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1790
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1791
// helper to map a jvmtiHeapReferenceKind to an old style jvmtiHeapRootKind
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1792
// (not performance critical as only used for roots)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1793
static jvmtiHeapRootKind toJvmtiHeapRootKind(jvmtiHeapReferenceKind kind) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1794
  switch (kind) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1795
    case JVMTI_HEAP_REFERENCE_JNI_GLOBAL:   return JVMTI_HEAP_ROOT_JNI_GLOBAL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1796
    case JVMTI_HEAP_REFERENCE_SYSTEM_CLASS: return JVMTI_HEAP_ROOT_SYSTEM_CLASS;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1797
    case JVMTI_HEAP_REFERENCE_MONITOR:      return JVMTI_HEAP_ROOT_MONITOR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1798
    case JVMTI_HEAP_REFERENCE_STACK_LOCAL:  return JVMTI_HEAP_ROOT_STACK_LOCAL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1799
    case JVMTI_HEAP_REFERENCE_JNI_LOCAL:    return JVMTI_HEAP_ROOT_JNI_LOCAL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1800
    case JVMTI_HEAP_REFERENCE_THREAD:       return JVMTI_HEAP_ROOT_THREAD;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1801
    case JVMTI_HEAP_REFERENCE_OTHER:        return JVMTI_HEAP_ROOT_OTHER;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1802
    default: ShouldNotReachHere();          return JVMTI_HEAP_ROOT_OTHER;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1803
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1804
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1805
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1806
// Base class for all heap walk contexts. The base class maintains a flag
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1807
// to indicate if the context is valid or not.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1808
class HeapWalkContext VALUE_OBJ_CLASS_SPEC {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1809
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1810
  bool _valid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1811
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1812
  HeapWalkContext(bool valid)                   { _valid = valid; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1813
  void invalidate()                             { _valid = false; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1814
  bool is_valid() const                         { return _valid; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1815
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1816
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1817
// A basic heap walk context for the deprecated heap walking functions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1818
// The context for a basic heap walk are the callbacks and fields used by
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1819
// the referrer caching scheme.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1820
class BasicHeapWalkContext: public HeapWalkContext {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1821
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1822
  jvmtiHeapRootCallback _heap_root_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1823
  jvmtiStackReferenceCallback _stack_ref_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1824
  jvmtiObjectReferenceCallback _object_ref_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1825
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1826
  // used for caching
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1827
  oop _last_referrer;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1828
  jlong _last_referrer_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1829
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1830
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1831
  BasicHeapWalkContext() : HeapWalkContext(false) { }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1832
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1833
  BasicHeapWalkContext(jvmtiHeapRootCallback heap_root_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1834
                       jvmtiStackReferenceCallback stack_ref_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1835
                       jvmtiObjectReferenceCallback object_ref_callback) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1836
    HeapWalkContext(true),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1837
    _heap_root_callback(heap_root_callback),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1838
    _stack_ref_callback(stack_ref_callback),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1839
    _object_ref_callback(object_ref_callback),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1840
    _last_referrer(NULL),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1841
    _last_referrer_tag(0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1842
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1843
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1844
  // accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1845
  jvmtiHeapRootCallback heap_root_callback() const         { return _heap_root_callback; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1846
  jvmtiStackReferenceCallback stack_ref_callback() const   { return _stack_ref_callback; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1847
  jvmtiObjectReferenceCallback object_ref_callback() const { return _object_ref_callback;  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1848
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1849
  oop last_referrer() const               { return _last_referrer; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1850
  void set_last_referrer(oop referrer)    { _last_referrer = referrer; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1851
  jlong last_referrer_tag() const         { return _last_referrer_tag; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1852
  void set_last_referrer_tag(jlong value) { _last_referrer_tag = value; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1853
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1854
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1855
// The advanced heap walk context for the FollowReferences functions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1856
// The context is the callbacks, and the fields used for filtering.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1857
class AdvancedHeapWalkContext: public HeapWalkContext {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1858
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1859
  jint _heap_filter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1860
  KlassHandle _klass_filter;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1861
  const jvmtiHeapCallbacks* _heap_callbacks;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1862
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1863
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1864
  AdvancedHeapWalkContext() : HeapWalkContext(false) { }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1865
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1866
  AdvancedHeapWalkContext(jint heap_filter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1867
                           KlassHandle klass_filter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1868
                           const jvmtiHeapCallbacks* heap_callbacks) :
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1869
    HeapWalkContext(true),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1870
    _heap_filter(heap_filter),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1871
    _klass_filter(klass_filter),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1872
    _heap_callbacks(heap_callbacks) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1873
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1874
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1875
  // accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1876
  jint heap_filter() const         { return _heap_filter; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1877
  KlassHandle klass_filter() const { return _klass_filter; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1878
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1879
  const jvmtiHeapReferenceCallback heap_reference_callback() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1880
    return _heap_callbacks->heap_reference_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1881
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1882
  const jvmtiPrimitiveFieldCallback primitive_field_callback() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1883
    return _heap_callbacks->primitive_field_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1884
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1885
  const jvmtiArrayPrimitiveValueCallback array_primitive_value_callback() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1886
    return _heap_callbacks->array_primitive_value_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1887
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1888
  const jvmtiStringPrimitiveValueCallback string_primitive_value_callback() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1889
    return _heap_callbacks->string_primitive_value_callback;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1890
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1891
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1892
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1893
// The CallbackInvoker is a class with static functions that the heap walk can call
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1894
// into to invoke callbacks. It works in one of two modes. The "basic" mode is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1895
// used for the deprecated IterateOverReachableObjects functions. The "advanced"
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1896
// mode is for the newer FollowReferences function which supports a lot of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1897
// additional callbacks.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1898
class CallbackInvoker : AllStatic {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1899
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1900
  // heap walk styles
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1901
  enum { basic, advanced };
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1902
  static int _heap_walk_type;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1903
  static bool is_basic_heap_walk()           { return _heap_walk_type == basic; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1904
  static bool is_advanced_heap_walk()        { return _heap_walk_type == advanced; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1905
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1906
  // context for basic style heap walk
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1907
  static BasicHeapWalkContext _basic_context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1908
  static BasicHeapWalkContext* basic_context() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1909
    assert(_basic_context.is_valid(), "invalid");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1910
    return &_basic_context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1911
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1912
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1913
  // context for advanced style heap walk
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1914
  static AdvancedHeapWalkContext _advanced_context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1915
  static AdvancedHeapWalkContext* advanced_context() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1916
    assert(_advanced_context.is_valid(), "invalid");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1917
    return &_advanced_context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1918
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1919
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1920
  // context needed for all heap walks
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1921
  static JvmtiTagMap* _tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1922
  static const void* _user_data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1923
  static GrowableArray<oop>* _visit_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1924
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1925
  // accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1926
  static JvmtiTagMap* tag_map()                        { return _tag_map; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1927
  static const void* user_data()                       { return _user_data; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1928
  static GrowableArray<oop>* visit_stack()             { return _visit_stack; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1929
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1930
  // if the object hasn't been visited then push it onto the visit stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1931
  // so that it will be visited later
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1932
  static inline bool check_for_visit(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1933
    if (!ObjectMarker::visited(obj)) visit_stack()->push(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1934
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1935
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1936
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1937
  // invoke basic style callbacks
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1938
  static inline bool invoke_basic_heap_root_callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1939
    (jvmtiHeapRootKind root_kind, oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1940
  static inline bool invoke_basic_stack_ref_callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1941
    (jvmtiHeapRootKind root_kind, jlong thread_tag, jint depth, jmethodID method,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1942
     int slot, oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1943
  static inline bool invoke_basic_object_reference_callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1944
    (jvmtiObjectReferenceKind ref_kind, oop referrer, oop referree, jint index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1945
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1946
  // invoke advanced style callbacks
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1947
  static inline bool invoke_advanced_heap_root_callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1948
    (jvmtiHeapReferenceKind ref_kind, oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1949
  static inline bool invoke_advanced_stack_ref_callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1950
    (jvmtiHeapReferenceKind ref_kind, jlong thread_tag, jlong tid, int depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1951
     jmethodID method, jlocation bci, jint slot, oop obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1952
  static inline bool invoke_advanced_object_reference_callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1953
    (jvmtiHeapReferenceKind ref_kind, oop referrer, oop referree, jint index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1954
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1955
  // used to report the value of primitive fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1956
  static inline bool report_primitive_field
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1957
    (jvmtiHeapReferenceKind ref_kind, oop obj, jint index, address addr, char type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1958
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1959
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1960
  // initialize for basic mode
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1961
  static void initialize_for_basic_heap_walk(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1962
                                             GrowableArray<oop>* visit_stack,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1963
                                             const void* user_data,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1964
                                             BasicHeapWalkContext context);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1965
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1966
  // initialize for advanced mode
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1967
  static void initialize_for_advanced_heap_walk(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1968
                                                GrowableArray<oop>* visit_stack,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1969
                                                const void* user_data,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1970
                                                AdvancedHeapWalkContext context);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1971
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1972
   // functions to report roots
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1973
  static inline bool report_simple_root(jvmtiHeapReferenceKind kind, oop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1974
  static inline bool report_jni_local_root(jlong thread_tag, jlong tid, jint depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1975
    jmethodID m, oop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1976
  static inline bool report_stack_ref_root(jlong thread_tag, jlong tid, jint depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1977
    jmethodID method, jlocation bci, jint slot, oop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1978
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1979
  // functions to report references
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1980
  static inline bool report_array_element_reference(oop referrer, oop referree, jint index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1981
  static inline bool report_class_reference(oop referrer, oop referree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1982
  static inline bool report_class_loader_reference(oop referrer, oop referree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1983
  static inline bool report_signers_reference(oop referrer, oop referree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1984
  static inline bool report_protection_domain_reference(oop referrer, oop referree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1985
  static inline bool report_superclass_reference(oop referrer, oop referree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1986
  static inline bool report_interface_reference(oop referrer, oop referree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1987
  static inline bool report_static_field_reference(oop referrer, oop referree, jint slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1988
  static inline bool report_field_reference(oop referrer, oop referree, jint slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1989
  static inline bool report_constant_pool_reference(oop referrer, oop referree, jint index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1990
  static inline bool report_primitive_array_values(oop array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1991
  static inline bool report_string_value(oop str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1992
  static inline bool report_primitive_instance_field(oop o, jint index, address value, char type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1993
  static inline bool report_primitive_static_field(oop o, jint index, address value, char type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1994
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1995
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1996
// statics
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1997
int CallbackInvoker::_heap_walk_type;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1998
BasicHeapWalkContext CallbackInvoker::_basic_context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1999
AdvancedHeapWalkContext CallbackInvoker::_advanced_context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2000
JvmtiTagMap* CallbackInvoker::_tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2001
const void* CallbackInvoker::_user_data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2002
GrowableArray<oop>* CallbackInvoker::_visit_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2003
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2004
// initialize for basic heap walk (IterateOverReachableObjects et al)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2005
void CallbackInvoker::initialize_for_basic_heap_walk(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2006
                                                     GrowableArray<oop>* visit_stack,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2007
                                                     const void* user_data,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2008
                                                     BasicHeapWalkContext context) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2009
  _tag_map = tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2010
  _visit_stack = visit_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2011
  _user_data = user_data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2012
  _basic_context = context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2013
  _advanced_context.invalidate();       // will trigger assertion if used
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2014
  _heap_walk_type = basic;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2015
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2016
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2017
// initialize for advanced heap walk (FollowReferences)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2018
void CallbackInvoker::initialize_for_advanced_heap_walk(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2019
                                                        GrowableArray<oop>* visit_stack,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2020
                                                        const void* user_data,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2021
                                                        AdvancedHeapWalkContext context) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2022
  _tag_map = tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2023
  _visit_stack = visit_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2024
  _user_data = user_data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2025
  _advanced_context = context;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2026
  _basic_context.invalidate();      // will trigger assertion if used
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2027
  _heap_walk_type = advanced;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2028
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2029
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2030
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2031
// invoke basic style heap root callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2032
inline bool CallbackInvoker::invoke_basic_heap_root_callback(jvmtiHeapRootKind root_kind, oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2033
  assert(ServiceUtil::visible_oop(obj), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2034
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2035
  // if we heap roots should be reported
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2036
  jvmtiHeapRootCallback cb = basic_context()->heap_root_callback();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2037
  if (cb == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2038
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2039
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2040
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2041
  CallbackWrapper wrapper(tag_map(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2042
  jvmtiIterationControl control = (*cb)(root_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2043
                                        wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2044
                                        wrapper.obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2045
                                        wrapper.obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2046
                                        (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2047
  // push root to visit stack when following references
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2048
  if (control == JVMTI_ITERATION_CONTINUE &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2049
      basic_context()->object_ref_callback() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2050
    visit_stack()->push(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2051
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2052
  return control != JVMTI_ITERATION_ABORT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2053
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2054
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2055
// invoke basic style stack ref callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2056
inline bool CallbackInvoker::invoke_basic_stack_ref_callback(jvmtiHeapRootKind root_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2057
                                                             jlong thread_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2058
                                                             jint depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2059
                                                             jmethodID method,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2060
                                                             jint slot,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2061
                                                             oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2062
  assert(ServiceUtil::visible_oop(obj), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2063
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2064
  // if we stack refs should be reported
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2065
  jvmtiStackReferenceCallback cb = basic_context()->stack_ref_callback();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2066
  if (cb == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2067
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2068
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2069
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2070
  CallbackWrapper wrapper(tag_map(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2071
  jvmtiIterationControl control = (*cb)(root_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2072
                                        wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2073
                                        wrapper.obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2074
                                        wrapper.obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2075
                                        thread_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2076
                                        depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2077
                                        method,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2078
                                        slot,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2079
                                        (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2080
  // push root to visit stack when following references
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2081
  if (control == JVMTI_ITERATION_CONTINUE &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2082
      basic_context()->object_ref_callback() != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2083
    visit_stack()->push(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2084
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2085
  return control != JVMTI_ITERATION_ABORT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2086
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2087
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2088
// invoke basic style object reference callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2089
inline bool CallbackInvoker::invoke_basic_object_reference_callback(jvmtiObjectReferenceKind ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2090
                                                                    oop referrer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2091
                                                                    oop referree,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2092
                                                                    jint index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2093
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2094
  assert(ServiceUtil::visible_oop(referrer), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2095
  assert(ServiceUtil::visible_oop(referree), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2096
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2097
  BasicHeapWalkContext* context = basic_context();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2098
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2099
  // callback requires the referrer's tag. If it's the same referrer
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2100
  // as the last call then we use the cached value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2101
  jlong referrer_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2102
  if (referrer == context->last_referrer()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2103
    referrer_tag = context->last_referrer_tag();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2104
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2105
    referrer_tag = tag_for(tag_map(), klassOop_if_java_lang_Class(referrer));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2106
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2107
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2108
  // do the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2109
  CallbackWrapper wrapper(tag_map(), referree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2110
  jvmtiObjectReferenceCallback cb = context->object_ref_callback();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2111
  jvmtiIterationControl control = (*cb)(ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2112
                                        wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2113
                                        wrapper.obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2114
                                        wrapper.obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2115
                                        referrer_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2116
                                        index,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2117
                                        (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2118
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2119
  // record referrer and referrer tag. For self-references record the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2120
  // tag value from the callback as this might differ from referrer_tag.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2121
  context->set_last_referrer(referrer);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2122
  if (referrer == referree) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2123
    context->set_last_referrer_tag(*wrapper.obj_tag_p());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2124
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2125
    context->set_last_referrer_tag(referrer_tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2126
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2127
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2128
  if (control == JVMTI_ITERATION_CONTINUE) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2129
    return check_for_visit(referree);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2130
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2131
    return control != JVMTI_ITERATION_ABORT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2132
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2133
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2134
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2135
// invoke advanced style heap root callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2136
inline bool CallbackInvoker::invoke_advanced_heap_root_callback(jvmtiHeapReferenceKind ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2137
                                                                oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2138
  assert(ServiceUtil::visible_oop(obj), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2139
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2140
  AdvancedHeapWalkContext* context = advanced_context();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2141
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2142
  // check that callback is provided
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2143
  jvmtiHeapReferenceCallback cb = context->heap_reference_callback();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2144
  if (cb == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2145
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2146
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2147
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2148
  // apply class filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2149
  if (is_filtered_by_klass_filter(obj, context->klass_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2150
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2151
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2152
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2153
  // setup the callback wrapper
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2154
  CallbackWrapper wrapper(tag_map(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2155
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2156
  // apply tag filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2157
  if (is_filtered_by_heap_filter(wrapper.obj_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2158
                                 wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2159
                                 context->heap_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2160
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2161
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2162
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2163
  // for arrays we need the length, otherwise -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2164
  jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2165
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2166
  // invoke the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2167
  jint res  = (*cb)(ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2168
                    NULL, // referrer info
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2169
                    wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2170
                    0,    // referrer_class_tag is 0 for heap root
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2171
                    wrapper.obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2172
                    wrapper.obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2173
                    NULL, // referrer_tag_p
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2174
                    len,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2175
                    (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2176
  if (res & JVMTI_VISIT_ABORT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2177
    return false;// referrer class tag
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2178
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2179
  if (res & JVMTI_VISIT_OBJECTS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2180
    check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2181
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2182
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2183
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2184
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2185
// report a reference from a thread stack to an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2186
inline bool CallbackInvoker::invoke_advanced_stack_ref_callback(jvmtiHeapReferenceKind ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2187
                                                                jlong thread_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2188
                                                                jlong tid,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2189
                                                                int depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2190
                                                                jmethodID method,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2191
                                                                jlocation bci,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2192
                                                                jint slot,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2193
                                                                oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2194
  assert(ServiceUtil::visible_oop(obj), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2195
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2196
  AdvancedHeapWalkContext* context = advanced_context();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2197
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2198
  // check that callback is provider
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2199
  jvmtiHeapReferenceCallback cb = context->heap_reference_callback();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2200
  if (cb == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2201
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2202
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2203
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2204
  // apply class filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2205
  if (is_filtered_by_klass_filter(obj, context->klass_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2206
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2207
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2208
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2209
  // setup the callback wrapper
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2210
  CallbackWrapper wrapper(tag_map(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2211
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2212
  // apply tag filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2213
  if (is_filtered_by_heap_filter(wrapper.obj_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2214
                                 wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2215
                                 context->heap_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2216
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2217
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2218
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2219
  // setup the referrer info
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2220
  jvmtiHeapReferenceInfo reference_info;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2221
  reference_info.stack_local.thread_tag = thread_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2222
  reference_info.stack_local.thread_id = tid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2223
  reference_info.stack_local.depth = depth;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2224
  reference_info.stack_local.method = method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2225
  reference_info.stack_local.location = bci;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2226
  reference_info.stack_local.slot = slot;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2227
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2228
  // for arrays we need the length, otherwise -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2229
  jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2230
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2231
  // call into the agent
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2232
  int res = (*cb)(ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2233
                  &reference_info,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2234
                  wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2235
                  0,    // referrer_class_tag is 0 for heap root (stack)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2236
                  wrapper.obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2237
                  wrapper.obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2238
                  NULL, // referrer_tag is 0 for root
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2239
                  len,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2240
                  (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2241
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2242
  if (res & JVMTI_VISIT_ABORT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2243
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2244
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2245
  if (res & JVMTI_VISIT_OBJECTS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2246
    check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2247
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2248
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2249
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2250
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2251
// This mask is used to pass reference_info to a jvmtiHeapReferenceCallback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2252
// only for ref_kinds defined by the JVM TI spec. Otherwise, NULL is passed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2253
#define REF_INFO_MASK  ((1 << JVMTI_HEAP_REFERENCE_FIELD)         \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2254
                      | (1 << JVMTI_HEAP_REFERENCE_STATIC_FIELD)  \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2255
                      | (1 << JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT) \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2256
                      | (1 << JVMTI_HEAP_REFERENCE_CONSTANT_POOL) \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2257
                      | (1 << JVMTI_HEAP_REFERENCE_STACK_LOCAL)   \
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2258
                      | (1 << JVMTI_HEAP_REFERENCE_JNI_LOCAL))
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2259
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2260
// invoke the object reference callback to report a reference
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2261
inline bool CallbackInvoker::invoke_advanced_object_reference_callback(jvmtiHeapReferenceKind ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2262
                                                                       oop referrer,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2263
                                                                       oop obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2264
                                                                       jint index)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2265
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2266
  // field index is only valid field in reference_info
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2267
  static jvmtiHeapReferenceInfo reference_info = { 0 };
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2268
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2269
  assert(ServiceUtil::visible_oop(referrer), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2270
  assert(ServiceUtil::visible_oop(obj), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2271
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2272
  AdvancedHeapWalkContext* context = advanced_context();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2273
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2274
  // check that callback is provider
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2275
  jvmtiHeapReferenceCallback cb = context->heap_reference_callback();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2276
  if (cb == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2277
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2278
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2279
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2280
  // apply class filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2281
  if (is_filtered_by_klass_filter(obj, context->klass_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2282
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2283
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2284
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2285
  // setup the callback wrapper
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2286
  TwoOopCallbackWrapper wrapper(tag_map(), referrer, obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2287
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2288
  // apply tag filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2289
  if (is_filtered_by_heap_filter(wrapper.obj_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2290
                                 wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2291
                                 context->heap_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2292
    return check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2293
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2294
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2295
  // field index is only valid field in reference_info
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2296
  reference_info.field.index = index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2297
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2298
  // for arrays we need the length, otherwise -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2299
  jint len = (jint)(obj->is_array() ? arrayOop(obj)->length() : -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2300
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2301
  // invoke the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2302
  int res = (*cb)(ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2303
                  (REF_INFO_MASK & (1 << ref_kind)) ? &reference_info : NULL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2304
                  wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2305
                  wrapper.referrer_klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2306
                  wrapper.obj_size(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2307
                  wrapper.obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2308
                  wrapper.referrer_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2309
                  len,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2310
                  (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2311
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2312
  if (res & JVMTI_VISIT_ABORT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2313
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2314
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2315
  if (res & JVMTI_VISIT_OBJECTS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2316
    check_for_visit(obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2317
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2318
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2319
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2320
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2321
// report a "simple root"
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2322
inline bool CallbackInvoker::report_simple_root(jvmtiHeapReferenceKind kind, oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2323
  assert(kind != JVMTI_HEAP_REFERENCE_STACK_LOCAL &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2324
         kind != JVMTI_HEAP_REFERENCE_JNI_LOCAL, "not a simple root");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2325
  assert(ServiceUtil::visible_oop(obj), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2326
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2327
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2328
    // map to old style root kind
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2329
    jvmtiHeapRootKind root_kind = toJvmtiHeapRootKind(kind);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2330
    return invoke_basic_heap_root_callback(root_kind, obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2331
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2332
    assert(is_advanced_heap_walk(), "wrong heap walk type");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2333
    return invoke_advanced_heap_root_callback(kind, obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2334
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2335
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2336
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2337
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2338
// invoke the primitive array values
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2339
inline bool CallbackInvoker::report_primitive_array_values(oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2340
  assert(obj->is_typeArray(), "not a primitive array");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2341
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2342
  AdvancedHeapWalkContext* context = advanced_context();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2343
  assert(context->array_primitive_value_callback() != NULL, "no callback");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2344
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2345
  // apply class filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2346
  if (is_filtered_by_klass_filter(obj, context->klass_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2347
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2348
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2349
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2350
  CallbackWrapper wrapper(tag_map(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2351
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2352
  // apply tag filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2353
  if (is_filtered_by_heap_filter(wrapper.obj_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2354
                                 wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2355
                                 context->heap_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2356
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2357
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2358
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2359
  // invoke the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2360
  int res = invoke_array_primitive_value_callback(context->array_primitive_value_callback(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2361
                                                  &wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2362
                                                  obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2363
                                                  (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2364
  return (!(res & JVMTI_VISIT_ABORT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2365
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2366
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2367
// invoke the string value callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2368
inline bool CallbackInvoker::report_string_value(oop str) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2369
  assert(str->klass() == SystemDictionary::string_klass(), "not a string");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2370
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2371
  AdvancedHeapWalkContext* context = advanced_context();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2372
  assert(context->string_primitive_value_callback() != NULL, "no callback");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2373
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2374
  // apply class filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2375
  if (is_filtered_by_klass_filter(str, context->klass_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2376
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2377
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2378
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2379
  CallbackWrapper wrapper(tag_map(), str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2380
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2381
  // apply tag filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2382
  if (is_filtered_by_heap_filter(wrapper.obj_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2383
                                 wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2384
                                 context->heap_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2385
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2386
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2387
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2388
  // invoke the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2389
  int res = invoke_string_value_callback(context->string_primitive_value_callback(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2390
                                         &wrapper,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2391
                                         str,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2392
                                         (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2393
  return (!(res & JVMTI_VISIT_ABORT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2394
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2395
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2396
// invoke the primitive field callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2397
inline bool CallbackInvoker::report_primitive_field(jvmtiHeapReferenceKind ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2398
                                                    oop obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2399
                                                    jint index,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2400
                                                    address addr,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2401
                                                    char type)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2402
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2403
  // for primitive fields only the index will be set
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2404
  static jvmtiHeapReferenceInfo reference_info = { 0 };
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2405
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2406
  AdvancedHeapWalkContext* context = advanced_context();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2407
  assert(context->primitive_field_callback() != NULL, "no callback");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2408
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2409
  // apply class filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2410
  if (is_filtered_by_klass_filter(obj, context->klass_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2411
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2412
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2413
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2414
  CallbackWrapper wrapper(tag_map(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2415
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2416
  // apply tag filter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2417
  if (is_filtered_by_heap_filter(wrapper.obj_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2418
                                 wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2419
                                 context->heap_filter())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2420
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2421
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2422
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2423
  // the field index in the referrer
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2424
  reference_info.field.index = index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2425
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2426
  // map the type
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2427
  jvmtiPrimitiveType value_type = (jvmtiPrimitiveType)type;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2428
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2429
  // setup the jvalue
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2430
  jvalue value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2431
  copy_to_jvalue(&value, addr, value_type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2432
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2433
  jvmtiPrimitiveFieldCallback cb = context->primitive_field_callback();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2434
  int res = (*cb)(ref_kind,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2435
                  &reference_info,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2436
                  wrapper.klass_tag(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2437
                  wrapper.obj_tag_p(),
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2438
                  value,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2439
                  value_type,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2440
                  (void*)user_data());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2441
  return (!(res & JVMTI_VISIT_ABORT));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2442
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2443
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2444
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2445
// instance field
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2446
inline bool CallbackInvoker::report_primitive_instance_field(oop obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2447
                                                             jint index,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2448
                                                             address value,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2449
                                                             char type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2450
  return report_primitive_field(JVMTI_HEAP_REFERENCE_FIELD,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2451
                                obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2452
                                index,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2453
                                value,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2454
                                type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2455
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2456
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2457
// static field
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2458
inline bool CallbackInvoker::report_primitive_static_field(oop obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2459
                                                           jint index,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2460
                                                           address value,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2461
                                                           char type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2462
  return report_primitive_field(JVMTI_HEAP_REFERENCE_STATIC_FIELD,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2463
                                obj,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2464
                                index,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2465
                                value,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2466
                                type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2467
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2468
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2469
// report a JNI local (root object) to the profiler
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2470
inline bool CallbackInvoker::report_jni_local_root(jlong thread_tag, jlong tid, jint depth, jmethodID m, oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2471
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2472
    return invoke_basic_stack_ref_callback(JVMTI_HEAP_ROOT_JNI_LOCAL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2473
                                           thread_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2474
                                           depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2475
                                           m,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2476
                                           -1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2477
                                           obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2478
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2479
    return invoke_advanced_stack_ref_callback(JVMTI_HEAP_REFERENCE_JNI_LOCAL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2480
                                              thread_tag, tid,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2481
                                              depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2482
                                              m,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2483
                                              (jlocation)-1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2484
                                              -1,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2485
                                              obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2486
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2487
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2488
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2489
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2490
// report a local (stack reference, root object)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2491
inline bool CallbackInvoker::report_stack_ref_root(jlong thread_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2492
                                                   jlong tid,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2493
                                                   jint depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2494
                                                   jmethodID method,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2495
                                                   jlocation bci,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2496
                                                   jint slot,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2497
                                                   oop obj) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2498
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2499
    return invoke_basic_stack_ref_callback(JVMTI_HEAP_ROOT_STACK_LOCAL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2500
                                           thread_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2501
                                           depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2502
                                           method,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2503
                                           slot,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2504
                                           obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2505
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2506
    return invoke_advanced_stack_ref_callback(JVMTI_HEAP_REFERENCE_STACK_LOCAL,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2507
                                              thread_tag,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2508
                                              tid,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2509
                                              depth,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2510
                                              method,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2511
                                              bci,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2512
                                              slot,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2513
                                              obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2514
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2515
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2516
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2517
// report an object referencing a class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2518
inline bool CallbackInvoker::report_class_reference(oop referrer, oop referree) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2519
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2520
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CLASS, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2521
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2522
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_CLASS, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2523
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2524
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2525
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2526
// report a class referencing its class loader.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2527
inline bool CallbackInvoker::report_class_loader_reference(oop referrer, oop referree) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2528
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2529
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CLASS_LOADER, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2530
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2531
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_CLASS_LOADER, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2532
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2533
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2534
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2535
// report a class referencing its signers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2536
inline bool CallbackInvoker::report_signers_reference(oop referrer, oop referree) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2537
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2538
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_SIGNERS, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2539
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2540
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_SIGNERS, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2541
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2542
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2543
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2544
// report a class referencing its protection domain..
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2545
inline bool CallbackInvoker::report_protection_domain_reference(oop referrer, oop referree) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2546
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2547
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_PROTECTION_DOMAIN, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2548
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2549
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_PROTECTION_DOMAIN, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2550
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2551
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2552
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2553
// report a class referencing its superclass.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2554
inline bool CallbackInvoker::report_superclass_reference(oop referrer, oop referree) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2555
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2556
    // Send this to be consistent with past implementation
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2557
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CLASS, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2558
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2559
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_SUPERCLASS, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2560
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2561
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2562
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2563
// report a class referencing one of its interfaces.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2564
inline bool CallbackInvoker::report_interface_reference(oop referrer, oop referree) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2565
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2566
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_INTERFACE, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2567
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2568
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_INTERFACE, referrer, referree, -1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2569
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2570
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2571
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2572
// report a class referencing one of its static fields.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2573
inline bool CallbackInvoker::report_static_field_reference(oop referrer, oop referree, jint slot) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2574
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2575
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_STATIC_FIELD, referrer, referree, slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2576
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2577
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_STATIC_FIELD, referrer, referree, slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2578
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2579
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2580
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2581
// report an array referencing an element object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2582
inline bool CallbackInvoker::report_array_element_reference(oop referrer, oop referree, jint index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2583
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2584
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_ARRAY_ELEMENT, referrer, referree, index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2585
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2586
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_ARRAY_ELEMENT, referrer, referree, index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2587
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2588
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2589
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2590
// report an object referencing an instance field object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2591
inline bool CallbackInvoker::report_field_reference(oop referrer, oop referree, jint slot) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2592
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2593
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_FIELD, referrer, referree, slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2594
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2595
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_FIELD, referrer, referree, slot);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2596
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2597
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2598
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2599
// report an array referencing an element object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2600
inline bool CallbackInvoker::report_constant_pool_reference(oop referrer, oop referree, jint index) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2601
  if (is_basic_heap_walk()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2602
    return invoke_basic_object_reference_callback(JVMTI_REFERENCE_CONSTANT_POOL, referrer, referree, index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2603
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2604
    return invoke_advanced_object_reference_callback(JVMTI_HEAP_REFERENCE_CONSTANT_POOL, referrer, referree, index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2605
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2606
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2607
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2608
// A supporting closure used to process simple roots
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2609
class SimpleRootsClosure : public OopClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2610
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2611
  jvmtiHeapReferenceKind _kind;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2612
  bool _continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2613
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2614
  jvmtiHeapReferenceKind root_kind()    { return _kind; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2615
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2616
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2617
  void set_kind(jvmtiHeapReferenceKind kind) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2618
    _kind = kind;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2619
    _continue = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2620
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2621
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2622
  inline bool stopped() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2623
    return !_continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2624
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2625
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2626
  void do_oop(oop* obj_p) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2627
    // iteration has terminated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2628
    if (stopped()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2629
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2630
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2631
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2632
    // ignore null or deleted handles
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2633
    oop o = *obj_p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2634
    if (o == NULL || o == JNIHandles::deleted_handle()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2635
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2636
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2637
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2638
    jvmtiHeapReferenceKind kind = root_kind();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2639
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2640
    // many roots are Klasses so we use the java mirror
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2641
    if (o->is_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2642
      klassOop k = (klassOop)o;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2643
      o = Klass::cast(k)->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2644
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2645
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2646
      // SystemDictionary::always_strong_oops_do reports the application
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2647
      // class loader as a root. We want this root to be reported as
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2648
      // a root kind of "OTHER" rather than "SYSTEM_CLASS".
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2649
      if (o->is_instance() && root_kind() == JVMTI_HEAP_REFERENCE_SYSTEM_CLASS) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2650
        kind = JVMTI_HEAP_REFERENCE_OTHER;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2651
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2652
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2653
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2654
    // some objects are ignored - in the case of simple
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2655
    // roots it's mostly symbolOops that we are skipping
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2656
    // here.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2657
    if (!ServiceUtil::visible_oop(o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2658
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2659
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2660
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2661
    // invoke the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2662
    _continue = CallbackInvoker::report_simple_root(kind, o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2663
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2664
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2665
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2666
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2667
// A supporting closure used to process JNI locals
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2668
class JNILocalRootsClosure : public OopClosure {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2669
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2670
  jlong _thread_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2671
  jlong _tid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2672
  jint _depth;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2673
  jmethodID _method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2674
  bool _continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2675
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2676
  void set_context(jlong thread_tag, jlong tid, jint depth, jmethodID method) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2677
    _thread_tag = thread_tag;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2678
    _tid = tid;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2679
    _depth = depth;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2680
    _method = method;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2681
    _continue = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2682
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2683
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2684
  inline bool stopped() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2685
    return !_continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2686
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2687
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2688
  void do_oop(oop* obj_p) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2689
    // iteration has terminated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2690
    if (stopped()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2691
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2692
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2693
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2694
    // ignore null or deleted handles
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2695
    oop o = *obj_p;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2696
    if (o == NULL || o == JNIHandles::deleted_handle()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2697
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2698
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2699
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2700
    if (!ServiceUtil::visible_oop(o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2701
      return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2702
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2703
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2704
    // invoke the callback
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2705
    _continue = CallbackInvoker::report_jni_local_root(_thread_tag, _tid, _depth, _method, o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2706
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2707
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2708
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2709
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2710
// A VM operation to iterate over objects that are reachable from
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2711
// a set of roots or an initial object.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2712
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2713
// For VM_HeapWalkOperation the set of roots used is :-
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2714
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2715
// - All JNI global references
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2716
// - All inflated monitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2717
// - All classes loaded by the boot class loader (or all classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2718
//     in the event that class unloading is disabled)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2719
// - All java threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2720
// - For each java thread then all locals and JNI local references
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2721
//      on the thread's execution stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2722
// - All visible/explainable objects from Universes::oops_do
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2723
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2724
class VM_HeapWalkOperation: public VM_Operation {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2725
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2726
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2727
    initial_visit_stack_size = 4000
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2728
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2729
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2730
  bool _is_advanced_heap_walk;                      // indicates FollowReferences
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2731
  JvmtiTagMap* _tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2732
  Handle _initial_object;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2733
  GrowableArray<oop>* _visit_stack;                 // the visit stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2734
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2735
  bool _collecting_heap_roots;                      // are we collecting roots
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2736
  bool _following_object_refs;                      // are we following object references
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2737
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2738
  bool _reporting_primitive_fields;                 // optional reporting
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2739
  bool _reporting_primitive_array_values;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2740
  bool _reporting_string_values;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2741
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2742
  GrowableArray<oop>* create_visit_stack() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2743
    return new (ResourceObj::C_HEAP) GrowableArray<oop>(initial_visit_stack_size, true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2744
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2745
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2746
  // accessors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2747
  bool is_advanced_heap_walk() const               { return _is_advanced_heap_walk; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2748
  JvmtiTagMap* tag_map() const                     { return _tag_map; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2749
  Handle initial_object() const                    { return _initial_object; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2750
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2751
  bool is_following_references() const             { return _following_object_refs; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2752
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2753
  bool is_reporting_primitive_fields()  const      { return _reporting_primitive_fields; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2754
  bool is_reporting_primitive_array_values() const { return _reporting_primitive_array_values; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2755
  bool is_reporting_string_values() const          { return _reporting_string_values; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2756
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2757
  GrowableArray<oop>* visit_stack() const          { return _visit_stack; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2758
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2759
  // iterate over the various object types
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2760
  inline bool iterate_over_array(oop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2761
  inline bool iterate_over_type_array(oop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2762
  inline bool iterate_over_class(klassOop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2763
  inline bool iterate_over_object(oop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2764
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2765
  // root collection
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2766
  inline bool collect_simple_roots();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2767
  inline bool collect_stack_roots();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2768
  inline bool collect_stack_roots(JavaThread* java_thread, JNILocalRootsClosure* blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2769
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2770
  // visit an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2771
  inline bool visit(oop o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2772
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2773
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2774
  VM_HeapWalkOperation(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2775
                       Handle initial_object,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2776
                       BasicHeapWalkContext callbacks,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2777
                       const void* user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2778
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2779
  VM_HeapWalkOperation(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2780
                       Handle initial_object,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2781
                       AdvancedHeapWalkContext callbacks,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2782
                       const void* user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2783
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2784
  ~VM_HeapWalkOperation();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2785
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2786
  VMOp_Type type() const { return VMOp_HeapWalkOperation; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2787
  void doit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2788
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2789
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2790
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2791
VM_HeapWalkOperation::VM_HeapWalkOperation(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2792
                                           Handle initial_object,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2793
                                           BasicHeapWalkContext callbacks,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2794
                                           const void* user_data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2795
  _is_advanced_heap_walk = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2796
  _tag_map = tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2797
  _initial_object = initial_object;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2798
  _following_object_refs = (callbacks.object_ref_callback() != NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2799
  _reporting_primitive_fields = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2800
  _reporting_primitive_array_values = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2801
  _reporting_string_values = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2802
  _visit_stack = create_visit_stack();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2803
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2804
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2805
  CallbackInvoker::initialize_for_basic_heap_walk(tag_map, _visit_stack, user_data, callbacks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2806
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2807
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2808
VM_HeapWalkOperation::VM_HeapWalkOperation(JvmtiTagMap* tag_map,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2809
                                           Handle initial_object,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2810
                                           AdvancedHeapWalkContext callbacks,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2811
                                           const void* user_data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2812
  _is_advanced_heap_walk = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2813
  _tag_map = tag_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2814
  _initial_object = initial_object;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2815
  _following_object_refs = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2816
  _reporting_primitive_fields = (callbacks.primitive_field_callback() != NULL);;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2817
  _reporting_primitive_array_values = (callbacks.array_primitive_value_callback() != NULL);;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2818
  _reporting_string_values = (callbacks.string_primitive_value_callback() != NULL);;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2819
  _visit_stack = create_visit_stack();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2820
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2821
  CallbackInvoker::initialize_for_advanced_heap_walk(tag_map, _visit_stack, user_data, callbacks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2822
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2823
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2824
VM_HeapWalkOperation::~VM_HeapWalkOperation() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2825
  if (_following_object_refs) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2826
    assert(_visit_stack != NULL, "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2827
    delete _visit_stack;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2828
    _visit_stack = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2829
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2830
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2831
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2832
// an array references its class and has a reference to
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2833
// each element in the array
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2834
inline bool VM_HeapWalkOperation::iterate_over_array(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2835
  objArrayOop array = objArrayOop(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2836
  if (array->klass() == Universe::systemObjArrayKlassObj()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2837
    // filtered out
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2838
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2839
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2840
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2841
  // array reference to its class
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2842
  oop mirror = objArrayKlass::cast(array->klass())->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2843
  if (!CallbackInvoker::report_class_reference(o, mirror)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2844
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2845
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2846
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2847
  // iterate over the array and report each reference to a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2848
  // non-null element
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2849
  for (int index=0; index<array->length(); index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2850
    oop elem = array->obj_at(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2851
    if (elem == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2852
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2853
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2854
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2855
    // report the array reference o[index] = elem
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2856
    if (!CallbackInvoker::report_array_element_reference(o, elem, index)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2857
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2858
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2859
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2860
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2861
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2862
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2863
// a type array references its class
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2864
inline bool VM_HeapWalkOperation::iterate_over_type_array(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2865
  klassOop k = o->klass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2866
  oop mirror = Klass::cast(k)->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2867
  if (!CallbackInvoker::report_class_reference(o, mirror)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2868
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2869
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2870
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2871
  // report the array contents if required
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2872
  if (is_reporting_primitive_array_values()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2873
    if (!CallbackInvoker::report_primitive_array_values(o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2874
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2875
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2876
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2877
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2878
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2879
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2880
// verify that a static oop field is in range
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2881
static inline bool verify_static_oop(instanceKlass* ik, oop* obj_p) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2882
  oop* start = ik->start_of_static_fields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2883
  oop* end = start + ik->static_oop_field_size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2884
  assert(end >= start, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2885
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2886
  if (obj_p >= start && obj_p < end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2887
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2888
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2889
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2890
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2891
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2892
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2893
// a class references its super class, interfaces, class loader, ...
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2894
// and finally its static fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2895
inline bool VM_HeapWalkOperation::iterate_over_class(klassOop k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2896
  int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2897
  Klass* klass = klassOop(k)->klass_part();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2898
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2899
  if (klass->oop_is_instance()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2900
    instanceKlass* ik = instanceKlass::cast(k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2901
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2902
    // ignore the class if it's has been initialized yet
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2903
    if (!ik->is_linked()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2904
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2905
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2906
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2907
    // get the java mirror
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2908
    oop mirror = klass->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2909
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2910
    // super (only if something more interesting than java.lang.Object)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2911
    klassOop java_super = ik->java_super();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2912
    if (java_super != NULL && java_super != SystemDictionary::object_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2913
      oop super = Klass::cast(java_super)->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2914
      if (!CallbackInvoker::report_superclass_reference(mirror, super)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2915
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2916
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2917
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2918
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2919
    // class loader
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2920
    oop cl = ik->class_loader();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2921
    if (cl != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2922
      if (!CallbackInvoker::report_class_loader_reference(mirror, cl)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2923
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2924
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2925
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2926
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2927
    // protection domain
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2928
    oop pd = ik->protection_domain();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2929
    if (pd != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2930
      if (!CallbackInvoker::report_protection_domain_reference(mirror, pd)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2931
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2932
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2933
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2934
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2935
    // signers
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2936
    oop signers = ik->signers();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2937
    if (signers != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2938
      if (!CallbackInvoker::report_signers_reference(mirror, signers)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2939
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2940
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2941
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2942
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2943
    // references from the constant pool
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2944
    {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2945
      const constantPoolOop pool = ik->constants();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2946
      for (int i = 1; i < pool->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2947
        constantTag tag = pool->tag_at(i).value();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2948
        if (tag.is_string() || tag.is_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2949
          oop entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2950
          if (tag.is_string()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2951
            entry = pool->resolved_string_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2952
            assert(java_lang_String::is_instance(entry), "must be string");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2953
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2954
            entry = Klass::cast(pool->resolved_klass_at(i))->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2955
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2956
          if (!CallbackInvoker::report_constant_pool_reference(mirror, entry, (jint)i)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2957
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2958
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2959
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2960
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2961
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2962
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2963
    // interfaces
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2964
    // (These will already have been reported as references from the constant pool
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2965
    //  but are specified by IterateOverReachableObjects and must be reported).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2966
    objArrayOop interfaces = ik->local_interfaces();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2967
    for (i = 0; i < interfaces->length(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2968
      oop interf = Klass::cast((klassOop)interfaces->obj_at(i))->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2969
      if (interf == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2970
        continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2971
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2972
      if (!CallbackInvoker::report_interface_reference(mirror, interf)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2973
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2974
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2975
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2976
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2977
    // iterate over the static fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2978
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2979
    ClassFieldMap* field_map = ClassFieldMap::create_map_of_static_fields(k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2980
    for (i=0; i<field_map->field_count(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2981
      ClassFieldDescriptor* field = field_map->field_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2982
      char type = field->field_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2983
      if (!is_primitive_field_type(type)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2984
        address addr = (address)k + field->field_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2985
        oop* f = (oop*)addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2986
        assert(verify_static_oop(ik, f), "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2987
        oop fld_o = *f;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2988
        if (fld_o != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2989
          int slot = field->field_index();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2990
          if (!CallbackInvoker::report_static_field_reference(mirror, fld_o, slot)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2991
            delete field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2992
            return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2993
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2994
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2995
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2996
         if (is_reporting_primitive_fields()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2997
           address addr = (address)k + field->field_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2998
           int slot = field->field_index();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2999
           if (!CallbackInvoker::report_primitive_static_field(mirror, slot, addr, type)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3000
             delete field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3001
             return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3002
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3003
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3004
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3005
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3006
    delete field_map;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3007
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3008
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3009
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3010
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3011
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3012
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3013
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3014
// an object references a class and its instance fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3015
// (static fields are ignored here as we report these as
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3016
// references from the class).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3017
inline bool VM_HeapWalkOperation::iterate_over_object(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3018
  // reference to the class
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3019
  if (!CallbackInvoker::report_class_reference(o, Klass::cast(o->klass())->java_mirror())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3020
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3021
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3022
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3023
  // iterate over instance fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3024
  ClassFieldMap* field_map = JvmtiCachedClassFieldMap::get_map_of_instance_fields(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3025
  for (int i=0; i<field_map->field_count(); i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3026
    ClassFieldDescriptor* field = field_map->field_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3027
    char type = field->field_type();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3028
    if (!is_primitive_field_type(type)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3029
      address addr = (address)o + field->field_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3030
      oop* f = (oop*)addr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3031
      oop fld_o = *f;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3032
      if (fld_o != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3033
        // reflection code may have a reference to a klassOop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3034
        // - see sun.reflect.UnsafeStaticFieldAccessorImpl and sun.misc.Unsafe
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3035
        if (fld_o->is_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3036
          klassOop k = (klassOop)fld_o;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3037
          fld_o = Klass::cast(k)->java_mirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3038
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3039
        int slot = field->field_index();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3040
        if (!CallbackInvoker::report_field_reference(o, fld_o, slot)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3041
          return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3042
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3043
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3044
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3045
      if (is_reporting_primitive_fields()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3046
        // primitive instance field
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3047
        address addr = (address)o + field->field_offset();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3048
        int slot = field->field_index();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3049
        if (!CallbackInvoker::report_primitive_instance_field(o, slot, addr, type)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3050
          return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3051
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3052
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3053
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3054
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3055
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3056
  // if the object is a java.lang.String
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3057
  if (is_reporting_string_values() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3058
      o->klass() == SystemDictionary::string_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3059
    if (!CallbackInvoker::report_string_value(o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3060
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3061
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3062
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3063
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3064
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3065
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3066
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3067
// collects all simple (non-stack) roots.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3068
// if there's a heap root callback provided then the callback is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3069
// invoked for each simple root.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3070
// if an object reference callback is provided then all simple
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3071
// roots are pushed onto the marking stack so that they can be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3072
// processed later
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3073
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3074
inline bool VM_HeapWalkOperation::collect_simple_roots() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3075
  SimpleRootsClosure blk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3076
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3077
  // JNI globals
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3078
  blk.set_kind(JVMTI_HEAP_REFERENCE_JNI_GLOBAL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3079
  JNIHandles::oops_do(&blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3080
  if (blk.stopped()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3081
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3082
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3083
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3084
  // Preloaded classes and loader from the system dictionary
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3085
  blk.set_kind(JVMTI_HEAP_REFERENCE_SYSTEM_CLASS);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3086
  SystemDictionary::always_strong_oops_do(&blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3087
  if (blk.stopped()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3088
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3089
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3090
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3091
  // Inflated monitors
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3092
  blk.set_kind(JVMTI_HEAP_REFERENCE_MONITOR);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3093
  ObjectSynchronizer::oops_do(&blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3094
  if (blk.stopped()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3095
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3096
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3097
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3098
  // Threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3099
  for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3100
    oop threadObj = thread->threadObj();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3101
    if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3102
      bool cont = CallbackInvoker::report_simple_root(JVMTI_HEAP_REFERENCE_THREAD, threadObj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3103
      if (!cont) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3104
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3105
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3106
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3107
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3108
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3109
  // Other kinds of roots maintained by HotSpot
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3110
  // Many of these won't be visible but others (such as instances of important
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3111
  // exceptions) will be visible.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3112
  blk.set_kind(JVMTI_HEAP_REFERENCE_OTHER);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3113
  Universe::oops_do(&blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3114
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3115
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3116
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3117
// Walk the stack of a given thread and find all references (locals
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3118
// and JNI calls) and report these as stack references
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3119
inline bool VM_HeapWalkOperation::collect_stack_roots(JavaThread* java_thread,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3120
                                                      JNILocalRootsClosure* blk)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3121
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3122
  oop threadObj = java_thread->threadObj();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3123
  assert(threadObj != NULL, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3124
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3125
  // only need to get the thread's tag once per thread
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3126
  jlong thread_tag = tag_for(_tag_map, threadObj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3127
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3128
  // also need the thread id
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3129
  jlong tid = java_lang_Thread::thread_id(threadObj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3130
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3131
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3132
  if (java_thread->has_last_Java_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3133
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3134
    // vframes are resource allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3135
    Thread* current_thread = Thread::current();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3136
    ResourceMark rm(current_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3137
    HandleMark hm(current_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3138
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3139
    RegisterMap reg_map(java_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3140
    frame f = java_thread->last_frame();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3141
    vframe* vf = vframe::new_vframe(&f, &reg_map, java_thread);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3142
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3143
    bool is_top_frame = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3144
    int depth = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3145
    frame* last_entry_frame = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3146
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3147
    while (vf != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3148
      if (vf->is_java_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3149
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3150
        // java frame (interpreted, compiled, ...)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3151
        javaVFrame *jvf = javaVFrame::cast(vf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3152
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3153
        // the jmethodID
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3154
        jmethodID method = jvf->method()->jmethod_id();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3155
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3156
        if (!(jvf->method()->is_native())) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3157
          jlocation bci = (jlocation)jvf->bci();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3158
          StackValueCollection* locals = jvf->locals();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3159
          for (int slot=0; slot<locals->size(); slot++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3160
            if (locals->at(slot)->type() == T_OBJECT) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3161
              oop o = locals->obj_at(slot)();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3162
              if (o == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3163
                continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3164
              }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3165
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3166
              // stack reference
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3167
              if (!CallbackInvoker::report_stack_ref_root(thread_tag, tid, depth, method,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3168
                                                   bci, slot, o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3169
                return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3170
              }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3171
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3172
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3173
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3174
          blk->set_context(thread_tag, tid, depth, method);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3175
          if (is_top_frame) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3176
            // JNI locals for the top frame.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3177
            java_thread->active_handles()->oops_do(blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3178
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3179
            if (last_entry_frame != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3180
              // JNI locals for the entry frame
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3181
              assert(last_entry_frame->is_entry_frame(), "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3182
              last_entry_frame->entry_frame_call_wrapper()->handles()->oops_do(blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3183
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3184
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3185
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3186
        last_entry_frame = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3187
        depth++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3188
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3189
        // externalVFrame - for an entry frame then we report the JNI locals
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3190
        // when we find the corresponding javaVFrame
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3191
        frame* fr = vf->frame_pointer();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3192
        assert(fr != NULL, "sanity check");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3193
        if (fr->is_entry_frame()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3194
          last_entry_frame = fr;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3195
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3196
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3197
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3198
      vf = vf->sender();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3199
      is_top_frame = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3200
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3201
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3202
    // no last java frame but there may be JNI locals
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3203
    blk->set_context(thread_tag, tid, 0, (jmethodID)NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3204
    java_thread->active_handles()->oops_do(blk);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3205
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3206
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3207
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3208
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3209
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3210
// collects all stack roots - for each thread it walks the execution
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3211
// stack to find all references and local JNI refs.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3212
inline bool VM_HeapWalkOperation::collect_stack_roots() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3213
  JNILocalRootsClosure blk;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3214
  for (JavaThread* thread = Threads::first(); thread != NULL ; thread = thread->next()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3215
    oop threadObj = thread->threadObj();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3216
    if (threadObj != NULL && !thread->is_exiting() && !thread->is_hidden_from_external_view()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3217
      if (!collect_stack_roots(thread, &blk)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3218
        return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3219
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3220
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3221
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3222
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3223
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3224
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3225
// visit an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3226
// first mark the object as visited
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3227
// second get all the outbound references from this object (in other words, all
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3228
// the objects referenced by this object).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3229
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3230
bool VM_HeapWalkOperation::visit(oop o) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3231
  // mark object as visited
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3232
  assert(!ObjectMarker::visited(o), "can't visit same object more than once");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3233
  ObjectMarker::mark(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3234
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3235
  // instance
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3236
  if (o->is_instance()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3237
    if (o->klass() == SystemDictionary::class_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3238
      o = klassOop_if_java_lang_Class(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3239
      if (o->is_klass()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3240
        // a java.lang.Class
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3241
        return iterate_over_class(klassOop(o));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3242
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3243
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3244
      return iterate_over_object(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3245
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3246
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3247
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3248
  // object array
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3249
  if (o->is_objArray()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3250
    return iterate_over_array(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3251
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3252
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3253
  // type array
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3254
  if (o->is_typeArray()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3255
    return iterate_over_type_array(o);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3256
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3257
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3258
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3259
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3260
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3261
void VM_HeapWalkOperation::doit() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3262
  ResourceMark rm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3263
  ObjectMarkerController marker;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3264
  ClassFieldMapCacheMark cm;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3265
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3266
  assert(visit_stack()->is_empty(), "visit stack must be empty");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3267
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3268
  // the heap walk starts with an initial object or the heap roots
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3269
  if (initial_object().is_null()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3270
    if (!collect_simple_roots()) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3271
    if (!collect_stack_roots()) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3272
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3273
    visit_stack()->push(initial_object()());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3274
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3275
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3276
  // object references required
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3277
  if (is_following_references()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3278
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3279
    // visit each object until all reachable objects have been
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3280
    // visited or the callback asked to terminate the iteration.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3281
    while (!visit_stack()->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3282
      oop o = visit_stack()->pop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3283
      if (!ObjectMarker::visited(o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3284
        if (!visit(o)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3285
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3286
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3287
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3288
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3289
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3290
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3291
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3292
// iterate over all objects that are reachable from a set of roots
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3293
void JvmtiTagMap::iterate_over_reachable_objects(jvmtiHeapRootCallback heap_root_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3294
                                                 jvmtiStackReferenceCallback stack_ref_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3295
                                                 jvmtiObjectReferenceCallback object_ref_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3296
                                                 const void* user_data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3297
  MutexLocker ml(Heap_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3298
  BasicHeapWalkContext context(heap_root_callback, stack_ref_callback, object_ref_callback);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3299
  VM_HeapWalkOperation op(this, Handle(), context, user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3300
  VMThread::execute(&op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3301
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3302
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3303
// iterate over all objects that are reachable from a given object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3304
void JvmtiTagMap::iterate_over_objects_reachable_from_object(jobject object,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3305
                                                             jvmtiObjectReferenceCallback object_ref_callback,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3306
                                                             const void* user_data) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3307
  oop obj = JNIHandles::resolve(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3308
  Handle initial_object(Thread::current(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3309
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3310
  MutexLocker ml(Heap_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3311
  BasicHeapWalkContext context(NULL, NULL, object_ref_callback);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3312
  VM_HeapWalkOperation op(this, initial_object, context, user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3313
  VMThread::execute(&op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3314
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3315
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3316
// follow references from an initial object or the GC roots
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3317
void JvmtiTagMap::follow_references(jint heap_filter,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3318
                                    KlassHandle klass,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3319
                                    jobject object,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3320
                                    const jvmtiHeapCallbacks* callbacks,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3321
                                    const void* user_data)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3322
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3323
  oop obj = JNIHandles::resolve(object);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3324
  Handle initial_object(Thread::current(), obj);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3325
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3326
  MutexLocker ml(Heap_lock);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3327
  AdvancedHeapWalkContext context(heap_filter, klass, callbacks);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3328
  VM_HeapWalkOperation op(this, initial_object, context, user_data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3329
  VMThread::execute(&op);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3330
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3331
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3332
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3333
// called post-GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3334
// - for each JVMTI environment with an object tag map, call its rehash
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3335
// function to re-sync with the new object locations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3336
void JvmtiTagMap::gc_epilogue(bool full) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3337
  assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3338
  if (JvmtiEnv::environments_might_exist()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3339
    // re-obtain the memory region for the young generation (might
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3340
    // changed due to adaptive resizing policy)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3341
    get_young_generation();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3342
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3343
    JvmtiEnvIterator it;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3344
    for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3345
      JvmtiTagMap* tag_map = env->tag_map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3346
      if (tag_map != NULL && !tag_map->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3347
        TraceTime t(full ? "JVMTI Full Rehash " : "JVMTI Rehash ", TraceJVMTIObjectTagging);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3348
        if (full) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3349
          tag_map->rehash(0, n_hashmaps);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3350
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3351
          tag_map->rehash(0, 0);        // tag map for young gen only
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3352
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3353
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3354
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3355
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3356
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3357
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3358
// CMS has completed referencing processing so we may have JNI weak refs
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3359
// to objects in the CMS generation that have been GC'ed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3360
void JvmtiTagMap::cms_ref_processing_epilogue() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3361
  assert(SafepointSynchronize::is_at_safepoint(), "must be executed at a safepoint");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3362
  assert(UseConcMarkSweepGC, "should only be used with CMS");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3363
  if (JvmtiEnv::environments_might_exist()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3364
    JvmtiEnvIterator it;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3365
    for (JvmtiEnvBase* env = it.first(); env != NULL; env = it.next(env)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3366
      JvmtiTagMap* tag_map = ((JvmtiEnvBase *)env)->tag_map();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3367
      if (tag_map != NULL && !tag_map->is_empty()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3368
        TraceTime t("JVMTI Rehash (CMS) ", TraceJVMTIObjectTagging);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3369
        tag_map->rehash(1, n_hashmaps);    // assume CMS not used in young gen
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3370
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3371
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3372
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3373
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3374
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3375
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3376
// For each entry in the hashmaps 'start' to 'end' :
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3377
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3378
// 1. resolve the JNI weak reference
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3379
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3380
// 2. If it resolves to NULL it means the object has been freed so the entry
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3381
//    is removed, the weak reference destroyed, and the object free event is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3382
//    posted (if enabled).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3383
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3384
// 3. If the weak reference resolves to an object then we re-hash the object
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3385
//    to see if it has moved or has been promoted (from the young to the old
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3386
//    generation for example).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3387
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3388
void JvmtiTagMap::rehash(int start, int end) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3389
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3390
  // does this environment have the OBJECT_FREE event enabled
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3391
  bool post_object_free = env()->is_enabled(JVMTI_EVENT_OBJECT_FREE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3392
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3393
  // counters used for trace message
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3394
  int freed = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3395
  int moved = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3396
  int promoted = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3397
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3398
  // we assume there are two hashmaps - one for the young generation
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3399
  // and the other for all other spaces.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3400
  assert(n_hashmaps == 2, "not implemented");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3401
  JvmtiTagHashmap* young_hashmap = _hashmap[0];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3402
  JvmtiTagHashmap* other_hashmap = _hashmap[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3403
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3404
  // reenable sizing (if disabled)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3405
  young_hashmap->set_resizing_enabled(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3406
  other_hashmap->set_resizing_enabled(true);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3407
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3408
  // when re-hashing the hashmap corresponding to the young generation we
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3409
  // collect the entries corresponding to objects that have been promoted.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3410
  JvmtiTagHashmapEntry* promoted_entries = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3411
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3412
  if (end >= n_hashmaps) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3413
    end = n_hashmaps - 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3414
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3415
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3416
  for (int i=start; i <= end; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3417
    JvmtiTagHashmap* hashmap = _hashmap[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3418
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3419
    // if the hashmap is empty then we can skip it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3420
    if (hashmap->_entry_count == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3421
      continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3422
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3423
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3424
    // now iterate through each entry in the table
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3425
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3426
    JvmtiTagHashmapEntry** table = hashmap->table();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3427
    int size = hashmap->size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3428
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3429
    for (int pos=0; pos<size; pos++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3430
      JvmtiTagHashmapEntry* entry = table[pos];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3431
      JvmtiTagHashmapEntry* prev = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3432
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3433
      while (entry != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3434
        JvmtiTagHashmapEntry* next = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3435
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3436
        jweak ref = entry->object();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3437
        oop oop = JNIHandles::resolve(ref);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3438
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3439
        // has object been GC'ed
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3440
        if (oop == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3441
          // grab the tag
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3442
          jlong tag = entry->tag();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3443
          guarantee(tag != 0, "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3444
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3445
          // remove GC'ed entry from hashmap and return the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3446
          // entry to the free list
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3447
          hashmap->remove(prev, pos, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3448
          destroy_entry(entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3449
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3450
          // destroy the weak ref
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3451
          JNIHandles::destroy_weak_global(ref);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3452
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3453
          // post the event to the profiler
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3454
          if (post_object_free) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3455
            JvmtiExport::post_object_free(env(), tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3456
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3457
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3458
          freed++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3459
          entry = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3460
          continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3461
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3462
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3463
        // if this is the young hashmap then the object is either promoted
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3464
        // or moved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3465
        // if this is the other hashmap then the object is moved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3466
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3467
        bool same_gen;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3468
        if (i == 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3469
          assert(hashmap == young_hashmap, "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3470
          same_gen = is_in_young(oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3471
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3472
          same_gen = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3473
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3474
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3475
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3476
        if (same_gen) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3477
          // if the object has moved then re-hash it and move its
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3478
          // entry to its new location.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3479
          unsigned int new_pos = JvmtiTagHashmap::hash(oop, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3480
          if (new_pos != (unsigned int)pos) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3481
            if (prev == NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3482
              table[pos] = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3483
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3484
              prev->set_next(next);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3485
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3486
            entry->set_next(table[new_pos]);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3487
            table[new_pos] = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3488
            moved++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3489
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3490
            // object didn't move
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3491
            prev = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3492
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3493
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3494
          // object has been promoted so remove the entry from the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3495
          // young hashmap
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3496
          assert(hashmap == young_hashmap, "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3497
          hashmap->remove(prev, pos, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3498
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3499
          // move the entry to the promoted list
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3500
          entry->set_next(promoted_entries);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3501
          promoted_entries = entry;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3502
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3503
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3504
        entry = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3505
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3506
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3507
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3508
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3509
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3510
  // add the entries, corresponding to the promoted objects, to the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3511
  // other hashmap.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3512
  JvmtiTagHashmapEntry* entry = promoted_entries;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3513
  while (entry != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3514
    oop o = JNIHandles::resolve(entry->object());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3515
    assert(hashmap_for(o) == other_hashmap, "checking");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3516
    JvmtiTagHashmapEntry* next = entry->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3517
    other_hashmap->add(o, entry);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3518
    entry = next;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3519
    promoted++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3520
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3521
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3522
  // stats
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3523
  if (TraceJVMTIObjectTagging) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3524
    int total_moves = promoted + moved;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3525
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3526
    int post_total = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3527
    for (int i=0; i<n_hashmaps; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3528
      post_total += _hashmap[i]->_entry_count;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3529
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3530
    int pre_total = post_total + freed;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3531
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3532
    tty->print("(%d->%d, %d freed, %d promoted, %d total moves)",
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3533
        pre_total, post_total, freed, promoted, total_moves);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3534
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3535
}