Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdb/interrupt/interrupt001/interrupt001.java
40955 views
/*1* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25* @test26*27* @summary converted from VM Testbase nsk/jdb/interrupt/interrupt001.28* VM Testbase keywords: [jpda, jdb]29* VM Testbase readme:30* DECSRIPTION31* A positive test case for the 'interrupt <thread id>' command.32* The debuggee program (interrupt001a.java) creates a number of additional33* threads with name like "MyThread-<number>" and starts them. The jdb34* suspends the debuggee at a moment when the additional threads are35* waiting for notification for lock objects and then tries to interrupt them.36* If these threads are interrupted then a value of the special37* "notInterrupted" variable should not be modified.38* Value of "notInterrupted" variable is checked by "eval <expr>" command.39* The test passes if the value is equal to 0 and fails otherwise..40* COMMENTS41* Modified due to fix of the bug:42* 4760826 TTY: 'interrupt <threadID>' command sometimes does not work in Mantis43* Modified due to fix of the bug:44* 4974992 Missed prompt with jdb testcase nsk/jdb/interrupt/interrupt00145*46* @library /vmTestbase47* /test/lib48* @build nsk.jdb.interrupt.interrupt001.interrupt001a49* @run main/othervm50* nsk.jdb.interrupt.interrupt001.interrupt00151* -arch=${os.family}-${os.simpleArch}52* -waittime=553* -debugee.vmkind=java54* -transport.address=dynamic55* -jdb=${test.jdk}/bin/jdb56* -java.options="${test.vm.opts} ${test.java.opts}"57* -workdir=.58* -debugee.vmkeys="${test.vm.opts} ${test.java.opts}"59*/6061package nsk.jdb.interrupt.interrupt001;6263import nsk.share.*;64import nsk.share.jdb.*;6566import java.io.*;67import java.util.*;68import java.util.regex.Matcher;69import java.util.regex.Pattern;70import java.util.stream.Collectors;7172public class interrupt001 extends JdbTest {7374public static void main (String argv[]) {75System.exit(run(argv, System.out) + JCK_STATUS_BASE);76}7778public static int run(String argv[], PrintStream out) {79debuggeeClass = DEBUGGEE_CLASS;80firstBreak = FIRST_BREAK;81return new interrupt001().runTest(argv, out);82}8384static final String PACKAGE_NAME = "nsk.jdb.interrupt.interrupt001";85static final String TEST_CLASS = PACKAGE_NAME + ".interrupt001";86static final String DEBUGGEE_CLASS = TEST_CLASS + "a";87static final String FIRST_BREAK = DEBUGGEE_CLASS + ".main";88static final String LAST_BREAK = DEBUGGEE_CLASS + ".breakHere";89static final String MYTHREAD = "MyThread";90static final String DEBUGGEE_THREAD = DEBUGGEE_CLASS + "$" + MYTHREAD;91static final String DEBUGGEE_RESULT = DEBUGGEE_CLASS + ".notInterrupted.get()";9293static int numThreads = nsk.jdb.interrupt.interrupt001.interrupt001a.numThreads;9495/*96* Pattern for finding the thread ID in a line like the following:97* (nsk.jdb.interrupt.interrupt001.interrupt001a$MyThread)651 Thread-0 cond. waiting98* Note we can't match on DEBUGGEE_THREAD because it includes a $, which Pattern99* uses to match the end of a line.100*/101private static Pattern tidPattern = Pattern.compile("\\(.+" + MYTHREAD + "\\)(\\S+)");102103protected void runCases() {104String[] reply;105Paragrep grep;106String found;107String[] threads;108109jdb.setBreakpointInMethod(LAST_BREAK);110reply = jdb.receiveReplyFor(JdbCommand.cont);111112threads = jdb.getThreadIds(DEBUGGEE_THREAD);113114if (threads.length != numThreads) {115log.complain("jdb should report " + numThreads + " instance of " + DEBUGGEE_THREAD);116log.complain("Found: " + threads.length);117success = false;118}119120pauseTillAllThreadsWaiting(threads);121122for (int i = 0; i < threads.length; i++) {123reply = jdb.receiveReplyFor(JdbCommand.interrupt + threads[i]);124}125126reply = jdb.receiveReplyFor(JdbCommand.threads);127reply = jdb.receiveReplyFor(JdbCommand.cont, true);128129reply = jdb.receiveReplyFor(JdbCommand.eval + DEBUGGEE_RESULT);130grep = new Paragrep(reply);131found = grep.findFirst(DEBUGGEE_RESULT + " =" );132if (found.length() > 0) {133if (found.indexOf(DEBUGGEE_RESULT + " = 0") < 0) {134log.complain("Not all " + MYTHREAD + "s were interrupted.");135log.complain(found);136success = false;137}138} else {139log.complain("TEST BUG: not found value for " + DEBUGGEE_RESULT);140}141142jdb.contToExit(1);143}144145private void pauseTillAllThreadsWaiting(String[] threads) {146String[] reply;147boolean tidswaiting = false;148149Set<String> tids = new HashSet<>(Arrays.asList(threads));150Set<String> waitingTids = null;151152do {153String[] thrdsRply = (String[])jdb.receiveReplyFor(JdbCommand.threads);154waitingTids = Arrays.asList(thrdsRply).stream()155.filter((r)-> r.endsWith("waiting"))156.map((r)->{157Matcher m = tidPattern.matcher(r);158if (m.find()) {159return m.group(1);160}161return null;162})163.filter((r)-> r != null)164.collect(Collectors.toSet());165166// If all Tids are waiting set allWorkersAreWaiting to true so167// the main test thread will get out of its breakpoint loop168// and continue with the test.169if (waitingTids.containsAll(tids)) {170reply = jdb.receiveReplyFor(JdbCommand.set + DEBUGGEE_CLASS + ".allWorkersAreWaiting=true");171tidswaiting = true;172} else {173reply = jdb.receiveReplyFor(JdbCommand.cont);174}175} while (!tidswaiting);176}177}178179180