Commit a9e5f535 authored by Teng Qin's avatar Teng Qin

Improve bcc_procutils_each_module logic

Parse everything with one `fscanf` instead of using an extra `fgets`
parent 7c9d8c93
...@@ -87,43 +87,35 @@ int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback, ...@@ -87,43 +87,35 @@ int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback,
void *payload) { void *payload) {
char procmap_filename[128]; char procmap_filename[128];
FILE *procmap; FILE *procmap;
int ret;
snprintf(procmap_filename, sizeof(procmap_filename), "/proc/%ld/maps", snprintf(procmap_filename, sizeof(procmap_filename), "/proc/%ld/maps",
(long)pid); (long)pid);
procmap = fopen(procmap_filename, "r"); procmap = fopen(procmap_filename, "r");
if (!procmap) if (!procmap)
return -1; return -1;
do { char buf[PATH_MAX + 1], perm[5], dev[8];
char endline[4096]; char *name;
char perm[8], dev[8]; uint64_t begin, end, inode;
long long begin, end, offset, inode; unsigned long long offset;
while (true) {
ret = fscanf(procmap, "%llx-%llx %s %llx %s %lld", &begin, &end, perm, buf[0] = '\0';
&offset, dev, &inode); // From fs/proc/task_mmu.c:show_map_vma
if (fscanf(procmap, "%lx-%lx %s %llx %s %lu%[^\n]", &begin, &end, perm,
if (!fgets(endline, sizeof(endline), procmap)) &offset, dev, &inode, buf) != 7)
break; break;
if (ret == 6) { if (perm[2] != 'x')
char *mapname = endline; continue;
char *newline = strchr(endline, '\n');
if (newline)
newline[0] = '\0';
while (isspace(mapname[0])) name = buf;
mapname++; while (isspace(*name))
name++;
if (!bcc_mapping_is_file_backed(name))
continue;
if (strchr(perm, 'x') && bcc_mapping_is_file_backed(mapname)) { if (callback(name, begin, end, (uint64_t)offset, true, payload) < 0)
if (callback(mapname, (uint64_t)begin, (uint64_t)end, (uint64_t)offset, break;
true, payload) < 0) }
break;
}
}
} while (ret && ret != EOF);
fclose(procmap); fclose(procmap);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment