test/lib/jdk/test/lib/hprof/parser/Reader.java
author sballal
Fri, 02 Jun 2017 12:29:08 +0530
changeset 45366 492133242622
parent 40614 b5d80754b40e
permissions -rw-r--r--
6760477: Update SA to include stack traces in the heap dump Summary: Update SA to include HPROF_TRACE and HPROF_FRAME records in the heap dump Reviewed-by: dsamersoff Contributed-by: sharath.ballal@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30539
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
     1
/*
45366
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
     2
 * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
30539
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
     4
 *
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    10
 *
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    15
 * accompanied this code).
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    16
 *
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    20
 *
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    23
 * questions.
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    24
 */
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    25
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    26
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    27
/*
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    28
 * The Original Code is HAT. The Initial Developer of the
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    29
 * Original Code is Bill Foote, with contributions from others
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    30
 * at JavaSoft/Sun.
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    31
 */
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    32
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    33
package jdk.test.lib.hprof.parser;
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    34
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    35
import java.io.*;
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    36
import jdk.test.lib.hprof.model.*;
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    37
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    38
/**
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    39
 * Abstract base class for reading object dump files.  A reader need not be
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    40
 * thread-safe.
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    41
 *
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    42
 * @author      Bill Foote
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    43
 */
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    44
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    45
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    46
public abstract class Reader {
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    47
    protected PositionDataInputStream in;
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    48
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    49
    protected Reader(PositionDataInputStream in) {
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    50
        this.in = in;
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    51
    }
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    52
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    53
    /**
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    54
     * Read a snapshot from a data input stream.  It is assumed that the magic
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    55
     * number has already been read.
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    56
     */
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    57
    abstract public Snapshot read() throws IOException;
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    58
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    59
    /**
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    60
     * Read a snapshot from a file.
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    61
     *
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    62
     * @param heapFile The name of a file containing a heap dump
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    63
     * @param callStack If true, read the call stack of allocaation sites
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    64
     */
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    65
    public static Snapshot readFile(String heapFile, boolean callStack,
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    66
                                    int debugLevel)
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    67
            throws IOException {
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    68
        int dumpNumber = 1;
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    69
        int pos = heapFile.lastIndexOf('#');
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    70
        if (pos > -1) {
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    71
            String num = heapFile.substring(pos+1, heapFile.length());
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    72
            try {
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    73
                dumpNumber = Integer.parseInt(num, 10);
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    74
            } catch (java.lang.NumberFormatException ex) {
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    75
                String msg = "In file name \"" + heapFile
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    76
                             + "\", a dump number was "
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    77
                             + "expected after the :, but \""
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    78
                             + num + "\" was found instead.";
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    79
                System.err.println(msg);
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    80
                throw new IOException(msg);
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    81
            }
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    82
            heapFile = heapFile.substring(0, pos);
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    83
        }
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    84
        try (PositionDataInputStream in = new PositionDataInputStream(
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    85
                new BufferedInputStream(new FileInputStream(heapFile)))) {
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    86
            int i = in.readInt();
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    87
            if (i == HprofReader.MAGIC_NUMBER) {
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    88
                Reader r
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    89
                    = new HprofReader(heapFile, in, dumpNumber,
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    90
                                      callStack, debugLevel);
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    91
                return r.read();
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    92
            } else {
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    93
                throw new IOException("Unrecognized magic number: " + i);
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    94
            }
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    95
        }
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
    96
    }
45366
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
    97
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
    98
    /**
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
    99
     * Get Stack Traces from a Hprof file.
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   100
     *
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   101
     * @param heapFile The name of a file containing a heap dump
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   102
     */
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   103
    public static String getStack(String heapFile, int debugLevel)
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   104
            throws IOException {
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   105
        int dumpNumber = 1;
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   106
        int pos = heapFile.lastIndexOf('#');
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   107
        if (pos > -1) {
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   108
            String num = heapFile.substring(pos+1, heapFile.length());
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   109
            try {
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   110
                dumpNumber = Integer.parseInt(num, 10);
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   111
            } catch (java.lang.NumberFormatException ex) {
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   112
                String msg = "In file name \"" + heapFile
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   113
                             + "\", a dump number was "
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   114
                             + "expected after the :, but \""
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   115
                             + num + "\" was found instead.";
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   116
                System.err.println(msg);
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   117
                throw new IOException(msg);
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   118
            }
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   119
            heapFile = heapFile.substring(0, pos);
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   120
        }
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   121
        try (PositionDataInputStream in = new PositionDataInputStream(
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   122
                new BufferedInputStream(new FileInputStream(heapFile)))) {
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   123
            int i = in.readInt();
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   124
            if (i == HprofReader.MAGIC_NUMBER) {
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   125
                HprofReader r
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   126
                    = new HprofReader(heapFile, in, dumpNumber,
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   127
                                      true, debugLevel);
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   128
                r.read();
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   129
                return r.printStackTraces();
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   130
            } else {
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   131
                throw new IOException("Unrecognized magic number: " + i);
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   132
            }
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   133
        }
492133242622 6760477: Update SA to include stack traces in the heap dump
sballal
parents: 40614
diff changeset
   134
    }
30539
0fc060f1876a 8059047: Extract parser/validator from jhat for use in tests
ykantser
parents:
diff changeset
   135
}