• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <linux/kernel.h>
2 #include <traceevent/event-parse.h>
3 
4 #include <byteswap.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/mman.h>
8 
9 #include "evlist.h"
10 #include "evsel.h"
11 #include "session.h"
12 #include "tool.h"
13 #include "sort.h"
14 #include "util.h"
15 #include "cpumap.h"
16 #include "perf_regs.h"
17 #include "asm/bug.h"
18 #include "auxtrace.h"
19 #include "thread-stack.h"
20 
21 static int perf_session__deliver_event(struct perf_session *session,
22 				       union perf_event *event,
23 				       struct perf_sample *sample,
24 				       struct perf_tool *tool,
25 				       u64 file_offset);
26 
perf_session__open(struct perf_session * session)27 static int perf_session__open(struct perf_session *session)
28 {
29 	struct perf_data_file *file = session->file;
30 
31 	if (perf_session__read_header(session) < 0) {
32 		pr_err("incompatible file format (rerun with -v to learn more)\n");
33 		return -1;
34 	}
35 
36 	if (perf_data_file__is_pipe(file))
37 		return 0;
38 
39 	if (!perf_evlist__valid_sample_type(session->evlist)) {
40 		pr_err("non matching sample_type\n");
41 		return -1;
42 	}
43 
44 	if (!perf_evlist__valid_sample_id_all(session->evlist)) {
45 		pr_err("non matching sample_id_all\n");
46 		return -1;
47 	}
48 
49 	if (!perf_evlist__valid_read_format(session->evlist)) {
50 		pr_err("non matching read_format\n");
51 		return -1;
52 	}
53 
54 	return 0;
55 }
56 
perf_session__set_id_hdr_size(struct perf_session * session)57 void perf_session__set_id_hdr_size(struct perf_session *session)
58 {
59 	u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
60 
61 	machines__set_id_hdr_size(&session->machines, id_hdr_size);
62 }
63 
perf_session__create_kernel_maps(struct perf_session * session)64 int perf_session__create_kernel_maps(struct perf_session *session)
65 {
66 	int ret = machine__create_kernel_maps(&session->machines.host);
67 
68 	if (ret >= 0)
69 		ret = machines__create_guest_kernel_maps(&session->machines);
70 	return ret;
71 }
72 
perf_session__destroy_kernel_maps(struct perf_session * session)73 static void perf_session__destroy_kernel_maps(struct perf_session *session)
74 {
75 	machines__destroy_kernel_maps(&session->machines);
76 }
77 
perf_session__has_comm_exec(struct perf_session * session)78 static bool perf_session__has_comm_exec(struct perf_session *session)
79 {
80 	struct perf_evsel *evsel;
81 
82 	evlist__for_each(session->evlist, evsel) {
83 		if (evsel->attr.comm_exec)
84 			return true;
85 	}
86 
87 	return false;
88 }
89 
perf_session__set_comm_exec(struct perf_session * session)90 static void perf_session__set_comm_exec(struct perf_session *session)
91 {
92 	bool comm_exec = perf_session__has_comm_exec(session);
93 
94 	machines__set_comm_exec(&session->machines, comm_exec);
95 }
96 
ordered_events__deliver_event(struct ordered_events * oe,struct ordered_event * event)97 static int ordered_events__deliver_event(struct ordered_events *oe,
98 					 struct ordered_event *event)
99 {
100 	struct perf_sample sample;
101 	struct perf_session *session = container_of(oe, struct perf_session,
102 						    ordered_events);
103 	int ret = perf_evlist__parse_sample(session->evlist, event->event, &sample);
104 
105 	if (ret) {
106 		pr_err("Can't parse sample, err = %d\n", ret);
107 		return ret;
108 	}
109 
110 	return perf_session__deliver_event(session, event->event, &sample,
111 					   session->tool, event->file_offset);
112 }
113 
perf_session__new(struct perf_data_file * file,bool repipe,struct perf_tool * tool)114 struct perf_session *perf_session__new(struct perf_data_file *file,
115 				       bool repipe, struct perf_tool *tool)
116 {
117 	struct perf_session *session = zalloc(sizeof(*session));
118 
119 	if (!session)
120 		goto out;
121 
122 	session->repipe = repipe;
123 	session->tool   = tool;
124 	INIT_LIST_HEAD(&session->auxtrace_index);
125 	machines__init(&session->machines);
126 	ordered_events__init(&session->ordered_events, ordered_events__deliver_event);
127 
128 	if (file) {
129 		if (perf_data_file__open(file))
130 			goto out_delete;
131 
132 		session->file = file;
133 
134 		if (perf_data_file__is_read(file)) {
135 			if (perf_session__open(session) < 0)
136 				goto out_close;
137 
138 			/*
139 			 * set session attributes that are present in perf.data
140 			 * but not in pipe-mode.
141 			 */
142 			if (!file->is_pipe) {
143 				perf_session__set_id_hdr_size(session);
144 				perf_session__set_comm_exec(session);
145 			}
146 		}
147 	} else  {
148 		session->machines.host.env = &perf_env;
149 	}
150 
151 	if (!file || perf_data_file__is_write(file)) {
152 		/*
153 		 * In O_RDONLY mode this will be performed when reading the
154 		 * kernel MMAP event, in perf_event__process_mmap().
155 		 */
156 		if (perf_session__create_kernel_maps(session) < 0)
157 			pr_warning("Cannot read kernel map\n");
158 	}
159 
160 	/*
161 	 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
162 	 * processed, so perf_evlist__sample_id_all is not meaningful here.
163 	 */
164 	if ((!file || !file->is_pipe) && tool && tool->ordering_requires_timestamps &&
165 	    tool->ordered_events && !perf_evlist__sample_id_all(session->evlist)) {
166 		dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
167 		tool->ordered_events = false;
168 	}
169 
170 	return session;
171 
172  out_close:
173 	perf_data_file__close(file);
174  out_delete:
175 	perf_session__delete(session);
176  out:
177 	return NULL;
178 }
179 
perf_session__delete_threads(struct perf_session * session)180 static void perf_session__delete_threads(struct perf_session *session)
181 {
182 	machine__delete_threads(&session->machines.host);
183 }
184 
perf_session__delete(struct perf_session * session)185 void perf_session__delete(struct perf_session *session)
186 {
187 	auxtrace__free(session);
188 	auxtrace_index__free(&session->auxtrace_index);
189 	perf_session__destroy_kernel_maps(session);
190 	perf_session__delete_threads(session);
191 	perf_env__exit(&session->header.env);
192 	machines__exit(&session->machines);
193 	if (session->file)
194 		perf_data_file__close(session->file);
195 	free(session);
196 }
197 
process_event_synth_tracing_data_stub(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_session * session __maybe_unused)198 static int process_event_synth_tracing_data_stub(struct perf_tool *tool
199 						 __maybe_unused,
200 						 union perf_event *event
201 						 __maybe_unused,
202 						 struct perf_session *session
203 						__maybe_unused)
204 {
205 	dump_printf(": unhandled!\n");
206 	return 0;
207 }
208 
process_event_synth_attr_stub(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_evlist ** pevlist __maybe_unused)209 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
210 					 union perf_event *event __maybe_unused,
211 					 struct perf_evlist **pevlist
212 					 __maybe_unused)
213 {
214 	dump_printf(": unhandled!\n");
215 	return 0;
216 }
217 
process_event_sample_stub(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_sample * sample __maybe_unused,struct perf_evsel * evsel __maybe_unused,struct machine * machine __maybe_unused)218 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
219 				     union perf_event *event __maybe_unused,
220 				     struct perf_sample *sample __maybe_unused,
221 				     struct perf_evsel *evsel __maybe_unused,
222 				     struct machine *machine __maybe_unused)
223 {
224 	dump_printf(": unhandled!\n");
225 	return 0;
226 }
227 
process_event_stub(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_sample * sample __maybe_unused,struct machine * machine __maybe_unused)228 static int process_event_stub(struct perf_tool *tool __maybe_unused,
229 			      union perf_event *event __maybe_unused,
230 			      struct perf_sample *sample __maybe_unused,
231 			      struct machine *machine __maybe_unused)
232 {
233 	dump_printf(": unhandled!\n");
234 	return 0;
235 }
236 
process_build_id_stub(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_session * session __maybe_unused)237 static int process_build_id_stub(struct perf_tool *tool __maybe_unused,
238 				 union perf_event *event __maybe_unused,
239 				 struct perf_session *session __maybe_unused)
240 {
241 	dump_printf(": unhandled!\n");
242 	return 0;
243 }
244 
process_finished_round_stub(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct ordered_events * oe __maybe_unused)245 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
246 				       union perf_event *event __maybe_unused,
247 				       struct ordered_events *oe __maybe_unused)
248 {
249 	dump_printf(": unhandled!\n");
250 	return 0;
251 }
252 
253 static int process_finished_round(struct perf_tool *tool,
254 				  union perf_event *event,
255 				  struct ordered_events *oe);
256 
process_id_index_stub(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_session * perf_session __maybe_unused)257 static int process_id_index_stub(struct perf_tool *tool __maybe_unused,
258 				 union perf_event *event __maybe_unused,
259 				 struct perf_session *perf_session
260 				 __maybe_unused)
261 {
262 	dump_printf(": unhandled!\n");
263 	return 0;
264 }
265 
process_event_auxtrace_info_stub(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_session * session __maybe_unused)266 static int process_event_auxtrace_info_stub(struct perf_tool *tool __maybe_unused,
267 				union perf_event *event __maybe_unused,
268 				struct perf_session *session __maybe_unused)
269 {
270 	dump_printf(": unhandled!\n");
271 	return 0;
272 }
273 
skipn(int fd,off_t n)274 static int skipn(int fd, off_t n)
275 {
276 	char buf[4096];
277 	ssize_t ret;
278 
279 	while (n > 0) {
280 		ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
281 		if (ret <= 0)
282 			return ret;
283 		n -= ret;
284 	}
285 
286 	return 0;
287 }
288 
process_event_auxtrace_stub(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_session * session __maybe_unused)289 static s64 process_event_auxtrace_stub(struct perf_tool *tool __maybe_unused,
290 				       union perf_event *event,
291 				       struct perf_session *session
292 				       __maybe_unused)
293 {
294 	dump_printf(": unhandled!\n");
295 	if (perf_data_file__is_pipe(session->file))
296 		skipn(perf_data_file__fd(session->file), event->auxtrace.size);
297 	return event->auxtrace.size;
298 }
299 
300 static
process_event_auxtrace_error_stub(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct perf_session * session __maybe_unused)301 int process_event_auxtrace_error_stub(struct perf_tool *tool __maybe_unused,
302 				      union perf_event *event __maybe_unused,
303 				      struct perf_session *session __maybe_unused)
304 {
305 	dump_printf(": unhandled!\n");
306 	return 0;
307 }
308 
perf_tool__fill_defaults(struct perf_tool * tool)309 void perf_tool__fill_defaults(struct perf_tool *tool)
310 {
311 	if (tool->sample == NULL)
312 		tool->sample = process_event_sample_stub;
313 	if (tool->mmap == NULL)
314 		tool->mmap = process_event_stub;
315 	if (tool->mmap2 == NULL)
316 		tool->mmap2 = process_event_stub;
317 	if (tool->comm == NULL)
318 		tool->comm = process_event_stub;
319 	if (tool->fork == NULL)
320 		tool->fork = process_event_stub;
321 	if (tool->exit == NULL)
322 		tool->exit = process_event_stub;
323 	if (tool->lost == NULL)
324 		tool->lost = perf_event__process_lost;
325 	if (tool->lost_samples == NULL)
326 		tool->lost_samples = perf_event__process_lost_samples;
327 	if (tool->aux == NULL)
328 		tool->aux = perf_event__process_aux;
329 	if (tool->itrace_start == NULL)
330 		tool->itrace_start = perf_event__process_itrace_start;
331 	if (tool->context_switch == NULL)
332 		tool->context_switch = perf_event__process_switch;
333 	if (tool->read == NULL)
334 		tool->read = process_event_sample_stub;
335 	if (tool->throttle == NULL)
336 		tool->throttle = process_event_stub;
337 	if (tool->unthrottle == NULL)
338 		tool->unthrottle = process_event_stub;
339 	if (tool->attr == NULL)
340 		tool->attr = process_event_synth_attr_stub;
341 	if (tool->tracing_data == NULL)
342 		tool->tracing_data = process_event_synth_tracing_data_stub;
343 	if (tool->build_id == NULL)
344 		tool->build_id = process_build_id_stub;
345 	if (tool->finished_round == NULL) {
346 		if (tool->ordered_events)
347 			tool->finished_round = process_finished_round;
348 		else
349 			tool->finished_round = process_finished_round_stub;
350 	}
351 	if (tool->id_index == NULL)
352 		tool->id_index = process_id_index_stub;
353 	if (tool->auxtrace_info == NULL)
354 		tool->auxtrace_info = process_event_auxtrace_info_stub;
355 	if (tool->auxtrace == NULL)
356 		tool->auxtrace = process_event_auxtrace_stub;
357 	if (tool->auxtrace_error == NULL)
358 		tool->auxtrace_error = process_event_auxtrace_error_stub;
359 }
360 
swap_sample_id_all(union perf_event * event,void * data)361 static void swap_sample_id_all(union perf_event *event, void *data)
362 {
363 	void *end = (void *) event + event->header.size;
364 	int size = end - data;
365 
366 	BUG_ON(size % sizeof(u64));
367 	mem_bswap_64(data, size);
368 }
369 
perf_event__all64_swap(union perf_event * event,bool sample_id_all __maybe_unused)370 static void perf_event__all64_swap(union perf_event *event,
371 				   bool sample_id_all __maybe_unused)
372 {
373 	struct perf_event_header *hdr = &event->header;
374 	mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
375 }
376 
perf_event__comm_swap(union perf_event * event,bool sample_id_all)377 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
378 {
379 	event->comm.pid = bswap_32(event->comm.pid);
380 	event->comm.tid = bswap_32(event->comm.tid);
381 
382 	if (sample_id_all) {
383 		void *data = &event->comm.comm;
384 
385 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
386 		swap_sample_id_all(event, data);
387 	}
388 }
389 
perf_event__mmap_swap(union perf_event * event,bool sample_id_all)390 static void perf_event__mmap_swap(union perf_event *event,
391 				  bool sample_id_all)
392 {
393 	event->mmap.pid	  = bswap_32(event->mmap.pid);
394 	event->mmap.tid	  = bswap_32(event->mmap.tid);
395 	event->mmap.start = bswap_64(event->mmap.start);
396 	event->mmap.len	  = bswap_64(event->mmap.len);
397 	event->mmap.pgoff = bswap_64(event->mmap.pgoff);
398 
399 	if (sample_id_all) {
400 		void *data = &event->mmap.filename;
401 
402 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
403 		swap_sample_id_all(event, data);
404 	}
405 }
406 
perf_event__mmap2_swap(union perf_event * event,bool sample_id_all)407 static void perf_event__mmap2_swap(union perf_event *event,
408 				  bool sample_id_all)
409 {
410 	event->mmap2.pid   = bswap_32(event->mmap2.pid);
411 	event->mmap2.tid   = bswap_32(event->mmap2.tid);
412 	event->mmap2.start = bswap_64(event->mmap2.start);
413 	event->mmap2.len   = bswap_64(event->mmap2.len);
414 	event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
415 	event->mmap2.maj   = bswap_32(event->mmap2.maj);
416 	event->mmap2.min   = bswap_32(event->mmap2.min);
417 	event->mmap2.ino   = bswap_64(event->mmap2.ino);
418 	event->mmap2.ino_generation = bswap_64(event->mmap2.ino_generation);
419 
420 	if (sample_id_all) {
421 		void *data = &event->mmap2.filename;
422 
423 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
424 		swap_sample_id_all(event, data);
425 	}
426 }
perf_event__task_swap(union perf_event * event,bool sample_id_all)427 static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
428 {
429 	event->fork.pid	 = bswap_32(event->fork.pid);
430 	event->fork.tid	 = bswap_32(event->fork.tid);
431 	event->fork.ppid = bswap_32(event->fork.ppid);
432 	event->fork.ptid = bswap_32(event->fork.ptid);
433 	event->fork.time = bswap_64(event->fork.time);
434 
435 	if (sample_id_all)
436 		swap_sample_id_all(event, &event->fork + 1);
437 }
438 
perf_event__read_swap(union perf_event * event,bool sample_id_all)439 static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
440 {
441 	event->read.pid		 = bswap_32(event->read.pid);
442 	event->read.tid		 = bswap_32(event->read.tid);
443 	event->read.value	 = bswap_64(event->read.value);
444 	event->read.time_enabled = bswap_64(event->read.time_enabled);
445 	event->read.time_running = bswap_64(event->read.time_running);
446 	event->read.id		 = bswap_64(event->read.id);
447 
448 	if (sample_id_all)
449 		swap_sample_id_all(event, &event->read + 1);
450 }
451 
perf_event__aux_swap(union perf_event * event,bool sample_id_all)452 static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
453 {
454 	event->aux.aux_offset = bswap_64(event->aux.aux_offset);
455 	event->aux.aux_size   = bswap_64(event->aux.aux_size);
456 	event->aux.flags      = bswap_64(event->aux.flags);
457 
458 	if (sample_id_all)
459 		swap_sample_id_all(event, &event->aux + 1);
460 }
461 
perf_event__itrace_start_swap(union perf_event * event,bool sample_id_all)462 static void perf_event__itrace_start_swap(union perf_event *event,
463 					  bool sample_id_all)
464 {
465 	event->itrace_start.pid	 = bswap_32(event->itrace_start.pid);
466 	event->itrace_start.tid	 = bswap_32(event->itrace_start.tid);
467 
468 	if (sample_id_all)
469 		swap_sample_id_all(event, &event->itrace_start + 1);
470 }
471 
perf_event__switch_swap(union perf_event * event,bool sample_id_all)472 static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
473 {
474 	if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
475 		event->context_switch.next_prev_pid =
476 				bswap_32(event->context_switch.next_prev_pid);
477 		event->context_switch.next_prev_tid =
478 				bswap_32(event->context_switch.next_prev_tid);
479 	}
480 
481 	if (sample_id_all)
482 		swap_sample_id_all(event, &event->context_switch + 1);
483 }
484 
perf_event__throttle_swap(union perf_event * event,bool sample_id_all)485 static void perf_event__throttle_swap(union perf_event *event,
486 				      bool sample_id_all)
487 {
488 	event->throttle.time	  = bswap_64(event->throttle.time);
489 	event->throttle.id	  = bswap_64(event->throttle.id);
490 	event->throttle.stream_id = bswap_64(event->throttle.stream_id);
491 
492 	if (sample_id_all)
493 		swap_sample_id_all(event, &event->throttle + 1);
494 }
495 
revbyte(u8 b)496 static u8 revbyte(u8 b)
497 {
498 	int rev = (b >> 4) | ((b & 0xf) << 4);
499 	rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
500 	rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
501 	return (u8) rev;
502 }
503 
504 /*
505  * XXX this is hack in attempt to carry flags bitfield
506  * throught endian village. ABI says:
507  *
508  * Bit-fields are allocated from right to left (least to most significant)
509  * on little-endian implementations and from left to right (most to least
510  * significant) on big-endian implementations.
511  *
512  * The above seems to be byte specific, so we need to reverse each
513  * byte of the bitfield. 'Internet' also says this might be implementation
514  * specific and we probably need proper fix and carry perf_event_attr
515  * bitfield flags in separate data file FEAT_ section. Thought this seems
516  * to work for now.
517  */
swap_bitfield(u8 * p,unsigned len)518 static void swap_bitfield(u8 *p, unsigned len)
519 {
520 	unsigned i;
521 
522 	for (i = 0; i < len; i++) {
523 		*p = revbyte(*p);
524 		p++;
525 	}
526 }
527 
528 /* exported for swapping attributes in file header */
perf_event__attr_swap(struct perf_event_attr * attr)529 void perf_event__attr_swap(struct perf_event_attr *attr)
530 {
531 	attr->type		= bswap_32(attr->type);
532 	attr->size		= bswap_32(attr->size);
533 
534 #define bswap_safe(f, n) 					\
535 	(attr->size > (offsetof(struct perf_event_attr, f) + 	\
536 		       sizeof(attr->f) * (n)))
537 #define bswap_field(f, sz) 			\
538 do { 						\
539 	if (bswap_safe(f, 0))			\
540 		attr->f = bswap_##sz(attr->f);	\
541 } while(0)
542 #define bswap_field_32(f) bswap_field(f, 32)
543 #define bswap_field_64(f) bswap_field(f, 64)
544 
545 	bswap_field_64(config);
546 	bswap_field_64(sample_period);
547 	bswap_field_64(sample_type);
548 	bswap_field_64(read_format);
549 	bswap_field_32(wakeup_events);
550 	bswap_field_32(bp_type);
551 	bswap_field_64(bp_addr);
552 	bswap_field_64(bp_len);
553 	bswap_field_64(branch_sample_type);
554 	bswap_field_64(sample_regs_user);
555 	bswap_field_32(sample_stack_user);
556 	bswap_field_32(aux_watermark);
557 
558 	/*
559 	 * After read_format are bitfields. Check read_format because
560 	 * we are unable to use offsetof on bitfield.
561 	 */
562 	if (bswap_safe(read_format, 1))
563 		swap_bitfield((u8 *) (&attr->read_format + 1),
564 			      sizeof(u64));
565 #undef bswap_field_64
566 #undef bswap_field_32
567 #undef bswap_field
568 #undef bswap_safe
569 }
570 
perf_event__hdr_attr_swap(union perf_event * event,bool sample_id_all __maybe_unused)571 static void perf_event__hdr_attr_swap(union perf_event *event,
572 				      bool sample_id_all __maybe_unused)
573 {
574 	size_t size;
575 
576 	perf_event__attr_swap(&event->attr.attr);
577 
578 	size = event->header.size;
579 	size -= (void *)&event->attr.id - (void *)event;
580 	mem_bswap_64(event->attr.id, size);
581 }
582 
perf_event__event_type_swap(union perf_event * event,bool sample_id_all __maybe_unused)583 static void perf_event__event_type_swap(union perf_event *event,
584 					bool sample_id_all __maybe_unused)
585 {
586 	event->event_type.event_type.event_id =
587 		bswap_64(event->event_type.event_type.event_id);
588 }
589 
perf_event__tracing_data_swap(union perf_event * event,bool sample_id_all __maybe_unused)590 static void perf_event__tracing_data_swap(union perf_event *event,
591 					  bool sample_id_all __maybe_unused)
592 {
593 	event->tracing_data.size = bswap_32(event->tracing_data.size);
594 }
595 
perf_event__auxtrace_info_swap(union perf_event * event,bool sample_id_all __maybe_unused)596 static void perf_event__auxtrace_info_swap(union perf_event *event,
597 					   bool sample_id_all __maybe_unused)
598 {
599 	size_t size;
600 
601 	event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
602 
603 	size = event->header.size;
604 	size -= (void *)&event->auxtrace_info.priv - (void *)event;
605 	mem_bswap_64(event->auxtrace_info.priv, size);
606 }
607 
perf_event__auxtrace_swap(union perf_event * event,bool sample_id_all __maybe_unused)608 static void perf_event__auxtrace_swap(union perf_event *event,
609 				      bool sample_id_all __maybe_unused)
610 {
611 	event->auxtrace.size      = bswap_64(event->auxtrace.size);
612 	event->auxtrace.offset    = bswap_64(event->auxtrace.offset);
613 	event->auxtrace.reference = bswap_64(event->auxtrace.reference);
614 	event->auxtrace.idx       = bswap_32(event->auxtrace.idx);
615 	event->auxtrace.tid       = bswap_32(event->auxtrace.tid);
616 	event->auxtrace.cpu       = bswap_32(event->auxtrace.cpu);
617 }
618 
perf_event__auxtrace_error_swap(union perf_event * event,bool sample_id_all __maybe_unused)619 static void perf_event__auxtrace_error_swap(union perf_event *event,
620 					    bool sample_id_all __maybe_unused)
621 {
622 	event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
623 	event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
624 	event->auxtrace_error.cpu  = bswap_32(event->auxtrace_error.cpu);
625 	event->auxtrace_error.pid  = bswap_32(event->auxtrace_error.pid);
626 	event->auxtrace_error.tid  = bswap_32(event->auxtrace_error.tid);
627 	event->auxtrace_error.ip   = bswap_64(event->auxtrace_error.ip);
628 }
629 
630 typedef void (*perf_event__swap_op)(union perf_event *event,
631 				    bool sample_id_all);
632 
633 static perf_event__swap_op perf_event__swap_ops[] = {
634 	[PERF_RECORD_MMAP]		  = perf_event__mmap_swap,
635 	[PERF_RECORD_MMAP2]		  = perf_event__mmap2_swap,
636 	[PERF_RECORD_COMM]		  = perf_event__comm_swap,
637 	[PERF_RECORD_FORK]		  = perf_event__task_swap,
638 	[PERF_RECORD_EXIT]		  = perf_event__task_swap,
639 	[PERF_RECORD_LOST]		  = perf_event__all64_swap,
640 	[PERF_RECORD_READ]		  = perf_event__read_swap,
641 	[PERF_RECORD_THROTTLE]		  = perf_event__throttle_swap,
642 	[PERF_RECORD_UNTHROTTLE]	  = perf_event__throttle_swap,
643 	[PERF_RECORD_SAMPLE]		  = perf_event__all64_swap,
644 	[PERF_RECORD_AUX]		  = perf_event__aux_swap,
645 	[PERF_RECORD_ITRACE_START]	  = perf_event__itrace_start_swap,
646 	[PERF_RECORD_LOST_SAMPLES]	  = perf_event__all64_swap,
647 	[PERF_RECORD_SWITCH]		  = perf_event__switch_swap,
648 	[PERF_RECORD_SWITCH_CPU_WIDE]	  = perf_event__switch_swap,
649 	[PERF_RECORD_HEADER_ATTR]	  = perf_event__hdr_attr_swap,
650 	[PERF_RECORD_HEADER_EVENT_TYPE]	  = perf_event__event_type_swap,
651 	[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
652 	[PERF_RECORD_HEADER_BUILD_ID]	  = NULL,
653 	[PERF_RECORD_ID_INDEX]		  = perf_event__all64_swap,
654 	[PERF_RECORD_AUXTRACE_INFO]	  = perf_event__auxtrace_info_swap,
655 	[PERF_RECORD_AUXTRACE]		  = perf_event__auxtrace_swap,
656 	[PERF_RECORD_AUXTRACE_ERROR]	  = perf_event__auxtrace_error_swap,
657 	[PERF_RECORD_HEADER_MAX]	  = NULL,
658 };
659 
660 /*
661  * When perf record finishes a pass on every buffers, it records this pseudo
662  * event.
663  * We record the max timestamp t found in the pass n.
664  * Assuming these timestamps are monotonic across cpus, we know that if
665  * a buffer still has events with timestamps below t, they will be all
666  * available and then read in the pass n + 1.
667  * Hence when we start to read the pass n + 2, we can safely flush every
668  * events with timestamps below t.
669  *
670  *    ============ PASS n =================
671  *       CPU 0         |   CPU 1
672  *                     |
673  *    cnt1 timestamps  |   cnt2 timestamps
674  *          1          |         2
675  *          2          |         3
676  *          -          |         4  <--- max recorded
677  *
678  *    ============ PASS n + 1 ==============
679  *       CPU 0         |   CPU 1
680  *                     |
681  *    cnt1 timestamps  |   cnt2 timestamps
682  *          3          |         5
683  *          4          |         6
684  *          5          |         7 <---- max recorded
685  *
686  *      Flush every events below timestamp 4
687  *
688  *    ============ PASS n + 2 ==============
689  *       CPU 0         |   CPU 1
690  *                     |
691  *    cnt1 timestamps  |   cnt2 timestamps
692  *          6          |         8
693  *          7          |         9
694  *          -          |         10
695  *
696  *      Flush every events below timestamp 7
697  *      etc...
698  */
process_finished_round(struct perf_tool * tool __maybe_unused,union perf_event * event __maybe_unused,struct ordered_events * oe)699 static int process_finished_round(struct perf_tool *tool __maybe_unused,
700 				  union perf_event *event __maybe_unused,
701 				  struct ordered_events *oe)
702 {
703 	if (dump_trace)
704 		fprintf(stdout, "\n");
705 	return ordered_events__flush(oe, OE_FLUSH__ROUND);
706 }
707 
perf_session__queue_event(struct perf_session * s,union perf_event * event,struct perf_sample * sample,u64 file_offset)708 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
709 			      struct perf_sample *sample, u64 file_offset)
710 {
711 	return ordered_events__queue(&s->ordered_events, event, sample, file_offset);
712 }
713 
callchain__lbr_callstack_printf(struct perf_sample * sample)714 static void callchain__lbr_callstack_printf(struct perf_sample *sample)
715 {
716 	struct ip_callchain *callchain = sample->callchain;
717 	struct branch_stack *lbr_stack = sample->branch_stack;
718 	u64 kernel_callchain_nr = callchain->nr;
719 	unsigned int i;
720 
721 	for (i = 0; i < kernel_callchain_nr; i++) {
722 		if (callchain->ips[i] == PERF_CONTEXT_USER)
723 			break;
724 	}
725 
726 	if ((i != kernel_callchain_nr) && lbr_stack->nr) {
727 		u64 total_nr;
728 		/*
729 		 * LBR callstack can only get user call chain,
730 		 * i is kernel call chain number,
731 		 * 1 is PERF_CONTEXT_USER.
732 		 *
733 		 * The user call chain is stored in LBR registers.
734 		 * LBR are pair registers. The caller is stored
735 		 * in "from" register, while the callee is stored
736 		 * in "to" register.
737 		 * For example, there is a call stack
738 		 * "A"->"B"->"C"->"D".
739 		 * The LBR registers will recorde like
740 		 * "C"->"D", "B"->"C", "A"->"B".
741 		 * So only the first "to" register and all "from"
742 		 * registers are needed to construct the whole stack.
743 		 */
744 		total_nr = i + 1 + lbr_stack->nr + 1;
745 		kernel_callchain_nr = i + 1;
746 
747 		printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
748 
749 		for (i = 0; i < kernel_callchain_nr; i++)
750 			printf("..... %2d: %016" PRIx64 "\n",
751 			       i, callchain->ips[i]);
752 
753 		printf("..... %2d: %016" PRIx64 "\n",
754 		       (int)(kernel_callchain_nr), lbr_stack->entries[0].to);
755 		for (i = 0; i < lbr_stack->nr; i++)
756 			printf("..... %2d: %016" PRIx64 "\n",
757 			       (int)(i + kernel_callchain_nr + 1), lbr_stack->entries[i].from);
758 	}
759 }
760 
callchain__printf(struct perf_evsel * evsel,struct perf_sample * sample)761 static void callchain__printf(struct perf_evsel *evsel,
762 			      struct perf_sample *sample)
763 {
764 	unsigned int i;
765 	struct ip_callchain *callchain = sample->callchain;
766 
767 	if (has_branch_callstack(evsel))
768 		callchain__lbr_callstack_printf(sample);
769 
770 	printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
771 
772 	for (i = 0; i < callchain->nr; i++)
773 		printf("..... %2d: %016" PRIx64 "\n",
774 		       i, callchain->ips[i]);
775 }
776 
branch_stack__printf(struct perf_sample * sample)777 static void branch_stack__printf(struct perf_sample *sample)
778 {
779 	uint64_t i;
780 
781 	printf("... branch stack: nr:%" PRIu64 "\n", sample->branch_stack->nr);
782 
783 	for (i = 0; i < sample->branch_stack->nr; i++) {
784 		struct branch_entry *e = &sample->branch_stack->entries[i];
785 
786 		printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
787 			i, e->from, e->to,
788 			e->flags.cycles,
789 			e->flags.mispred ? "M" : " ",
790 			e->flags.predicted ? "P" : " ",
791 			e->flags.abort ? "A" : " ",
792 			e->flags.in_tx ? "T" : " ",
793 			(unsigned)e->flags.reserved);
794 	}
795 }
796 
regs_dump__printf(u64 mask,u64 * regs)797 static void regs_dump__printf(u64 mask, u64 *regs)
798 {
799 	unsigned rid, i = 0;
800 
801 	for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
802 		u64 val = regs[i++];
803 
804 		printf(".... %-5s 0x%" PRIx64 "\n",
805 		       perf_reg_name(rid), val);
806 	}
807 }
808 
809 static const char *regs_abi[] = {
810 	[PERF_SAMPLE_REGS_ABI_NONE] = "none",
811 	[PERF_SAMPLE_REGS_ABI_32] = "32-bit",
812 	[PERF_SAMPLE_REGS_ABI_64] = "64-bit",
813 };
814 
regs_dump_abi(struct regs_dump * d)815 static inline const char *regs_dump_abi(struct regs_dump *d)
816 {
817 	if (d->abi > PERF_SAMPLE_REGS_ABI_64)
818 		return "unknown";
819 
820 	return regs_abi[d->abi];
821 }
822 
regs__printf(const char * type,struct regs_dump * regs)823 static void regs__printf(const char *type, struct regs_dump *regs)
824 {
825 	u64 mask = regs->mask;
826 
827 	printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
828 	       type,
829 	       mask,
830 	       regs_dump_abi(regs));
831 
832 	regs_dump__printf(mask, regs->regs);
833 }
834 
regs_user__printf(struct perf_sample * sample)835 static void regs_user__printf(struct perf_sample *sample)
836 {
837 	struct regs_dump *user_regs = &sample->user_regs;
838 
839 	if (user_regs->regs)
840 		regs__printf("user", user_regs);
841 }
842 
regs_intr__printf(struct perf_sample * sample)843 static void regs_intr__printf(struct perf_sample *sample)
844 {
845 	struct regs_dump *intr_regs = &sample->intr_regs;
846 
847 	if (intr_regs->regs)
848 		regs__printf("intr", intr_regs);
849 }
850 
stack_user__printf(struct stack_dump * dump)851 static void stack_user__printf(struct stack_dump *dump)
852 {
853 	printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
854 	       dump->size, dump->offset);
855 }
856 
perf_evlist__print_tstamp(struct perf_evlist * evlist,union perf_event * event,struct perf_sample * sample)857 static void perf_evlist__print_tstamp(struct perf_evlist *evlist,
858 				       union perf_event *event,
859 				       struct perf_sample *sample)
860 {
861 	u64 sample_type = __perf_evlist__combined_sample_type(evlist);
862 
863 	if (event->header.type != PERF_RECORD_SAMPLE &&
864 	    !perf_evlist__sample_id_all(evlist)) {
865 		fputs("-1 -1 ", stdout);
866 		return;
867 	}
868 
869 	if ((sample_type & PERF_SAMPLE_CPU))
870 		printf("%u ", sample->cpu);
871 
872 	if (sample_type & PERF_SAMPLE_TIME)
873 		printf("%" PRIu64 " ", sample->time);
874 }
875 
sample_read__printf(struct perf_sample * sample,u64 read_format)876 static void sample_read__printf(struct perf_sample *sample, u64 read_format)
877 {
878 	printf("... sample_read:\n");
879 
880 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
881 		printf("...... time enabled %016" PRIx64 "\n",
882 		       sample->read.time_enabled);
883 
884 	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
885 		printf("...... time running %016" PRIx64 "\n",
886 		       sample->read.time_running);
887 
888 	if (read_format & PERF_FORMAT_GROUP) {
889 		u64 i;
890 
891 		printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
892 
893 		for (i = 0; i < sample->read.group.nr; i++) {
894 			struct sample_read_value *value;
895 
896 			value = &sample->read.group.values[i];
897 			printf("..... id %016" PRIx64
898 			       ", value %016" PRIx64 "\n",
899 			       value->id, value->value);
900 		}
901 	} else
902 		printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
903 			sample->read.one.id, sample->read.one.value);
904 }
905 
dump_event(struct perf_evlist * evlist,union perf_event * event,u64 file_offset,struct perf_sample * sample)906 static void dump_event(struct perf_evlist *evlist, union perf_event *event,
907 		       u64 file_offset, struct perf_sample *sample)
908 {
909 	if (!dump_trace)
910 		return;
911 
912 	printf("\n%#" PRIx64 " [%#x]: event: %d\n",
913 	       file_offset, event->header.size, event->header.type);
914 
915 	trace_event(event);
916 
917 	if (sample)
918 		perf_evlist__print_tstamp(evlist, event, sample);
919 
920 	printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
921 	       event->header.size, perf_event__name(event->header.type));
922 }
923 
dump_sample(struct perf_evsel * evsel,union perf_event * event,struct perf_sample * sample)924 static void dump_sample(struct perf_evsel *evsel, union perf_event *event,
925 			struct perf_sample *sample)
926 {
927 	u64 sample_type;
928 
929 	if (!dump_trace)
930 		return;
931 
932 	printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
933 	       event->header.misc, sample->pid, sample->tid, sample->ip,
934 	       sample->period, sample->addr);
935 
936 	sample_type = evsel->attr.sample_type;
937 
938 	if (sample_type & PERF_SAMPLE_CALLCHAIN)
939 		callchain__printf(evsel, sample);
940 
941 	if ((sample_type & PERF_SAMPLE_BRANCH_STACK) && !has_branch_callstack(evsel))
942 		branch_stack__printf(sample);
943 
944 	if (sample_type & PERF_SAMPLE_REGS_USER)
945 		regs_user__printf(sample);
946 
947 	if (sample_type & PERF_SAMPLE_REGS_INTR)
948 		regs_intr__printf(sample);
949 
950 	if (sample_type & PERF_SAMPLE_STACK_USER)
951 		stack_user__printf(&sample->user_stack);
952 
953 	if (sample_type & PERF_SAMPLE_WEIGHT)
954 		printf("... weight: %" PRIu64 "\n", sample->weight);
955 
956 	if (sample_type & PERF_SAMPLE_DATA_SRC)
957 		printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
958 
959 	if (sample_type & PERF_SAMPLE_TRANSACTION)
960 		printf("... transaction: %" PRIx64 "\n", sample->transaction);
961 
962 	if (sample_type & PERF_SAMPLE_READ)
963 		sample_read__printf(sample, evsel->attr.read_format);
964 }
965 
machines__find_for_cpumode(struct machines * machines,union perf_event * event,struct perf_sample * sample)966 static struct machine *machines__find_for_cpumode(struct machines *machines,
967 					       union perf_event *event,
968 					       struct perf_sample *sample)
969 {
970 	const u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
971 	struct machine *machine;
972 
973 	if (perf_guest &&
974 	    ((cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
975 	     (cpumode == PERF_RECORD_MISC_GUEST_USER))) {
976 		u32 pid;
977 
978 		if (event->header.type == PERF_RECORD_MMAP
979 		    || event->header.type == PERF_RECORD_MMAP2)
980 			pid = event->mmap.pid;
981 		else
982 			pid = sample->pid;
983 
984 		machine = machines__find(machines, pid);
985 		if (!machine)
986 			machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
987 		return machine;
988 	}
989 
990 	return &machines->host;
991 }
992 
deliver_sample_value(struct perf_evlist * evlist,struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct sample_read_value * v,struct machine * machine)993 static int deliver_sample_value(struct perf_evlist *evlist,
994 				struct perf_tool *tool,
995 				union perf_event *event,
996 				struct perf_sample *sample,
997 				struct sample_read_value *v,
998 				struct machine *machine)
999 {
1000 	struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
1001 
1002 	if (sid) {
1003 		sample->id     = v->id;
1004 		sample->period = v->value - sid->period;
1005 		sid->period    = v->value;
1006 	}
1007 
1008 	if (!sid || sid->evsel == NULL) {
1009 		++evlist->stats.nr_unknown_id;
1010 		return 0;
1011 	}
1012 
1013 	return tool->sample(tool, event, sample, sid->evsel, machine);
1014 }
1015 
deliver_sample_group(struct perf_evlist * evlist,struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct machine * machine)1016 static int deliver_sample_group(struct perf_evlist *evlist,
1017 				struct perf_tool *tool,
1018 				union  perf_event *event,
1019 				struct perf_sample *sample,
1020 				struct machine *machine)
1021 {
1022 	int ret = -EINVAL;
1023 	u64 i;
1024 
1025 	for (i = 0; i < sample->read.group.nr; i++) {
1026 		ret = deliver_sample_value(evlist, tool, event, sample,
1027 					   &sample->read.group.values[i],
1028 					   machine);
1029 		if (ret)
1030 			break;
1031 	}
1032 
1033 	return ret;
1034 }
1035 
1036 static int
perf_evlist__deliver_sample(struct perf_evlist * evlist,struct perf_tool * tool,union perf_event * event,struct perf_sample * sample,struct perf_evsel * evsel,struct machine * machine)1037  perf_evlist__deliver_sample(struct perf_evlist *evlist,
1038 			     struct perf_tool *tool,
1039 			     union  perf_event *event,
1040 			     struct perf_sample *sample,
1041 			     struct perf_evsel *evsel,
1042 			     struct machine *machine)
1043 {
1044 	/* We know evsel != NULL. */
1045 	u64 sample_type = evsel->attr.sample_type;
1046 	u64 read_format = evsel->attr.read_format;
1047 
1048 	/* Standard sample delievery. */
1049 	if (!(sample_type & PERF_SAMPLE_READ))
1050 		return tool->sample(tool, event, sample, evsel, machine);
1051 
1052 	/* For PERF_SAMPLE_READ we have either single or group mode. */
1053 	if (read_format & PERF_FORMAT_GROUP)
1054 		return deliver_sample_group(evlist, tool, event, sample,
1055 					    machine);
1056 	else
1057 		return deliver_sample_value(evlist, tool, event, sample,
1058 					    &sample->read.one, machine);
1059 }
1060 
machines__deliver_event(struct machines * machines,struct perf_evlist * evlist,union perf_event * event,struct perf_sample * sample,struct perf_tool * tool,u64 file_offset)1061 static int machines__deliver_event(struct machines *machines,
1062 				   struct perf_evlist *evlist,
1063 				   union perf_event *event,
1064 				   struct perf_sample *sample,
1065 				   struct perf_tool *tool, u64 file_offset)
1066 {
1067 	struct perf_evsel *evsel;
1068 	struct machine *machine;
1069 
1070 	dump_event(evlist, event, file_offset, sample);
1071 
1072 	evsel = perf_evlist__id2evsel(evlist, sample->id);
1073 
1074 	machine = machines__find_for_cpumode(machines, event, sample);
1075 
1076 	switch (event->header.type) {
1077 	case PERF_RECORD_SAMPLE:
1078 		if (evsel == NULL) {
1079 			++evlist->stats.nr_unknown_id;
1080 			return 0;
1081 		}
1082 		dump_sample(evsel, event, sample);
1083 		if (machine == NULL) {
1084 			++evlist->stats.nr_unprocessable_samples;
1085 			return 0;
1086 		}
1087 		return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
1088 	case PERF_RECORD_MMAP:
1089 		return tool->mmap(tool, event, sample, machine);
1090 	case PERF_RECORD_MMAP2:
1091 		if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1092 			++evlist->stats.nr_proc_map_timeout;
1093 		return tool->mmap2(tool, event, sample, machine);
1094 	case PERF_RECORD_COMM:
1095 		return tool->comm(tool, event, sample, machine);
1096 	case PERF_RECORD_FORK:
1097 		return tool->fork(tool, event, sample, machine);
1098 	case PERF_RECORD_EXIT:
1099 		return tool->exit(tool, event, sample, machine);
1100 	case PERF_RECORD_LOST:
1101 		if (tool->lost == perf_event__process_lost)
1102 			evlist->stats.total_lost += event->lost.lost;
1103 		return tool->lost(tool, event, sample, machine);
1104 	case PERF_RECORD_LOST_SAMPLES:
1105 		if (tool->lost_samples == perf_event__process_lost_samples)
1106 			evlist->stats.total_lost_samples += event->lost_samples.lost;
1107 		return tool->lost_samples(tool, event, sample, machine);
1108 	case PERF_RECORD_READ:
1109 		return tool->read(tool, event, sample, evsel, machine);
1110 	case PERF_RECORD_THROTTLE:
1111 		return tool->throttle(tool, event, sample, machine);
1112 	case PERF_RECORD_UNTHROTTLE:
1113 		return tool->unthrottle(tool, event, sample, machine);
1114 	case PERF_RECORD_AUX:
1115 		if (tool->aux == perf_event__process_aux &&
1116 		    (event->aux.flags & PERF_AUX_FLAG_TRUNCATED))
1117 			evlist->stats.total_aux_lost += 1;
1118 		return tool->aux(tool, event, sample, machine);
1119 	case PERF_RECORD_ITRACE_START:
1120 		return tool->itrace_start(tool, event, sample, machine);
1121 	case PERF_RECORD_SWITCH:
1122 	case PERF_RECORD_SWITCH_CPU_WIDE:
1123 		return tool->context_switch(tool, event, sample, machine);
1124 	default:
1125 		++evlist->stats.nr_unknown_events;
1126 		return -1;
1127 	}
1128 }
1129 
perf_session__deliver_event(struct perf_session * session,union perf_event * event,struct perf_sample * sample,struct perf_tool * tool,u64 file_offset)1130 static int perf_session__deliver_event(struct perf_session *session,
1131 				       union perf_event *event,
1132 				       struct perf_sample *sample,
1133 				       struct perf_tool *tool,
1134 				       u64 file_offset)
1135 {
1136 	int ret;
1137 
1138 	ret = auxtrace__process_event(session, event, sample, tool);
1139 	if (ret < 0)
1140 		return ret;
1141 	if (ret > 0)
1142 		return 0;
1143 
1144 	return machines__deliver_event(&session->machines, session->evlist,
1145 				       event, sample, tool, file_offset);
1146 }
1147 
perf_session__process_user_event(struct perf_session * session,union perf_event * event,u64 file_offset)1148 static s64 perf_session__process_user_event(struct perf_session *session,
1149 					    union perf_event *event,
1150 					    u64 file_offset)
1151 {
1152 	struct ordered_events *oe = &session->ordered_events;
1153 	struct perf_tool *tool = session->tool;
1154 	int fd = perf_data_file__fd(session->file);
1155 	int err;
1156 
1157 	dump_event(session->evlist, event, file_offset, NULL);
1158 
1159 	/* These events are processed right away */
1160 	switch (event->header.type) {
1161 	case PERF_RECORD_HEADER_ATTR:
1162 		err = tool->attr(tool, event, &session->evlist);
1163 		if (err == 0) {
1164 			perf_session__set_id_hdr_size(session);
1165 			perf_session__set_comm_exec(session);
1166 		}
1167 		return err;
1168 	case PERF_RECORD_HEADER_EVENT_TYPE:
1169 		/*
1170 		 * Depreceated, but we need to handle it for sake
1171 		 * of old data files create in pipe mode.
1172 		 */
1173 		return 0;
1174 	case PERF_RECORD_HEADER_TRACING_DATA:
1175 		/* setup for reading amidst mmap */
1176 		lseek(fd, file_offset, SEEK_SET);
1177 		return tool->tracing_data(tool, event, session);
1178 	case PERF_RECORD_HEADER_BUILD_ID:
1179 		return tool->build_id(tool, event, session);
1180 	case PERF_RECORD_FINISHED_ROUND:
1181 		return tool->finished_round(tool, event, oe);
1182 	case PERF_RECORD_ID_INDEX:
1183 		return tool->id_index(tool, event, session);
1184 	case PERF_RECORD_AUXTRACE_INFO:
1185 		return tool->auxtrace_info(tool, event, session);
1186 	case PERF_RECORD_AUXTRACE:
1187 		/* setup for reading amidst mmap */
1188 		lseek(fd, file_offset + event->header.size, SEEK_SET);
1189 		return tool->auxtrace(tool, event, session);
1190 	case PERF_RECORD_AUXTRACE_ERROR:
1191 		perf_session__auxtrace_error_inc(session, event);
1192 		return tool->auxtrace_error(tool, event, session);
1193 	default:
1194 		return -EINVAL;
1195 	}
1196 }
1197 
perf_session__deliver_synth_event(struct perf_session * session,union perf_event * event,struct perf_sample * sample)1198 int perf_session__deliver_synth_event(struct perf_session *session,
1199 				      union perf_event *event,
1200 				      struct perf_sample *sample)
1201 {
1202 	struct perf_evlist *evlist = session->evlist;
1203 	struct perf_tool *tool = session->tool;
1204 
1205 	events_stats__inc(&evlist->stats, event->header.type);
1206 
1207 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1208 		return perf_session__process_user_event(session, event, 0);
1209 
1210 	return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
1211 }
1212 
event_swap(union perf_event * event,bool sample_id_all)1213 static void event_swap(union perf_event *event, bool sample_id_all)
1214 {
1215 	perf_event__swap_op swap;
1216 
1217 	swap = perf_event__swap_ops[event->header.type];
1218 	if (swap)
1219 		swap(event, sample_id_all);
1220 }
1221 
perf_session__peek_event(struct perf_session * session,off_t file_offset,void * buf,size_t buf_sz,union perf_event ** event_ptr,struct perf_sample * sample)1222 int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1223 			     void *buf, size_t buf_sz,
1224 			     union perf_event **event_ptr,
1225 			     struct perf_sample *sample)
1226 {
1227 	union perf_event *event;
1228 	size_t hdr_sz, rest;
1229 	int fd;
1230 
1231 	if (session->one_mmap && !session->header.needs_swap) {
1232 		event = file_offset - session->one_mmap_offset +
1233 			session->one_mmap_addr;
1234 		goto out_parse_sample;
1235 	}
1236 
1237 	if (perf_data_file__is_pipe(session->file))
1238 		return -1;
1239 
1240 	fd = perf_data_file__fd(session->file);
1241 	hdr_sz = sizeof(struct perf_event_header);
1242 
1243 	if (buf_sz < hdr_sz)
1244 		return -1;
1245 
1246 	if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
1247 	    readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
1248 		return -1;
1249 
1250 	event = (union perf_event *)buf;
1251 
1252 	if (session->header.needs_swap)
1253 		perf_event_header__bswap(&event->header);
1254 
1255 	if (event->header.size < hdr_sz || event->header.size > buf_sz)
1256 		return -1;
1257 
1258 	buf += hdr_sz;
1259 	rest = event->header.size - hdr_sz;
1260 
1261 	if (readn(fd, buf, rest) != (ssize_t)rest)
1262 		return -1;
1263 
1264 	if (session->header.needs_swap)
1265 		event_swap(event, perf_evlist__sample_id_all(session->evlist));
1266 
1267 out_parse_sample:
1268 
1269 	if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1270 	    perf_evlist__parse_sample(session->evlist, event, sample))
1271 		return -1;
1272 
1273 	*event_ptr = event;
1274 
1275 	return 0;
1276 }
1277 
perf_session__process_event(struct perf_session * session,union perf_event * event,u64 file_offset)1278 static s64 perf_session__process_event(struct perf_session *session,
1279 				       union perf_event *event, u64 file_offset)
1280 {
1281 	struct perf_evlist *evlist = session->evlist;
1282 	struct perf_tool *tool = session->tool;
1283 	struct perf_sample sample;
1284 	int ret;
1285 
1286 	if (session->header.needs_swap)
1287 		event_swap(event, perf_evlist__sample_id_all(evlist));
1288 
1289 	if (event->header.type >= PERF_RECORD_HEADER_MAX)
1290 		return -EINVAL;
1291 
1292 	events_stats__inc(&evlist->stats, event->header.type);
1293 
1294 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1295 		return perf_session__process_user_event(session, event, file_offset);
1296 
1297 	/*
1298 	 * For all kernel events we get the sample data
1299 	 */
1300 	ret = perf_evlist__parse_sample(evlist, event, &sample);
1301 	if (ret)
1302 		return ret;
1303 
1304 	if (tool->ordered_events) {
1305 		ret = perf_session__queue_event(session, event, &sample, file_offset);
1306 		if (ret != -ETIME)
1307 			return ret;
1308 	}
1309 
1310 	return perf_session__deliver_event(session, event, &sample, tool,
1311 					   file_offset);
1312 }
1313 
perf_event_header__bswap(struct perf_event_header * hdr)1314 void perf_event_header__bswap(struct perf_event_header *hdr)
1315 {
1316 	hdr->type = bswap_32(hdr->type);
1317 	hdr->misc = bswap_16(hdr->misc);
1318 	hdr->size = bswap_16(hdr->size);
1319 }
1320 
perf_session__findnew(struct perf_session * session,pid_t pid)1321 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1322 {
1323 	return machine__findnew_thread(&session->machines.host, -1, pid);
1324 }
1325 
perf_session__register_idle_thread(struct perf_session * session)1326 struct thread *perf_session__register_idle_thread(struct perf_session *session)
1327 {
1328 	struct thread *thread;
1329 
1330 	thread = machine__findnew_thread(&session->machines.host, 0, 0);
1331 	if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
1332 		pr_err("problem inserting idle task.\n");
1333 		thread = NULL;
1334 	}
1335 
1336 	return thread;
1337 }
1338 
perf_session__warn_about_errors(const struct perf_session * session)1339 static void perf_session__warn_about_errors(const struct perf_session *session)
1340 {
1341 	const struct events_stats *stats = &session->evlist->stats;
1342 	const struct ordered_events *oe = &session->ordered_events;
1343 
1344 	if (session->tool->lost == perf_event__process_lost &&
1345 	    stats->nr_events[PERF_RECORD_LOST] != 0) {
1346 		ui__warning("Processed %d events and lost %d chunks!\n\n"
1347 			    "Check IO/CPU overload!\n\n",
1348 			    stats->nr_events[0],
1349 			    stats->nr_events[PERF_RECORD_LOST]);
1350 	}
1351 
1352 	if (session->tool->lost_samples == perf_event__process_lost_samples) {
1353 		double drop_rate;
1354 
1355 		drop_rate = (double)stats->total_lost_samples /
1356 			    (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1357 		if (drop_rate > 0.05) {
1358 			ui__warning("Processed %" PRIu64 " samples and lost %3.2f%% samples!\n\n",
1359 				    stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1360 				    drop_rate * 100.0);
1361 		}
1362 	}
1363 
1364 	if (session->tool->aux == perf_event__process_aux &&
1365 	    stats->total_aux_lost != 0) {
1366 		ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1367 			    stats->total_aux_lost,
1368 			    stats->nr_events[PERF_RECORD_AUX]);
1369 	}
1370 
1371 	if (stats->nr_unknown_events != 0) {
1372 		ui__warning("Found %u unknown events!\n\n"
1373 			    "Is this an older tool processing a perf.data "
1374 			    "file generated by a more recent tool?\n\n"
1375 			    "If that is not the case, consider "
1376 			    "reporting to linux-kernel@vger.kernel.org.\n\n",
1377 			    stats->nr_unknown_events);
1378 	}
1379 
1380 	if (stats->nr_unknown_id != 0) {
1381 		ui__warning("%u samples with id not present in the header\n",
1382 			    stats->nr_unknown_id);
1383 	}
1384 
1385 	if (stats->nr_invalid_chains != 0) {
1386 		ui__warning("Found invalid callchains!\n\n"
1387 			    "%u out of %u events were discarded for this reason.\n\n"
1388 			    "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1389 			    stats->nr_invalid_chains,
1390 			    stats->nr_events[PERF_RECORD_SAMPLE]);
1391 	}
1392 
1393 	if (stats->nr_unprocessable_samples != 0) {
1394 		ui__warning("%u unprocessable samples recorded.\n"
1395 			    "Do you have a KVM guest running and not using 'perf kvm'?\n",
1396 			    stats->nr_unprocessable_samples);
1397 	}
1398 
1399 	if (oe->nr_unordered_events != 0)
1400 		ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1401 
1402 	events_stats__auxtrace_error_warn(stats);
1403 
1404 	if (stats->nr_proc_map_timeout != 0) {
1405 		ui__warning("%d map information files for pre-existing threads were\n"
1406 			    "not processed, if there are samples for addresses they\n"
1407 			    "will not be resolved, you may find out which are these\n"
1408 			    "threads by running with -v and redirecting the output\n"
1409 			    "to a file.\n"
1410 			    "The time limit to process proc map is too short?\n"
1411 			    "Increase it by --proc-map-timeout\n",
1412 			    stats->nr_proc_map_timeout);
1413 	}
1414 }
1415 
perf_session__flush_thread_stack(struct thread * thread,void * p __maybe_unused)1416 static int perf_session__flush_thread_stack(struct thread *thread,
1417 					    void *p __maybe_unused)
1418 {
1419 	return thread_stack__flush(thread);
1420 }
1421 
perf_session__flush_thread_stacks(struct perf_session * session)1422 static int perf_session__flush_thread_stacks(struct perf_session *session)
1423 {
1424 	return machines__for_each_thread(&session->machines,
1425 					 perf_session__flush_thread_stack,
1426 					 NULL);
1427 }
1428 
1429 volatile int session_done;
1430 
__perf_session__process_pipe_events(struct perf_session * session)1431 static int __perf_session__process_pipe_events(struct perf_session *session)
1432 {
1433 	struct ordered_events *oe = &session->ordered_events;
1434 	struct perf_tool *tool = session->tool;
1435 	int fd = perf_data_file__fd(session->file);
1436 	union perf_event *event;
1437 	uint32_t size, cur_size = 0;
1438 	void *buf = NULL;
1439 	s64 skip = 0;
1440 	u64 head;
1441 	ssize_t err;
1442 	void *p;
1443 
1444 	perf_tool__fill_defaults(tool);
1445 
1446 	head = 0;
1447 	cur_size = sizeof(union perf_event);
1448 
1449 	buf = malloc(cur_size);
1450 	if (!buf)
1451 		return -errno;
1452 	ordered_events__set_copy_on_queue(oe, true);
1453 more:
1454 	event = buf;
1455 	err = readn(fd, event, sizeof(struct perf_event_header));
1456 	if (err <= 0) {
1457 		if (err == 0)
1458 			goto done;
1459 
1460 		pr_err("failed to read event header\n");
1461 		goto out_err;
1462 	}
1463 
1464 	if (session->header.needs_swap)
1465 		perf_event_header__bswap(&event->header);
1466 
1467 	size = event->header.size;
1468 	if (size < sizeof(struct perf_event_header)) {
1469 		pr_err("bad event header size\n");
1470 		goto out_err;
1471 	}
1472 
1473 	if (size > cur_size) {
1474 		void *new = realloc(buf, size);
1475 		if (!new) {
1476 			pr_err("failed to allocate memory to read event\n");
1477 			goto out_err;
1478 		}
1479 		buf = new;
1480 		cur_size = size;
1481 		event = buf;
1482 	}
1483 	p = event;
1484 	p += sizeof(struct perf_event_header);
1485 
1486 	if (size - sizeof(struct perf_event_header)) {
1487 		err = readn(fd, p, size - sizeof(struct perf_event_header));
1488 		if (err <= 0) {
1489 			if (err == 0) {
1490 				pr_err("unexpected end of event stream\n");
1491 				goto done;
1492 			}
1493 
1494 			pr_err("failed to read event data\n");
1495 			goto out_err;
1496 		}
1497 	}
1498 
1499 	if ((skip = perf_session__process_event(session, event, head)) < 0) {
1500 		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1501 		       head, event->header.size, event->header.type);
1502 		err = -EINVAL;
1503 		goto out_err;
1504 	}
1505 
1506 	head += size;
1507 
1508 	if (skip > 0)
1509 		head += skip;
1510 
1511 	if (!session_done())
1512 		goto more;
1513 done:
1514 	/* do the final flush for ordered samples */
1515 	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
1516 	if (err)
1517 		goto out_err;
1518 	err = auxtrace__flush_events(session, tool);
1519 	if (err)
1520 		goto out_err;
1521 	err = perf_session__flush_thread_stacks(session);
1522 out_err:
1523 	free(buf);
1524 	perf_session__warn_about_errors(session);
1525 	ordered_events__free(&session->ordered_events);
1526 	auxtrace__free_events(session);
1527 	return err;
1528 }
1529 
1530 static union perf_event *
fetch_mmaped_event(struct perf_session * session,u64 head,size_t mmap_size,char * buf)1531 fetch_mmaped_event(struct perf_session *session,
1532 		   u64 head, size_t mmap_size, char *buf)
1533 {
1534 	union perf_event *event;
1535 
1536 	/*
1537 	 * Ensure we have enough space remaining to read
1538 	 * the size of the event in the headers.
1539 	 */
1540 	if (head + sizeof(event->header) > mmap_size)
1541 		return NULL;
1542 
1543 	event = (union perf_event *)(buf + head);
1544 
1545 	if (session->header.needs_swap)
1546 		perf_event_header__bswap(&event->header);
1547 
1548 	if (head + event->header.size > mmap_size) {
1549 		/* We're not fetching the event so swap back again */
1550 		if (session->header.needs_swap)
1551 			perf_event_header__bswap(&event->header);
1552 		return NULL;
1553 	}
1554 
1555 	return event;
1556 }
1557 
1558 /*
1559  * On 64bit we can mmap the data file in one go. No need for tiny mmap
1560  * slices. On 32bit we use 32MB.
1561  */
1562 #if BITS_PER_LONG == 64
1563 #define MMAP_SIZE ULLONG_MAX
1564 #define NUM_MMAPS 1
1565 #else
1566 #define MMAP_SIZE (32 * 1024 * 1024ULL)
1567 #define NUM_MMAPS 128
1568 #endif
1569 
__perf_session__process_events(struct perf_session * session,u64 data_offset,u64 data_size,u64 file_size)1570 static int __perf_session__process_events(struct perf_session *session,
1571 					  u64 data_offset, u64 data_size,
1572 					  u64 file_size)
1573 {
1574 	struct ordered_events *oe = &session->ordered_events;
1575 	struct perf_tool *tool = session->tool;
1576 	int fd = perf_data_file__fd(session->file);
1577 	u64 head, page_offset, file_offset, file_pos, size;
1578 	int err, mmap_prot, mmap_flags, map_idx = 0;
1579 	size_t	mmap_size;
1580 	char *buf, *mmaps[NUM_MMAPS];
1581 	union perf_event *event;
1582 	struct ui_progress prog;
1583 	s64 skip;
1584 
1585 	perf_tool__fill_defaults(tool);
1586 
1587 	page_offset = page_size * (data_offset / page_size);
1588 	file_offset = page_offset;
1589 	head = data_offset - page_offset;
1590 
1591 	if (data_size == 0)
1592 		goto out;
1593 
1594 	if (data_offset + data_size < file_size)
1595 		file_size = data_offset + data_size;
1596 
1597 	ui_progress__init(&prog, file_size, "Processing events...");
1598 
1599 	mmap_size = MMAP_SIZE;
1600 	if (mmap_size > file_size) {
1601 		mmap_size = file_size;
1602 		session->one_mmap = true;
1603 	}
1604 
1605 	memset(mmaps, 0, sizeof(mmaps));
1606 
1607 	mmap_prot  = PROT_READ;
1608 	mmap_flags = MAP_SHARED;
1609 
1610 	if (session->header.needs_swap) {
1611 		mmap_prot  |= PROT_WRITE;
1612 		mmap_flags = MAP_PRIVATE;
1613 	}
1614 remap:
1615 	buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, fd,
1616 		   file_offset);
1617 	if (buf == MAP_FAILED) {
1618 		pr_err("failed to mmap file\n");
1619 		err = -errno;
1620 		goto out_err;
1621 	}
1622 	mmaps[map_idx] = buf;
1623 	map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
1624 	file_pos = file_offset + head;
1625 	if (session->one_mmap) {
1626 		session->one_mmap_addr = buf;
1627 		session->one_mmap_offset = file_offset;
1628 	}
1629 
1630 more:
1631 	event = fetch_mmaped_event(session, head, mmap_size, buf);
1632 	if (!event) {
1633 		if (mmaps[map_idx]) {
1634 			munmap(mmaps[map_idx], mmap_size);
1635 			mmaps[map_idx] = NULL;
1636 		}
1637 
1638 		page_offset = page_size * (head / page_size);
1639 		file_offset += page_offset;
1640 		head -= page_offset;
1641 		goto remap;
1642 	}
1643 
1644 	size = event->header.size;
1645 
1646 	if (size < sizeof(struct perf_event_header) ||
1647 	    (skip = perf_session__process_event(session, event, file_pos)) < 0) {
1648 		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
1649 		       file_offset + head, event->header.size,
1650 		       event->header.type);
1651 		err = -EINVAL;
1652 		goto out_err;
1653 	}
1654 
1655 	if (skip)
1656 		size += skip;
1657 
1658 	head += size;
1659 	file_pos += size;
1660 
1661 	ui_progress__update(&prog, size);
1662 
1663 	if (session_done())
1664 		goto out;
1665 
1666 	if (file_pos < file_size)
1667 		goto more;
1668 
1669 out:
1670 	/* do the final flush for ordered samples */
1671 	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
1672 	if (err)
1673 		goto out_err;
1674 	err = auxtrace__flush_events(session, tool);
1675 	if (err)
1676 		goto out_err;
1677 	err = perf_session__flush_thread_stacks(session);
1678 out_err:
1679 	ui_progress__finish();
1680 	perf_session__warn_about_errors(session);
1681 	ordered_events__free(&session->ordered_events);
1682 	auxtrace__free_events(session);
1683 	session->one_mmap = false;
1684 	return err;
1685 }
1686 
perf_session__process_events(struct perf_session * session)1687 int perf_session__process_events(struct perf_session *session)
1688 {
1689 	u64 size = perf_data_file__size(session->file);
1690 	int err;
1691 
1692 	if (perf_session__register_idle_thread(session) == NULL)
1693 		return -ENOMEM;
1694 
1695 	if (!perf_data_file__is_pipe(session->file))
1696 		err = __perf_session__process_events(session,
1697 						     session->header.data_offset,
1698 						     session->header.data_size, size);
1699 	else
1700 		err = __perf_session__process_pipe_events(session);
1701 
1702 	return err;
1703 }
1704 
perf_session__has_traces(struct perf_session * session,const char * msg)1705 bool perf_session__has_traces(struct perf_session *session, const char *msg)
1706 {
1707 	struct perf_evsel *evsel;
1708 
1709 	evlist__for_each(session->evlist, evsel) {
1710 		if (evsel->attr.type == PERF_TYPE_TRACEPOINT)
1711 			return true;
1712 	}
1713 
1714 	pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
1715 	return false;
1716 }
1717 
maps__set_kallsyms_ref_reloc_sym(struct map ** maps,const char * symbol_name,u64 addr)1718 int maps__set_kallsyms_ref_reloc_sym(struct map **maps,
1719 				     const char *symbol_name, u64 addr)
1720 {
1721 	char *bracket;
1722 	enum map_type i;
1723 	struct ref_reloc_sym *ref;
1724 
1725 	ref = zalloc(sizeof(struct ref_reloc_sym));
1726 	if (ref == NULL)
1727 		return -ENOMEM;
1728 
1729 	ref->name = strdup(symbol_name);
1730 	if (ref->name == NULL) {
1731 		free(ref);
1732 		return -ENOMEM;
1733 	}
1734 
1735 	bracket = strchr(ref->name, ']');
1736 	if (bracket)
1737 		*bracket = '\0';
1738 
1739 	ref->addr = addr;
1740 
1741 	for (i = 0; i < MAP__NR_TYPES; ++i) {
1742 		struct kmap *kmap = map__kmap(maps[i]);
1743 
1744 		if (!kmap)
1745 			continue;
1746 		kmap->ref_reloc_sym = ref;
1747 	}
1748 
1749 	return 0;
1750 }
1751 
perf_session__fprintf_dsos(struct perf_session * session,FILE * fp)1752 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
1753 {
1754 	return machines__fprintf_dsos(&session->machines, fp);
1755 }
1756 
perf_session__fprintf_dsos_buildid(struct perf_session * session,FILE * fp,bool (skip)(struct dso * dso,int parm),int parm)1757 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
1758 					  bool (skip)(struct dso *dso, int parm), int parm)
1759 {
1760 	return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
1761 }
1762 
perf_session__fprintf_nr_events(struct perf_session * session,FILE * fp)1763 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
1764 {
1765 	size_t ret;
1766 	const char *msg = "";
1767 
1768 	if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
1769 		msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
1770 
1771 	ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
1772 
1773 	ret += events_stats__fprintf(&session->evlist->stats, fp);
1774 	return ret;
1775 }
1776 
perf_session__fprintf(struct perf_session * session,FILE * fp)1777 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
1778 {
1779 	/*
1780 	 * FIXME: Here we have to actually print all the machines in this
1781 	 * session, not just the host...
1782 	 */
1783 	return machine__fprintf(&session->machines.host, fp);
1784 }
1785 
perf_session__find_first_evtype(struct perf_session * session,unsigned int type)1786 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
1787 					      unsigned int type)
1788 {
1789 	struct perf_evsel *pos;
1790 
1791 	evlist__for_each(session->evlist, pos) {
1792 		if (pos->attr.type == type)
1793 			return pos;
1794 	}
1795 	return NULL;
1796 }
1797 
perf_evsel__print_ip(struct perf_evsel * evsel,struct perf_sample * sample,struct addr_location * al,unsigned int print_opts,unsigned int stack_depth)1798 void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
1799 			  struct addr_location *al,
1800 			  unsigned int print_opts, unsigned int stack_depth)
1801 {
1802 	struct callchain_cursor_node *node;
1803 	int print_ip = print_opts & PRINT_IP_OPT_IP;
1804 	int print_sym = print_opts & PRINT_IP_OPT_SYM;
1805 	int print_dso = print_opts & PRINT_IP_OPT_DSO;
1806 	int print_symoffset = print_opts & PRINT_IP_OPT_SYMOFFSET;
1807 	int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
1808 	int print_srcline = print_opts & PRINT_IP_OPT_SRCLINE;
1809 	char s = print_oneline ? ' ' : '\t';
1810 
1811 	if (symbol_conf.use_callchain && sample->callchain) {
1812 		struct addr_location node_al;
1813 
1814 		if (thread__resolve_callchain(al->thread, evsel,
1815 					      sample, NULL, NULL,
1816 					      stack_depth) != 0) {
1817 			if (verbose)
1818 				error("Failed to resolve callchain. Skipping\n");
1819 			return;
1820 		}
1821 		callchain_cursor_commit(&callchain_cursor);
1822 
1823 		if (print_symoffset)
1824 			node_al = *al;
1825 
1826 		while (stack_depth) {
1827 			u64 addr = 0;
1828 
1829 			node = callchain_cursor_current(&callchain_cursor);
1830 			if (!node)
1831 				break;
1832 
1833 			if (node->sym && node->sym->ignore)
1834 				goto next;
1835 
1836 			if (print_ip)
1837 				printf("%c%16" PRIx64, s, node->ip);
1838 
1839 			if (node->map)
1840 				addr = node->map->map_ip(node->map, node->ip);
1841 
1842 			if (print_sym) {
1843 				printf(" ");
1844 				if (print_symoffset) {
1845 					node_al.addr = addr;
1846 					node_al.map  = node->map;
1847 					symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
1848 				} else
1849 					symbol__fprintf_symname(node->sym, stdout);
1850 			}
1851 
1852 			if (print_dso) {
1853 				printf(" (");
1854 				map__fprintf_dsoname(node->map, stdout);
1855 				printf(")");
1856 			}
1857 
1858 			if (print_srcline)
1859 				map__fprintf_srcline(node->map, addr, "\n  ",
1860 						     stdout);
1861 
1862 			if (!print_oneline)
1863 				printf("\n");
1864 
1865 			stack_depth--;
1866 next:
1867 			callchain_cursor_advance(&callchain_cursor);
1868 		}
1869 
1870 	} else {
1871 		if (al->sym && al->sym->ignore)
1872 			return;
1873 
1874 		if (print_ip)
1875 			printf("%16" PRIx64, sample->ip);
1876 
1877 		if (print_sym) {
1878 			printf(" ");
1879 			if (print_symoffset)
1880 				symbol__fprintf_symname_offs(al->sym, al,
1881 							     stdout);
1882 			else
1883 				symbol__fprintf_symname(al->sym, stdout);
1884 		}
1885 
1886 		if (print_dso) {
1887 			printf(" (");
1888 			map__fprintf_dsoname(al->map, stdout);
1889 			printf(")");
1890 		}
1891 
1892 		if (print_srcline)
1893 			map__fprintf_srcline(al->map, al->addr, "\n  ", stdout);
1894 	}
1895 }
1896 
perf_session__cpu_bitmap(struct perf_session * session,const char * cpu_list,unsigned long * cpu_bitmap)1897 int perf_session__cpu_bitmap(struct perf_session *session,
1898 			     const char *cpu_list, unsigned long *cpu_bitmap)
1899 {
1900 	int i, err = -1;
1901 	struct cpu_map *map;
1902 
1903 	for (i = 0; i < PERF_TYPE_MAX; ++i) {
1904 		struct perf_evsel *evsel;
1905 
1906 		evsel = perf_session__find_first_evtype(session, i);
1907 		if (!evsel)
1908 			continue;
1909 
1910 		if (!(evsel->attr.sample_type & PERF_SAMPLE_CPU)) {
1911 			pr_err("File does not contain CPU events. "
1912 			       "Remove -c option to proceed.\n");
1913 			return -1;
1914 		}
1915 	}
1916 
1917 	map = cpu_map__new(cpu_list);
1918 	if (map == NULL) {
1919 		pr_err("Invalid cpu_list\n");
1920 		return -1;
1921 	}
1922 
1923 	for (i = 0; i < map->nr; i++) {
1924 		int cpu = map->map[i];
1925 
1926 		if (cpu >= MAX_NR_CPUS) {
1927 			pr_err("Requested CPU %d too large. "
1928 			       "Consider raising MAX_NR_CPUS\n", cpu);
1929 			goto out_delete_map;
1930 		}
1931 
1932 		set_bit(cpu, cpu_bitmap);
1933 	}
1934 
1935 	err = 0;
1936 
1937 out_delete_map:
1938 	cpu_map__put(map);
1939 	return err;
1940 }
1941 
perf_session__fprintf_info(struct perf_session * session,FILE * fp,bool full)1942 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
1943 				bool full)
1944 {
1945 	struct stat st;
1946 	int fd, ret;
1947 
1948 	if (session == NULL || fp == NULL)
1949 		return;
1950 
1951 	fd = perf_data_file__fd(session->file);
1952 
1953 	ret = fstat(fd, &st);
1954 	if (ret == -1)
1955 		return;
1956 
1957 	fprintf(fp, "# ========\n");
1958 	fprintf(fp, "# captured on: %s", ctime(&st.st_ctime));
1959 	perf_header__fprintf_info(session, fp, full);
1960 	fprintf(fp, "# ========\n#\n");
1961 }
1962 
1963 
__perf_session__set_tracepoints_handlers(struct perf_session * session,const struct perf_evsel_str_handler * assocs,size_t nr_assocs)1964 int __perf_session__set_tracepoints_handlers(struct perf_session *session,
1965 					     const struct perf_evsel_str_handler *assocs,
1966 					     size_t nr_assocs)
1967 {
1968 	struct perf_evsel *evsel;
1969 	size_t i;
1970 	int err;
1971 
1972 	for (i = 0; i < nr_assocs; i++) {
1973 		/*
1974 		 * Adding a handler for an event not in the session,
1975 		 * just ignore it.
1976 		 */
1977 		evsel = perf_evlist__find_tracepoint_by_name(session->evlist, assocs[i].name);
1978 		if (evsel == NULL)
1979 			continue;
1980 
1981 		err = -EEXIST;
1982 		if (evsel->handler != NULL)
1983 			goto out;
1984 		evsel->handler = assocs[i].handler;
1985 	}
1986 
1987 	err = 0;
1988 out:
1989 	return err;
1990 }
1991 
perf_event__process_id_index(struct perf_tool * tool __maybe_unused,union perf_event * event,struct perf_session * session)1992 int perf_event__process_id_index(struct perf_tool *tool __maybe_unused,
1993 				 union perf_event *event,
1994 				 struct perf_session *session)
1995 {
1996 	struct perf_evlist *evlist = session->evlist;
1997 	struct id_index_event *ie = &event->id_index;
1998 	size_t i, nr, max_nr;
1999 
2000 	max_nr = (ie->header.size - sizeof(struct id_index_event)) /
2001 		 sizeof(struct id_index_entry);
2002 	nr = ie->nr;
2003 	if (nr > max_nr)
2004 		return -EINVAL;
2005 
2006 	if (dump_trace)
2007 		fprintf(stdout, " nr: %zu\n", nr);
2008 
2009 	for (i = 0; i < nr; i++) {
2010 		struct id_index_entry *e = &ie->entries[i];
2011 		struct perf_sample_id *sid;
2012 
2013 		if (dump_trace) {
2014 			fprintf(stdout,	" ... id: %"PRIu64, e->id);
2015 			fprintf(stdout,	"  idx: %"PRIu64, e->idx);
2016 			fprintf(stdout,	"  cpu: %"PRId64, e->cpu);
2017 			fprintf(stdout,	"  tid: %"PRId64"\n", e->tid);
2018 		}
2019 
2020 		sid = perf_evlist__id2sid(evlist, e->id);
2021 		if (!sid)
2022 			return -ENOENT;
2023 		sid->idx = e->idx;
2024 		sid->cpu = e->cpu;
2025 		sid->tid = e->tid;
2026 	}
2027 	return 0;
2028 }
2029 
perf_event__synthesize_id_index(struct perf_tool * tool,perf_event__handler_t process,struct perf_evlist * evlist,struct machine * machine)2030 int perf_event__synthesize_id_index(struct perf_tool *tool,
2031 				    perf_event__handler_t process,
2032 				    struct perf_evlist *evlist,
2033 				    struct machine *machine)
2034 {
2035 	union perf_event *ev;
2036 	struct perf_evsel *evsel;
2037 	size_t nr = 0, i = 0, sz, max_nr, n;
2038 	int err;
2039 
2040 	pr_debug2("Synthesizing id index\n");
2041 
2042 	max_nr = (UINT16_MAX - sizeof(struct id_index_event)) /
2043 		 sizeof(struct id_index_entry);
2044 
2045 	evlist__for_each(evlist, evsel)
2046 		nr += evsel->ids;
2047 
2048 	n = nr > max_nr ? max_nr : nr;
2049 	sz = sizeof(struct id_index_event) + n * sizeof(struct id_index_entry);
2050 	ev = zalloc(sz);
2051 	if (!ev)
2052 		return -ENOMEM;
2053 
2054 	ev->id_index.header.type = PERF_RECORD_ID_INDEX;
2055 	ev->id_index.header.size = sz;
2056 	ev->id_index.nr = n;
2057 
2058 	evlist__for_each(evlist, evsel) {
2059 		u32 j;
2060 
2061 		for (j = 0; j < evsel->ids; j++) {
2062 			struct id_index_entry *e;
2063 			struct perf_sample_id *sid;
2064 
2065 			if (i >= n) {
2066 				err = process(tool, ev, NULL, machine);
2067 				if (err)
2068 					goto out_err;
2069 				nr -= n;
2070 				i = 0;
2071 			}
2072 
2073 			e = &ev->id_index.entries[i++];
2074 
2075 			e->id = evsel->id[j];
2076 
2077 			sid = perf_evlist__id2sid(evlist, e->id);
2078 			if (!sid) {
2079 				free(ev);
2080 				return -ENOENT;
2081 			}
2082 
2083 			e->idx = sid->idx;
2084 			e->cpu = sid->cpu;
2085 			e->tid = sid->tid;
2086 		}
2087 	}
2088 
2089 	sz = sizeof(struct id_index_event) + nr * sizeof(struct id_index_entry);
2090 	ev->id_index.header.size = sz;
2091 	ev->id_index.nr = nr;
2092 
2093 	err = process(tool, ev, NULL, machine);
2094 out_err:
2095 	free(ev);
2096 
2097 	return err;
2098 }
2099