blob: aacef07ebf31525227b372e6c40e9a3039c5d580 [file] [log] [blame]
Irina Tirdea68e94f42012-10-16 02:33:38 +03001#include <stdio.h>
2#include <sys/utsname.h>
3#include "common.h"
4#include "../util/debug.h"
5
6const char *const arm_triplets[] = {
7 "arm-eabi-",
8 "arm-linux-androideabi-",
9 "arm-unknown-linux-",
10 "arm-unknown-linux-gnu-",
11 "arm-unknown-linux-gnueabi-",
12 NULL
13};
14
15const char *const powerpc_triplets[] = {
16 "powerpc-unknown-linux-gnu-",
17 "powerpc64-unknown-linux-gnu-",
18 NULL
19};
20
21const char *const s390_triplets[] = {
22 "s390-ibm-linux-",
23 NULL
24};
25
26const char *const sh_triplets[] = {
27 "sh-unknown-linux-gnu-",
28 "sh64-unknown-linux-gnu-",
29 NULL
30};
31
32const char *const sparc_triplets[] = {
33 "sparc-unknown-linux-gnu-",
34 "sparc64-unknown-linux-gnu-",
35 NULL
36};
37
38const char *const x86_triplets[] = {
39 "x86_64-pc-linux-gnu-",
40 "x86_64-unknown-linux-gnu-",
41 "i686-pc-linux-gnu-",
42 "i586-pc-linux-gnu-",
43 "i486-pc-linux-gnu-",
44 "i386-pc-linux-gnu-",
45 "i686-linux-android-",
46 "i686-android-linux-",
47 NULL
48};
49
50const char *const mips_triplets[] = {
51 "mips-unknown-linux-gnu-",
52 "mipsel-linux-android-",
53 NULL
54};
55
56static bool lookup_path(char *name)
57{
58 bool found = false;
59 char *path, *tmp;
60 char buf[PATH_MAX];
61 char *env = getenv("PATH");
62
63 if (!env)
64 return false;
65
66 env = strdup(env);
67 if (!env)
68 return false;
69
70 path = strtok_r(env, ":", &tmp);
71 while (path) {
72 scnprintf(buf, sizeof(buf), "%s/%s", path, name);
73 if (access(buf, F_OK) == 0) {
74 found = true;
75 break;
76 }
77 path = strtok_r(NULL, ":", &tmp);
78 }
79 free(env);
80 return found;
81}
82
83static int lookup_triplets(const char *const *triplets, const char *name)
84{
85 int i;
86 char buf[PATH_MAX];
87
88 for (i = 0; triplets[i] != NULL; i++) {
89 scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
90 if (lookup_path(buf))
91 return i;
92 }
93 return -1;
94}
95
Namhyung Kim48ed0ec2012-11-02 14:50:04 +090096/*
97 * Return architecture name in a normalized form.
98 * The conversion logic comes from the Makefile.
99 */
100static const char *normalize_arch(char *arch)
101{
102 if (!strcmp(arch, "x86_64"))
103 return "x86";
104 if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
105 return "x86";
106 if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
107 return "sparc";
108 if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
109 return "arm";
110 if (!strncmp(arch, "s390", 4))
111 return "s390";
112 if (!strncmp(arch, "parisc", 6))
113 return "parisc";
114 if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
115 return "powerpc";
116 if (!strncmp(arch, "mips", 4))
117 return "mips";
118 if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
119 return "sh";
120
121 return arch;
122}
123
Irina Tirdea68e94f42012-10-16 02:33:38 +0300124static int perf_session_env__lookup_binutils_path(struct perf_session_env *env,
125 const char *name,
126 const char **path)
127{
128 int idx;
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900129 const char *arch, *cross_env;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300130 struct utsname uts;
131 const char *const *path_list;
132 char *buf = NULL;
133
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900134 arch = normalize_arch(env->arch);
135
Irina Tirdea68e94f42012-10-16 02:33:38 +0300136 if (uname(&uts) < 0)
137 goto out;
138
139 /*
140 * We don't need to try to find objdump path for native system.
141 * Just use default binutils path (e.g.: "objdump").
142 */
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900143 if (!strcmp(normalize_arch(uts.machine), arch))
Irina Tirdea68e94f42012-10-16 02:33:38 +0300144 goto out;
145
146 cross_env = getenv("CROSS_COMPILE");
147 if (cross_env) {
148 if (asprintf(&buf, "%s%s", cross_env, name) < 0)
149 goto out_error;
150 if (buf[0] == '/') {
151 if (access(buf, F_OK) == 0)
152 goto out;
153 goto out_error;
154 }
155 if (lookup_path(buf))
156 goto out;
157 free(buf);
Thomas Jarosch68c465a2013-01-25 10:57:08 +0100158 buf = NULL;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300159 }
160
Irina Tirdea68e94f42012-10-16 02:33:38 +0300161 if (!strcmp(arch, "arm"))
162 path_list = arm_triplets;
163 else if (!strcmp(arch, "powerpc"))
164 path_list = powerpc_triplets;
165 else if (!strcmp(arch, "sh"))
166 path_list = sh_triplets;
167 else if (!strcmp(arch, "s390"))
168 path_list = s390_triplets;
169 else if (!strcmp(arch, "sparc"))
170 path_list = sparc_triplets;
Namhyung Kim48ed0ec2012-11-02 14:50:04 +0900171 else if (!strcmp(arch, "x86"))
Irina Tirdea68e94f42012-10-16 02:33:38 +0300172 path_list = x86_triplets;
173 else if (!strcmp(arch, "mips"))
174 path_list = mips_triplets;
175 else {
176 ui__error("binutils for %s not supported.\n", arch);
177 goto out_error;
178 }
179
180 idx = lookup_triplets(path_list, name);
181 if (idx < 0) {
182 ui__error("Please install %s for %s.\n"
183 "You can add it to PATH, set CROSS_COMPILE or "
184 "override the default using --%s.\n",
185 name, arch, name);
186 goto out_error;
187 }
188
189 if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
190 goto out_error;
191
192out:
193 *path = buf;
194 return 0;
195out_error:
196 free(buf);
197 *path = NULL;
198 return -1;
199}
200
201int perf_session_env__lookup_objdump(struct perf_session_env *env)
202{
Namhyung Kimff6f7772012-11-07 10:30:15 +0900203 /*
204 * For live mode, env->arch will be NULL and we can use
205 * the native objdump tool.
206 */
207 if (env->arch == NULL)
208 return 0;
209
Irina Tirdea68e94f42012-10-16 02:33:38 +0300210 return perf_session_env__lookup_binutils_path(env, "objdump",
211 &objdump_path);
212}