begin porting all lists to mlist

This commit is contained in:
mykola2312 2024-09-03 02:52:48 +03:00
parent 27fc37d6cf
commit aa9c426f66
4 changed files with 32 additions and 29 deletions

View file

@ -27,12 +27,9 @@ int main(int argc, char** argv)
{
// map test
{
procstat_map_t* maps;
size_t map_count;
procstat_maps_t* maps = procstat_parse_maps(1);
procstat_parse_maps(1, &maps, &map_count);
procstat_free_maps(maps, map_count);
procstat_free_maps(maps);
return 0;
}

View file

@ -707,7 +707,9 @@ typedef void (*__mlist_callback_t)(__mlist_proto_t*);
#define MLIST_DECLARE(list_type, item_type, item_name, count_name, item_callback) \
static const unsigned __MLIST_ITEM_SIZE(list_type) = sizeof(item_type); \
typedef void (*__MLIST_ITEM_CALLBACK_TYPE(list_type))(item_type*); \
__MLIST_ITEM_CALLBACK_TYPE(list_type) __MLIST_ITEM_CALLBACK(list_type) = item_callback; \
static const __MLIST_ITEM_CALLBACK_TYPE(list_type) \
__attribute__((unused)) __MLIST_ITEM_CALLBACK(list_type) \
= item_callback; \
typedef struct { \
item_type* item_name; \
unsigned count; \

View file

@ -226,14 +226,19 @@ int procstat_find_active(procstat_status_t* list, size_t count, procstat_status_
return 1;
}
int procstat_parse_maps(pid_t pid, procstat_map_t** maps, size_t* count)
void procstat_free_map(procstat_map_t* map)
{
if (map->path) free(map->path);
}
procstat_maps_t* procstat_parse_maps(pid_t pid)
{
// open proc maps
char mapsPath[256] = {0};
snprintf(mapsPath, sizeof(mapsPath), "/proc/%d/maps", pid);
int fd = open(mapsPath, O_RDONLY);
if (fd < 0) return -1;
if (fd < 0) return NULL;
// now, block by block read contents
const unsigned blockSize = 512;
@ -249,7 +254,7 @@ int procstat_parse_maps(pid_t pid, procstat_map_t** maps, size_t* count)
// error
free(buffer);
close(fd);
return -1;
return NULL;
}
else if (rd < blockSize)
{
@ -271,8 +276,8 @@ int procstat_parse_maps(pid_t pid, procstat_map_t** maps, size_t* count)
// we got our maps buffer, now we can close file
close(fd);
procstat_map_t* _maps = NULL;
size_t _count = 0;
procstat_maps_t* maps;
MLIST_ALLOC(procstat_maps_t, maps);
char* lineptr = NULL, *line = strtok_r(buffer, "\n", &lineptr);
while (line != NULL)
@ -287,10 +292,9 @@ int procstat_parse_maps(pid_t pid, procstat_map_t** maps, size_t* count)
const char* pathname = strtok_r(NULL, " ", &fieldptr);
// allocate new map entry
if (_maps) _maps = (procstat_map_t*)realloc(_maps, ++_count * sizeof(procstat_map_t));
else _maps = (procstat_map_t*)malloc(++_count * sizeof(procstat_map_t));
MLIST_ADD(procstat_maps_t, maps);
procstat_map_t* map = &_maps[_count - 1];
procstat_map_t* map = (procstat_map_t*)MLIST_LAST(procstat_maps_t, maps);
memset(map, '\0', sizeof(procstat_map_t));
// v_start and v_end
sscanf(v_range, "%"SCNx64 "-" "%"SCNx64, &map->v_start, &map->v_end);
@ -335,15 +339,10 @@ int procstat_parse_maps(pid_t pid, procstat_map_t** maps, size_t* count)
free(buffer);
*maps = _maps;
*count = _count;
return 0;
return maps;
}
void procstat_free_maps(procstat_map_t* maps, size_t count)
void procstat_free_maps(procstat_maps_t* maps)
{
for (unsigned i = 0; i < count; i++)
if (maps[i].path) free(maps[i].path);
free(maps);
MLIST_FREE(procstat_maps_t, maps);
}

View file

@ -73,11 +73,16 @@ typedef struct {
char* path; // don't forget to free
} procstat_map_t;
// parse process file mappings. return 0 on success and -1 on error
int procstat_parse_maps(pid_t pid, procstat_map_t** maps, size_t* count);
void procstat_free_map(procstat_map_t* map);
// will take care of freeing everything related to procstat_map_t lists
void procstat_free_maps(procstat_map_t* maps, size_t count);
MLIST_DECLARE(procstat_maps_t, procstat_map_t, maps, count, procstat_free_map);
// parse process file mappings. return 0 on success and -1 on error
procstat_maps_t* procstat_parse_maps(pid_t pid);
// free any memory allocated by procstat map,
// or you can just call MLIST_FREE
void procstat_free_maps(procstat_maps_t* maps);
// list of modules that are mapped into process memory
typedef struct {
@ -90,13 +95,13 @@ typedef struct {
unsigned map_count;
} procstat_module_t;
MLIST_DECLARE(procstat_modules_t, procstat_module_t, modules, module_count);
//MLIST_DECLARE(procstat_modules_t, procstat_module_t, modules, module_count);
// analyze file mappings and parse them into modules
// that way, we can figure where libc.so.6 loaded in memory
procstat_modules_t* procstat_parse_modules(procstat_map_t* maps, size_t count);
//procstat_modules_t* procstat_parse_modules(procstat_map_t* maps, size_t count);
// free any allocated memory by procstat_parse_modules
void procstat_free_modules(procstat_modules_t* modules);
//void procstat_free_modules(procstat_modules_t* modules);
#endif