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