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