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