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 UNWIND_LOC_H
17 #define UNWIND_LOC_H
18 
19 #include <cinttypes>
20 #include <string>
21 #include <vector>
22 
23 #include "dfx_regs_qut.h"
24 #include "unwind_define.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 enum RegLocEnum : uint8_t {
29     REG_LOC_UNUSED = 0,     // register has same value as in prev. frame
30     REG_LOC_UNDEFINED,      // register isn't saved at all
31     REG_LOC_VAL_OFFSET,     // register that is the value, cfa = cfa + off
32     REG_LOC_MEM_OFFSET,     // register saved at CFA-relative address, cfa = [r14 + off], r14 = [cfa + off]
33     REG_LOC_REGISTER,       // register saved in another register, cfa = [r14], pc = [lr]
34     REG_LOC_VAL_EXPRESSION, // register value is expression result, r11 = cfa + expr_result
35     REG_LOC_MEM_EXPRESSION, // register stored in expression result, r11 = [cfa + expr_result]
36 };
37 
38 struct RegLoc {
39     RegLocEnum type = REG_LOC_UNUSED; /* see DWARF_LOC_* macros. */
40     intptr_t val = 0;
41 };
42 
43 // saved register status after running call frame instructions
44 // it should describe how register saved
45 struct RegLocState {
RegLocStateRegLocState46     RegLocState() { locs.resize(DfxRegsQut::GetQutRegsSize()); }
RegLocStateRegLocState47     explicit RegLocState(size_t locsSize) { locs.resize(locsSize); }
48     ~RegLocState() = default;
49 
50     uintptr_t pcStart = 0;
51     uintptr_t pcEnd = 0;
52     uint32_t cfaReg = 0; // cfa = [r14]
53     union {
54         int32_t cfaRegOffset = 0; // cfa = cfa + offset
55         uintptr_t cfaExprPtr; // cfa = expr
56     };
57     bool returnAddressUndefined = false;
58     bool returnAddressSame = false;
59     uint16_t returnAddressRegister = 0; // lr
60     std::vector<RegLoc> locs;
61 };
62 } // namespace HiviewDFX
63 } // namespace OHOS
64 #endif
65