1 /*
   2  * Copyright (c) 2000, 2011, 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 
  25 package sun.jvm.hotspot;
  26 
  27 import sun.jvm.hotspot.debugger.*;
  28 import sun.jvm.hotspot.debugger.proc.*;
  29 
  30 // A test of the debugger backend. This should be used to connect to
  31 // the helloWorld.cpp program.
  32 
  33 public class TestDebugger {
  34 
  35   private static void usage() {
  36     System.out.println("usage: java TestDebugger [pid]");
  37     System.out.println("pid must be the process ID of the helloWorld process");
  38     System.exit(1);
  39   }
  40 
  41   public static void main(String[] args) {
  42     try {
  43       if (args.length != 1) {
  44         usage();
  45       }
  46 
  47       int pid = 0;
  48       try {
  49         pid = Integer.parseInt(args[0]);
  50       }
  51       catch (NumberFormatException e) {
  52         usage();
  53       }
  54 
  55       JVMDebugger debugger = new ProcDebuggerLocal(null, true);
  56 
  57       try {
  58         debugger.attach(pid);
  59       }
  60       catch (DebuggerException e) {
  61         System.err.print("Error attaching to process ID " + pid + ": ");
  62         if (e.getMessage() != null) {
  63           System.err.print(e.getMessage());
  64         }
  65         System.err.println();
  66         System.exit(1);
  67       }
  68 
  69       // HACK: configure debugger with primitive type sizes to get
  70       // Java types going
  71       debugger.configureJavaPrimitiveTypeSizes(1, 1, 2, 8, 4, 4, 8, 2);
  72 
  73       // FIXME: figure out how to canonicalize and/or eliminate
  74       // loadobject specification
  75       String loadObjectName = "-";
  76 
  77       //    long strAddr = debugger.lookup("helloWorld", "helloWorldString");
  78       Address addr = debugger.lookup(loadObjectName, "helloWorldString");
  79       if (addr == null) {
  80         System.err.println("Error looking up symbol \"helloWorldString\" in context \"" +
  81                            loadObjectName + "\"");
  82         System.exit(1);
  83       }
  84 
  85       // This is a pointer which points to the start of storage.
  86       // Dereference it.
  87       addr = addr.getAddressAt(0);
  88 
  89       // Read the number of bytes we know we need
  90       int helloWorldLen = 13;
  91       byte[] data = new byte[helloWorldLen];
  92       for (int i = 0; i < helloWorldLen; ++i) {
  93         data[i] = (byte) addr.getCIntegerAt(i, 1, false);
  94       }
  95 
  96       // Convert to characters
  97       char[] chars = new char[data.length];
  98       for (int i = 0; i < data.length; ++i) {
  99         chars[i] = (char) data[i];
 100       }
 101       String helloWorldStr = new String(chars);
 102 
 103       System.out.println("Successfully read string \"" + helloWorldStr + "\" from target process\n");
 104 
 105       // Test all Java data types (see helloWorld.cpp)
 106       byte   expectedByteValue   = (byte) 132;
 107       short  expectedShortValue  = (short) 27890;
 108       int    expectedIntValue    = 1020304050;
 109       long   expectedLongValue   = 102030405060708090L;
 110       float  expectedFloatValue  = 35.4F;
 111       double expectedDoubleValue = 1.23456789;
 112       byte   byteValue   = 0;
 113       short  shortValue  = 0;
 114       int    intValue    = 0;
 115       long   longValue   = 0;
 116       float  floatValue  = 0;
 117       double doubleValue = 0;
 118 
 119       addr = debugger.lookup(loadObjectName, "testByte");
 120       if (addr == null) {
 121         System.err.println("Error looking up symbol \"testByte\" in context \"" +
 122                            loadObjectName + "\"");
 123         System.exit(1);
 124       }
 125       byteValue = addr.getJByteAt(0);
 126       if (byteValue != expectedByteValue) {
 127         System.err.println("Error: unexpected byte value (got " +
 128                            byteValue + ", expected " + expectedByteValue + ")");
 129         System.exit(1);
 130       }
 131 
 132       addr = debugger.lookup(loadObjectName, "testShort");
 133       if (addr == null) {
 134         System.err.println("Error looking up symbol \"testShort\" in context \"" +
 135                            loadObjectName + "\"");
 136         System.exit(1);
 137       }
 138       shortValue = addr.getJShortAt(0);
 139       if (shortValue != expectedShortValue) {
 140         System.err.println("Error: unexpected short value (got " +
 141                            shortValue + ", expected " + expectedShortValue + ")");
 142         System.exit(1);
 143       }
 144 
 145       addr = debugger.lookup(loadObjectName, "testInt");
 146       if (addr == null) {
 147         System.err.println("Error looking up symbol \"testInt\" in context \"" +
 148                            loadObjectName + "\"");
 149         System.exit(1);
 150       }
 151       intValue = addr.getJIntAt(0);
 152       if (intValue != expectedIntValue) {
 153         System.err.println("Error: unexpected int value (got " +
 154                            intValue + ", expected " + expectedIntValue + ")");
 155         System.exit(1);
 156       }
 157 
 158       addr = debugger.lookup(loadObjectName, "testLong");
 159       if (addr == null) {
 160         System.err.println("Error looking up symbol \"testLong\" in context \"" +
 161                            loadObjectName + "\"");
 162         System.exit(1);
 163       }
 164       longValue = addr.getJLongAt(0);
 165       if (longValue != expectedLongValue) {
 166         System.err.println("Error: unexpected long value (got " +
 167                            longValue + ", expected " + expectedLongValue + ")");
 168         System.exit(1);
 169       }
 170 
 171       addr = debugger.lookup(loadObjectName, "testFloat");
 172       if (addr == null) {
 173         System.err.println("Error looking up symbol \"testFloat\" in context \"" +
 174                            loadObjectName + "\"");
 175         System.exit(1);
 176       }
 177       floatValue = addr.getJFloatAt(0);
 178       if (floatValue != expectedFloatValue) {
 179         System.err.println("Error: unexpected float value (got " +
 180                            floatValue + ", expected " + expectedFloatValue + ")");
 181         System.exit(1);
 182       }
 183 
 184       addr = debugger.lookup(loadObjectName, "testDouble");
 185       if (addr == null) {
 186         System.err.println("Error looking up symbol \"testDouble\" in context \"" +
 187                            loadObjectName + "\"");
 188         System.exit(1);
 189       }
 190       doubleValue = addr.getJDoubleAt(0);
 191       if (doubleValue != expectedDoubleValue) {
 192         System.err.println("Error: unexpected double value (got " +
 193                            doubleValue + ", expected " + expectedDoubleValue + ")");
 194         System.exit(1);
 195       }
 196 
 197       System.err.println("All tests passed successfully.");
 198 
 199       debugger.detach();
 200     }
 201     catch (AddressException e) {
 202       System.err.println("Error occurred during test:");
 203       e.printStackTrace();
 204       System.exit(1);
 205     }
 206   }
 207 }