Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <sys/utsname.h> |
| 3 | #include "common.h" |
Arnaldo Carvalho de Melo | 175729f | 2016-07-07 11:38:09 -0300 | [diff] [blame] | 4 | #include "../util/util.h" |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 5 | #include "../util/debug.h" |
| 6 | |
Arnaldo Carvalho de Melo | 3d689ed | 2017-04-17 16:10:49 -0300 | [diff] [blame] | 7 | #include "sane_ctype.h" |
| 8 | |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 9 | const char *const arm_triplets[] = { |
| 10 | "arm-eabi-", |
| 11 | "arm-linux-androideabi-", |
| 12 | "arm-unknown-linux-", |
| 13 | "arm-unknown-linux-gnu-", |
| 14 | "arm-unknown-linux-gnueabi-", |
Ravi Bangoria | 78f69b5 | 2016-06-24 17:24:00 +0530 | [diff] [blame] | 15 | "arm-linux-gnu-", |
| 16 | "arm-linux-gnueabihf-", |
| 17 | "arm-none-eabi-", |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 18 | NULL |
| 19 | }; |
| 20 | |
Elliott Hughes | c4d2df4 | 2014-08-14 12:39:20 -0700 | [diff] [blame] | 21 | const char *const arm64_triplets[] = { |
| 22 | "aarch64-linux-android-", |
Ravi Bangoria | 78f69b5 | 2016-06-24 17:24:00 +0530 | [diff] [blame] | 23 | "aarch64-linux-gnu-", |
Elliott Hughes | c4d2df4 | 2014-08-14 12:39:20 -0700 | [diff] [blame] | 24 | NULL |
| 25 | }; |
| 26 | |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 27 | const char *const powerpc_triplets[] = { |
| 28 | "powerpc-unknown-linux-gnu-", |
| 29 | "powerpc64-unknown-linux-gnu-", |
Ravi Bangoria | 78f69b5 | 2016-06-24 17:24:00 +0530 | [diff] [blame] | 30 | "powerpc64-linux-gnu-", |
| 31 | "powerpc64le-linux-gnu-", |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 32 | NULL |
| 33 | }; |
| 34 | |
| 35 | const char *const s390_triplets[] = { |
| 36 | "s390-ibm-linux-", |
Ravi Bangoria | 78f69b5 | 2016-06-24 17:24:00 +0530 | [diff] [blame] | 37 | "s390x-linux-gnu-", |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 38 | NULL |
| 39 | }; |
| 40 | |
| 41 | const char *const sh_triplets[] = { |
| 42 | "sh-unknown-linux-gnu-", |
| 43 | "sh64-unknown-linux-gnu-", |
Ravi Bangoria | 78f69b5 | 2016-06-24 17:24:00 +0530 | [diff] [blame] | 44 | "sh-linux-gnu-", |
| 45 | "sh64-linux-gnu-", |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 46 | NULL |
| 47 | }; |
| 48 | |
| 49 | const char *const sparc_triplets[] = { |
| 50 | "sparc-unknown-linux-gnu-", |
| 51 | "sparc64-unknown-linux-gnu-", |
Ravi Bangoria | 78f69b5 | 2016-06-24 17:24:00 +0530 | [diff] [blame] | 52 | "sparc64-linux-gnu-", |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 53 | NULL |
| 54 | }; |
| 55 | |
| 56 | const char *const x86_triplets[] = { |
| 57 | "x86_64-pc-linux-gnu-", |
| 58 | "x86_64-unknown-linux-gnu-", |
| 59 | "i686-pc-linux-gnu-", |
| 60 | "i586-pc-linux-gnu-", |
| 61 | "i486-pc-linux-gnu-", |
| 62 | "i386-pc-linux-gnu-", |
| 63 | "i686-linux-android-", |
| 64 | "i686-android-linux-", |
Ravi Bangoria | 78f69b5 | 2016-06-24 17:24:00 +0530 | [diff] [blame] | 65 | "x86_64-linux-gnu-", |
| 66 | "i586-linux-gnu-", |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 67 | NULL |
| 68 | }; |
| 69 | |
| 70 | const char *const mips_triplets[] = { |
| 71 | "mips-unknown-linux-gnu-", |
| 72 | "mipsel-linux-android-", |
Ravi Bangoria | 78f69b5 | 2016-06-24 17:24:00 +0530 | [diff] [blame] | 73 | "mips-linux-gnu-", |
| 74 | "mips64-linux-gnu-", |
| 75 | "mips64el-linux-gnuabi64-", |
| 76 | "mips64-linux-gnuabi64-", |
| 77 | "mipsel-linux-gnu-", |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 78 | NULL |
| 79 | }; |
| 80 | |
| 81 | static bool lookup_path(char *name) |
| 82 | { |
| 83 | bool found = false; |
Martin Liška | 5bcaaca | 2015-05-26 11:41:37 -0300 | [diff] [blame] | 84 | char *path, *tmp = NULL; |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 85 | char buf[PATH_MAX]; |
| 86 | char *env = getenv("PATH"); |
| 87 | |
| 88 | if (!env) |
| 89 | return false; |
| 90 | |
| 91 | env = strdup(env); |
| 92 | if (!env) |
| 93 | return false; |
| 94 | |
| 95 | path = strtok_r(env, ":", &tmp); |
| 96 | while (path) { |
| 97 | scnprintf(buf, sizeof(buf), "%s/%s", path, name); |
| 98 | if (access(buf, F_OK) == 0) { |
| 99 | found = true; |
| 100 | break; |
| 101 | } |
| 102 | path = strtok_r(NULL, ":", &tmp); |
| 103 | } |
| 104 | free(env); |
| 105 | return found; |
| 106 | } |
| 107 | |
| 108 | static int lookup_triplets(const char *const *triplets, const char *name) |
| 109 | { |
| 110 | int i; |
| 111 | char buf[PATH_MAX]; |
| 112 | |
| 113 | for (i = 0; triplets[i] != NULL; i++) { |
| 114 | scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name); |
| 115 | if (lookup_path(buf)) |
| 116 | return i; |
| 117 | } |
| 118 | return -1; |
| 119 | } |
| 120 | |
Namhyung Kim | 48ed0ec | 2012-11-02 14:50:04 +0900 | [diff] [blame] | 121 | /* |
| 122 | * Return architecture name in a normalized form. |
| 123 | * The conversion logic comes from the Makefile. |
| 124 | */ |
He Kuang | 940e6987 | 2016-06-03 03:33:18 +0000 | [diff] [blame] | 125 | const char *normalize_arch(char *arch) |
Namhyung Kim | 48ed0ec | 2012-11-02 14:50:04 +0900 | [diff] [blame] | 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"; |
Elliott Hughes | c4d2df4 | 2014-08-14 12:39:20 -0700 | [diff] [blame] | 133 | if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64")) |
| 134 | return "arm64"; |
Namhyung Kim | 48ed0ec | 2012-11-02 14:50:04 +0900 | [diff] [blame] | 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 | |
Arnaldo Carvalho de Melo | eebd0bf | 2015-09-08 15:52:20 -0300 | [diff] [blame] | 151 | static int perf_env__lookup_binutils_path(struct perf_env *env, |
| 152 | const char *name, const char **path) |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 153 | { |
| 154 | int idx; |
Namhyung Kim | 48ed0ec | 2012-11-02 14:50:04 +0900 | [diff] [blame] | 155 | const char *arch, *cross_env; |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 156 | struct utsname uts; |
| 157 | const char *const *path_list; |
| 158 | char *buf = NULL; |
| 159 | |
Namhyung Kim | 48ed0ec | 2012-11-02 14:50:04 +0900 | [diff] [blame] | 160 | arch = normalize_arch(env->arch); |
| 161 | |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 162 | if (uname(&uts) < 0) |
| 163 | goto out; |
| 164 | |
| 165 | /* |
| 166 | * We don't need to try to find objdump path for native system. |
| 167 | * Just use default binutils path (e.g.: "objdump"). |
| 168 | */ |
Namhyung Kim | 48ed0ec | 2012-11-02 14:50:04 +0900 | [diff] [blame] | 169 | if (!strcmp(normalize_arch(uts.machine), arch)) |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 170 | goto out; |
| 171 | |
| 172 | cross_env = getenv("CROSS_COMPILE"); |
| 173 | if (cross_env) { |
| 174 | if (asprintf(&buf, "%s%s", cross_env, name) < 0) |
| 175 | goto out_error; |
| 176 | if (buf[0] == '/') { |
| 177 | if (access(buf, F_OK) == 0) |
| 178 | goto out; |
| 179 | goto out_error; |
| 180 | } |
| 181 | if (lookup_path(buf)) |
| 182 | goto out; |
Arnaldo Carvalho de Melo | 0466252 | 2013-12-26 17:41:15 -0300 | [diff] [blame] | 183 | zfree(&buf); |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 184 | } |
| 185 | |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 186 | if (!strcmp(arch, "arm")) |
| 187 | path_list = arm_triplets; |
Elliott Hughes | c4d2df4 | 2014-08-14 12:39:20 -0700 | [diff] [blame] | 188 | else if (!strcmp(arch, "arm64")) |
| 189 | path_list = arm64_triplets; |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 190 | else if (!strcmp(arch, "powerpc")) |
| 191 | path_list = powerpc_triplets; |
| 192 | else if (!strcmp(arch, "sh")) |
| 193 | path_list = sh_triplets; |
| 194 | else if (!strcmp(arch, "s390")) |
| 195 | path_list = s390_triplets; |
| 196 | else if (!strcmp(arch, "sparc")) |
| 197 | path_list = sparc_triplets; |
Namhyung Kim | 48ed0ec | 2012-11-02 14:50:04 +0900 | [diff] [blame] | 198 | else if (!strcmp(arch, "x86")) |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 199 | path_list = x86_triplets; |
| 200 | else if (!strcmp(arch, "mips")) |
| 201 | path_list = mips_triplets; |
| 202 | else { |
| 203 | ui__error("binutils for %s not supported.\n", arch); |
| 204 | goto out_error; |
| 205 | } |
| 206 | |
| 207 | idx = lookup_triplets(path_list, name); |
| 208 | if (idx < 0) { |
| 209 | ui__error("Please install %s for %s.\n" |
| 210 | "You can add it to PATH, set CROSS_COMPILE or " |
| 211 | "override the default using --%s.\n", |
| 212 | name, arch, name); |
| 213 | goto out_error; |
| 214 | } |
| 215 | |
| 216 | if (asprintf(&buf, "%s%s", path_list[idx], name) < 0) |
| 217 | goto out_error; |
| 218 | |
| 219 | out: |
| 220 | *path = buf; |
| 221 | return 0; |
| 222 | out_error: |
| 223 | free(buf); |
| 224 | *path = NULL; |
| 225 | return -1; |
| 226 | } |
| 227 | |
Arnaldo Carvalho de Melo | eebd0bf | 2015-09-08 15:52:20 -0300 | [diff] [blame] | 228 | int perf_env__lookup_objdump(struct perf_env *env) |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 229 | { |
Namhyung Kim | ff6f777 | 2012-11-07 10:30:15 +0900 | [diff] [blame] | 230 | /* |
| 231 | * For live mode, env->arch will be NULL and we can use |
| 232 | * the native objdump tool. |
| 233 | */ |
| 234 | if (env->arch == NULL) |
| 235 | return 0; |
| 236 | |
Arnaldo Carvalho de Melo | eebd0bf | 2015-09-08 15:52:20 -0300 | [diff] [blame] | 237 | return perf_env__lookup_binutils_path(env, "objdump", &objdump_path); |
Irina Tirdea | 68e94f4 | 2012-10-16 02:33:38 +0300 | [diff] [blame] | 238 | } |