2 #include <sys/utsname.h>
4 #include "../util/util.h"
5 #include "../util/debug.h"
7 #include "sane_ctype.h"
9 const char *const arm_triplets[] = {
11 "arm-linux-androideabi-",
13 "arm-unknown-linux-gnu-",
14 "arm-unknown-linux-gnueabi-",
16 "arm-linux-gnueabihf-",
21 const char *const arm64_triplets[] = {
22 "aarch64-linux-android-",
27 const char *const powerpc_triplets[] = {
28 "powerpc-unknown-linux-gnu-",
30 "powerpc64-unknown-linux-gnu-",
31 "powerpc64-linux-gnu-",
32 "powerpc64le-linux-gnu-",
36 const char *const s390_triplets[] = {
42 const char *const sh_triplets[] = {
43 "sh-unknown-linux-gnu-",
44 "sh64-unknown-linux-gnu-",
50 const char *const sparc_triplets[] = {
51 "sparc-unknown-linux-gnu-",
52 "sparc64-unknown-linux-gnu-",
57 const char *const x86_triplets[] = {
58 "x86_64-pc-linux-gnu-",
59 "x86_64-unknown-linux-gnu-",
64 "i686-linux-android-",
65 "i686-android-linux-",
71 const char *const mips_triplets[] = {
72 "mips-unknown-linux-gnu-",
73 "mipsel-linux-android-",
76 "mips64el-linux-gnuabi64-",
77 "mips64-linux-gnuabi64-",
82 static bool lookup_path(char *name)
85 char *path, *tmp = NULL;
87 char *env = getenv("PATH");
96 path = strtok_r(env, ":", &tmp);
98 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
99 if (access(buf, F_OK) == 0) {
103 path = strtok_r(NULL, ":", &tmp);
109 static int lookup_triplets(const char *const *triplets, const char *name)
114 for (i = 0; triplets[i] != NULL; i++) {
115 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
116 if (lookup_path(buf))
123 * Return architecture name in a normalized form.
124 * The conversion logic comes from the Makefile.
126 const char *normalize_arch(char *arch)
128 if (!strcmp(arch, "x86_64"))
130 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
132 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
134 if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
136 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
138 if (!strncmp(arch, "s390", 4))
140 if (!strncmp(arch, "parisc", 6))
142 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
144 if (!strncmp(arch, "mips", 4))
146 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
152 static int perf_env__lookup_binutils_path(struct perf_env *env,
153 const char *name, const char **path)
156 const char *arch, *cross_env;
158 const char *const *path_list;
161 arch = normalize_arch(env->arch);
167 * We don't need to try to find objdump path for native system.
168 * Just use default binutils path (e.g.: "objdump").
170 if (!strcmp(normalize_arch(uts.machine), arch))
173 cross_env = getenv("CROSS_COMPILE");
175 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
178 if (access(buf, F_OK) == 0)
182 if (lookup_path(buf))
187 if (!strcmp(arch, "arm"))
188 path_list = arm_triplets;
189 else if (!strcmp(arch, "arm64"))
190 path_list = arm64_triplets;
191 else if (!strcmp(arch, "powerpc"))
192 path_list = powerpc_triplets;
193 else if (!strcmp(arch, "sh"))
194 path_list = sh_triplets;
195 else if (!strcmp(arch, "s390"))
196 path_list = s390_triplets;
197 else if (!strcmp(arch, "sparc"))
198 path_list = sparc_triplets;
199 else if (!strcmp(arch, "x86"))
200 path_list = x86_triplets;
201 else if (!strcmp(arch, "mips"))
202 path_list = mips_triplets;
204 ui__error("binutils for %s not supported.\n", arch);
208 idx = lookup_triplets(path_list, name);
210 ui__error("Please install %s for %s.\n"
211 "You can add it to PATH, set CROSS_COMPILE or "
212 "override the default using --%s.\n",
217 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
229 int perf_env__lookup_objdump(struct perf_env *env)
232 * For live mode, env->arch will be NULL and we can use
233 * the native objdump tool.
235 if (env->arch == NULL)
238 return perf_env__lookup_binutils_path(env, "objdump", &objdump_path);