diff options
author | David Srbecky <dsrbecky@google.com> | 2021-02-14 21:42:08 +0000 |
---|---|---|
committer | David Srbecky <dsrbecky@google.com> | 2021-03-09 22:10:03 +0000 |
commit | 0b6d9eefdc4996ce99c799dc6d23a388e969c2af (patch) | |
tree | 787db46db092526de0ca9f393c8210c672732804 | |
parent | 8c9b52bdf55e82a4d60fedc12db3784c1ffd4a50 (diff) | |
download | unwinding-0b6d9eefdc4996ce99c799dc6d23a388e969c2af.tar.gz |
Add helpers to create JitDebug and DexFiles.
These helpers will make it easier to replace the
implementation without affecting users of the classes.
The helpers take the arch, which ensures it is set and
it allows to return templated variant based on the arch.
Test: art/test.py -r -b --host -t 137-cfi
Test: libunwindstack_test
Change-Id: I6e09ce29e39faa57c95fc8a3e86f2260bf91a76e
-rw-r--r-- | libunwindstack/DexFiles.cpp | 10 | ||||
-rw-r--r-- | libunwindstack/JitDebug.cpp | 9 | ||||
-rw-r--r-- | libunwindstack/Unwinder.cpp | 4 | ||||
-rw-r--r-- | libunwindstack/include/unwindstack/DexFiles.h | 3 | ||||
-rw-r--r-- | libunwindstack/include/unwindstack/JitDebug.h | 3 | ||||
-rw-r--r-- | libunwindstack/tests/JitDebugTest.cpp | 14 | ||||
-rw-r--r-- | libunwindstack/tests/UnwindOfflineTest.cpp | 17 | ||||
-rw-r--r-- | libunwindstack/tests/UnwinderTest.cpp | 14 |
8 files changed, 47 insertions, 27 deletions
diff --git a/libunwindstack/DexFiles.cpp b/libunwindstack/DexFiles.cpp index 43181fb..bc660da 100644 --- a/libunwindstack/DexFiles.cpp +++ b/libunwindstack/DexFiles.cpp @@ -27,6 +27,8 @@ #include <unwindstack/Maps.h> #include <unwindstack/Memory.h> +#include "Check.h" + #if defined(DEXFILE_SUPPORT) #include "DexFile.h" #endif @@ -196,4 +198,12 @@ void DexFiles::GetFunctionName(Maps* maps, MapInfo* info, uint64_t dex_pc, std:: void DexFiles::GetFunctionName(Maps*, MapInfo*, uint64_t, std::string*, uint64_t*) {} #endif +std::unique_ptr<DexFiles> CreateDexFiles(ArchEnum arch, std::shared_ptr<Memory>& memory, + std::vector<std::string> search_libs) { + CHECK(arch != ARCH_UNKNOWN); + std::unique_ptr<DexFiles> dex_files(new DexFiles(memory, search_libs)); + dex_files->SetArch(arch); + return dex_files; +} + } // namespace unwindstack diff --git a/libunwindstack/JitDebug.cpp b/libunwindstack/JitDebug.cpp index 8ad6a57..1dd4e11 100644 --- a/libunwindstack/JitDebug.cpp +++ b/libunwindstack/JitDebug.cpp @@ -24,6 +24,7 @@ #include <unwindstack/JitDebug.h> #include <unwindstack/Maps.h> +#include "Check.h" #include "MemoryRange.h" // This implements the JIT Compilation Interface. @@ -219,4 +220,12 @@ Elf* JitDebug::Find(Maps* maps, uint64_t pc) { return nullptr; } +std::unique_ptr<JitDebug> CreateJitDebug(ArchEnum arch, std::shared_ptr<Memory>& memory, + std::vector<std::string> search_libs) { + CHECK(arch != ARCH_UNKNOWN); + std::unique_ptr<JitDebug> jit_debug(new JitDebug(memory, search_libs)); + jit_debug->SetArch(arch); + return jit_debug; +} + } // namespace unwindstack diff --git a/libunwindstack/Unwinder.cpp b/libunwindstack/Unwinder.cpp index a380fba..fb55944 100644 --- a/libunwindstack/Unwinder.cpp +++ b/libunwindstack/Unwinder.cpp @@ -397,11 +397,11 @@ bool UnwinderFromPid::Init() { process_memory_ = Memory::CreateProcessMemoryCached(pid_); - jit_debug_ptr_.reset(new JitDebug(process_memory_)); + jit_debug_ptr_ = CreateJitDebug(arch_, process_memory_); jit_debug_ = jit_debug_ptr_.get(); SetJitDebug(jit_debug_); #if defined(DEXFILE_SUPPORT) - dex_files_ptr_.reset(new DexFiles(process_memory_)); + dex_files_ptr_ = CreateDexFiles(arch_, process_memory_); dex_files_ = dex_files_ptr_.get(); SetDexFiles(dex_files_); #endif diff --git a/libunwindstack/include/unwindstack/DexFiles.h b/libunwindstack/include/unwindstack/DexFiles.h index 8b480df..554f7d7 100644 --- a/libunwindstack/include/unwindstack/DexFiles.h +++ b/libunwindstack/include/unwindstack/DexFiles.h @@ -74,6 +74,9 @@ class DexFiles : public Global { std::vector<uint64_t> addrs_; }; +std::unique_ptr<DexFiles> CreateDexFiles(ArchEnum arch, std::shared_ptr<Memory>& memory, + std::vector<std::string> search_libs = {}); + } // namespace unwindstack #endif // _LIBUNWINDSTACK_DEX_FILES_H diff --git a/libunwindstack/include/unwindstack/JitDebug.h b/libunwindstack/include/unwindstack/JitDebug.h index 2090f3f..d8c04e0 100644 --- a/libunwindstack/include/unwindstack/JitDebug.h +++ b/libunwindstack/include/unwindstack/JitDebug.h @@ -66,6 +66,9 @@ class JitDebug : public Global { std::mutex lock_; }; +std::unique_ptr<JitDebug> CreateJitDebug(ArchEnum arch, std::shared_ptr<Memory>& memory, + std::vector<std::string> search_libs = {}); + } // namespace unwindstack #endif // _LIBUNWINDSTACK_JIT_DEBUG_H diff --git a/libunwindstack/tests/JitDebugTest.cpp b/libunwindstack/tests/JitDebugTest.cpp index b6e87ae..c8134fd 100644 --- a/libunwindstack/tests/JitDebugTest.cpp +++ b/libunwindstack/tests/JitDebugTest.cpp @@ -50,8 +50,7 @@ class JitDebugTest : public ::testing::Test { } void Init(ArchEnum arch) { - jit_debug_.reset(new JitDebug(process_memory_)); - jit_debug_->SetArch(arch); + jit_debug_ = CreateJitDebug(arch, process_memory_); maps_.reset( new BufferMaps("1000-4000 ---s 00000000 00:00 0 /fake/elf1\n" @@ -318,8 +317,7 @@ TEST_F(JitDebugTest, get_multiple_jit_debug_descriptors_valid) { // Now clear the descriptor entry for the first one. WriteDescriptor32(0x11800, 0); - jit_debug_.reset(new JitDebug(process_memory_)); - jit_debug_->SetArch(ARCH_ARM); + jit_debug_ = CreateJitDebug(ARCH_ARM, process_memory_); ASSERT_TRUE(jit_debug_->Find(maps_.get(), 0x1500) == nullptr); ASSERT_TRUE(jit_debug_->Find(maps_.get(), 0x2000) != nullptr); @@ -333,7 +331,7 @@ TEST_F(JitDebugTest, get_elf_x86) { WriteDescriptor32(0x11800, 0x200000); WriteEntry32Pack(0x200000, 0, 0, 0x4000, 0x1000); - jit_debug_->SetArch(ARCH_X86); + jit_debug_ = CreateJitDebug(ARCH_X86, process_memory_); Elf* elf = jit_debug_->Find(maps_.get(), 0x1500); ASSERT_TRUE(elf != nullptr); @@ -394,8 +392,7 @@ TEST_F(JitDebugTest, get_elf_search_libs) { // Only search a given named list of libs. std::vector<std::string> libs{"libart.so"}; - jit_debug_.reset(new JitDebug(process_memory_, libs)); - jit_debug_->SetArch(ARCH_ARM); + jit_debug_ = CreateJitDebug(ARCH_ARM, process_memory_, libs); EXPECT_TRUE(jit_debug_->Find(maps_.get(), 0x1500) == nullptr); // Change the name of the map that includes the value and verify this works. @@ -403,11 +400,10 @@ TEST_F(JitDebugTest, get_elf_search_libs) { map_info->name = "/system/lib/libart.so"; map_info = maps_->Get(6); map_info->name = "/system/lib/libart.so"; - jit_debug_.reset(new JitDebug(process_memory_, libs)); + jit_debug_ = CreateJitDebug(ARCH_ARM, process_memory_, libs); // Make sure that clearing our copy of the libs doesn't affect the // JitDebug object. libs.clear(); - jit_debug_->SetArch(ARCH_ARM); EXPECT_TRUE(jit_debug_->Find(maps_.get(), 0x1500) != nullptr); } diff --git a/libunwindstack/tests/UnwindOfflineTest.cpp b/libunwindstack/tests/UnwindOfflineTest.cpp index ab427b5..04fb892 100644 --- a/libunwindstack/tests/UnwindOfflineTest.cpp +++ b/libunwindstack/tests/UnwindOfflineTest.cpp @@ -312,9 +312,9 @@ TEST_F(UnwindOfflineTest, jit_debug_x86) { } process_memory_.reset(memory); - JitDebug jit_debug(process_memory_); + std::unique_ptr<JitDebug> jit_debug = CreateJitDebug(regs_->Arch(), process_memory_); Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_); - unwinder.SetJitDebug(&jit_debug); + unwinder.SetJitDebug(jit_debug.get()); unwinder.Unwind(); std::string frame_info(DumpFrames(unwinder)); @@ -614,9 +614,9 @@ TEST_F(UnwindOfflineTest, jit_debug_arm) { } process_memory_.reset(memory); - JitDebug jit_debug(process_memory_); + std::unique_ptr<JitDebug> jit_debug = CreateJitDebug(regs_->Arch(), process_memory_); Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_); - unwinder.SetJitDebug(&jit_debug); + unwinder.SetJitDebug(jit_debug.get()); unwinder.Unwind(); std::string frame_info(DumpFrames(unwinder)); @@ -937,9 +937,10 @@ static void OfflineUnwind(void* data) { LeakType* leak_data = reinterpret_cast<LeakType*>(data); std::unique_ptr<Regs> regs_copy(leak_data->regs->Clone()); - JitDebug jit_debug(leak_data->process_memory); + std::unique_ptr<JitDebug> jit_debug = + CreateJitDebug(leak_data->regs->Arch(), leak_data->process_memory); Unwinder unwinder(128, leak_data->maps, regs_copy.get(), leak_data->process_memory); - unwinder.SetJitDebug(&jit_debug); + unwinder.SetJitDebug(jit_debug.get()); unwinder.Unwind(); ASSERT_EQ(76U, unwinder.NumFrames()); } @@ -1060,9 +1061,9 @@ TEST_F(UnwindOfflineTest, art_quick_osr_stub_arm) { } process_memory_.reset(memory); - JitDebug jit_debug(process_memory_); + std::unique_ptr<JitDebug> jit_debug = CreateJitDebug(regs_->Arch(), process_memory_); Unwinder unwinder(128, maps_.get(), regs_.get(), process_memory_); - unwinder.SetJitDebug(&jit_debug); + unwinder.SetJitDebug(jit_debug.get()); unwinder.Unwind(); std::string frame_info(DumpFrames(unwinder)); diff --git a/libunwindstack/tests/UnwinderTest.cpp b/libunwindstack/tests/UnwinderTest.cpp index 12c23b3..4268c87 100644 --- a/libunwindstack/tests/UnwinderTest.cpp +++ b/libunwindstack/tests/UnwinderTest.cpp @@ -1180,9 +1180,9 @@ TEST_F(UnwinderTest, dex_pc_not_in_map_valid_dex_files) { regs_.set_sp(0x10000); regs_.FakeSetDexPc(0x50000); - DexFiles dex_files(process_memory_); + std::unique_ptr<DexFiles> dex_files = CreateDexFiles(regs_.Arch(), process_memory_); Unwinder unwinder(64, maps_.get(), ®s_, process_memory_); - unwinder.SetDexFiles(&dex_files); + unwinder.SetDexFiles(dex_files.get()); unwinder.Unwind(); EXPECT_EQ(ERROR_NONE, unwinder.LastErrorCode()); EXPECT_EQ(WARNING_DEX_PC_NOT_IN_MAP, unwinder.warnings()); @@ -1734,9 +1734,9 @@ TEST_F(UnwinderTest, build_frame_pc_in_jit) { RegsFake regs(10); regs.FakeSetArch(ARCH_ARM); - JitDebug jit_debug(process_memory_); + std::unique_ptr<JitDebug> jit_debug = CreateJitDebug(regs.Arch(), process_memory_); Unwinder unwinder(10, maps_.get(), ®s, process_memory_); - unwinder.SetJitDebug(&jit_debug); + unwinder.SetJitDebug(jit_debug.get()); FrameData frame = unwinder.BuildFrameFromPcOnly(0x100310); EXPECT_EQ(0x10030eU, frame.pc); @@ -1759,14 +1759,12 @@ TEST_F(UnwinderTest, unwinder_from_pid_init_error) { TEST_F(UnwinderTest, set_jit_debug_error) { Unwinder unwinder(10, maps_.get(), process_memory_); - JitDebug jit_debug(process_memory_); - ASSERT_DEATH(unwinder.SetJitDebug(&jit_debug), ""); + ASSERT_DEATH(CreateJitDebug(ARCH_UNKNOWN, process_memory_), ""); } TEST_F(UnwinderTest, set_dex_files_error) { Unwinder unwinder(10, maps_.get(), process_memory_); - DexFiles dex_files(process_memory_); - ASSERT_DEATH(unwinder.SetDexFiles(&dex_files), ""); + ASSERT_DEATH(CreateDexFiles(ARCH_UNKNOWN, process_memory_), ""); } } // namespace unwindstack |