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