| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1 | #include "builtin.h" | 
 | 2 |  | 
| Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 3 | #include "perf.h" | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 4 | #include "util/cache.h" | 
| Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 5 | #include "util/debug.h" | 
 | 6 | #include "util/exec_cmd.h" | 
 | 7 | #include "util/header.h" | 
 | 8 | #include "util/parse-options.h" | 
 | 9 | #include "util/session.h" | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 10 | #include "util/symbol.h" | 
 | 11 | #include "util/thread.h" | 
| Ingo Molnar | cf72344 | 2009-11-28 10:11:00 +0100 | [diff] [blame] | 12 | #include "util/trace-event.h" | 
| Andrea Gelmini | b7eead8 | 2010-08-05 15:51:38 +0200 | [diff] [blame] | 13 | #include "util/util.h" | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 14 | #include "util/evlist.h" | 
 | 15 | #include "util/evsel.h" | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 16 |  | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 17 | static char const		*script_name; | 
 | 18 | static char const		*generate_script_lang; | 
| Frederic Weisbecker | ffabd99 | 2010-05-27 16:27:47 +0200 | [diff] [blame] | 19 | static bool			debug_mode; | 
| Frederic Weisbecker | e1889d7 | 2010-04-24 01:55:09 +0200 | [diff] [blame] | 20 | static u64			last_timestamp; | 
| Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 21 | static u64			nr_unordered; | 
| Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 22 | extern const struct option	record_options[]; | 
| David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 23 | static bool			no_callchain; | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 24 |  | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 25 | enum perf_output_field { | 
 | 26 | 	PERF_OUTPUT_COMM            = 1U << 0, | 
 | 27 | 	PERF_OUTPUT_TID             = 1U << 1, | 
 | 28 | 	PERF_OUTPUT_PID             = 1U << 2, | 
 | 29 | 	PERF_OUTPUT_TIME            = 1U << 3, | 
 | 30 | 	PERF_OUTPUT_CPU             = 1U << 4, | 
 | 31 | 	PERF_OUTPUT_EVNAME          = 1U << 5, | 
 | 32 | 	PERF_OUTPUT_TRACE           = 1U << 6, | 
| David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 33 | 	PERF_OUTPUT_SYM             = 1U << 7, | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 34 | }; | 
 | 35 |  | 
 | 36 | struct output_option { | 
 | 37 | 	const char *str; | 
 | 38 | 	enum perf_output_field field; | 
 | 39 | } all_output_options[] = { | 
 | 40 | 	{.str = "comm",  .field = PERF_OUTPUT_COMM}, | 
 | 41 | 	{.str = "tid",   .field = PERF_OUTPUT_TID}, | 
 | 42 | 	{.str = "pid",   .field = PERF_OUTPUT_PID}, | 
 | 43 | 	{.str = "time",  .field = PERF_OUTPUT_TIME}, | 
 | 44 | 	{.str = "cpu",   .field = PERF_OUTPUT_CPU}, | 
 | 45 | 	{.str = "event", .field = PERF_OUTPUT_EVNAME}, | 
 | 46 | 	{.str = "trace", .field = PERF_OUTPUT_TRACE}, | 
| David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 47 | 	{.str = "sym",   .field = PERF_OUTPUT_SYM}, | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 48 | }; | 
 | 49 |  | 
 | 50 | /* default set to maintain compatibility with current format */ | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 51 | static struct { | 
 | 52 | 	bool user_set; | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 53 | 	bool wildcard_set; | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 54 | 	u64 fields; | 
 | 55 | 	u64 invalid_fields; | 
 | 56 | } output[PERF_TYPE_MAX] = { | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 57 |  | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 58 | 	[PERF_TYPE_HARDWARE] = { | 
 | 59 | 		.user_set = false, | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 60 |  | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 61 | 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | 
 | 62 | 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | 
 | 63 | 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM, | 
 | 64 |  | 
 | 65 | 		.invalid_fields = PERF_OUTPUT_TRACE, | 
 | 66 | 	}, | 
 | 67 |  | 
 | 68 | 	[PERF_TYPE_SOFTWARE] = { | 
 | 69 | 		.user_set = false, | 
 | 70 |  | 
 | 71 | 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | 
 | 72 | 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | 
 | 73 | 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM, | 
 | 74 |  | 
 | 75 | 		.invalid_fields = PERF_OUTPUT_TRACE, | 
 | 76 | 	}, | 
 | 77 |  | 
 | 78 | 	[PERF_TYPE_TRACEPOINT] = { | 
 | 79 | 		.user_set = false, | 
 | 80 |  | 
 | 81 | 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | 
 | 82 | 				  PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | 
 | 83 | 				  PERF_OUTPUT_EVNAME | PERF_OUTPUT_TRACE, | 
 | 84 | 	}, | 
| Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 85 |  | 
 | 86 | 	[PERF_TYPE_RAW] = { | 
 | 87 | 		.user_set = false, | 
 | 88 |  | 
 | 89 | 		.fields = PERF_OUTPUT_COMM | PERF_OUTPUT_TID | | 
 | 90 | 			      PERF_OUTPUT_CPU | PERF_OUTPUT_TIME | | 
 | 91 | 			      PERF_OUTPUT_EVNAME | PERF_OUTPUT_SYM, | 
 | 92 |  | 
 | 93 | 		.invalid_fields = PERF_OUTPUT_TRACE, | 
 | 94 | 	}, | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 95 | }; | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 96 |  | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 97 | static bool output_set_by_user(void) | 
 | 98 | { | 
 | 99 | 	int j; | 
 | 100 | 	for (j = 0; j < PERF_TYPE_MAX; ++j) { | 
 | 101 | 		if (output[j].user_set) | 
 | 102 | 			return true; | 
 | 103 | 	} | 
 | 104 | 	return false; | 
 | 105 | } | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 106 |  | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 107 | static const char *output_field2str(enum perf_output_field field) | 
 | 108 | { | 
 | 109 | 	int i, imax = ARRAY_SIZE(all_output_options); | 
 | 110 | 	const char *str = ""; | 
 | 111 |  | 
 | 112 | 	for (i = 0; i < imax; ++i) { | 
 | 113 | 		if (all_output_options[i].field == field) { | 
 | 114 | 			str = all_output_options[i].str; | 
 | 115 | 			break; | 
 | 116 | 		} | 
 | 117 | 	} | 
 | 118 | 	return str; | 
 | 119 | } | 
 | 120 |  | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 121 | #define PRINT_FIELD(x)  (output[attr->type].fields & PERF_OUTPUT_##x) | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 122 |  | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 123 | static int perf_event_attr__check_stype(struct perf_event_attr *attr, | 
 | 124 | 				  u64 sample_type, const char *sample_msg, | 
 | 125 | 				  enum perf_output_field field) | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 126 | { | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 127 | 	int type = attr->type; | 
 | 128 | 	const char *evname; | 
 | 129 |  | 
 | 130 | 	if (attr->sample_type & sample_type) | 
 | 131 | 		return 0; | 
 | 132 |  | 
 | 133 | 	if (output[type].user_set) { | 
 | 134 | 		evname = __event_name(attr->type, attr->config); | 
 | 135 | 		pr_err("Samples for '%s' event do not have %s attribute set. " | 
 | 136 | 		       "Cannot print '%s' field.\n", | 
 | 137 | 		       evname, sample_msg, output_field2str(field)); | 
 | 138 | 		return -1; | 
 | 139 | 	} | 
 | 140 |  | 
 | 141 | 	/* user did not ask for it explicitly so remove from the default list */ | 
 | 142 | 	output[type].fields &= ~field; | 
 | 143 | 	evname = __event_name(attr->type, attr->config); | 
 | 144 | 	pr_debug("Samples for '%s' event do not have %s attribute set. " | 
 | 145 | 		 "Skipping '%s' field.\n", | 
 | 146 | 		 evname, sample_msg, output_field2str(field)); | 
 | 147 |  | 
 | 148 | 	return 0; | 
 | 149 | } | 
 | 150 |  | 
 | 151 | static int perf_evsel__check_attr(struct perf_evsel *evsel, | 
 | 152 | 				  struct perf_session *session) | 
 | 153 | { | 
 | 154 | 	struct perf_event_attr *attr = &evsel->attr; | 
 | 155 |  | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 156 | 	if (PRINT_FIELD(TRACE) && | 
 | 157 | 		!perf_session__has_traces(session, "record -R")) | 
 | 158 | 		return -EINVAL; | 
 | 159 |  | 
 | 160 | 	if (PRINT_FIELD(SYM)) { | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 161 | 		if (perf_event_attr__check_stype(attr, PERF_SAMPLE_IP, "IP", | 
 | 162 | 					   PERF_OUTPUT_SYM)) | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 163 | 			return -EINVAL; | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 164 |  | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 165 | 		if (!no_callchain && | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 166 | 		    !(attr->sample_type & PERF_SAMPLE_CALLCHAIN)) | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 167 | 			symbol_conf.use_callchain = false; | 
 | 168 | 	} | 
 | 169 |  | 
 | 170 | 	if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) && | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 171 | 		perf_event_attr__check_stype(attr, PERF_SAMPLE_TID, "TID", | 
 | 172 | 				       PERF_OUTPUT_TID|PERF_OUTPUT_PID)) | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 173 | 		return -EINVAL; | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 174 |  | 
 | 175 | 	if (PRINT_FIELD(TIME) && | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 176 | 		perf_event_attr__check_stype(attr, PERF_SAMPLE_TIME, "TIME", | 
 | 177 | 				       PERF_OUTPUT_TIME)) | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 178 | 		return -EINVAL; | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 179 |  | 
 | 180 | 	if (PRINT_FIELD(CPU) && | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 181 | 		perf_event_attr__check_stype(attr, PERF_SAMPLE_CPU, "CPU", | 
 | 182 | 				       PERF_OUTPUT_CPU)) | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 183 | 		return -EINVAL; | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 184 |  | 
 | 185 | 	return 0; | 
 | 186 | } | 
 | 187 |  | 
 | 188 | /* | 
 | 189 |  * verify all user requested events exist and the samples | 
 | 190 |  * have the expected data | 
 | 191 |  */ | 
 | 192 | static int perf_session__check_output_opt(struct perf_session *session) | 
 | 193 | { | 
 | 194 | 	int j; | 
 | 195 | 	struct perf_evsel *evsel; | 
 | 196 |  | 
 | 197 | 	for (j = 0; j < PERF_TYPE_MAX; ++j) { | 
 | 198 | 		evsel = perf_session__find_first_evtype(session, j); | 
 | 199 |  | 
 | 200 | 		/* | 
 | 201 | 		 * even if fields is set to 0 (ie., show nothing) event must | 
 | 202 | 		 * exist if user explicitly includes it on the command line | 
 | 203 | 		 */ | 
 | 204 | 		if (!evsel && output[j].user_set && !output[j].wildcard_set) { | 
 | 205 | 			pr_err("%s events do not exist. " | 
 | 206 | 			       "Remove corresponding -f option to proceed.\n", | 
 | 207 | 			       event_type(j)); | 
 | 208 | 			return -1; | 
 | 209 | 		} | 
 | 210 |  | 
 | 211 | 		if (evsel && output[j].fields && | 
 | 212 | 			perf_evsel__check_attr(evsel, session)) | 
 | 213 | 			return -1; | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 214 | 	} | 
 | 215 |  | 
 | 216 | 	return 0; | 
 | 217 | } | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 218 |  | 
| David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 219 | static void print_sample_start(struct perf_sample *sample, | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 220 | 			       struct thread *thread, | 
 | 221 | 			       struct perf_event_attr *attr) | 
| David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 222 | { | 
 | 223 | 	int type; | 
 | 224 | 	struct event *event; | 
 | 225 | 	const char *evname = NULL; | 
 | 226 | 	unsigned long secs; | 
 | 227 | 	unsigned long usecs; | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 228 | 	unsigned long long nsecs; | 
| David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 229 |  | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 230 | 	if (PRINT_FIELD(COMM)) { | 
 | 231 | 		if (latency_format) | 
 | 232 | 			printf("%8.8s ", thread->comm); | 
| David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 233 | 		else if (PRINT_FIELD(SYM) && symbol_conf.use_callchain) | 
 | 234 | 			printf("%s ", thread->comm); | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 235 | 		else | 
 | 236 | 			printf("%16s ", thread->comm); | 
 | 237 | 	} | 
| David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 238 |  | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 239 | 	if (PRINT_FIELD(PID) && PRINT_FIELD(TID)) | 
 | 240 | 		printf("%5d/%-5d ", sample->pid, sample->tid); | 
 | 241 | 	else if (PRINT_FIELD(PID)) | 
 | 242 | 		printf("%5d ", sample->pid); | 
 | 243 | 	else if (PRINT_FIELD(TID)) | 
 | 244 | 		printf("%5d ", sample->tid); | 
| David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 245 |  | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 246 | 	if (PRINT_FIELD(CPU)) { | 
 | 247 | 		if (latency_format) | 
 | 248 | 			printf("%3d ", sample->cpu); | 
 | 249 | 		else | 
 | 250 | 			printf("[%03d] ", sample->cpu); | 
 | 251 | 	} | 
| David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 252 |  | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 253 | 	if (PRINT_FIELD(TIME)) { | 
 | 254 | 		nsecs = sample->time; | 
 | 255 | 		secs = nsecs / NSECS_PER_SEC; | 
 | 256 | 		nsecs -= secs * NSECS_PER_SEC; | 
 | 257 | 		usecs = nsecs / NSECS_PER_USEC; | 
 | 258 | 		printf("%5lu.%06lu: ", secs, usecs); | 
 | 259 | 	} | 
 | 260 |  | 
 | 261 | 	if (PRINT_FIELD(EVNAME)) { | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 262 | 		if (attr->type == PERF_TYPE_TRACEPOINT) { | 
 | 263 | 			type = trace_parse_common_type(sample->raw_data); | 
 | 264 | 			event = trace_find_event(type); | 
 | 265 | 			if (event) | 
 | 266 | 				evname = event->name; | 
 | 267 | 		} else | 
 | 268 | 			evname = __event_name(attr->type, attr->config); | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 269 |  | 
 | 270 | 		printf("%s: ", evname ? evname : "(unknown)"); | 
 | 271 | 	} | 
| David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 272 | } | 
 | 273 |  | 
| David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 274 | static void process_event(union perf_event *event __unused, | 
 | 275 | 			  struct perf_sample *sample, | 
| Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 276 | 			  struct perf_evsel *evsel, | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 277 | 			  struct perf_session *session, | 
| David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 278 | 			  struct thread *thread) | 
 | 279 | { | 
| Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 280 | 	struct perf_event_attr *attr = &evsel->attr; | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 281 |  | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 282 | 	if (output[attr->type].fields == 0) | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 283 | 		return; | 
 | 284 |  | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 285 | 	print_sample_start(sample, thread, attr); | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 286 |  | 
 | 287 | 	if (PRINT_FIELD(TRACE)) | 
 | 288 | 		print_trace_event(sample->cpu, sample->raw_data, | 
 | 289 | 				  sample->raw_size); | 
 | 290 |  | 
| David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 291 | 	if (PRINT_FIELD(SYM)) { | 
 | 292 | 		if (!symbol_conf.use_callchain) | 
 | 293 | 			printf(" "); | 
 | 294 | 		else | 
 | 295 | 			printf("\n"); | 
 | 296 | 		perf_session__print_symbols(event, sample, session); | 
 | 297 | 	} | 
 | 298 |  | 
| David Ahern | c70c94b | 2011-03-09 22:23:25 -0700 | [diff] [blame] | 299 | 	printf("\n"); | 
| David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 300 | } | 
 | 301 |  | 
| Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 302 | static int default_start_script(const char *script __unused, | 
 | 303 | 				int argc __unused, | 
 | 304 | 				const char **argv __unused) | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 305 | { | 
 | 306 | 	return 0; | 
 | 307 | } | 
 | 308 |  | 
 | 309 | static int default_stop_script(void) | 
 | 310 | { | 
 | 311 | 	return 0; | 
 | 312 | } | 
 | 313 |  | 
| Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 314 | static int default_generate_script(const char *outfile __unused) | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 315 | { | 
 | 316 | 	return 0; | 
 | 317 | } | 
 | 318 |  | 
 | 319 | static struct scripting_ops default_scripting_ops = { | 
 | 320 | 	.start_script		= default_start_script, | 
 | 321 | 	.stop_script		= default_stop_script, | 
| David Ahern | be6d842 | 2011-03-09 22:23:23 -0700 | [diff] [blame] | 322 | 	.process_event		= process_event, | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 323 | 	.generate_script	= default_generate_script, | 
 | 324 | }; | 
 | 325 |  | 
 | 326 | static struct scripting_ops	*scripting_ops; | 
 | 327 |  | 
 | 328 | static void setup_scripting(void) | 
 | 329 | { | 
| Tom Zanussi | 16c632d | 2009-11-25 01:15:48 -0600 | [diff] [blame] | 330 | 	setup_perl_scripting(); | 
| Tom Zanussi | 7e4b21b | 2010-01-27 02:27:57 -0600 | [diff] [blame] | 331 | 	setup_python_scripting(); | 
| Tom Zanussi | 16c632d | 2009-11-25 01:15:48 -0600 | [diff] [blame] | 332 |  | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 333 | 	scripting_ops = &default_scripting_ops; | 
 | 334 | } | 
 | 335 |  | 
 | 336 | static int cleanup_scripting(void) | 
 | 337 | { | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 338 | 	pr_debug("\nperf script stopped\n"); | 
| Tom Zanussi | 3824a4e | 2010-05-09 23:46:57 -0500 | [diff] [blame] | 339 |  | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 340 | 	return scripting_ops->stop_script(); | 
 | 341 | } | 
 | 342 |  | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 343 | static char const		*input_name = "perf.data"; | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 344 |  | 
| Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 345 | static int process_sample_event(union perf_event *event, | 
 | 346 | 				struct perf_sample *sample, | 
| Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 347 | 				struct perf_evsel *evsel, | 
| Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 348 | 				struct perf_session *session) | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 349 | { | 
| Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 350 | 	struct thread *thread = perf_session__findnew(session, event->ip.pid); | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 351 |  | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 352 | 	if (thread == NULL) { | 
| Arnaldo Carvalho de Melo | 6beba7a | 2009-10-21 17:34:06 -0200 | [diff] [blame] | 353 | 		pr_debug("problem processing %d event, skipping it.\n", | 
 | 354 | 			 event->header.type); | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 355 | 		return -1; | 
 | 356 | 	} | 
 | 357 |  | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 358 | 	if (debug_mode) { | 
 | 359 | 		if (sample->time < last_timestamp) { | 
 | 360 | 			pr_err("Samples misordered, previous: %" PRIu64 | 
 | 361 | 				" this: %" PRIu64 "\n", last_timestamp, | 
 | 362 | 				sample->time); | 
 | 363 | 			nr_unordered++; | 
| Frederic Weisbecker | e1889d7 | 2010-04-24 01:55:09 +0200 | [diff] [blame] | 364 | 		} | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 365 | 		last_timestamp = sample->time; | 
 | 366 | 		return 0; | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 367 | 	} | 
| Arnaldo Carvalho de Melo | 9e69c21 | 2011-03-15 15:44:01 -0300 | [diff] [blame] | 368 | 	scripting_ops->process_event(event, sample, evsel, session, thread); | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 369 |  | 
| Arnaldo Carvalho de Melo | 640c03c | 2010-12-02 14:10:21 -0200 | [diff] [blame] | 370 | 	session->hists.stats.total_period += sample->period; | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 371 | 	return 0; | 
 | 372 | } | 
 | 373 |  | 
| Arnaldo Carvalho de Melo | 301a0b0 | 2009-12-13 19:50:25 -0200 | [diff] [blame] | 374 | static struct perf_event_ops event_ops = { | 
| Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 375 | 	.sample		 = process_sample_event, | 
| David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 376 | 	.mmap		 = perf_event__process_mmap, | 
| Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 377 | 	.comm		 = perf_event__process_comm, | 
| David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 378 | 	.exit		 = perf_event__process_task, | 
 | 379 | 	.fork		 = perf_event__process_task, | 
| Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 380 | 	.attr		 = perf_event__process_attr, | 
 | 381 | 	.event_type	 = perf_event__process_event_type, | 
 | 382 | 	.tracing_data	 = perf_event__process_tracing_data, | 
 | 383 | 	.build_id	 = perf_event__process_build_id, | 
| Frederic Weisbecker | e0a808c | 2010-04-24 00:38:33 +0200 | [diff] [blame] | 384 | 	.ordered_samples = true, | 
| Arnaldo Carvalho de Melo | 8115d60 | 2011-01-29 14:01:45 -0200 | [diff] [blame] | 385 | 	.ordering_requires_timestamps = true, | 
| Frederic Weisbecker | 016e92f | 2009-10-07 12:47:31 +0200 | [diff] [blame] | 386 | }; | 
 | 387 |  | 
| Tom Zanussi | c239da3 | 2010-04-01 23:59:18 -0500 | [diff] [blame] | 388 | extern volatile int session_done; | 
 | 389 |  | 
 | 390 | static void sig_handler(int sig __unused) | 
 | 391 | { | 
 | 392 | 	session_done = 1; | 
 | 393 | } | 
 | 394 |  | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 395 | static int __cmd_script(struct perf_session *session) | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 396 | { | 
| Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 397 | 	int ret; | 
 | 398 |  | 
| Tom Zanussi | c239da3 | 2010-04-01 23:59:18 -0500 | [diff] [blame] | 399 | 	signal(SIGINT, sig_handler); | 
 | 400 |  | 
| Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 401 | 	ret = perf_session__process_events(session, &event_ops); | 
 | 402 |  | 
| Arnaldo Carvalho de Melo | 6d8afb5 | 2011-01-04 16:27:30 -0200 | [diff] [blame] | 403 | 	if (debug_mode) | 
| Arnaldo Carvalho de Melo | 9486aa3 | 2011-01-22 20:37:02 -0200 | [diff] [blame] | 404 | 		pr_err("Misordered timestamps: %" PRIu64 "\n", nr_unordered); | 
| Frederic Weisbecker | 6fcf7dd | 2010-05-27 15:46:25 +0200 | [diff] [blame] | 405 |  | 
 | 406 | 	return ret; | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 407 | } | 
 | 408 |  | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 409 | struct script_spec { | 
 | 410 | 	struct list_head	node; | 
 | 411 | 	struct scripting_ops	*ops; | 
 | 412 | 	char			spec[0]; | 
 | 413 | }; | 
 | 414 |  | 
| Arnaldo Carvalho de Melo | eccdfe2 | 2011-01-04 16:32:52 -0200 | [diff] [blame] | 415 | static LIST_HEAD(script_specs); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 416 |  | 
 | 417 | static struct script_spec *script_spec__new(const char *spec, | 
 | 418 | 					    struct scripting_ops *ops) | 
 | 419 | { | 
 | 420 | 	struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); | 
 | 421 |  | 
 | 422 | 	if (s != NULL) { | 
 | 423 | 		strcpy(s->spec, spec); | 
 | 424 | 		s->ops = ops; | 
 | 425 | 	} | 
 | 426 |  | 
 | 427 | 	return s; | 
 | 428 | } | 
 | 429 |  | 
 | 430 | static void script_spec__delete(struct script_spec *s) | 
 | 431 | { | 
 | 432 | 	free(s->spec); | 
 | 433 | 	free(s); | 
 | 434 | } | 
 | 435 |  | 
 | 436 | static void script_spec__add(struct script_spec *s) | 
 | 437 | { | 
 | 438 | 	list_add_tail(&s->node, &script_specs); | 
 | 439 | } | 
 | 440 |  | 
 | 441 | static struct script_spec *script_spec__find(const char *spec) | 
 | 442 | { | 
 | 443 | 	struct script_spec *s; | 
 | 444 |  | 
 | 445 | 	list_for_each_entry(s, &script_specs, node) | 
 | 446 | 		if (strcasecmp(s->spec, spec) == 0) | 
 | 447 | 			return s; | 
 | 448 | 	return NULL; | 
 | 449 | } | 
 | 450 |  | 
 | 451 | static struct script_spec *script_spec__findnew(const char *spec, | 
 | 452 | 						struct scripting_ops *ops) | 
 | 453 | { | 
 | 454 | 	struct script_spec *s = script_spec__find(spec); | 
 | 455 |  | 
 | 456 | 	if (s) | 
 | 457 | 		return s; | 
 | 458 |  | 
 | 459 | 	s = script_spec__new(spec, ops); | 
 | 460 | 	if (!s) | 
 | 461 | 		goto out_delete_spec; | 
 | 462 |  | 
 | 463 | 	script_spec__add(s); | 
 | 464 |  | 
 | 465 | 	return s; | 
 | 466 |  | 
 | 467 | out_delete_spec: | 
 | 468 | 	script_spec__delete(s); | 
 | 469 |  | 
 | 470 | 	return NULL; | 
 | 471 | } | 
 | 472 |  | 
 | 473 | int script_spec_register(const char *spec, struct scripting_ops *ops) | 
 | 474 | { | 
 | 475 | 	struct script_spec *s; | 
 | 476 |  | 
 | 477 | 	s = script_spec__find(spec); | 
 | 478 | 	if (s) | 
 | 479 | 		return -1; | 
 | 480 |  | 
 | 481 | 	s = script_spec__findnew(spec, ops); | 
 | 482 | 	if (!s) | 
 | 483 | 		return -1; | 
 | 484 |  | 
 | 485 | 	return 0; | 
 | 486 | } | 
 | 487 |  | 
 | 488 | static struct scripting_ops *script_spec__lookup(const char *spec) | 
 | 489 | { | 
 | 490 | 	struct script_spec *s = script_spec__find(spec); | 
 | 491 | 	if (!s) | 
 | 492 | 		return NULL; | 
 | 493 |  | 
 | 494 | 	return s->ops; | 
 | 495 | } | 
 | 496 |  | 
 | 497 | static void list_available_languages(void) | 
 | 498 | { | 
 | 499 | 	struct script_spec *s; | 
 | 500 |  | 
 | 501 | 	fprintf(stderr, "\n"); | 
 | 502 | 	fprintf(stderr, "Scripting language extensions (used in " | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 503 | 		"perf script -s [spec:]script.[spec]):\n\n"); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 504 |  | 
 | 505 | 	list_for_each_entry(s, &script_specs, node) | 
 | 506 | 		fprintf(stderr, "  %-42s [%s]\n", s->spec, s->ops->name); | 
 | 507 |  | 
 | 508 | 	fprintf(stderr, "\n"); | 
 | 509 | } | 
 | 510 |  | 
 | 511 | static int parse_scriptname(const struct option *opt __used, | 
 | 512 | 			    const char *str, int unset __used) | 
 | 513 | { | 
 | 514 | 	char spec[PATH_MAX]; | 
 | 515 | 	const char *script, *ext; | 
 | 516 | 	int len; | 
 | 517 |  | 
| Tom Zanussi | f526d68 | 2010-01-27 02:27:52 -0600 | [diff] [blame] | 518 | 	if (strcmp(str, "lang") == 0) { | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 519 | 		list_available_languages(); | 
| Tom Zanussi | f526d68 | 2010-01-27 02:27:52 -0600 | [diff] [blame] | 520 | 		exit(0); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 521 | 	} | 
 | 522 |  | 
 | 523 | 	script = strchr(str, ':'); | 
 | 524 | 	if (script) { | 
 | 525 | 		len = script - str; | 
 | 526 | 		if (len >= PATH_MAX) { | 
 | 527 | 			fprintf(stderr, "invalid language specifier"); | 
 | 528 | 			return -1; | 
 | 529 | 		} | 
 | 530 | 		strncpy(spec, str, len); | 
 | 531 | 		spec[len] = '\0'; | 
 | 532 | 		scripting_ops = script_spec__lookup(spec); | 
 | 533 | 		if (!scripting_ops) { | 
 | 534 | 			fprintf(stderr, "invalid language specifier"); | 
 | 535 | 			return -1; | 
 | 536 | 		} | 
 | 537 | 		script++; | 
 | 538 | 	} else { | 
 | 539 | 		script = str; | 
| Ben Hutchings | d1e95bb | 2010-10-10 16:11:02 +0100 | [diff] [blame] | 540 | 		ext = strrchr(script, '.'); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 541 | 		if (!ext) { | 
 | 542 | 			fprintf(stderr, "invalid script extension"); | 
 | 543 | 			return -1; | 
 | 544 | 		} | 
 | 545 | 		scripting_ops = script_spec__lookup(++ext); | 
 | 546 | 		if (!scripting_ops) { | 
 | 547 | 			fprintf(stderr, "invalid script extension"); | 
 | 548 | 			return -1; | 
 | 549 | 		} | 
 | 550 | 	} | 
 | 551 |  | 
 | 552 | 	script_name = strdup(script); | 
 | 553 |  | 
 | 554 | 	return 0; | 
 | 555 | } | 
 | 556 |  | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 557 | static int parse_output_fields(const struct option *opt __used, | 
 | 558 | 			    const char *arg, int unset __used) | 
 | 559 | { | 
 | 560 | 	char *tok; | 
 | 561 | 	int i, imax = sizeof(all_output_options) / sizeof(struct output_option); | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 562 | 	int j; | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 563 | 	int rc = 0; | 
 | 564 | 	char *str = strdup(arg); | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 565 | 	int type = -1; | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 566 |  | 
 | 567 | 	if (!str) | 
 | 568 | 		return -ENOMEM; | 
 | 569 |  | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 570 | 	/* first word can state for which event type the user is specifying | 
 | 571 | 	 * the fields. If no type exists, the specified fields apply to all | 
 | 572 | 	 * event types found in the file minus the invalid fields for a type. | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 573 | 	 */ | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 574 | 	tok = strchr(str, ':'); | 
 | 575 | 	if (tok) { | 
 | 576 | 		*tok = '\0'; | 
 | 577 | 		tok++; | 
 | 578 | 		if (!strcmp(str, "hw")) | 
 | 579 | 			type = PERF_TYPE_HARDWARE; | 
 | 580 | 		else if (!strcmp(str, "sw")) | 
 | 581 | 			type = PERF_TYPE_SOFTWARE; | 
 | 582 | 		else if (!strcmp(str, "trace")) | 
 | 583 | 			type = PERF_TYPE_TRACEPOINT; | 
| Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 584 | 		else if (!strcmp(str, "raw")) | 
 | 585 | 			type = PERF_TYPE_RAW; | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 586 | 		else { | 
 | 587 | 			fprintf(stderr, "Invalid event type in field string.\n"); | 
 | 588 | 			return -EINVAL; | 
 | 589 | 		} | 
 | 590 |  | 
 | 591 | 		if (output[type].user_set) | 
 | 592 | 			pr_warning("Overriding previous field request for %s events.\n", | 
 | 593 | 				   event_type(type)); | 
 | 594 |  | 
 | 595 | 		output[type].fields = 0; | 
 | 596 | 		output[type].user_set = true; | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 597 | 		output[type].wildcard_set = false; | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 598 |  | 
 | 599 | 	} else { | 
 | 600 | 		tok = str; | 
 | 601 | 		if (strlen(str) == 0) { | 
 | 602 | 			fprintf(stderr, | 
 | 603 | 				"Cannot set fields to 'none' for all event types.\n"); | 
 | 604 | 			rc = -EINVAL; | 
 | 605 | 			goto out; | 
 | 606 | 		} | 
 | 607 |  | 
 | 608 | 		if (output_set_by_user()) | 
 | 609 | 			pr_warning("Overriding previous field request for all events.\n"); | 
 | 610 |  | 
 | 611 | 		for (j = 0; j < PERF_TYPE_MAX; ++j) { | 
 | 612 | 			output[j].fields = 0; | 
 | 613 | 			output[j].user_set = true; | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 614 | 			output[j].wildcard_set = true; | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 615 | 		} | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 616 | 	} | 
 | 617 |  | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 618 | 	tok = strtok(tok, ","); | 
 | 619 | 	while (tok) { | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 620 | 		for (i = 0; i < imax; ++i) { | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 621 | 			if (strcmp(tok, all_output_options[i].str) == 0) | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 622 | 				break; | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 623 | 		} | 
 | 624 | 		if (i == imax) { | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 625 | 			fprintf(stderr, "Invalid field requested.\n"); | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 626 | 			rc = -EINVAL; | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 627 | 			goto out; | 
 | 628 | 		} | 
 | 629 |  | 
 | 630 | 		if (type == -1) { | 
 | 631 | 			/* add user option to all events types for | 
 | 632 | 			 * which it is valid | 
 | 633 | 			 */ | 
 | 634 | 			for (j = 0; j < PERF_TYPE_MAX; ++j) { | 
 | 635 | 				if (output[j].invalid_fields & all_output_options[i].field) { | 
 | 636 | 					pr_warning("\'%s\' not valid for %s events. Ignoring.\n", | 
 | 637 | 						   all_output_options[i].str, event_type(j)); | 
 | 638 | 				} else | 
 | 639 | 					output[j].fields |= all_output_options[i].field; | 
 | 640 | 			} | 
 | 641 | 		} else { | 
 | 642 | 			if (output[type].invalid_fields & all_output_options[i].field) { | 
 | 643 | 				fprintf(stderr, "\'%s\' not valid for %s events.\n", | 
 | 644 | 					 all_output_options[i].str, event_type(type)); | 
 | 645 |  | 
 | 646 | 				rc = -EINVAL; | 
 | 647 | 				goto out; | 
 | 648 | 			} | 
 | 649 | 			output[type].fields |= all_output_options[i].field; | 
 | 650 | 		} | 
 | 651 |  | 
 | 652 | 		tok = strtok(NULL, ","); | 
 | 653 | 	} | 
 | 654 |  | 
 | 655 | 	if (type >= 0) { | 
 | 656 | 		if (output[type].fields == 0) { | 
 | 657 | 			pr_debug("No fields requested for %s type. " | 
 | 658 | 				 "Events will not be displayed.\n", event_type(type)); | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 659 | 		} | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 660 | 	} | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 661 |  | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 662 | out: | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 663 | 	free(str); | 
 | 664 | 	return rc; | 
 | 665 | } | 
 | 666 |  | 
| Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 667 | /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */ | 
 | 668 | static int is_directory(const char *base_path, const struct dirent *dent) | 
 | 669 | { | 
 | 670 | 	char path[PATH_MAX]; | 
 | 671 | 	struct stat st; | 
 | 672 |  | 
 | 673 | 	sprintf(path, "%s/%s", base_path, dent->d_name); | 
 | 674 | 	if (stat(path, &st)) | 
 | 675 | 		return 0; | 
 | 676 |  | 
 | 677 | 	return S_ISDIR(st.st_mode); | 
 | 678 | } | 
 | 679 |  | 
 | 680 | #define for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next)\ | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 681 | 	while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) &&	\ | 
 | 682 | 	       lang_next)						\ | 
| Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 683 | 		if ((lang_dirent.d_type == DT_DIR ||			\ | 
 | 684 | 		     (lang_dirent.d_type == DT_UNKNOWN &&		\ | 
 | 685 | 		      is_directory(scripts_path, &lang_dirent))) &&	\ | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 686 | 		    (strcmp(lang_dirent.d_name, ".")) &&		\ | 
 | 687 | 		    (strcmp(lang_dirent.d_name, ".."))) | 
 | 688 |  | 
| Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 689 | #define for_each_script(lang_path, lang_dir, script_dirent, script_next)\ | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 690 | 	while (!readdir_r(lang_dir, &script_dirent, &script_next) &&	\ | 
 | 691 | 	       script_next)						\ | 
| Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 692 | 		if (script_dirent.d_type != DT_DIR &&			\ | 
 | 693 | 		    (script_dirent.d_type != DT_UNKNOWN ||		\ | 
 | 694 | 		     !is_directory(lang_path, &script_dirent))) | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 695 |  | 
 | 696 |  | 
 | 697 | #define RECORD_SUFFIX			"-record" | 
 | 698 | #define REPORT_SUFFIX			"-report" | 
 | 699 |  | 
 | 700 | struct script_desc { | 
 | 701 | 	struct list_head	node; | 
 | 702 | 	char			*name; | 
 | 703 | 	char			*half_liner; | 
 | 704 | 	char			*args; | 
 | 705 | }; | 
 | 706 |  | 
| Arnaldo Carvalho de Melo | eccdfe2 | 2011-01-04 16:32:52 -0200 | [diff] [blame] | 707 | static LIST_HEAD(script_descs); | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 708 |  | 
 | 709 | static struct script_desc *script_desc__new(const char *name) | 
 | 710 | { | 
 | 711 | 	struct script_desc *s = zalloc(sizeof(*s)); | 
 | 712 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 713 | 	if (s != NULL && name) | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 714 | 		s->name = strdup(name); | 
 | 715 |  | 
 | 716 | 	return s; | 
 | 717 | } | 
 | 718 |  | 
 | 719 | static void script_desc__delete(struct script_desc *s) | 
 | 720 | { | 
 | 721 | 	free(s->name); | 
| Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 722 | 	free(s->half_liner); | 
 | 723 | 	free(s->args); | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 724 | 	free(s); | 
 | 725 | } | 
 | 726 |  | 
 | 727 | static void script_desc__add(struct script_desc *s) | 
 | 728 | { | 
 | 729 | 	list_add_tail(&s->node, &script_descs); | 
 | 730 | } | 
 | 731 |  | 
 | 732 | static struct script_desc *script_desc__find(const char *name) | 
 | 733 | { | 
 | 734 | 	struct script_desc *s; | 
 | 735 |  | 
 | 736 | 	list_for_each_entry(s, &script_descs, node) | 
 | 737 | 		if (strcasecmp(s->name, name) == 0) | 
 | 738 | 			return s; | 
 | 739 | 	return NULL; | 
 | 740 | } | 
 | 741 |  | 
 | 742 | static struct script_desc *script_desc__findnew(const char *name) | 
 | 743 | { | 
 | 744 | 	struct script_desc *s = script_desc__find(name); | 
 | 745 |  | 
 | 746 | 	if (s) | 
 | 747 | 		return s; | 
 | 748 |  | 
 | 749 | 	s = script_desc__new(name); | 
 | 750 | 	if (!s) | 
 | 751 | 		goto out_delete_desc; | 
 | 752 |  | 
 | 753 | 	script_desc__add(s); | 
 | 754 |  | 
 | 755 | 	return s; | 
 | 756 |  | 
 | 757 | out_delete_desc: | 
 | 758 | 	script_desc__delete(s); | 
 | 759 |  | 
 | 760 | 	return NULL; | 
 | 761 | } | 
 | 762 |  | 
| Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 763 | static const char *ends_with(const char *str, const char *suffix) | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 764 | { | 
 | 765 | 	size_t suffix_len = strlen(suffix); | 
| Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 766 | 	const char *p = str; | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 767 |  | 
 | 768 | 	if (strlen(str) > suffix_len) { | 
 | 769 | 		p = str + strlen(str) - suffix_len; | 
 | 770 | 		if (!strncmp(p, suffix, suffix_len)) | 
 | 771 | 			return p; | 
 | 772 | 	} | 
 | 773 |  | 
 | 774 | 	return NULL; | 
 | 775 | } | 
 | 776 |  | 
 | 777 | static char *ltrim(char *str) | 
 | 778 | { | 
 | 779 | 	int len = strlen(str); | 
 | 780 |  | 
 | 781 | 	while (len && isspace(*str)) { | 
 | 782 | 		len--; | 
 | 783 | 		str++; | 
 | 784 | 	} | 
 | 785 |  | 
 | 786 | 	return str; | 
 | 787 | } | 
 | 788 |  | 
 | 789 | static int read_script_info(struct script_desc *desc, const char *filename) | 
 | 790 | { | 
 | 791 | 	char line[BUFSIZ], *p; | 
 | 792 | 	FILE *fp; | 
 | 793 |  | 
 | 794 | 	fp = fopen(filename, "r"); | 
 | 795 | 	if (!fp) | 
 | 796 | 		return -1; | 
 | 797 |  | 
 | 798 | 	while (fgets(line, sizeof(line), fp)) { | 
 | 799 | 		p = ltrim(line); | 
 | 800 | 		if (strlen(p) == 0) | 
 | 801 | 			continue; | 
 | 802 | 		if (*p != '#') | 
 | 803 | 			continue; | 
 | 804 | 		p++; | 
 | 805 | 		if (strlen(p) && *p == '!') | 
 | 806 | 			continue; | 
 | 807 |  | 
 | 808 | 		p = ltrim(p); | 
 | 809 | 		if (strlen(p) && p[strlen(p) - 1] == '\n') | 
 | 810 | 			p[strlen(p) - 1] = '\0'; | 
 | 811 |  | 
 | 812 | 		if (!strncmp(p, "description:", strlen("description:"))) { | 
 | 813 | 			p += strlen("description:"); | 
 | 814 | 			desc->half_liner = strdup(ltrim(p)); | 
 | 815 | 			continue; | 
 | 816 | 		} | 
 | 817 |  | 
 | 818 | 		if (!strncmp(p, "args:", strlen("args:"))) { | 
 | 819 | 			p += strlen("args:"); | 
 | 820 | 			desc->args = strdup(ltrim(p)); | 
 | 821 | 			continue; | 
 | 822 | 		} | 
 | 823 | 	} | 
 | 824 |  | 
 | 825 | 	fclose(fp); | 
 | 826 |  | 
 | 827 | 	return 0; | 
 | 828 | } | 
 | 829 |  | 
 | 830 | static int list_available_scripts(const struct option *opt __used, | 
 | 831 | 				  const char *s __used, int unset __used) | 
 | 832 | { | 
 | 833 | 	struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | 
 | 834 | 	char scripts_path[MAXPATHLEN]; | 
 | 835 | 	DIR *scripts_dir, *lang_dir; | 
 | 836 | 	char script_path[MAXPATHLEN]; | 
 | 837 | 	char lang_path[MAXPATHLEN]; | 
 | 838 | 	struct script_desc *desc; | 
 | 839 | 	char first_half[BUFSIZ]; | 
 | 840 | 	char *script_root; | 
 | 841 | 	char *str; | 
 | 842 |  | 
 | 843 | 	snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | 
 | 844 |  | 
 | 845 | 	scripts_dir = opendir(scripts_path); | 
 | 846 | 	if (!scripts_dir) | 
 | 847 | 		return -1; | 
 | 848 |  | 
| Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 849 | 	for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 850 | 		snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, | 
 | 851 | 			 lang_dirent.d_name); | 
 | 852 | 		lang_dir = opendir(lang_path); | 
 | 853 | 		if (!lang_dir) | 
 | 854 | 			continue; | 
 | 855 |  | 
| Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 856 | 		for_each_script(lang_path, lang_dir, script_dirent, script_next) { | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 857 | 			script_root = strdup(script_dirent.d_name); | 
| Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 858 | 			str = (char *)ends_with(script_root, REPORT_SUFFIX); | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 859 | 			if (str) { | 
 | 860 | 				*str = '\0'; | 
 | 861 | 				desc = script_desc__findnew(script_root); | 
 | 862 | 				snprintf(script_path, MAXPATHLEN, "%s/%s", | 
 | 863 | 					 lang_path, script_dirent.d_name); | 
 | 864 | 				read_script_info(desc, script_path); | 
 | 865 | 			} | 
 | 866 | 			free(script_root); | 
 | 867 | 		} | 
 | 868 | 	} | 
 | 869 |  | 
 | 870 | 	fprintf(stdout, "List of available trace scripts:\n"); | 
 | 871 | 	list_for_each_entry(desc, &script_descs, node) { | 
 | 872 | 		sprintf(first_half, "%s %s", desc->name, | 
 | 873 | 			desc->args ? desc->args : ""); | 
 | 874 | 		fprintf(stdout, "  %-36s %s\n", first_half, | 
 | 875 | 			desc->half_liner ? desc->half_liner : ""); | 
 | 876 | 	} | 
 | 877 |  | 
 | 878 | 	exit(0); | 
 | 879 | } | 
 | 880 |  | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 881 | static char *get_script_path(const char *script_root, const char *suffix) | 
 | 882 | { | 
 | 883 | 	struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | 
 | 884 | 	char scripts_path[MAXPATHLEN]; | 
 | 885 | 	char script_path[MAXPATHLEN]; | 
 | 886 | 	DIR *scripts_dir, *lang_dir; | 
 | 887 | 	char lang_path[MAXPATHLEN]; | 
 | 888 | 	char *str, *__script_root; | 
 | 889 | 	char *path = NULL; | 
 | 890 |  | 
 | 891 | 	snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | 
 | 892 |  | 
 | 893 | 	scripts_dir = opendir(scripts_path); | 
 | 894 | 	if (!scripts_dir) | 
 | 895 | 		return NULL; | 
 | 896 |  | 
| Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 897 | 	for_each_lang(scripts_path, scripts_dir, lang_dirent, lang_next) { | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 898 | 		snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, | 
 | 899 | 			 lang_dirent.d_name); | 
 | 900 | 		lang_dir = opendir(lang_path); | 
 | 901 | 		if (!lang_dir) | 
 | 902 | 			continue; | 
 | 903 |  | 
| Shawn Bohrer | 008f29d | 2010-11-21 10:09:39 -0600 | [diff] [blame] | 904 | 		for_each_script(lang_path, lang_dir, script_dirent, script_next) { | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 905 | 			__script_root = strdup(script_dirent.d_name); | 
| Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 906 | 			str = (char *)ends_with(__script_root, suffix); | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 907 | 			if (str) { | 
 | 908 | 				*str = '\0'; | 
 | 909 | 				if (strcmp(__script_root, script_root)) | 
 | 910 | 					continue; | 
 | 911 | 				snprintf(script_path, MAXPATHLEN, "%s/%s", | 
 | 912 | 					 lang_path, script_dirent.d_name); | 
 | 913 | 				path = strdup(script_path); | 
 | 914 | 				free(__script_root); | 
 | 915 | 				break; | 
 | 916 | 			} | 
 | 917 | 			free(__script_root); | 
 | 918 | 		} | 
 | 919 | 	} | 
 | 920 |  | 
 | 921 | 	return path; | 
 | 922 | } | 
 | 923 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 924 | static bool is_top_script(const char *script_path) | 
 | 925 | { | 
| Stephane Eranian | 965bb6b | 2010-12-03 17:52:01 +0200 | [diff] [blame] | 926 | 	return ends_with(script_path, "top") == NULL ? false : true; | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 927 | } | 
 | 928 |  | 
 | 929 | static int has_required_arg(char *script_path) | 
 | 930 | { | 
 | 931 | 	struct script_desc *desc; | 
 | 932 | 	int n_args = 0; | 
 | 933 | 	char *p; | 
 | 934 |  | 
 | 935 | 	desc = script_desc__new(NULL); | 
 | 936 |  | 
 | 937 | 	if (read_script_info(desc, script_path)) | 
 | 938 | 		goto out; | 
 | 939 |  | 
 | 940 | 	if (!desc->args) | 
 | 941 | 		goto out; | 
 | 942 |  | 
 | 943 | 	for (p = desc->args; *p; p++) | 
 | 944 | 		if (*p == '<') | 
 | 945 | 			n_args++; | 
 | 946 | out: | 
 | 947 | 	script_desc__delete(desc); | 
 | 948 |  | 
 | 949 | 	return n_args; | 
 | 950 | } | 
 | 951 |  | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 952 | static const char * const script_usage[] = { | 
 | 953 | 	"perf script [<options>]", | 
 | 954 | 	"perf script [<options>] record <script> [<record-options>] <command>", | 
 | 955 | 	"perf script [<options>] report <script> [script-args]", | 
 | 956 | 	"perf script [<options>] <script> [<record-options>] <command>", | 
 | 957 | 	"perf script [<options>] <top-script> [script-args]", | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 958 | 	NULL | 
 | 959 | }; | 
 | 960 |  | 
 | 961 | static const struct option options[] = { | 
 | 962 | 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace, | 
 | 963 | 		    "dump raw trace in ASCII"), | 
| Ian Munsie | c055564 | 2010-04-13 18:37:33 +1000 | [diff] [blame] | 964 | 	OPT_INCR('v', "verbose", &verbose, | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 965 | 		    "be more verbose (show symbol address, etc)"), | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 966 | 	OPT_BOOLEAN('L', "Latency", &latency_format, | 
| Steven Rostedt | cda4846 | 2009-10-14 15:43:42 -0400 | [diff] [blame] | 967 | 		    "show latency attributes (irqs/preemption disabled, etc)"), | 
| Tom Zanussi | 4b9c0c5 | 2009-12-15 02:53:38 -0600 | [diff] [blame] | 968 | 	OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", | 
 | 969 | 			   list_available_scripts), | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 970 | 	OPT_CALLBACK('s', "script", NULL, "name", | 
 | 971 | 		     "script file name (lang:script name, script name, or *)", | 
 | 972 | 		     parse_scriptname), | 
 | 973 | 	OPT_STRING('g', "gen-script", &generate_script_lang, "lang", | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 974 | 		   "generate perf-script.xx script in specified language"), | 
| Hitoshi Mitake | 408f0d1 | 2010-01-22 22:45:29 +0900 | [diff] [blame] | 975 | 	OPT_STRING('i', "input", &input_name, "file", | 
 | 976 | 		    "input file name"), | 
| Frederic Weisbecker | ffabd99 | 2010-05-27 16:27:47 +0200 | [diff] [blame] | 977 | 	OPT_BOOLEAN('d', "debug-mode", &debug_mode, | 
 | 978 | 		   "do various checks like samples ordering and lost events"), | 
| David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 979 | 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name, | 
 | 980 | 		   "file", "vmlinux pathname"), | 
 | 981 | 	OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name, | 
 | 982 | 		   "file", "kallsyms pathname"), | 
 | 983 | 	OPT_BOOLEAN('G', "hide-call-graph", &no_callchain, | 
 | 984 | 		    "When printing symbols do not display call chain"), | 
 | 985 | 	OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory", | 
 | 986 | 		    "Look for files with symbols relative to this directory"), | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 987 | 	OPT_CALLBACK('f', "fields", NULL, "str", | 
| Arun Sharma | 0817a6a | 2011-04-14 10:38:18 -0700 | [diff] [blame] | 988 | 		     "comma separated output fields prepend with 'type:'. Valid types: hw,sw,trace,raw. Fields: comm,tid,pid,time,cpu,event,trace,sym", | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 989 | 		     parse_output_fields), | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 990 |  | 
| Masami Hiramatsu | 1909629 | 2009-08-21 14:56:03 -0400 | [diff] [blame] | 991 | 	OPT_END() | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 992 | }; | 
 | 993 |  | 
| Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 994 | static bool have_cmd(int argc, const char **argv) | 
 | 995 | { | 
 | 996 | 	char **__argv = malloc(sizeof(const char *) * argc); | 
 | 997 |  | 
 | 998 | 	if (!__argv) | 
 | 999 | 		die("malloc"); | 
 | 1000 | 	memcpy(__argv, argv, sizeof(const char *) * argc); | 
 | 1001 | 	argc = parse_options(argc, (const char **)__argv, record_options, | 
 | 1002 | 			     NULL, PARSE_OPT_STOP_AT_NON_OPTION); | 
 | 1003 | 	free(__argv); | 
 | 1004 |  | 
 | 1005 | 	return argc != 0; | 
 | 1006 | } | 
 | 1007 |  | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1008 | int cmd_script(int argc, const char **argv, const char *prefix __used) | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1009 | { | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1010 | 	char *rec_script_path = NULL; | 
 | 1011 | 	char *rep_script_path = NULL; | 
| Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1012 | 	struct perf_session *session; | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1013 | 	char *script_path = NULL; | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1014 | 	const char **__argv; | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1015 | 	bool system_wide; | 
 | 1016 | 	int i, j, err; | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1017 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1018 | 	setup_scripting(); | 
 | 1019 |  | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1020 | 	argc = parse_options(argc, argv, options, script_usage, | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1021 | 			     PARSE_OPT_STOP_AT_NON_OPTION); | 
 | 1022 |  | 
 | 1023 | 	if (argc > 1 && !strncmp(argv[0], "rec", strlen("rec"))) { | 
 | 1024 | 		rec_script_path = get_script_path(argv[1], RECORD_SUFFIX); | 
 | 1025 | 		if (!rec_script_path) | 
 | 1026 | 			return cmd_record(argc, argv, NULL); | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1027 | 	} | 
 | 1028 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1029 | 	if (argc > 1 && !strncmp(argv[0], "rep", strlen("rep"))) { | 
 | 1030 | 		rep_script_path = get_script_path(argv[1], REPORT_SUFFIX); | 
 | 1031 | 		if (!rep_script_path) { | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1032 | 			fprintf(stderr, | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1033 | 				"Please specify a valid report script" | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1034 | 				"(see 'perf script -l' for listing)\n"); | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1035 | 			return -1; | 
 | 1036 | 		} | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1037 | 	} | 
 | 1038 |  | 
| Ben Hutchings | 44e668c | 2010-10-10 16:10:03 +0100 | [diff] [blame] | 1039 | 	/* make sure PERF_EXEC_PATH is set for scripts */ | 
 | 1040 | 	perf_set_argv_exec_path(perf_exec_path()); | 
 | 1041 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1042 | 	if (argc && !script_name && !rec_script_path && !rep_script_path) { | 
| Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1043 | 		int live_pipe[2]; | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1044 | 		int rep_args; | 
| Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1045 | 		pid_t pid; | 
 | 1046 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1047 | 		rec_script_path = get_script_path(argv[0], RECORD_SUFFIX); | 
 | 1048 | 		rep_script_path = get_script_path(argv[0], REPORT_SUFFIX); | 
 | 1049 |  | 
 | 1050 | 		if (!rec_script_path && !rep_script_path) { | 
 | 1051 | 			fprintf(stderr, " Couldn't find script %s\n\n See perf" | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1052 | 				" script -l for available scripts.\n", argv[0]); | 
 | 1053 | 			usage_with_options(script_usage, options); | 
| Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1054 | 		} | 
 | 1055 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1056 | 		if (is_top_script(argv[0])) { | 
 | 1057 | 			rep_args = argc - 1; | 
 | 1058 | 		} else { | 
 | 1059 | 			int rec_args; | 
 | 1060 |  | 
 | 1061 | 			rep_args = has_required_arg(rep_script_path); | 
 | 1062 | 			rec_args = (argc - 1) - rep_args; | 
 | 1063 | 			if (rec_args < 0) { | 
 | 1064 | 				fprintf(stderr, " %s script requires options." | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1065 | 					"\n\n See perf script -l for available " | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1066 | 					"scripts and options.\n", argv[0]); | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1067 | 				usage_with_options(script_usage, options); | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1068 | 			} | 
| Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1069 | 		} | 
 | 1070 |  | 
 | 1071 | 		if (pipe(live_pipe) < 0) { | 
 | 1072 | 			perror("failed to create pipe"); | 
 | 1073 | 			exit(-1); | 
 | 1074 | 		} | 
 | 1075 |  | 
 | 1076 | 		pid = fork(); | 
 | 1077 | 		if (pid < 0) { | 
 | 1078 | 			perror("failed to fork"); | 
 | 1079 | 			exit(-1); | 
 | 1080 | 		} | 
 | 1081 |  | 
 | 1082 | 		if (!pid) { | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1083 | 			system_wide = true; | 
 | 1084 | 			j = 0; | 
 | 1085 |  | 
| Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1086 | 			dup2(live_pipe[1], 1); | 
 | 1087 | 			close(live_pipe[0]); | 
 | 1088 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1089 | 			if (!is_top_script(argv[0])) | 
 | 1090 | 				system_wide = !have_cmd(argc - rep_args, | 
 | 1091 | 							&argv[rep_args]); | 
 | 1092 |  | 
 | 1093 | 			__argv = malloc((argc + 6) * sizeof(const char *)); | 
| Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 1094 | 			if (!__argv) | 
 | 1095 | 				die("malloc"); | 
 | 1096 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1097 | 			__argv[j++] = "/bin/sh"; | 
 | 1098 | 			__argv[j++] = rec_script_path; | 
 | 1099 | 			if (system_wide) | 
 | 1100 | 				__argv[j++] = "-a"; | 
 | 1101 | 			__argv[j++] = "-q"; | 
 | 1102 | 			__argv[j++] = "-o"; | 
 | 1103 | 			__argv[j++] = "-"; | 
 | 1104 | 			for (i = rep_args + 1; i < argc; i++) | 
 | 1105 | 				__argv[j++] = argv[i]; | 
 | 1106 | 			__argv[j++] = NULL; | 
| Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1107 |  | 
 | 1108 | 			execvp("/bin/sh", (char **)__argv); | 
| Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 1109 | 			free(__argv); | 
| Tom Zanussi | a0cccc2 | 2010-04-01 23:59:25 -0500 | [diff] [blame] | 1110 | 			exit(-1); | 
 | 1111 | 		} | 
 | 1112 |  | 
 | 1113 | 		dup2(live_pipe[0], 0); | 
 | 1114 | 		close(live_pipe[1]); | 
 | 1115 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1116 | 		__argv = malloc((argc + 4) * sizeof(const char *)); | 
| Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 1117 | 		if (!__argv) | 
 | 1118 | 			die("malloc"); | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1119 | 		j = 0; | 
| Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1120 | 		__argv[j++] = "/bin/sh"; | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1121 | 		__argv[j++] = rep_script_path; | 
 | 1122 | 		for (i = 1; i < rep_args + 1; i++) | 
| Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1123 | 			__argv[j++] = argv[i]; | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1124 | 		__argv[j++] = "-i"; | 
 | 1125 | 		__argv[j++] = "-"; | 
| Tom Zanussi | 34c86ea | 2010-11-10 08:15:43 -0600 | [diff] [blame] | 1126 | 		__argv[j++] = NULL; | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1127 |  | 
 | 1128 | 		execvp("/bin/sh", (char **)__argv); | 
| Tom Zanussi | e8719ad | 2010-11-10 07:52:32 -0600 | [diff] [blame] | 1129 | 		free(__argv); | 
| Tom Zanussi | 3875294 | 2009-12-15 02:53:39 -0600 | [diff] [blame] | 1130 | 		exit(-1); | 
 | 1131 | 	} | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1132 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1133 | 	if (rec_script_path) | 
 | 1134 | 		script_path = rec_script_path; | 
 | 1135 | 	if (rep_script_path) | 
 | 1136 | 		script_path = rep_script_path; | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1137 |  | 
| Tom Zanussi | b5b8731 | 2010-11-10 08:16:51 -0600 | [diff] [blame] | 1138 | 	if (script_path) { | 
 | 1139 | 		system_wide = false; | 
 | 1140 | 		j = 0; | 
 | 1141 |  | 
 | 1142 | 		if (rec_script_path) | 
 | 1143 | 			system_wide = !have_cmd(argc - 1, &argv[1]); | 
 | 1144 |  | 
 | 1145 | 		__argv = malloc((argc + 2) * sizeof(const char *)); | 
 | 1146 | 		if (!__argv) | 
 | 1147 | 			die("malloc"); | 
 | 1148 | 		__argv[j++] = "/bin/sh"; | 
 | 1149 | 		__argv[j++] = script_path; | 
 | 1150 | 		if (system_wide) | 
 | 1151 | 			__argv[j++] = "-a"; | 
 | 1152 | 		for (i = 2; i < argc; i++) | 
 | 1153 | 			__argv[j++] = argv[i]; | 
 | 1154 | 		__argv[j++] = NULL; | 
 | 1155 |  | 
 | 1156 | 		execvp("/bin/sh", (char **)__argv); | 
 | 1157 | 		free(__argv); | 
 | 1158 | 		exit(-1); | 
 | 1159 | 	} | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1160 |  | 
| Arnaldo Carvalho de Melo | 655000e | 2009-12-15 20:04:40 -0200 | [diff] [blame] | 1161 | 	if (symbol__init() < 0) | 
 | 1162 | 		return -1; | 
| Tom Zanussi | cf4fee5 | 2010-03-03 01:04:33 -0600 | [diff] [blame] | 1163 | 	if (!script_name) | 
 | 1164 | 		setup_pager(); | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1165 |  | 
| Ian Munsie | 21ef97f | 2010-12-10 14:09:16 +1100 | [diff] [blame] | 1166 | 	session = perf_session__new(input_name, O_RDONLY, 0, false, &event_ops); | 
| Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1167 | 	if (session == NULL) | 
 | 1168 | 		return -ENOMEM; | 
 | 1169 |  | 
| David Ahern | 1424dc9 | 2011-03-09 22:23:28 -0700 | [diff] [blame] | 1170 | 	if (!no_callchain) | 
| David Ahern | c0230b2 | 2011-03-09 22:23:27 -0700 | [diff] [blame] | 1171 | 		symbol_conf.use_callchain = true; | 
 | 1172 | 	else | 
 | 1173 | 		symbol_conf.use_callchain = false; | 
 | 1174 |  | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1175 | 	if (generate_script_lang) { | 
 | 1176 | 		struct stat perf_stat; | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1177 | 		int input; | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1178 |  | 
| David Ahern | 2c9e45f | 2011-03-17 10:03:21 -0600 | [diff] [blame] | 1179 | 		if (output_set_by_user()) { | 
| David Ahern | 745f43e | 2011-03-09 22:23:26 -0700 | [diff] [blame] | 1180 | 			fprintf(stderr, | 
 | 1181 | 				"custom fields not supported for generated scripts"); | 
 | 1182 | 			return -1; | 
 | 1183 | 		} | 
 | 1184 |  | 
 | 1185 | 		input = open(input_name, O_RDONLY); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1186 | 		if (input < 0) { | 
 | 1187 | 			perror("failed to open file"); | 
 | 1188 | 			exit(-1); | 
 | 1189 | 		} | 
 | 1190 |  | 
 | 1191 | 		err = fstat(input, &perf_stat); | 
 | 1192 | 		if (err < 0) { | 
 | 1193 | 			perror("failed to stat file"); | 
 | 1194 | 			exit(-1); | 
 | 1195 | 		} | 
 | 1196 |  | 
 | 1197 | 		if (!perf_stat.st_size) { | 
 | 1198 | 			fprintf(stderr, "zero-sized file, nothing to do!\n"); | 
 | 1199 | 			exit(0); | 
 | 1200 | 		} | 
 | 1201 |  | 
 | 1202 | 		scripting_ops = script_spec__lookup(generate_script_lang); | 
 | 1203 | 		if (!scripting_ops) { | 
 | 1204 | 			fprintf(stderr, "invalid language specifier"); | 
 | 1205 | 			return -1; | 
 | 1206 | 		} | 
 | 1207 |  | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1208 | 		err = scripting_ops->generate_script("perf-script"); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1209 | 		goto out; | 
 | 1210 | 	} | 
 | 1211 |  | 
 | 1212 | 	if (script_name) { | 
| Tom Zanussi | 586bc5c | 2009-12-15 02:53:35 -0600 | [diff] [blame] | 1213 | 		err = scripting_ops->start_script(script_name, argc, argv); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1214 | 		if (err) | 
 | 1215 | 			goto out; | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1216 | 		pr_debug("perf script started with script %s\n\n", script_name); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1217 | 	} | 
 | 1218 |  | 
| David Ahern | 9cbdb70 | 2011-04-06 21:54:20 -0600 | [diff] [blame] | 1219 |  | 
 | 1220 | 	err = perf_session__check_output_opt(session); | 
 | 1221 | 	if (err < 0) | 
 | 1222 | 		goto out; | 
 | 1223 |  | 
| Ingo Molnar | 133dc4c | 2010-11-16 18:45:39 +0100 | [diff] [blame] | 1224 | 	err = __cmd_script(session); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1225 |  | 
| Arnaldo Carvalho de Melo | d8f6624 | 2009-12-13 19:50:24 -0200 | [diff] [blame] | 1226 | 	perf_session__delete(session); | 
| Tom Zanussi | 956ffd0 | 2009-11-25 01:15:46 -0600 | [diff] [blame] | 1227 | 	cleanup_scripting(); | 
 | 1228 | out: | 
 | 1229 | 	return err; | 
| Frederic Weisbecker | 5f9c39d | 2009-08-17 16:18:08 +0200 | [diff] [blame] | 1230 | } |