1 /*
   2  * Copyright (c) 2018, 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  * @bug 8211736
  27  * @summary Tests that the breakpoint location and prompt are printed in the debugger output
  28  * when breakpoint is hit and suspend policy is set to SUSPEND_EVENT_THREAD or SUSPEND_NONE
  29  *
  30  * @library /test/lib
  31  * @run compile -g JdbStopThreadTest.java
  32  * @run main/othervm JdbStopThreadTest
  33  */
  34 
  35 import jdk.test.lib.process.OutputAnalyzer;
  36 import lib.jdb.JdbCommand;
  37 import lib.jdb.JdbTest;
  38 
  39 class JdbStopThreadTestTarg {
  40     public static void main(String[] args) {
  41         test();
  42     }
  43 
  44     private static void test() {
  45         Thread thread = Thread.currentThread();
  46         print(thread); // @1 breakpoint
  47         String str = "test";
  48         print(str); // @2 breakpoint
  49     }
  50 
  51     public static void print(Object obj) {
  52         System.out.println(obj);
  53     }
  54 }
  55 
  56 public class JdbStopThreadTest extends JdbTest {
  57     public static void main(String argv[]) {
  58         new JdbStopThreadTest().run();
  59     }
  60 
  61     private JdbStopThreadTest() {
  62         super(DEBUGGEE_CLASS);
  63     }
  64 
  65     private static final String DEBUGGEE_CLASS = JdbStopThreadTestTarg.class.getName();
  66     private static final String PATTERN1_TEMPLATE = "^Breakpoint hit: \"thread=main\", " +
  67             "JdbStopThreadTestTarg\\.test\\(\\), line=%LINE_NUMBER.*\\R%LINE_NUMBER\\s+print\\(thread\\);.*\\R>\\s";
  68     private static final String PATTERN2_TEMPLATE = "^Breakpoint hit: \"thread=main\", " +
  69             "JdbStopThreadTestTarg\\.test\\(\\), line=%LINE_NUMBER.*\\R%LINE_NUMBER\\s+print\\(str\\);.*\\R>\\s";
  70 
  71     @Override
  72     protected void runCases() {
  73         // Test suspend policy SUSPEND_EVENT_THREAD
  74         int bpLine1 = parseBreakpoints(getTestSourcePath("JdbStopThreadTest.java"), 1).get(0);
  75         jdb.command(JdbCommand.stopThreadAt(DEBUGGEE_CLASS, bpLine1));
  76         String pattern1 = PATTERN1_TEMPLATE.replaceAll("%LINE_NUMBER", String.valueOf(bpLine1));
  77         // Run to breakpoint #1
  78         jdb.command(JdbCommand.run().waitForPrompt(pattern1, true));
  79         new OutputAnalyzer(jdb.getJdbOutput()).shouldMatch(pattern1);
  80 
  81 
  82         // Test suspend policy SUSPEND_NONE
  83         jdb.command(JdbCommand.thread(1));
  84         int bpLine2 = parseBreakpoints(getTestSourcePath("JdbStopThreadTest.java"), 2).get(0);
  85         jdb.command(JdbCommand.stopGoAt(DEBUGGEE_CLASS, bpLine2));
  86         String pattern2 = PATTERN2_TEMPLATE.replaceAll("%LINE_NUMBER", String.valueOf(bpLine2));
  87         jdb.command(JdbCommand.cont().allowExit());
  88         new OutputAnalyzer(jdb.getJdbOutput()).shouldMatch(pattern2);
  89     }
  90 }