--- /dev/null 2018-10-03 20:26:02.000000000 -0700 +++ new/test/jdk/com/sun/jdi/MethodInvokeWithTraceOnTest.java 2018-10-03 20:26:02.000000000 -0700 @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * @test + * @bug 8193801 8129348 + * @summary Invokes static and instance methods when debugger trace + * mode is on. + * + * @library /test/lib + * @run build TestScaffold VMConnection TargetListener TargetAdapter + * @run compile -g MethodInvokeWithTraceOnTest.java + * @run driver MethodInvokeWithTraceOnTest + */ + +import com.sun.jdi.*; +import com.sun.jdi.event.*; + +import java.util.*; + +import static lib.jdb.JdbTest.*; + +/********** target program **********/ + +class MethodInvokeWithTraceOnTestTarg { + public static void main(String[] args) { + new MethodInvokeWithTraceOnTestTarg().test(); + } + + private void test() { + Thread thread = Thread.currentThread(); + print(thread); // @1 breakpoint + } + + public void print(Object obj) { + System.out.println(obj); + } +} + + +/********** test program **********/ + +public class MethodInvokeWithTraceOnTest extends TestScaffold { + + MethodInvokeWithTraceOnTest(String args[]) { + super(args); + } + + public static void main(String[] args) + throws Exception { + new MethodInvokeWithTraceOnTest(args).startTests(); + } + + /********** test core **********/ + + protected void runTests() + throws Exception { + BreakpointEvent be = init(); + testStaticMethod(be); + testInstanceMethod(be); + + /* + * resume the target listening for events + */ + listenUntilVMDisconnect(); + } + + private BreakpointEvent init() throws Exception{ + startToMain("MethodInvokeWithTraceOnTestTarg"); + vm().setDebugTraceMode(VirtualMachine.TRACE_ALL); + int bkpLine = parseBreakpoints(getTestSourcePath("MethodInvokeWithTraceOnTest.java"), 1).get(0); + System.out.println("Running to line: " + bkpLine); + return resumeTo("MethodInvokeWithTraceOnTestTarg", bkpLine); + } + + private void testStaticMethod( BreakpointEvent be) throws Exception { + System.out.println("Testing static method..."); + ClassType classType = getClassType("java.lang.Class"); + Method forName = classType.methodsByName("forName", + "(Ljava/lang/String;)Ljava/lang/Class;").get(0); + StringReference classNameParam = vm().mirrorOf("java.lang.String"); + classType.invokeMethod(be.thread(), forName, Collections.singletonList(classNameParam), 0); + + } + + private void testInstanceMethod( BreakpointEvent be) throws Exception { + System.out.println("Testing instance method..."); + ThreadReference thread = be.thread(); + StackFrame frame = thread.frame(0); + ObjectReference thisObj = frame.thisObject(); + LocalVariable threadVar = frame.visibleVariableByName("thread"); + ThreadReference threadObj = (ThreadReference)frame.getValue(threadVar); + + ClassType classType = (ClassType)thisObj.referenceType(); + for(Method m: classType.methods()){ + System.out.println(m.signature() + ":" + m.name()); + } + Method printMethod = classType.methodsByName("print", + "(Ljava/lang/Object;)V").get(0); + StringReference objectToPrint = vm().mirrorOf("test string"); + System.out.println("Passing StringReference to instance method..."); + thisObj.invokeMethod(thread, printMethod, Collections.singletonList(objectToPrint), 0); + System.out.println("Passing ThreadReference to instance method..."); + thisObj.invokeMethod(thread, printMethod, Collections.singletonList(threadObj), 0); + + } + + private ClassType getClassType(String className) throws Exception { + List classes = vm().classesByName(className); + return (ClassType) classes.get(0); + } + +}