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(__x86_64__)
17 #include "dfx_regs.h"
18
19 #include <csignal>
20 #include <cstdio>
21 #include <cstdlib>
22 #include <securec.h>
23 #include "dfx_define.h"
24 #include "dfx_regs_define.h"
25 #include "string_util.h"
26
27 namespace OHOS {
28 namespace HiviewDFX {
DfxRegsX86_64(const ucontext_t & context)29 DfxRegsX86_64::DfxRegsX86_64(const ucontext_t &context)
30 {
31 std::vector<uintptr_t> regs {};
32
33 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RAX]));
34 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RDX]));
35 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RCX]));
36 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RBX]));
37 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RSI]));
38 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RDI]));
39 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RBP]));
40 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RSP]));
41 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R8]));
42 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R9]));
43 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R10]));
44 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R11]));
45 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R12]));
46 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R13]));
47 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R14]));
48 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_R15]));
49 regs.push_back((uintptr_t)(context.uc_mcontext.gregs[REG_RIP]));
50
51 SetRegsData(regs);
52 }
53
PrintRegs() const54 std::string DfxRegsX86_64::PrintRegs() const
55 {
56 char buf[REGS_PRINT_LEN_X86] = {0};
57 std::vector<uintptr_t> regs = GetRegsData();
58
59 BufferPrintf(buf, sizeof(buf), " rax:%016lx rdx:%016lx rcx:%016lx rbx:%016lx\n", \
60 regs[REG_X86_64_RAX], regs[REG_X86_64_RDX], regs[REG_X86_64_RCX], regs[REG_X86_64_RBX]);
61
62 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " rsi:%016lx rdi:%016lx rbp:%016lx rsp:%016lx\n", \
63 regs[REG_X86_64_RSI], regs[REG_X86_64_RDI], regs[REG_X86_64_RBP], regs[REG_X86_64_RSP]);
64
65 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), " r8:%016lx r9:%016lx r10:%016lx r11:%016lx\n", \
66 regs[REG_X86_64_R8], regs[REG_X86_64_R9], regs[REG_X86_64_R10], regs[REG_X86_64_R11]);
67
68 BufferPrintf(buf + strlen(buf), sizeof(buf) - strlen(buf), \
69 " r12:%016lx r13:%016lx r14:%016lx r15:%016lx rip:%016lx\n", \
70 regs[REG_X86_64_R12], regs[REG_X86_64_R13], regs[REG_X86_64_R14], regs[REG_X86_64_R15], regs[REG_X86_64_RIP]);
71
72 std::string regString = StringPrintf("Registers:\n%s", buf);
73 return regString;
74 }
75
76 } // namespace HiviewDFX
77 } // namespace OHOS
78 #endif
79