1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.test.lib.hprof;
  25 
  26 import java.io.BufferedOutputStream;
  27 import java.io.File;
  28 import java.io.FileOutputStream;
  29 import java.io.PrintStream;
  30 
  31 import jdk.test.lib.hprof.model.Snapshot;
  32 import jdk.test.lib.hprof.parser.Reader;
  33 
  34 /**
  35  * Helper class to parse a java heap dump file.
  36  */
  37 public class HprofParser {
  38 
  39     public static void main(String[] args) throws Exception {
  40         if (args.length < 1) {
  41             System.out.println("No arguments supplied");
  42         }
  43         File dump = new File(args[0]);
  44         if (!dump.exists() || !dump.isFile()) {
  45             throw new RuntimeException("The dump file does not exist or not a file");
  46         }
  47         parse(dump);
  48     }
  49 
  50     /**
  51      * @see #parse(File, boolean, boolean, boolean)
  52      */
  53     public static File parse(File dump) throws Exception {
  54         return parse(dump, false, true, true);
  55     }
  56 
  57     /**
  58      * @see #parse(File, boolean, boolean, boolean)
  59      */
  60     public static File parseWithDebugInfo(File dump) throws Exception {
  61         return parse(dump, true, true, true);
  62     }
  63 
  64     /**
  65      * Parse a java heap dump file
  66      *
  67      * @param dump Heap dump file to parse
  68      * @param debug Turn on/off debug file parsing
  69      * @param callStack Turn on/off tracking of object allocation call stack
  70      * @param calculateRefs Turn on/off tracking object allocation call stack
  71      * @throws Exception
  72      * @return File containing output from the parser
  73      */
  74     public static File parse(File dump, boolean debug, boolean callStack, boolean calculateRefs) throws Exception {
  75         File out = new File("hprof." + System.currentTimeMillis() + ".out");
  76         if (out.exists()) {
  77             out.delete();
  78         }
  79 
  80         PrintStream psSystemOut = System.out;
  81         try (PrintStream psHprof = new PrintStream(new BufferedOutputStream(new FileOutputStream(out.getAbsolutePath())))) {
  82             System.setOut(psHprof);
  83 
  84             int debugLevel = debug ? 2 : 0;
  85             try (Snapshot snapshot = Reader.readFile(dump.getAbsolutePath(), callStack, debugLevel)) {
  86                 System.out.println("Snapshot read, resolving...");
  87                 snapshot.resolve(calculateRefs);
  88                 System.out.println("Snapshot resolved.");
  89             }
  90        } finally {
  91            System.setOut(psSystemOut);
  92        }
  93 
  94         return out;
  95     }
  96 
  97 }