From 432981b9055b411c4f4f1c5d6f60c94f8648c0b7 Mon Sep 17 00:00:00 2001 From: Christopher Ferris Date: Thu, 22 Feb 2018 19:42:53 -0800 Subject: [PATCH] Modify the offline handling interface. - Add a new function Backtrace::UnwindOffline that takes the stack data. - Modify BacktraceMap::CreateOffline so it doesn't take the stack data. This makes it easier to reuse the map object created this way. Reusing the map object increases simpleperf speed (unwinds per second) by 50%. Test: backtrace_test libunwindstack_test Change-Id: I90cfbae9e50d95d8a0e3cd394b33ba36d65d45f7 --- libbacktrace/UnwindStack.cpp | 24 +++++++++++++++---- libbacktrace/UnwindStackMap.cpp | 20 ++++++++-------- libbacktrace/UnwindStackMap.h | 4 +++- libbacktrace/include/backtrace/Backtrace.h | 5 ++++ libbacktrace/include/backtrace/BacktraceMap.h | 3 +-- 5 files changed, 39 insertions(+), 17 deletions(-) diff --git a/libbacktrace/UnwindStack.cpp b/libbacktrace/UnwindStack.cpp index 0e32e4740..711a12a6c 100644 --- a/libbacktrace/UnwindStack.cpp +++ b/libbacktrace/UnwindStack.cpp @@ -128,6 +128,22 @@ bool Backtrace::Unwind(unwindstack::Regs* regs, BacktraceMap* back_map, return true; } +bool Backtrace::UnwindOffline(unwindstack::Regs* regs, BacktraceMap* back_map, + const backtrace_stackinfo_t& stack, + std::vector* frames, + BacktraceUnwindError* error) { + UnwindStackOfflineMap* offline_map = reinterpret_cast(back_map); + // Create the process memory from the stack data since this will almost + // always be different each unwind. + if (!offline_map->CreateProcessMemory(stack)) { + if (error != nullptr) { + error->error_code = BACKTRACE_UNWIND_ERROR_SETUP_FAILED; + } + return false; + } + return Backtrace::Unwind(regs, back_map, frames, 0U, nullptr, error); +} + UnwindStackCurrent::UnwindStackCurrent(pid_t pid, pid_t tid, BacktraceMap* map) : BacktraceCurrent(pid, tid, map) {} @@ -221,12 +237,12 @@ bool UnwindStackOffline::ReadWord(uint64_t, word_t*) { Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, const std::vector& maps, const backtrace_stackinfo_t& stack) { - BacktraceMap* map = BacktraceMap::CreateOffline(pid, maps, stack); - if (map == nullptr) { + std::unique_ptr map( + reinterpret_cast(BacktraceMap::CreateOffline(pid, maps))); + if (map.get() == nullptr || !map->CreateProcessMemory(stack)) { return nullptr; } - - return new UnwindStackOffline(arch, pid, tid, map, false); + return new UnwindStackOffline(arch, pid, tid, map.release(), false); } Backtrace* Backtrace::CreateOffline(ArchEnum arch, pid_t pid, pid_t tid, BacktraceMap* map) { diff --git a/libbacktrace/UnwindStackMap.cpp b/libbacktrace/UnwindStackMap.cpp index 6dcc6210f..9c6fed43c 100644 --- a/libbacktrace/UnwindStackMap.cpp +++ b/libbacktrace/UnwindStackMap.cpp @@ -127,12 +127,7 @@ bool UnwindStackOfflineMap::Build() { return false; } -bool UnwindStackOfflineMap::Build(const std::vector& backtrace_maps, - const backtrace_stackinfo_t& stack) { - if (stack.start >= stack.end) { - return false; - } - +bool UnwindStackOfflineMap::Build(const std::vector& backtrace_maps) { for (const backtrace_map_t& map : backtrace_maps) { maps_.push_back(map); } @@ -145,6 +140,13 @@ bool UnwindStackOfflineMap::Build(const std::vector& backtrace_ for (const backtrace_map_t& map : maps_) { maps->Add(map.start, map.end, map.offset, map.flags, map.name, map.load_bias); } + return true; +} + +bool UnwindStackOfflineMap::CreateProcessMemory(const backtrace_stackinfo_t& stack) { + if (stack.start >= stack.end) { + return false; + } // Create the process memory from the stack data. uint64_t size = stack.end - stack.start; @@ -154,7 +156,6 @@ bool UnwindStackOfflineMap::Build(const std::vector& backtrace_ std::shared_ptr shared_memory(memory); process_memory_.reset(new unwindstack::MemoryRange(shared_memory, 0, size, stack.start)); - return true; } @@ -182,10 +183,9 @@ BacktraceMap* BacktraceMap::Create(pid_t pid, bool uncached) { //------------------------------------------------------------------------- // BacktraceMap create offline function. //------------------------------------------------------------------------- -BacktraceMap* BacktraceMap::CreateOffline(pid_t pid, const std::vector& maps, - const backtrace_stackinfo_t& stack) { +BacktraceMap* BacktraceMap::CreateOffline(pid_t pid, const std::vector& maps) { UnwindStackOfflineMap* map = new UnwindStackOfflineMap(pid); - if (!map->Build(maps, stack)) { + if (!map->Build(maps)) { delete map; return nullptr; } diff --git a/libbacktrace/UnwindStackMap.h b/libbacktrace/UnwindStackMap.h index 94cbfb2a0..ec0d9c148 100644 --- a/libbacktrace/UnwindStackMap.h +++ b/libbacktrace/UnwindStackMap.h @@ -76,7 +76,9 @@ class UnwindStackOfflineMap : public UnwindStackMap { bool Build() override; - bool Build(const std::vector& maps, const backtrace_stackinfo_t& stack); + bool Build(const std::vector& maps); + + bool CreateProcessMemory(const backtrace_stackinfo_t& stack); }; #endif // _LIBBACKTRACE_UNWINDSTACK_MAP_H diff --git a/libbacktrace/include/backtrace/Backtrace.h b/libbacktrace/include/backtrace/Backtrace.h index 7a37015d6..a0882079e 100644 --- a/libbacktrace/include/backtrace/Backtrace.h +++ b/libbacktrace/include/backtrace/Backtrace.h @@ -151,6 +151,11 @@ class Backtrace { std::vector* frames, size_t num_ignore_frames, std::vector* skip_names, BacktraceUnwindError* error = nullptr); + static bool UnwindOffline(unwindstack::Regs* regs, BacktraceMap* back_map, + const backtrace_stackinfo_t& stack_info, + std::vector* frames, + BacktraceUnwindError* error = nullptr); + // Get the function name and offset into the function given the pc. // If the string is empty, then no valid function name was found, // or the pc is not in any valid map. diff --git a/libbacktrace/include/backtrace/BacktraceMap.h b/libbacktrace/include/backtrace/BacktraceMap.h index da544725d..473d195ba 100644 --- a/libbacktrace/include/backtrace/BacktraceMap.h +++ b/libbacktrace/include/backtrace/BacktraceMap.h @@ -64,8 +64,7 @@ public: // is unsupported. static BacktraceMap* Create(pid_t pid, bool uncached = false); - static BacktraceMap* CreateOffline(pid_t pid, const std::vector& maps, - const backtrace_stackinfo_t& stack); + static BacktraceMap* CreateOffline(pid_t pid, const std::vector& maps); virtual ~BacktraceMap();