builtin-trace.c 7.4 KB
Newer Older
F
Frederic Weisbecker 已提交
1 2 3 4 5 6 7
#include "builtin.h"

#include "util/util.h"
#include "util/cache.h"
#include "util/symbol.h"
#include "util/thread.h"
#include "util/header.h"
I
Ingo Molnar 已提交
8 9
#include "util/exec_cmd.h"
#include "util/trace-event.h"
10
#include "util/session.h"
F
Frederic Weisbecker 已提交
11

T
Tom Zanussi 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
static char const		*script_name;
static char const		*generate_script_lang;

static int default_start_script(const char *script __attribute((unused)))
{
	return 0;
}

static int default_stop_script(void)
{
	return 0;
}

static int default_generate_script(const char *outfile __attribute ((unused)))
{
	return 0;
}

static struct scripting_ops default_scripting_ops = {
	.start_script		= default_start_script,
	.stop_script		= default_stop_script,
	.process_event		= print_event,
	.generate_script	= default_generate_script,
};

static struct scripting_ops	*scripting_ops;

static void setup_scripting(void)
{
	/* make sure PERF_EXEC_PATH is set for scripts */
	perf_set_argv_exec_path(perf_exec_path());

44 45
	setup_perl_scripting();

T
Tom Zanussi 已提交
46 47 48 49 50 51 52 53
	scripting_ops = &default_scripting_ops;
}

static int cleanup_scripting(void)
{
	return scripting_ops->stop_script();
}

F
Frederic Weisbecker 已提交
54 55 56 57 58 59
#include "util/parse-options.h"

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

#include "util/trace-event.h"
60
#include "util/data_map.h"
T
Tom Zanussi 已提交
61
#include "util/exec_cmd.h"
F
Frederic Weisbecker 已提交
62

T
Tom Zanussi 已提交
63
static char const		*input_name = "perf.data";
F
Frederic Weisbecker 已提交
64

65
static struct perf_session 	*session;
T
Tom Zanussi 已提交
66
static u64			sample_type;
F
Frederic Weisbecker 已提交
67

68
static int process_sample_event(event_t *event)
F
Frederic Weisbecker 已提交
69
{
70 71
	struct sample_data data;
	struct thread *thread;
72

73 74 75 76
	memset(&data, 0, sizeof(data));
	data.time = -1;
	data.cpu = -1;
	data.period = 1;
I
Ingo Molnar 已提交
77

78
	event__parse_sample(event, sample_type, &data);
F
Frederic Weisbecker 已提交
79

80
	dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
F
Frederic Weisbecker 已提交
81
		event->header.misc,
82 83 84
		data.pid, data.tid,
		(void *)(long)data.ip,
		(long long)data.period);
F
Frederic Weisbecker 已提交
85

86
	thread = threads__findnew(event->ip.pid);
F
Frederic Weisbecker 已提交
87
	if (thread == NULL) {
88 89
		pr_debug("problem processing %d event, skipping it.\n",
			 event->header.type);
F
Frederic Weisbecker 已提交
90 91 92 93 94 95 96 97 98
		return -1;
	}

	if (sample_type & PERF_SAMPLE_RAW) {
		/*
		 * FIXME: better resolve from pid from the struct trace_entry
		 * field, although it should be the same than this perf
		 * event pid
		 */
99 100 101
		scripting_ops->process_event(data.cpu, data.raw_data,
					     data.raw_size,
					     data.time, thread->comm);
F
Frederic Weisbecker 已提交
102
	}
103
	event__stats.total += data.period;
F
Frederic Weisbecker 已提交
104 105 106 107

	return 0;
}

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

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

	return 0;
}

122 123
static struct perf_file_handler file_handler = {
	.process_sample_event	= process_sample_event,
124
	.process_comm_event	= event__process_comm,
125 126 127
	.sample_type_check	= sample_type_check,
};

F
Frederic Weisbecker 已提交
128 129
static int __cmd_trace(void)
{
130 131 132 133 134 135
	int err;

	session = perf_session__new(input_name, O_RDONLY, 0);
	if (session == NULL)
		return -ENOMEM;

136
	register_idle_thread();
137
	register_perf_file_handler(&file_handler);
F
Frederic Weisbecker 已提交
138

139 140 141
	err = perf_session__process_events(session, 0, &event__cwdlen, &event__cwd);
	perf_session__delete(session);
	return err;
F
Frederic Weisbecker 已提交
142 143
}

T
Tom Zanussi 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 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 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 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
struct script_spec {
	struct list_head	node;
	struct scripting_ops	*ops;
	char			spec[0];
};

LIST_HEAD(script_specs);

static struct script_spec *script_spec__new(const char *spec,
					    struct scripting_ops *ops)
{
	struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);

	if (s != NULL) {
		strcpy(s->spec, spec);
		s->ops = ops;
	}

	return s;
}

static void script_spec__delete(struct script_spec *s)
{
	free(s->spec);
	free(s);
}

static void script_spec__add(struct script_spec *s)
{
	list_add_tail(&s->node, &script_specs);
}

static struct script_spec *script_spec__find(const char *spec)
{
	struct script_spec *s;

	list_for_each_entry(s, &script_specs, node)
		if (strcasecmp(s->spec, spec) == 0)
			return s;
	return NULL;
}

static struct script_spec *script_spec__findnew(const char *spec,
						struct scripting_ops *ops)
{
	struct script_spec *s = script_spec__find(spec);

	if (s)
		return s;

	s = script_spec__new(spec, ops);
	if (!s)
		goto out_delete_spec;

	script_spec__add(s);

	return s;

out_delete_spec:
	script_spec__delete(s);

	return NULL;
}

int script_spec_register(const char *spec, struct scripting_ops *ops)
{
	struct script_spec *s;

	s = script_spec__find(spec);
	if (s)
		return -1;

	s = script_spec__findnew(spec, ops);
	if (!s)
		return -1;

	return 0;
}

static struct scripting_ops *script_spec__lookup(const char *spec)
{
	struct script_spec *s = script_spec__find(spec);
	if (!s)
		return NULL;

	return s->ops;
}

static void list_available_languages(void)
{
	struct script_spec *s;

	fprintf(stderr, "\n");
	fprintf(stderr, "Scripting language extensions (used in "
		"perf trace -s [spec:]script.[spec]):\n\n");

	list_for_each_entry(s, &script_specs, node)
		fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name);

	fprintf(stderr, "\n");
}

static int parse_scriptname(const struct option *opt __used,
			    const char *str, int unset __used)
{
	char spec[PATH_MAX];
	const char *script, *ext;
	int len;

	if (strcmp(str, "list") == 0) {
		list_available_languages();
		return 0;
	}

	script = strchr(str, ':');
	if (script) {
		len = script - str;
		if (len >= PATH_MAX) {
			fprintf(stderr, "invalid language specifier");
			return -1;
		}
		strncpy(spec, str, len);
		spec[len] = '\0';
		scripting_ops = script_spec__lookup(spec);
		if (!scripting_ops) {
			fprintf(stderr, "invalid language specifier");
			return -1;
		}
		script++;
	} else {
		script = str;
		ext = strchr(script, '.');
		if (!ext) {
			fprintf(stderr, "invalid script extension");
			return -1;
		}
		scripting_ops = script_spec__lookup(++ext);
		if (!scripting_ops) {
			fprintf(stderr, "invalid script extension");
			return -1;
		}
	}

	script_name = strdup(script);

	return 0;
}

F
Frederic Weisbecker 已提交
292 293 294 295 296 297 298 299 300 301
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)"),
302 303
	OPT_BOOLEAN('l', "latency", &latency_format,
		    "show latency attributes (irqs/preemption disabled, etc)"),
T
Tom Zanussi 已提交
304 305 306 307 308 309
	OPT_CALLBACK('s', "script", NULL, "name",
		     "script file name (lang:script name, script name, or *)",
		     parse_scriptname),
	OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
		   "generate perf-trace.xx script in specified language"),

310
	OPT_END()
F
Frederic Weisbecker 已提交
311 312 313 314
};

int cmd_trace(int argc, const char **argv, const char *prefix __used)
{
T
Tom Zanussi 已提交
315 316
	int err;

317
	symbol__init(0);
F
Frederic Weisbecker 已提交
318

T
Tom Zanussi 已提交
319 320
	setup_scripting();

F
Frederic Weisbecker 已提交
321 322 323 324 325 326 327 328 329 330 331 332
	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();

T
Tom Zanussi 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	if (generate_script_lang) {
		struct stat perf_stat;

		int input = open(input_name, O_RDONLY);
		if (input < 0) {
			perror("failed to open file");
			exit(-1);
		}

		err = fstat(input, &perf_stat);
		if (err < 0) {
			perror("failed to stat file");
			exit(-1);
		}

		if (!perf_stat.st_size) {
			fprintf(stderr, "zero-sized file, nothing to do!\n");
			exit(0);
		}

		scripting_ops = script_spec__lookup(generate_script_lang);
		if (!scripting_ops) {
			fprintf(stderr, "invalid language specifier");
			return -1;
		}

359
		perf_header__read(&session->header, input);
T
Tom Zanussi 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374
		err = scripting_ops->generate_script("perf-trace");
		goto out;
	}

	if (script_name) {
		err = scripting_ops->start_script(script_name);
		if (err)
			goto out;
	}

	err = __cmd_trace();

	cleanup_scripting();
out:
	return err;
F
Frederic Weisbecker 已提交
375
}