Path: blob/master/test/jdk/com/sun/jdi/FetchLocals.java
41152 views
/*1* Copyright (c) 2013, 2017, 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// THIS TEST IS LINE NUMBER SENSITIVE2425/**26* @test27* @bug 4386002 442924528* @summary Test fix for: Incorrect values reported for some locals of type long29* @author Tim Bell30*31* @run build TestScaffold VMConnection TargetListener TargetAdapter32* @run compile -g FetchLocals.java33* @run driver FetchLocals34*/35import com.sun.jdi.*;36import com.sun.jdi.event.*;37import java.util.*;3839class FetchLocalsDebugee {40public long testMethod() {41short s = 12345;42int i = 8675309;43boolean pt = true;44long w = 973230999L;45byte b = 0x3b;46long x = w * 1000L;47char c = '\u005A'; // 005A = "Z"48long y = 22;49float f = 6.66f;50double d = 7.77;5152System.out.print("pt is: ");53System.out.println(pt);54System.out.print("b is: ");55System.out.println(b);56System.out.print("c is: ");57System.out.println(c);58System.out.print("s is: ");59System.out.println(s);60System.out.print("i is: ");61System.out.println(i);6263System.out.print("w is: ");64System.out.print(w);65System.out.print(" (0x");66System.out.print(Long.toHexString(w));67System.out.println(")");6869System.out.print("x is: ");70System.out.print(x);71System.out.print(" (0x");72System.out.print(Long.toHexString(x));73System.out.println(")");7475System.out.print("y is: ");76System.out.print(y);77System.out.print(" (0x");78System.out.print(Long.toHexString(y));79System.out.println(")");8081System.out.print("f is: ");82System.out.println(f);83System.out.print("d is: ");84System.out.println(d);85System.out.println(); // This is FetchLocals::LINE86if (w == 0xde00ad00be00ef00L) {87System.out.print ("The debugger was here. w modified to: 0x");88System.out.println(Long.toHexString(w));89} else {90System.out.print ("w contains : 0x");91System.out.println(Long.toHexString(w));92}93System.out.println();94return x;95}96public static void main(String[] args) {97System.out.print ("FetchLocalsDebugee");98System.out.println(" Starting up...");99FetchLocalsDebugee my = new FetchLocalsDebugee ();100long result = my.testMethod();101System.out.print ("testMethod() returned: ");102System.out.print (result);103System.out.print (" (0x");104System.out.print (Long.toHexString(result));105System.out.println(")");106107System.out.print ("FetchLocalsDebugee");108System.out.println(" Shutting down...");109}110}111112public class FetchLocals extends TestScaffold {113static final int LINE = 86;114115FetchLocals (String args[]) {116super(args);117}118119public static void main(String[] args)120throws Exception121{122new FetchLocals (args).startTests();123}124125/** Express a 64 bit double as a hex string126*/127private static String hexify(double d) {128long bits = Double.doubleToLongBits(d);129return (" (0x" + java.lang.Long.toHexString(bits) + ")");130}131/** Express a 32 bit float as a hex string132*/133private static String hexify(float f) {134int bits = Float.floatToIntBits(f);135return (" (0x" + java.lang.Integer.toHexString(bits) + ")");136}137138protected void test(String name, BooleanValue testV, boolean expectV)139throws Exception140{141if (testV.value() != expectV) {142System.out.println("Error for " + name + " = " + testV.value() +143" should be: " + expectV);144testFailed = true;145} else {146System.out.print ("Tested OK: ");147System.out.print (name);148System.out.print (" = ");149System.out.println(expectV);150}151}152153protected void test(String name, ByteValue testV, byte expectV)154throws Exception155{156if (testV.value() != expectV) {157System.out.println("Error for " + name + " = " + testV.value() +158" should be: " + expectV);159testFailed = true;160} else {161System.out.print ("Tested OK: ");162System.out.print (name);163System.out.print (" = ");164System.out.println(expectV);165}166}167168protected void test(String name, CharValue testV, char expectV)169throws Exception170{171if (testV.value() != expectV) {172System.out.println("Error for " + name + " = " + testV.value() +173" should be: " + expectV);174testFailed = true;175} else {176System.out.print ("Tested OK: ");177System.out.print (name);178System.out.print (" = ");179System.out.println(expectV);180}181}182183protected void test(String name, ShortValue testV, short expectV)184throws Exception185{186if (testV.value() != expectV) {187System.out.println("Error for " + name + " = " + testV.value() +188" should be: " + expectV);189testFailed = true;190} else {191System.out.print ("Tested OK: ");192System.out.print (name);193System.out.print (" = ");194System.out.println(expectV);195}196}197198protected void test(String name, IntegerValue testV, int expectV)199throws Exception200{201if (testV.value() != expectV) {202System.out.println("Error for " + name + " = " + testV.value() +203" should be: " + expectV);204testFailed = true;205} else {206System.out.print ("Tested OK: ");207System.out.print (name);208System.out.print (" = ");209System.out.println(expectV);210}211}212213protected void test(String name, LongValue testV, long expectV)214throws Exception215{216if (testV.value() != expectV) {217System.out.println("Error for " + name + " = 0x" +218Long.toHexString(testV.value()) +219" should be: 0x" +220Long.toHexString(expectV));221testFailed = true;222} else {223System.out.print ("Tested OK: ");224System.out.print (name);225System.out.print (" = ");226System.out.println(expectV);227}228}229230protected void test(String name, FloatValue testV, float expectV)231throws Exception232{233if (testV.value() != expectV) {234System.out.println("Error for " + name + " = " + testV.value() +235hexify(testV.value()) +236" should be: " + expectV +237hexify(expectV));238testFailed = true;239} else {240System.out.print ("Tested OK: ");241System.out.print (name);242System.out.print (" = ");243System.out.println(expectV);244}245}246247protected void test(String name, DoubleValue testV, double expectV)248throws Exception249{250if (testV.value() != expectV) {251System.out.println("Error for " + name + " = " + testV.value() +252hexify(testV.value()) +253" should be: " + expectV +254hexify(expectV));255testFailed = true;256} else {257System.out.print ("Tested OK: ");258System.out.print (name);259System.out.print (" = ");260System.out.println(expectV);261}262}263264protected void testLocalVariables (StackFrame sf)265throws Exception266{267/*268* Test values in the local method testMethod ():269* 1) Read current data270* 2) Test against the expected value271* 3) Set to a new value272* 4) Read again273* 5) Test against the expected value274*/275LocalVariable lv = sf.visibleVariableByName("pt");276BooleanValue booleanV = (BooleanValue) sf.getValue(lv);277test("pt", booleanV, true);278booleanV = vm().mirrorOf(false);279sf.setValue(lv, booleanV);280booleanV = (BooleanValue) sf.getValue(lv);281test("pt", booleanV, false);282283lv = sf.visibleVariableByName("b");284ByteValue byteV = (ByteValue) sf.getValue(lv);285byte bTmp = 0x3b;286test("b", byteV, bTmp);287bTmp = 0x7e;288byteV = vm().mirrorOf(bTmp);289sf.setValue(lv, byteV);290byteV = (ByteValue) sf.getValue(lv);291test("b", byteV, bTmp);292293lv = sf.visibleVariableByName("c");294CharValue charV = (CharValue) sf.getValue(lv);295char cTmp = '\u005A';296test("c", charV, cTmp);297cTmp = 'A';298charV = vm().mirrorOf(cTmp);299sf.setValue(lv, charV);300charV = (CharValue) sf.getValue(lv);301test("c", charV, cTmp);302303lv = sf.visibleVariableByName("s");304ShortValue shortV = (ShortValue) sf.getValue(lv);305short sTmp = 12345;306test("s", shortV, sTmp);307sTmp = -32766;308shortV = vm().mirrorOf(sTmp);309sf.setValue(lv, shortV);310shortV = (ShortValue) sf.getValue(lv);311test("s", shortV, sTmp);312313lv = sf.visibleVariableByName("i");314IntegerValue integerV = (IntegerValue) sf.getValue(lv);315int iTmp = 8675309;316test("i", integerV, iTmp);317iTmp = -42;318integerV = vm().mirrorOf(iTmp);319sf.setValue(lv, integerV);320integerV = (IntegerValue) sf.getValue(lv);321test("i", integerV, iTmp);322323lv = sf.visibleVariableByName("w");324LongValue longV = (LongValue) sf.getValue(lv);325long wTmp = 973230999L;326test("w", longV, wTmp);327wTmp = 0xde00ad00be00ef00L;328longV = vm().mirrorOf(wTmp);329sf.setValue(lv, longV);330longV = (LongValue) sf.getValue(lv);331test("w", longV, wTmp);332333lv = sf.visibleVariableByName("x");334longV = (LongValue) sf.getValue(lv);335long xTmp = 973230999L * 1000L;336test("x", longV, xTmp);337xTmp = 0xca00fe00ba00be00L;338longV = vm().mirrorOf(xTmp);339sf.setValue(lv, longV);340longV = (LongValue) sf.getValue(lv);341test("x", longV, xTmp);342343lv = sf.visibleVariableByName("y");344longV = (LongValue) sf.getValue(lv);345long yTmp = 22;346test("y", longV, yTmp);347yTmp = 0xdeadbeefcafebabeL;348longV = vm().mirrorOf(yTmp);349sf.setValue(lv, longV);350longV = (LongValue) sf.getValue(lv);351test("x", longV, yTmp);352353lv = sf.visibleVariableByName("f");354FloatValue floatV = (FloatValue) sf.getValue(lv);355float fTmp = 6.66f;356test("f", floatV, fTmp);357fTmp = (float)java.lang.Math.PI;358floatV = vm().mirrorOf(fTmp);359sf.setValue(lv, floatV);360floatV = (FloatValue)sf.getValue(lv);361test("f", floatV, fTmp);362363lv = sf.visibleVariableByName("d");364DoubleValue doubleV = (DoubleValue) sf.getValue(lv);365double dTmp = 7.77;366test("d", doubleV, dTmp);367dTmp = java.lang.Math.E;368doubleV = vm().mirrorOf(dTmp);369sf.setValue(lv, doubleV);370doubleV = (DoubleValue) sf.getValue(lv);371test("d", doubleV, dTmp);372}373374protected void runTests()375throws Exception376{377startToMain("FetchLocalsDebugee");378/*379* Get to the bottom of testMethod():380*/381try {382BreakpointEvent bpe = resumeTo("FetchLocalsDebugee", LINE);383/*384* Fetch values from fields; what did we get?385*/386StackFrame sf = bpe.thread().frame(0);387testLocalVariables (sf);388389} catch(Exception ex) {390ex.printStackTrace();391testFailed = true;392} finally {393// Allow application to complete and shut down394resumeToVMDisconnect();395}396if (!testFailed) {397System.out.println("FetchLocals: passed");398} else {399throw new Exception("FetchLocals: failed");400}401}402}403404405