8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD
authordtitov
Tue, 23 Oct 2018 07:57:26 -0700
changeset 52228 0ca91992dd8e
parent 52227 eadd0abbfdf4
child 52229 d8843761f478
8211736: jdb doesn't print prompt when breakpoint is hit and suspend policy is STOP_EVENT_THREAD Reviewed-by: cjplummer, amenkov, gadams, jcbeyler
src/jdk.jdi/share/classes/com/sun/tools/example/debug/tty/TTY.java
test/jdk/com/sun/jdi/JdbStopThreadTest.java
test/jdk/com/sun/jdi/lib/jdb/Jdb.java
test/jdk/com/sun/jdi/lib/jdb/JdbCommand.java
--- a/src/jdk.jdi/share/classes/com/sun/tools/example/debug/tty/TTY.java	Tue Oct 23 13:24:36 2018 +0200
+++ b/src/jdk.jdi/share/classes/com/sun/tools/example/debug/tty/TTY.java	Tue Oct 23 07:57:26 2018 -0700
@@ -100,6 +100,17 @@
     public void breakpointEvent(BreakpointEvent be)  {
         Thread.yield();  // fetch output
         MessageOutput.lnprint("Breakpoint hit:");
+        // Print breakpoint location and prompt if suspend policy is
+        // SUSPEND_NONE or SUSPEND_EVENT_THREAD. In case of SUSPEND_ALL
+        // policy this is handled by vmInterrupted() method.
+        int suspendPolicy = be.request().suspendPolicy();
+        switch (suspendPolicy) {
+            case EventRequest.SUSPEND_EVENT_THREAD:
+            case EventRequest.SUSPEND_NONE:
+                printBreakpointLocation(be);
+                MessageOutput.printPrompt();
+                break;
+        }
     }
 
     @Override
@@ -227,6 +238,10 @@
                                              Commands.locationString(loc)});
     }
 
+    private void printBreakpointLocation(BreakpointEvent be) {
+        printLocationWithSourceLine(be.thread().name(), be.location());
+    }
+
     private void printCurrentLocation() {
         ThreadInfo threadInfo = ThreadInfo.getCurrentThreadInfo();
         StackFrame frame;
@@ -239,24 +254,27 @@
         if (frame == null) {
             MessageOutput.println("No frames on the current call stack");
         } else {
-            Location loc = frame.location();
-            printBaseLocation(threadInfo.getThread().name(), loc);
-            // Output the current source line, if possible
-            if (loc.lineNumber() != -1) {
-                String line;
-                try {
-                    line = Env.sourceLine(loc, loc.lineNumber());
-                } catch (java.io.IOException e) {
-                    line = null;
-                }
-                if (line != null) {
-                    MessageOutput.println("source line number and line",
-                                          new Object [] {loc.lineNumber(),
-                                                         line});
-                }
+            printLocationWithSourceLine(threadInfo.getThread().name(), frame.location());
+        }
+        MessageOutput.println();
+    }
+
+    private void printLocationWithSourceLine(String threadName, Location loc) {
+        printBaseLocation(threadName, loc);
+        // Output the current source line, if possible
+        if (loc.lineNumber() != -1) {
+            String line;
+            try {
+                line = Env.sourceLine(loc, loc.lineNumber());
+            } catch (java.io.IOException e) {
+                line = null;
+            }
+            if (line != null) {
+                MessageOutput.println("source line number and line",
+                                           new Object [] {loc.lineNumber(),
+                                                          line});
             }
         }
-        MessageOutput.println();
     }
 
     private void printLocationOfEvent(LocatableEvent theEvent) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/com/sun/jdi/JdbStopThreadTest.java	Tue Oct 23 07:57:26 2018 -0700
@@ -0,0 +1,90 @@
+/*
+ * Copyright (c) 2018, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8211736
+ * @summary Tests that the breakpoint location and prompt are printed in the debugger output
+ * when breakpoint is hit and suspend policy is set to SUSPEND_EVENT_THREAD or SUSPEND_NONE
+ *
+ * @library /test/lib
+ * @run compile -g JdbStopThreadTest.java
+ * @run main/othervm JdbStopThreadTest
+ */
+
+import jdk.test.lib.process.OutputAnalyzer;
+import lib.jdb.JdbCommand;
+import lib.jdb.JdbTest;
+
+class JdbStopThreadTestTarg {
+    public static void main(String[] args) {
+        test();
+    }
+
+    private static void test() {
+        Thread thread = Thread.currentThread();
+        print(thread); // @1 breakpoint
+        String str = "test";
+        print(str); // @2 breakpoint
+    }
+
+    public static void print(Object obj) {
+        System.out.println(obj);
+    }
+}
+
+public class JdbStopThreadTest extends JdbTest {
+    public static void main(String argv[]) {
+        new JdbStopThreadTest().run();
+    }
+
+    private JdbStopThreadTest() {
+        super(DEBUGGEE_CLASS);
+    }
+
+    private static final String DEBUGGEE_CLASS = JdbStopThreadTestTarg.class.getName();
+    private static final String PATTERN1_TEMPLATE = "^Breakpoint hit: \"thread=main\", " +
+            "JdbStopThreadTestTarg\\.test\\(\\), line=%LINE_NUMBER.*\\R%LINE_NUMBER\\s+print\\(thread\\);.*\\R>\\s";
+    private static final String PATTERN2_TEMPLATE = "^Breakpoint hit: \"thread=main\", " +
+            "JdbStopThreadTestTarg\\.test\\(\\), line=%LINE_NUMBER.*\\R%LINE_NUMBER\\s+print\\(str\\);.*\\R>\\s";
+
+    @Override
+    protected void runCases() {
+        // Test suspend policy SUSPEND_EVENT_THREAD
+        int bpLine1 = parseBreakpoints(getTestSourcePath("JdbStopThreadTest.java"), 1).get(0);
+        jdb.command(JdbCommand.stopThreadAt(DEBUGGEE_CLASS, bpLine1));
+        String pattern1 = PATTERN1_TEMPLATE.replaceAll("%LINE_NUMBER", String.valueOf(bpLine1));
+        // Run to breakpoint #1
+        jdb.command(JdbCommand.run().waitForPrompt(pattern1, true));
+        new OutputAnalyzer(jdb.getJdbOutput()).shouldMatch(pattern1);
+
+
+        // Test suspend policy SUSPEND_NONE
+        jdb.command(JdbCommand.thread(1));
+        int bpLine2 = parseBreakpoints(getTestSourcePath("JdbStopThreadTest.java"), 2).get(0);
+        jdb.command(JdbCommand.stopGoAt(DEBUGGEE_CLASS, bpLine2));
+        String pattern2 = PATTERN2_TEMPLATE.replaceAll("%LINE_NUMBER", String.valueOf(bpLine2));
+        jdb.command(JdbCommand.cont().allowExit());
+        new OutputAnalyzer(jdb.getJdbOutput()).shouldMatch(pattern2);
+    }
+}
--- a/test/jdk/com/sun/jdi/lib/jdb/Jdb.java	Tue Oct 23 13:24:36 2018 +0200
+++ b/test/jdk/com/sun/jdi/lib/jdb/Jdb.java	Tue Oct 23 07:57:26 2018 -0700
@@ -150,10 +150,11 @@
         # i.e., the > prompt comes out AFTER the prompt we we need to wait for.
     */
     // compile regexp once
-    private final String promptPattern = "[a-zA-Z0-9_-][a-zA-Z0-9_-]*\\[[1-9][0-9]*\\] [ >]*$";
-    private final Pattern promptRegexp = Pattern.compile(promptPattern);
+    private final static String promptPattern = "[a-zA-Z0-9_-][a-zA-Z0-9_-]*\\[[1-9][0-9]*\\] [ >]*$";
+    final static Pattern PROMPT_REGEXP = Pattern.compile(promptPattern);
+
     public List<String> waitForPrompt(int lines, boolean allowExit) {
-        return waitForPrompt(lines, allowExit, promptRegexp);
+        return waitForPrompt(lines, allowExit, PROMPT_REGEXP);
     }
 
     // jdb prompt when debuggee is not started and is not suspended after breakpoint
@@ -183,11 +184,19 @@
                 }
             }
             List<String> reply = outputHandler.get();
-            for (String line: reply.subList(Math.max(0, reply.size() - lines), reply.size())) {
-                if (promptRegexp.matcher(line).find()) {
+            if ((promptRegexp.flags() & Pattern.MULTILINE) > 0) {
+                String replyString = reply.stream().collect(Collectors.joining(lineSeparator));
+                if (promptRegexp.matcher(replyString).find()) {
                     logJdb(reply);
                     return outputHandler.reset();
                 }
+            } else {
+                for (String line : reply.subList(Math.max(0, reply.size() - lines), reply.size())) {
+                    if (promptRegexp.matcher(line).find()) {
+                        logJdb(reply);
+                        return outputHandler.reset();
+                    }
+                }
             }
             if (!jdb.isAlive()) {
                 // ensure we get the whole output
@@ -195,7 +204,7 @@
                 logJdb(reply);
                 if (!allowExit) {
                     throw new RuntimeException("waitForPrompt timed out after " + (timeout/1000)
-                            + " seconds, looking for '" + promptPattern + "', in " + lines + " lines");
+                            + " seconds, looking for '" + promptRegexp.pattern() + "', in " + lines + " lines");
                 }
                 return reply;
             }
@@ -203,7 +212,7 @@
         // timeout
         logJdb(outputHandler.get());
         throw new RuntimeException("waitForPrompt timed out after " + (timeout/1000)
-                + " seconds, looking for '" + promptPattern + "', in " + lines + " lines");
+                + " seconds, looking for '" + promptRegexp.pattern() + "', in " + lines + " lines");
     }
 
     public List<String> command(JdbCommand cmd) {
@@ -223,7 +232,7 @@
             throw new RuntimeException("Unexpected IO error while writing command '" + cmd.cmd + "' to jdb stdin stream");
         }
 
-        return waitForPrompt(1, cmd.allowExit);
+        return waitForPrompt(1, cmd.allowExit, cmd.waitForPattern);
     }
 
     public List<String> command(String cmd) {
--- a/test/jdk/com/sun/jdi/lib/jdb/JdbCommand.java	Tue Oct 23 13:24:36 2018 +0200
+++ b/test/jdk/com/sun/jdi/lib/jdb/JdbCommand.java	Tue Oct 23 07:57:26 2018 -0700
@@ -24,6 +24,7 @@
 package lib.jdb;
 
 import java.util.Arrays;
+import java.util.regex.Pattern;
 import java.util.stream.Collectors;
 
 /**
@@ -118,6 +119,8 @@
 public class JdbCommand {
     final String cmd;
     boolean allowExit = false;
+    // Default pattern to wait for command to complete
+    Pattern waitForPattern = Jdb.PROMPT_REGEXP;
 
     public JdbCommand(String cmd) {
         this.cmd = cmd;
@@ -128,6 +131,11 @@
         return this;
     }
 
+    public JdbCommand waitForPrompt(String pattern, boolean isMultiline) {
+        waitForPattern = Pattern.compile(pattern, isMultiline ? Pattern.MULTILINE : 0);
+        return this;
+    }
+
 
     public static JdbCommand run(String ... params) {
         return new JdbCommand("run " + Arrays.stream(params).collect(Collectors.joining(" ")));
@@ -145,10 +153,18 @@
     public static JdbCommand stopAt(String targetClass, int lineNum) {
         return new JdbCommand("stop at " + targetClass + ":" + lineNum);
     }
+    public static JdbCommand stopThreadAt(String targetClass, int lineNum) {
+        return new JdbCommand("stop thread at " + targetClass + ":" + lineNum);
+    }
+    public static JdbCommand stopGoAt(String targetClass, int lineNum) {
+        return new JdbCommand("stop go at " + targetClass + ":" + lineNum);
+    }
     public static JdbCommand stopIn(String targetClass, String methodName) {
         return new JdbCommand("stop in " + targetClass + "." + methodName);
     }
-
+    public static JdbCommand thread(int threadNumber) {
+        return new JdbCommand("thread " + threadNumber);
+    }
     // clear <class id>:<line>   -- clear a breakpoint at a line
     public static JdbCommand clear(String targetClass, int lineNum) {
         return new JdbCommand("clear " + targetClass + ":" + lineNum);