1 /*
   2  * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 //    THIS TEST IS LINE NUMBER SENSITIVE
  25 
  26 /**
  27  * @test
  28  * @bug 4386002 4429245
  29  * @summary Test fix for: Incorrect values reported for some locals of type long
  30  * @author Tim Bell
  31  *
  32  * @run build TestScaffold VMConnection TargetListener TargetAdapter
  33  * @run compile -g FetchLocals.java
  34  * @run driver FetchLocals
  35  */
  36 import com.sun.jdi.*;
  37 import com.sun.jdi.event.*;
  38 import java.util.*;
  39 
  40 class FetchLocalsDebugee {
  41     public long testMethod() {
  42         short s = 12345;
  43         int i = 8675309;
  44         boolean pt = true;
  45         long w = 973230999L;
  46         byte b = 0x3b;
  47         long x = w * 1000L;
  48         char c = '\u005A';      // 005A = "Z"
  49         long y = 22;
  50         float f = 6.66f;
  51         double d = 7.77;
  52 
  53         System.out.print("pt is: ");
  54         System.out.println(pt);
  55         System.out.print("b is: ");
  56         System.out.println(b);
  57         System.out.print("c is: ");
  58         System.out.println(c);
  59         System.out.print("s is: ");
  60         System.out.println(s);
  61         System.out.print("i is: ");
  62         System.out.println(i);
  63 
  64         System.out.print("w is: ");
  65         System.out.print(w);
  66         System.out.print("     (0x");
  67         System.out.print(Long.toHexString(w));
  68         System.out.println(")");
  69 
  70         System.out.print("x is: ");
  71         System.out.print(x);
  72         System.out.print("  (0x");
  73         System.out.print(Long.toHexString(x));
  74         System.out.println(")");
  75 
  76         System.out.print("y is: ");
  77         System.out.print(y);
  78         System.out.print("            (0x");
  79         System.out.print(Long.toHexString(y));
  80         System.out.println(")");
  81 
  82         System.out.print("f is: ");
  83         System.out.println(f);
  84         System.out.print("d is: ");
  85         System.out.println(d);
  86         System.out.println();   // This is FetchLocals::LINE
  87         if (w == 0xde00ad00be00ef00L) {
  88           System.out.print  ("The debugger was here.  w modified to: 0x");
  89           System.out.println(Long.toHexString(w));
  90         } else {
  91           System.out.print  ("w contains : 0x");
  92           System.out.println(Long.toHexString(w));
  93         }
  94         System.out.println();
  95         return x;
  96     }
  97     public static void main(String[] args) {
  98         System.out.print  ("FetchLocalsDebugee");
  99         System.out.println(" Starting up...");
 100         FetchLocalsDebugee my = new FetchLocalsDebugee ();
 101         long result = my.testMethod();
 102         System.out.print  ("testMethod() returned: ");
 103         System.out.print  (result);
 104         System.out.print  (" (0x");
 105         System.out.print  (Long.toHexString(result));
 106         System.out.println(")");
 107 
 108         System.out.print  ("FetchLocalsDebugee");
 109         System.out.println(" Shutting down...");
 110     }
 111 }
 112 
 113 public class FetchLocals extends TestScaffold {
 114     static final int LINE = 86;
 115 
 116     FetchLocals (String args[]) {
 117         super(args);
 118     }
 119 
 120     public static void main(String[] args)
 121         throws Exception
 122     {
 123         new FetchLocals (args).startTests();
 124     }
 125 
 126     /** Express a 64 bit double as a hex string
 127       */
 128     private static String hexify(double d) {
 129         long bits = Double.doubleToLongBits(d);
 130         return (" (0x" + java.lang.Long.toHexString(bits) + ")");
 131     }
 132     /** Express a 32 bit float as a hex string
 133       */
 134     private static String hexify(float f) {
 135         int bits = Float.floatToIntBits(f);
 136         return (" (0x" + java.lang.Integer.toHexString(bits) + ")");
 137     }
 138 
 139     protected void test(String name, BooleanValue testV, boolean expectV)
 140         throws Exception
 141     {
 142         if (testV.value() != expectV) {
 143             System.out.println("Error for " + name + " = " + testV.value() +
 144                                  " should be: " + expectV);
 145             testFailed = true;
 146         } else {
 147             System.out.print  ("Tested OK: ");
 148             System.out.print  (name);
 149             System.out.print  (" = ");
 150             System.out.println(expectV);
 151         }
 152     }
 153 
 154     protected void test(String name, ByteValue testV, byte expectV)
 155         throws Exception
 156     {
 157         if (testV.value() != expectV) {
 158             System.out.println("Error for " + name + " = " + testV.value() +
 159                                  " should be: " + expectV);
 160             testFailed = true;
 161         } else {
 162             System.out.print  ("Tested OK: ");
 163             System.out.print  (name);
 164             System.out.print  (" = ");
 165             System.out.println(expectV);
 166         }
 167     }
 168 
 169     protected void test(String name, CharValue testV, char expectV)
 170         throws Exception
 171     {
 172         if (testV.value() != expectV) {
 173             System.out.println("Error for " + name + " = " + testV.value() +
 174                                  " should be: " + expectV);
 175             testFailed = true;
 176         } else {
 177             System.out.print  ("Tested OK: ");
 178             System.out.print  (name);
 179             System.out.print  (" = ");
 180             System.out.println(expectV);
 181         }
 182     }
 183 
 184     protected void test(String name, ShortValue testV, short expectV)
 185         throws Exception
 186     {
 187         if (testV.value() != expectV) {
 188             System.out.println("Error for " + name + " = " + testV.value() +
 189                                  " should be: " + expectV);
 190             testFailed = true;
 191         } else {
 192             System.out.print  ("Tested OK: ");
 193             System.out.print  (name);
 194             System.out.print  (" = ");
 195             System.out.println(expectV);
 196         }
 197     }
 198 
 199     protected void test(String name, IntegerValue testV, int expectV)
 200         throws Exception
 201     {
 202         if (testV.value() != expectV) {
 203             System.out.println("Error for " + name + " = " + testV.value() +
 204                                  " should be: " + expectV);
 205             testFailed = true;
 206         } else {
 207             System.out.print  ("Tested OK: ");
 208             System.out.print  (name);
 209             System.out.print  (" = ");
 210             System.out.println(expectV);
 211         }
 212     }
 213 
 214     protected void test(String name, LongValue testV, long expectV)
 215         throws Exception
 216     {
 217         if (testV.value() != expectV) {
 218             System.out.println("Error for " + name + " = 0x" +
 219                                  Long.toHexString(testV.value()) +
 220                                  " should be: 0x" +
 221                                  Long.toHexString(expectV));
 222             testFailed = true;
 223         } else {
 224             System.out.print  ("Tested OK: ");
 225             System.out.print  (name);
 226             System.out.print  (" = ");
 227             System.out.println(expectV);
 228         }
 229     }
 230 
 231     protected void test(String name, FloatValue testV, float expectV)
 232         throws Exception
 233     {
 234         if (testV.value() != expectV) {
 235             System.out.println("Error for " + name + " = " + testV.value() +
 236                                hexify(testV.value()) +
 237                                " should be: " + expectV +
 238                                hexify(expectV));
 239             testFailed = true;
 240         } else {
 241             System.out.print  ("Tested OK: ");
 242             System.out.print  (name);
 243             System.out.print  (" = ");
 244             System.out.println(expectV);
 245         }
 246     }
 247 
 248     protected void test(String name, DoubleValue testV, double expectV)
 249         throws Exception
 250     {
 251         if (testV.value() != expectV) {
 252             System.out.println("Error for " + name + " = " + testV.value() +
 253                                hexify(testV.value()) +
 254                                " should be: " + expectV +
 255                                hexify(expectV));
 256             testFailed = true;
 257         } else {
 258             System.out.print  ("Tested OK: ");
 259             System.out.print  (name);
 260             System.out.print  (" = ");
 261             System.out.println(expectV);
 262         }
 263     }
 264 
 265     protected void testLocalVariables (StackFrame sf)
 266         throws Exception
 267     {
 268         /*
 269          * Test values in the local method testMethod ():
 270          *   1) Read current data
 271          *   2) Test against the expected value
 272          *   3) Set to a new value
 273          *   4) Read again
 274          *   5) Test against the expected value
 275          */
 276         LocalVariable lv = sf.visibleVariableByName("pt");
 277         BooleanValue booleanV = (BooleanValue) sf.getValue(lv);
 278         test("pt", booleanV, true);
 279         booleanV = vm().mirrorOf(false);
 280         sf.setValue(lv, booleanV);
 281         booleanV = (BooleanValue) sf.getValue(lv);
 282         test("pt", booleanV, false);
 283 
 284         lv = sf.visibleVariableByName("b");
 285         ByteValue byteV = (ByteValue) sf.getValue(lv);
 286         byte bTmp = 0x3b;
 287         test("b", byteV, bTmp);
 288         bTmp = 0x7e;
 289         byteV = vm().mirrorOf(bTmp);
 290         sf.setValue(lv, byteV);
 291         byteV = (ByteValue) sf.getValue(lv);
 292         test("b", byteV, bTmp);
 293 
 294         lv = sf.visibleVariableByName("c");
 295         CharValue charV = (CharValue) sf.getValue(lv);
 296         char cTmp = '\u005A';
 297         test("c", charV, cTmp);
 298         cTmp = 'A';
 299         charV = vm().mirrorOf(cTmp);
 300         sf.setValue(lv, charV);
 301         charV = (CharValue) sf.getValue(lv);
 302         test("c", charV, cTmp);
 303 
 304         lv = sf.visibleVariableByName("s");
 305         ShortValue shortV = (ShortValue) sf.getValue(lv);
 306         short sTmp = 12345;
 307         test("s", shortV, sTmp);
 308         sTmp = -32766;
 309         shortV = vm().mirrorOf(sTmp);
 310         sf.setValue(lv, shortV);
 311         shortV = (ShortValue) sf.getValue(lv);
 312         test("s", shortV, sTmp);
 313 
 314         lv = sf.visibleVariableByName("i");
 315         IntegerValue integerV = (IntegerValue) sf.getValue(lv);
 316         int iTmp = 8675309;
 317         test("i", integerV, iTmp);
 318         iTmp = -42;
 319         integerV = vm().mirrorOf(iTmp);
 320         sf.setValue(lv, integerV);
 321         integerV = (IntegerValue) sf.getValue(lv);
 322         test("i", integerV, iTmp);
 323 
 324         lv = sf.visibleVariableByName("w");
 325         LongValue longV = (LongValue) sf.getValue(lv);
 326         long wTmp = 973230999L;
 327         test("w", longV, wTmp);
 328         wTmp = 0xde00ad00be00ef00L;
 329         longV = vm().mirrorOf(wTmp);
 330         sf.setValue(lv, longV);
 331         longV = (LongValue) sf.getValue(lv);
 332         test("w", longV, wTmp);
 333 
 334         lv = sf.visibleVariableByName("x");
 335         longV = (LongValue) sf.getValue(lv);
 336         long xTmp = 973230999L * 1000L;
 337         test("x", longV, xTmp);
 338         xTmp = 0xca00fe00ba00be00L;
 339         longV = vm().mirrorOf(xTmp);
 340         sf.setValue(lv, longV);
 341         longV = (LongValue) sf.getValue(lv);
 342         test("x", longV, xTmp);
 343 
 344         lv = sf.visibleVariableByName("y");
 345         longV = (LongValue) sf.getValue(lv);
 346         long yTmp = 22;
 347         test("y", longV, yTmp);
 348         yTmp = 0xdeadbeefcafebabeL;
 349         longV = vm().mirrorOf(yTmp);
 350         sf.setValue(lv, longV);
 351         longV = (LongValue) sf.getValue(lv);
 352         test("x", longV, yTmp);
 353 
 354         lv = sf.visibleVariableByName("f");
 355         FloatValue floatV = (FloatValue) sf.getValue(lv);
 356         float fTmp = 6.66f;
 357         test("f", floatV, fTmp);
 358         fTmp = (float)java.lang.Math.PI;
 359         floatV = vm().mirrorOf(fTmp);
 360         sf.setValue(lv, floatV);
 361         floatV = (FloatValue)sf.getValue(lv);
 362         test("f", floatV, fTmp);
 363 
 364         lv = sf.visibleVariableByName("d");
 365         DoubleValue doubleV = (DoubleValue) sf.getValue(lv);
 366         double dTmp = 7.77;
 367         test("d", doubleV, dTmp);
 368         dTmp = java.lang.Math.E;
 369         doubleV = vm().mirrorOf(dTmp);
 370         sf.setValue(lv, doubleV);
 371         doubleV = (DoubleValue) sf.getValue(lv);
 372         test("d", doubleV, dTmp);
 373     }
 374 
 375     protected void runTests()
 376         throws Exception
 377     {
 378         startToMain("FetchLocalsDebugee");
 379         /*
 380          * Get to the bottom of testMethod():
 381          */
 382         try {
 383             BreakpointEvent bpe = resumeTo("FetchLocalsDebugee", LINE);
 384             /*
 385              * Fetch values from fields; what did we get?
 386              */
 387             StackFrame sf = bpe.thread().frame(0);
 388             testLocalVariables (sf);
 389 
 390         } catch(Exception ex) {
 391             ex.printStackTrace();
 392             testFailed = true;
 393         } finally {
 394             // Allow application to complete and shut down
 395             resumeToVMDisconnect();
 396         }
 397         if (!testFailed) {
 398             System.out.println("FetchLocals: passed");
 399         } else {
 400             throw new Exception("FetchLocals: failed");
 401         }
 402     }
 403 }