Hardcode actual conversions to read the maps file data

This commit is contained in:
Benny Baumann 2020-11-17 18:04:30 +01:00 committed by BenBE
parent 721d9112d9
commit cceab5f803
1 changed files with 49 additions and 8 deletions

View File

@ -514,22 +514,62 @@ static uint64_t LinuxProcessList_calcLibSize(const char* dirname, const char* na
while (fgets(buffer, sizeof(buffer), mapsfile)) { while (fgets(buffer, sizeof(buffer), mapsfile)) {
uint64_t map_start; uint64_t map_start;
uint64_t map_end; uint64_t map_end;
char map_perm[16]; char map_perm[5];
uint32_t map_dummy;
int map_devmaj; int map_devmaj;
int map_devmin; int map_devmin;
uint64_t map_inode; uint64_t map_inode;
if (7 != sscanf(buffer, "%"PRIx64"-%"PRIx64" %4s %"PRIx32" %d:%d %" PRIu64, // Short circuit test: Look for a slash
&map_start, &map_end, map_perm, &map_dummy, if (!strchr(buffer, '/'))
&map_devmaj, &map_devmin, &map_inode)) continue;
// Parse format: "%Lx-%Lx %4s %x %2x:%2x %Ld"
char *readptr = buffer;
errno = 0;
map_start = strtoull(readptr, &readptr, 16);
if (errno || !readptr || '-' != *readptr++)
continue;
errno = 0;
map_end = strtoull(readptr, &readptr, 16);
if (errno || !readptr || ' ' != *readptr++)
continue;
memcpy(map_perm, readptr, 4);
map_perm[4] = '\0';
readptr += 4;
if (' ' != *readptr++)
continue;
errno = 0;
strtoul(readptr, &readptr, 16);
if (errno || !readptr || ' ' != *readptr++)
continue;
errno = 0;
map_devmaj = strtol(readptr, &readptr, 16);
if (errno || !readptr || ':' != *readptr++)
continue;
errno = 0;
map_devmin = strtol(readptr, &readptr, 16);
if (errno || !readptr || ' ' != *readptr++)
continue;
//Minor shortcut: Once we know there's no file for this region, we skip
if (!map_devmaj && !map_devmin)
continue;
errno = 0;
map_inode = strtoull(readptr, &readptr, 10);
if (errno || !readptr) // Don't check next character here, because that might be undefined
continue; continue;
if (!map_inode) if (!map_inode)
continue; continue;
LibraryData * libdata; LibraryData* libdata = Hashtable_get(ht, map_inode);
libdata = Hashtable_get(ht, map_inode);
if (!libdata) { if (!libdata) {
libdata = xCalloc(1, sizeof(LibraryData)); libdata = xCalloc(1, sizeof(LibraryData));
Hashtable_put(ht, map_inode, libdata); Hashtable_put(ht, map_inode, libdata);
@ -539,12 +579,13 @@ static uint64_t LinuxProcessList_calcLibSize(const char* dirname, const char* na
libdata->exec |= 'x' == map_perm[2]; libdata->exec |= 'x' == map_perm[2];
} }
fclose(mapsfile);
uint64_t total_size = 0; uint64_t total_size = 0;
Hashtable_foreach(ht, LinuxProcessList_calcLibSize_helper, &total_size); Hashtable_foreach(ht, LinuxProcessList_calcLibSize_helper, &total_size);
Hashtable_delete(ht); Hashtable_delete(ht);
fclose(mapsfile);
return total_size / CRT_pageSize; return total_size / CRT_pageSize;
} }