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(__arm__)
17 #include "dfx_regs.h"
18
19 #include <cstdio>
20 #include <cstdlib>
21 #include <securec.h>
22 #include "dfx_define.h"
23 #include "dfx_log.h"
24 #include "dfx_regs_define.h"
25 #include "string_util.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
DfxRegsArm(const ucontext_t & context)29 DfxRegsArm::DfxRegsArm(const ucontext_t& context)
30 {
31 std::vector<uintptr_t> regs {};
32
33 regs.push_back(uintptr_t(context.uc_mcontext.arm_r0)); // 0:r0
34 regs.push_back(uintptr_t(context.uc_mcontext.arm_r1)); // 1:r1
35 regs.push_back(uintptr_t(context.uc_mcontext.arm_r2)); // 2:r2
36 regs.push_back(uintptr_t(context.uc_mcontext.arm_r3)); // 3:r3
37 regs.push_back(uintptr_t(context.uc_mcontext.arm_r4)); // 4:r4
38 regs.push_back(uintptr_t(context.uc_mcontext.arm_r5)); // 5:r5
39 regs.push_back(uintptr_t(context.uc_mcontext.arm_r6)); // 6:r6
40 regs.push_back(uintptr_t(context.uc_mcontext.arm_r7)); // 7:r7
41 regs.push_back(uintptr_t(context.uc_mcontext.arm_r8)); // 8:r8
42 regs.push_back(uintptr_t(context.uc_mcontext.arm_r9)); // 9:r9
43 regs.push_back(uintptr_t(context.uc_mcontext.arm_r10)); // 10:r10
44 regs.push_back(uintptr_t(context.uc_mcontext.arm_fp)); // 11:fp
45 regs.push_back(uintptr_t(context.uc_mcontext.arm_ip)); // 12:ip
46 regs.push_back(uintptr_t(context.uc_mcontext.arm_sp)); // 13:sp
47 regs.push_back(uintptr_t(context.uc_mcontext.arm_lr)); // 14:lr
48 regs.push_back(uintptr_t(context.uc_mcontext.arm_pc)); // 15:pc
49
50 SetRegsData(regs);
51 }
52
PrintRegs() const53 std::string DfxRegsArm::PrintRegs() const
54 {
55 char buf[REGS_PRINT_LEN_ARM] = {0};
56 std::vector<uintptr_t> regs = GetRegsData();
57
58 BufferPrintf(buf, sizeof(buf), "r0:%08x r1:%08x r2:%08x r3:%08x\n", \
59 regs[REG_ARM_R0], regs[REG_ARM_R1], regs[REG_ARM_R2], regs[REG_ARM_R3]);
60
61 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "r4:%08x r5:%08x r6:%08x r7:%08x\n", \
62 regs[REG_ARM_R4], regs[REG_ARM_R5], regs[REG_ARM_R6], regs[REG_ARM_R7]);
63
64 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "r8:%08x r9:%08x r10:%08x\n", \
65 regs[REG_ARM_R8], regs[REG_ARM_R9], regs[REG_ARM_R10]);
66
67 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), "fp:%08x ip:%08x sp:%08x lr:%08x pc:%08x\n", \
68 regs[REG_ARM_R11], regs[REG_ARM_R12], regs[REG_ARM_R13], regs[REG_ARM_R14], regs[REG_ARM_R15]);
69
70 std::string regString = StringPrintf("Registers:\n%s", buf);
71 return regString;
72 }
73
74 } // namespace HiviewDFX
75 } // namespace OHOS
76 #endif
77