2 // Compile this with g++ -O6 -o CoreMemWalker CoreMemWalker.cpp -static
12 // ======================================================================
14 struct SystemAllocation
23 // Block is 16 for debug build, 12 for release build (release build doesn't have array)
24 unsigned int prev
, next
;
32 // AllocatedBlock starts here
33 unsigned int owner
[64];
41 // ======================================================================
44 AddressOffset ao
[1024];
45 char const *coreMem
= 0;
46 unsigned int coreLen
= 0;
47 const unsigned int MAX_MAP_SIZE
= 2000000000;
49 // ======================================================================
51 bool mapCore(char const *coreName
)
53 int fd
= open(coreName
, O_RDONLY
);
59 if (coreLen
> MAX_MAP_SIZE
)
60 coreLen
= MAX_MAP_SIZE
;
61 coreMem
= reinterpret_cast<char const *>(mmap(0, coreLen
, PROT_READ
, MAP_PRIVATE
|MAP_NORESERVE
, fd
, 0));
64 return coreMem
&& coreMem
!= ((char const *)MAP_FAILED
) ? true : false;
67 // ----------------------------------------------------------------------
72 munmap(reinterpret_cast<void *>(const_cast<char *>(coreMem
)), coreLen
);
75 // ----------------------------------------------------------------------
77 bool loadMemoryMap(char const *mapName
)
79 FILE *fp
= fopen(mapName
, "r");
82 unsigned int a
, c
, d
, e
, f
;
84 while (fscanf(fp
, "%d %s %x %x %x %x %s\n", &a
, b
, &c
, &d
, &e
, &f
, g
) == 7)
97 // ----------------------------------------------------------------------
99 char const *getMem(unsigned int addr
)
101 for (int i
= 0; i
< ao_c
; ++i
)
102 if (addr
>= ao
[i
].s
&& addr
< ao
[i
].e
)
103 return coreMem
+addr
+ao
[i
].o
;
107 // ----------------------------------------------------------------------
109 int main(int argc
, char **argv
)
114 "Usage: CoreMemWalker coreName mapName firstSystemAllocationAddr ownerCount\n"
115 " This is only meant to be used from the coreMemReport.sh script.\n");
118 fprintf(stderr
, "sizeof(AllocatedBlock)=%d, sizeof(AllocatedBlock) - owner=%d\n", sizeof(AllocatedBlock
), sizeof(AllocatedBlock
) - sizeof(a
.owner
));
119 fprintf(stderr
, "&AllocatedBlock=%p, sizeof=%d\n", &a
, sizeof(a
));
120 fprintf(stderr
, "&AllocatedBlock.prev=%p, sizeof=%d\n", &a
.prev
, sizeof(a
.prev
));
121 fprintf(stderr
, "&AllocatedBlock.next=%p, sizeof=%d\n", &a
.next
, sizeof(a
.next
));
122 fprintf(stderr
, "&AllocatedBlock.owner=%p, sizeof=%d\n", &a
.owner
, sizeof(a
.owner
));
127 char const *coreName
= argv
[1];
128 char const *mapName
= argv
[2];
129 unsigned int firstSystemAllocationAddr
= atoi(argv
[3]);
130 unsigned int ownerCount
= atoi(argv
[4]);
133 if ( mapCore(coreName
)
134 && loadMemoryMap(mapName
))
136 unsigned int systemAllocationAddr
= firstSystemAllocationAddr
;
137 while (systemAllocationAddr
)
139 SystemAllocation
const * const systemAllocation
= reinterpret_cast<SystemAllocation
const *>(getMem(systemAllocationAddr
));
140 AllocatedBlock
const * const firstBlockForSA
= reinterpret_cast<AllocatedBlock
const *>(reinterpret_cast<char const *>(systemAllocation
)+16);
141 AllocatedBlock
const * const lastBlockForSA
= reinterpret_cast<AllocatedBlock
const *>(reinterpret_cast<char const *>(systemAllocation
)+systemAllocation
->size
-16);
142 unsigned int blockAddr
= firstBlockForSA
->next
;
143 AllocatedBlock
const *block
= reinterpret_cast<AllocatedBlock
const *>(getMem(blockAddr
));
144 while (block
!= lastBlockForSA
)
148 printf("0x%x %d", blockAddr
, block
->size
);
149 for (int i
= 0; i
< ownerCount
; ++i
)
150 printf(" 0x%x", block
->owner
[i
]);
154 // printf("%d\n", count);
156 // if (count > 2153024)
159 if (blockAddr
== block
->next
)
161 blockAddr
= block
->next
;
162 block
= reinterpret_cast<AllocatedBlock
const *>(getMem(blockAddr
));
165 // if (count > 2153024)
168 if (systemAllocationAddr
== systemAllocation
->next
)
170 systemAllocationAddr
= systemAllocation
->next
;