aboutsummaryrefslogtreecommitdiffstats
path: root/src/tracefs-hist.c
blob: 302b9a75e6eef190628d91f08c19fe4caa678c64 (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
// SPDX-License-Identifier: LGPL-2.1
/*
 * Copyright (C) 2021 VMware Inc, Steven Rostedt <rostedt@goodmis.org>
 *
 * Updates:
 * Copyright (C) 2021, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/types.h>

#include "tracefs.h"
#include "tracefs-local.h"

#define HIST_FILE "hist"

#define ASCENDING ".ascending"
#define DESCENDING ".descending"

#define SYNTHETIC_GROUP "synthetic"

struct tracefs_hist {
	struct tep_handle	*tep;
	struct tep_event	*event;
	char			*system;
	char			*event_name;
	char			*name;
	char			**keys;
	char			**values;
	char			**sort;
	char			*filter;
	int			size;
	unsigned int		filter_parens;
	unsigned int		filter_state;
};

/*
 * tracefs_hist_get_name - get the name of the histogram
 * @hist: The histogram to get the name for
 *
 * Returns name string owned by @hist on success, or NULL on error.
 */
const char *tracefs_hist_get_name(struct tracefs_hist *hist)
{
	return hist ? hist->name : NULL;
}

/*
 * tracefs_hist_get_event - get the event name of the histogram
 * @hist: The histogram to get the event name for
 *
 * Returns event name string owned by @hist on success, or NULL on error.
 */
const char *tracefs_hist_get_event(struct tracefs_hist *hist)
{
	return hist ? hist->event_name : NULL;
}

/*
 * tracefs_hist_get_system - get the system name of the histogram
 * @hist: The histogram to get the system name for
 *
 * Returns system name string owned by @hist on success, or NULL on error.
 */
const char *tracefs_hist_get_system(struct tracefs_hist *hist)
{
	return hist ? hist->system : NULL;
}

static void add_list(struct trace_seq *seq, const char *start,
		     char **list)
{
	int i;

	trace_seq_puts(seq, start);
	for (i = 0; list[i]; i++) {
		if (i)
			trace_seq_putc(seq, ',');
		trace_seq_puts(seq, list[i]);
	}
}

static void add_hist_commands(struct trace_seq *seq, struct tracefs_hist *hist,
			     enum tracefs_hist_command command)
{
	if (command == TRACEFS_HIST_CMD_DESTROY)
		trace_seq_putc(seq, '!');

	add_list(seq, "hist:keys=", hist->keys);

	if (hist->values)
		add_list(seq, ":vals=", hist->values);

	if (hist->sort)
		add_list(seq, ":sort=", hist->sort);

	if (hist->size)
		trace_seq_printf(seq, ":size=%d", hist->size);

	switch(command) {
	case TRACEFS_HIST_CMD_START: break;
	case TRACEFS_HIST_CMD_PAUSE: trace_seq_puts(seq, ":pause"); break;
	case TRACEFS_HIST_CMD_CONT: trace_seq_puts(seq, ":cont"); break;
	case TRACEFS_HIST_CMD_CLEAR: trace_seq_puts(seq, ":clear"); break;
	default: break;
	}

	if (hist->name)
		trace_seq_printf(seq, ":name=%s", hist->name);

	if (hist->filter)
		trace_seq_printf(seq, " if %s", hist->filter);

}

/*
 * trace_hist_echo_cmd - show how to start the histogram
 * @seq: A trace_seq to store the commands to create
 * @hist: The histogram to write into the trigger file
 * @command: If not zero, can pause, continue or clear the histogram
 *
 * This shows the echo commands to create the histogram for an event
 * with the given fields.
 *
 * Returns 0 on succes -1 on error.
 */
int
tracefs_hist_echo_cmd(struct trace_seq *seq, struct tracefs_instance *instance,
		      struct tracefs_hist *hist,
		      enum tracefs_hist_command command)
{
	const char *system = hist->system;
	const char *event = hist->event_name;
	char *path;

	if (!hist->keys) {
		errno = -EINVAL;
		return -1;
	}

	path = tracefs_event_get_file(instance, system, event, "trigger");
	if (!path)
		return -1;

	trace_seq_puts(seq, "echo '");

	add_hist_commands(seq, hist, command);

	trace_seq_printf(seq, "' > %s\n", path);

	tracefs_put_tracing_file(path);

	return 0;
}

/*
 * tracefs_hist_command - Create, start, pause, destroy a histogram for an event
 * @instance: The instance the histogram will be in (NULL for toplevel)
 * @hist: The histogram to write into the trigger file
 * @command: Command to perform on a histogram.
 *
 * Creates, pause, continue, clears, or destroys a histogram.
 *
 * Returns 0 on succes -1 on error.
 */
int tracefs_hist_command(struct tracefs_instance *instance,
			 struct tracefs_hist *hist,
			 enum tracefs_hist_command command)
{
	const char *system = hist->system;
	const char *event = hist->event_name;
	struct trace_seq seq;
	int ret;

	if (!tracefs_event_file_exists(instance, system, event, HIST_FILE))
		return -1;

	errno = -EINVAL;
	if (!hist->keys)
		return -1;

	trace_seq_init(&seq);

	add_hist_commands(&seq, hist, command);

	trace_seq_putc(&seq, '\n');
	trace_seq_terminate(&seq);

	ret = -1;
	if (seq.state == TRACE_SEQ__GOOD)
		ret = tracefs_event_file_append(instance, system, event,
						"trigger", seq.buffer);

	trace_seq_destroy(&seq);

	return ret < 0 ? -1 : 0;
}

/**
 * tracefs_hist_free - free a tracefs_hist element
 * @hist: The histogram to free
 */
void tracefs_hist_free(struct tracefs_hist *hist)
{
	if (!hist)
		return;

	tep_unref(hist->tep);
	free(hist->system);
	free(hist->event_name);
	free(hist->name);
	free(hist->filter);
	tracefs_list_free(hist->keys);
	tracefs_list_free(hist->values);
	tracefs_list_free(hist->sort);
	free(hist);
}

/**
 * tracefs_hist_alloc - Initialize one-dimensional histogram
 * @tep: The tep handle that has the @system and @event.
 * @system: The system the histogram event is in.
 * @event_name: The name of the event that the histogram will be attached to.
 * @key: The primary key the histogram will use
 * @type: The format type of the key.
 *
 * Will initialize a histogram descriptor that will be attached to
 * the @system/@event with the given @key as the primary. This only
 * initializes the descriptor, it does not start the histogram
 * in the kernel.
 *
 * Returns an initialized histogram on success.
 * NULL on failure.
 */
struct tracefs_hist *
tracefs_hist_alloc(struct tep_handle *tep,
		   const char *system, const char *event_name,
		   const char *key, enum tracefs_hist_key_type type)
{
	struct tracefs_hist_axis axis[] = {{key, type}, {NULL, 0}};

	return tracefs_hist_alloc_nd(tep, system, event_name, axis);
}

/**
 * tracefs_hist_alloc_2d - Initialize two-dimensional histogram
 * @tep: The tep handle that has the @system and @event.
 * @system: The system the histogram event is in.
 * @event: The event that the histogram will be attached to.
 * @key1: The first primary key the histogram will use
 * @type1: The format type of the first key.
 * @key2: The second primary key the histogram will use
 * @type2: The format type of the second key.
 *
 * Will initialize a histogram descriptor that will be attached to
 * the @system/@event with the given @key1 and @key2 as the primaries.
 * This only initializes the descriptor, it does not start the histogram
 * in the kernel.
 *
 * Returns an initialized histogram on success.
 * NULL on failure.
 */
struct tracefs_hist *
tracefs_hist_alloc_2d(struct tep_handle *tep,
		      const char *system, const char *event_name,
		      const char *key1, enum tracefs_hist_key_type type1,
		      const char *key2, enum tracefs_hist_key_type type2)
{
	struct tracefs_hist_axis axis[] = {{key1, type1},
					   {key2, type2},
					   {NULL, 0}};

	return tracefs_hist_alloc_nd(tep, system, event_name, axis);
}

/**
 * tracefs_hist_alloc_nd - Initialize N-dimensional histogram
 * @tep: The tep handle that has the @system and @event.
 * @system: The system the histogram event is in
 * @event: The event that the histogram will be attached to
 * @axes: An array of histogram axes, terminated by a {NULL, 0} entry
 *
 * Will initialize a histogram descriptor that will be attached to
 * the @system/@event. This only initializes the descriptor with the given
 * @axes keys as primaries. This only initializes the descriptor, it does
 * not start the histogram in the kernel.
 *
 * Returns an initialized histogram on success.
 * NULL on failure.
 */
struct tracefs_hist *
tracefs_hist_alloc_nd(struct tep_handle *tep,
		      const char *system, const char *event_name,
		      struct tracefs_hist_axis *axes)
{
	struct tep_event *event;
	struct tracefs_hist *hist;

	if (!system || !event_name)
		return NULL;

	event = tep_find_event_by_name(tep, system, event_name);
	if (!event)
		return NULL;

	hist = calloc(1, sizeof(*hist));
	if (!hist)
		return NULL;

	tep_ref(tep);
	hist->tep = tep;
	hist->event = event;
	hist->system = strdup(system);
	hist->event_name = strdup(event_name);
	if (!hist->system || !hist->event_name)
		goto fail;

	for (; axes && axes->key; axes++)
		if (tracefs_hist_add_key(hist, axes->key, axes->type) < 0)
			goto fail;

	return hist;

 fail:
	tracefs_hist_free(hist);
	return NULL;
}

/**
 * tracefs_hist_add_key - add to a key to a histogram
 * @hist: The histogram to add the key to.
 * @key: The name of the key field.
 * @type: The type of the key format.
 *
 * This adds a secondary or tertiary key to the histogram.
 *
 * Returns 0 on success, -1 on error.
 */
int tracefs_hist_add_key(struct tracefs_hist *hist, const char *key,
			 enum tracefs_hist_key_type type)
{
	bool use_key = false;
	char *key_type = NULL;
	char **new_list;
	int ret = -1;

	switch (type) {
	case TRACEFS_HIST_KEY_NORMAL:
		use_key = true;
		ret = 0;
		break;
	case TRACEFS_HIST_KEY_HEX:
		ret = asprintf(&key_type, "%s.hex", key);
		break;
	case TRACEFS_HIST_KEY_SYM:
		ret = asprintf(&key_type, "%s.sym", key);
		break;
	case TRACEFS_HIST_KEY_SYM_OFFSET:
		ret = asprintf(&key_type, "%s.sym-offset", key);
		break;
	case TRACEFS_HIST_KEY_SYSCALL:
		ret = asprintf(&key_type, "%s.syscall", key);
		break;
	case TRACEFS_HIST_KEY_EXECNAME:
		ret = asprintf(&key_type, "%s.execname", key);
		break;
	case TRACEFS_HIST_KEY_LOG:
		ret = asprintf(&key_type, "%s.log2", key);
		break;
	case TRACEFS_HIST_KEY_USECS:
		ret = asprintf(&key_type, "%s.usecs", key);
		break;
	case TRACEFS_HIST_KEY_MAX:
		/* error */
		break;
	}

	if (ret < 0)
		return -1;

	new_list = tracefs_list_add(hist->keys, use_key ? key : key_type);
	free(key_type);
	if (!new_list)
		return -1;

	hist->keys = new_list;

	return 0;
}

/**
 * tracefs_hist_add_value - add to a value to a histogram
 * @hist: The histogram to add the value to.
 * @key: The name of the value field.
 *
 * This adds a value field to the histogram.
 *
 * Returns 0 on success, -1 on error.
 */
int tracefs_hist_add_value(struct tracefs_hist *hist, const char *value)
{
	char **new_list;

	new_list = tracefs_list_add(hist->values, value);
	if (!new_list)
		return -1;

	hist->values = new_list;

	return 0;
}

/**
 * tracefs_hist_add_name - name a histogram
 * @hist: The histogram to name.
 * @name: The name of the histogram.
 *
 * Adds a name to the histogram. Named histograms will share their
 * data with other events that have the same name as if it was
 * a single histogram.
 *
 * If the histogram already has a name, this will fail.
 *
 * Returns 0 on success, -1 on error.
 */
int tracefs_hist_add_name(struct tracefs_hist *hist, const char *name)
{
	if (hist->name)
		return -1;

	hist->name = strdup(name);

	return hist->name ? 0 : -1;
}

static char **
add_sort_key(struct tracefs_hist *hist, const char *sort_key, char **list)
{
	char **key_list = hist->keys;
	char **val_list = hist->values;
	int i;

	if (strcmp(sort_key, TRACEFS_HIST_HITCOUNT) == 0)
		goto out;

	for (i = 0; key_list[i]; i++) {
		if (strcmp(key_list[i], sort_key) == 0)
			break;
	}

	if (!key_list[i] && val_list) {
		for (i = 0; val_list[i]; i++) {
			if (strcmp(val_list[i], sort_key) == 0)
				break;
			if (!val_list[i])
				return NULL;
		}
	}


 out:
	return tracefs_list_add(list, sort_key);
}

/**
 * tracefs_hist_add_sort_key - add a key for sorting the histogram
 * @hist: The histogram to add the sort key to
 * @sort_key: The key to sort
 *
 * Call the function to add to the list of sort keys of the histohram in
 * the order of priority that the keys would be sorted on output.
 *
 * Returns 0 on success, -1 on error.
 */
int tracefs_hist_add_sort_key(struct tracefs_hist *hist,
			      const char *sort_key)
{
	char **list = hist->sort;

	if (!hist || !sort_key)
		return -1;

	list = add_sort_key(hist, sort_key, hist->sort);
	if (!list)
		return -1;

	hist->sort = list;

	return 0;
}

/**
 * tracefs_hist_set_sort_key - set a key for sorting the histogram
 * @hist: The histogram to set the sort key to
 * @sort_key: The key to sort (and the strings after it)
 *  Last one must be NULL.
 *
 * Set a list of sort keys in the order of priority that the
 * keys would be sorted on output. Keys must be added first.
 *
 * Returns 0 on success, -1 on error.
 */
int tracefs_hist_set_sort_key(struct tracefs_hist *hist,
			      const char *sort_key, ...)
{
	char **list = NULL;
	char **tmp;
	va_list ap;

	if (!hist || !sort_key)
		return -1;

	tmp = add_sort_key(hist, sort_key, list);
	if (!tmp)
		goto fail;
	list = tmp;

	va_start(ap, sort_key);
	for (;;) {
		sort_key = va_arg(ap, const char *);
		if (!sort_key)
			break;
		tmp = add_sort_key(hist, sort_key, list);
		if (!tmp)
			goto fail;
		list = tmp;
	}
	va_end(ap);

	tracefs_list_free(hist->sort);
	hist->sort = list;

	return 0;
 fail:
	tracefs_list_free(list);
	return -1;
}

static int end_match(const char *sort_key, const char *ending)
{
	int key_len = strlen(sort_key);
	int end_len = strlen(ending);

	if (key_len <= end_len)
		return 0;

	sort_key += key_len - end_len;

	return strcmp(sort_key, ending) == 0 ? key_len - end_len : 0;
}

/**
 * tracefs_hist_sort_key_direction - set direction of a sort key
 * @hist: The histogram to modify.
 * @sort_str: The sort key to set the direction for
 * @dir: The direction to set the sort key to.
 *
 * Returns 0 on success, and -1 on error;
 */
int tracefs_hist_sort_key_direction(struct tracefs_hist *hist,
				    const char *sort_str,
				    enum tracefs_hist_sort_direction dir)
{
	char **sort = hist->sort;
	char *sort_key;
	char *direct;
	int match;
	int i;

	if (!sort)
		return -1;

	for (i = 0; sort[i]; i++) {
		if (strcmp(sort[i], sort_str) == 0)
			break;
	}
	if (!sort[i])
		return -1;

	sort_key = sort[i];

	switch (dir) {
	case TRACEFS_HIST_SORT_ASCENDING:
		direct = ASCENDING;
		break;
	case TRACEFS_HIST_SORT_DESCENDING:
		direct = DESCENDING;
		break;
	default:
		return -1;
	}

	match = end_match(sort_key, ASCENDING);
	if (match) {
		/* Already match? */
		if (dir == TRACEFS_HIST_SORT_ASCENDING)
			return 0;
	} else {
		match = end_match(sort_key, DESCENDING);
		/* Already match? */
		if (match && dir == TRACEFS_HIST_SORT_DESCENDING)
			return 0;
	}

	if (match)
		/* Clear the original text */
		sort_key[match] = '\0';

	sort_key = realloc(sort_key, strlen(sort_key) + strlen(direct) + 1);
	if (!sort_key) {
		/* Failed to alloc, may need to put back the match */
		sort_key = sort[i];
		if (match)
			sort_key[match] = '.';
		return -1;
	}

	strcat(sort_key, direct);
	sort[i] = sort_key;
	return 0;
}

int tracefs_hist_append_filter(struct tracefs_hist *hist,
			       enum tracefs_filter type,
			       const char *field,
			       enum tracefs_compare compare,
			       const char *val)
{
	return trace_append_filter(&hist->filter, &hist->filter_state,
				   &hist->filter_parens,
				   hist->event,
				   type, field, compare, val);
}

enum action_type {
	ACTION_NONE,
	ACTION_TRACE,
	ACTION_SNAPSHOT,
	ACTION_SAVE,
};

struct action {
	struct action			*next;
	enum action_type		type;
	enum tracefs_synth_handler	handler;
	char				*handle_field;
	char				*save;
};

struct name_hash {
	struct name_hash	*next;
	char			*name;
	char			*hash;
};

/*
 * @name: name of the synthetic event
 * @start_system: system of the starting event
 * @start_event: the starting event
 * @end_system: system of the ending event
 * @end_event: the ending event
 * @actions: List of actions to take
 * @match_names: If a match set is to be a synthetic field, it has a name
 * @start_match: list of keys in the start event that matches end event
 * @end_match: list of keys in the end event that matches the start event
 * @compare_names: The synthetic field names of the compared fields
 * @start_compare: A list of compare fields in the start to compare to end
 * @end_compare: A list of compare fields in the end to compare to start
 * @compare_ops: The type of operations to perform between the start and end
 * @start_names: The fields in the start event to record
 * @end_names: The fields in the end event to record
 * @start_filters: The fields in the end event to record
 * @end_filters: The fields in the end event to record
 * @start_parens: Current parenthesis level for start event
 * @end_parens: Current parenthesis level for end event
 * @new_format: onmatch().trace(synth_event,..) or onmatch().synth_event(...)
 */
struct tracefs_synth {
	struct tracefs_instance *instance;
	struct tep_handle	*tep;
	struct tep_event	*start_event;
	struct tep_event	*end_event;
	struct action		*actions;
	struct action		**next_action;
	struct tracefs_dynevent	*dyn_event;
	struct name_hash	*name_hash[1 << HASH_BITS];
	char			*start_hist;
	char			*end_hist;
	char			*name;
	char			**synthetic_fields;
	char			**synthetic_args;
	char			**start_selection;
	char			**start_keys;
	char			**end_keys;
	char			**start_vars;
	char			**end_vars;
	char			*start_filter;
	char			*end_filter;
	unsigned int		start_parens;
	unsigned int		start_state;
	unsigned int		end_parens;
	unsigned int		end_state;
	int			*start_type;
	char			arg_name[16];
	int			arg_cnt;
	bool			new_format;
};

 /*
 * tracefs_synth_get_name - get the name of the synthetic event
 * @synth: The synthetic event to get the name for
 *
 * Returns name string owned by @synth on success, or NULL on error.
 */
const char *tracefs_synth_get_name(struct tracefs_synth *synth)
{
	return synth ? synth->name : NULL;
}

static void action_free(struct action *action)
{
	free(action->handle_field);
	free(action->save);
	free(action);
}

static void free_name_hash(struct name_hash **hash)
{
	struct name_hash *item;
	int i;

	for (i = 0; i < 1 << HASH_BITS; i++) {
		while ((item = hash[i])) {
			hash[i] = item->next;
			free(item->name);
			free(item->hash);
			free(item);
		}
	}
}

/**
 * tracefs_synth_free - free the resources alloced to a synth
 * @synth: The tracefs_synth descriptor
 *
 * Frees the resources allocated for a @synth created with
 * tracefs_synth_alloc(). It does not touch the system. That is,
 * any synthetic event created, will not be destroyed by this
 * function.
 */
void tracefs_synth_free(struct tracefs_synth *synth)
{
	struct action *action;

	if (!synth)
		return;

	free(synth->name);
	free(synth->start_hist);
	free(synth->end_hist);
	tracefs_list_free(synth->synthetic_fields);
	tracefs_list_free(synth->synthetic_args);
	tracefs_list_free(synth->start_selection);
	tracefs_list_free(synth->start_keys);
	tracefs_list_free(synth->end_keys);
	tracefs_list_free(synth->start_vars);
	tracefs_list_free(synth->end_vars);
	free_name_hash(synth->name_hash);
	free(synth->start_filter);
	free(synth->end_filter);
	free(synth->start_type);

	tep_unref(synth->tep);

	while ((action = synth->actions)) {
		synth->actions = action->next;
		action_free(action);
	}
	tracefs_dynevent_free(synth->dyn_event);

	free(synth);
}

static bool verify_event_fields(struct tep_event *start_event,
				struct tep_event *end_event,
				const char *start_field_name,
				const char *end_field_name,
				const struct tep_format_field **ptr_start_field)
{
	const struct tep_format_field *start_field;
	const struct tep_format_field *end_field;

	if (!trace_verify_event_field(start_event, start_field_name,
				      &start_field))
		return false;

	if (end_event) {
		if (!trace_verify_event_field(end_event, end_field_name,
					      &end_field))
			return false;

		if (start_field->flags != end_field->flags ||
		    start_field->size != end_field->size) {
			errno = EBADE;
			return false;
		}
	}

	if (ptr_start_field)
		*ptr_start_field = start_field;

	return true;
}

__hidden char *append_string(char *str, const char *space, const char *add)
{
	char *new;
	int len;

	/* String must already be allocated */
	if (!str)
		return NULL;

	len = strlen(str) + strlen(add) + 2;
	if (space)
		len += strlen(space);

	new = realloc(str, len);
	if (!new) {
		free(str);
		return NULL;
	}
	str = new;

	if (space)
		strcat(str, space);
	strcat(str, add);

	return str;
}

static char *add_synth_field(const struct tep_format_field *field,
			     const char *name)
{
	const char *type;
	char size[64];
	char *str;
	bool sign;

	if (field->flags & TEP_FIELD_IS_ARRAY) {
		str = strdup("char");
		str = append_string(str, " ", name);
		str = append_string(str, NULL, "[");

		if (!(field->flags & TEP_FIELD_IS_DYNAMIC)) {
			snprintf(size, 64, "%d", field->size);
			str = append_string(str, NULL, size);
		}
		return append_string(str, NULL, "];");
	}

	/* Synthetic events understand pid_t, gfp_t and bool */
	if (strcmp(field->type, "pid_t") == 0 ||
	    strcmp(field->type, "gfp_t") == 0 ||
	    strcmp(field->type, "bool") == 0) {
		str = strdup(field->type);
		str = append_string(str, " ", name);
		return append_string(str, NULL, ";");
	}

	sign = field->flags & TEP_FIELD_IS_SIGNED;

	switch (field->size) {
	case 1:
		if (!sign)
			type = "unsigned char";
		else
			type = "char";
		break;
	case 2:
		if (sign)
			type = "s16";
		else
			type = "u16";
		break;
	case 4:
		if (sign)
			type = "s32";
		else
			type = "u32";
		break;
	case 8:
		if (sign)
			type = "s64";
		else
			type = "u64";
		break;
	default:
		errno = EBADF;
		return NULL;
	}

	str = strdup(type);
	str = append_string(str, " ", name);
	return append_string(str, NULL, ";");
}

static int add_var(char ***list, const char *name, const char *var, bool is_var)
{
	char **new;
	char *assign;
	int ret;

	if (is_var)
		ret = asprintf(&assign, "%s=$%s", name, var);
	else
		ret = asprintf(&assign, "%s=%s", name, var);

	if (ret < 0)
		return -1;

	new = tracefs_list_add(*list, assign);
	free(assign);

	if (!new)
		return -1;
	*list = new;
	return 0;
}

__hidden struct tracefs_synth *
synth_init_from(struct tep_handle *tep, const char *start_system,
		const char *start_event_name)
{
	struct tep_event *start_event;
	struct tracefs_synth *synth;

	start_event = tep_find_event_by_name(tep, start_system,
					     start_event_name);
	if (!start_event) {
		errno = ENODEV;
		return NULL;
	}

	synth = calloc(1, sizeof(*synth));
	if (!synth)
		return NULL;

	synth->start_event = start_event;
	synth->next_action = &synth->actions;

	/* Hold onto a reference to this handler */
	tep_ref(tep);
	synth->tep = tep;

	return synth;
}

static int alloc_synthetic_event(struct tracefs_synth *synth)
{
	char *format;
	const char *field;
	int i;

	format = strdup("");
	if (!format)
		return -1;

	for (i = 0; synth->synthetic_fields && synth->synthetic_fields[i]; i++) {
		field = synth->synthetic_fields[i];
		format = append_string(format, i ? " " : NULL, field);
	}

	synth->dyn_event = dynevent_alloc(TRACEFS_DYNEVENT_SYNTH, SYNTHETIC_GROUP,
					  synth->name, NULL, format);
	free(format);

	return synth->dyn_event ? 0 : -1;
}

/*
 * See if it is onmatch().trace(synth_event,...) or
 *   onmatch().synth_event(...)
 */
static bool has_new_format()
{
	char *readme;
	char *p;
	int size;

	readme = tracefs_instance_file_read(NULL, "README", &size);
	if (!readme)
		return false;

	p = strstr(readme, "trace(<synthetic_event>,param list)");
	free(readme);

	return p != NULL;
}

/**
 * tracefs_synth_alloc - create a new tracefs_synth instance
 * @tep: The tep handle that holds the events to work on
 * @name: The name of the synthetic event being created
 * @start_system: The name of the system of the start event (can be NULL)
 * @start_event_name: The name of the start event
 * @end_system: The name of the system of the end event (can be NULL)
 * @end_event_name: The name of the end event
 * @start_match_field: The name of the field in start event to match @end_match_field
 * @end_match_field: The name of the field in end event to match @start_match_field
 * @match_name: Name to call the fields that match (can be NULL)
 * 
 * Creates a tracefs_synth instance that has the minimum requirements to
 * create a synthetic event.
 *
 * @name is will be the name of the synthetic event that this can create.
 *
 * The start event is found with @start_system and @start_event_name. If
 * @start_system is NULL, then the first event with @start_event_name will
 * be used.
 *
 * The end event is found with @end_system and @end_event_name. If
 * @end_system is NULL, then the first event with @end_event_name will
 * be used.
 *
 * The @start_match_field is the field in the start event that will be used
 * to match the @end_match_field of the end event.
 *
 * If @match_name is given, then the field that matched the start and
 * end events will be passed an a field to the sythetic event with this
 * as the field name.
 *
 * Returns an allocated tracefs_synth descriptor on success and NULL
 * on error, with the following set in errno.
 *
 * ENOMEM - memory allocation failure.
 * ENIVAL - a parameter is passed as NULL that should not be
 * ENODEV - could not find an event or field
 * EBADE - The start and end fields are not compatible to match 
 * 
 * Note, this does not modify the system. That is, the synthetic
 * event on the system is not created. That needs to be done with
 * tracefs_synth_create().
 */
struct tracefs_synth *tracefs_synth_alloc(struct tep_handle *tep,
					  const char *name,
					  const char *start_system,
					  const char *start_event_name,
					  const char *end_system,
					  const char *end_event_name,
					  const char *start_match_field,
					  const char *end_match_field,
					  const char *match_name)
{
	struct tep_event *end_event;
	struct tracefs_synth *synth;
	int ret = 0;

	if (!tep || !name || !start_event_name || !end_event_name ||
	    !start_match_field || !end_match_field) {
		errno = EINVAL;
		return NULL;
	}

	synth = synth_init_from(tep, start_system, start_event_name);
	if (!synth)
		return NULL;

	end_event = tep_find_event_by_name(tep, end_system,
					   end_event_name);
	if (!end_event) {
		tep_unref(tep);
		errno = ENODEV;
		return NULL;
	}

	synth->end_event = end_event;

	synth->name = strdup(name);

	ret = tracefs_synth_add_match_field(synth, start_match_field,
					    end_match_field, match_name);

	if (!synth->name || !synth->start_keys || !synth->end_keys || ret) {
		tracefs_synth_free(synth);
		synth = NULL;
	} else
		synth->new_format = has_new_format();

	return synth;
}

static int add_synth_fields(struct tracefs_synth *synth,
			    const struct tep_format_field *field,
			    const char *name, const char *var)
{
	char **list;
	char *str;
	int ret;

	str = add_synth_field(field, name ? : field->name);
	if (!str)
		return -1;

	if (!name)
		name = var;

	list = tracefs_list_add(synth->synthetic_fields, str);
	free(str);
	if (!list)
		return -1;
	synth->synthetic_fields = list;

	ret = asprintf(&str, "$%s", var ? : name);
	if (ret < 0) {
		trace_list_pop(synth->synthetic_fields);
		return -1;
	}

	list = tracefs_list_add(synth->synthetic_args, str);
	free(str);
	if (!list) {
		trace_list_pop(synth->synthetic_fields);
		return -1;
	}

	synth->synthetic_args = list;

	return 0;
}

/**
 * tracefs_synth_add_match_field - add another key to match events
 * @synth: The tracefs_synth descriptor
 * @start_match_field: The field of the start event to match the end event
 * @end_match_field: The field of the end event to match the start event
 * @name: The name to show in the synthetic event (NULL is allowed)
 *
 * This will add another set of keys to use for a match between
 * the start event and the end event.
 *
 * Returns 0 on succes and -1 on error.
 * On error, errno is set to:
 * ENOMEM - memory allocation failure.
 * ENIVAL - a parameter is passed as NULL that should not be
 * ENODEV - could not find a field
 * EBADE - The start and end fields are not compatible to match 
 */
int tracefs_synth_add_match_field(struct tracefs_synth *synth,
				  const char *start_match_field,
				  const char *end_match_field,
				  const char *name)
{
	const struct tep_format_field *key_field;
	char **list;
	int ret;

	if (!synth || !start_match_field || !end_match_field) {
		errno = EINVAL;
		return -1;
	}

	if (!verify_event_fields(synth->start_event, synth->end_event,
				 start_match_field, end_match_field,
				 &key_field))
		return -1;

	list = tracefs_list_add(synth->start_keys, start_match_field);
	if (!list)
		return -1;

	synth->start_keys = list;

	list = tracefs_list_add(synth->end_keys, end_match_field);
	if (!list) {
		trace_list_pop(synth->start_keys);
		return -1;
	}
	synth->end_keys = list;

	if (!name)
		return 0;

	ret = add_var(&synth->end_vars, name, end_match_field, false);

	if (ret < 0)
		goto pop_lists;

	ret = add_synth_fields(synth, key_field, name, NULL);
	if (ret < 0)
		goto pop_lists;

	return 0;

 pop_lists:
	trace_list_pop(synth->start_keys);
	trace_list_pop(synth->end_keys);
	return -1;
}

static unsigned int make_rand(void)
{
	struct timeval tv;
	unsigned long seed;

	gettimeofday(&tv, NULL);
	seed = (tv.tv_sec + tv.tv_usec) + getpid();

	/* taken from the rand(3) man page */
	seed = seed * 1103515245 + 12345;
	return((unsigned)(seed/65536) % 32768);
}

static char *new_name(struct tracefs_synth *synth, const char *name)
{
	int cnt = synth->arg_cnt + 1;
	char *arg;
	int ret;

	/* Create a unique argument name */
	if (!synth->arg_name[0]) {
		/* make_rand() returns at most 32768 (total 13 bytes in use) */
		sprintf(synth->arg_name, "%u", make_rand());
	}
	ret = asprintf(&arg, "__%s_%s_%d", name, synth->arg_name, cnt);
	if (ret < 0)
		return NULL;

	synth->arg_cnt = cnt;
	return arg;
}

static struct name_hash *find_name(struct tracefs_synth *synth, const char *name)
{
	unsigned int key = quick_hash(name);
	struct name_hash *hash = synth->name_hash[key];

	for (; hash; hash = hash->next) {
		if (!strcmp(hash->name, name))
			return hash;
	}
	return NULL;
}

static const char *hash_name(struct tracefs_synth *synth, const char *name)
{
	struct name_hash *hash;
	int key;

	hash = find_name(synth, name);
	if (hash)
		return hash->hash;

	hash = malloc(sizeof(*hash));
	if (!hash)
		return name;

	hash->hash = new_name(synth, name);
	if (!hash->hash) {
		free(hash);
		return name;
	}

	key = quick_hash(name);
	hash->next = synth->name_hash[key];
	synth->name_hash[key] = hash;

	hash->name = strdup(name);
	if (!hash->name) {
		free(hash->hash);
		free(hash);
		return name;
	}
	return hash->hash;
}

static char *new_arg(struct tracefs_synth *synth)
{
	return new_name(synth, "arg");
}

/**
 * tracefs_synth_add_compare_field - add a comparison between start and end
 * @synth: The tracefs_synth descriptor
 * @start_compare_field: The field of the start event to compare to the end
 * @end_compare_field: The field of the end event to compare to the start
 * @calc - How to go about the comparing the fields.
 * @name: The name to show in the synthetic event (must NOT be NULL)
 *
 * This will add a way to compare two different fields between the
 * start end end events.
 *
 * The comparing between events is decided by @calc:
 *    TRACEFS_SYNTH_DELTA_END       - name = end - start
 *    TRACEFS_SYNTH_DELTA_START     - name = start - end
 *    TRACEFS_SYNTH_ADD             - name = end + start
 *
 * Returns 0 on succes and -1 on error.
 * On error, errno is set to:
 * ENOMEM - memory allocation failure.
 * ENIVAL - a parameter is passed as NULL that should not be
 * ENODEV - could not find a field
 * EBADE - The start and end fields are not compatible to compare
 */
int tracefs_synth_add_compare_field(struct tracefs_synth *synth,
				    const char *start_compare_field,
				    const char *end_compare_field,
				    enum tracefs_synth_calc calc,
				    const char *name)
{
	const struct tep_format_field *start_field;
	const char *hname;
	char *start_arg;
	char *compare;
	int ret;

	/* Compare fields require a name */
	if (!name || !start_compare_field || !end_compare_field) {
		errno = -EINVAL;
		return -1;
	}

	if (!verify_event_fields(synth->start_event, synth->end_event,
				 start_compare_field, end_compare_field,
				 &start_field))
		return -1;

	/* Calculations are not allowed on string */
	if (start_field->flags & (TEP_FIELD_IS_ARRAY |
				  TEP_FIELD_IS_DYNAMIC)) {
		errno = -EINVAL;
		return -1;
	}

	start_arg = new_arg(synth);
	if (!start_arg)
		return -1;

	ret = add_var(&synth->start_vars, start_arg, start_compare_field, false);
	if (ret < 0) {
		free(start_arg);
		return -1;
	}

	ret = -1;
	switch (calc) {
	case TRACEFS_SYNTH_DELTA_END:
		ret = asprintf(&compare, "%s-$%s", end_compare_field,
			       start_arg);
		break;
	case TRACEFS_SYNTH_DELTA_START:
		ret = asprintf(&compare, "$%s-%s", start_arg,
			       end_compare_field);
		break;
	case TRACEFS_SYNTH_ADD:
		ret = asprintf(&compare, "%s+$%s", end_compare_field,
			       start_arg);
		break;
	}
	free(start_arg);
	if (ret < 0)
		return -1;

	hname = hash_name(synth, name);
	ret = add_var(&synth->end_vars, hname, compare, false);
	if (ret < 0)
		goto out_free;

	ret = add_synth_fields(synth, start_field, name, hname);
	if (ret < 0)
		goto out_free;

 out_free:
	free(compare);

	return ret ? -1 : 0;
}

__hidden int synth_add_start_field(struct tracefs_synth *synth,
				   const char *start_field,
				   const char *name,
				   enum tracefs_hist_key_type type)
{
	const struct tep_format_field *field;
	const char *var;
	char *start_arg;
	char **tmp;
	int *types;
	int len;
	int ret;

	if (!synth || !start_field) {
		errno = EINVAL;
		return -1;
	}

	if (!name)
		name = start_field;

	var = hash_name(synth, name);

	if (!trace_verify_event_field(synth->start_event, start_field, &field))
		return -1;

	start_arg = new_arg(synth);
	if (!start_arg)
		return -1;

	ret = add_var(&synth->start_vars, start_arg, start_field, false);
	if (ret)
		goto out_free;

	ret = add_var(&synth->end_vars, var, start_arg, true);
	if (ret)
		goto out_free;

	ret = add_synth_fields(synth, field, name, var);
	if (ret)
		goto out_free;

	tmp = tracefs_list_add(synth->start_selection, start_field);
	if (!tmp) {
		ret = -1;
		goto out_free;
	}
	synth->start_selection = tmp;

	len = tracefs_list_size(tmp);
	if (len <= 0) { /* ?? */
		ret = -1;
		goto out_free;
	}

	types = realloc(synth->start_type, sizeof(*types) * len);
	if (!types) {
		ret = -1;
		goto out_free;
	}
	synth->start_type = types;
	synth->start_type[len - 1] = type;

 out_free:
	free(start_arg);
	return ret;
}

/**
 * tracefs_synth_add_start_field - add a start field to save
 * @synth: The tracefs_synth descriptor
 * @start_field: The field of the start event to save
 * @name: The name to show in the synthetic event (if NULL @start_field is used)
 *
 * This adds a field named by @start_field of the start event to
 * record in the synthetic event.
 *
 * Returns 0 on succes and -1 on error.
 * On error, errno is set to:
 * ENOMEM - memory allocation failure.
 * ENIVAL - a parameter is passed as NULL that should not be
 * ENODEV - could not find a field
 */
int tracefs_synth_add_start_field(struct tracefs_synth *synth,
				  const char *start_field,
				  const char *name)
{
	return synth_add_start_field(synth, start_field, name, 0);
}

/**
 * tracefs_synth_add_end_field - add a end field to save
 * @synth: The tracefs_synth descriptor
 * @end_field: The field of the end event to save
 * @name: The name to show in the synthetic event (if NULL @end_field is used)
 *
 * This adds a field named by @end_field of the start event to
 * record in the synthetic event.
 *
 * Returns 0 on succes and -1 on error.
 * On error, errno is set to:
 * ENOMEM - memory allocation failure.
 * ENIVAL - a parameter is passed as NULL that should not be
 * ENODEV - could not find a field
 */
int tracefs_synth_add_end_field(struct tracefs_synth *synth,
				const char *end_field,
				const char *name)
{
	const struct tep_format_field *field;
	const char *hname = NULL;
	char *tmp_var = NULL;
	int ret;

	if (!synth || !end_field) {
		errno = EINVAL;
		return -1;
	}

	if (name) {
		if (strncmp(name, "__arg", 5) != 0)
			hname = hash_name(synth, name);
		else
			hname = name;
	}

	if (!name)
		tmp_var = new_arg(synth);

	if (!trace_verify_event_field(synth->end_event, end_field, &field))
		return -1;

	ret = add_var(&synth->end_vars, name ? hname : tmp_var, end_field, false);
	if (ret)
		goto out;

	ret = add_synth_fields(synth, field, name, hname ? : tmp_var);
	free(tmp_var);
 out:
	return ret;
}

/**
 * tracefs_synth_append_start_filter - create or append a filter
 * @synth: The tracefs_synth descriptor
 * @type: The type of element to add to the filter
 * @field: For @type == TRACEFS_FILTER_COMPARE, the field to compare
 * @compare: For @type == TRACEFS_FILTER_COMPARE, how to compare @field to @val
 * @val: For @type == TRACEFS_FILTER_COMPARE, what value @field is to be
 *
 * This will put together a filter string for the starting event
 * of @synth. It check to make sure that what is added is correct compared
 * to the filter that is already built.
 *
 * @type can be:
 *     TRACEFS_FILTER_COMPARE:        See below
 *     TRACEFS_FILTER_AND:            Append "&&" to the filter
 *     TRACEFS_FILTER_OR:             Append "||" to the filter
 *     TRACEFS_FILTER_NOT:            Append "!" to the filter
 *     TRACEFS_FILTER_OPEN_PAREN:     Append "(" to the filter
 *     TRACEFS_FILTER_CLOSE_PAREN:    Append ")" to the filter
 *
 * For all types except TRACEFS_FILTER_COMPARE, the @field, @compare,
 * and @val are ignored.
 *
 * For @type == TRACEFS_FILTER_COMPARE.
 *
 *  @field is the name of the field for the start event to compare.
 *         If it is not a field for the start event, this return an
 *         error.
 *
 *  @compare can be one of:
 *     TRACEFS_COMPARE_EQ:       Test @field == @val
 *     TRACEFS_COMPARE_NE:       Test @field != @val
 *     TRACEFS_COMPARE_GT:       Test @field > @val
 *     TRACEFS_COMPARE_GE:       Test @field >= @val
 *     TRACEFS_COMPARE_LT:       Test @field < @val
 *     TRACEFS_COMPARE_LE:       Test @field <= @val
 *     TRACEFS_COMPARE_RE:       Test @field ~ @val
 *     TRACEFS_COMPARE_AND:      Test @field & @val
 *
 * If the @field is of type string, and @compare is not
 *   TRACEFS_COMPARE_EQ, TRACEFS_COMPARE_NE or TRACEFS_COMPARE_RE,
 *   then this will return an error.
 *
 * Various other checks are made, for instance, if more CLOSE_PARENs
 * are added than existing OPEN_PARENs. Or if AND is added after an
 * OPEN_PAREN or another AND or an OR or a NOT.
 *
 * Returns 0 on success and -1 on failure.
 */
int tracefs_synth_append_start_filter(struct tracefs_synth *synth,
				      enum tracefs_filter type,
				      const char *field,
				      enum tracefs_compare compare,
				      const char *val)
{
	return trace_append_filter(&synth->start_filter, &synth->start_state,
				   &synth->start_parens,
				   synth->start_event,
				   type, field, compare, val);
}

/**
 * tracefs_synth_append_end_filter - create or append a filter
 * @synth: The tracefs_synth descriptor
 * @type: The type of element to add to the filter
 * @field: For @type == TRACEFS_FILTER_COMPARE, the field to compare
 * @compare: For @type == TRACEFS_FILTER_COMPARE, how to compare @field to @val
 * @val: For @type == TRACEFS_FILTER_COMPARE, what value @field is to be
 *
 * Performs the same thing as tracefs_synth_append_start_filter() but
 * for the @synth end event.
 */
int tracefs_synth_append_end_filter(struct tracefs_synth *synth,
				    enum tracefs_filter type,
				    const char *field,
				    enum tracefs_compare compare,
				    const char *val)
{
	return trace_append_filter(&synth->end_filter, &synth->end_state,
				   &synth->end_parens,
				   synth->end_event,
				   type, field, compare, val);
}

static int test_max_var(struct tracefs_synth *synth, const char *var)
{
	char **vars = synth->end_vars;
	char *p;
	int len;
	int i;

	len = strlen(var);

	/* Make sure the var is defined for the end event */
	for (i = 0; vars[i]; i++) {
		p = strchr(vars[i], '=');
		if (!p)
			continue;
		if (p - vars[i] != len)
			continue;
		if (!strncmp(var, vars[i], len))
			return 0;
	}
	errno = ENODEV;
	return -1;
}

static struct action *create_action(enum tracefs_synth_handler type,
				    struct tracefs_synth *synth,
				    const char *var)
{
	struct action *action;
	int ret;

	switch (type) {
	case TRACEFS_SYNTH_HANDLE_MAX:
	case TRACEFS_SYNTH_HANDLE_CHANGE:
		ret = test_max_var(synth, var);
		if (ret < 0)
			return NULL;
		break;
	default:
		break;
	}

	action = calloc(1, sizeof(*action));
	if (!action)
		return NULL;

	if (var) {
		ret = asprintf(&action->handle_field, "$%s", var);
		if (!action->handle_field) {
			free(action);
			return NULL;
		}
	}
	return action;
}

static void add_action(struct tracefs_synth *synth, struct action *action)
{
	*synth->next_action = action;
	synth->next_action = &action->next;
}

/**
 * tracefs_synth_trace - Execute the trace option
 * @synth: The tracefs_synth descriptor
 * @type: The type of handler to attach the trace action with
 * @field: The field for handlers onmax and onchange (ignored otherwise)
 *
 * Add the action 'trace' for handlers onmatch, onmax and onchange.
 *
 * Returns 0 on succes, -1 on error.
 */
int tracefs_synth_trace(struct tracefs_synth *synth,
			enum tracefs_synth_handler type, const char *field)
{
	struct action *action;

	if (!synth || (!field && (type != TRACEFS_SYNTH_HANDLE_MATCH))) {
		errno = EINVAL;
		return -1;
	}

	action = create_action(type, synth, field);
	if (!action)
		return -1;

	action->type = ACTION_TRACE;
	action->handler = type;
	add_action(synth, action);
	return 0;
}

/**
 * tracefs_synth_snapshot - create a snapshot command to the histogram
 * @synth: The tracefs_synth descriptor
 * @type: The type of handler to attach the snapshot action with
 * @field: The field for handlers onmax and onchange
 *
 * Add the action to do a snapshot for handlers onmax and onchange.
 *
 * Returns 0 on succes, -1 on error.
 */
int tracefs_synth_snapshot(struct tracefs_synth *synth,
			   enum tracefs_synth_handler type, const char *field)
{
	struct action *action;

	if (!synth || !field || (type == TRACEFS_SYNTH_HANDLE_MATCH)) {
		errno = EINVAL;
		return -1;
	}

	action = create_action(type, synth, field);
	if (!action)
		return -1;

	action->type = ACTION_SNAPSHOT;
	action->handler = type;
	add_action(synth, action);
	return 0;
}

/**
 * tracefs_synth_save - create a save command to the histogram
 * @synth: The tracefs_synth descriptor
 * @type: The type of handler to attach the save action
 * @max_field: The field for handlers onmax and onchange
 * @fields: The fields to save for the end variable
 *
 * Add the action to save fields for handlers onmax and onchange.
 *
 * Returns 0 on succes, -1 on error.
 */
int tracefs_synth_save(struct tracefs_synth *synth,
		       enum tracefs_synth_handler type, const char *max_field,
		       char **fields)
{
	struct action *action;
	char *save;
	int i;

	if (!synth || !max_field || (type == TRACEFS_SYNTH_HANDLE_MATCH)) {
		errno = EINVAL;
		return -1;
	}

	action = create_action(type, synth, max_field);
	if (!action)
		return -1;

	action->type = ACTION_SAVE;
	action->handler = type;
	*synth->next_action = action;
	synth->next_action = &action->next;

	save = strdup(".save(");
	if (!save)
		goto error;

	for (i = 0; fields[i]; i++) {
		char *delim = i ? "," : NULL;

		if (!trace_verify_event_field(synth->end_event, fields[i], NULL))
			goto error;
		save = append_string(save, delim, fields[i]);
	}
	save = append_string(save, NULL, ")");
	if (!save)
		goto error;

	action->save = save;
	add_action(synth, action);
	return 0;
 error:
	action_free(action);
	free(save);
	return -1;
	return 0;
}

static int remove_hist(struct tracefs_instance *instance,
		       struct tep_event *event, const char *hist)
{
	char *str;
	int ret;

	ret = asprintf(&str, "!%s", hist);
	if (ret < 0)
		return -1;

	ret = tracefs_event_file_append(instance, event->system, event->name,
				  "trigger", str);
	free(str);
	return ret < 0 ? -1 : 0;
}

static char *create_hist(char **keys, char **vars)
{
	char *hist = strdup("hist:keys=");
	char *name;
	int i;

	if (!hist)
		return NULL;

	for (i = 0; keys[i]; i++) {
		name = keys[i];
		if (i)
			hist = append_string(hist, NULL, ",");
		hist = append_string(hist, NULL, name);
	}

	if (!vars)
		return hist;

	hist = append_string(hist, NULL, ":");

	for (i = 0; vars[i]; i++) {
		name = vars[i];
		if (i)
			hist = append_string(hist, NULL, ",");
		hist = append_string(hist, NULL, name);
	}

	return hist;
}

static char *create_onmatch(char *hist, struct tracefs_synth *synth)
{
	hist = append_string(hist, NULL, ":onmatch(");
	hist = append_string(hist, NULL, synth->start_event->system);
	hist = append_string(hist, NULL, ".");
	hist = append_string(hist, NULL, synth->start_event->name);
	return append_string(hist, NULL, ")");
}

static char *create_trace(char *hist, struct tracefs_synth *synth)
{
	char *name;
	int i;

	if (synth->new_format) {
		hist = append_string(hist, NULL, ".trace(");
		hist = append_string(hist, NULL, synth->name);
		hist = append_string(hist, NULL, ",");
	} else {
		hist = append_string(hist, NULL, ".");
		hist = append_string(hist, NULL, synth->name);
		hist = append_string(hist, NULL, "(");
	}

	for (i = 0; synth->synthetic_args && synth->synthetic_args[i]; i++) {
		name = synth->synthetic_args[i];

		if (i)
			hist = append_string(hist, NULL, ",");
		hist = append_string(hist, NULL, name);
	}

	return append_string(hist, NULL, ")");
}

static char *create_max(char *hist, struct tracefs_synth *synth, char *field)
{
	hist = append_string(hist, NULL, ":onmax(");
	hist = append_string(hist, NULL, field);
	return append_string(hist, NULL, ")");
}

static char *create_change(char *hist, struct tracefs_synth *synth, char *field)
{
	hist = append_string(hist, NULL, ":onchange(");
	hist = append_string(hist, NULL, field);
	return append_string(hist, NULL, ")");
}

static char *create_actions(char *hist, struct tracefs_synth *synth)
{
	struct action *action;

	if (!synth->actions) {
		/* Default is "onmatch" and "trace" */
		hist = create_onmatch(hist, synth);
		return create_trace(hist, synth);
	}

	for (action = synth->actions; action; action = action->next) {
		switch (action->handler) {
		case TRACEFS_SYNTH_HANDLE_MATCH:
			hist = create_onmatch(hist, synth);
			break;
		case TRACEFS_SYNTH_HANDLE_MAX:
			hist = create_max(hist, synth, action->handle_field);
			break;
		case TRACEFS_SYNTH_HANDLE_CHANGE:
			hist = create_change(hist, synth, action->handle_field);
			break;
		default:
			continue;
		}
		switch (action->type) {
		case ACTION_TRACE:
			hist = create_trace(hist, synth);
			break;
		case ACTION_SNAPSHOT:
			hist = append_string(hist, NULL, ".snapshot()");
			break;
		case ACTION_SAVE:
			hist = append_string(hist, NULL, action->save);
			break;
		default:
			continue;
		}
	}
	return hist;
}

static char *create_end_hist(struct tracefs_synth *synth)
{
	char *end_hist;

	end_hist = create_hist(synth->end_keys, synth->end_vars);
	return create_actions(end_hist, synth);
}

/*
 * tracefs_synth_raw_fmt - show the raw format of a synthetic event
 * @seq: A trace_seq to store the format string
 * @synth: The synthetic event to read format from
 *
 * This shows the raw format that describes the synthetic event, including
 * the format of the dynamic event and the start / end histograms.
 *
 * Returns 0 on succes -1 on error.
 */
int tracefs_synth_raw_fmt(struct trace_seq *seq, struct tracefs_synth *synth)
{
	if (!synth->dyn_event)
		return -1;

	trace_seq_printf(seq, "%s", synth->dyn_event->format);
	trace_seq_printf(seq, "\n%s", synth->start_hist);
	trace_seq_printf(seq, "\n%s", synth->end_hist);

	return 0;
}

/*
 * tracefs_synth_show_event - show the dynamic event used by a synth event
 * @synth: The synthetic event to read format from
 *
 * This shows the raw format of the dynamic event used by the synthetic event.
 *
 * Returns format string owned by @synth on success, or NULL on error.
 */
const char *tracefs_synth_show_event(struct tracefs_synth *synth)
{
	return synth->dyn_event ? synth->dyn_event->format : NULL;
}

/*
 * tracefs_synth_show_start_hist - show the start histogram used by a synth event
 * @synth: The synthetic event to read format from
 *
 * This shows the raw format of the start histogram used by the synthetic event.
 *
 * Returns format string owned by @synth on success, or NULL on error.
 */
const char *tracefs_synth_show_start_hist(struct tracefs_synth *synth)
{
	return synth->start_hist;
}

/*
 * tracefs_synth_show_end_hist - show the end histogram used by a synth event
 * @synth: The synthetic event to read format from
 *
 * This shows the raw format of the end histogram used by the synthetic event.
 *
 * Returns format string owned by @synth on success, or NULL on error.
 */
const char *tracefs_synth_show_end_hist(struct tracefs_synth *synth)
{
	return synth->end_hist;
}

static char *append_filter(char *hist, char *filter, unsigned int parens)
{
	int i;

	if (!filter)
		return hist;

	hist = append_string(hist, NULL, " if ");
	hist = append_string(hist, NULL, filter);
	for (i = 0; i < parens; i++)
		hist = append_string(hist, NULL, ")");
	return hist;
}

static int verify_state(struct tracefs_synth *synth)
{
	if (trace_test_state(synth->start_state) < 0 ||
	    trace_test_state(synth->end_state) < 0)
		return -1;
	return 0;
}

/**
 * tracefs_synth_complete - tell if the tracefs_synth is complete or not
 * @synth: The synthetic event to get the start hist from.
 *
 * Retruns true if the synthetic event @synth has both a start and
 * end event (ie. a synthetic event, or just a histogram), and
 * false otherwise.
 */
bool tracefs_synth_complete(struct tracefs_synth *synth)
{
	return synth && synth->start_event && synth->end_event;
}

/**
 * tracefs_synth_get_start_hist - Return the histogram of the start event
 * @synth: The synthetic event to get the start hist from.
 *
 * On success, returns a tracefs_hist descriptor that holds the
 * histogram information of the start_event of the synthetic event
 * structure. Returns NULL on failure.
 */
struct tracefs_hist *
tracefs_synth_get_start_hist(struct tracefs_synth *synth)
{
	struct tracefs_hist *hist = NULL;
	struct tep_handle *tep;
	const char *system;
	const char *event;
	const char *key;
	char **keys;
	int *types;
	int ret;
	int i;

	if (!synth) {
		errno = EINVAL;
		return NULL;
	}

	system = synth->start_event->system;
	event = synth->start_event->name;
	types = synth->start_type;
	keys = synth->start_keys;
	tep = synth->tep;

	if (!keys)
		keys = synth->start_selection;

	if (!keys)
		return NULL;

	for (i = 0; keys[i]; i++) {
		int type = types ? types[i] : 0;

		if (type == HIST_COUNTER_TYPE)
			continue;

		key = keys[i];

		if (i) {
			ret = tracefs_hist_add_key(hist, key, type);
			if (ret < 0) {
				tracefs_hist_free(hist);
				return NULL;
			}
		} else {
			hist = tracefs_hist_alloc(tep, system, event,
						  key, type);
			if (!hist)
				return NULL;
		}
	}

	if (!hist)
		return NULL;

	for (i = 0; keys[i]; i++) {
		int type = types ? types[i] : 0;

		if (type != HIST_COUNTER_TYPE)
			continue;

		key = keys[i];

		ret = tracefs_hist_add_value(hist, key);
		if (ret < 0) {
			tracefs_hist_free(hist);
			return NULL;
		}
	}

	if (synth->start_filter) {
		hist->filter = strdup(synth->start_filter);
		if (!hist->filter) {
			tracefs_hist_free(hist);
			return NULL;
		}
	}

	return hist;
}

/**
 * tracefs_synth_create - creates the synthetic event on the system
 * @synth: The tracefs_synth descriptor
 *
 * This creates the synthetic events.
 *
 * Returns 0 on succes and -1 on error.
 * On error, errno is set to:
 * ENOMEM - memory allocation failure.
 * ENIVAL - a parameter is passed as NULL that should not be or a problem
 *   writing into the system.
 */
int tracefs_synth_create(struct tracefs_synth *synth)
{
	int ret;

	if (!synth) {
		errno = EINVAL;
		return -1;
	}

	if (!synth->name || !synth->end_event) {
		errno = EUNATCH;
		return -1;
	}

	if (verify_state(synth) < 0)
		return -1;

	if (!synth->dyn_event && alloc_synthetic_event(synth))
		return -1;
	if (tracefs_dynevent_create(synth->dyn_event))
		return -1;

	synth->start_hist = create_hist(synth->start_keys, synth->start_vars);
	synth->start_hist = append_filter(synth->start_hist, synth->start_filter,
					  synth->start_parens);
	if (!synth->start_hist)
		goto remove_synthetic;

	synth->end_hist = create_end_hist(synth);
	synth->end_hist = append_filter(synth->end_hist, synth->end_filter,
					synth->end_parens);
	if (!synth->end_hist)
		goto remove_synthetic;

	ret = tracefs_event_file_append(synth->instance, synth->start_event->system,
					synth->start_event->name,
					"trigger", synth->start_hist);
	if (ret < 0)
		goto remove_synthetic;

	ret = tracefs_event_file_append(synth->instance, synth->end_event->system,
					synth->end_event->name,
					"trigger", synth->end_hist);
	if (ret < 0)
		goto remove_start_hist;

	return 0;

 remove_start_hist:
	remove_hist(synth->instance, synth->start_event, synth->start_hist);
 remove_synthetic:
	tracefs_dynevent_destroy(synth->dyn_event, false);
	return -1;
}

/**
 * tracefs_synth_destroy - delete the synthetic event from the system
 * @synth: The tracefs_synth descriptor
 *
 * This will destroy a synthetic event created by tracefs_synth_create()
 * with the same @synth.
 *
 * It will attempt to disable the synthetic event in its instance (top by default),
 * but if other instances have it active, it is likely to fail, which will likely
 * fail on all other parts of tearing down the synthetic event.
 *
 * Returns 0 on succes and -1 on error.
 * On error, errno is set to:
 * ENOMEM - memory allocation failure.
 * ENIVAL - a parameter is passed as NULL that should not be or a problem
 *   writing into the system.
 */
int tracefs_synth_destroy(struct tracefs_synth *synth)
{
	char *hist;
	int ret;

	if (!synth) {
		errno = EINVAL;
		return -1;
	}

	if (!synth->name || !synth->end_event) {
		errno = EUNATCH;
		return -1;
	}

	/* Try to disable the event if possible */
	tracefs_event_disable(synth->instance, "synthetic", synth->name);

	hist = create_end_hist(synth);
	hist = append_filter(hist, synth->end_filter,
			     synth->end_parens);
	if (!hist)
		return -1;
	ret = remove_hist(synth->instance, synth->end_event, hist);
	free(hist);

	hist = create_hist(synth->start_keys, synth->start_vars);
	hist = append_filter(hist, synth->start_filter,
			     synth->start_parens);
	if (!hist)
		return -1;

	ret = remove_hist(synth->instance, synth->start_event, hist);
	free(hist);

	ret = tracefs_dynevent_destroy(synth->dyn_event, true);

	return ret ? -1 : 0;
}

/**
 * tracefs_synth_echo_cmd - show the command lines to create the synthetic event
 * @seq: The trace_seq to store the command lines in
 * @synth: The tracefs_synth descriptor
 *
 * This will list the "echo" commands that are equivalent to what would
 * be executed by the tracefs_synth_create() command.
 *
 * Returns 0 on succes and -1 on error.
 * On error, errno is set to:
 * ENOMEM - memory allocation failure.
 */
int tracefs_synth_echo_cmd(struct trace_seq *seq,
			   struct tracefs_synth *synth)
{
	bool new_event = false;
	char *hist = NULL;
	char *path;
	int ret = -1;

	if (!synth) {
		errno = EINVAL;
		return -1;
	}

	if (!synth->name || !synth->end_event) {
		errno = EUNATCH;
		return -1;
	}

	if (!synth->dyn_event) {
		if (alloc_synthetic_event(synth))
			return -1;
		new_event = true;
	}

	path = trace_find_tracing_dir(false);
	if (!path)
		goto out_free;

	trace_seq_printf(seq, "echo '%s%s%s %s' >> %s/%s\n",
			 synth->dyn_event->prefix,
			 strlen(synth->dyn_event->prefix) ? ":" : "",
			 synth->dyn_event->event,
			 synth->dyn_event->format, path, synth->dyn_event->trace_file);

	tracefs_put_tracing_file(path);
	path = tracefs_instance_get_dir(synth->instance);

	hist = create_hist(synth->start_keys, synth->start_vars);
	hist = append_filter(hist, synth->start_filter,
			     synth->start_parens);
	if (!hist)
		goto out_free;

	trace_seq_printf(seq, "echo '%s' >> %s/events/%s/%s/trigger\n",
			 hist, path, synth->start_event->system,
			 synth->start_event->name);
	free(hist);
	hist = create_end_hist(synth);
	hist = append_filter(hist, synth->end_filter,
			     synth->end_parens);
	if (!hist)
		goto out_free;

	trace_seq_printf(seq, "echo '%s' >> %s/events/%s/%s/trigger\n",
			 hist, path, synth->end_event->system,
			 synth->end_event->name);

	ret = 0;
 out_free:
	free(hist);
	tracefs_put_tracing_file(path);
	if (new_event) {
		tracefs_dynevent_free(synth->dyn_event);
		synth->dyn_event = NULL;
	}
	return ret;
}

/**
 * tracefs_synth_get_event - return tep event representing the given synthetic event
 * @tep: a handle to the trace event parser context that holds the events
 * @synth: a synthetic event context, describing given synthetic event.
 *
 * Returns a pointer to a tep event describing the given synthetic event. The pointer
 * is managed by the @tep handle and must not be freed. In case of an error, or in case
 * the requested synthetic event is missing in the @tep handler - NULL is returned.
 */
struct tep_event *
tracefs_synth_get_event(struct tep_handle *tep, struct tracefs_synth *synth)
{
	if (!tep || !synth || !synth->name)
		return NULL;

	return get_tep_event(tep, SYNTHETIC_GROUP, synth->name);
}