Linux Audio

Check our new training course

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