Path: blob/master/test/jdk/com/sun/jdi/DeferredStepTest.java
41152 views
/*1* Copyright (c) 2002, 2018, 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*/2223/*24* @test25* @bug 462954826* @summary Deferred StepRequests are lost in multithreaded debuggee27* @comment converted from test/jdk/com/sun/jdi/DeferredStepTest.sh28*29* @library /test/lib30* @build DeferredStepTest31* @run main/othervm DeferredStepTest32*/3334import jdk.test.lib.Asserts;35import jdk.test.lib.Utils;36import lib.jdb.JdbCommand;37import lib.jdb.JdbTest;3839import java.util.HashMap;40import java.util.List;41import java.util.Map;42import java.util.regex.Matcher;43import java.util.regex.Pattern;44import java.util.stream.Collectors;4546class DeferredStepTestTarg {47static class jj1 implements Runnable {48public void run() {49int count = 0;5051for (int ii = 0; ii < 15; ii++) {52int intInPotato04 = 666;53++count; // @1 breakpoint54System.out.println("Thread: " + Thread.currentThread().getName());55}56}57}5859static class jj2 implements Runnable {60public void run() {61int count2 = 0;6263for (int ii = 0; ii < 15; ii++) {64String StringInPotato05 = "I am";65++count2; // @2 breakpoint66System.out.println("Thread: " + Thread.currentThread().getName());67}68}69}7071public static void main(String argv[]) {72System.out.println("Version = " + System.getProperty("java.version"));7374jj1 obj1 = new jj1();75jj2 obj2 = new jj2();76new Thread(obj1, "jj1").start();77new Thread(obj2, "jj2").start();78}79}8081public class DeferredStepTest extends JdbTest {82public static void main(String argv[]) {83new DeferredStepTest().run();84}8586private DeferredStepTest() {87super(DeferredStepTestTarg.class.getName());88}8990private static class ThreadData {91int lastLine = -1; // line of the last stop92int minLine = -1; // min line (-1 means "not known yet")93int maxLine = -1; // max line (-1 means "not known yet")94}9596private Map<String, ThreadData> threadData = new HashMap<>();9798private Pattern threadRegexp = Pattern.compile("^(.+)\\[\\d+\\].*");99private Pattern lineRegexp = Pattern.compile("^(\\d+)\\b.*", Pattern.MULTILINE);100101// returns the 1st group of the pattern.102private String parse(Pattern p, String input) {103Matcher m = p.matcher(input);104if (!m.find()) {105throw new RuntimeException("Input '" + input + "' does not matches '" + p.pattern() + "'");106}107return m.group(1);108}109110private void next() {111List<String> reply = jdb.command(JdbCommand.next());112/*113* Each "next" produces something like ("Breakpoint hit" line only if the line has BP)114* Step completed:115* Breakpoint hit: "thread=jj2", DeferredStepTestTarg$jj2.run(), line=74 bci=12116* 74 ++count2; // @ 2 breakpoint117* <empty line>118* jj2[1]119*/120// detect thread from the last line121String lastLine = reply.get(reply.size() - 1);122String threadName = parse(threadRegexp, lastLine);123String wholeReply = reply.stream().collect(Collectors.joining(Utils.NEW_LINE));124int lineNum = Integer.parseInt(parse(lineRegexp, wholeReply));125126System.out.println("got: thread=" + threadName + ", line=" + lineNum);127128ThreadData data = threadData.get(threadName);129if (data == null) {130data = new ThreadData();131threadData.put(threadName, data);132}133processThreadData(threadName, lineNum, data);134}135136private void processThreadData(String threadName, int lineNum, ThreadData data) {137int lastLine = data.lastLine;138data.lastLine = lineNum;139if (lastLine < 0) {140// the 1st stop in the thread141return;142}143if (lineNum == lastLine + 1) {144// expected.145return;146}147if (lineNum < lastLine) {148// looks like step to the beginning of the cycle149if (data.minLine > 0) {150// minLine and maxLine are not set - verify151Asserts.assertEquals(lineNum, data.minLine, threadName + " - minLine");152Asserts.assertEquals(lastLine, data.maxLine, threadName + " - maxLine");153} else {154// set minLine/maxLine155data.minLine = lineNum;156data.maxLine = lastLine;157}158return;159}160throw new RuntimeException(threadName + " (line " + lineNum + ") - unexpected."161+ " lastLine=" + lastLine + ", minLine=" + data.minLine + ", maxLine=" + data.maxLine);162}163164@Override165protected void runCases() {166setBreakpoints(jdb, DeferredStepTestTarg.jj1.class.getName(),167getTestSourcePath("DeferredStepTest.java"), 1);168setBreakpoints(jdb, DeferredStepTestTarg.jj2.class.getName(),169getTestSourcePath("DeferredStepTest.java"), 2);170171// Run to breakpoint #1172jdb.command(JdbCommand.run());173174// 2 cycles (15 iterations) with 4 lines each, 1st break at 3rd line - 58 stops175for (int i = 0; i < 50; i++) {176next();177}178}179}180181182