/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "dfx_regs.h" #include #include #include "dfx_define.h" #include "dfx_log.h" #include "dfx_regs_define.h" #include "string_util.h" #include "unwinder_define.h" namespace OHOS { namespace HiviewDFX { std::shared_ptr DfxRegs::Create() { std::shared_ptr dfxregs; #if defined(__arm__) dfxregs = std::make_shared(); #elif defined(__aarch64__) dfxregs = std::make_shared(); #elif defined(__x86_64__) dfxregs = std::make_shared(); #else #error "Unsupported architecture" #endif return dfxregs; } std::shared_ptr DfxRegs::CreateFromContext(const ucontext_t &context) { std::shared_ptr dfxregs; #if defined(__arm__) dfxregs = std::make_shared(context); #elif defined(__aarch64__) dfxregs = std::make_shared(context); #elif defined(__x86_64__) dfxregs = std::make_shared(context); #else #error "Unsupported architecture" #endif return dfxregs; } std::vector DfxRegs::GetRegsData() const { return regsData_; } void DfxRegs::SetRegsData(const std::vector& regs) { regsData_ = regs; #if defined(__arm__) fp_ = regs[REG_ARM_R11]; sp_ = regs[REG_ARM_R13]; lr_ = regs[REG_ARM_R14]; pc_ = regs[REG_ARM_R15]; #elif defined(__aarch64__) fp_ = regs[REG_AARCH64_FP]; sp_ = regs[REG_AARCH64_SP]; lr_ = regs[REG_AARCH64_X30]; pc_ = regs[REG_AARCH64_PC]; #elif defined(__x86_64__) sp_ = regs[REG_X86_64_SP]; pc_ = regs[REG_X86_64_PC]; #else #error "Unsupported architecture" #endif DFXLOG_INFO("%s", PrintSpecialRegs().c_str()); } std::string DfxRegs::GetSpecialRegisterName(uintptr_t val) const { if (val == pc_) { return "pc"; } else if (val == lr_) { return "lr"; } else if (val == sp_) { return "sp"; } else if (val == fp_) { return "fp"; } return ""; } std::string DfxRegs::PrintSpecialRegs() const { char buf[REGS_PRINT_LEN_SPECIAL] = {0}; #ifdef __LP64__ BufferPrintf(buf, sizeof(buf), "fp:%016lx sp:%016lx lr:%016lx pc:%016lx\n", fp_, sp_, lr_, pc_); #else BufferPrintf(buf, sizeof(buf), "fp:%08x sp:%08x lr:%08x pc:%08x\n", fp_, sp_, lr_, pc_); #endif return std::string(buf); } } // namespace HiviewDFX } // namespace OHOS