libprocinfo: provide MapInfo structure for reading maps.

Bug: none
Test: run libprocinfo_test.
Change-Id: I4d3c0f3012e91571aef7ebf4b154df1c9c5addc2
This commit is contained in:
Yabin Cui 2018-10-30 17:14:35 -07:00
parent e94eb5140c
commit 0fe48f943d
3 changed files with 33 additions and 25 deletions

View file

@ -22,6 +22,7 @@
#include <functional>
#include <string>
#include <vector>
#include <android-base/file.h>
@ -147,5 +148,23 @@ inline bool ReadProcessMaps(
return ReadMapFile("/proc/" + std::to_string(pid) + "/maps", callback);
}
struct MapInfo {
uint64_t start;
uint64_t end;
uint16_t flags;
uint64_t pgoff;
std::string name;
MapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name)
: start(start), end(end), flags(flags), pgoff(pgoff), name(name) {}
};
inline bool ReadProcessMaps(pid_t pid, std::vector<MapInfo>* maps) {
return ReadProcessMaps(
pid, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name) {
maps->emplace_back(start, end, flags, pgoff, name);
});
}
} /* namespace procinfo */
} /* namespace android */

View file

@ -27,21 +27,10 @@
#include <benchmark/benchmark.h>
struct MapInfo {
uint64_t start;
uint64_t end;
uint16_t flags;
uint64_t pgoff;
const std::string name;
MapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name)
: start(start), end(end), flags(flags), pgoff(pgoff), name(name) {}
};
static void BM_ReadMapFile(benchmark::State& state) {
std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
for (auto _ : state) {
std::vector<MapInfo> maps;
std::vector<android::procinfo::MapInfo> maps;
android::procinfo::ReadMapFile(
map_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
const char* name) { maps.emplace_back(start, end, flags, pgoff, name); });

View file

@ -22,20 +22,9 @@
#include <gtest/gtest.h>
struct MapInfo {
uint64_t start;
uint64_t end;
uint16_t flags;
uint64_t pgoff;
const std::string name;
MapInfo(uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff, const char* name)
: start(start), end(end), flags(flags), pgoff(pgoff), name(name) {}
};
TEST(process_map, smoke) {
TEST(process_map, ReadMapFile) {
std::string map_file = android::base::GetExecutableDirectory() + "/testdata/maps";
std::vector<MapInfo> maps;
std::vector<android::procinfo::MapInfo> maps;
ASSERT_TRUE(android::procinfo::ReadMapFile(
map_file, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
const char* name) { maps.emplace_back(start, end, flags, pgoff, name); }));
@ -58,3 +47,14 @@ TEST(process_map, smoke) {
"[anon:dalvik-classes.dex extracted in memory from "
"/data/app/com.google.sample.tunnel-HGGRU03Gu1Mwkf_-RnFmvw==/base.apk]");
}
TEST(process_map, ReadProcessMaps) {
std::vector<android::procinfo::MapInfo> maps;
ASSERT_TRUE(android::procinfo::ReadProcessMaps(
getpid(), [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t pgoff,
const char* name) { maps.emplace_back(start, end, flags, pgoff, name); }));
ASSERT_GT(maps.size(), 0u);
maps.clear();
ASSERT_TRUE(android::procinfo::ReadProcessMaps(getpid(), &maps));
ASSERT_GT(maps.size(), 0u);
}