summaryrefslogtreecommitdiffstats
path: root/tools/perf/util/auxtrace.c
blob: 265d20cc126b13b09457d72c97d11bd22576cb77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
// SPDX-License-Identifier: GPL-2.0-only
/*
 * auxtrace.c: AUX area trace support
 * Copyright (c) 2013-2015, Intel Corporation.
 */

#include <inttypes.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#include <errno.h>

#include <linux/kernel.h>
#include <linux/perf_event.h>
#include <linux/types.h>
#include <linux/bitops.h>
#include <linux/log2.h>
#include <linux/string.h>
#include <linux/time64.h>

#include <sys/param.h>
#include <stdlib.h>
#include <stdio.h>
#include <linux/list.h>
#include <linux/zalloc.h>

#include "config.h"
#include "evlist.h"
#include "dso.h"
#include "map.h"
#include "pmu.h"
#include "evsel.h"
#include "evsel_config.h"
#include "symbol.h"
#include "util/perf_api_probe.h"
#include "util/synthetic-events.h"
#include "thread_map.h"
#include "asm/bug.h"
#include "auxtrace.h"

#include <linux/hash.h>

#include "event.h"
#include "record.h"
#include "session.h"
#include "debug.h"
#include <subcmd/parse-options.h>

#include "cs-etm.h"
#include "intel-pt.h"
#include "intel-bts.h"
#include "arm-spe.h"
#include "hisi-ptt.h"
#include "s390-cpumsf.h"
#include "util/mmap.h"

#include <linux/ctype.h>
#include "symbol/kallsyms.h"
#include <internal/lib.h>
#include "util/sample.h"

/*
 * Make a group from 'leader' to 'last', requiring that the events were not
 * already grouped to a different leader.
 */
static int evlist__regroup(struct evlist *evlist, struct evsel *leader, struct evsel *last)
{
	struct evsel *evsel;
	bool grp;

	if (!evsel__is_group_leader(leader))
		return -EINVAL;

	grp = false;
	evlist__for_each_entry(evlist, evsel) {
		if (grp) {
			if (!(evsel__leader(evsel) == leader ||
			     (evsel__leader(evsel) == evsel &&
			      evsel->core.nr_members <= 1)))
				return -EINVAL;
		} else if (evsel == leader) {
			grp = true;
		}
		if (evsel == last)
			break;
	}

	grp = false;
	evlist__for_each_entry(evlist, evsel) {
		if (grp) {
			if (!evsel__has_leader(evsel, leader)) {
				evsel__set_leader(evsel, leader);
				if (leader->core.nr_members < 1)
					leader->core.nr_members = 1;
				leader->core.nr_members += 1;
			}
		} else if (evsel == leader) {
			grp = true;
		}
		if (evsel == last)
			break;
	}

	return 0;
}

static bool auxtrace__dont_decode(struct perf_session *session)
{
	return !session->itrace_synth_opts ||
	       session->itrace_synth_opts->dont_decode;
}

int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
			struct auxtrace_mmap_params *mp,
			void *userpg, int fd)
{
	struct perf_event_mmap_page *pc = userpg;

	WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");

	mm->userpg = userpg;
	mm->mask = mp->mask;
	mm->len = mp->len;
	mm->prev = 0;
	mm->idx = mp->idx;
	mm->tid = mp->tid;
	mm->cpu = mp->cpu.cpu;

	if (!mp->len || !mp->mmap_needed) {
		mm->base = NULL;
		return 0;
	}

	pc->aux_offset = mp->offset;
	pc->aux_size = mp->len;

	mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
	if (mm->base == MAP_FAILED) {
		pr_debug2("failed to mmap AUX area\n");
		mm->base = NULL;
		return -1;
	}

	return 0;
}

void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
{
	if (mm->base) {
		munmap(mm->base, mm->len);
		mm->base = NULL;
	}
}

void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
				off_t auxtrace_offset,
				unsigned int auxtrace_pages,
				bool auxtrace_overwrite)
{
	if (auxtrace_pages) {
		mp->offset = auxtrace_offset;
		mp->len = auxtrace_pages * (size_t)page_size;
		mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
		mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
		pr_debug2("AUX area mmap length %zu\n", mp->len);
	} else {
		mp->len = 0;
	}
}

void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
				   struct evlist *evlist,
				   struct evsel *evsel, int idx)
{
	bool per_cpu = !perf_cpu_map__empty(evlist->core.user_requested_cpus);

	mp->mmap_needed = evsel->needs_auxtrace_mmap;

	if (!mp->mmap_needed)
		return;

	mp->idx = idx;

	if (per_cpu) {
		mp->cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
		if (evlist->core.threads)
			mp->tid = perf_thread_map__pid(evlist->core.threads, 0);
		else
			mp->tid = -1;
	} else {
		mp->cpu.cpu = -1;
		mp->tid = perf_thread_map__pid(evlist->core.threads, idx);
	}
}

#define AUXTRACE_INIT_NR_QUEUES	32

static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
{
	struct auxtrace_queue *queue_array;
	unsigned int max_nr_queues, i;

	max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
	if (nr_queues > max_nr_queues)
		return NULL;

	queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
	if (!queue_array)
		return NULL;

	for (i = 0; i < nr_queues; i++) {
		INIT_LIST_HEAD(&queue_array[i].head);
		queue_array[i].priv = NULL;
	}

	return queue_array;
}

int auxtrace_queues__init(struct auxtrace_queues *queues)
{
	queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
	queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
	if (!queues->queue_array)
		return -ENOMEM;
	return 0;
}

static int auxtrace_queues__grow(struct auxtrace_queues *queues,
				 unsigned int new_nr_queues)
{
	unsigned int nr_queues = queues->nr_queues;
	struct auxtrace_queue *queue_array;
	unsigned int i;

	if (!nr_queues)
		nr_queues = AUXTRACE_INIT_NR_QUEUES;

	while (nr_queues && nr_queues < new_nr_queues)
		nr_queues <<= 1;

	if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
		return -EINVAL;

	queue_array = auxtrace_alloc_queue_array(nr_queues);
	if (!queue_array)
		return -ENOMEM;

	for (i = 0; i < queues->nr_queues; i++) {
		list_splice_tail(&queues->queue_array[i].head,
				 &queue_array[i].head);
		queue_array[i].tid = queues->queue_array[i].tid;
		queue_array[i].cpu = queues->queue_array[i].cpu;
		queue_array[i].set = queues->queue_array[i].set;
		queue_array[i].priv = queues->queue_array[i].priv;
	}

	queues->nr_queues = nr_queues;
	queues->queue_array = queue_array;

	return 0;
}

static void *auxtrace_copy_data(u64 size, struct perf_session *session)
{
	int fd = perf_data__fd(session->data);
	void *p;
	ssize_t ret;

	if (size > SSIZE_MAX)
		return NULL;

	p = malloc(size);
	if (!p)
		return NULL;

	ret = readn(fd, p, size);
	if (ret != (ssize_t)size) {
		free(p);
		return NULL;
	}

	return p;
}

static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues,
					 unsigned int idx,
					 struct auxtrace_buffer *buffer)
{
	struct auxtrace_queue *queue;
	int err;

	if (idx >= queues->nr_queues) {
		err = auxtrace_queues__grow(queues, idx + 1);
		if (err)
			return err;
	}

	queue = &queues->queue_array[idx];

	if (!queue->set) {
		queue->set = true;
		queue->tid = buffer->tid;
		queue->cpu = buffer->cpu.cpu;
	}

	buffer->buffer_nr = queues->next_buffer_nr++;

	list_add_tail(&buffer->list, &queue->head);

	queues->new_data = true;
	queues->populated = true;

	return 0;
}

/* Limit buffers to 32MiB on 32-bit */
#define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)

static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
					 unsigned int idx,
					 struct auxtrace_buffer *buffer)
{
	u64 sz = buffer->size;
	bool consecutive = false;
	struct auxtrace_buffer *b;
	int err;

	while (sz > BUFFER_LIMIT_FOR_32_BIT) {
		b = memdup(buffer, sizeof(struct auxtrace_buffer));
		if (!b)
			return -ENOMEM;
		b->size = BUFFER_LIMIT_FOR_32_BIT;
		b->consecutive = consecutive;
		err = auxtrace_queues__queue_buffer(queues, idx, b);
		if (err) {
			auxtrace_buffer__free(b);
			return err;
		}
		buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
		sz -= BUFFER_LIMIT_FOR_32_BIT;
		consecutive = true;
	}

	buffer->size = sz;
	buffer->consecutive = consecutive;

	return 0;
}

static bool filter_cpu(struct perf_session *session, struct perf_cpu cpu)
{
	unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap;

	return cpu_bitmap && cpu.cpu != -1 && !test_bit(cpu.cpu, cpu_bitmap);
}

static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
				       struct perf_session *session,
				       unsigned int idx,
				       struct auxtrace_buffer *buffer,
				       struct auxtrace_buffer **buffer_ptr)
{
	int err = -ENOMEM;

	if (filter_cpu(session, buffer->cpu))
		return 0;

	buffer = memdup(buffer, sizeof(*buffer));
	if (!buffer)
		return -ENOMEM;

	if (session->one_mmap) {
		buffer->data = buffer->data_offset - session->one_mmap_offset +
			       session->one_mmap_addr;
	} else if (perf_data__is_pipe(session->data)) {
		buffer->data = auxtrace_copy_data(buffer->size, session);
		if (!buffer->data)
			goto out_free;
		buffer->data_needs_freeing = true;
	} else if (BITS_PER_LONG == 32 &&
		   buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
		err = auxtrace_queues__split_buffer(queues, idx, buffer);
		if (err)
			goto out_free;
	}

	err = auxtrace_queues__queue_buffer(queues, idx, buffer);
	if (err)
		goto out_free;

	/* FIXME: Doesn't work for split buffer */
	if (buffer_ptr)
		*buffer_ptr = buffer;

	return 0;

out_free:
	auxtrace_buffer__free(buffer);
	return err;
}

int auxtrace_queues__add_event(struct auxtrace_queues *queues,
			       struct perf_session *session,
			       union perf_event *event, off_t data_offset,
			       struct auxtrace_buffer **buffer_ptr)
{
	struct auxtrace_buffer buffer = {
		.pid = -1,
		.tid = event->auxtrace.tid,
		.cpu = { event->auxtrace.cpu },
		.data_offset = data_offset,
		.offset = event->auxtrace.offset,
		.reference = event->auxtrace.reference,
		.size = event->auxtrace.size,
	};
	unsigned int idx = event->auxtrace.idx;

	return auxtrace_queues__add_buffer(queues, session, idx, &buffer,
					   buffer_ptr);
}

static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
					      struct perf_session *session,
					      off_t file_offset, size_t sz)
{
	union perf_event *event;
	int err;
	char buf[PERF_SAMPLE_MAX_SIZE];

	err = perf_session__peek_event(session, file_offset, buf,
				       PERF_SAMPLE_MAX_SIZE, &event, NULL);
	if (err)
		return err;

	if (event->header.type == PERF_RECORD_AUXTRACE) {
		if (event->header.size < sizeof(struct perf_record_auxtrace) ||
		    event->header.size != sz) {
			err = -EINVAL;
			goto out;
		}
		file_offset += event->header.size;
		err = auxtrace_queues__add_event(queues, session, event,
						 file_offset, NULL);
	}
out:
	return err;
}

void auxtrace_queues__free(struct auxtrace_queues *queues)
{
	unsigned int i;

	for (i = 0; i < queues->nr_queues; i++) {
		while (!list_empty(&queues->queue_array[i].head)) {
			struct auxtrace_buffer *buffer;

			buffer = list_entry(queues->queue_array[i].head.next,
					    struct auxtrace_buffer, list);
			list_del_init(&buffer->list);
			auxtrace_buffer__free(buffer);
		}
	}

	zfree(&queues->queue_array);
	queues->nr_queues = 0;
}

static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
			     unsigned int pos, unsigned int queue_nr,
			     u64 ordinal)
{
	unsigned int parent;

	while (pos) {
		parent = (pos - 1) >> 1;
		if (heap_array[parent].ordinal <= ordinal)
			break;
		heap_array[pos] = heap_array[parent];
		pos = parent;
	}
	heap_array[pos].queue_nr = queue_nr;
	heap_array[pos].ordinal = ordinal;
}

int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
		       u64 ordinal)
{
	struct auxtrace_heap_item *heap_array;

	if (queue_nr >= heap->heap_sz) {
		unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;

		while (heap_sz <= queue_nr)
			heap_sz <<= 1;
		heap_array = realloc(heap->heap_array,
				     heap_sz * sizeof(struct auxtrace_heap_item));
		if (!heap_array)
			return -ENOMEM;
		heap->heap_array = heap_array;
		heap->heap_sz = heap_sz;
	}

	auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);

	return 0;
}

void auxtrace_heap__free(struct auxtrace_heap *heap)
{
	zfree(&heap->heap_array);
	heap->heap_cnt = 0;
	heap->heap_sz = 0;
}

void auxtrace_heap__pop(struct auxtrace_heap *heap)
{
	unsigned int pos, last, heap_cnt = heap->heap_cnt;
	struct auxtrace_heap_item *heap_array;

	if (!heap_cnt)
		return;

	heap->heap_cnt -= 1;

	heap_array = heap->heap_array;

	pos = 0;
	while (1) {
		unsigned int left, right;

		left = (pos << 1) + 1;
		if (left >= heap_cnt)
			break;
		right = left + 1;
		if (right >= heap_cnt) {
			heap_array[pos] = heap_array[left];
			return;
		}
		if (heap_array[left].ordinal < heap_array[right].ordinal) {
			heap_array[pos] = heap_array[left];
			pos = left;
		} else {
			heap_array[pos] = heap_array[right];
			pos = right;
		}
	}

	last = heap_cnt - 1;
	auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
			 heap_array[last].ordinal);
}

size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
				       struct evlist *evlist)
{
	if (itr)
		return itr->info_priv_size(itr, evlist);
	return 0;
}

static int auxtrace_not_supported(void)
{
	pr_err("AUX area tracing is not supported on this architecture\n");
	return -EINVAL;
}

int auxtrace_record__info_fill(struct auxtrace_record *itr,
			       struct perf_session *session,
			       struct perf_record_auxtrace_info *auxtrace_info,
			       size_t priv_size)
{
	if (itr)
		return itr->info_fill(itr, session, auxtrace_info, priv_size);
	return auxtrace_not_supported();
}

void auxtrace_record__free(struct auxtrace_record *itr)
{
	if (itr)
		itr->free(itr);
}

int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
{
	if (itr && itr->snapshot_start)
		return itr->snapshot_start(itr);
	return 0;
}

int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit)
{
	if (!on_exit && itr && itr->snapshot_finish)
		return itr->snapshot_finish(itr);
	return 0;
}

int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
				   struct auxtrace_mmap *mm,
				   unsigned char *data, u64 *head, u64 *old)
{
	if (itr && itr->find_snapshot)
		return itr->find_snapshot(itr, idx, mm, data, head, old);
	return 0;
}

int auxtrace_record__options(struct auxtrace_record *itr,
			     struct evlist *evlist,
			     struct record_opts *opts)
{
	if (itr) {
		itr->evlist = evlist;
		return itr->recording_options(itr, evlist, opts);
	}
	return 0;
}

u64 auxtrace_record__reference(struct auxtrace_record *itr)
{
	if (itr)
		return itr->reference(itr);
	return 0;
}

int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
				    struct record_opts *opts, const char *str)
{
	if (!str)
		return 0;

	/* PMU-agnostic options */
	switch (*str) {
	case 'e':
		opts->auxtrace_snapshot_on_exit = true;
		str++;
		break;
	default:
		break;
	}

	if (itr && itr->parse_snapshot_options)
		return itr->parse_snapshot_options(itr, opts, str);

	pr_err("No AUX area tracing to snapshot\n");
	return -EINVAL;
}

static int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
{
	bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.user_requested_cpus);

	if (per_cpu_mmaps) {
		struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
		int cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu);

		if (cpu_map_idx == -1)
			return -EINVAL;
		return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx);
	}

	return perf_evsel__enable_thread(&evsel->core, idx);
}

int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
{
	struct evsel *evsel;

	if (!itr->evlist || !itr->pmu)
		return -EINVAL;

	evlist__for_each_entry(itr->evlist, evsel) {
		if (evsel->core.attr.type == itr->pmu->type) {
			if (evsel->disabled)
				return 0;
			return evlist__enable_event_idx(itr->evlist, evsel, idx);
		}
	}
	return -EINVAL;
}

/*
 * Event record size is 16-bit which results in a maximum size of about 64KiB.
 * Allow about 4KiB for the rest of the sample record, to give a maximum
 * AUX area sample size of 60KiB.
 */
#define MAX_AUX_SAMPLE_SIZE (60 * 1024)

/* Arbitrary default size if no other default provided */
#define DEFAULT_AUX_SAMPLE_SIZE (4 * 1024)

static int auxtrace_validate_aux_sample_size(struct evlist *evlist,
					     struct record_opts *opts)
{
	struct evsel *evsel;
	bool has_aux_leader = false;
	u32 sz;

	evlist__for_each_entry(evlist, evsel) {
		sz = evsel->core.attr.aux_sample_size;
		if (evsel__is_group_leader(evsel)) {
			has_aux_leader = evsel__is_aux_event(evsel);
			if (sz) {
				if (has_aux_leader)
					pr_err("Cannot add AUX area sampling to an AUX area event\n");
				else
					pr_err("Cannot add AUX area sampling to a group leader\n");
				return -EINVAL;
			}
		}
		if (sz > MAX_AUX_SAMPLE_SIZE) {
			pr_err("AUX area sample size %u too big, max. %d\n",
			       sz, MAX_AUX_SAMPLE_SIZE);
			return -EINVAL;
		}
		if (sz) {
			if (!has_aux_leader) {
				pr_err("Cannot add AUX area sampling because group leader is not an AUX area event\n");
				return -EINVAL;
			}
			evsel__set_sample_bit(evsel, AUX);
			opts->auxtrace_sample_mode = true;
		} else {
			evsel__reset_sample_bit(evsel, AUX);
		}
	}

	if (!opts->auxtrace_sample_mode) {
		pr_err("AUX area sampling requires an AUX area event group leader plus other events to which to add samples\n");
		return -EINVAL;
	}

	if (!perf_can_aux_sample()) {
		pr_err("AUX area sampling is not supported by kernel\n");
		return -EINVAL;
	}

	return 0;
}

int auxtrace_parse_sample_options(struct auxtrace_record *itr,
				  struct evlist *evlist,
				  struct record_opts *opts, const char *str)
{
	struct evsel_config_term *term;
	struct evsel *aux_evsel;
	bool has_aux_sample_size = false;
	bool has_aux_leader = false;
	struct evsel *evsel;
	char *endptr;
	unsigned long sz;

	if (!str)
		goto no_opt;

	if (!itr) {
		pr_err("No AUX area event to sample\n");
		return -EINVAL;
	}

	sz = strtoul(str, &endptr, 0);
	if (*endptr || sz > UINT_MAX) {
		pr_err("Bad AUX area sampling option: '%s'\n", str);
		return -EINVAL;
	}

	if (!sz)
		sz = itr->default_aux_sample_size;

	if (!sz)
		sz = DEFAULT_AUX_SAMPLE_SIZE;

	/* Set aux_sample_size based on --aux-sample option */
	evlist__for_each_entry(evlist, evsel) {
		if (evsel__is_group_leader(evsel)) {
			has_aux_leader = evsel__is_aux_event(evsel);
		} else if (has_aux_leader) {
			evsel->core.attr.aux_sample_size = sz;
		}
	}
no_opt:
	aux_evsel = NULL;
	/* Override with aux_sample_size from config term */
	evlist__for_each_entry(evlist, evsel) {
		if (evsel__is_aux_event(evsel))
			aux_evsel = evsel;
		term = evsel__get_config_term(evsel, AUX_SAMPLE_SIZE);
		if (term) {
			has_aux_sample_size = true;
			evsel->core.attr.aux_sample_size = term->val.aux_sample_size;
			/* If possible, group with the AUX event */
			if (aux_evsel && evsel->core.attr.aux_sample_size)
				evlist__regroup(evlist, aux_evsel, evsel);
		}
	}

	if (!str && !has_aux_sample_size)
		return 0;

	if (!itr) {
		pr_err("No AUX area event to sample\n");
		return -EINVAL;
	}

	return auxtrace_validate_aux_sample_size(evlist, opts);
}

void auxtrace_regroup_aux_output(struct evlist *evlist)
{
	struct evsel *evsel, *aux_evsel = NULL;
	struct evsel_config_term *term;

	evlist__for_each_entry(evlist, evsel) {
		if (evsel__is_aux_event(evsel))
			aux_evsel = evsel;
		term = evsel__get_config_term(evsel, AUX_OUTPUT);
		/* If possible, group with the AUX event */
		if (term && aux_evsel)
			evlist__regroup(evlist, aux_evsel, evsel);
	}
}

struct auxtrace_record *__weak
auxtrace_record__init(struct evlist *evlist __maybe_unused, int *err)
{
	*err = 0;
	return NULL;
}

static int auxtrace_index__alloc(struct list_head *head)
{
	struct auxtrace_index *auxtrace_index;

	auxtrace_index = malloc(sizeof(struct auxtrace_index));
	if (!auxtrace_index)
		return -ENOMEM;

	auxtrace_index->nr = 0;
	INIT_LIST_HEAD(&auxtrace_index->list);

	list_add_tail(&auxtrace_index->list, head);

	return 0;
}

void auxtrace_index__free(struct list_head *head)
{
	struct auxtrace_index *auxtrace_index, *n;

	list_for_each_entry_safe(auxtrace_index, n, head, list) {
		list_del_init(&auxtrace_index->list);
		free(auxtrace_index);
	}
}

static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
{
	struct auxtrace_index *auxtrace_index;
	int err;

	if (list_empty(head)) {
		err = auxtrace_index__alloc(head);
		if (err)
			return NULL;
	}

	auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);

	if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
		err = auxtrace_index__alloc(head);
		if (err)
			return NULL;
		auxtrace_index = list_entry(head->prev, struct auxtrace_index,
					    list);
	}

	return auxtrace_index;
}

int auxtrace_index__auxtrace_event(struct list_head *head,
				   union perf_event *event, off_t file_offset)
{
	struct auxtrace_index *auxtrace_index;
	size_t nr;

	auxtrace_index = auxtrace_index__last(head);
	if (!auxtrace_index)
		return -ENOMEM;

	nr = auxtrace_index->nr;
	auxtrace_index->entries[nr].file_offset = file_offset;
	auxtrace_index->entries[nr].sz = event->header.size;
	auxtrace_index->nr += 1;

	return 0;
}

static int auxtrace_index__do_write(int fd,
				    struct auxtrace_index *auxtrace_index)
{
	struct auxtrace_index_entry ent;
	size_t i;

	for (i = 0; i < auxtrace_index->nr; i++) {
		ent.file_offset = auxtrace_index->entries[i].file_offset;
		ent.sz = auxtrace_index->entries[i].sz;
		if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
			return -errno;
	}
	return 0;
}

int auxtrace_index__write(int fd, struct list_head *head)
{
	struct auxtrace_index *auxtrace_index;
	u64 total = 0;
	int err;

	list_for_each_entry(auxtrace_index, head, list)
		total += auxtrace_index->nr;

	if (writen(fd, &total, sizeof(total)) != sizeof(total))
		return -errno;

	list_for_each_entry(auxtrace_index, head, list) {
		err = auxtrace_index__do_write(fd, auxtrace_index);
		if (err)
			return err;
	}

	return 0;
}

static int auxtrace_index__process_entry(int fd, struct list_head *head,
					 bool needs_swap)
{
	struct auxtrace_index *auxtrace_index;
	struct auxtrace_index_entry ent;
	size_t nr;

	if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
		return -1;

	auxtrace_index = auxtrace_index__last(head);
	if (!auxtrace_index)
		return -1;

	nr = auxtrace_index->nr;
	if (needs_swap) {
		auxtrace_index->entries[nr].file_offset =
						bswap_64(ent.file_offset);
		auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
	} else {
		auxtrace_index->entries[nr].file_offset = ent.file_offset;
		auxtrace_index->entries[nr].sz = ent.sz;
	}

	auxtrace_index->nr = nr + 1;

	return 0;
}

int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
			    bool needs_swap)
{
	struct list_head *head = &session->auxtrace_index;
	u64 nr;

	if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
		return -1;

	if (needs_swap)
		nr = bswap_64(nr);

	if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
		return -1;

	while (nr--) {
		int err;

		err = auxtrace_index__process_entry(fd, head, needs_swap);
		if (err)
			return -1;
	}

	return 0;
}

static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
						struct perf_session *session,
						struct auxtrace_index_entry *ent)
{
	return auxtrace_queues__add_indexed_event(queues, session,
						  ent->file_offset, ent->sz);
}

int auxtrace_queues__process_index(struct auxtrace_queues *queues,
				   struct perf_session *session)
{
	struct auxtrace_index *auxtrace_index;
	struct auxtrace_index_entry *ent;
	size_t i;
	int err;

	if (auxtrace__dont_decode(session))
		return 0;

	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
		for (i = 0; i < auxtrace_index->nr; i++) {
			ent = &auxtrace_index->entries[i];
			err = auxtrace_queues__process_index_entry(queues,
								   session,
								   ent);
			if (err)
				return err;
		}
	}
	return 0;
}

struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
					      struct auxtrace_buffer *buffer)
{
	if (buffer) {
		if (list_is_last(&buffer->list, &queue->head))
			return NULL;
		return list_entry(buffer->list.next, struct auxtrace_buffer,
				  list);
	} else {
		if (list_empty(&queue->head))
			return NULL;
		return list_entry(queue->head.next, struct auxtrace_buffer,
				  list);
	}
}

struct auxtrace_queue *auxtrace_queues__sample_queue(struct auxtrace_queues *queues,
						     struct perf_sample *sample,
						     struct perf_session *session)
{
	struct perf_sample_id *sid;
	unsigned int idx;
	u64 id;

	id = sample->id;
	if (!id)
		return NULL;

	sid = evlist__id2sid(session->evlist, id);
	if (!sid)
		return NULL;

	idx = sid->idx;

	if (idx >= queues->nr_queues)
		return NULL;

	return &queues->queue_array[idx];
}

int auxtrace_queues__add_sample(struct auxtrace_queues *queues,
				struct perf_session *session,
				struct perf_sample *sample, u64 data_offset,
				u64 reference)
{
	struct auxtrace_buffer buffer = {
		.pid = -1,
		.data_offset = data_offset,
		.reference = reference,
		.size = sample->aux_sample.size,
	};
	struct perf_sample_id *sid;
	u64 id = sample->id;
	unsigned int idx;

	if (!id)
		return -EINVAL;

	sid = evlist__id2sid(session->evlist, id);
	if (!sid)
		return -ENOENT;

	idx = sid->idx;
	buffer.tid = sid->tid;
	buffer.cpu = sid->cpu;

	return auxtrace_queues__add_buffer(queues, session, idx, &buffer, NULL);
}

struct queue_data {
	bool samples;
	bool events;
};

static int auxtrace_queue_data_cb(struct perf_session *session,
				  union perf_event *event, u64 offset,
				  void *data)
{
	struct queue_data *qd = data;
	struct perf_sample sample;
	int err;

	if (qd->events && event->header.type == PERF_RECORD_AUXTRACE) {
		if (event->header.size < sizeof(struct perf_record_auxtrace))
			return -EINVAL;
		offset += event->header.size;
		return session->auxtrace->queue_data(session, NULL, event,
						     offset);
	}

	if (!qd->samples || event->header.type != PERF_RECORD_SAMPLE)
		return 0;

	err = evlist__parse_sample(session->evlist, event, &sample);
	if (err)
		return err;

	if (!sample.aux_sample.size)
		return 0;

	offset += sample.aux_sample.data - (void *)event;

	return session->auxtrace->queue_data(session, &sample, NULL, offset);
}

int auxtrace_queue_data(struct perf_session *session, bool samples, bool events)
{
	struct queue_data qd = {
		.samples = samples,
		.events = events,
	};

	if (auxtrace__dont_decode(session))
		return 0;

	if (!session->auxtrace || !session->auxtrace->queue_data)
		return -EINVAL;

	return perf_session__peek_events(session, session->header.data_offset,
					 session->header.data_size,
					 auxtrace_queue_data_cb, &qd);
}

void *auxtrace_buffer__get_data_rw(struct auxtrace_buffer *buffer, int fd, bool rw)
{
	int prot = rw ? PROT_READ | PROT_WRITE : PROT_READ;
	size_t adj = buffer->data_offset & (page_size - 1);
	size_t size = buffer->size + adj;
	off_t file_offset = buffer->data_offset - adj;
	void *addr;

	if (buffer->data)
		return buffer->data;

	addr = mmap(NULL, size, prot, MAP_SHARED, fd, file_offset);
	if (addr == MAP_FAILED)
		return NULL;

	buffer->mmap_addr = addr;
	buffer->mmap_size = size;

	buffer->data = addr + adj;

	return buffer->data;
}

void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
{
	if (!buffer->data || !buffer->mmap_addr)
		return;
	munmap(buffer->mmap_addr, buffer->mmap_size);
	buffer->mmap_addr = NULL;
	buffer->mmap_size = 0;
	buffer->data = NULL;
	buffer->use_data = NULL;
}

void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
{
	auxtrace_buffer__put_data(buffer);
	if (buffer->data_needs_freeing) {
		buffer->data_needs_freeing = false;
		zfree(&buffer->data);
		buffer->use_data = NULL;
		buffer->size = 0;
	}
}

void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
{
	auxtrace_buffer__drop_data(buffer);
	free(buffer);
}

void auxtrace_synth_guest_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
				int code, int cpu, pid_t pid, pid_t tid, u64 ip,
				const char *msg, u64 timestamp,
				pid_t machine_pid, int vcpu)
{
	size_t size;

	memset(auxtrace_error, 0, sizeof(struct perf_record_auxtrace_error));

	auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
	auxtrace_error->type = type;
	auxtrace_error->code = code;
	auxtrace_error->cpu = cpu;
	auxtrace_error->pid = pid;
	auxtrace_error->tid = tid;
	auxtrace_error->fmt = 1;
	auxtrace_error->ip = ip;
	auxtrace_error->time = timestamp;
	strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
	if (machine_pid) {
		auxtrace_error->fmt = 2;
		auxtrace_error->machine_pid = machine_pid;
		auxtrace_error->vcpu = vcpu;
		size = sizeof(*auxtrace_error);
	} else {
		size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
		       strlen(auxtrace_error->msg) + 1;
	}
	auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
}

void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
			  int code, int cpu, pid_t pid, pid_t tid, u64 ip,
			  const char *msg, u64 timestamp)
{
	auxtrace_synth_guest_error(auxtrace_error, type, code, cpu, pid, tid,
				   ip, msg, timestamp, 0, -1);
}

int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
					 struct perf_tool *tool,
					 struct perf_session *session,
					 perf_event__handler_t process)
{
	union perf_event *ev;
	size_t priv_size;
	int err;

	pr_debug2("Synthesizing auxtrace information\n");
	priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
	ev = zalloc(sizeof(struct perf_record_auxtrace_info) + priv_size);
	if (!ev)
		return -ENOMEM;

	ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
	ev->auxtrace_info.header.size = sizeof(struct perf_record_auxtrace_info) +
					priv_size;
	err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
					 priv_size);
	if (err)
		goto out_free;

	err = process(tool, ev, NULL, NULL);
out_free:
	free(ev);
	return err;
}

static void unleader_evsel(struct evlist *evlist, struct evsel *leader)
{
	struct evsel *new_leader = NULL;
	struct evsel *evsel;

	/* Find new leader for the group */
	evlist__for_each_entry(evlist, evsel) {
		if (!evsel__has_leader(evsel, leader) || evsel == leader)
			continue;
		if (!new_leader)
			new_leader = evsel;
		evsel__set_leader(evsel, new_leader);
	}

	/* Update group information */
	if (new_leader) {
		zfree(&new_leader->group_name);
		new_leader->group_name = leader->group_name;
		leader->group_name = NULL;

		new_leader->core.nr_members = leader->core.nr_members - 1;
		leader->core.nr_members = 1;
	}
}

static void unleader_auxtrace(struct perf_session *session)
{
	struct evsel *evsel;

	evlist__for_each_entry(session->evlist, evsel) {
		if (auxtrace__evsel_is_auxtrace(session, evsel) &&
		    evsel__is_group_leader(evsel)) {
			unleader_evsel(session->evlist, evsel);
		}
	}
}

int perf_event__process_auxtrace_info(struct perf_session *session,
				      union perf_event *event)
{
	enum auxtrace_type type = event->auxtrace_info.type;
	int err;

	if (dump_trace)
		fprintf(stdout, " type: %u\n", type);

	switch (type) {
	case PERF_AUXTRACE_INTEL_PT:
		err = intel_pt_process_auxtrace_info(event, session);
		break;
	case PERF_AUXTRACE_INTEL_BTS:
		err = intel_bts_process_auxtrace_info(event, session);
		break;
	case PERF_AUXTRACE_ARM_SPE:
		err = arm_spe_process_auxtrace_info(event, session);
		break;
	case PERF_AUXTRACE_CS_ETM:
		err = cs_etm__process_auxtrace_info(event, session);
		break;
	case PERF_AUXTRACE_S390_CPUMSF:
		err = s390_cpumsf_process_auxtrace_info(event, session);
		break;
	case PERF_AUXTRACE_HISI_PTT:
		err = hisi_ptt_process_auxtrace_info(event, session);
		break;
	case PERF_AUXTRACE_UNKNOWN:
	default:
		return -EINVAL;
	}

	if (err)
		return err;

	unleader_auxtrace(session);

	return 0;
}

s64 perf_event__process_auxtrace(struct perf_session *session,
				 union perf_event *event)
{
	s64 err;

	if (dump_trace)
		fprintf(stdout, " size: %#"PRI_lx64"  offset: %#"PRI_lx64"  ref: %#"PRI_lx64"  idx: %u  tid: %d  cpu: %d\n",
			event->auxtrace.size, event->auxtrace.offset,
			event->auxtrace.reference, event->auxtrace.idx,
			event->auxtrace.tid, event->auxtrace.cpu);

	if (auxtrace__dont_decode(session))
		return event->auxtrace.size;

	if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
		return -EINVAL;

	err = session->auxtrace->process_auxtrace_event(session, event, session->tool);
	if (err < 0)
		return err;

	return event->auxtrace.size;
}

#define PERF_ITRACE_DEFAULT_PERIOD_TYPE		PERF_ITRACE_PERIOD_NANOSECS
#define PERF_ITRACE_DEFAULT_PERIOD		100000
#define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ	16
#define PERF_ITRACE_MAX_CALLCHAIN_SZ		1024
#define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ	64
#define PERF_ITRACE_MAX_LAST_BRANCH_SZ		1024

void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts,
				    bool no_sample)
{
	synth_opts->branches = true;
	synth_opts->transactions = true;
	synth_opts->ptwrites = true;
	synth_opts->pwr_events = true;
	synth_opts->other_events = true;
	synth_opts->intr_events = true;
	synth_opts->errors = true;
	synth_opts->flc = true;
	synth_opts->llc = true;
	synth_opts->tlb = true;
	synth_opts->mem = true;
	synth_opts->remote_access = true;

	if (no_sample) {
		synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS;
		synth_opts->period = 1;
		synth_opts->calls = true;
	} else {
		synth_opts->instructions = true;
		synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
		synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
	}
	synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
	synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
	synth_opts->initial_skip = 0;
}

static int get_flag(const char **ptr, unsigned int *flags)
{
	while (1) {
		char c = **ptr;

		if (c >= 'a' && c <= 'z') {
			*flags |= 1 << (c - 'a');
			++*ptr;
			return 0;
		} else if (c == ' ') {
			++*ptr;
			continue;
		} else {
			return -1;
		}
	}
}

static int get_flags(const char **ptr, unsigned int *plus_flags, unsigned int *minus_flags)
{
	while (1) {
		switch (**ptr) {
		case '+':
			++*ptr;
			if (get_flag(ptr, plus_flags))
				return -1;
			break;
		case '-':
			++*ptr;
			if (get_flag(ptr, minus_flags))
				return -1;
			break;
		case ' ':
			++*ptr;
			break;
		default:
			return 0;
		}
	}
}

#define ITRACE_DFLT_LOG_ON_ERROR_SZ 16384

static unsigned int itrace_log_on_error_size(void)
{
	unsigned int sz = 0;

	perf_config_scan("itrace.debug-log-buffer-size", "%u", &sz);
	return sz ?: ITRACE_DFLT_LOG_ON_ERROR_SZ;
}

/*
 * Please check tools/perf/Documentation/perf-script.txt for information
 * about the options parsed here, which is introduced after this cset,
 * when support in 'perf script' for these options is introduced.
 */
int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,
			       const char *str, int unset)
{
	const char *p;
	char *endptr;
	bool period_type_set = false;
	bool period_set = false;

	synth_opts->set = true;

	if (unset) {
		synth_opts->dont_decode = true;
		return 0;
	}

	if (!str) {
		itrace_synth_opts__set_default(synth_opts,
					       synth_opts->default_no_sample);
		return 0;
	}

	for (p = str; *p;) {
		switch (*p++) {
		case 'i':
			synth_opts->instructions = true;
			while (*p == ' ' || *p == ',')
				p += 1;
			if (isdigit(*p)) {
				synth_opts->period = strtoull(p, &endptr, 10);
				period_set = true;
				p = endptr;
				while (*p == ' ' || *p == ',')
					p += 1;
				switch (*p++) {
				case 'i':
					synth_opts->period_type =
						PERF_ITRACE_PERIOD_INSTRUCTIONS;
					period_type_set = true;
					break;
				case 't':
					synth_opts->period_type =
						PERF_ITRACE_PERIOD_TICKS;
					period_type_set = true;
					break;
				case 'm':
					synth_opts->period *= 1000;
					/* Fall through */
				case 'u':
					synth_opts->period *= 1000;
					/* Fall through */
				case 'n':
					if (*p++ != 's')
						goto out_err;
					synth_opts->period_type =
						PERF_ITRACE_PERIOD_NANOSECS;
					period_type_set = true;
					break;
				case '\0':
					goto out;
				default:
					goto out_err;
				}
			}
			break;
		case 'b':
			synth_opts->branches = true;
			break;
		case 'x':
			synth_opts->transactions = true;
			break;
		case 'w':
			synth_opts->ptwrites = true;
			break;
		case 'p':
			synth_opts->pwr_events = true;
			break;
		case 'o':
			synth_opts->other_events = true;
			break;
		case 'I':
			synth_opts->intr_events = true;
			break;
		case 'e':
			synth_opts->errors = true;
			if (get_flags(&p, &synth_opts->error_plus_flags,
				      &synth_opts->error_minus_flags))
				goto out_err;
			break;
		case 'd':
			synth_opts->log = true;
			if (get_flags(&p, &synth_opts->log_plus_flags,
				      &synth_opts->log_minus_flags))
				goto out_err;
			if (synth_opts->log_plus_flags & AUXTRACE_LOG_FLG_ON_ERROR)
				synth_opts->log_on_error_size = itrace_log_on_error_size();
			break;
		case 'c':
			synth_opts->branches = true;
			synth_opts->calls = true;
			break;
		case 'r':
			synth_opts->branches = true;
			synth_opts->returns = true;
			break;
		case 'G':
		case 'g':
			if (p[-1] == 'G')
				synth_opts->add_callchain = true;
			else
				synth_opts->callchain = true;
			synth_opts->callchain_sz =
					PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
			while (*p == ' ' || *p == ',')
				p += 1;
			if (isdigit(*p)) {
				unsigned int val;

				val = strtoul(p, &endptr, 10);
				p = endptr;
				if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
					goto out_err;
				synth_opts->callchain_sz = val;
			}
			break;
		case 'L':
		case 'l':
			if (p[-1] == 'L')
				synth_opts->add_last_branch = true;
			else
				synth_opts->last_branch = true;
			synth_opts->last_branch_sz =
					PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
			while (*p == ' ' || *p == ',')
				p += 1;
			if (isdigit(*p)) {
				unsigned int val;

				val = strtoul(p, &endptr, 10);
				p = endptr;
				if (!val ||
				    val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
					goto out_err;
				synth_opts->last_branch_sz = val;
			}
			break;
		case 's':
			synth_opts->initial_skip = strtoul(p, &endptr, 10);
			if (p == endptr)
				goto out_err;
			p = endptr;
			break;
		case 'f':
			synth_opts->flc = true;
			break;
		case 'm':
			synth_opts->llc = true;
			break;
		case 't':
			synth_opts->tlb = true;
			break;
		case 'a':
			synth_opts->remote_access = true;
			break;
		case 'M':
			synth_opts->mem = true;
			break;
		case 'q':
			synth_opts->quick += 1;
			break;
		case 'A':
			synth_opts->approx_ipc = true;
			break;
		case 'Z':
			synth_opts->timeless_decoding = true;
			break;
		case ' ':
		case ',':
			break;
		default:
			goto out_err;
		}
	}
out:
	if (synth_opts->instructions) {
		if (!period_type_set)
			synth_opts->period_type =
					PERF_ITRACE_DEFAULT_PERIOD_TYPE;
		if (!period_set)
			synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
	}

	return 0;

out_err:
	pr_err("Bad Instruction Tracing options '%s'\n", str);
	return -EINVAL;
}

int itrace_parse_synth_opts(const struct option *opt, const char *str, int unset)
{
	return itrace_do_parse_synth_opts(opt->value, str, unset);
}

static const char * const auxtrace_error_type_name[] = {
	[PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
};

static const char *auxtrace_error_name(int type)
{
	const char *error_type_name = NULL;

	if (type < PERF_AUXTRACE_ERROR_MAX)
		error_type_name = auxtrace_error_type_name[type];
	if (!error_type_name)
		error_type_name = "unknown AUX";
	return error_type_name;
}

size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
{
	struct perf_record_auxtrace_error *e = &event->auxtrace_error;
	unsigned long long nsecs = e->time;
	const char *msg = e->msg;
	int ret;

	ret = fprintf(fp, " %s error type %u",
		      auxtrace_error_name(e->type), e->type);

	if (e->fmt && nsecs) {
		unsigned long secs = nsecs / NSEC_PER_SEC;

		nsecs -= secs * NSEC_PER_SEC;
		ret += fprintf(fp, " time %lu.%09llu", secs, nsecs);
	} else {
		ret += fprintf(fp, " time 0");
	}

	if (!e->fmt)
		msg = (const char *)&e->time;

	if (e->fmt >= 2 && e->machine_pid)
		ret += fprintf(fp, " machine_pid %d vcpu %d", e->machine_pid, e->vcpu);

	ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRI_lx64" code %u: %s\n",
		       e->cpu, e->pid, e->tid, e->ip, e->code, msg);
	return ret;
}

void perf_session__auxtrace_error_inc(struct perf_session *session,
				      union perf_event *event)
{
	struct perf_record_auxtrace_error *e = &event->auxtrace_error;

	if (e->type < PERF_AUXTRACE_ERROR_MAX)
		session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
}

void events_stats__auxtrace_error_warn(const struct events_stats *stats)
{
	int i;

	for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
		if (!stats->nr_auxtrace_errors[i])
			continue;
		ui__warning("%u %s errors\n",
			    stats->nr_auxtrace_errors[i],
			    auxtrace_error_name(i));
	}
}

int perf_event__process_auxtrace_error(struct perf_session *session,
				       union perf_event *event)
{
	if (auxtrace__dont_decode(session))
		return 0;

	perf_event__fprintf_auxtrace_error(event, stdout);
	return 0;
}

/*
 * In the compat mode kernel runs in 64-bit and perf tool runs in 32-bit mode,
 * 32-bit perf tool cannot access 64-bit value atomically, which might lead to
 * the issues caused by the below sequence on multiple CPUs: when perf tool
 * accesses either the load operation or the store operation for 64-bit value,
 * on some architectures the operation is divided into two instructions, one
 * is for accessing the low 32-bit value and another is for the high 32-bit;
 * thus these two user operations can give the kernel chances to access the
 * 64-bit value, and thus leads to the unexpected load values.
 *
 *   kernel (64-bit)                        user (32-bit)
 *
 *   if (LOAD ->aux_tail) { --,             LOAD ->aux_head_lo
 *       STORE $aux_data      |       ,--->
 *       FLUSH $aux_data      |       |     LOAD ->aux_head_hi
 *       STORE ->aux_head   --|-------`     smp_rmb()
 *   }                        |             LOAD $data
 *                            |             smp_mb()
 *                            |             STORE ->aux_tail_lo
 *                            `----------->
 *                                          STORE ->aux_tail_hi
 *
 * For this reason, it's impossible for the perf tool to work correctly when
 * the AUX head or tail is bigger than 4GB (more than 32 bits length); and we
 * can not simply limit the AUX ring buffer to less than 4GB, the reason is
 * the pointers can be increased monotonically, whatever the buffer size it is,
 * at the end the head and tail can be bigger than 4GB and carry out to the
 * high 32-bit.
 *
 * To mitigate the issues and improve the user experience, we can allow the
 * perf tool working in certain conditions and bail out with error if detect
 * any overflow cannot be handled.
 *
 * For reading the AUX head, it reads out the values for three times, and
 * compares the high 4 bytes of the values between the first time and the last
 * time, if there has no change for high 4 bytes injected by the kernel during
 * the user reading sequence, it's safe for use the second value.
 *
 * When compat_auxtrace_mmap__write_tail() detects any carrying in the high
 * 32 bits, it means there have two store operations in user space and it cannot
 * promise the atomicity for 64-bit write, so return '-1' in this case to tell
 * the caller an overflow error has happened.
 */
u64 __weak compat_auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
{
	struct perf_event_mmap_page *pc = mm->userpg;
	u64 first, second, last;
	u64 mask = (u64)(UINT32_MAX) << 32;

	do {
		first = READ_ONCE(pc->aux_head);
		/* Ensure all reads are done after we read the head */
		smp_rmb();
		second = READ_ONCE(pc->aux_head);
		/* Ensure all reads are done after we read the head */
		smp_rmb();
		last = READ_ONCE(pc->aux_head);
	} while ((first & mask) != (last & mask));

	return second;
}

int __weak compat_auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
{
	struct perf_event_mmap_page *pc = mm->userpg;
	u64 mask = (u64)(UINT32_MAX) << 32;

	if (tail & mask)
		return -1;

	/* Ensure all reads are done before we write the tail out */
	smp_mb();
	WRITE_ONCE(pc->aux_tail, tail);
	return 0;
}

static int __auxtrace_mmap__read(struct mmap *map,
				 struct auxtrace_record *itr,
				 struct perf_tool *tool, process_auxtrace_t fn,
				 bool snapshot, size_t snapshot_size)
{
	struct auxtrace_mmap *mm = &map->auxtrace_mmap;
	u64 head, old = mm->prev, offset, ref;
	unsigned char *data = mm->base;
	size_t size, head_off, old_off, len1, len2, padding;
	union perf_event ev;
	void *data1, *data2;
	int kernel_is_64_bit = perf_env__kernel_is_64_bit(evsel__env(NULL));

	head = auxtrace_mmap__read_head(mm, kernel_is_64_bit);

	if (snapshot &&
	    auxtrace_record__find_snapshot(itr, mm->idx, mm, data, &head, &old))
		return -1;

	if (old == head)
		return 0;

	pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
		  mm->idx, old, head, head - old);

	if (mm->mask) {
		head_off = head & mm->mask;
		old_off = old & mm->mask;
	} else {
		head_off = head % mm->len;
		old_off = old % mm->len;
	}

	if (head_off > old_off)
		size = head_off - old_off;
	else
		size = mm->len - (old_off - head_off);

	if (snapshot && size > snapshot_size)
		size = snapshot_size;

	ref = auxtrace_record__reference(itr);

	if (head > old || size <= head || mm->mask) {
		offset = head - size;
	} else {
		/*
		 * When the buffer size is not a power of 2, 'head' wraps at the
		 * highest multiple of the buffer size, so we have to subtract
		 * the remainder here.
		 */
		u64 rem = (0ULL - mm->len) % mm->len;

		offset = head - size - rem;
	}

	if (size > head_off) {
		len1 = size - head_off;
		data1 = &data[mm->len - len1];
		len2 = head_off;
		data2 = &data[0];
	} else {
		len1 = size;
		data1 = &data[head_off - len1];
		len2 = 0;
		data2 = NULL;
	}

	if (itr->alignment) {
		unsigned int unwanted = len1 % itr->alignment;

		len1 -= unwanted;
		size -= unwanted;
	}

	/* padding must be written by fn() e.g. record__process_auxtrace() */
	padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
	if (padding)
		padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;

	memset(&ev, 0, sizeof(ev));
	ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
	ev.auxtrace.header.size = sizeof(ev.auxtrace);
	ev.auxtrace.size = size + padding;
	ev.auxtrace.offset = offset;
	ev.auxtrace.reference = ref;
	ev.auxtrace.idx = mm->idx;
	ev.auxtrace.tid = mm->tid;
	ev.auxtrace.cpu = mm->cpu;

	if (fn(tool, map, &ev, data1, len1, data2, len2))
		return -1;

	mm->prev = head;

	if (!snapshot) {
		int err;

		err = auxtrace_mmap__write_tail(mm, head, kernel_is_64_bit);
		if (err < 0)
			return err;

		if (itr->read_finish) {
			err = itr->read_finish(itr, mm->idx);
			if (err < 0)
				return err;
		}
	}

	return 1;
}

int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr,
			struct perf_tool *tool, process_auxtrace_t fn)
{
	return __auxtrace_mmap__read(map, itr, tool, fn, false, 0);
}

int auxtrace_mmap__read_snapshot(struct mmap *map,
				 struct auxtrace_record *itr,
				 struct perf_tool *tool, process_auxtrace_t fn,
				 size_t snapshot_size)
{
	return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size);
}

/**
 * struct auxtrace_cache - hash table to implement a cache
 * @hashtable: the hashtable
 * @sz: hashtable size (number of hlists)
 * @entry_size: size of an entry
 * @limit: limit the number of entries to this maximum, when reached the cache
 *         is dropped and caching begins again with an empty cache
 * @cnt: current number of entries
 * @bits: hashtable size (@sz = 2^@bits)
 */
struct auxtrace_cache {
	struct hlist_head *hashtable;
	size_t sz;
	size_t entry_size;
	size_t limit;
	size_t cnt;
	unsigned int bits;
};

struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
					   unsigned int limit_percent)
{
	struct auxtrace_cache *c;
	struct hlist_head *ht;
	size_t sz, i;

	c = zalloc(sizeof(struct auxtrace_cache));
	if (!c)
		return NULL;

	sz = 1UL << bits;

	ht = calloc(sz, sizeof(struct hlist_head));
	if (!ht)
		goto out_free;

	for (i = 0; i < sz; i++)
		INIT_HLIST_HEAD(&ht[i]);

	c->hashtable = ht;
	c->sz = sz;
	c->entry_size = entry_size;
	c->limit = (c->sz * limit_percent) / 100;
	c->bits = bits;

	return c;

out_free:
	free(c);
	return NULL;
}

static void auxtrace_cache__drop(struct auxtrace_cache *c)
{
	struct auxtrace_cache_entry *entry;
	struct hlist_node *tmp;
	size_t i;

	if (!c)
		return;

	for (i = 0; i < c->sz; i++) {
		hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
			hlist_del(&entry->hash);
			auxtrace_cache__free_entry(c, entry);
		}
	}

	c->cnt = 0;
}

void auxtrace_cache__free(struct auxtrace_cache *c)
{
	if (!c)
		return;

	auxtrace_cache__drop(c);
	zfree(&c->hashtable);
	free(c);
}

void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
{
	return malloc(c->entry_size);
}

void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
				void *entry)
{
	free(entry);
}

int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
			struct auxtrace_cache_entry *entry)
{
	if (c->limit && ++c->cnt > c->limit)
		auxtrace_cache__drop(c);

	entry->key = key;
	hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);

	return 0;
}

static struct auxtrace_cache_entry *auxtrace_cache__rm(struct auxtrace_cache *c,
						       u32 key)
{
	struct auxtrace_cache_entry *entry;
	struct hlist_head *hlist;
	struct hlist_node *n;

	if (!c)
		return NULL;

	hlist = &c->hashtable[hash_32(key, c->bits)];
	hlist_for_each_entry_safe(entry, n, hlist, hash) {
		if (entry->key == key) {
			hlist_del(&entry->hash);
			return entry;
		}
	}

	return NULL;
}

void auxtrace_cache__remove(struct auxtrace_cache *c, u32 key)
{
	struct auxtrace_cache_entry *entry = auxtrace_cache__rm(c, key);

	auxtrace_cache__free_entry(c, entry);
}

void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
{
	struct auxtrace_cache_entry *entry;
	struct hlist_head *hlist;

	if (!c)
		return NULL;

	hlist = &c->hashtable[hash_32(key, c->bits)];
	hlist_for_each_entry(entry, hlist, hash) {
		if (entry->key == key)
			return entry;
	}

	return NULL;
}

static void addr_filter__free_str(struct addr_filter *filt)
{
	zfree(&filt->str);
	filt->action   = NULL;
	filt->sym_from = NULL;
	filt->sym_to   = NULL;
	filt->filename = NULL;
}

static struct addr_filter *addr_filter__new(void)
{
	struct addr_filter *filt = zalloc(sizeof(*filt));

	if (filt)
		INIT_LIST_HEAD(&filt->list);

	return filt;
}

static void addr_filter__free(struct addr_filter *filt)
{
	if (filt)
		addr_filter__free_str(filt);
	free(filt);
}

static void addr_filters__add(struct addr_filters *filts,
			      struct addr_filter *filt)
{
	list_add_tail(&filt->list, &filts->head);
	filts->cnt += 1;
}

static void addr_filters__del(struct addr_filters *filts,
			      struct addr_filter *filt)
{
	list_del_init(&filt->list);
	filts->cnt -= 1;
}

void addr_filters__init(struct addr_filters *filts)
{
	INIT_LIST_HEAD(&filts->head);
	filts->cnt = 0;
}

void addr_filters__exit(struct addr_filters *filts)
{
	struct addr_filter *filt, *n;

	list_for_each_entry_safe(filt, n, &filts->head, list) {
		addr_filters__del(filts, filt);
		addr_filter__free(filt);
	}
}

static int parse_num_or_str(char **inp, u64 *num, const char **str,
			    const char *str_delim)
{
	*inp += strspn(*inp, " ");

	if (isdigit(**inp)) {
		char *endptr;

		if (!num)
			return -EINVAL;
		errno = 0;
		*num = strtoull(*inp, &endptr, 0);
		if (errno)
			return -errno;
		if (endptr == *inp)
			return -EINVAL;
		*inp = endptr;
	} else {
		size_t n;

		if (!str)
			return -EINVAL;
		*inp += strspn(*inp, " ");
		*str = *inp;
		n = strcspn(*inp, str_delim);
		if (!n)
			return -EINVAL;
		*inp += n;
		if (**inp) {
			**inp = '\0';
			*inp += 1;
		}
	}
	return 0;
}

static int parse_action(struct addr_filter *filt)
{
	if (!strcmp(filt->action, "filter")) {
		filt->start = true;
		filt->range = true;
	} else if (!strcmp(filt->action, "start")) {
		filt->start = true;
	} else if (!strcmp(filt->action, "stop")) {
		filt->start = false;
	} else if (!strcmp(filt->action, "tracestop")) {
		filt->start = false;
		filt->range = true;
		filt->action += 5; /* Change 'tracestop' to 'stop' */
	} else {
		return -EINVAL;
	}
	return 0;
}

static int parse_sym_idx(char **inp, int *idx)
{
	*idx = -1;

	*inp += strspn(*inp, " ");

	if (**inp != '#')
		return 0;

	*inp += 1;

	if (**inp == 'g' || **inp == 'G') {
		*inp += 1;
		*idx = 0;
	} else {
		unsigned long num;
		char *endptr;

		errno = 0;
		num = strtoul(*inp, &endptr, 0);
		if (errno)
			return -errno;
		if (endptr == *inp || num > INT_MAX)
			return -EINVAL;
		*inp = endptr;
		*idx = num;
	}

	return 0;
}

static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
{
	int err = parse_num_or_str(inp, num, str, " ");

	if (!err && *str)
		err = parse_sym_idx(inp, idx);

	return err;
}

static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
{
	char *fstr;
	int err;

	filt->str = fstr = strdup(*filter_inp);
	if (!fstr)
		return -ENOMEM;

	err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
	if (err)
		goto out_err;

	err = parse_action(filt);
	if (err)
		goto out_err;

	err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
			      &filt->sym_from_idx);
	if (err)
		goto out_err;

	fstr += strspn(fstr, " ");

	if (*fstr == '/') {
		fstr += 1;
		err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
				      &filt->sym_to_idx);
		if (err)
			goto out_err;
		filt->range = true;
	}

	fstr += strspn(fstr, " ");

	if (*fstr == '@') {
		fstr += 1;
		err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
		if (err)
			goto out_err;
	}

	fstr += strspn(fstr, " ,");

	*filter_inp += fstr - filt->str;

	return 0;

out_err:
	addr_filter__free_str(filt);

	return err;
}

int addr_filters__parse_bare_filter(struct addr_filters *filts,
				    const char *filter)
{
	struct addr_filter *filt;
	const char *fstr = filter;
	int err;

	while (*fstr) {
		filt = addr_filter__new();
		err = parse_one_filter(filt, &fstr);
		if (err) {
			addr_filter__free(filt);
			addr_filters__exit(filts);
			return err;
		}
		addr_filters__add(filts, filt);
	}

	return 0;
}

struct sym_args {
	const char	*name;
	u64		start;
	u64		size;
	int		idx;
	int		cnt;
	bool		started;
	bool		global;
	bool		selected;
	bool		duplicate;
	bool		near;
};

static bool kern_sym_name_match(const char *kname, const char *name)
{
	size_t n = strlen(name);

	return !strcmp(kname, name) ||
	       (!strncmp(kname, name, n) && kname[n] == '\t');
}

static bool kern_sym_match(struct sym_args *args, const char *name, char type)
{
	/* A function with the same name, and global or the n'th found or any */
	return kallsyms__is_function(type) &&
	       kern_sym_name_match(name, args->name) &&
	       ((args->global && isupper(type)) ||
		(args->selected && ++(args->cnt) == args->idx) ||
		(!args->global && !args->selected));
}

static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
{
	struct sym_args *args = arg;

	if (args->started) {
		if (!args->size)
			args->size = start - args->start;
		if (args->selected) {
			if (args->size)
				return 1;
		} else if (kern_sym_match(args, name, type)) {
			args->duplicate = true;
			return 1;
		}
	} else if (kern_sym_match(args, name, type)) {
		args->started = true;
		args->start = start;
	}

	return 0;
}

static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
{
	struct sym_args *args = arg;

	if (kern_sym_match(args, name, type)) {
		pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
		       ++args->cnt, start, type, name);
		args->near = true;
	} else if (args->near) {
		args->near = false;
		pr_err("\t\twhich is near\t\t%s\n", name);
	}

	return 0;
}

static int sym_not_found_error(const char *sym_name, int idx)
{
	if (idx > 0) {
		pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
		       idx, sym_name);
	} else if (!idx) {
		pr_err("Global symbol '%s' not found.\n", sym_name);
	} else {
		pr_err("Symbol '%s' not found.\n", sym_name);
	}
	pr_err("Note that symbols must be functions.\n");

	return -EINVAL;
}

static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
{
	struct sym_args args = {
		.name = sym_name,
		.idx = idx,
		.global = !idx,
		.selected = idx > 0,
	};
	int err;

	*start = 0;
	*size = 0;

	err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
	if (err < 0) {
		pr_err("Failed to parse /proc/kallsyms\n");
		return err;
	}

	if (args.duplicate) {
		pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
		args.cnt = 0;
		kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
		pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
		       sym_name);
		pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
		return -EINVAL;
	}

	if (!args.started) {
		pr_err("Kernel symbol lookup: ");
		return sym_not_found_error(sym_name, idx);
	}

	*start = args.start;
	*size = args.size;

	return 0;
}

static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
			       char type, u64 start)
{
	struct sym_args *args = arg;

	if (!kallsyms__is_function(type))
		return 0;

	if (!args->started) {
		args->started = true;
		args->start = start;
	}
	/* Don't know exactly where the kernel ends, so we add a page */
	args->size = round_up(start, page_size) + page_size - args->start;

	return 0;
}

static int addr_filter__entire_kernel(struct addr_filter *filt)
{
	struct sym_args args = { .started = false };
	int err;

	err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
	if (err < 0 || !args.started) {
		pr_err("Failed to parse /proc/kallsyms\n");
		return err;
	}

	filt->addr = args.start;
	filt->size = args.size;

	return 0;
}

static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
{
	if (start + size >= filt->addr)
		return 0;

	if (filt->sym_from) {
		pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
		       filt->sym_to, start, filt->sym_from, filt->addr);
	} else {
		pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
		       filt->sym_to, start, filt->addr);
	}

	return -EINVAL;
}

static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
{
	bool no_size = false;
	u64 start, size;
	int err;

	if (symbol_conf.kptr_restrict) {
		pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
		return -EINVAL;
	}

	if (filt->sym_from && !strcmp(filt->sym_from, "*"))
		return addr_filter__entire_kernel(filt);

	if (filt->sym_from) {
		err = find_kern_sym(filt->sym_from, &start, &size,
				    filt->sym_from_idx);
		if (err)
			return err;
		filt->addr = start;
		if (filt->range && !filt->size && !filt->sym_to) {
			filt->size = size;
			no_size = !size;
		}
	}

	if (filt->sym_to) {
		err = find_kern_sym(filt->sym_to, &start, &size,
				    filt->sym_to_idx);
		if (err)
			return err;

		err = check_end_after_start(filt, start, size);
		if (err)
			return err;
		filt->size = start + size - filt->addr;
		no_size = !size;
	}

	/* The very last symbol in kallsyms does not imply a particular size */
	if (no_size) {
		pr_err("Cannot determine size of symbol '%s'\n",
		       filt->sym_to ? filt->sym_to : filt->sym_from);
		return -EINVAL;
	}

	return 0;
}

static struct dso *load_dso(const char *name)
{
	struct map *map;
	struct dso *dso;

	map = dso__new_map(name);
	if (!map)
		return NULL;

	if (map__load(map) < 0)
		pr_err("File '%s' not found or has no symbols.\n", name);

	dso = dso__get(map->dso);

	map__put(map);

	return dso;
}

static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
			  int idx)
{
	/* Same name, and global or the n'th found or any */
	return !arch__compare_symbol_names(name, sym->name) &&
	       ((!idx && sym->binding == STB_GLOBAL) ||
		(idx > 0 && ++*cnt == idx) ||
		idx < 0);
}

static void print_duplicate_syms(struct dso *dso, const char *sym_name)
{
	struct symbol *sym;
	bool near = false;
	int cnt = 0;

	pr_err("Multiple symbols with name '%s'\n", sym_name);

	sym = dso__first_symbol(dso);
	while (sym) {
		if (dso_sym_match(sym, sym_name, &cnt, -1)) {
			pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
			       ++cnt, sym->start,
			       sym->binding == STB_GLOBAL ? 'g' :
			       sym->binding == STB_LOCAL  ? 'l' : 'w',
			       sym->name);
			near = true;
		} else if (near) {
			near = false;
			pr_err("\t\twhich is near\t\t%s\n", sym->name);
		}
		sym = dso__next_symbol(sym);
	}

	pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
	       sym_name);
	pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
}

static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
			u64 *size, int idx)
{
	struct symbol *sym;
	int cnt = 0;

	*start = 0;
	*size = 0;

	sym = dso__first_symbol(dso);
	while (sym) {
		if (*start) {
			if (!*size)
				*size = sym->start - *start;
			if (idx > 0) {
				if (*size)
					return 1;
			} else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
				print_duplicate_syms(dso, sym_name);
				return -EINVAL;
			}
		} else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
			*start = sym->start;
			*size = sym->end - sym->start;
		}
		sym = dso__next_symbol(sym);
	}

	if (!*start)
		return sym_not_found_error(sym_name, idx);

	return 0;
}

static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
{
	if (dso__data_file_size(dso, NULL)) {
		pr_err("Failed to determine filter for %s\nCannot determine file size.\n",
		       filt->filename);
		return -EINVAL;
	}

	filt->addr = 0;
	filt->size = dso->data.file_size;

	return 0;
}

static int addr_filter__resolve_syms(struct addr_filter *filt)
{
	u64 start, size;
	struct dso *dso;
	int err = 0;

	if (!filt->sym_from && !filt->sym_to)
		return 0;

	if (!filt->filename)
		return addr_filter__resolve_kernel_syms(filt);

	dso = load_dso(filt->filename);
	if (!dso) {
		pr_err("Failed to load symbols from: %s\n", filt->filename);
		return -EINVAL;
	}

	if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
		err = addr_filter__entire_dso(filt, dso);
		goto put_dso;
	}

	if (filt->sym_from) {
		err = find_dso_sym(dso, filt->sym_from, &start, &size,
				   filt->sym_from_idx);
		if (err)
			goto put_dso;
		filt->addr = start;
		if (filt->range && !filt->size && !filt->sym_to)
			filt->size = size;
	}

	if (filt->sym_to) {
		err = find_dso_sym(dso, filt->sym_to, &start, &size,
				   filt->sym_to_idx);
		if (err)
			goto put_dso;

		err = check_end_after_start(filt, start, size);
		if (err)
			return err;

		filt->size = start + size - filt->addr;
	}

put_dso:
	dso__put(dso);

	return err;
}

static char *addr_filter__to_str(struct addr_filter *filt)
{
	char filename_buf[PATH_MAX];
	const char *at = "";
	const char *fn = "";
	char *filter;
	int err;

	if (filt->filename) {
		at = "@";
		fn = realpath(filt->filename, filename_buf);
		if (!fn)
			return NULL;
	}

	if (filt->range) {
		err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
			       filt->action, filt->addr, filt->size, at, fn);
	} else {
		err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
			       filt->action, filt->addr, at, fn);
	}

	return err < 0 ? NULL : filter;
}

static int parse_addr_filter(struct evsel *evsel, const char *filter,
			     int max_nr)
{
	struct addr_filters filts;
	struct addr_filter *filt;
	int err;

	addr_filters__init(&filts);

	err = addr_filters__parse_bare_filter(&filts, filter);
	if (err)
		goto out_exit;

	if (filts.cnt > max_nr) {
		pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
		       filts.cnt, max_nr);
		err = -EINVAL;
		goto out_exit;
	}

	list_for_each_entry(filt, &filts.head, list) {
		char *new_filter;

		err = addr_filter__resolve_syms(filt);
		if (err)
			goto out_exit;

		new_filter = addr_filter__to_str(filt);
		if (!new_filter) {
			err = -ENOMEM;
			goto out_exit;
		}

		if (evsel__append_addr_filter(evsel, new_filter)) {
			err = -ENOMEM;
			goto out_exit;
		}
	}

out_exit:
	addr_filters__exit(&filts);

	if (err) {
		pr_err("Failed to parse address filter: '%s'\n", filter);
		pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
		pr_err("Where multiple filters are separated by space or comma.\n");
	}

	return err;
}

static int evsel__nr_addr_filter(struct evsel *evsel)
{
	struct perf_pmu *pmu = evsel__find_pmu(evsel);
	int nr_addr_filters = 0;

	if (!pmu)
		return 0;

	perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);

	return nr_addr_filters;
}

int auxtrace_parse_filters(struct evlist *evlist)
{
	struct evsel *evsel;
	char *filter;
	int err, max_nr;

	evlist__for_each_entry(evlist, evsel) {
		filter = evsel->filter;
		max_nr = evsel__nr_addr_filter(evsel);
		if (!filter || !max_nr)
			continue;
		evsel->filter = NULL;
		err = parse_addr_filter(evsel, filter, max_nr);
		free(filter);
		if (err)
			return err;
		pr_debug("Address filter: %s\n", evsel->filter);
	}

	return 0;
}

int auxtrace__process_event(struct perf_session *session, union perf_event *event,
			    struct perf_sample *sample, struct perf_tool *tool)
{
	if (!session->auxtrace)
		return 0;

	return session->auxtrace->process_event(session, event, sample, tool);
}

void auxtrace__dump_auxtrace_sample(struct perf_session *session,
				    struct perf_sample *sample)
{
	if (!session->auxtrace || !session->auxtrace->dump_auxtrace_sample ||
	    auxtrace__dont_decode(session))
		return;

	session->auxtrace->dump_auxtrace_sample(session, sample);
}

int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool)
{
	if (!session->auxtrace)
		return 0;

	return session->auxtrace->flush_events(session, tool);
}

void auxtrace__free_events(struct perf_session *session)
{
	if (!session->auxtrace)
		return;

	return session->auxtrace->free_events(session);
}

void auxtrace__free(struct perf_session *session)
{
	if (!session->auxtrace)
		return;

	return session->auxtrace->free(session);
}

bool auxtrace__evsel_is_auxtrace(struct perf_session *session,
				 struct evsel *evsel)
{
	if (!session->auxtrace || !session->auxtrace->evsel_is_auxtrace)
		return false;

	return session->auxtrace->evsel_is_auxtrace(session, evsel);
}