1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16#ifndef __PERF_AUXTRACE_H
17#define __PERF_AUXTRACE_H
18
19#include <sys/types.h>
20#include <errno.h>
21#include <stdbool.h>
22#include <stddef.h>
23#include <linux/list.h>
24#include <linux/perf_event.h>
25#include <linux/types.h>
26
27#include "../perf.h"
28#include "event.h"
29#include "session.h"
30#include "debug.h"
31
32union perf_event;
33struct perf_session;
34struct perf_evlist;
35struct perf_tool;
36struct option;
37struct record_opts;
38struct auxtrace_info_event;
39struct events_stats;
40
41enum auxtrace_type {
42 PERF_AUXTRACE_UNKNOWN,
43 PERF_AUXTRACE_INTEL_PT,
44 PERF_AUXTRACE_INTEL_BTS,
45 PERF_AUXTRACE_CS_ETM,
46};
47
48enum itrace_period_type {
49 PERF_ITRACE_PERIOD_INSTRUCTIONS,
50 PERF_ITRACE_PERIOD_TICKS,
51 PERF_ITRACE_PERIOD_NANOSECS,
52};
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79struct itrace_synth_opts {
80 bool set;
81 bool inject;
82 bool instructions;
83 bool branches;
84 bool transactions;
85 bool ptwrites;
86 bool pwr_events;
87 bool errors;
88 bool dont_decode;
89 bool log;
90 bool calls;
91 bool returns;
92 bool callchain;
93 bool thread_stack;
94 bool last_branch;
95 unsigned int callchain_sz;
96 unsigned int last_branch_sz;
97 unsigned long long period;
98 enum itrace_period_type period_type;
99 unsigned long initial_skip;
100 unsigned long *cpu_bitmap;
101};
102
103
104
105
106
107
108
109struct auxtrace_index_entry {
110 u64 file_offset;
111 u64 sz;
112};
113
114#define PERF_AUXTRACE_INDEX_ENTRY_COUNT 256
115
116
117
118
119
120
121
122
123struct auxtrace_index {
124 struct list_head list;
125 size_t nr;
126 struct auxtrace_index_entry entries[PERF_AUXTRACE_INDEX_ENTRY_COUNT];
127};
128
129
130
131
132
133
134
135
136struct auxtrace {
137 int (*process_event)(struct perf_session *session,
138 union perf_event *event,
139 struct perf_sample *sample,
140 struct perf_tool *tool);
141 int (*process_auxtrace_event)(struct perf_session *session,
142 union perf_event *event,
143 struct perf_tool *tool);
144 int (*flush_events)(struct perf_session *session,
145 struct perf_tool *tool);
146 void (*free_events)(struct perf_session *session);
147 void (*free)(struct perf_session *session);
148};
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173struct auxtrace_buffer {
174 struct list_head list;
175 size_t size;
176 pid_t pid;
177 pid_t tid;
178 int cpu;
179 void *data;
180 off_t data_offset;
181 void *mmap_addr;
182 size_t mmap_size;
183 bool data_needs_freeing;
184 bool consecutive;
185 u64 offset;
186 u64 reference;
187 u64 buffer_nr;
188 size_t use_size;
189 void *use_data;
190};
191
192
193
194
195
196
197
198
199
200struct auxtrace_queue {
201 struct list_head head;
202 pid_t tid;
203 int cpu;
204 bool set;
205 void *priv;
206};
207
208
209
210
211
212
213
214
215
216struct auxtrace_queues {
217 struct auxtrace_queue *queue_array;
218 unsigned int nr_queues;
219 bool new_data;
220 bool populated;
221 u64 next_buffer_nr;
222};
223
224
225
226
227
228
229
230struct auxtrace_heap_item {
231 unsigned int queue_nr;
232 u64 ordinal;
233};
234
235
236
237
238
239
240
241struct auxtrace_heap {
242 struct auxtrace_heap_item *heap_array;
243 unsigned int heap_cnt;
244 unsigned int heap_sz;
245};
246
247
248
249
250
251
252
253
254
255
256
257
258
259struct auxtrace_mmap {
260 void *base;
261 void *userpg;
262 size_t mask;
263 size_t len;
264 u64 prev;
265 int idx;
266 pid_t tid;
267 int cpu;
268};
269
270
271
272
273
274
275
276
277
278
279
280
281struct auxtrace_mmap_params {
282 size_t mask;
283 off_t offset;
284 size_t len;
285 int prot;
286 int idx;
287 pid_t tid;
288 int cpu;
289};
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304struct auxtrace_record {
305 int (*recording_options)(struct auxtrace_record *itr,
306 struct perf_evlist *evlist,
307 struct record_opts *opts);
308 size_t (*info_priv_size)(struct auxtrace_record *itr,
309 struct perf_evlist *evlist);
310 int (*info_fill)(struct auxtrace_record *itr,
311 struct perf_session *session,
312 struct auxtrace_info_event *auxtrace_info,
313 size_t priv_size);
314 void (*free)(struct auxtrace_record *itr);
315 int (*snapshot_start)(struct auxtrace_record *itr);
316 int (*snapshot_finish)(struct auxtrace_record *itr);
317 int (*find_snapshot)(struct auxtrace_record *itr, int idx,
318 struct auxtrace_mmap *mm, unsigned char *data,
319 u64 *head, u64 *old);
320 int (*parse_snapshot_options)(struct auxtrace_record *itr,
321 struct record_opts *opts,
322 const char *str);
323 u64 (*reference)(struct auxtrace_record *itr);
324 int (*read_finish)(struct auxtrace_record *itr, int idx);
325 unsigned int alignment;
326};
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345struct addr_filter {
346 struct list_head list;
347 bool range;
348 bool start;
349 const char *action;
350 const char *sym_from;
351 const char *sym_to;
352 int sym_from_idx;
353 int sym_to_idx;
354 u64 addr;
355 u64 size;
356 const char *filename;
357 char *str;
358};
359
360
361
362
363
364
365struct addr_filters {
366 struct list_head head;
367 int cnt;
368};
369
370#ifdef HAVE_AUXTRACE_SUPPORT
371
372
373
374
375
376
377
378static inline u64 auxtrace_mmap__read_snapshot_head(struct auxtrace_mmap *mm)
379{
380 struct perf_event_mmap_page *pc = mm->userpg;
381 u64 head = ACCESS_ONCE(pc->aux_head);
382
383
384 rmb();
385 return head;
386}
387
388static inline u64 auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
389{
390 struct perf_event_mmap_page *pc = mm->userpg;
391#if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
392 u64 head = ACCESS_ONCE(pc->aux_head);
393#else
394 u64 head = __sync_val_compare_and_swap(&pc->aux_head, 0, 0);
395#endif
396
397
398 rmb();
399 return head;
400}
401
402static inline void auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
403{
404 struct perf_event_mmap_page *pc = mm->userpg;
405#if BITS_PER_LONG != 64 && defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
406 u64 old_tail;
407#endif
408
409
410 mb();
411#if BITS_PER_LONG == 64 || !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
412 pc->aux_tail = tail;
413#else
414 do {
415 old_tail = __sync_val_compare_and_swap(&pc->aux_tail, 0, 0);
416 } while (!__sync_bool_compare_and_swap(&pc->aux_tail, old_tail, tail));
417#endif
418}
419
420int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
421 struct auxtrace_mmap_params *mp,
422 void *userpg, int fd);
423void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
424void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
425 off_t auxtrace_offset,
426 unsigned int auxtrace_pages,
427 bool auxtrace_overwrite);
428void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
429 struct perf_evlist *evlist, int idx,
430 bool per_cpu);
431
432typedef int (*process_auxtrace_t)(struct perf_tool *tool,
433 union perf_event *event, void *data1,
434 size_t len1, void *data2, size_t len2);
435
436int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
437 struct perf_tool *tool, process_auxtrace_t fn);
438
439int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
440 struct auxtrace_record *itr,
441 struct perf_tool *tool, process_auxtrace_t fn,
442 size_t snapshot_size);
443
444int auxtrace_queues__init(struct auxtrace_queues *queues);
445int auxtrace_queues__add_event(struct auxtrace_queues *queues,
446 struct perf_session *session,
447 union perf_event *event, off_t data_offset,
448 struct auxtrace_buffer **buffer_ptr);
449void auxtrace_queues__free(struct auxtrace_queues *queues);
450int auxtrace_queues__process_index(struct auxtrace_queues *queues,
451 struct perf_session *session);
452struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
453 struct auxtrace_buffer *buffer);
454void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd);
455void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer);
456void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer);
457void auxtrace_buffer__free(struct auxtrace_buffer *buffer);
458
459int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
460 u64 ordinal);
461void auxtrace_heap__pop(struct auxtrace_heap *heap);
462void auxtrace_heap__free(struct auxtrace_heap *heap);
463
464struct auxtrace_cache_entry {
465 struct hlist_node hash;
466 u32 key;
467};
468
469struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
470 unsigned int limit_percent);
471void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache);
472void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c);
473void auxtrace_cache__free_entry(struct auxtrace_cache *c, void *entry);
474int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
475 struct auxtrace_cache_entry *entry);
476void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key);
477
478struct auxtrace_record *auxtrace_record__init(struct perf_evlist *evlist,
479 int *err);
480
481int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
482 struct record_opts *opts,
483 const char *str);
484int auxtrace_record__options(struct auxtrace_record *itr,
485 struct perf_evlist *evlist,
486 struct record_opts *opts);
487size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
488 struct perf_evlist *evlist);
489int auxtrace_record__info_fill(struct auxtrace_record *itr,
490 struct perf_session *session,
491 struct auxtrace_info_event *auxtrace_info,
492 size_t priv_size);
493void auxtrace_record__free(struct auxtrace_record *itr);
494int auxtrace_record__snapshot_start(struct auxtrace_record *itr);
495int auxtrace_record__snapshot_finish(struct auxtrace_record *itr);
496int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
497 struct auxtrace_mmap *mm,
498 unsigned char *data, u64 *head, u64 *old);
499u64 auxtrace_record__reference(struct auxtrace_record *itr);
500
501int auxtrace_index__auxtrace_event(struct list_head *head, union perf_event *event,
502 off_t file_offset);
503int auxtrace_index__write(int fd, struct list_head *head);
504int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
505 bool needs_swap);
506void auxtrace_index__free(struct list_head *head);
507
508void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
509 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
510 const char *msg);
511
512int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
513 struct perf_tool *tool,
514 struct perf_session *session,
515 perf_event__handler_t process);
516int perf_event__process_auxtrace_info(struct perf_tool *tool,
517 union perf_event *event,
518 struct perf_session *session);
519s64 perf_event__process_auxtrace(struct perf_tool *tool,
520 union perf_event *event,
521 struct perf_session *session);
522int perf_event__process_auxtrace_error(struct perf_tool *tool,
523 union perf_event *event,
524 struct perf_session *session);
525int itrace_parse_synth_opts(const struct option *opt, const char *str,
526 int unset);
527void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts);
528
529size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp);
530void perf_session__auxtrace_error_inc(struct perf_session *session,
531 union perf_event *event);
532void events_stats__auxtrace_error_warn(const struct events_stats *stats);
533
534void addr_filters__init(struct addr_filters *filts);
535void addr_filters__exit(struct addr_filters *filts);
536int addr_filters__parse_bare_filter(struct addr_filters *filts,
537 const char *filter);
538int auxtrace_parse_filters(struct perf_evlist *evlist);
539
540static inline int auxtrace__process_event(struct perf_session *session,
541 union perf_event *event,
542 struct perf_sample *sample,
543 struct perf_tool *tool)
544{
545 if (!session->auxtrace)
546 return 0;
547
548 return session->auxtrace->process_event(session, event, sample, tool);
549}
550
551static inline int auxtrace__flush_events(struct perf_session *session,
552 struct perf_tool *tool)
553{
554 if (!session->auxtrace)
555 return 0;
556
557 return session->auxtrace->flush_events(session, tool);
558}
559
560static inline void auxtrace__free_events(struct perf_session *session)
561{
562 if (!session->auxtrace)
563 return;
564
565 return session->auxtrace->free_events(session);
566}
567
568static inline void auxtrace__free(struct perf_session *session)
569{
570 if (!session->auxtrace)
571 return;
572
573 return session->auxtrace->free(session);
574}
575
576#else
577
578static inline struct auxtrace_record *
579auxtrace_record__init(struct perf_evlist *evlist __maybe_unused,
580 int *err)
581{
582 *err = 0;
583 return NULL;
584}
585
586static inline
587void auxtrace_record__free(struct auxtrace_record *itr __maybe_unused)
588{
589}
590
591static inline int
592perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr __maybe_unused,
593 struct perf_tool *tool __maybe_unused,
594 struct perf_session *session __maybe_unused,
595 perf_event__handler_t process __maybe_unused)
596{
597 return -EINVAL;
598}
599
600static inline
601int auxtrace_record__options(struct auxtrace_record *itr __maybe_unused,
602 struct perf_evlist *evlist __maybe_unused,
603 struct record_opts *opts __maybe_unused)
604{
605 return 0;
606}
607
608#define perf_event__process_auxtrace_info 0
609#define perf_event__process_auxtrace 0
610#define perf_event__process_auxtrace_error 0
611
612static inline
613void perf_session__auxtrace_error_inc(struct perf_session *session
614 __maybe_unused,
615 union perf_event *event
616 __maybe_unused)
617{
618}
619
620static inline
621void events_stats__auxtrace_error_warn(const struct events_stats *stats
622 __maybe_unused)
623{
624}
625
626static inline
627int itrace_parse_synth_opts(const struct option *opt __maybe_unused,
628 const char *str __maybe_unused,
629 int unset __maybe_unused)
630{
631 pr_err("AUX area tracing not supported\n");
632 return -EINVAL;
633}
634
635static inline
636int auxtrace_parse_snapshot_options(struct auxtrace_record *itr __maybe_unused,
637 struct record_opts *opts __maybe_unused,
638 const char *str)
639{
640 if (!str)
641 return 0;
642 pr_err("AUX area tracing not supported\n");
643 return -EINVAL;
644}
645
646static inline
647int auxtrace__process_event(struct perf_session *session __maybe_unused,
648 union perf_event *event __maybe_unused,
649 struct perf_sample *sample __maybe_unused,
650 struct perf_tool *tool __maybe_unused)
651{
652 return 0;
653}
654
655static inline
656int auxtrace__flush_events(struct perf_session *session __maybe_unused,
657 struct perf_tool *tool __maybe_unused)
658{
659 return 0;
660}
661
662static inline
663void auxtrace__free_events(struct perf_session *session __maybe_unused)
664{
665}
666
667static inline
668void auxtrace_cache__free(struct auxtrace_cache *auxtrace_cache __maybe_unused)
669{
670}
671
672static inline
673void auxtrace__free(struct perf_session *session __maybe_unused)
674{
675}
676
677static inline
678int auxtrace_index__write(int fd __maybe_unused,
679 struct list_head *head __maybe_unused)
680{
681 return -EINVAL;
682}
683
684static inline
685int auxtrace_index__process(int fd __maybe_unused,
686 u64 size __maybe_unused,
687 struct perf_session *session __maybe_unused,
688 bool needs_swap __maybe_unused)
689{
690 return -EINVAL;
691}
692
693static inline
694void auxtrace_index__free(struct list_head *head __maybe_unused)
695{
696}
697
698static inline
699int auxtrace_parse_filters(struct perf_evlist *evlist __maybe_unused)
700{
701 return 0;
702}
703
704int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
705 struct auxtrace_mmap_params *mp,
706 void *userpg, int fd);
707void auxtrace_mmap__munmap(struct auxtrace_mmap *mm);
708void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
709 off_t auxtrace_offset,
710 unsigned int auxtrace_pages,
711 bool auxtrace_overwrite);
712void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
713 struct perf_evlist *evlist, int idx,
714 bool per_cpu);
715
716#endif
717
718#endif
719