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-",
15 const char *const powerpc_triplets
[] = {
16 "powerpc-unknown-linux-gnu-",
17 "powerpc64-unknown-linux-gnu-",
21 const char *const s390_triplets
[] = {
26 const char *const sh_triplets
[] = {
27 "sh-unknown-linux-gnu-",
28 "sh64-unknown-linux-gnu-",
32 const char *const sparc_triplets
[] = {
33 "sparc-unknown-linux-gnu-",
34 "sparc64-unknown-linux-gnu-",
38 const char *const x86_triplets
[] = {
39 "x86_64-pc-linux-gnu-",
40 "x86_64-unknown-linux-gnu-",
45 "i686-linux-android-",
46 "i686-android-linux-",
50 const char *const mips_triplets
[] = {
51 "mips-unknown-linux-gnu-",
52 "mipsel-linux-android-",
56 static bool lookup_path(char *name
)
61 char *env
= getenv("PATH");
70 path
= strtok_r(env
, ":", &tmp
);
72 scnprintf(buf
, sizeof(buf
), "%s/%s", path
, name
);
73 if (access(buf
, F_OK
) == 0) {
77 path
= strtok_r(NULL
, ":", &tmp
);
83 static int lookup_triplets(const char *const *triplets
, const char *name
)
88 for (i
= 0; triplets
[i
] != NULL
; i
++) {
89 scnprintf(buf
, sizeof(buf
), "%s%s", triplets
[i
], name
);
97 * Return architecture name in a normalized form.
98 * The conversion logic comes from the Makefile.
100 static const char *normalize_arch(char *arch
)
102 if (!strcmp(arch
, "x86_64"))
104 if (arch
[0] == 'i' && arch
[2] == '8' && arch
[3] == '6')
106 if (!strcmp(arch
, "sun4u") || !strncmp(arch
, "sparc", 5))
108 if (!strncmp(arch
, "arm", 3) || !strcmp(arch
, "sa110"))
110 if (!strncmp(arch
, "s390", 4))
112 if (!strncmp(arch
, "parisc", 6))
114 if (!strncmp(arch
, "powerpc", 7) || !strncmp(arch
, "ppc", 3))
116 if (!strncmp(arch
, "mips", 4))
118 if (!strncmp(arch
, "sh", 2) && isdigit(arch
[2]))
124 static int perf_session_env__lookup_binutils_path(struct perf_session_env
*env
,
129 const char *arch
, *cross_env
;
131 const char *const *path_list
;
134 arch
= normalize_arch(env
->arch
);
140 * We don't need to try to find objdump path for native system.
141 * Just use default binutils path (e.g.: "objdump").
143 if (!strcmp(normalize_arch(uts
.machine
), arch
))
146 cross_env
= getenv("CROSS_COMPILE");
148 if (asprintf(&buf
, "%s%s", cross_env
, name
) < 0)
151 if (access(buf
, F_OK
) == 0)
155 if (lookup_path(buf
))
160 if (!strcmp(arch
, "arm"))
161 path_list
= arm_triplets
;
162 else if (!strcmp(arch
, "powerpc"))
163 path_list
= powerpc_triplets
;
164 else if (!strcmp(arch
, "sh"))
165 path_list
= sh_triplets
;
166 else if (!strcmp(arch
, "s390"))
167 path_list
= s390_triplets
;
168 else if (!strcmp(arch
, "sparc"))
169 path_list
= sparc_triplets
;
170 else if (!strcmp(arch
, "x86"))
171 path_list
= x86_triplets
;
172 else if (!strcmp(arch
, "mips"))
173 path_list
= mips_triplets
;
175 ui__error("binutils for %s not supported.\n", arch
);
179 idx
= lookup_triplets(path_list
, name
);
181 ui__error("Please install %s for %s.\n"
182 "You can add it to PATH, set CROSS_COMPILE or "
183 "override the default using --%s.\n",
188 if (asprintf(&buf
, "%s%s", path_list
[idx
], name
) < 0)
200 int perf_session_env__lookup_objdump(struct perf_session_env
*env
)
203 * For live mode, env->arch will be NULL and we can use
204 * the native objdump tool.
206 if (env
->arch
== NULL
)
209 return perf_session_env__lookup_binutils_path(env
, "objdump",