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 #include "dfx_regs.h"
17 #include "dfx_regs_qut.h"
18
19 #include <elf.h>
20 #include <securec.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 namespace {
30 #undef LOG_DOMAIN
31 #undef LOG_TAG
32 #define LOG_DOMAIN 0xD002D11
33 #define LOG_TAG "DfxRegs"
34 }
35 std::vector<uint16_t> DfxRegsQut::qutRegs_ = {};
36
Create()37 std::shared_ptr<DfxRegs> DfxRegs::Create()
38 {
39 #if defined(__arm__)
40 auto dfxregs = std::make_shared<DfxRegsArm>();
41 #elif defined(__aarch64__)
42 auto dfxregs = std::make_shared<DfxRegsArm64>();
43 #elif defined(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
44 auto dfxregs = std::make_shared<DfxRegsRiscv64>();
45 #elif defined(__x86_64__)
46 auto dfxregs = std::make_shared<DfxRegsX86_64>();
47 #else
48 #error "Unsupported architecture"
49 #endif
50 return dfxregs;
51 }
52
CreateFromUcontext(const ucontext_t & context)53 std::shared_ptr<DfxRegs> DfxRegs::CreateFromUcontext(const ucontext_t& context)
54 {
55 auto dfxregs = DfxRegs::Create();
56 dfxregs->SetFromUcontext(context);
57 return dfxregs;
58 }
59
CreateFromRegs(const UnwindMode mode,const uintptr_t * regs,size_t size)60 std::shared_ptr<DfxRegs> DfxRegs::CreateFromRegs(const UnwindMode mode, const uintptr_t* regs,
61 size_t size)
62 {
63 auto dfxregs = DfxRegs::Create();
64 if ((mode == UnwindMode::FRAMEPOINTER_UNWIND && size < FP_MINI_REGS_SIZE) ||
65 (mode == UnwindMode::MINIMAL_UNWIND && size < QUT_MINI_REGS_SIZE)) {
66 LOGE("The number of long groups is too short");
67 return dfxregs;
68 }
69 if (mode == UnwindMode::DWARF_UNWIND) {
70 dfxregs->SetRegsData(regs, REG_LAST);
71 } else if (mode == UnwindMode::FRAMEPOINTER_UNWIND) {
72 dfxregs->SetFromFpMiniRegs(regs, FP_MINI_REGS_SIZE);
73 } else if (mode == UnwindMode::MINIMAL_UNWIND) {
74 dfxregs->SetFromQutMiniRegs(regs, QUT_MINI_REGS_SIZE);
75 }
76 return dfxregs;
77 }
78
CreateRemoteRegs(pid_t pid)79 std::shared_ptr<DfxRegs> DfxRegs::CreateRemoteRegs(pid_t pid)
80 {
81 if (pid <= 0) {
82 return nullptr;
83 }
84 auto dfxregs = DfxRegs::Create();
85 gregset_t regs;
86 struct iovec iov;
87 iov.iov_base = ®s;
88 iov.iov_len = sizeof(regs);
89 // must be attach first
90 if (ptrace(PTRACE_GETREGSET, pid, NT_PRSTATUS, &iov) == -1) {
91 LOGE("Failed to ptrace pid(%d), errno=%d", pid, errno);
92 return nullptr;
93 }
94 #if defined(__x86_64__)
95 dfxregs->regsData_[REG_X86_64_RAX] = regs[RAX];
96 dfxregs->regsData_[REG_X86_64_RDX] = regs[RDX];
97 dfxregs->regsData_[REG_X86_64_RCX] = regs[RCX];
98 dfxregs->regsData_[REG_X86_64_RBX] = regs[RBX];
99 dfxregs->regsData_[REG_X86_64_RSI] = regs[RSI];
100 dfxregs->regsData_[REG_X86_64_RDI] = regs[RDI];
101 dfxregs->regsData_[REG_X86_64_RBP] = regs[RBP];
102 dfxregs->regsData_[REG_X86_64_RSP] = regs[RSP];
103 dfxregs->regsData_[REG_X86_64_R8] = regs[R8];
104 dfxregs->regsData_[REG_X86_64_R9] = regs[R9];
105 dfxregs->regsData_[REG_X86_64_R10] = regs[R10];
106 dfxregs->regsData_[REG_X86_64_R11] = regs[R11];
107 dfxregs->regsData_[REG_X86_64_R12] = regs[R12];
108 dfxregs->regsData_[REG_X86_64_R13] = regs[R13];
109 dfxregs->regsData_[REG_X86_64_R14] = regs[R14];
110 dfxregs->regsData_[REG_X86_64_R15] = regs[R15];
111 dfxregs->regsData_[REG_X86_64_RIP] = regs[RIP];
112 #else
113 if (memcpy_s(dfxregs->regsData_.data(), REG_LAST * sizeof(uintptr_t), ®s, REG_LAST * sizeof(uintptr_t)) != 0) {
114 LOGE("Failed to memcpy regs data, errno=%d", errno);
115 return nullptr;
116 }
117 #endif
118 return dfxregs;
119 }
120
GetRegsData() const121 std::vector<uintptr_t> DfxRegs::GetRegsData() const
122 {
123 return regsData_;
124 }
125
SetRegsData(const std::vector<uintptr_t> & regs)126 void DfxRegs::SetRegsData(const std::vector<uintptr_t>& regs)
127 {
128 regsData_ = regs;
129 }
130
SetRegsData(const uintptr_t * regs,const size_t size)131 void DfxRegs::SetRegsData(const uintptr_t* regs, const size_t size)
132 {
133 size_t cpySize = (size > RegsSize()) ? RegsSize() : size;
134 if (memcpy_s(RawData(), cpySize * sizeof(uintptr_t), regs, cpySize * sizeof(uintptr_t)) != 0) {
135 LOGE("Failed to set regs data, errno=%d", errno);
136 }
137 }
138
GetReg(size_t idx)139 uintptr_t* DfxRegs::GetReg(size_t idx)
140 {
141 if (idx >= REG_LAST) {
142 return nullptr;
143 }
144 return &(regsData_[idx]);
145 }
146
SetReg(const int idx,const uintptr_t * val)147 void DfxRegs::SetReg(const int idx, const uintptr_t* val)
148 {
149 if (idx >= REG_LAST) {
150 return;
151 }
152 regsData_[idx] = *val;
153 }
154
GetSpecialRegs(uintptr_t & fp,uintptr_t & lr,uintptr_t & sp,uintptr_t & pc) const155 void DfxRegs::GetSpecialRegs(uintptr_t& fp, uintptr_t& lr, uintptr_t& sp, uintptr_t& pc) const
156 {
157 #if defined(__arm__) || defined(__aarch64__) || defined(__riscv)
158 fp = regsData_[REG_FP];
159 lr = regsData_[REG_LR];
160 #endif
161 sp = regsData_[REG_SP];
162 pc = regsData_[REG_PC];
163 }
164
SetSpecialRegs(uintptr_t fp,uintptr_t lr,uintptr_t sp,uintptr_t pc)165 void DfxRegs::SetSpecialRegs(uintptr_t fp, uintptr_t lr, uintptr_t sp, uintptr_t pc)
166 {
167 #if defined(__arm__) || defined(__aarch64__) || defined(__riscv)
168 regsData_[REG_FP] = fp;
169 regsData_[REG_LR] = lr;
170 #endif
171 regsData_[REG_SP] = sp;
172 regsData_[REG_PC] = pc;
173 }
174
GetSp() const175 uintptr_t DfxRegs::GetSp() const
176 {
177 return regsData_[REG_SP];
178 }
179
SetSp(uintptr_t sp)180 void DfxRegs::SetSp(uintptr_t sp)
181 {
182 regsData_[REG_SP] = sp;
183 }
184
GetPc() const185 uintptr_t DfxRegs::GetPc() const
186 {
187 return regsData_[REG_PC];
188 }
189
SetPc(uintptr_t pc)190 void DfxRegs::SetPc(uintptr_t pc)
191 {
192 regsData_[REG_PC] = pc;
193 }
194
GetFp() const195 uintptr_t DfxRegs::GetFp() const
196 {
197 #if defined(__arm__) || defined(__aarch64__)
198 return regsData_[REG_FP];
199 #else
200 return 0;
201 #endif
202 }
203
SetFp(uintptr_t fp)204 void DfxRegs::SetFp(uintptr_t fp)
205 {
206 #if defined(__arm__) || defined(__aarch64__) || defined(__riscv)
207 regsData_[REG_FP] = fp;
208 #endif
209 }
210
GetSpecialRegsName(uintptr_t val) const211 std::string DfxRegs::GetSpecialRegsName(uintptr_t val) const
212 {
213 uintptr_t fp = 0, lr = 0, sp = 0, pc = 0;
214 GetSpecialRegs(fp, lr, sp, pc);
215 if (val == pc) {
216 return "pc";
217 } else if (val == lr) {
218 return "lr";
219 } else if (val == sp) {
220 return "sp";
221 } else if (val == fp) {
222 return "fp";
223 }
224 return "";
225 }
226
PrintSpecialRegs() const227 std::string DfxRegs::PrintSpecialRegs() const
228 {
229 uintptr_t fp = 0, lr = 0, sp = 0, pc = 0;
230 GetSpecialRegs(fp, lr, sp, pc);
231 std::string regsStr;
232 #ifdef __LP64__
233 regsStr = StringPrintf("fp:%016lx sp:%016lx lr:%016lx pc:%016lx\n", fp, sp, lr, pc);
234 #else
235 regsStr = StringPrintf("fp:%08x sp:%08x lr:%08x pc:%08x\n", fp, sp, lr, pc);
236 #endif
237 return regsStr;
238 }
239 } // namespace HiviewDFX
240 } // namespace OHOS
241