Populated Bestine Capitol Building with missing NPCs. Also spawns several other missi...
[swg-src.git] / tools / CoreMemWalker.cpp
blob42cf8cd555f68fe9506d9697d4bbccb60da40b2e
2 // Compile this with g++ -O6 -o CoreMemWalker CoreMemWalker.cpp -static
4 #include <sys/fcntl.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
8 #include <unistd.h>
9 #include <cstdio>
10 #include <cstdlib>
12 // ======================================================================
14 struct SystemAllocation
16 int size;
17 unsigned int next;
18 int pad1, pad2;
21 struct AllocatedBlock
23 // Block is 16 for debug build, 12 for release build (release build doesn't have array)
24 unsigned int prev, next;
25 bool free:1;
26 #ifdef _DEBUG
27 bool array:1;
28 #endif
29 bool leakTest:1;
30 unsigned int size:30;
32 // AllocatedBlock starts here
33 unsigned int owner[64];
36 struct AddressOffset
38 unsigned int s, e, o;
41 // ======================================================================
43 int ao_c = 0;
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);
54 if (fd != -1)
56 struct stat s;
57 fstat(fd, &s);
58 coreLen = s.st_size;
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));
62 close(fd);
64 return coreMem && coreMem != ((char const *)MAP_FAILED) ? true : false;
67 // ----------------------------------------------------------------------
69 void unmapCore()
71 if (coreMem)
72 munmap(reinterpret_cast<void *>(const_cast<char *>(coreMem)), coreLen);
75 // ----------------------------------------------------------------------
77 bool loadMemoryMap(char const *mapName)
79 FILE *fp = fopen(mapName, "r");
80 if (fp)
82 unsigned int a, c, d, e, f;
83 char b[512], g[512];
84 while (fscanf(fp, "%d %s %x %x %x %x %s\n", &a, b, &c, &d, &e, &f, g) == 7)
86 ao[ao_c].s = d;
87 ao[ao_c].e = d+c;
88 ao[ao_c].o = f-d;
89 ++ao_c;
91 fclose(fp);
92 return true;
94 return false;
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;
104 return coreMem;
107 // ----------------------------------------------------------------------
109 int main(int argc, char **argv)
111 if (argc != 5)
113 fprintf(stderr,
114 "Usage: CoreMemWalker coreName mapName firstSystemAllocationAddr ownerCount\n"
115 " This is only meant to be used from the coreMemReport.sh script.\n");
117 AllocatedBlock a;
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));
124 return 1;
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]);
131 // int count = 0;
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)
146 if (!block->free)
148 printf("0x%x %d", blockAddr, block->size);
149 for (int i = 0; i < ownerCount; ++i)
150 printf(" 0x%x", block->owner[i]);
151 printf("\n");
153 // ++count;
154 // printf("%d\n", count);
156 // if (count > 2153024)
157 // break;
159 if (blockAddr == block->next)
160 break;
161 blockAddr = block->next;
162 block = reinterpret_cast<AllocatedBlock const *>(getMem(blockAddr));
165 // if (count > 2153024)
166 // break;
168 if (systemAllocationAddr == systemAllocation->next)
169 break;
170 systemAllocationAddr = systemAllocation->next;
172 unmapCore();
175 return 0;