Commit a4545db7 authored by David Carlier's avatar David Carlier Committed by Commit Bot

FreeBSD update of process library mapping listing.

Not best to rely on /proc presence basically when
the linux compatibily layer is enabled so
going through more programmatically.

Change-Id: Ida4973f9da6dec6e9caa6e419f3612ec5ef95048
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1710664Reviewed-by: 's avatarUlan Degenbaev <ulan@chromium.org>
Commit-Queue: Ulan Degenbaev <ulan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65442}
parent 83786cb4
...@@ -13,10 +13,12 @@ ...@@ -13,10 +13,12 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/ucontext.h> #include <sys/ucontext.h>
#include <sys/user.h>
#include <sys/fcntl.h> // open #include <sys/fcntl.h> // open
#include <sys/mman.h> // mmap & munmap #include <sys/mman.h> // mmap & munmap
#include <sys/stat.h> // open #include <sys/stat.h> // open
#include <sys/sysctl.h>
#include <unistd.h> // getpagesize #include <unistd.h> // getpagesize
// If you don't have execinfo.h then you need devel/libexecinfo from ports. // If you don't have execinfo.h then you need devel/libexecinfo from ports.
#include <errno.h> #include <errno.h>
...@@ -46,41 +48,47 @@ static unsigned StringToLong(char* buffer) { ...@@ -46,41 +48,47 @@ static unsigned StringToLong(char* buffer) {
std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() { std::vector<OS::SharedLibraryAddress> OS::GetSharedLibraryAddresses() {
std::vector<SharedLibraryAddress> result; std::vector<SharedLibraryAddress> result;
static const int MAP_LENGTH = 1024; int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_VMMAP, getpid()};
int fd = open("/proc/self/maps", O_RDONLY); size_t miblen = sizeof(mib) / sizeof(mib[0]);
if (fd < 0) return result; size_t buffer_size;
while (true) { if (sysctl(mib, miblen, nullptr, &buffer_size, nullptr, 0) == 0) {
char addr_buffer[11]; // Overallocate the buffer by 1/3 to account for concurrent
addr_buffer[0] = '0'; // kinfo_vmentry change. 1/3 is an arbitrary constant that
addr_buffer[1] = 'x'; // works in practice.
addr_buffer[10] = 0; buffer_size = buffer_size * 4 / 3;
ssize_t bytes_read = read(fd, addr_buffer + 2, 8); std::vector<char> buffer(buffer_size);
if (bytes_read < 8) break; int ret = sysctl(mib, miblen, buffer.data(), &buffer_size, nullptr, 0);
unsigned start = StringToLong(addr_buffer);
bytes_read = read(fd, addr_buffer + 2, 1); if (ret == 0 || (ret == -1 && errno == ENOMEM)) {
if (bytes_read < 1) break; char* start = buffer.data();
if (addr_buffer[2] != '-') break; char* end = start + buffer_size;
bytes_read = read(fd, addr_buffer + 2, 8);
if (bytes_read < 8) break; while (start < end) {
unsigned end = StringToLong(addr_buffer); struct kinfo_vmentry* map =
char buffer[MAP_LENGTH]; reinterpret_cast<struct kinfo_vmentry*>(start);
bytes_read = -1; const size_t ssize = map->kve_structsize;
do { char* path = map->kve_path;
bytes_read++;
if (bytes_read >= MAP_LENGTH - 1) break; CHECK_NE(0, ssize);
bytes_read = read(fd, buffer + bytes_read, 1);
if (bytes_read < 1) break; if ((map->kve_protection & KVME_PROT_READ) != 0 &&
} while (buffer[bytes_read] != '\n'); (map->kve_protection & KVME_PROT_EXEC) != 0 && path[0] != '\0') {
buffer[bytes_read] = 0; char* sep = strrchr(path, '/');
// Ignore mappings that are not executable. std::string lib_name;
if (buffer[3] != 'x') continue; if (sep != nullptr) {
char* start_of_path = index(buffer, '/'); lib_name = std::string(++sep);
// There may be no filename in this line. Skip to next. } else {
if (start_of_path == nullptr) continue; lib_name = std::string(path);
buffer[bytes_read] = 0; }
result.push_back(SharedLibraryAddress(start_of_path, start, end)); result.push_back(SharedLibraryAddress(
lib_name, reinterpret_cast<uintptr_t>(map->kve_start),
reinterpret_cast<uintptr_t>(map->kve_end)));
}
start += ssize;
}
}
} }
close(fd);
return result; return result;
} }
......
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