common.c 4.9 KB

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