1 /*
2 * Copyright (c) 2021-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
16 #if defined(__aarch64__)
17 #include "dfx_regs.h"
18
19 #include <securec.h>
20 #include "dfx_define.h"
21 #include "dfx_log.h"
22 #include "dfx_regs_define.h"
23 #include "string_util.h"
24
25 namespace OHOS {
26 namespace HiviewDFX {
DfxRegsArm64(const ucontext_t & context)27 DfxRegsArm64::DfxRegsArm64(const ucontext_t &context)
28 {
29 std::vector<uintptr_t> regs {};
30 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X0])); // 0:x0
31 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X1])); // 1:x1
32 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X2])); // 2:x2
33 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X3])); // 3:x3
34 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X4])); // 4:x4
35 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X5])); // 5:x5
36 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X6])); // 6:x6
37 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X7])); // 7:x7
38 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X8])); // 8:x8
39 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X9])); // 9:x9
40 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X10])); // 10:x10
41 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X11])); // 11:x11
42 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X12])); // 12:x12
43 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X13])); // 13:x13
44 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X14])); // 14:x14
45 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X15])); // 15:x15
46 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X16])); // 16:x16
47 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X17])); // 17:x17
48 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X18])); // 18:x18
49 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X19])); // 19:x19
50 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X20])); // 20:x20
51 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X21])); // 21:x21
52 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X22])); // 22:x22
53 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X23])); // 23:x23
54 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X24])); // 24:x24
55 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X25])); // 25:x25
56 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X26])); // 26:x26
57 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X27])); // 27:x27
58 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X28])); // 28:x28
59 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X29])); // 29:x29
60 regs.push_back(uintptr_t(context.uc_mcontext.regs[REG_AARCH64_X30])); // 30:lr
61 regs.push_back(uintptr_t(context.uc_mcontext.sp)); // 31:sp
62 regs.push_back(uintptr_t(context.uc_mcontext.pc)); // 32:pc
63
64 SetRegsData(regs);
65 }
66
PrintRegs() const67 std::string DfxRegsArm64::PrintRegs() const
68 {
69 char buf[REGS_PRINT_LEN_ARM64] = {0};
70 std::vector<uintptr_t> regs = GetRegsData();
71
72 BufferPrintf(buf, sizeof(buf), "x0:%016lx x1:%016lx x2:%016lx x3:%016lx\n", \
73 regs[REG_AARCH64_X0], regs[REG_AARCH64_X1], regs[REG_AARCH64_X2], regs[REG_AARCH64_X3]);
74
75 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x4:%016lx x5:%016lx x6:%016lx x7:%016lx\n", \
76 regs[REG_AARCH64_X4], regs[REG_AARCH64_X5], regs[REG_AARCH64_X6], regs[REG_AARCH64_X7]);
77
78 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x8:%016lx x9:%016lx x10:%016lx x11:%016lx\n", \
79 regs[REG_AARCH64_X8], regs[REG_AARCH64_X9], regs[REG_AARCH64_X10], regs[REG_AARCH64_X11]);
80
81 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x12:%016lx x13:%016lx x14:%016lx x15:%016lx\n", \
82 regs[REG_AARCH64_X12], regs[REG_AARCH64_X13], regs[REG_AARCH64_X14], regs[REG_AARCH64_X15]);
83
84 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x16:%016lx x17:%016lx x18:%016lx x19:%016lx\n", \
85 regs[REG_AARCH64_X16], regs[REG_AARCH64_X17], regs[REG_AARCH64_X18], regs[REG_AARCH64_X19]);
86
87 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x20:%016lx x21:%016lx x22:%016lx x23:%016lx\n", \
88 regs[REG_AARCH64_X20], regs[REG_AARCH64_X21], regs[REG_AARCH64_X22], regs[REG_AARCH64_X23]);
89
90 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x24:%016lx x25:%016lx x26:%016lx x27:%016lx\n", \
91 regs[REG_AARCH64_X24], regs[REG_AARCH64_X25], regs[REG_AARCH64_X26], regs[REG_AARCH64_X27]);
92
93 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x28:%016lx x29:%016lx\n", \
94 regs[REG_AARCH64_X28], regs[REG_AARCH64_X29]);
95
96 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "lr:%016lx sp:%016lx pc:%016lx\n", \
97 regs[REG_AARCH64_X30], regs[REG_AARCH64_SP], regs[REG_AARCH64_PC]);
98
99 std::string regString = StringPrintf("Registers:\n%s", buf);
100 return regString;
101 }
102
103 } // namespace HiviewDFX
104 } // namespace OHOS
105 #endif
106