linux/tools/perf/arch/common.c
<<
>>
Prefs
   1#include <stdio.h>
   2#include <sys/utsname.h>
   3#include "common.h"
   4#include "../util/util.h"
   5#include "../util/debug.h"
   6
   7#include "sane_ctype.h"
   8
   9const 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-",
  15        "arm-linux-gnu-",
  16        "arm-linux-gnueabihf-",
  17        "arm-none-eabi-",
  18        NULL
  19};
  20
  21const char *const arm64_triplets[] = {
  22        "aarch64-linux-android-",
  23        "aarch64-linux-gnu-",
  24        NULL
  25};
  26
  27const char *const powerpc_triplets[] = {
  28        "powerpc-unknown-linux-gnu-",
  29        "powerpc-linux-gnu-",
  30        "powerpc64-unknown-linux-gnu-",
  31        "powerpc64-linux-gnu-",
  32        "powerpc64le-linux-gnu-",
  33        NULL
  34};
  35
  36const char *const s390_triplets[] = {
  37        "s390-ibm-linux-",
  38        "s390x-linux-gnu-",
  39        NULL
  40};
  41
  42const char *const sh_triplets[] = {
  43        "sh-unknown-linux-gnu-",
  44        "sh64-unknown-linux-gnu-",
  45        "sh-linux-gnu-",
  46        "sh64-linux-gnu-",
  47        NULL
  48};
  49
  50const char *const sparc_triplets[] = {
  51        "sparc-unknown-linux-gnu-",
  52        "sparc64-unknown-linux-gnu-",
  53        "sparc64-linux-gnu-",
  54        NULL
  55};
  56
  57const char *const x86_triplets[] = {
  58        "x86_64-pc-linux-gnu-",
  59        "x86_64-unknown-linux-gnu-",
  60        "i686-pc-linux-gnu-",
  61        "i586-pc-linux-gnu-",
  62        "i486-pc-linux-gnu-",
  63        "i386-pc-linux-gnu-",
  64        "i686-linux-android-",
  65        "i686-android-linux-",
  66        "x86_64-linux-gnu-",
  67        "i586-linux-gnu-",
  68        NULL
  69};
  70
  71const char *const mips_triplets[] = {
  72        "mips-unknown-linux-gnu-",
  73        "mipsel-linux-android-",
  74        "mips-linux-gnu-",
  75        "mips64-linux-gnu-",
  76        "mips64el-linux-gnuabi64-",
  77        "mips64-linux-gnuabi64-",
  78        "mipsel-linux-gnu-",
  79        NULL
  80};
  81
  82static bool lookup_path(char *name)
  83{
  84        bool found = false;
  85        char *path, *tmp = NULL;
  86        char buf[PATH_MAX];
  87        char *env = getenv("PATH");
  88
  89        if (!env)
  90                return false;
  91
  92        env = strdup(env);
  93        if (!env)
  94                return false;
  95
  96        path = strtok_r(env, ":", &tmp);
  97        while (path) {
  98                scnprintf(buf, sizeof(buf), "%s/%s", path, name);
  99                if (access(buf, F_OK) == 0) {
 100                        found = true;
 101                        break;
 102                }
 103                path = strtok_r(NULL, ":", &tmp);
 104        }
 105        free(env);
 106        return found;
 107}
 108
 109static int lookup_triplets(const char *const *triplets, const char *name)
 110{
 111        int i;
 112        char buf[PATH_MAX];
 113
 114        for (i = 0; triplets[i] != NULL; i++) {
 115                scnprintf(buf, sizeof(buf), "%s%s", triplets[i], name);
 116                if (lookup_path(buf))
 117                        return i;
 118        }
 119        return -1;
 120}
 121
 122/*
 123 * Return architecture name in a normalized form.
 124 * The conversion logic comes from the Makefile.
 125 */
 126const char *normalize_arch(char *arch)
 127{
 128        if (!strcmp(arch, "x86_64"))
 129                return "x86";
 130        if (arch[0] == 'i' && arch[2] == '8' && arch[3] == '6')
 131                return "x86";
 132        if (!strcmp(arch, "sun4u") || !strncmp(arch, "sparc", 5))
 133                return "sparc";
 134        if (!strcmp(arch, "aarch64") || !strcmp(arch, "arm64"))
 135                return "arm64";
 136        if (!strncmp(arch, "arm", 3) || !strcmp(arch, "sa110"))
 137                return "arm";
 138        if (!strncmp(arch, "s390", 4))
 139                return "s390";
 140        if (!strncmp(arch, "parisc", 6))
 141                return "parisc";
 142        if (!strncmp(arch, "powerpc", 7) || !strncmp(arch, "ppc", 3))
 143                return "powerpc";
 144        if (!strncmp(arch, "mips", 4))
 145                return "mips";
 146        if (!strncmp(arch, "sh", 2) && isdigit(arch[2]))
 147                return "sh";
 148
 149        return arch;
 150}
 151
 152static int perf_env__lookup_binutils_path(struct perf_env *env,
 153                                          const char *name, const char **path)
 154{
 155        int idx;
 156        const char *arch, *cross_env;
 157        struct utsname uts;
 158        const char *const *path_list;
 159        char *buf = NULL;
 160
 161        arch = normalize_arch(env->arch);
 162
 163        if (uname(&uts) < 0)
 164                goto out;
 165
 166        /*
 167         * We don't need to try to find objdump path for native system.
 168         * Just use default binutils path (e.g.: "objdump").
 169         */
 170        if (!strcmp(normalize_arch(uts.machine), arch))
 171                goto out;
 172
 173        cross_env = getenv("CROSS_COMPILE");
 174        if (cross_env) {
 175                if (asprintf(&buf, "%s%s", cross_env, name) < 0)
 176                        goto out_error;
 177                if (buf[0] == '/') {
 178                        if (access(buf, F_OK) == 0)
 179                                goto out;
 180                        goto out_error;
 181                }
 182                if (lookup_path(buf))
 183                        goto out;
 184                zfree(&buf);
 185        }
 186
 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;
 203        else {
 204                ui__error("binutils for %s not supported.\n", arch);
 205                goto out_error;
 206        }
 207
 208        idx = lookup_triplets(path_list, name);
 209        if (idx < 0) {
 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",
 213                          name, arch, name);
 214                goto out_error;
 215        }
 216
 217        if (asprintf(&buf, "%s%s", path_list[idx], name) < 0)
 218                goto out_error;
 219
 220out:
 221        *path = buf;
 222        return 0;
 223out_error:
 224        free(buf);
 225        *path = NULL;
 226        return -1;
 227}
 228
 229int perf_env__lookup_objdump(struct perf_env *env)
 230{
 231        /*
 232         * For live mode, env->arch will be NULL and we can use
 233         * the native objdump tool.
 234         */
 235        if (env->arch == NULL)
 236                return 0;
 237
 238        return perf_env__lookup_binutils_path(env, "objdump", &objdump_path);
 239}
 240