test/jdk/com/sun/jdi/JdbStopThreadidTest.java
author mbaesken
Tue, 03 Sep 2019 16:52:55 +0200
changeset 58000 62926eb5e40e
parent 54003 f3468c1bdf5f
permissions -rw-r--r--
8230485: add handling of aix tar in configure Reviewed-by: erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53918
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
     1
/*
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
     4
 *
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
     7
 * published by the Free Software Foundation.
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
     8
 *
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    13
 * accompanied this code).
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    14
 *
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    18
 *
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    21
 * questions.
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    22
 */
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    23
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    24
/*
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    25
 * @test
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    26
 * @bug 8219143
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    27
 * @summary Tests that using the "stop in" threadid option will properly cause the
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    28
 * breakpoint to only be triggered when hit in the specified thread.
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    29
 *
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    30
 * @library /test/lib
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    31
 * @run compile -g JdbStopThreadidTest.java
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    32
 * @run main/othervm JdbStopThreadidTest
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    33
 */
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    34
54003
f3468c1bdf5f 8220030: JdbStopThreadidTest.java failed due to "Unexpected IO error while writing command 'quit' to jdb stdin stream"
cjplummer
parents: 53918
diff changeset
    35
import jdk.test.lib.process.OutputAnalyzer;
53918
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    36
import lib.jdb.Jdb;
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    37
import lib.jdb.JdbCommand;
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    38
import lib.jdb.JdbTest;
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    39
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    40
import java.util.regex.*;
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    41
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    42
class JdbStopThreadidTestTarg {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    43
    static Object lockObj = new Object();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    44
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    45
    public static void main(String[] args) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    46
        test();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    47
    }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    48
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    49
    private static void test() {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    50
        JdbStopThreadidTestTarg test = new JdbStopThreadidTestTarg();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    51
        MyThread myThread1 = test.new MyThread("MYTHREAD-1");
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    52
        MyThread myThread2 = test.new MyThread("MYTHREAD-2");
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    53
        MyThread myThread3 = test.new MyThread("MYTHREAD-3");
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    54
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    55
        synchronized (lockObj) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    56
            myThread1.start();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    57
            myThread2.start();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    58
            myThread3.start();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    59
            // Wait for all threads to have started. Note they all block on lockObj after starting.
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    60
            while (!myThread1.started || !myThread2.started || !myThread3.started) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    61
                try {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    62
                    Thread.sleep(50);
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    63
                } catch (InterruptedException e) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    64
                }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    65
            }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    66
            // Stop here so the test can setup the breakpoint in MYTHREAD-2
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    67
            brkMethod();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    68
        }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    69
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    70
        // Wait for all threads to finish before exiting
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    71
        try {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    72
            myThread1.join();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    73
            myThread2.join();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    74
            myThread3.join();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    75
        } catch (InterruptedException e) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    76
        }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    77
    }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    78
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    79
    static void brkMethod() {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    80
    }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    81
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    82
    public static void print(Object obj) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    83
        System.out.println(obj);
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    84
    }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    85
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    86
    class MyThread extends Thread {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    87
        volatile boolean started = false;
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    88
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    89
        public MyThread(String name) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    90
            super(name);
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    91
        }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    92
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    93
        public void run() {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    94
            started = true;
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    95
            synchronized (JdbStopThreadidTestTarg.lockObj) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    96
            }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    97
            brkMethod();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    98
        }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
    99
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   100
        void brkMethod() {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   101
        }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   102
    }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   103
}
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   104
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   105
public class JdbStopThreadidTest extends JdbTest {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   106
    public static void main(String argv[]) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   107
        new JdbStopThreadidTest().run();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   108
    }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   109
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   110
    private JdbStopThreadidTest() {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   111
        super(DEBUGGEE_CLASS);
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   112
    }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   113
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   114
    private static final String DEBUGGEE_CLASS = JdbStopThreadidTestTarg.class.getName();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   115
    private static final String DEBUGGEE_THREAD_CLASS = JdbStopThreadidTestTarg.class.getName() + "$MyThread";
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   116
    private static Pattern threadidPattern = Pattern.compile("MyThread\\)(\\S+)\\s+MYTHREAD-2");
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   117
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   118
    @Override
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   119
    protected void runCases() {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   120
        jdb.command(JdbCommand.stopIn(DEBUGGEE_CLASS, "brkMethod"));
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   121
        jdb.command(JdbCommand.run().waitForPrompt("Breakpoint hit: \"thread=main\"", true));
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   122
        jdb.command(JdbCommand.threads());
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   123
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   124
        // Find the threadid for MYTHREAD-2 in the "threads" command output
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   125
        String output = jdb.getJdbOutput();
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   126
        Matcher m = threadidPattern.matcher(output);
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   127
        String threadid = null;
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   128
        if (m.find()) {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   129
            threadid = m.group(1);
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   130
        } else {
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   131
            throw new RuntimeException("FAILED: Did not match threadid pattern.");
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   132
        }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   133
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   134
        // Setup a breakpoint in MYTHREAD-2.
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   135
        jdb.command(JdbCommand.stopInThreadid(DEBUGGEE_THREAD_CLASS, "brkMethod", threadid));
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   136
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   137
        // Continue until MYTHREAD-2 breakpoint is hit. If we hit any other breakpoint before
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   138
        // then (we aren't suppose to), then this test will fail.
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   139
        jdb.command(JdbCommand.cont().waitForPrompt("Breakpoint hit: \"thread=MYTHREAD-2\", \\S+MyThread.brkMethod", true));
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   140
        // Continue until the application exits. Once again, hitting a breakpoint will cause
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   141
        // a failure because we are not suppose to hit one.
54003
f3468c1bdf5f 8220030: JdbStopThreadidTest.java failed due to "Unexpected IO error while writing command 'quit' to jdb stdin stream"
cjplummer
parents: 53918
diff changeset
   142
        jdb.contToExit(1);
f3468c1bdf5f 8220030: JdbStopThreadidTest.java failed due to "Unexpected IO error while writing command 'quit' to jdb stdin stream"
cjplummer
parents: 53918
diff changeset
   143
        new OutputAnalyzer(getJdbOutput()).shouldContain(Jdb.APPLICATION_EXIT);
53918
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   144
    }
616a32d6b463 8219143: jdb should support breakpoint thread filters
cjplummer
parents:
diff changeset
   145
}