• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "dfx_regs.h"
17 
18 #include <cstdio>
19 #include <cstdlib>
20 #include "dfx_define.h"
21 #include "dfx_log.h"
22 #include "dfx_regs_define.h"
23 #include "string_util.h"
24 #include "unwinder_define.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
Create()28 std::shared_ptr<DfxRegs> DfxRegs::Create()
29 {
30     std::shared_ptr<DfxRegs> dfxregs;
31 #if defined(__arm__)
32     dfxregs = std::make_shared<DfxRegsArm>();
33 #elif defined(__aarch64__)
34     dfxregs = std::make_shared<DfxRegsArm64>();
35 #elif defined(__x86_64__)
36     dfxregs = std::make_shared<DfxRegsX86_64>();
37 #else
38 #error "Unsupported architecture"
39 #endif
40     return dfxregs;
41 }
42 
CreateFromContext(const ucontext_t & context)43 std::shared_ptr<DfxRegs> DfxRegs::CreateFromContext(const ucontext_t &context)
44 {
45     std::shared_ptr<DfxRegs> dfxregs;
46 #if defined(__arm__)
47     dfxregs = std::make_shared<DfxRegsArm>(context);
48 #elif defined(__aarch64__)
49     dfxregs = std::make_shared<DfxRegsArm64>(context);
50 #elif defined(__x86_64__)
51     dfxregs = std::make_shared<DfxRegsX86_64>(context);
52 #else
53 #error "Unsupported architecture"
54 #endif
55     return dfxregs;
56 }
57 
GetRegsData() const58 std::vector<uintptr_t> DfxRegs::GetRegsData() const
59 {
60     return regsData_;
61 }
62 
SetRegsData(const std::vector<uintptr_t> & regs)63 void DfxRegs::SetRegsData(const std::vector<uintptr_t>& regs)
64 {
65     regsData_ = regs;
66 #if defined(__arm__)
67     fp_ = regs[REG_ARM_R11];
68     sp_ = regs[REG_ARM_R13];
69     lr_ = regs[REG_ARM_R14];
70     pc_ = regs[REG_ARM_R15];
71 #elif defined(__aarch64__)
72     fp_ = regs[REG_AARCH64_FP];
73     sp_ = regs[REG_AARCH64_SP];
74     lr_ = regs[REG_AARCH64_X30];
75     pc_ = regs[REG_AARCH64_PC];
76 #elif defined(__x86_64__)
77     sp_ = regs[REG_X86_64_SP];
78     pc_ = regs[REG_X86_64_PC];
79 #else
80 #error "Unsupported architecture"
81 #endif
82     DFXLOG_INFO("%s", PrintSpecialRegs().c_str());
83 }
84 
GetSpecialRegisterName(uintptr_t val) const85 std::string DfxRegs::GetSpecialRegisterName(uintptr_t val) const
86 {
87     if (val == pc_) {
88         return "pc";
89     } else if (val == lr_) {
90         return "lr";
91     } else if (val == sp_) {
92         return "sp";
93     } else if (val == fp_) {
94         return "fp";
95     }
96     return "";
97 }
98 
PrintSpecialRegs() const99 std::string DfxRegs::PrintSpecialRegs() const
100 {
101     char buf[REGS_PRINT_LEN_SPECIAL] = {0};
102 #ifdef __LP64__
103     BufferPrintf(buf, sizeof(buf), "fp:%016lx sp:%016lx lr:%016lx pc:%016lx\n", fp_, sp_, lr_, pc_);
104 #else
105     BufferPrintf(buf, sizeof(buf), "fp:%08x sp:%08x lr:%08x pc:%08x\n", fp_, sp_, lr_, pc_);
106 #endif
107     return std::string(buf);
108 }
109 } // namespace HiviewDFX
110 } // namespace OHOS
111