1 /*
   2  * Copyright 2000-2002 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *  
  23  */
  24 
  25 package sun.jvm.hotspot.debugger.dbx;
  26 
  27 import sun.jvm.hotspot.debugger.*;
  28 
  29 class DbxAddress implements Address {
  30   protected DbxDebugger debugger;
  31   protected long addr;
  32 
  33   DbxAddress(DbxDebugger debugger, long addr) {
  34     this.debugger = debugger;
  35     this.addr = addr;
  36   }
  37 
  38   //
  39   // Basic Java routines
  40   //
  41 
  42   public boolean equals(Object arg) {
  43     if (arg == null) {
  44       return false;
  45     }
  46 
  47     if (!(arg instanceof DbxAddress)) {
  48       return false;
  49     }
  50 
  51     return (addr == ((DbxAddress) arg).addr);
  52   }
  53 
  54   public int hashCode() {
  55     // FIXME: suggestions on a better hash code?
  56     return (int) addr;
  57   }
  58 
  59   public String toString() {
  60     return debugger.addressValueToString(addr);
  61   }
  62 
  63   //
  64   // C/C++-related routines
  65   //
  66 
  67   public long getCIntegerAt(long offset, long numBytes, boolean isUnsigned) throws UnalignedAddressException, UnmappedAddressException {
  68     return debugger.readCInteger(addr + offset, numBytes, isUnsigned);
  69   }
  70 
  71   public Address getAddressAt(long offset) throws UnalignedAddressException, UnmappedAddressException {
  72     return debugger.readAddress(addr + offset);
  73   }
  74 
  75   //
  76   // Java-related routines
  77   //
  78 
  79   public boolean getJBooleanAt(long offset) throws UnalignedAddressException, UnmappedAddressException {
  80     return debugger.readJBoolean(addr + offset);
  81   }
  82 
  83   public byte getJByteAt(long offset) throws UnalignedAddressException, UnmappedAddressException {
  84     return debugger.readJByte(addr + offset);
  85   }
  86 
  87   public char getJCharAt(long offset) throws UnalignedAddressException, UnmappedAddressException {
  88     return debugger.readJChar(addr + offset);
  89   }
  90 
  91   public double getJDoubleAt(long offset) throws UnalignedAddressException, UnmappedAddressException {
  92     return debugger.readJDouble(addr + offset);
  93   }
  94 
  95   public float getJFloatAt(long offset) throws UnalignedAddressException, UnmappedAddressException {
  96     return debugger.readJFloat(addr + offset);
  97   }
  98 
  99   public int getJIntAt(long offset) throws UnalignedAddressException, UnmappedAddressException {
 100     return debugger.readJInt(addr + offset);
 101   }
 102 
 103   public long getJLongAt(long offset) throws UnalignedAddressException, UnmappedAddressException {
 104     return debugger.readJLong(addr + offset);
 105   }
 106 
 107   public short getJShortAt(long offset) throws UnalignedAddressException, UnmappedAddressException {
 108     return debugger.readJShort(addr + offset);
 109   }
 110 
 111   public OopHandle getOopHandleAt(long offset)
 112     throws UnalignedAddressException, UnmappedAddressException, NotInHeapException {
 113     return debugger.readOopHandle(addr + offset);
 114   }
 115 
 116   // Mutators -- not implemented for now (FIXME)
 117   public void setCIntegerAt(long offset, long numBytes, long value) {
 118     throw new DebuggerException("Unimplemented");
 119   }
 120   public void setAddressAt(long offset, Address value) {
 121     throw new DebuggerException("Unimplemented");
 122   }
 123   public void       setJBooleanAt      (long offset, boolean value)
 124     throws UnmappedAddressException, UnalignedAddressException {
 125     throw new DebuggerException("Unimplemented");
 126   }
 127   public void       setJByteAt         (long offset, byte value)
 128     throws UnmappedAddressException, UnalignedAddressException {
 129     throw new DebuggerException("Unimplemented");
 130   }
 131   public void       setJCharAt         (long offset, char value)
 132     throws UnmappedAddressException, UnalignedAddressException {
 133     throw new DebuggerException("Unimplemented");
 134   }
 135   public void       setJDoubleAt       (long offset, double value)
 136     throws UnmappedAddressException, UnalignedAddressException {
 137     throw new DebuggerException("Unimplemented");
 138   }
 139   public void       setJFloatAt        (long offset, float value)
 140     throws UnmappedAddressException, UnalignedAddressException {
 141     throw new DebuggerException("Unimplemented");
 142   }
 143   public void       setJIntAt          (long offset, int value)
 144     throws UnmappedAddressException, UnalignedAddressException {
 145     throw new DebuggerException("Unimplemented");
 146   }
 147   public void       setJLongAt         (long offset, long value)
 148     throws UnmappedAddressException, UnalignedAddressException {
 149     throw new DebuggerException("Unimplemented");
 150   }
 151   public void       setJShortAt        (long offset, short value)
 152     throws UnmappedAddressException, UnalignedAddressException {
 153     throw new DebuggerException("Unimplemented");
 154   }
 155   public void       setOopHandleAt     (long offset, OopHandle value)
 156     throws UnmappedAddressException, UnalignedAddressException {
 157     throw new DebuggerException("Unimplemented");
 158   }
 159 
 160   //
 161   // Arithmetic operations -- necessary evil.
 162   //
 163 
 164   public Address    addOffsetTo       (long offset) throws UnsupportedOperationException {
 165     long value = addr + offset;
 166     if (value == 0) {
 167       return null;
 168     }
 169     return new DbxAddress(debugger, value);
 170   }
 171 
 172   public OopHandle  addOffsetToAsOopHandle(long offset) throws UnsupportedOperationException {
 173     long value = addr + offset;
 174     if (value == 0) {
 175       return null;
 176     }
 177     return new DbxOopHandle(debugger, value);
 178   }
 179 
 180   /** (FIXME: any signed/unsigned issues? Should this work for
 181       OopHandles?) */
 182   public long       minus(Address arg) {
 183     if (arg == null) {
 184       return addr;
 185     }
 186     return addr - ((DbxAddress) arg).addr;
 187   }
 188 
 189   // Two's complement representation.
 190   // All negative numbers are larger than positive numbers.
 191   // Numbers with the same sign can be compared normally.
 192   // Test harness is below in main().
 193 
 194   public boolean    lessThan          (Address arg) {
 195     if (arg == null) {
 196       return false;
 197     }
 198     DbxAddress dbxArg = (DbxAddress) arg;
 199     if ((addr >= 0) && (dbxArg.addr < 0)) {
 200       return true;
 201     }
 202     if ((addr < 0) && (dbxArg.addr >= 0)) {
 203       return false;
 204     }
 205     return (addr < dbxArg.addr);
 206   }
 207 
 208   public boolean    lessThanOrEqual   (Address arg) {
 209     if (arg == null) {
 210       return false;
 211     }
 212     DbxAddress dbxArg = (DbxAddress) arg;
 213     if ((addr >= 0) && (dbxArg.addr < 0)) {
 214       return true;
 215     }
 216     if ((addr < 0) && (dbxArg.addr >= 0)) {
 217       return false;
 218     }
 219     return (addr <= dbxArg.addr);
 220   }
 221 
 222   public boolean    greaterThan       (Address arg) {
 223     if (arg == null) {
 224       return true;
 225     }
 226     DbxAddress dbxArg = (DbxAddress) arg;
 227     if ((addr >= 0) && (dbxArg.addr < 0)) {
 228       return false;
 229     }
 230     if ((addr < 0) && (dbxArg.addr >= 0)) {
 231       return true;
 232     }
 233     return (addr > dbxArg.addr);
 234   }
 235 
 236   public boolean    greaterThanOrEqual(Address arg) {
 237     if (arg == null) {
 238       return true;
 239     }
 240     DbxAddress dbxArg = (DbxAddress) arg;
 241     if ((addr >= 0) && (dbxArg.addr < 0)) {
 242       return false;
 243     }
 244     if ((addr < 0) && (dbxArg.addr >= 0)) {
 245       return true;
 246     }
 247     return (addr >= dbxArg.addr);
 248   }
 249 
 250   public Address    andWithMask(long mask) throws UnsupportedOperationException {
 251     long value = addr & mask;
 252     if (value == 0) {
 253       return null;
 254     }
 255     return new DbxAddress(debugger, value);
 256   }
 257 
 258   public Address    orWithMask(long mask) throws UnsupportedOperationException {
 259     long value = addr | mask;
 260     if (value == 0) {
 261       return null;
 262     }
 263     return new DbxAddress(debugger, value);
 264   }
 265 
 266   public Address    xorWithMask(long mask) throws UnsupportedOperationException {
 267     long value = addr ^ mask;
 268     if (value == 0) {
 269       return null;
 270     }
 271     return new DbxAddress(debugger, value);
 272   }
 273 
 274 
 275   //--------------------------------------------------------------------------------
 276   // Internals only below this point
 277   //
 278 
 279   long getValue() {
 280     return addr;
 281   }
 282 
 283 
 284   private static void check(boolean arg, String failMessage) {
 285     if (!arg) {
 286       System.err.println(failMessage + ": FAILED");
 287       System.exit(1);
 288     }
 289   }
 290 
 291   // Test harness
 292   public static void main(String[] args) {
 293     // p/n indicates whether the interior address is really positive
 294     // or negative. In unsigned terms, p1 < p2 < n1 < n2.
 295 
 296     DbxAddress p1 = new DbxAddress(null, 0x7FFFFFFFFFFFFFF0L);
 297     DbxAddress p2 = (DbxAddress) p1.addOffsetTo(10);
 298     DbxAddress n1 = (DbxAddress) p2.addOffsetTo(10);
 299     DbxAddress n2 = (DbxAddress) n1.addOffsetTo(10);
 300     
 301     // lessThan positive tests
 302     check(p1.lessThan(p2), "lessThan 1");
 303     check(p1.lessThan(n1), "lessThan 2");
 304     check(p1.lessThan(n2), "lessThan 3");
 305     check(p2.lessThan(n1), "lessThan 4");
 306     check(p2.lessThan(n2), "lessThan 5");
 307     check(n1.lessThan(n2), "lessThan 6");
 308 
 309     // lessThan negative tests
 310     check(!p1.lessThan(p1), "lessThan 7");
 311     check(!p2.lessThan(p2), "lessThan 8");
 312     check(!n1.lessThan(n1), "lessThan 9");
 313     check(!n2.lessThan(n2), "lessThan 10");
 314 
 315     check(!p2.lessThan(p1), "lessThan 11");
 316     check(!n1.lessThan(p1), "lessThan 12");
 317     check(!n2.lessThan(p1), "lessThan 13");
 318     check(!n1.lessThan(p2), "lessThan 14");
 319     check(!n2.lessThan(p2), "lessThan 15");
 320     check(!n2.lessThan(n1), "lessThan 16");
 321 
 322     // lessThanOrEqual positive tests
 323     check(p1.lessThanOrEqual(p1), "lessThanOrEqual 1");
 324     check(p2.lessThanOrEqual(p2), "lessThanOrEqual 2");
 325     check(n1.lessThanOrEqual(n1), "lessThanOrEqual 3");
 326     check(n2.lessThanOrEqual(n2), "lessThanOrEqual 4");
 327 
 328     check(p1.lessThanOrEqual(p2), "lessThanOrEqual 5");
 329     check(p1.lessThanOrEqual(n1), "lessThanOrEqual 6");
 330     check(p1.lessThanOrEqual(n2), "lessThanOrEqual 7");
 331     check(p2.lessThanOrEqual(n1), "lessThanOrEqual 8");
 332     check(p2.lessThanOrEqual(n2), "lessThanOrEqual 9");
 333     check(n1.lessThanOrEqual(n2), "lessThanOrEqual 10");
 334 
 335     // lessThanOrEqual negative tests
 336     check(!p2.lessThanOrEqual(p1), "lessThanOrEqual 11");
 337     check(!n1.lessThanOrEqual(p1), "lessThanOrEqual 12");
 338     check(!n2.lessThanOrEqual(p1), "lessThanOrEqual 13");
 339     check(!n1.lessThanOrEqual(p2), "lessThanOrEqual 14");
 340     check(!n2.lessThanOrEqual(p2), "lessThanOrEqual 15");
 341     check(!n2.lessThanOrEqual(n1), "lessThanOrEqual 16");
 342 
 343     // greaterThan positive tests
 344     check(n2.greaterThan(p1), "greaterThan 1");
 345     check(n2.greaterThan(p2), "greaterThan 2");
 346     check(n2.greaterThan(n1), "greaterThan 3");
 347     check(n1.greaterThan(p1), "greaterThan 4");
 348     check(n1.greaterThan(p2), "greaterThan 5");
 349     check(p2.greaterThan(p1), "greaterThan 6");
 350 
 351     // greaterThan negative tests
 352     check(!p1.greaterThan(p1), "greaterThan 7");
 353     check(!p2.greaterThan(p2), "greaterThan 8");
 354     check(!n1.greaterThan(n1), "greaterThan 9");
 355     check(!n2.greaterThan(n2), "greaterThan 10");
 356 
 357     check(!p1.greaterThan(n2), "greaterThan 11");
 358     check(!p2.greaterThan(n2), "greaterThan 12");
 359     check(!n1.greaterThan(n2), "greaterThan 13");
 360     check(!p1.greaterThan(n1), "greaterThan 14");
 361     check(!p2.greaterThan(n1), "greaterThan 15");
 362     check(!p1.greaterThan(p2), "greaterThan 16");
 363     
 364     // greaterThanOrEqual positive tests
 365     check(p1.greaterThanOrEqual(p1), "greaterThanOrEqual 1");
 366     check(p2.greaterThanOrEqual(p2), "greaterThanOrEqual 2");
 367     check(n1.greaterThanOrEqual(n1), "greaterThanOrEqual 3");
 368     check(n2.greaterThanOrEqual(n2), "greaterThanOrEqual 4");
 369     
 370     check(n2.greaterThanOrEqual(p1), "greaterThanOrEqual 5");
 371     check(n2.greaterThanOrEqual(p2), "greaterThanOrEqual 6");
 372     check(n2.greaterThanOrEqual(n1), "greaterThanOrEqual 7");
 373     check(n1.greaterThanOrEqual(p1), "greaterThanOrEqual 8");
 374     check(n1.greaterThanOrEqual(p2), "greaterThanOrEqual 9");
 375     check(p2.greaterThanOrEqual(p1), "greaterThanOrEqual 10");
 376 
 377     // greaterThanOrEqual negative tests
 378     check(!p1.greaterThanOrEqual(n2), "greaterThanOrEqual 11");
 379     check(!p2.greaterThanOrEqual(n2), "greaterThanOrEqual 12");
 380     check(!n1.greaterThanOrEqual(n2), "greaterThanOrEqual 13");
 381     check(!p1.greaterThanOrEqual(n1), "greaterThanOrEqual 14");
 382     check(!p2.greaterThanOrEqual(n1), "greaterThanOrEqual 15");
 383     check(!p1.greaterThanOrEqual(p2), "greaterThanOrEqual 16");
 384 
 385     System.err.println("DbxAddress: all tests passed successfully.");
 386   }
 387 }