summaryrefslogtreecommitdiffstats
path: root/src/bldprogs/VBoxTpG.cpp
blob: 516b749e90022af58df3a0e221d52a659f8720e8 (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
/* $Id: VBoxTpG.cpp $ */
/** @file
 * VBox Build Tool - VBox Tracepoint Generator.
 */

/*
 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <VBox/VBoxTpG.h>

#include <iprt/alloca.h>
#include <iprt/assert.h>
#include <iprt/ctype.h>
#include <iprt/env.h>
#include <iprt/errcore.h>
#include <iprt/file.h>
#include <iprt/getopt.h>
#include <iprt/initterm.h>
#include <iprt/list.h>
#include <iprt/mem.h>
#include <iprt/message.h>
#include <iprt/path.h>
#include <iprt/process.h>
#include <iprt/stream.h>
#include <iprt/string.h>
#include <iprt/uuid.h>

#include "scmstream.h"


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/
typedef struct VTGPROBE *PVTGPROBE;

typedef struct VTGATTRS
{
    kVTGStability   enmCode;
    kVTGStability   enmData;
    kVTGClass       enmDataDep;
} VTGATTRS;
typedef VTGATTRS *PVTGATTRS;


typedef struct VTGARG
{
    RTLISTNODE      ListEntry;
    /** The argument name. (heap) */
    char           *pszName;
    /** The type presented to the tracer (in string table). */
    const char     *pszTracerType;
    /** The argument type used in the probe method in that context. (heap) */
    char           *pszCtxType;
    /** Argument passing format string.  First and only argument is the name.
     *  (const string) */
    const char     *pszArgPassingFmt;
    /** The type flags. */
    uint32_t        fType;
    /** The argument number (0-based) for complaining/whatever. */
    uint16_t        iArgNo;
    /** The probe the argument belongs to (for complaining/whatever). */
    PVTGPROBE       pProbe;
    /** The absolute source position. */
    size_t          offSrc;
} VTGARG;
typedef VTGARG *PVTGARG;

typedef struct VTGPROBE
{
    RTLISTNODE      ListEntry;
    char           *pszMangledName;
    const char     *pszUnmangledName;
    RTLISTANCHOR    ArgHead;
    uint32_t        cArgs;
    bool            fHaveLargeArgs;
    uint32_t        offArgList;
    uint32_t        iProbe;
    size_t          iLine;
} VTGPROBE;

typedef struct VTGPROVIDER
{
    RTLISTNODE      ListEntry;
    const char     *pszName;

    uint16_t        iFirstProbe;
    uint16_t        cProbes;

    VTGATTRS        AttrSelf;
    VTGATTRS        AttrModules;
    VTGATTRS        AttrFunctions;
    VTGATTRS        AttrName;
    VTGATTRS        AttrArguments;

    RTLISTANCHOR    ProbeHead;
} VTGPROVIDER;
typedef VTGPROVIDER *PVTGPROVIDER;

/**
 * A string table string.
 */
typedef struct VTGSTRING
{
    /** The string space core. */
    RTSTRSPACECORE  Core;
    /** The string table offset. */
    uint32_t        offStrTab;
    /** The actual string. */
    char            szString[1];
} VTGSTRING;
typedef VTGSTRING *PVTGSTRING;


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
/** The string space organizing the string table strings. Each node is a VTGSTRING. */
static RTSTRSPACE       g_StrSpace = NULL;
/** Used by the string table enumerator to set VTGSTRING::offStrTab. */
static uint32_t         g_offStrTab;
/** List of providers created by the parser. */
static RTLISTANCHOR     g_ProviderHead;
/** The number of type errors. */
static uint32_t         g_cTypeErrors = 0;


/** @name Options
 * @{ */
static enum
{
    kVBoxTpGAction_Nothing,
    kVBoxTpGAction_GenerateHeader,
    kVBoxTpGAction_GenerateWrapperHeader,
    kVBoxTpGAction_GenerateObject
}                           g_enmAction                 = kVBoxTpGAction_Nothing;
static uint32_t             g_cBits                     = HC_ARCH_BITS;
static uint32_t             g_cHostBits                 = HC_ARCH_BITS;
static uint32_t             g_fTypeContext              = VTG_TYPE_CTX_R0;
static const char          *g_pszContextDefine          = "IN_RING0";
static const char          *g_pszContextDefine2         = NULL;
static bool                 g_fApplyCpp                 = false;
static uint32_t             g_cVerbosity                = 0;
static const char          *g_pszOutput                 = NULL;
static const char          *g_pszScript                 = NULL;
static const char          *g_pszTempAsm                = NULL;
#ifdef RT_OS_DARWIN
static const char          *g_pszAssembler              = "yasm";
static const char          *g_pszAssemblerFmtOpt        = "-f";
static const char           g_szAssemblerFmtVal32[]     = "macho32";
static const char           g_szAssemblerFmtVal64[]     = "macho64";
static const char           g_szAssemblerOsDef[]        = "RT_OS_DARWIN";
#elif defined(RT_OS_OS2)
static const char          *g_pszAssembler              = "nasm.exe";
static const char          *g_pszAssemblerFmtOpt        = "-f";
static const char           g_szAssemblerFmtVal32[]     = "obj";
static const char           g_szAssemblerFmtVal64[]     = "elf64";
static const char           g_szAssemblerOsDef[]        = "RT_OS_OS2";
#elif defined(RT_OS_WINDOWS)
static const char          *g_pszAssembler              = "yasm.exe";
static const char          *g_pszAssemblerFmtOpt        = "-f";
static const char           g_szAssemblerFmtVal32[]     = "win32";
static const char           g_szAssemblerFmtVal64[]     = "win64";
static const char           g_szAssemblerOsDef[]        = "RT_OS_WINDOWS";
#else
static const char          *g_pszAssembler              = "yasm";
static const char          *g_pszAssemblerFmtOpt        = "-f";
static const char           g_szAssemblerFmtVal32[]     = "elf32";
static const char           g_szAssemblerFmtVal64[]     = "elf64";
# ifdef RT_OS_FREEBSD
static const char           g_szAssemblerOsDef[]        = "RT_OS_FREEBSD";
# elif  defined(RT_OS_NETBSD)
static const char           g_szAssemblerOsDef[]        = "RT_OS_NETBSD";
# elif  defined(RT_OS_OPENBSD)
static const char           g_szAssemblerOsDef[]        = "RT_OS_OPENBSD";
# elif  defined(RT_OS_LINUX)
static const char           g_szAssemblerOsDef[]        = "RT_OS_LINUX";
# elif  defined(RT_OS_SOLARIS)
static const char           g_szAssemblerOsDef[]        = "RT_OS_SOLARIS";
# else
#  error "Port me!"
# endif
#endif
static const char          *g_pszAssemblerFmtVal        = RT_CONCAT(g_szAssemblerFmtVal, HC_ARCH_BITS);
static const char          *g_pszAssemblerDefOpt        = "-D";
static const char          *g_pszAssemblerIncOpt        = "-I";
static char                 g_szAssemblerIncVal[RTPATH_MAX];
static const char          *g_pszAssemblerIncVal        = __FILE__ "/../../../include/";
static const char          *g_pszAssemblerOutputOpt     = "-o";
static unsigned             g_cAssemblerOptions         = 0;
static const char          *g_apszAssemblerOptions[32];
static const char          *g_pszProbeFnName            = "SUPR0TracerFireProbe";
static bool                 g_fProbeFnImported          = true;
static bool                 g_fPic                      = false;
/** @} */




/**
 * Inserts a string into the string table, reusing any matching existing string
 * if possible.
 *
 * @returns Read only string.
 * @param   pch                 The string to insert (need not be terminated).
 * @param   cch                 The length of the string.
 */
static const char *strtabInsertN(const char *pch, size_t cch)
{
    PVTGSTRING pStr = (PVTGSTRING)RTStrSpaceGetN(&g_StrSpace, pch, cch);
    if (pStr)
        return pStr->szString;

    /*
     * Create a new entry.
     */
    pStr = (PVTGSTRING)RTMemAlloc(RT_UOFFSETOF_DYN(VTGSTRING, szString[cch + 1]));
    if (!pStr)
        return NULL;

    pStr->Core.pszString = pStr->szString;
    memcpy(pStr->szString, pch, cch);
    pStr->szString[cch]  = '\0';
    pStr->offStrTab      = UINT32_MAX;

    bool fRc = RTStrSpaceInsert(&g_StrSpace, &pStr->Core);
    Assert(fRc); NOREF(fRc);
    return pStr->szString;
}


/**
 * Retrieves the string table offset of the given string table string.
 *
 * @returns String table offset.
 * @param   pszStrTabString     The string table string.
 */
static uint32_t strtabGetOff(const char *pszStrTabString)
{
    PVTGSTRING pStr = RT_FROM_MEMBER(pszStrTabString, VTGSTRING, szString[0]);
    Assert(pStr->Core.pszString == pszStrTabString);
    return pStr->offStrTab;
}


/**
 * Invokes the assembler.
 *
 * @returns Exit code.
 * @param   pszOutput           The output file.
 * @param   pszTempAsm          The source file.
 */
static RTEXITCODE generateInvokeAssembler(const char *pszOutput, const char *pszTempAsm)
{
    const char     *apszArgs[64];
    unsigned        iArg = 0;

    apszArgs[iArg++] = g_pszAssembler;
    apszArgs[iArg++] = g_pszAssemblerFmtOpt;
    apszArgs[iArg++] = g_pszAssemblerFmtVal;
    apszArgs[iArg++] = g_pszAssemblerDefOpt;
    if (!strcmp(g_pszAssemblerFmtVal, "macho32") || !strcmp(g_pszAssemblerFmtVal, "macho64"))
        apszArgs[iArg++] = "ASM_FORMAT_MACHO";
    else if (!strcmp(g_pszAssemblerFmtVal, "obj") || !strcmp(g_pszAssemblerFmtVal, "omf"))
        apszArgs[iArg++] = "ASM_FORMAT_OMF";
    else if (   !strcmp(g_pszAssemblerFmtVal, "win32")
             || !strcmp(g_pszAssemblerFmtVal, "win64")
             || !strcmp(g_pszAssemblerFmtVal, "pe32")
             || !strcmp(g_pszAssemblerFmtVal, "pe64")
             || !strcmp(g_pszAssemblerFmtVal, "pe") )
        apszArgs[iArg++] = "ASM_FORMAT_PE";
    else if (   !strcmp(g_pszAssemblerFmtVal, "elf32")
             || !strcmp(g_pszAssemblerFmtVal, "elf64")
             || !strcmp(g_pszAssemblerFmtVal, "elf"))
        apszArgs[iArg++] = "ASM_FORMAT_ELF";
    else
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unknown assembler format '%s'", g_pszAssemblerFmtVal);
    apszArgs[iArg++] = g_pszAssemblerDefOpt;
    if (g_cBits == 32)
        apszArgs[iArg++] = "ARCH_BITS=32";
    else
        apszArgs[iArg++] = "ARCH_BITS=64";
    apszArgs[iArg++] = g_pszAssemblerDefOpt;
    if (g_cHostBits == 32)
        apszArgs[iArg++] = "HC_ARCH_BITS=32";
    else
        apszArgs[iArg++] = "HC_ARCH_BITS=64";
    apszArgs[iArg++] = g_pszAssemblerDefOpt;
    if (g_cBits == 32)
        apszArgs[iArg++] = "RT_ARCH_X86";
    else
        apszArgs[iArg++] = "RT_ARCH_AMD64";
    apszArgs[iArg++] = g_pszAssemblerDefOpt;
    apszArgs[iArg++] = g_pszContextDefine;
    if (g_pszContextDefine2)
    {
        apszArgs[iArg++] = g_pszAssemblerDefOpt;
        apszArgs[iArg++] = g_pszContextDefine2;
    }
    if (g_szAssemblerOsDef[0])
    {
        apszArgs[iArg++] = g_pszAssemblerDefOpt;
        apszArgs[iArg++] = g_szAssemblerOsDef;
    }
    apszArgs[iArg++] = g_pszAssemblerIncOpt;
    apszArgs[iArg++] = g_pszAssemblerIncVal;
    apszArgs[iArg++] = g_pszAssemblerOutputOpt;
    apszArgs[iArg++] = pszOutput;
    for (unsigned i = 0; i < g_cAssemblerOptions; i++)
        apszArgs[iArg++] = g_apszAssemblerOptions[i];
    apszArgs[iArg++] = pszTempAsm;
    apszArgs[iArg]   = NULL;
    Assert(iArg <= RT_ELEMENTS(apszArgs));

    if (g_cVerbosity > 1)
    {
        RTMsgInfo("Starting assmbler '%s' with arguments:\n",  g_pszAssembler);
        for (unsigned i = 0; i < iArg; i++)
            RTMsgInfo("  #%02u: '%s'\n",  i, apszArgs[i]);
    }

    RTPROCESS hProc;
    int rc = RTProcCreate(apszArgs[0], apszArgs, RTENV_DEFAULT, RTPROC_FLAGS_SEARCH_PATH, &hProc);
    if (RT_FAILURE(rc))
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to start '%s' (assembler): %Rrc", apszArgs[0], rc);

    RTPROCSTATUS Status;
    rc = RTProcWait(hProc, RTPROCWAIT_FLAGS_BLOCK, &Status);
    if (RT_FAILURE(rc))
    {
        RTProcTerminate(hProc);
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTProcWait failed: %Rrc", rc);
    }
    if (Status.enmReason == RTPROCEXITREASON_SIGNAL)
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "The assembler failed: signal %d", Status.iStatus);
    if (Status.enmReason != RTPROCEXITREASON_NORMAL)
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "The assembler failed: abend");
    if (Status.iStatus != 0)
        return RTMsgErrorExit((RTEXITCODE)Status.iStatus, "The assembler failed: exit code %d", Status.iStatus);

    return RTEXITCODE_SUCCESS;
}


/**
 * Worker that does the boring bits when generating a file.
 *
 * @returns Exit code.
 * @param   pszOutput           The name of the output file.
 * @param   pszWhat             What kind of file it is.
 * @param   pfnGenerator        The callback function that provides the contents
 *                              of the file.
 */
static RTEXITCODE generateFile(const char *pszOutput, const char *pszWhat,
                               RTEXITCODE (*pfnGenerator)(PSCMSTREAM))
{
    SCMSTREAM Strm;
    int rc = ScmStreamInitForWriting(&Strm, NULL);
    if (RT_FAILURE(rc))
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "ScmStreamInitForWriting returned %Rrc when generating the %s file",
                              rc, pszWhat);

    RTEXITCODE rcExit = pfnGenerator(&Strm);
    if (RT_FAILURE(ScmStreamGetStatus(&Strm)))
        rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "Stream error %Rrc generating the %s file",
                                ScmStreamGetStatus(&Strm), pszWhat);
    if (rcExit == RTEXITCODE_SUCCESS)
    {
        rc = ScmStreamWriteToFile(&Strm, "%s", pszOutput);
        if (RT_FAILURE(rc))
            rcExit = RTMsgErrorExit(RTEXITCODE_FAILURE, "ScmStreamWriteToFile returned %Rrc when writing '%s' (%s)",
                                    rc, pszOutput, pszWhat);
        if (rcExit == RTEXITCODE_SUCCESS)
        {
            if (g_cVerbosity > 0)
                RTMsgInfo("Successfully generated '%s'.", pszOutput);
            if (g_cVerbosity > 1)
            {
                RTMsgInfo("================ %s - start ================", pszWhat);
                ScmStreamRewindForReading(&Strm);
                const char *pszLine;
                size_t      cchLine;
                SCMEOL      enmEol;
                while ((pszLine = ScmStreamGetLine(&Strm, &cchLine, &enmEol)) != NULL)
                    RTPrintf("%.*s\n", cchLine, pszLine);
                RTMsgInfo("================ %s - end   ================", pszWhat);
            }
        }
    }
    ScmStreamDelete(&Strm);
    return rcExit;
}


/**
 * @callback_method_impl{FNRTSTRSPACECALLBACK, Writes the string table strings.}
 */
static DECLCALLBACK(int) generateAssemblyStrTabCallback(PRTSTRSPACECORE pStr, void *pvUser)
{
    PVTGSTRING pVtgStr = (PVTGSTRING)pStr;
    PSCMSTREAM pStrm   = (PSCMSTREAM)pvUser;

    pVtgStr->offStrTab = g_offStrTab;
    g_offStrTab += (uint32_t)pVtgStr->Core.cchString + 1;

    ScmStreamPrintf(pStrm,
                    "    db '%s', 0 ; off=%u len=%zu\n",
                    pVtgStr->szString, pVtgStr->offStrTab, pVtgStr->Core.cchString);
    return VINF_SUCCESS;
}


/**
 * Generate assembly source that can be turned into an object file.
 *
 * (This is a generateFile callback.)
 *
 * @returns Exit code.
 * @param   pStrm               The output stream.
 */
static RTEXITCODE generateAssembly(PSCMSTREAM pStrm)
{
    PVTGPROVIDER    pProvider;
    PVTGPROBE       pProbe;
    PVTGARG         pArg;


    if (g_cVerbosity > 0)
        RTMsgInfo("Generating assembly code...");

    /*
     * Write the file header.
     */
    ScmStreamPrintf(pStrm,
                    "; $Id: VBoxTpG.cpp $ \n"
                    ";; @file\n"
                    "; Automatically generated from %s. Do NOT edit!\n"
                    ";\n"
                    "\n"
                    "%%include \"iprt/asmdefs.mac\"\n"
                    "\n"
                    "\n"
                    ";"
                    "; We put all the data in a dedicated section / segment.\n"
                    ";\n"
                    "; In order to find the probe location specifiers, we do the necessary\n"
                    "; trickery here, ASSUMING that this object comes in first in the link\n"
                    "; editing process.\n"
                    ";\n"
                    "%%ifdef ASM_FORMAT_OMF\n"
                    " %%macro VTG_GLOBAL 2\n"
                    "  global NAME(%%1)\n"
                    "  NAME(%%1):\n"
                    " %%endmacro\n"
                    " segment VTG.Obj public CLASS=VTG align=4096 use32\n"
                    "\n"
                    "%%elifdef ASM_FORMAT_MACHO\n"
                    " %%macro VTG_GLOBAL 2\n"
                    "  global NAME(%%1)\n"
                    "  NAME(%%1):\n"
                    " %%endmacro\n"
                    "  %%ifdef IN_RING3\n"
                    "   %%define VTG_NEW_MACHO_LINKER\n"
                    "  %%elif ARCH_BITS == 64\n"
                    "   %%define VTG_NEW_MACHO_LINKER\n"
                    "  %%elifdef IN_RING0_AGNOSTIC\n"
                    "   %%define VTG_NEW_MACHO_LINKER\n"
                    "  %%endif\n"
                    " %%ifdef VTG_NEW_MACHO_LINKER\n"
                    "  ; Section order hack!\n"
                    "  ; With the ld64-97.17 linker there was a problem with it determining the section\n"
                    "  ; order based on symbol references. The references to the start and end of the\n"
                    "  ; __VTGPrLc section forced it in front of __VTGObj, we want __VTGObj first.\n"
                    "  extern section$start$__VTG$__VTGObj\n"
                    "  extern section$end$__VTG$__VTGObj\n"
                    " %%else\n"
                    "  ; Creating 32-bit kext of the type MH_OBJECT. No fancy section end/start symbols handy.\n"
                    "  [section __VTG __VTGObj        align=16]\n"
                    "VTG_GLOBAL g_aVTGObj_LinkerPleaseNoticeMe, data\n"
                    "  [section __VTG __VTGPrLc.Begin align=16]\n"
                    "  dq 0, 0 ; Paranoia, related to the fudge below.\n"
                    "VTG_GLOBAL g_aVTGPrLc, data\n"
                    "  [section __VTG __VTGPrLc align=16]\n"
                    "VTG_GLOBAL g_aVTGPrLc_LinkerPleaseNoticeMe, data\n"
                    "  [section __VTG __VTGPrLc.End   align=16]\n"
                    "VTG_GLOBAL g_aVTGPrLc_End, data\n"
                    "  dq 0, 0 ; Fudge to work around unidentified linker where it would otherwise generate\n"
                    "          ; a fix up of the first dword in __VTGPrLc.Begin despite the fact that it were\n"
                    "          ; an empty section with nothing whatsoever to fix up.\n"
                    " %%endif\n"
                    " [section __VTG __VTGObj]\n"
                    "\n"
                    "%%elifdef ASM_FORMAT_PE\n"
                    " %%macro VTG_GLOBAL 2\n"
                    "  global NAME(%%1)\n"
                    "  NAME(%%1):\n"
                    " %%endmacro\n"
                    " [section VTGPrLc.Begin data align=64]\n"
                    /*"   times 16 db 0xcc\n"*/
                    "VTG_GLOBAL g_aVTGPrLc, data\n"
                    " [section VTGPrLc.Data  data align=4]\n"
                    " [section VTGPrLc.End   data align=4]\n"
                    "VTG_GLOBAL g_aVTGPrLc_End, data\n"
                    /*"   times 16 db 0xcc\n"*/
                    " [section VTGObj   data align=32]\n"
                    "\n"
                    "%%elifdef ASM_FORMAT_ELF\n"
                    " %%macro VTG_GLOBAL 2\n"
                    "  global NAME(%%1):%%2 hidden\n"
                    "  NAME(%%1):\n"
                    " %%endmacro\n"
                    " [section .VTGData progbits alloc noexec write align=4096]\n"
                    " [section .VTGPrLc.Begin progbits alloc noexec write align=32]\n"
                    " dd 0,0,0,0, 0,0,0,0\n"
                    "VTG_GLOBAL g_aVTGPrLc, data\n"
                    " [section .VTGPrLc       progbits alloc noexec write align=1]\n"
                    " [section .VTGPrLc.End   progbits alloc noexec write align=1]\n"
                    "VTG_GLOBAL g_aVTGPrLc_End, data\n"
                    " dd 0,0,0,0, 0,0,0,0\n"
                    " [section .VTGData]\n"
                    "\n"
                    "%%else\n"
                    " %%error \"ASM_FORMAT_XXX is not defined\"\n"
                    "%%endif\n"
                    "\n"
                    "\n"
                    "VTG_GLOBAL g_VTGObjHeader, data\n"
                    "                ;0         1         2         3\n"
                    "                ;012345678901234567890123456789012\n"
                    "    db          'VTG Object Header v1.7', 0, 0\n"
                    "    dd          %u\n"
                    "    dd          NAME(g_acVTGProbeEnabled_End) - NAME(g_VTGObjHeader)\n"
                    "    dd          NAME(g_achVTGStringTable)     - NAME(g_VTGObjHeader)\n"
                    "    dd          NAME(g_achVTGStringTable_End) - NAME(g_achVTGStringTable)\n"
                    "    dd          NAME(g_aVTGArgLists)          - NAME(g_VTGObjHeader)\n"
                    "    dd          NAME(g_aVTGArgLists_End)      - NAME(g_aVTGArgLists)\n"
                    "    dd          NAME(g_aVTGProbes)            - NAME(g_VTGObjHeader)\n"
                    "    dd          NAME(g_aVTGProbes_End)        - NAME(g_aVTGProbes)\n"
                    "    dd          NAME(g_aVTGProviders)         - NAME(g_VTGObjHeader)\n"
                    "    dd          NAME(g_aVTGProviders_End)     - NAME(g_aVTGProviders)\n"
                    "    dd          NAME(g_acVTGProbeEnabled)     - NAME(g_VTGObjHeader)\n"
                    "    dd          NAME(g_acVTGProbeEnabled_End) - NAME(g_acVTGProbeEnabled)\n"
                    "    dd          0\n"
                    "    dd          0\n"
                    "%%ifdef VTG_NEW_MACHO_LINKER\n"
                    " extern section$start$__VTG$__VTGPrLc\n"
                    "    RTCCPTR_DEF section$start$__VTG$__VTGPrLc\n"
                    " %%if ARCH_BITS == 32\n"
                    "    dd          0\n"
                    " %%endif\n"
                    " extern section$end$__VTG$__VTGPrLc\n"
                    "    RTCCPTR_DEF section$end$__VTG$__VTGPrLc\n"
                    " %%if ARCH_BITS == 32\n"
                    "    dd          0\n"
                    " %%endif\n"
                    "%%else\n"
                    "    RTCCPTR_DEF NAME(g_aVTGPrLc)\n"
                    " %%if ARCH_BITS == 32\n"
                    "    dd          0\n"
                    " %%endif\n"
                    "    RTCCPTR_DEF NAME(g_aVTGPrLc_End)\n"
                    " %%if ARCH_BITS == 32\n"
                    "    dd          0\n"
                    " %%endif\n"
                    "%%endif\n"
                    ,
                    g_pszScript, g_cBits);
    RTUUID Uuid;
    int rc = RTUuidCreate(&Uuid);
    if (RT_FAILURE(rc))
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTUuidCreate failed: %Rrc", rc);
    ScmStreamPrintf(pStrm,
                    "    dd 0%08xh, 0%08xh, 0%08xh, 0%08xh\n"
                    "%%ifdef VTG_NEW_MACHO_LINKER\n"
                    "    RTCCPTR_DEF section$start$__VTG$__VTGObj\n"
                    " %%if ARCH_BITS == 32\n"
                    "    dd          0\n"
                    " %%endif\n"
                    "%%else\n"
                    "    dd 0, 0\n"
                    "%%endif\n"
                    "    dd 0, 0\n"
                    , Uuid.au32[0], Uuid.au32[1], Uuid.au32[2], Uuid.au32[3]);

    /*
     * Dump the string table before we start using the strings.
     */
    ScmStreamPrintf(pStrm,
                    "\n"
                    ";\n"
                    "; The string table.\n"
                    ";\n"
                    "VTG_GLOBAL g_achVTGStringTable, data\n");
    g_offStrTab = 0;
    RTStrSpaceEnumerate(&g_StrSpace, generateAssemblyStrTabCallback, pStrm);
    ScmStreamPrintf(pStrm,
                    "VTG_GLOBAL g_achVTGStringTable_End, data\n");

    /*
     * Write out the argument lists before we use them.
     */
    ScmStreamPrintf(pStrm,
                    "\n"
                    ";\n"
                    "; The argument lists.\n"
                    ";\n"
                    "ALIGNDATA(16)\n"
                    "VTG_GLOBAL g_aVTGArgLists, data\n");
    uint32_t off = 0;
    RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
    {
        RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
        {
            if (pProbe->offArgList != UINT32_MAX)
                continue;

            /* Write it. */
            pProbe->offArgList = off;
            ScmStreamPrintf(pStrm,
                            "    ; off=%u\n"
                            "    db        %2u  ; Argument count\n"
                            "    db         %u  ; fHaveLargeArgs\n"
                            "    db      0, 0  ; Reserved\n"
                            , off, pProbe->cArgs, (int)pProbe->fHaveLargeArgs);
            off += 4;
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                ScmStreamPrintf(pStrm,
                                "    dd  %8u  ; type '%s' (name '%s')\n"
                                "    dd 0%08xh ; type flags\n",
                                strtabGetOff(pArg->pszTracerType), pArg->pszTracerType, pArg->pszName,
                                pArg->fType);
                off += 8;
            }

            /* Look for matching argument lists (lazy bird walks the whole list). */
            PVTGPROVIDER pProv2;
            RTListForEach(&g_ProviderHead, pProv2, VTGPROVIDER, ListEntry)
            {
                PVTGPROBE pProbe2;
                RTListForEach(&pProvider->ProbeHead, pProbe2, VTGPROBE, ListEntry)
                {
                    if (pProbe2->offArgList != UINT32_MAX)
                        continue;
                    if (pProbe2->cArgs != pProbe->cArgs)
                        continue;

                    PVTGARG pArg2;
                    pArg  = RTListNodeGetNext(&pProbe->ArgHead, VTGARG, ListEntry);
                    pArg2 = RTListNodeGetNext(&pProbe2->ArgHead, VTGARG, ListEntry);
                    int32_t cArgs = pProbe->cArgs;
                    while (   cArgs-- > 0
                           && pArg2->pszTracerType == pArg->pszTracerType
                           && pArg2->fType         == pArg->fType)
                    {
                        pArg  = RTListNodeGetNext(&pArg->ListEntry, VTGARG, ListEntry);
                        pArg2 = RTListNodeGetNext(&pArg2->ListEntry, VTGARG, ListEntry);
                    }
                    if (cArgs >= 0)
                        continue;
                    pProbe2->offArgList = pProbe->offArgList;
                }
            }
        }
    }
    ScmStreamPrintf(pStrm,
                    "VTG_GLOBAL g_aVTGArgLists_End, data\n");


    /*
     * Probe definitions.
     */
    ScmStreamPrintf(pStrm,
                    "\n"
                    ";\n"
                    "; Prob definitions.\n"
                    ";\n"
                    "ALIGNDATA(16)\n"
                    "VTG_GLOBAL g_aVTGProbes, data\n"
                    "\n");
    uint32_t iProvider = 0;
    uint32_t iProbe = 0;
    RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
    {
        pProvider->iFirstProbe = iProbe;
        RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
        {
            ScmStreamPrintf(pStrm,
                            "VTG_GLOBAL g_VTGProbeData_%s_%s, data ; idx=#%4u\n"
                            "    dd %6u  ; offName\n"
                            "    dd %6u  ; offArgList\n"
                            "    dw (NAME(g_cVTGProbeEnabled_%s_%s) - NAME(g_acVTGProbeEnabled)) / 4 ; idxEnabled\n"
                            "    dw %6u  ; idxProvider\n"
                            "    dd NAME(g_VTGObjHeader) - NAME(g_VTGProbeData_%s_%s) ; offObjHdr\n"
                            ,
                            pProvider->pszName, pProbe->pszMangledName, iProbe,
                            strtabGetOff(pProbe->pszUnmangledName),
                            pProbe->offArgList,
                            pProvider->pszName, pProbe->pszMangledName,
                            iProvider,
                            pProvider->pszName, pProbe->pszMangledName
                            );
            pProbe->iProbe = iProbe;
            iProbe++;
        }
        pProvider->cProbes = iProbe - pProvider->iFirstProbe;
        iProvider++;
    }
    ScmStreamPrintf(pStrm, "VTG_GLOBAL g_aVTGProbes_End, data\n");

    /*
     * The provider data.
     */
    ScmStreamPrintf(pStrm,
                    "\n"
                    ";\n"
                    "; Provider data.\n"
                    ";\n"
                    "ALIGNDATA(16)\n"
                    "VTG_GLOBAL g_aVTGProviders, data\n");
    iProvider = 0;
    RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
    {
        ScmStreamPrintf(pStrm,
                        "    ; idx=#%4u - %s\n"
                        "    dd %6u  ; name\n"
                        "    dw %6u  ; index of first probe\n"
                        "    dw %6u  ; count of probes\n"
                        "    db %d, %d, %d ; AttrSelf\n"
                        "    db %d, %d, %d ; AttrModules\n"
                        "    db %d, %d, %d ; AttrFunctions\n"
                        "    db %d, %d, %d ; AttrName\n"
                        "    db %d, %d, %d ; AttrArguments\n"
                        "    db 0       ; reserved\n"
                        "VTG_GLOBAL g_cVTGProviderProbesEnabled_%s, data\n"
                        "    dd 0\n"
                        "VTG_GLOBAL g_cVTGProviderSettingsSeqNo_%s, data\n"
                        "    dd 0\n"
                        ,
                        iProvider, pProvider->pszName,
                        strtabGetOff(pProvider->pszName),
                        pProvider->iFirstProbe,
                        pProvider->cProbes,
                        pProvider->AttrSelf.enmCode,        pProvider->AttrSelf.enmData,        pProvider->AttrSelf.enmDataDep,
                        pProvider->AttrModules.enmCode,     pProvider->AttrModules.enmData,     pProvider->AttrModules.enmDataDep,
                        pProvider->AttrFunctions.enmCode,   pProvider->AttrFunctions.enmData,   pProvider->AttrFunctions.enmDataDep,
                        pProvider->AttrName.enmCode,        pProvider->AttrName.enmData,        pProvider->AttrName.enmDataDep,
                        pProvider->AttrArguments.enmCode,   pProvider->AttrArguments.enmData,   pProvider->AttrArguments.enmDataDep,
                        pProvider->pszName,
                        pProvider->pszName);
        iProvider++;
    }
    ScmStreamPrintf(pStrm, "VTG_GLOBAL g_aVTGProviders_End, data\n");

    /*
     * Declare the probe enable flags.
     *
     * These must be placed at the end so they'll end up adjacent to the probe
     * locations.  This is important for reducing the amount of memory we need
     * to lock down for user mode modules.
     */
    ScmStreamPrintf(pStrm,
                    ";\n"
                    "; Probe enabled flags.\n"
                    ";\n"
                    "ALIGNDATA(16)\n"
                    "VTG_GLOBAL g_acVTGProbeEnabled, data\n"
                    );
    uint32_t        cProbes = 0;
    RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
    {
        RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
        {
            ScmStreamPrintf(pStrm,
                            "VTG_GLOBAL g_cVTGProbeEnabled_%s_%s, data\n"
                            "    dd 0\n",
                            pProvider->pszName, pProbe->pszMangledName);
            cProbes++;
        }
    }
    ScmStreamPrintf(pStrm, "VTG_GLOBAL g_acVTGProbeEnabled_End, data\n");
    if (cProbes >= _32K)
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Too many probes: %u (max %u)", cProbes, _32K - 1);


    /*
     * Emit code for the stub functions.
     */
    bool const fWin64   = g_cBits == 64 && (!strcmp(g_pszAssemblerFmtVal, "win64") || !strcmp(g_pszAssemblerFmtVal, "pe64"));
    bool const fElf     = !strcmp(g_pszAssemblerFmtVal, "elf32") || !strcmp(g_pszAssemblerFmtVal, "elf64");
    ScmStreamPrintf(pStrm,
                    "\n"
                    ";\n"
                    "; Prob stubs.\n"
                    ";\n"
                    "BEGINCODE\n"
                    );
    if (g_fProbeFnImported)
        ScmStreamPrintf(pStrm,
                        "EXTERN_IMP2 %s\n"
                        "BEGINCODE ; EXTERN_IMP2 changes section\n",
                        g_pszProbeFnName);
    else
        ScmStreamPrintf(pStrm, "extern NAME(%s)\n", g_pszProbeFnName);

    RTListForEach(&g_ProviderHead, pProvider, VTGPROVIDER, ListEntry)
    {
        RTListForEach(&pProvider->ProbeHead, pProbe, VTGPROBE, ListEntry)
        {
            ScmStreamPrintf(pStrm,
                            "\n"
                            "VTG_GLOBAL VTGProbeStub_%s_%s, function; (VBOXTPGPROBELOC pVTGProbeLoc",
                            pProvider->pszName, pProbe->pszMangledName);
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                ScmStreamPrintf(pStrm, ", %s %s", pArg->pszTracerType, pArg->pszName);
            }
            ScmStreamPrintf(pStrm,
                            ");\n");

            /*
             * Check if the probe in question is enabled.
             */
            if (g_cBits == 32)
                ScmStreamPrintf(pStrm,
                                "        mov     eax, [esp + 4]\n"
                                "        test    byte [eax+3], 0x80 ; fEnabled == true?\n"
                                "        jz      .return            ; jump on false\n");
            else if (fWin64)
                ScmStreamPrintf(pStrm,
                                "        test    byte [rcx+3], 0x80 ; fEnabled == true?\n"
                                "        jz      .return            ; jump on false\n");
            else
                ScmStreamPrintf(pStrm,
                                "        test    byte [rdi+3], 0x80 ; fEnabled == true?\n"
                                "        jz      .return            ; jump on false\n");

            /*
             * Jump to the fire-probe function.
             */
            if (g_cBits == 32)
                ScmStreamPrintf(pStrm, g_fPic && fElf ?
                                "        jmp     %s wrt ..plt\n"
                                : g_fProbeFnImported ?
                                "        mov     ecx, IMP2(%s)\n"
                                "        jmp     ecx\n"
                                :
                                "        jmp     NAME(%s)\n"
                                , g_pszProbeFnName);
            else
                ScmStreamPrintf(pStrm, g_fPic && fElf ?
                                "        jmp     [rel %s wrt ..got]\n"
                                : g_fProbeFnImported ?
                                "        jmp     IMP2(%s)\n"
                                :
                                "        jmp     NAME(%s)\n"
                                , g_pszProbeFnName);

            ScmStreamPrintf(pStrm,
                            ".return:\n"
                            "        ret                        ; The probe was disabled, return\n"
                            "\n");
        }
    }

    return RTEXITCODE_SUCCESS;
}


static RTEXITCODE generateObject(const char *pszOutput, const char *pszTempAsm)
{
    if (!pszTempAsm)
    {
        size_t cch = strlen(pszOutput);
        char  *psz = (char *)alloca(cch + sizeof(".asm"));
        memcpy(psz, pszOutput, cch);
        memcpy(psz + cch, ".asm", sizeof(".asm"));
        pszTempAsm = psz;
    }

    RTEXITCODE rcExit = generateFile(pszTempAsm, "assembly", generateAssembly);
    if (rcExit == RTEXITCODE_SUCCESS)
        rcExit = generateInvokeAssembler(pszOutput, pszTempAsm);
    RTFileDelete(pszTempAsm);
    return rcExit;
}


static RTEXITCODE generateProbeDefineName(char *pszBuf, size_t cbBuf, const char *pszProvider, const char *pszProbe)
{
    size_t cbMax = strlen(pszProvider) + 1 + strlen(pszProbe) + 1;
    if (cbMax > cbBuf || cbMax > 80)
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Probe '%s' in provider '%s' ends up with a too long defined\n", pszProbe, pszProvider);

    while (*pszProvider)
        *pszBuf++ = RT_C_TO_UPPER(*pszProvider++);

    *pszBuf++ = '_';

    while (*pszProbe)
    {
        if (pszProbe[0] == '_' && pszProbe[1] == '_')
            pszProbe++;
        *pszBuf++ = RT_C_TO_UPPER(*pszProbe++);
    }

    *pszBuf = '\0';
    return RTEXITCODE_SUCCESS;
}


static RTEXITCODE generateProviderDefineName(char *pszBuf, size_t cbBuf, const char *pszProvider)
{
    size_t cbMax = strlen(pszProvider) + 1;
    if (cbMax > cbBuf || cbMax > 80)
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Provider '%s' ends up with a too long defined\n", pszProvider);

    while (*pszProvider)
        *pszBuf++ = RT_C_TO_UPPER(*pszProvider++);

    *pszBuf = '\0';
    return RTEXITCODE_SUCCESS;
}


/**
 * Called via generateFile to generate the header file.
 *
 * @returns Exit code status.
 * @param   pStrm               The output stream.
 */
static RTEXITCODE generateHeader(PSCMSTREAM pStrm)
{
    /*
     * Calc the double inclusion blocker define and then write the file header.
     */
    char szTmp[4096];
    const char *pszName = RTPathFilename(g_pszScript);
    size_t      cchName = strlen(pszName);
    if (cchName >= sizeof(szTmp) - 64)
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "File name is too long '%s'", pszName);
    szTmp[0] = '_';
    szTmp[1] = '_';
    szTmp[2] = '_';
    memcpy(&szTmp[3], pszName, cchName);
    szTmp[3 + cchName + 0] = '_';
    szTmp[3 + cchName + 1] = '_';
    szTmp[3 + cchName + 2] = '_';
    szTmp[3 + cchName + 3] = '\0';
    char *psz = &szTmp[3];
    while (*psz)
    {
        if (!RT_C_IS_ALNUM(*psz) && *psz != '_')
            *psz = '_';
        psz++;
    }

    ScmStreamPrintf(pStrm,
                    "/* $Id: VBoxTpG.cpp $ */\n"
                    "/** @file\n"
                    " * Automatically generated from %s.  Do NOT edit!\n"
                    " */\n"
                    "\n"
                    "#ifndef %s\n"
                    "#define %s\n"
                    "#ifndef RT_WITHOUT_PRAGMA_ONCE\n"
                    "# pragma once\n"
                    "#endif\n"
                    "\n"
                    "#include <VBox/VBoxTpG.h>\n"
                    "\n"
                    "#ifndef %s\n"
                    "# error \"Expected '%s' to be defined\"\n"
                    "#endif\n"
                    "\n"
                    "RT_C_DECLS_BEGIN\n"
                    "\n"
                    "#ifdef VBOX_WITH_DTRACE\n"
                    "\n"
                    "# ifdef _MSC_VER\n"
                    "#  pragma data_seg(VTG_LOC_SECT)\n"
                    "#  pragma data_seg()\n"
                    "# endif\n"
                    "\n"
                    ,
                    g_pszScript,
                    szTmp,
                    szTmp,
                    g_pszContextDefine,
                    g_pszContextDefine);

    /*
     * Declare data, code and macros for each probe.
     */
    PVTGPROVIDER pProv;
    PVTGPROBE    pProbe;
    PVTGARG      pArg;
    RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
    {
        /* This macro is not available in ring-3 because we don't have
           anything similar available for native dtrace. */
        ScmStreamPrintf(pStrm, "\n\n");
        if (g_fTypeContext != VTG_TYPE_CTX_R3)
        {
            generateProviderDefineName(szTmp, sizeof(szTmp), pProv->pszName);
            ScmStreamPrintf(pStrm,
                            "extern uint32_t const volatile g_cVTGProviderProbesEnabled_%s;\n"
                            "# define %s_ANY_PROBES_ENABLED() \\\n"
                            "    (RT_UNLIKELY(g_cVTGProviderProbesEnabled_%s != 0))\n"
                            "extern uint32_t const volatile g_cVTGProviderSettingsSeqNo_%s;\n"
                            "# define %s_GET_SETTINGS_SEQ_NO() (g_cVTGProviderSettingsSeqNo_%s)\n"
                            "\n",
                            pProv->pszName,
                            szTmp, pProv->pszName,
                            pProv->pszName,
                            szTmp, pProv->pszName);
        }

        RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
        {
            ScmStreamPrintf(pStrm,
                            "extern uint32_t const volatile g_cVTGProbeEnabled_%s_%s;\n"
                            "extern VTGDESCPROBE            g_VTGProbeData_%s_%s;\n"
                            "DECLASM(void)                  VTGProbeStub_%s_%s(PVTGPROBELOC",
                            pProv->pszName, pProbe->pszMangledName,
                            pProv->pszName, pProbe->pszMangledName,
                            pProv->pszName, pProbe->pszMangledName);
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                ScmStreamPrintf(pStrm, ", %s", pArg->pszCtxType);
            }
            generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
            ScmStreamPrintf(pStrm,
                            ");\n"
                            "# define %s_ENABLED() (RT_UNLIKELY(g_cVTGProbeEnabled_%s_%s != 0))\n"
                            "# define %s_ENABLED_RAW() (g_cVTGProbeEnabled_%s_%s)\n"
                            "# define %s("
                            ,
                            szTmp, pProv->pszName, pProbe->pszMangledName,
                            szTmp, pProv->pszName, pProbe->pszMangledName,
                            szTmp);
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
                    ScmStreamPrintf(pStrm, "%s", pArg->pszName);
                else
                    ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
            }
            ScmStreamPrintf(pStrm,
                            ") \\\n"
                            "    do { \\\n"
                            "        if (RT_UNLIKELY(g_cVTGProbeEnabled_%s_%s)) \\\n"
                            "        { \\\n"
                            "            VTG_DECL_VTGPROBELOC(s_VTGProbeLoc) = \\\n"
                            "            { __LINE__, 0, 0, __FUNCTION__, &g_VTGProbeData_%s_%s }; \\\n"
                            "            VTGProbeStub_%s_%s(&s_VTGProbeLoc",
                            pProv->pszName, pProbe->pszMangledName,
                            pProv->pszName, pProbe->pszMangledName,
                            pProv->pszName, pProbe->pszMangledName);
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                ScmStreamPrintf(pStrm, pArg->pszArgPassingFmt, pArg->pszName);
            }
            ScmStreamPrintf(pStrm,
                            "); \\\n"
                            "        } \\\n"
                            "        { \\\n" );
            uint32_t iArg = 0;
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                if ((pArg->fType & (VTG_TYPE_FIXED_SIZED | VTG_TYPE_AUTO_CONV_PTR)) == VTG_TYPE_FIXED_SIZED)
                    ScmStreamPrintf(pStrm,
                                    "        AssertCompile(sizeof(%s) == %u); \\\n"
                                    "        AssertCompile(sizeof(%s) <= %u); \\\n",
                                    pArg->pszTracerType, pArg->fType & VTG_TYPE_SIZE_MASK,
                                    pArg->pszName, pArg->fType & VTG_TYPE_SIZE_MASK);
                else if (pArg->fType & (VTG_TYPE_POINTER | VTG_TYPE_HC_ARCH_SIZED))
                    ScmStreamPrintf(pStrm,
                                    "        AssertCompile(sizeof(%s) <= sizeof(uintptr_t)); \\\n"
                                    "        AssertCompile(sizeof(%s) <= sizeof(uintptr_t)); \\\n",
                                    pArg->pszName,
                                    pArg->pszTracerType);
                iArg++;
            }
            ScmStreamPrintf(pStrm,
                            "        } \\\n"
                            "    } while (0)\n"
                            "\n");
        }
    }

    ScmStreamPrintf(pStrm,
                    "\n"
                    "#else\n"
                    "\n");
    RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
    {
        if (g_fTypeContext != VTG_TYPE_CTX_R3)
        {
            generateProviderDefineName(szTmp, sizeof(szTmp), pProv->pszName);
            ScmStreamPrintf(pStrm,
                            "# define %s_ANY_PROBES_ENABLED() (false)\n"
                            "# define %s_GET_SETTINGS_SEQ_NO() UINT32_C(0)\n"
                            "\n",
                            szTmp, szTmp);
        }

        RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
        {
            generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
            ScmStreamPrintf(pStrm,
                            "# define %s_ENABLED() (false)\n"
                            "# define %s_ENABLED_RAW() UINT32_C(0)\n"
                            "# define %s("
                            , szTmp, szTmp, szTmp);
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
                    ScmStreamPrintf(pStrm, "%s", pArg->pszName);
                else
                    ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
            }
            ScmStreamPrintf(pStrm,
                            ") do { } while (0)\n");
        }
    }

    ScmStreamWrite(pStrm, RT_STR_TUPLE("\n"
                                       "#endif\n"
                                       "\n"
                                       "RT_C_DECLS_END\n"
                                       "#endif\n"));
    return RTEXITCODE_SUCCESS;
}


/**
 * Called via generateFile to generate the wrapper header file.
 *
 * @returns Exit code status.
 * @param   pStrm               The output stream.
 */
static RTEXITCODE generateWrapperHeader(PSCMSTREAM pStrm)
{
    /*
     * Calc the double inclusion blocker define and then write the file header.
     */
    char szTmp[4096];
    const char *pszName = RTPathFilename(g_pszScript);
    size_t      cchName = strlen(pszName);
    if (cchName >= sizeof(szTmp) - 64)
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "File name is too long '%s'", pszName);
    szTmp[0] = '_';
    szTmp[1] = '_';
    szTmp[2] = '_';
    memcpy(&szTmp[3], pszName, cchName);
    strcpy(&szTmp[3 + cchName ], "___WRAPPER___");
    char *psz = &szTmp[3];
    while (*psz)
    {
        if (!RT_C_IS_ALNUM(*psz) && *psz != '_')
            *psz = '_';
        psz++;
    }

    ScmStreamPrintf(pStrm,
                    "/* $Id: VBoxTpG.cpp $ */\n"
                    "/** @file\n"
                    " * Automatically generated from %s.  Do NOT edit!\n"
                    " */\n"
                    "\n"
                    "#ifndef %s\n"
                    "#define %s\n"
                    "\n"
                    "#include <VBox/VBoxTpG.h>\n"
                    "\n"
                    "#ifndef %s\n"
                    "# error \"Expected '%s' to be defined\"\n"
                    "#endif\n"
                    "\n"
                    "#ifdef VBOX_WITH_DTRACE\n"
                    "\n"
                    ,
                    g_pszScript,
                    szTmp,
                    szTmp,
                    g_pszContextDefine,
                    g_pszContextDefine);

    /*
     * Declare macros for each probe.
     */
    PVTGPROVIDER pProv;
    PVTGPROBE    pProbe;
    PVTGARG      pArg;
    RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
    {
        RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
        {
            generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
            ScmStreamPrintf(pStrm,
                            "# define %s("
                            , szTmp);
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
                    ScmStreamPrintf(pStrm, "%s", pArg->pszName);
                else
                    ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
            }
            ScmStreamPrintf(pStrm,
                            ") \\\n"
                            "    do { \\\n"
                            "        if (RT_UNLIKELY(%s_ENABLED())) \\\n"
                            "        { \\\n"
                            "            %s_ORIGINAL("
                            , szTmp, szTmp);
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                const char *pszFmt = pArg->pszArgPassingFmt;
                if (pArg->fType & VTG_TYPE_AUTO_CONV_PTR)
                {
                    /* Casting is required.  ASSUMES sizeof(RTR0PTR) == sizeof(RTR3PTR) - safe! */
                    pszFmt += sizeof(", ") - 1;
                    if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
                        ScmStreamPrintf(pStrm, "(%s)%M", pArg->pszTracerType, pszFmt, pArg->pszName);
                    else
                        ScmStreamPrintf(pStrm, ", (%s)%M", pArg->pszTracerType, pszFmt, pArg->pszName);
                }
                else if (pArg->fType & VTG_TYPE_CONST_CHAR_PTR)
                {
                    /* Casting from 'const char *' (probe) to 'char *' (dtrace) is required to shut up warnings. */
                    pszFmt += sizeof(", ") - 1;
                    if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
                        ScmStreamPrintf(pStrm, "(char *)%M", pszFmt, pArg->pszName);
                    else
                        ScmStreamPrintf(pStrm, ", (char *)%M", pszFmt, pArg->pszName);
                }
                else
                {
                    if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
                        ScmStreamPrintf(pStrm, pArg->pszArgPassingFmt + sizeof(", ") - 1, pArg->pszName);
                    else
                        ScmStreamPrintf(pStrm, pArg->pszArgPassingFmt, pArg->pszName);
                }
            }
            ScmStreamPrintf(pStrm,
                            "); \\\n"
                            "        } \\\n"
                            "    } while (0)\n"
                            "\n");
        }
    }

    ScmStreamPrintf(pStrm,
                    "\n"
                    "#else\n"
                    "\n");
    RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
    {
        RTListForEach(&pProv->ProbeHead, pProbe, VTGPROBE, ListEntry)
        {
            generateProbeDefineName(szTmp, sizeof(szTmp), pProv->pszName, pProbe->pszMangledName);
            ScmStreamPrintf(pStrm,
                            "# define %s("
                            , szTmp);
            RTListForEach(&pProbe->ArgHead, pArg, VTGARG, ListEntry)
            {
                if (RTListNodeIsFirst(&pProbe->ArgHead, &pArg->ListEntry))
                    ScmStreamPrintf(pStrm, "%s", pArg->pszName);
                else
                    ScmStreamPrintf(pStrm, ", %s", pArg->pszName);
            }
            ScmStreamPrintf(pStrm,
                            ") do { } while (0)\n");
        }
    }

    ScmStreamWrite(pStrm, RT_STR_TUPLE("\n"
                                       "#endif\n"
                                       "\n"
                                       "#endif\n"));
    return RTEXITCODE_SUCCESS;
}


/**
 * Parser error with line and position.
 *
 * @returns RTEXITCODE_FAILURE.
 * @param   pStrm       The stream.
 * @param   fAbs        Absolute or relative offset.
 * @param   offSeek     When @a fAbs is @c false, the offset from the current
 *                      position to the point of failure.  When @a fAbs is @c
 *                      true, it's the absolute position.
 * @param   pszFormat   The message format string.
 * @param   va          Format arguments.
 */
static RTEXITCODE parseErrorExV(PSCMSTREAM pStrm, bool fAbs, size_t offSeek, const char *pszFormat, va_list va)
{
    if (fAbs)
        ScmStreamSeekAbsolute(pStrm, offSeek);
    else if (offSeek != 0)
        ScmStreamSeekRelative(pStrm, -(ssize_t)offSeek);
    size_t const off     = ScmStreamTell(pStrm);
    size_t const iLine   = ScmStreamTellLine(pStrm);
    ScmStreamSeekByLine(pStrm, iLine);
    size_t const offLine = ScmStreamTell(pStrm);

    va_list va2;
    va_copy(va2, va);
    RTPrintf("%s:%d:%zd: error: %N.\n", g_pszScript, iLine + 1, off - offLine + 1, pszFormat, &va2);
    va_end(va2);

    size_t cchLine;
    SCMEOL enmEof;
    const char *pszLine = ScmStreamGetLineByNo(pStrm, iLine, &cchLine, &enmEof);
    if (pszLine)
        RTPrintf("  %.*s\n"
                 "  %*s^\n",
                 cchLine, pszLine, off - offLine, "");
    return RTEXITCODE_FAILURE;
}


/**
 * Parser error with line and position.
 *
 * @returns RTEXITCODE_FAILURE.
 * @param   pStrm       The stream.
 * @param   off         The offset from the current position to the point of
 *                      failure.
 * @param   pszFormat   The message format string.
 * @param   ...         Format arguments.
 */
static RTEXITCODE parseError(PSCMSTREAM pStrm, size_t off, const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    RTEXITCODE rcExit = parseErrorExV(pStrm, false, off, pszFormat, va);
    va_end(va);
    return rcExit;
}


/**
 * Parser error with line and position, absolute version.
 *
 * @returns RTEXITCODE_FAILURE.
 * @param   pStrm       The stream.
 * @param   off         The offset from the current position to the point of
 *                      failure.
 * @param   pszFormat   The message format string.
 * @param   ...         Format arguments.
 */
static RTEXITCODE parseErrorAbs(PSCMSTREAM pStrm, size_t off, const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    RTEXITCODE rcExit = parseErrorExV(pStrm, true /*fAbs*/, off, pszFormat, va);
    va_end(va);
    return rcExit;
}


/**
 * Parser warning with line and position.
 *
 * @param   pStrm       The stream.
 * @param   fAbs        Absolute or relative offset.
 * @param   offSeek     When @a fAbs is @c false, the offset from the current
 *                      position to the point of failure.  When @a fAbs is @c
 *                      true, it's the absolute position.
 * @param   pszFormat   The message format string.
 * @param   va          Format arguments.
 */
static void parseWarnExV(PSCMSTREAM pStrm, bool fAbs, size_t offSeek, const char *pszFormat, va_list va)
{
    /* Save the stream position. */
    size_t const offOrg = ScmStreamTell(pStrm);

    if (fAbs)
        ScmStreamSeekAbsolute(pStrm, offSeek);
    else if (offSeek != 0)
        ScmStreamSeekRelative(pStrm, -(ssize_t)offSeek);
    size_t const off     = ScmStreamTell(pStrm);
    size_t const iLine   = ScmStreamTellLine(pStrm);
    ScmStreamSeekByLine(pStrm, iLine);
    size_t const offLine = ScmStreamTell(pStrm);

    va_list va2;
    va_copy(va2, va);
    RTPrintf("%s:%d:%zd: warning: %N.\n", g_pszScript, iLine + 1, off - offLine + 1, pszFormat, &va2);
    va_end(va2);

    size_t cchLine;
    SCMEOL enmEof;
    const char *pszLine = ScmStreamGetLineByNo(pStrm, iLine, &cchLine, &enmEof);
    if (pszLine)
        RTPrintf("  %.*s\n"
                 "  %*s^\n",
                 cchLine, pszLine, off - offLine, "");

    /* restore the position. */
    int rc = ScmStreamSeekAbsolute(pStrm, offOrg);
    AssertRC(rc);
}

#if 0 /* unused */
/**
 * Parser warning with line and position.
 *
 * @param   pStrm       The stream.
 * @param   off         The offset from the current position to the point of
 *                      failure.
 * @param   pszFormat   The message format string.
 * @param   ...         Format arguments.
 */
static void parseWarn(PSCMSTREAM pStrm, size_t off, const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    parseWarnExV(pStrm, false, off, pszFormat, va);
    va_end(va);
}
#endif /* unused */

/**
 * Parser warning with line and position, absolute version.
 *
 * @param   pStrm       The stream.
 * @param   off         The offset from the current position to the point of
 *                      failure.
 * @param   pszFormat   The message format string.
 * @param   ...         Format arguments.
 */
static void parseWarnAbs(PSCMSTREAM pStrm, size_t off, const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    parseWarnExV(pStrm, true /*fAbs*/, off, pszFormat, va);
    va_end(va);
}


/**
 * Handles a C++ one line comment.
 *
 * @returns Exit code.
 * @param   pStrm               The stream.
 */
static RTEXITCODE parseOneLineComment(PSCMSTREAM pStrm)
{
    ScmStreamSeekByLine(pStrm, ScmStreamTellLine(pStrm) + 1);
    return RTEXITCODE_SUCCESS;
}


/**
 * Handles a multi-line C/C++ comment.
 *
 * @returns Exit code.
 * @param   pStrm               The stream.
 */
static RTEXITCODE parseMultiLineComment(PSCMSTREAM pStrm)
{
    unsigned ch;
    while ((ch = ScmStreamGetCh(pStrm)) != ~(unsigned)0)
    {
        if (ch == '*')
        {
            do
                ch = ScmStreamGetCh(pStrm);
            while (ch == '*');
            if (ch == '/')
                return RTEXITCODE_SUCCESS;
        }
    }

    parseError(pStrm, 1, "Expected end of comment, got end of file");
    return RTEXITCODE_FAILURE;
}


/**
 * Skips spaces and comments.
 *
 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
 * @param   pStrm               The stream..
 */
static RTEXITCODE parseSkipSpacesAndComments(PSCMSTREAM pStrm)
{
    unsigned ch;
    while ((ch = ScmStreamPeekCh(pStrm)) != ~(unsigned)0)
    {
        if (!RT_C_IS_SPACE(ch) && ch != '/')
            return RTEXITCODE_SUCCESS;
        unsigned ch2 = ScmStreamGetCh(pStrm); AssertBreak(ch == ch2); NOREF(ch2);
        if (ch == '/')
        {
            ch = ScmStreamGetCh(pStrm);
            RTEXITCODE rcExit;
            if (ch == '*')
                rcExit = parseMultiLineComment(pStrm);
            else if (ch == '/')
                rcExit = parseOneLineComment(pStrm);
            else
                rcExit = parseError(pStrm, 2, "Unexpected character");
            if (rcExit != RTEXITCODE_SUCCESS)
                return rcExit;
        }
    }

    return parseError(pStrm, 0, "Unexpected end of file");
}


/**
 * Skips spaces and comments, returning the next character.
 *
 * @returns Next non-space-non-comment character. ~(unsigned)0 on EOF or
 *          failure.
 * @param   pStrm               The stream.
 */
static unsigned parseGetNextNonSpaceNonCommentCh(PSCMSTREAM pStrm)
{
    unsigned ch;
    while ((ch = ScmStreamGetCh(pStrm)) != ~(unsigned)0)
    {
        if (!RT_C_IS_SPACE(ch) && ch != '/')
            return ch;
        if (ch == '/')
        {
            ch = ScmStreamGetCh(pStrm);
            RTEXITCODE rcExit;
            if (ch == '*')
                rcExit = parseMultiLineComment(pStrm);
            else if (ch == '/')
                rcExit = parseOneLineComment(pStrm);
            else
                rcExit = parseError(pStrm, 2, "Unexpected character");
            if (rcExit != RTEXITCODE_SUCCESS)
                return ~(unsigned)0;
        }
    }

    parseError(pStrm, 0, "Unexpected end of file");
    return ~(unsigned)0;
}


/**
 * Get the next non-space-non-comment character on a preprocessor line.
 *
 * @returns The next character. On error message and ~(unsigned)0.
 * @param   pStrm               The stream.
 */
static unsigned parseGetNextNonSpaceNonCommentChOnPpLine(PSCMSTREAM pStrm)
{
    size_t   off = ScmStreamTell(pStrm) - 1;
    unsigned ch;
    while ((ch = ScmStreamGetCh(pStrm)) != ~(unsigned)0)
    {
        if (RT_C_IS_SPACE(ch))
        {
            if (ch == '\n' || ch == '\r')
            {
                parseErrorAbs(pStrm, off, "Invalid preprocessor statement");
                break;
            }
        }
        else if (ch == '\\')
        {
            size_t off2 = ScmStreamTell(pStrm) - 1;
            ch = ScmStreamGetCh(pStrm);
            if (ch == '\r')
                ch = ScmStreamGetCh(pStrm);
            if (ch != '\n')
            {
                parseErrorAbs(pStrm, off2, "Expected new line");
                break;
            }
        }
        else
            return ch;
    }
    return ~(unsigned)0;
}



/**
 * Skips spaces and comments.
 *
 * @returns Same as ScmStreamCGetWord
 * @param   pStrm               The stream..
 * @param   pcchWord            Where to return the length.
 */
static const char *parseGetNextCWord(PSCMSTREAM pStrm, size_t *pcchWord)
{
    if (parseSkipSpacesAndComments(pStrm) != RTEXITCODE_SUCCESS)
        return NULL;
    return ScmStreamCGetWord(pStrm, pcchWord);
}



/**
 * Parses interface stability.
 *
 * @returns Interface stability if parsed correctly, otherwise error message and
 *          kVTGStability_Invalid.
 * @param   pStrm               The stream.
 * @param   ch                  The first character in the stability spec.
 */
static kVTGStability parseStability(PSCMSTREAM pStrm, unsigned ch)
{
    switch (ch)
    {
        case 'E':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("External")))
                return kVTGStability_External;
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Evolving")))
                return kVTGStability_Evolving;
            break;
        case 'I':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Internal")))
                return kVTGStability_Internal;
            break;
        case 'O':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Obsolete")))
                return kVTGStability_Obsolete;
            break;
        case 'P':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Private")))
                return kVTGStability_Private;
            break;
        case 'S':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Stable")))
                return kVTGStability_Stable;
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Standard")))
                return kVTGStability_Standard;
            break;
        case 'U':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Unstable")))
                return kVTGStability_Unstable;
            break;
    }
    parseError(pStrm, 1, "Unknown stability specifier");
    return kVTGStability_Invalid;
}


/**
 * Parses data depndency class.
 *
 * @returns Data dependency class if parsed correctly, otherwise error message
 *          and kVTGClass_Invalid.
 * @param   pStrm               The stream.
 * @param   ch                  The first character in the stability spec.
 */
static kVTGClass parseDataDepClass(PSCMSTREAM pStrm, unsigned ch)
{
    switch (ch)
    {
        case 'C':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Common")))
                return kVTGClass_Common;
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Cpu")))
                return kVTGClass_Cpu;
            break;
        case 'G':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Group")))
                return kVTGClass_Group;
            break;
        case 'I':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Isa")))
                return kVTGClass_Isa;
            break;
        case 'P':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Platform")))
                return kVTGClass_Platform;
            break;
        case 'U':
            if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("Unknown")))
                return kVTGClass_Unknown;
            break;
    }
    parseError(pStrm, 1, "Unknown data dependency class specifier");
    return kVTGClass_Invalid;
}

/**
 * Parses a pragma D attributes statement.
 *
 * @returns Suitable exit code, errors message already written on failure.
 * @param   pStrm               The stream.
 */
static RTEXITCODE parsePragmaDAttributes(PSCMSTREAM pStrm)
{
    /*
     * "CodeStability/DataStability/DataDepClass" - no spaces allowed.
     */
    unsigned ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
    if (ch == ~(unsigned)0)
        return RTEXITCODE_FAILURE;

    kVTGStability enmCode = parseStability(pStrm, ch);
    if (enmCode == kVTGStability_Invalid)
        return RTEXITCODE_FAILURE;
    ch = ScmStreamGetCh(pStrm);
    if (ch != '/')
        return parseError(pStrm, 1, "Expected '/' following the code stability specifier");

    kVTGStability enmData = parseStability(pStrm, ScmStreamGetCh(pStrm));
    if (enmData == kVTGStability_Invalid)
        return RTEXITCODE_FAILURE;
    ch = ScmStreamGetCh(pStrm);
    if (ch != '/')
        return parseError(pStrm, 1, "Expected '/' following the data stability specifier");

    kVTGClass enmDataDep =  parseDataDepClass(pStrm, ScmStreamGetCh(pStrm));
    if (enmDataDep == kVTGClass_Invalid)
        return RTEXITCODE_FAILURE;

    /*
     * Expecting 'provider' followed by the name of an provider defined earlier.
     */
    ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
    if (ch == ~(unsigned)0)
        return RTEXITCODE_FAILURE;
    if (ch != 'p' || !ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("provider")))
        return parseError(pStrm, 1, "Expected 'provider'");

    size_t      cchName;
    const char *pszName = parseGetNextCWord(pStrm, &cchName);
    if (!pszName)
        return parseError(pStrm, 1, "Expected provider name");

    PVTGPROVIDER pProv;
    RTListForEach(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry)
    {
        if (   !strncmp(pProv->pszName, pszName, cchName)
            && pProv->pszName[cchName] == '\0')
            break;
    }
    if (RTListNodeIsDummy(&g_ProviderHead, pProv, VTGPROVIDER, ListEntry))
        return parseError(pStrm, cchName, "Provider not found");

    /*
     * Which aspect of the provider?
     */
    size_t      cchAspect;
    const char *pszAspect = parseGetNextCWord(pStrm, &cchAspect);
    if (!pszAspect)
        return parseError(pStrm, 1, "Expected provider aspect");

    PVTGATTRS pAttrs;
    if (cchAspect == 8 && !memcmp(pszAspect, "provider", 8))
        pAttrs = &pProv->AttrSelf;
    else if (cchAspect == 8 && !memcmp(pszAspect, "function", 8))
        pAttrs = &pProv->AttrFunctions;
    else if (cchAspect == 6 && !memcmp(pszAspect, "module", 6))
        pAttrs = &pProv->AttrModules;
    else if (cchAspect == 4 && !memcmp(pszAspect, "name", 4))
        pAttrs = &pProv->AttrName;
    else if (cchAspect == 4 && !memcmp(pszAspect, "args", 4))
        pAttrs = &pProv->AttrArguments;
    else
        return parseError(pStrm, cchAspect, "Unknown aspect");

    if (pAttrs->enmCode != kVTGStability_Invalid)
        return parseError(pStrm, cchAspect, "You have already specified these attributes");

    pAttrs->enmCode     = enmCode;
    pAttrs->enmData     = enmData;
    pAttrs->enmDataDep  = enmDataDep;
    return RTEXITCODE_SUCCESS;
}

/**
 * Parses a D pragma statement.
 *
 * @returns Suitable exit code, errors message already written on failure.
 * @param   pStrm               The stream.
 */
static RTEXITCODE parsePragma(PSCMSTREAM pStrm)
{
    RTEXITCODE rcExit;
    unsigned   ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
    if (ch == ~(unsigned)0)
        rcExit = RTEXITCODE_FAILURE;
    else if (ch == 'D' && ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("D")))
    {
        ch = parseGetNextNonSpaceNonCommentChOnPpLine(pStrm);
        if (ch == ~(unsigned)0)
            rcExit = RTEXITCODE_FAILURE;
        else if (ch == 'a' && ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("attributes")))
            rcExit = parsePragmaDAttributes(pStrm);
        else
            rcExit = parseError(pStrm, 1, "Unknown pragma D");
    }
    else
        rcExit = parseError(pStrm, 1, "Unknown pragma");
    return rcExit;
}


/**
 * Classifies the given type expression.
 *
 * @return  Type flags.
 * @param   pszType         The type expression.
 * @param   pStrm           The input stream (for errors + warnings).
 * @param   offSrc          The absolute source position of this expression (for
 *                          warnings).
 */
static uint32_t parseTypeExpression(const char *pszType, PSCMSTREAM pStrm, size_t offSrc)
{
    size_t cchType = strlen(pszType);
#define MY_STRMATCH(a_sz)  (cchType == sizeof(a_sz) - 1 && !memcmp(a_sz, pszType, sizeof(a_sz) - 1))

    /*
     * Try detect pointers.
     */
    if (pszType[cchType - 1] == '*')
    {
        if (MY_STRMATCH("const char *")) return VTG_TYPE_POINTER | VTG_TYPE_CONST_CHAR_PTR;
        return VTG_TYPE_POINTER;
    }
    if (pszType[cchType - 1] == '&')
    {
        parseWarnAbs(pStrm, offSrc, "Please avoid using references like '%s' for probe arguments!", pszType);
        return VTG_TYPE_POINTER;
    }

    /*
     * Standard integer types and IPRT variants.
     * It's important that we catch all types larger than 32-bit here or we'll
     * screw up the probe argument handling.
     */
    if (MY_STRMATCH("int"))             return VTG_TYPE_FIXED_SIZED | sizeof(int)   | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("uintptr_t"))       return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("intptr_t"))        return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_SIGNED;

    //if (MY_STRMATCH("uint128_t"))       return VTG_TYPE_FIXED_SIZED | sizeof(uint128_t) | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("uint64_t"))        return VTG_TYPE_FIXED_SIZED | sizeof(uint64_t)  | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("uint32_t"))        return VTG_TYPE_FIXED_SIZED | sizeof(uint32_t)  | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("uint16_t"))        return VTG_TYPE_FIXED_SIZED | sizeof(uint16_t)  | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("uint8_t"))         return VTG_TYPE_FIXED_SIZED | sizeof(uint8_t)   | VTG_TYPE_UNSIGNED;

    //if (MY_STRMATCH("int128_t"))        return VTG_TYPE_FIXED_SIZED | sizeof(int128_t)  | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("int64_t"))         return VTG_TYPE_FIXED_SIZED | sizeof(int64_t)   | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("int32_t"))         return VTG_TYPE_FIXED_SIZED | sizeof(int32_t)   | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("int16_t"))         return VTG_TYPE_FIXED_SIZED | sizeof(int16_t)   | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("int8_t"))          return VTG_TYPE_FIXED_SIZED | sizeof(int8_t)    | VTG_TYPE_SIGNED;

    if (MY_STRMATCH("RTUINT64U"))       return VTG_TYPE_FIXED_SIZED | sizeof(uint64_t)  | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTUINT32U"))       return VTG_TYPE_FIXED_SIZED | sizeof(uint32_t)  | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTUINT16U"))       return VTG_TYPE_FIXED_SIZED | sizeof(uint16_t)  | VTG_TYPE_UNSIGNED;

    if (MY_STRMATCH("RTMSINTERVAL"))    return VTG_TYPE_FIXED_SIZED | sizeof(RTMSINTERVAL) | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTTIMESPEC"))      return VTG_TYPE_FIXED_SIZED | sizeof(RTTIMESPEC)   | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("RTPROCESS"))       return VTG_TYPE_FIXED_SIZED | sizeof(RTPROCESS)    | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTHCPHYS"))        return VTG_TYPE_FIXED_SIZED | sizeof(RTHCPHYS)     | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS;

    if (MY_STRMATCH("RTR3PTR"))         return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3;
    if (MY_STRMATCH("RTR0PTR"))         return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0;
    if (MY_STRMATCH("RTRCPTR"))         return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_RC;
    if (MY_STRMATCH("RTHCPTR"))         return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0;

    if (MY_STRMATCH("RTR3UINTPTR"))     return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTR0UINTPTR"))     return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0 | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTRCUINTPTR"))     return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_RC | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTHCUINTPTR"))     return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_UNSIGNED;

    if (MY_STRMATCH("RTR3INTPTR"))      return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("RTR0INTPTR"))      return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0 | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("RTRCINTPTR"))      return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_RC | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("RTHCINTPTR"))      return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_SIGNED;

    if (MY_STRMATCH("RTUINTPTR"))       return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_CTX_RC | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTINTPTR"))        return VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_CTX_RC | VTG_TYPE_SIGNED;

    if (MY_STRMATCH("RTHCUINTREG"))     return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_CTX_R3 | VTG_TYPE_CTX_R0 | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTR3UINTREG"))     return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_CTX_R3 | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("RTR0UINTREG"))     return VTG_TYPE_HC_ARCH_SIZED | VTG_TYPE_CTX_R3 | VTG_TYPE_UNSIGNED;

    if (MY_STRMATCH("RTGCUINTREG"))     return VTG_TYPE_FIXED_SIZED | sizeof(RTGCUINTREG) | VTG_TYPE_UNSIGNED | VTG_TYPE_CTX_GST;
    if (MY_STRMATCH("RTGCPTR"))         return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPTR)     | VTG_TYPE_UNSIGNED | VTG_TYPE_CTX_GST;
    if (MY_STRMATCH("RTGCINTPTR"))      return VTG_TYPE_FIXED_SIZED | sizeof(RTGCUINTPTR) | VTG_TYPE_SIGNED   | VTG_TYPE_CTX_GST;
    if (MY_STRMATCH("RTGCPTR32"))       return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPTR32)   | VTG_TYPE_SIGNED   | VTG_TYPE_CTX_GST;
    if (MY_STRMATCH("RTGCPTR64"))       return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPTR64)   | VTG_TYPE_SIGNED   | VTG_TYPE_CTX_GST;
    if (MY_STRMATCH("RTGCPHYS"))        return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPHYS)    | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS | VTG_TYPE_CTX_GST;
    if (MY_STRMATCH("RTGCPHYS32"))      return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPHYS32)  | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS | VTG_TYPE_CTX_GST;
    if (MY_STRMATCH("RTGCPHYS64"))      return VTG_TYPE_FIXED_SIZED | sizeof(RTGCPHYS64)  | VTG_TYPE_UNSIGNED | VTG_TYPE_PHYS | VTG_TYPE_CTX_GST;

    /*
     * The special VBox types.
     */
    if (MY_STRMATCH("PVM"))             return VTG_TYPE_POINTER;
    if (MY_STRMATCH("PVMCPU"))          return VTG_TYPE_POINTER;
    if (MY_STRMATCH("PCPUMCTX"))        return VTG_TYPE_POINTER;

    /*
     * Preaching time.
     */
    if (   MY_STRMATCH("unsigned long")
        || MY_STRMATCH("unsigned long long")
        || MY_STRMATCH("signed long")
        || MY_STRMATCH("signed long long")
        || MY_STRMATCH("long")
        || MY_STRMATCH("long long")
        || MY_STRMATCH("char")
        || MY_STRMATCH("signed char")
        || MY_STRMATCH("unsigned char")
        || MY_STRMATCH("double")
        || MY_STRMATCH("long double")
        || MY_STRMATCH("float")
       )
    {
        RTMsgError("Please do NOT use the type '%s' for probe arguments!", pszType);
        g_cTypeErrors++;
        return 0;
    }

    if (   MY_STRMATCH("unsigned")
        || MY_STRMATCH("signed")
        || MY_STRMATCH("signed int")
        || MY_STRMATCH("unsigned int")
        || MY_STRMATCH("short")
        || MY_STRMATCH("signed short")
        || MY_STRMATCH("unsigned short")
       )
        parseWarnAbs(pStrm, offSrc, "Please avoid using the type '%s' for probe arguments!", pszType);
    if (MY_STRMATCH("unsigned"))        return VTG_TYPE_FIXED_SIZED | sizeof(int)   | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("unsigned int"))    return VTG_TYPE_FIXED_SIZED | sizeof(int)   | VTG_TYPE_UNSIGNED;
    if (MY_STRMATCH("signed"))          return VTG_TYPE_FIXED_SIZED | sizeof(int)   | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("signed int"))      return VTG_TYPE_FIXED_SIZED | sizeof(int)   | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("short"))           return VTG_TYPE_FIXED_SIZED | sizeof(short) | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("signed short"))    return VTG_TYPE_FIXED_SIZED | sizeof(short) | VTG_TYPE_SIGNED;
    if (MY_STRMATCH("unsigned short"))  return VTG_TYPE_FIXED_SIZED | sizeof(short) | VTG_TYPE_UNSIGNED;

    /*
     * What we haven't caught by now is either unknown to us or wrong.
     */
    if (pszType[0] == 'P')
    {
        RTMsgError("Type '%s' looks like a pointer typedef, please do NOT use those "
                   "but rather the non-pointer typedef or struct with '*'",
                   pszType);
        g_cTypeErrors++;
        return VTG_TYPE_POINTER;
    }

    RTMsgError("Don't know '%s' - please change or fix VBoxTpG", pszType);
    g_cTypeErrors++;

#undef MY_STRCMP
    return 0;
}


/**
 * Initializes the members of an argument.
 *
 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE+msg.
 * @param   pProbe              The probe.
 * @param   pArg                The argument.
 * @param   pStrm               The input stream (for errors + warnings).
 * @param   pchType             The type.
 * @param   cchType             The type length.
 * @param   pchName             The name.
 * @param   cchName             The name length.
 */
static RTEXITCODE parseInitArgument(PVTGPROBE pProbe, PVTGARG pArg, PSCMSTREAM pStrm,
                                    char *pchType, size_t cchType, char *pchName, size_t cchName)
{
    Assert(!pArg->pszName); Assert(!pArg->pszTracerType); Assert(!pArg->pszCtxType); Assert(!pArg->fType);

    pArg->pszArgPassingFmt  = ", %s";
    pArg->pszName           = RTStrDupN(pchName, cchName);
    pArg->pszTracerType     = strtabInsertN(pchType, cchType);
    if (!pArg->pszTracerType || !pArg->pszName)
        return parseError(pStrm, 1, "Out of memory");
    pArg->fType             = parseTypeExpression(pArg->pszTracerType, pStrm, pArg->offSrc);

    if (   (pArg->fType & VTG_TYPE_POINTER)
        && !(g_fTypeContext & VTG_TYPE_CTX_R0) )
    {
        pArg->fType &= ~VTG_TYPE_POINTER;
        if (   !strcmp(pArg->pszTracerType, "struct VM *")          || !strcmp(pArg->pszTracerType, "PVM")
            || !strcmp(pArg->pszTracerType, "struct VMCPU *")       || !strcmp(pArg->pszTracerType, "PVMCPU")
            || !strcmp(pArg->pszTracerType, "struct CPUMCTX *")     || !strcmp(pArg->pszTracerType, "PCPUMCTX")
            )
        {
            pArg->fType |= VTG_TYPE_CTX_POINTER | VTG_TYPE_CTX_R0
                         | VTG_TYPE_FIXED_SIZED | (g_cHostBits / 8)
                         | VTG_TYPE_AUTO_CONV_PTR;
            pArg->pszCtxType = RTStrDup("RTR0PTR");

            if (!strcmp(pArg->pszTracerType, "struct VM *")         || !strcmp(pArg->pszTracerType, "PVM"))
                pArg->pszArgPassingFmt = ", VTG_VM_TO_R0(%s)";
            else if (!strcmp(pArg->pszTracerType, "struct VMCPU *") || !strcmp(pArg->pszTracerType, "PVMCPU"))
                pArg->pszArgPassingFmt = ", VTG_VMCPU_TO_R0(%s)";
            else
            {
                PVTGARG pFirstArg = RTListGetFirst(&pProbe->ArgHead, VTGARG, ListEntry);
                if (   !pFirstArg
                    || pFirstArg == pArg
                    || strcmp(pFirstArg->pszName, "a_pVCpu")
                    || (   strcmp(pFirstArg->pszTracerType, "struct VMCPU *")
                        && strcmp(pFirstArg->pszTracerType, "PVMCPU *")) )
                    return parseError(pStrm, 1, "The automatic ring-0 pointer conversion requires 'a_pVCpu' with type 'struct VMCPU *' as the first argument");

                if (!strcmp(pArg->pszTracerType, "struct CPUMCTX *")|| !strcmp(pArg->pszTracerType, "PCPUMCTX"))
                    pArg->pszArgPassingFmt = ", VTG_CPUMCTX_TO_R0(a_pVCpu, %s)";
                else
                    pArg->pszArgPassingFmt = ", VBoxTpG-Is-Buggy!!";
            }
        }
        else
        {
            pArg->fType |= VTG_TYPE_CTX_POINTER | g_fTypeContext | VTG_TYPE_FIXED_SIZED | (g_cBits / 8);
            pArg->pszCtxType = RTStrDupN(pchType, cchType);
        }
    }
    else
        pArg->pszCtxType = RTStrDupN(pchType, cchType);
    if (!pArg->pszCtxType)
        return parseError(pStrm, 1, "Out of memory");

    return RTEXITCODE_SUCCESS;
}


/**
 * Unmangles the probe name.
 *
 * This involves translating double underscore to dash.
 *
 * @returns Pointer to the unmangled name in the string table.
 * @param   pszMangled          The mangled name.
 */
static const char *parseUnmangleProbeName(const char *pszMangled)
{
    size_t      cchMangled = strlen(pszMangled);
    char       *pszTmp     = (char *)alloca(cchMangled + 2);
    const char *pszSrc     = pszMangled;
    char       *pszDst     = pszTmp;

    while (*pszSrc)
    {
        if (pszSrc[0] == '_' && pszSrc[1] == '_' && pszSrc[2] != '_')
        {
            *pszDst++ = '-';
            pszSrc   += 2;
        }
        else
            *pszDst++ = *pszSrc++;
    }
    *pszDst = '\0';

    return strtabInsertN(pszTmp, pszDst - pszTmp);
}


/**
 * Parses a D probe statement.
 *
 * @returns Suitable exit code, errors message already written on failure.
 * @param   pStrm               The stream.
 * @param   pProv               The provider being parsed.
 */
static RTEXITCODE parseProbe(PSCMSTREAM pStrm, PVTGPROVIDER pProv)
{
    size_t const iProbeLine = ScmStreamTellLine(pStrm);

    /*
     * Next up is a name followed by an opening parenthesis.
     */
    size_t      cchProbe;
    const char *pszProbe = parseGetNextCWord(pStrm, &cchProbe);
    if (!pszProbe)
        return parseError(pStrm, 1, "Expected a probe name starting with an alphabetical character");
    unsigned ch = parseGetNextNonSpaceNonCommentCh(pStrm);
    if (ch != '(')
        return parseError(pStrm, 1, "Expected '(' after the probe name");

    /*
     * Create a probe instance.
     */
    PVTGPROBE pProbe = (PVTGPROBE)RTMemAllocZ(sizeof(*pProbe));
    if (!pProbe)
        return parseError(pStrm, 0, "Out of memory");
    RTListInit(&pProbe->ArgHead);
    RTListAppend(&pProv->ProbeHead, &pProbe->ListEntry);
    pProbe->offArgList     = UINT32_MAX;
    pProbe->iLine          = iProbeLine;
    pProbe->pszMangledName = RTStrDupN(pszProbe, cchProbe);
    if (!pProbe->pszMangledName)
        return parseError(pStrm, 0, "Out of memory");
    pProbe->pszUnmangledName = parseUnmangleProbeName(pProbe->pszMangledName);
    if (!pProbe->pszUnmangledName)
        return parseError(pStrm, 0, "Out of memory");

    /*
     * Parse loop for the argument.
     */
    PVTGARG pArg    = NULL;
    size_t  cchName = 0;
    size_t  cchArg  = 0;
    char    szArg[4096];
    for (;;)
    {
        ch = parseGetNextNonSpaceNonCommentCh(pStrm);
        switch (ch)
        {
            case ')':
            case ',':
            {
                /* commit the argument */
                if (pArg)
                {
                    if (!cchName)
                        return parseError(pStrm, 1, "Argument has no name");
                    if (cchArg - cchName - 1 >= 128)
                        return parseError(pStrm, 1, "Argument type too long");
                    RTEXITCODE rcExit = parseInitArgument(pProbe, pArg, pStrm,
                                                          szArg, cchArg - cchName - 1,
                                                          &szArg[cchArg - cchName], cchName);
                    if (rcExit != RTEXITCODE_SUCCESS)
                        return rcExit;
                    if (VTG_TYPE_IS_LARGE(pArg->fType))
                        pProbe->fHaveLargeArgs = true;
                    pArg = NULL;
                    cchName = cchArg = 0;
                }
                if (ch == ')')
                {
                    size_t off = ScmStreamTell(pStrm);
                    ch = parseGetNextNonSpaceNonCommentCh(pStrm);
                    if (ch != ';')
                        return parseErrorAbs(pStrm, off, "Expected ';'");
                    return RTEXITCODE_SUCCESS;
                }
                break;
            }

            default:
            {
                size_t      cchWord;
                const char *pszWord = ScmStreamCGetWordM1(pStrm, &cchWord);
                if (!pszWord)
                    return parseError(pStrm, 0, "Expected argument");
                if (!pArg)
                {
                    pArg = (PVTGARG)RTMemAllocZ(sizeof(*pArg));
                    if (!pArg)
                        return parseError(pStrm, 1, "Out of memory");
                    RTListAppend(&pProbe->ArgHead, &pArg->ListEntry);
                    pArg->iArgNo = pProbe->cArgs++;
                    pArg->pProbe = pProbe;
                    pArg->offSrc = ScmStreamTell(pStrm) - cchWord;

                    if (cchWord + 1 > sizeof(szArg))
                        return parseError(pStrm, 1, "Too long parameter declaration");
                    memcpy(szArg, pszWord, cchWord);
                    szArg[cchWord] = '\0';
                    cchArg  = cchWord;
                    cchName = 0;
                }
                else
                {
                    if (cchArg + 1 + cchWord + 1 > sizeof(szArg))
                        return parseError(pStrm, 1, "Too long parameter declaration");

                    szArg[cchArg++] = ' ';
                    memcpy(&szArg[cchArg], pszWord, cchWord);
                    cchArg += cchWord;
                    szArg[cchArg] = '\0';
                    cchName = cchWord;
                }
                break;
            }

            case '*':
            {
                if (!pArg)
                    return parseError(pStrm, 1, "A parameter type does not start with an asterix");
                if (cchArg + sizeof(" *") >= sizeof(szArg))
                    return parseError(pStrm, 1, "Too long parameter declaration");
                szArg[cchArg++] = ' ';
                szArg[cchArg++] = '*';
                szArg[cchArg  ] = '\0';
                cchName = 0;
                break;
            }

            case ~(unsigned)0:
                return parseError(pStrm, 0, "Missing closing ')' on probe");
        }
    }
}

/**
 * Parses a D provider statement.
 *
 * @returns Suitable exit code, errors message already written on failure.
 * @param   pStrm               The stream.
 */
static RTEXITCODE parseProvider(PSCMSTREAM pStrm)
{
    /*
     * Next up is a name followed by a curly bracket. Ignore comments.
     */
    RTEXITCODE rcExit = parseSkipSpacesAndComments(pStrm);
    if (rcExit != RTEXITCODE_SUCCESS)
        return parseError(pStrm, 1, "Expected a provider name starting with an alphabetical character");
    size_t      cchName;
    const char *pszName = ScmStreamCGetWord(pStrm, &cchName);
    if (!pszName)
        return parseError(pStrm, 0, "Bad provider name");
    if (RT_C_IS_DIGIT(pszName[cchName - 1]))
        return parseError(pStrm, 1, "A provider name cannot end with digit");

    unsigned ch = parseGetNextNonSpaceNonCommentCh(pStrm);
    if (ch != '{')
        return parseError(pStrm, 1, "Expected '{' after the provider name");

    /*
     * Create a provider instance.
     */
    PVTGPROVIDER pProv = (PVTGPROVIDER)RTMemAllocZ(sizeof(*pProv));
    if (!pProv)
        return parseError(pStrm, 0, "Out of memory");
    RTListInit(&pProv->ProbeHead);
    RTListAppend(&g_ProviderHead, &pProv->ListEntry);
    pProv->pszName = strtabInsertN(pszName, cchName);
    if (!pProv->pszName)
        return parseError(pStrm, 0, "Out of memory");

    /*
     * Parse loop.
     */
    for (;;)
    {
        ch = parseGetNextNonSpaceNonCommentCh(pStrm);
        switch (ch)
        {
            case 'p':
                if (ScmStreamCMatchingWordM1(pStrm, RT_STR_TUPLE("probe")))
                    rcExit = parseProbe(pStrm, pProv);
                else
                    rcExit = parseError(pStrm, 1, "Unexpected character");
                break;

            case '}':
            {
                size_t off = ScmStreamTell(pStrm);
                ch = parseGetNextNonSpaceNonCommentCh(pStrm);
                if (ch == ';')
                    return RTEXITCODE_SUCCESS;
                rcExit = parseErrorAbs(pStrm, off, "Expected ';'");
                break;
            }

            case ~(unsigned)0:
                rcExit = parseError(pStrm, 0, "Missing closing '}' on provider");
                break;

            default:
                rcExit = parseError(pStrm, 1, "Unexpected character");
                break;
        }
        if (rcExit != RTEXITCODE_SUCCESS)
            return rcExit;
    }
}


static RTEXITCODE parseScript(const char *pszScript)
{
    SCMSTREAM Strm;
    int rc = ScmStreamInitForReading(&Strm, pszScript);
    if (RT_FAILURE(rc))
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to open & read '%s' into memory: %Rrc", pszScript, rc);
    if (g_cVerbosity > 0)
        RTMsgInfo("Parsing '%s'...", pszScript);

    RTEXITCODE  rcExit = RTEXITCODE_SUCCESS;
    unsigned    ch;
    while ((ch = ScmStreamGetCh(&Strm)) != ~(unsigned)0)
    {
        if (RT_C_IS_SPACE(ch))
            continue;
        switch (ch)
        {
            case '/':
                ch = ScmStreamGetCh(&Strm);
                if (ch == '*')
                    rcExit = parseMultiLineComment(&Strm);
                else if (ch == '/')
                    rcExit = parseOneLineComment(&Strm);
                else
                    rcExit = parseError(&Strm, 2, "Unexpected character");
                break;

            case 'p':
                if (ScmStreamCMatchingWordM1(&Strm, RT_STR_TUPLE("provider")))
                    rcExit = parseProvider(&Strm);
                else
                    rcExit = parseError(&Strm, 1, "Unexpected character");
                break;

            case '#':
            {
                ch = parseGetNextNonSpaceNonCommentChOnPpLine(&Strm);
                if (ch == ~(unsigned)0)
                    rcExit = RTEXITCODE_FAILURE;
                else if (ch == 'p' && ScmStreamCMatchingWordM1(&Strm, RT_STR_TUPLE("pragma")))
                    rcExit = parsePragma(&Strm);
                else
                    rcExit = parseError(&Strm, 1, "Unsupported preprocessor directive");
                break;
            }

            default:
                rcExit = parseError(&Strm, 1, "Unexpected character");
                break;
        }
        if (rcExit != RTEXITCODE_SUCCESS)
            return rcExit;
    }

    ScmStreamDelete(&Strm);
    if (g_cVerbosity > 0 && rcExit == RTEXITCODE_SUCCESS)
        RTMsgInfo("Successfully parsed '%s'.", pszScript);
    return rcExit;
}


/**
 * Parses the arguments.
 */
static RTEXITCODE parseArguments(int argc,  char **argv)
{
    /*
     * Set / Adjust defaults.
     */
    int rc = RTPathAbs(g_pszAssemblerIncVal, g_szAssemblerIncVal, sizeof(g_szAssemblerIncVal) - 1);
    if (RT_FAILURE(rc))
        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTPathAbs failed: %Rrc", rc);
    strcat(g_szAssemblerIncVal, "/");
    g_pszAssemblerIncVal = g_szAssemblerIncVal;

    /*
     * Option config.
     */
    enum
    {
        kVBoxTpGOpt_32Bit = 1000,
        kVBoxTpGOpt_64Bit,
        kVBoxTpGOpt_GenerateWrapperHeader,
        kVBoxTpGOpt_Assembler,
        kVBoxTpGOpt_AssemblerFmtOpt,
        kVBoxTpGOpt_AssemblerFmtVal,
        kVBoxTpGOpt_AssemblerOutputOpt,
        kVBoxTpGOpt_AssemblerOption,
        kVBoxTpGOpt_Pic,
        kVBoxTpGOpt_ProbeFnName,
        kVBoxTpGOpt_ProbeFnImported,
        kVBoxTpGOpt_ProbeFnNotImported,
        kVBoxTpGOpt_Host32Bit,
        kVBoxTpGOpt_Host64Bit,
        kVBoxTpGOpt_RawModeContext,
        kVBoxTpGOpt_Ring0Context,
        kVBoxTpGOpt_Ring0ContextAgnostic,
        kVBoxTpGOpt_Ring3Context,
        kVBoxTpGOpt_End
    };

    static RTGETOPTDEF const s_aOpts[] =
    {
        /* dtrace w/ long options */
        { "-32",                                kVBoxTpGOpt_32Bit,                      RTGETOPT_REQ_NOTHING },
        { "-64",                                kVBoxTpGOpt_64Bit,                      RTGETOPT_REQ_NOTHING },
        { "--apply-cpp",                        'C',                                    RTGETOPT_REQ_NOTHING },
        { "--generate-obj",                     'G',                                    RTGETOPT_REQ_NOTHING },
        { "--generate-header",                  'h',                                    RTGETOPT_REQ_NOTHING },
        { "--output",                           'o',                                    RTGETOPT_REQ_STRING  },
        { "--script",                           's',                                    RTGETOPT_REQ_STRING  },
        { "--verbose",                          'v',                                    RTGETOPT_REQ_NOTHING },
        /* our stuff */
        { "--generate-wrapper-header",          kVBoxTpGOpt_GenerateWrapperHeader,      RTGETOPT_REQ_NOTHING },
        { "--assembler",                        kVBoxTpGOpt_Assembler,                  RTGETOPT_REQ_STRING  },
        { "--assembler-fmt-opt",                kVBoxTpGOpt_AssemblerFmtOpt,            RTGETOPT_REQ_STRING  },
        { "--assembler-fmt-val",                kVBoxTpGOpt_AssemblerFmtVal,            RTGETOPT_REQ_STRING  },
        { "--assembler-output-opt",             kVBoxTpGOpt_AssemblerOutputOpt,         RTGETOPT_REQ_STRING  },
        { "--assembler-option",                 kVBoxTpGOpt_AssemblerOption,            RTGETOPT_REQ_STRING  },
        { "--pic",                              kVBoxTpGOpt_Pic,                        RTGETOPT_REQ_NOTHING },
        { "--probe-fn-name",                    kVBoxTpGOpt_ProbeFnName,                RTGETOPT_REQ_STRING  },
        { "--probe-fn-imported",                kVBoxTpGOpt_ProbeFnImported,            RTGETOPT_REQ_NOTHING },
        { "--probe-fn-not-imported",            kVBoxTpGOpt_ProbeFnNotImported,         RTGETOPT_REQ_NOTHING },
        { "--host-32-bit",                      kVBoxTpGOpt_Host32Bit,                  RTGETOPT_REQ_NOTHING },
        { "--host-64-bit",                      kVBoxTpGOpt_Host64Bit,                  RTGETOPT_REQ_NOTHING },
        { "--raw-mode-context",                 kVBoxTpGOpt_RawModeContext,             RTGETOPT_REQ_NOTHING },
        { "--ring-0-context",                   kVBoxTpGOpt_Ring0Context,               RTGETOPT_REQ_NOTHING },
        { "--ring-0-context-agnostic",          kVBoxTpGOpt_Ring0ContextAgnostic,       RTGETOPT_REQ_NOTHING },
        { "--ring-3-context",                   kVBoxTpGOpt_Ring3Context,               RTGETOPT_REQ_NOTHING },
        /** @todo We're missing a bunch of assembler options! */
    };

    RTGETOPTUNION   ValueUnion;
    RTGETOPTSTATE   GetOptState;
    rc = RTGetOptInit(&GetOptState, argc, argv, &s_aOpts[0], RT_ELEMENTS(s_aOpts), 1, RTGETOPTINIT_FLAGS_OPTS_FIRST);
    AssertReleaseRCReturn(rc, RTEXITCODE_FAILURE);

    /*
     * Process the options.
     */
    while ((rc = RTGetOpt(&GetOptState, &ValueUnion)) != 0)
    {
        switch (rc)
        {
            /*
             * DTrace compatible options.
             */
            case kVBoxTpGOpt_32Bit:
                g_cHostBits = g_cBits = 32;
                g_pszAssemblerFmtVal = g_szAssemblerFmtVal32;
                break;

            case kVBoxTpGOpt_64Bit:
                g_cHostBits = g_cBits = 64;
                g_pszAssemblerFmtVal = g_szAssemblerFmtVal64;
                break;

            case 'C':
                g_fApplyCpp = true;
                RTMsgWarning("Ignoring the -C option - no preprocessing of the D script will be performed");
                break;

            case 'G':
                if (   g_enmAction != kVBoxTpGAction_Nothing
                    && g_enmAction != kVBoxTpGAction_GenerateObject)
                    return RTMsgErrorExit(RTEXITCODE_SYNTAX, "-G does not mix with -h or --generate-wrapper-header");
                g_enmAction = kVBoxTpGAction_GenerateObject;
                break;

            case 'h':
                if (!strcmp(GetOptState.pDef->pszLong, "--generate-header"))
                {
                    if (   g_enmAction != kVBoxTpGAction_Nothing
                        && g_enmAction != kVBoxTpGAction_GenerateHeader)
                        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "-h does not mix with -G or --generate-wrapper-header");
                    g_enmAction = kVBoxTpGAction_GenerateHeader;
                }
                else
                {
                    /* --help or similar */
                    RTPrintf("VirtualBox Tracepoint Generator\n"
                             "\n"
                             "Usage: %s [options]\n"
                             "\n"
                             "Options:\n", RTProcShortName());
                    for (size_t i = 0; i < RT_ELEMENTS(s_aOpts); i++)
                        if ((unsigned)s_aOpts[i].iShort < 128)
                            RTPrintf("   -%c,%s\n", s_aOpts[i].iShort, s_aOpts[i].pszLong);
                        else
                            RTPrintf("   %s\n", s_aOpts[i].pszLong);
                    return RTEXITCODE_SUCCESS;
                }
                break;

            case 'o':
                if (g_pszOutput)
                    return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Output file is already set to '%s'", g_pszOutput);
                g_pszOutput = ValueUnion.psz;
                break;

            case 's':
                if (g_pszScript)
                    return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Script file is already set to '%s'", g_pszScript);
                g_pszScript = ValueUnion.psz;
                break;

            case 'v':
                g_cVerbosity++;
                break;

            case 'V':
            {
                /* The following is assuming that svn does it's job here. */
                static const char s_szRev[] = "$Revision: 155244 $";
                const char *psz = RTStrStripL(strchr(s_szRev, ' '));
                RTPrintf("r%.*s\n", strchr(psz, ' ') - psz, psz);
                return RTEXITCODE_SUCCESS;
            }

            case VINF_GETOPT_NOT_OPTION:
                if (g_enmAction == kVBoxTpGAction_GenerateObject)
                    break; /* object files, ignore them. */
                return RTGetOptPrintError(rc, &ValueUnion);


            /*
             * Our options.
             */
            case kVBoxTpGOpt_GenerateWrapperHeader:
                if (   g_enmAction != kVBoxTpGAction_Nothing
                    && g_enmAction != kVBoxTpGAction_GenerateWrapperHeader)
                    return RTMsgErrorExit(RTEXITCODE_SYNTAX, "--generate-wrapper-header does not mix with -h or -G");
                g_enmAction = kVBoxTpGAction_GenerateWrapperHeader;
                break;

            case kVBoxTpGOpt_Assembler:
                g_pszAssembler = ValueUnion.psz;
                break;

            case kVBoxTpGOpt_AssemblerFmtOpt:
                g_pszAssemblerFmtOpt = ValueUnion.psz;
                break;

            case kVBoxTpGOpt_AssemblerFmtVal:
                g_pszAssemblerFmtVal = ValueUnion.psz;
                break;

            case kVBoxTpGOpt_AssemblerOutputOpt:
                g_pszAssemblerOutputOpt = ValueUnion.psz;
                break;

            case kVBoxTpGOpt_AssemblerOption:
                if (g_cAssemblerOptions >= RT_ELEMENTS(g_apszAssemblerOptions))
                    return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Too many assembly options (max %u)", RT_ELEMENTS(g_apszAssemblerOptions));
                g_apszAssemblerOptions[g_cAssemblerOptions] = ValueUnion.psz;
                g_cAssemblerOptions++;
                break;

            case kVBoxTpGOpt_Pic:
                g_fPic = true;
                break;

            case kVBoxTpGOpt_ProbeFnName:
                g_pszProbeFnName = ValueUnion.psz;
                break;

            case kVBoxTpGOpt_ProbeFnImported:
                g_fProbeFnImported = true;
                break;

            case kVBoxTpGOpt_ProbeFnNotImported:
                g_fProbeFnImported = false;
                break;

            case kVBoxTpGOpt_Host32Bit:
                g_cHostBits = 32;
                break;

            case kVBoxTpGOpt_Host64Bit:
                g_cHostBits = 64;
                break;

            case kVBoxTpGOpt_RawModeContext:
                g_fTypeContext = VTG_TYPE_CTX_RC;
                g_pszContextDefine = "IN_RC";
                g_pszContextDefine2 = NULL;
                break;

            case kVBoxTpGOpt_Ring0Context:
                g_fTypeContext = VTG_TYPE_CTX_R0;
                g_pszContextDefine = "IN_RING0";
                g_pszContextDefine2 = NULL;
                break;

            case kVBoxTpGOpt_Ring0ContextAgnostic:
                g_fTypeContext = VTG_TYPE_CTX_R0;
                g_pszContextDefine = "IN_RING0_AGNOSTIC";
                g_pszContextDefine2 = "IN_RING0";
                break;

            case kVBoxTpGOpt_Ring3Context:
                g_fTypeContext = VTG_TYPE_CTX_R3;
                g_pszContextDefine = "IN_RING3";
                g_pszContextDefine2 = NULL;
                break;


            /*
             * Errors and bugs.
             */
            default:
                return RTGetOptPrintError(rc, &ValueUnion);
        }
    }

    /*
     * Check that we've got all we need.
     */
    if (g_enmAction == kVBoxTpGAction_Nothing)
        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No action specified (-h, -G or --generate-wrapper-header)");
    if (!g_pszScript)
        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No script file specified (-s)");
    if (!g_pszOutput)
        return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No output file specified (-o)");

    return RTEXITCODE_SUCCESS;
}


int main(int argc, char **argv)
{
    int rc = RTR3InitExe(argc, &argv, 0);
    if (RT_FAILURE(rc))
        return 1;

    RTEXITCODE rcExit = parseArguments(argc, argv);
    if (rcExit == RTEXITCODE_SUCCESS)
    {
        /*
         * Parse the script.
         */
        RTListInit(&g_ProviderHead);
        rcExit = parseScript(g_pszScript);
        if (rcExit == RTEXITCODE_SUCCESS)
        {
            /*
             * Take action.
             */
            if (g_enmAction == kVBoxTpGAction_GenerateHeader)
                rcExit = generateFile(g_pszOutput, "header", generateHeader);
            else if (g_enmAction == kVBoxTpGAction_GenerateWrapperHeader)
                rcExit = generateFile(g_pszOutput, "wrapper header", generateWrapperHeader);
            else
                rcExit = generateObject(g_pszOutput, g_pszTempAsm);
        }
    }

    if (rcExit == RTEXITCODE_SUCCESS && g_cTypeErrors > 0)
        rcExit = RTEXITCODE_FAILURE;
    return rcExit;
}