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()25 ArchType 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(__riscv) && defined(__riscv_xlen) && __riscv_xlen == 64
33     curArch = ArchType::ARCH_RISCV64;
34 #elif defined(__i386__)
35     curArch = ArchType::ARCH_X86;
36 #elif defined(__x86_64__)
37     curArch = ArchType::ARCH_X86_64;
38 #else
39 #error "Unsupported architecture"
40 #endif
41     return curArch;
42 }
43 
GetArchFromUname(const std::string & machine)44 ArchType GetArchFromUname(const std::string& machine)
45 {
46     if (StartsWith(machine, "arm")) {
47         if (machine == "armv8l") {
48             return ArchType::ARCH_ARM64;
49         }
50         return ArchType::ARCH_ARM;
51     } else if (machine == "aarch64") {
52         return ArchType::ARCH_ARM64;
53     } else if (machine == "riscv64") {
54         return ArchType::ARCH_RISCV64;
55     } else if (machine == "x86_64") {
56         return ArchType::ARCH_X86_64;
57     } else if (machine == "x86" || machine == "i686") {
58         return ArchType::ARCH_X86;
59     } else {
60         return ArchType::ARCH_UNKNOWN;
61     }
62 }
63 
GetArchName(ArchType arch)64 const std::string GetArchName(ArchType arch)
65 {
66     switch (arch) {
67         case ArchType::ARCH_X86:
68             return "X86_32";
69         case ArchType::ARCH_X86_64:
70             return "X86_64";
71         case ArchType::ARCH_ARM:
72             return "ARM";
73         case ArchType::ARCH_ARM64:
74             return "ARM64";
75         case ArchType::ARCH_RISCV64:
76             return "RISCV64";
77         default:
78             return "Unsupport";
79     }
80 }
81 }   // namespace HiviewDFX
82 }   // namespace OHOS
83