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