1 /*
   2  * Copyright (c) 2016, 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  /*
  25  * @test
  26  * @key nmt
  27  * @summary Running with NMT detail should produce expected stack traces.
  28  * @library /testlibrary
  29  * @modules java.base/jdk.internal.misc
  30  *          java.management
  31  */
  32 
  33 import jdk.test.lib.*;
  34 import java.util.regex.Matcher;
  35 import java.util.regex.Pattern;
  36 
  37 public class CheckForProperDetailStackTrace {
  38     /* The stack trace we look for by default. Note that :: has been replaced by .*
  39        to make sure it maches even if the symbol is not unmangled. */
  40     public static String stackTraceDefault =
  41         ".*ModuleEntryTable.*new_entry.*\n" +
  42         ".*ModuleEntryTable.*locked_create_entry_or_null.*\n" +
  43         ".*Modules.*define_module.*\n" +
  44         ".*JVM_DefineModule.*\n";
  45 
  46     /* The stack trace we look for on Solaris and Windows slowdebug builds. For some
  47        reason ALWAYSINLINE for AllocateHeap is ignored, so it appears in the stack strace. */
  48     public static String stackTraceAllocateHeap =
  49         ".*AllocateHeap.*\n" +
  50         ".*ModuleEntryTable.*new_entry.*\n" +
  51         ".*ModuleEntryTable.*locked_create_entry_or_null.*\n" +
  52         ".*Modules.*define_module.*\n";
  53 
  54     /* A symbol that should always be present in NMT detail output. */
  55     private static String expectedSymbol = 
  56         "locked_create_entry_or_null";
  57 
  58     private static final String jdkDebug = System.getProperty("jdk.debug");
  59     private static boolean isSlowDebugBuild() {
  60         return (jdkDebug.toLowerCase().equals("slowdebug"));
  61     }
  62 
  63     public static void main(String args[]) throws Exception {
  64         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(
  65             "-XX:+UnlockDiagnosticVMOptions",
  66             "-XX:NativeMemoryTracking=detail",
  67             "-XX:+PrintNMTStatistics",
  68             "-version");
  69         OutputAnalyzer output = new OutputAnalyzer(pb.start());
  70 
  71         output.shouldHaveExitValue(0);
  72 
  73         // We should never see either of these frames because they are supposed to be skipped. */
  74         output.shouldNotContain("NativeCallStack::NativeCallStack");
  75         output.shouldNotContain("os::get_native_stack");
  76 
  77         // AllocateHeap shouldn't be in the output because it is suppose to always be inlined.
  78         // We check for that here, but allow it for Windows and Solaris slowdebug builds because
  79         // the compiler ends up not inlining AllocateHeap.
  80         Boolean okToHaveAllocateHeap = 
  81             isSlowDebugBuild() && (Platform.isSolaris() || Platform.isWindows());
  82         if (!okToHaveAllocateHeap) {
  83             output.shouldNotContain("AllocateHeap");
  84         }
  85 
  86         // See if we have any stack trace symbols in the output 
  87         boolean hasSymbols =
  88             output.getStdout().contains(expectedSymbol) || output.getStderr().contains(expectedSymbol);
  89         if (!hasSymbols) {
  90             // It's ok for ARM not to have symbols, because it does not support NMT detail
  91             // when targeting thumb2. It's also ok for Windows not to have symbols, because
  92             // they are only available if the symbols file is included with the build.
  93             if (Platform.isWindows() || Platform.isARM()) {
  94                 return; // we are done
  95             }
  96             output.reportDiagnosticSummary();
  97             throw new RuntimeException("Expected symbol missing missing from output: " + expectedSymbol);
  98         }
  99 
 100         /* Make sure the expected NMT detail stack trace is found. */
 101         String expectedStackTrace =
 102             (okToHaveAllocateHeap ? stackTraceAllocateHeap : stackTraceDefault);
 103         if (!stackTraceMatches(expectedStackTrace, output)) {
 104             output.reportDiagnosticSummary();
 105             throw new RuntimeException("Expected stack trace missing missing from output: " + expectedStackTrace);
 106         }
 107     }
 108 
 109     public static boolean stackTraceMatches(String stackTrace, OutputAnalyzer output) {
 110         Matcher stdoutMatcher = Pattern.compile(stackTrace, Pattern.MULTILINE).matcher(output.getStdout());
 111         Matcher stderrMatcher = Pattern.compile(stackTrace, Pattern.MULTILINE).matcher(output.getStderr());
 112         return (stdoutMatcher.find() || stderrMatcher.find());
 113     }
 114 }