1 /*
2 * Copyright (c) 2021-2022 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 /* This files contains process dump arm reg module. */
17
18 #include "dfx_regs.h"
19
20 #include <cstdio>
21 #include <cstdlib>
22 #include <securec.h>
23 #include "dfx_define.h"
24 #include "dfx_logger.h"
25
26 namespace OHOS {
27 namespace HiviewDFX {
28 enum RegisterSeqNum {
29 REG_ARM_R0 = 0,
30 REG_ARM_R1,
31 REG_ARM_R2,
32 REG_ARM_R3,
33 REG_ARM_R4,
34 REG_ARM_R5,
35 REG_ARM_R6,
36 REG_ARM_R7,
37 REG_ARM_R8,
38 REG_ARM_R9,
39 REG_ARM_R10,
40 REG_ARM_R11,
41 REG_ARM_R12,
42 REG_ARM_R13,
43 REG_ARM_R14,
44 REG_ARM_R15
45 };
46
DfxRegsArm(const ucontext_t & context)47 DfxRegsArm::DfxRegsArm(const ucontext_t& context)
48 {
49 std::vector<uintptr_t> regs {};
50
51 regs.push_back(uintptr_t(context.uc_mcontext.arm_r0)); // 0:r0
52 regs.push_back(uintptr_t(context.uc_mcontext.arm_r1)); // 1:r1
53 regs.push_back(uintptr_t(context.uc_mcontext.arm_r2)); // 2:r2
54 regs.push_back(uintptr_t(context.uc_mcontext.arm_r3)); // 3:r3
55 regs.push_back(uintptr_t(context.uc_mcontext.arm_r4)); // 4:r4
56 regs.push_back(uintptr_t(context.uc_mcontext.arm_r5)); // 5:r5
57 regs.push_back(uintptr_t(context.uc_mcontext.arm_r6)); // 6:r6
58 regs.push_back(uintptr_t(context.uc_mcontext.arm_r7)); // 7:r7
59 regs.push_back(uintptr_t(context.uc_mcontext.arm_r8)); // 8:r8
60 regs.push_back(uintptr_t(context.uc_mcontext.arm_r9)); // 9:r9
61 regs.push_back(uintptr_t(context.uc_mcontext.arm_r10)); // 10:r10
62 regs.push_back(uintptr_t(context.uc_mcontext.arm_fp)); // 11:fp
63 regs.push_back(uintptr_t(context.uc_mcontext.arm_ip)); // 12:ip
64 regs.push_back(uintptr_t(context.uc_mcontext.arm_sp)); // 13:sp
65 regs.push_back(uintptr_t(context.uc_mcontext.arm_lr)); // 14:lr
66 regs.push_back(uintptr_t(context.uc_mcontext.arm_pc)); // 15:pc
67
68 SetRegs(regs);
69 DfxLogDebug("fp:%08x ip:%08x sp:%08x lr:%08x pc:%08x \n", regs[REG_ARM_R11], regs[REG_ARM_R12],
70 regs[REG_ARM_R13], regs[REG_ARM_R14], regs[REG_ARM_R15]);
71 }
72
GetSpecialRegisterName(uintptr_t val) const73 std::string DfxRegsArm::GetSpecialRegisterName(uintptr_t val) const
74 {
75 if (val == regsData_[REG_ARM_R15]) {
76 return "pc";
77 } else if (val == regsData_[REG_ARM_R14]) {
78 return "lr";
79 } else if (val == regsData_[REG_ARM_R13]) {
80 return "sp";
81 } else if (val == regsData_[REG_ARM_R11]) {
82 return "fp";
83 }
84 return "";
85 }
86
PrintRegs() const87 std::string DfxRegsArm::PrintRegs() const
88 {
89 std::string regString = "";
90 char buf[REGS_PRINT_LEN_ARM] = {0};
91
92 regString = regString + "Registers:\n";
93
94 std::vector<uintptr_t> regs = GetRegsData();
95
96 PrintFormat(buf, sizeof(buf), "r0:%08x r1:%08x r2:%08x r3:%08x\n", \
97 regs[REG_ARM_R0], regs[REG_ARM_R1], regs[REG_ARM_R2], regs[REG_ARM_R3]);
98
99 PrintFormat(buf + strlen(buf), sizeof(buf) - strlen(buf), "r4:%08x r5:%08x r6:%08x r7:%08x\n", \
100 regs[REG_ARM_R4], regs[REG_ARM_R5], regs[REG_ARM_R6], regs[REG_ARM_R7]);
101
102 PrintFormat(buf + strlen(buf), sizeof(buf) - strlen(buf), "r8:%08x r9:%08x r10:%08x\n", \
103 regs[REG_ARM_R8], regs[REG_ARM_R9], regs[REG_ARM_R10]);
104
105 PrintFormat(buf + strlen(buf), sizeof(buf) - strlen(buf), "fp:%08x ip:%08x sp:%08x lr:%08x pc:%08x\n", \
106 regs[REG_ARM_R11], regs[REG_ARM_R12], regs[REG_ARM_R13], regs[REG_ARM_R14], regs[REG_ARM_R15]);
107
108 regString = regString + std::string(buf);
109 return regString;
110 }
111
GetPC() const112 uintptr_t DfxRegsArm::GetPC() const
113 {
114 return regsData_[REG_ARM_R15];
115 }
116
GetLR() const117 uintptr_t DfxRegsArm::GetLR() const
118 {
119 return regsData_[REG_ARM_R14];
120 }
121 } // namespace HiviewDFX
122 } // namespace OHOS
123