diff options
author | Yabin Cui <yabinc@google.com> | 2020-11-10 13:11:01 -0800 |
---|---|---|
committer | Yabin Cui <yabinc@google.com> | 2020-11-11 10:38:47 -0800 |
commit | c13ff898d0876060bf425f4be0da1ecbda51052d (patch) | |
tree | 9e03d39390535c0bac65c7d26670caacb36c5744 /simpleperf/environment.cpp | |
parent | ee9f8c79e104eeb328cfb02ce126171b4531146a (diff) | |
download | extras-c13ff898d0876060bf425f4be0da1ecbda51052d.tar.gz |
simpleperf: use kernel start address in kernel mmap record.
The kernel start address is used to match with the start address in
.text section in vmlinux. It works even when kernel address space
layout randomization is used.
Bug: 172933381
Test: run simpleperf_unit_test
Change-Id: I3eb2bee8dd30b049504f4ec6f8f4b6d742cdd6b5
Diffstat (limited to 'simpleperf/environment.cpp')
-rw-r--r-- | simpleperf/environment.cpp | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/simpleperf/environment.cpp b/simpleperf/environment.cpp index 9f3fd3bc..f3333ca1 100644 --- a/simpleperf/environment.cpp +++ b/simpleperf/environment.cpp @@ -175,10 +175,33 @@ static std::vector<KernelMmap> GetModulesInUse() { return module_mmaps; } +static uint64_t GetKernelStartAddress() { + FILE* fp = fopen("/proc/kallsyms", "re"); + if (fp == nullptr) { + return 0; + } + LineReader reader(fp); + char* line; + while ((line = reader.ReadLine()) != nullptr) { + if (strstr(line, "_stext") != nullptr) { + uint64_t addr; + if (sscanf(line, "%" PRIx64, &addr) == 1) { + return addr; + } + } + } + return 0; +} + void GetKernelAndModuleMmaps(KernelMmap* kernel_mmap, std::vector<KernelMmap>* module_mmaps) { kernel_mmap->name = DEFAULT_KERNEL_MMAP_NAME; kernel_mmap->start_addr = 0; kernel_mmap->len = std::numeric_limits<uint64_t>::max(); + if (uint64_t kstart_addr = GetKernelStartAddress(); kstart_addr != 0) { + kernel_mmap->name = std::string(DEFAULT_KERNEL_MMAP_NAME) + "_stext"; + kernel_mmap->start_addr = kstart_addr; + kernel_mmap->len = std::numeric_limits<uint64_t>::max() - kstart_addr; + } kernel_mmap->filepath = kernel_mmap->name; *module_mmaps = GetModulesInUse(); for (auto& map : *module_mmaps) { |