1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef DFX_FAULT_STACK_H 16 #define DFX_FAULT_STACK_H 17 18 #include <memory> 19 #include <vector> 20 #include "dfx_frame.h" 21 #include "dfx_regs.h" 22 #include "dfx_maps.h" 23 24 namespace OHOS { 25 namespace HiviewDFX { 26 struct MemoryBlockInfo { 27 uintptr_t startAddr; 28 uintptr_t nameAddr; 29 uintptr_t size; 30 std::vector<uintptr_t> content; 31 std::string name; 32 }; 33 34 // Only print first three frame stack 35 class FaultStack { 36 public: FaultStack(int32_t tid)37 explicit FaultStack(int32_t tid) : tid_(tid) {}; 38 ~FaultStack() = default; 39 40 bool CollectStackInfo(const std::vector<std::shared_ptr<DfxFrame>> &frames); 41 void CollectRegistersBlock(std::shared_ptr<DfxRegs> regs, std::shared_ptr<DfxElfMaps> maps); 42 void Print() const; 43 void PrintRegisterMemory() const; 44 45 private: 46 bool ReadTargetMemory(uintptr_t addr, uintptr_t &value) const; 47 uintptr_t AdjustAndCreateMemoryBlock(size_t index, uintptr_t prevSp, uintptr_t prevEndAddr, uintptr_t size); 48 uintptr_t PrintMemoryBlock(const MemoryBlockInfo& info) const; 49 MemoryBlockInfo CreateMemoryBlock(uintptr_t addr, uintptr_t offset, uintptr_t size, const std::string& name) const; 50 51 private: 52 int32_t tid_; 53 std::vector<MemoryBlockInfo> blocks_; 54 std::vector<MemoryBlockInfo> registerBlocks_; 55 }; 56 } // namespace HiviewDFX 57 } // namespace OHOS 58 #endif 59