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 <stdint.h>
21 #include <sys/ptrace.h>
22 #include <sys/uio.h>
23 #include "dfx_define.h"
24 #include "dfx_log.h"
25 #include "string_printf.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
29
30 constexpr int VREGS_ARRAY_LEN = 32;
31
32 struct Aarch64CtxHead {
33 uint32_t magic;
34 uint32_t size;
35 };
36
37 struct Aarch64FpsimdContext {
38 struct Aarch64CtxHead head;
39 uint32_t fpsr;
40 uint32_t fpcr;
41 __uint128_t vregs[VREGS_ARRAY_LEN];
42 };
43
SetFromUcontext(const ucontext_t & context)44 void DfxRegsArm64::SetFromUcontext(const ucontext_t &context)
45 {
46 if (regsData_.size() < REG_LAST) {
47 return;
48 }
49 for (uint16_t index = REG_AARCH64_X0; index <= REG_AARCH64_X30; index++) {
50 regsData_[index] = static_cast<uintptr_t>(context.uc_mcontext.regs[index]);
51 }
52 regsData_[REG_AARCH64_X31] = static_cast<uintptr_t>(context.uc_mcontext.sp); // sp register
53 regsData_[REG_AARCH64_PC] = static_cast<uintptr_t>(context.uc_mcontext.pc); // pc register
54 regsData_[REG_AARCH64_PSTATE] = static_cast<uintptr_t>(context.uc_mcontext.pstate);
55 uint8_t* pMctxRes = reinterpret_cast<uint8_t*>(const_cast<long double*>(&context.uc_mcontext.__reserved[0]));
56 regsData_[REG_AARCH64_ESR] = *reinterpret_cast<uintptr_t*>(pMctxRes + sizeof(Aarch64FpsimdContext) +
57 sizeof(Aarch64CtxHead));
58 }
59
SetFromFpMiniRegs(const uintptr_t * regs,const size_t size)60 void DfxRegsArm64::SetFromFpMiniRegs(const uintptr_t* regs, const size_t size)
61 {
62 if (regs == nullptr || size < FP_MINI_REGS_SIZE) {
63 return;
64 }
65 regsData_[REG_FP] = regs[0]; // 0 : fp offset
66 regsData_[REG_LR] = regs[1]; // 1 : lr offset
67 regsData_[REG_SP] = regs[2]; // 2 : sp offset
68 regsData_[REG_PC] = regs[3]; // 3 : pc offset
69 }
70
SetFromQutMiniRegs(const uintptr_t * regs,const size_t size)71 void DfxRegsArm64::SetFromQutMiniRegs(const uintptr_t* regs, const size_t size)
72 {
73 if (regs == nullptr || size < QUT_MINI_REGS_SIZE) {
74 return;
75 }
76 regsData_[REG_AARCH64_X20] = regs[1]; // 1 : X20 offset
77 regsData_[REG_AARCH64_X28] = regs[2]; // 2 : X28 offset
78 regsData_[REG_FP] = regs[3]; // 3 : fp offset
79 regsData_[REG_SP] = regs[4]; // 4 : sp offset
80 regsData_[REG_PC] = regs[5]; // 5 : pc offset
81 regsData_[REG_LR] = regs[6]; // 6 : lr offset
82 }
83
SetPcFromReturnAddress(MAYBE_UNUSED std::shared_ptr<DfxMemory> memory)84 bool DfxRegsArm64::SetPcFromReturnAddress(MAYBE_UNUSED std::shared_ptr<DfxMemory> memory)
85 {
86 uintptr_t lr = regsData_[REG_LR];
87 if (regsData_[REG_PC] == lr) {
88 return false;
89 }
90 regsData_[REG_PC] = lr;
91 return true;
92 }
93
PrintRegs() const94 std::string DfxRegsArm64::PrintRegs() const
95 {
96 char buf[REGS_PRINT_LEN] = {0};
97 auto regs = GetRegsData();
98
99 BufferPrintf(buf, sizeof(buf), "x0:%016lx x1:%016lx x2:%016lx x3:%016lx\n", \
100 regs[REG_AARCH64_X0], regs[REG_AARCH64_X1], regs[REG_AARCH64_X2], regs[REG_AARCH64_X3]);
101
102 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x4:%016lx x5:%016lx x6:%016lx x7:%016lx\n", \
103 regs[REG_AARCH64_X4], regs[REG_AARCH64_X5], regs[REG_AARCH64_X6], regs[REG_AARCH64_X7]);
104
105 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x8:%016lx x9:%016lx x10:%016lx x11:%016lx\n", \
106 regs[REG_AARCH64_X8], regs[REG_AARCH64_X9], regs[REG_AARCH64_X10], regs[REG_AARCH64_X11]);
107
108 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x12:%016lx x13:%016lx x14:%016lx x15:%016lx\n", \
109 regs[REG_AARCH64_X12], regs[REG_AARCH64_X13], regs[REG_AARCH64_X14], regs[REG_AARCH64_X15]);
110
111 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x16:%016lx x17:%016lx x18:%016lx x19:%016lx\n", \
112 regs[REG_AARCH64_X16], regs[REG_AARCH64_X17], regs[REG_AARCH64_X18], regs[REG_AARCH64_X19]);
113
114 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x20:%016lx x21:%016lx x22:%016lx x23:%016lx\n", \
115 regs[REG_AARCH64_X20], regs[REG_AARCH64_X21], regs[REG_AARCH64_X22], regs[REG_AARCH64_X23]);
116
117 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x24:%016lx x25:%016lx x26:%016lx x27:%016lx\n", \
118 regs[REG_AARCH64_X24], regs[REG_AARCH64_X25], regs[REG_AARCH64_X26], regs[REG_AARCH64_X27]);
119
120 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "x28:%016lx x29:%016lx\n", \
121 regs[REG_AARCH64_X28], regs[REG_AARCH64_X29]);
122
123 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "lr:%016lx sp:%016lx pc:%016lx\n", \
124 regs[REG_AARCH64_X30], regs[REG_SP], regs[REG_PC]);
125
126 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "pstate:%016lx esr:%016lx\n", \
127 regs[REG_AARCH64_PSTATE], regs[REG_AARCH64_ESR]);
128 std::string regString = StringPrintf("Registers:\n%s", buf);
129 return regString;
130 }
131
StepIfSignalFrame(uintptr_t pc,std::shared_ptr<DfxMemory> memory)132 bool DfxRegsArm64::StepIfSignalFrame(uintptr_t pc, std::shared_ptr<DfxMemory> memory)
133 {
134 if (memory == nullptr) {
135 return false;
136 }
137 uint64_t data;
138 if (!memory->Read<uint64_t>(pc, &data, false)) {
139 return false;
140 }
141 DFXLOGU("data: %{public}lx", data);
142
143 // Look for the kernel sigreturn function.
144 // __kernel_rt_sigreturn:
145 // 0xd2801168 mov x8, #0x8b
146 // 0xd4000001 svc #0x0
147 if (data != 0xd4000001d2801168ULL) {
148 return false;
149 }
150
151 // SP + sizeof(siginfo_t) + uc_mcontext offset + X0 offset.
152 uintptr_t scAddr = regsData_[REG_SP] + sizeof(siginfo_t) + 0xb0 + 0x08;
153 DFXLOGU("scAddr: %{public}lx", scAddr);
154 memory->Read(scAddr, regsData_.data(), sizeof(uint64_t) * REG_LAST, false);
155 return true;
156 }
157 } // namespace HiviewDFX
158 } // namespace OHOS
159 #endif
160