• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 UNWIND_CONTEXT_H
16 #define UNWIND_CONTEXT_H
17 
18 #include <memory>
19 #if is_ohos
20 #include <ucontext.h>
21 #endif
22 
23 #include "byte_order.h"
24 #include "dfx_errors.h"
25 #include "dfx_regs_qut.h"
26 #include "unwind_define.h"
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 class DfxMap;
31 class DfxMaps;
32 class DfxRegs;
33 
34 struct UnwindTableInfo {
35     uintptr_t startPc = 0;
36     uintptr_t endPc = 0;
37     uintptr_t gp = 0; /* global-pointer in effect for this entry */
38     int format = -1;
39     bool isLinear = false;
40     uintptr_t namePtr = 0;
41     uintptr_t segbase = 0;
42     uintptr_t tableLen = 0;
43     uintptr_t tableData = 0;
44 };
45 
46 struct UnwindEntryInfo {
47     uintptr_t startPc;
48     uintptr_t endPc;
49     uintptr_t lsda;
50     uintptr_t handler;
51     uintptr_t gp;
52     int format = -1;
53     int unwindInfoSize;
54     void *unwindInfo;
55 };
56 
57 struct UnwindAccessors {
58     int (*FindUnwindTable)(uintptr_t, UnwindTableInfo &, void *);
59     int (*AccessMem)(uintptr_t, uintptr_t *, void *);
60     int (*AccessReg)(int, uintptr_t *, void *);
61     int (*GetMapByPc)(uintptr_t, std::shared_ptr<DfxMap> &, void *);
62 };
63 
64 struct UnwindContext {
65     bool stackCheck = false;
66     uintptr_t stackBottom = 0;
67     uintptr_t stackTop = 0;
68     int pid = 0;
69     std::shared_ptr<DfxRegs> regs = nullptr;
70     std::shared_ptr<DfxMaps> maps = nullptr;
71     std::shared_ptr<DfxMap> map = nullptr;
72     struct UnwindTableInfo di;
73 };
74 
75 enum RegLocEnum : uint8_t {
76     REG_LOC_UNUSED = 0,     // register has same value as in prev. frame
77     REG_LOC_UNDEFINED,      // register isn't saved at all
78     REG_LOC_VAL_OFFSET,     // register that is the value, cfa = cfa + off
79     REG_LOC_MEM_OFFSET,     // register saved at CFA-relative address, cfa = [r14 + off], r14 = [cfa + off]
80     REG_LOC_REGISTER,       // register saved in another register, cfa = [r14], pc = [lr]
81     REG_LOC_VAL_EXPRESSION, // register value is expression result, r11 = cfa + expr_result
82     REG_LOC_MEM_EXPRESSION, // register stored in expression result, r11 = [cfa + expr_result]
83 };
84 
85 struct RegLoc {
86     RegLocEnum type = REG_LOC_UNUSED; /* see DWARF_LOC_* macros. */
87     intptr_t val = 0;
88 };
89 
90 // saved register status after running call frame instructions
91 // it should describe how register saved
92 struct RegLocState {
RegLocStateRegLocState93     RegLocState() { locs.resize(DfxRegsQut::GetQutRegsSize()); }
RegLocStateRegLocState94     explicit RegLocState(size_t locsSize) { locs.resize(locsSize); }
95     ~RegLocState() = default;
96 
97     uintptr_t pcStart = 0;
98     uintptr_t pcEnd = 0;
99     uint32_t cfaReg = 0; // cfa = [r14]
100     union {
101         int32_t cfaRegOffset = 0; // cfa = cfa + offset
102         uintptr_t cfaExprPtr; // cfa = expr
103     };
104     bool returnAddressUndefined = false;
105     bool returnAddressSame = false;
106     uint16_t returnAddressRegister = 0; // lr
107     std::vector<RegLoc> locs;
108 };
109 } // namespace HiviewDFX
110 } // namespace OHOS
111 #endif
112