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