18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <errno.h>
38c2ecf20Sopenharmony_ci#include <inttypes.h>
48c2ecf20Sopenharmony_ci#include <linux/err.h>
58c2ecf20Sopenharmony_ci#include <linux/kernel.h>
68c2ecf20Sopenharmony_ci#include <linux/zalloc.h>
78c2ecf20Sopenharmony_ci#include <api/fs/fs.h>
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <byteswap.h>
108c2ecf20Sopenharmony_ci#include <unistd.h>
118c2ecf20Sopenharmony_ci#include <sys/types.h>
128c2ecf20Sopenharmony_ci#include <sys/mman.h>
138c2ecf20Sopenharmony_ci#include <perf/cpumap.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "map_symbol.h"
168c2ecf20Sopenharmony_ci#include "branch.h"
178c2ecf20Sopenharmony_ci#include "debug.h"
188c2ecf20Sopenharmony_ci#include "evlist.h"
198c2ecf20Sopenharmony_ci#include "evsel.h"
208c2ecf20Sopenharmony_ci#include "memswap.h"
218c2ecf20Sopenharmony_ci#include "map.h"
228c2ecf20Sopenharmony_ci#include "symbol.h"
238c2ecf20Sopenharmony_ci#include "session.h"
248c2ecf20Sopenharmony_ci#include "tool.h"
258c2ecf20Sopenharmony_ci#include "perf_regs.h"
268c2ecf20Sopenharmony_ci#include "asm/bug.h"
278c2ecf20Sopenharmony_ci#include "auxtrace.h"
288c2ecf20Sopenharmony_ci#include "thread.h"
298c2ecf20Sopenharmony_ci#include "thread-stack.h"
308c2ecf20Sopenharmony_ci#include "sample-raw.h"
318c2ecf20Sopenharmony_ci#include "stat.h"
328c2ecf20Sopenharmony_ci#include "ui/progress.h"
338c2ecf20Sopenharmony_ci#include "../perf.h"
348c2ecf20Sopenharmony_ci#include "arch/common.h"
358c2ecf20Sopenharmony_ci#include <internal/lib.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#ifdef HAVE_ZSTD_SUPPORT
388c2ecf20Sopenharmony_cistatic int perf_session__process_compressed_event(struct perf_session *session,
398c2ecf20Sopenharmony_ci						  union perf_event *event, u64 file_offset)
408c2ecf20Sopenharmony_ci{
418c2ecf20Sopenharmony_ci	void *src;
428c2ecf20Sopenharmony_ci	size_t decomp_size, src_size;
438c2ecf20Sopenharmony_ci	u64 decomp_last_rem = 0;
448c2ecf20Sopenharmony_ci	size_t mmap_len, decomp_len = session->header.env.comp_mmap_len;
458c2ecf20Sopenharmony_ci	struct decomp *decomp, *decomp_last = session->decomp_last;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (decomp_last) {
488c2ecf20Sopenharmony_ci		decomp_last_rem = decomp_last->size - decomp_last->head;
498c2ecf20Sopenharmony_ci		decomp_len += decomp_last_rem;
508c2ecf20Sopenharmony_ci	}
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_ci	mmap_len = sizeof(struct decomp) + decomp_len;
538c2ecf20Sopenharmony_ci	decomp = mmap(NULL, mmap_len, PROT_READ|PROT_WRITE,
548c2ecf20Sopenharmony_ci		      MAP_ANONYMOUS|MAP_PRIVATE, -1, 0);
558c2ecf20Sopenharmony_ci	if (decomp == MAP_FAILED) {
568c2ecf20Sopenharmony_ci		pr_err("Couldn't allocate memory for decompression\n");
578c2ecf20Sopenharmony_ci		return -1;
588c2ecf20Sopenharmony_ci	}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	decomp->file_pos = file_offset;
618c2ecf20Sopenharmony_ci	decomp->mmap_len = mmap_len;
628c2ecf20Sopenharmony_ci	decomp->head = 0;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	if (decomp_last_rem) {
658c2ecf20Sopenharmony_ci		memcpy(decomp->data, &(decomp_last->data[decomp_last->head]), decomp_last_rem);
668c2ecf20Sopenharmony_ci		decomp->size = decomp_last_rem;
678c2ecf20Sopenharmony_ci	}
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	src = (void *)event + sizeof(struct perf_record_compressed);
708c2ecf20Sopenharmony_ci	src_size = event->pack.header.size - sizeof(struct perf_record_compressed);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	decomp_size = zstd_decompress_stream(&(session->zstd_data), src, src_size,
738c2ecf20Sopenharmony_ci				&(decomp->data[decomp_last_rem]), decomp_len - decomp_last_rem);
748c2ecf20Sopenharmony_ci	if (!decomp_size) {
758c2ecf20Sopenharmony_ci		munmap(decomp, mmap_len);
768c2ecf20Sopenharmony_ci		pr_err("Couldn't decompress data\n");
778c2ecf20Sopenharmony_ci		return -1;
788c2ecf20Sopenharmony_ci	}
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	decomp->size += decomp_size;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	if (session->decomp == NULL) {
838c2ecf20Sopenharmony_ci		session->decomp = decomp;
848c2ecf20Sopenharmony_ci		session->decomp_last = decomp;
858c2ecf20Sopenharmony_ci	} else {
868c2ecf20Sopenharmony_ci		session->decomp_last->next = decomp;
878c2ecf20Sopenharmony_ci		session->decomp_last = decomp;
888c2ecf20Sopenharmony_ci	}
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	pr_debug("decomp (B): %zd to %zd\n", src_size, decomp_size);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	return 0;
938c2ecf20Sopenharmony_ci}
948c2ecf20Sopenharmony_ci#else /* !HAVE_ZSTD_SUPPORT */
958c2ecf20Sopenharmony_ci#define perf_session__process_compressed_event perf_session__process_compressed_event_stub
968c2ecf20Sopenharmony_ci#endif
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic int perf_session__deliver_event(struct perf_session *session,
998c2ecf20Sopenharmony_ci				       union perf_event *event,
1008c2ecf20Sopenharmony_ci				       struct perf_tool *tool,
1018c2ecf20Sopenharmony_ci				       u64 file_offset);
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic int perf_session__open(struct perf_session *session)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	struct perf_data *data = session->data;
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	if (perf_session__read_header(session) < 0) {
1088c2ecf20Sopenharmony_ci		pr_err("incompatible file format (rerun with -v to learn more)\n");
1098c2ecf20Sopenharmony_ci		return -1;
1108c2ecf20Sopenharmony_ci	}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	if (perf_data__is_pipe(data))
1138c2ecf20Sopenharmony_ci		return 0;
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	if (perf_header__has_feat(&session->header, HEADER_STAT))
1168c2ecf20Sopenharmony_ci		return 0;
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	if (!evlist__valid_sample_type(session->evlist)) {
1198c2ecf20Sopenharmony_ci		pr_err("non matching sample_type\n");
1208c2ecf20Sopenharmony_ci		return -1;
1218c2ecf20Sopenharmony_ci	}
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	if (!evlist__valid_sample_id_all(session->evlist)) {
1248c2ecf20Sopenharmony_ci		pr_err("non matching sample_id_all\n");
1258c2ecf20Sopenharmony_ci		return -1;
1268c2ecf20Sopenharmony_ci	}
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (!perf_evlist__valid_read_format(session->evlist)) {
1298c2ecf20Sopenharmony_ci		pr_err("non matching read_format\n");
1308c2ecf20Sopenharmony_ci		return -1;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	return 0;
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_civoid perf_session__set_id_hdr_size(struct perf_session *session)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	u16 id_hdr_size = perf_evlist__id_hdr_size(session->evlist);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	machines__set_id_hdr_size(&session->machines, id_hdr_size);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ciint perf_session__create_kernel_maps(struct perf_session *session)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	int ret = machine__create_kernel_maps(&session->machines.host);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (ret >= 0)
1488c2ecf20Sopenharmony_ci		ret = machines__create_guest_kernel_maps(&session->machines);
1498c2ecf20Sopenharmony_ci	return ret;
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic void perf_session__destroy_kernel_maps(struct perf_session *session)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	machines__destroy_kernel_maps(&session->machines);
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic bool perf_session__has_comm_exec(struct perf_session *session)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct evsel *evsel;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	evlist__for_each_entry(session->evlist, evsel) {
1628c2ecf20Sopenharmony_ci		if (evsel->core.attr.comm_exec)
1638c2ecf20Sopenharmony_ci			return true;
1648c2ecf20Sopenharmony_ci	}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return false;
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic void perf_session__set_comm_exec(struct perf_session *session)
1708c2ecf20Sopenharmony_ci{
1718c2ecf20Sopenharmony_ci	bool comm_exec = perf_session__has_comm_exec(session);
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	machines__set_comm_exec(&session->machines, comm_exec);
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic int ordered_events__deliver_event(struct ordered_events *oe,
1778c2ecf20Sopenharmony_ci					 struct ordered_event *event)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	struct perf_session *session = container_of(oe, struct perf_session,
1808c2ecf20Sopenharmony_ci						    ordered_events);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	return perf_session__deliver_event(session, event->event,
1838c2ecf20Sopenharmony_ci					   session->tool, event->file_offset);
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistruct perf_session *perf_session__new(struct perf_data *data,
1878c2ecf20Sopenharmony_ci				       bool repipe, struct perf_tool *tool)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	int ret = -ENOMEM;
1908c2ecf20Sopenharmony_ci	struct perf_session *session = zalloc(sizeof(*session));
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	if (!session)
1938c2ecf20Sopenharmony_ci		goto out;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	session->repipe = repipe;
1968c2ecf20Sopenharmony_ci	session->tool   = tool;
1978c2ecf20Sopenharmony_ci	INIT_LIST_HEAD(&session->auxtrace_index);
1988c2ecf20Sopenharmony_ci	machines__init(&session->machines);
1998c2ecf20Sopenharmony_ci	ordered_events__init(&session->ordered_events,
2008c2ecf20Sopenharmony_ci			     ordered_events__deliver_event, NULL);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_ci	perf_env__init(&session->header.env);
2038c2ecf20Sopenharmony_ci	if (data) {
2048c2ecf20Sopenharmony_ci		ret = perf_data__open(data);
2058c2ecf20Sopenharmony_ci		if (ret < 0)
2068c2ecf20Sopenharmony_ci			goto out_delete;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci		session->data = data;
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci		if (perf_data__is_read(data)) {
2118c2ecf20Sopenharmony_ci			ret = perf_session__open(session);
2128c2ecf20Sopenharmony_ci			if (ret < 0)
2138c2ecf20Sopenharmony_ci				goto out_delete;
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_ci			/*
2168c2ecf20Sopenharmony_ci			 * set session attributes that are present in perf.data
2178c2ecf20Sopenharmony_ci			 * but not in pipe-mode.
2188c2ecf20Sopenharmony_ci			 */
2198c2ecf20Sopenharmony_ci			if (!data->is_pipe) {
2208c2ecf20Sopenharmony_ci				perf_session__set_id_hdr_size(session);
2218c2ecf20Sopenharmony_ci				perf_session__set_comm_exec(session);
2228c2ecf20Sopenharmony_ci			}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci			perf_evlist__init_trace_event_sample_raw(session->evlist);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci			/* Open the directory data. */
2278c2ecf20Sopenharmony_ci			if (data->is_dir) {
2288c2ecf20Sopenharmony_ci				ret = perf_data__open_dir(data);
2298c2ecf20Sopenharmony_ci				if (ret)
2308c2ecf20Sopenharmony_ci					goto out_delete;
2318c2ecf20Sopenharmony_ci			}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci			if (!symbol_conf.kallsyms_name &&
2348c2ecf20Sopenharmony_ci			    !symbol_conf.vmlinux_name)
2358c2ecf20Sopenharmony_ci				symbol_conf.kallsyms_name = perf_data__kallsyms_name(data);
2368c2ecf20Sopenharmony_ci		}
2378c2ecf20Sopenharmony_ci	} else  {
2388c2ecf20Sopenharmony_ci		session->machines.host.env = &perf_env;
2398c2ecf20Sopenharmony_ci	}
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	session->machines.host.single_address_space =
2428c2ecf20Sopenharmony_ci		perf_env__single_address_space(session->machines.host.env);
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	if (!data || perf_data__is_write(data)) {
2458c2ecf20Sopenharmony_ci		/*
2468c2ecf20Sopenharmony_ci		 * In O_RDONLY mode this will be performed when reading the
2478c2ecf20Sopenharmony_ci		 * kernel MMAP event, in perf_event__process_mmap().
2488c2ecf20Sopenharmony_ci		 */
2498c2ecf20Sopenharmony_ci		if (perf_session__create_kernel_maps(session) < 0)
2508c2ecf20Sopenharmony_ci			pr_warning("Cannot read kernel map\n");
2518c2ecf20Sopenharmony_ci	}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/*
2548c2ecf20Sopenharmony_ci	 * In pipe-mode, evlist is empty until PERF_RECORD_HEADER_ATTR is
2558c2ecf20Sopenharmony_ci	 * processed, so evlist__sample_id_all is not meaningful here.
2568c2ecf20Sopenharmony_ci	 */
2578c2ecf20Sopenharmony_ci	if ((!data || !data->is_pipe) && tool && tool->ordering_requires_timestamps &&
2588c2ecf20Sopenharmony_ci	    tool->ordered_events && !evlist__sample_id_all(session->evlist)) {
2598c2ecf20Sopenharmony_ci		dump_printf("WARNING: No sample_id_all support, falling back to unordered processing\n");
2608c2ecf20Sopenharmony_ci		tool->ordered_events = false;
2618c2ecf20Sopenharmony_ci	}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	return session;
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_ci out_delete:
2668c2ecf20Sopenharmony_ci	perf_session__delete(session);
2678c2ecf20Sopenharmony_ci out:
2688c2ecf20Sopenharmony_ci	return ERR_PTR(ret);
2698c2ecf20Sopenharmony_ci}
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_cistatic void perf_session__delete_threads(struct perf_session *session)
2728c2ecf20Sopenharmony_ci{
2738c2ecf20Sopenharmony_ci	machine__delete_threads(&session->machines.host);
2748c2ecf20Sopenharmony_ci}
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_cistatic void perf_session__release_decomp_events(struct perf_session *session)
2778c2ecf20Sopenharmony_ci{
2788c2ecf20Sopenharmony_ci	struct decomp *next, *decomp;
2798c2ecf20Sopenharmony_ci	size_t mmap_len;
2808c2ecf20Sopenharmony_ci	next = session->decomp;
2818c2ecf20Sopenharmony_ci	do {
2828c2ecf20Sopenharmony_ci		decomp = next;
2838c2ecf20Sopenharmony_ci		if (decomp == NULL)
2848c2ecf20Sopenharmony_ci			break;
2858c2ecf20Sopenharmony_ci		next = decomp->next;
2868c2ecf20Sopenharmony_ci		mmap_len = decomp->mmap_len;
2878c2ecf20Sopenharmony_ci		munmap(decomp, mmap_len);
2888c2ecf20Sopenharmony_ci	} while (1);
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_civoid perf_session__delete(struct perf_session *session)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	if (session == NULL)
2948c2ecf20Sopenharmony_ci		return;
2958c2ecf20Sopenharmony_ci	auxtrace__free(session);
2968c2ecf20Sopenharmony_ci	auxtrace_index__free(&session->auxtrace_index);
2978c2ecf20Sopenharmony_ci	perf_session__destroy_kernel_maps(session);
2988c2ecf20Sopenharmony_ci	perf_session__delete_threads(session);
2998c2ecf20Sopenharmony_ci	perf_session__release_decomp_events(session);
3008c2ecf20Sopenharmony_ci	perf_env__exit(&session->header.env);
3018c2ecf20Sopenharmony_ci	machines__exit(&session->machines);
3028c2ecf20Sopenharmony_ci	if (session->data)
3038c2ecf20Sopenharmony_ci		perf_data__close(session->data);
3048c2ecf20Sopenharmony_ci	free(session);
3058c2ecf20Sopenharmony_ci}
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_cistatic int process_event_synth_tracing_data_stub(struct perf_session *session
3088c2ecf20Sopenharmony_ci						 __maybe_unused,
3098c2ecf20Sopenharmony_ci						 union perf_event *event
3108c2ecf20Sopenharmony_ci						 __maybe_unused)
3118c2ecf20Sopenharmony_ci{
3128c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
3138c2ecf20Sopenharmony_ci	return 0;
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic int process_event_synth_attr_stub(struct perf_tool *tool __maybe_unused,
3178c2ecf20Sopenharmony_ci					 union perf_event *event __maybe_unused,
3188c2ecf20Sopenharmony_ci					 struct evlist **pevlist
3198c2ecf20Sopenharmony_ci					 __maybe_unused)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
3228c2ecf20Sopenharmony_ci	return 0;
3238c2ecf20Sopenharmony_ci}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic int process_event_synth_event_update_stub(struct perf_tool *tool __maybe_unused,
3268c2ecf20Sopenharmony_ci						 union perf_event *event __maybe_unused,
3278c2ecf20Sopenharmony_ci						 struct evlist **pevlist
3288c2ecf20Sopenharmony_ci						 __maybe_unused)
3298c2ecf20Sopenharmony_ci{
3308c2ecf20Sopenharmony_ci	if (dump_trace)
3318c2ecf20Sopenharmony_ci		perf_event__fprintf_event_update(event, stdout);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
3348c2ecf20Sopenharmony_ci	return 0;
3358c2ecf20Sopenharmony_ci}
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_cistatic int process_event_sample_stub(struct perf_tool *tool __maybe_unused,
3388c2ecf20Sopenharmony_ci				     union perf_event *event __maybe_unused,
3398c2ecf20Sopenharmony_ci				     struct perf_sample *sample __maybe_unused,
3408c2ecf20Sopenharmony_ci				     struct evsel *evsel __maybe_unused,
3418c2ecf20Sopenharmony_ci				     struct machine *machine __maybe_unused)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
3448c2ecf20Sopenharmony_ci	return 0;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic int process_event_stub(struct perf_tool *tool __maybe_unused,
3488c2ecf20Sopenharmony_ci			      union perf_event *event __maybe_unused,
3498c2ecf20Sopenharmony_ci			      struct perf_sample *sample __maybe_unused,
3508c2ecf20Sopenharmony_ci			      struct machine *machine __maybe_unused)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
3538c2ecf20Sopenharmony_ci	return 0;
3548c2ecf20Sopenharmony_ci}
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_cistatic int process_finished_round_stub(struct perf_tool *tool __maybe_unused,
3578c2ecf20Sopenharmony_ci				       union perf_event *event __maybe_unused,
3588c2ecf20Sopenharmony_ci				       struct ordered_events *oe __maybe_unused)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
3618c2ecf20Sopenharmony_ci	return 0;
3628c2ecf20Sopenharmony_ci}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_cistatic int process_finished_round(struct perf_tool *tool,
3658c2ecf20Sopenharmony_ci				  union perf_event *event,
3668c2ecf20Sopenharmony_ci				  struct ordered_events *oe);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_cistatic int skipn(int fd, off_t n)
3698c2ecf20Sopenharmony_ci{
3708c2ecf20Sopenharmony_ci	char buf[4096];
3718c2ecf20Sopenharmony_ci	ssize_t ret;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	while (n > 0) {
3748c2ecf20Sopenharmony_ci		ret = read(fd, buf, min(n, (off_t)sizeof(buf)));
3758c2ecf20Sopenharmony_ci		if (ret <= 0)
3768c2ecf20Sopenharmony_ci			return ret;
3778c2ecf20Sopenharmony_ci		n -= ret;
3788c2ecf20Sopenharmony_ci	}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	return 0;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_cistatic s64 process_event_auxtrace_stub(struct perf_session *session __maybe_unused,
3848c2ecf20Sopenharmony_ci				       union perf_event *event)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
3878c2ecf20Sopenharmony_ci	if (perf_data__is_pipe(session->data))
3888c2ecf20Sopenharmony_ci		skipn(perf_data__fd(session->data), event->auxtrace.size);
3898c2ecf20Sopenharmony_ci	return event->auxtrace.size;
3908c2ecf20Sopenharmony_ci}
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_cistatic int process_event_op2_stub(struct perf_session *session __maybe_unused,
3938c2ecf20Sopenharmony_ci				  union perf_event *event __maybe_unused)
3948c2ecf20Sopenharmony_ci{
3958c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
3968c2ecf20Sopenharmony_ci	return 0;
3978c2ecf20Sopenharmony_ci}
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic
4018c2ecf20Sopenharmony_ciint process_event_thread_map_stub(struct perf_session *session __maybe_unused,
4028c2ecf20Sopenharmony_ci				  union perf_event *event __maybe_unused)
4038c2ecf20Sopenharmony_ci{
4048c2ecf20Sopenharmony_ci	if (dump_trace)
4058c2ecf20Sopenharmony_ci		perf_event__fprintf_thread_map(event, stdout);
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
4088c2ecf20Sopenharmony_ci	return 0;
4098c2ecf20Sopenharmony_ci}
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_cistatic
4128c2ecf20Sopenharmony_ciint process_event_cpu_map_stub(struct perf_session *session __maybe_unused,
4138c2ecf20Sopenharmony_ci			       union perf_event *event __maybe_unused)
4148c2ecf20Sopenharmony_ci{
4158c2ecf20Sopenharmony_ci	if (dump_trace)
4168c2ecf20Sopenharmony_ci		perf_event__fprintf_cpu_map(event, stdout);
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
4198c2ecf20Sopenharmony_ci	return 0;
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_cistatic
4238c2ecf20Sopenharmony_ciint process_event_stat_config_stub(struct perf_session *session __maybe_unused,
4248c2ecf20Sopenharmony_ci				   union perf_event *event __maybe_unused)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	if (dump_trace)
4278c2ecf20Sopenharmony_ci		perf_event__fprintf_stat_config(event, stdout);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
4308c2ecf20Sopenharmony_ci	return 0;
4318c2ecf20Sopenharmony_ci}
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cistatic int process_stat_stub(struct perf_session *perf_session __maybe_unused,
4348c2ecf20Sopenharmony_ci			     union perf_event *event)
4358c2ecf20Sopenharmony_ci{
4368c2ecf20Sopenharmony_ci	if (dump_trace)
4378c2ecf20Sopenharmony_ci		perf_event__fprintf_stat(event, stdout);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
4408c2ecf20Sopenharmony_ci	return 0;
4418c2ecf20Sopenharmony_ci}
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_cistatic int process_stat_round_stub(struct perf_session *perf_session __maybe_unused,
4448c2ecf20Sopenharmony_ci				   union perf_event *event)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	if (dump_trace)
4478c2ecf20Sopenharmony_ci		perf_event__fprintf_stat_round(event, stdout);
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_ci	dump_printf(": unhandled!\n");
4508c2ecf20Sopenharmony_ci	return 0;
4518c2ecf20Sopenharmony_ci}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic int perf_session__process_compressed_event_stub(struct perf_session *session __maybe_unused,
4548c2ecf20Sopenharmony_ci						       union perf_event *event __maybe_unused,
4558c2ecf20Sopenharmony_ci						       u64 file_offset __maybe_unused)
4568c2ecf20Sopenharmony_ci{
4578c2ecf20Sopenharmony_ci       dump_printf(": unhandled!\n");
4588c2ecf20Sopenharmony_ci       return 0;
4598c2ecf20Sopenharmony_ci}
4608c2ecf20Sopenharmony_ci
4618c2ecf20Sopenharmony_civoid perf_tool__fill_defaults(struct perf_tool *tool)
4628c2ecf20Sopenharmony_ci{
4638c2ecf20Sopenharmony_ci	if (tool->sample == NULL)
4648c2ecf20Sopenharmony_ci		tool->sample = process_event_sample_stub;
4658c2ecf20Sopenharmony_ci	if (tool->mmap == NULL)
4668c2ecf20Sopenharmony_ci		tool->mmap = process_event_stub;
4678c2ecf20Sopenharmony_ci	if (tool->mmap2 == NULL)
4688c2ecf20Sopenharmony_ci		tool->mmap2 = process_event_stub;
4698c2ecf20Sopenharmony_ci	if (tool->comm == NULL)
4708c2ecf20Sopenharmony_ci		tool->comm = process_event_stub;
4718c2ecf20Sopenharmony_ci	if (tool->namespaces == NULL)
4728c2ecf20Sopenharmony_ci		tool->namespaces = process_event_stub;
4738c2ecf20Sopenharmony_ci	if (tool->cgroup == NULL)
4748c2ecf20Sopenharmony_ci		tool->cgroup = process_event_stub;
4758c2ecf20Sopenharmony_ci	if (tool->fork == NULL)
4768c2ecf20Sopenharmony_ci		tool->fork = process_event_stub;
4778c2ecf20Sopenharmony_ci	if (tool->exit == NULL)
4788c2ecf20Sopenharmony_ci		tool->exit = process_event_stub;
4798c2ecf20Sopenharmony_ci	if (tool->lost == NULL)
4808c2ecf20Sopenharmony_ci		tool->lost = perf_event__process_lost;
4818c2ecf20Sopenharmony_ci	if (tool->lost_samples == NULL)
4828c2ecf20Sopenharmony_ci		tool->lost_samples = perf_event__process_lost_samples;
4838c2ecf20Sopenharmony_ci	if (tool->aux == NULL)
4848c2ecf20Sopenharmony_ci		tool->aux = perf_event__process_aux;
4858c2ecf20Sopenharmony_ci	if (tool->itrace_start == NULL)
4868c2ecf20Sopenharmony_ci		tool->itrace_start = perf_event__process_itrace_start;
4878c2ecf20Sopenharmony_ci	if (tool->context_switch == NULL)
4888c2ecf20Sopenharmony_ci		tool->context_switch = perf_event__process_switch;
4898c2ecf20Sopenharmony_ci	if (tool->ksymbol == NULL)
4908c2ecf20Sopenharmony_ci		tool->ksymbol = perf_event__process_ksymbol;
4918c2ecf20Sopenharmony_ci	if (tool->bpf == NULL)
4928c2ecf20Sopenharmony_ci		tool->bpf = perf_event__process_bpf;
4938c2ecf20Sopenharmony_ci	if (tool->text_poke == NULL)
4948c2ecf20Sopenharmony_ci		tool->text_poke = perf_event__process_text_poke;
4958c2ecf20Sopenharmony_ci	if (tool->read == NULL)
4968c2ecf20Sopenharmony_ci		tool->read = process_event_sample_stub;
4978c2ecf20Sopenharmony_ci	if (tool->throttle == NULL)
4988c2ecf20Sopenharmony_ci		tool->throttle = process_event_stub;
4998c2ecf20Sopenharmony_ci	if (tool->unthrottle == NULL)
5008c2ecf20Sopenharmony_ci		tool->unthrottle = process_event_stub;
5018c2ecf20Sopenharmony_ci	if (tool->attr == NULL)
5028c2ecf20Sopenharmony_ci		tool->attr = process_event_synth_attr_stub;
5038c2ecf20Sopenharmony_ci	if (tool->event_update == NULL)
5048c2ecf20Sopenharmony_ci		tool->event_update = process_event_synth_event_update_stub;
5058c2ecf20Sopenharmony_ci	if (tool->tracing_data == NULL)
5068c2ecf20Sopenharmony_ci		tool->tracing_data = process_event_synth_tracing_data_stub;
5078c2ecf20Sopenharmony_ci	if (tool->build_id == NULL)
5088c2ecf20Sopenharmony_ci		tool->build_id = process_event_op2_stub;
5098c2ecf20Sopenharmony_ci	if (tool->finished_round == NULL) {
5108c2ecf20Sopenharmony_ci		if (tool->ordered_events)
5118c2ecf20Sopenharmony_ci			tool->finished_round = process_finished_round;
5128c2ecf20Sopenharmony_ci		else
5138c2ecf20Sopenharmony_ci			tool->finished_round = process_finished_round_stub;
5148c2ecf20Sopenharmony_ci	}
5158c2ecf20Sopenharmony_ci	if (tool->id_index == NULL)
5168c2ecf20Sopenharmony_ci		tool->id_index = process_event_op2_stub;
5178c2ecf20Sopenharmony_ci	if (tool->auxtrace_info == NULL)
5188c2ecf20Sopenharmony_ci		tool->auxtrace_info = process_event_op2_stub;
5198c2ecf20Sopenharmony_ci	if (tool->auxtrace == NULL)
5208c2ecf20Sopenharmony_ci		tool->auxtrace = process_event_auxtrace_stub;
5218c2ecf20Sopenharmony_ci	if (tool->auxtrace_error == NULL)
5228c2ecf20Sopenharmony_ci		tool->auxtrace_error = process_event_op2_stub;
5238c2ecf20Sopenharmony_ci	if (tool->thread_map == NULL)
5248c2ecf20Sopenharmony_ci		tool->thread_map = process_event_thread_map_stub;
5258c2ecf20Sopenharmony_ci	if (tool->cpu_map == NULL)
5268c2ecf20Sopenharmony_ci		tool->cpu_map = process_event_cpu_map_stub;
5278c2ecf20Sopenharmony_ci	if (tool->stat_config == NULL)
5288c2ecf20Sopenharmony_ci		tool->stat_config = process_event_stat_config_stub;
5298c2ecf20Sopenharmony_ci	if (tool->stat == NULL)
5308c2ecf20Sopenharmony_ci		tool->stat = process_stat_stub;
5318c2ecf20Sopenharmony_ci	if (tool->stat_round == NULL)
5328c2ecf20Sopenharmony_ci		tool->stat_round = process_stat_round_stub;
5338c2ecf20Sopenharmony_ci	if (tool->time_conv == NULL)
5348c2ecf20Sopenharmony_ci		tool->time_conv = process_event_op2_stub;
5358c2ecf20Sopenharmony_ci	if (tool->feature == NULL)
5368c2ecf20Sopenharmony_ci		tool->feature = process_event_op2_stub;
5378c2ecf20Sopenharmony_ci	if (tool->compressed == NULL)
5388c2ecf20Sopenharmony_ci		tool->compressed = perf_session__process_compressed_event;
5398c2ecf20Sopenharmony_ci}
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_cistatic void swap_sample_id_all(union perf_event *event, void *data)
5428c2ecf20Sopenharmony_ci{
5438c2ecf20Sopenharmony_ci	void *end = (void *) event + event->header.size;
5448c2ecf20Sopenharmony_ci	int size = end - data;
5458c2ecf20Sopenharmony_ci
5468c2ecf20Sopenharmony_ci	BUG_ON(size % sizeof(u64));
5478c2ecf20Sopenharmony_ci	mem_bswap_64(data, size);
5488c2ecf20Sopenharmony_ci}
5498c2ecf20Sopenharmony_ci
5508c2ecf20Sopenharmony_cistatic void perf_event__all64_swap(union perf_event *event,
5518c2ecf20Sopenharmony_ci				   bool sample_id_all __maybe_unused)
5528c2ecf20Sopenharmony_ci{
5538c2ecf20Sopenharmony_ci	struct perf_event_header *hdr = &event->header;
5548c2ecf20Sopenharmony_ci	mem_bswap_64(hdr + 1, event->header.size - sizeof(*hdr));
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_cistatic void perf_event__comm_swap(union perf_event *event, bool sample_id_all)
5588c2ecf20Sopenharmony_ci{
5598c2ecf20Sopenharmony_ci	event->comm.pid = bswap_32(event->comm.pid);
5608c2ecf20Sopenharmony_ci	event->comm.tid = bswap_32(event->comm.tid);
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	if (sample_id_all) {
5638c2ecf20Sopenharmony_ci		void *data = &event->comm.comm;
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
5668c2ecf20Sopenharmony_ci		swap_sample_id_all(event, data);
5678c2ecf20Sopenharmony_ci	}
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic void perf_event__mmap_swap(union perf_event *event,
5718c2ecf20Sopenharmony_ci				  bool sample_id_all)
5728c2ecf20Sopenharmony_ci{
5738c2ecf20Sopenharmony_ci	event->mmap.pid	  = bswap_32(event->mmap.pid);
5748c2ecf20Sopenharmony_ci	event->mmap.tid	  = bswap_32(event->mmap.tid);
5758c2ecf20Sopenharmony_ci	event->mmap.start = bswap_64(event->mmap.start);
5768c2ecf20Sopenharmony_ci	event->mmap.len	  = bswap_64(event->mmap.len);
5778c2ecf20Sopenharmony_ci	event->mmap.pgoff = bswap_64(event->mmap.pgoff);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	if (sample_id_all) {
5808c2ecf20Sopenharmony_ci		void *data = &event->mmap.filename;
5818c2ecf20Sopenharmony_ci
5828c2ecf20Sopenharmony_ci		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
5838c2ecf20Sopenharmony_ci		swap_sample_id_all(event, data);
5848c2ecf20Sopenharmony_ci	}
5858c2ecf20Sopenharmony_ci}
5868c2ecf20Sopenharmony_ci
5878c2ecf20Sopenharmony_cistatic void perf_event__mmap2_swap(union perf_event *event,
5888c2ecf20Sopenharmony_ci				  bool sample_id_all)
5898c2ecf20Sopenharmony_ci{
5908c2ecf20Sopenharmony_ci	event->mmap2.pid   = bswap_32(event->mmap2.pid);
5918c2ecf20Sopenharmony_ci	event->mmap2.tid   = bswap_32(event->mmap2.tid);
5928c2ecf20Sopenharmony_ci	event->mmap2.start = bswap_64(event->mmap2.start);
5938c2ecf20Sopenharmony_ci	event->mmap2.len   = bswap_64(event->mmap2.len);
5948c2ecf20Sopenharmony_ci	event->mmap2.pgoff = bswap_64(event->mmap2.pgoff);
5958c2ecf20Sopenharmony_ci	event->mmap2.maj   = bswap_32(event->mmap2.maj);
5968c2ecf20Sopenharmony_ci	event->mmap2.min   = bswap_32(event->mmap2.min);
5978c2ecf20Sopenharmony_ci	event->mmap2.ino   = bswap_64(event->mmap2.ino);
5988c2ecf20Sopenharmony_ci	event->mmap2.ino_generation = bswap_64(event->mmap2.ino_generation);
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_ci	if (sample_id_all) {
6018c2ecf20Sopenharmony_ci		void *data = &event->mmap2.filename;
6028c2ecf20Sopenharmony_ci
6038c2ecf20Sopenharmony_ci		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
6048c2ecf20Sopenharmony_ci		swap_sample_id_all(event, data);
6058c2ecf20Sopenharmony_ci	}
6068c2ecf20Sopenharmony_ci}
6078c2ecf20Sopenharmony_cistatic void perf_event__task_swap(union perf_event *event, bool sample_id_all)
6088c2ecf20Sopenharmony_ci{
6098c2ecf20Sopenharmony_ci	event->fork.pid	 = bswap_32(event->fork.pid);
6108c2ecf20Sopenharmony_ci	event->fork.tid	 = bswap_32(event->fork.tid);
6118c2ecf20Sopenharmony_ci	event->fork.ppid = bswap_32(event->fork.ppid);
6128c2ecf20Sopenharmony_ci	event->fork.ptid = bswap_32(event->fork.ptid);
6138c2ecf20Sopenharmony_ci	event->fork.time = bswap_64(event->fork.time);
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	if (sample_id_all)
6168c2ecf20Sopenharmony_ci		swap_sample_id_all(event, &event->fork + 1);
6178c2ecf20Sopenharmony_ci}
6188c2ecf20Sopenharmony_ci
6198c2ecf20Sopenharmony_cistatic void perf_event__read_swap(union perf_event *event, bool sample_id_all)
6208c2ecf20Sopenharmony_ci{
6218c2ecf20Sopenharmony_ci	event->read.pid		 = bswap_32(event->read.pid);
6228c2ecf20Sopenharmony_ci	event->read.tid		 = bswap_32(event->read.tid);
6238c2ecf20Sopenharmony_ci	event->read.value	 = bswap_64(event->read.value);
6248c2ecf20Sopenharmony_ci	event->read.time_enabled = bswap_64(event->read.time_enabled);
6258c2ecf20Sopenharmony_ci	event->read.time_running = bswap_64(event->read.time_running);
6268c2ecf20Sopenharmony_ci	event->read.id		 = bswap_64(event->read.id);
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	if (sample_id_all)
6298c2ecf20Sopenharmony_ci		swap_sample_id_all(event, &event->read + 1);
6308c2ecf20Sopenharmony_ci}
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_cistatic void perf_event__aux_swap(union perf_event *event, bool sample_id_all)
6338c2ecf20Sopenharmony_ci{
6348c2ecf20Sopenharmony_ci	event->aux.aux_offset = bswap_64(event->aux.aux_offset);
6358c2ecf20Sopenharmony_ci	event->aux.aux_size   = bswap_64(event->aux.aux_size);
6368c2ecf20Sopenharmony_ci	event->aux.flags      = bswap_64(event->aux.flags);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (sample_id_all)
6398c2ecf20Sopenharmony_ci		swap_sample_id_all(event, &event->aux + 1);
6408c2ecf20Sopenharmony_ci}
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic void perf_event__itrace_start_swap(union perf_event *event,
6438c2ecf20Sopenharmony_ci					  bool sample_id_all)
6448c2ecf20Sopenharmony_ci{
6458c2ecf20Sopenharmony_ci	event->itrace_start.pid	 = bswap_32(event->itrace_start.pid);
6468c2ecf20Sopenharmony_ci	event->itrace_start.tid	 = bswap_32(event->itrace_start.tid);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	if (sample_id_all)
6498c2ecf20Sopenharmony_ci		swap_sample_id_all(event, &event->itrace_start + 1);
6508c2ecf20Sopenharmony_ci}
6518c2ecf20Sopenharmony_ci
6528c2ecf20Sopenharmony_cistatic void perf_event__switch_swap(union perf_event *event, bool sample_id_all)
6538c2ecf20Sopenharmony_ci{
6548c2ecf20Sopenharmony_ci	if (event->header.type == PERF_RECORD_SWITCH_CPU_WIDE) {
6558c2ecf20Sopenharmony_ci		event->context_switch.next_prev_pid =
6568c2ecf20Sopenharmony_ci				bswap_32(event->context_switch.next_prev_pid);
6578c2ecf20Sopenharmony_ci		event->context_switch.next_prev_tid =
6588c2ecf20Sopenharmony_ci				bswap_32(event->context_switch.next_prev_tid);
6598c2ecf20Sopenharmony_ci	}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci	if (sample_id_all)
6628c2ecf20Sopenharmony_ci		swap_sample_id_all(event, &event->context_switch + 1);
6638c2ecf20Sopenharmony_ci}
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_cistatic void perf_event__text_poke_swap(union perf_event *event, bool sample_id_all)
6668c2ecf20Sopenharmony_ci{
6678c2ecf20Sopenharmony_ci	event->text_poke.addr    = bswap_64(event->text_poke.addr);
6688c2ecf20Sopenharmony_ci	event->text_poke.old_len = bswap_16(event->text_poke.old_len);
6698c2ecf20Sopenharmony_ci	event->text_poke.new_len = bswap_16(event->text_poke.new_len);
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci	if (sample_id_all) {
6728c2ecf20Sopenharmony_ci		size_t len = sizeof(event->text_poke.old_len) +
6738c2ecf20Sopenharmony_ci			     sizeof(event->text_poke.new_len) +
6748c2ecf20Sopenharmony_ci			     event->text_poke.old_len +
6758c2ecf20Sopenharmony_ci			     event->text_poke.new_len;
6768c2ecf20Sopenharmony_ci		void *data = &event->text_poke.old_len;
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci		data += PERF_ALIGN(len, sizeof(u64));
6798c2ecf20Sopenharmony_ci		swap_sample_id_all(event, data);
6808c2ecf20Sopenharmony_ci	}
6818c2ecf20Sopenharmony_ci}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_cistatic void perf_event__throttle_swap(union perf_event *event,
6848c2ecf20Sopenharmony_ci				      bool sample_id_all)
6858c2ecf20Sopenharmony_ci{
6868c2ecf20Sopenharmony_ci	event->throttle.time	  = bswap_64(event->throttle.time);
6878c2ecf20Sopenharmony_ci	event->throttle.id	  = bswap_64(event->throttle.id);
6888c2ecf20Sopenharmony_ci	event->throttle.stream_id = bswap_64(event->throttle.stream_id);
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_ci	if (sample_id_all)
6918c2ecf20Sopenharmony_ci		swap_sample_id_all(event, &event->throttle + 1);
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_cistatic void perf_event__namespaces_swap(union perf_event *event,
6958c2ecf20Sopenharmony_ci					bool sample_id_all)
6968c2ecf20Sopenharmony_ci{
6978c2ecf20Sopenharmony_ci	u64 i;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	event->namespaces.pid		= bswap_32(event->namespaces.pid);
7008c2ecf20Sopenharmony_ci	event->namespaces.tid		= bswap_32(event->namespaces.tid);
7018c2ecf20Sopenharmony_ci	event->namespaces.nr_namespaces	= bswap_64(event->namespaces.nr_namespaces);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	for (i = 0; i < event->namespaces.nr_namespaces; i++) {
7048c2ecf20Sopenharmony_ci		struct perf_ns_link_info *ns = &event->namespaces.link_info[i];
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci		ns->dev = bswap_64(ns->dev);
7078c2ecf20Sopenharmony_ci		ns->ino = bswap_64(ns->ino);
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	if (sample_id_all)
7118c2ecf20Sopenharmony_ci		swap_sample_id_all(event, &event->namespaces.link_info[i]);
7128c2ecf20Sopenharmony_ci}
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_cistatic void perf_event__cgroup_swap(union perf_event *event, bool sample_id_all)
7158c2ecf20Sopenharmony_ci{
7168c2ecf20Sopenharmony_ci	event->cgroup.id = bswap_64(event->cgroup.id);
7178c2ecf20Sopenharmony_ci
7188c2ecf20Sopenharmony_ci	if (sample_id_all) {
7198c2ecf20Sopenharmony_ci		void *data = &event->cgroup.path;
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci		data += PERF_ALIGN(strlen(data) + 1, sizeof(u64));
7228c2ecf20Sopenharmony_ci		swap_sample_id_all(event, data);
7238c2ecf20Sopenharmony_ci	}
7248c2ecf20Sopenharmony_ci}
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_cistatic u8 revbyte(u8 b)
7278c2ecf20Sopenharmony_ci{
7288c2ecf20Sopenharmony_ci	int rev = (b >> 4) | ((b & 0xf) << 4);
7298c2ecf20Sopenharmony_ci	rev = ((rev & 0xcc) >> 2) | ((rev & 0x33) << 2);
7308c2ecf20Sopenharmony_ci	rev = ((rev & 0xaa) >> 1) | ((rev & 0x55) << 1);
7318c2ecf20Sopenharmony_ci	return (u8) rev;
7328c2ecf20Sopenharmony_ci}
7338c2ecf20Sopenharmony_ci
7348c2ecf20Sopenharmony_ci/*
7358c2ecf20Sopenharmony_ci * XXX this is hack in attempt to carry flags bitfield
7368c2ecf20Sopenharmony_ci * through endian village. ABI says:
7378c2ecf20Sopenharmony_ci *
7388c2ecf20Sopenharmony_ci * Bit-fields are allocated from right to left (least to most significant)
7398c2ecf20Sopenharmony_ci * on little-endian implementations and from left to right (most to least
7408c2ecf20Sopenharmony_ci * significant) on big-endian implementations.
7418c2ecf20Sopenharmony_ci *
7428c2ecf20Sopenharmony_ci * The above seems to be byte specific, so we need to reverse each
7438c2ecf20Sopenharmony_ci * byte of the bitfield. 'Internet' also says this might be implementation
7448c2ecf20Sopenharmony_ci * specific and we probably need proper fix and carry perf_event_attr
7458c2ecf20Sopenharmony_ci * bitfield flags in separate data file FEAT_ section. Thought this seems
7468c2ecf20Sopenharmony_ci * to work for now.
7478c2ecf20Sopenharmony_ci */
7488c2ecf20Sopenharmony_cistatic void swap_bitfield(u8 *p, unsigned len)
7498c2ecf20Sopenharmony_ci{
7508c2ecf20Sopenharmony_ci	unsigned i;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
7538c2ecf20Sopenharmony_ci		*p = revbyte(*p);
7548c2ecf20Sopenharmony_ci		p++;
7558c2ecf20Sopenharmony_ci	}
7568c2ecf20Sopenharmony_ci}
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci/* exported for swapping attributes in file header */
7598c2ecf20Sopenharmony_civoid perf_event__attr_swap(struct perf_event_attr *attr)
7608c2ecf20Sopenharmony_ci{
7618c2ecf20Sopenharmony_ci	attr->type		= bswap_32(attr->type);
7628c2ecf20Sopenharmony_ci	attr->size		= bswap_32(attr->size);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci#define bswap_safe(f, n) 					\
7658c2ecf20Sopenharmony_ci	(attr->size > (offsetof(struct perf_event_attr, f) + 	\
7668c2ecf20Sopenharmony_ci		       sizeof(attr->f) * (n)))
7678c2ecf20Sopenharmony_ci#define bswap_field(f, sz) 			\
7688c2ecf20Sopenharmony_cido { 						\
7698c2ecf20Sopenharmony_ci	if (bswap_safe(f, 0))			\
7708c2ecf20Sopenharmony_ci		attr->f = bswap_##sz(attr->f);	\
7718c2ecf20Sopenharmony_ci} while(0)
7728c2ecf20Sopenharmony_ci#define bswap_field_16(f) bswap_field(f, 16)
7738c2ecf20Sopenharmony_ci#define bswap_field_32(f) bswap_field(f, 32)
7748c2ecf20Sopenharmony_ci#define bswap_field_64(f) bswap_field(f, 64)
7758c2ecf20Sopenharmony_ci
7768c2ecf20Sopenharmony_ci	bswap_field_64(config);
7778c2ecf20Sopenharmony_ci	bswap_field_64(sample_period);
7788c2ecf20Sopenharmony_ci	bswap_field_64(sample_type);
7798c2ecf20Sopenharmony_ci	bswap_field_64(read_format);
7808c2ecf20Sopenharmony_ci	bswap_field_32(wakeup_events);
7818c2ecf20Sopenharmony_ci	bswap_field_32(bp_type);
7828c2ecf20Sopenharmony_ci	bswap_field_64(bp_addr);
7838c2ecf20Sopenharmony_ci	bswap_field_64(bp_len);
7848c2ecf20Sopenharmony_ci	bswap_field_64(branch_sample_type);
7858c2ecf20Sopenharmony_ci	bswap_field_64(sample_regs_user);
7868c2ecf20Sopenharmony_ci	bswap_field_32(sample_stack_user);
7878c2ecf20Sopenharmony_ci	bswap_field_32(aux_watermark);
7888c2ecf20Sopenharmony_ci	bswap_field_16(sample_max_stack);
7898c2ecf20Sopenharmony_ci	bswap_field_32(aux_sample_size);
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	/*
7928c2ecf20Sopenharmony_ci	 * After read_format are bitfields. Check read_format because
7938c2ecf20Sopenharmony_ci	 * we are unable to use offsetof on bitfield.
7948c2ecf20Sopenharmony_ci	 */
7958c2ecf20Sopenharmony_ci	if (bswap_safe(read_format, 1))
7968c2ecf20Sopenharmony_ci		swap_bitfield((u8 *) (&attr->read_format + 1),
7978c2ecf20Sopenharmony_ci			      sizeof(u64));
7988c2ecf20Sopenharmony_ci#undef bswap_field_64
7998c2ecf20Sopenharmony_ci#undef bswap_field_32
8008c2ecf20Sopenharmony_ci#undef bswap_field
8018c2ecf20Sopenharmony_ci#undef bswap_safe
8028c2ecf20Sopenharmony_ci}
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_cistatic void perf_event__hdr_attr_swap(union perf_event *event,
8058c2ecf20Sopenharmony_ci				      bool sample_id_all __maybe_unused)
8068c2ecf20Sopenharmony_ci{
8078c2ecf20Sopenharmony_ci	size_t size;
8088c2ecf20Sopenharmony_ci
8098c2ecf20Sopenharmony_ci	perf_event__attr_swap(&event->attr.attr);
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci	size = event->header.size;
8128c2ecf20Sopenharmony_ci	size -= (void *)&event->attr.id - (void *)event;
8138c2ecf20Sopenharmony_ci	mem_bswap_64(event->attr.id, size);
8148c2ecf20Sopenharmony_ci}
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_cistatic void perf_event__event_update_swap(union perf_event *event,
8178c2ecf20Sopenharmony_ci					  bool sample_id_all __maybe_unused)
8188c2ecf20Sopenharmony_ci{
8198c2ecf20Sopenharmony_ci	event->event_update.type = bswap_64(event->event_update.type);
8208c2ecf20Sopenharmony_ci	event->event_update.id   = bswap_64(event->event_update.id);
8218c2ecf20Sopenharmony_ci}
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_cistatic void perf_event__event_type_swap(union perf_event *event,
8248c2ecf20Sopenharmony_ci					bool sample_id_all __maybe_unused)
8258c2ecf20Sopenharmony_ci{
8268c2ecf20Sopenharmony_ci	event->event_type.event_type.event_id =
8278c2ecf20Sopenharmony_ci		bswap_64(event->event_type.event_type.event_id);
8288c2ecf20Sopenharmony_ci}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_cistatic void perf_event__tracing_data_swap(union perf_event *event,
8318c2ecf20Sopenharmony_ci					  bool sample_id_all __maybe_unused)
8328c2ecf20Sopenharmony_ci{
8338c2ecf20Sopenharmony_ci	event->tracing_data.size = bswap_32(event->tracing_data.size);
8348c2ecf20Sopenharmony_ci}
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_cistatic void perf_event__auxtrace_info_swap(union perf_event *event,
8378c2ecf20Sopenharmony_ci					   bool sample_id_all __maybe_unused)
8388c2ecf20Sopenharmony_ci{
8398c2ecf20Sopenharmony_ci	size_t size;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	event->auxtrace_info.type = bswap_32(event->auxtrace_info.type);
8428c2ecf20Sopenharmony_ci
8438c2ecf20Sopenharmony_ci	size = event->header.size;
8448c2ecf20Sopenharmony_ci	size -= (void *)&event->auxtrace_info.priv - (void *)event;
8458c2ecf20Sopenharmony_ci	mem_bswap_64(event->auxtrace_info.priv, size);
8468c2ecf20Sopenharmony_ci}
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_cistatic void perf_event__auxtrace_swap(union perf_event *event,
8498c2ecf20Sopenharmony_ci				      bool sample_id_all __maybe_unused)
8508c2ecf20Sopenharmony_ci{
8518c2ecf20Sopenharmony_ci	event->auxtrace.size      = bswap_64(event->auxtrace.size);
8528c2ecf20Sopenharmony_ci	event->auxtrace.offset    = bswap_64(event->auxtrace.offset);
8538c2ecf20Sopenharmony_ci	event->auxtrace.reference = bswap_64(event->auxtrace.reference);
8548c2ecf20Sopenharmony_ci	event->auxtrace.idx       = bswap_32(event->auxtrace.idx);
8558c2ecf20Sopenharmony_ci	event->auxtrace.tid       = bswap_32(event->auxtrace.tid);
8568c2ecf20Sopenharmony_ci	event->auxtrace.cpu       = bswap_32(event->auxtrace.cpu);
8578c2ecf20Sopenharmony_ci}
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_cistatic void perf_event__auxtrace_error_swap(union perf_event *event,
8608c2ecf20Sopenharmony_ci					    bool sample_id_all __maybe_unused)
8618c2ecf20Sopenharmony_ci{
8628c2ecf20Sopenharmony_ci	event->auxtrace_error.type = bswap_32(event->auxtrace_error.type);
8638c2ecf20Sopenharmony_ci	event->auxtrace_error.code = bswap_32(event->auxtrace_error.code);
8648c2ecf20Sopenharmony_ci	event->auxtrace_error.cpu  = bswap_32(event->auxtrace_error.cpu);
8658c2ecf20Sopenharmony_ci	event->auxtrace_error.pid  = bswap_32(event->auxtrace_error.pid);
8668c2ecf20Sopenharmony_ci	event->auxtrace_error.tid  = bswap_32(event->auxtrace_error.tid);
8678c2ecf20Sopenharmony_ci	event->auxtrace_error.fmt  = bswap_32(event->auxtrace_error.fmt);
8688c2ecf20Sopenharmony_ci	event->auxtrace_error.ip   = bswap_64(event->auxtrace_error.ip);
8698c2ecf20Sopenharmony_ci	if (event->auxtrace_error.fmt)
8708c2ecf20Sopenharmony_ci		event->auxtrace_error.time = bswap_64(event->auxtrace_error.time);
8718c2ecf20Sopenharmony_ci}
8728c2ecf20Sopenharmony_ci
8738c2ecf20Sopenharmony_cistatic void perf_event__thread_map_swap(union perf_event *event,
8748c2ecf20Sopenharmony_ci					bool sample_id_all __maybe_unused)
8758c2ecf20Sopenharmony_ci{
8768c2ecf20Sopenharmony_ci	unsigned i;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	event->thread_map.nr = bswap_64(event->thread_map.nr);
8798c2ecf20Sopenharmony_ci
8808c2ecf20Sopenharmony_ci	for (i = 0; i < event->thread_map.nr; i++)
8818c2ecf20Sopenharmony_ci		event->thread_map.entries[i].pid = bswap_64(event->thread_map.entries[i].pid);
8828c2ecf20Sopenharmony_ci}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_cistatic void perf_event__cpu_map_swap(union perf_event *event,
8858c2ecf20Sopenharmony_ci				     bool sample_id_all __maybe_unused)
8868c2ecf20Sopenharmony_ci{
8878c2ecf20Sopenharmony_ci	struct perf_record_cpu_map_data *data = &event->cpu_map.data;
8888c2ecf20Sopenharmony_ci	struct cpu_map_entries *cpus;
8898c2ecf20Sopenharmony_ci	struct perf_record_record_cpu_map *mask;
8908c2ecf20Sopenharmony_ci	unsigned i;
8918c2ecf20Sopenharmony_ci
8928c2ecf20Sopenharmony_ci	data->type = bswap_64(data->type);
8938c2ecf20Sopenharmony_ci
8948c2ecf20Sopenharmony_ci	switch (data->type) {
8958c2ecf20Sopenharmony_ci	case PERF_CPU_MAP__CPUS:
8968c2ecf20Sopenharmony_ci		cpus = (struct cpu_map_entries *)data->data;
8978c2ecf20Sopenharmony_ci
8988c2ecf20Sopenharmony_ci		cpus->nr = bswap_16(cpus->nr);
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci		for (i = 0; i < cpus->nr; i++)
9018c2ecf20Sopenharmony_ci			cpus->cpu[i] = bswap_16(cpus->cpu[i]);
9028c2ecf20Sopenharmony_ci		break;
9038c2ecf20Sopenharmony_ci	case PERF_CPU_MAP__MASK:
9048c2ecf20Sopenharmony_ci		mask = (struct perf_record_record_cpu_map *)data->data;
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci		mask->nr = bswap_16(mask->nr);
9078c2ecf20Sopenharmony_ci		mask->long_size = bswap_16(mask->long_size);
9088c2ecf20Sopenharmony_ci
9098c2ecf20Sopenharmony_ci		switch (mask->long_size) {
9108c2ecf20Sopenharmony_ci		case 4: mem_bswap_32(&mask->mask, mask->nr); break;
9118c2ecf20Sopenharmony_ci		case 8: mem_bswap_64(&mask->mask, mask->nr); break;
9128c2ecf20Sopenharmony_ci		default:
9138c2ecf20Sopenharmony_ci			pr_err("cpu_map swap: unsupported long size\n");
9148c2ecf20Sopenharmony_ci		}
9158c2ecf20Sopenharmony_ci	default:
9168c2ecf20Sopenharmony_ci		break;
9178c2ecf20Sopenharmony_ci	}
9188c2ecf20Sopenharmony_ci}
9198c2ecf20Sopenharmony_ci
9208c2ecf20Sopenharmony_cistatic void perf_event__stat_config_swap(union perf_event *event,
9218c2ecf20Sopenharmony_ci					 bool sample_id_all __maybe_unused)
9228c2ecf20Sopenharmony_ci{
9238c2ecf20Sopenharmony_ci	u64 size;
9248c2ecf20Sopenharmony_ci
9258c2ecf20Sopenharmony_ci	size  = event->stat_config.nr * sizeof(event->stat_config.data[0]);
9268c2ecf20Sopenharmony_ci	size += 1; /* nr item itself */
9278c2ecf20Sopenharmony_ci	mem_bswap_64(&event->stat_config.nr, size);
9288c2ecf20Sopenharmony_ci}
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_cistatic void perf_event__stat_swap(union perf_event *event,
9318c2ecf20Sopenharmony_ci				  bool sample_id_all __maybe_unused)
9328c2ecf20Sopenharmony_ci{
9338c2ecf20Sopenharmony_ci	event->stat.id     = bswap_64(event->stat.id);
9348c2ecf20Sopenharmony_ci	event->stat.thread = bswap_32(event->stat.thread);
9358c2ecf20Sopenharmony_ci	event->stat.cpu    = bswap_32(event->stat.cpu);
9368c2ecf20Sopenharmony_ci	event->stat.val    = bswap_64(event->stat.val);
9378c2ecf20Sopenharmony_ci	event->stat.ena    = bswap_64(event->stat.ena);
9388c2ecf20Sopenharmony_ci	event->stat.run    = bswap_64(event->stat.run);
9398c2ecf20Sopenharmony_ci}
9408c2ecf20Sopenharmony_ci
9418c2ecf20Sopenharmony_cistatic void perf_event__stat_round_swap(union perf_event *event,
9428c2ecf20Sopenharmony_ci					bool sample_id_all __maybe_unused)
9438c2ecf20Sopenharmony_ci{
9448c2ecf20Sopenharmony_ci	event->stat_round.type = bswap_64(event->stat_round.type);
9458c2ecf20Sopenharmony_ci	event->stat_round.time = bswap_64(event->stat_round.time);
9468c2ecf20Sopenharmony_ci}
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_cistatic void perf_event__time_conv_swap(union perf_event *event,
9498c2ecf20Sopenharmony_ci				       bool sample_id_all __maybe_unused)
9508c2ecf20Sopenharmony_ci{
9518c2ecf20Sopenharmony_ci	event->time_conv.time_shift = bswap_64(event->time_conv.time_shift);
9528c2ecf20Sopenharmony_ci	event->time_conv.time_mult  = bswap_64(event->time_conv.time_mult);
9538c2ecf20Sopenharmony_ci	event->time_conv.time_zero  = bswap_64(event->time_conv.time_zero);
9548c2ecf20Sopenharmony_ci
9558c2ecf20Sopenharmony_ci	if (event_contains(event->time_conv, time_cycles)) {
9568c2ecf20Sopenharmony_ci		event->time_conv.time_cycles = bswap_64(event->time_conv.time_cycles);
9578c2ecf20Sopenharmony_ci		event->time_conv.time_mask = bswap_64(event->time_conv.time_mask);
9588c2ecf20Sopenharmony_ci	}
9598c2ecf20Sopenharmony_ci}
9608c2ecf20Sopenharmony_ci
9618c2ecf20Sopenharmony_citypedef void (*perf_event__swap_op)(union perf_event *event,
9628c2ecf20Sopenharmony_ci				    bool sample_id_all);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_cistatic perf_event__swap_op perf_event__swap_ops[] = {
9658c2ecf20Sopenharmony_ci	[PERF_RECORD_MMAP]		  = perf_event__mmap_swap,
9668c2ecf20Sopenharmony_ci	[PERF_RECORD_MMAP2]		  = perf_event__mmap2_swap,
9678c2ecf20Sopenharmony_ci	[PERF_RECORD_COMM]		  = perf_event__comm_swap,
9688c2ecf20Sopenharmony_ci	[PERF_RECORD_FORK]		  = perf_event__task_swap,
9698c2ecf20Sopenharmony_ci	[PERF_RECORD_EXIT]		  = perf_event__task_swap,
9708c2ecf20Sopenharmony_ci	[PERF_RECORD_LOST]		  = perf_event__all64_swap,
9718c2ecf20Sopenharmony_ci	[PERF_RECORD_READ]		  = perf_event__read_swap,
9728c2ecf20Sopenharmony_ci	[PERF_RECORD_THROTTLE]		  = perf_event__throttle_swap,
9738c2ecf20Sopenharmony_ci	[PERF_RECORD_UNTHROTTLE]	  = perf_event__throttle_swap,
9748c2ecf20Sopenharmony_ci	[PERF_RECORD_SAMPLE]		  = perf_event__all64_swap,
9758c2ecf20Sopenharmony_ci	[PERF_RECORD_AUX]		  = perf_event__aux_swap,
9768c2ecf20Sopenharmony_ci	[PERF_RECORD_ITRACE_START]	  = perf_event__itrace_start_swap,
9778c2ecf20Sopenharmony_ci	[PERF_RECORD_LOST_SAMPLES]	  = perf_event__all64_swap,
9788c2ecf20Sopenharmony_ci	[PERF_RECORD_SWITCH]		  = perf_event__switch_swap,
9798c2ecf20Sopenharmony_ci	[PERF_RECORD_SWITCH_CPU_WIDE]	  = perf_event__switch_swap,
9808c2ecf20Sopenharmony_ci	[PERF_RECORD_NAMESPACES]	  = perf_event__namespaces_swap,
9818c2ecf20Sopenharmony_ci	[PERF_RECORD_CGROUP]		  = perf_event__cgroup_swap,
9828c2ecf20Sopenharmony_ci	[PERF_RECORD_TEXT_POKE]		  = perf_event__text_poke_swap,
9838c2ecf20Sopenharmony_ci	[PERF_RECORD_HEADER_ATTR]	  = perf_event__hdr_attr_swap,
9848c2ecf20Sopenharmony_ci	[PERF_RECORD_HEADER_EVENT_TYPE]	  = perf_event__event_type_swap,
9858c2ecf20Sopenharmony_ci	[PERF_RECORD_HEADER_TRACING_DATA] = perf_event__tracing_data_swap,
9868c2ecf20Sopenharmony_ci	[PERF_RECORD_HEADER_BUILD_ID]	  = NULL,
9878c2ecf20Sopenharmony_ci	[PERF_RECORD_ID_INDEX]		  = perf_event__all64_swap,
9888c2ecf20Sopenharmony_ci	[PERF_RECORD_AUXTRACE_INFO]	  = perf_event__auxtrace_info_swap,
9898c2ecf20Sopenharmony_ci	[PERF_RECORD_AUXTRACE]		  = perf_event__auxtrace_swap,
9908c2ecf20Sopenharmony_ci	[PERF_RECORD_AUXTRACE_ERROR]	  = perf_event__auxtrace_error_swap,
9918c2ecf20Sopenharmony_ci	[PERF_RECORD_THREAD_MAP]	  = perf_event__thread_map_swap,
9928c2ecf20Sopenharmony_ci	[PERF_RECORD_CPU_MAP]		  = perf_event__cpu_map_swap,
9938c2ecf20Sopenharmony_ci	[PERF_RECORD_STAT_CONFIG]	  = perf_event__stat_config_swap,
9948c2ecf20Sopenharmony_ci	[PERF_RECORD_STAT]		  = perf_event__stat_swap,
9958c2ecf20Sopenharmony_ci	[PERF_RECORD_STAT_ROUND]	  = perf_event__stat_round_swap,
9968c2ecf20Sopenharmony_ci	[PERF_RECORD_EVENT_UPDATE]	  = perf_event__event_update_swap,
9978c2ecf20Sopenharmony_ci	[PERF_RECORD_TIME_CONV]		  = perf_event__time_conv_swap,
9988c2ecf20Sopenharmony_ci	[PERF_RECORD_HEADER_MAX]	  = NULL,
9998c2ecf20Sopenharmony_ci};
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci/*
10028c2ecf20Sopenharmony_ci * When perf record finishes a pass on every buffers, it records this pseudo
10038c2ecf20Sopenharmony_ci * event.
10048c2ecf20Sopenharmony_ci * We record the max timestamp t found in the pass n.
10058c2ecf20Sopenharmony_ci * Assuming these timestamps are monotonic across cpus, we know that if
10068c2ecf20Sopenharmony_ci * a buffer still has events with timestamps below t, they will be all
10078c2ecf20Sopenharmony_ci * available and then read in the pass n + 1.
10088c2ecf20Sopenharmony_ci * Hence when we start to read the pass n + 2, we can safely flush every
10098c2ecf20Sopenharmony_ci * events with timestamps below t.
10108c2ecf20Sopenharmony_ci *
10118c2ecf20Sopenharmony_ci *    ============ PASS n =================
10128c2ecf20Sopenharmony_ci *       CPU 0         |   CPU 1
10138c2ecf20Sopenharmony_ci *                     |
10148c2ecf20Sopenharmony_ci *    cnt1 timestamps  |   cnt2 timestamps
10158c2ecf20Sopenharmony_ci *          1          |         2
10168c2ecf20Sopenharmony_ci *          2          |         3
10178c2ecf20Sopenharmony_ci *          -          |         4  <--- max recorded
10188c2ecf20Sopenharmony_ci *
10198c2ecf20Sopenharmony_ci *    ============ PASS n + 1 ==============
10208c2ecf20Sopenharmony_ci *       CPU 0         |   CPU 1
10218c2ecf20Sopenharmony_ci *                     |
10228c2ecf20Sopenharmony_ci *    cnt1 timestamps  |   cnt2 timestamps
10238c2ecf20Sopenharmony_ci *          3          |         5
10248c2ecf20Sopenharmony_ci *          4          |         6
10258c2ecf20Sopenharmony_ci *          5          |         7 <---- max recorded
10268c2ecf20Sopenharmony_ci *
10278c2ecf20Sopenharmony_ci *      Flush every events below timestamp 4
10288c2ecf20Sopenharmony_ci *
10298c2ecf20Sopenharmony_ci *    ============ PASS n + 2 ==============
10308c2ecf20Sopenharmony_ci *       CPU 0         |   CPU 1
10318c2ecf20Sopenharmony_ci *                     |
10328c2ecf20Sopenharmony_ci *    cnt1 timestamps  |   cnt2 timestamps
10338c2ecf20Sopenharmony_ci *          6          |         8
10348c2ecf20Sopenharmony_ci *          7          |         9
10358c2ecf20Sopenharmony_ci *          -          |         10
10368c2ecf20Sopenharmony_ci *
10378c2ecf20Sopenharmony_ci *      Flush every events below timestamp 7
10388c2ecf20Sopenharmony_ci *      etc...
10398c2ecf20Sopenharmony_ci */
10408c2ecf20Sopenharmony_cistatic int process_finished_round(struct perf_tool *tool __maybe_unused,
10418c2ecf20Sopenharmony_ci				  union perf_event *event __maybe_unused,
10428c2ecf20Sopenharmony_ci				  struct ordered_events *oe)
10438c2ecf20Sopenharmony_ci{
10448c2ecf20Sopenharmony_ci	if (dump_trace)
10458c2ecf20Sopenharmony_ci		fprintf(stdout, "\n");
10468c2ecf20Sopenharmony_ci	return ordered_events__flush(oe, OE_FLUSH__ROUND);
10478c2ecf20Sopenharmony_ci}
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ciint perf_session__queue_event(struct perf_session *s, union perf_event *event,
10508c2ecf20Sopenharmony_ci			      u64 timestamp, u64 file_offset)
10518c2ecf20Sopenharmony_ci{
10528c2ecf20Sopenharmony_ci	return ordered_events__queue(&s->ordered_events, event, timestamp, file_offset);
10538c2ecf20Sopenharmony_ci}
10548c2ecf20Sopenharmony_ci
10558c2ecf20Sopenharmony_cistatic void callchain__lbr_callstack_printf(struct perf_sample *sample)
10568c2ecf20Sopenharmony_ci{
10578c2ecf20Sopenharmony_ci	struct ip_callchain *callchain = sample->callchain;
10588c2ecf20Sopenharmony_ci	struct branch_stack *lbr_stack = sample->branch_stack;
10598c2ecf20Sopenharmony_ci	struct branch_entry *entries = perf_sample__branch_entries(sample);
10608c2ecf20Sopenharmony_ci	u64 kernel_callchain_nr = callchain->nr;
10618c2ecf20Sopenharmony_ci	unsigned int i;
10628c2ecf20Sopenharmony_ci
10638c2ecf20Sopenharmony_ci	for (i = 0; i < kernel_callchain_nr; i++) {
10648c2ecf20Sopenharmony_ci		if (callchain->ips[i] == PERF_CONTEXT_USER)
10658c2ecf20Sopenharmony_ci			break;
10668c2ecf20Sopenharmony_ci	}
10678c2ecf20Sopenharmony_ci
10688c2ecf20Sopenharmony_ci	if ((i != kernel_callchain_nr) && lbr_stack->nr) {
10698c2ecf20Sopenharmony_ci		u64 total_nr;
10708c2ecf20Sopenharmony_ci		/*
10718c2ecf20Sopenharmony_ci		 * LBR callstack can only get user call chain,
10728c2ecf20Sopenharmony_ci		 * i is kernel call chain number,
10738c2ecf20Sopenharmony_ci		 * 1 is PERF_CONTEXT_USER.
10748c2ecf20Sopenharmony_ci		 *
10758c2ecf20Sopenharmony_ci		 * The user call chain is stored in LBR registers.
10768c2ecf20Sopenharmony_ci		 * LBR are pair registers. The caller is stored
10778c2ecf20Sopenharmony_ci		 * in "from" register, while the callee is stored
10788c2ecf20Sopenharmony_ci		 * in "to" register.
10798c2ecf20Sopenharmony_ci		 * For example, there is a call stack
10808c2ecf20Sopenharmony_ci		 * "A"->"B"->"C"->"D".
10818c2ecf20Sopenharmony_ci		 * The LBR registers will recorde like
10828c2ecf20Sopenharmony_ci		 * "C"->"D", "B"->"C", "A"->"B".
10838c2ecf20Sopenharmony_ci		 * So only the first "to" register and all "from"
10848c2ecf20Sopenharmony_ci		 * registers are needed to construct the whole stack.
10858c2ecf20Sopenharmony_ci		 */
10868c2ecf20Sopenharmony_ci		total_nr = i + 1 + lbr_stack->nr + 1;
10878c2ecf20Sopenharmony_ci		kernel_callchain_nr = i + 1;
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci		printf("... LBR call chain: nr:%" PRIu64 "\n", total_nr);
10908c2ecf20Sopenharmony_ci
10918c2ecf20Sopenharmony_ci		for (i = 0; i < kernel_callchain_nr; i++)
10928c2ecf20Sopenharmony_ci			printf("..... %2d: %016" PRIx64 "\n",
10938c2ecf20Sopenharmony_ci			       i, callchain->ips[i]);
10948c2ecf20Sopenharmony_ci
10958c2ecf20Sopenharmony_ci		printf("..... %2d: %016" PRIx64 "\n",
10968c2ecf20Sopenharmony_ci		       (int)(kernel_callchain_nr), entries[0].to);
10978c2ecf20Sopenharmony_ci		for (i = 0; i < lbr_stack->nr; i++)
10988c2ecf20Sopenharmony_ci			printf("..... %2d: %016" PRIx64 "\n",
10998c2ecf20Sopenharmony_ci			       (int)(i + kernel_callchain_nr + 1), entries[i].from);
11008c2ecf20Sopenharmony_ci	}
11018c2ecf20Sopenharmony_ci}
11028c2ecf20Sopenharmony_ci
11038c2ecf20Sopenharmony_cistatic void callchain__printf(struct evsel *evsel,
11048c2ecf20Sopenharmony_ci			      struct perf_sample *sample)
11058c2ecf20Sopenharmony_ci{
11068c2ecf20Sopenharmony_ci	unsigned int i;
11078c2ecf20Sopenharmony_ci	struct ip_callchain *callchain = sample->callchain;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	if (evsel__has_branch_callstack(evsel))
11108c2ecf20Sopenharmony_ci		callchain__lbr_callstack_printf(sample);
11118c2ecf20Sopenharmony_ci
11128c2ecf20Sopenharmony_ci	printf("... FP chain: nr:%" PRIu64 "\n", callchain->nr);
11138c2ecf20Sopenharmony_ci
11148c2ecf20Sopenharmony_ci	for (i = 0; i < callchain->nr; i++)
11158c2ecf20Sopenharmony_ci		printf("..... %2d: %016" PRIx64 "\n",
11168c2ecf20Sopenharmony_ci		       i, callchain->ips[i]);
11178c2ecf20Sopenharmony_ci}
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_cistatic void branch_stack__printf(struct perf_sample *sample, bool callstack)
11208c2ecf20Sopenharmony_ci{
11218c2ecf20Sopenharmony_ci	struct branch_entry *entries = perf_sample__branch_entries(sample);
11228c2ecf20Sopenharmony_ci	uint64_t i;
11238c2ecf20Sopenharmony_ci
11248c2ecf20Sopenharmony_ci	printf("%s: nr:%" PRIu64 "\n",
11258c2ecf20Sopenharmony_ci		!callstack ? "... branch stack" : "... branch callstack",
11268c2ecf20Sopenharmony_ci		sample->branch_stack->nr);
11278c2ecf20Sopenharmony_ci
11288c2ecf20Sopenharmony_ci	for (i = 0; i < sample->branch_stack->nr; i++) {
11298c2ecf20Sopenharmony_ci		struct branch_entry *e = &entries[i];
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci		if (!callstack) {
11328c2ecf20Sopenharmony_ci			printf("..... %2"PRIu64": %016" PRIx64 " -> %016" PRIx64 " %hu cycles %s%s%s%s %x\n",
11338c2ecf20Sopenharmony_ci				i, e->from, e->to,
11348c2ecf20Sopenharmony_ci				(unsigned short)e->flags.cycles,
11358c2ecf20Sopenharmony_ci				e->flags.mispred ? "M" : " ",
11368c2ecf20Sopenharmony_ci				e->flags.predicted ? "P" : " ",
11378c2ecf20Sopenharmony_ci				e->flags.abort ? "A" : " ",
11388c2ecf20Sopenharmony_ci				e->flags.in_tx ? "T" : " ",
11398c2ecf20Sopenharmony_ci				(unsigned)e->flags.reserved);
11408c2ecf20Sopenharmony_ci		} else {
11418c2ecf20Sopenharmony_ci			printf("..... %2"PRIu64": %016" PRIx64 "\n",
11428c2ecf20Sopenharmony_ci				i, i > 0 ? e->from : e->to);
11438c2ecf20Sopenharmony_ci		}
11448c2ecf20Sopenharmony_ci	}
11458c2ecf20Sopenharmony_ci}
11468c2ecf20Sopenharmony_ci
11478c2ecf20Sopenharmony_cistatic void regs_dump__printf(u64 mask, u64 *regs)
11488c2ecf20Sopenharmony_ci{
11498c2ecf20Sopenharmony_ci	unsigned rid, i = 0;
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	for_each_set_bit(rid, (unsigned long *) &mask, sizeof(mask) * 8) {
11528c2ecf20Sopenharmony_ci		u64 val = regs[i++];
11538c2ecf20Sopenharmony_ci
11548c2ecf20Sopenharmony_ci		printf(".... %-5s 0x%016" PRIx64 "\n",
11558c2ecf20Sopenharmony_ci		       perf_reg_name(rid), val);
11568c2ecf20Sopenharmony_ci	}
11578c2ecf20Sopenharmony_ci}
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_cistatic const char *regs_abi[] = {
11608c2ecf20Sopenharmony_ci	[PERF_SAMPLE_REGS_ABI_NONE] = "none",
11618c2ecf20Sopenharmony_ci	[PERF_SAMPLE_REGS_ABI_32] = "32-bit",
11628c2ecf20Sopenharmony_ci	[PERF_SAMPLE_REGS_ABI_64] = "64-bit",
11638c2ecf20Sopenharmony_ci};
11648c2ecf20Sopenharmony_ci
11658c2ecf20Sopenharmony_cistatic inline const char *regs_dump_abi(struct regs_dump *d)
11668c2ecf20Sopenharmony_ci{
11678c2ecf20Sopenharmony_ci	if (d->abi > PERF_SAMPLE_REGS_ABI_64)
11688c2ecf20Sopenharmony_ci		return "unknown";
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	return regs_abi[d->abi];
11718c2ecf20Sopenharmony_ci}
11728c2ecf20Sopenharmony_ci
11738c2ecf20Sopenharmony_cistatic void regs__printf(const char *type, struct regs_dump *regs)
11748c2ecf20Sopenharmony_ci{
11758c2ecf20Sopenharmony_ci	u64 mask = regs->mask;
11768c2ecf20Sopenharmony_ci
11778c2ecf20Sopenharmony_ci	printf("... %s regs: mask 0x%" PRIx64 " ABI %s\n",
11788c2ecf20Sopenharmony_ci	       type,
11798c2ecf20Sopenharmony_ci	       mask,
11808c2ecf20Sopenharmony_ci	       regs_dump_abi(regs));
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	regs_dump__printf(mask, regs->regs);
11838c2ecf20Sopenharmony_ci}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_cistatic void regs_user__printf(struct perf_sample *sample)
11868c2ecf20Sopenharmony_ci{
11878c2ecf20Sopenharmony_ci	struct regs_dump *user_regs = &sample->user_regs;
11888c2ecf20Sopenharmony_ci
11898c2ecf20Sopenharmony_ci	if (user_regs->regs)
11908c2ecf20Sopenharmony_ci		regs__printf("user", user_regs);
11918c2ecf20Sopenharmony_ci}
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_cistatic void regs_intr__printf(struct perf_sample *sample)
11948c2ecf20Sopenharmony_ci{
11958c2ecf20Sopenharmony_ci	struct regs_dump *intr_regs = &sample->intr_regs;
11968c2ecf20Sopenharmony_ci
11978c2ecf20Sopenharmony_ci	if (intr_regs->regs)
11988c2ecf20Sopenharmony_ci		regs__printf("intr", intr_regs);
11998c2ecf20Sopenharmony_ci}
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_cistatic void stack_user__printf(struct stack_dump *dump)
12028c2ecf20Sopenharmony_ci{
12038c2ecf20Sopenharmony_ci	printf("... ustack: size %" PRIu64 ", offset 0x%x\n",
12048c2ecf20Sopenharmony_ci	       dump->size, dump->offset);
12058c2ecf20Sopenharmony_ci}
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_cistatic void perf_evlist__print_tstamp(struct evlist *evlist,
12088c2ecf20Sopenharmony_ci				       union perf_event *event,
12098c2ecf20Sopenharmony_ci				       struct perf_sample *sample)
12108c2ecf20Sopenharmony_ci{
12118c2ecf20Sopenharmony_ci	u64 sample_type = __evlist__combined_sample_type(evlist);
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	if (event->header.type != PERF_RECORD_SAMPLE &&
12148c2ecf20Sopenharmony_ci	    !evlist__sample_id_all(evlist)) {
12158c2ecf20Sopenharmony_ci		fputs("-1 -1 ", stdout);
12168c2ecf20Sopenharmony_ci		return;
12178c2ecf20Sopenharmony_ci	}
12188c2ecf20Sopenharmony_ci
12198c2ecf20Sopenharmony_ci	if ((sample_type & PERF_SAMPLE_CPU))
12208c2ecf20Sopenharmony_ci		printf("%u ", sample->cpu);
12218c2ecf20Sopenharmony_ci
12228c2ecf20Sopenharmony_ci	if (sample_type & PERF_SAMPLE_TIME)
12238c2ecf20Sopenharmony_ci		printf("%" PRIu64 " ", sample->time);
12248c2ecf20Sopenharmony_ci}
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_cistatic void sample_read__printf(struct perf_sample *sample, u64 read_format)
12278c2ecf20Sopenharmony_ci{
12288c2ecf20Sopenharmony_ci	printf("... sample_read:\n");
12298c2ecf20Sopenharmony_ci
12308c2ecf20Sopenharmony_ci	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
12318c2ecf20Sopenharmony_ci		printf("...... time enabled %016" PRIx64 "\n",
12328c2ecf20Sopenharmony_ci		       sample->read.time_enabled);
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
12358c2ecf20Sopenharmony_ci		printf("...... time running %016" PRIx64 "\n",
12368c2ecf20Sopenharmony_ci		       sample->read.time_running);
12378c2ecf20Sopenharmony_ci
12388c2ecf20Sopenharmony_ci	if (read_format & PERF_FORMAT_GROUP) {
12398c2ecf20Sopenharmony_ci		u64 i;
12408c2ecf20Sopenharmony_ci
12418c2ecf20Sopenharmony_ci		printf(".... group nr %" PRIu64 "\n", sample->read.group.nr);
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_ci		for (i = 0; i < sample->read.group.nr; i++) {
12448c2ecf20Sopenharmony_ci			struct sample_read_value *value;
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci			value = &sample->read.group.values[i];
12478c2ecf20Sopenharmony_ci			printf("..... id %016" PRIx64
12488c2ecf20Sopenharmony_ci			       ", value %016" PRIx64 "\n",
12498c2ecf20Sopenharmony_ci			       value->id, value->value);
12508c2ecf20Sopenharmony_ci		}
12518c2ecf20Sopenharmony_ci	} else
12528c2ecf20Sopenharmony_ci		printf("..... id %016" PRIx64 ", value %016" PRIx64 "\n",
12538c2ecf20Sopenharmony_ci			sample->read.one.id, sample->read.one.value);
12548c2ecf20Sopenharmony_ci}
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_cistatic void dump_event(struct evlist *evlist, union perf_event *event,
12578c2ecf20Sopenharmony_ci		       u64 file_offset, struct perf_sample *sample)
12588c2ecf20Sopenharmony_ci{
12598c2ecf20Sopenharmony_ci	if (!dump_trace)
12608c2ecf20Sopenharmony_ci		return;
12618c2ecf20Sopenharmony_ci
12628c2ecf20Sopenharmony_ci	printf("\n%#" PRIx64 " [%#x]: event: %d\n",
12638c2ecf20Sopenharmony_ci	       file_offset, event->header.size, event->header.type);
12648c2ecf20Sopenharmony_ci
12658c2ecf20Sopenharmony_ci	trace_event(event);
12668c2ecf20Sopenharmony_ci	if (event->header.type == PERF_RECORD_SAMPLE && evlist->trace_event_sample_raw)
12678c2ecf20Sopenharmony_ci		evlist->trace_event_sample_raw(evlist, event, sample);
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_ci	if (sample)
12708c2ecf20Sopenharmony_ci		perf_evlist__print_tstamp(evlist, event, sample);
12718c2ecf20Sopenharmony_ci
12728c2ecf20Sopenharmony_ci	printf("%#" PRIx64 " [%#x]: PERF_RECORD_%s", file_offset,
12738c2ecf20Sopenharmony_ci	       event->header.size, perf_event__name(event->header.type));
12748c2ecf20Sopenharmony_ci}
12758c2ecf20Sopenharmony_ci
12768c2ecf20Sopenharmony_cistatic void dump_sample(struct evsel *evsel, union perf_event *event,
12778c2ecf20Sopenharmony_ci			struct perf_sample *sample)
12788c2ecf20Sopenharmony_ci{
12798c2ecf20Sopenharmony_ci	u64 sample_type;
12808c2ecf20Sopenharmony_ci
12818c2ecf20Sopenharmony_ci	if (!dump_trace)
12828c2ecf20Sopenharmony_ci		return;
12838c2ecf20Sopenharmony_ci
12848c2ecf20Sopenharmony_ci	printf("(IP, 0x%x): %d/%d: %#" PRIx64 " period: %" PRIu64 " addr: %#" PRIx64 "\n",
12858c2ecf20Sopenharmony_ci	       event->header.misc, sample->pid, sample->tid, sample->ip,
12868c2ecf20Sopenharmony_ci	       sample->period, sample->addr);
12878c2ecf20Sopenharmony_ci
12888c2ecf20Sopenharmony_ci	sample_type = evsel->core.attr.sample_type;
12898c2ecf20Sopenharmony_ci
12908c2ecf20Sopenharmony_ci	if (evsel__has_callchain(evsel))
12918c2ecf20Sopenharmony_ci		callchain__printf(evsel, sample);
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	if (evsel__has_br_stack(evsel))
12948c2ecf20Sopenharmony_ci		branch_stack__printf(sample, evsel__has_branch_callstack(evsel));
12958c2ecf20Sopenharmony_ci
12968c2ecf20Sopenharmony_ci	if (sample_type & PERF_SAMPLE_REGS_USER)
12978c2ecf20Sopenharmony_ci		regs_user__printf(sample);
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	if (sample_type & PERF_SAMPLE_REGS_INTR)
13008c2ecf20Sopenharmony_ci		regs_intr__printf(sample);
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_ci	if (sample_type & PERF_SAMPLE_STACK_USER)
13038c2ecf20Sopenharmony_ci		stack_user__printf(&sample->user_stack);
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_ci	if (sample_type & PERF_SAMPLE_WEIGHT)
13068c2ecf20Sopenharmony_ci		printf("... weight: %" PRIu64 "\n", sample->weight);
13078c2ecf20Sopenharmony_ci
13088c2ecf20Sopenharmony_ci	if (sample_type & PERF_SAMPLE_DATA_SRC)
13098c2ecf20Sopenharmony_ci		printf(" . data_src: 0x%"PRIx64"\n", sample->data_src);
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	if (sample_type & PERF_SAMPLE_PHYS_ADDR)
13128c2ecf20Sopenharmony_ci		printf(" .. phys_addr: 0x%"PRIx64"\n", sample->phys_addr);
13138c2ecf20Sopenharmony_ci
13148c2ecf20Sopenharmony_ci	if (sample_type & PERF_SAMPLE_TRANSACTION)
13158c2ecf20Sopenharmony_ci		printf("... transaction: %" PRIx64 "\n", sample->transaction);
13168c2ecf20Sopenharmony_ci
13178c2ecf20Sopenharmony_ci	if (sample_type & PERF_SAMPLE_READ)
13188c2ecf20Sopenharmony_ci		sample_read__printf(sample, evsel->core.attr.read_format);
13198c2ecf20Sopenharmony_ci}
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_cistatic void dump_read(struct evsel *evsel, union perf_event *event)
13228c2ecf20Sopenharmony_ci{
13238c2ecf20Sopenharmony_ci	struct perf_record_read *read_event = &event->read;
13248c2ecf20Sopenharmony_ci	u64 read_format;
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	if (!dump_trace)
13278c2ecf20Sopenharmony_ci		return;
13288c2ecf20Sopenharmony_ci
13298c2ecf20Sopenharmony_ci	printf(": %d %d %s %" PRI_lu64 "\n", event->read.pid, event->read.tid,
13308c2ecf20Sopenharmony_ci	       evsel__name(evsel), event->read.value);
13318c2ecf20Sopenharmony_ci
13328c2ecf20Sopenharmony_ci	if (!evsel)
13338c2ecf20Sopenharmony_ci		return;
13348c2ecf20Sopenharmony_ci
13358c2ecf20Sopenharmony_ci	read_format = evsel->core.attr.read_format;
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_ci	if (read_format & PERF_FORMAT_TOTAL_TIME_ENABLED)
13388c2ecf20Sopenharmony_ci		printf("... time enabled : %" PRI_lu64 "\n", read_event->time_enabled);
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	if (read_format & PERF_FORMAT_TOTAL_TIME_RUNNING)
13418c2ecf20Sopenharmony_ci		printf("... time running : %" PRI_lu64 "\n", read_event->time_running);
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	if (read_format & PERF_FORMAT_ID)
13448c2ecf20Sopenharmony_ci		printf("... id           : %" PRI_lu64 "\n", read_event->id);
13458c2ecf20Sopenharmony_ci}
13468c2ecf20Sopenharmony_ci
13478c2ecf20Sopenharmony_cistatic struct machine *machines__find_for_cpumode(struct machines *machines,
13488c2ecf20Sopenharmony_ci					       union perf_event *event,
13498c2ecf20Sopenharmony_ci					       struct perf_sample *sample)
13508c2ecf20Sopenharmony_ci{
13518c2ecf20Sopenharmony_ci	struct machine *machine;
13528c2ecf20Sopenharmony_ci
13538c2ecf20Sopenharmony_ci	if (perf_guest &&
13548c2ecf20Sopenharmony_ci	    ((sample->cpumode == PERF_RECORD_MISC_GUEST_KERNEL) ||
13558c2ecf20Sopenharmony_ci	     (sample->cpumode == PERF_RECORD_MISC_GUEST_USER))) {
13568c2ecf20Sopenharmony_ci		u32 pid;
13578c2ecf20Sopenharmony_ci
13588c2ecf20Sopenharmony_ci		if (event->header.type == PERF_RECORD_MMAP
13598c2ecf20Sopenharmony_ci		    || event->header.type == PERF_RECORD_MMAP2)
13608c2ecf20Sopenharmony_ci			pid = event->mmap.pid;
13618c2ecf20Sopenharmony_ci		else
13628c2ecf20Sopenharmony_ci			pid = sample->pid;
13638c2ecf20Sopenharmony_ci
13648c2ecf20Sopenharmony_ci		machine = machines__find(machines, pid);
13658c2ecf20Sopenharmony_ci		if (!machine)
13668c2ecf20Sopenharmony_ci			machine = machines__findnew(machines, DEFAULT_GUEST_KERNEL_ID);
13678c2ecf20Sopenharmony_ci		return machine;
13688c2ecf20Sopenharmony_ci	}
13698c2ecf20Sopenharmony_ci
13708c2ecf20Sopenharmony_ci	return &machines->host;
13718c2ecf20Sopenharmony_ci}
13728c2ecf20Sopenharmony_ci
13738c2ecf20Sopenharmony_cistatic int deliver_sample_value(struct evlist *evlist,
13748c2ecf20Sopenharmony_ci				struct perf_tool *tool,
13758c2ecf20Sopenharmony_ci				union perf_event *event,
13768c2ecf20Sopenharmony_ci				struct perf_sample *sample,
13778c2ecf20Sopenharmony_ci				struct sample_read_value *v,
13788c2ecf20Sopenharmony_ci				struct machine *machine)
13798c2ecf20Sopenharmony_ci{
13808c2ecf20Sopenharmony_ci	struct perf_sample_id *sid = perf_evlist__id2sid(evlist, v->id);
13818c2ecf20Sopenharmony_ci	struct evsel *evsel;
13828c2ecf20Sopenharmony_ci
13838c2ecf20Sopenharmony_ci	if (sid) {
13848c2ecf20Sopenharmony_ci		sample->id     = v->id;
13858c2ecf20Sopenharmony_ci		sample->period = v->value - sid->period;
13868c2ecf20Sopenharmony_ci		sid->period    = v->value;
13878c2ecf20Sopenharmony_ci	}
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	if (!sid || sid->evsel == NULL) {
13908c2ecf20Sopenharmony_ci		++evlist->stats.nr_unknown_id;
13918c2ecf20Sopenharmony_ci		return 0;
13928c2ecf20Sopenharmony_ci	}
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_ci	/*
13958c2ecf20Sopenharmony_ci	 * There's no reason to deliver sample
13968c2ecf20Sopenharmony_ci	 * for zero period, bail out.
13978c2ecf20Sopenharmony_ci	 */
13988c2ecf20Sopenharmony_ci	if (!sample->period)
13998c2ecf20Sopenharmony_ci		return 0;
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	evsel = container_of(sid->evsel, struct evsel, core);
14028c2ecf20Sopenharmony_ci	return tool->sample(tool, event, sample, evsel, machine);
14038c2ecf20Sopenharmony_ci}
14048c2ecf20Sopenharmony_ci
14058c2ecf20Sopenharmony_cistatic int deliver_sample_group(struct evlist *evlist,
14068c2ecf20Sopenharmony_ci				struct perf_tool *tool,
14078c2ecf20Sopenharmony_ci				union  perf_event *event,
14088c2ecf20Sopenharmony_ci				struct perf_sample *sample,
14098c2ecf20Sopenharmony_ci				struct machine *machine)
14108c2ecf20Sopenharmony_ci{
14118c2ecf20Sopenharmony_ci	int ret = -EINVAL;
14128c2ecf20Sopenharmony_ci	u64 i;
14138c2ecf20Sopenharmony_ci
14148c2ecf20Sopenharmony_ci	for (i = 0; i < sample->read.group.nr; i++) {
14158c2ecf20Sopenharmony_ci		ret = deliver_sample_value(evlist, tool, event, sample,
14168c2ecf20Sopenharmony_ci					   &sample->read.group.values[i],
14178c2ecf20Sopenharmony_ci					   machine);
14188c2ecf20Sopenharmony_ci		if (ret)
14198c2ecf20Sopenharmony_ci			break;
14208c2ecf20Sopenharmony_ci	}
14218c2ecf20Sopenharmony_ci
14228c2ecf20Sopenharmony_ci	return ret;
14238c2ecf20Sopenharmony_ci}
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_cistatic int
14268c2ecf20Sopenharmony_ci perf_evlist__deliver_sample(struct evlist *evlist,
14278c2ecf20Sopenharmony_ci			     struct perf_tool *tool,
14288c2ecf20Sopenharmony_ci			     union  perf_event *event,
14298c2ecf20Sopenharmony_ci			     struct perf_sample *sample,
14308c2ecf20Sopenharmony_ci			     struct evsel *evsel,
14318c2ecf20Sopenharmony_ci			     struct machine *machine)
14328c2ecf20Sopenharmony_ci{
14338c2ecf20Sopenharmony_ci	/* We know evsel != NULL. */
14348c2ecf20Sopenharmony_ci	u64 sample_type = evsel->core.attr.sample_type;
14358c2ecf20Sopenharmony_ci	u64 read_format = evsel->core.attr.read_format;
14368c2ecf20Sopenharmony_ci
14378c2ecf20Sopenharmony_ci	/* Standard sample delivery. */
14388c2ecf20Sopenharmony_ci	if (!(sample_type & PERF_SAMPLE_READ))
14398c2ecf20Sopenharmony_ci		return tool->sample(tool, event, sample, evsel, machine);
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	/* For PERF_SAMPLE_READ we have either single or group mode. */
14428c2ecf20Sopenharmony_ci	if (read_format & PERF_FORMAT_GROUP)
14438c2ecf20Sopenharmony_ci		return deliver_sample_group(evlist, tool, event, sample,
14448c2ecf20Sopenharmony_ci					    machine);
14458c2ecf20Sopenharmony_ci	else
14468c2ecf20Sopenharmony_ci		return deliver_sample_value(evlist, tool, event, sample,
14478c2ecf20Sopenharmony_ci					    &sample->read.one, machine);
14488c2ecf20Sopenharmony_ci}
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_cistatic int machines__deliver_event(struct machines *machines,
14518c2ecf20Sopenharmony_ci				   struct evlist *evlist,
14528c2ecf20Sopenharmony_ci				   union perf_event *event,
14538c2ecf20Sopenharmony_ci				   struct perf_sample *sample,
14548c2ecf20Sopenharmony_ci				   struct perf_tool *tool, u64 file_offset)
14558c2ecf20Sopenharmony_ci{
14568c2ecf20Sopenharmony_ci	struct evsel *evsel;
14578c2ecf20Sopenharmony_ci	struct machine *machine;
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_ci	dump_event(evlist, event, file_offset, sample);
14608c2ecf20Sopenharmony_ci
14618c2ecf20Sopenharmony_ci	evsel = perf_evlist__id2evsel(evlist, sample->id);
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	machine = machines__find_for_cpumode(machines, event, sample);
14648c2ecf20Sopenharmony_ci
14658c2ecf20Sopenharmony_ci	switch (event->header.type) {
14668c2ecf20Sopenharmony_ci	case PERF_RECORD_SAMPLE:
14678c2ecf20Sopenharmony_ci		if (evsel == NULL) {
14688c2ecf20Sopenharmony_ci			++evlist->stats.nr_unknown_id;
14698c2ecf20Sopenharmony_ci			return 0;
14708c2ecf20Sopenharmony_ci		}
14718c2ecf20Sopenharmony_ci		dump_sample(evsel, event, sample);
14728c2ecf20Sopenharmony_ci		if (machine == NULL) {
14738c2ecf20Sopenharmony_ci			++evlist->stats.nr_unprocessable_samples;
14748c2ecf20Sopenharmony_ci			return 0;
14758c2ecf20Sopenharmony_ci		}
14768c2ecf20Sopenharmony_ci		return perf_evlist__deliver_sample(evlist, tool, event, sample, evsel, machine);
14778c2ecf20Sopenharmony_ci	case PERF_RECORD_MMAP:
14788c2ecf20Sopenharmony_ci		return tool->mmap(tool, event, sample, machine);
14798c2ecf20Sopenharmony_ci	case PERF_RECORD_MMAP2:
14808c2ecf20Sopenharmony_ci		if (event->header.misc & PERF_RECORD_MISC_PROC_MAP_PARSE_TIMEOUT)
14818c2ecf20Sopenharmony_ci			++evlist->stats.nr_proc_map_timeout;
14828c2ecf20Sopenharmony_ci		return tool->mmap2(tool, event, sample, machine);
14838c2ecf20Sopenharmony_ci	case PERF_RECORD_COMM:
14848c2ecf20Sopenharmony_ci		return tool->comm(tool, event, sample, machine);
14858c2ecf20Sopenharmony_ci	case PERF_RECORD_NAMESPACES:
14868c2ecf20Sopenharmony_ci		return tool->namespaces(tool, event, sample, machine);
14878c2ecf20Sopenharmony_ci	case PERF_RECORD_CGROUP:
14888c2ecf20Sopenharmony_ci		return tool->cgroup(tool, event, sample, machine);
14898c2ecf20Sopenharmony_ci	case PERF_RECORD_FORK:
14908c2ecf20Sopenharmony_ci		return tool->fork(tool, event, sample, machine);
14918c2ecf20Sopenharmony_ci	case PERF_RECORD_EXIT:
14928c2ecf20Sopenharmony_ci		return tool->exit(tool, event, sample, machine);
14938c2ecf20Sopenharmony_ci	case PERF_RECORD_LOST:
14948c2ecf20Sopenharmony_ci		if (tool->lost == perf_event__process_lost)
14958c2ecf20Sopenharmony_ci			evlist->stats.total_lost += event->lost.lost;
14968c2ecf20Sopenharmony_ci		return tool->lost(tool, event, sample, machine);
14978c2ecf20Sopenharmony_ci	case PERF_RECORD_LOST_SAMPLES:
14988c2ecf20Sopenharmony_ci		if (tool->lost_samples == perf_event__process_lost_samples)
14998c2ecf20Sopenharmony_ci			evlist->stats.total_lost_samples += event->lost_samples.lost;
15008c2ecf20Sopenharmony_ci		return tool->lost_samples(tool, event, sample, machine);
15018c2ecf20Sopenharmony_ci	case PERF_RECORD_READ:
15028c2ecf20Sopenharmony_ci		dump_read(evsel, event);
15038c2ecf20Sopenharmony_ci		return tool->read(tool, event, sample, evsel, machine);
15048c2ecf20Sopenharmony_ci	case PERF_RECORD_THROTTLE:
15058c2ecf20Sopenharmony_ci		return tool->throttle(tool, event, sample, machine);
15068c2ecf20Sopenharmony_ci	case PERF_RECORD_UNTHROTTLE:
15078c2ecf20Sopenharmony_ci		return tool->unthrottle(tool, event, sample, machine);
15088c2ecf20Sopenharmony_ci	case PERF_RECORD_AUX:
15098c2ecf20Sopenharmony_ci		if (tool->aux == perf_event__process_aux) {
15108c2ecf20Sopenharmony_ci			if (event->aux.flags & PERF_AUX_FLAG_TRUNCATED)
15118c2ecf20Sopenharmony_ci				evlist->stats.total_aux_lost += 1;
15128c2ecf20Sopenharmony_ci			if (event->aux.flags & PERF_AUX_FLAG_PARTIAL)
15138c2ecf20Sopenharmony_ci				evlist->stats.total_aux_partial += 1;
15148c2ecf20Sopenharmony_ci		}
15158c2ecf20Sopenharmony_ci		return tool->aux(tool, event, sample, machine);
15168c2ecf20Sopenharmony_ci	case PERF_RECORD_ITRACE_START:
15178c2ecf20Sopenharmony_ci		return tool->itrace_start(tool, event, sample, machine);
15188c2ecf20Sopenharmony_ci	case PERF_RECORD_SWITCH:
15198c2ecf20Sopenharmony_ci	case PERF_RECORD_SWITCH_CPU_WIDE:
15208c2ecf20Sopenharmony_ci		return tool->context_switch(tool, event, sample, machine);
15218c2ecf20Sopenharmony_ci	case PERF_RECORD_KSYMBOL:
15228c2ecf20Sopenharmony_ci		return tool->ksymbol(tool, event, sample, machine);
15238c2ecf20Sopenharmony_ci	case PERF_RECORD_BPF_EVENT:
15248c2ecf20Sopenharmony_ci		return tool->bpf(tool, event, sample, machine);
15258c2ecf20Sopenharmony_ci	case PERF_RECORD_TEXT_POKE:
15268c2ecf20Sopenharmony_ci		return tool->text_poke(tool, event, sample, machine);
15278c2ecf20Sopenharmony_ci	default:
15288c2ecf20Sopenharmony_ci		++evlist->stats.nr_unknown_events;
15298c2ecf20Sopenharmony_ci		return -1;
15308c2ecf20Sopenharmony_ci	}
15318c2ecf20Sopenharmony_ci}
15328c2ecf20Sopenharmony_ci
15338c2ecf20Sopenharmony_cistatic int perf_session__deliver_event(struct perf_session *session,
15348c2ecf20Sopenharmony_ci				       union perf_event *event,
15358c2ecf20Sopenharmony_ci				       struct perf_tool *tool,
15368c2ecf20Sopenharmony_ci				       u64 file_offset)
15378c2ecf20Sopenharmony_ci{
15388c2ecf20Sopenharmony_ci	struct perf_sample sample;
15398c2ecf20Sopenharmony_ci	int ret;
15408c2ecf20Sopenharmony_ci
15418c2ecf20Sopenharmony_ci	ret = perf_evlist__parse_sample(session->evlist, event, &sample);
15428c2ecf20Sopenharmony_ci	if (ret) {
15438c2ecf20Sopenharmony_ci		pr_err("Can't parse sample, err = %d\n", ret);
15448c2ecf20Sopenharmony_ci		return ret;
15458c2ecf20Sopenharmony_ci	}
15468c2ecf20Sopenharmony_ci
15478c2ecf20Sopenharmony_ci	ret = auxtrace__process_event(session, event, &sample, tool);
15488c2ecf20Sopenharmony_ci	if (ret < 0)
15498c2ecf20Sopenharmony_ci		return ret;
15508c2ecf20Sopenharmony_ci	if (ret > 0)
15518c2ecf20Sopenharmony_ci		return 0;
15528c2ecf20Sopenharmony_ci
15538c2ecf20Sopenharmony_ci	ret = machines__deliver_event(&session->machines, session->evlist,
15548c2ecf20Sopenharmony_ci				      event, &sample, tool, file_offset);
15558c2ecf20Sopenharmony_ci
15568c2ecf20Sopenharmony_ci	if (dump_trace && sample.aux_sample.size)
15578c2ecf20Sopenharmony_ci		auxtrace__dump_auxtrace_sample(session, &sample);
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	return ret;
15608c2ecf20Sopenharmony_ci}
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_cistatic s64 perf_session__process_user_event(struct perf_session *session,
15638c2ecf20Sopenharmony_ci					    union perf_event *event,
15648c2ecf20Sopenharmony_ci					    u64 file_offset)
15658c2ecf20Sopenharmony_ci{
15668c2ecf20Sopenharmony_ci	struct ordered_events *oe = &session->ordered_events;
15678c2ecf20Sopenharmony_ci	struct perf_tool *tool = session->tool;
15688c2ecf20Sopenharmony_ci	struct perf_sample sample = { .time = 0, };
15698c2ecf20Sopenharmony_ci	int fd = perf_data__fd(session->data);
15708c2ecf20Sopenharmony_ci	int err;
15718c2ecf20Sopenharmony_ci
15728c2ecf20Sopenharmony_ci	if (event->header.type != PERF_RECORD_COMPRESSED ||
15738c2ecf20Sopenharmony_ci	    tool->compressed == perf_session__process_compressed_event_stub)
15748c2ecf20Sopenharmony_ci		dump_event(session->evlist, event, file_offset, &sample);
15758c2ecf20Sopenharmony_ci
15768c2ecf20Sopenharmony_ci	/* These events are processed right away */
15778c2ecf20Sopenharmony_ci	switch (event->header.type) {
15788c2ecf20Sopenharmony_ci	case PERF_RECORD_HEADER_ATTR:
15798c2ecf20Sopenharmony_ci		err = tool->attr(tool, event, &session->evlist);
15808c2ecf20Sopenharmony_ci		if (err == 0) {
15818c2ecf20Sopenharmony_ci			perf_session__set_id_hdr_size(session);
15828c2ecf20Sopenharmony_ci			perf_session__set_comm_exec(session);
15838c2ecf20Sopenharmony_ci		}
15848c2ecf20Sopenharmony_ci		return err;
15858c2ecf20Sopenharmony_ci	case PERF_RECORD_EVENT_UPDATE:
15868c2ecf20Sopenharmony_ci		return tool->event_update(tool, event, &session->evlist);
15878c2ecf20Sopenharmony_ci	case PERF_RECORD_HEADER_EVENT_TYPE:
15888c2ecf20Sopenharmony_ci		/*
15898c2ecf20Sopenharmony_ci		 * Depreceated, but we need to handle it for sake
15908c2ecf20Sopenharmony_ci		 * of old data files create in pipe mode.
15918c2ecf20Sopenharmony_ci		 */
15928c2ecf20Sopenharmony_ci		return 0;
15938c2ecf20Sopenharmony_ci	case PERF_RECORD_HEADER_TRACING_DATA:
15948c2ecf20Sopenharmony_ci		/*
15958c2ecf20Sopenharmony_ci		 * Setup for reading amidst mmap, but only when we
15968c2ecf20Sopenharmony_ci		 * are in 'file' mode. The 'pipe' fd is in proper
15978c2ecf20Sopenharmony_ci		 * place already.
15988c2ecf20Sopenharmony_ci		 */
15998c2ecf20Sopenharmony_ci		if (!perf_data__is_pipe(session->data))
16008c2ecf20Sopenharmony_ci			lseek(fd, file_offset, SEEK_SET);
16018c2ecf20Sopenharmony_ci		return tool->tracing_data(session, event);
16028c2ecf20Sopenharmony_ci	case PERF_RECORD_HEADER_BUILD_ID:
16038c2ecf20Sopenharmony_ci		return tool->build_id(session, event);
16048c2ecf20Sopenharmony_ci	case PERF_RECORD_FINISHED_ROUND:
16058c2ecf20Sopenharmony_ci		return tool->finished_round(tool, event, oe);
16068c2ecf20Sopenharmony_ci	case PERF_RECORD_ID_INDEX:
16078c2ecf20Sopenharmony_ci		return tool->id_index(session, event);
16088c2ecf20Sopenharmony_ci	case PERF_RECORD_AUXTRACE_INFO:
16098c2ecf20Sopenharmony_ci		return tool->auxtrace_info(session, event);
16108c2ecf20Sopenharmony_ci	case PERF_RECORD_AUXTRACE:
16118c2ecf20Sopenharmony_ci		/* setup for reading amidst mmap */
16128c2ecf20Sopenharmony_ci		lseek(fd, file_offset + event->header.size, SEEK_SET);
16138c2ecf20Sopenharmony_ci		return tool->auxtrace(session, event);
16148c2ecf20Sopenharmony_ci	case PERF_RECORD_AUXTRACE_ERROR:
16158c2ecf20Sopenharmony_ci		perf_session__auxtrace_error_inc(session, event);
16168c2ecf20Sopenharmony_ci		return tool->auxtrace_error(session, event);
16178c2ecf20Sopenharmony_ci	case PERF_RECORD_THREAD_MAP:
16188c2ecf20Sopenharmony_ci		return tool->thread_map(session, event);
16198c2ecf20Sopenharmony_ci	case PERF_RECORD_CPU_MAP:
16208c2ecf20Sopenharmony_ci		return tool->cpu_map(session, event);
16218c2ecf20Sopenharmony_ci	case PERF_RECORD_STAT_CONFIG:
16228c2ecf20Sopenharmony_ci		return tool->stat_config(session, event);
16238c2ecf20Sopenharmony_ci	case PERF_RECORD_STAT:
16248c2ecf20Sopenharmony_ci		return tool->stat(session, event);
16258c2ecf20Sopenharmony_ci	case PERF_RECORD_STAT_ROUND:
16268c2ecf20Sopenharmony_ci		return tool->stat_round(session, event);
16278c2ecf20Sopenharmony_ci	case PERF_RECORD_TIME_CONV:
16288c2ecf20Sopenharmony_ci		session->time_conv = event->time_conv;
16298c2ecf20Sopenharmony_ci		return tool->time_conv(session, event);
16308c2ecf20Sopenharmony_ci	case PERF_RECORD_HEADER_FEATURE:
16318c2ecf20Sopenharmony_ci		return tool->feature(session, event);
16328c2ecf20Sopenharmony_ci	case PERF_RECORD_COMPRESSED:
16338c2ecf20Sopenharmony_ci		err = tool->compressed(session, event, file_offset);
16348c2ecf20Sopenharmony_ci		if (err)
16358c2ecf20Sopenharmony_ci			dump_event(session->evlist, event, file_offset, &sample);
16368c2ecf20Sopenharmony_ci		return err;
16378c2ecf20Sopenharmony_ci	default:
16388c2ecf20Sopenharmony_ci		return -EINVAL;
16398c2ecf20Sopenharmony_ci	}
16408c2ecf20Sopenharmony_ci}
16418c2ecf20Sopenharmony_ci
16428c2ecf20Sopenharmony_ciint perf_session__deliver_synth_event(struct perf_session *session,
16438c2ecf20Sopenharmony_ci				      union perf_event *event,
16448c2ecf20Sopenharmony_ci				      struct perf_sample *sample)
16458c2ecf20Sopenharmony_ci{
16468c2ecf20Sopenharmony_ci	struct evlist *evlist = session->evlist;
16478c2ecf20Sopenharmony_ci	struct perf_tool *tool = session->tool;
16488c2ecf20Sopenharmony_ci
16498c2ecf20Sopenharmony_ci	events_stats__inc(&evlist->stats, event->header.type);
16508c2ecf20Sopenharmony_ci
16518c2ecf20Sopenharmony_ci	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
16528c2ecf20Sopenharmony_ci		return perf_session__process_user_event(session, event, 0);
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_ci	return machines__deliver_event(&session->machines, evlist, event, sample, tool, 0);
16558c2ecf20Sopenharmony_ci}
16568c2ecf20Sopenharmony_ci
16578c2ecf20Sopenharmony_cistatic void event_swap(union perf_event *event, bool sample_id_all)
16588c2ecf20Sopenharmony_ci{
16598c2ecf20Sopenharmony_ci	perf_event__swap_op swap;
16608c2ecf20Sopenharmony_ci
16618c2ecf20Sopenharmony_ci	swap = perf_event__swap_ops[event->header.type];
16628c2ecf20Sopenharmony_ci	if (swap)
16638c2ecf20Sopenharmony_ci		swap(event, sample_id_all);
16648c2ecf20Sopenharmony_ci}
16658c2ecf20Sopenharmony_ci
16668c2ecf20Sopenharmony_ciint perf_session__peek_event(struct perf_session *session, off_t file_offset,
16678c2ecf20Sopenharmony_ci			     void *buf, size_t buf_sz,
16688c2ecf20Sopenharmony_ci			     union perf_event **event_ptr,
16698c2ecf20Sopenharmony_ci			     struct perf_sample *sample)
16708c2ecf20Sopenharmony_ci{
16718c2ecf20Sopenharmony_ci	union perf_event *event;
16728c2ecf20Sopenharmony_ci	size_t hdr_sz, rest;
16738c2ecf20Sopenharmony_ci	int fd;
16748c2ecf20Sopenharmony_ci
16758c2ecf20Sopenharmony_ci	if (session->one_mmap && !session->header.needs_swap) {
16768c2ecf20Sopenharmony_ci		event = file_offset - session->one_mmap_offset +
16778c2ecf20Sopenharmony_ci			session->one_mmap_addr;
16788c2ecf20Sopenharmony_ci		goto out_parse_sample;
16798c2ecf20Sopenharmony_ci	}
16808c2ecf20Sopenharmony_ci
16818c2ecf20Sopenharmony_ci	if (perf_data__is_pipe(session->data))
16828c2ecf20Sopenharmony_ci		return -1;
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_ci	fd = perf_data__fd(session->data);
16858c2ecf20Sopenharmony_ci	hdr_sz = sizeof(struct perf_event_header);
16868c2ecf20Sopenharmony_ci
16878c2ecf20Sopenharmony_ci	if (buf_sz < hdr_sz)
16888c2ecf20Sopenharmony_ci		return -1;
16898c2ecf20Sopenharmony_ci
16908c2ecf20Sopenharmony_ci	if (lseek(fd, file_offset, SEEK_SET) == (off_t)-1 ||
16918c2ecf20Sopenharmony_ci	    readn(fd, buf, hdr_sz) != (ssize_t)hdr_sz)
16928c2ecf20Sopenharmony_ci		return -1;
16938c2ecf20Sopenharmony_ci
16948c2ecf20Sopenharmony_ci	event = (union perf_event *)buf;
16958c2ecf20Sopenharmony_ci
16968c2ecf20Sopenharmony_ci	if (session->header.needs_swap)
16978c2ecf20Sopenharmony_ci		perf_event_header__bswap(&event->header);
16988c2ecf20Sopenharmony_ci
16998c2ecf20Sopenharmony_ci	if (event->header.size < hdr_sz || event->header.size > buf_sz)
17008c2ecf20Sopenharmony_ci		return -1;
17018c2ecf20Sopenharmony_ci
17028c2ecf20Sopenharmony_ci	buf += hdr_sz;
17038c2ecf20Sopenharmony_ci	rest = event->header.size - hdr_sz;
17048c2ecf20Sopenharmony_ci
17058c2ecf20Sopenharmony_ci	if (readn(fd, buf, rest) != (ssize_t)rest)
17068c2ecf20Sopenharmony_ci		return -1;
17078c2ecf20Sopenharmony_ci
17088c2ecf20Sopenharmony_ci	if (session->header.needs_swap)
17098c2ecf20Sopenharmony_ci		event_swap(event, evlist__sample_id_all(session->evlist));
17108c2ecf20Sopenharmony_ci
17118c2ecf20Sopenharmony_ciout_parse_sample:
17128c2ecf20Sopenharmony_ci
17138c2ecf20Sopenharmony_ci	if (sample && event->header.type < PERF_RECORD_USER_TYPE_START &&
17148c2ecf20Sopenharmony_ci	    perf_evlist__parse_sample(session->evlist, event, sample))
17158c2ecf20Sopenharmony_ci		return -1;
17168c2ecf20Sopenharmony_ci
17178c2ecf20Sopenharmony_ci	*event_ptr = event;
17188c2ecf20Sopenharmony_ci
17198c2ecf20Sopenharmony_ci	return 0;
17208c2ecf20Sopenharmony_ci}
17218c2ecf20Sopenharmony_ci
17228c2ecf20Sopenharmony_ciint perf_session__peek_events(struct perf_session *session, u64 offset,
17238c2ecf20Sopenharmony_ci			      u64 size, peek_events_cb_t cb, void *data)
17248c2ecf20Sopenharmony_ci{
17258c2ecf20Sopenharmony_ci	u64 max_offset = offset + size;
17268c2ecf20Sopenharmony_ci	char buf[PERF_SAMPLE_MAX_SIZE];
17278c2ecf20Sopenharmony_ci	union perf_event *event;
17288c2ecf20Sopenharmony_ci	int err;
17298c2ecf20Sopenharmony_ci
17308c2ecf20Sopenharmony_ci	do {
17318c2ecf20Sopenharmony_ci		err = perf_session__peek_event(session, offset, buf,
17328c2ecf20Sopenharmony_ci					       PERF_SAMPLE_MAX_SIZE, &event,
17338c2ecf20Sopenharmony_ci					       NULL);
17348c2ecf20Sopenharmony_ci		if (err)
17358c2ecf20Sopenharmony_ci			return err;
17368c2ecf20Sopenharmony_ci
17378c2ecf20Sopenharmony_ci		err = cb(session, event, offset, data);
17388c2ecf20Sopenharmony_ci		if (err)
17398c2ecf20Sopenharmony_ci			return err;
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_ci		offset += event->header.size;
17428c2ecf20Sopenharmony_ci		if (event->header.type == PERF_RECORD_AUXTRACE)
17438c2ecf20Sopenharmony_ci			offset += event->auxtrace.size;
17448c2ecf20Sopenharmony_ci
17458c2ecf20Sopenharmony_ci	} while (offset < max_offset);
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci	return err;
17488c2ecf20Sopenharmony_ci}
17498c2ecf20Sopenharmony_ci
17508c2ecf20Sopenharmony_cistatic s64 perf_session__process_event(struct perf_session *session,
17518c2ecf20Sopenharmony_ci				       union perf_event *event, u64 file_offset)
17528c2ecf20Sopenharmony_ci{
17538c2ecf20Sopenharmony_ci	struct evlist *evlist = session->evlist;
17548c2ecf20Sopenharmony_ci	struct perf_tool *tool = session->tool;
17558c2ecf20Sopenharmony_ci	int ret;
17568c2ecf20Sopenharmony_ci
17578c2ecf20Sopenharmony_ci	if (session->header.needs_swap)
17588c2ecf20Sopenharmony_ci		event_swap(event, evlist__sample_id_all(evlist));
17598c2ecf20Sopenharmony_ci
17608c2ecf20Sopenharmony_ci	if (event->header.type >= PERF_RECORD_HEADER_MAX)
17618c2ecf20Sopenharmony_ci		return -EINVAL;
17628c2ecf20Sopenharmony_ci
17638c2ecf20Sopenharmony_ci	events_stats__inc(&evlist->stats, event->header.type);
17648c2ecf20Sopenharmony_ci
17658c2ecf20Sopenharmony_ci	if (event->header.type >= PERF_RECORD_USER_TYPE_START)
17668c2ecf20Sopenharmony_ci		return perf_session__process_user_event(session, event, file_offset);
17678c2ecf20Sopenharmony_ci
17688c2ecf20Sopenharmony_ci	if (tool->ordered_events) {
17698c2ecf20Sopenharmony_ci		u64 timestamp = -1ULL;
17708c2ecf20Sopenharmony_ci
17718c2ecf20Sopenharmony_ci		ret = perf_evlist__parse_sample_timestamp(evlist, event, &timestamp);
17728c2ecf20Sopenharmony_ci		if (ret && ret != -1)
17738c2ecf20Sopenharmony_ci			return ret;
17748c2ecf20Sopenharmony_ci
17758c2ecf20Sopenharmony_ci		ret = perf_session__queue_event(session, event, timestamp, file_offset);
17768c2ecf20Sopenharmony_ci		if (ret != -ETIME)
17778c2ecf20Sopenharmony_ci			return ret;
17788c2ecf20Sopenharmony_ci	}
17798c2ecf20Sopenharmony_ci
17808c2ecf20Sopenharmony_ci	return perf_session__deliver_event(session, event, tool, file_offset);
17818c2ecf20Sopenharmony_ci}
17828c2ecf20Sopenharmony_ci
17838c2ecf20Sopenharmony_civoid perf_event_header__bswap(struct perf_event_header *hdr)
17848c2ecf20Sopenharmony_ci{
17858c2ecf20Sopenharmony_ci	hdr->type = bswap_32(hdr->type);
17868c2ecf20Sopenharmony_ci	hdr->misc = bswap_16(hdr->misc);
17878c2ecf20Sopenharmony_ci	hdr->size = bswap_16(hdr->size);
17888c2ecf20Sopenharmony_ci}
17898c2ecf20Sopenharmony_ci
17908c2ecf20Sopenharmony_cistruct thread *perf_session__findnew(struct perf_session *session, pid_t pid)
17918c2ecf20Sopenharmony_ci{
17928c2ecf20Sopenharmony_ci	return machine__findnew_thread(&session->machines.host, -1, pid);
17938c2ecf20Sopenharmony_ci}
17948c2ecf20Sopenharmony_ci
17958c2ecf20Sopenharmony_ci/*
17968c2ecf20Sopenharmony_ci * Threads are identified by pid and tid, and the idle task has pid == tid == 0.
17978c2ecf20Sopenharmony_ci * So here a single thread is created for that, but actually there is a separate
17988c2ecf20Sopenharmony_ci * idle task per cpu, so there should be one 'struct thread' per cpu, but there
17998c2ecf20Sopenharmony_ci * is only 1. That causes problems for some tools, requiring workarounds. For
18008c2ecf20Sopenharmony_ci * example get_idle_thread() in builtin-sched.c, or thread_stack__per_cpu().
18018c2ecf20Sopenharmony_ci */
18028c2ecf20Sopenharmony_ciint perf_session__register_idle_thread(struct perf_session *session)
18038c2ecf20Sopenharmony_ci{
18048c2ecf20Sopenharmony_ci	struct thread *thread;
18058c2ecf20Sopenharmony_ci	int err = 0;
18068c2ecf20Sopenharmony_ci
18078c2ecf20Sopenharmony_ci	thread = machine__findnew_thread(&session->machines.host, 0, 0);
18088c2ecf20Sopenharmony_ci	if (thread == NULL || thread__set_comm(thread, "swapper", 0)) {
18098c2ecf20Sopenharmony_ci		pr_err("problem inserting idle task.\n");
18108c2ecf20Sopenharmony_ci		err = -1;
18118c2ecf20Sopenharmony_ci	}
18128c2ecf20Sopenharmony_ci
18138c2ecf20Sopenharmony_ci	if (thread == NULL || thread__set_namespaces(thread, 0, NULL)) {
18148c2ecf20Sopenharmony_ci		pr_err("problem inserting idle task.\n");
18158c2ecf20Sopenharmony_ci		err = -1;
18168c2ecf20Sopenharmony_ci	}
18178c2ecf20Sopenharmony_ci
18188c2ecf20Sopenharmony_ci	/* machine__findnew_thread() got the thread, so put it */
18198c2ecf20Sopenharmony_ci	thread__put(thread);
18208c2ecf20Sopenharmony_ci	return err;
18218c2ecf20Sopenharmony_ci}
18228c2ecf20Sopenharmony_ci
18238c2ecf20Sopenharmony_cistatic void
18248c2ecf20Sopenharmony_ciperf_session__warn_order(const struct perf_session *session)
18258c2ecf20Sopenharmony_ci{
18268c2ecf20Sopenharmony_ci	const struct ordered_events *oe = &session->ordered_events;
18278c2ecf20Sopenharmony_ci	struct evsel *evsel;
18288c2ecf20Sopenharmony_ci	bool should_warn = true;
18298c2ecf20Sopenharmony_ci
18308c2ecf20Sopenharmony_ci	evlist__for_each_entry(session->evlist, evsel) {
18318c2ecf20Sopenharmony_ci		if (evsel->core.attr.write_backward)
18328c2ecf20Sopenharmony_ci			should_warn = false;
18338c2ecf20Sopenharmony_ci	}
18348c2ecf20Sopenharmony_ci
18358c2ecf20Sopenharmony_ci	if (!should_warn)
18368c2ecf20Sopenharmony_ci		return;
18378c2ecf20Sopenharmony_ci	if (oe->nr_unordered_events != 0)
18388c2ecf20Sopenharmony_ci		ui__warning("%u out of order events recorded.\n", oe->nr_unordered_events);
18398c2ecf20Sopenharmony_ci}
18408c2ecf20Sopenharmony_ci
18418c2ecf20Sopenharmony_cistatic void perf_session__warn_about_errors(const struct perf_session *session)
18428c2ecf20Sopenharmony_ci{
18438c2ecf20Sopenharmony_ci	const struct events_stats *stats = &session->evlist->stats;
18448c2ecf20Sopenharmony_ci
18458c2ecf20Sopenharmony_ci	if (session->tool->lost == perf_event__process_lost &&
18468c2ecf20Sopenharmony_ci	    stats->nr_events[PERF_RECORD_LOST] != 0) {
18478c2ecf20Sopenharmony_ci		ui__warning("Processed %d events and lost %d chunks!\n\n"
18488c2ecf20Sopenharmony_ci			    "Check IO/CPU overload!\n\n",
18498c2ecf20Sopenharmony_ci			    stats->nr_events[0],
18508c2ecf20Sopenharmony_ci			    stats->nr_events[PERF_RECORD_LOST]);
18518c2ecf20Sopenharmony_ci	}
18528c2ecf20Sopenharmony_ci
18538c2ecf20Sopenharmony_ci	if (session->tool->lost_samples == perf_event__process_lost_samples) {
18548c2ecf20Sopenharmony_ci		double drop_rate;
18558c2ecf20Sopenharmony_ci
18568c2ecf20Sopenharmony_ci		drop_rate = (double)stats->total_lost_samples /
18578c2ecf20Sopenharmony_ci			    (double) (stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples);
18588c2ecf20Sopenharmony_ci		if (drop_rate > 0.05) {
18598c2ecf20Sopenharmony_ci			ui__warning("Processed %" PRIu64 " samples and lost %3.2f%%!\n\n",
18608c2ecf20Sopenharmony_ci				    stats->nr_events[PERF_RECORD_SAMPLE] + stats->total_lost_samples,
18618c2ecf20Sopenharmony_ci				    drop_rate * 100.0);
18628c2ecf20Sopenharmony_ci		}
18638c2ecf20Sopenharmony_ci	}
18648c2ecf20Sopenharmony_ci
18658c2ecf20Sopenharmony_ci	if (session->tool->aux == perf_event__process_aux &&
18668c2ecf20Sopenharmony_ci	    stats->total_aux_lost != 0) {
18678c2ecf20Sopenharmony_ci		ui__warning("AUX data lost %" PRIu64 " times out of %u!\n\n",
18688c2ecf20Sopenharmony_ci			    stats->total_aux_lost,
18698c2ecf20Sopenharmony_ci			    stats->nr_events[PERF_RECORD_AUX]);
18708c2ecf20Sopenharmony_ci	}
18718c2ecf20Sopenharmony_ci
18728c2ecf20Sopenharmony_ci	if (session->tool->aux == perf_event__process_aux &&
18738c2ecf20Sopenharmony_ci	    stats->total_aux_partial != 0) {
18748c2ecf20Sopenharmony_ci		bool vmm_exclusive = false;
18758c2ecf20Sopenharmony_ci
18768c2ecf20Sopenharmony_ci		(void)sysfs__read_bool("module/kvm_intel/parameters/vmm_exclusive",
18778c2ecf20Sopenharmony_ci		                       &vmm_exclusive);
18788c2ecf20Sopenharmony_ci
18798c2ecf20Sopenharmony_ci		ui__warning("AUX data had gaps in it %" PRIu64 " times out of %u!\n\n"
18808c2ecf20Sopenharmony_ci		            "Are you running a KVM guest in the background?%s\n\n",
18818c2ecf20Sopenharmony_ci			    stats->total_aux_partial,
18828c2ecf20Sopenharmony_ci			    stats->nr_events[PERF_RECORD_AUX],
18838c2ecf20Sopenharmony_ci			    vmm_exclusive ?
18848c2ecf20Sopenharmony_ci			    "\nReloading kvm_intel module with vmm_exclusive=0\n"
18858c2ecf20Sopenharmony_ci			    "will reduce the gaps to only guest's timeslices." :
18868c2ecf20Sopenharmony_ci			    "");
18878c2ecf20Sopenharmony_ci	}
18888c2ecf20Sopenharmony_ci
18898c2ecf20Sopenharmony_ci	if (stats->nr_unknown_events != 0) {
18908c2ecf20Sopenharmony_ci		ui__warning("Found %u unknown events!\n\n"
18918c2ecf20Sopenharmony_ci			    "Is this an older tool processing a perf.data "
18928c2ecf20Sopenharmony_ci			    "file generated by a more recent tool?\n\n"
18938c2ecf20Sopenharmony_ci			    "If that is not the case, consider "
18948c2ecf20Sopenharmony_ci			    "reporting to linux-kernel@vger.kernel.org.\n\n",
18958c2ecf20Sopenharmony_ci			    stats->nr_unknown_events);
18968c2ecf20Sopenharmony_ci	}
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_ci	if (stats->nr_unknown_id != 0) {
18998c2ecf20Sopenharmony_ci		ui__warning("%u samples with id not present in the header\n",
19008c2ecf20Sopenharmony_ci			    stats->nr_unknown_id);
19018c2ecf20Sopenharmony_ci	}
19028c2ecf20Sopenharmony_ci
19038c2ecf20Sopenharmony_ci	if (stats->nr_invalid_chains != 0) {
19048c2ecf20Sopenharmony_ci		ui__warning("Found invalid callchains!\n\n"
19058c2ecf20Sopenharmony_ci			    "%u out of %u events were discarded for this reason.\n\n"
19068c2ecf20Sopenharmony_ci			    "Consider reporting to linux-kernel@vger.kernel.org.\n\n",
19078c2ecf20Sopenharmony_ci			    stats->nr_invalid_chains,
19088c2ecf20Sopenharmony_ci			    stats->nr_events[PERF_RECORD_SAMPLE]);
19098c2ecf20Sopenharmony_ci	}
19108c2ecf20Sopenharmony_ci
19118c2ecf20Sopenharmony_ci	if (stats->nr_unprocessable_samples != 0) {
19128c2ecf20Sopenharmony_ci		ui__warning("%u unprocessable samples recorded.\n"
19138c2ecf20Sopenharmony_ci			    "Do you have a KVM guest running and not using 'perf kvm'?\n",
19148c2ecf20Sopenharmony_ci			    stats->nr_unprocessable_samples);
19158c2ecf20Sopenharmony_ci	}
19168c2ecf20Sopenharmony_ci
19178c2ecf20Sopenharmony_ci	perf_session__warn_order(session);
19188c2ecf20Sopenharmony_ci
19198c2ecf20Sopenharmony_ci	events_stats__auxtrace_error_warn(stats);
19208c2ecf20Sopenharmony_ci
19218c2ecf20Sopenharmony_ci	if (stats->nr_proc_map_timeout != 0) {
19228c2ecf20Sopenharmony_ci		ui__warning("%d map information files for pre-existing threads were\n"
19238c2ecf20Sopenharmony_ci			    "not processed, if there are samples for addresses they\n"
19248c2ecf20Sopenharmony_ci			    "will not be resolved, you may find out which are these\n"
19258c2ecf20Sopenharmony_ci			    "threads by running with -v and redirecting the output\n"
19268c2ecf20Sopenharmony_ci			    "to a file.\n"
19278c2ecf20Sopenharmony_ci			    "The time limit to process proc map is too short?\n"
19288c2ecf20Sopenharmony_ci			    "Increase it by --proc-map-timeout\n",
19298c2ecf20Sopenharmony_ci			    stats->nr_proc_map_timeout);
19308c2ecf20Sopenharmony_ci	}
19318c2ecf20Sopenharmony_ci}
19328c2ecf20Sopenharmony_ci
19338c2ecf20Sopenharmony_cistatic int perf_session__flush_thread_stack(struct thread *thread,
19348c2ecf20Sopenharmony_ci					    void *p __maybe_unused)
19358c2ecf20Sopenharmony_ci{
19368c2ecf20Sopenharmony_ci	return thread_stack__flush(thread);
19378c2ecf20Sopenharmony_ci}
19388c2ecf20Sopenharmony_ci
19398c2ecf20Sopenharmony_cistatic int perf_session__flush_thread_stacks(struct perf_session *session)
19408c2ecf20Sopenharmony_ci{
19418c2ecf20Sopenharmony_ci	return machines__for_each_thread(&session->machines,
19428c2ecf20Sopenharmony_ci					 perf_session__flush_thread_stack,
19438c2ecf20Sopenharmony_ci					 NULL);
19448c2ecf20Sopenharmony_ci}
19458c2ecf20Sopenharmony_ci
19468c2ecf20Sopenharmony_civolatile int session_done;
19478c2ecf20Sopenharmony_ci
19488c2ecf20Sopenharmony_cistatic int __perf_session__process_decomp_events(struct perf_session *session);
19498c2ecf20Sopenharmony_ci
19508c2ecf20Sopenharmony_cistatic int __perf_session__process_pipe_events(struct perf_session *session)
19518c2ecf20Sopenharmony_ci{
19528c2ecf20Sopenharmony_ci	struct ordered_events *oe = &session->ordered_events;
19538c2ecf20Sopenharmony_ci	struct perf_tool *tool = session->tool;
19548c2ecf20Sopenharmony_ci	int fd = perf_data__fd(session->data);
19558c2ecf20Sopenharmony_ci	union perf_event *event;
19568c2ecf20Sopenharmony_ci	uint32_t size, cur_size = 0;
19578c2ecf20Sopenharmony_ci	void *buf = NULL;
19588c2ecf20Sopenharmony_ci	s64 skip = 0;
19598c2ecf20Sopenharmony_ci	u64 head;
19608c2ecf20Sopenharmony_ci	ssize_t err;
19618c2ecf20Sopenharmony_ci	void *p;
19628c2ecf20Sopenharmony_ci
19638c2ecf20Sopenharmony_ci	perf_tool__fill_defaults(tool);
19648c2ecf20Sopenharmony_ci
19658c2ecf20Sopenharmony_ci	head = 0;
19668c2ecf20Sopenharmony_ci	cur_size = sizeof(union perf_event);
19678c2ecf20Sopenharmony_ci
19688c2ecf20Sopenharmony_ci	buf = malloc(cur_size);
19698c2ecf20Sopenharmony_ci	if (!buf)
19708c2ecf20Sopenharmony_ci		return -errno;
19718c2ecf20Sopenharmony_ci	ordered_events__set_copy_on_queue(oe, true);
19728c2ecf20Sopenharmony_cimore:
19738c2ecf20Sopenharmony_ci	event = buf;
19748c2ecf20Sopenharmony_ci	err = readn(fd, event, sizeof(struct perf_event_header));
19758c2ecf20Sopenharmony_ci	if (err <= 0) {
19768c2ecf20Sopenharmony_ci		if (err == 0)
19778c2ecf20Sopenharmony_ci			goto done;
19788c2ecf20Sopenharmony_ci
19798c2ecf20Sopenharmony_ci		pr_err("failed to read event header\n");
19808c2ecf20Sopenharmony_ci		goto out_err;
19818c2ecf20Sopenharmony_ci	}
19828c2ecf20Sopenharmony_ci
19838c2ecf20Sopenharmony_ci	if (session->header.needs_swap)
19848c2ecf20Sopenharmony_ci		perf_event_header__bswap(&event->header);
19858c2ecf20Sopenharmony_ci
19868c2ecf20Sopenharmony_ci	size = event->header.size;
19878c2ecf20Sopenharmony_ci	if (size < sizeof(struct perf_event_header)) {
19888c2ecf20Sopenharmony_ci		pr_err("bad event header size\n");
19898c2ecf20Sopenharmony_ci		goto out_err;
19908c2ecf20Sopenharmony_ci	}
19918c2ecf20Sopenharmony_ci
19928c2ecf20Sopenharmony_ci	if (size > cur_size) {
19938c2ecf20Sopenharmony_ci		void *new = realloc(buf, size);
19948c2ecf20Sopenharmony_ci		if (!new) {
19958c2ecf20Sopenharmony_ci			pr_err("failed to allocate memory to read event\n");
19968c2ecf20Sopenharmony_ci			goto out_err;
19978c2ecf20Sopenharmony_ci		}
19988c2ecf20Sopenharmony_ci		buf = new;
19998c2ecf20Sopenharmony_ci		cur_size = size;
20008c2ecf20Sopenharmony_ci		event = buf;
20018c2ecf20Sopenharmony_ci	}
20028c2ecf20Sopenharmony_ci	p = event;
20038c2ecf20Sopenharmony_ci	p += sizeof(struct perf_event_header);
20048c2ecf20Sopenharmony_ci
20058c2ecf20Sopenharmony_ci	if (size - sizeof(struct perf_event_header)) {
20068c2ecf20Sopenharmony_ci		err = readn(fd, p, size - sizeof(struct perf_event_header));
20078c2ecf20Sopenharmony_ci		if (err <= 0) {
20088c2ecf20Sopenharmony_ci			if (err == 0) {
20098c2ecf20Sopenharmony_ci				pr_err("unexpected end of event stream\n");
20108c2ecf20Sopenharmony_ci				goto done;
20118c2ecf20Sopenharmony_ci			}
20128c2ecf20Sopenharmony_ci
20138c2ecf20Sopenharmony_ci			pr_err("failed to read event data\n");
20148c2ecf20Sopenharmony_ci			goto out_err;
20158c2ecf20Sopenharmony_ci		}
20168c2ecf20Sopenharmony_ci	}
20178c2ecf20Sopenharmony_ci
20188c2ecf20Sopenharmony_ci	if ((skip = perf_session__process_event(session, event, head)) < 0) {
20198c2ecf20Sopenharmony_ci		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
20208c2ecf20Sopenharmony_ci		       head, event->header.size, event->header.type);
20218c2ecf20Sopenharmony_ci		err = -EINVAL;
20228c2ecf20Sopenharmony_ci		goto out_err;
20238c2ecf20Sopenharmony_ci	}
20248c2ecf20Sopenharmony_ci
20258c2ecf20Sopenharmony_ci	head += size;
20268c2ecf20Sopenharmony_ci
20278c2ecf20Sopenharmony_ci	if (skip > 0)
20288c2ecf20Sopenharmony_ci		head += skip;
20298c2ecf20Sopenharmony_ci
20308c2ecf20Sopenharmony_ci	err = __perf_session__process_decomp_events(session);
20318c2ecf20Sopenharmony_ci	if (err)
20328c2ecf20Sopenharmony_ci		goto out_err;
20338c2ecf20Sopenharmony_ci
20348c2ecf20Sopenharmony_ci	if (!session_done())
20358c2ecf20Sopenharmony_ci		goto more;
20368c2ecf20Sopenharmony_cidone:
20378c2ecf20Sopenharmony_ci	/* do the final flush for ordered samples */
20388c2ecf20Sopenharmony_ci	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
20398c2ecf20Sopenharmony_ci	if (err)
20408c2ecf20Sopenharmony_ci		goto out_err;
20418c2ecf20Sopenharmony_ci	err = auxtrace__flush_events(session, tool);
20428c2ecf20Sopenharmony_ci	if (err)
20438c2ecf20Sopenharmony_ci		goto out_err;
20448c2ecf20Sopenharmony_ci	err = perf_session__flush_thread_stacks(session);
20458c2ecf20Sopenharmony_ciout_err:
20468c2ecf20Sopenharmony_ci	free(buf);
20478c2ecf20Sopenharmony_ci	if (!tool->no_warn)
20488c2ecf20Sopenharmony_ci		perf_session__warn_about_errors(session);
20498c2ecf20Sopenharmony_ci	ordered_events__free(&session->ordered_events);
20508c2ecf20Sopenharmony_ci	auxtrace__free_events(session);
20518c2ecf20Sopenharmony_ci	return err;
20528c2ecf20Sopenharmony_ci}
20538c2ecf20Sopenharmony_ci
20548c2ecf20Sopenharmony_cistatic union perf_event *
20558c2ecf20Sopenharmony_ciprefetch_event(char *buf, u64 head, size_t mmap_size,
20568c2ecf20Sopenharmony_ci	       bool needs_swap, union perf_event *error)
20578c2ecf20Sopenharmony_ci{
20588c2ecf20Sopenharmony_ci	union perf_event *event;
20598c2ecf20Sopenharmony_ci	u16 event_size;
20608c2ecf20Sopenharmony_ci
20618c2ecf20Sopenharmony_ci	/*
20628c2ecf20Sopenharmony_ci	 * Ensure we have enough space remaining to read
20638c2ecf20Sopenharmony_ci	 * the size of the event in the headers.
20648c2ecf20Sopenharmony_ci	 */
20658c2ecf20Sopenharmony_ci	if (head + sizeof(event->header) > mmap_size)
20668c2ecf20Sopenharmony_ci		return NULL;
20678c2ecf20Sopenharmony_ci
20688c2ecf20Sopenharmony_ci	event = (union perf_event *)(buf + head);
20698c2ecf20Sopenharmony_ci	if (needs_swap)
20708c2ecf20Sopenharmony_ci		perf_event_header__bswap(&event->header);
20718c2ecf20Sopenharmony_ci
20728c2ecf20Sopenharmony_ci	event_size = event->header.size;
20738c2ecf20Sopenharmony_ci	if (head + event_size <= mmap_size)
20748c2ecf20Sopenharmony_ci		return event;
20758c2ecf20Sopenharmony_ci
20768c2ecf20Sopenharmony_ci	/* We're not fetching the event so swap back again */
20778c2ecf20Sopenharmony_ci	if (needs_swap)
20788c2ecf20Sopenharmony_ci		perf_event_header__bswap(&event->header);
20798c2ecf20Sopenharmony_ci
20808c2ecf20Sopenharmony_ci	/* Check if the event fits into the next mmapped buf. */
20818c2ecf20Sopenharmony_ci	if (event_size <= mmap_size - head % page_size) {
20828c2ecf20Sopenharmony_ci		/* Remap buf and fetch again. */
20838c2ecf20Sopenharmony_ci		return NULL;
20848c2ecf20Sopenharmony_ci	}
20858c2ecf20Sopenharmony_ci
20868c2ecf20Sopenharmony_ci	/* Invalid input. Event size should never exceed mmap_size. */
20878c2ecf20Sopenharmony_ci	pr_debug("%s: head=%#" PRIx64 " event->header.size=%#x, mmap_size=%#zx:"
20888c2ecf20Sopenharmony_ci		 " fuzzed or compressed perf.data?\n", __func__, head, event_size, mmap_size);
20898c2ecf20Sopenharmony_ci
20908c2ecf20Sopenharmony_ci	return error;
20918c2ecf20Sopenharmony_ci}
20928c2ecf20Sopenharmony_ci
20938c2ecf20Sopenharmony_cistatic union perf_event *
20948c2ecf20Sopenharmony_cifetch_mmaped_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
20958c2ecf20Sopenharmony_ci{
20968c2ecf20Sopenharmony_ci	return prefetch_event(buf, head, mmap_size, needs_swap, ERR_PTR(-EINVAL));
20978c2ecf20Sopenharmony_ci}
20988c2ecf20Sopenharmony_ci
20998c2ecf20Sopenharmony_cistatic union perf_event *
21008c2ecf20Sopenharmony_cifetch_decomp_event(u64 head, size_t mmap_size, char *buf, bool needs_swap)
21018c2ecf20Sopenharmony_ci{
21028c2ecf20Sopenharmony_ci	return prefetch_event(buf, head, mmap_size, needs_swap, NULL);
21038c2ecf20Sopenharmony_ci}
21048c2ecf20Sopenharmony_ci
21058c2ecf20Sopenharmony_cistatic int __perf_session__process_decomp_events(struct perf_session *session)
21068c2ecf20Sopenharmony_ci{
21078c2ecf20Sopenharmony_ci	s64 skip;
21088c2ecf20Sopenharmony_ci	u64 size, file_pos = 0;
21098c2ecf20Sopenharmony_ci	struct decomp *decomp = session->decomp_last;
21108c2ecf20Sopenharmony_ci
21118c2ecf20Sopenharmony_ci	if (!decomp)
21128c2ecf20Sopenharmony_ci		return 0;
21138c2ecf20Sopenharmony_ci
21148c2ecf20Sopenharmony_ci	while (decomp->head < decomp->size && !session_done()) {
21158c2ecf20Sopenharmony_ci		union perf_event *event = fetch_decomp_event(decomp->head, decomp->size, decomp->data,
21168c2ecf20Sopenharmony_ci							     session->header.needs_swap);
21178c2ecf20Sopenharmony_ci
21188c2ecf20Sopenharmony_ci		if (!event)
21198c2ecf20Sopenharmony_ci			break;
21208c2ecf20Sopenharmony_ci
21218c2ecf20Sopenharmony_ci		size = event->header.size;
21228c2ecf20Sopenharmony_ci
21238c2ecf20Sopenharmony_ci		if (size < sizeof(struct perf_event_header) ||
21248c2ecf20Sopenharmony_ci		    (skip = perf_session__process_event(session, event, file_pos)) < 0) {
21258c2ecf20Sopenharmony_ci			pr_err("%#" PRIx64 " [%#x]: failed to process type: %d\n",
21268c2ecf20Sopenharmony_ci				decomp->file_pos + decomp->head, event->header.size, event->header.type);
21278c2ecf20Sopenharmony_ci			return -EINVAL;
21288c2ecf20Sopenharmony_ci		}
21298c2ecf20Sopenharmony_ci
21308c2ecf20Sopenharmony_ci		if (skip)
21318c2ecf20Sopenharmony_ci			size += skip;
21328c2ecf20Sopenharmony_ci
21338c2ecf20Sopenharmony_ci		decomp->head += size;
21348c2ecf20Sopenharmony_ci	}
21358c2ecf20Sopenharmony_ci
21368c2ecf20Sopenharmony_ci	return 0;
21378c2ecf20Sopenharmony_ci}
21388c2ecf20Sopenharmony_ci
21398c2ecf20Sopenharmony_ci/*
21408c2ecf20Sopenharmony_ci * On 64bit we can mmap the data file in one go. No need for tiny mmap
21418c2ecf20Sopenharmony_ci * slices. On 32bit we use 32MB.
21428c2ecf20Sopenharmony_ci */
21438c2ecf20Sopenharmony_ci#if BITS_PER_LONG == 64
21448c2ecf20Sopenharmony_ci#define MMAP_SIZE ULLONG_MAX
21458c2ecf20Sopenharmony_ci#define NUM_MMAPS 1
21468c2ecf20Sopenharmony_ci#else
21478c2ecf20Sopenharmony_ci#define MMAP_SIZE (32 * 1024 * 1024ULL)
21488c2ecf20Sopenharmony_ci#define NUM_MMAPS 128
21498c2ecf20Sopenharmony_ci#endif
21508c2ecf20Sopenharmony_ci
21518c2ecf20Sopenharmony_cistruct reader;
21528c2ecf20Sopenharmony_ci
21538c2ecf20Sopenharmony_citypedef s64 (*reader_cb_t)(struct perf_session *session,
21548c2ecf20Sopenharmony_ci			   union perf_event *event,
21558c2ecf20Sopenharmony_ci			   u64 file_offset);
21568c2ecf20Sopenharmony_ci
21578c2ecf20Sopenharmony_cistruct reader {
21588c2ecf20Sopenharmony_ci	int		 fd;
21598c2ecf20Sopenharmony_ci	u64		 data_size;
21608c2ecf20Sopenharmony_ci	u64		 data_offset;
21618c2ecf20Sopenharmony_ci	reader_cb_t	 process;
21628c2ecf20Sopenharmony_ci};
21638c2ecf20Sopenharmony_ci
21648c2ecf20Sopenharmony_cistatic int
21658c2ecf20Sopenharmony_cireader__process_events(struct reader *rd, struct perf_session *session,
21668c2ecf20Sopenharmony_ci		       struct ui_progress *prog)
21678c2ecf20Sopenharmony_ci{
21688c2ecf20Sopenharmony_ci	u64 data_size = rd->data_size;
21698c2ecf20Sopenharmony_ci	u64 head, page_offset, file_offset, file_pos, size;
21708c2ecf20Sopenharmony_ci	int err = 0, mmap_prot, mmap_flags, map_idx = 0;
21718c2ecf20Sopenharmony_ci	size_t	mmap_size;
21728c2ecf20Sopenharmony_ci	char *buf, *mmaps[NUM_MMAPS];
21738c2ecf20Sopenharmony_ci	union perf_event *event;
21748c2ecf20Sopenharmony_ci	s64 skip;
21758c2ecf20Sopenharmony_ci
21768c2ecf20Sopenharmony_ci	page_offset = page_size * (rd->data_offset / page_size);
21778c2ecf20Sopenharmony_ci	file_offset = page_offset;
21788c2ecf20Sopenharmony_ci	head = rd->data_offset - page_offset;
21798c2ecf20Sopenharmony_ci
21808c2ecf20Sopenharmony_ci	ui_progress__init_size(prog, data_size, "Processing events...");
21818c2ecf20Sopenharmony_ci
21828c2ecf20Sopenharmony_ci	data_size += rd->data_offset;
21838c2ecf20Sopenharmony_ci
21848c2ecf20Sopenharmony_ci	mmap_size = MMAP_SIZE;
21858c2ecf20Sopenharmony_ci	if (mmap_size > data_size) {
21868c2ecf20Sopenharmony_ci		mmap_size = data_size;
21878c2ecf20Sopenharmony_ci		session->one_mmap = true;
21888c2ecf20Sopenharmony_ci	}
21898c2ecf20Sopenharmony_ci
21908c2ecf20Sopenharmony_ci	memset(mmaps, 0, sizeof(mmaps));
21918c2ecf20Sopenharmony_ci
21928c2ecf20Sopenharmony_ci	mmap_prot  = PROT_READ;
21938c2ecf20Sopenharmony_ci	mmap_flags = MAP_SHARED;
21948c2ecf20Sopenharmony_ci
21958c2ecf20Sopenharmony_ci	if (session->header.needs_swap) {
21968c2ecf20Sopenharmony_ci		mmap_prot  |= PROT_WRITE;
21978c2ecf20Sopenharmony_ci		mmap_flags = MAP_PRIVATE;
21988c2ecf20Sopenharmony_ci	}
21998c2ecf20Sopenharmony_ciremap:
22008c2ecf20Sopenharmony_ci	buf = mmap(NULL, mmap_size, mmap_prot, mmap_flags, rd->fd,
22018c2ecf20Sopenharmony_ci		   file_offset);
22028c2ecf20Sopenharmony_ci	if (buf == MAP_FAILED) {
22038c2ecf20Sopenharmony_ci		pr_err("failed to mmap file\n");
22048c2ecf20Sopenharmony_ci		err = -errno;
22058c2ecf20Sopenharmony_ci		goto out;
22068c2ecf20Sopenharmony_ci	}
22078c2ecf20Sopenharmony_ci	mmaps[map_idx] = buf;
22088c2ecf20Sopenharmony_ci	map_idx = (map_idx + 1) & (ARRAY_SIZE(mmaps) - 1);
22098c2ecf20Sopenharmony_ci	file_pos = file_offset + head;
22108c2ecf20Sopenharmony_ci	if (session->one_mmap) {
22118c2ecf20Sopenharmony_ci		session->one_mmap_addr = buf;
22128c2ecf20Sopenharmony_ci		session->one_mmap_offset = file_offset;
22138c2ecf20Sopenharmony_ci	}
22148c2ecf20Sopenharmony_ci
22158c2ecf20Sopenharmony_cimore:
22168c2ecf20Sopenharmony_ci	event = fetch_mmaped_event(head, mmap_size, buf, session->header.needs_swap);
22178c2ecf20Sopenharmony_ci	if (IS_ERR(event))
22188c2ecf20Sopenharmony_ci		return PTR_ERR(event);
22198c2ecf20Sopenharmony_ci
22208c2ecf20Sopenharmony_ci	if (!event) {
22218c2ecf20Sopenharmony_ci		if (mmaps[map_idx]) {
22228c2ecf20Sopenharmony_ci			munmap(mmaps[map_idx], mmap_size);
22238c2ecf20Sopenharmony_ci			mmaps[map_idx] = NULL;
22248c2ecf20Sopenharmony_ci		}
22258c2ecf20Sopenharmony_ci
22268c2ecf20Sopenharmony_ci		page_offset = page_size * (head / page_size);
22278c2ecf20Sopenharmony_ci		file_offset += page_offset;
22288c2ecf20Sopenharmony_ci		head -= page_offset;
22298c2ecf20Sopenharmony_ci		goto remap;
22308c2ecf20Sopenharmony_ci	}
22318c2ecf20Sopenharmony_ci
22328c2ecf20Sopenharmony_ci	size = event->header.size;
22338c2ecf20Sopenharmony_ci
22348c2ecf20Sopenharmony_ci	skip = -EINVAL;
22358c2ecf20Sopenharmony_ci
22368c2ecf20Sopenharmony_ci	if (size < sizeof(struct perf_event_header) ||
22378c2ecf20Sopenharmony_ci	    (skip = rd->process(session, event, file_pos)) < 0) {
22388c2ecf20Sopenharmony_ci		pr_err("%#" PRIx64 " [%#x]: failed to process type: %d [%s]\n",
22398c2ecf20Sopenharmony_ci		       file_offset + head, event->header.size,
22408c2ecf20Sopenharmony_ci		       event->header.type, strerror(-skip));
22418c2ecf20Sopenharmony_ci		err = skip;
22428c2ecf20Sopenharmony_ci		goto out;
22438c2ecf20Sopenharmony_ci	}
22448c2ecf20Sopenharmony_ci
22458c2ecf20Sopenharmony_ci	if (skip)
22468c2ecf20Sopenharmony_ci		size += skip;
22478c2ecf20Sopenharmony_ci
22488c2ecf20Sopenharmony_ci	head += size;
22498c2ecf20Sopenharmony_ci	file_pos += size;
22508c2ecf20Sopenharmony_ci
22518c2ecf20Sopenharmony_ci	err = __perf_session__process_decomp_events(session);
22528c2ecf20Sopenharmony_ci	if (err)
22538c2ecf20Sopenharmony_ci		goto out;
22548c2ecf20Sopenharmony_ci
22558c2ecf20Sopenharmony_ci	ui_progress__update(prog, size);
22568c2ecf20Sopenharmony_ci
22578c2ecf20Sopenharmony_ci	if (session_done())
22588c2ecf20Sopenharmony_ci		goto out;
22598c2ecf20Sopenharmony_ci
22608c2ecf20Sopenharmony_ci	if (file_pos < data_size)
22618c2ecf20Sopenharmony_ci		goto more;
22628c2ecf20Sopenharmony_ci
22638c2ecf20Sopenharmony_ciout:
22648c2ecf20Sopenharmony_ci	return err;
22658c2ecf20Sopenharmony_ci}
22668c2ecf20Sopenharmony_ci
22678c2ecf20Sopenharmony_cistatic s64 process_simple(struct perf_session *session,
22688c2ecf20Sopenharmony_ci			  union perf_event *event,
22698c2ecf20Sopenharmony_ci			  u64 file_offset)
22708c2ecf20Sopenharmony_ci{
22718c2ecf20Sopenharmony_ci	return perf_session__process_event(session, event, file_offset);
22728c2ecf20Sopenharmony_ci}
22738c2ecf20Sopenharmony_ci
22748c2ecf20Sopenharmony_cistatic int __perf_session__process_events(struct perf_session *session)
22758c2ecf20Sopenharmony_ci{
22768c2ecf20Sopenharmony_ci	struct reader rd = {
22778c2ecf20Sopenharmony_ci		.fd		= perf_data__fd(session->data),
22788c2ecf20Sopenharmony_ci		.data_size	= session->header.data_size,
22798c2ecf20Sopenharmony_ci		.data_offset	= session->header.data_offset,
22808c2ecf20Sopenharmony_ci		.process	= process_simple,
22818c2ecf20Sopenharmony_ci	};
22828c2ecf20Sopenharmony_ci	struct ordered_events *oe = &session->ordered_events;
22838c2ecf20Sopenharmony_ci	struct perf_tool *tool = session->tool;
22848c2ecf20Sopenharmony_ci	struct ui_progress prog;
22858c2ecf20Sopenharmony_ci	int err;
22868c2ecf20Sopenharmony_ci
22878c2ecf20Sopenharmony_ci	perf_tool__fill_defaults(tool);
22888c2ecf20Sopenharmony_ci
22898c2ecf20Sopenharmony_ci	if (rd.data_size == 0)
22908c2ecf20Sopenharmony_ci		return -1;
22918c2ecf20Sopenharmony_ci
22928c2ecf20Sopenharmony_ci	ui_progress__init_size(&prog, rd.data_size, "Processing events...");
22938c2ecf20Sopenharmony_ci
22948c2ecf20Sopenharmony_ci	err = reader__process_events(&rd, session, &prog);
22958c2ecf20Sopenharmony_ci	if (err)
22968c2ecf20Sopenharmony_ci		goto out_err;
22978c2ecf20Sopenharmony_ci	/* do the final flush for ordered samples */
22988c2ecf20Sopenharmony_ci	err = ordered_events__flush(oe, OE_FLUSH__FINAL);
22998c2ecf20Sopenharmony_ci	if (err)
23008c2ecf20Sopenharmony_ci		goto out_err;
23018c2ecf20Sopenharmony_ci	err = auxtrace__flush_events(session, tool);
23028c2ecf20Sopenharmony_ci	if (err)
23038c2ecf20Sopenharmony_ci		goto out_err;
23048c2ecf20Sopenharmony_ci	err = perf_session__flush_thread_stacks(session);
23058c2ecf20Sopenharmony_ciout_err:
23068c2ecf20Sopenharmony_ci	ui_progress__finish();
23078c2ecf20Sopenharmony_ci	if (!tool->no_warn)
23088c2ecf20Sopenharmony_ci		perf_session__warn_about_errors(session);
23098c2ecf20Sopenharmony_ci	/*
23108c2ecf20Sopenharmony_ci	 * We may switching perf.data output, make ordered_events
23118c2ecf20Sopenharmony_ci	 * reusable.
23128c2ecf20Sopenharmony_ci	 */
23138c2ecf20Sopenharmony_ci	ordered_events__reinit(&session->ordered_events);
23148c2ecf20Sopenharmony_ci	auxtrace__free_events(session);
23158c2ecf20Sopenharmony_ci	session->one_mmap = false;
23168c2ecf20Sopenharmony_ci	return err;
23178c2ecf20Sopenharmony_ci}
23188c2ecf20Sopenharmony_ci
23198c2ecf20Sopenharmony_ciint perf_session__process_events(struct perf_session *session)
23208c2ecf20Sopenharmony_ci{
23218c2ecf20Sopenharmony_ci	if (perf_session__register_idle_thread(session) < 0)
23228c2ecf20Sopenharmony_ci		return -ENOMEM;
23238c2ecf20Sopenharmony_ci
23248c2ecf20Sopenharmony_ci	if (perf_data__is_pipe(session->data))
23258c2ecf20Sopenharmony_ci		return __perf_session__process_pipe_events(session);
23268c2ecf20Sopenharmony_ci
23278c2ecf20Sopenharmony_ci	return __perf_session__process_events(session);
23288c2ecf20Sopenharmony_ci}
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_cibool perf_session__has_traces(struct perf_session *session, const char *msg)
23318c2ecf20Sopenharmony_ci{
23328c2ecf20Sopenharmony_ci	struct evsel *evsel;
23338c2ecf20Sopenharmony_ci
23348c2ecf20Sopenharmony_ci	evlist__for_each_entry(session->evlist, evsel) {
23358c2ecf20Sopenharmony_ci		if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT)
23368c2ecf20Sopenharmony_ci			return true;
23378c2ecf20Sopenharmony_ci	}
23388c2ecf20Sopenharmony_ci
23398c2ecf20Sopenharmony_ci	pr_err("No trace sample to read. Did you call 'perf %s'?\n", msg);
23408c2ecf20Sopenharmony_ci	return false;
23418c2ecf20Sopenharmony_ci}
23428c2ecf20Sopenharmony_ci
23438c2ecf20Sopenharmony_ciint map__set_kallsyms_ref_reloc_sym(struct map *map, const char *symbol_name, u64 addr)
23448c2ecf20Sopenharmony_ci{
23458c2ecf20Sopenharmony_ci	char *bracket;
23468c2ecf20Sopenharmony_ci	struct ref_reloc_sym *ref;
23478c2ecf20Sopenharmony_ci	struct kmap *kmap;
23488c2ecf20Sopenharmony_ci
23498c2ecf20Sopenharmony_ci	ref = zalloc(sizeof(struct ref_reloc_sym));
23508c2ecf20Sopenharmony_ci	if (ref == NULL)
23518c2ecf20Sopenharmony_ci		return -ENOMEM;
23528c2ecf20Sopenharmony_ci
23538c2ecf20Sopenharmony_ci	ref->name = strdup(symbol_name);
23548c2ecf20Sopenharmony_ci	if (ref->name == NULL) {
23558c2ecf20Sopenharmony_ci		free(ref);
23568c2ecf20Sopenharmony_ci		return -ENOMEM;
23578c2ecf20Sopenharmony_ci	}
23588c2ecf20Sopenharmony_ci
23598c2ecf20Sopenharmony_ci	bracket = strchr(ref->name, ']');
23608c2ecf20Sopenharmony_ci	if (bracket)
23618c2ecf20Sopenharmony_ci		*bracket = '\0';
23628c2ecf20Sopenharmony_ci
23638c2ecf20Sopenharmony_ci	ref->addr = addr;
23648c2ecf20Sopenharmony_ci
23658c2ecf20Sopenharmony_ci	kmap = map__kmap(map);
23668c2ecf20Sopenharmony_ci	if (kmap)
23678c2ecf20Sopenharmony_ci		kmap->ref_reloc_sym = ref;
23688c2ecf20Sopenharmony_ci
23698c2ecf20Sopenharmony_ci	return 0;
23708c2ecf20Sopenharmony_ci}
23718c2ecf20Sopenharmony_ci
23728c2ecf20Sopenharmony_cisize_t perf_session__fprintf_dsos(struct perf_session *session, FILE *fp)
23738c2ecf20Sopenharmony_ci{
23748c2ecf20Sopenharmony_ci	return machines__fprintf_dsos(&session->machines, fp);
23758c2ecf20Sopenharmony_ci}
23768c2ecf20Sopenharmony_ci
23778c2ecf20Sopenharmony_cisize_t perf_session__fprintf_dsos_buildid(struct perf_session *session, FILE *fp,
23788c2ecf20Sopenharmony_ci					  bool (skip)(struct dso *dso, int parm), int parm)
23798c2ecf20Sopenharmony_ci{
23808c2ecf20Sopenharmony_ci	return machines__fprintf_dsos_buildid(&session->machines, fp, skip, parm);
23818c2ecf20Sopenharmony_ci}
23828c2ecf20Sopenharmony_ci
23838c2ecf20Sopenharmony_cisize_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp)
23848c2ecf20Sopenharmony_ci{
23858c2ecf20Sopenharmony_ci	size_t ret;
23868c2ecf20Sopenharmony_ci	const char *msg = "";
23878c2ecf20Sopenharmony_ci
23888c2ecf20Sopenharmony_ci	if (perf_header__has_feat(&session->header, HEADER_AUXTRACE))
23898c2ecf20Sopenharmony_ci		msg = " (excludes AUX area (e.g. instruction trace) decoded / synthesized events)";
23908c2ecf20Sopenharmony_ci
23918c2ecf20Sopenharmony_ci	ret = fprintf(fp, "\nAggregated stats:%s\n", msg);
23928c2ecf20Sopenharmony_ci
23938c2ecf20Sopenharmony_ci	ret += events_stats__fprintf(&session->evlist->stats, fp);
23948c2ecf20Sopenharmony_ci	return ret;
23958c2ecf20Sopenharmony_ci}
23968c2ecf20Sopenharmony_ci
23978c2ecf20Sopenharmony_cisize_t perf_session__fprintf(struct perf_session *session, FILE *fp)
23988c2ecf20Sopenharmony_ci{
23998c2ecf20Sopenharmony_ci	/*
24008c2ecf20Sopenharmony_ci	 * FIXME: Here we have to actually print all the machines in this
24018c2ecf20Sopenharmony_ci	 * session, not just the host...
24028c2ecf20Sopenharmony_ci	 */
24038c2ecf20Sopenharmony_ci	return machine__fprintf(&session->machines.host, fp);
24048c2ecf20Sopenharmony_ci}
24058c2ecf20Sopenharmony_ci
24068c2ecf20Sopenharmony_cistruct evsel *perf_session__find_first_evtype(struct perf_session *session,
24078c2ecf20Sopenharmony_ci					      unsigned int type)
24088c2ecf20Sopenharmony_ci{
24098c2ecf20Sopenharmony_ci	struct evsel *pos;
24108c2ecf20Sopenharmony_ci
24118c2ecf20Sopenharmony_ci	evlist__for_each_entry(session->evlist, pos) {
24128c2ecf20Sopenharmony_ci		if (pos->core.attr.type == type)
24138c2ecf20Sopenharmony_ci			return pos;
24148c2ecf20Sopenharmony_ci	}
24158c2ecf20Sopenharmony_ci	return NULL;
24168c2ecf20Sopenharmony_ci}
24178c2ecf20Sopenharmony_ci
24188c2ecf20Sopenharmony_ciint perf_session__cpu_bitmap(struct perf_session *session,
24198c2ecf20Sopenharmony_ci			     const char *cpu_list, unsigned long *cpu_bitmap)
24208c2ecf20Sopenharmony_ci{
24218c2ecf20Sopenharmony_ci	int i, err = -1;
24228c2ecf20Sopenharmony_ci	struct perf_cpu_map *map;
24238c2ecf20Sopenharmony_ci	int nr_cpus = min(session->header.env.nr_cpus_avail, MAX_NR_CPUS);
24248c2ecf20Sopenharmony_ci
24258c2ecf20Sopenharmony_ci	for (i = 0; i < PERF_TYPE_MAX; ++i) {
24268c2ecf20Sopenharmony_ci		struct evsel *evsel;
24278c2ecf20Sopenharmony_ci
24288c2ecf20Sopenharmony_ci		evsel = perf_session__find_first_evtype(session, i);
24298c2ecf20Sopenharmony_ci		if (!evsel)
24308c2ecf20Sopenharmony_ci			continue;
24318c2ecf20Sopenharmony_ci
24328c2ecf20Sopenharmony_ci		if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CPU)) {
24338c2ecf20Sopenharmony_ci			pr_err("File does not contain CPU events. "
24348c2ecf20Sopenharmony_ci			       "Remove -C option to proceed.\n");
24358c2ecf20Sopenharmony_ci			return -1;
24368c2ecf20Sopenharmony_ci		}
24378c2ecf20Sopenharmony_ci	}
24388c2ecf20Sopenharmony_ci
24398c2ecf20Sopenharmony_ci	map = perf_cpu_map__new(cpu_list);
24408c2ecf20Sopenharmony_ci	if (map == NULL) {
24418c2ecf20Sopenharmony_ci		pr_err("Invalid cpu_list\n");
24428c2ecf20Sopenharmony_ci		return -1;
24438c2ecf20Sopenharmony_ci	}
24448c2ecf20Sopenharmony_ci
24458c2ecf20Sopenharmony_ci	for (i = 0; i < map->nr; i++) {
24468c2ecf20Sopenharmony_ci		int cpu = map->map[i];
24478c2ecf20Sopenharmony_ci
24488c2ecf20Sopenharmony_ci		if (cpu >= nr_cpus) {
24498c2ecf20Sopenharmony_ci			pr_err("Requested CPU %d too large. "
24508c2ecf20Sopenharmony_ci			       "Consider raising MAX_NR_CPUS\n", cpu);
24518c2ecf20Sopenharmony_ci			goto out_delete_map;
24528c2ecf20Sopenharmony_ci		}
24538c2ecf20Sopenharmony_ci
24548c2ecf20Sopenharmony_ci		set_bit(cpu, cpu_bitmap);
24558c2ecf20Sopenharmony_ci	}
24568c2ecf20Sopenharmony_ci
24578c2ecf20Sopenharmony_ci	err = 0;
24588c2ecf20Sopenharmony_ci
24598c2ecf20Sopenharmony_ciout_delete_map:
24608c2ecf20Sopenharmony_ci	perf_cpu_map__put(map);
24618c2ecf20Sopenharmony_ci	return err;
24628c2ecf20Sopenharmony_ci}
24638c2ecf20Sopenharmony_ci
24648c2ecf20Sopenharmony_civoid perf_session__fprintf_info(struct perf_session *session, FILE *fp,
24658c2ecf20Sopenharmony_ci				bool full)
24668c2ecf20Sopenharmony_ci{
24678c2ecf20Sopenharmony_ci	if (session == NULL || fp == NULL)
24688c2ecf20Sopenharmony_ci		return;
24698c2ecf20Sopenharmony_ci
24708c2ecf20Sopenharmony_ci	fprintf(fp, "# ========\n");
24718c2ecf20Sopenharmony_ci	perf_header__fprintf_info(session, fp, full);
24728c2ecf20Sopenharmony_ci	fprintf(fp, "# ========\n#\n");
24738c2ecf20Sopenharmony_ci}
24748c2ecf20Sopenharmony_ci
24758c2ecf20Sopenharmony_ciint perf_event__process_id_index(struct perf_session *session,
24768c2ecf20Sopenharmony_ci				 union perf_event *event)
24778c2ecf20Sopenharmony_ci{
24788c2ecf20Sopenharmony_ci	struct evlist *evlist = session->evlist;
24798c2ecf20Sopenharmony_ci	struct perf_record_id_index *ie = &event->id_index;
24808c2ecf20Sopenharmony_ci	size_t i, nr, max_nr;
24818c2ecf20Sopenharmony_ci
24828c2ecf20Sopenharmony_ci	max_nr = (ie->header.size - sizeof(struct perf_record_id_index)) /
24838c2ecf20Sopenharmony_ci		 sizeof(struct id_index_entry);
24848c2ecf20Sopenharmony_ci	nr = ie->nr;
24858c2ecf20Sopenharmony_ci	if (nr > max_nr)
24868c2ecf20Sopenharmony_ci		return -EINVAL;
24878c2ecf20Sopenharmony_ci
24888c2ecf20Sopenharmony_ci	if (dump_trace)
24898c2ecf20Sopenharmony_ci		fprintf(stdout, " nr: %zu\n", nr);
24908c2ecf20Sopenharmony_ci
24918c2ecf20Sopenharmony_ci	for (i = 0; i < nr; i++) {
24928c2ecf20Sopenharmony_ci		struct id_index_entry *e = &ie->entries[i];
24938c2ecf20Sopenharmony_ci		struct perf_sample_id *sid;
24948c2ecf20Sopenharmony_ci
24958c2ecf20Sopenharmony_ci		if (dump_trace) {
24968c2ecf20Sopenharmony_ci			fprintf(stdout,	" ... id: %"PRI_lu64, e->id);
24978c2ecf20Sopenharmony_ci			fprintf(stdout,	"  idx: %"PRI_lu64, e->idx);
24988c2ecf20Sopenharmony_ci			fprintf(stdout,	"  cpu: %"PRI_ld64, e->cpu);
24998c2ecf20Sopenharmony_ci			fprintf(stdout,	"  tid: %"PRI_ld64"\n", e->tid);
25008c2ecf20Sopenharmony_ci		}
25018c2ecf20Sopenharmony_ci
25028c2ecf20Sopenharmony_ci		sid = perf_evlist__id2sid(evlist, e->id);
25038c2ecf20Sopenharmony_ci		if (!sid)
25048c2ecf20Sopenharmony_ci			return -ENOENT;
25058c2ecf20Sopenharmony_ci		sid->idx = e->idx;
25068c2ecf20Sopenharmony_ci		sid->cpu = e->cpu;
25078c2ecf20Sopenharmony_ci		sid->tid = e->tid;
25088c2ecf20Sopenharmony_ci	}
25098c2ecf20Sopenharmony_ci	return 0;
25108c2ecf20Sopenharmony_ci}
2511