From e2c07d5044587476fc0fff14260e2b73816d2062 Mon Sep 17 00:00:00 2001
From: Chris Plummer <cjplummer@openjdk.org>
Date: Mon, 5 Aug 2024 18:42:27 +0000
Subject: [PATCH] 8337299:
 vmTestbase/nsk/jdb/stop_at/stop_at002/stop_at002.java failure goes undetected

Reviewed-by: amenkov, sspitsyn
---
 .../jdb/stop_at/stop_at002/stop_at002.java    | 66 ++++++++++++++-----
 .../jdb/stop_at/stop_at002/stop_at002a.java   |  8 ++-
 2 files changed, 53 insertions(+), 21 deletions(-)

diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jdb/stop_at/stop_at002/stop_at002.java b/test/hotspot/jtreg/vmTestbase/nsk/jdb/stop_at/stop_at002/stop_at002.java
index 620c50fa2ae..9dae8c17cfc 100644
--- a/test/hotspot/jtreg/vmTestbase/nsk/jdb/stop_at/stop_at002/stop_at002.java
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jdb/stop_at/stop_at002/stop_at002.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2024, 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
@@ -82,17 +82,10 @@ public class stop_at002 extends JdbTest {
     static final String DEBUGGEE_CLASS = TEST_CLASS + "a";
     static final String FIRST_BREAK        = DEBUGGEE_CLASS + ".main";
     static final String LAST_BREAK         = DEBUGGEE_CLASS + ".lastBreak";
-    static final String DEBUGGEE_LOCATION1 = DEBUGGEE_CLASS + "$Nested$DeeperNested$DeepestNested:43";
-    static final String DEBUGGEE_LOCATION2 = DEBUGGEE_CLASS + "$Inner$MoreInner:57";
-    static final String FAILURE_PATTERN = "Unable to set";
+    static final String DEBUGGEE_LOCATION1 = DEBUGGEE_CLASS + "$Nested$DeeperNested$DeepestNested:64";
+    static final String DEBUGGEE_LOCATION2 = DEBUGGEE_CLASS + "$Inner$MoreInner:78";
 
     protected void runCases() {
-        String[] reply;
-        Paragrep grep;
-        int count;
-        Vector v;
-        String found;
-
         if (!checkStop(DEBUGGEE_LOCATION1)) {
             success = false;
         }
@@ -101,25 +94,62 @@ public class stop_at002 extends JdbTest {
             success = false;
         }
 
-        jdb.contToExit(3);
+        if (!checkBreakpointHit(DEBUGGEE_LOCATION1)) {
+            success = false;
+        }
+
+        if (!checkBreakpointHit(DEBUGGEE_LOCATION2)) {
+            success = false;
+        }
+
+        jdb.contToExit(1);
     }
 
-    private boolean checkStop (String location) {
+    private boolean checkStop(String location) {
         Paragrep grep;
         String[] reply;
         String found;
-        boolean result = true;
 
         log.display("Trying to set breakpoint at line: " + location);
         reply = jdb.receiveReplyFor(JdbCommand.stop_at + location);
 
         grep = new Paragrep(reply);
-        found = grep.findFirst(FAILURE_PATTERN);
-        if (found.length() > 0) {
-            log.complain("jdb failed to set line breakpoint at line: " + found);
-            result = false;
+        found = grep.findFirst("Deferring breakpoint " + location);
+        if (found.length() == 0) {
+            log.complain("jdb failed to setup deferred breakpoint at line: " + location);
+            return false;
         }
 
-        return result;
+        return true;
+    }
+
+    private boolean checkBreakpointHit(String location) {
+        Paragrep grep;
+        String[] reply;
+        String found;
+
+        log.display("continuing to breakpoint at line: " + location);
+        reply = jdb.receiveReplyFor(JdbCommand.cont);
+        grep = new Paragrep(reply);
+
+        found = grep.findFirst("Unable to set deferred breakpoint");
+        if (found.length() > 0) {
+            log.complain("jdb failed to set deferred breakpoint at line: " + location);
+            return false;
+        }
+
+        found = grep.findFirst("Set deferred breakpoint " + location);
+        if (found.length() == 0) {
+            log.complain("jdb failed to set deferred breakpoint at line: " + location);
+            return false;
+        }
+
+        found = grep.findFirst("Breakpoint hit: \"thread=main\", ");
+        if (found.length() == 0) {
+            log.complain("jdb failed to hit breakpoint at line: " + location);
+            return false;
+        }
+
+        return true;
     }
 }
diff --git a/test/hotspot/jtreg/vmTestbase/nsk/jdb/stop_at/stop_at002/stop_at002a.java b/test/hotspot/jtreg/vmTestbase/nsk/jdb/stop_at/stop_at002/stop_at002a.java
index 9195dd26986..d4a40689372 100644
--- a/test/hotspot/jtreg/vmTestbase/nsk/jdb/stop_at/stop_at002/stop_at002a.java
+++ b/test/hotspot/jtreg/vmTestbase/nsk/jdb/stop_at/stop_at002/stop_at002a.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2002, 2024, 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
@@ -21,6 +21,8 @@
  * questions.
  */
 
+//    THIS TEST IS LINE NUMBER SENSITIVE
+
 package nsk.jdb.stop_at.stop_at002;
 
 import nsk.share.*;
@@ -59,7 +61,7 @@ public class stop_at002a {
         class DeeperNested {
             class  DeepestNested {
                 public void foo(boolean input) {
-                    flag = input; /* <--------  This is line number 43 */
+                    flag = input; /* <--------  This is line number 64 */
                 }
             }
         }
@@ -73,7 +75,7 @@ public class stop_at002a {
                 content = "";
             }
             public void foo(String input) {
-                content += input; /* <--------  This is line number 57 */
+                content += input; /* <--------  This is line number 78 */
             }
         }
     }