builtin-trace.c 3.7 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

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

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

static struct perf_header *header;
static u64		sample_type;

25 26 27
static char		*cwd;
static int		cwdlen;

F
Frederic Weisbecker 已提交
28 29 30 31

static int
process_comm_event(event_t *event, unsigned long offset, unsigned long head)
{
32
	struct thread *thread = threads__findnew(event->comm.pid);
F
Frederic Weisbecker 已提交
33

34
	dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n",
F
Frederic Weisbecker 已提交
35 36 37 38 39 40
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->comm.comm, event->comm.pid);

	if (thread == NULL ||
	    thread__set_comm(thread, event->comm.comm)) {
41
		dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n");
F
Frederic Weisbecker 已提交
42 43 44 45 46 47 48 49 50 51 52
		return -1;
	}
	total_comm++;

	return 0;
}

static int
process_sample_event(event_t *event, unsigned long offset, unsigned long head)
{
	u64 ip = event->ip.ip;
53
	u64 timestamp = -1;
I
Ingo Molnar 已提交
54
	u32 cpu = -1;
F
Frederic Weisbecker 已提交
55 56
	u64 period = 1;
	void *more_data = event->ip.__more_data;
57
	struct thread *thread = threads__findnew(event->ip.pid);
F
Frederic Weisbecker 已提交
58

59 60 61 62 63
	if (sample_type & PERF_SAMPLE_TIME) {
		timestamp = *(u64 *)more_data;
		more_data += sizeof(u64);
	}

I
Ingo Molnar 已提交
64 65 66 67 68 69
	if (sample_type & PERF_SAMPLE_CPU) {
		cpu = *(u32 *)more_data;
		more_data += sizeof(u32);
		more_data += sizeof(u32); /* reserved */
	}

F
Frederic Weisbecker 已提交
70 71 72 73 74
	if (sample_type & PERF_SAMPLE_PERIOD) {
		period = *(u64 *)more_data;
		more_data += sizeof(u64);
	}

75
	dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n",
F
Frederic Weisbecker 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88
		(void *)(offset + head),
		(void *)(long)(event->header.size),
		event->header.misc,
		event->ip.pid, event->ip.tid,
		(void *)(long)ip,
		(long long)period);

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

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

F
Frederic Weisbecker 已提交
91 92 93 94 95 96 97 98 99 100 101
	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
		 */
102
		print_event(cpu, raw->data, raw->size, timestamp, thread->comm);
F
Frederic Weisbecker 已提交
103 104 105 106 107 108
	}
	total += period;

	return 0;
}

109
static int sample_type_check(u64 type)
F
Frederic Weisbecker 已提交
110
{
111
	sample_type = type;
F
Frederic Weisbecker 已提交
112

113 114 115 116
	if (!(sample_type & PERF_SAMPLE_RAW)) {
		fprintf(stderr,
			"No trace sample to read. Did you call perf record "
			"without -R?");
F
Frederic Weisbecker 已提交
117 118 119 120 121 122
		return -1;
	}

	return 0;
}

123 124 125 126 127 128
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 已提交
129 130
static int __cmd_trace(void)
{
131
	register_idle_thread();
132
	register_perf_file_handler(&file_handler);
F
Frederic Weisbecker 已提交
133

134
	return mmap_dispatch_perf_file(&header, input_name, 0, 0, &cwdlen, &cwd);
F
Frederic Weisbecker 已提交
135 136 137 138 139 140 141 142 143 144 145 146
}

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)"),
147 148
	OPT_BOOLEAN('l', "latency", &latency_format,
		    "show latency attributes (irqs/preemption disabled, etc)"),
149
	OPT_END()
F
Frederic Weisbecker 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
};

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();
}