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 import org.testng.annotations.Test;
  25 
  26 import java.io.IOException;
  27 
  28 import com.oracle.java.testlibrary.JDKToolFinder;
  29 import com.oracle.java.testlibrary.OutputAnalyzer;
  30 import com.oracle.java.testlibrary.dcmd.CommandExecutor;
  31 import com.oracle.java.testlibrary.dcmd.PidJcmdExecutor;
  32 
  33 /*
  34  * @test
  35  * @library /testlibrary
  36  * @build com.oracle.java.testlibrary.*
  37  * @build com.oracle.java.testlibrary.dcmd.*
  38  * @run testng HeapDumpTest
  39  */
  40 public class HeapDumpTest {
  41     protected String heapDumpArgs = "";
  42 
  43     public void run(CommandExecutor executor) {
  44         String fileName = "jcmd.gc.heap_dump." + System.currentTimeMillis() + ".hprof";
  45         String cmd = "GC.heap_dump " + heapDumpArgs + " " + fileName;
  46         executor.execute(cmd);
  47 
  48         verifyHeapDump(fileName);
  49     }
  50 
  51     private void verifyHeapDump(String fileName) {
  52         String jhat = JDKToolFinder.getTestJDKTool("jhat");
  53         String[] cmd = { jhat, "-parseonly", "true", fileName };
  54 
  55         ProcessBuilder pb = new ProcessBuilder(cmd);
  56         pb.redirectErrorStream(true);
  57         Process p = null;
  58         OutputAnalyzer output;
  59 
  60         try {
  61             p = pb.start();
  62             output = new OutputAnalyzer(p);
  63 
  64             /*
  65              * Some hprof dumps of all objects contain constantPoolOop references that cannot be resolved, so we ignore
  66              * failures about resolving constantPoolOop fields using a negative lookahead
  67              */
  68             output.shouldNotMatch(".*WARNING(?!.*Failed to resolve object.*constantPoolOop.*).*");
  69         } catch (IOException e) {
  70             throw new RuntimeException("Test error: Caught exception while reading stdout/err of jhat", e);
  71         } finally {
  72             if (p != null) {
  73                 p.destroy();
  74             }
  75         }
  76 
  77         if (output.getExitValue() != 0) {
  78             throw new RuntimeException("Test error: jhat exit code was nonzero");
  79         }
  80     }
  81 
  82     /* GC.heap_dump is not available over JMX, running jcmd pid executor instead */
  83     @Test
  84     public void pid() {
  85         run(new PidJcmdExecutor());
  86     }
  87 }
  88