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 "arch_util.h" 17 18 #include <securec.h> 19 #include "dfx_define.h" 20 #include "dfx_log.h" 21 #include "string_util.h" 22 23 namespace OHOS { 24 namespace HiviewDFX { GetCurrentArch()25ArchType GetCurrentArch() 26 { 27 ArchType curArch = ArchType::ARCH_UNKNOWN; 28 #if defined(__arm__) 29 curArch = ArchType::ARCH_ARM; 30 #elif defined(__aarch64__) 31 curArch = ArchType::ARCH_ARM64; 32 #elif defined(__i386__) 33 curArch = ArchType::ARCH_X86; 34 #elif defined(__x86_64__) 35 curArch = ArchType::ARCH_X86_64; 36 #else 37 #error "Unsupported architecture" 38 #endif 39 return curArch; 40 } 41 GetArchFromUname(const std::string & machine)42ArchType GetArchFromUname(const std::string& machine) 43 { 44 if (StartsWith(machine, "arm")) { 45 if (machine == "armv8l") { 46 return ArchType::ARCH_ARM64; 47 } 48 return ArchType::ARCH_ARM; 49 } else if (machine == "aarch64") { 50 return ArchType::ARCH_ARM64; 51 } else if (machine == "x86_64") { 52 return ArchType::ARCH_X86_64; 53 } else if (machine == "x86" || machine == "i686") { 54 return ArchType::ARCH_X86; 55 } else { 56 return ArchType::ARCH_UNKNOWN; 57 } 58 } 59 GetArchName(ArchType arch)60const std::string GetArchName(ArchType arch) 61 { 62 switch (arch) { 63 case ArchType::ARCH_X86: 64 return "X86_32"; 65 case ArchType::ARCH_X86_64: 66 return "X86_64"; 67 case ArchType::ARCH_ARM: 68 return "ARM"; 69 case ArchType::ARCH_ARM64: 70 return "ARM64"; 71 default: 72 return "Unsupport"; 73 } 74 } 75 } // namespace HiviewDFX 76 } // namespace OHOS 77