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