common.c 4.3 KB

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