Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf env: Adopt perf_env__arch() from the annotate code

And use it in the libunwind case, with both passing a valid perf_env to
extract the arch to be normalized from and passing NULL with the same
semantic as in the annotate code: to get it from uname() uts.machine.

Now the code to generate per arch errno translation tables (int/string)
can use it to decode perf.data files recorded in a different arch than
that where 'perf trace' (or any other analysis tool) runs.

Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
Cc: Jiri Olsa <jolsa@kernel.org>
Cc: Michael Petlan <mpetlan@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Thomas Richter <tmricht@linux.vnet.ibm.com>
Cc: Wang Nan <wangnan0@huawei.com>
Link: https://lkml.kernel.org/n/tip-p2epffgash69w38kvj3ntpc9@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

+54 -60
+3 -41
tools/perf/arch/common.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 #include <stdio.h> 3 - #include <sys/utsname.h> 4 3 #include "common.h" 4 + #include "../util/env.h" 5 5 #include "../util/util.h" 6 6 #include "../util/debug.h" 7 - 8 - #include "sane_ctype.h" 9 7 10 8 const char *const arm_triplets[] = { 11 9 "arm-eabi-", ··· 118 120 return -1; 119 121 } 120 122 121 - /* 122 - * Return architecture name in a normalized form. 123 - * The conversion logic comes from the Makefile. 124 - */ 125 - const char *normalize_arch(char *arch) 126 - { 127 - if (!strcmp(arch, "x86_64")) 128 - return "x86"; 129 - if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6') 130 - return "x86"; 131 - if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5)) 132 - return "sparc"; 133 - if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64")) 134 - return "arm64"; 135 - if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110")) 136 - return "arm"; 137 - if (!strncmp(arch, "s390", 4)) 138 - return "s390"; 139 - if (!strncmp(arch, "parisc", 6)) 140 - return "parisc"; 141 - if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3)) 142 - return "powerpc"; 143 - if (!strncmp(arch, "mips", 4)) 144 - return "mips"; 145 - if (!strncmp(arch, "sh", 2) && isdigit(arch[2])) 146 - return "sh"; 147 - 148 - return arch; 149 - } 150 - 151 123 static int perf_env__lookup_binutils_path(struct perf_env *env, 152 124 const char *name, const char **path) 153 125 { 154 126 int idx; 155 - const char *arch, *cross_env; 156 - struct utsname uts; 127 + const char *arch = perf_env__arch(env), *cross_env; 157 128 const char *const *path_list; 158 129 char *buf = NULL; 159 - 160 - arch = normalize_arch(env->arch); 161 - 162 - if (uname(&uts) < 0) 163 - goto out; 164 130 165 131 /* 166 132 * We don't need to try to find objdump path for native system. 167 133 * Just use default binutils path (e.g.: "objdump"). 168 134 */ 169 - if (!strcmp(normalize_arch(uts.machine), arch)) 135 + if (!strcmp(perf_env__arch(NULL), arch)) 170 136 goto out; 171 137 172 138 cross_env = getenv("CROSS_COMPILE");
-1
tools/perf/arch/common.h
··· 7 7 extern const char *objdump_path; 8 8 9 9 int perf_env__lookup_objdump(struct perf_env *env); 10 - const char *normalize_arch(char *arch); 11 10 12 11 #endif /* ARCH_PERF_COMMON_H */
-16
tools/perf/util/annotate.c
··· 26 26 #include <pthread.h> 27 27 #include <linux/bitops.h> 28 28 #include <linux/kernel.h> 29 - #include <sys/utsname.h> 30 29 31 30 #include "sane_ctype.h" 32 31 ··· 1417 1418 1418 1419 free(build_id_path); 1419 1420 return 0; 1420 - } 1421 - 1422 - static const char *perf_env__arch(struct perf_env *env) 1423 - { 1424 - struct utsname uts; 1425 - char *arch_name; 1426 - 1427 - if (!env) { /* Assume local operation */ 1428 - if (uname(&uts) < 0) 1429 - return NULL; 1430 - arch_name = uts.machine; 1431 - } else 1432 - arch_name = env->arch; 1433 - 1434 - return normalize_arch(arch_name); 1435 1421 } 1436 1422 1437 1423 static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
+47
tools/perf/util/env.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 #include "cpumap.h" 3 3 #include "env.h" 4 + #include "sane_ctype.h" 4 5 #include "util.h" 5 6 #include <errno.h> 7 + #include <sys/utsname.h> 6 8 7 9 struct perf_env perf_env; 8 10 ··· 94 92 free(cache->type); 95 93 free(cache->map); 96 94 free(cache->size); 95 + } 96 + 97 + /* 98 + * Return architecture name in a normalized form. 99 + * The conversion logic comes from the Makefile. 100 + */ 101 + static const char *normalize_arch(char *arch) 102 + { 103 + if (!strcmp(arch, "x86_64")) 104 + return "x86"; 105 + if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6') 106 + return "x86"; 107 + if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5)) 108 + return "sparc"; 109 + if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64")) 110 + return "arm64"; 111 + if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110")) 112 + return "arm"; 113 + if (!strncmp(arch, "s390", 4)) 114 + return "s390"; 115 + if (!strncmp(arch, "parisc", 6)) 116 + return "parisc"; 117 + if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3)) 118 + return "powerpc"; 119 + if (!strncmp(arch, "mips", 4)) 120 + return "mips"; 121 + if (!strncmp(arch, "sh", 2) && isdigit(arch[2])) 122 + return "sh"; 123 + 124 + return arch; 125 + } 126 + 127 + const char *perf_env__arch(struct perf_env *env) 128 + { 129 + struct utsname uts; 130 + char *arch_name; 131 + 132 + if (!env) { /* Assume local operation */ 133 + if (uname(&uts) < 0) 134 + return NULL; 135 + arch_name = uts.machine; 136 + } else 137 + arch_name = env->arch; 138 + 139 + return normalize_arch(arch_name); 97 140 }
+2
tools/perf/util/env.h
··· 65 65 int perf_env__read_cpu_topology_map(struct perf_env *env); 66 66 67 67 void cpu_cache_level__free(struct cpu_cache_level *cache); 68 + 69 + const char *perf_env__arch(struct perf_env *env); 68 70 #endif /* __PERF_ENV_H */
+2 -2
tools/perf/util/unwind-libunwind.c
··· 3 3 #include "thread.h" 4 4 #include "session.h" 5 5 #include "debug.h" 6 - #include "arch/common.h" 6 + #include "env.h" 7 7 8 8 struct unwind_libunwind_ops __weak *local_unwind_libunwind_ops; 9 9 struct unwind_libunwind_ops __weak *x86_32_unwind_libunwind_ops; ··· 39 39 if (dso_type == DSO__TYPE_UNKNOWN) 40 40 return 0; 41 41 42 - arch = normalize_arch(thread->mg->machine->env->arch); 42 + arch = perf_env__arch(thread->mg->machine->env); 43 43 44 44 if (!strcmp(arch, "x86")) { 45 45 if (dso_type != DSO__TYPE_64BIT)