builtin-trace.c 3.8 KB
Newer Older
F
Frederic Weisbecker 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include "builtin.h"

#include "util/util.h"
#include "util/cache.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/header.h"

#include "util/parse-options.h"

#include "perf.h"
#include "util/debug.h"

#include "util/trace-event.h"
15
#include "util/data_map.h"
F
Frederic Weisbecker 已提交
16 17 18 19 20 21 22 23 24 25 26 27

static char		const *input_name = "perf.data";

static unsigned long	total = 0;
static unsigned long	total_comm = 0;

static struct rb_root	threads;
static struct thread	*last_match;

static struct perf_header *header;
static u64		sample_type;

28 29 30
static char		*cwd;
static int		cwdlen;

F
Frederic Weisbecker 已提交
31 32 33 34 35 36 37 38

static int
process_comm_event(event_t *event, unsigned long offset, unsigned long head)
{
	struct thread *thread;

	thread = threads__findnew(event->comm.pid, &threads, &last_match);

39
	dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
F
Frederic Weisbecker 已提交
40 41 42 43 44 45
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->comm.comm, event->comm.pid);

	if (thread == NULL ||
	    thread__set_comm(thread, event->comm.comm)) {
46
		dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
F
Frederic Weisbecker 已提交
47 48 49 50 51 52 53 54 55 56 57 58
		return -1;
	}
	total_comm++;

	return 0;
}

static int
process_sample_event(event_t *event, unsigned long offset, unsigned long head)
{
	struct thread *thread;
	u64 ip = event->ip.ip;
59
	u64 timestamp = -1;
I
Ingo Molnar 已提交
60
	u32 cpu = -1;
F
Frederic Weisbecker 已提交
61 62 63 64 65
	u64 period = 1;
	void *more_data = event->ip.__more_data;

	thread = threads__findnew(event->ip.pid, &threads, &last_match);

66 67 68 69 70
	if (sample_type & PERF_SAMPLE_TIME) {
		timestamp = *(u64 *)more_data;
		more_data += sizeof(u64);
	}

I
Ingo Molnar 已提交
71 72 73 74 75 76
	if (sample_type & PERF_SAMPLE_CPU) {
		cpu = *(u32 *)more_data;
		more_data += sizeof(u32);
		more_data += sizeof(u32); /* reserved */
	}

F
Frederic Weisbecker 已提交
77 78 79 80 81
	if (sample_type & PERF_SAMPLE_PERIOD) {
		period = *(u64 *)more_data;
		more_data += sizeof(u64);
	}

82
	dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
F
Frederic Weisbecker 已提交
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 108
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->header.misc,
		event->ip.pid, event->ip.tid,
		(void *)(long)ip,
		(long long)period);

	dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid);

	if (thread == NULL) {
		eprintf("problem processing %d event, skipping it.\n",
			event->header.type);
		return -1;
	}

	if (sample_type & PERF_SAMPLE_RAW) {
		struct {
			u32 size;
			char data[0];
		} *raw = more_data;

		/*
		 * FIXME: better resolve from pid from the struct trace_entry
		 * field, although it should be the same than this perf
		 * event pid
		 */
109
		print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
F
Frederic Weisbecker 已提交
110 111 112 113 114 115
	}
	total += period;

	return 0;
}

116
static int sample_type_check(u64 type)
F
Frederic Weisbecker 已提交
117
{
118
	sample_type = type;
F
Frederic Weisbecker 已提交
119

120 121 122 123
	if (!(sample_type & PERF_SAMPLE_RAW)) {
		fprintf(stderr,
			"No trace sample to read. Did you call perf record "
			"without -R?");
F
Frederic Weisbecker 已提交
124 125 126 127 128 129
		return -1;
	}

	return 0;
}

130 131 132 133 134 135
static struct perf_file_handler file_handler = {
	.process_sample_event	= process_sample_event,
	.process_comm_event	= process_comm_event,
	.sample_type_check	= sample_type_check,
};

F
Frederic Weisbecker 已提交
136 137
static int __cmd_trace(void)
{
138
	register_idle_thread(&threads, &last_match);
139
	register_perf_file_handler(&file_handler);
F
Frederic Weisbecker 已提交
140

141
	return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd);
F
Frederic Weisbecker 已提交
142 143 144 145 146 147 148 149 150 151 152 153
}

static const char * const annotate_usage[] = {
	"perf trace [<options>] <command>",
	NULL
};

static const struct option options[] = {
	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
		    "dump raw trace in ASCII"),
	OPT_BOOLEAN('v', "verbose", &verbose,
		    "be more verbose (show symbol address, etc)"),
154
	OPT_END()
F
Frederic Weisbecker 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
};

int cmd_trace(int argc, const char **argv, const char *prefix __used)
{
	symbol__init();

	argc = parse_options(argc, argv, options, annotate_usage, 0);
	if (argc) {
		/*
		 * Special case: if there's an argument left then assume tha
		 * it's a symbol filter:
		 */
		if (argc > 1)
			usage_with_options(annotate_usage, options);
	}

	setup_pager();

	return __cmd_trace();
}