• 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 "smart_fd.h"
26 #include "unwind_context.h"
27 
28 #if is_mingw
29 #ifndef UNWIND_BYTE_ORDER
30 #define UNWIND_BYTE_ORDER -1 // unknown
31 #endif
32 #endif
33 
34 namespace OHOS {
35 namespace HiviewDFX {
36 class DfxMap;
37 
38 class DfxAccessors {
39 public:
bigEndian_(bigEndian)40     DfxAccessors(int bigEndian = UNWIND_BYTE_ORDER) : bigEndian_(bigEndian) {}
41     virtual ~DfxAccessors() = default;
42     static bool GetMapByPcAndCtx(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg);
43 
44     virtual int AccessMem(uintptr_t addr, uintptr_t *val, void *arg) = 0;
45     virtual int AccessReg(int regIdx, uintptr_t *val, void *arg) = 0;
46     virtual int FindUnwindTable(uintptr_t pc, UnwindTableInfo& uti, void *arg) = 0;
47     virtual int GetMapByPc(uintptr_t pc, std::shared_ptr<DfxMap>& map, void *arg) = 0;
48 
49     int bigEndian_ = UNWIND_BYTE_ORDER;
50 };
51 
52 class DfxAccessorsLocal : public DfxAccessors {
53 public:
54     DfxAccessorsLocal() = default;
55     ~DfxAccessorsLocal() override = default;
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     SmartFd readFd_;
68     SmartFd writeFd_;
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