Loading libunwindstack/Android.bp +17 −0 Original line number Diff line number Diff line Loading @@ -62,6 +62,7 @@ cc_library { "MapInfo.cpp", "Maps.cpp", "Memory.cpp", "LocalUnwinder.cpp", "Regs.cpp", "RegsArm.cpp", "RegsArm64.cpp", Loading Loading @@ -125,6 +126,21 @@ cc_library { //------------------------------------------------------------------------- // Unit Tests //------------------------------------------------------------------------- cc_test_library { name: "libunwindstack_local", defaults: ["libunwindstack_flags"], srcs: ["tests/TestLocal.cpp"], cflags: [ "-O0", "-g", ], shared_libs: [ "libunwindstack", ], } cc_test { name: "libunwindstack_test", defaults: ["libunwindstack_flags"], Loading @@ -151,6 +167,7 @@ cc_test { "tests/ElfTest.cpp", "tests/ElfTestUtils.cpp", "tests/JitDebugTest.cpp", "tests/LocalUnwinderTest.cpp", "tests/LogFake.cpp", "tests/MapInfoGetElfTest.cpp", "tests/MapInfoGetLoadBiasTest.cpp", Loading libunwindstack/LocalUnwinder.cpp 0 → 100644 +146 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <pthread.h> #include <stdint.h> #include <memory> #include <string> #include <vector> #include <unwindstack/Elf.h> #include <unwindstack/LocalUnwinder.h> #include <unwindstack/MapInfo.h> #include <unwindstack/Maps.h> #include <unwindstack/Memory.h> #include <unwindstack/Regs.h> #include <unwindstack/RegsGetLocal.h> namespace unwindstack { bool LocalUnwinder::Init() { pthread_rwlock_init(&maps_rwlock_, nullptr); // Create the maps. maps_.reset(new unwindstack::LocalUpdatableMaps()); if (!maps_->Parse()) { maps_.reset(); return false; } process_memory_ = unwindstack::Memory::CreateProcessMemory(getpid()); return true; } bool LocalUnwinder::ShouldSkipLibrary(const std::string& map_name) { for (const std::string& skip_library : skip_libraries_) { if (skip_library == map_name) { return true; } } return false; } MapInfo* LocalUnwinder::GetMapInfo(uint64_t pc) { pthread_rwlock_rdlock(&maps_rwlock_); MapInfo* map_info = maps_->Find(pc); pthread_rwlock_unlock(&maps_rwlock_); if (map_info == nullptr) { pthread_rwlock_wrlock(&maps_rwlock_); // This is guaranteed not to invalidate any previous MapInfo objects so // we don't need to worry about any MapInfo* values already in use. if (maps_->Reparse()) { map_info = maps_->Find(pc); } pthread_rwlock_unlock(&maps_rwlock_); } return map_info; } bool LocalUnwinder::Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames) { std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal()); unwindstack::RegsGetLocal(regs.get()); size_t num_frames = 0; bool adjust_pc = false; while (true) { uint64_t cur_pc = regs->pc(); uint64_t cur_sp = regs->sp(); MapInfo* map_info = GetMapInfo(cur_pc); if (map_info == nullptr) { break; } Elf* elf = map_info->GetElf(process_memory_, true); uint64_t rel_pc = elf->GetRelPc(cur_pc, map_info); uint64_t step_pc = rel_pc; uint64_t pc_adjustment; if (adjust_pc) { pc_adjustment = regs->GetPcAdjustment(rel_pc, elf); } else { pc_adjustment = 0; } step_pc -= pc_adjustment; // Skip any locations that are within this library. if (num_frames != 0 || !ShouldSkipLibrary(map_info->name)) { // Add frame information. std::string func_name; uint64_t func_offset; if (elf->GetFunctionName(rel_pc, &func_name, &func_offset)) { frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, func_name, func_offset); } else { frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, "", 0); } num_frames++; } if (!elf->valid()) { break; } if (frame_info->size() == max_frames) { break; } adjust_pc = true; bool finished; if (!elf->Step(rel_pc, step_pc, regs.get(), process_memory_.get(), &finished) || finished) { break; } // pc and sp are the same, terminate the unwind. if (cur_pc == regs->pc() && cur_sp == regs->sp()) { break; } } return num_frames != 0; } } // namespace unwindstack libunwindstack/Maps.cpp +79 −0 Original line number Diff line number Diff line Loading @@ -105,4 +105,83 @@ const std::string RemoteMaps::GetMapsFile() const { return "/proc/" + std::to_string(pid_) + "/maps"; } const std::string LocalUpdatableMaps::GetMapsFile() const { return "/proc/self/maps"; } bool LocalUpdatableMaps::Reparse() { // New maps will be added at the end without deleting the old ones. size_t last_map_idx = maps_.size(); if (!Parse()) { // Delete any maps added by the Parse call. for (size_t i = last_map_idx; i < maps_.size(); i++) { delete maps_[i]; } maps_.resize(last_map_idx); return false; } size_t total_entries = maps_.size(); size_t search_map_idx = 0; for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) { MapInfo* new_map_info = maps_[new_map_idx]; uint64_t start = new_map_info->start; uint64_t end = new_map_info->end; uint64_t flags = new_map_info->flags; std::string* name = &new_map_info->name; for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) { MapInfo* info = maps_[old_map_idx]; if (start == info->start && end == info->end && flags == info->flags && *name == info->name) { // No need to check search_map_idx = old_map_idx + 1; delete new_map_info; maps_[new_map_idx] = nullptr; total_entries--; break; } else if (info->start > start) { // Stop, there isn't going to be a match. search_map_idx = old_map_idx; break; } // Never delete these maps, they may be in use. The assumption is // that there will only every be a handfull of these so waiting // to destroy them is not too expensive. saved_maps_.push_back(info); maps_[old_map_idx] = nullptr; total_entries--; } if (search_map_idx >= last_map_idx) { break; } } // Now move out any of the maps that never were found. for (size_t i = search_map_idx; i < last_map_idx; i++) { saved_maps_.push_back(maps_[i]); maps_[i] = nullptr; total_entries--; } // Sort all of the values such that the nullptrs wind up at the end, then // resize them away. std::sort(maps_.begin(), maps_.end(), [](const auto* a, const auto* b) { if (a == nullptr) { return false; } else if (b == nullptr) { return true; } return a->start < b->start; }); maps_.resize(total_entries); return true; } LocalUpdatableMaps::~LocalUpdatableMaps() { for (auto map_info : saved_maps_) { delete map_info; } } } // namespace unwindstack libunwindstack/include/unwindstack/LocalUnwinder.h 0 → 100644 +86 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef _LIBUNWINDSTACK_LOCAL_UNWINDER_H #define _LIBUNWINDSTACK_LOCAL_UNWINDER_H #include <pthread.h> #include <stdint.h> #include <sys/types.h> #include <memory> #include <string> #include <vector> #include <unwindstack/Error.h> #include <unwindstack/Maps.h> #include <unwindstack/Memory.h> namespace unwindstack { // Forward declarations. class Elf; struct MapInfo; struct LocalFrameData { LocalFrameData(MapInfo* map_info, uint64_t pc, uint64_t rel_pc, const std::string& function_name, uint64_t function_offset) : map_info(map_info), pc(pc), rel_pc(rel_pc), function_name(function_name), function_offset(function_offset) {} MapInfo* map_info; uint64_t pc; uint64_t rel_pc; std::string function_name; uint64_t function_offset; }; // This is a specialized class that should only be used for doing local unwinds. // The Unwind call can be made as multiple times on the same object, and it can // be called by multiple threads at the same time. // It is designed to be used in debugging circumstances to get a stack trace // as fast as possible. class LocalUnwinder { public: LocalUnwinder() = default; LocalUnwinder(const std::vector<std::string>& skip_libraries) : skip_libraries_(skip_libraries) {} ~LocalUnwinder() = default; bool Init(); bool Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames); bool ShouldSkipLibrary(const std::string& map_name); MapInfo* GetMapInfo(uint64_t pc); ErrorCode LastErrorCode() { return last_error_.code; } uint64_t LastErrorAddress() { return last_error_.address; } private: pthread_rwlock_t maps_rwlock_; std::unique_ptr<LocalUpdatableMaps> maps_ = nullptr; std::shared_ptr<Memory> process_memory_; std::vector<std::string> skip_libraries_; ErrorData last_error_; }; } // namespace unwindstack #endif // _LIBUNWINDSTACK_LOCAL_UNWINDER_H libunwindstack/include/unwindstack/Maps.h +13 −0 Original line number Diff line number Diff line Loading @@ -87,6 +87,19 @@ class LocalMaps : public RemoteMaps { virtual ~LocalMaps() = default; }; class LocalUpdatableMaps : public Maps { public: LocalUpdatableMaps() : Maps() {} virtual ~LocalUpdatableMaps(); bool Reparse(); const std::string GetMapsFile() const override; private: std::vector<MapInfo*> saved_maps_; }; class BufferMaps : public Maps { public: BufferMaps(const char* buffer) : buffer_(buffer) {} Loading Loading
libunwindstack/Android.bp +17 −0 Original line number Diff line number Diff line Loading @@ -62,6 +62,7 @@ cc_library { "MapInfo.cpp", "Maps.cpp", "Memory.cpp", "LocalUnwinder.cpp", "Regs.cpp", "RegsArm.cpp", "RegsArm64.cpp", Loading Loading @@ -125,6 +126,21 @@ cc_library { //------------------------------------------------------------------------- // Unit Tests //------------------------------------------------------------------------- cc_test_library { name: "libunwindstack_local", defaults: ["libunwindstack_flags"], srcs: ["tests/TestLocal.cpp"], cflags: [ "-O0", "-g", ], shared_libs: [ "libunwindstack", ], } cc_test { name: "libunwindstack_test", defaults: ["libunwindstack_flags"], Loading @@ -151,6 +167,7 @@ cc_test { "tests/ElfTest.cpp", "tests/ElfTestUtils.cpp", "tests/JitDebugTest.cpp", "tests/LocalUnwinderTest.cpp", "tests/LogFake.cpp", "tests/MapInfoGetElfTest.cpp", "tests/MapInfoGetLoadBiasTest.cpp", Loading
libunwindstack/LocalUnwinder.cpp 0 → 100644 +146 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include <pthread.h> #include <stdint.h> #include <memory> #include <string> #include <vector> #include <unwindstack/Elf.h> #include <unwindstack/LocalUnwinder.h> #include <unwindstack/MapInfo.h> #include <unwindstack/Maps.h> #include <unwindstack/Memory.h> #include <unwindstack/Regs.h> #include <unwindstack/RegsGetLocal.h> namespace unwindstack { bool LocalUnwinder::Init() { pthread_rwlock_init(&maps_rwlock_, nullptr); // Create the maps. maps_.reset(new unwindstack::LocalUpdatableMaps()); if (!maps_->Parse()) { maps_.reset(); return false; } process_memory_ = unwindstack::Memory::CreateProcessMemory(getpid()); return true; } bool LocalUnwinder::ShouldSkipLibrary(const std::string& map_name) { for (const std::string& skip_library : skip_libraries_) { if (skip_library == map_name) { return true; } } return false; } MapInfo* LocalUnwinder::GetMapInfo(uint64_t pc) { pthread_rwlock_rdlock(&maps_rwlock_); MapInfo* map_info = maps_->Find(pc); pthread_rwlock_unlock(&maps_rwlock_); if (map_info == nullptr) { pthread_rwlock_wrlock(&maps_rwlock_); // This is guaranteed not to invalidate any previous MapInfo objects so // we don't need to worry about any MapInfo* values already in use. if (maps_->Reparse()) { map_info = maps_->Find(pc); } pthread_rwlock_unlock(&maps_rwlock_); } return map_info; } bool LocalUnwinder::Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames) { std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal()); unwindstack::RegsGetLocal(regs.get()); size_t num_frames = 0; bool adjust_pc = false; while (true) { uint64_t cur_pc = regs->pc(); uint64_t cur_sp = regs->sp(); MapInfo* map_info = GetMapInfo(cur_pc); if (map_info == nullptr) { break; } Elf* elf = map_info->GetElf(process_memory_, true); uint64_t rel_pc = elf->GetRelPc(cur_pc, map_info); uint64_t step_pc = rel_pc; uint64_t pc_adjustment; if (adjust_pc) { pc_adjustment = regs->GetPcAdjustment(rel_pc, elf); } else { pc_adjustment = 0; } step_pc -= pc_adjustment; // Skip any locations that are within this library. if (num_frames != 0 || !ShouldSkipLibrary(map_info->name)) { // Add frame information. std::string func_name; uint64_t func_offset; if (elf->GetFunctionName(rel_pc, &func_name, &func_offset)) { frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, func_name, func_offset); } else { frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, "", 0); } num_frames++; } if (!elf->valid()) { break; } if (frame_info->size() == max_frames) { break; } adjust_pc = true; bool finished; if (!elf->Step(rel_pc, step_pc, regs.get(), process_memory_.get(), &finished) || finished) { break; } // pc and sp are the same, terminate the unwind. if (cur_pc == regs->pc() && cur_sp == regs->sp()) { break; } } return num_frames != 0; } } // namespace unwindstack
libunwindstack/Maps.cpp +79 −0 Original line number Diff line number Diff line Loading @@ -105,4 +105,83 @@ const std::string RemoteMaps::GetMapsFile() const { return "/proc/" + std::to_string(pid_) + "/maps"; } const std::string LocalUpdatableMaps::GetMapsFile() const { return "/proc/self/maps"; } bool LocalUpdatableMaps::Reparse() { // New maps will be added at the end without deleting the old ones. size_t last_map_idx = maps_.size(); if (!Parse()) { // Delete any maps added by the Parse call. for (size_t i = last_map_idx; i < maps_.size(); i++) { delete maps_[i]; } maps_.resize(last_map_idx); return false; } size_t total_entries = maps_.size(); size_t search_map_idx = 0; for (size_t new_map_idx = last_map_idx; new_map_idx < maps_.size(); new_map_idx++) { MapInfo* new_map_info = maps_[new_map_idx]; uint64_t start = new_map_info->start; uint64_t end = new_map_info->end; uint64_t flags = new_map_info->flags; std::string* name = &new_map_info->name; for (size_t old_map_idx = search_map_idx; old_map_idx < last_map_idx; old_map_idx++) { MapInfo* info = maps_[old_map_idx]; if (start == info->start && end == info->end && flags == info->flags && *name == info->name) { // No need to check search_map_idx = old_map_idx + 1; delete new_map_info; maps_[new_map_idx] = nullptr; total_entries--; break; } else if (info->start > start) { // Stop, there isn't going to be a match. search_map_idx = old_map_idx; break; } // Never delete these maps, they may be in use. The assumption is // that there will only every be a handfull of these so waiting // to destroy them is not too expensive. saved_maps_.push_back(info); maps_[old_map_idx] = nullptr; total_entries--; } if (search_map_idx >= last_map_idx) { break; } } // Now move out any of the maps that never were found. for (size_t i = search_map_idx; i < last_map_idx; i++) { saved_maps_.push_back(maps_[i]); maps_[i] = nullptr; total_entries--; } // Sort all of the values such that the nullptrs wind up at the end, then // resize them away. std::sort(maps_.begin(), maps_.end(), [](const auto* a, const auto* b) { if (a == nullptr) { return false; } else if (b == nullptr) { return true; } return a->start < b->start; }); maps_.resize(total_entries); return true; } LocalUpdatableMaps::~LocalUpdatableMaps() { for (auto map_info : saved_maps_) { delete map_info; } } } // namespace unwindstack
libunwindstack/include/unwindstack/LocalUnwinder.h 0 → 100644 +86 −0 Original line number Diff line number Diff line /* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef _LIBUNWINDSTACK_LOCAL_UNWINDER_H #define _LIBUNWINDSTACK_LOCAL_UNWINDER_H #include <pthread.h> #include <stdint.h> #include <sys/types.h> #include <memory> #include <string> #include <vector> #include <unwindstack/Error.h> #include <unwindstack/Maps.h> #include <unwindstack/Memory.h> namespace unwindstack { // Forward declarations. class Elf; struct MapInfo; struct LocalFrameData { LocalFrameData(MapInfo* map_info, uint64_t pc, uint64_t rel_pc, const std::string& function_name, uint64_t function_offset) : map_info(map_info), pc(pc), rel_pc(rel_pc), function_name(function_name), function_offset(function_offset) {} MapInfo* map_info; uint64_t pc; uint64_t rel_pc; std::string function_name; uint64_t function_offset; }; // This is a specialized class that should only be used for doing local unwinds. // The Unwind call can be made as multiple times on the same object, and it can // be called by multiple threads at the same time. // It is designed to be used in debugging circumstances to get a stack trace // as fast as possible. class LocalUnwinder { public: LocalUnwinder() = default; LocalUnwinder(const std::vector<std::string>& skip_libraries) : skip_libraries_(skip_libraries) {} ~LocalUnwinder() = default; bool Init(); bool Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames); bool ShouldSkipLibrary(const std::string& map_name); MapInfo* GetMapInfo(uint64_t pc); ErrorCode LastErrorCode() { return last_error_.code; } uint64_t LastErrorAddress() { return last_error_.address; } private: pthread_rwlock_t maps_rwlock_; std::unique_ptr<LocalUpdatableMaps> maps_ = nullptr; std::shared_ptr<Memory> process_memory_; std::vector<std::string> skip_libraries_; ErrorData last_error_; }; } // namespace unwindstack #endif // _LIBUNWINDSTACK_LOCAL_UNWINDER_H
libunwindstack/include/unwindstack/Maps.h +13 −0 Original line number Diff line number Diff line Loading @@ -87,6 +87,19 @@ class LocalMaps : public RemoteMaps { virtual ~LocalMaps() = default; }; class LocalUpdatableMaps : public Maps { public: LocalUpdatableMaps() : Maps() {} virtual ~LocalUpdatableMaps(); bool Reparse(); const std::string GetMapsFile() const override; private: std::vector<MapInfo*> saved_maps_; }; class BufferMaps : public Maps { public: BufferMaps(const char* buffer) : buffer_(buffer) {} Loading