Skip to content

Commit

Permalink
Support loading elf notes from memfd backed map (iovisor#2314)
Browse files Browse the repository at this point in the history
* Support loading elf notes from file backed on memfd

* Use const for arguments to _procutils_memfd_path

* Cleanups for procuptils_memfd_path
  • Loading branch information
dalehamel authored and yonghong-song committed Apr 16, 2019
1 parent 6b19790 commit 47618fe
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/cc/bcc_proc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
* limitations under the License.
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <ctype.h>
#include <dirent.h>
#include <fcntl.h>
#include <limits.h>
#include <math.h>
Expand All @@ -26,6 +24,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include "bcc_perf_map.h"
Expand Down Expand Up @@ -82,6 +83,42 @@ int bcc_mapping_is_file_backed(const char *mapname) {
STARTS_WITH(mapname, "[vsyscall]"));
}

/*
Finds a file descriptor for a given inode if it's a memory-backed fd.
*/
static char *_procutils_memfd_path(const int pid, const uint64_t inum) {
char path_buffer[PATH_MAX + 1];
char *path = NULL;
char *dirstr;
DIR *dirstream;
struct stat sb;
struct dirent *dent;

snprintf(path_buffer, (PATH_MAX + 1), "/proc/%d/fd", pid);
dirstr = malloc(strlen(path_buffer) + 1);
strcpy(dirstr, path_buffer);
dirstream = opendir(dirstr);

if (dirstream == NULL)
return NULL;

while (path == NULL && (dent = readdir(dirstream)) != NULL) {
snprintf(path_buffer, (PATH_MAX + 1), "%s/%s", dirstr, dent->d_name);
if (stat(path_buffer, &sb) == -1)
continue;

if (sb.st_ino == inum) {
char *pid_fd_path = malloc(strlen(path_buffer) + 1);
strcpy(pid_fd_path, path_buffer);
path = pid_fd_path;
}
}
closedir(dirstream);
free(dirstr);

return path;
}

int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback,
void *payload) {
char procmap_filename[128];
Expand Down Expand Up @@ -112,6 +149,15 @@ int bcc_procutils_each_module(int pid, bcc_procutils_modulecb callback,
if (!bcc_mapping_is_file_backed(name))
continue;

if (strstr(name, "/memfd:")) {
char *memfd_name = _procutils_memfd_path(pid, inode);
if (memfd_name != NULL) {
strcpy(buf, memfd_name);
free(memfd_name);
name = buf;
}
}

if (callback(name, begin, end, (uint64_t)offset, true, payload) < 0)
break;
}
Expand Down

0 comments on commit 47618fe

Please sign in to comment.