1 /* 2 * Copyright (c) 2023-2024 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 16 #ifndef DFX_ACCESSORS_H 17 #define DFX_ACCESSORS_H 18 19 #include <atomic> 20 #include <cstdint> 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 #include "dfx_define.h" 25 #include "unwind_context.h" 26 27 #if is_mingw 28 #ifndef UNWIND_BYTE_ORDER 29 #define UNWIND_BYTE_ORDER -1 // unknown 30 #endif 31 #endif 32 33 namespace OHOS { 34 namespace HiviewDFX { 35 class DfxMap; 36 37 class DfxAccessors { 38 public: 39 DfxAccessors(int bigEndian = UNWIND_BYTE_ORDER) : bigEndian_(bigEndian) {} 40 virtual ~DfxAccessors() = default; 41 static bool GetMapByPcAndCtx(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg); 42 43 virtual int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) = 0; 44 virtual int AccessReg(int regIdx, uintptr_t *val, void *arg) = 0; 45 virtual int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) = 0; 46 virtual int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) = 0; 47 48 int bigEndian_ = UNWIND_BYTE_ORDER; 49 }; 50 51 class DfxAccessorsLocal : public DfxAccessors { 52 public: 53 DfxAccessorsLocal() = default; 54 ~DfxAccessorsLocal(); 55 bool IsValidFrame(uintptr_t addr, uintptr_t stackBottom, uintptr_t stackTop); 56 57 int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) override; 58 int AccessReg(int regIdx, uintptr_t *val, void *arg) override; 59 int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) override; 60 int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) override; 61 private: 62 DfxAccessorsLocal(const DfxAccessorsLocal&) = delete; 63 DfxAccessorsLocal& operator= (const DfxAccessorsLocal&) = delete; 64 bool CreatePipe(); 65 66 std::mutex mutex_; 67 int32_t pfd_[PIPE_NUM_SZ] = {-1, -1}; 68 bool initPipe_ = false; 69 }; 70 71 class DfxAccessorsRemote : public DfxAccessors { 72 public: 73 DfxAccessorsRemote() = default; 74 virtual ~DfxAccessorsRemote() = default; 75 76 int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) override; 77 int AccessReg(int regIdx, uintptr_t *val, void *arg) override; 78 int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) override; 79 int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) override; 80 }; 81 82 class DfxAccessorsCustomize : public DfxAccessors { 83 public: DfxAccessorsCustomize(std::shared_ptr<UnwindAccessors> accessors)84 DfxAccessorsCustomize(std::shared_ptr<UnwindAccessors> accessors) : accessors_(accessors) {} 85 virtual ~DfxAccessorsCustomize() = default; 86 87 int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) override; 88 int AccessReg(int regIdx, uintptr_t *val, void *arg) override; 89 int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) override; 90 int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) override; 91 private: 92 std::shared_ptr<UnwindAccessors> accessors_; 93 }; 94 } // namespace HiviewDFX 95 } // namespace OHOS 96 #endif 97