File: debug_session_imp.cpp

package info (click to toggle)
intel-compute-runtime 25.44.36015.8-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 79,632 kB
  • sloc: cpp: 931,547; lisp: 2,074; sh: 719; makefile: 162; python: 21
file content (1917 lines) | stat: -rw-r--r-- 78,522 bytes parent folder | download
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
/*
 * Copyright (C) 2021-2025 Intel Corporation
 *
 * SPDX-License-Identifier: MIT
 *
 */

#include "level_zero/tools/source/debug/debug_session_imp.h"

#include "shared/source/built_ins/sip.h"
#include "shared/source/device/device.h"
#include "shared/source/execution_environment/root_device_environment.h"
#include "shared/source/gmm_helper/gmm_helper.h"
#include "shared/source/helpers/basic_math.h"
#include "shared/source/helpers/file_io.h"
#include "shared/source/helpers/gfx_core_helper.h"
#include "shared/source/helpers/hw_info.h"
#include "shared/source/helpers/sleep.h"
#include "shared/source/helpers/string.h"
#include "shared/source/os_interface/os_interface.h"
#include "shared/source/sip_external_lib/sip_external_lib.h"

#include "level_zero/core/source/device/device_imp.h"
#include "level_zero/core/source/gfx_core_helpers/l0_gfx_core_helper.h"
#include "level_zero/zet_intel_gpu_debug.h"

namespace L0 {

DebugSession::DebugSession(const zet_debug_config_t &config, Device *device) : connectedDevice(device), config(config) {
}

const NEO::TopologyMap &DebugSession::getTopologyMap() {
    return connectedDevice->getOsInterface()->getDriverModel()->getTopologyMap();
};

void DebugSession::createEuThreads() {
    if (connectedDevice) {

        bool isSubDevice = connectedDevice->getNEODevice()->isSubDevice();

        auto &hwInfo = connectedDevice->getHwInfo();
        const uint32_t numSubslicesPerSlice = std::max(hwInfo.gtSystemInfo.MaxSubSlicesSupported, hwInfo.gtSystemInfo.MaxDualSubSlicesSupported) / hwInfo.gtSystemInfo.MaxSlicesSupported;
        const uint32_t numEuPerSubslice = hwInfo.gtSystemInfo.MaxEuPerSubSlice;
        const uint32_t numThreadsPerEu = hwInfo.gtSystemInfo.NumThreadsPerEu;
        uint32_t subDeviceCount = std::max(1u, connectedDevice->getNEODevice()->getNumSubDevices());
        UNRECOVERABLE_IF(isSubDevice && subDeviceCount > 1);

        for (uint32_t tileIndex = 0; tileIndex < subDeviceCount; tileIndex++) {

            if (isSubDevice || subDeviceCount == 1) {
                tileIndex = Math::log2(static_cast<uint32_t>(connectedDevice->getNEODevice()->getDeviceBitfield().to_ulong()));
            }

            for (uint32_t sliceID = 0; sliceID < NEO::GfxCoreHelper::getHighestEnabledSlice(hwInfo); sliceID++) {
                for (uint32_t subsliceID = 0; subsliceID < numSubslicesPerSlice; subsliceID++) {
                    for (uint32_t euID = 0; euID < numEuPerSubslice; euID++) {

                        for (uint32_t threadID = 0; threadID < numThreadsPerEu; threadID++) {

                            EuThread::ThreadId thread = {tileIndex, sliceID, subsliceID, euID, threadID};

                            allThreads[uint64_t(thread)] = std::make_unique<EuThread>(thread);
                        }
                    }
                }
            }

            if (isSubDevice || subDeviceCount == 1) {
                break;
            }
        }
    }
}

uint32_t DebugSession::getDeviceIndexFromApiThread(ze_device_thread_t thread) {
    auto deviceBitfield = connectedDevice->getNEODevice()->getDeviceBitfield();
    uint32_t deviceIndex = Math::log2(static_cast<uint32_t>(deviceBitfield.to_ulong()));
    auto deviceCount = std::max(1u, connectedDevice->getNEODevice()->getNumSubDevices());
    const auto &topologyMap = getTopologyMap();

    if (connectedDevice->getNEODevice()->isSubDevice()) {
        return deviceIndex;
    }

    if (deviceCount > 1) {

        if (thread.slice == UINT32_MAX) {
            deviceIndex = UINT32_MAX;
        } else {
            uint32_t sliceId = thread.slice;
            for (uint32_t i = 0; i < topologyMap.size(); i++) {
                if (deviceBitfield.test(i)) {
                    if (sliceId < topologyMap.at(i).sliceIndices.size()) {
                        deviceIndex = i;
                    }
                    sliceId = sliceId - static_cast<uint32_t>(topologyMap.at(i).sliceIndices.size());
                }
            }
        }
    }

    return deviceIndex;
}

ze_device_thread_t DebugSession::convertToPhysicalWithinDevice(ze_device_thread_t thread, uint32_t deviceIndex) {
    auto deviceImp = static_cast<DeviceImp *>(connectedDevice);
    const auto &topologyMap = getTopologyMap();

    // set slice for single slice config to allow subslice remapping
    auto mapping = topologyMap.find(deviceIndex);
    if (thread.slice == UINT32_MAX && mapping != topologyMap.end() && mapping->second.sliceIndices.size() == 1) {
        thread.slice = 0;
    }

    if (thread.slice != UINT32_MAX) {
        if (thread.subslice != UINT32_MAX) {
            deviceImp->toPhysicalSliceId(topologyMap, thread.slice, thread.subslice, deviceIndex);
        } else {
            uint32_t dummy = 0;
            deviceImp->toPhysicalSliceId(topologyMap, thread.slice, dummy, deviceIndex);
        }
    }

    return thread;
}

EuThread::ThreadId DebugSession::convertToThreadId(ze_device_thread_t thread) {
    auto deviceImp = static_cast<DeviceImp *>(connectedDevice);
    UNRECOVERABLE_IF(!DebugSession::isSingleThread(thread));

    uint32_t deviceIndex = 0;
    deviceImp->toPhysicalSliceId(getTopologyMap(), thread.slice, thread.subslice, deviceIndex);

    EuThread::ThreadId threadId(deviceIndex, thread.slice, thread.subslice, thread.eu, thread.thread);
    return threadId;
}

ze_device_thread_t DebugSession::convertToApi(EuThread::ThreadId threadId) {
    auto deviceImp = static_cast<DeviceImp *>(connectedDevice);

    ze_device_thread_t thread = {static_cast<uint32_t>(threadId.slice), static_cast<uint32_t>(threadId.subslice), static_cast<uint32_t>(threadId.eu), static_cast<uint32_t>(threadId.thread)};
    deviceImp->toApiSliceId(getTopologyMap(), thread.slice, thread.subslice, threadId.tileIndex);

    return thread;
}

std::vector<EuThread::ThreadId> DebugSession::getSingleThreadsForDevice(uint32_t deviceIndex, ze_device_thread_t physicalThread, const NEO::HardwareInfo &hwInfo) {

    const uint32_t numSubslicesPerSlice = hwInfo.gtSystemInfo.MaxSubSlicesSupported / hwInfo.gtSystemInfo.MaxSlicesSupported;
    const uint32_t numEuPerSubslice = hwInfo.gtSystemInfo.MaxEuPerSubSlice;
    const uint32_t numThreadsPerEu = hwInfo.gtSystemInfo.NumThreadsPerEu;

    UNRECOVERABLE_IF(numThreadsPerEu > 16);

    std::vector<EuThread::ThreadId> threads;

    const uint32_t slice = physicalThread.slice;
    const uint32_t subslice = physicalThread.subslice;
    const uint32_t eu = physicalThread.eu;
    const uint32_t thread = physicalThread.thread;

    for (uint32_t sliceID = 0; sliceID < NEO::GfxCoreHelper::getHighestEnabledSlice(hwInfo); sliceID++) {
        if (hwInfo.gtSystemInfo.IsDynamicallyPopulated && !hwInfo.gtSystemInfo.SliceInfo[sliceID].Enabled) {
            continue;
        }

        if (slice != UINT32_MAX) {
            sliceID = slice;
        }

        for (uint32_t subsliceID = 0; subsliceID < numSubslicesPerSlice; subsliceID++) {
            if (subslice != UINT32_MAX) {
                subsliceID = subslice;
            }

            for (uint32_t euID = 0; euID < numEuPerSubslice; euID++) {
                if (eu != UINT32_MAX) {
                    euID = eu;
                }

                for (uint32_t threadID = 0; threadID < numThreadsPerEu; threadID++) {
                    if (thread != UINT32_MAX) {
                        threadID = thread;
                    }
                    threads.push_back({deviceIndex, sliceID, subsliceID, euID, threadID});

                    if (thread != UINT32_MAX) {
                        break;
                    }
                }

                if (eu != UINT32_MAX) {
                    break;
                }
            }
            if (subslice != UINT32_MAX) {
                break;
            }
        }
        if (slice != UINT32_MAX) {
            break;
        }
    }

    return threads;
}

bool DebugSession::areRequestedThreadsStopped(ze_device_thread_t thread) {
    auto &hwInfo = connectedDevice->getHwInfo();
    bool requestedThreadsStopped = true;

    auto deviceCount = std::max(1u, connectedDevice->getNEODevice()->getNumSubDevices());
    uint32_t deviceIndex = getDeviceIndexFromApiThread(thread);

    auto areAllThreadsStopped = [this, &hwInfo](uint32_t deviceIndex, const ze_device_thread_t &thread) -> bool {
        auto physicalThread = convertToPhysicalWithinDevice(thread, deviceIndex);
        auto singleThreads = getSingleThreadsForDevice(deviceIndex, physicalThread, hwInfo);

        for (auto &threadId : singleThreads) {

            if (allThreads[threadId]->isReportedAsStopped()) {
                continue;
            }
            return false;
        }
        return true;
    };

    if (deviceIndex != UINT32_MAX) {
        return areAllThreadsStopped(deviceIndex, thread);
    }

    for (uint32_t i = 0; i < deviceCount; i++) {

        if (areAllThreadsStopped(i, thread) == false) {
            return false;
        }
    }

    return requestedThreadsStopped;
}

ze_result_t DebugSession::sanityMemAccessThreadCheck(ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc) {
    if (DebugSession::isThreadAll(thread)) {
        if (desc->type != ZET_DEBUG_MEMORY_SPACE_TYPE_DEFAULT) {
            return ZE_RESULT_ERROR_INVALID_ARGUMENT;
        } else {
            return ZE_RESULT_SUCCESS;
        }
    } else if (DebugSession::isSingleThread(thread)) {
        if (!areRequestedThreadsStopped(thread)) {
            return ZE_RESULT_ERROR_NOT_AVAILABLE;
        } else {
            return ZE_RESULT_SUCCESS;
        }
    }

    return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}

void DebugSession::fillDevicesFromThread(ze_device_thread_t thread, std::vector<uint8_t> &devices) {
    auto deviceCount = std::max(1u, connectedDevice->getNEODevice()->getNumSubDevices());
    UNRECOVERABLE_IF(devices.size() < deviceCount);

    uint32_t deviceIndex = getDeviceIndexFromApiThread(thread);
    bool singleDevice = (thread.slice != UINT32_MAX && deviceCount > 1) || deviceCount == 1;

    if (singleDevice) {
        devices[deviceIndex] = 1;
    } else {
        for (uint32_t i = 0; i < deviceCount; i++) {
            devices[i] = 1;
        }
    }
}

bool DebugSession::isBindlessSystemRoutine() {
    if (debugArea.reserved1 &= 1) {
        return true;
    }
    return false;
}

size_t DebugSession::getPerThreadScratchOffset(size_t ptss, EuThread::ThreadId threadId) {
    auto &hwInfo = connectedDevice->getHwInfo();
    const uint32_t numSubslicesPerSlice = hwInfo.gtSystemInfo.MaxSubSlicesSupported / hwInfo.gtSystemInfo.MaxSlicesSupported;
    const uint32_t numEuPerSubslice = hwInfo.gtSystemInfo.MaxEuPerSubSlice;
    const uint32_t numThreadsPerEu = hwInfo.gtSystemInfo.NumThreadsPerEu;

    const auto &productHelper = connectedDevice->getProductHelper();
    uint32_t threadEuRatio = productHelper.getThreadEuRatioForScratch(hwInfo);
    uint32_t multiplyFactor = 1;
    if (threadEuRatio / numThreadsPerEu > 1) {
        multiplyFactor = threadEuRatio / numThreadsPerEu;
    }

    auto threadOffset = (((threadId.slice * numSubslicesPerSlice + threadId.subslice) * numEuPerSubslice + threadId.eu) * numThreadsPerEu * multiplyFactor + threadId.thread) * ptss;
    return threadOffset;
}

void DebugSession::printBitmask(uint8_t *bitmask, size_t bitmaskSize) {
    if (NEO::debugManager.flags.DebuggerLogBitmask.get() & NEO::DebugVariables::DEBUGGER_LOG_BITMASK::LOG_INFO) {

        DEBUG_BREAK_IF(bitmaskSize % sizeof(uint64_t) != 0);

        PRINT_DEBUGGER_LOG(stdout, "\nINFO: Bitmask: ", "");

        for (size_t i = 0; i < bitmaskSize / sizeof(uint64_t); i++) {
            uint64_t bitmask64 = 0;
            memcpy_s(&bitmask64, sizeof(uint64_t), &bitmask[i * sizeof(uint64_t)], sizeof(uint64_t));
            PRINT_DEBUGGER_LOG(stdout, "\n [%lu] = %#018" PRIx64, static_cast<uint64_t>(i), bitmask64);
        }
    }
}

DebugSession *DebugSessionImp::attachTileDebugSession(Device *device) {
    std::unique_lock<std::mutex> lock(asyncThreadMutex);

    uint32_t subDeviceIndex = Math::log2(static_cast<uint32_t>(device->getNEODevice()->getDeviceBitfield().to_ulong()));

    auto &[tileSession, attached] = tileSessions[subDeviceIndex];
    if (attached) {
        return nullptr;
    }

    tileSessions[subDeviceIndex].first->attachTile();
    attached = true;

    PRINT_DEBUGGER_INFO_LOG("TileDebugSession attached, deviceIndex = %lu\n", subDeviceIndex);
    return tileSession;
}

void DebugSessionImp::detachTileDebugSession(DebugSession *tileSession) {

    uint32_t subDeviceIndex = Math::log2(static_cast<uint32_t>(tileSession->getConnectedDevice()->getNEODevice()->getDeviceBitfield().to_ulong()));

    tileSessions[subDeviceIndex].second = false;
    tileSessions[subDeviceIndex].first->detachTile();
    cleanRootSessionAfterDetach(subDeviceIndex);

    PRINT_DEBUGGER_INFO_LOG("TileDebugSession detached, deviceIndex = %lu\n", subDeviceIndex);
}

bool DebugSessionImp::areAllTileDebugSessionDetached() {
    for (const auto &session : tileSessions) {
        if (session.second == true) {
            return false;
        }
    }
    return true;
}

ze_result_t DebugSessionImp::interrupt(ze_device_thread_t thread) {
    if (areRequestedThreadsStopped(thread)) {
        return ZE_RESULT_ERROR_NOT_AVAILABLE;
    }

    {
        std::unique_lock<std::mutex> lock(interruptMutex);

        for (auto &previousThread : pendingInterrupts) {
            if (areThreadsEqual(thread, previousThread.first)) {
                return ZE_RESULT_NOT_READY;
            }
        }

        interruptRequests.push(thread);
    }

    return ZE_RESULT_SUCCESS;
}

uint32_t DebugSessionImp::readSipMemory(void *userArg, uint32_t offset, uint32_t size, void *destination) {
    struct SipMemoryAccessArgs *args = reinterpret_cast<struct SipMemoryAccessArgs *>(userArg);
    if (args->debugSession->readGpuMemory(args->contextHandle, static_cast<char *>(destination), size, offset + args->gpuVa) != ZE_RESULT_SUCCESS) {
        return 0;
    }
    return size;
}

uint32_t DebugSessionImp::writeSipMemory(void *userArg, uint32_t offset, uint32_t size, void *source) {
    struct SipMemoryAccessArgs *args = reinterpret_cast<struct SipMemoryAccessArgs *>(userArg);
    if (args->debugSession->writeGpuMemory(args->contextHandle, static_cast<const char *>(source), size, offset + args->gpuVa) != ZE_RESULT_SUCCESS) {
        return 0;
    }
    return size;
}

DebugSessionImp::Error DebugSessionImp::resumeThreadsWithinDevice(uint32_t deviceIndex, ze_device_thread_t apiThread) {
    auto &hwInfo = connectedDevice->getHwInfo();
    bool allThreadsRunning = true;
    auto physicalThread = convertToPhysicalWithinDevice(apiThread, deviceIndex);
    auto singleThreads = getSingleThreadsForDevice(deviceIndex, physicalThread, hwInfo);
    Error retVal = Error::unknown;
    uint64_t memoryHandle = EuThread::invalidHandle;

    std::vector<ze_device_thread_t> resumeThreads;
    std::vector<EuThread::ThreadId> resumeThreadIds;
    for (auto &threadId : singleThreads) {
        if (allThreads[threadId]->isRunning()) {
            continue;
        }
        if (!allThreads[threadId]->isReportedAsStopped()) {
            continue;
        }
        allThreadsRunning = false;
        resumeThreads.emplace_back(ze_device_thread_t{static_cast<uint32_t>(threadId.slice), static_cast<uint32_t>(threadId.subslice), static_cast<uint32_t>(threadId.eu), static_cast<uint32_t>(threadId.thread)});
        resumeThreadIds.push_back(threadId);
    }

    if (allThreadsRunning) {
        return Error::threadsRunning;
    }

    std::unique_lock<std::mutex> lock(threadStateMutex);
    memoryHandle = allThreads[resumeThreadIds[0]]->getMemoryHandle();

    [[maybe_unused]] auto sipCommandResult = writeResumeCommand(resumeThreadIds);
    DEBUG_BREAK_IF(sipCommandResult != true);

    auto result = resumeImp(resumeThreadIds, deviceIndex);

    // For resume(ALL) and multiple threads to resume - read whole state save area
    // to avoid multiple calls to KMD
    if (resumeThreadIds.size() > 1 && isThreadAll(apiThread)) {

        auto gpuVa = getContextStateSaveAreaGpuVa(memoryHandle);
        auto stateSaveAreaSize = getContextStateSaveAreaSize(memoryHandle);

        std::unique_ptr<char[]> stateSaveArea = nullptr;
        auto stateSaveReadResult = ZE_RESULT_ERROR_UNKNOWN;

        if (gpuVa != 0 && stateSaveAreaSize != 0) {
            stateSaveArea = std::make_unique<char[]>(stateSaveAreaSize);
            stateSaveReadResult = readGpuMemory(memoryHandle, stateSaveArea.get(), stateSaveAreaSize, gpuVa);
        } else {
            DEBUG_BREAK_IF(true);
        }

        for (auto &threadID : resumeThreadIds) {
            if (stateSaveReadResult == ZE_RESULT_SUCCESS) {
                while (checkThreadIsResumed(threadID, stateSaveArea.get()) == false) {
                    // read state save area again to update sr counters
                    stateSaveReadResult = readGpuMemory(allThreads[threadID]->getMemoryHandle(), stateSaveArea.get(), stateSaveAreaSize, gpuVa);
                }
            }
            allThreads[threadID]->resumeThread();
        }

    } else {

        for (auto &threadID : resumeThreadIds) {
            while (checkThreadIsResumed(threadID) == false) {
                ;
            }

            allThreads[threadID]->resumeThread();
        }
    }
    checkStoppedThreadsAndGenerateEvents(resumeThreadIds, memoryHandle, deviceIndex);

    if (sipCommandResult && result == ZE_RESULT_SUCCESS) {
        retVal = Error::success;
    }

    return retVal;
}

void DebugSessionImp::applyResumeWa(uint8_t *bitmask, size_t bitmaskSize) {

    UNRECOVERABLE_IF(bitmaskSize % 8 != 0);

    auto &l0GfxCoreHelper = connectedDevice->getNEODevice()->getRootDeviceEnvironment().getHelper<L0GfxCoreHelper>();

    if (l0GfxCoreHelper.isResumeWARequired()) {

        uint32_t *dwordBitmask = reinterpret_cast<uint32_t *>(bitmask);
        for (uint32_t i = 0; i < bitmaskSize / sizeof(uint32_t) - 1; i = i + 2) {
            dwordBitmask[i] = dwordBitmask[i] | dwordBitmask[i + 1];
            dwordBitmask[i + 1] = dwordBitmask[i] | dwordBitmask[i + 1];
        }
    }
    return;
}

bool DebugSessionImp::writeResumeCommand(const std::vector<EuThread::ThreadId> &threadIds) {
    auto stateSaveAreaHeader = getStateSaveAreaHeader();
    bool success = true;

    if (stateSaveAreaHeader->versionHeader.version.major < 2u) {
        auto &hwInfo = connectedDevice->getHwInfo();
        auto &l0GfxCoreHelper = connectedDevice->getNEODevice()->getRootDeviceEnvironment().getHelper<L0GfxCoreHelper>();

        if (l0GfxCoreHelper.isResumeWARequired()) {
            constexpr uint32_t sipResumeValue = 0x40000000;

            bool isBindlessSip = (debugArea.reserved1 == 1);
            auto registerType = ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU;
            uint32_t dword = 1;

            if (!isBindlessSip) {
                registerType = ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU;
                dword = 4;
            }

            const auto regSize = std::max(getRegisterSize(registerType), hwInfo.capabilityTable.grfSize);
            auto reg = std::make_unique<uint32_t[]>(regSize / sizeof(uint32_t));

            for (auto &threadID : threadIds) {
                memset(reg.get(), 0, regSize);

                if (readRegistersImp(threadID, registerType, 0, 1, reg.get()) != ZE_RESULT_SUCCESS) {
                    success = false;
                } else {
                    reg[dword] |= sipResumeValue;
                    if (writeRegistersImp(threadID, registerType, 0, 1, reg.get()) != ZE_RESULT_SUCCESS) {
                        success = false;
                    }
                }
            }
        }
    } else // >= 2u
    {
        SIP::sip_command resumeCommand = {0};
        resumeCommand.command = static_cast<uint32_t>(NEO::SipKernel::Command::resume);

        for (auto &threadID : threadIds) {
            ze_result_t result = cmdRegisterAccessHelper(threadID, resumeCommand, true);
            if (result != ZE_RESULT_SUCCESS) {
                success = false;
            }
        }
    }
    return success;
}

bool DebugSessionImp::checkThreadIsResumed(const EuThread::ThreadId &threadID) {
    auto stateSaveAreaHeader = getStateSaveAreaHeader();
    bool resumed = true;

    if (stateSaveAreaHeader->versionHeader.version.major >= 2u) {
        SIP::sr_ident srMagic = {{0}};
        const auto thread = allThreads[threadID].get();

        if (!readSystemRoutineIdent(thread, thread->getMemoryHandle(), srMagic)) {
            return resumed;
        }

        PRINT_DEBUGGER_THREAD_LOG("checkThreadIsResumed - Read counter for thread %s, counter == %d\n", EuThread::toString(threadID).c_str(), (int)srMagic.count);

        // Counter greater than last one means thread was resumed
        if (srMagic.count == thread->getLastCounter()) {
            resumed = false;
        }
    }
    return resumed;
}

bool DebugSessionImp::checkThreadIsResumed(const EuThread::ThreadId &threadID, const void *stateSaveArea) {
    auto stateSaveAreaHeader = getStateSaveAreaHeader();
    bool resumed = true;

    if (stateSaveAreaHeader->versionHeader.version.major >= 2u) {
        SIP::sr_ident srMagic = {{0}};
        const auto thread = allThreads[threadID].get();

        if (!readSystemRoutineIdentFromMemory(thread, stateSaveArea, srMagic)) {
            return resumed;
        }

        PRINT_DEBUGGER_THREAD_LOG("checkThreadIsResumed - Read counter for thread %s, counter == %d\n", EuThread::toString(threadID).c_str(), (int)srMagic.count);

        // Counter greater than last one means thread was resumed
        if (srMagic.count == thread->getLastCounter()) {
            resumed = false;
        }
    }
    return resumed;
}

ze_result_t DebugSessionImp::resume(ze_device_thread_t thread) {

    auto deviceCount = std::max(1u, connectedDevice->getNEODevice()->getNumSubDevices());
    bool singleDevice = (thread.slice != UINT32_MAX && deviceCount > 1) || deviceCount == 1;
    ze_result_t retVal = ZE_RESULT_SUCCESS;

    if (singleDevice) {
        uint32_t deviceIndex = Math::log2(static_cast<uint32_t>(connectedDevice->getNEODevice()->getDeviceBitfield().to_ulong()));

        if (connectedDevice->getNEODevice()->isSubDevice()) {
            deviceIndex = Math::log2(static_cast<uint32_t>(connectedDevice->getNEODevice()->getDeviceBitfield().to_ulong()));
        } else if (thread.slice != UINT32_MAX) {
            deviceIndex = getDeviceIndexFromApiThread(thread);
        }

        auto result = resumeThreadsWithinDevice(deviceIndex, thread);

        if (result == Error::threadsRunning) {
            retVal = ZE_RESULT_ERROR_NOT_AVAILABLE;
        } else if (result != Error::success) {
            return ZE_RESULT_ERROR_UNKNOWN;
        }
    } else {
        bool allThreadsRunning = true;

        for (uint32_t deviceId = 0; deviceId < deviceCount; deviceId++) {
            auto result = resumeThreadsWithinDevice(deviceId, thread);

            if (result == Error::threadsRunning) {
                continue;
            } else if (result != Error::success) {
                retVal = ZE_RESULT_ERROR_UNKNOWN;
            }
            allThreadsRunning = false;
        }

        if (allThreadsRunning) {
            return ZE_RESULT_ERROR_NOT_AVAILABLE;
        }
    }

    return retVal;
}

void DebugSessionImp::sendInterrupts() {

    if (interruptSent) {
        return;
    }

    {
        std::unique_lock<std::mutex> lock(interruptMutex);

        while (interruptRequests.size() > 0) {
            auto thread = interruptRequests.front();
            pendingInterrupts.push_back(std::pair<ze_device_thread_t, bool>(thread, false));
            interruptRequests.pop();
        }
    }

    if (pendingInterrupts.size() == 0) {
        return;
    }

    expectedAttentionEvents = 0;
    auto deviceCount = std::max(1u, connectedDevice->getNEODevice()->getNumSubDevices());

    if (deviceCount == 1) {
        uint32_t deviceIndex = Math::log2(static_cast<uint32_t>(connectedDevice->getNEODevice()->getDeviceBitfield().to_ulong()));

        ze_result_t result;
        {
            std::unique_lock<std::mutex> lock(threadStateMutex);
            result = interruptImp(deviceIndex);
        }

        if (result == ZE_RESULT_SUCCESS) {
            interruptTime = std::chrono::high_resolution_clock::now();
            interruptSent = true;
        } else {
            zet_debug_event_t debugEvent = {};
            debugEvent.type = ZET_DEBUG_EVENT_TYPE_THREAD_UNAVAILABLE;

            for (auto &request : pendingInterrupts) {
                debugEvent.info.thread.thread = request.first;
                enqueueApiEvent(debugEvent);
            }

            {
                std::unique_lock<std::mutex> lock(interruptMutex);
                pendingInterrupts.clear();
            }
        }
    } else {
        std::vector<uint8_t> devices(deviceCount);

        for (auto &request : pendingInterrupts) {
            auto thread = request.first;
            fillDevicesFromThread(thread, devices);
        }

        std::vector<ze_result_t> results(deviceCount);

        for (uint32_t i = 0; i < deviceCount; i++) {
            if (devices[i]) {
                std::unique_lock<std::mutex> lock(threadStateMutex);
                results[i] = interruptImp(i);
                if (results[i] == ZE_RESULT_SUCCESS) {
                    expectedAttentionEvents++;
                }
            } else {
                results[i] = ZE_RESULT_SUCCESS;
            }
        }

        const bool allFailed = std::all_of(results.begin(), results.end(),
                                           [](const auto &result) { return result != ZE_RESULT_SUCCESS; });

        PRINT_DEBUGGER_INFO_LOG("Successful interrupt requests = %u \n", expectedAttentionEvents);

        if (allFailed) {
            zet_debug_event_t debugEvent = {};
            debugEvent.type = ZET_DEBUG_EVENT_TYPE_THREAD_UNAVAILABLE;

            for (auto &request : pendingInterrupts) {
                debugEvent.info.thread.thread = request.first;
                enqueueApiEvent(debugEvent);
            }

            expectedAttentionEvents = 0;

            {
                std::unique_lock<std::mutex> lock(interruptMutex);
                pendingInterrupts.clear();
            }
        } else {
            interruptTime = std::chrono::high_resolution_clock::now();
            interruptSent = true;
        }
    }
}

size_t DebugSessionImp::calculateSrMagicOffset(const NEO::StateSaveAreaHeader *stateSaveAreaHeader, EuThread *thread) {
    auto threadSlotOffset = calculateThreadSlotOffset(thread->getThreadId());
    size_t srMagicOffset = 0;
    if (stateSaveAreaHeader->versionHeader.version.major == 3) {
        srMagicOffset = threadSlotOffset + stateSaveAreaHeader->regHeaderV3.sr_magic_offset;
    } else if (stateSaveAreaHeader->versionHeader.version.major < 3) {
        srMagicOffset = threadSlotOffset + stateSaveAreaHeader->regHeader.sr_magic_offset;
    } else {
        PRINT_DEBUGGER_ERROR_LOG("%s: Unsupported version of State Save Area Header\n", __func__);
        DEBUG_BREAK_IF(true);
    }
    return srMagicOffset;
}

bool DebugSessionImp::readSystemRoutineIdent(EuThread *thread, uint64_t memoryHandle, SIP::sr_ident &srIdent) {
    auto stateSaveAreaHeader = getStateSaveAreaHeader();
    if (!stateSaveAreaHeader) {
        return false;
    }

    auto gpuVa = getContextStateSaveAreaGpuVa(memoryHandle);
    if (gpuVa == 0) {
        return false;
    }

    auto srMagicOffset = calculateSrMagicOffset(stateSaveAreaHeader, thread);

    if (ZE_RESULT_SUCCESS != readGpuMemory(memoryHandle, reinterpret_cast<char *>(&srIdent), sizeof(srIdent), gpuVa + srMagicOffset)) {
        return false;
    }

    if (0 != strcmp(srIdent.magic, "srmagic")) {
        PRINT_DEBUGGER_ERROR_LOG("readSystemRoutineIdent - Failed to read srMagic for thread %s\n", EuThread::toString(thread->getThreadId()).c_str());
        return false;
    }

    return true;
}
bool DebugSessionImp::readSystemRoutineIdentFromMemory(EuThread *thread, const void *stateSaveArea, SIP::sr_ident &srIdent) {
    auto stateSaveAreaHeader = getStateSaveAreaHeader();

    auto srMagicOffset = calculateSrMagicOffset(stateSaveAreaHeader, thread);
    auto threadSlot = ptrOffset(stateSaveArea, srMagicOffset);
    memcpy_s(&srIdent, sizeof(SIP::sr_ident), threadSlot, sizeof(SIP::sr_ident));

    PRINT_DEBUGGER_INFO_LOG("readSystemRoutineIdentFromMemory - srMagicOffset %lu for thread %s\n", srMagicOffset, EuThread::toString(thread->getThreadId()).c_str());
    if (0 != strcmp(srIdent.magic, "srmagic")) {
        PRINT_DEBUGGER_ERROR_LOG("readSystemRoutineIdentFromMemory - Failed to read srMagic for thread %s\n", EuThread::toString(thread->getThreadId()).c_str());
        return false;
    }

    return true;
}

void DebugSessionImp::addThreadToNewlyStoppedFromRaisedAttention(EuThread::ThreadId threadId, uint64_t memoryHandle, const void *stateSaveArea) {

    SIP::sr_ident srMagic = {{0}};
    srMagic.count = 0;

    DEBUG_BREAK_IF(stateSaveArea == nullptr);

    bool wasStopped = false;
    {
        wasStopped = allThreads[threadId]->isStopped();

        if (!wasStopped) {

            if (!readSystemRoutineIdentFromMemory(allThreads[threadId].get(), stateSaveArea, srMagic)) {
                PRINT_DEBUGGER_ERROR_LOG("Failed to read SR IDENT\n", "");
                return;
            } else {
                PRINT_DEBUGGER_INFO_LOG("SIP version == %d.%d.%d\n", (int)srMagic.version.major, (int)srMagic.version.minor, (int)srMagic.version.patch);
            }

            if (!allThreads[threadId]->verifyStopped(srMagic.count)) {
                return;
            }

            allThreads[threadId]->stopThread(memoryHandle);
        }
    }

    if (!wasStopped) {
        newlyStoppedThreads.push_back(threadId);
    }
}

void DebugSessionImp::generateEventsAndResumeStoppedThreads() {

    if (interruptSent && !triggerEvents) {
        auto timeDiff = getTimeDifferenceMilliseconds(interruptTime);
        if (timeDiff > interruptTimeout) {
            triggerEvents = true;
            interruptTime = std::chrono::high_resolution_clock::now();
        }
    }

    if (triggerEvents) {
        std::vector<EuThread::ThreadId> resumeThreads;
        std::vector<EuThread::ThreadId> stoppedThreadsToReport;
        std::vector<EuThread::ThreadId> interruptedThreads;

        fillResumeAndStoppedThreadsFromNewlyStopped(resumeThreads, stoppedThreadsToReport, interruptedThreads);

        resumeAccidentallyStoppedThreads(resumeThreads);
        generateEventsForPendingInterrupts();
        generateEventsForStoppedThreads(stoppedThreadsToReport);

        interruptSent = false;
        triggerEvents = false;
    }
}

bool DebugSessionImp::isForceExceptionOrForceExternalHaltOnlyExceptionReason(uint32_t *cr0) {
    const uint32_t cr0ExceptionBitmask = 0xFC810000;
    const uint32_t cr0ForcedExceptionBitmask = 0x44000000;

    return (((cr0[1] & cr0ExceptionBitmask) & (~cr0ForcedExceptionBitmask)) == 0);
}

bool DebugSessionImp::isAIPequalToThreadStartIP(uint32_t *cr0, uint32_t *dbg0) {
    if (cr0[2] == dbg0[0]) {
        return true;
    } else {
        return false;
    }
}

void DebugSessionImp::fillResumeAndStoppedThreadsFromNewlyStopped(std::vector<EuThread::ThreadId> &resumeThreads, std::vector<EuThread::ThreadId> &stoppedThreadsToReport, std::vector<EuThread::ThreadId> &interruptedThreads) {

    if (newlyStoppedThreads.empty()) {
        PRINT_DEBUGGER_INFO_LOG("%s", "No newly stopped threads found. Returning");
        return;
    }
    const auto regSize = std::max(getRegisterSize(ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU), 64u);
    auto reg = std::make_unique<uint32_t[]>(regSize / sizeof(uint32_t));

    for (auto &newlyStopped : newlyStoppedThreads) {
        if (allThreads[newlyStopped]->isStopped()) {
            memset(reg.get(), 0, regSize);
            readRegistersImp(newlyStopped, ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU, 0, 1, reg.get());

            if (allThreads[newlyStopped]->getPageFault()) {
                uint32_t dbgreg[64u] = {0};
                readRegistersImp(newlyStopped, ZET_DEBUG_REGSET_TYPE_DBG_INTEL_GPU, 0, 1, dbgreg);
                if (isAIPequalToThreadStartIP(reg.get(), dbgreg)) {
                    PRINT_DEBUGGER_THREAD_LOG("Thread %s with PF AIP is equal to StartIP. Filtering out\n", allThreads[newlyStopped]->toString().c_str());
                } else {
                    const uint32_t cr0PFBit16 = 0x10000;
                    reg[1] = reg[1] | cr0PFBit16;
                    writeRegistersImp(newlyStopped, ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU, 0, 1, reg.get());
                }
            }
            if (isForceExceptionOrForceExternalHaltOnlyExceptionReason(reg.get())) {
                bool threadWasInterrupted = false;

                for (auto &request : pendingInterrupts) {
                    ze_device_thread_t apiThread = convertToApi(newlyStopped);

                    auto isInterrupted = checkSingleThreadWithinDeviceThread(apiThread, request.first);

                    if (isInterrupted) {
                        // mark pending interrupt as completed successfully
                        request.second = true;
                        threadWasInterrupted = true;
                        allThreads[newlyStopped]->reportAsStopped();
                    }
                }

                if (threadWasInterrupted) {
                    interruptedThreads.push_back(newlyStopped);
                } else {
                    PRINT_DEBUGGER_THREAD_LOG("RESUME accidentally stopped thread = %s\n", allThreads[newlyStopped]->toString().c_str());
                    resumeThreads.push_back(newlyStopped);
                }
            } else {
                PRINT_DEBUGGER_THREAD_LOG("Newly stopped thread = %s, exception bits = %#010" PRIx32 "\n", allThreads[newlyStopped]->toString().c_str(), reg[1]);
                allThreads[newlyStopped]->setPageFault(false);
                stoppedThreadsToReport.push_back(newlyStopped);
            }
        }
    }

    newlyStoppedThreads.clear();
}

void DebugSessionImp::generateEventsForPendingInterrupts() {
    zet_debug_event_t debugEvent = {};

    for (auto &request : pendingInterrupts) {
        if (request.second == true) {
            debugEvent.type = ZET_DEBUG_EVENT_TYPE_THREAD_STOPPED;
            debugEvent.info.thread.thread = request.first;
            enqueueApiEvent(debugEvent);
        } else {
            debugEvent.type = ZET_DEBUG_EVENT_TYPE_THREAD_UNAVAILABLE;
            debugEvent.info.thread.thread = request.first;
            enqueueApiEvent(debugEvent);
        }
    }

    {
        std::unique_lock<std::mutex> lock(interruptMutex);
        pendingInterrupts.clear();
    }
}

void DebugSessionImp::resumeAccidentallyStoppedThreads(const std::vector<EuThread::ThreadId> &threadIds) {
    std::vector<EuThread::ThreadId> threadIdsPerDevice[4];

    for (auto &threadID : threadIds) {
        uint32_t deviceIndex = static_cast<uint32_t>(threadID.tileIndex);

        UNRECOVERABLE_IF((connectedDevice->getNEODevice()->getNumSubDevices() > 0) &&
                         (deviceIndex >= connectedDevice->getNEODevice()->getNumSubDevices()));

        threadIdsPerDevice[deviceIndex].push_back(threadID);
    }

    for (uint32_t i = 0; i < 4; i++) {
        std::unique_lock<std::mutex> lock(threadStateMutex);

        if (threadIdsPerDevice[i].size() > 0) {
            [[maybe_unused]] auto writeSipCommandResult = writeResumeCommand(threadIdsPerDevice[i]);
            DEBUG_BREAK_IF(writeSipCommandResult != true);
            resumeImp(threadIdsPerDevice[i], i);
        }

        for (auto &threadID : threadIdsPerDevice[i]) {
            while (checkThreadIsResumed(threadID) == false) {
                ;
            }

            allThreads[threadID]->resumeThread();
        }
    }
}

void DebugSessionImp::generateEventsForStoppedThreads(const std::vector<EuThread::ThreadId> &threadIds) {
    zet_debug_event_t debugEvent = {};
    for (auto &threadID : threadIds) {
        ze_device_thread_t thread = convertToApi(threadID);

        debugEvent.type = ZET_DEBUG_EVENT_TYPE_THREAD_STOPPED;
        debugEvent.info.thread.thread = thread;
        enqueueApiEvent(debugEvent);
    }
}

ze_result_t DebugSessionImp::readEvent(uint64_t timeout, zet_debug_event_t *outputEvent) {

    if (outputEvent) {
        outputEvent->type = ZET_DEBUG_EVENT_TYPE_INVALID;
        outputEvent->flags = 0;
    } else {
        return ZE_RESULT_ERROR_INVALID_NULL_POINTER;
    }

    do {
        std::unique_lock<std::mutex> lock(asyncThreadMutex);

        if (timeout > 0 && apiEvents.size() == 0) {
            apiEventCondition.wait_for(lock, std::chrono::milliseconds(timeout));
        }

        if (apiEvents.size() > 0) {
            zet_debug_event_t temp = apiEvents.front();

            if (temp.type == ZET_DEBUG_EVENT_TYPE_THREAD_STOPPED) {

                if (isSingleThread(temp.info.thread.thread)) {
                    auto threadID = convertToThreadId(temp.info.thread.thread);
                    allThreads[threadID]->reportAsStopped();
                }
            }
            *outputEvent = temp;
            apiEvents.pop();
            return ZE_RESULT_SUCCESS;
        }
    } while (timeout == UINT64_MAX && asyncThread.threadActive);

    return ZE_RESULT_NOT_READY;
}

void DebugSessionImp::dumpDebugSurfaceToFile(uint64_t vmHandle, uint64_t gpuVa, const std::string &path) {
    auto stateSaveAreaSize = getContextStateSaveAreaSize(vmHandle);
    std::vector<char> data(stateSaveAreaSize);
    auto retVal = readGpuMemory(vmHandle, data.data(), data.size(), gpuVa);
    if (retVal != ZE_RESULT_SUCCESS) {
        PRINT_DEBUGGER_ERROR_LOG("Reading context state save area failed, error = %d\n", retVal);
        return;
    }
    writeDataToFile(path.c_str(), std::string_view(data.data(), data.size()));
}

void DebugSessionImp::validateAndSetStateSaveAreaHeader(uint64_t vmHandle, uint64_t gpuVa) {
    const std::string dumpDebugSurfacePath = NEO::debugManager.flags.DumpDebugSurfaceFile.get();
    if (dumpDebugSurfacePath != "unk") {
        dumpDebugSurfaceToFile(vmHandle, gpuVa, dumpDebugSurfacePath);
    }

    auto headerSize = sizeof(NEO::StateSaveAreaHeader);
    std::vector<char> data(headerSize);
    auto retVal = readGpuMemory(vmHandle, data.data(), sizeof(SIP::StateSaveArea), gpuVa);

    if (retVal != ZE_RESULT_SUCCESS) {
        PRINT_DEBUGGER_ERROR_LOG("Reading Context State Save Area Version Header failed, error = %d\n", retVal);
        return;
    }

    auto pStateSaveArea = reinterpret_cast<const NEO::StateSaveAreaHeader *>(data.data());
    if (0 == strcmp(pStateSaveArea->versionHeader.magic, "tssarea")) {
        size_t size = pStateSaveArea->versionHeader.size * 8u;
        size_t regHeaderSize = 0;
        if (pStateSaveArea->versionHeader.version.major == 3) {
            DEBUG_BREAK_IF(size != sizeof(NEO::StateSaveAreaHeader));
            regHeaderSize = sizeof(SIP::intelgt_state_save_area_V3);
        } else if (pStateSaveArea->versionHeader.version.major < 3) {
            DEBUG_BREAK_IF(size != sizeof(NEO::StateSaveAreaHeader::regHeader) + sizeof(NEO::StateSaveAreaHeader::versionHeader));
            regHeaderSize = sizeof(SIP::intelgt_state_save_area);
        } else {
            PRINT_DEBUGGER_ERROR_LOG("Setting Context State Save Area: unsupported version == %d.%d.%d\n", (int)pStateSaveArea->versionHeader.version.major, (int)pStateSaveArea->versionHeader.version.minor, (int)pStateSaveArea->versionHeader.version.patch);
            DEBUG_BREAK_IF(true);
            return;
        }
        auto retVal = readGpuMemory(vmHandle, data.data() + sizeof(SIP::StateSaveArea), regHeaderSize, gpuVa + sizeof(SIP::StateSaveArea));
        if (retVal != ZE_RESULT_SUCCESS) {
            PRINT_DEBUGGER_ERROR_LOG("Reading Context State Save Area Reg Header failed, error = %d\n", retVal);
            return;
        }
        stateSaveAreaHeader.assign(data.begin(), data.begin() + size);
        PRINT_DEBUGGER_INFO_LOG("Context State Save Area : version == %d.%d.%d\n", (int)pStateSaveArea->versionHeader.version.major, (int)pStateSaveArea->versionHeader.version.minor, (int)pStateSaveArea->versionHeader.version.patch);
        slmSipVersionCheck();
    } else {
        PRINT_DEBUGGER_ERROR_LOG("Setting Context State Save Area: failed to match magic numbers\n", "");
    }
}

void DebugSessionImp::slmSipVersionCheck() {

    SIP::version sipVersion = getStateSaveAreaHeader()->versionHeader.version;
    if ((sipVersion.major < minSlmSipVersion.major) ||
        ((sipVersion.major == minSlmSipVersion.major) && (sipVersion.minor < minSlmSipVersion.minor)) ||
        ((sipVersion.major == minSlmSipVersion.major) && (sipVersion.minor == minSlmSipVersion.minor) && (sipVersion.patch < minSlmSipVersion.patch))) {

        sipSupportsSlm = false;
    } else {
        sipSupportsSlm = true;
    }
}

const NEO::StateSaveAreaHeader *DebugSessionImp::getStateSaveAreaHeader() {
    if (stateSaveAreaHeader.empty()) {
        readStateSaveAreaHeader();
    }
    return reinterpret_cast<NEO::StateSaveAreaHeader *>(stateSaveAreaHeader.data());
}

const SIP::regset_desc *DebugSessionImp::getModeFlagsRegsetDesc() {
    static const SIP::regset_desc mode = {0, 1, 32, 4};
    return &mode;
}

const SIP::regset_desc *DebugSessionImp::getDebugScratchRegsetDesc() {
    static const SIP::regset_desc debugScratch = {0, 2, 64, 8};
    return &debugScratch;
}

const SIP::regset_desc *DebugSessionImp::getThreadScratchRegsetDesc() {
    static const SIP::regset_desc threadScratch = {0, 2, 64, 8};
    return &threadScratch;
}

bool DebugSessionImp::isHeaplessMode(L0::Device *device, const SIP::intelgt_state_save_area_V3 &ssa) {
    if (device->getNEODevice()->getSipExternalLibInterface()) {
        return true;
    }
    return (ssa.sip_flags & SIP::SIP_FLAG_HEAPLESS);
}

const SIP::regset_desc *DebugSessionImp::getSbaRegsetDesc(L0::Device *device, const NEO::StateSaveAreaHeader &ssah) {

    static const SIP::regset_desc sbaHeapless = {0, 0, 0, 0};
    static const SIP::regset_desc sba = {0, ZET_DEBUG_SBA_COUNT_INTEL_GPU, 64, 8};
    if (ssah.versionHeader.version.major > 3) {
        DEBUG_BREAK_IF(true);
        PRINT_DEBUGGER_ERROR_LOG("Unsupported version of State Save Area Header\n", "");
        return nullptr;
    } else if (ssah.versionHeader.version.major == 3 && isHeaplessMode(device, ssah.regHeaderV3)) {
        return &sbaHeapless;
    } else {
        return &sba;
    }
}

const SIP::regset_desc *DebugSessionImp::typeToRegsetDesc(const NEO::StateSaveAreaHeader *pStateSaveAreaHeader, uint32_t type, L0::Device *device) {

    if (pStateSaveAreaHeader == nullptr) {
        DEBUG_BREAK_IF(pStateSaveAreaHeader == nullptr);
        return nullptr;
    }

    auto sipLibInterface = device->getNEODevice()->getSipExternalLibInterface();
    if (sipLibInterface) {
        switch (type) {
        case ZET_DEBUG_REGSET_TYPE_MODE_FLAGS_INTEL_GPU:
            return DebugSessionImp::getModeFlagsRegsetDesc();
        case ZET_DEBUG_REGSET_TYPE_DEBUG_SCRATCH_INTEL_GPU:
            return DebugSessionImp::getDebugScratchRegsetDesc();
        case ZET_DEBUG_REGSET_TYPE_THREAD_SCRATCH_INTEL_GPU:
            return DebugSessionImp::getThreadScratchRegsetDesc();
        case ZET_DEBUG_REGSET_TYPE_SBA_INTEL_GPU: {
            auto &stateSaveAreaHeader = NEO::SipKernel::getDebugSipKernel(*device->getNEODevice()).getStateSaveAreaHeader();
            auto pStateSaveArea = reinterpret_cast<const NEO::StateSaveAreaHeader *>(stateSaveAreaHeader.data());
            return DebugSessionImp::getSbaRegsetDesc(device, *pStateSaveArea);
        }
        default:
            return DebugSessionImp::getRegsetDesc(static_cast<zet_debug_regset_type_intel_gpu_t>(type), sipLibInterface);
        }
    }

    if (pStateSaveAreaHeader->versionHeader.version.major == 3) {
        switch (type) {
        case ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.grf;
        case ZET_DEBUG_REGSET_TYPE_ADDR_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.addr;
        case ZET_DEBUG_REGSET_TYPE_FLAG_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.flag;
        case ZET_DEBUG_REGSET_TYPE_CE_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.emask;
        case ZET_DEBUG_REGSET_TYPE_SR_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.sr;
        case ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.cr;
        case ZET_DEBUG_REGSET_TYPE_TDR_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.tdr;
        case ZET_DEBUG_REGSET_TYPE_ACC_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.acc;
        case ZET_DEBUG_REGSET_TYPE_MME_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.mme;
        case ZET_DEBUG_REGSET_TYPE_SP_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.sp;
        case ZET_DEBUG_REGSET_TYPE_DBG_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.dbg_reg;
        case ZET_DEBUG_REGSET_TYPE_FC_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.fc;
        case ZET_DEBUG_REGSET_TYPE_MODE_FLAGS_INTEL_GPU:
            return DebugSessionImp::getModeFlagsRegsetDesc();
        case ZET_DEBUG_REGSET_TYPE_DEBUG_SCRATCH_INTEL_GPU:
            return DebugSessionImp::getDebugScratchRegsetDesc();
        case ZET_DEBUG_REGSET_TYPE_THREAD_SCRATCH_INTEL_GPU:
            return DebugSessionImp::getThreadScratchRegsetDesc();
        case ZET_DEBUG_REGSET_TYPE_SCALAR_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.scalar;
        case ZET_DEBUG_REGSET_TYPE_MSG_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeaderV3.msg;
        case ZET_DEBUG_REGSET_TYPE_SBA_INTEL_GPU: {
            auto &stateSaveAreaHeader = NEO::SipKernel::getDebugSipKernel(*device->getNEODevice()).getStateSaveAreaHeader();
            auto pStateSaveArea = reinterpret_cast<const NEO::StateSaveAreaHeader *>(stateSaveAreaHeader.data());
            return DebugSessionImp::getSbaRegsetDesc(device, *pStateSaveArea);
        }
        default:
            return nullptr;
        }
    } else if (pStateSaveAreaHeader->versionHeader.version.major < 3) {
        switch (type) {
        case ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.grf;
        case ZET_DEBUG_REGSET_TYPE_ADDR_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.addr;
        case ZET_DEBUG_REGSET_TYPE_FLAG_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.flag;
        case ZET_DEBUG_REGSET_TYPE_CE_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.emask;
        case ZET_DEBUG_REGSET_TYPE_SR_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.sr;
        case ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.cr;
        case ZET_DEBUG_REGSET_TYPE_TDR_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.tdr;
        case ZET_DEBUG_REGSET_TYPE_ACC_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.acc;
        case ZET_DEBUG_REGSET_TYPE_MME_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.mme;
        case ZET_DEBUG_REGSET_TYPE_SP_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.sp;
        case ZET_DEBUG_REGSET_TYPE_DBG_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.dbg_reg;
        case ZET_DEBUG_REGSET_TYPE_FC_INTEL_GPU:
            return &pStateSaveAreaHeader->regHeader.fc;
        case ZET_DEBUG_REGSET_TYPE_SBA_INTEL_GPU: {
            auto &stateSaveAreaHeader = NEO::SipKernel::getDebugSipKernel(*device->getNEODevice()).getStateSaveAreaHeader();
            auto pStateSaveArea = reinterpret_cast<const NEO::StateSaveAreaHeader *>(stateSaveAreaHeader.data());
            return DebugSessionImp::getSbaRegsetDesc(device, *pStateSaveArea);
        }
        default:
            return nullptr;
        }
    } else {
        PRINT_DEBUGGER_ERROR_LOG("Unsupported version of State Save Area Header\n", "");
        DEBUG_BREAK_IF(true);
        return nullptr;
    }
}

uint32_t DebugSessionImp::getRegisterSize(uint32_t type) {
    auto regset = typeToRegsetDesc(getStateSaveAreaHeader(), type, connectedDevice);
    if (regset) {
        return regset->bytes;
    }
    return 0;
}

uint32_t DebugSessionImp::typeToRegsetFlags(uint32_t type) {
    switch (type) {
    case ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_ADDR_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_FLAG_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_SR_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_ACC_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_MME_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_SP_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_DBG_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_FC_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_SCALAR_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_MSG_INTEL_GPU:
        return ZET_DEBUG_REGSET_FLAG_READABLE | ZET_DEBUG_REGSET_FLAG_WRITEABLE;

    case ZET_DEBUG_REGSET_TYPE_CE_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_TDR_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_SBA_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_MODE_FLAGS_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_DEBUG_SCRATCH_INTEL_GPU:
    case ZET_DEBUG_REGSET_TYPE_THREAD_SCRATCH_INTEL_GPU:
        return ZET_DEBUG_REGSET_FLAG_READABLE;

    default:
        return 0;
    }
}

size_t DebugSessionImp::calculateThreadSlotOffset(EuThread::ThreadId threadId) {
    auto pStateSaveAreaHeader = getStateSaveAreaHeader();
    if (pStateSaveAreaHeader->versionHeader.version.major > 3) {
        DEBUG_BREAK_IF(true);
        PRINT_DEBUGGER_ERROR_LOG("Unsupported version of State Save Area Header\n", "");
        return 0;
    } else if (pStateSaveAreaHeader->versionHeader.version.major == 3) {
        return pStateSaveAreaHeader->versionHeader.size * 8 + pStateSaveAreaHeader->regHeaderV3.state_area_offset + ((((threadId.slice * pStateSaveAreaHeader->regHeaderV3.num_subslices_per_slice + threadId.subslice) * pStateSaveAreaHeader->regHeaderV3.num_eus_per_subslice + threadId.eu) * pStateSaveAreaHeader->regHeaderV3.num_threads_per_eu + threadId.thread) * pStateSaveAreaHeader->regHeaderV3.state_save_size);
    } else {
        return pStateSaveAreaHeader->versionHeader.size * 8 + pStateSaveAreaHeader->regHeader.state_area_offset + ((((threadId.slice * pStateSaveAreaHeader->regHeader.num_subslices_per_slice + threadId.subslice) * pStateSaveAreaHeader->regHeader.num_eus_per_subslice + threadId.eu) * pStateSaveAreaHeader->regHeader.num_threads_per_eu + threadId.thread) * pStateSaveAreaHeader->regHeader.state_save_size);
    }
}

size_t DebugSessionImp::calculateRegisterOffsetInThreadSlot(const SIP::regset_desc *regdesc, uint32_t start) {
    return regdesc->offset + regdesc->bytes * start;
}

ze_result_t DebugSessionImp::readModeFlags(uint32_t start, uint32_t count, void *pRegisterValues) {
    if (start != 0 || count != 1) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }
    auto &stateSaveAreaHeader = NEO::SipKernel::getDebugSipKernel(*connectedDevice->getNEODevice()).getStateSaveAreaHeader();
    auto pStateSaveArea = reinterpret_cast<const NEO::StateSaveAreaHeader *>(stateSaveAreaHeader.data());
    const size_t size = 4;
    memcpy_s(pRegisterValues, size, &pStateSaveArea->regHeaderV3.sip_flags, size);
    return ZE_RESULT_SUCCESS;
}
ze_result_t DebugSessionImp::readDebugScratchRegisters(uint32_t start, uint32_t count, void *pRegisterValues) {

    auto debugScratchRegDesc = DebugSessionImp::getDebugScratchRegsetDesc();

    if (start >= debugScratchRegDesc->num) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }

    if (start + count > debugScratchRegDesc->num) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }

    auto info = getModuleDebugAreaInfo();

    std::vector<uint64_t> packed;
    packed.push_back(info.gpuVa);
    packed.push_back(info.size);

    size_t size = count * debugScratchRegDesc->bytes;
    memcpy_s(pRegisterValues, size, &packed[start], size);

    return ZE_RESULT_SUCCESS;
}

ze_result_t DebugSessionImp::readSbaRegisters(EuThread::ThreadId threadId, uint32_t start, uint32_t count, void *pRegisterValues) {

    auto &stateSaveAreaHeader = NEO::SipKernel::getDebugSipKernel(*connectedDevice->getNEODevice()).getStateSaveAreaHeader();
    auto pStateSaveArea = reinterpret_cast<const NEO::StateSaveAreaHeader *>(stateSaveAreaHeader.data());
    auto sbaRegDesc = DebugSessionImp::getSbaRegsetDesc(connectedDevice, *pStateSaveArea);

    if (start >= sbaRegDesc->num) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }

    if (start + count > sbaRegDesc->num) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }

    ze_result_t ret = ZE_RESULT_SUCCESS;

    alignas(64) NEO::SbaTrackedAddresses sbaBuffer{};
    ret = readSbaBuffer(threadId, sbaBuffer);
    if (ret != ZE_RESULT_SUCCESS) {
        return ret;
    }

    const auto &hwInfo = connectedDevice->getHwInfo();
    const auto regSize = std::max(getRegisterSize(ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU), hwInfo.capabilityTable.grfSize);
    auto r0 = std::make_unique<uint32_t[]>(regSize / sizeof(uint32_t));

    ret = readRegistersImp(threadId, ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU, 0, 1, r0.get());
    if (ret != ZE_RESULT_SUCCESS) {
        return ret;
    }

    uint64_t bindingTableBaseAddress = ((r0[4] >> 5) << 5) + sbaBuffer.surfaceStateBaseAddress;
    uint64_t scratchSpaceBaseAddress = 0;

    auto &gfxCoreHelper = connectedDevice->getGfxCoreHelper();
    if (gfxCoreHelper.isScratchSpaceSurfaceStateAccessible()) {
        auto surfaceStateForScratch = ((r0[5] >> 10) << 6);

        if (surfaceStateForScratch > 0) {
            uint64_t renderSurfaceStateGpuVa = surfaceStateForScratch + sbaBuffer.surfaceStateBaseAddress;
            constexpr size_t renderSurfaceStateSize = 64;
            std::vector<char> renderSurfaceState(renderSurfaceStateSize, 0);

            ret = readGpuMemory(allThreads[threadId]->getMemoryHandle(), renderSurfaceState.data(), renderSurfaceStateSize, renderSurfaceStateGpuVa);

            if (ret != ZE_RESULT_SUCCESS) {
                return ret;
            }

            auto scratchSpacePTSize = gfxCoreHelper.getRenderSurfaceStatePitch(renderSurfaceState.data(), connectedDevice->getProductHelper());
            auto threadOffset = getPerThreadScratchOffset(scratchSpacePTSize, threadId);
            auto gmmHelper = connectedDevice->getNEODevice()->getGmmHelper();
            auto scratchAllocationBase = gmmHelper->decanonize(gfxCoreHelper.getRenderSurfaceStateBaseAddress(renderSurfaceState.data()));
            if (scratchAllocationBase != 0) {
                scratchSpaceBaseAddress = threadOffset + scratchAllocationBase;
            }
        }
    } else {
        auto scratchPointer = ((r0[5] >> 10) << 10);
        if (scratchPointer != 0) {
            scratchSpaceBaseAddress = scratchPointer + sbaBuffer.generalStateBaseAddress;
        }
    }

    std::vector<uint64_t> packed;
    packed.push_back(sbaBuffer.generalStateBaseAddress);
    packed.push_back(sbaBuffer.surfaceStateBaseAddress);
    packed.push_back(sbaBuffer.dynamicStateBaseAddress);
    packed.push_back(sbaBuffer.indirectObjectBaseAddress);
    packed.push_back(sbaBuffer.instructionBaseAddress);
    packed.push_back(sbaBuffer.bindlessSurfaceStateBaseAddress);
    packed.push_back(sbaBuffer.bindlessSamplerStateBaseAddress);
    packed.push_back(bindingTableBaseAddress);
    packed.push_back(scratchSpaceBaseAddress);

    PRINT_DEBUGGER_INFO_LOG("Debug session : SBA ssh = %" SCNx64
                            " gsba = %" SCNx64
                            " dsba =  %" SCNx64
                            " ioba =  %" SCNx64
                            " iba =  %" SCNx64
                            " bsurfsba =  %" SCNx64
                            " btba =  %" SCNx64
                            " scrsba =  %" SCNx64 "\n",
                            sbaBuffer.surfaceStateBaseAddress, sbaBuffer.generalStateBaseAddress, sbaBuffer.dynamicStateBaseAddress,
                            sbaBuffer.indirectObjectBaseAddress, sbaBuffer.instructionBaseAddress, sbaBuffer.bindlessSurfaceStateBaseAddress, bindingTableBaseAddress, scratchSpaceBaseAddress);

    size_t size = count * sbaRegDesc->bytes;
    memcpy_s(pRegisterValues, size, &packed[start], size);

    return ZE_RESULT_SUCCESS;
}

void DebugSession::updateGrfRegisterSetProperties(EuThread::ThreadId thread, uint32_t *pCount, zet_debug_regset_properties_t *pRegisterSetProperties) {
    if (pRegisterSetProperties == nullptr) {
        return;
    }

    auto &l0GfxCoreHelper = connectedDevice->getNEODevice()->getRootDeviceEnvironment().getHelper<L0GfxCoreHelper>();
    auto regsetType = l0GfxCoreHelper.getRegsetTypeForLargeGrfDetection();
    const auto regSize = std::max(getRegisterSize(regsetType), 64u);
    auto reg = std::make_unique<uint32_t[]>(regSize / sizeof(uint32_t));
    memset(reg.get(), 0, regSize);
    readRegistersImp(thread, regsetType, 0, 1, reg.get());
    for (uint32_t i = 0; i < *pCount; i++) {
        if (pRegisterSetProperties[i].type == ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU) {
            pRegisterSetProperties[i].count = l0GfxCoreHelper.getGrfRegisterCount(reg.get());
        }
    }
}

ze_result_t DebugSession::getThreadRegisterSetProperties(ze_device_thread_t thread, uint32_t *pCount, zet_debug_regset_properties_t *pRegisterSetProperties) {
    if (!isSingleThread(thread)) {
        return ZE_RESULT_ERROR_NOT_AVAILABLE;
    }
    auto threadId = convertToThreadId(thread);

    if (!allThreads[threadId]->isReportedAsStopped()) {
        return ZE_RESULT_ERROR_NOT_AVAILABLE;
    }

    auto ret = getRegisterSetProperties(this->connectedDevice, pCount, pRegisterSetProperties);
    if (ret != ZE_RESULT_SUCCESS) {
        return ret;
    }

    auto sipExternalLib = this->connectedDevice->getNEODevice()->getSipExternalLibInterface();
    if (sipExternalLib) {
        getRegisterAccessProperties(&threadId, pCount, pRegisterSetProperties);
    } else {
        updateGrfRegisterSetProperties(threadId, pCount, pRegisterSetProperties);
    }
    return ret;
}

ze_result_t DebugSession::getRegisterSetProperties(Device *device, uint32_t *pCount, zet_debug_regset_properties_t *pRegisterSetProperties) {
    if (nullptr == pCount) {
        return ZE_RESULT_ERROR_INVALID_NULL_POINTER;
    }

    if (*pCount && !pRegisterSetProperties) {
        return ZE_RESULT_ERROR_INVALID_NULL_POINTER;
    }

    auto &stateSaveAreaHeader = NEO::SipKernel::getDebugSipKernel(*device->getNEODevice()).getStateSaveAreaHeader();

    if (stateSaveAreaHeader.size() == 0) {
        *pCount = 0;
        return ZE_RESULT_SUCCESS;
    }

    auto *sipLibInterface = device->getNEODevice()->getSipExternalLibInterface();

    if (sipLibInterface) {
        std::vector<char> sipBinary;
        std::vector<char> stateSaveAreaHeader;

        auto ret = sipLibInterface->getSipKernelBinary(*device->getNEODevice(), NEO::SipKernelType::dbgHeapless, sipBinary, stateSaveAreaHeader);
        if (ret != 0) {
            PRINT_DEBUGGER_ERROR_LOG("SIP external library failed to get SIP kernel binary\n", "");
            return ZE_RESULT_ERROR_UNKNOWN;
        }

        if (!sipLibInterface->createRegisterDescriptorMap()) {
            PRINT_DEBUGGER_ERROR_LOG("SIP external library failed to create register descriptor map\n", "");
            return ZE_RESULT_ERROR_UNKNOWN;
        }
    }

    uint32_t totalRegsetNum = 0;
    auto parseRegsetDesc = [&](const SIP::regset_desc &regsetDesc, zet_debug_regset_type_intel_gpu_t regsetType) {
        if (regsetDesc.num) {
            if (totalRegsetNum < *pCount) {
                uint16_t num = (regsetType == ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU) ? 128 : regsetDesc.num;
                zet_debug_regset_properties_t regsetProps = {
                    ZET_STRUCTURE_TYPE_DEBUG_REGSET_PROPERTIES,
                    nullptr,
                    static_cast<uint32_t>(regsetType),
                    0,
                    DebugSessionImp::typeToRegsetFlags(regsetType),
                    0,
                    num,
                    regsetDesc.bits,
                    regsetDesc.bytes,
                };
                pRegisterSetProperties[totalRegsetNum] = regsetProps;
            }
            ++totalRegsetNum;
        }
    };

    auto pStateSaveArea = reinterpret_cast<const NEO::StateSaveAreaHeader *>(stateSaveAreaHeader.data());

    // Define register types to process
    std::vector<zet_debug_regset_type_intel_gpu_t> regsetTypes = {
        ZET_DEBUG_REGSET_TYPE_GRF_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_ADDR_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_FLAG_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_CE_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_SR_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_CR_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_TDR_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_ACC_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_MME_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_SP_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_SBA_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_DBG_INTEL_GPU,
        ZET_DEBUG_REGSET_TYPE_FC_INTEL_GPU};

    // Add V3-specific register types
    if (pStateSaveArea->versionHeader.version.major == 3) {
        regsetTypes.insert(regsetTypes.end(), {ZET_DEBUG_REGSET_TYPE_MSG_INTEL_GPU,
                                               ZET_DEBUG_REGSET_TYPE_MODE_FLAGS_INTEL_GPU,
                                               ZET_DEBUG_REGSET_TYPE_DEBUG_SCRATCH_INTEL_GPU});

        // Conditionally add thread scratch for heapless mode
        if (DebugSessionImp::isHeaplessMode(device, pStateSaveArea->regHeaderV3)) {
            regsetTypes.push_back(ZET_DEBUG_REGSET_TYPE_THREAD_SCRATCH_INTEL_GPU);
        }

        regsetTypes.push_back(ZET_DEBUG_REGSET_TYPE_SCALAR_INTEL_GPU);
    }

    // Process each register type using the conditional helper
    for (auto regsetType : regsetTypes) {
        const SIP::regset_desc *regsetDesc = DebugSessionImp::typeToRegsetDesc(pStateSaveArea, regsetType, device);
        if (regsetDesc != nullptr) {
            parseRegsetDesc(*regsetDesc, regsetType);
        }
    }

    // Handle unsupported version case
    if (pStateSaveArea->versionHeader.version.major > 3) {
        PRINT_DEBUGGER_ERROR_LOG("Unsupported version of State Save Area Header\n", "");
        DEBUG_BREAK_IF(true);
        return ZE_RESULT_ERROR_UNKNOWN;
    }

    if (!*pCount || (*pCount > totalRegsetNum)) {
        *pCount = totalRegsetNum;
    }

    return ZE_RESULT_SUCCESS;
}

ze_result_t DebugSessionImp::registersAccessHelper(const EuThread *thread, const SIP::regset_desc *regdesc,
                                                   uint32_t start, uint32_t count, uint32_t type, void *pRegisterValues, bool write) {
    if (start >= regdesc->num) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }

    if (start + count > regdesc->num) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }

    auto gpuVa = getContextStateSaveAreaGpuVa(thread->getMemoryHandle());
    if (gpuVa == 0) {
        return ZE_RESULT_ERROR_UNKNOWN;
    }

    size_t startRegOffset = 0;
    auto sipLibInterface = connectedDevice->getNEODevice()->getSipExternalLibInterface();
    if (sipLibInterface) {
        uint32_t registerCount = 0;
        uint32_t registerStartOffset = 0;
        auto threadId = thread->getThreadId();
        struct NEO::SipLibThreadId threadIdStruct = {};
        threadIdStruct.slice = threadId.slice;
        threadIdStruct.subslice = threadId.subslice;
        threadIdStruct.eu = threadId.eu;
        threadIdStruct.thread = threadId.thread;
        sipLibInterface->getSipLibRegisterAccess(this->getSipHandle(thread->getMemoryHandle()), threadIdStruct, type, &registerCount, &registerStartOffset);
        if (start + count > registerCount) {
            return ZE_RESULT_ERROR_INVALID_ARGUMENT;
        }
        startRegOffset = static_cast<size_t>(registerStartOffset);
    } else {
        auto threadSlotOffset = calculateThreadSlotOffset(thread->getThreadId());
        startRegOffset = threadSlotOffset + calculateRegisterOffsetInThreadSlot(regdesc, start);
    }

    int ret = 0;
    if (write) {
        ret = writeGpuMemory(thread->getMemoryHandle(), static_cast<const char *>(pRegisterValues), count * regdesc->bytes, gpuVa + startRegOffset);
    } else {
        ret = readGpuMemory(thread->getMemoryHandle(), static_cast<char *>(pRegisterValues), count * regdesc->bytes, gpuVa + startRegOffset);
    }

    return ret == 0 ? ZE_RESULT_SUCCESS : ZE_RESULT_ERROR_UNKNOWN;
}

ze_result_t DebugSessionImp::cmdRegisterAccessHelper(const EuThread::ThreadId &threadId, SIP::sip_command &command, bool write) {
    auto stateSaveAreaHeader = getStateSaveAreaHeader();
    const SIP::regset_desc *regdesc = nullptr;
    if (stateSaveAreaHeader->versionHeader.version.major == 3) {
        regdesc = &stateSaveAreaHeader->regHeaderV3.cmd;
    } else if (stateSaveAreaHeader->versionHeader.version.major < 3) {
        regdesc = &stateSaveAreaHeader->regHeader.cmd;
    } else {
        PRINT_DEBUGGER_ERROR_LOG("%s: Unsupported version of State Save Area Header\n", __func__);
        DEBUG_BREAK_IF(true);
        return ZE_RESULT_ERROR_UNKNOWN;
    }

    PRINT_DEBUGGER_INFO_LOG("Access CMD %d for thread %s\n", command.command, EuThread::toString(threadId).c_str());
    uint32_t type = 0;
    if (connectedDevice->getNEODevice()->getSipExternalLibInterface()) {
        type = connectedDevice->getNEODevice()->getSipExternalLibInterface()->getSipLibCommandRegisterType();
    }
    ze_result_t result = registersAccessHelper(allThreads[threadId].get(), regdesc, 0, 1, type, &command, write);
    if (result != ZE_RESULT_SUCCESS) {
        PRINT_DEBUGGER_ERROR_LOG("Failed to access CMD for thread %s\n", EuThread::toString(threadId).c_str());
    }

    return result;
}

ze_result_t DebugSessionImp::readRegisters(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) {
    if (!isSingleThread(thread)) {
        return ZE_RESULT_ERROR_NOT_AVAILABLE;
    }
    auto threadId = convertToThreadId(thread);

    if (!allThreads[threadId]->isReportedAsStopped()) {
        return ZE_RESULT_ERROR_NOT_AVAILABLE;
    }

    auto stateSaveAreaHeader = getStateSaveAreaHeader();
    if (stateSaveAreaHeader == nullptr) {
        return ZE_RESULT_ERROR_UNKNOWN;
    }

    if (type == ZET_DEBUG_REGSET_TYPE_SBA_INTEL_GPU) {
        return readSbaRegisters(threadId, start, count, pRegisterValues);
    } else if (type == ZET_DEBUG_REGSET_TYPE_MODE_FLAGS_INTEL_GPU) {
        return readModeFlags(start, count, pRegisterValues);
    } else if (type == ZET_DEBUG_REGSET_TYPE_DEBUG_SCRATCH_INTEL_GPU) {
        return readDebugScratchRegisters(start, count, pRegisterValues);
    } else if (type == ZET_DEBUG_REGSET_TYPE_THREAD_SCRATCH_INTEL_GPU) {
        return readThreadScratchRegisters(threadId, start, count, pRegisterValues);
    }

    return readRegistersImp(threadId, type, start, count, pRegisterValues);
}

ze_result_t DebugSessionImp::readRegistersImp(EuThread::ThreadId threadId, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) {
    auto regdesc = typeToRegsetDesc(getStateSaveAreaHeader(), type, connectedDevice);
    if (nullptr == regdesc) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }
    type = getSipRegisterType(static_cast<zet_debug_regset_type_intel_gpu_t>(type));
    return registersAccessHelper(allThreads[threadId].get(), regdesc, start, count, type, pRegisterValues, false);
}

ze_result_t DebugSessionImp::writeRegisters(ze_device_thread_t thread, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) {
    if (!isSingleThread(thread)) {
        return ZE_RESULT_ERROR_NOT_AVAILABLE;
    }
    auto threadId = convertToThreadId(thread);

    if (!allThreads[threadId]->isReportedAsStopped()) {
        return ZE_RESULT_ERROR_NOT_AVAILABLE;
    }

    auto stateSaveAreaHeader = getStateSaveAreaHeader();
    if (stateSaveAreaHeader == nullptr) {
        return ZE_RESULT_ERROR_UNKNOWN;
    }

    return writeRegistersImp(threadId, type, start, count, pRegisterValues);
}

ze_result_t DebugSessionImp::writeRegistersImp(EuThread::ThreadId threadId, uint32_t type, uint32_t start, uint32_t count, void *pRegisterValues) {
    auto regdesc = typeToRegsetDesc(getStateSaveAreaHeader(), type, connectedDevice);
    if (nullptr == regdesc) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }

    if (ZET_DEBUG_REGSET_FLAG_WRITEABLE != ((DebugSessionImp::typeToRegsetFlags(type) & ZET_DEBUG_REGSET_FLAG_WRITEABLE))) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }
    type = getSipRegisterType(static_cast<zet_debug_regset_type_intel_gpu_t>(type));
    return registersAccessHelper(allThreads[threadId].get(), regdesc, start, count, type, pRegisterValues, true);
}

bool DebugSessionImp::isValidGpuAddress(const zet_debug_memory_space_desc_t *desc) const {

    if (desc->type == ZET_DEBUG_MEMORY_SPACE_TYPE_DEFAULT) {
        auto gmmHelper = connectedDevice->getNEODevice()->getGmmHelper();
        auto decanonizedAddress = gmmHelper->decanonize(desc->address);
        bool validAddress = gmmHelper->isValidCanonicalGpuAddress(desc->address);

        if (desc->address == decanonizedAddress || validAddress) {
            return true;
        }
    } else if (desc->type == ZET_DEBUG_MEMORY_SPACE_TYPE_SLM) {
        if (desc->address & (1 << slmAddressSpaceTag)) { // IGC sets bit 28 to identify SLM address
            return true;
        }
    }

    return false;
}

ze_result_t DebugSessionImp::validateThreadAndDescForMemoryAccess(ze_device_thread_t thread, const zet_debug_memory_space_desc_t *desc) {
    if (!isValidGpuAddress(desc)) {
        return ZE_RESULT_ERROR_INVALID_ARGUMENT;
    }

    ze_result_t status = sanityMemAccessThreadCheck(thread, desc);
    if (status != ZE_RESULT_SUCCESS) {
        return status;
    }
    return ZE_RESULT_SUCCESS;
}

ze_result_t DebugSessionImp::waitForCmdReady(EuThread::ThreadId threadId, uint16_t retryCount) {
    ze_result_t status;
    SIP::sip_command sipCommand = {0};

    for (uint16_t attempts = 0; attempts < retryCount; attempts++) {
        status = cmdRegisterAccessHelper(threadId, sipCommand, false);
        if (status != ZE_RESULT_SUCCESS) {
            return status;
        }

        if (sipCommand.command == static_cast<uint32_t>(NEO::SipKernel::Command::ready)) {
            break;
        }
        NEO::sleep(std::chrono::microseconds(100));
    }

    if (sipCommand.command != static_cast<uint32_t>(NEO::SipKernel::Command::ready)) {
        return ZE_RESULT_ERROR_NOT_AVAILABLE;
    }

    return ZE_RESULT_SUCCESS;
}

void DebugSessionImp::getNotStoppedThreads(const std::vector<EuThread::ThreadId> &threadsWithAtt, std::vector<EuThread::ThreadId> &notStoppedThreads) {
    for (const auto &threadId : threadsWithAtt) {

        bool wasStopped = false;

        if (tileSessionsEnabled) {
            wasStopped = tileSessions[threadId.tileIndex].first->allThreads[threadId]->isStopped();
        } else {
            wasStopped = allThreads[threadId]->isStopped();
        }

        if (!wasStopped) {
            notStoppedThreads.push_back(threadId);
        }
    }
}

ze_result_t DebugSessionImp::isValidNode(uint64_t vmHandle, uint64_t gpuVa, SIP::fifo_node &node) {
    constexpr uint32_t failsafeTimeoutMax = 100, failsafeTimeoutWait = 50;
    uint32_t timeCount = 0;
    while (!node.valid && (timeCount < failsafeTimeoutMax)) {
        auto retVal = readGpuMemory(vmHandle, reinterpret_cast<char *>(&node), sizeof(SIP::fifo_node), gpuVa);
        if (retVal != ZE_RESULT_SUCCESS) {
            PRINT_DEBUGGER_ERROR_LOG("Reading FIFO failed, error = %d\n", retVal);
            return retVal;
        }
        NEO::sleep(std::chrono::milliseconds(failsafeTimeoutWait));
        timeCount += failsafeTimeoutWait;
    }
    return ZE_RESULT_SUCCESS;
}

ze_result_t DebugSessionImp::readFifo(uint64_t vmHandle, std::vector<EuThread::ThreadId> &threadsWithAttention) {
    auto stateSaveAreaHeader = getStateSaveAreaHeader();
    if (!stateSaveAreaHeader) {
        return ZE_RESULT_ERROR_UNKNOWN;
    } else if (stateSaveAreaHeader->versionHeader.version.major != 3) {
        return ZE_RESULT_SUCCESS;
    }

    auto gpuVa = getContextStateSaveAreaGpuVa(vmHandle);

    // Drain the fifo
    uint32_t drainRetries = 2, lastHead = ~0u;
    const uint64_t offsetTail = (sizeof(SIP::StateSaveArea)) + offsetof(struct SIP::intelgt_state_save_area_V3, fifo_tail);
    const uint64_t offsetFifoSize = (sizeof(SIP::StateSaveArea)) + offsetof(struct SIP::intelgt_state_save_area_V3, fifo_size);
    const uint64_t offsetFifo = gpuVa + (stateSaveAreaHeader->versionHeader.size * 8) + stateSaveAreaHeader->regHeaderV3.fifo_offset;

    while (drainRetries--) {
        constexpr uint32_t failsafeTimeoutWait = 50;
        std::vector<uint32_t> fifoIndices(3);
        uint32_t fifoHeadIndex = 0, fifoTailIndex = 0, fifoSize = 0;

        auto retVal = readGpuMemory(vmHandle, reinterpret_cast<char *>(fifoIndices.data()), fifoIndices.size() * sizeof(uint32_t), gpuVa + offsetFifoSize);
        if (retVal != ZE_RESULT_SUCCESS) {
            PRINT_DEBUGGER_ERROR_LOG("Reading FIFO indices failed, error = %d\n", retVal);
            return retVal;
        }
        fifoSize = fifoIndices[0];
        fifoHeadIndex = fifoIndices[1];
        fifoTailIndex = fifoIndices[2];

        if (lastHead != fifoHeadIndex) {
            drainRetries++;
        }
        PRINT_DEBUGGER_FIFO_LOG("fifoHeadIndex: %u fifoTailIndex: %u fifoSize: %u lastHead: %u drainRetries: %u\n",
                                fifoHeadIndex, fifoTailIndex, fifoSize, lastHead, drainRetries);

        lastHead = fifoHeadIndex;
        bool updateTailIndex = false;
        while (fifoTailIndex != fifoHeadIndex) {
            uint32_t readSize = fifoTailIndex < fifoHeadIndex ? fifoHeadIndex - fifoTailIndex : fifoSize - fifoTailIndex;
            std::vector<SIP::fifo_node> nodes(readSize);
            uint64_t currentFifoOffset = offsetFifo + (sizeof(SIP::fifo_node) * fifoTailIndex);

            retVal = readGpuMemory(vmHandle, reinterpret_cast<char *>(nodes.data()), readSize * sizeof(SIP::fifo_node), currentFifoOffset);
            if (retVal != ZE_RESULT_SUCCESS) {
                PRINT_DEBUGGER_ERROR_LOG("Reading FIFO failed, error = %d\n", retVal);
                return retVal;
            }
            for (uint32_t i = 0; i < readSize; i++) {
                const uint64_t gpuVa = currentFifoOffset + (i * sizeof(SIP::fifo_node));
                PRINT_DEBUGGER_FIFO_LOG("Validate entry at index %u in SW Fifo:: vmHandle: %" SCNx64
                                        " gpuVa = %" SCNx64
                                        " valid = %" SCNx8
                                        " slice = %" SCNx8
                                        " subslice = %" SCNx8
                                        " eu = %" SCNx8
                                        " thread = %" SCNx8
                                        "\n",
                                        (i + fifoTailIndex), vmHandle, gpuVa, nodes[i].valid, nodes[i].slice_id, nodes[i].subslice_id, nodes[i].eu_id, nodes[i].thread_id);
                retVal = isValidNode(vmHandle, gpuVa, nodes[i]);
                if (retVal != ZE_RESULT_SUCCESS) {
                    return retVal;
                }
                UNRECOVERABLE_IF(!nodes[i].valid);
                threadsWithAttention.emplace_back(0, nodes[i].slice_id, nodes[i].subslice_id, nodes[i].eu_id, nodes[i].thread_id);
                nodes[i].valid = 0;
            }
            retVal = writeGpuMemory(vmHandle, reinterpret_cast<char *>(nodes.data()), readSize * sizeof(SIP::fifo_node), currentFifoOffset);
            if (retVal != ZE_RESULT_SUCCESS) {
                PRINT_DEBUGGER_ERROR_LOG("Writing FIFO failed, error = %d\n", retVal);
                return retVal;
            }

            if (fifoTailIndex < fifoHeadIndex) {
                // then we read to the head and are done
                fifoTailIndex = fifoHeadIndex;
            } else {
                // wrap around
                fifoTailIndex = 0;
            }
            updateTailIndex = true;
        }

        if (updateTailIndex) {
            retVal = writeGpuMemory(vmHandle, reinterpret_cast<char *>(&fifoTailIndex), sizeof(uint32_t), gpuVa + offsetTail);
            if (retVal != ZE_RESULT_SUCCESS) {
                PRINT_DEBUGGER_ERROR_LOG("Writing FIFO failed, error = %d\n", retVal);
                return retVal;
            }
            NEO::sleep(std::chrono::milliseconds(failsafeTimeoutWait));
        } else {
            break;
        }
    }
    return ZE_RESULT_SUCCESS;
}

void DebugSessionImp::pollFifo() {
    if (attentionEventContext.empty()) {
        return;
    }
    auto now = std::chrono::steady_clock::now();
    auto currentTime = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());

    auto timeSinceLastFifoRead = currentTime - lastFifoReadTime;
    if (timeSinceLastFifoRead.count() > fifoPollInterval) {
        PRINT_DEBUGGER_FIFO_LOG("%s", "Polling FIFO start\n");
        handleStoppedThreads();
        PRINT_DEBUGGER_FIFO_LOG("%s", "Polling FIFO ends\n");
    }
}

void DebugSessionImp::handleStoppedThreads() {
    for (const auto &entry : attentionEventContext) {
        auto vmHandle = entry.first;
        std::vector<EuThread::ThreadId> threadsWithAttention;
        auto result = readFifo(vmHandle, threadsWithAttention);
        if (result != ZE_RESULT_SUCCESS) {
            return;
        }
        auto now = std::chrono::steady_clock::now();
        lastFifoReadTime = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());

        if (threadsWithAttention.empty()) {
            return;
        }
        updateStoppedThreadsAndCheckTriggerEvents(entry.second, 0, threadsWithAttention);
    }
}

} // namespace L0