builtin-ftrace.c 11.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * builtin-ftrace.c
 *
 * Copyright (c) 2013  LG Electronics,  Namhyung Kim <namhyung@kernel.org>
 *
 * Released under the GPL v2.
 */

#include "builtin.h"
#include "perf.h"

12
#include <errno.h>
13 14
#include <unistd.h>
#include <signal.h>
15
#include <fcntl.h>
16
#include <poll.h>
17 18 19

#include "debug.h"
#include <subcmd/parse-options.h>
20
#include <api/fs/tracing_path.h>
21 22
#include "evlist.h"
#include "target.h"
23
#include "cpumap.h"
24
#include "thread_map.h"
25
#include "util/config.h"
26 27 28 29 30


#define DEFAULT_TRACER  "function_graph"

struct perf_ftrace {
31 32 33 34 35 36 37
	struct perf_evlist	*evlist;
	struct target		target;
	const char		*tracer;
	struct list_head	filters;
	struct list_head	notrace;
	struct list_head	graph_funcs;
	struct list_head	nograph_funcs;
38
	int			graph_depth;
39 40 41 42 43
};

struct filter_entry {
	struct list_head	list;
	char			name[];
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
};

static bool done;

static void sig_handler(int sig __maybe_unused)
{
	done = true;
}

/*
 * perf_evlist__prepare_workload will send a SIGUSR1 if the fork fails, since
 * we asked by setting its exec_error to the function below,
 * ftrace__workload_exec_failed_signal.
 *
 * XXX We need to handle this more appropriately, emitting an error, etc.
 */
static void ftrace__workload_exec_failed_signal(int signo __maybe_unused,
						siginfo_t *info __maybe_unused,
						void *ucontext __maybe_unused)
{
	/* workload_exec_errno = info->si_value.sival_int; */
	done = true;
}

68
static int __write_tracing_file(const char *name, const char *val, bool append)
69 70 71 72
{
	char *file;
	int fd, ret = -1;
	ssize_t size = strlen(val);
73
	int flags = O_WRONLY;
74
	char errbuf[512];
75
	char *val_copy;
76 77 78 79 80 81 82

	file = get_tracing_file(name);
	if (!file) {
		pr_debug("cannot get tracing file: %s\n", name);
		return -1;
	}

83 84 85 86 87 88
	if (append)
		flags |= O_APPEND;
	else
		flags |= O_TRUNC;

	fd = open(file, flags);
89
	if (fd < 0) {
90 91
		pr_debug("cannot open tracing file: %s: %s\n",
			 name, str_error_r(errno, errbuf, sizeof(errbuf)));
92 93 94
		goto out;
	}

95 96 97 98 99 100 101 102 103 104
	/*
	 * Copy the original value and append a '\n'. Without this,
	 * the kernel can hide possible errors.
	 */
	val_copy = strdup(val);
	if (!val_copy)
		goto out_close;
	val_copy[size] = '\n';

	if (write(fd, val_copy, size + 1) == size + 1)
105 106
		ret = 0;
	else
107 108
		pr_debug("write '%s' to tracing/%s failed: %s\n",
			 val, name, str_error_r(errno, errbuf, sizeof(errbuf)));
109

110 111
	free(val_copy);
out_close:
112 113 114 115 116 117
	close(fd);
out:
	put_tracing_file(file);
	return ret;
}

118 119 120 121 122 123 124 125 126 127
static int write_tracing_file(const char *name, const char *val)
{
	return __write_tracing_file(name, val, false);
}

static int append_tracing_file(const char *name, const char *val)
{
	return __write_tracing_file(name, val, true);
}

128
static int reset_tracing_cpu(void);
129
static void reset_tracing_filters(void);
130

131 132 133 134 135 136 137 138 139 140 141
static int reset_tracing_files(struct perf_ftrace *ftrace __maybe_unused)
{
	if (write_tracing_file("tracing_on", "0") < 0)
		return -1;

	if (write_tracing_file("current_tracer", "nop") < 0)
		return -1;

	if (write_tracing_file("set_ftrace_pid", " ") < 0)
		return -1;

142 143 144
	if (reset_tracing_cpu() < 0)
		return -1;

145 146 147
	if (write_tracing_file("max_graph_depth", "0") < 0)
		return -1;

148
	reset_tracing_filters();
149 150 151
	return 0;
}

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
static int set_tracing_pid(struct perf_ftrace *ftrace)
{
	int i;
	char buf[16];

	if (target__has_cpu(&ftrace->target))
		return 0;

	for (i = 0; i < thread_map__nr(ftrace->evlist->threads); i++) {
		scnprintf(buf, sizeof(buf), "%d",
			  ftrace->evlist->threads->map[i]);
		if (append_tracing_file("set_ftrace_pid", buf) < 0)
			return -1;
	}
	return 0;
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
static int set_tracing_cpumask(struct cpu_map *cpumap)
{
	char *cpumask;
	size_t mask_size;
	int ret;
	int last_cpu;

	last_cpu = cpu_map__cpu(cpumap, cpumap->nr - 1);
	mask_size = (last_cpu + 3) / 4 + 1;
	mask_size += last_cpu / 32; /* ',' is needed for every 32th cpus */

	cpumask = malloc(mask_size);
	if (cpumask == NULL) {
		pr_debug("failed to allocate cpu mask\n");
		return -1;
	}

	cpu_map__snprint_mask(cpumap, cpumask, mask_size);

	ret = write_tracing_file("tracing_cpumask", cpumask);

	free(cpumask);
	return ret;
}

static int set_tracing_cpu(struct perf_ftrace *ftrace)
{
	struct cpu_map *cpumap = ftrace->evlist->cpus;

	if (!target__has_cpu(&ftrace->target))
		return 0;

	return set_tracing_cpumask(cpumap);
}

static int reset_tracing_cpu(void)
{
	struct cpu_map *cpumap = cpu_map__new(NULL);
	int ret;

	ret = set_tracing_cpumask(cpumap);
	cpu_map__put(cpumap);
	return ret;
}

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
static int __set_tracing_filter(const char *filter_file, struct list_head *funcs)
{
	struct filter_entry *pos;

	list_for_each_entry(pos, funcs, list) {
		if (append_tracing_file(filter_file, pos->name) < 0)
			return -1;
	}

	return 0;
}

static int set_tracing_filters(struct perf_ftrace *ftrace)
{
	int ret;

	ret = __set_tracing_filter("set_ftrace_filter", &ftrace->filters);
	if (ret < 0)
		return ret;

	ret = __set_tracing_filter("set_ftrace_notrace", &ftrace->notrace);
	if (ret < 0)
		return ret;

	ret = __set_tracing_filter("set_graph_function", &ftrace->graph_funcs);
	if (ret < 0)
		return ret;

	/* old kernels do not have this filter */
	__set_tracing_filter("set_graph_notrace", &ftrace->nograph_funcs);

	return ret;
}

static void reset_tracing_filters(void)
{
	write_tracing_file("set_ftrace_filter", " ");
	write_tracing_file("set_ftrace_notrace", " ");
	write_tracing_file("set_graph_function", " ");
	write_tracing_file("set_graph_notrace", " ");
}

256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static int set_tracing_depth(struct perf_ftrace *ftrace)
{
	char buf[16];

	if (ftrace->graph_depth == 0)
		return 0;

	if (ftrace->graph_depth < 0) {
		pr_err("invalid graph depth: %d\n", ftrace->graph_depth);
		return -1;
	}

	snprintf(buf, sizeof(buf), "%d", ftrace->graph_depth);

	if (write_tracing_file("max_graph_depth", buf) < 0)
		return -1;

	return 0;
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static int __cmd_ftrace(struct perf_ftrace *ftrace, int argc, const char **argv)
{
	char *trace_file;
	int trace_fd;
	char buf[4096];
	struct pollfd pollfd = {
		.events = POLLIN,
	};

	if (geteuid() != 0) {
		pr_err("ftrace only works for root!\n");
		return -1;
	}

	signal(SIGINT, sig_handler);
	signal(SIGUSR1, sig_handler);
	signal(SIGCHLD, sig_handler);
293
	signal(SIGPIPE, sig_handler);
294

295 296
	if (reset_tracing_files(ftrace) < 0) {
		pr_err("failed to reset ftrace\n");
297
		goto out;
298
	}
299 300 301 302 303

	/* reset ftrace buffer */
	if (write_tracing_file("trace", "0") < 0)
		goto out;

304 305 306
	if (argc && perf_evlist__prepare_workload(ftrace->evlist,
				&ftrace->target, argv, false,
				ftrace__workload_exec_failed_signal) < 0) {
307 308 309
		goto out;
	}

310 311 312
	if (set_tracing_pid(ftrace) < 0) {
		pr_err("failed to set ftrace pid\n");
		goto out_reset;
313 314
	}

315 316 317 318 319
	if (set_tracing_cpu(ftrace) < 0) {
		pr_err("failed to set tracing cpumask\n");
		goto out_reset;
	}

320 321 322 323 324
	if (set_tracing_filters(ftrace) < 0) {
		pr_err("failed to set tracing filters\n");
		goto out_reset;
	}

325 326 327 328 329
	if (set_tracing_depth(ftrace) < 0) {
		pr_err("failed to set graph depth\n");
		goto out_reset;
	}

330 331 332
	if (write_tracing_file("current_tracer", ftrace->tracer) < 0) {
		pr_err("failed to set current_tracer to %s\n", ftrace->tracer);
		goto out_reset;
333 334
	}

335 336
	setup_pager();

337 338 339
	trace_file = get_tracing_file("trace_pipe");
	if (!trace_file) {
		pr_err("failed to open trace_pipe\n");
340
		goto out_reset;
341 342 343 344 345 346 347 348
	}

	trace_fd = open(trace_file, O_RDONLY);

	put_tracing_file(trace_file);

	if (trace_fd < 0) {
		pr_err("failed to open trace_pipe\n");
349
		goto out_reset;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
	}

	fcntl(trace_fd, F_SETFL, O_NONBLOCK);
	pollfd.fd = trace_fd;

	if (write_tracing_file("tracing_on", "1") < 0) {
		pr_err("can't enable tracing\n");
		goto out_close_fd;
	}

	perf_evlist__start_workload(ftrace->evlist);

	while (!done) {
		if (poll(&pollfd, 1, -1) < 0)
			break;

		if (pollfd.revents & POLLIN) {
			int n = read(trace_fd, buf, sizeof(buf));
			if (n < 0)
				break;
			if (fwrite(buf, n, 1, stdout) != 1)
				break;
		}
	}

	write_tracing_file("tracing_on", "0");

	/* read remaining buffer contents */
	while (true) {
		int n = read(trace_fd, buf, sizeof(buf));
		if (n <= 0)
			break;
		if (fwrite(buf, n, 1, stdout) != 1)
			break;
	}

out_close_fd:
	close(trace_fd);
388
out_reset:
389
	reset_tracing_files(ftrace);
390
out:
391 392 393
	return done ? 0 : -1;
}

394 395 396 397
static int perf_ftrace_config(const char *var, const char *value, void *cb)
{
	struct perf_ftrace *ftrace = cb;

398
	if (!strstarts(var, "ftrace."))
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
		return 0;

	if (strcmp(var, "ftrace.tracer"))
		return -1;

	if (!strcmp(value, "function_graph") ||
	    !strcmp(value, "function")) {
		ftrace->tracer = value;
		return 0;
	}

	pr_err("Please select \"function_graph\" (default) or \"function\"\n");
	return -1;
}

414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
static int parse_filter_func(const struct option *opt, const char *str,
			     int unset __maybe_unused)
{
	struct list_head *head = opt->value;
	struct filter_entry *entry;

	entry = malloc(sizeof(*entry) + strlen(str) + 1);
	if (entry == NULL)
		return -ENOMEM;

	strcpy(entry->name, str);
	list_add_tail(&entry->list, head);

	return 0;
}

static void delete_filter_func(struct list_head *head)
{
	struct filter_entry *pos, *tmp;

	list_for_each_entry_safe(pos, tmp, head, list) {
		list_del(&pos->list);
		free(pos);
	}
}

440
int cmd_ftrace(int argc, const char **argv)
441 442 443
{
	int ret;
	struct perf_ftrace ftrace = {
444
		.tracer = DEFAULT_TRACER,
445 446 447
		.target = { .uid = UINT_MAX, },
	};
	const char * const ftrace_usage[] = {
448
		"perf ftrace [<options>] [<command>]",
449 450 451 452 453
		"perf ftrace [<options>] -- <command> [<options>]",
		NULL
	};
	const struct option ftrace_options[] = {
	OPT_STRING('t', "tracer", &ftrace.tracer, "tracer",
454
		   "tracer to use: function_graph(default) or function"),
455 456
	OPT_STRING('p', "pid", &ftrace.target.pid, "pid",
		   "trace on existing process id"),
457 458
	OPT_INCR('v', "verbose", &verbose,
		 "be more verbose"),
459 460 461 462
	OPT_BOOLEAN('a', "all-cpus", &ftrace.target.system_wide,
		    "system-wide collection from all CPUs"),
	OPT_STRING('C', "cpu", &ftrace.target.cpu_list, "cpu",
		    "list of cpus to monitor"),
463 464 465 466 467 468 469 470
	OPT_CALLBACK('T', "trace-funcs", &ftrace.filters, "func",
		     "trace given functions only", parse_filter_func),
	OPT_CALLBACK('N', "notrace-funcs", &ftrace.notrace, "func",
		     "do not trace given functions", parse_filter_func),
	OPT_CALLBACK('G', "graph-funcs", &ftrace.graph_funcs, "func",
		     "Set graph filter on given functions", parse_filter_func),
	OPT_CALLBACK('g', "nograph-funcs", &ftrace.nograph_funcs, "func",
		     "Set nograph filter on given functions", parse_filter_func),
471 472
	OPT_INTEGER('D', "graph-depth", &ftrace.graph_depth,
		    "Max depth for function graph tracer"),
473 474 475
	OPT_END()
	};

476 477 478 479 480
	INIT_LIST_HEAD(&ftrace.filters);
	INIT_LIST_HEAD(&ftrace.notrace);
	INIT_LIST_HEAD(&ftrace.graph_funcs);
	INIT_LIST_HEAD(&ftrace.nograph_funcs);

481 482 483 484
	ret = perf_config(perf_ftrace_config, &ftrace);
	if (ret < 0)
		return -1;

485 486
	argc = parse_options(argc, argv, ftrace_options, ftrace_usage,
			    PARSE_OPT_STOP_AT_NON_OPTION);
487
	if (!argc && target__none(&ftrace.target))
488 489
		usage_with_options(ftrace_usage, ftrace_options);

490 491 492 493 494 495
	ret = target__validate(&ftrace.target);
	if (ret) {
		char errbuf[512];

		target__strerror(&ftrace.target, ret, errbuf, 512);
		pr_err("%s\n", errbuf);
496
		goto out_delete_filters;
497 498
	}

499
	ftrace.evlist = perf_evlist__new();
500 501 502 503
	if (ftrace.evlist == NULL) {
		ret = -ENOMEM;
		goto out_delete_filters;
	}
504 505 506 507 508 509 510 511 512 513

	ret = perf_evlist__create_maps(ftrace.evlist, &ftrace.target);
	if (ret < 0)
		goto out_delete_evlist;

	ret = __cmd_ftrace(&ftrace, argc, argv);

out_delete_evlist:
	perf_evlist__delete(ftrace.evlist);

514 515 516 517 518 519
out_delete_filters:
	delete_filter_func(&ftrace.filters);
	delete_filter_func(&ftrace.notrace);
	delete_filter_func(&ftrace.graph_funcs);
	delete_filter_func(&ftrace.nograph_funcs);

520 521
	return ret;
}