summaryrefslogtreecommitdiff
path: root/src/VBox/VMM/VMMR3/DBGFR3FlowTrace.cpp
blob: 17c1864b3ff89099fecceeb8abd30acf7a5d5135 (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
/* $Id$ */
/** @file
 * DBGF - Debugger Facility, Guest Execution Flow Tracing.
 */

/*
 * Copyright (C) 2016-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
 */


/** @page pg_dbgf_flow DBGFR3FlowTrace - Flow Trace Interface
 *
 * @todo
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_DBGF
#include <VBox/vmm/dbgfflowtrace.h>
#include "DBGFInternal.h"
#include <VBox/vmm/mm.h>
#include <VBox/vmm/uvm.h>
#include <VBox/vmm/vm.h>
#include <VBox/err.h>
#include <VBox/log.h>

#include <iprt/assert.h>
#include <iprt/semaphore.h>
#include <iprt/list.h>
#include <iprt/time.h>


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/



/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/

/** Pointer to the internal trace module instance data. */
typedef struct DBGFFLOWTRACEMODINT *PDBGFFLOWTRACEMODINT;
/** Pointer to a trace module probe location. */
typedef struct DBGFFLOWTRACEMODPROBELOC *PDBGFFLOWTRACEMODPROBELOC;

/**
 * Internal probe instance data.
 */
typedef struct DBGFFLOWTRACEPROBEINT
{
    /** External and internal references hold. */
    volatile uint32_t           cRefs;
    /** Trace modules referencing this probe. */
    volatile uint32_t           cRefsMod;
    /** The user mode VM handle. */
    PUVM                        pUVM;
    /** Description of this probe. */
    char                        *pszDescr;
    /** Overall memory consumed for this probe for each invocation. */
    size_t                      cbProbe;
    /** Number of entries for this probe. */
    uint32_t                    cEntries;
    /** Maximum number of entries the array can hold. */
    uint32_t                    cEntriesMax;
    /** Pointer to the probe entry array. */
    PDBGFFLOWTRACEPROBEENTRY    paEntries;
} DBGFFLOWTRACEPROBEINT;
/** Pointer to the internal probe instance data. */
typedef DBGFFLOWTRACEPROBEINT *PDBGFFLOWTRACEPROBEINT;
/** Pointer to a constant internal probe instance data. */
typedef const DBGFFLOWTRACEPROBEINT *PCDBGFFLOWTRACEPROBEINT;

/**
 * Record collected for one probe hit.
 */
typedef struct DBGFFLOWTRACERECORDINT
{
    /** Data list node. */
    RTLISTNODE                  NdRecord;
    /** The probe instance the record was created for. */
    PDBGFFLOWTRACEPROBEINT      pProbe;
    /** The common probe instance data was collected for. */
    PDBGFFLOWTRACEPROBEINT      pProbeCmn;
    /** Address of the probe location. */
    DBGFADDRESS                 AddrProbe;
    /** Reference counter. */
    volatile uint32_t           cRefs;
    /** CPU ID this data was collected on. */
    VMCPUID                     idCpu;
    /** Sequence number for this data. */
    uint64_t                    u64SeqNo;
    /** Timestamp in nanoseconds when the data was collected. */
    uint64_t                    u64TsCollected;
    /** Pointer to the values for the common probe if available. */
    PDBGFFLOWTRACEPROBEVAL      paValCmn;
    /** The probe values collected - size defined
     * by the number of entries in the probe. */
    DBGFFLOWTRACEPROBEVAL       aVal[1];
} DBGFFLOWTRACERECORDINT;
/** Pointer to one collected probe data. */
typedef DBGFFLOWTRACERECORDINT *PDBGFFLOWTRACERECORDINT;

/**
 * Trace module state.
 */
typedef enum DBGFFLOWTRACEMODSTATE
{
    /** Invalid state. */
    DBGFFLOWTRACEMODSTATE_INVALID = 0,
    /** The module was created. */
    DBGFFLOWTRACEMODSTATE_CREATED,
    /** The module is active, no probes can be added. */
    DBGFFLOWTRACEMODSTATE_ENABLED,
    /** The VM is destroyed but there are still references to the module,
     * functionality is limited (query records only). */
    DBGFFLOWTRACEMODSTATE_VM_DESTROYED,
    /** The trace module is destroyed. */
    DBGFFLOWTRACEMODSTATE_DESTROYED,
    /** 32bit hack. */
    DBGFFLOWTRACEMODSTATE_32BIT_HACK = 0x7fffffff
} DBGFFLOWTRACEMODSTATE;

/**
 * Internal trace module instance data.
 */
typedef struct DBGFFLOWTRACEMODINT
{
    /** References hold for this trace module. */
    volatile uint32_t                cRefs;
    /** The user mode VM handle. */
    PUVM                             pUVM;
    /** CPU ID the module is for. */
    VMCPUID                          idCpu;
    /** The DBGF owner handle. */
    DBGFBPOWNER                      hBpOwner;
    /** State of the trace module. */
    volatile DBGFFLOWTRACEMODSTATE   enmState;
    /** Next free sequence number. */
    volatile uint64_t                u64SeqNoNext;
    /** Optional ocmmon probe describing data to collect. */
    PDBGFFLOWTRACEPROBEINT           pProbeCmn;
    /** Flags whether to record only a limited amount of data as indicated
     * by cHitsLeft. */
    bool                             fLimit;
    /** Number of hits left until the module is disabled automatically. */
    volatile uint32_t                cHitsLeft;
    /** Number of records to keep before evicting the oldest one. */
    uint32_t                         cRecordsMax;
    /** Number of records collected in this module. */
    volatile uint32_t                cRecords;
    /** Number of probes in this trace module. */
    uint32_t                         cProbes;
    /** List of probes active for this module - DBGFFLOWTRACEMODPROBELOC. */
    RTLISTANCHOR                     LstProbes;
    /** List of collected data for this module. */
    RTLISTANCHOR                     LstRecords;
    /** Semaphore protecting access to the probe and record list. */
    RTSEMFASTMUTEX                   hMtx;
} DBGFFLOWTRACEMODINT;
/** Pointer to a const internal trace module instance data. */
typedef const DBGFFLOWTRACEMODINT *PCDBGFFLOWTRACEMODINT;

/**
 * Trace module probe location data.
 */
typedef struct DBGFFLOWTRACEMODPROBELOC
{
    /** List node for the list of probes. */
    RTLISTNODE                       NdProbes;
    /** The owning trace module. */
    PDBGFFLOWTRACEMODINT             pTraceMod;
    /** The probe instance. */
    PDBGFFLOWTRACEPROBEINT           pProbe;
    /** Address of the probe location. */
    DBGFADDRESS                      AddrProbe;
    /** The DBGF breakpoint handle. */
    DBGFBP                           hBp;
    /** Flags controlling the collection behavior for the probe. */
    uint32_t                         fFlags;
} DBGFFLOWTRACEMODPROBELOC;


/**
 * Flow trace report state.
 */
typedef struct DBGFFLOWTRACEREPORTINT
{
    /** The user mode VM handle. */
    PUVM                            pUVM;
    /** Reference count. */
    volatile uint32_t               cRefs;
    /** Number of records. */
    uint32_t                        cRecords;
    /** Array with handle of records - variable in size. */
    PDBGFFLOWTRACERECORDINT         apRec[1];
} DBGFFLOWTRACEMODREPORTINT;
/** Pointer to the internal flow trace report state. */
typedef DBGFFLOWTRACEREPORTINT *PDBGFFLOWTRACEREPORTINT;


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/

/**
 * Creates a new trace record.
 *
 * @returns Pointer to the create flow trace record or NULL if out of memory.
 * @param   pProbeLoc               The probe location to allocate the record for.
 * @param   idCpu                   The CPU ID this record was created for.
 * @param   ppbBuf                  Where to store the pointer to the data buffer for this probe.
 * @param   ppbBufCmn               Where to store the pointer to the data buffer for the common probe
 *                                  if available.
 */
static PDBGFFLOWTRACERECORDINT dbgfR3FlowTraceRecordCreate(PDBGFFLOWTRACEMODPROBELOC pProbeLoc, VMCPUID idCpu,
                                                           uint8_t **ppbBuf, uint8_t **ppbBufCmn)
{
    PDBGFFLOWTRACEMODINT pTraceMod = pProbeLoc->pTraceMod;
    PCDBGFFLOWTRACEPROBEINT pProbe = pProbeLoc->pProbe;
    PCDBGFFLOWTRACEPROBEINT pProbeCmn = pTraceMod->pProbeCmn;
    size_t cbProbeBuf = pProbe->cbProbe;
    if (pProbeCmn)
        cbProbeBuf += pProbeCmn->cbProbe;

    *ppbBuf = NULL;
    *ppbBufCmn = NULL;

    PDBGFFLOWTRACERECORDINT pRecord = (PDBGFFLOWTRACERECORDINT)MMR3HeapAllocZU(pTraceMod->pUVM, MM_TAG_DBGF_FLOWTRACE,
                                                                               sizeof(DBGFFLOWTRACERECORDINT) + cbProbeBuf);
    if (RT_LIKELY(pRecord))
    {
        DBGFR3FlowTraceProbeRetain(pProbeLoc->pProbe);
        if (pProbeLoc->pTraceMod->pProbeCmn)
            DBGFR3FlowTraceProbeRetain(pProbeLoc->pTraceMod->pProbeCmn);

        pRecord->pProbe         = pProbeLoc->pProbe;
        pRecord->pProbeCmn      = pProbeLoc->pTraceMod->pProbeCmn;
        pRecord->AddrProbe      = pProbeLoc->AddrProbe;
        pRecord->cRefs          = 1;
        pRecord->idCpu          = idCpu;
        pRecord->u64SeqNo       = ASMAtomicIncU64(&pTraceMod->u64SeqNoNext);
        pRecord->u64TsCollected = RTTimeNanoTS();
        pRecord->paValCmn       = NULL;

        *ppbBuf = (uint8_t *)&pRecord->aVal[pProbe->cEntries];

        if (pProbeCmn)
        {
            size_t offValCmn = pProbe->cbProbe - pProbe->cEntries * sizeof(DBGFFLOWTRACEPROBEVAL);
            pRecord->paValCmn = (PDBGFFLOWTRACEPROBEVAL)(*ppbBuf + offValCmn);
            *ppbBufCmn = (uint8_t *)&pRecord->paValCmn[pProbeCmn->cEntries];
        }
    }

    return pRecord;
}


/**
 * Destroys the given record.
 *
 * @returns nothing.
 * @param   pRecord                 The record to destroy.
 */
static void dbgfR3FlowTraceRecordDestroy(PDBGFFLOWTRACERECORDINT pRecord)
{
    DBGFR3FlowTraceProbeRelease(pRecord->pProbe);
    pRecord->pProbe = NULL;
    MMR3HeapFree(pRecord);
}


/**
 * Creates a new flow trace report which can hold the given amount o records.
 *
 * @returns Pointer to the newly created report state or NULL if out of memory.
 * @param   pUVM                    The usermode VM handle.
 * @param   cRecords                Number of records the report shoudld be able to hold.
 */
static PDBGFFLOWTRACEREPORTINT dbgfR3FlowTraceReportCreate(PUVM pUVM, uint32_t cRecords)
{
    PDBGFFLOWTRACEREPORTINT pReport = (PDBGFFLOWTRACEREPORTINT)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_FLOWTRACE,
                                                                               RT_UOFFSETOF_DYN(DBGFFLOWTRACEREPORTINT, apRec[cRecords]));
    if (RT_LIKELY(pReport))
    {
        pReport->pUVM     = pUVM;
        pReport->cRefs    = 1;
        pReport->cRecords = cRecords;
    }

    return pReport;
}


/**
 * Destroys the given report releasing all references hold to the containing records.
 *
 * @returns nothing.
 * @param   pReport                 The report to destroy.
 */
static void dbgfR3FlowTraceReportDestroy(PDBGFFLOWTRACEREPORTINT pReport)
{
    for (uint32_t i = 0; i < pReport->cRecords; i++)
        DBGFR3FlowTraceRecordRelease(pReport->apRec[i]);
    MMR3HeapFree(pReport);
}


/**
 * Queries the given register and returns the value as a guest pointer.
 *
 * @returns VBox status code.
 * @param   pUVM                    The usermode VM handle.
 * @param   idCpu                   VM CPU identifier.
 * @param   pszReg                  The register name to query.
 * @param   pGCPtr                  Where to store the register value on success.
 */
static int dbgfR3FlowTraceModProbeQueryRegAsGCPtr(PUVM pUVM, VMCPUID idCpu, const char *pszReg,
                                                  PRTGCPTR pGCPtr)
{
    DBGFREGVAL Val;
    DBGFREGVALTYPE enmValType;
    int rc = DBGFR3RegNmQuery(pUVM, idCpu, pszReg, &Val, &enmValType);
    if (RT_SUCCESS(rc))
    {
        switch (enmValType)
        {
            case DBGFREGVALTYPE_U8:
                *pGCPtr = Val.u8;
                break;
            case DBGFREGVALTYPE_U16:
                *pGCPtr = Val.u16;
                break;
            case DBGFREGVALTYPE_U32:
                *pGCPtr = Val.u32;
                break;
            case DBGFREGVALTYPE_U64:
                *pGCPtr = Val.u64;
                break;
            case DBGFREGVALTYPE_U128:
            case DBGFREGVALTYPE_R80:
            case DBGFREGVALTYPE_DTR:
            default:
                rc = VERR_INVALID_PARAMETER;
        }
    }

    return rc;
}


/**
 * Resolves the guest address from an indirect memory probe entry.
 *
 * @returns VBox status code.
 * @param   pUVM                    The usermode VM handle.
 * @param   idCpu                   VM CPU identifier.
 * @param   pEntry                  The probe entry.
 * @param   pAddr                   Where to store the address on success.
 */
static int dbgfR3FlowTraceModProbeResolveIndirectAddr(PUVM pUVM, VMCPUID idCpu, PDBGFFLOWTRACEPROBEENTRY pEntry,
                                                      PDBGFADDRESS pAddr)
{
    Assert(pEntry->enmType == DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM);

    RTGCPTR GCPtrBase = 0;
    RTGCPTR GCPtrIndex = 0;
    int rc = dbgfR3FlowTraceModProbeQueryRegAsGCPtr(pUVM, idCpu, pEntry->Type.IndirectMem.RegBase.pszName,
                                                    &GCPtrBase);
    if (   RT_SUCCESS(rc)
        && pEntry->Type.IndirectMem.RegIndex.pszName)
        rc = dbgfR3FlowTraceModProbeQueryRegAsGCPtr(pUVM, idCpu, pEntry->Type.IndirectMem.RegIndex.pszName,
                                                    &GCPtrIndex);
    if (RT_SUCCESS(rc))
    {
        RTGCPTR GCPtr = GCPtrBase + GCPtrIndex * pEntry->Type.IndirectMem.uScale;
        DBGFR3AddrFromFlat(pUVM, pAddr, GCPtr);
        if (pEntry->Type.IndirectMem.iOffset > 0)
            DBGFR3AddrAdd(pAddr, pEntry->Type.IndirectMem.iOffset);
        else if (pEntry->Type.IndirectMem.iOffset < 0)
            DBGFR3AddrSub(pAddr, -pEntry->Type.IndirectMem.iOffset);
    }

    return rc;
}


/**
 * Destroys the given flow trace module freeing all allocated resources.
 *
 * @returns nothing.
 * @param   pThis                   The flow trace module instance data.
 */
static void dbgfR3FlowTraceModDestroy(PDBGFFLOWTRACEMODINT pThis)
{
    if (ASMAtomicReadU32((volatile uint32_t *)&pThis->enmState) == DBGFFLOWTRACEMODSTATE_ENABLED)
    {
        int rc = DBGFR3FlowTraceModDisable(pThis);
        AssertRC(rc);
    }

    Assert(   pThis->enmState == DBGFFLOWTRACEMODSTATE_CREATED
           || pThis->enmState == DBGFFLOWTRACEMODSTATE_VM_DESTROYED);

    /* Do the cleanup under the semaphore. */
    RTSemFastMutexRequest(pThis->hMtx);
    if (pThis->pProbeCmn)
        DBGFR3FlowTraceProbeRelease(pThis->pProbeCmn);

    PDBGFFLOWTRACEMODPROBELOC pIt, pItNext;
    RTListForEachSafe(&pThis->LstProbes, pIt, pItNext, DBGFFLOWTRACEMODPROBELOC, NdProbes)
    {
        RTListNodeRemove(&pIt->NdProbes);
        ASMAtomicDecU32(&pIt->pProbe->cRefsMod);
        DBGFR3FlowTraceProbeRelease(pIt->pProbe);
        MMR3HeapFree(pIt);
    }

    PDBGFFLOWTRACERECORDINT pRecIt, pRecItNext;
    RTListForEachSafe(&pThis->LstRecords, pRecIt, pRecItNext, DBGFFLOWTRACERECORDINT, NdRecord)
    {
        RTListNodeRemove(&pRecIt->NdRecord);
        DBGFR3FlowTraceRecordRelease(pRecIt);
    }

    DBGFR3BpOwnerDestroy(pThis->pUVM, pThis->hBpOwner);
    RTSemFastMutexRelease(pThis->hMtx);
    RTSemFastMutexDestroy(pThis->hMtx);
    MMR3HeapFree(pThis);
}


/**
 * Checks whether the given basic block and address intersect.
 *
 * @returns true if they intersect, false otherwise.
 * @param   pAddr               The address to check for.
 * @param   pAddrStart          The start address.
 * @param   pAddrLast           The last address.
 */
static bool dbgfR3FlowTraceAddrIntersect(PDBGFADDRESS pAddr, PDBGFADDRESS pAddrStart,
                                         PDBGFADDRESS pAddrLast)
{
    return    (pAddrStart->Sel == pAddr->Sel)
           && (pAddrStart->off <= pAddr->off)
           && (pAddrLast->off >= pAddr->off);
}


/**
 * Matches a single value against a given filter value.
 *
 * @returns Flag whether the value matches against the single value.
 * @param   pVal                    The value to match.
 * @param   pValFilter              The value filter to match against.
 */
static bool dbgfR3FlowTraceRecordMatchSingleValue(PCDBGFFLOWTRACEPROBEVAL pVal,
                                                  PCDBGFFLOWTRACEPROBEVAL pValFilter)
{
    if (pVal->pProbeEntry->enmType != pValFilter->pProbeEntry->enmType)
        return false;

    switch (pVal->pProbeEntry->enmType)
    {
        case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
        {
            if (pVal->Type.Reg.enmType != pValFilter->Type.Reg.enmType)
                return false;

            if (strcmp(pVal->Type.Reg.pszName, pValFilter->Type.Reg.pszName))
                return false;

            switch (pVal->Type.Reg.enmType)
            {
                case DBGFREGVALTYPE_U8:
                    if (pVal->Type.Reg.Val.u8 != pValFilter->Type.Reg.Val.u8)
                        return false;
                    break;
                case DBGFREGVALTYPE_U16:
                    if (pVal->Type.Reg.Val.u16 != pValFilter->Type.Reg.Val.u16)
                        return false;
                    break;
                case DBGFREGVALTYPE_U32:
                    if (pVal->Type.Reg.Val.u32 != pValFilter->Type.Reg.Val.u32)
                        return false;
                    break;
                case DBGFREGVALTYPE_U64:
                    if (pVal->Type.Reg.Val.u64 != pValFilter->Type.Reg.Val.u64)
                        return false;
                    break;
                case DBGFREGVALTYPE_U128:
                    if (memcmp(&pVal->Type.Reg.Val.u128, &pValFilter->Type.Reg.Val.u128,
                               sizeof(RTUINT128U)))
                        return false;
                    break;
                case DBGFREGVALTYPE_R80:
                    if (memcmp(&pVal->Type.Reg.Val.r80Ex, &pValFilter->Type.Reg.Val.r80Ex,
                               sizeof(RTFLOAT80U2)))
                        return false;
                    break;
                case DBGFREGVALTYPE_DTR:
                    if (   pVal->Type.Reg.Val.dtr.u64Base != pValFilter->Type.Reg.Val.dtr.u64Base
                        || pVal->Type.Reg.Val.dtr.u32Limit != pValFilter->Type.Reg.Val.dtr.u32Limit)
                        return false;
                    break;
                default:
                    AssertFailed();
                    return false;
            }
            break;
        }
        case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
        case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
            if (   memcmp(&pVal->Type.Mem.Addr, &pValFilter->Type.Mem.Addr,
                          sizeof(DBGFADDRESS))
                || pVal->Type.Mem.cbBuf != pValFilter->Type.Mem.cbBuf
                || memcmp(pVal->Type.Mem.pvBuf, pValFilter->Type.Mem.pvBuf,
                          pValFilter->Type.Mem.cbBuf))
                return false;
            break;
        default:
            AssertFailed();
            return false;
    }

    return true;
}


/**
 * Matches the given values against the filter values returning a flag whether they match.
 *
 * @returns Flag whether the given values match the filter.
 * @param   paVal                   Pointer to the array of values.
 * @param   cVals                   Number of values in the array.
 * @param   paValFilter             Pointer to the filter values.
 * @param   cValsFilter             Number of entries in the filter values.
 */
static bool dbgfR3FlowTraceRecordMatchValues(PCDBGFFLOWTRACEPROBEVAL paVal, uint32_t cVals,
                                             PCDBGFFLOWTRACEPROBEVAL paValFilter, uint32_t cValsFilter)
{
    bool fMatch = false;

    /*
     * The order in which the filters and values are doesn't need to match but for every filter
     * there should be at least one entry matching.
     */
    while (   cValsFilter-- > 0
           && fMatch)
    {
        for (uint32_t i = 0; i < cVals; i++)
        {
            fMatch = dbgfR3FlowTraceRecordMatchSingleValue(&paVal[i], paValFilter);
            if (fMatch)
                break;
        }
        paValFilter++;
    }

    return fMatch;
}


/**
 * Checks the given record against the given filter, returning whether the filter
 * matches.
 *
 * @returns Flag whether the record matches the given filter.
 * @param   pRecord                 The record to check.
 * @param   pFilter                 The filter to check against.
 */
static bool dbgfR3FlowTraceRecordMatchSingleFilter(PDBGFFLOWTRACERECORDINT pRecord,
                                                   PDBGFFLOWTRACEREPORTFILTER pFilter)
{
    bool fMatch = false;

    switch (pFilter->enmType)
    {
        case DBGFFLOWTRACEREPORTFILTERTYPE_SEQ_NUM:
        {
            if (   pRecord->u64SeqNo >= pFilter->Type.SeqNo.u64SeqNoFirst
                && pRecord->u64SeqNo <= pFilter->Type.SeqNo.u64SeqNoLast)
                fMatch = true;
            break;
        }
        case DBGFFLOWTRACEREPORTFILTERTYPE_TIMESTAMP:
        {
            if (   pRecord->u64TsCollected >= pFilter->Type.Timestamp.u64TsFirst
                && pRecord->u64TsCollected <= pFilter->Type.Timestamp.u64TsLast)
                fMatch = true;
            break;
        }
        case DBGFFLOWTRACEREPORTFILTERTYPE_ADDR:
        {
            if (dbgfR3FlowTraceAddrIntersect(&pRecord->AddrProbe,
                                             &pFilter->Type.Addr.AddrStart,
                                             &pFilter->Type.Addr.AddrLast))
                fMatch = true;
            break;
        }
        case DBGFFLOWTRACEREPORTFILTERTYPE_VMCPU_ID:
        {
            if (   pRecord->idCpu >= pFilter->Type.VCpuId.idCpuStart
                && pRecord->idCpu <= pFilter->Type.VCpuId.idCpuLast)
                fMatch = true;
            break;
        }
        case DBGFFLOWTRACEREPORTFILTERTYPE_PROBE_DATA:
        {
            if (pFilter->Type.ProbeData.fValCmn)
            {
                if (pRecord->paValCmn)
                {
                    PCDBGFFLOWTRACEPROBEINT pProbeCmn = pRecord->pProbeCmn;
                    AssertPtr(pProbeCmn);

                    fMatch = dbgfR3FlowTraceRecordMatchValues(pRecord->paValCmn, pProbeCmn->cEntries,
                                                              pFilter->Type.ProbeData.paVal,
                                                              pFilter->Type.ProbeData.cVals);
                }
            }
            else
                fMatch = dbgfR3FlowTraceRecordMatchValues(&pRecord->aVal[0], pRecord->pProbe->cEntries,
                                                          pFilter->Type.ProbeData.paVal, pFilter->Type.ProbeData.cVals);
            break;
        }
        default:
            AssertMsgFailed(("Invalid filter type %u!\n", pFilter->enmType));
    }

    return fMatch;
}


/**
 * Checks the given record against the given filters.
 *
 * @returns Flag whether the record matches the filters.
 * @param   pRecord                 The record to check.
 * @param   paFilters               Array of filters to check.
 * @param   cFilters                Number of filters in the array.
 * @param   enmOp                   How the record should match against the filters.
 */
static bool dbgfR3FlowTraceDoesRecordMatchFilter(PDBGFFLOWTRACERECORDINT pRecord,
                                                 PDBGFFLOWTRACEREPORTFILTER paFilters,
                                                 uint32_t cFilters, DBGFFLOWTRACEREPORTFILTEROP enmOp)
{
    bool fMatch = false;

    if (enmOp == DBGFFLOWTRACEREPORTFILTEROP_AND)
    {
        fMatch = true;
        while (cFilters-- > 0)
        {
            if (!dbgfR3FlowTraceRecordMatchSingleFilter(pRecord, &paFilters[cFilters]))
            {
                fMatch = false;
                break;
            }
        }
    }
    else if (enmOp == DBGFFLOWTRACEREPORTFILTEROP_OR)
    {
        while (cFilters-- > 0)
        {
            if (dbgfR3FlowTraceRecordMatchSingleFilter(pRecord, &paFilters[cFilters]))
            {
                fMatch = true;
                break;
            }
        }
    }
    else
        AssertMsgFailed(("Invalid filter operation %u!\n", enmOp));

    return fMatch;
}


/**
 * Collects all the data specified in the given probe.
 *
 * @returns Flag whether to enter the debugger.
 * @param   pUVM                    The user mode VM handle.
 * @param   idCpu                   The virtual CPU ID.
 * @param   pTraceMod               The trace module instance.
 * @param   pAddrProbe              Location of the probe, NULL if a common probe.
 * @param   pProbe                  The probe instance.
 * @param   pVal                    Pointer to the array of values to fill.
 * @param   pbBuf                   Poitner to the memory buffer holding additional data.
 */
static bool dbgfR3FlowTraceModProbeCollectData(PUVM pUVM, VMCPUID idCpu,
                                               PDBGFFLOWTRACEMODINT pTraceMod,
                                               PCDBGFADDRESS pAddrProbe,
                                               PDBGFFLOWTRACEPROBEINT pProbe,
                                               PDBGFFLOWTRACEPROBEVAL pVal, uint8_t *pbBuf)
{
    bool fDbgDefer = false;

    for (uint32_t i = 0; i < pProbe->cEntries; i++)
    {
        int rc;
        PDBGFFLOWTRACEPROBEENTRY pEntry = &pProbe->paEntries[i];

        pVal->pProbeEntry = pEntry;

        switch (pEntry->enmType)
        {
            case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
                rc = DBGFR3RegNmQuery(pUVM, idCpu, pEntry->Type.Reg.pszName,
                                      &pVal->Type.Reg.Val, &pVal->Type.Reg.enmType);
                AssertRC(rc);
                pVal->Type.Reg.pszName = pEntry->Type.Reg.pszName;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
            {
                DBGFADDRESS Addr;
                rc = dbgfR3FlowTraceModProbeResolveIndirectAddr(pUVM, idCpu, pEntry, &Addr);
                if (RT_SUCCESS(rc))
                {
                    pVal->Type.Mem.pvBuf = pbBuf;
                    pVal->Type.Mem.cbBuf = pEntry->Type.IndirectMem.cbMem;
                    pVal->Type.Mem.Addr  = Addr;
                    rc = DBGFR3MemRead(pUVM, idCpu, &pVal->Type.Mem.Addr, pbBuf,
                                       pVal->Type.Mem.cbBuf);
                    AssertRC(rc);
                    pbBuf += pVal->Type.Mem.cbBuf;
                }
                break;
            }
            case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
                pVal->Type.Mem.pvBuf = pbBuf;
                pVal->Type.Mem.cbBuf = pEntry->Type.ConstMem.cbMem;
                pVal->Type.Mem.Addr  = pEntry->Type.ConstMem.AddrMem;
                rc = DBGFR3MemRead(pUVM, idCpu, &pVal->Type.Mem.Addr, pbBuf,
                                   pVal->Type.Mem.cbBuf);
                AssertRC(rc);
                pbBuf += pVal->Type.Mem.cbBuf;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_CALLBACK:
                rc = pEntry->Type.Callback.pfnCallback(pUVM, idCpu, pTraceMod,
                                                       pAddrProbe, pProbe, pEntry,
                                                       pEntry->Type.Callback.pvUser);
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_DEBUGGER:
                fDbgDefer = true;
                break;
            default:
                AssertFailed();
        }

        pVal++;
    }

    return fDbgDefer;
}


/**
 * @callback_method_impl{FNDBGFBPHIT}
 */
static DECLCALLBACK(VBOXSTRICTRC) dbgfR3FlowTraceModProbeFiredWorker(PVM pVM, VMCPUID idCpu, void *pvUserBp, DBGFBP hBp, PCDBGFBPPUB pBpPub, uint16_t fFlags)
{
    RT_NOREF(pVM, hBp, pBpPub, fFlags);
    LogFlowFunc(("pVM=%#p idCpu=%u pvUserBp=%#p hBp=%#x pBpPub=%p\n",
                 pVM, idCpu, pvUserBp, hBp, pBpPub));

    PDBGFFLOWTRACEMODPROBELOC pProbeLoc = (PDBGFFLOWTRACEMODPROBELOC)pvUserBp;
    PDBGFFLOWTRACEPROBEINT pProbe = pProbeLoc->pProbe;
    PDBGFFLOWTRACEMODINT pTraceMod = pProbeLoc->pTraceMod;
    bool fDisabledModule = false;
    bool fDbgDefer = false;

    /* Check whether the trace module is still active and we are tracing the correct VCPU. */
    if (ASMAtomicReadU32((volatile uint32_t *)&pTraceMod->enmState) != DBGFFLOWTRACEMODSTATE_ENABLED
        || (   idCpu != pTraceMod->idCpu
            && pTraceMod->idCpu != VMCPUID_ANY))
        return VINF_SUCCESS;

    if (   pTraceMod->fLimit
        && ASMAtomicReadU32(&pTraceMod->cHitsLeft))
    {
        uint32_t cHitsLeftNew = ASMAtomicDecU32(&pTraceMod->cHitsLeft);
        if (cHitsLeftNew > cHitsLeftNew + 1) /* Underflow => reached the limit. */
        {
            ASMAtomicIncU32(&pTraceMod->cHitsLeft);
            return VINF_SUCCESS;
        }

        if (!cHitsLeftNew)
        {
            /* We got the last record, disable the trace module. */
            fDisabledModule = ASMAtomicCmpXchgU32((volatile uint32_t *)&pTraceMod->enmState, DBGFFLOWTRACEMODSTATE_CREATED,
                                                  DBGFFLOWTRACEMODSTATE_ENABLED);
        }
    }

    uint8_t *pbBuf = NULL;
    uint8_t *pbBufCmn = NULL;
    PDBGFFLOWTRACERECORDINT pRecord = dbgfR3FlowTraceRecordCreate(pProbeLoc, idCpu, &pbBuf, &pbBufCmn);
    if (pRecord)
    {
        fDbgDefer = dbgfR3FlowTraceModProbeCollectData(pTraceMod->pUVM, idCpu, pTraceMod, &pProbeLoc->AddrProbe, pProbe,
                                                       &pRecord->aVal[0], pbBuf);
        if (pTraceMod->pProbeCmn)
            fDbgDefer = dbgfR3FlowTraceModProbeCollectData(pTraceMod->pUVM, idCpu, pTraceMod, NULL, pTraceMod->pProbeCmn,
                                                           pRecord->paValCmn, pbBufCmn);

        RTSemFastMutexRequest(pTraceMod->hMtx);
        uint32_t cRecordsNew = ASMAtomicIncU32(&pTraceMod->cRecords);
        RTListAppend(&pTraceMod->LstRecords, &pRecord->NdRecord);
        if (   (cRecordsNew > pTraceMod->cRecordsMax)
            && pTraceMod->cRecordsMax > 0)
        {
            /* Get the first record and destroy it. */
            pRecord = RTListRemoveFirst(&pTraceMod->LstRecords, DBGFFLOWTRACERECORDINT, NdRecord);
            AssertPtr(pRecord);
            DBGFR3FlowTraceRecordRelease(pRecord);
            ASMAtomicDecU32(&pTraceMod->cRecords);
        }
        RTSemFastMutexRelease(pTraceMod->hMtx);
    }

    if (fDisabledModule)
    {
        int rc = DBGFR3FlowTraceModDisable(pTraceMod);
        AssertRC(rc);
    }

    return fDbgDefer ? VINF_DBGF_BP_HALT : VINF_SUCCESS;
}


/**
 * Worker for DBGFR3FlowTraceModEnable(), doing the work in an EMT rendezvous point to
 * ensure no probe is hit in an inconsistent state.
 *
 * @returns Strict VBox status code.
 * @param   pVM                     The VM instance data.
 * @param   pVCpu                   The virtual CPU we execute on.
 * @param   pvUser                  Opaque user data.
 */
static DECLCALLBACK(VBOXSTRICTRC) dbgfR3FlowTraceModEnableWorker(PVM pVM, PVMCPU pVCpu, void *pvUser)
{
    RT_NOREF2(pVM, pVCpu);
    PDBGFFLOWTRACEMODINT pThis = (PDBGFFLOWTRACEMODINT)pvUser;
    PDBGFFLOWTRACEMODPROBELOC pProbeLoc = NULL;
    int rc = VINF_SUCCESS;

    pThis->enmState = DBGFFLOWTRACEMODSTATE_ENABLED;

    RTListForEach(&pThis->LstProbes, pProbeLoc, DBGFFLOWTRACEMODPROBELOC, NdProbes)
    {
        uint16_t fBpFlags = DBGF_BP_F_ENABLED;

        if (pProbeLoc->fFlags & DBGF_FLOW_TRACE_PROBE_ADD_F_BEFORE_EXEC)
            fBpFlags |= DBGF_BP_F_HIT_EXEC_BEFORE;
        if (pProbeLoc->fFlags & DBGF_FLOW_TRACE_PROBE_ADD_F_AFTER_EXEC)
            fBpFlags |= DBGF_BP_F_HIT_EXEC_AFTER;

        rc = DBGFR3BpSetInt3Ex(pThis->pUVM, pThis->hBpOwner, pProbeLoc,
                               0 /*idSrcCpu*/, &pProbeLoc->AddrProbe, fBpFlags,
                               0 /*iHitTrigger*/, ~0ULL /*iHitDisable*/, &pProbeLoc->hBp);
        if (RT_FAILURE(rc))
            break;
    }

    if (RT_FAILURE(rc))
        pThis->enmState = DBGFFLOWTRACEMODSTATE_CREATED;

    return rc;
}


/**
 * Worker for DBGFR3FlowTraceModDisable(), doing the work in an EMT rendezvous point to
 * ensure no probe is hit in an inconsistent state.
 *
 * @returns Struct VBox status code.
 * @param   pVM                     The VM instance data.
 * @param   pVCpu                   The virtual CPU we execute on.
 * @param   pvUser                  Opaque user data.
 */
static DECLCALLBACK(VBOXSTRICTRC) dbgfR3FlowTraceModDisableWorker(PVM pVM, PVMCPU pVCpu, void *pvUser)
{
    RT_NOREF2(pVM, pVCpu);
    PDBGFFLOWTRACEMODINT pThis = (PDBGFFLOWTRACEMODINT)pvUser;
    PDBGFFLOWTRACEMODPROBELOC pProbeLoc = NULL;
    int rc = VINF_SUCCESS;

    pThis->enmState = DBGFFLOWTRACEMODSTATE_CREATED;

    RTListForEach(&pThis->LstProbes, pProbeLoc, DBGFFLOWTRACEMODPROBELOC, NdProbes)
    {
        rc = DBGFR3BpClear(pThis->pUVM, pProbeLoc->hBp);
        AssertRC(rc);
    }

    return rc;
}


/**
 * Checks whether both addresses are equal.
 *
 * @returns true if both addresses point to the same location, false otherwise.
 * @param   pAddr1              First address.
 * @param   pAddr2              Second address.
 */
static bool dbgfR3FlowTraceAddrEqual(PCDBGFADDRESS pAddr1, PCDBGFADDRESS pAddr2)
{
    return    pAddr1->Sel == pAddr2->Sel
           && pAddr1->off == pAddr2->off;
}


/**
 * Returns the probe location pointer at the given address for the given trace module.
 *
 * @returns Pointer to the probe location or NULL if there is no probe at the given location.
 * @param   pThis                   The flow trace module instance data.
 * @param   pAddrProbe              Address of the probe to check.
 */
static PDBGFFLOWTRACEMODPROBELOC dbgfR3TraceModGetProbeLocAtAddr(PDBGFFLOWTRACEMODINT pThis, PCDBGFADDRESS pAddrProbe)
{
    RTSemFastMutexRequest(pThis->hMtx);

    PDBGFFLOWTRACEMODPROBELOC pIt;
    RTListForEach(&pThis->LstProbes, pIt, DBGFFLOWTRACEMODPROBELOC, NdProbes)
    {
        if (dbgfR3FlowTraceAddrEqual(&pIt->AddrProbe, pAddrProbe))
        {
            RTSemFastMutexRelease(pThis->hMtx);
            return pIt;
        }
    }
    RTSemFastMutexRelease(pThis->hMtx);
    return NULL;
}


/**
 * Cleans up any allocated resources for each entry in the given probe for the given range.
 *
 * @returns nothing.
 * @param   pProbe                  The probe instance.
 * @param   idxStart                Start index to clean up.
 * @param   cEntries                How many entries to clean up.
 */
static void dbgfR3ProbeEntryCleanup(PDBGFFLOWTRACEPROBEINT pProbe, uint32_t idxStart, uint32_t cEntries)
{
    AssertReturnVoid(pProbe->cEntriesMax >= idxStart + cEntries);

    for (uint32_t i = idxStart; i < idxStart + cEntries; i++)
    {
        PDBGFFLOWTRACEPROBEENTRY pEntry = &pProbe->paEntries[i];

        switch (pEntry->enmType)
        {
            case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
                if (pEntry->Type.Reg.pszName)
                    MMR3HeapFree((void *)pEntry->Type.Reg.pszName);
                pEntry->Type.Reg.pszName = NULL;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
                pEntry->Type.ConstMem.cbMem = 0;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
                pEntry->Type.IndirectMem.uScale = 0;
                pEntry->Type.IndirectMem.cbMem  = 0;
                if (pEntry->Type.IndirectMem.RegBase.pszName)
                    MMR3HeapFree((void *)pEntry->Type.IndirectMem.RegBase.pszName);
                if (pEntry->Type.IndirectMem.RegIndex.pszName)
                    MMR3HeapFree((void *)pEntry->Type.IndirectMem.RegIndex.pszName);
                pEntry->Type.IndirectMem.RegBase.pszName = NULL;
                pEntry->Type.IndirectMem.RegIndex.pszName = NULL;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_CALLBACK:
                pEntry->Type.Callback.pfnCallback = NULL;
                pEntry->Type.Callback.pvUser      = NULL;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_DEBUGGER:
                break;
            default:
                AssertFailed();
        }
    }
}


/**
 * Destroys the given flow trace probe freeing all allocated resources.
 *
 * @returns nothing.
 * @param   pProbe                  The flow trace probe instance data.
 */
static void dbgfR3FlowTraceProbeDestroy(PDBGFFLOWTRACEPROBEINT pProbe)
{
    dbgfR3ProbeEntryCleanup(pProbe, 0, pProbe->cEntries);
    MMR3HeapFree(pProbe->paEntries);
    MMR3HeapFree(pProbe);
}


/**
 * Ensures that the given probe has the given amount of additional entries available,
 * increasing the size if necessary.
 *
 * @returns VBox status code.
 * @retval  VERR_NO_MEMORY if increasing the size failed due to an out of memory condition.
 * @param   pProbe                  The probe insatnce.
 * @param   cEntriesAdd             Number of additional entries required.
 */
static int dbgfR3ProbeEnsureSize(PDBGFFLOWTRACEPROBEINT pProbe, uint32_t cEntriesAdd)
{
    uint32_t cEntriesNew = pProbe->cEntries + cEntriesAdd;
    int rc = VINF_SUCCESS;

    if (pProbe->cEntriesMax < cEntriesNew)
    {
        PDBGFFLOWTRACEPROBEENTRY paEntriesNew;
        if (!pProbe->cEntriesMax)
            paEntriesNew = (PDBGFFLOWTRACEPROBEENTRY)MMR3HeapAllocZU(pProbe->pUVM, MM_TAG_DBGF_FLOWTRACE,
                                                                     cEntriesNew * sizeof(DBGFFLOWTRACEPROBEENTRY));
        else
            paEntriesNew = (PDBGFFLOWTRACEPROBEENTRY)MMR3HeapRealloc(pProbe->paEntries,
                                                                     cEntriesNew * sizeof(DBGFFLOWTRACEPROBEENTRY));
        if (RT_LIKELY(paEntriesNew))
        {
            pProbe->paEntries   = paEntriesNew;
            pProbe->cEntriesMax = cEntriesNew;
        }
        else
            rc = VERR_NO_MEMORY;
    }

    return rc;
}


/**
 * Duplicates a probe registry entry.
 * @returns VBox status code.
 * @param   pUVM                    The usermode VM handle.
 * @param   pDst                    Where to copy the entry to.
 * @param   pSrc                    What to copy.
 */
static int dbgfR3ProbeEntryRegDup(PUVM pUVM, PDBGFFLOWTRACEPROBEENTRYREG pDst, PCDBGFFLOWTRACEPROBEENTRYREG pSrc)
{
    int rc = VINF_SUCCESS;

    pDst->enmType = pSrc->enmType;
    pDst->pszName = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_FLOWTRACE, pSrc->pszName);
    if (!pDst->pszName)
        rc = VERR_NO_MEMORY;

    return rc;
}


/**
 * Duplicates a given probe entry in the given destination doing a deep copy (strings are duplicated).
 *
 * @returns VBox status code.
 * @param   pUVM                    The usermode VM handle.
 * @param   pDst                    Where to copy the entry to.
 * @param   pSrc                    What to copy.
 */
static int dbgfR3ProbeEntryDup(PUVM pUVM, PDBGFFLOWTRACEPROBEENTRY pDst, PCDBGFFLOWTRACEPROBEENTRY pSrc)
{
    int rc = VINF_SUCCESS;

    pDst->enmType = pSrc->enmType;
    pDst->pszDesc = NULL;
    if (pSrc->pszDesc)
    {
        pDst->pszDesc = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_FLOWTRACE, pSrc->pszDesc);
        if (!pDst->pszDesc)
            rc = VERR_NO_MEMORY;
    }

    if (RT_SUCCESS(rc))
    {
        switch (pDst->enmType)
        {
            case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
                rc = dbgfR3ProbeEntryRegDup(pUVM, &pDst->Type.Reg, &pSrc->Type.Reg);
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
                pDst->Type.ConstMem.AddrMem = pSrc->Type.ConstMem.AddrMem;
                pDst->Type.ConstMem.cbMem   = pSrc->Type.ConstMem.cbMem;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
                pDst->Type.IndirectMem.uScale  = pSrc->Type.IndirectMem.uScale;
                pDst->Type.IndirectMem.cbMem   = pSrc->Type.IndirectMem.cbMem;
                pDst->Type.IndirectMem.iOffset = pSrc->Type.IndirectMem.iOffset;
                rc = dbgfR3ProbeEntryRegDup(pUVM, &pDst->Type.IndirectMem.RegBase, &pSrc->Type.IndirectMem.RegBase);
                if (   RT_SUCCESS(rc)
                    && pDst->Type.IndirectMem.RegIndex.pszName)
                {
                    rc = dbgfR3ProbeEntryRegDup(pUVM, &pDst->Type.IndirectMem.RegIndex, &pSrc->Type.IndirectMem.RegIndex);
                    if (RT_FAILURE(rc))
                        MMR3HeapFree((void *)pDst->Type.IndirectMem.RegBase.pszName);
                }
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_CALLBACK:
                pDst->Type.Callback.pfnCallback = pSrc->Type.Callback.pfnCallback;
                pDst->Type.Callback.pvUser      = pSrc->Type.Callback.pvUser;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_DEBUGGER:
                break;
            default:
                rc = VERR_INVALID_PARAMETER;
        }
    }

    if (   RT_FAILURE(rc)
        && pDst->pszDesc)
    {
        MMR3HeapFree((void *)pDst->pszDesc);
        pDst->pszDesc = NULL;
    }

    return rc;
}


/**
 * Recalculates the size occupied by the data of this probe for each invocation.
 *
 * @returns nothing.
 * @param   pProbe                  The probe instance.
 */
static void dbgfR3ProbeRecalcSize(PDBGFFLOWTRACEPROBEINT pProbe)
{
    size_t cbProbe = 0;

    for (uint32_t i = 0; i < pProbe->cEntries; i++)
    {
        PDBGFFLOWTRACEPROBEENTRY pEntry = &pProbe->paEntries[i];

        cbProbe += sizeof(DBGFFLOWTRACEPROBEVAL);

        switch (pEntry->enmType)
        {
            case DBGFFLOWTRACEPROBEENTRYTYPE_CONST_MEM:
                cbProbe += pEntry->Type.ConstMem.cbMem;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_INDIRECT_MEM:
                cbProbe += pEntry->Type.IndirectMem.cbMem;
                break;
            case DBGFFLOWTRACEPROBEENTRYTYPE_CALLBACK:
            case DBGFFLOWTRACEPROBEENTRYTYPE_REG:
            case DBGFFLOWTRACEPROBEENTRYTYPE_DEBUGGER:
                break;
            default:
                AssertFailed();
        }
    }

    pProbe->cbProbe = cbProbe;
}


/**
 * Creates a new empty flow trace module.
 *
 * @returns VBox status code.
 * @param   pUVM                    The usermode VM handle.
 * @param   idCpu                   CPU ID the module is for, use VMCPUID_ANY for any CPU.
 * @param   hFlowTraceProbeCommon   Optional probe handle of data to capture regardless of the actual
 *                                  probe.
 * @param   phFlowTraceMod          Where to store the handle to the created module on success.
 */
VMMR3DECL(int) DBGFR3FlowTraceModCreate(PUVM pUVM, VMCPUID idCpu,
                                        DBGFFLOWTRACEPROBE hFlowTraceProbeCommon,
                                        PDBGFFLOWTRACEMOD phFlowTraceMod)
{
    int rc = VINF_SUCCESS;
    PDBGFFLOWTRACEMODINT pThis = (PDBGFFLOWTRACEMODINT)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_FLOWTRACE,
                                                                       sizeof(DBGFFLOWTRACEMODINT));
    if (RT_LIKELY(pThis))
    {
        pThis->cRefs        = 1;
        pThis->pUVM         = pUVM;
        pThis->idCpu        = idCpu;
        pThis->enmState     = DBGFFLOWTRACEMODSTATE_CREATED;
        pThis->u64SeqNoNext = 0;
        pThis->cHitsLeft    = 0;
        pThis->cRecordsMax  = 0;
        pThis->cRecords     = 0;
        pThis->cProbes      = 0;
        RTListInit(&pThis->LstProbes);
        RTListInit(&pThis->LstRecords);

        rc = RTSemFastMutexCreate(&pThis->hMtx);
        if (RT_SUCCESS(rc))
        {
            rc = DBGFR3BpOwnerCreate(pUVM, dbgfR3FlowTraceModProbeFiredWorker, NULL /*pfnBpIoHit*/, &pThis->hBpOwner);
            if (RT_SUCCESS(rc))
            {
                PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbeCommon;
                if (pProbe)
                {
                    DBGFR3FlowTraceProbeRetain(pProbe);
                    pThis->pProbeCmn = pProbe;
                }
            }

            *phFlowTraceMod = pThis;
        }

        if (RT_FAILURE(rc))
            MMR3HeapFree(pThis);
    }
    else
        rc = VERR_NO_MEMORY;

    return rc;
}


/**
 * Create a new flow trace module from the given control flow graph adding the given probes
 * at the entries, exits and branches.
 *
 * @returns VBox status code.
 * @param   pUVM                    The usermode VM handle.
 * @param   idCpu                   CPU ID the module is for, use VMCPUID_ANY for any CPU.
 * @param   hFlow                   Control flow graph handle to use.
 * @param   hFlowTraceProbeCommon   Optional probe handle of data to capture regardless of the actual
 *                                  probe.
 * @param   hFlowTraceProbeEntry    Probe handle to use for all entry blocks.
 * @param   hFlowTraceProbeRegular  Probe handle to use for all branches.
 * @param   hFlowTraceProbeExit     Probe handle to use for all exits.
 * @param   phFlowTraceMod          Where to store the handle to the created module on success.
 */
VMMR3DECL(int) DBGFR3FlowTraceModCreateFromFlowGraph(PUVM pUVM, VMCPUID idCpu, DBGFFLOW hFlow,
                                                     DBGFFLOWTRACEPROBE hFlowTraceProbeCommon,
                                                     DBGFFLOWTRACEPROBE hFlowTraceProbeEntry,
                                                     DBGFFLOWTRACEPROBE hFlowTraceProbeRegular,
                                                     DBGFFLOWTRACEPROBE hFlowTraceProbeExit,
                                                     PDBGFFLOWTRACEMOD phFlowTraceMod)
{
    DBGFFLOWIT hFlowIt;
    int rc = DBGFR3FlowItCreate(hFlow, DBGFFLOWITORDER_BY_ADDR_LOWEST_FIRST, &hFlowIt);
    if (RT_SUCCESS(rc))
    {
        DBGFFLOWTRACEMOD hFlowTraceMod;
        rc = DBGFR3FlowTraceModCreate(pUVM, idCpu, hFlowTraceProbeCommon, &hFlowTraceMod);
        if (RT_SUCCESS(rc))
        {
            DBGFFLOWBB hFlowBb = DBGFR3FlowItNext(hFlowIt);
            while (hFlowBb && RT_SUCCESS(rc))
            {
                uint32_t fFlags = DBGFR3FlowBbGetFlags(hFlowBb);

                if (!(fFlags & (DBGF_FLOW_BB_F_EMPTY | DBGF_FLOW_BB_F_INCOMPLETE_ERR)))
                {
                    DBGFADDRESS AddrInstr;

                    if (fFlags & DBGF_FLOW_BB_F_ENTRY)
                    {
                        rc = DBGFR3FlowBbQueryInstr(hFlowBb, 0, &AddrInstr, NULL, NULL);
                        AssertRC(rc);

                        rc = DBGFR3FlowTraceModAddProbe(hFlowTraceMod, &AddrInstr, hFlowTraceProbeEntry,
                                                        DBGF_FLOW_TRACE_PROBE_ADD_F_BEFORE_EXEC);
                    }
                    else
                    {
                        DBGFFLOWBBENDTYPE enmType = DBGFR3FlowBbGetType(hFlowBb);
                        uint32_t cInstr = enmType == DBGFFLOWBBENDTYPE_EXIT ? DBGFR3FlowBbGetInstrCount(hFlowBb) - 1 : 0;
                        rc = DBGFR3FlowBbQueryInstr(hFlowBb, cInstr, &AddrInstr, NULL, NULL);
                        if (RT_SUCCESS(rc))
                        {
                            if (enmType == DBGFFLOWBBENDTYPE_EXIT)
                                rc = DBGFR3FlowTraceModAddProbe(hFlowTraceMod, &AddrInstr, hFlowTraceProbeExit,
                                                                DBGF_FLOW_TRACE_PROBE_ADD_F_AFTER_EXEC);
                            else
                                rc = DBGFR3FlowTraceModAddProbe(hFlowTraceMod, &AddrInstr, hFlowTraceProbeRegular,
                                                                DBGF_FLOW_TRACE_PROBE_ADD_F_BEFORE_EXEC);
                        }
                    }
                }

                hFlowBb = DBGFR3FlowItNext(hFlowIt);
            }

            if (RT_FAILURE(rc))
                DBGFR3FlowTraceModRelease(hFlowTraceMod);
            else
                *phFlowTraceMod = hFlowTraceMod;
        }

        DBGFR3FlowItDestroy(hFlowIt);
    }

    return rc;
}


/**
 * Retain a reference to the given flow trace module.
 *
 * @returns New reference count.
 * @param   hFlowTraceMod           Flow trace module handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceModRetain(DBGFFLOWTRACEMOD hFlowTraceMod)
{
    PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
    AssertPtrReturn(pThis, UINT32_MAX);

    uint32_t cRefs = ASMAtomicIncU32(&pThis->cRefs);
    AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pThis));
    return cRefs;
}


/**
 * Release a reference of the given flow trace module.
 *
 * @returns New reference count, on 0 the module is destroyed and all containing records
 *          are deleted.
 * @param   hFlowTraceMod           Flow trace module handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceModRelease(DBGFFLOWTRACEMOD hFlowTraceMod)
{
    PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
    if (!pThis)
        return 0;
    AssertPtrReturn(pThis, UINT32_MAX);

    uint32_t cRefs = ASMAtomicDecU32(&pThis->cRefs);
    AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pThis));
    if (cRefs == 0)
        dbgfR3FlowTraceModDestroy(pThis);
    return cRefs;
}


/**
 * Enables and arms all probes in the given flow trace module.
 *
 * @returns VBox status code.
 * @param   hFlowTraceMod           Flow trace module handle.
 * @param   cHits                   Number of hits inside this module until the module is disabled
 *                                  automatically, 0 if not to disable automatically.
 * @param   cRecordsMax             Maximum number of records to keep until the oldest is evicted.
 */
VMMR3DECL(int) DBGFR3FlowTraceModEnable(DBGFFLOWTRACEMOD hFlowTraceMod, uint32_t cHits, uint32_t cRecordsMax)
{
    PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->cProbes > 0, VERR_INVALID_STATE);
    AssertReturn(pThis->enmState == DBGFFLOWTRACEMODSTATE_CREATED, VERR_INVALID_STATE);

    pThis->cHitsLeft   = cHits;
    pThis->cRecordsMax = cRecordsMax;

    return VMMR3EmtRendezvous(pThis->pUVM->pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
                              dbgfR3FlowTraceModEnableWorker, pThis);
}


/**
 * Disables all probes in the given flow trace module.
 *
 * @returns VBox status code.
 * @param   hFlowTraceMod           Flow trace module handle.
 */
VMMR3DECL(int) DBGFR3FlowTraceModDisable(DBGFFLOWTRACEMOD hFlowTraceMod)
{
    PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertReturn(pThis->enmState == DBGFFLOWTRACEMODSTATE_ENABLED, VERR_INVALID_STATE);

    return VMMR3EmtRendezvous(pThis->pUVM->pVM, VMMEMTRENDEZVOUS_FLAGS_TYPE_ONCE,
                              dbgfR3FlowTraceModDisableWorker, pThis);
}


/**
 * Returns a report containing all existing records in the given flow trace module.
 *
 * @returns VBox status code.
 * @param   hFlowTraceMod           Flow trace module handle.
 * @param   phFlowTraceReport       Where to store the flow trace report handle on success.
 */
VMMR3DECL(int) DBGFR3FlowTraceModQueryReport(DBGFFLOWTRACEMOD hFlowTraceMod,
                                             PDBGFFLOWTRACEREPORT phFlowTraceReport)
{
    PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertPtrReturn(phFlowTraceReport, VERR_INVALID_POINTER);

    /** @todo Locking. */
    int rc = VINF_SUCCESS;
    PDBGFFLOWTRACEREPORTINT pReport = dbgfR3FlowTraceReportCreate(pThis->pUVM, pThis->cRecords);
    if (RT_LIKELY(pReport))
    {
        PDBGFFLOWTRACERECORDINT pIt;
        uint32_t idx = 0;

        RTSemFastMutexRequest(pThis->hMtx);
        RTListForEach(&pThis->LstRecords, pIt, DBGFFLOWTRACERECORDINT, NdRecord)
        {
            DBGFR3FlowTraceRecordRetain(pIt);
            pReport->apRec[idx++] = pIt;
        }
        RTSemFastMutexRelease(pThis->hMtx);

        *phFlowTraceReport = pReport;
    }
    else
        rc = VERR_NO_MEMORY;

    return rc;
}


/**
 * Clears all records contained in the flow trace module.
 *
 * @returns VBox status code.
 * @param   hFlowTraceMod           Flow trace module handle.
 */
VMMR3DECL(int) DBGFR3FlowTraceModClear(DBGFFLOWTRACEMOD hFlowTraceMod)
{
    PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);

    RTSemFastMutexRequest(pThis->hMtx);
    RTLISTANCHOR LstTmp;
    RTListMove(&LstTmp, &pThis->LstRecords);
    ASMAtomicWriteU32(&pThis->cRecords, 0);
    RTSemFastMutexRelease(pThis->hMtx);

    PDBGFFLOWTRACERECORDINT pIt, pItNext;
    RTListForEachSafe(&LstTmp, pIt, pItNext, DBGFFLOWTRACERECORDINT, NdRecord)
    {
        RTListNodeRemove(&pIt->NdRecord);
        DBGFR3FlowTraceRecordRelease(pIt);
    }

    return VINF_SUCCESS;
}


/**
 * Adds a new probe to the given flow trace module.
 *
 * @returns VBox status code
 * @retval VERR_INVALID_STATE if the probe is active or was destroyed already.
 * @retval VERR_ALREADY_EXISTS if there is already a probe at the specified location.
 * @param   hFlowTraceMod           Flow trace module handle.
 * @param   pAddrProbe              Guest address to insert the probe at.
 * @param   hFlowTraceProbe         The handle of the probe to insert.
 * @param   fFlags                  Combination of DBGF_FLOW_TRACE_PROBE_ADD_F_*.
 */
VMMR3DECL(int) DBGFR3FlowTraceModAddProbe(DBGFFLOWTRACEMOD hFlowTraceMod, PCDBGFADDRESS pAddrProbe,
                                          DBGFFLOWTRACEPROBE hFlowTraceProbe, uint32_t fFlags)
{
    PDBGFFLOWTRACEMODINT pThis = hFlowTraceMod;
    PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbe;
    AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
    AssertPtrReturn(pProbe, VERR_INVALID_HANDLE);
    AssertPtrReturn(pAddrProbe, VERR_INVALID_POINTER);
    AssertReturn(!(fFlags & ~DBGF_FLOW_TRACE_PROBE_ADD_F_VALID_MASK), VERR_INVALID_PARAMETER);
    AssertReturn(pThis->enmState == DBGFFLOWTRACEMODSTATE_CREATED, VERR_INVALID_STATE);

    int rc = VINF_SUCCESS;
    PDBGFFLOWTRACEMODPROBELOC pProbeLoc = dbgfR3TraceModGetProbeLocAtAddr(pThis, pAddrProbe);
    if (!pProbeLoc)
    {
        pProbeLoc = (PDBGFFLOWTRACEMODPROBELOC)MMR3HeapAllocZU(pThis->pUVM, MM_TAG_DBGF_FLOWTRACE,
                                                               sizeof(DBGFFLOWTRACEMODPROBELOC));
        if (RT_LIKELY(pProbeLoc))
        {
            pProbeLoc->pTraceMod = pThis;
            pProbeLoc->pProbe    = pProbe;
            pProbeLoc->AddrProbe = *pAddrProbe;
            pProbeLoc->fFlags    = fFlags;
            ASMAtomicIncU32(&pProbe->cRefs);
            ASMAtomicIncU32(&pProbe->cRefsMod);
            RTSemFastMutexRequest(pThis->hMtx);
            RTListAppend(&pThis->LstProbes, &pProbeLoc->NdProbes);
            pThis->cProbes++;
            RTSemFastMutexRelease(pThis->hMtx);
        }
        else
            rc = VERR_NO_MEMORY;
    }
    else
        rc = VERR_ALREADY_EXISTS;

    return rc;
}


/**
 * Creates a new empty probe.
 *
 * @returns VBox status code.
 * @param   pUVM                    The usermode VM handle.
 * @param   pszDescr                Description of the probe, optional.
 * @param   phFlowTraceProbe        Where to store the probe handle on success.
 */
VMMR3DECL(int) DBGFR3FlowTraceProbeCreate(PUVM pUVM, const char *pszDescr, PDBGFFLOWTRACEPROBE phFlowTraceProbe)
{
    int rc = VINF_SUCCESS;
    PDBGFFLOWTRACEPROBEINT pProbe = (PDBGFFLOWTRACEPROBEINT)MMR3HeapAllocZU(pUVM, MM_TAG_DBGF_FLOWTRACE,
                                                                            sizeof(DBGFFLOWTRACEPROBEINT));
    if (RT_LIKELY(pProbe))
    {
        pProbe->cRefs       = 1;
        pProbe->cRefsMod    = 0;
        pProbe->pUVM        = pUVM;
        pProbe->cbProbe     = 0;
        pProbe->cEntries    = 0;
        pProbe->cEntriesMax = 0;
        pProbe->paEntries   = NULL;
        pProbe->pszDescr    = NULL;
        if (pszDescr)
        {
            pProbe->pszDescr = MMR3HeapStrDupU(pUVM, MM_TAG_DBGF_FLOWTRACE, pszDescr);
            if (!pProbe->pszDescr)
            {
                MMR3HeapFree(pProbe);
                rc = VERR_NO_MEMORY;
            }
        }

        if (RT_SUCCESS(rc))
            *phFlowTraceProbe = pProbe;
    }
    else
        rc = VERR_NO_MEMORY;

    return rc;
}


/**
 * Retains a reference to the probe.
 *
 * @returns New reference count.
 * @param   hFlowTraceProbe         Flow trace probe handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceProbeRetain(DBGFFLOWTRACEPROBE hFlowTraceProbe)
{
    PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbe;
    AssertPtrReturn(pProbe, UINT32_MAX);

    uint32_t cRefs = ASMAtomicIncU32(&pProbe->cRefs);
    AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pProbe));
    return cRefs;
}


/**
 * Release a probe reference.
 *
 * @returns New reference count, on 0 the probe is destroyed.
 * @param   hFlowTraceProbe         Flow trace probe handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceProbeRelease(DBGFFLOWTRACEPROBE hFlowTraceProbe)
{
    PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbe;
    if (!pProbe)
        return 0;
    AssertPtrReturn(pProbe, UINT32_MAX);

    uint32_t cRefs = ASMAtomicDecU32(&pProbe->cRefs);
    AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pProbe));
    if (cRefs == 0)
        dbgfR3FlowTraceProbeDestroy(pProbe);
    return cRefs;
}


/**
 * Adds new data to log in the given probe.
 *
 * @returns VBox status code.
 * @retval  VERR_INVALID_STATE if the probe is already part of a trace module and it is not
 *          possible to add new entries at this point.
 * @param   hFlowTraceProbe         Flow trace probe handle.
 * @param   paEntries               Pointer to the array of entry descriptors.
 * @param   cEntries                Number of entries in the array.
 */
VMMR3DECL(int) DBGFR3FlowTraceProbeEntriesAdd(DBGFFLOWTRACEPROBE hFlowTraceProbe,
                                              PCDBGFFLOWTRACEPROBEENTRY paEntries, uint32_t cEntries)
{
    PDBGFFLOWTRACEPROBEINT pProbe = hFlowTraceProbe;
    AssertPtrReturn(pProbe, VERR_INVALID_HANDLE);
    AssertPtrReturn(paEntries, VERR_INVALID_POINTER);
    AssertReturn(cEntries > 0, VERR_INVALID_PARAMETER);
    AssertReturn(!pProbe->cRefsMod, VERR_INVALID_STATE);

    int rc = dbgfR3ProbeEnsureSize(pProbe, cEntries);
    if (RT_SUCCESS(rc))
    {
        uint32_t idxEntry;

        for (idxEntry = 0; idxEntry < cEntries && RT_SUCCESS(rc); idxEntry++)
        {
            PCDBGFFLOWTRACEPROBEENTRY pEntry = &paEntries[idxEntry];
            PDBGFFLOWTRACEPROBEENTRY pProbeEntry = &pProbe->paEntries[pProbe->cEntries + idxEntry];

            rc = dbgfR3ProbeEntryDup(pProbe->pUVM, pProbeEntry, pEntry);
        }

        if (RT_FAILURE(rc))
            dbgfR3ProbeEntryCleanup(pProbe, pProbe->cEntries, idxEntry + 1);
        else
        {
            pProbe->cEntries += cEntries;
            dbgfR3ProbeRecalcSize(pProbe);
        }
    }

    return rc;
}


/**
 * Retains a reference to the given flow trace report.
 *
 * @returns New reference count.
 * @param   hFlowTraceReport        Flow trace report handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceReportRetain(DBGFFLOWTRACEREPORT hFlowTraceReport)
{
    PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
    AssertPtrReturn(pReport, UINT32_MAX);

    uint32_t cRefs = ASMAtomicIncU32(&pReport->cRefs);
    AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pReport));
    return cRefs;
}


/**
 * Releases a reference of the given flow trace report.
 *
 * @returns New reference count, on 0 the report is destroyed.
 * @param   hFlowTraceReport        Flow trace report handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceReportRelease(DBGFFLOWTRACEREPORT hFlowTraceReport)
{
    PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
    if (!pReport)
        return 0;
    AssertPtrReturn(pReport, UINT32_MAX);

    uint32_t cRefs = ASMAtomicDecU32(&pReport->cRefs);
    AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pReport));
    if (cRefs == 0)
        dbgfR3FlowTraceReportDestroy(pReport);
    return cRefs;
}


/**
 * Returns the number of records in the given report.
 *
 * @returns Number of records.
 * @param   hFlowTraceReport        Flow trace report handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceReportGetRecordCount(DBGFFLOWTRACEREPORT hFlowTraceReport)
{
    PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
    AssertPtrReturn(pReport, 0);

    return pReport->cRecords;
}


/**
 * Queries the specified record contained in the given report.
 *
 * @returns VBox status code.
 * @param   hFlowTraceReport        Flow trace report handle.
 * @param   idxRec                  The record index to query.
 * @param   phFlowTraceRec          Where to store the retained handle of the record on success.
 */
VMMR3DECL(int) DBGFR3FlowTraceReportQueryRecord(DBGFFLOWTRACEREPORT hFlowTraceReport, uint32_t idxRec, PDBGFFLOWTRACERECORD phFlowTraceRec)
{
    PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
    AssertPtrReturn(pReport, 0);
    AssertPtrReturn(phFlowTraceRec, VERR_INVALID_POINTER);
    AssertReturn(idxRec < pReport->cRecords, VERR_INVALID_PARAMETER);

    DBGFR3FlowTraceRecordRetain(pReport->apRec[idxRec]);
    *phFlowTraceRec = pReport->apRec[idxRec];
    return VINF_SUCCESS;
}


/**
 * Filters the given flow trace report by the given criterias and returns a filtered report.
 *
 * @returns VBox status code.
 * @param   hFlowTraceReport          Flow trace report handle.
 * @param   fFlags                    Combination of DBGF_FLOW_TRACE_REPORT_FILTER_F_*.
 * @param   paFilters                 Pointer to the array of filters.
 * @param   cFilters                  Number of entries in the filter array.
 * @param   enmOp                     How the filters are connected to each other.
 * @param   phFlowTraceReportFiltered Where to return the handle to the report containing the
 *                                    filtered records on success.
 */
VMMR3DECL(int) DBGFR3FlowTraceReportQueryFiltered(DBGFFLOWTRACEREPORT hFlowTraceReport, uint32_t fFlags,
                                                  PDBGFFLOWTRACEREPORTFILTER paFilters, uint32_t cFilters,
                                                  DBGFFLOWTRACEREPORTFILTEROP enmOp,
                                                  PDBGFFLOWTRACEREPORT phFlowTraceReportFiltered)
{
    PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
    AssertPtrReturn(pReport, VERR_INVALID_HANDLE);
    AssertReturn(!(fFlags & DBGF_FLOW_TRACE_REPORT_FILTER_F_VALID), VERR_INVALID_PARAMETER);
    AssertPtrReturn(paFilters, VERR_INVALID_POINTER);
    AssertReturn(cFilters > 0, VERR_INVALID_PARAMETER);
    AssertReturn(enmOp > DBGFFLOWTRACEREPORTFILTEROP_INVALID && enmOp <= DBGFFLOWTRACEREPORTFILTEROP_OR,
                 VERR_INVALID_PARAMETER);
    AssertPtrReturn(phFlowTraceReportFiltered, VERR_INVALID_POINTER);

    int rc = VINF_SUCCESS;
    PDBGFFLOWTRACEREPORTINT pReportFiltered = dbgfR3FlowTraceReportCreate(pReport->pUVM, pReport->cRecords);
    if (RT_LIKELY(pReport))
    {
        uint32_t idxFiltered = 0;

        for (uint32_t i = 0; i < pReport->cRecords; i++)
        {
            PDBGFFLOWTRACERECORDINT pCur = pReport->apRec[i];
            bool fRecFilterMatch = dbgfR3FlowTraceDoesRecordMatchFilter(pCur, paFilters, cFilters, enmOp);

            if (   (   fRecFilterMatch
                    && !(fFlags & DBGF_FLOW_TRACE_REPORT_FILTER_F_REVERSE))
                || (   !fRecFilterMatch
                    && (fFlags & DBGF_FLOW_TRACE_REPORT_FILTER_F_REVERSE)))
            {
                DBGFR3FlowTraceRecordRetain(pCur);
                pReportFiltered->apRec[idxFiltered++] = pCur;
            }
        }

        pReportFiltered->cRecords = idxFiltered;
        *phFlowTraceReportFiltered = pReportFiltered;
    }
    else
        rc = VERR_NO_MEMORY;

    return rc;
}


/**
 * Enumerates all records in the given flow trace report calling the supplied
 * enumeration callback.
 *
 * @returns VBox status code, return value of pfnEnum on error.
 * @param   hFlowTraceReport          Flow trace report handle.
 * @param   pfnEnum                   The callback to call for every record.
 * @param   pvUser                    Opaque user data to pass to the callback.
 */
VMMR3DECL(int) DBGFR3FlowTraceReportEnumRecords(DBGFFLOWTRACEREPORT hFlowTraceReport,
                                                PFNDBGFFLOWTRACEREPORTENUMCLBK pfnEnum,
                                                void *pvUser)
{
    PDBGFFLOWTRACEREPORTINT pReport = hFlowTraceReport;
    AssertPtrReturn(pReport, VERR_INVALID_HANDLE);

    int rc = VINF_SUCCESS;
    for (uint32_t i = 0; i < pReport->cRecords && RT_SUCCESS(rc); i++)
        rc = pfnEnum(pReport, pReport->apRec[i], pvUser);

    return rc;
}


/**
 * Retains a reference to the given flow trace record handle.
 *
 * @returns New reference count.
 * @param   hFlowTraceRecord        The record handle to retain.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceRecordRetain(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, UINT32_MAX);

    uint32_t cRefs = ASMAtomicIncU32(&pRecord->cRefs);
    AssertMsg(cRefs > 1 && cRefs < _1M, ("%#x %p\n", cRefs, pRecord));
    return cRefs;
}


/**
 * Releases a reference of the given flow trace record.
 *
 * @returns New reference count, on 0 the record is destroyed.
 * @param   hFlowTraceRecord        Flow trace record handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceRecordRelease(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    if (!pRecord)
        return 0;
    AssertPtrReturn(pRecord, UINT32_MAX);

    uint32_t cRefs = ASMAtomicDecU32(&pRecord->cRefs);
    AssertMsg(cRefs < _1M, ("%#x %p\n", cRefs, pRecord));
    if (cRefs == 0)
        dbgfR3FlowTraceRecordDestroy(pRecord);
    return cRefs;
}


/**
 * Gets the sequence number of the given record handle.
 *
 * @returns Sequence number.
 * @param   hFlowTraceRecord        Flow trace record handle.
 */
VMMR3DECL(uint64_t) DBGFR3FlowTraceRecordGetSeqNo(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, 0);

    return pRecord->u64SeqNo;
}


/**
 * Returns the timestamp when the record was created.
 *
 * @returns Timestamp in nano seconds.
 * @param   hFlowTraceRecord        Flow trace record handle.
 */
VMMR3DECL(uint64_t) DBGFR3FlowTraceRecordGetTimestamp(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, 0);

    return pRecord->u64TsCollected;
}


/**
 * Gets the address in the guest the record was created.
 *
 * @returns Pointer to the address containing the guest location the record was created at.
 * @param   hFlowTraceRecord        Flow trace record handle.
 * @param   pAddr                   Where to store the guest address.
 */
VMMR3DECL(PDBGFADDRESS) DBGFR3FlowTraceRecordGetAddr(DBGFFLOWTRACERECORD hFlowTraceRecord, PDBGFADDRESS pAddr)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, NULL);
    AssertPtrReturn(pAddr, NULL);

    *pAddr = pRecord->AddrProbe;
    return pAddr;
}


/**
 * Returns the handle to the probe for the given record.
 *
 * @returns Handle to the probe.
 * @param   hFlowTraceRecord        Flow trace record handle.
 */
VMMR3DECL(DBGFFLOWTRACEPROBE) DBGFR3FlowTraceRecordGetProbe(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, NULL);

    DBGFR3FlowTraceProbeRetain(pRecord->pProbe);
    return pRecord->pProbe;
}


/**
 * Returns the number of values contained in the record.
 *
 * @returns Number of values in the record.
 * @param   hFlowTraceRecord        Flow trace record handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceRecordGetValCount(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, 0);

    return pRecord->pProbe->cEntries;
}


/**
 * Returns the number of values contained in the record.
 *
 * @returns Number of values in the record.
 * @param   hFlowTraceRecord        Flow trace record handle.
 */
VMMR3DECL(uint32_t) DBGFR3FlowTraceRecordGetValCommonCount(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, 0);

    return pRecord->pProbeCmn ? pRecord->pProbeCmn->cEntries : 0;
}


/**
 * Returns the values for the given record.
 *
 * @returns Pointer to the array of values.
 * @param   hFlowTraceRecord        Flow trace record handle.
 */
VMMR3DECL(PCDBGFFLOWTRACEPROBEVAL) DBGFR3FlowTraceRecordGetVals(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, NULL);

    return &pRecord->aVal[0];
}


/**
 * Returns data collected by the common probe for the trace module this record is in if one
 * is active.
 *
 * @returns Pointer to the array of common probe values or NULL if no common probe was specified
 *          for the trace module.
 * @param   hFlowTraceRecord        Flow trace record handle.
 */
VMMR3DECL(PCDBGFFLOWTRACEPROBEVAL) DBGFR3FlowTraceRecordGetValsCommon(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, NULL);

    return pRecord->paValCmn;
}


/**
 * Returns the vCPU ID the record was created on.
 *
 * @returns vCPU ID.
 * @param   hFlowTraceRecord        Flow trace record handle.
 */
VMMR3DECL(VMCPUID) DBGFR3FlowTraceRecordGetCpuId(DBGFFLOWTRACERECORD hFlowTraceRecord)
{
    PDBGFFLOWTRACERECORDINT pRecord = hFlowTraceRecord;
    AssertPtrReturn(pRecord, VMCPUID_ANY);

    return pRecord->idCpu;
}