• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 /* This files contains process dump x86 64 regs module. */
17 
18 #include "dfx_regs.h"
19 
20 #include <csignal>
21 #include <cstdio>
22 #include <cstdlib>
23 #include <securec.h>
24 #include "dfx_define.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 enum UnWindRegIndex {
29     RAX = 0,
30     RDX,
31     RCX,
32     RBX,
33     RSI,
34     RDI,
35     RBP,
36     RSP,
37     R8,
38     R9,
39     R10,
40     R11,
41     R12,
42     R13,
43     R14,
44     R15,
45     RIP
46 };
GetSpecialRegisterName(uintptr_t val) const47 std::string DfxRegsX86_64::GetSpecialRegisterName(uintptr_t val) const
48 {
49     return "";
50 }
51 
DfxRegsX86_64(const ucontext_t & context)52 DfxRegsX86_64::DfxRegsX86_64(const ucontext_t &context)
53 {
54     std::vector<uintptr_t> regs {};
55 
56     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RAX]));
57     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RDX]));
58     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RCX]));
59     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RBX]));
60     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RSI]));
61     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RDI]));
62     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RBP]));
63     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RSP]));
64     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R8]));
65     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R9]));
66     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R10]));
67     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R11]));
68     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R12]));
69     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R13]));
70     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R14]));
71     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R15]));
72     regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RIP]));
73 
74     SetRegs(regs);
75 }
76 
PrintRegs() const77 std::string DfxRegsX86_64::PrintRegs() const
78 {
79     std::string regString = "";
80     char buf[REGS_PRINT_LEN_X86] = {0};
81 
82     regString = regString + "Registers:\n";
83 
84     std::vector<uintptr_t> regs = GetRegsData();
85 
86     PrintFormat(buf, sizeof(buf), "  rax:%016lx rdx:%016lx rcx:%016lx rbx:%016lx\n", \
87                 regs[RAX], regs[RDX], regs[RCX], regs[RBX]);
88 
89     PrintFormat(buf + strlen(buf), sizeof(buf) - strlen(buf), "  rsi:%016lx rdi:%016lx rbp:%016lx rsp:%016lx\n", \
90                 regs[RSI], regs[RDI], regs[RBP], regs[RSP]);
91 
92     PrintFormat(buf + strlen(buf), sizeof(buf) - strlen(buf), "  r8:%016lx r9:%016lx r10:%016lx r11:%016lx\n", \
93                 regs[R8], regs[R9], regs[R10], regs[R11]);
94 
95     PrintFormat(buf + strlen(buf), sizeof(buf) - strlen(buf), \
96                 "  r12:%016lx r13:%016lx r14:%016lx r15:%016lx rip:%016lx\n", \
97                 regs[R12], regs[R13], regs[R14], regs[R15], regs[RIP]);
98 
99     regString = regString + std::string(buf);
100     return regString;
101 }
102 
GetPC() const103 uintptr_t DfxRegsX86_64::GetPC() const
104 {
105     return regsData_[RIP];
106 }
107 
GetLR() const108 uintptr_t DfxRegsX86_64::GetLR() const
109 {
110     return 0;
111 }
112 } // namespace HiviewDFX
113 } // namespace OHOS
114