hotspot/agent/src/share/classes/sun/jvm/hotspot/utilities/HeapHprofBinWriter.java
author coleenp
Wed, 22 May 2013 14:37:49 -0400
changeset 17826 9ad5cd464a75
parent 13728 882756847a04
child 20388 2cf7b26682dc
permissions -rw-r--r--
8003421: NPG: Move oops out of InstanceKlass into mirror Summary: Inject protection_domain, signers, init_lock into java_lang_Class Reviewed-by: stefank, dholmes, sla
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 8878
diff changeset
     2
 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 670
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
package sun.jvm.hotspot.utilities;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
import java.nio.channels.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
import sun.jvm.hotspot.debugger.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
import sun.jvm.hotspot.memory.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
import sun.jvm.hotspot.oops.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
import sun.jvm.hotspot.runtime.*;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
 * This class writes Java heap in hprof binary format. This format is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
 * used by Heap Analysis Tool (HAT). The class is heavily influenced
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
 * by 'hprof_io.c' of 1.5 new hprof implementation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
/* hprof binary format: (result either written to a file or sent over
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
 * the network).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
 * WARNING: This format is still under development, and is subject to
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
 * change without notice.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
 * header    "JAVA PROFILE 1.0.1" (0-terminated)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
 * u4        size of identifiers. Identifiers are used to represent
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
 *            UTF8 strings, objects, stack traces, etc. They usually
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
 *            have the same size as host pointers. For example, on
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
 *            Solaris and Win32, the size is 4.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
 * u4         high word
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
 * u4         low word    number of milliseconds since 0:00 GMT, 1/1/70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
 * [record]*  a sequence of records.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
 * Record format:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
 * u1         a TAG denoting the type of the record
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
 * u4         number of *microseconds* since the time stamp in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
 *            header. (wraps around in a little more than an hour)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
 * u4         number of bytes *remaining* in the record. Note that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
 *            this number excludes the tag and the length field itself.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
 * [u1]*      BODY of the record (a sequence of bytes)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
 * The following TAGs are supported:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
 * TAG           BODY       notes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
 *----------------------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
 * HPROF_UTF8               a UTF8-encoded name
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
 *               id         name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
 *               [u1]*      UTF8 characters (no trailing zero)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
 * HPROF_LOAD_CLASS         a newly loaded class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
 *                u4        class serial number (> 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
 *                id        class object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
 *                u4        stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
 *                id        class name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
 * HPROF_UNLOAD_CLASS       an unloading class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
 *                u4        class serial_number
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
 * HPROF_FRAME              a Java stack frame
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
 *                id        stack frame ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
 *                id        method name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
 *                id        method signature ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
 *                id        source file name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
 *                u4        class serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
 *                i4        line number. >0: normal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
 *                                       -1: unknown
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
 *                                       -2: compiled method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
 *                                       -3: native method
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
 * HPROF_TRACE              a Java stack trace
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
 *               u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
 *               u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
 *               u4         number of frames
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
 *               [id]*      stack frame IDs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
 * HPROF_ALLOC_SITES        a set of heap allocation sites, obtained after GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
 *               u2         flags 0x0001: incremental vs. complete
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
 *                                0x0002: sorted by allocation vs. live
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
 *                                0x0004: whether to force a GC
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
 *               u4         cutoff ratio
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
 *               u4         total live bytes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
 *               u4         total live instances
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
 *               u8         total bytes allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
 *               u8         total instances allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
 *               u4         number of sites that follow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
 *               [u1        is_array: 0:  normal object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
 *                                    2:  object array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
 *                                    4:  boolean array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
 *                                    5:  char array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
 *                                    6:  float array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
 *                                    7:  double array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
 *                                    8:  byte array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
 *                                    9:  short array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
 *                                    10: int array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
 *                                    11: long array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
 *                u4        class serial number (may be zero during startup)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
 *                u4        stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
 *                u4        number of bytes alive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
 *                u4        number of instances alive
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
 *                u4        number of bytes allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
 *                u4]*      number of instance allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
 * HPROF_START_THREAD       a newly started thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
 *               u4         thread serial number (> 0)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
 *               id         thread object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
 *               u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
 *               id         thread name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
 *               id         thread group name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
 *               id         thread group parent name ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
 * HPROF_END_THREAD         a terminating thread.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
 *               u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
 * HPROF_HEAP_SUMMARY       heap summary
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
 *               u4         total live bytes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
 *               u4         total live instances
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
 *               u8         total bytes allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
 *               u8         total instances allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
 * HPROF_HEAP_DUMP          denote a heap dump
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
 *               [heap dump sub-records]*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
 *                          There are four kinds of heap dump sub-records:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
 *               u1         sub-record type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
 *               HPROF_GC_ROOT_UNKNOWN         unknown root
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
 *               HPROF_GC_ROOT_THREAD_OBJ      thread object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
 *                          id         thread object ID  (may be 0 for a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
 *                                     thread newly attached through JNI)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
 *                          u4         thread sequence number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
 *                          u4         stack trace sequence number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
 *               HPROF_GC_ROOT_JNI_GLOBAL      JNI global ref root
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
 *                          id         JNI global ref ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
 *               HPROF_GC_ROOT_JNI_LOCAL       JNI local ref
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
 *                          u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
 *                          u4         frame # in stack trace (-1 for empty)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
 *               HPROF_GC_ROOT_JAVA_FRAME      Java stack frame
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
 *                          u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
 *                          u4         frame # in stack trace (-1 for empty)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
 *               HPROF_GC_ROOT_NATIVE_STACK    Native stack
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
 *                          u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
 *               HPROF_GC_ROOT_STICKY_CLASS    System class
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
 *               HPROF_GC_ROOT_THREAD_BLOCK    Reference from thread block
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
 *                          u4         thread serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
 *               HPROF_GC_ROOT_MONITOR_USED    Busy monitor
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
 *               HPROF_GC_CLASS_DUMP           dump of a class object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
 *                          id         class object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
 *                          u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
 *                          id         super class object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
 *                          id         class loader object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
 *                          id         signers object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
 *                          id         protection domain object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
 *                          id         reserved
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
 *                          id         reserved
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
 *                          u4         instance size (in bytes)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
 *                          u2         size of constant pool
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
 *                          [u2,       constant pool index,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
 *                           ty,       type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
 *                                     2:  object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
 *                                     4:  boolean
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
 *                                     5:  char
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
 *                                     6:  float
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
 *                                     7:  double
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
 *                                     8:  byte
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
 *                                     9:  short
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
 *                                     10: int
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
 *                                     11: long
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
 *                           vl]*      and value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
 *                          u2         number of static fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
 *                          [id,       static field name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
 *                           ty,       type,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
 *                           vl]*      and value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
 *                          u2         number of inst. fields (not inc. super)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
 *                          [id,       instance field name,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
 *                           ty]*      type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
 *               HPROF_GC_INSTANCE_DUMP        dump of a normal object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
 *                          id         object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
 *                          u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
 *                          id         class object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
 *                          u4         number of bytes that follow
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
 *                          [vl]*      instance field values (class, followed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
 *                                     by super, super's super ...)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
 *               HPROF_GC_OBJ_ARRAY_DUMP       dump of an object array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
 *                          id         array object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
 *                          u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
 *                          u4         number of elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
 *                          id         array class ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
 *                          [id]*      elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
 *               HPROF_GC_PRIM_ARRAY_DUMP      dump of a primitive array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
 *                          id         array object ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
 *                          u4         stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
 *                          u4         number of elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
 *                          u1         element type
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
 *                                     4:  boolean array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
 *                                     5:  char array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
 *                                     6:  float array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
 *                                     7:  double array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
 *                                     8:  byte array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
 *                                     9:  short array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
 *                                     10: int array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
 *                                     11: long array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
 *                          [u1]*      elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
 * HPROF_CPU_SAMPLES        a set of sample traces of running threads
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
 *                u4        total number of samples
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
 *                u4        # of traces
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
 *               [u4        # of samples
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
 *                u4]*      stack trace serial number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
 * HPROF_CONTROL_SETTINGS   the settings of on/off switches
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
 *                u4        0x00000001: alloc traces on/off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
 *                          0x00000002: cpu sampling on/off
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
 *                u2        stack trace depth
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
public class HeapHprofBinWriter extends AbstractHeapGraphWriter {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
    // hprof binary file header
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
    private static final String HPROF_HEADER = "JAVA PROFILE 1.0.1";
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    // constants in enum HprofTag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    private static final int HPROF_UTF8             = 0x01;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    private static final int HPROF_LOAD_CLASS       = 0x02;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    private static final int HPROF_UNLOAD_CLASS     = 0x03;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    private static final int HPROF_FRAME            = 0x04;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    private static final int HPROF_TRACE            = 0x05;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
    private static final int HPROF_ALLOC_SITES      = 0x06;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
    private static final int HPROF_HEAP_SUMMARY     = 0x07;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
    private static final int HPROF_START_THREAD     = 0x0A;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
    private static final int HPROF_END_THREAD       = 0x0B;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
    private static final int HPROF_HEAP_DUMP        = 0x0C;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
    private static final int HPROF_CPU_SAMPLES      = 0x0D;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
    private static final int HPROF_CONTROL_SETTINGS = 0x0E;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
    // Heap dump constants
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
    // constants in enum HprofGcTag
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
    private static final int HPROF_GC_ROOT_UNKNOWN       = 0xFF;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
    private static final int HPROF_GC_ROOT_JNI_GLOBAL    = 0x01;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    private static final int HPROF_GC_ROOT_JNI_LOCAL     = 0x02;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
    private static final int HPROF_GC_ROOT_JAVA_FRAME    = 0x03;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
    private static final int HPROF_GC_ROOT_NATIVE_STACK  = 0x04;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
    private static final int HPROF_GC_ROOT_STICKY_CLASS  = 0x05;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
    private static final int HPROF_GC_ROOT_THREAD_BLOCK  = 0x06;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
    private static final int HPROF_GC_ROOT_MONITOR_USED  = 0x07;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    private static final int HPROF_GC_ROOT_THREAD_OBJ    = 0x08;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    private static final int HPROF_GC_CLASS_DUMP         = 0x20;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
    private static final int HPROF_GC_INSTANCE_DUMP      = 0x21;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
    private static final int HPROF_GC_OBJ_ARRAY_DUMP     = 0x22;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
    private static final int HPROF_GC_PRIM_ARRAY_DUMP    = 0x23;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
    // constants in enum HprofType
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    private static final int HPROF_ARRAY_OBJECT  = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    private static final int HPROF_NORMAL_OBJECT = 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    private static final int HPROF_BOOLEAN       = 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
    private static final int HPROF_CHAR          = 5;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    private static final int HPROF_FLOAT         = 6;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
    private static final int HPROF_DOUBLE        = 7;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    private static final int HPROF_BYTE          = 8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
    private static final int HPROF_SHORT         = 9;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
    private static final int HPROF_INT           = 10;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
    private static final int HPROF_LONG          = 11;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
    // Java type codes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
    private static final int JVM_SIGNATURE_BOOLEAN = 'Z';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
    private static final int JVM_SIGNATURE_CHAR    = 'C';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
    private static final int JVM_SIGNATURE_BYTE    = 'B';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
    private static final int JVM_SIGNATURE_SHORT   = 'S';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    private static final int JVM_SIGNATURE_INT     = 'I';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
    private static final int JVM_SIGNATURE_LONG    = 'J';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
    private static final int JVM_SIGNATURE_FLOAT   = 'F';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    private static final int JVM_SIGNATURE_DOUBLE  = 'D';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
    private static final int JVM_SIGNATURE_ARRAY   = '[';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
    private static final int JVM_SIGNATURE_CLASS   = 'L';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
    public synchronized void write(String fileName) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
        // open file stream and create buffered data output stream
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
        FileOutputStream fos = new FileOutputStream(fileName);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
        FileChannel chn = fos.getChannel();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
        out = new DataOutputStream(new BufferedOutputStream(fos));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
        VM vm = VM.getVM();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
        dbg = vm.getDebugger();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
        objectHeap = vm.getObjectHeap();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
        symTbl = vm.getSymbolTable();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
        OBJ_ID_SIZE = (int) vm.getOopSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
        BOOLEAN_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_BOOLEAN);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
        BYTE_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_BYTE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
        CHAR_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_CHAR);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
        SHORT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_SHORT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
        INT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_INT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
        LONG_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_LONG);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
        FLOAT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_FLOAT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
        DOUBLE_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_DOUBLE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
        OBJECT_BASE_OFFSET = TypeArray.baseOffsetInBytes(BasicType.T_OBJECT);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
        BOOLEAN_SIZE = objectHeap.getBooleanSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
        BYTE_SIZE = objectHeap.getByteSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
        CHAR_SIZE = objectHeap.getCharSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
        SHORT_SIZE = objectHeap.getShortSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
        INT_SIZE = objectHeap.getIntSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
        LONG_SIZE = objectHeap.getLongSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
        FLOAT_SIZE = objectHeap.getFloatSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
        DOUBLE_SIZE = objectHeap.getDoubleSize();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
        // hprof bin format header
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
        writeFileHeader();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
        // dummy stack trace without any frames so that
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
        // HAT can be run without -stack false option
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
        writeDummyTrace();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
        // hprof UTF-8 symbols section
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
        writeSymbols();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
        // HPROF_LOAD_CLASS records for all classes
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
        writeClasses();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
        // write heap data now
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
        out.writeByte((byte)HPROF_HEAP_DUMP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
        out.writeInt(0); // relative timestamp
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
        // remember position of dump length, we will fixup
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
        // length later - hprof format requires length.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
        out.flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
        long dumpStart = chn.position();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
        // write dummy length of 0 and we'll fix it later.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
        out.writeInt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
        // write CLASS_DUMP records
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
        writeClassDumpRecords();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
        // this will write heap data into the buffer stream
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
        super.write();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
        // flush buffer stream and throw it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
        out.flush();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
        out = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
        // now get current position to calculate length
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
        long dumpEnd = chn.position();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
        // calculate length of heap data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
        int dumpLen = (int) (dumpEnd - dumpStart - 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
        // seek the position to write length
489c9b5090e2 Initial load
duke
parents:
diff changeset
   428
        chn.position(dumpStart);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   429
489c9b5090e2 Initial load
duke
parents:
diff changeset
   430
        // write length as integer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   431
        fos.write((dumpLen >>> 24) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   432
        fos.write((dumpLen >>> 16) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   433
        fos.write((dumpLen >>> 8) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   434
        fos.write((dumpLen >>> 0) & 0xFF);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   435
489c9b5090e2 Initial load
duke
parents:
diff changeset
   436
        // close the file stream
489c9b5090e2 Initial load
duke
parents:
diff changeset
   437
        fos.close();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   438
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   439
489c9b5090e2 Initial load
duke
parents:
diff changeset
   440
    private void writeClassDumpRecords() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   441
        SystemDictionary sysDict = VM.getVM().getSystemDictionary();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   442
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   443
            sysDict.allClassesDo(new SystemDictionary.ClassVisitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   444
                            public void visit(Klass k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   445
                                try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   446
                                    writeClassDumpRecord(k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   447
                                } catch (IOException e) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   448
                                    throw new RuntimeException(e);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   449
                                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   450
                            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   451
                        });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   452
        } catch (RuntimeException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   453
            handleRuntimeException(re);
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
    protected void writeClass(Instance instance) throws IOException {
8878
a6283814032c 7031614: jmap -permstat fails with java.lang.InternalError in sun.jvm.hotspot.oops.OopField.getValue
never
parents: 8729
diff changeset
   458
        Klass reflectedKlass = java_lang_Class.asKlass(instance);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   459
        // dump instance record only for primitive type Class objects.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   460
        // all other Class objects are covered by writeClassDumpRecords.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   461
        if (reflectedKlass == null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   462
            writeInstance(instance);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   463
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   464
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   465
489c9b5090e2 Initial load
duke
parents:
diff changeset
   466
    private void writeClassDumpRecord(Klass k) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   467
        out.writeByte((byte)HPROF_GC_CLASS_DUMP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   468
        writeObjectID(k.getJavaMirror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   469
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   470
        Klass superKlass = k.getJavaSuper();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   471
        if (superKlass != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   472
            writeObjectID(superKlass.getJavaMirror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   473
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   474
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   475
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   476
489c9b5090e2 Initial load
duke
parents:
diff changeset
   477
        if (k instanceof InstanceKlass) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   478
            InstanceKlass ik = (InstanceKlass) k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   479
            writeObjectID(ik.getClassLoader());
17826
9ad5cd464a75 8003421: NPG: Move oops out of InstanceKlass into mirror
coleenp
parents: 13728
diff changeset
   480
            writeObjectID(null);  // ik.getJavaMirror().getSigners());
9ad5cd464a75 8003421: NPG: Move oops out of InstanceKlass into mirror
coleenp
parents: 13728
diff changeset
   481
            writeObjectID(null);  // ik.getJavaMirror().getProtectionDomain());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   482
            // two reserved id fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   483
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   484
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   485
            List fields = getInstanceFields(ik);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   486
            int instSize = getSizeForFields(fields);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   487
            classDataCache.put(ik, new ClassData(instSize, fields));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   488
            out.writeInt(instSize);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   489
489c9b5090e2 Initial load
duke
parents:
diff changeset
   490
            // For now, ignore constant pool - HAT ignores too!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   491
            // output number of cp entries as zero.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   492
            out.writeShort((short) 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   493
489c9b5090e2 Initial load
duke
parents:
diff changeset
   494
            List declaredFields = ik.getImmediateFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   495
            List staticFields = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   496
            List instanceFields = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   497
            Iterator itr = null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   498
            for (itr = declaredFields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   499
                Field field = (Field) itr.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   500
                if (field.isStatic()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   501
                    staticFields.add(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   502
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   503
                    instanceFields.add(field);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   504
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   505
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   506
489c9b5090e2 Initial load
duke
parents:
diff changeset
   507
            // dump static field descriptors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   508
            writeFieldDescriptors(staticFields, ik);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   509
489c9b5090e2 Initial load
duke
parents:
diff changeset
   510
            // dump instance field descriptors
489c9b5090e2 Initial load
duke
parents:
diff changeset
   511
            writeFieldDescriptors(instanceFields, null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   512
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   513
            if (k instanceof ObjArrayKlass) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   514
                ObjArrayKlass oak = (ObjArrayKlass) k;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   515
                Klass bottomKlass = oak.getBottomKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   516
                if (bottomKlass instanceof InstanceKlass) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   517
                    InstanceKlass ik = (InstanceKlass) bottomKlass;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   518
                    writeObjectID(ik.getClassLoader());
17826
9ad5cd464a75 8003421: NPG: Move oops out of InstanceKlass into mirror
coleenp
parents: 13728
diff changeset
   519
                    writeObjectID(null); // ik.getJavaMirror().getSigners());
9ad5cd464a75 8003421: NPG: Move oops out of InstanceKlass into mirror
coleenp
parents: 13728
diff changeset
   520
                    writeObjectID(null); // ik.getJavaMirror().getProtectionDomain());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   521
                } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   522
                    writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   523
                    writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   524
                    writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   525
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   526
            } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   527
                writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   528
                writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   529
                writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   530
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   531
            // two reserved id fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   532
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   533
            writeObjectID(null);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   534
            // write zero instance size -- as instance size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   535
            // is variable for arrays.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   536
            out.writeInt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   537
            // no constant pool for array klasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   538
            out.writeShort((short) 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   539
            // no static fields for array klasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   540
            out.writeShort((short) 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   541
            // no instance fields for array klasses
489c9b5090e2 Initial load
duke
parents:
diff changeset
   542
            out.writeShort((short) 0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   543
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   544
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   545
489c9b5090e2 Initial load
duke
parents:
diff changeset
   546
    protected void writeJavaThread(JavaThread jt, int index) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   547
        out.writeByte((byte) HPROF_GC_ROOT_THREAD_OBJ);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   548
        writeObjectID(jt.getThreadObj());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   549
        out.writeInt(index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   550
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   551
        writeLocalJNIHandles(jt, index);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   552
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   553
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
    protected void writeLocalJNIHandles(JavaThread jt, int index) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
        final int threadIndex = index;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
        JNIHandleBlock blk = jt.activeHandles();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
        if (blk != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   558
            try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
                blk.oopsDo(new AddressVisitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
                           public void visitAddress(Address handleAddr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
                               try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   562
                                   if (handleAddr != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   563
                                       OopHandle oopHandle = handleAddr.getOopHandleAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   564
                                       Oop oop = objectHeap.newOop(oopHandle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   565
                                       // exclude JNI handles hotspot internal objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   566
                                       if (oop != null && isJavaVisible(oop)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   567
                                           out.writeByte((byte) HPROF_GC_ROOT_JNI_LOCAL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   568
                                           writeObjectID(oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   569
                                           out.writeInt(threadIndex);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   570
                                           out.writeInt(EMPTY_FRAME_DEPTH);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   571
                                       }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
                                   }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
                               } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   574
                                   throw new RuntimeException(exp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
                               }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
                           }
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   577
                           public void visitCompOopAddress(Address handleAddr) {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   578
                             throw new RuntimeException(
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   579
                                   " Should not reach here. JNIHandles are not compressed \n");
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   580
                           }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
                       });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
            } catch (RuntimeException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
                handleRuntimeException(re);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   584
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
489c9b5090e2 Initial load
duke
parents:
diff changeset
   588
    protected void writeGlobalJNIHandle(Address handleAddr) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
        OopHandle oopHandle = handleAddr.getOopHandleAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
        Oop oop = objectHeap.newOop(oopHandle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
        // exclude JNI handles of hotspot internal objects
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
        if (oop != null && isJavaVisible(oop)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
            out.writeByte((byte) HPROF_GC_ROOT_JNI_GLOBAL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
            writeObjectID(oop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
            // use JNIHandle address as ID
489c9b5090e2 Initial load
duke
parents:
diff changeset
   596
            writeObjectID(getAddressValue(handleAddr));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   597
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   598
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   599
489c9b5090e2 Initial load
duke
parents:
diff changeset
   600
    protected void writeObjectArray(ObjArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   601
        out.writeByte((byte) HPROF_GC_OBJ_ARRAY_DUMP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   602
        writeObjectID(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   603
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
        out.writeInt((int) array.getLength());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
        writeObjectID(array.getKlass().getJavaMirror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
        final int length = (int) array.getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
        for (int index = 0; index < length; index++) {
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   608
            OopHandle handle = array.getOopHandleAt(index);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
            writeObjectID(getAddressValue(handle));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
    protected void writePrimitiveArray(TypeArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   614
        out.writeByte((byte) HPROF_GC_PRIM_ARRAY_DUMP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   615
        writeObjectID(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   616
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   617
        out.writeInt((int) array.getLength());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   618
        TypeArrayKlass tak = (TypeArrayKlass) array.getKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   619
        final int type = (int) tak.getElementType();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
        out.writeByte((byte) type);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
        switch (type) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
            case TypeArrayKlass.T_BOOLEAN:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
                writeBooleanArray(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
            case TypeArrayKlass.T_CHAR:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
                writeCharArray(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   627
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   628
            case TypeArrayKlass.T_FLOAT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
                writeFloatArray(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
            case TypeArrayKlass.T_DOUBLE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
                writeDoubleArray(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
            case TypeArrayKlass.T_BYTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
                writeByteArray(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
            case TypeArrayKlass.T_SHORT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
                writeShortArray(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
            case TypeArrayKlass.T_INT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
                writeIntArray(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   643
            case TypeArrayKlass.T_LONG:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   644
                writeLongArray(array);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   646
            default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   647
                throw new RuntimeException("should not reach here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   650
489c9b5090e2 Initial load
duke
parents:
diff changeset
   651
    private void writeBooleanArray(TypeArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   652
        final int length = (int) array.getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   654
             long offset = BOOLEAN_BASE_OFFSET + index * BOOLEAN_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   655
             out.writeBoolean(array.getHandle().getJBooleanAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
489c9b5090e2 Initial load
duke
parents:
diff changeset
   659
    private void writeByteArray(TypeArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   660
        final int length = (int) array.getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   661
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   662
             long offset = BYTE_BASE_OFFSET + index * BYTE_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
             out.writeByte(array.getHandle().getJByteAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
    private void writeShortArray(TypeArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
        final int length = (int) array.getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   670
             long offset = SHORT_BASE_OFFSET + index * SHORT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   671
             out.writeShort(array.getHandle().getJShortAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   672
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   673
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   674
489c9b5090e2 Initial load
duke
parents:
diff changeset
   675
    private void writeIntArray(TypeArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   676
        final int length = (int) array.getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   677
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   678
             long offset = INT_BASE_OFFSET + index * INT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   679
             out.writeInt(array.getHandle().getJIntAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   680
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   681
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   682
489c9b5090e2 Initial load
duke
parents:
diff changeset
   683
    private void writeLongArray(TypeArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   684
        final int length = (int) array.getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   685
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   686
             long offset = LONG_BASE_OFFSET + index * LONG_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   687
             out.writeLong(array.getHandle().getJLongAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   688
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   689
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   690
489c9b5090e2 Initial load
duke
parents:
diff changeset
   691
    private void writeCharArray(TypeArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   692
        final int length = (int) array.getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   693
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   694
             long offset = CHAR_BASE_OFFSET + index * CHAR_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   695
             out.writeChar(array.getHandle().getJCharAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   696
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   697
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   698
489c9b5090e2 Initial load
duke
parents:
diff changeset
   699
    private void writeFloatArray(TypeArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   700
        final int length = (int) array.getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   701
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   702
             long offset = FLOAT_BASE_OFFSET + index * FLOAT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   703
             out.writeFloat(array.getHandle().getJFloatAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   704
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   705
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   706
489c9b5090e2 Initial load
duke
parents:
diff changeset
   707
    private void writeDoubleArray(TypeArray array) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   708
        final int length = (int) array.getLength();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   709
        for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   710
             long offset = DOUBLE_BASE_OFFSET + index * DOUBLE_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   711
             out.writeDouble(array.getHandle().getJDoubleAt(offset));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   712
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   713
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   714
489c9b5090e2 Initial load
duke
parents:
diff changeset
   715
    protected void writeInstance(Instance instance) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   716
        out.writeByte((byte) HPROF_GC_INSTANCE_DUMP);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   717
        writeObjectID(instance);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   718
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
        Klass klass = instance.getKlass();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
        writeObjectID(klass.getJavaMirror());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
        ClassData cd = (ClassData) classDataCache.get(klass);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
        if (Assert.ASSERTS_ENABLED) {
13728
882756847a04 6964458: Reimplement class meta-data storage to use native memory
coleenp
parents: 8878
diff changeset
   724
            Assert.that(cd != null, "can not get class data for " + klass.getName().asString() + klass.getAddress());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   725
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
        List fields = cd.fields;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
        int size = cd.instSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   728
        out.writeInt(size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
        for (Iterator itr = fields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   730
            writeField((Field) itr.next(), instance);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   731
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   733
489c9b5090e2 Initial load
duke
parents:
diff changeset
   734
    //-- Internals only below this point
489c9b5090e2 Initial load
duke
parents:
diff changeset
   735
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
    private void writeFieldDescriptors(List fields, InstanceKlass ik)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   737
        throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   738
        // ik == null for instance fields.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   739
        out.writeShort((short) fields.size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   740
        for (Iterator itr = fields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   741
            Field field = (Field) itr.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   742
            Symbol name = symTbl.probe(field.getID().getName());
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   743
            writeSymbolID(name);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   744
            char typeCode = (char) field.getSignature().getByteAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   745
            int kind = signatureToHprofKind(typeCode);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   746
            out.writeByte((byte)kind);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   747
            if (ik != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   748
                // static field
8729
cdb7525d39cb 7030300: more nightly failures after statics in Class changes
never
parents: 8076
diff changeset
   749
                writeField(field, ik.getJavaMirror());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
    public static int signatureToHprofKind(char ch) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
        switch (ch) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
        case JVM_SIGNATURE_CLASS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
        case JVM_SIGNATURE_ARRAY:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
            return HPROF_NORMAL_OBJECT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
        case JVM_SIGNATURE_BOOLEAN:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
            return HPROF_BOOLEAN;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
        case JVM_SIGNATURE_CHAR:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
            return HPROF_CHAR;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
        case JVM_SIGNATURE_FLOAT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
            return HPROF_FLOAT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
        case JVM_SIGNATURE_DOUBLE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
            return HPROF_DOUBLE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
        case JVM_SIGNATURE_BYTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   768
            return HPROF_BYTE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
        case JVM_SIGNATURE_SHORT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
            return HPROF_SHORT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
        case JVM_SIGNATURE_INT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
            return HPROF_INT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
        case JVM_SIGNATURE_LONG:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
            return HPROF_LONG;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   775
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   776
            throw new RuntimeException("should not reach here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   777
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
    private void writeField(Field field, Oop oop) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   781
        char typeCode = (char) field.getSignature().getByteAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
        switch (typeCode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   783
        case JVM_SIGNATURE_BOOLEAN:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
            out.writeBoolean(((BooleanField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   786
        case JVM_SIGNATURE_CHAR:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   787
            out.writeChar(((CharField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   788
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   789
        case JVM_SIGNATURE_BYTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
            out.writeByte(((ByteField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   792
        case JVM_SIGNATURE_SHORT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   793
            out.writeShort(((ShortField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   794
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
        case JVM_SIGNATURE_INT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
            out.writeInt(((IntField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
        case JVM_SIGNATURE_LONG:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
            out.writeLong(((LongField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   801
        case JVM_SIGNATURE_FLOAT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   802
            out.writeFloat(((FloatField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   803
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   804
        case JVM_SIGNATURE_DOUBLE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   805
            out.writeDouble(((DoubleField)field).getValue(oop));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   806
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   807
        case JVM_SIGNATURE_CLASS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
        case JVM_SIGNATURE_ARRAY: {
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   809
            if (VM.getVM().isCompressedOopsEnabled()) {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   810
              OopHandle handle = ((NarrowOopField)field).getValueAsOopHandle(oop);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   811
              writeObjectID(getAddressValue(handle));
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   812
            } else {
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   813
              OopHandle handle = ((OopField)field).getValueAsOopHandle(oop);
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   814
              writeObjectID(getAddressValue(handle));
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 1
diff changeset
   815
            }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   816
            break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   817
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   818
        default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   819
            throw new RuntimeException("should not reach here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   820
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   821
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   822
489c9b5090e2 Initial load
duke
parents:
diff changeset
   823
    private void writeHeader(int tag, int len) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   824
        out.writeByte((byte)tag);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   825
        out.writeInt(0); // current ticks
489c9b5090e2 Initial load
duke
parents:
diff changeset
   826
        out.writeInt(len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   827
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   828
489c9b5090e2 Initial load
duke
parents:
diff changeset
   829
    private void writeDummyTrace() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   830
        writeHeader(HPROF_TRACE, 3 * 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   831
        out.writeInt(DUMMY_STACK_TRACE_ID);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   832
        out.writeInt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   833
        out.writeInt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   834
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   835
489c9b5090e2 Initial load
duke
parents:
diff changeset
   836
    private void writeSymbols() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   837
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   838
            symTbl.symbolsDo(new SymbolTable.SymbolVisitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   839
                    public void visit(Symbol sym) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   840
                        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
                            writeSymbol(sym);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
                        } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   843
                            throw new RuntimeException(exp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   844
                        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   845
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   846
                });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   847
        } catch (RuntimeException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   848
            handleRuntimeException(re);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   849
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   850
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   851
489c9b5090e2 Initial load
duke
parents:
diff changeset
   852
    private void writeSymbol(Symbol sym) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   853
        byte[] buf = sym.asString().getBytes("UTF-8");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   854
        writeHeader(HPROF_UTF8, buf.length + OBJ_ID_SIZE);
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   855
        writeSymbolID(sym);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   856
        out.write(buf);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   857
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   858
489c9b5090e2 Initial load
duke
parents:
diff changeset
   859
    private void writeClasses() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   860
        // write class list (id, name) association
489c9b5090e2 Initial load
duke
parents:
diff changeset
   861
        SystemDictionary sysDict = VM.getVM().getSystemDictionary();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   862
        try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   863
            sysDict.allClassesDo(new SystemDictionary.ClassVisitor() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   864
                private int serialNum = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   865
                public void visit(Klass k) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   866
                    try {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   867
                        Instance clazz = k.getJavaMirror();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
                        writeHeader(HPROF_LOAD_CLASS, 2 * (OBJ_ID_SIZE + 4));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   869
                        out.writeInt(serialNum);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   870
                        writeObjectID(clazz);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   871
                        out.writeInt(DUMMY_STACK_TRACE_ID);
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   872
                        writeSymbolID(k.getName());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   873
                        serialNum++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
                    } catch (IOException exp) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
                        throw new RuntimeException(exp);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
                    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   877
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   878
            });
489c9b5090e2 Initial load
duke
parents:
diff changeset
   879
        } catch (RuntimeException re) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   880
            handleRuntimeException(re);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   881
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   882
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
    // writes hprof binary file header
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
    private void writeFileHeader() throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
        // version string
489c9b5090e2 Initial load
duke
parents:
diff changeset
   887
        out.writeBytes(HPROF_HEADER);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   888
        out.writeByte((byte)'\0');
489c9b5090e2 Initial load
duke
parents:
diff changeset
   889
489c9b5090e2 Initial load
duke
parents:
diff changeset
   890
        // write identifier size. we use pointers as identifiers.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   891
        out.writeInt(OBJ_ID_SIZE);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   892
489c9b5090e2 Initial load
duke
parents:
diff changeset
   893
        // timestamp -- file creation time.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   894
        out.writeLong(System.currentTimeMillis());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   895
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   896
489c9b5090e2 Initial load
duke
parents:
diff changeset
   897
    // writes unique ID for an object
489c9b5090e2 Initial load
duke
parents:
diff changeset
   898
    private void writeObjectID(Oop oop) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   899
        OopHandle handle = (oop != null)? oop.getHandle() : null;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   900
        long address = getAddressValue(handle);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   901
        writeObjectID(address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   902
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   903
8076
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   904
    private void writeSymbolID(Symbol sym) throws IOException {
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   905
        writeObjectID(getAddressValue(sym.getAddress()));
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   906
    }
96d498ec7ae1 6990754: Use native memory and reference counting to implement SymbolTable
coleenp
parents: 5547
diff changeset
   907
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   908
    private void writeObjectID(long address) throws IOException {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   909
        if (OBJ_ID_SIZE == 4) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   910
            out.writeInt((int) address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   911
        } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   912
            out.writeLong(address);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   913
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   914
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
    private long getAddressValue(Address addr) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
        return (addr == null)? 0L : dbg.getAddressValue(addr);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   918
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
    // get all declared as well as inherited (directly/indirectly) fields
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
    private static List/*<Field>*/ getInstanceFields(InstanceKlass ik) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
        InstanceKlass klass = ik;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
        List res = new ArrayList();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
        while (klass != null) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
            List curFields = klass.getImmediateFields();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
            for (Iterator itr = curFields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
                Field f = (Field) itr.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
                if (! f.isStatic()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   929
                    res.add(f);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
                }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
            klass = (InstanceKlass) klass.getSuper();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
        return res;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   935
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
    // get size in bytes (in stream) required for given fields.  Note
489c9b5090e2 Initial load
duke
parents:
diff changeset
   938
    // that this is not the same as object size in heap. The size in
489c9b5090e2 Initial load
duke
parents:
diff changeset
   939
    // heap will include size of padding/alignment bytes as well.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   940
    private int getSizeForFields(List fields) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
        int size = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
        for (Iterator itr = fields.iterator(); itr.hasNext();) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   943
            Field field = (Field) itr.next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
            char typeCode = (char) field.getSignature().getByteAt(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   945
            switch (typeCode) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   946
            case JVM_SIGNATURE_BOOLEAN:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
            case JVM_SIGNATURE_BYTE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
                size++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   950
            case JVM_SIGNATURE_CHAR:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   951
            case JVM_SIGNATURE_SHORT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   952
                size += 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   953
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   954
            case JVM_SIGNATURE_INT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   955
            case JVM_SIGNATURE_FLOAT:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   956
                size += 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   957
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
            case JVM_SIGNATURE_CLASS:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
            case JVM_SIGNATURE_ARRAY:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
                size += OBJ_ID_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
            case JVM_SIGNATURE_LONG:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
            case JVM_SIGNATURE_DOUBLE:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
                size += 8;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
                break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
            default:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
                throw new RuntimeException("should not reach here");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
        return size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
    // We don't have allocation site info. We write a dummy
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
    // stack trace with this id.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
    private static final int DUMMY_STACK_TRACE_ID = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
    private static final int EMPTY_FRAME_DEPTH = -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
    private DataOutputStream out;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
    private Debugger dbg;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
    private ObjectHeap objectHeap;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
    private SymbolTable symTbl;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
    // oopSize of the debuggee
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
    private int OBJ_ID_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
    private long BOOLEAN_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
    private long BYTE_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
    private long CHAR_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
    private long SHORT_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
    private long INT_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
    private long LONG_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   992
    private long FLOAT_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   993
    private long DOUBLE_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   994
    private long OBJECT_BASE_OFFSET;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   995
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
    private long BOOLEAN_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
    private long BYTE_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
    private long CHAR_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
    private long SHORT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
    private long INT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
    private long LONG_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
    private long FLOAT_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
    private long DOUBLE_SIZE;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
    private static class ClassData {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
        int instSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
        List fields;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1008
        ClassData(int instSize, List fields) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
            this.instSize = instSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
            this.fields = fields;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1011
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1012
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1013
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1014
    private Map classDataCache = new HashMap(); // <InstanceKlass, ClassData>
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1015
}