changeset 45366:492133242622

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
author sballal
date Fri, 02 Jun 2017 12:29:08 +0530
parents b0f2b8ff25a2
children ebc2f494b6cd
files test/lib/jdk/test/lib/hprof/parser/HprofReader.java test/lib/jdk/test/lib/hprof/parser/Reader.java
diffstat 2 files changed, 57 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/test/lib/jdk/test/lib/hprof/parser/HprofReader.java	Wed Jul 05 23:33:53 2017 +0200
+++ b/test/lib/jdk/test/lib/hprof/parser/HprofReader.java	Fri Jun 02 12:29:08 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -35,6 +35,7 @@
 import java.io.*;
 import java.util.Date;
 import java.util.Hashtable;
+import java.util.Map;
 import jdk.test.lib.hprof.model.ArrayTypeCodes;
 import jdk.test.lib.hprof.model.*;
 
@@ -357,6 +358,22 @@
         return snapshot;
     }
 
+    public String printStackTraces() {
+        StringBuffer output = new StringBuffer();
+        for (Map.Entry<Integer, StackTrace> entry : stackTraces.entrySet()) {
+            StackFrame[] frames = entry.getValue().getFrames();
+            output.append("SerialNo " + entry.getKey() + "\n");
+            for (int i = 0; i < frames.length; i++) {
+                output.append("  " + frames[i].getClassName() + "." + frames[i].getMethodName()
+                        + frames[i].getMethodSignature() + " (" + frames[i].getSourceFileName()
+                        + ":" + frames[i].getLineNumber() + ")" + "\n");
+            }
+        }
+
+        System.out.println(output);
+        return output.toString();
+    }
+
     private void skipBytes(long length) throws IOException {
         while (length > 0) {
             long skipped = in.skip(length);
--- a/test/lib/jdk/test/lib/hprof/parser/Reader.java	Wed Jul 05 23:33:53 2017 +0200
+++ b/test/lib/jdk/test/lib/hprof/parser/Reader.java	Fri Jun 02 12:29:08 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -94,4 +94,42 @@
             }
         }
     }
+
+    /**
+     * Get Stack Traces from a Hprof file.
+     *
+     * @param heapFile The name of a file containing a heap dump
+     */
+    public static String getStack(String heapFile, int debugLevel)
+            throws IOException {
+        int dumpNumber = 1;
+        int pos = heapFile.lastIndexOf('#');
+        if (pos > -1) {
+            String num = heapFile.substring(pos+1, heapFile.length());
+            try {
+                dumpNumber = Integer.parseInt(num, 10);
+            } catch (java.lang.NumberFormatException ex) {
+                String msg = "In file name \"" + heapFile
+                             + "\", a dump number was "
+                             + "expected after the :, but \""
+                             + num + "\" was found instead.";
+                System.err.println(msg);
+                throw new IOException(msg);
+            }
+            heapFile = heapFile.substring(0, pos);
+        }
+        try (PositionDataInputStream in = new PositionDataInputStream(
+                new BufferedInputStream(new FileInputStream(heapFile)))) {
+            int i = in.readInt();
+            if (i == HprofReader.MAGIC_NUMBER) {
+                HprofReader r
+                    = new HprofReader(heapFile, in, dumpNumber,
+                                      true, debugLevel);
+                r.read();
+                return r.printStackTraces();
+            } else {
+                throw new IOException("Unrecognized magic number: " + i);
+            }
+        }
+    }
 }