xref: /linux/tools/perf/util/session.c (revision 4359a011e259a4608afc7fb3635370c9d4ba5943)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <errno.h>
3 #include <inttypes.h>
4 #include <linux/err.h>
5 #include <linux/kernel.h>
6 #include <linux/zalloc.h>
7 #include <api/fs/fs.h>
8 
9 #include <byteswap.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/mman.h>
13 #include <perf/cpumap.h>
14 
15 #include "map_symbol.h"
16 #include "branch.h"
17 #include "debug.h"
18 #include "env.h"
19 #include "evlist.h"
20 #include "evsel.h"
21 #include "memswap.h"
22 #include "map.h"
23 #include "symbol.h"
24 #include "session.h"
25 #include "tool.h"
26 #include "perf_regs.h"
27 #include "asm/bug.h"
28 #include "auxtrace.h"
29 #include "thread.h"
30 #include "thread-stack.h"
31 #include "sample-raw.h"
32 #include "stat.h"
33 #include "tsc.h"
34 #include "ui/progress.h"
35 #include "../perf.h"
36 #include "arch/common.h"
37 #include "units.h"
38 #include <internal/lib.h>
39 
40 #ifdef HAVE_ZSTD_SUPPORT
41 static int perf_session__process_compressed_event(struct perf_session *session,
42 						  union perf_event *event, u64 file_offset,
43 						  const char *file_path)
44 {
45 	void *src;
46 	size_t decomp_size, src_size;
47 	u64 decomp_last_rem = 0;
48 	size_t mmap_len, decomp_len = session->header.env.comp_mmap_len;
49 	struct decomp *decomp, *decomp_last = session->active_decomp->decomp_last;
50 
51 	if (decomp_last) {
52 		decomp_last_rem = decomp_last->size - decomp_last->head;
53 		decomp_len += decomp_last_rem;
54 	}
55 
56 	mmap_len = sizeof(struct decomp) + decomp_len;
57 	decomp = mmap(NULL, mmap_len, PROT_READ|PROT_WRITE,
58 		      MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
59 	if (decomp == MAP_FAILED) {
60 		pr_err("Couldn't allocate memory for decompression\n");
61 		return -1;
62 	}
63 
64 	decomp->file_pos = file_offset;
65 	decomp->file_path = file_path;
66 	decomp->mmap_len = mmap_len;
67 	decomp->head = 0;
68 
69 	if (decomp_last_rem) {
70 		memcpy(decomp->data, &(decomp_last->data[decomp_last->head]), decomp_last_rem);
71 		decomp->size = decomp_last_rem;
72 	}
73 
74 	src = (void *)event + sizeof(struct perf_record_compressed);
75 	src_size = event->pack.header.size - sizeof(struct perf_record_compressed);
76 
77 	decomp_size = zstd_decompress_stream(session->active_decomp->zstd_decomp, src, src_size,
78 				&(decomp->data[decomp_last_rem]), decomp_len - decomp_last_rem);
79 	if (!decomp_size) {
80 		munmap(decomp, mmap_len);
81 		pr_err("Couldn't decompress data\n");
82 		return -1;
83 	}
84 
85 	decomp->size += decomp_size;
86 
87 	if (session->active_decomp->decomp == NULL)
88 		session->active_decomp->decomp = decomp;
89 	else
90 		session->active_decomp->decomp_last->next = decomp;
91 
92 	session->active_decomp->decomp_last = decomp;
93 
94 	pr_debug("decomp (B): %zd to %zd\n", src_size, decomp_size);
95 
96 	return 0;
97 }
98 #else /* !HAVE_ZSTD_SUPPORT */
99 #define perf_session__process_compressed_event perf_session__process_compressed_event_stub
100 #endif
101 
102 static int perf_session__deliver_event(struct perf_session *session,
103 				       union perf_event *event,
104 				       struct perf_tool *tool,
105 				       u64 file_offset,
106 				       const char *file_path);
107 
108 static int perf_session__open(struct perf_session *session, int repipe_fd)
109 {
110 	struct perf_data *data = session->data;
111 
112 	if (perf_session__read_header(session, repipe_fd) < 0) {
113 		pr_err("incompatible file format (rerun with -v to learn more)\n");
114 		return -1;
115 	}
116 
117 	if (perf_data__is_pipe(data))
118 		return 0;
119 
120 	if (perf_header__has_feat(&session->header, HEADER_STAT))
121 		return 0;
122 
123 	if (!evlist__valid_sample_type(session->evlist)) {
124 		pr_err("non matching sample_type\n");
125 		return -1;
126 	}
127 
128 	if (!evlist__valid_sample_id_all(session->evlist)) {
129 		pr_err("non matching sample_id_all\n");
130 		return -1;
131 	}
132 
133 	if (!evlist__valid_read_format(session->evlist)) {
134 		pr_err("non matching read_format\n");
135 		return -1;
136 	}
137 
138 	return 0;
139 }
140 
141 void perf_session__set_id_hdr_size(struct perf_session *session)
142 {
143 	u16 id_hdr_size = evlist__id_hdr_size(session->evlist);
144 
145 	machines__set_id_hdr_size(&session->machines, id_hdr_size);
146 }
147 
148 int perf_session__create_kernel_maps(struct perf_session *session)
149 {
150 	int ret = machine__create_kernel_maps(&session->machines.host);
151 
152 	if (ret >= 0)
153 		ret = machines__create_guest_kernel_maps(&session->machines);
154 	return ret;
155 }
156 
157 static void perf_session__destroy_kernel_maps(struct perf_session *session)
158 {
159 	machines__destroy_kernel_maps(&session->machines);
160 }
161 
162 static bool perf_session__has_comm_exec(struct perf_session *session)
163 {
164 	struct evsel *evsel;
165 
166 	evlist__for_each_entry(session->evlist, evsel) {
167 		if (evsel->core.attr.comm_exec)
168 			return true;
169 	}
170 
171 	return false;
172 }
173 
174 static void perf_session__set_comm_exec(struct perf_session *session)
175 {
176 	bool comm_exec = perf_session__has_comm_exec(session);
177 
178 	machines__set_comm_exec(&session->machines, comm_exec);
179 }
180 
181 static int ordered_events__deliver_event(struct ordered_events *oe,
182 					 struct ordered_event *event)
183 {
184 	struct perf_session *session = container_of(oe, struct perf_session,
185 						    ordered_events);
186 
187 	return perf_session__deliver_event(session, event->event,
188 					   session->tool, event->file_offset,
189 					   event->file_path);
190 }
191 
192 struct perf_session *__perf_session__new(struct perf_data *data,
193 					 bool repipe, int repipe_fd,
194 					 struct perf_tool *tool)
195 {
196 	int ret = -ENOMEM;
197 	struct perf_session *session = zalloc(sizeof(*session));
198 
199 	if (!session)
200 		goto out;
201 
202 	session->repipe = repipe;
203 	session->tool   = tool;
204 	session->decomp_data.zstd_decomp = &session->zstd_data;
205 	session->active_decomp = &session->decomp_data;
206 	INIT_LIST_HEAD(&session->auxtrace_index);
207 	machines__init(&session->machines);
208 	ordered_events__init(&session->ordered_events,
209 			     ordered_events__deliver_event, NULL);
210 
211 	perf_env__init(&session->header.env);
212 	if (data) {
213 		ret = perf_data__open(data);
214 		if (ret < 0)
215 			goto out_delete;
216 
217 		session->data = data;
218 
219 		if (perf_data__is_read(data)) {
220 			ret = perf_session__open(session, repipe_fd);
221 			if (ret < 0)
222 				goto out_delete;
223 
224 			/*
225 			 * set session attributes that are present in perf.data
226 			 * but not in pipe-mode.
227 			 */
228 			if (!data->is_pipe) {
229 				perf_session__set_id_hdr_size(session);
230 				perf_session__set_comm_exec(session);
231 			}
232 
233 			evlist__init_trace_event_sample_raw(session->evlist);
234 
235 			/* Open the directory data. */
236 			if (data->is_dir) {
237 				ret = perf_data__open_dir(data);
238 				if (ret)
239 					goto out_delete;
240 			}
241 
242 			if (!symbol_conf.kallsyms_name &&
243 			    !symbol_conf.vmlinux_name)
244 				symbol_conf.kallsyms_name = perf_data__kallsyms_name(data);
245 		}
246 	} else  {
247 		session->machines.host.env = &perf_env;
248 	}
249 
250 	session->machines.host.single_address_space =
251 		perf_env__single_address_space(session->machines.host.env);
252 
253 	if (!data || perf_data__is_write(data)) {
254 		/*
255 		 * In O_RDONLY mode this will be performed when reading the
256 		 * kernel MMAP event, in perf_event__process_mmap().
257 		 */
258 		if (perf_session__create_kernel_maps(session) < 0)
259 			pr_warning("Cannot read kernel map\n");
260 	}
261 
262 	/*
263 	 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
264 	 * processed, so evlist__sample_id_all is not meaningful here.
265 	 */
266 	if ((!data || !data->is_pipe) && tool && tool->ordering_requires_timestamps &&
267 	    tool->ordered_events && !evlist__sample_id_all(session->evlist)) {
268 		dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
269 		tool->ordered_events = false;
270 	}
271 
272 	return session;
273 
274  out_delete:
275 	perf_session__delete(session);
276  out:
277 	return ERR_PTR(ret);
278 }
279 
280 static void perf_session__delete_threads(struct perf_session *session)
281 {
282 	machine__delete_threads(&session->machines.host);
283 }
284 
285 static void perf_decomp__release_events(struct decomp *next)
286 {
287 	struct decomp *decomp;
288 	size_t mmap_len;
289 
290 	do {
291 		decomp = next;
292 		if (decomp == NULL)
293 			break;
294 		next = decomp->next;
295 		mmap_len = decomp->mmap_len;
296 		munmap(decomp, mmap_len);
297 	} while (1);
298 }
299 
300 void perf_session__delete(struct perf_session *session)
301 {
302 	if (session == NULL)
303 		return;
304 	auxtrace__free(session);
305 	auxtrace_index__free(&session->auxtrace_index);
306 	perf_session__destroy_kernel_maps(session);
307 	perf_session__delete_threads(session);
308 	perf_decomp__release_events(session->decomp_data.decomp);
309 	perf_env__exit(&session->header.env);
310 	machines__exit(&session->machines);
311 	if (session->data) {
312 		if (perf_data__is_read(session->data))
313 			evlist__delete(session->evlist);
314 		perf_data__close(session->data);
315 	}
316 	trace_event__cleanup(&session->tevent);
317 	free(session);
318 }
319 
320 static int process_event_synth_tracing_data_stub(struct perf_session *session
321 						 __maybe_unused,
322 						 union perf_event *event
323 						 __maybe_unused)
324 {
325 	dump_printf(": unhandled!\n");
326 	return 0;
327 }
328 
329 static int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
330 					 union perf_event *event __maybe_unused,
331 					 struct evlist **pevlist
332 					 __maybe_unused)
333 {
334 	dump_printf(": unhandled!\n");
335 	return 0;
336 }
337 
338 static int process_event_synth_event_update_stub(struct perf_tool *tool __maybe_unused,
339 						 union perf_event *event __maybe_unused,
340 						 struct evlist **pevlist
341 						 __maybe_unused)
342 {
343 	if (dump_trace)
344 		perf_event__fprintf_event_update(event, stdout);
345 
346 	dump_printf(": unhandled!\n");
347 	return 0;
348 }
349 
350 static int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
351 				     union perf_event *event __maybe_unused,
352 				     struct perf_sample *sample __maybe_unused,
353 				     struct evsel *evsel __maybe_unused,
354 				     struct machine *machine __maybe_unused)
355 {
356 	dump_printf(": unhandled!\n");
357 	return 0;
358 }
359 
360 static int process_event_stub(struct perf_tool *tool __maybe_unused,
361 			      union perf_event *event __maybe_unused,
362 			      struct perf_sample *sample __maybe_unused,
363 			      struct machine *machine __maybe_unused)
364 {
365 	dump_printf(": unhandled!\n");
366 	return 0;
367 }
368 
369 static int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
370 				       union perf_event *event __maybe_unused,
371 				       struct ordered_events *oe __maybe_unused)
372 {
373 	dump_printf(": unhandled!\n");
374 	return 0;
375 }
376 
377 static int skipn(int fd, off_t n)
378 {
379 	char buf[4096];
380 	ssize_t ret;
381 
382 	while (n > 0) {
383 		ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
384 		if (ret <= 0)
385 			return ret;
386 		n -= ret;
387 	}
388 
389 	return 0;
390 }
391 
392 static s64 process_event_auxtrace_stub(struct perf_session *session __maybe_unused,
393 				       union perf_event *event)
394 {
395 	dump_printf(": unhandled!\n");
396 	if (perf_data__is_pipe(session->data))
397 		skipn(perf_data__fd(session->data), event->auxtrace.size);
398 	return event->auxtrace.size;
399 }
400 
401 static int process_event_op2_stub(struct perf_session *session __maybe_unused,
402 				  union perf_event *event __maybe_unused)
403 {
404 	dump_printf(": unhandled!\n");
405 	return 0;
406 }
407 
408 
409 static
410 int process_event_thread_map_stub(struct perf_session *session __maybe_unused,
411 				  union perf_event *event __maybe_unused)
412 {
413 	if (dump_trace)
414 		perf_event__fprintf_thread_map(event, stdout);
415 
416 	dump_printf(": unhandled!\n");
417 	return 0;
418 }
419 
420 static
421 int process_event_cpu_map_stub(struct perf_session *session __maybe_unused,
422 			       union perf_event *event __maybe_unused)
423 {
424 	if (dump_trace)
425 		perf_event__fprintf_cpu_map(event, stdout);
426 
427 	dump_printf(": unhandled!\n");
428 	return 0;
429 }
430 
431 static
432 int process_event_stat_config_stub(struct perf_session *session __maybe_unused,
433 				   union perf_event *event __maybe_unused)
434 {
435 	if (dump_trace)
436 		perf_event__fprintf_stat_config(event, stdout);
437 
438 	dump_printf(": unhandled!\n");
439 	return 0;
440 }
441 
442 static int process_stat_stub(struct perf_session *perf_session __maybe_unused,
443 			     union perf_event *event)
444 {
445 	if (dump_trace)
446 		perf_event__fprintf_stat(event, stdout);
447 
448 	dump_printf(": unhandled!\n");
449 	return 0;
450 }
451 
452 static int process_stat_round_stub(struct perf_session *perf_session __maybe_unused,
453 				   union perf_event *event)
454 {
455 	if (dump_trace)
456 		perf_event__fprintf_stat_round(event, stdout);
457 
458 	dump_printf(": unhandled!\n");
459 	return 0;
460 }
461 
462 static int process_event_time_conv_stub(struct perf_session *perf_session __maybe_unused,
463 					union perf_event *event)
464 {
465 	if (dump_trace)
466 		perf_event__fprintf_time_conv(event, stdout);
467 
468 	dump_printf(": unhandled!\n");
469 	return 0;
470 }
471 
472 static int perf_session__process_compressed_event_stub(struct perf_session *session __maybe_unused,
473 						       union perf_event *event __maybe_unused,
474 						       u64 file_offset __maybe_unused,
475 						       const char *file_path __maybe_unused)
476 {
477        dump_printf(": unhandled!\n");
478        return 0;
479 }
480 
481 void perf_tool__fill_defaults(struct perf_tool *tool)
482 {
483 	if (tool->sample == NULL)
484 		tool->sample = process_event_sample_stub;
485 	if (tool->mmap == NULL)
486 		tool->mmap = process_event_stub;
487 	if (tool->mmap2 == NULL)
488 		tool->mmap2 = process_event_stub;
489 	if (tool->comm == NULL)
490 		tool->comm = process_event_stub;
491 	if (tool->namespaces == NULL)
492 		tool->namespaces = process_event_stub;
493 	if (tool->cgroup == NULL)
494 		tool->cgroup = process_event_stub;
495 	if (tool->fork == NULL)
496 		tool->fork = process_event_stub;
497 	if (tool->exit == NULL)
498 		tool->exit = process_event_stub;
499 	if (tool->lost == NULL)
500 		tool->lost = perf_event__process_lost;
501 	if (tool->lost_samples == NULL)
502 		tool->lost_samples = perf_event__process_lost_samples;
503 	if (tool->aux == NULL)
504 		tool->aux = perf_event__process_aux;
505 	if (tool->itrace_start == NULL)
506 		tool->itrace_start = perf_event__process_itrace_start;
507 	if (tool->context_switch == NULL)
508 		tool->context_switch = perf_event__process_switch;
509 	if (tool->ksymbol == NULL)
510 		tool->ksymbol = perf_event__process_ksymbol;
511 	if (tool->bpf == NULL)
512 		tool->bpf = perf_event__process_bpf;
513 	if (tool->text_poke == NULL)
514 		tool->text_poke = perf_event__process_text_poke;
515 	if (tool->aux_output_hw_id == NULL)
516 		tool->aux_output_hw_id = perf_event__process_aux_output_hw_id;
517 	if (tool->read == NULL)
518 		tool->read = process_event_sample_stub;
519 	if (tool->throttle == NULL)
520 		tool->throttle = process_event_stub;
521 	if (tool->unthrottle == NULL)
522 		tool->unthrottle = process_event_stub;
523 	if (tool->attr == NULL)
524 		tool->attr = process_event_synth_attr_stub;
525 	if (tool->event_update == NULL)
526 		tool->event_update = process_event_synth_event_update_stub;
527 	if (tool->tracing_data == NULL)
528 		tool->tracing_data = process_event_synth_tracing_data_stub;
529 	if (tool->build_id == NULL)
530 		tool->build_id = process_event_op2_stub;
531 	if (tool->finished_round == NULL) {
532 		if (tool->ordered_events)
533 			tool->finished_round = perf_event__process_finished_round;
534 		else
535 			tool->finished_round = process_finished_round_stub;
536 	}
537 	if (tool->id_index == NULL)
538 		tool->id_index = process_event_op2_stub;
539 	if (tool->auxtrace_info == NULL)
540 		tool->auxtrace_info = process_event_op2_stub;
541 	if (tool->auxtrace == NULL)
542 		tool->auxtrace = process_event_auxtrace_stub;
543 	if (tool->auxtrace_error == NULL)
544 		tool->auxtrace_error = process_event_op2_stub;
545 	if (tool->thread_map == NULL)
546 		tool->thread_map = process_event_thread_map_stub;
547 	if (tool->cpu_map == NULL)
548 		tool->cpu_map = process_event_cpu_map_stub;
549 	if (tool->stat_config == NULL)
550 		tool->stat_config = process_event_stat_config_stub;
551 	if (tool->stat == NULL)
552 		tool->stat = process_stat_stub;
553 	if (tool->stat_round == NULL)
554 		tool->stat_round = process_stat_round_stub;
555 	if (tool->time_conv == NULL)
556 		tool->time_conv = process_event_time_conv_stub;
557 	if (tool->feature == NULL)
558 		tool->feature = process_event_op2_stub;
559 	if (tool->compressed == NULL)
560 		tool->compressed = perf_session__process_compressed_event;
561 	if (tool->finished_init == NULL)
562 		tool->finished_init = process_event_op2_stub;
563 }
564 
565 static void swap_sample_id_all(union perf_event *event, void *data)
566 {
567 	void *end = (void *) event + event->header.size;
568 	int size = end - data;
569 
570 	BUG_ON(size % sizeof(u64));
571 	mem_bswap_64(data, size);
572 }
573 
574 static void perf_event__all64_swap(union perf_event *event,
575 				   bool sample_id_all __maybe_unused)
576 {
577 	struct perf_event_header *hdr = &event->header;
578 	mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
579 }
580 
581 static void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
582 {
583 	event->comm.pid = bswap_32(event->comm.pid);
584 	event->comm.tid = bswap_32(event->comm.tid);
585 
586 	if (sample_id_all) {
587 		void *data = &event->comm.comm;
588 
589 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
590 		swap_sample_id_all(event, data);
591 	}
592 }
593 
594 static void perf_event__mmap_swap(union perf_event *event,
595 				  bool sample_id_all)
596 {
597 	event->mmap.pid	  = bswap_32(event->mmap.pid);
598 	event->mmap.tid	  = bswap_32(event->mmap.tid);
599 	event->mmap.start = bswap_64(event->mmap.start);
600 	event->mmap.len	  = bswap_64(event->mmap.len);
601 	event->mmap.pgoff = bswap_64(event->mmap.pgoff);
602 
603 	if (sample_id_all) {
604 		void *data = &event->mmap.filename;
605 
606 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
607 		swap_sample_id_all(event, data);
608 	}
609 }
610 
611 static void perf_event__mmap2_swap(union perf_event *event,
612 				  bool sample_id_all)
613 {
614 	event->mmap2.pid   = bswap_32(event->mmap2.pid);
615 	event->mmap2.tid   = bswap_32(event->mmap2.tid);
616 	event->mmap2.start = bswap_64(event->mmap2.start);
617 	event->mmap2.len   = bswap_64(event->mmap2.len);
618 	event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
619 
620 	if (!(event->header.misc & PERF_RECORD_MISC_MMAP_BUILD_ID)) {
621 		event->mmap2.maj   = bswap_32(event->mmap2.maj);
622 		event->mmap2.min   = bswap_32(event->mmap2.min);
623 		event->mmap2.ino   = bswap_64(event->mmap2.ino);
624 		event->mmap2.ino_generation = bswap_64(event->mmap2.ino_generation);
625 	}
626 
627 	if (sample_id_all) {
628 		void *data = &event->mmap2.filename;
629 
630 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
631 		swap_sample_id_all(event, data);
632 	}
633 }
634 static void perf_event__task_swap(union perf_event *event, bool sample_id_all)
635 {
636 	event->fork.pid	 = bswap_32(event->fork.pid);
637 	event->fork.tid	 = bswap_32(event->fork.tid);
638 	event->fork.ppid = bswap_32(event->fork.ppid);
639 	event->fork.ptid = bswap_32(event->fork.ptid);
640 	event->fork.time = bswap_64(event->fork.time);
641 
642 	if (sample_id_all)
643 		swap_sample_id_all(event, &event->fork + 1);
644 }
645 
646 static void perf_event__read_swap(union perf_event *event, bool sample_id_all)
647 {
648 	event->read.pid		 = bswap_32(event->read.pid);
649 	event->read.tid		 = bswap_32(event->read.tid);
650 	event->read.value	 = bswap_64(event->read.value);
651 	event->read.time_enabled = bswap_64(event->read.time_enabled);
652 	event->read.time_running = bswap_64(event->read.time_running);
653 	event->read.id		 = bswap_64(event->read.id);
654 
655 	if (sample_id_all)
656 		swap_sample_id_all(event, &event->read + 1);
657 }
658 
659 static void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
660 {
661 	event->aux.aux_offset = bswap_64(event->aux.aux_offset);
662 	event->aux.aux_size   = bswap_64(event->aux.aux_size);
663 	event->aux.flags      = bswap_64(event->aux.flags);
664 
665 	if (sample_id_all)
666 		swap_sample_id_all(event, &event->aux + 1);
667 }
668 
669 static void perf_event__itrace_start_swap(union perf_event *event,
670 					  bool sample_id_all)
671 {
672 	event->itrace_start.pid	 = bswap_32(event->itrace_start.pid);
673 	event->itrace_start.tid	 = bswap_32(event->itrace_start.tid);
674 
675 	if (sample_id_all)
676 		swap_sample_id_all(event, &event->itrace_start + 1);
677 }
678 
679 static void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
680 {
681 	if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
682 		event->context_switch.next_prev_pid =
683 				bswap_32(event->context_switch.next_prev_pid);
684 		event->context_switch.next_prev_tid =
685 				bswap_32(event->context_switch.next_prev_tid);
686 	}
687 
688 	if (sample_id_all)
689 		swap_sample_id_all(event, &event->context_switch + 1);
690 }
691 
692 static void perf_event__text_poke_swap(union perf_event *event, bool sample_id_all)
693 {
694 	event->text_poke.addr    = bswap_64(event->text_poke.addr);
695 	event->text_poke.old_len = bswap_16(event->text_poke.old_len);
696 	event->text_poke.new_len = bswap_16(event->text_poke.new_len);
697 
698 	if (sample_id_all) {
699 		size_t len = sizeof(event->text_poke.old_len) +
700 			     sizeof(event->text_poke.new_len) +
701 			     event->text_poke.old_len +
702 			     event->text_poke.new_len;
703 		void *data = &event->text_poke.old_len;
704 
705 		data += PERF_ALIGN(len, sizeof(u64));
706 		swap_sample_id_all(event, data);
707 	}
708 }
709 
710 static void perf_event__throttle_swap(union perf_event *event,
711 				      bool sample_id_all)
712 {
713 	event->throttle.time	  = bswap_64(event->throttle.time);
714 	event->throttle.id	  = bswap_64(event->throttle.id);
715 	event->throttle.stream_id = bswap_64(event->throttle.stream_id);
716 
717 	if (sample_id_all)
718 		swap_sample_id_all(event, &event->throttle + 1);
719 }
720 
721 static void perf_event__namespaces_swap(union perf_event *event,
722 					bool sample_id_all)
723 {
724 	u64 i;
725 
726 	event->namespaces.pid		= bswap_32(event->namespaces.pid);
727 	event->namespaces.tid		= bswap_32(event->namespaces.tid);
728 	event->namespaces.nr_namespaces	= bswap_64(event->namespaces.nr_namespaces);
729 
730 	for (i = 0; i < event->namespaces.nr_namespaces; i++) {
731 		struct perf_ns_link_info *ns = &event->namespaces.link_info[i];
732 
733 		ns->dev = bswap_64(ns->dev);
734 		ns->ino = bswap_64(ns->ino);
735 	}
736 
737 	if (sample_id_all)
738 		swap_sample_id_all(event, &event->namespaces.link_info[i]);
739 }
740 
741 static void perf_event__cgroup_swap(union perf_event *event, bool sample_id_all)
742 {
743 	event->cgroup.id = bswap_64(event->cgroup.id);
744 
745 	if (sample_id_all) {
746 		void *data = &event->cgroup.path;
747 
748 		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
749 		swap_sample_id_all(event, data);
750 	}
751 }
752 
753 static u8 revbyte(u8 b)
754 {
755 	int rev = (b >> 4) | ((b & 0xf) << 4);
756 	rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
757 	rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
758 	return (u8) rev;
759 }
760 
761 /*
762  * XXX this is hack in attempt to carry flags bitfield
763  * through endian village. ABI says:
764  *
765  * Bit-fields are allocated from right to left (least to most significant)
766  * on little-endian implementations and from left to right (most to least
767  * significant) on big-endian implementations.
768  *
769  * The above seems to be byte specific, so we need to reverse each
770  * byte of the bitfield. 'Internet' also says this might be implementation
771  * specific and we probably need proper fix and carry perf_event_attr
772  * bitfield flags in separate data file FEAT_ section. Thought this seems
773  * to work for now.
774  */
775 static void swap_bitfield(u8 *p, unsigned len)
776 {
777 	unsigned i;
778 
779 	for (i = 0; i < len; i++) {
780 		*p = revbyte(*p);
781 		p++;
782 	}
783 }
784 
785 /* exported for swapping attributes in file header */
786 void perf_event__attr_swap(struct perf_event_attr *attr)
787 {
788 	attr->type		= bswap_32(attr->type);
789 	attr->size		= bswap_32(attr->size);
790 
791 #define bswap_safe(f, n) 					\
792 	(attr->size > (offsetof(struct perf_event_attr, f) + 	\
793 		       sizeof(attr->f) * (n)))
794 #define bswap_field(f, sz) 			\
795 do { 						\
796 	if (bswap_safe(f, 0))			\
797 		attr->f = bswap_##sz(attr->f);	\
798 } while(0)
799 #define bswap_field_16(f) bswap_field(f, 16)
800 #define bswap_field_32(f) bswap_field(f, 32)
801 #define bswap_field_64(f) bswap_field(f, 64)
802 
803 	bswap_field_64(config);
804 	bswap_field_64(sample_period);
805 	bswap_field_64(sample_type);
806 	bswap_field_64(read_format);
807 	bswap_field_32(wakeup_events);
808 	bswap_field_32(bp_type);
809 	bswap_field_64(bp_addr);
810 	bswap_field_64(bp_len);
811 	bswap_field_64(branch_sample_type);
812 	bswap_field_64(sample_regs_user);
813 	bswap_field_32(sample_stack_user);
814 	bswap_field_32(aux_watermark);
815 	bswap_field_16(sample_max_stack);
816 	bswap_field_32(aux_sample_size);
817 
818 	/*
819 	 * After read_format are bitfields. Check read_format because
820 	 * we are unable to use offsetof on bitfield.
821 	 */
822 	if (bswap_safe(read_format, 1))
823 		swap_bitfield((u8 *) (&attr->read_format + 1),
824 			      sizeof(u64));
825 #undef bswap_field_64
826 #undef bswap_field_32
827 #undef bswap_field
828 #undef bswap_safe
829 }
830 
831 static void perf_event__hdr_attr_swap(union perf_event *event,
832 				      bool sample_id_all __maybe_unused)
833 {
834 	size_t size;
835 
836 	perf_event__attr_swap(&event->attr.attr);
837 
838 	size = event->header.size;
839 	size -= (void *)&event->attr.id - (void *)event;
840 	mem_bswap_64(event->attr.id, size);
841 }
842 
843 static void perf_event__event_update_swap(union perf_event *event,
844 					  bool sample_id_all __maybe_unused)
845 {
846 	event->event_update.type = bswap_64(event->event_update.type);
847 	event->event_update.id   = bswap_64(event->event_update.id);
848 }
849 
850 static void perf_event__event_type_swap(union perf_event *event,
851 					bool sample_id_all __maybe_unused)
852 {
853 	event->event_type.event_type.event_id =
854 		bswap_64(event->event_type.event_type.event_id);
855 }
856 
857 static void perf_event__tracing_data_swap(union perf_event *event,
858 					  bool sample_id_all __maybe_unused)
859 {
860 	event->tracing_data.size = bswap_32(event->tracing_data.size);
861 }
862 
863 static void perf_event__auxtrace_info_swap(union perf_event *event,
864 					   bool sample_id_all __maybe_unused)
865 {
866 	size_t size;
867 
868 	event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
869 
870 	size = event->header.size;
871 	size -= (void *)&event->auxtrace_info.priv - (void *)event;
872 	mem_bswap_64(event->auxtrace_info.priv, size);
873 }
874 
875 static void perf_event__auxtrace_swap(union perf_event *event,
876 				      bool sample_id_all __maybe_unused)
877 {
878 	event->auxtrace.size      = bswap_64(event->auxtrace.size);
879 	event->auxtrace.offset    = bswap_64(event->auxtrace.offset);
880 	event->auxtrace.reference = bswap_64(event->auxtrace.reference);
881 	event->auxtrace.idx       = bswap_32(event->auxtrace.idx);
882 	event->auxtrace.tid       = bswap_32(event->auxtrace.tid);
883 	event->auxtrace.cpu       = bswap_32(event->auxtrace.cpu);
884 }
885 
886 static void perf_event__auxtrace_error_swap(union perf_event *event,
887 					    bool sample_id_all __maybe_unused)
888 {
889 	event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
890 	event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
891 	event->auxtrace_error.cpu  = bswap_32(event->auxtrace_error.cpu);
892 	event->auxtrace_error.pid  = bswap_32(event->auxtrace_error.pid);
893 	event->auxtrace_error.tid  = bswap_32(event->auxtrace_error.tid);
894 	event->auxtrace_error.fmt  = bswap_32(event->auxtrace_error.fmt);
895 	event->auxtrace_error.ip   = bswap_64(event->auxtrace_error.ip);
896 	if (event->auxtrace_error.fmt)
897 		event->auxtrace_error.time = bswap_64(event->auxtrace_error.time);
898 	if (event->auxtrace_error.fmt >= 2) {
899 		event->auxtrace_error.machine_pid = bswap_32(event->auxtrace_error.machine_pid);
900 		event->auxtrace_error.vcpu = bswap_32(event->auxtrace_error.vcpu);
901 	}
902 }
903 
904 static void perf_event__thread_map_swap(union perf_event *event,
905 					bool sample_id_all __maybe_unused)
906 {
907 	unsigned i;
908 
909 	event->thread_map.nr = bswap_64(event->thread_map.nr);
910 
911 	for (i = 0; i < event->thread_map.nr; i++)
912 		event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid);
913 }
914 
915 static void perf_event__cpu_map_swap(union perf_event *event,
916 				     bool sample_id_all __maybe_unused)
917 {
918 	struct perf_record_cpu_map_data *data = &event->cpu_map.data;
919 
920 	data->type = bswap_16(data->type);
921 
922 	switch (data->type) {
923 	case PERF_CPU_MAP__CPUS:
924 		data->cpus_data.nr = bswap_16(data->cpus_data.nr);
925 
926 		for (unsigned i = 0; i < data->cpus_data.nr; i++)
927 			data->cpus_data.cpu[i] = bswap_16(data->cpus_data.cpu[i]);
928 		break;
929 	case PERF_CPU_MAP__MASK:
930 		data->mask32_data.long_size = bswap_16(data->mask32_data.long_size);
931 
932 		switch (data->mask32_data.long_size) {
933 		case 4:
934 			data->mask32_data.nr = bswap_16(data->mask32_data.nr);
935 			for (unsigned i = 0; i < data->mask32_data.nr; i++)
936 				data->mask32_data.mask[i] = bswap_32(data->mask32_data.mask[i]);
937 			break;
938 		case 8:
939 			data->mask64_data.nr = bswap_16(data->mask64_data.nr);
940 			for (unsigned i = 0; i < data->mask64_data.nr; i++)
941 				data->mask64_data.mask[i] = bswap_64(data->mask64_data.mask[i]);
942 			break;
943 		default:
944 			pr_err("cpu_map swap: unsupported long size\n");
945 		}
946 	default:
947 		break;
948 	}
949 }
950 
951 static void perf_event__stat_config_swap(union perf_event *event,
952 					 bool sample_id_all __maybe_unused)
953 {
954 	u64 size;
955 
956 	size  = bswap_64(event->stat_config.nr) * sizeof(event->stat_config.data[0]);
957 	size += 1; /* nr item itself */
958 	mem_bswap_64(&event->stat_config.nr, size);
959 }
960 
961 static void perf_event__stat_swap(union perf_event *event,
962 				  bool sample_id_all __maybe_unused)
963 {
964 	event->stat.id     = bswap_64(event->stat.id);
965 	event->stat.thread = bswap_32(event->stat.thread);
966 	event->stat.cpu    = bswap_32(event->stat.cpu);
967 	event->stat.val    = bswap_64(event->stat.val);
968 	event->stat.ena    = bswap_64(event->stat.ena);
969 	event->stat.run    = bswap_64(event->stat.run);
970 }
971 
972 static void perf_event__stat_round_swap(union perf_event *event,
973 					bool sample_id_all __maybe_unused)
974 {
975 	event->stat_round.type = bswap_64(event->stat_round.type);
976 	event->stat_round.time = bswap_64(event->stat_round.time);
977 }
978 
979 static void perf_event__time_conv_swap(union perf_event *event,
980 				       bool sample_id_all __maybe_unused)
981 {
982 	event->time_conv.time_shift = bswap_64(event->time_conv.time_shift);
983 	event->time_conv.time_mult  = bswap_64(event->time_conv.time_mult);
984 	event->time_conv.time_zero  = bswap_64(event->time_conv.time_zero);
985 
986 	if (event_contains(event->time_conv, time_cycles)) {
987 		event->time_conv.time_cycles = bswap_64(event->time_conv.time_cycles);
988 		event->time_conv.time_mask = bswap_64(event->time_conv.time_mask);
989 	}
990 }
991 
992 typedef void (*perf_event__swap_op)(union perf_event *event,
993 				    bool sample_id_all);
994 
995 static perf_event__swap_op perf_event__swap_ops[] = {
996 	[PERF_RECORD_MMAP]		  = perf_event__mmap_swap,
997 	[PERF_RECORD_MMAP2]		  = perf_event__mmap2_swap,
998 	[PERF_RECORD_COMM]		  = perf_event__comm_swap,
999 	[PERF_RECORD_FORK]		  = perf_event__task_swap,
1000 	[PERF_RECORD_EXIT]		  = perf_event__task_swap,
1001 	[PERF_RECORD_LOST]		  = perf_event__all64_swap,
1002 	[PERF_RECORD_READ]		  = perf_event__read_swap,
1003 	[PERF_RECORD_THROTTLE]		  = perf_event__throttle_swap,
1004 	[PERF_RECORD_UNTHROTTLE]	  = perf_event__throttle_swap,
1005 	[PERF_RECORD_SAMPLE]		  = perf_event__all64_swap,
1006 	[PERF_RECORD_AUX]		  = perf_event__aux_swap,
1007 	[PERF_RECORD_ITRACE_START]	  = perf_event__itrace_start_swap,
1008 	[PERF_RECORD_LOST_SAMPLES]	  = perf_event__all64_swap,
1009 	[PERF_RECORD_SWITCH]		  = perf_event__switch_swap,
1010 	[PERF_RECORD_SWITCH_CPU_WIDE]	  = perf_event__switch_swap,
1011 	[PERF_RECORD_NAMESPACES]	  = perf_event__namespaces_swap,
1012 	[PERF_RECORD_CGROUP]		  = perf_event__cgroup_swap,
1013 	[PERF_RECORD_TEXT_POKE]		  = perf_event__text_poke_swap,
1014 	[PERF_RECORD_AUX_OUTPUT_HW_ID]	  = perf_event__all64_swap,
1015 	[PERF_RECORD_HEADER_ATTR]	  = perf_event__hdr_attr_swap,
1016 	[PERF_RECORD_HEADER_EVENT_TYPE]	  = perf_event__event_type_swap,
1017 	[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
1018 	[PERF_RECORD_HEADER_BUILD_ID]	  = NULL,
1019 	[PERF_RECORD_ID_INDEX]		  = perf_event__all64_swap,
1020 	[PERF_RECORD_AUXTRACE_INFO]	  = perf_event__auxtrace_info_swap,
1021 	[PERF_RECORD_AUXTRACE]		  = perf_event__auxtrace_swap,
1022 	[PERF_RECORD_AUXTRACE_ERROR]	  = perf_event__auxtrace_error_swap,
1023 	[PERF_RECORD_THREAD_MAP]	  = perf_event__thread_map_swap,
1024 	[PERF_RECORD_CPU_MAP]		  = perf_event__cpu_map_swap,
1025 	[PERF_RECORD_STAT_CONFIG]	  = perf_event__stat_config_swap,
1026 	[PERF_RECORD_STAT]		  = perf_event__stat_swap,
1027 	[PERF_RECORD_STAT_ROUND]	  = perf_event__stat_round_swap,
1028 	[PERF_RECORD_EVENT_UPDATE]	  = perf_event__event_update_swap,
1029 	[PERF_RECORD_TIME_CONV]		  = perf_event__time_conv_swap,
1030 	[PERF_RECORD_HEADER_MAX]	  = NULL,
1031 };
1032 
1033 /*
1034  * When perf record finishes a pass on every buffers, it records this pseudo
1035  * event.
1036  * We record the max timestamp t found in the pass n.
1037  * Assuming these timestamps are monotonic across cpus, we know that if
1038  * a buffer still has events with timestamps below t, they will be all
1039  * available and then read in the pass n + 1.
1040  * Hence when we start to read the pass n + 2, we can safely flush every
1041  * events with timestamps below t.
1042  *
1043  *    ============ PASS n =================
1044  *       CPU 0         |   CPU 1
1045  *                     |
1046  *    cnt1 timestamps  |   cnt2 timestamps
1047  *          1          |         2
1048  *          2          |         3
1049  *          -          |         4  <--- max recorded
1050  *
1051  *    ============ PASS n + 1 ==============
1052  *       CPU 0         |   CPU 1
1053  *                     |
1054  *    cnt1 timestamps  |   cnt2 timestamps
1055  *          3          |         5
1056  *          4          |         6
1057  *          5          |         7 <---- max recorded
1058  *
1059  *      Flush every events below timestamp 4
1060  *
1061  *    ============ PASS n + 2 ==============
1062  *       CPU 0         |   CPU 1
1063  *                     |
1064  *    cnt1 timestamps  |   cnt2 timestamps
1065  *          6          |         8
1066  *          7          |         9
1067  *          -          |         10
1068  *
1069  *      Flush every events below timestamp 7
1070  *      etc...
1071  */
1072 int perf_event__process_finished_round(struct perf_tool *tool __maybe_unused,
1073 				       union perf_event *event __maybe_unused,
1074 				       struct ordered_events *oe)
1075 {
1076 	if (dump_trace)
1077 		fprintf(stdout, "\n");
1078 	return ordered_events__flush(oe, OE_FLUSH__ROUND);
1079 }
1080 
1081 int perf_session__queue_event(struct perf_session *s, union perf_event *event,
1082 			      u64 timestamp, u64 file_offset, const char *file_path)
1083 {
1084 	return ordered_events__queue(&s->ordered_events, event, timestamp, file_offset, file_path);
1085 }
1086 
1087 static void callchain__lbr_callstack_printf(struct perf_sample *sample)
1088 {
1089 	struct ip_callchain *callchain = sample->callchain;
1090 	struct branch_stack *lbr_stack = sample->branch_stack;
1091 	struct branch_entry *entries = perf_sample__branch_entries(sample);
1092 	u64 kernel_callchain_nr = callchain->nr;
1093 	unsigned int i;
1094 
1095 	for (i = 0; i < kernel_callchain_nr; i++) {
1096 		if (callchain->ips[i] == PERF_CONTEXT_USER)
1097 			break;
1098 	}
1099 
1100 	if ((i != kernel_callchain_nr) && lbr_stack->nr) {
1101 		u64 total_nr;
1102 		/*
1103 		 * LBR callstack can only get user call chain,
1104 		 * i is kernel call chain number,
1105 		 * 1 is PERF_CONTEXT_USER.
1106 		 *
1107 		 * The user call chain is stored in LBR registers.
1108 		 * LBR are pair registers. The caller is stored
1109 		 * in "from" register, while the callee is stored
1110 		 * in "to" register.
1111 		 * For example, there is a call stack
1112 		 * "A"->"B"->"C"->"D".
1113 		 * The LBR registers will be recorded like
1114 		 * "C"->"D", "B"->"C", "A"->"B".
1115 		 * So only the first "to" register and all "from"
1116 		 * registers are needed to construct the whole stack.
1117 		 */
1118 		total_nr = i + 1 + lbr_stack->nr + 1;
1119 		kernel_callchain_nr = i + 1;
1120 
1121 		printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
1122 
1123 		for (i = 0; i < kernel_callchain_nr; i++)
1124 			printf("..... %2d: %016" PRIx64 "\n",
1125 			       i, callchain->ips[i]);
1126 
1127 		printf("..... %2d: %016" PRIx64 "\n",
1128 		       (int)(kernel_callchain_nr), entries[0].to);
1129 		for (i = 0; i < lbr_stack->nr; i++)
1130 			printf("..... %2d: %016" PRIx64 "\n",
1131 			       (int)(i + kernel_callchain_nr + 1), entries[i].from);
1132 	}
1133 }
1134 
1135 static void callchain__printf(struct evsel *evsel,
1136 			      struct perf_sample *sample)
1137 {
1138 	unsigned int i;
1139 	struct ip_callchain *callchain = sample->callchain;
1140 
1141 	if (evsel__has_branch_callstack(evsel))
1142 		callchain__lbr_callstack_printf(sample);
1143 
1144 	printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
1145 
1146 	for (i = 0; i < callchain->nr; i++)
1147 		printf("..... %2d: %016" PRIx64 "\n",
1148 		       i, callchain->ips[i]);
1149 }
1150 
1151 static void branch_stack__printf(struct perf_sample *sample, bool callstack)
1152 {
1153 	struct branch_entry *entries = perf_sample__branch_entries(sample);
1154 	uint64_t i;
1155 
1156 	if (!callstack) {
1157 		printf("%s: nr:%" PRIu64 "\n", "... branch stack", sample->branch_stack->nr);
1158 	} else {
1159 		/* the reason of adding 1 to nr is because after expanding
1160 		 * branch stack it generates nr + 1 callstack records. e.g.,
1161 		 *         B()->C()
1162 		 *         A()->B()
1163 		 * the final callstack should be:
1164 		 *         C()
1165 		 *         B()
1166 		 *         A()
1167 		 */
1168 		printf("%s: nr:%" PRIu64 "\n", "... branch callstack", sample->branch_stack->nr+1);
1169 	}
1170 
1171 	for (i = 0; i < sample->branch_stack->nr; i++) {
1172 		struct branch_entry *e = &entries[i];
1173 
1174 		if (!callstack) {
1175 			printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x %s\n",
1176 				i, e->from, e->to,
1177 				(unsigned short)e->flags.cycles,
1178 				e->flags.mispred ? "M" : " ",
1179 				e->flags.predicted ? "P" : " ",
1180 				e->flags.abort ? "A" : " ",
1181 				e->flags.in_tx ? "T" : " ",
1182 				(unsigned)e->flags.reserved,
1183 				e->flags.type ? branch_type_name(e->flags.type) : "");
1184 		} else {
1185 			if (i == 0) {
1186 				printf("..... %2"PRIu64": %016" PRIx64 "\n"
1187 				       "..... %2"PRIu64": %016" PRIx64 "\n",
1188 						i, e->to, i+1, e->from);
1189 			} else {
1190 				printf("..... %2"PRIu64": %016" PRIx64 "\n", i+1, e->from);
1191 			}
1192 		}
1193 	}
1194 }
1195 
1196 static void regs_dump__printf(u64 mask, u64 *regs, const char *arch)
1197 {
1198 	unsigned rid, i = 0;
1199 
1200 	for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
1201 		u64 val = regs[i++];
1202 
1203 		printf(".... %-5s 0x%016" PRIx64 "\n",
1204 		       perf_reg_name(rid, arch), val);
1205 	}
1206 }
1207 
1208 static const char *regs_abi[] = {
1209 	[PERF_SAMPLE_REGS_ABI_NONE] = "none",
1210 	[PERF_SAMPLE_REGS_ABI_32] = "32-bit",
1211 	[PERF_SAMPLE_REGS_ABI_64] = "64-bit",
1212 };
1213 
1214 static inline const char *regs_dump_abi(struct regs_dump *d)
1215 {
1216 	if (d->abi > PERF_SAMPLE_REGS_ABI_64)
1217 		return "unknown";
1218 
1219 	return regs_abi[d->abi];
1220 }
1221 
1222 static void regs__printf(const char *type, struct regs_dump *regs, const char *arch)
1223 {
1224 	u64 mask = regs->mask;
1225 
1226 	printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
1227 	       type,
1228 	       mask,
1229 	       regs_dump_abi(regs));
1230 
1231 	regs_dump__printf(mask, regs->regs, arch);
1232 }
1233 
1234 static void regs_user__printf(struct perf_sample *sample, const char *arch)
1235 {
1236 	struct regs_dump *user_regs = &sample->user_regs;
1237 
1238 	if (user_regs->regs)
1239 		regs__printf("user", user_regs, arch);
1240 }
1241 
1242 static void regs_intr__printf(struct perf_sample *sample, const char *arch)
1243 {
1244 	struct regs_dump *intr_regs = &sample->intr_regs;
1245 
1246 	if (intr_regs->regs)
1247 		regs__printf("intr", intr_regs, arch);
1248 }
1249 
1250 static void stack_user__printf(struct stack_dump *dump)
1251 {
1252 	printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
1253 	       dump->size, dump->offset);
1254 }
1255 
1256 static void evlist__print_tstamp(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
1257 {
1258 	u64 sample_type = __evlist__combined_sample_type(evlist);
1259 
1260 	if (event->header.type != PERF_RECORD_SAMPLE &&
1261 	    !evlist__sample_id_all(evlist)) {
1262 		fputs("-1 -1 ", stdout);
1263 		return;
1264 	}
1265 
1266 	if ((sample_type & PERF_SAMPLE_CPU))
1267 		printf("%u ", sample->cpu);
1268 
1269 	if (sample_type & PERF_SAMPLE_TIME)
1270 		printf("%" PRIu64 " ", sample->time);
1271 }
1272 
1273 static void sample_read__printf(struct perf_sample *sample, u64 read_format)
1274 {
1275 	printf("... sample_read:\n");
1276 
1277 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1278 		printf("...... time enabled %016" PRIx64 "\n",
1279 		       sample->read.time_enabled);
1280 
1281 	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1282 		printf("...... time running %016" PRIx64 "\n",
1283 		       sample->read.time_running);
1284 
1285 	if (read_format & PERF_FORMAT_GROUP) {
1286 		struct sample_read_value *value = sample->read.group.values;
1287 
1288 		printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
1289 
1290 		sample_read_group__for_each(value, sample->read.group.nr, read_format) {
1291 			printf("..... id %016" PRIx64
1292 			       ", value %016" PRIx64,
1293 			       value->id, value->value);
1294 			if (read_format & PERF_FORMAT_LOST)
1295 				printf(", lost %" PRIu64, value->lost);
1296 			printf("\n");
1297 		}
1298 	} else {
1299 		printf("..... id %016" PRIx64 ", value %016" PRIx64,
1300 			sample->read.one.id, sample->read.one.value);
1301 		if (read_format & PERF_FORMAT_LOST)
1302 			printf(", lost %" PRIu64, sample->read.one.lost);
1303 		printf("\n");
1304 	}
1305 }
1306 
1307 static void dump_event(struct evlist *evlist, union perf_event *event,
1308 		       u64 file_offset, struct perf_sample *sample,
1309 		       const char *file_path)
1310 {
1311 	if (!dump_trace)
1312 		return;
1313 
1314 	printf("\n%#" PRIx64 "@%s [%#x]: event: %d\n",
1315 	       file_offset, file_path, event->header.size, event->header.type);
1316 
1317 	trace_event(event);
1318 	if (event->header.type == PERF_RECORD_SAMPLE && evlist->trace_event_sample_raw)
1319 		evlist->trace_event_sample_raw(evlist, event, sample);
1320 
1321 	if (sample)
1322 		evlist__print_tstamp(evlist, event, sample);
1323 
1324 	printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
1325 	       event->header.size, perf_event__name(event->header.type));
1326 }
1327 
1328 char *get_page_size_name(u64 size, char *str)
1329 {
1330 	if (!size || !unit_number__scnprintf(str, PAGE_SIZE_NAME_LEN, size))
1331 		snprintf(str, PAGE_SIZE_NAME_LEN, "%s", "N/A");
1332 
1333 	return str;
1334 }
1335 
1336 static void dump_sample(struct evsel *evsel, union perf_event *event,
1337 			struct perf_sample *sample, const char *arch)
1338 {
1339 	u64 sample_type;
1340 	char str[PAGE_SIZE_NAME_LEN];
1341 
1342 	if (!dump_trace)
1343 		return;
1344 
1345 	printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
1346 	       event->header.misc, sample->pid, sample->tid, sample->ip,
1347 	       sample->period, sample->addr);
1348 
1349 	sample_type = evsel->core.attr.sample_type;
1350 
1351 	if (evsel__has_callchain(evsel))
1352 		callchain__printf(evsel, sample);
1353 
1354 	if (evsel__has_br_stack(evsel))
1355 		branch_stack__printf(sample, evsel__has_branch_callstack(evsel));
1356 
1357 	if (sample_type & PERF_SAMPLE_REGS_USER)
1358 		regs_user__printf(sample, arch);
1359 
1360 	if (sample_type & PERF_SAMPLE_REGS_INTR)
1361 		regs_intr__printf(sample, arch);
1362 
1363 	if (sample_type & PERF_SAMPLE_STACK_USER)
1364 		stack_user__printf(&sample->user_stack);
1365 
1366 	if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
1367 		printf("... weight: %" PRIu64 "", sample->weight);
1368 			if (sample_type & PERF_SAMPLE_WEIGHT_STRUCT) {
1369 				printf(",0x%"PRIx16"", sample->ins_lat);
1370 				printf(",0x%"PRIx16"", sample->p_stage_cyc);
1371 			}
1372 		printf("\n");
1373 	}
1374 
1375 	if (sample_type & PERF_SAMPLE_DATA_SRC)
1376 		printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
1377 
1378 	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
1379 		printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr);
1380 
1381 	if (sample_type & PERF_SAMPLE_DATA_PAGE_SIZE)
1382 		printf(" .. data page size: %s\n", get_page_size_name(sample->data_page_size, str));
1383 
1384 	if (sample_type & PERF_SAMPLE_CODE_PAGE_SIZE)
1385 		printf(" .. code page size: %s\n", get_page_size_name(sample->code_page_size, str));
1386 
1387 	if (sample_type & PERF_SAMPLE_TRANSACTION)
1388 		printf("... transaction: %" PRIx64 "\n", sample->transaction);
1389 
1390 	if (sample_type & PERF_SAMPLE_READ)
1391 		sample_read__printf(sample, evsel->core.attr.read_format);
1392 }
1393 
1394 static void dump_read(struct evsel *evsel, union perf_event *event)
1395 {
1396 	struct perf_record_read *read_event = &event->read;
1397 	u64 read_format;
1398 
1399 	if (!dump_trace)
1400 		return;
1401 
1402 	printf(": %d %d %s %" PRI_lu64 "\n", event->read.pid, event->read.tid,
1403 	       evsel__name(evsel), event->read.value);
1404 
1405 	if (!evsel)
1406 		return;
1407 
1408 	read_format = evsel->core.attr.read_format;
1409 
1410 	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
1411 		printf("... time enabled : %" PRI_lu64 "\n", read_event->time_enabled);
1412 
1413 	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
1414 		printf("... time running : %" PRI_lu64 "\n", read_event->time_running);
1415 
1416 	if (read_format & PERF_FORMAT_ID)
1417 		printf("... id           : %" PRI_lu64 "\n", read_event->id);
1418 
1419 	if (read_format & PERF_FORMAT_LOST)
1420 		printf("... lost         : %" PRI_lu64 "\n", read_event->lost);
1421 }
1422 
1423 static struct machine *machines__find_for_cpumode(struct machines *machines,
1424 					       union perf_event *event,
1425 					       struct perf_sample *sample)
1426 {
1427 	if (perf_guest &&
1428 	    ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
1429 	     (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) {
1430 		u32 pid;
1431 
1432 		if (sample->machine_pid)
1433 			pid = sample->machine_pid;
1434 		else if (event->header.type == PERF_RECORD_MMAP
1435 		    || event->header.type == PERF_RECORD_MMAP2)
1436 			pid = event->mmap.pid;
1437 		else
1438 			pid = sample->pid;
1439 
1440 		/*
1441 		 * Guest code machine is created as needed and does not use
1442 		 * DEFAULT_GUEST_KERNEL_ID.
1443 		 */
1444 		if (symbol_conf.guest_code)
1445 			return machines__findnew(machines, pid);
1446 
1447 		return machines__find_guest(machines, pid);
1448 	}
1449 
1450 	return &machines->host;
1451 }
1452 
1453 static int deliver_sample_value(struct evlist *evlist,
1454 				struct perf_tool *tool,
1455 				union perf_event *event,
1456 				struct perf_sample *sample,
1457 				struct sample_read_value *v,
1458 				struct machine *machine)
1459 {
1460 	struct perf_sample_id *sid = evlist__id2sid(evlist, v->id);
1461 	struct evsel *evsel;
1462 
1463 	if (sid) {
1464 		sample->id     = v->id;
1465 		sample->period = v->value - sid->period;
1466 		sid->period    = v->value;
1467 	}
1468 
1469 	if (!sid || sid->evsel == NULL) {
1470 		++evlist->stats.nr_unknown_id;
1471 		return 0;
1472 	}
1473 
1474 	/*
1475 	 * There's no reason to deliver sample
1476 	 * for zero period, bail out.
1477 	 */
1478 	if (!sample->period)
1479 		return 0;
1480 
1481 	evsel = container_of(sid->evsel, struct evsel, core);
1482 	return tool->sample(tool, event, sample, evsel, machine);
1483 }
1484 
1485 static int deliver_sample_group(struct evlist *evlist,
1486 				struct perf_tool *tool,
1487 				union  perf_event *event,
1488 				struct perf_sample *sample,
1489 				struct machine *machine,
1490 				u64 read_format)
1491 {
1492 	int ret = -EINVAL;
1493 	struct sample_read_value *v = sample->read.group.values;
1494 
1495 	sample_read_group__for_each(v, sample->read.group.nr, read_format) {
1496 		ret = deliver_sample_value(evlist, tool, event, sample, v,
1497 					   machine);
1498 		if (ret)
1499 			break;
1500 	}
1501 
1502 	return ret;
1503 }
1504 
1505 static int evlist__deliver_sample(struct evlist *evlist, struct perf_tool *tool,
1506 				  union  perf_event *event, struct perf_sample *sample,
1507 				  struct evsel *evsel, struct machine *machine)
1508 {
1509 	/* We know evsel != NULL. */
1510 	u64 sample_type = evsel->core.attr.sample_type;
1511 	u64 read_format = evsel->core.attr.read_format;
1512 
1513 	/* Standard sample delivery. */
1514 	if (!(sample_type & PERF_SAMPLE_READ))
1515 		return tool->sample(tool, event, sample, evsel, machine);
1516 
1517 	/* For PERF_SAMPLE_READ we have either single or group mode. */
1518 	if (read_format & PERF_FORMAT_GROUP)
1519 		return deliver_sample_group(evlist, tool, event, sample,
1520 					    machine, read_format);
1521 	else
1522 		return deliver_sample_value(evlist, tool, event, sample,
1523 					    &sample->read.one, machine);
1524 }
1525 
1526 static int machines__deliver_event(struct machines *machines,
1527 				   struct evlist *evlist,
1528 				   union perf_event *event,
1529 				   struct perf_sample *sample,
1530 				   struct perf_tool *tool, u64 file_offset,
1531 				   const char *file_path)
1532 {
1533 	struct evsel *evsel;
1534 	struct machine *machine;
1535 
1536 	dump_event(evlist, event, file_offset, sample, file_path);
1537 
1538 	evsel = evlist__id2evsel(evlist, sample->id);
1539 
1540 	machine = machines__find_for_cpumode(machines, event, sample);
1541 
1542 	switch (event->header.type) {
1543 	case PERF_RECORD_SAMPLE:
1544 		if (evsel == NULL) {
1545 			++evlist->stats.nr_unknown_id;
1546 			return 0;
1547 		}
1548 		if (machine == NULL) {
1549 			++evlist->stats.nr_unprocessable_samples;
1550 			dump_sample(evsel, event, sample, perf_env__arch(NULL));
1551 			return 0;
1552 		}
1553 		dump_sample(evsel, event, sample, perf_env__arch(machine->env));
1554 		return evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
1555 	case PERF_RECORD_MMAP:
1556 		return tool->mmap(tool, event, sample, machine);
1557 	case PERF_RECORD_MMAP2:
1558 		if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
1559 			++evlist->stats.nr_proc_map_timeout;
1560 		return tool->mmap2(tool, event, sample, machine);
1561 	case PERF_RECORD_COMM:
1562 		return tool->comm(tool, event, sample, machine);
1563 	case PERF_RECORD_NAMESPACES:
1564 		return tool->namespaces(tool, event, sample, machine);
1565 	case PERF_RECORD_CGROUP:
1566 		return tool->cgroup(tool, event, sample, machine);
1567 	case PERF_RECORD_FORK:
1568 		return tool->fork(tool, event, sample, machine);
1569 	case PERF_RECORD_EXIT:
1570 		return tool->exit(tool, event, sample, machine);
1571 	case PERF_RECORD_LOST:
1572 		if (tool->lost == perf_event__process_lost)
1573 			evlist->stats.total_lost += event->lost.lost;
1574 		return tool->lost(tool, event, sample, machine);
1575 	case PERF_RECORD_LOST_SAMPLES:
1576 		if (tool->lost_samples == perf_event__process_lost_samples)
1577 			evlist->stats.total_lost_samples += event->lost_samples.lost;
1578 		return tool->lost_samples(tool, event, sample, machine);
1579 	case PERF_RECORD_READ:
1580 		dump_read(evsel, event);
1581 		return tool->read(tool, event, sample, evsel, machine);
1582 	case PERF_RECORD_THROTTLE:
1583 		return tool->throttle(tool, event, sample, machine);
1584 	case PERF_RECORD_UNTHROTTLE:
1585 		return tool->unthrottle(tool, event, sample, machine);
1586 	case PERF_RECORD_AUX:
1587 		if (tool->aux == perf_event__process_aux) {
1588 			if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
1589 				evlist->stats.total_aux_lost += 1;
1590 			if (event->aux.flags & PERF_AUX_FLAG_PARTIAL)
1591 				evlist->stats.total_aux_partial += 1;
1592 			if (event->aux.flags & PERF_AUX_FLAG_COLLISION)
1593 				evlist->stats.total_aux_collision += 1;
1594 		}
1595 		return tool->aux(tool, event, sample, machine);
1596 	case PERF_RECORD_ITRACE_START:
1597 		return tool->itrace_start(tool, event, sample, machine);
1598 	case PERF_RECORD_SWITCH:
1599 	case PERF_RECORD_SWITCH_CPU_WIDE:
1600 		return tool->context_switch(tool, event, sample, machine);
1601 	case PERF_RECORD_KSYMBOL:
1602 		return tool->ksymbol(tool, event, sample, machine);
1603 	case PERF_RECORD_BPF_EVENT:
1604 		return tool->bpf(tool, event, sample, machine);
1605 	case PERF_RECORD_TEXT_POKE:
1606 		return tool->text_poke(tool, event, sample, machine);
1607 	case PERF_RECORD_AUX_OUTPUT_HW_ID:
1608 		return tool->aux_output_hw_id(tool, event, sample, machine);
1609 	default:
1610 		++evlist->stats.nr_unknown_events;
1611 		return -1;
1612 	}
1613 }
1614 
1615 static int perf_session__deliver_event(struct perf_session *session,
1616 				       union perf_event *event,
1617 				       struct perf_tool *tool,
1618 				       u64 file_offset,
1619 				       const char *file_path)
1620 {
1621 	struct perf_sample sample;
1622 	int ret = evlist__parse_sample(session->evlist, event, &sample);
1623 
1624 	if (ret) {
1625 		pr_err("Can't parse sample, err = %d\n", ret);
1626 		return ret;
1627 	}
1628 
1629 	ret = auxtrace__process_event(session, event, &sample, tool);
1630 	if (ret < 0)
1631 		return ret;
1632 	if (ret > 0)
1633 		return 0;
1634 
1635 	ret = machines__deliver_event(&session->machines, session->evlist,
1636 				      event, &sample, tool, file_offset, file_path);
1637 
1638 	if (dump_trace && sample.aux_sample.size)
1639 		auxtrace__dump_auxtrace_sample(session, &sample);
1640 
1641 	return ret;
1642 }
1643 
1644 static s64 perf_session__process_user_event(struct perf_session *session,
1645 					    union perf_event *event,
1646 					    u64 file_offset,
1647 					    const char *file_path)
1648 {
1649 	struct ordered_events *oe = &session->ordered_events;
1650 	struct perf_tool *tool = session->tool;
1651 	struct perf_sample sample = { .time = 0, };
1652 	int fd = perf_data__fd(session->data);
1653 	int err;
1654 
1655 	if (event->header.type != PERF_RECORD_COMPRESSED ||
1656 	    tool->compressed == perf_session__process_compressed_event_stub)
1657 		dump_event(session->evlist, event, file_offset, &sample, file_path);
1658 
1659 	/* These events are processed right away */
1660 	switch (event->header.type) {
1661 	case PERF_RECORD_HEADER_ATTR:
1662 		err = tool->attr(tool, event, &session->evlist);
1663 		if (err == 0) {
1664 			perf_session__set_id_hdr_size(session);
1665 			perf_session__set_comm_exec(session);
1666 		}
1667 		return err;
1668 	case PERF_RECORD_EVENT_UPDATE:
1669 		return tool->event_update(tool, event, &session->evlist);
1670 	case PERF_RECORD_HEADER_EVENT_TYPE:
1671 		/*
1672 		 * Deprecated, but we need to handle it for sake
1673 		 * of old data files create in pipe mode.
1674 		 */
1675 		return 0;
1676 	case PERF_RECORD_HEADER_TRACING_DATA:
1677 		/*
1678 		 * Setup for reading amidst mmap, but only when we
1679 		 * are in 'file' mode. The 'pipe' fd is in proper
1680 		 * place already.
1681 		 */
1682 		if (!perf_data__is_pipe(session->data))
1683 			lseek(fd, file_offset, SEEK_SET);
1684 		return tool->tracing_data(session, event);
1685 	case PERF_RECORD_HEADER_BUILD_ID:
1686 		return tool->build_id(session, event);
1687 	case PERF_RECORD_FINISHED_ROUND:
1688 		return tool->finished_round(tool, event, oe);
1689 	case PERF_RECORD_ID_INDEX:
1690 		return tool->id_index(session, event);
1691 	case PERF_RECORD_AUXTRACE_INFO:
1692 		return tool->auxtrace_info(session, event);
1693 	case PERF_RECORD_AUXTRACE:
1694 		/* setup for reading amidst mmap */
1695 		lseek(fd, file_offset + event->header.size, SEEK_SET);
1696 		return tool->auxtrace(session, event);
1697 	case PERF_RECORD_AUXTRACE_ERROR:
1698 		perf_session__auxtrace_error_inc(session, event);
1699 		return tool->auxtrace_error(session, event);
1700 	case PERF_RECORD_THREAD_MAP:
1701 		return tool->thread_map(session, event);
1702 	case PERF_RECORD_CPU_MAP:
1703 		return tool->cpu_map(session, event);
1704 	case PERF_RECORD_STAT_CONFIG:
1705 		return tool->stat_config(session, event);
1706 	case PERF_RECORD_STAT:
1707 		return tool->stat(session, event);
1708 	case PERF_RECORD_STAT_ROUND:
1709 		return tool->stat_round(session, event);
1710 	case PERF_RECORD_TIME_CONV:
1711 		session->time_conv = event->time_conv;
1712 		return tool->time_conv(session, event);
1713 	case PERF_RECORD_HEADER_FEATURE:
1714 		return tool->feature(session, event);
1715 	case PERF_RECORD_COMPRESSED:
1716 		err = tool->compressed(session, event, file_offset, file_path);
1717 		if (err)
1718 			dump_event(session->evlist, event, file_offset, &sample, file_path);
1719 		return err;
1720 	case PERF_RECORD_FINISHED_INIT:
1721 		return tool->finished_init(session, event);
1722 	default:
1723 		return -EINVAL;
1724 	}
1725 }
1726 
1727 int perf_session__deliver_synth_event(struct perf_session *session,
1728 				      union perf_event *event,
1729 				      struct perf_sample *sample)
1730 {
1731 	struct evlist *evlist = session->evlist;
1732 	struct perf_tool *tool = session->tool;
1733 
1734 	events_stats__inc(&evlist->stats, event->header.type);
1735 
1736 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1737 		return perf_session__process_user_event(session, event, 0, NULL);
1738 
1739 	return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0, NULL);
1740 }
1741 
1742 static void event_swap(union perf_event *event, bool sample_id_all)
1743 {
1744 	perf_event__swap_op swap;
1745 
1746 	swap = perf_event__swap_ops[event->header.type];
1747 	if (swap)
1748 		swap(event, sample_id_all);
1749 }
1750 
1751 int perf_session__peek_event(struct perf_session *session, off_t file_offset,
1752 			     void *buf, size_t buf_sz,
1753 			     union perf_event **event_ptr,
1754 			     struct perf_sample *sample)
1755 {
1756 	union perf_event *event;
1757 	size_t hdr_sz, rest;
1758 	int fd;
1759 
1760 	if (session->one_mmap && !session->header.needs_swap) {
1761 		event = file_offset - session->one_mmap_offset +
1762 			session->one_mmap_addr;
1763 		goto out_parse_sample;
1764 	}
1765 
1766 	if (perf_data__is_pipe(session->data))
1767 		return -1;
1768 
1769 	fd = perf_data__fd(session->data);
1770 	hdr_sz = sizeof(struct perf_event_header);
1771 
1772 	if (buf_sz < hdr_sz)
1773 		return -1;
1774 
1775 	if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
1776 	    readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
1777 		return -1;
1778 
1779 	event = (union perf_event *)buf;
1780 
1781 	if (session->header.needs_swap)
1782 		perf_event_header__bswap(&event->header);
1783 
1784 	if (event->header.size < hdr_sz || event->header.size > buf_sz)
1785 		return -1;
1786 
1787 	buf += hdr_sz;
1788 	rest = event->header.size - hdr_sz;
1789 
1790 	if (readn(fd, buf, rest) != (ssize_t)rest)
1791 		return -1;
1792 
1793 	if (session->header.needs_swap)
1794 		event_swap(event, evlist__sample_id_all(session->evlist));
1795 
1796 out_parse_sample:
1797 
1798 	if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
1799 	    evlist__parse_sample(session->evlist, event, sample))
1800 		return -1;
1801 
1802 	*event_ptr = event;
1803 
1804 	return 0;
1805 }
1806 
1807 int perf_session__peek_events(struct perf_session *session, u64 offset,
1808 			      u64 size, peek_events_cb_t cb, void *data)
1809 {
1810 	u64 max_offset = offset + size;
1811 	char buf[PERF_SAMPLE_MAX_SIZE];
1812 	union perf_event *event;
1813 	int err;
1814 
1815 	do {
1816 		err = perf_session__peek_event(session, offset, buf,
1817 					       PERF_SAMPLE_MAX_SIZE, &event,
1818 					       NULL);
1819 		if (err)
1820 			return err;
1821 
1822 		err = cb(session, event, offset, data);
1823 		if (err)
1824 			return err;
1825 
1826 		offset += event->header.size;
1827 		if (event->header.type == PERF_RECORD_AUXTRACE)
1828 			offset += event->auxtrace.size;
1829 
1830 	} while (offset < max_offset);
1831 
1832 	return err;
1833 }
1834 
1835 static s64 perf_session__process_event(struct perf_session *session,
1836 				       union perf_event *event, u64 file_offset,
1837 				       const char *file_path)
1838 {
1839 	struct evlist *evlist = session->evlist;
1840 	struct perf_tool *tool = session->tool;
1841 	int ret;
1842 
1843 	if (session->header.needs_swap)
1844 		event_swap(event, evlist__sample_id_all(evlist));
1845 
1846 	if (event->header.type >= PERF_RECORD_HEADER_MAX)
1847 		return -EINVAL;
1848 
1849 	events_stats__inc(&evlist->stats, event->header.type);
1850 
1851 	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
1852 		return perf_session__process_user_event(session, event, file_offset, file_path);
1853 
1854 	if (tool->ordered_events) {
1855 		u64 timestamp = -1ULL;
1856 
1857 		ret = evlist__parse_sample_timestamp(evlist, event, &timestamp);
1858 		if (ret && ret != -1)
1859 			return ret;
1860 
1861 		ret = perf_session__queue_event(session, event, timestamp, file_offset, file_path);
1862 		if (ret != -ETIME)
1863 			return ret;
1864 	}
1865 
1866 	return perf_session__deliver_event(session, event, tool, file_offset, file_path);
1867 }
1868 
1869 void perf_event_header__bswap(struct perf_event_header *hdr)
1870 {
1871 	hdr->type = bswap_32(hdr->type);
1872 	hdr->misc = bswap_16(hdr->misc);
1873 	hdr->size = bswap_16(hdr->size);
1874 }
1875 
1876 struct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
1877 {
1878 	return machine__findnew_thread(&session->machines.host, -1, pid);
1879 }
1880 
1881 int perf_session__register_idle_thread(struct perf_session *session)
1882 {
1883 	struct thread *thread = machine__idle_thread(&session->machines.host);
1884 
1885 	/* machine__idle_thread() got the thread, so put it */
1886 	thread__put(thread);
1887 	return thread ? 0 : -1;
1888 }
1889 
1890 static void
1891 perf_session__warn_order(const struct perf_session *session)
1892 {
1893 	const struct ordered_events *oe = &session->ordered_events;
1894 	struct evsel *evsel;
1895 	bool should_warn = true;
1896 
1897 	evlist__for_each_entry(session->evlist, evsel) {
1898 		if (evsel->core.attr.write_backward)
1899 			should_warn = false;
1900 	}
1901 
1902 	if (!should_warn)
1903 		return;
1904 	if (oe->nr_unordered_events != 0)
1905 		ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
1906 }
1907 
1908 static void perf_session__warn_about_errors(const struct perf_session *session)
1909 {
1910 	const struct events_stats *stats = &session->evlist->stats;
1911 
1912 	if (session->tool->lost == perf_event__process_lost &&
1913 	    stats->nr_events[PERF_RECORD_LOST] != 0) {
1914 		ui__warning("Processed %d events and lost %d chunks!\n\n"
1915 			    "Check IO/CPU overload!\n\n",
1916 			    stats->nr_events[0],
1917 			    stats->nr_events[PERF_RECORD_LOST]);
1918 	}
1919 
1920 	if (session->tool->lost_samples == perf_event__process_lost_samples) {
1921 		double drop_rate;
1922 
1923 		drop_rate = (double)stats->total_lost_samples /
1924 			    (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
1925 		if (drop_rate > 0.05) {
1926 			ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n",
1927 				    stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
1928 				    drop_rate * 100.0);
1929 		}
1930 	}
1931 
1932 	if (session->tool->aux == perf_event__process_aux &&
1933 	    stats->total_aux_lost != 0) {
1934 		ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
1935 			    stats->total_aux_lost,
1936 			    stats->nr_events[PERF_RECORD_AUX]);
1937 	}
1938 
1939 	if (session->tool->aux == perf_event__process_aux &&
1940 	    stats->total_aux_partial != 0) {
1941 		bool vmm_exclusive = false;
1942 
1943 		(void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
1944 		                       &vmm_exclusive);
1945 
1946 		ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n"
1947 		            "Are you running a KVM guest in the background?%s\n\n",
1948 			    stats->total_aux_partial,
1949 			    stats->nr_events[PERF_RECORD_AUX],
1950 			    vmm_exclusive ?
1951 			    "\nReloading kvm_intel module with vmm_exclusive=0\n"
1952 			    "will reduce the gaps to only guest's timeslices." :
1953 			    "");
1954 	}
1955 
1956 	if (session->tool->aux == perf_event__process_aux &&
1957 	    stats->total_aux_collision != 0) {
1958 		ui__warning("AUX data detected collision  %" PRIu64 " times out of %u!\n\n",
1959 			    stats->total_aux_collision,
1960 			    stats->nr_events[PERF_RECORD_AUX]);
1961 	}
1962 
1963 	if (stats->nr_unknown_events != 0) {
1964 		ui__warning("Found %u unknown events!\n\n"
1965 			    "Is this an older tool processing a perf.data "
1966 			    "file generated by a more recent tool?\n\n"
1967 			    "If that is not the case, consider "
1968 			    "reporting to linux-kernel@vger.kernel.org.\n\n",
1969 			    stats->nr_unknown_events);
1970 	}
1971 
1972 	if (stats->nr_unknown_id != 0) {
1973 		ui__warning("%u samples with id not present in the header\n",
1974 			    stats->nr_unknown_id);
1975 	}
1976 
1977 	if (stats->nr_invalid_chains != 0) {
1978 		ui__warning("Found invalid callchains!\n\n"
1979 			    "%u out of %u events were discarded for this reason.\n\n"
1980 			    "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
1981 			    stats->nr_invalid_chains,
1982 			    stats->nr_events[PERF_RECORD_SAMPLE]);
1983 	}
1984 
1985 	if (stats->nr_unprocessable_samples != 0) {
1986 		ui__warning("%u unprocessable samples recorded.\n"
1987 			    "Do you have a KVM guest running and not using 'perf kvm'?\n",
1988 			    stats->nr_unprocessable_samples);
1989 	}
1990 
1991 	perf_session__warn_order(session);
1992 
1993 	events_stats__auxtrace_error_warn(stats);
1994 
1995 	if (stats->nr_proc_map_timeout != 0) {
1996 		ui__warning("%d map information files for pre-existing threads were\n"
1997 			    "not processed, if there are samples for addresses they\n"
1998 			    "will not be resolved, you may find out which are these\n"
1999 			    "threads by running with -v and redirecting the output\n"
2000 			    "to a file.\n"
2001 			    "The time limit to process proc map is too short?\n"
2002 			    "Increase it by --proc-map-timeout\n",
2003 			    stats->nr_proc_map_timeout);
2004 	}
2005 }
2006 
2007 static int perf_session__flush_thread_stack(struct thread *thread,
2008 					    void *p __maybe_unused)
2009 {
2010 	return thread_stack__flush(thread);
2011 }
2012 
2013 static int perf_session__flush_thread_stacks(struct perf_session *session)
2014 {
2015 	return machines__for_each_thread(&session->machines,
2016 					 perf_session__flush_thread_stack,
2017 					 NULL);
2018 }
2019 
2020 volatile int session_done;
2021 
2022 static int __perf_session__process_decomp_events(struct perf_session *session);
2023 
2024 static int __perf_session__process_pipe_events(struct perf_session *session)
2025 {
2026 	struct ordered_events *oe = &session->ordered_events;
2027 	struct perf_tool *tool = session->tool;
2028 	union perf_event *event;
2029 	uint32_t size, cur_size = 0;
2030 	void *buf = NULL;
2031 	s64 skip = 0;
2032 	u64 head;
2033 	ssize_t err;
2034 	void *p;
2035 
2036 	perf_tool__fill_defaults(tool);
2037 
2038 	head = 0;
2039 	cur_size = sizeof(union perf_event);
2040 
2041 	buf = malloc(cur_size);
2042 	if (!buf)
2043 		return -errno;
2044 	ordered_events__set_copy_on_queue(oe, true);
2045 more:
2046 	event = buf;
2047 	err = perf_data__read(session->data, event,
2048 			      sizeof(struct perf_event_header));
2049 	if (err <= 0) {
2050 		if (err == 0)
2051 			goto done;
2052 
2053 		pr_err("failed to read event header\n");
2054 		goto out_err;
2055 	}
2056 
2057 	if (session->header.needs_swap)
2058 		perf_event_header__bswap(&event->header);
2059 
2060 	size = event->header.size;
2061 	if (size < sizeof(struct perf_event_header)) {
2062 		pr_err("bad event header size\n");
2063 		goto out_err;
2064 	}
2065 
2066 	if (size > cur_size) {
2067 		void *new = realloc(buf, size);
2068 		if (!new) {
2069 			pr_err("failed to allocate memory to read event\n");
2070 			goto out_err;
2071 		}
2072 		buf = new;
2073 		cur_size = size;
2074 		event = buf;
2075 	}
2076 	p = event;
2077 	p += sizeof(struct perf_event_header);
2078 
2079 	if (size - sizeof(struct perf_event_header)) {
2080 		err = perf_data__read(session->data, p,
2081 				      size - sizeof(struct perf_event_header));
2082 		if (err <= 0) {
2083 			if (err == 0) {
2084 				pr_err("unexpected end of event stream\n");
2085 				goto done;
2086 			}
2087 
2088 			pr_err("failed to read event data\n");
2089 			goto out_err;
2090 		}
2091 	}
2092 
2093 	if ((skip = perf_session__process_event(session, event, head, "pipe")) < 0) {
2094 		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
2095 		       head, event->header.size, event->header.type);
2096 		err = -EINVAL;
2097 		goto out_err;
2098 	}
2099 
2100 	head += size;
2101 
2102 	if (skip > 0)
2103 		head += skip;
2104 
2105 	err = __perf_session__process_decomp_events(session);
2106 	if (err)
2107 		goto out_err;
2108 
2109 	if (!session_done())
2110 		goto more;
2111 done:
2112 	/* do the final flush for ordered samples */
2113 	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
2114 	if (err)
2115 		goto out_err;
2116 	err = auxtrace__flush_events(session, tool);
2117 	if (err)
2118 		goto out_err;
2119 	err = perf_session__flush_thread_stacks(session);
2120 out_err:
2121 	free(buf);
2122 	if (!tool->no_warn)
2123 		perf_session__warn_about_errors(session);
2124 	ordered_events__free(&session->ordered_events);
2125 	auxtrace__free_events(session);
2126 	return err;
2127 }
2128 
2129 static union perf_event *
2130 prefetch_event(char *buf, u64 head, size_t mmap_size,
2131 	       bool needs_swap, union perf_event *error)
2132 {
2133 	union perf_event *event;
2134 	u16 event_size;
2135 
2136 	/*
2137 	 * Ensure we have enough space remaining to read
2138 	 * the size of the event in the headers.
2139 	 */
2140 	if (head + sizeof(event->header) > mmap_size)
2141 		return NULL;
2142 
2143 	event = (union perf_event *)(buf + head);
2144 	if (needs_swap)
2145 		perf_event_header__bswap(&event->header);
2146 
2147 	event_size = event->header.size;
2148 	if (head + event_size <= mmap_size)
2149 		return event;
2150 
2151 	/* We're not fetching the event so swap back again */
2152 	if (needs_swap)
2153 		perf_event_header__bswap(&event->header);
2154 
2155 	/* Check if the event fits into the next mmapped buf. */
2156 	if (event_size <= mmap_size - head % page_size) {
2157 		/* Remap buf and fetch again. */
2158 		return NULL;
2159 	}
2160 
2161 	/* Invalid input. Event size should never exceed mmap_size. */
2162 	pr_debug("%s: head=%#" PRIx64 " event->header.size=%#x, mmap_size=%#zx:"
2163 		 " fuzzed or compressed perf.data?\n", __func__, head, event_size, mmap_size);
2164 
2165 	return error;
2166 }
2167 
2168 static union perf_event *
2169 fetch_mmaped_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
2170 {
2171 	return prefetch_event(buf, head, mmap_size, needs_swap, ERR_PTR(-EINVAL));
2172 }
2173 
2174 static union perf_event *
2175 fetch_decomp_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
2176 {
2177 	return prefetch_event(buf, head, mmap_size, needs_swap, NULL);
2178 }
2179 
2180 static int __perf_session__process_decomp_events(struct perf_session *session)
2181 {
2182 	s64 skip;
2183 	u64 size;
2184 	struct decomp *decomp = session->active_decomp->decomp_last;
2185 
2186 	if (!decomp)
2187 		return 0;
2188 
2189 	while (decomp->head < decomp->size && !session_done()) {
2190 		union perf_event *event = fetch_decomp_event(decomp->head, decomp->size, decomp->data,
2191 							     session->header.needs_swap);
2192 
2193 		if (!event)
2194 			break;
2195 
2196 		size = event->header.size;
2197 
2198 		if (size < sizeof(struct perf_event_header) ||
2199 		    (skip = perf_session__process_event(session, event, decomp->file_pos,
2200 							decomp->file_path)) < 0) {
2201 			pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
2202 				decomp->file_pos + decomp->head, event->header.size, event->header.type);
2203 			return -EINVAL;
2204 		}
2205 
2206 		if (skip)
2207 			size += skip;
2208 
2209 		decomp->head += size;
2210 	}
2211 
2212 	return 0;
2213 }
2214 
2215 /*
2216  * On 64bit we can mmap the data file in one go. No need for tiny mmap
2217  * slices. On 32bit we use 32MB.
2218  */
2219 #if BITS_PER_LONG == 64
2220 #define MMAP_SIZE ULLONG_MAX
2221 #define NUM_MMAPS 1
2222 #else
2223 #define MMAP_SIZE (32 * 1024 * 1024ULL)
2224 #define NUM_MMAPS 128
2225 #endif
2226 
2227 struct reader;
2228 
2229 typedef s64 (*reader_cb_t)(struct perf_session *session,
2230 			   union perf_event *event,
2231 			   u64 file_offset,
2232 			   const char *file_path);
2233 
2234 struct reader {
2235 	int		 fd;
2236 	const char	 *path;
2237 	u64		 data_size;
2238 	u64		 data_offset;
2239 	reader_cb_t	 process;
2240 	bool		 in_place_update;
2241 	char		 *mmaps[NUM_MMAPS];
2242 	size_t		 mmap_size;
2243 	int		 mmap_idx;
2244 	char		 *mmap_cur;
2245 	u64		 file_pos;
2246 	u64		 file_offset;
2247 	u64		 head;
2248 	u64		 size;
2249 	bool		 done;
2250 	struct zstd_data   zstd_data;
2251 	struct decomp_data decomp_data;
2252 };
2253 
2254 static int
2255 reader__init(struct reader *rd, bool *one_mmap)
2256 {
2257 	u64 data_size = rd->data_size;
2258 	char **mmaps = rd->mmaps;
2259 
2260 	rd->head = rd->data_offset;
2261 	data_size += rd->data_offset;
2262 
2263 	rd->mmap_size = MMAP_SIZE;
2264 	if (rd->mmap_size > data_size) {
2265 		rd->mmap_size = data_size;
2266 		if (one_mmap)
2267 			*one_mmap = true;
2268 	}
2269 
2270 	memset(mmaps, 0, sizeof(rd->mmaps));
2271 
2272 	if (zstd_init(&rd->zstd_data, 0))
2273 		return -1;
2274 	rd->decomp_data.zstd_decomp = &rd->zstd_data;
2275 
2276 	return 0;
2277 }
2278 
2279 static void
2280 reader__release_decomp(struct reader *rd)
2281 {
2282 	perf_decomp__release_events(rd->decomp_data.decomp);
2283 	zstd_fini(&rd->zstd_data);
2284 }
2285 
2286 static int
2287 reader__mmap(struct reader *rd, struct perf_session *session)
2288 {
2289 	int mmap_prot, mmap_flags;
2290 	char *buf, **mmaps = rd->mmaps;
2291 	u64 page_offset;
2292 
2293 	mmap_prot  = PROT_READ;
2294 	mmap_flags = MAP_SHARED;
2295 
2296 	if (rd->in_place_update) {
2297 		mmap_prot  |= PROT_WRITE;
2298 	} else if (session->header.needs_swap) {
2299 		mmap_prot  |= PROT_WRITE;
2300 		mmap_flags = MAP_PRIVATE;
2301 	}
2302 
2303 	if (mmaps[rd->mmap_idx]) {
2304 		munmap(mmaps[rd->mmap_idx], rd->mmap_size);
2305 		mmaps[rd->mmap_idx] = NULL;
2306 	}
2307 
2308 	page_offset = page_size * (rd->head / page_size);
2309 	rd->file_offset += page_offset;
2310 	rd->head -= page_offset;
2311 
2312 	buf = mmap(NULL, rd->mmap_size, mmap_prot, mmap_flags, rd->fd,
2313 		   rd->file_offset);
2314 	if (buf == MAP_FAILED) {
2315 		pr_err("failed to mmap file\n");
2316 		return -errno;
2317 	}
2318 	mmaps[rd->mmap_idx] = rd->mmap_cur = buf;
2319 	rd->mmap_idx = (rd->mmap_idx + 1) & (ARRAY_SIZE(rd->mmaps) - 1);
2320 	rd->file_pos = rd->file_offset + rd->head;
2321 	if (session->one_mmap) {
2322 		session->one_mmap_addr = buf;
2323 		session->one_mmap_offset = rd->file_offset;
2324 	}
2325 
2326 	return 0;
2327 }
2328 
2329 enum {
2330 	READER_OK,
2331 	READER_NODATA,
2332 };
2333 
2334 static int
2335 reader__read_event(struct reader *rd, struct perf_session *session,
2336 		   struct ui_progress *prog)
2337 {
2338 	u64 size;
2339 	int err = READER_OK;
2340 	union perf_event *event;
2341 	s64 skip;
2342 
2343 	event = fetch_mmaped_event(rd->head, rd->mmap_size, rd->mmap_cur,
2344 				   session->header.needs_swap);
2345 	if (IS_ERR(event))
2346 		return PTR_ERR(event);
2347 
2348 	if (!event)
2349 		return READER_NODATA;
2350 
2351 	size = event->header.size;
2352 
2353 	skip = -EINVAL;
2354 
2355 	if (size < sizeof(struct perf_event_header) ||
2356 	    (skip = rd->process(session, event, rd->file_pos, rd->path)) < 0) {
2357 		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d [%s]\n",
2358 		       rd->file_offset + rd->head, event->header.size,
2359 		       event->header.type, strerror(-skip));
2360 		err = skip;
2361 		goto out;
2362 	}
2363 
2364 	if (skip)
2365 		size += skip;
2366 
2367 	rd->size += size;
2368 	rd->head += size;
2369 	rd->file_pos += size;
2370 
2371 	err = __perf_session__process_decomp_events(session);
2372 	if (err)
2373 		goto out;
2374 
2375 	ui_progress__update(prog, size);
2376 
2377 out:
2378 	return err;
2379 }
2380 
2381 static inline bool
2382 reader__eof(struct reader *rd)
2383 {
2384 	return (rd->file_pos >= rd->data_size + rd->data_offset);
2385 }
2386 
2387 static int
2388 reader__process_events(struct reader *rd, struct perf_session *session,
2389 		       struct ui_progress *prog)
2390 {
2391 	int err;
2392 
2393 	err = reader__init(rd, &session->one_mmap);
2394 	if (err)
2395 		goto out;
2396 
2397 	session->active_decomp = &rd->decomp_data;
2398 
2399 remap:
2400 	err = reader__mmap(rd, session);
2401 	if (err)
2402 		goto out;
2403 
2404 more:
2405 	err = reader__read_event(rd, session, prog);
2406 	if (err < 0)
2407 		goto out;
2408 	else if (err == READER_NODATA)
2409 		goto remap;
2410 
2411 	if (session_done())
2412 		goto out;
2413 
2414 	if (!reader__eof(rd))
2415 		goto more;
2416 
2417 out:
2418 	session->active_decomp = &session->decomp_data;
2419 	return err;
2420 }
2421 
2422 static s64 process_simple(struct perf_session *session,
2423 			  union perf_event *event,
2424 			  u64 file_offset,
2425 			  const char *file_path)
2426 {
2427 	return perf_session__process_event(session, event, file_offset, file_path);
2428 }
2429 
2430 static int __perf_session__process_events(struct perf_session *session)
2431 {
2432 	struct reader rd = {
2433 		.fd		= perf_data__fd(session->data),
2434 		.path		= session->data->file.path,
2435 		.data_size	= session->header.data_size,
2436 		.data_offset	= session->header.data_offset,
2437 		.process	= process_simple,
2438 		.in_place_update = session->data->in_place_update,
2439 	};
2440 	struct ordered_events *oe = &session->ordered_events;
2441 	struct perf_tool *tool = session->tool;
2442 	struct ui_progress prog;
2443 	int err;
2444 
2445 	perf_tool__fill_defaults(tool);
2446 
2447 	if (rd.data_size == 0)
2448 		return -1;
2449 
2450 	ui_progress__init_size(&prog, rd.data_size, "Processing events...");
2451 
2452 	err = reader__process_events(&rd, session, &prog);
2453 	if (err)
2454 		goto out_err;
2455 	/* do the final flush for ordered samples */
2456 	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
2457 	if (err)
2458 		goto out_err;
2459 	err = auxtrace__flush_events(session, tool);
2460 	if (err)
2461 		goto out_err;
2462 	err = perf_session__flush_thread_stacks(session);
2463 out_err:
2464 	ui_progress__finish();
2465 	if (!tool->no_warn)
2466 		perf_session__warn_about_errors(session);
2467 	/*
2468 	 * We may switching perf.data output, make ordered_events
2469 	 * reusable.
2470 	 */
2471 	ordered_events__reinit(&session->ordered_events);
2472 	auxtrace__free_events(session);
2473 	reader__release_decomp(&rd);
2474 	session->one_mmap = false;
2475 	return err;
2476 }
2477 
2478 /*
2479  * Processing 2 MB of data from each reader in sequence,
2480  * because that's the way the ordered events sorting works
2481  * most efficiently.
2482  */
2483 #define READER_MAX_SIZE (2 * 1024 * 1024)
2484 
2485 /*
2486  * This function reads, merge and process directory data.
2487  * It assumens the version 1 of directory data, where each
2488  * data file holds per-cpu data, already sorted by kernel.
2489  */
2490 static int __perf_session__process_dir_events(struct perf_session *session)
2491 {
2492 	struct perf_data *data = session->data;
2493 	struct perf_tool *tool = session->tool;
2494 	int i, ret, readers, nr_readers;
2495 	struct ui_progress prog;
2496 	u64 total_size = perf_data__size(session->data);
2497 	struct reader *rd;
2498 
2499 	perf_tool__fill_defaults(tool);
2500 
2501 	ui_progress__init_size(&prog, total_size, "Sorting events...");
2502 
2503 	nr_readers = 1;
2504 	for (i = 0; i < data->dir.nr; i++) {
2505 		if (data->dir.files[i].size)
2506 			nr_readers++;
2507 	}
2508 
2509 	rd = zalloc(nr_readers * sizeof(struct reader));
2510 	if (!rd)
2511 		return -ENOMEM;
2512 
2513 	rd[0] = (struct reader) {
2514 		.fd		 = perf_data__fd(session->data),
2515 		.path		 = session->data->file.path,
2516 		.data_size	 = session->header.data_size,
2517 		.data_offset	 = session->header.data_offset,
2518 		.process	 = process_simple,
2519 		.in_place_update = session->data->in_place_update,
2520 	};
2521 	ret = reader__init(&rd[0], NULL);
2522 	if (ret)
2523 		goto out_err;
2524 	ret = reader__mmap(&rd[0], session);
2525 	if (ret)
2526 		goto out_err;
2527 	readers = 1;
2528 
2529 	for (i = 0; i < data->dir.nr; i++) {
2530 		if (!data->dir.files[i].size)
2531 			continue;
2532 		rd[readers] = (struct reader) {
2533 			.fd		 = data->dir.files[i].fd,
2534 			.path		 = data->dir.files[i].path,
2535 			.data_size	 = data->dir.files[i].size,
2536 			.data_offset	 = 0,
2537 			.process	 = process_simple,
2538 			.in_place_update = session->data->in_place_update,
2539 		};
2540 		ret = reader__init(&rd[readers], NULL);
2541 		if (ret)
2542 			goto out_err;
2543 		ret = reader__mmap(&rd[readers], session);
2544 		if (ret)
2545 			goto out_err;
2546 		readers++;
2547 	}
2548 
2549 	i = 0;
2550 	while (readers) {
2551 		if (session_done())
2552 			break;
2553 
2554 		if (rd[i].done) {
2555 			i = (i + 1) % nr_readers;
2556 			continue;
2557 		}
2558 		if (reader__eof(&rd[i])) {
2559 			rd[i].done = true;
2560 			readers--;
2561 			continue;
2562 		}
2563 
2564 		session->active_decomp = &rd[i].decomp_data;
2565 		ret = reader__read_event(&rd[i], session, &prog);
2566 		if (ret < 0) {
2567 			goto out_err;
2568 		} else if (ret == READER_NODATA) {
2569 			ret = reader__mmap(&rd[i], session);
2570 			if (ret)
2571 				goto out_err;
2572 		}
2573 
2574 		if (rd[i].size >= READER_MAX_SIZE) {
2575 			rd[i].size = 0;
2576 			i = (i + 1) % nr_readers;
2577 		}
2578 	}
2579 
2580 	ret = ordered_events__flush(&session->ordered_events, OE_FLUSH__FINAL);
2581 	if (ret)
2582 		goto out_err;
2583 
2584 	ret = perf_session__flush_thread_stacks(session);
2585 out_err:
2586 	ui_progress__finish();
2587 
2588 	if (!tool->no_warn)
2589 		perf_session__warn_about_errors(session);
2590 
2591 	/*
2592 	 * We may switching perf.data output, make ordered_events
2593 	 * reusable.
2594 	 */
2595 	ordered_events__reinit(&session->ordered_events);
2596 
2597 	session->one_mmap = false;
2598 
2599 	session->active_decomp = &session->decomp_data;
2600 	for (i = 0; i < nr_readers; i++)
2601 		reader__release_decomp(&rd[i]);
2602 	zfree(&rd);
2603 
2604 	return ret;
2605 }
2606 
2607 int perf_session__process_events(struct perf_session *session)
2608 {
2609 	if (perf_session__register_idle_thread(session) < 0)
2610 		return -ENOMEM;
2611 
2612 	if (perf_data__is_pipe(session->data))
2613 		return __perf_session__process_pipe_events(session);
2614 
2615 	if (perf_data__is_dir(session->data) && session->data->dir.nr)
2616 		return __perf_session__process_dir_events(session);
2617 
2618 	return __perf_session__process_events(session);
2619 }
2620 
2621 bool perf_session__has_traces(struct perf_session *session, const char *msg)
2622 {
2623 	struct evsel *evsel;
2624 
2625 	evlist__for_each_entry(session->evlist, evsel) {
2626 		if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT)
2627 			return true;
2628 	}
2629 
2630 	pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
2631 	return false;
2632 }
2633 
2634 int map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr)
2635 {
2636 	char *bracket;
2637 	struct ref_reloc_sym *ref;
2638 	struct kmap *kmap;
2639 
2640 	ref = zalloc(sizeof(struct ref_reloc_sym));
2641 	if (ref == NULL)
2642 		return -ENOMEM;
2643 
2644 	ref->name = strdup(symbol_name);
2645 	if (ref->name == NULL) {
2646 		free(ref);
2647 		return -ENOMEM;
2648 	}
2649 
2650 	bracket = strchr(ref->name, ']');
2651 	if (bracket)
2652 		*bracket = '\0';
2653 
2654 	ref->addr = addr;
2655 
2656 	kmap = map__kmap(map);
2657 	if (kmap)
2658 		kmap->ref_reloc_sym = ref;
2659 
2660 	return 0;
2661 }
2662 
2663 size_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
2664 {
2665 	return machines__fprintf_dsos(&session->machines, fp);
2666 }
2667 
2668 size_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
2669 					  bool (skip)(struct dso *dso, int parm), int parm)
2670 {
2671 	return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
2672 }
2673 
2674 size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp,
2675 				       bool skip_empty)
2676 {
2677 	size_t ret;
2678 	const char *msg = "";
2679 
2680 	if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
2681 		msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
2682 
2683 	ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
2684 
2685 	ret += events_stats__fprintf(&session->evlist->stats, fp, skip_empty);
2686 	return ret;
2687 }
2688 
2689 size_t perf_session__fprintf(struct perf_session *session, FILE *fp)
2690 {
2691 	/*
2692 	 * FIXME: Here we have to actually print all the machines in this
2693 	 * session, not just the host...
2694 	 */
2695 	return machine__fprintf(&session->machines.host, fp);
2696 }
2697 
2698 struct evsel *perf_session__find_first_evtype(struct perf_session *session,
2699 					      unsigned int type)
2700 {
2701 	struct evsel *pos;
2702 
2703 	evlist__for_each_entry(session->evlist, pos) {
2704 		if (pos->core.attr.type == type)
2705 			return pos;
2706 	}
2707 	return NULL;
2708 }
2709 
2710 int perf_session__cpu_bitmap(struct perf_session *session,
2711 			     const char *cpu_list, unsigned long *cpu_bitmap)
2712 {
2713 	int i, err = -1;
2714 	struct perf_cpu_map *map;
2715 	int nr_cpus = min(session->header.env.nr_cpus_avail, MAX_NR_CPUS);
2716 
2717 	for (i = 0; i < PERF_TYPE_MAX; ++i) {
2718 		struct evsel *evsel;
2719 
2720 		evsel = perf_session__find_first_evtype(session, i);
2721 		if (!evsel)
2722 			continue;
2723 
2724 		if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CPU)) {
2725 			pr_err("File does not contain CPU events. "
2726 			       "Remove -C option to proceed.\n");
2727 			return -1;
2728 		}
2729 	}
2730 
2731 	map = perf_cpu_map__new(cpu_list);
2732 	if (map == NULL) {
2733 		pr_err("Invalid cpu_list\n");
2734 		return -1;
2735 	}
2736 
2737 	for (i = 0; i < perf_cpu_map__nr(map); i++) {
2738 		struct perf_cpu cpu = perf_cpu_map__cpu(map, i);
2739 
2740 		if (cpu.cpu >= nr_cpus) {
2741 			pr_err("Requested CPU %d too large. "
2742 			       "Consider raising MAX_NR_CPUS\n", cpu.cpu);
2743 			goto out_delete_map;
2744 		}
2745 
2746 		set_bit(cpu.cpu, cpu_bitmap);
2747 	}
2748 
2749 	err = 0;
2750 
2751 out_delete_map:
2752 	perf_cpu_map__put(map);
2753 	return err;
2754 }
2755 
2756 void perf_session__fprintf_info(struct perf_session *session, FILE *fp,
2757 				bool full)
2758 {
2759 	if (session == NULL || fp == NULL)
2760 		return;
2761 
2762 	fprintf(fp, "# ========\n");
2763 	perf_header__fprintf_info(session, fp, full);
2764 	fprintf(fp, "# ========\n#\n");
2765 }
2766 
2767 static int perf_session__register_guest(struct perf_session *session, pid_t machine_pid)
2768 {
2769 	struct machine *machine = machines__findnew(&session->machines, machine_pid);
2770 	struct thread *thread;
2771 
2772 	if (!machine)
2773 		return -ENOMEM;
2774 
2775 	machine->single_address_space = session->machines.host.single_address_space;
2776 
2777 	thread = machine__idle_thread(machine);
2778 	if (!thread)
2779 		return -ENOMEM;
2780 	thread__put(thread);
2781 
2782 	machine->kallsyms_filename = perf_data__guest_kallsyms_name(session->data, machine_pid);
2783 
2784 	return 0;
2785 }
2786 
2787 static int perf_session__set_guest_cpu(struct perf_session *session, pid_t pid,
2788 				       pid_t tid, int guest_cpu)
2789 {
2790 	struct machine *machine = &session->machines.host;
2791 	struct thread *thread = machine__findnew_thread(machine, pid, tid);
2792 
2793 	if (!thread)
2794 		return -ENOMEM;
2795 	thread->guest_cpu = guest_cpu;
2796 	thread__put(thread);
2797 
2798 	return 0;
2799 }
2800 
2801 int perf_event__process_id_index(struct perf_session *session,
2802 				 union perf_event *event)
2803 {
2804 	struct evlist *evlist = session->evlist;
2805 	struct perf_record_id_index *ie = &event->id_index;
2806 	size_t sz = ie->header.size - sizeof(*ie);
2807 	size_t i, nr, max_nr;
2808 	size_t e1_sz = sizeof(struct id_index_entry);
2809 	size_t e2_sz = sizeof(struct id_index_entry_2);
2810 	size_t etot_sz = e1_sz + e2_sz;
2811 	struct id_index_entry_2 *e2;
2812 	pid_t last_pid = 0;
2813 
2814 	max_nr = sz / e1_sz;
2815 	nr = ie->nr;
2816 	if (nr > max_nr) {
2817 		printf("Too big: nr %zu max_nr %zu\n", nr, max_nr);
2818 		return -EINVAL;
2819 	}
2820 
2821 	if (sz >= nr * etot_sz) {
2822 		max_nr = sz / etot_sz;
2823 		if (nr > max_nr) {
2824 			printf("Too big2: nr %zu max_nr %zu\n", nr, max_nr);
2825 			return -EINVAL;
2826 		}
2827 		e2 = (void *)ie + sizeof(*ie) + nr * e1_sz;
2828 	} else {
2829 		e2 = NULL;
2830 	}
2831 
2832 	if (dump_trace)
2833 		fprintf(stdout, " nr: %zu\n", nr);
2834 
2835 	for (i = 0; i < nr; i++, (e2 ? e2++ : 0)) {
2836 		struct id_index_entry *e = &ie->entries[i];
2837 		struct perf_sample_id *sid;
2838 		int ret;
2839 
2840 		if (dump_trace) {
2841 			fprintf(stdout,	" ... id: %"PRI_lu64, e->id);
2842 			fprintf(stdout,	"  idx: %"PRI_lu64, e->idx);
2843 			fprintf(stdout,	"  cpu: %"PRI_ld64, e->cpu);
2844 			fprintf(stdout, "  tid: %"PRI_ld64, e->tid);
2845 			if (e2) {
2846 				fprintf(stdout, "  machine_pid: %"PRI_ld64, e2->machine_pid);
2847 				fprintf(stdout, "  vcpu: %"PRI_lu64"\n", e2->vcpu);
2848 			} else {
2849 				fprintf(stdout, "\n");
2850 			}
2851 		}
2852 
2853 		sid = evlist__id2sid(evlist, e->id);
2854 		if (!sid)
2855 			return -ENOENT;
2856 
2857 		sid->idx = e->idx;
2858 		sid->cpu.cpu = e->cpu;
2859 		sid->tid = e->tid;
2860 
2861 		if (!e2)
2862 			continue;
2863 
2864 		sid->machine_pid = e2->machine_pid;
2865 		sid->vcpu.cpu = e2->vcpu;
2866 
2867 		if (!sid->machine_pid)
2868 			continue;
2869 
2870 		if (sid->machine_pid != last_pid) {
2871 			ret = perf_session__register_guest(session, sid->machine_pid);
2872 			if (ret)
2873 				return ret;
2874 			last_pid = sid->machine_pid;
2875 			perf_guest = true;
2876 		}
2877 
2878 		ret = perf_session__set_guest_cpu(session, sid->machine_pid, e->tid, e2->vcpu);
2879 		if (ret)
2880 			return ret;
2881 	}
2882 	return 0;
2883 }
2884