From b6252a4d4d2abdd2741e9214644112887dc1d7a8 Mon Sep 17 00:00:00 2001
From: Yasumasa Suenaga <ysuenaga@openjdk.org>
Date: Mon, 4 Dec 2017 10:23:08 +0900
Subject: [PATCH] 8192897: NPE occurs on clhsdb jstack

Reviewed-by: dholmes, sspitsyn, jgeorge, sballal
---
 .../jvm/hotspot/runtime/CompiledVFrame.java   |  3 +++
 .../jtreg/serviceability/sa/ClhsdbJstack.java | 21 +++++++++++++------
 2 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/CompiledVFrame.java b/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/CompiledVFrame.java
index a2d67b3fe3e..8e9f67a2cd4 100644
--- a/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/CompiledVFrame.java
+++ b/src/jdk.hotspot.agent/share/classes/sun/jvm/hotspot/runtime/CompiledVFrame.java
@@ -128,6 +128,9 @@ public class CompiledVFrame extends JavaVFrame {
 
   /** Returns List<MonitorInfo> */
   public List<MonitorInfo> getMonitors() {
+    if (getScope() == null) {
+      return new ArrayList<>();
+    }
     List monitors = getScope().getMonitors();
     if (monitors == null) {
       return new ArrayList<>();
diff --git a/test/hotspot/jtreg/serviceability/sa/ClhsdbJstack.java b/test/hotspot/jtreg/serviceability/sa/ClhsdbJstack.java
index bbbc81e1963..a3588e0e944 100644
--- a/test/hotspot/jtreg/serviceability/sa/ClhsdbJstack.java
+++ b/test/hotspot/jtreg/serviceability/sa/ClhsdbJstack.java
@@ -38,14 +38,17 @@ import jdk.test.lib.Platform;
 
 public class ClhsdbJstack {
 
-    public static void main(String[] args) throws Exception {
-        System.out.println("Starting ClhsdbJstack test");
-
+    private static void testJstack(boolean withXcomp) throws Exception {
         LingeredApp theApp = null;
         try {
             ClhsdbLauncher test = new ClhsdbLauncher();
-            theApp = LingeredApp.startApp();
-            System.out.println("Started LingeredApp with pid " + theApp.getPid());
+            theApp = withXcomp ? LingeredApp.startApp(List.of("-Xcomp"))
+                               : LingeredApp.startApp();
+            System.out.print("Started LingeredApp ");
+            if (withXcomp) {
+                System.out.print("(-Xcomp) ");
+            }
+            System.out.println("with pid " + theApp.getPid());
 
             List<String> cmds = List.of("jstack -v");
 
@@ -61,10 +64,16 @@ public class ClhsdbJstack {
 
             test.run(theApp.getPid(), cmds, expStrMap, null);
         } catch (Exception ex) {
-            throw new RuntimeException("Test ERROR " + ex, ex);
+            throw new RuntimeException("Test ERROR (with -Xcomp=" + withXcomp + ") " + ex, ex);
         } finally {
             LingeredApp.stopApp(theApp);
         }
+    }
+
+    public static void main(String[] args) throws Exception {
+        System.out.println("Starting ClhsdbJstack test");
+        testJstack(false);
+        testJstack(true);
         System.out.println("Test PASSED");
     }
 }