1 11 12 package org.eclipse.debug.internal.ui.elements.adapters; 13 14 import java.math.BigInteger ; 15 import java.util.ArrayList ; 16 import java.util.Hashtable ; 17 import java.util.Vector ; 18 19 import org.eclipse.core.runtime.CoreException; 20 import org.eclipse.debug.core.DebugException; 21 import org.eclipse.debug.core.model.IDebugTarget; 22 import org.eclipse.debug.core.model.IMemoryBlock; 23 import org.eclipse.debug.core.model.IMemoryBlockExtension; 24 import org.eclipse.debug.core.model.MemoryByte; 25 import org.eclipse.debug.internal.ui.DebugUIMessages; 26 import org.eclipse.debug.internal.ui.DebugUIPlugin; 27 import org.eclipse.debug.internal.ui.memory.provisional.AbstractAsyncTableRendering; 28 import org.eclipse.debug.internal.ui.memory.provisional.MemoryViewPresentationContext; 29 import org.eclipse.debug.internal.ui.viewers.model.provisional.IPresentationContext; 30 import org.eclipse.debug.internal.ui.viewers.provisional.AsynchronousContentAdapter; 31 import org.eclipse.debug.internal.ui.views.memory.MemoryViewUtil; 32 import org.eclipse.debug.internal.ui.views.memory.renderings.MemorySegment; 33 import org.eclipse.debug.internal.ui.views.memory.renderings.TableRenderingContentDescriptor; 34 import org.eclipse.debug.ui.memory.IMemoryRendering; 35 36 public class MemoryBlockContentAdapter extends AsynchronousContentAdapter { 37 38 protected Hashtable contentCache; 42 43 public MemoryBlockContentAdapter() 44 { 45 contentCache = new Hashtable (); 46 } 47 48 protected Object [] getChildren(Object parent, IPresentationContext context) 49 throws CoreException { 50 51 if (!(parent instanceof IMemoryBlock)) 52 return new Object [0]; 53 54 if (!(context instanceof MemoryViewPresentationContext)) 55 return new Object [0]; 56 57 MemoryViewPresentationContext memoryViewContext = (MemoryViewPresentationContext)context; 58 IMemoryRendering rendering = memoryViewContext.getRendering(); 59 60 if (!(rendering instanceof AbstractAsyncTableRendering)) 61 return new Object [0]; 62 63 try 64 { 65 return getMemoryFromMemoryBlock(memoryViewContext); 66 } catch (DebugException e) { 67 throw e; 68 } 69 70 } 71 72 protected boolean hasChildren(Object element, IPresentationContext context) 73 throws CoreException { 74 75 if (context instanceof MemoryViewPresentationContext) 76 { 77 if (((MemoryViewPresentationContext)context).getRendering() != null) 78 return true; 79 } 80 81 return false; 82 } 83 84 protected boolean supportsPartId(String id) { 85 return true; 86 } 87 88 89 private Object [] getMemoryFromMemoryBlock(MemoryViewPresentationContext context) throws DebugException { 90 IMemoryBlock memoryBlock = context.getRendering().getMemoryBlock(); 91 if (memoryBlock instanceof IMemoryBlockExtension) 92 { 93 return loadContentForExtendedMemoryBlock(context); 94 } 95 96 return loadContentForSimpleMemoryBlock(context); 97 98 } 99 100 103 public Object [] loadContentForSimpleMemoryBlock(MemoryViewPresentationContext context) throws DebugException { 104 AbstractAsyncTableRendering rendering = getTableRendering(context); 105 if (rendering != null) 106 { 107 IMemoryBlock memoryBlock = rendering.getMemoryBlock(); 108 long startAddress = memoryBlock.getStartAddress(); 109 BigInteger address = BigInteger.valueOf(startAddress); 110 long length = memoryBlock.getLength(); 111 long numLines = length / rendering.getBytesPerLine(); 112 return getMemoryToFitTable(address, numLines, context); 113 } 114 return EMPTY; 115 } 116 117 120 public Object [] loadContentForExtendedMemoryBlock(MemoryViewPresentationContext context) throws DebugException { 121 122 AbstractAsyncTableRendering rendering = getTableRendering(context); 123 if (rendering != null) 124 { 125 TableRenderingContentDescriptor descriptor = (TableRenderingContentDescriptor)rendering.getAdapter(TableRenderingContentDescriptor.class); 126 127 if (descriptor == null) 128 return new Object [0]; 129 130 if (descriptor.getNumLines() <= 0) 131 return new Object [0]; 132 133 BigInteger loadAddress = descriptor.getLoadAddress(); 135 if (loadAddress == null) 136 { 137 loadAddress = new BigInteger ("0"); } 139 140 BigInteger mbStart = descriptor.getStartAddress(); 141 BigInteger mbEnd = descriptor.getEndAddress(); 142 143 if (loadAddress.compareTo(mbStart) < 0 || loadAddress.compareTo(mbEnd) > 0) 145 { 146 loadAddress = ((IMemoryBlockExtension)descriptor.getMemoryBlock()).getBigBaseAddress(); 148 descriptor.setLoadAddress(loadAddress); 149 } 150 151 if (loadAddress.compareTo(mbStart) < 0 || loadAddress.compareTo(mbEnd) > 0) 153 { 154 throw new DebugException(DebugUIPlugin.newErrorStatus(DebugUIMessages.TableRenderingContentProvider_0 + loadAddress.toString(16), null)); 155 } 156 157 int addressableUnitsPerLine = rendering.getAddressableUnitPerLine(); 158 BigInteger bufferStart = loadAddress.subtract(BigInteger.valueOf(descriptor.getPreBuffer()*addressableUnitsPerLine)); 159 BigInteger bufferEnd = loadAddress.add(BigInteger.valueOf(descriptor.getPostBuffer()*addressableUnitsPerLine)); 160 bufferEnd = bufferEnd.add(BigInteger.valueOf(descriptor.getNumLines()*addressableUnitsPerLine)); 161 162 if (descriptor.isDynamicLoad()) 165 { 166 if (bufferStart.compareTo(mbStart) < 0) 167 bufferStart = mbStart; 168 169 if (bufferEnd.compareTo(mbEnd) > 0) 170 { 171 bufferEnd = mbEnd; 172 173 int numLines = bufferEnd.subtract(bufferStart).divide(BigInteger.valueOf(addressableUnitsPerLine)).intValue(); 174 if (numLines < descriptor.getNumLines()) 175 { 176 bufferStart = bufferEnd.subtract(BigInteger.valueOf(descriptor.getNumLines()*addressableUnitsPerLine)); 178 bufferStart = bufferStart.subtract(BigInteger.valueOf(descriptor.getPreBuffer()*addressableUnitsPerLine)); 179 } 180 } 181 182 if (bufferEnd.compareTo(bufferStart) <= 0) 184 throw new DebugException(DebugUIPlugin.newErrorStatus(DebugUIMessages.TableRenderingContentProvider_1, null)); 185 186 int numLines = bufferEnd.subtract(bufferStart).divide(BigInteger.valueOf(addressableUnitsPerLine)).intValue()+1; 187 return getMemoryToFitTable(bufferStart, numLines,context); 189 } 190 else 191 { 192 if (bufferStart.compareTo(mbStart) < 0) 193 bufferStart = mbStart; 194 195 if (bufferEnd.compareTo(mbEnd) > 0) 196 { 197 bufferStart = mbEnd.subtract(BigInteger.valueOf((descriptor.getNumLines()-1)*addressableUnitsPerLine)); 198 } 199 200 if (bufferEnd.compareTo(bufferStart) <= 0) 202 throw new DebugException(DebugUIPlugin.newErrorStatus(DebugUIMessages.TableRenderingContentProvider_2, null)); 203 204 int numLines = descriptor.getNumLines(); 205 return getMemoryToFitTable(bufferStart, numLines, context); 207 } 208 } 209 return EMPTY; 210 } 211 212 219 public Object [] getMemoryToFitTable(BigInteger startAddress, long numberOfLines, MemoryViewPresentationContext context) throws DebugException 220 { 221 AbstractAsyncTableRendering tableRendering = getTableRendering(context); 222 if (tableRendering == null) 223 { 224 DebugException e = new DebugException(DebugUIPlugin.newErrorStatus(DebugUIMessages.MemoryViewContentProvider_Unable_to_retrieve_content, null)); 225 throw e; 226 } 227 228 TableRenderingContentDescriptor descriptor = (TableRenderingContentDescriptor)tableRendering.getAdapter(TableRenderingContentDescriptor.class); 229 if(descriptor == null) 230 return new Object [0]; 231 232 IDebugTarget target = descriptor.getMemoryBlock().getDebugTarget(); 235 236 if (target.isDisconnected() || target.isTerminated()) 237 return new Object [0]; 238 239 boolean error = false; 240 DebugException dbgEvt = null; 241 242 String adjustedAddress = startAddress.toString(16); 243 244 if (descriptor.isAlignAddressToBoundary() && descriptor.getMemoryBlock() instanceof IMemoryBlockExtension) 246 { 247 startAddress = MemoryViewUtil.alignToBoundary(startAddress, tableRendering.getAddressableUnitPerLine()); 248 } 249 250 IMemoryBlockExtension extMemoryBlock = null; 251 MemoryByte[] memoryBuffer = null; 252 253 long reqNumBytes = 0; 254 try 255 { 256 257 if (descriptor.getMemoryBlock() instanceof IMemoryBlockExtension) 258 { 259 reqNumBytes = tableRendering.getBytesPerLine() * numberOfLines; 260 extMemoryBlock = (IMemoryBlockExtension) descriptor.getMemoryBlock(); 262 263 long reqNumberOfUnits = tableRendering.getAddressableUnitPerLine() * numberOfLines; 264 265 memoryBuffer = extMemoryBlock.getBytesFromAddress(startAddress, reqNumberOfUnits); 266 267 if(memoryBuffer == null) 268 { 269 DebugException e = new DebugException(DebugUIPlugin.newErrorStatus(DebugUIMessages.MemoryViewContentProvider_Unable_to_retrieve_content, null)); 270 throw e; 271 } 272 } 273 else 274 { 275 byte[] memory = descriptor.getMemoryBlock().getBytes(); 277 278 if (memory == null) 279 { 280 DebugException e = new DebugException(DebugUIPlugin.newErrorStatus(DebugUIMessages.MemoryViewContentProvider_Unable_to_retrieve_content, null)); 281 throw e; 282 } 283 284 int prefillNumBytes = 0; 285 286 if (!startAddress.toString(16).endsWith("0")) { 289 adjustedAddress = startAddress.toString(16).substring(0, adjustedAddress.length() - 1); 290 adjustedAddress += "0"; BigInteger adjustedStart = new BigInteger (adjustedAddress, 16); 292 prefillNumBytes = startAddress.subtract(adjustedStart).intValue(); 293 startAddress = adjustedStart; 294 } 295 reqNumBytes = descriptor.getMemoryBlock().getLength() + prefillNumBytes; 296 297 while (reqNumBytes % tableRendering.getBytesPerLine() != 0) 299 { 300 reqNumBytes ++; 301 } 302 303 numberOfLines = reqNumBytes / tableRendering.getBytesPerLine(); 304 305 memoryBuffer = new MemoryByte[(int)reqNumBytes]; 307 308 for (int i=0; i<prefillNumBytes; i++) 310 { 311 MemoryByte tmp = new MemoryByte(); 312 tmp.setValue((byte)0); 313 tmp.setWritable(false); 314 tmp.setReadable(false); 315 tmp.setEndianessKnown(false); 316 memoryBuffer[i] = tmp; 317 } 318 319 int j = prefillNumBytes; for (int i=0; i<memory.length; i++) 322 { 323 MemoryByte tmp = new MemoryByte(); 324 tmp.setValue(memory[i]); 325 tmp.setReadable(true); 326 tmp.setWritable(true); 327 tmp.setEndianessKnown(false); 328 memoryBuffer[j] = tmp; 329 j++; 330 } 331 332 for (int i=j; i<memoryBuffer.length; i++) 334 { 335 MemoryByte tmp = new MemoryByte(); 336 tmp.setValue((byte)0); 337 tmp.setWritable(false); 338 tmp.setReadable(false); 339 tmp.setEndianessKnown(false); 340 memoryBuffer[i] = tmp; 341 } 342 } 343 } 344 catch (DebugException e) 345 { 346 memoryBuffer = makeDummyContent(numberOfLines, tableRendering.getBytesPerLine()); 347 348 error = true; 350 dbgEvt = e; 351 } 352 catch (Throwable e) 353 { 354 memoryBuffer = makeDummyContent(numberOfLines, tableRendering.getBytesPerLine()); 356 357 error = true; 359 dbgEvt = new DebugException(DebugUIPlugin.newErrorStatus(e.getMessage(), e)); 360 } 361 362 if (memoryBuffer.length < reqNumBytes) 364 { 365 ArrayList newBuffer = new ArrayList (); 366 367 for (int i=0; i<memoryBuffer.length; i++) 368 { 369 newBuffer.add(memoryBuffer[i]); 370 } 371 372 for (int i=memoryBuffer.length; i<reqNumBytes; i++) 373 { 374 MemoryByte mb = new MemoryByte(); 375 mb.setReadable(false); 376 mb.setWritable(false); 377 mb.setEndianessKnown(false); 378 newBuffer.add(mb); 379 } 380 381 memoryBuffer = (MemoryByte[])newBuffer.toArray(new MemoryByte[newBuffer.size()]); 382 383 } 384 385 boolean manageDelta = true; 386 387 if (descriptor.getMemoryBlock() instanceof IMemoryBlockExtension) 391 { 392 manageDelta = !((IMemoryBlockExtension)descriptor.getMemoryBlock()).supportsChangeManagement(); 393 } 394 395 if (error){ 396 throw dbgEvt; 397 } 398 399 return organizeLines(numberOfLines, memoryBuffer, startAddress, manageDelta, context); 401 402 403 } 404 405 private Object [] organizeLines(long numberOfLines, MemoryByte[] memoryBuffer, BigInteger address, boolean manageDelta, MemoryViewPresentationContext context) 406 { 407 Vector lineCache = new Vector (); 408 IMemoryRendering rendering = context.getRendering(); 409 if (!(rendering instanceof AbstractAsyncTableRendering)) 410 return lineCache.toArray(); 411 412 AbstractAsyncTableRendering tableRendering = (AbstractAsyncTableRendering)rendering; 413 int addressableUnit = tableRendering.getBytesPerLine()/tableRendering.getAddressableSize(); 414 415 for (int i = 0; i < numberOfLines; i++) 416 { 417 int bytesPerLine = tableRendering.getBytesPerLine(); 418 MemoryByte[] memory = new MemoryByte[bytesPerLine]; 419 420 int k = 0; 422 for (int j = i * bytesPerLine; 424 j < i * bytesPerLine + bytesPerLine; 425 j++) 426 { 427 428 byte changeFlag = memoryBuffer[j].getFlags(); 429 if (manageDelta) 430 { 431 435 changeFlag |= MemoryByte.HISTORY_KNOWN; 436 changeFlag ^= MemoryByte.HISTORY_KNOWN; 437 438 changeFlag |= MemoryByte.CHANGED; 439 changeFlag ^= MemoryByte.CHANGED; 440 } 441 442 MemoryByte newByteObj = new MemoryByte(memoryBuffer[j].getValue(), changeFlag); 443 memory[k] = newByteObj; 444 k++; 445 } 446 447 MemorySegment newLine = new MemorySegment(address, memory, addressableUnit); 448 lineCache.add(newLine); 449 address = address.add(BigInteger.valueOf(addressableUnit)); 450 } 451 return lineCache.toArray(); 452 } 453 454 458 private MemoryByte[] makeDummyContent(long numberOfLines, int bytesPerLine) { 459 MemoryByte[] memoryBuffer; 460 463 int numBytes = (int)(bytesPerLine * numberOfLines); 464 memoryBuffer = new MemoryByte[numBytes]; 465 466 for (int i=0; i<memoryBuffer.length; i++){ 467 memoryBuffer[i] = new MemoryByte(); 468 memoryBuffer[i].setValue((byte)0); 469 memoryBuffer[i].setWritable(false); 470 memoryBuffer[i].setReadable(false); 471 memoryBuffer[i].setEndianessKnown(false); 472 } 473 return memoryBuffer; 474 } 475 476 protected AbstractAsyncTableRendering getTableRendering(MemoryViewPresentationContext context) 477 { 478 IMemoryRendering memRendering = context.getRendering(); 479 if (memRendering != null && memRendering instanceof AbstractAsyncTableRendering) 480 { 481 return (AbstractAsyncTableRendering)memRendering; 482 } 483 return null; 484 } 485 } 486 | Popular Tags |