2 #include <sys/utsname.h>
4 #include "../util/debug.h"
6 const char *const arm_triplets[] = {
8 "arm-linux-androideabi-",
10 "arm-unknown-linux-gnu-",
11 "arm-unknown-linux-gnueabi-",
13 "arm-linux-gnueabihf-",
18 const char *const arm64_triplets[] = {
19 "aarch64-linux-android-",
24 const char *const powerpc_triplets[] = {
25 "powerpc-unknown-linux-gnu-",
26 "powerpc64-unknown-linux-gnu-",
27 "powerpc64-linux-gnu-",
28 "powerpc64le-linux-gnu-",
32 const char *const s390_triplets[] = {
38 const char *const sh_triplets[] = {
39 "sh-unknown-linux-gnu-",
40 "sh64-unknown-linux-gnu-",
46 const char *const sparc_triplets[] = {
47 "sparc-unknown-linux-gnu-",
48 "sparc64-unknown-linux-gnu-",
53 const char *const x86_triplets[] = {
54 "x86_64-pc-linux-gnu-",
55 "x86_64-unknown-linux-gnu-",
60 "i686-linux-android-",
61 "i686-android-linux-",
67 const char *const mips_triplets[] = {
68 "mips-unknown-linux-gnu-",
69 "mipsel-linux-android-",
72 "mips64el-linux-gnuabi64-",
73 "mips64-linux-gnuabi64-",
78 static bool lookup_path(char *name)
81 char *path, *tmp = NULL;
83 char *env = getenv("PATH");
92 path = strtok_r(env, ":", &tmp);
94 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
95 if (access(buf, F_OK) == 0) {
99 path = strtok_r(NULL, ":", &tmp);
105 static int lookup_triplets(const char *const *triplets, const char *name)
110 for (i = 0; triplets[i] != NULL; i++) {
111 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
112 if (lookup_path(buf))
119 * Return architecture name in a normalized form.
120 * The conversion logic comes from the Makefile.
122 const char *normalize_arch(char *arch)
124 if (!strcmp(arch, "x86_64"))
126 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
128 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
130 if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
132 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
134 if (!strncmp(arch, "s390", 4))
136 if (!strncmp(arch, "parisc", 6))
138 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
140 if (!strncmp(arch, "mips", 4))
142 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
148 static int perf_env__lookup_binutils_path(struct perf_env *env,
149 const char *name, const char **path)
152 const char *arch, *cross_env;
154 const char *const *path_list;
157 arch = normalize_arch(env->arch);
163 * We don't need to try to find objdump path for native system.
164 * Just use default binutils path (e.g.: "objdump").
166 if (!strcmp(normalize_arch(uts.machine), arch))
169 cross_env = getenv("CROSS_COMPILE");
171 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
174 if (access(buf, F_OK) == 0)
178 if (lookup_path(buf))
183 if (!strcmp(arch, "arm"))
184 path_list = arm_triplets;
185 else if (!strcmp(arch, "arm64"))
186 path_list = arm64_triplets;
187 else if (!strcmp(arch, "powerpc"))
188 path_list = powerpc_triplets;
189 else if (!strcmp(arch, "sh"))
190 path_list = sh_triplets;
191 else if (!strcmp(arch, "s390"))
192 path_list = s390_triplets;
193 else if (!strcmp(arch, "sparc"))
194 path_list = sparc_triplets;
195 else if (!strcmp(arch, "x86"))
196 path_list = x86_triplets;
197 else if (!strcmp(arch, "mips"))
198 path_list = mips_triplets;
200 ui__error("binutils for %s not supported.\n", arch);
204 idx = lookup_triplets(path_list, name);
206 ui__error("Please install %s for %s.\n"
207 "You can add it to PATH, set CROSS_COMPILE or "
208 "override the default using --%s.\n",
213 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
225 int perf_env__lookup_objdump(struct perf_env *env)
228 * For live mode, env->arch will be NULL and we can use
229 * the native objdump tool.
231 if (env->arch == NULL)
234 return perf_env__lookup_binutils_path(env, "objdump", &objdump_path);