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