Debugger.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #include "Debugger.h"
  2. #include "DebugManager.h"
  3. #include "Compiler/BfSystem.h"
  4. USING_NS_BF;
  5. DbgModuleMemoryCache::DbgModuleMemoryCache(uintptr addr, int size)
  6. {
  7. mAddr = addr;
  8. mSize = size;
  9. mBlockSize = 4096;
  10. mBlockCount = size / mBlockSize;
  11. BF_ASSERT((size & (mBlockSize - 1)) == 0);
  12. //mBlocks = (uint8**)dwarf->mAlloc.AllocBytes(dwarf->mOrigImageBlockCount * sizeof(uint8*), alignof(uint8*));
  13. mBlocks = new uint8*[mBlockCount];
  14. memset(mBlocks, 0, mBlockCount * sizeof(uint8*));
  15. mFlags = new DbgMemoryFlags[mBlockCount];
  16. memset(mFlags, 0, mBlockCount * sizeof(DbgMemoryFlags));
  17. mOwns = true;
  18. }
  19. // DbgModuleMemoryCache::DbgModuleMemoryCache(uintptr addr, uint8* data, int size, bool makeCopy)
  20. // {
  21. // mAddr = addr;
  22. // mBlockSize = size;
  23. // mBlocks = new uint8*[1];
  24. // mFlags = new DbgMemoryFlags[1];
  25. // mSize = size;
  26. //
  27. // if (makeCopy)
  28. // {
  29. // uint8* dataCopy = new uint8[size];
  30. // if (data != NULL)
  31. // memcpy(dataCopy, data, size);
  32. // else
  33. // memset(dataCopy, 0, size);
  34. // mBlocks[0] = dataCopy;
  35. // }
  36. // else
  37. // {
  38. // mBlocks[0] = data;
  39. // }
  40. //
  41. // mOwns = makeCopy;
  42. // mBlockCount = 1;
  43. // }
  44. DbgModuleMemoryCache::~DbgModuleMemoryCache()
  45. {
  46. if (mOwns)
  47. {
  48. for (int i = 0; i < mBlockCount; i++)
  49. delete mBlocks[i];
  50. }
  51. delete [] mBlocks;
  52. delete [] mFlags;
  53. }
  54. DbgMemoryFlags DbgModuleMemoryCache::Read(uintptr addr, uint8* data, int size)
  55. {
  56. BF_ASSERT(mAddr != 0);
  57. int sizeLeft = size;
  58. DbgMemoryFlags flags = DbgMemoryFlags_None;
  59. if ((addr < mAddr) || (addr > mAddr + mSize))
  60. {
  61. gDebugger->ReadMemory(addr, size, data);
  62. flags = gDebugger->GetMemoryFlags(addr);
  63. BfLogDbg("Got memory flags %p = %d\n", addr, flags);
  64. return flags;
  65. }
  66. int relAddr = (int)(addr - mAddr);
  67. int blockIdx = relAddr / mBlockSize;
  68. int curOffset = relAddr % mBlockSize;
  69. while (sizeLeft > 0)
  70. {
  71. uint8* block = mBlocks[blockIdx];
  72. if (block == NULL)
  73. {
  74. block = new uint8[mBlockSize];
  75. gDebugger->ReadMemory(mAddr + blockIdx * mBlockSize, mBlockSize, block);
  76. mBlocks[blockIdx] = block;
  77. mFlags[blockIdx] = gDebugger->GetMemoryFlags(mAddr + blockIdx * mBlockSize);
  78. BfLogDbg("Memory flags for %p = %d\n", mAddr + blockIdx * mBlockSize, mFlags[blockIdx]);
  79. }
  80. flags = mFlags[blockIdx];
  81. int readSize = BF_MIN(sizeLeft, mBlockSize - curOffset);
  82. if (data != NULL)
  83. {
  84. memcpy(data, block + curOffset, readSize);
  85. data += readSize;
  86. }
  87. sizeLeft -= readSize;
  88. curOffset = 0;
  89. blockIdx++;
  90. }
  91. return flags;
  92. }
  93. void DbgModuleMemoryCache::ReportMemory(MemReporter * memReporter)
  94. {
  95. int totalMemory = 0;
  96. if (mOwns)
  97. {
  98. for (int i = 0; i < mBlockCount; i++)
  99. if (mBlocks[i] != NULL)
  100. totalMemory += mBlockSize;
  101. }
  102. memReporter->Add(totalMemory);
  103. }