summaryrefslogtreecommitdiff
path: root/simpleperf/dso_test.cpp
diff options
context:
space:
mode:
authorYabin Cui <yabinc@google.com>2020-11-10 16:24:12 -0800
committerYabin Cui <yabinc@google.com>2020-11-11 10:38:55 -0800
commit7078c67e1c389a16d32873d42bd45eebbb97a598 (patch)
tree96c6e2e7e51acb6dedd3d8175f3eadd6c6c11c59 /simpleperf/dso_test.cpp
parentc13ff898d0876060bf425f4be0da1ecbda51052d (diff)
downloadextras-7078c67e1c389a16d32873d42bd45eebbb97a598.tar.gz
simpleperf: support kernel etm data in inject cmd.
1. Refactor KernelDso class to handle kernel address randomization. 2. Add Dso::IpToFileOffset() to convert ip addresses to file offsets, and use special operation for KernelDso. 3. Search for vmlinux in symbol dirs. 4. Delay creating KernelDso to use the kernel build id in recording files. Bug: 172933381 Test: run simpleperf_unit_test Test: run `simpleperf inject` manually to inject kernel etm data. Change-Id: Ice7f465a6f1e870740cc8ceaf8da76220026adef
Diffstat (limited to 'simpleperf/dso_test.cpp')
-rw-r--r--simpleperf/dso_test.cpp41
1 files changed, 41 insertions, 0 deletions
diff --git a/simpleperf/dso_test.cpp b/simpleperf/dso_test.cpp
index 566d76b9..96663d6b 100644
--- a/simpleperf/dso_test.cpp
+++ b/simpleperf/dso_test.cpp
@@ -24,6 +24,7 @@
#include "get_test_data.h"
#include "read_apk.h"
+#include "thread_tree.h"
#include "utils.h"
using namespace simpleperf_dso_impl;
@@ -210,6 +211,46 @@ TEST(dso, IpToVaddrInFile) {
ASSERT_EQ(0xa5140, dso->IpToVaddrInFile(0xe9201140, 0xe9201000, 0xa5000));
}
+TEST(dso, kernel_address_randomization) {
+ // Use ELF_FILE as a fake kernel vmlinux.
+ const std::string vmlinux_path = GetTestData(ELF_FILE);
+ Dso::SetVmlinux(vmlinux_path);
+ std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
+ ASSERT_TRUE(dso);
+ ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path);
+ // When map_start = 0, can't fix kernel address randomization. So vmlinux isn't used.
+ ASSERT_EQ(dso->IpToVaddrInFile(0x800500, 0, 0), 0x800500);
+ ASSERT_FALSE(dso->IpToFileOffset(0x800500, 0, 0));
+ ASSERT_TRUE(dso->FindSymbol(0x400510) == nullptr);
+
+ dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
+ ASSERT_TRUE(dso);
+ ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path);
+ // When map_start != 0, can fix kernel address randomization. So vmlinux is used.
+ ASSERT_EQ(dso->IpToVaddrInFile(0x800500, 0x800400, 0), 0x400500);
+ ASSERT_EQ(dso->IpToFileOffset(0x800500, 0x800400, 0).value(), 0x500);
+ const Symbol* symbol = dso->FindSymbol(0x400510);
+ ASSERT_TRUE(symbol != nullptr);
+ ASSERT_STREQ(symbol->Name(), "GlobalFunc");
+}
+
+TEST(dso, find_vmlinux_in_symdirs) {
+ // Create a symdir.
+ TemporaryDir tmpdir;
+ std::string vmlinux_path = std::string(tmpdir.path) + OS_PATH_SEPARATOR + "elf";
+ std::string data;
+ ASSERT_TRUE(android::base::ReadFileToString(GetTestData(ELF_FILE), &data));
+ ASSERT_TRUE(android::base::WriteStringToFile(data, vmlinux_path));
+
+ // Find vmlinux in symbol dirs.
+ Dso::SetVmlinux("");
+ Dso::AddSymbolDir(tmpdir.path);
+ Dso::SetBuildIds({std::make_pair(DEFAULT_KERNEL_MMAP_NAME, BuildId(ELF_FILE_BUILD_ID))});
+ std::unique_ptr<Dso> dso = Dso::CreateDso(DSO_KERNEL, DEFAULT_KERNEL_MMAP_NAME);
+ ASSERT_TRUE(dso);
+ ASSERT_EQ(dso->GetDebugFilePath(), vmlinux_path);
+}
+
TEST(dso, kernel_module) {
// Test finding debug files for kernel modules.
Dso::SetSymFsDir(GetTestDataDir());