• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
bigEndian_(bigEndian)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 
56     int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) override;
57     int AccessReg(int regIdx, uintptr_t *val, void *arg) override;
58     int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) override;
59     int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) override;
60 private:
61     DfxAccessorsLocal(const DfxAccessorsLocal&) = delete;
62     DfxAccessorsLocal& operator= (const DfxAccessorsLocal&) = delete;
63     bool CreatePipe();
64 
65     std::mutex mutex_;
66     int32_t pfd_[PIPE_NUM_SZ] = {-1, -1};
67     bool initPipe_ = false;
68 };
69 
70 class DfxAccessorsRemote : public DfxAccessors {
71 public:
72     DfxAccessorsRemote() = default;
73     virtual ~DfxAccessorsRemote() = default;
74 
75     int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) override;
76     int AccessReg(int regIdx, uintptr_t *val, void *arg) override;
77     int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) override;
78     int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) override;
79 };
80 
81 class DfxAccessorsCustomize : public DfxAccessors {
82 public:
DfxAccessorsCustomize(std::shared_ptr<UnwindAccessors> accessors)83     DfxAccessorsCustomize(std::shared_ptr<UnwindAccessors> accessors) : accessors_(accessors) {}
84     virtual ~DfxAccessorsCustomize() = default;
85 
86     int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) override;
87     int AccessReg(int regIdx, uintptr_t *val, void *arg) override;
88     int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) override;
89     int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) override;
90 private:
91     std::shared_ptr<UnwindAccessors> accessors_;
92 };
93 } // namespace HiviewDFX
94 } // namespace OHOS
95 #endif
96