trace-event-info.c 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2008,2009, Steven Rostedt <srostedt@redhat.com>
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License (not later!)
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 */
#define _GNU_SOURCE
#include <dirent.h>
23
#include <mntent.h>
24 25 26 27 28 29 30 31 32 33 34 35
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <pthread.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
36
#include <stdbool.h>
37
#include <linux/list.h>
38
#include <linux/kernel.h>
39

40
#include "../perf.h"
41
#include "trace-event.h"
42
#include "debugfs.h"
43
#include "evsel.h"
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

#define VERSION "0.5"

#define _STR(x) #x
#define STR(x) _STR(x)
#define MAX_PATH 256

#define TRACE_CTRL	"tracing_on"
#define TRACE		"trace"
#define AVAILABLE	"available_tracers"
#define CURRENT		"current_tracer"
#define ITER_CTRL	"trace_options"
#define MAX_LATENCY	"tracing_max_latency"

unsigned int page_size;

static const char *output_file = "trace.info";
static int output_fd;

struct event_list {
	struct event_list *next;
	const char *event;
};

struct events {
	struct events *sibling;
	struct events *children;
	struct events *next;
	char *name;
};



static void die(const char *fmt, ...)
{
	va_list ap;
	int ret = errno;

	if (errno)
		perror("trace-cmd");
	else
		ret = -1;

	va_start(ap, fmt);
	fprintf(stderr, "  ");
	vfprintf(stderr, fmt, ap);
	va_end(ap);

	fprintf(stderr, "\n");
	exit(ret);
}

void *malloc_or_die(unsigned int size)
{
	void *data;

	data = malloc(size);
	if (!data)
		die("malloc");
	return data;
}

static const char *find_debugfs(void)
{
108
	const char *path = debugfs_mount(NULL);
109

110 111
	if (!path)
		die("Your kernel not support debugfs filesystem");
112

113
	return path;
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
}

/*
 * Finds the path to the debugfs/tracing
 * Allocates the string and stores it.
 */
static const char *find_tracing_dir(void)
{
	static char *tracing;
	static int tracing_found;
	const char *debugfs;

	if (tracing_found)
		return tracing;

	debugfs = find_debugfs();

	tracing = malloc_or_die(strlen(debugfs) + 9);

	sprintf(tracing, "%s/tracing", debugfs);

	tracing_found = 1;
	return tracing;
}

static char *get_tracing_file(const char *name)
{
	const char *tracing;
	char *file;

	tracing = find_tracing_dir();
	if (!tracing)
		return NULL;

	file = malloc_or_die(strlen(tracing) + strlen(name) + 2);

	sprintf(file, "%s/%s", tracing, name);
	return file;
}

static void put_tracing_file(char *file)
{
	free(file);
}

159 160
static ssize_t calc_data_size;

161 162 163 164
static ssize_t write_or_die(const void *buf, size_t len)
{
	int ret;

165 166 167 168 169
	if (calc_data_size) {
		calc_data_size += len;
		return len;
	}

170 171 172 173 174 175 176 177 178 179 180 181
	ret = write(output_fd, buf, len);
	if (ret < 0)
		die("writing to '%s'", output_file);

	return ret;
}

int bigendian(void)
{
	unsigned char str[] = { 0x1, 0x2, 0x3, 0x4, 0x0, 0x0, 0x0, 0x0};
	unsigned int *ptr;

182
	ptr = (unsigned int *)(void *)str;
183 184 185
	return *ptr == 0x01020304;
}

186 187
/* unfortunately, you can not stat debugfs or proc files for size */
static void record_file(const char *file, size_t hdr_sz)
188 189
{
	unsigned long long size = 0;
190 191 192
	char buf[BUFSIZ], *sizep;
	off_t hdr_pos = lseek(output_fd, 0, SEEK_CUR);
	int r, fd;
193 194 195 196 197

	fd = open(file, O_RDONLY);
	if (fd < 0)
		die("Can't read '%s'", file);

198 199
	/* put in zeros for file size, then fill true size later */
	write_or_die(&size, hdr_sz);
200 201 202

	do {
		r = read(fd, buf, BUFSIZ);
203
		if (r > 0) {
204
			size += r;
205 206
			write_or_die(buf, r);
		}
207 208 209
	} while (r > 0);
	close(fd);

210 211 212 213 214 215 216
	/* ugh, handle big-endian hdr_size == 4 */
	sizep = (char*)&size;
	if (bigendian())
		sizep += sizeof(u64) - hdr_sz;

	if (pwrite(output_fd, sizep, hdr_sz, hdr_pos) < 0)
		die("writing to %s", output_file);
217 218 219 220 221
}

static void read_header_files(void)
{
	char *path;
222
	struct stat st;
223 224

	path = get_tracing_file("events/header_page");
225
	if (stat(path, &st) < 0)
226 227 228
		die("can't read '%s'", path);

	write_or_die("header_page", 12);
229
	record_file(path, 8);
230 231 232
	put_tracing_file(path);

	path = get_tracing_file("events/header_event");
233
	if (stat(path, &st) < 0)
234 235 236
		die("can't read '%s'", path);

	write_or_die("header_event", 13);
237
	record_file(path, 8);
238 239 240
	put_tracing_file(path);
}

241 242 243 244 245 246 247 248 249 250 251 252
static bool name_in_tp_list(char *sys, struct tracepoint_path *tps)
{
	while (tps) {
		if (!strcmp(sys, tps->name))
			return true;
		tps = tps->next;
	}

	return false;
}

static void copy_event_system(const char *sys, struct tracepoint_path *tps)
253 254 255 256 257 258 259 260 261 262 263 264 265
{
	struct dirent *dent;
	struct stat st;
	char *format;
	DIR *dir;
	int count = 0;
	int ret;

	dir = opendir(sys);
	if (!dir)
		die("can't read directory '%s'", sys);

	while ((dent = readdir(dir))) {
266 267
		if (dent->d_type != DT_DIR ||
		    strcmp(dent->d_name, ".") == 0 ||
268 269
		    strcmp(dent->d_name, "..") == 0 ||
		    !name_in_tp_list(dent->d_name, tps))
270 271 272 273 274 275 276 277 278 279 280 281 282 283
			continue;
		format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
		sprintf(format, "%s/%s/format", sys, dent->d_name);
		ret = stat(format, &st);
		free(format);
		if (ret < 0)
			continue;
		count++;
	}

	write_or_die(&count, 4);

	rewinddir(dir);
	while ((dent = readdir(dir))) {
284 285
		if (dent->d_type != DT_DIR ||
		    strcmp(dent->d_name, ".") == 0 ||
286 287
		    strcmp(dent->d_name, "..") == 0 ||
		    !name_in_tp_list(dent->d_name, tps))
288 289 290 291 292
			continue;
		format = malloc_or_die(strlen(sys) + strlen(dent->d_name) + 10);
		sprintf(format, "%s/%s/format", sys, dent->d_name);
		ret = stat(format, &st);

293 294
		if (ret >= 0)
			record_file(format, 8);
295 296 297

		free(format);
	}
298
	closedir(dir);
299 300
}

301
static void read_ftrace_files(struct tracepoint_path *tps)
302 303 304 305 306
{
	char *path;

	path = get_tracing_file("events/ftrace");

307
	copy_event_system(path, tps);
308 309 310 311

	put_tracing_file(path);
}

312 313 314 315 316 317 318 319 320 321 322 323
static bool system_in_tp_list(char *sys, struct tracepoint_path *tps)
{
	while (tps) {
		if (!strcmp(sys, tps->system))
			return true;
		tps = tps->next;
	}

	return false;
}

static void read_event_files(struct tracepoint_path *tps)
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
{
	struct dirent *dent;
	struct stat st;
	char *path;
	char *sys;
	DIR *dir;
	int count = 0;
	int ret;

	path = get_tracing_file("events");

	dir = opendir(path);
	if (!dir)
		die("can't read directory '%s'", path);

	while ((dent = readdir(dir))) {
340 341
		if (dent->d_type != DT_DIR ||
		    strcmp(dent->d_name, ".") == 0 ||
342
		    strcmp(dent->d_name, "..") == 0 ||
343 344
		    strcmp(dent->d_name, "ftrace") == 0 ||
		    !system_in_tp_list(dent->d_name, tps))
345
			continue;
346
		count++;
347 348 349 350 351 352
	}

	write_or_die(&count, 4);

	rewinddir(dir);
	while ((dent = readdir(dir))) {
353 354
		if (dent->d_type != DT_DIR ||
		    strcmp(dent->d_name, ".") == 0 ||
355
		    strcmp(dent->d_name, "..") == 0 ||
356 357
		    strcmp(dent->d_name, "ftrace") == 0 ||
		    !system_in_tp_list(dent->d_name, tps))
358 359 360 361 362
			continue;
		sys = malloc_or_die(strlen(path) + strlen(dent->d_name) + 2);
		sprintf(sys, "%s/%s", path, dent->d_name);
		ret = stat(sys, &st);
		if (ret >= 0) {
363 364
			write_or_die(dent->d_name, strlen(dent->d_name) + 1);
			copy_event_system(sys, tps);
365 366 367 368
		}
		free(sys);
	}

369
	closedir(dir);
370 371 372 373 374
	put_tracing_file(path);
}

static void read_proc_kallsyms(void)
{
375
	unsigned int size;
376 377 378 379 380 381 382 383 384 385 386
	const char *path = "/proc/kallsyms";
	struct stat st;
	int ret;

	ret = stat(path, &st);
	if (ret < 0) {
		/* not found */
		size = 0;
		write_or_die(&size, 4);
		return;
	}
387
	record_file(path, 4);
388 389 390 391
}

static void read_ftrace_printk(void)
{
392
	unsigned int size;
393
	char *path;
394 395 396 397 398 399 400 401 402
	struct stat st;
	int ret;

	path = get_tracing_file("printk_formats");
	ret = stat(path, &st);
	if (ret < 0) {
		/* not found */
		size = 0;
		write_or_die(&size, 4);
403
		goto out;
404
	}
405 406
	record_file(path, 4);

407 408
out:
	put_tracing_file(path);
409 410
}

411
static struct tracepoint_path *
412
get_tracepoints_path(struct list_head *pattrs)
413 414
{
	struct tracepoint_path path, *ppath = &path;
415 416
	struct perf_evsel *pos;
	int nr_tracepoints = 0;
417

418 419
	list_for_each_entry(pos, pattrs, node) {
		if (pos->attr.type != PERF_TYPE_TRACEPOINT)
420
			continue;
421
		++nr_tracepoints;
422
		ppath->next = tracepoint_id_to_path(pos->attr.config);
423 424 425 426 427
		if (!ppath->next)
			die("%s\n", "No memory to alloc tracepoints list");
		ppath = ppath->next;
	}

428
	return nr_tracepoints > 0 ? path.next : NULL;
429
}
430

431
bool have_tracepoints(struct list_head *pattrs)
432
{
433
	struct perf_evsel *pos;
434

435 436
	list_for_each_entry(pos, pattrs, node)
		if (pos->attr.type == PERF_TYPE_TRACEPOINT)
437 438 439
			return true;

	return false;
440 441
}

442
int read_tracing_data(int fd, struct list_head *pattrs)
443 444
{
	char buf[BUFSIZ];
445
	struct tracepoint_path *tps = get_tracepoints_path(pattrs);
446 447 448 449 450 451

	/*
	 * What? No tracepoints? No sense writing anything here, bail out.
	 */
	if (tps == NULL)
		return -1;
452

453
	output_fd = fd;
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476

	buf[0] = 23;
	buf[1] = 8;
	buf[2] = 68;
	memcpy(buf + 3, "tracing", 7);

	write_or_die(buf, 10);

	write_or_die(VERSION, strlen(VERSION) + 1);

	/* save endian */
	if (bigendian())
		buf[0] = 1;
	else
		buf[0] = 0;

	write_or_die(buf, 1);

	/* save size of long */
	buf[0] = sizeof(long);
	write_or_die(buf, 1);

	/* save page_size */
477
	page_size = sysconf(_SC_PAGESIZE);
478 479 480
	write_or_die(&page_size, 4);

	read_header_files();
481 482
	read_ftrace_files(tps);
	read_event_files(tps);
483 484
	read_proc_kallsyms();
	read_ftrace_printk();
485 486

	return 0;
487
}
488

489
ssize_t read_tracing_data_size(int fd, struct list_head *pattrs)
490 491 492 493 494
{
	ssize_t size;
	int err = 0;

	calc_data_size = 1;
495
	err = read_tracing_data(fd, pattrs);
496 497 498 499 500 501 502 503
	size = calc_data_size - 1;
	calc_data_size = 0;

	if (err < 0)
		return err;

	return size;
}