common.c 4.9 KB

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