aboutsummaryrefslogtreecommitdiff
path: root/src/zen/trace/symbol_resolver.cpp
blob: 53374cd649d4a576751a92065da116878b7281c8 (plain) (blame)
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
// Copyright Epic Games, Inc. All Rights Reserved.

#include "symbol_resolver.h"

#include <zencore/filesystem.h>
#include <zencore/fmtutils.h>
#include <zencore/logging.h>
#include <zencore/process.h>
#include <zencore/string.h>
#include <zenhttp/httpclient.h>

#include <algorithm>
#include <filesystem>
#include <mutex>
#include <unordered_map>
#include <vector>

#if !ZEN_PLATFORM_WINDOWS
#	include <cerrno>
#	include <unistd.h>
#endif

#if ZEN_PLATFORM_WINDOWS

ZEN_THIRD_PARTY_INCLUDES_START
#	include <Foundation/PDB_PointerUtil.h>
#	include <PDB.h>
#	include <PDB_CoalescedMSFStream.h>
#	include <PDB_DBIStream.h>
#	include <PDB_ImageSectionStream.h>
#	include <PDB_InfoStream.h>
#	include <PDB_ModuleInfoStream.h>
#	include <PDB_ModuleLineStream.h>
#	include <PDB_ModuleSymbolStream.h>
#	include <PDB_PublicSymbolStream.h>
#	include <PDB_RawFile.h>
ZEN_THIRD_PARTY_INCLUDES_END

#	include <zencore/windows.h>

ZEN_THIRD_PARTY_INCLUDES_START
#	include <DbgHelp.h>
ZEN_THIRD_PARTY_INCLUDES_END

#endif	// ZEN_PLATFORM_WINDOWS

namespace zen::trace_detail {

//////////////////////////////////////////////////////////////////////////////
// Null resolver (used when symbolication is off or unsupported)

class NullSymbolResolver final : public SymbolResolver
{
public:
	void		LoadModule(const ModuleInfo&) override {}
	std::string Resolve(uint64_t) const override { return {}; }
};

#if ZEN_PLATFORM_WINDOWS

//////////////////////////////////////////////////////////////////////////////
// Helpers shared by Windows backends

static std::string
FormatSymbol(std::string_view Name, uint64_t Displacement)
{
	if (Displacement == 0)
	{
		return std::string(Name);
	}
	return fmt::format("{} + 0x{:X}", Name, Displacement);
}

//////////////////////////////////////////////////////////////////////////////
// Memory-mapped file helper

namespace {

	struct MappedFile
	{
		const void* Data		  = nullptr;
		size_t		Size		  = 0;
		HANDLE		FileHandle	  = INVALID_HANDLE_VALUE;
		HANDLE		MappingHandle = nullptr;

		MappedFile() = default;
		~MappedFile() { Close(); }

		bool Open(const std::filesystem::path& Path)
		{
			FileHandle = CreateFileW(Path.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr);
			if (FileHandle == INVALID_HANDLE_VALUE)
			{
				return false;
			}

			LARGE_INTEGER FileSize;
			if (!GetFileSizeEx(FileHandle, &FileSize) || FileSize.QuadPart == 0)
			{
				Close();
				return false;
			}

			MappingHandle = CreateFileMappingW(FileHandle, nullptr, PAGE_READONLY, 0, 0, nullptr);
			if (MappingHandle == nullptr)
			{
				Close();
				return false;
			}

			Data = MapViewOfFile(MappingHandle, FILE_MAP_READ, 0, 0, 0);
			if (Data == nullptr)
			{
				Close();
				return false;
			}

			Size = size_t(FileSize.QuadPart);
			return true;
		}

		void Close()
		{
			if (Data != nullptr)
			{
				UnmapViewOfFile(Data);
				Data = nullptr;
			}
			if (MappingHandle != nullptr)
			{
				CloseHandle(MappingHandle);
				MappingHandle = nullptr;
			}
			if (FileHandle != INVALID_HANDLE_VALUE)
			{
				CloseHandle(FileHandle);
				FileHandle = INVALID_HANDLE_VALUE;
			}
			Size = 0;
		}

		MappedFile(const MappedFile&) = delete;
		MappedFile& operator=(const MappedFile&) = delete;
	};

	// Format an ImageId (16-byte GUID + 4-byte Age) as the hex key used in
	// symbol server URLs: <GUID_no_dashes><Age_hex>, e.g. "A1B2C3...1".
	std::string FormatImageIdKey(const eastl::vector<uint8_t>& ImageId)
	{
		if (ImageId.size() < 20)
		{
			return {};
		}

		// GUID bytes are stored as {Data1 LE, Data2 LE, Data3 LE, Data4[8]}.
		// The symbol server key encodes Data1/2/3 as big-endian hex.
		const uint8_t* G = ImageId.data();

		uint32_t Data1;
		uint16_t Data2;
		uint16_t Data3;
		memcpy(&Data1, G + 0, 4);
		memcpy(&Data2, G + 4, 2);
		memcpy(&Data3, G + 6, 2);

		uint32_t Age;
		memcpy(&Age, ImageId.data() + 16, 4);

		return fmt::format("{:08X}{:04X}{:04X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}{:x}",
						   Data1,
						   Data2,
						   Data3,
						   G[8],
						   G[9],
						   G[10],
						   G[11],
						   G[12],
						   G[13],
						   G[14],
						   G[15],
						   Age);
	}

	// PdbName originates from module metadata in an untrusted trace file and is used
	// to build both a filesystem path and an HTTP request path. Restrict it to a safe
	// subset so a malicious trace cannot traverse out of the cache dir, inject URL
	// syntax, or trip cross-platform path parsing quirks (e.g. `\` is a separator on
	// Windows but not POSIX, so filename() doesn't always strip it).
	bool IsSafePdbName(std::string_view Name)
	{
		constexpr AsciiSet SafeChars(
			"abcdefghijklmnopqrstuvwxyz"
			"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
			"0123456789"
			"._-+");

		if (Name.empty() || Name.size() > 255 || Name == "." || Name == "..")
		{
			return false;
		}
		return AsciiSet::HasOnly(Name, SafeChars);
	}

	const std::filesystem::path& GetSymbolCacheDir()
	{
		// Use %TEMP%/zen-symbols as the default cache location.
		static const std::filesystem::path s_CacheDir = [] {
			std::filesystem::path TempDir = std::filesystem::temp_directory_path();
			return TempDir / "zen-symbols";
		}();
		return s_CacheDir;
	}

	// Try to download a PDB from a symbol server URL.
	// Returns the local cache path on success, empty path on failure.
	std::filesystem::path DownloadPdb(std::string_view			   ServerUrl,
									  std::string_view			   PdbName,
									  const std::string&		   ImageIdKey,
									  const std::filesystem::path& CacheDir)
	{
		// Cache path mirrors the server structure
		std::filesystem::path CachePath = CacheDir / PdbName / ImageIdKey / PdbName;

		// Already cached?
		std::error_code Ec;
		if (std::filesystem::exists(CachePath, Ec))
		{
			return CachePath;
		}

		ZEN_INFO("Downloading {} from symbol server...", PdbName);

		try
		{
			std::string RequestPath = fmt::format("/{}/{}/{}", PdbName, ImageIdKey, PdbName);

			zen::HttpClientSettings Settings;
			Settings.Timeout			= std::chrono::milliseconds(30000);
			Settings.ConnectTimeout		= std::chrono::milliseconds(5000);
			Settings.FollowRedirects	= true;
			Settings.ExpectedErrorCodes = {zen::HttpResponseCode::NotFound};

			zen::HttpClient Http(ServerUrl, Settings);

			zen::HttpClient::Response Response = Http.Get(RequestPath);

			if (!Response)
			{
				ZEN_DEBUG("Symbol server: {} not found (HTTP {})", PdbName, int(Response.StatusCode));
				return {};
			}

			// Write to cache using zencore file I/O
			std::filesystem::create_directories(CachePath.parent_path(), Ec);
			zen::WriteFile(CachePath, Response.ResponsePayload);

			ZEN_INFO("Cached {} ({})", PdbName, zen::NiceBytes(Response.ResponsePayload.GetSize()));
			return CachePath;
		}
		catch (const std::exception& Ex)
		{
			ZEN_WARN("Symbol server download failed for {}: {}", PdbName, Ex.what());
			return {};
		}
	}

	// Parse _NT_SYMBOL_PATH to extract symbol server URLs.
	// Format: "srv*<cache>*<url>" or "symsrv*symsrv.dll*<cache>*<url>" or just a URL.
	// Returns a list of server URLs to try.
	const std::vector<std::string>& ParseSymbolPath()
	{
		static const std::vector<std::string> s_Servers = [] {
			std::vector<std::string> Servers;

			const char* EnvPath = std::getenv("_NT_SYMBOL_PATH");
			if (EnvPath == nullptr || EnvPath[0] == '\0')
			{
				// Default to Microsoft public symbol server
				Servers.push_back("https://msdl.microsoft.com/download/symbols");
				return Servers;
			}

			std::string_view Path(EnvPath);

			// Split on ';' for multiple entries
			while (!Path.empty())
			{
				size_t			 Semi  = Path.find(';');
				std::string_view Entry = (Semi != std::string_view::npos) ? Path.substr(0, Semi) : Path;
				Path				   = (Semi != std::string_view::npos) ? Path.substr(Semi + 1) : std::string_view{};

				// Look for srv* or symsrv* prefix — the last '*'-delimited token is the server URL.
				if (Entry.substr(0, 4) == "srv*" || Entry.substr(0, 7) == "symsrv*")
				{
					size_t LastStar = Entry.rfind('*');
					if (LastStar != std::string_view::npos && LastStar + 1 < Entry.size())
					{
						std::string_view Url = Entry.substr(LastStar + 1);
						if (Url.substr(0, 4) == "http")
						{
							Servers.emplace_back(Url);
						}
					}
				}
			}

			if (Servers.empty())
			{
				Servers.push_back("https://msdl.microsoft.com/download/symbols");
			}

			return Servers;
		}();
		return s_Servers;
	}

	// Copy a local PDB into the symbol cache so that future analysis of traces
	// from this build succeeds even after the binary is recompiled.
	void CacheLocalPdb(const std::filesystem::path& PdbPath,
					   std::string_view				PdbName,
					   const std::string&			ImageIdKey,
					   const std::filesystem::path& CacheDir)
	{
		std::filesystem::path CachePath = CacheDir / PdbName / ImageIdKey / PdbName;
		std::error_code		  Ec;
		if (std::filesystem::exists(CachePath, Ec))
		{
			return;
		}

		std::filesystem::create_directories(CachePath.parent_path(), Ec);
		if (Ec)
		{
			return;
		}

		std::filesystem::copy_file(PdbPath, CachePath, std::filesystem::copy_options::skip_existing, Ec);
		if (!Ec)
		{
			uint64_t Size = std::filesystem::file_size(PdbPath, Ec);
			ZEN_INFO("Cached local PDB {} ({})", PdbName, zen::NiceBytes(Size));
		}
	}

	// Look for a PDB in the local symbol cache or download from symbol servers.
	// Returns the cache path on success, empty path on failure.
	std::filesystem::path FindPdbInCacheOrServer(std::string_view			  PdbName,
												 const std::string&			  ImageIdKey,
												 const std::filesystem::path& CacheDir)
	{
		if (ImageIdKey.empty())
		{
			return {};
		}

		// Check local cache first (includes previously cached local PDBs and
		// earlier symbol server downloads).
		std::filesystem::path CachePath = CacheDir / PdbName / ImageIdKey / PdbName;
		std::error_code		  Ec;
		if (std::filesystem::exists(CachePath, Ec))
		{
			return CachePath;
		}

		// Try symbol servers
		const std::vector<std::string>& Servers = ParseSymbolPath();
		for (const std::string& Server : Servers)
		{
			std::filesystem::path Downloaded = DownloadPdb(Server, PdbName, ImageIdKey, CacheDir);
			if (!Downloaded.empty())
			{
				return Downloaded;
			}
		}

		return {};
	}

}  // namespace

//////////////////////////////////////////////////////////////////////////////
// RawPdb backend — reads PDB files directly

class PdbSymbolResolver final : public SymbolResolver
{
public:
	void		LoadModule(const ModuleInfo& Module) override;
	std::string Resolve(uint64_t Address) const override;

private:
	struct FunctionEntry
	{
		uint64_t	Address;
		uint32_t	Size;
		std::string Name;
	};

	struct LineEntry
	{
		uint64_t	Address;
		uint32_t	CodeSize;
		uint32_t	Line;
		std::string File;  // shortened: basename only
	};

	std::vector<FunctionEntry> m_Functions;
	std::vector<LineEntry>	   m_Lines;
};

void
PdbSymbolResolver::LoadModule(const ModuleInfo& Module)
{
	if (Module.FullPath.empty() || Module.Base == 0)
	{
		return;
	}

	std::string					 ImageIdKey = FormatImageIdKey(Module.ImageId);
	std::string					 PdbName	= std::filesystem::path(Module.FullPath).filename().replace_extension(".pdb").string();
	const std::filesystem::path& CacheDir	= GetSymbolCacheDir();

	if (!IsSafePdbName(PdbName))
	{
		ZEN_WARN("Rejecting unsafe PDB name from trace: '{}'", PdbName);
		return;
	}

	// Try local PDB first (next to the binary)
	std::filesystem::path PdbPath(Module.FullPath);
	PdbPath.replace_extension(".pdb");

	std::error_code Ec;
	bool			FromLocal = false;

	if (std::filesystem::exists(PdbPath, Ec))
	{
		FromLocal = true;
	}
	else
	{
		// Try symbol cache / symbol server download
		PdbPath = FindPdbInCacheOrServer(PdbName, ImageIdKey, CacheDir);
		if (PdbPath.empty())
		{
			ZEN_DEBUG("PDB not found locally or on symbol server: {}", PdbName);
			return;
		}
	}

	MappedFile File;
	if (!File.Open(PdbPath))
	{
		ZEN_DEBUG("Failed to open PDB: {}", PdbPath.string());
		return;
	}

	if (PDB::ValidateFile(File.Data, File.Size) != PDB::ErrorCode::Success)
	{
		ZEN_DEBUG("Invalid PDB file: {}", PdbPath.string());
		return;
	}

	PDB::RawFile	RawFile = PDB::CreateRawFile(File.Data);
	PDB::InfoStream PdbInfoStream(RawFile);

	// Verify the PDB matches the traced module by comparing GUID + Age.
	// The trace stores ImageId as 16 bytes GUID followed by 4 bytes Age.
	if (Module.ImageId.size() >= 20)
	{
		const PDB::Header* PdbHeader = PdbInfoStream.GetHeader();

		// Only compare the GUID, not the age. The symbol server may return a
		// PDB with a higher age (from incremental linking) which is compatible.
		if (memcmp(&PdbHeader->guid, Module.ImageId.data(), 16) != 0)
		{
			if (FromLocal)
			{
				// The local PDB no longer matches — the binary was recompiled
				// since the trace was taken. Try the symbol cache / servers for
				// the original PDB.
				File.Close();
				PdbPath = FindPdbInCacheOrServer(PdbName, ImageIdKey, CacheDir);
				if (PdbPath.empty())
				{
					ZEN_WARN("PDB mismatch for {} — binary was recompiled and no cached symbols available", Module.Name);
					return;
				}

				FromLocal = false;

				if (!File.Open(PdbPath))
				{
					ZEN_DEBUG("Failed to open cached PDB: {}", PdbPath.string());
					return;
				}

				if (PDB::ValidateFile(File.Data, File.Size) != PDB::ErrorCode::Success)
				{
					ZEN_DEBUG("Invalid cached PDB: {}", PdbPath.string());
					return;
				}

				RawFile		  = PDB::CreateRawFile(File.Data);
				PdbInfoStream = PDB::InfoStream(RawFile);

				const PDB::Header* CachedHeader = PdbInfoStream.GetHeader();
				if (memcmp(&CachedHeader->guid, Module.ImageId.data(), 16) != 0)
				{
					ZEN_WARN("PDB GUID mismatch for {} — skipping", Module.Name);
					return;
				}
			}
			else
			{
				ZEN_WARN("PDB GUID mismatch for {} — skipping", Module.Name);
				return;
			}
		}
	}

	// Cache the local PDB so that future analysis of traces from this build
	// succeeds even after the binary is recompiled.
	if (FromLocal && !ImageIdKey.empty())
	{
		CacheLocalPdb(PdbPath, PdbName, ImageIdKey, CacheDir);
	}

	if (PDB::HasValidDBIStream(RawFile) != PDB::ErrorCode::Success)
	{
		return;
	}

	const PDB::DBIStream DbiStream = PDB::CreateDBIStream(RawFile);
	if (DbiStream.HasValidImageSectionStream(RawFile) != PDB::ErrorCode::Success)
	{
		return;
	}

	const PDB::ImageSectionStream ImageSections					   = DbiStream.CreateImageSectionStream(RawFile);
	uint64_t					  ModuleBase					   = Module.Base;
	uint32_t					  SkippedModules				   = 0;
	size_t						  FunctionCountBeforeModuleSymbols = m_Functions.size();

	// Collect functions from module symbol streams (S_GPROC32 / S_LPROC32)
	{
		const PDB::ModuleInfoStream							ModInfoStream = DbiStream.CreateModuleInfoStream(RawFile);
		const PDB::ArrayView<PDB::ModuleInfoStream::Module> Modules		  = ModInfoStream.GetModules();
		for (const PDB::ModuleInfoStream::Module& Mod : Modules)
		{
			if (!Mod.HasSymbolStream())
			{
				++SkippedModules;
				continue;
			}

			const PDB::ModuleSymbolStream SymStream = Mod.CreateSymbolStream(RawFile);

			SymStream.ForEachSymbol([&](const PDB::CodeView::DBI::Record* Record) {
				using Kind		 = PDB::CodeView::DBI::SymbolRecordKind;
				const Kind	K	 = Record->header.kind;
				const auto& Data = Record->data;

				if (K == Kind::S_GPROC32 || K == Kind::S_LPROC32 || K == Kind::S_GPROC32_ID || K == Kind::S_LPROC32_ID)
				{
					uint32_t Rva = ImageSections.ConvertSectionOffsetToRVA(Data.S_GPROC32.section, Data.S_GPROC32.offset);
					if (Rva != 0)
					{
						m_Functions.push_back({ModuleBase + Rva, Data.S_GPROC32.codeSize, Data.S_GPROC32.name});
					}
				}
			});
		}
	}

	// Public symbols as fallback only when module symbol streams did not yield any
	// functions. Building the coalesced symbol-record stream is expensive and can
	// allocate tens of megabytes for large PDBs.
	if (FunctionCountBeforeModuleSymbols == m_Functions.size() && DbiStream.HasValidPublicSymbolStream(RawFile) == PDB::ErrorCode::Success)
	{
		const PDB::PublicSymbolStream PubStream	 = DbiStream.CreatePublicSymbolStream(RawFile);
		const PDB::CoalescedMSFStream SymRecords = DbiStream.CreateSymbolRecordStream(RawFile);

		for (const PDB::HashRecord& Hash : PubStream.GetRecords())
		{
			const PDB::CodeView::DBI::Record* Record = SymRecords.GetDataAtOffset<PDB::CodeView::DBI::Record>(Hash.offset);
			if (Record->header.kind == PDB::CodeView::DBI::SymbolRecordKind::S_PUB32)
			{
				uint32_t Rva = ImageSections.ConvertSectionOffsetToRVA(Record->data.S_PUB32.section, Record->data.S_PUB32.offset);
				if (Rva != 0)
				{
					m_Functions.push_back({ModuleBase + Rva, 0, Record->data.S_PUB32.name});
				}
			}
		}
	}

	// Collect line information from module line streams
	if (PdbInfoStream.HasNamesStream())
	{
		const PDB::NamesStream		NamesStream	   = PdbInfoStream.CreateNamesStream(RawFile);
		const PDB::ModuleInfoStream ModInfoStream2 = DbiStream.CreateModuleInfoStream(RawFile);

		for (const PDB::ModuleInfoStream::Module& Mod : ModInfoStream2.GetModules())
		{
			if (!Mod.HasLineStream())
			{
				continue;
			}

			const PDB::ModuleLineStream LineStream = Mod.CreateLineStream(RawFile);

			// Two passes: first find the checksums section, then process lines.
			const PDB::CodeView::DBI::FileChecksumHeader* ModuleChecksumBase = nullptr;

			LineStream.ForEachSection([&](const PDB::CodeView::DBI::LineSection* Section) {
				if (Section->header.kind == PDB::CodeView::DBI::DebugSubsectionKind::S_FILECHECKSUMS)
				{
					ModuleChecksumBase = &Section->checksumHeader;
				}
			});

			if (ModuleChecksumBase == nullptr)
			{
				continue;
			}

			LineStream.ForEachSection([&](const PDB::CodeView::DBI::LineSection* Section) {
				if (Section->header.kind != PDB::CodeView::DBI::DebugSubsectionKind::S_LINES)
				{
					return;
				}

				uint16_t SecIdx = Section->linesHeader.sectionIndex;
				uint32_t SecOff = Section->linesHeader.sectionOffset;

				LineStream.ForEachLinesBlock(Section,
											 [&](const PDB::CodeView::DBI::LinesFileBlockHeader* Block,
												 const PDB::CodeView::DBI::Line*				 Lines,
												 const PDB::CodeView::DBI::Column*) {
												 if (Block->numLines == 0)
												 {
													 return;
												 }

												 // Resolve filename for this block
												 const auto* Checksum = PDB::Pointer::Offset<const PDB::CodeView::DBI::FileChecksumHeader*>(
													 ModuleChecksumBase,
													 Block->fileChecksumOffset);
												 const char* FullFile = NamesStream.GetFilename(Checksum->filenameOffset);

												 // Extract basename
												 std::string_view FileView(FullFile);
												 size_t			  Cut = FileView.find_last_of("\\/");
												 std::string Basename(Cut != std::string_view::npos ? FileView.substr(Cut + 1) : FileView);

												 for (uint32_t I = 0; I < Block->numLines; ++I)
												 {
													 uint32_t Rva =
														 ImageSections.ConvertSectionOffsetToRVA(SecIdx, SecOff + Lines[I].offset);
													 if (Rva == 0)
													 {
														 continue;
													 }

													 uint32_t CodeSize = 0;
													 if (I + 1 < Block->numLines)
													 {
														 CodeSize = Lines[I + 1].offset - Lines[I].offset;
													 }
													 else
													 {
														 CodeSize = Section->linesHeader.codeSize - Lines[I].offset;
													 }

													 m_Lines.push_back({ModuleBase + Rva, CodeSize, Lines[I].linenumStart, Basename});
												 }
											 });
			});
		}
	}

	std::sort(m_Functions.begin(), m_Functions.end(), [](const FunctionEntry& A, const FunctionEntry& B) { return A.Address < B.Address; });

	std::sort(m_Lines.begin(), m_Lines.end(), [](const LineEntry& A, const LineEntry& B) { return A.Address < B.Address; });

	if (SkippedModules > 0)
	{
		ZEN_INFO("Loaded {} symbols, {} line records from {} ({} modules without embedded debug info)",
				 m_Functions.size(),
				 m_Lines.size(),
				 Module.Name,
				 SkippedModules);
	}
	else
	{
		ZEN_INFO("Loaded {} symbols, {} line records from {}", m_Functions.size(), m_Lines.size(), Module.Name);
	}
}

std::string
PdbSymbolResolver::Resolve(uint64_t Address) const
{
	if (m_Functions.empty())
	{
		return {};
	}

	// Resolve function name
	auto FnIt = std::upper_bound(m_Functions.begin(), m_Functions.end(), Address, [](uint64_t Addr, const FunctionEntry& E) {
		return Addr < E.Address;
	});

	if (FnIt == m_Functions.begin())
	{
		return {};
	}

	--FnIt;

	if (FnIt->Size > 0 && Address >= FnIt->Address + FnIt->Size)
	{
		return {};
	}

	std::string Result = FormatSymbol(FnIt->Name, Address - FnIt->Address);

	// Resolve file:line
	if (!m_Lines.empty())
	{
		auto LineIt =
			std::upper_bound(m_Lines.begin(), m_Lines.end(), Address, [](uint64_t Addr, const LineEntry& E) { return Addr < E.Address; });

		if (LineIt != m_Lines.begin())
		{
			--LineIt;
			if (LineIt->CodeSize == 0 || Address < LineIt->Address + LineIt->CodeSize)
			{
				Result += fmt::format(" [{}:{}]", LineIt->File, LineIt->Line);
			}
		}
	}

	return Result;
}

//////////////////////////////////////////////////////////////////////////////
// DbgHelp backend — uses Windows symbol API, supports _NT_SYMBOL_PATH

class DbgHelpSymbolResolver final : public SymbolResolver
{
public:
	DbgHelpSymbolResolver();
	~DbgHelpSymbolResolver() override;

	void		LoadModule(const ModuleInfo& Module) override;
	std::string Resolve(uint64_t Address) const override;

private:
	// Map trace addresses to DbgHelp addresses when the loaded base differs.
	struct ModuleMapping
	{
		uint64_t TraceBase;
		uint64_t TraceEnd;
		int64_t	 Delta;	 // DbgHelpBase - TraceBase
	};

	HANDLE					   m_Process = nullptr;
	std::vector<ModuleMapping> m_Mappings;
	// DbgHelp is not thread-safe; its API functions require serialized access. This
	// mutex covers every DbgHelp call (SymInitialize/SymLoadModuleExW/SymFromAddr/
	// SymGetLineFromAddr64) and therefore serializes all parallel symbol lookups in
	// trace_analyze. For workloads where lookup throughput matters, prefer
	// PdbSymbolResolver, which parses PDBs directly and is lock-free per-module.
	mutable std::mutex m_Mutex;
};

DbgHelpSymbolResolver::DbgHelpSymbolResolver()
{
	std::lock_guard Lock(m_Mutex);

	// Use a unique pseudo-handle so we don't conflict with the runtime
	// symbol handler used by callstack.cpp / crashhandler.cpp.
	m_Process = reinterpret_cast<HANDLE>(static_cast<uintptr_t>(0xDEAD0042));

	// NULL search path lets DbgHelp use _NT_SYMBOL_PATH and its defaults.
	if (!SymInitialize(m_Process, nullptr, FALSE))
	{
		ZEN_WARN("DbgHelp: SymInitialize failed (error {})", GetLastError());
		m_Process = nullptr;
	}
}

DbgHelpSymbolResolver::~DbgHelpSymbolResolver()
{
	std::lock_guard Lock(m_Mutex);

	if (m_Process != nullptr)
	{
		SymCleanup(m_Process);
	}
}

void
DbgHelpSymbolResolver::LoadModule(const ModuleInfo& Module)
{
	std::lock_guard Lock(m_Mutex);

	if (m_Process == nullptr || Module.FullPath.empty() || Module.Base == 0)
	{
		return;
	}

	std::filesystem::path ModulePath(Module.FullPath);
	std::wstring		  WidePath = ModulePath.wstring();

	DWORD64 LoadedBase = SymLoadModuleExW(m_Process, nullptr, WidePath.c_str(), nullptr, Module.Base, Module.Size, nullptr, 0);

	if (LoadedBase == 0)
	{
		DWORD Err = GetLastError();
		if (Err != ERROR_SUCCESS)
		{
			ZEN_DEBUG("DbgHelp: failed to load {}: error {}", Module.Name, Err);
		}
		return;
	}

	int64_t Delta = int64_t(LoadedBase) - int64_t(Module.Base);
	if (Delta != 0)
	{
		ZEN_DEBUG("DbgHelp: {} loaded at 0x{:X} (trace base 0x{:X}, delta {:+})", Module.Name, LoadedBase, Module.Base, Delta);
	}

	uint64_t TraceEnd = Module.Base + (Module.Size > 0 ? Module.Size : 0x1000000);
	m_Mappings.push_back({Module.Base, TraceEnd, Delta});

	ZEN_INFO("DbgHelp: loaded symbols for {}", Module.Name);
}

std::string
DbgHelpSymbolResolver::Resolve(uint64_t Address) const
{
	std::lock_guard Lock(m_Mutex);

	if (m_Process == nullptr)
	{
		return {};
	}

	// Translate the trace address to the DbgHelp address space
	uint64_t DbgAddr = Address;
	for (const ModuleMapping& M : m_Mappings)
	{
		if (Address >= M.TraceBase && Address < M.TraceEnd)
		{
			DbgAddr = uint64_t(int64_t(Address) + M.Delta);
			break;
		}
	}

	alignas(SYMBOL_INFO) char Buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME];
	SYMBOL_INFO*			  SymInfo = reinterpret_cast<SYMBOL_INFO*>(Buffer);
	SymInfo->SizeOfStruct			  = sizeof(SYMBOL_INFO);
	SymInfo->MaxNameLen				  = MAX_SYM_NAME;

	DWORD64 Displacement = 0;
	if (!SymFromAddr(m_Process, DbgAddr, &Displacement, SymInfo))
	{
		return {};
	}

	std::string Result = FormatSymbol(std::string_view(SymInfo->Name, SymInfo->NameLen), Displacement);

	IMAGEHLP_LINE64 LineInfo = {};
	LineInfo.SizeOfStruct	 = sizeof(IMAGEHLP_LINE64);
	DWORD LineDisplacement	 = 0;
	if (SymGetLineFromAddr64(m_Process, DbgAddr, &LineDisplacement, &LineInfo))
	{
		std::string_view FileView(LineInfo.FileName);
		size_t			 Cut	  = FileView.find_last_of("\\/");
		std::string_view Basename = (Cut != std::string_view::npos) ? FileView.substr(Cut + 1) : FileView;
		Result += fmt::format(" [{}:{}]", Basename, LineInfo.LineNumber);
	}

	return Result;
}

#endif	// ZEN_PLATFORM_WINDOWS

//////////////////////////////////////////////////////////////////////////////
// Shared helpers for subprocess-based backends

namespace {

	// CreateProc parses the command line as space-separated tokens, so argv[0]
	// must be quoted if the resolved executable path contains spaces (e.g. an
	// Xcode toolchain location on macOS).
	std::string QuoteIfNeeded(std::string_view Path)
	{
		if (Path.find(' ') == std::string_view::npos)
		{
			return std::string(Path);
		}
		return fmt::format("\"{}\"", Path);
	}

}  // namespace

//////////////////////////////////////////////////////////////////////////////
// llvm-symbolizer backend — cross-platform, shells out to `llvm-symbolizer`
// and speaks its interactive protocol over pipes.
//
// Protocol (one request / response):
//   We write:   "<path-to-binary> 0x<relative-address>\n"
//   It replies: "FunctionName\n"
//               "file:line:col\n"
//               "\n"                  <-- blank line terminates the record
//
// Launch flags:
//   --demangle              demangle C++ names (default, but explicit)
//   --output-style=LLVM     stable two-line format described above
//   --functions=linkage     keep template arguments visible
//   --relative-address      treat the address as an offset from module base
//   --inlining=false        emit one frame per address (no inline expansion)

class LlvmSymbolizerResolver final : public SymbolResolver
{
public:
	LlvmSymbolizerResolver() = default;
	~LlvmSymbolizerResolver() override;

	void		LoadModule(const ModuleInfo& Module) override;
	std::string Resolve(uint64_t Address) const override;

private:
	struct Module
	{
		std::string FullPath;
		uint64_t	Base = 0;
		uint64_t	End	 = 0;
	};

	const Module* FindModule(uint64_t Address) const;
	bool		  EnsureProcess() const;
	bool		  ReadLine(std::string& Out) const;
	std::string	  DoQuery(const Module& M, uint64_t RelAddress) const;

	std::vector<Module> m_Modules;

	// Subprocess + IO state. All accesses serialized under m_Mutex.
	mutable std::mutex			   m_Mutex;
	mutable bool				   m_Attempted = false;
	mutable bool				   m_Alive	   = false;
	mutable zen::ProcessHandle	   m_Process;
	mutable zen::StdinPipeHandles  m_StdinPipe;
	mutable zen::StdoutPipeHandles m_StdoutPipe;
	mutable std::string			   m_ReadBuffer;

	// Cache resolved addresses (same mutex).
	mutable std::unordered_map<uint64_t, std::string> m_Cache;
};

LlvmSymbolizerResolver::~LlvmSymbolizerResolver()
{
	std::lock_guard Lock(m_Mutex);
	if (m_Alive)
	{
		// Closing stdin lets llvm-symbolizer exit cleanly on EOF.
		m_StdinPipe.CloseWriteEnd();
		m_Process.Wait(2000);
		if (m_Process.IsRunning())
		{
			m_Process.Terminate(0);
		}
	}
}

void
LlvmSymbolizerResolver::LoadModule(const ModuleInfo& Mod)
{
	if (Mod.FullPath.empty() || Mod.Base == 0)
	{
		return;
	}

	// llvm-symbolizer auto-discovers adjacent debug info (Foo.dSYM on Mac,
	// .gnu_debuglink / build-id sources on Linux, Foo.pdb on Windows). If the
	// binary itself isn't present locally, there's nothing we can do.
	std::error_code Ec;
	if (!std::filesystem::exists(Mod.FullPath, Ec))
	{
		ZEN_DEBUG("llvm-symbolizer: binary not found for {} at {}", Mod.Name, Mod.FullPath);
		return;
	}

	uint64_t End = Mod.Base + (Mod.Size > 0 ? Mod.Size : 0x1000000);

	std::lock_guard Lock(m_Mutex);
	m_Modules.push_back({Mod.FullPath, Mod.Base, End});
	ZEN_INFO("llvm-symbolizer: registered {} [0x{:X}..0x{:X})", Mod.Name, Mod.Base, End);
}

std::string
LlvmSymbolizerResolver::Resolve(uint64_t Address) const
{
	std::lock_guard Lock(m_Mutex);

	auto CacheIt = m_Cache.find(Address);
	if (CacheIt != m_Cache.end())
	{
		return CacheIt->second;
	}

	const Module* M = FindModule(Address);
	if (M == nullptr)
	{
		m_Cache.emplace(Address, std::string{});
		return {};
	}

	std::string Result = DoQuery(*M, Address - M->Base);
	m_Cache.emplace(Address, Result);
	return Result;
}

const LlvmSymbolizerResolver::Module*
LlvmSymbolizerResolver::FindModule(uint64_t Address) const
{
	for (const Module& M : m_Modules)
	{
		if (Address >= M.Base && Address < M.End)
		{
			return &M;
		}
	}
	return nullptr;
}

bool
LlvmSymbolizerResolver::EnsureProcess() const
{
	if (m_Attempted)
	{
		return m_Alive;
	}
	m_Attempted = true;

	std::filesystem::path Executable = SearchPathForExecutable("llvm-symbolizer");

	if (!CreateStdinPipe(m_StdinPipe) || !CreateStdoutPipe(m_StdoutPipe))
	{
		ZEN_WARN("llvm-symbolizer: failed to create pipes");
		return false;
	}

	// Build the command line. CommandLine begins with the executable name (arg[0]).
	std::string CommandLine = fmt::format("{} --demangle --output-style=LLVM --functions=linkage --relative-address --inlining=false",
										  QuoteIfNeeded(Executable.string()));

	CreateProcOptions Options;
	Options.StdinPipe  = &m_StdinPipe;
	Options.StdoutPipe = &m_StdoutPipe;

	CreateProcResult Handle = CreateProc(Executable, CommandLine, Options);

#if ZEN_PLATFORM_WINDOWS
	if (Handle == nullptr)
#else
	if (Handle <= 0)
#endif
	{
		ZEN_WARN("llvm-symbolizer: failed to launch '{}' - install LLVM or add to PATH", Executable.string());
		m_StdinPipe.Close();
		m_StdoutPipe.Close();
		return false;
	}

#if ZEN_PLATFORM_WINDOWS
	m_Process.Initialize(Handle);
#else
	std::error_code Ec;
	m_Process.Initialize(int(Handle), Ec);
	if (Ec)
	{
		ZEN_WARN("llvm-symbolizer: ProcessHandle init failed: {}", Ec.message());
		m_StdinPipe.Close();
		m_StdoutPipe.Close();
		return false;
	}
#endif

	// Close the child-side handles in the parent.
	m_StdinPipe.CloseReadEnd();
	m_StdoutPipe.CloseWriteEnd();

	m_Alive = true;
	return true;
}

bool
LlvmSymbolizerResolver::ReadLine(std::string& Out) const
{
	// Search for a newline already in the buffer; if not, read more.
	for (;;)
	{
		size_t NewlinePos = m_ReadBuffer.find('\n');
		if (NewlinePos != std::string::npos)
		{
			Out.assign(m_ReadBuffer, 0, NewlinePos);
			m_ReadBuffer.erase(0, NewlinePos + 1);
			// Trim a trailing \r (in case of CRLF line endings).
			if (!Out.empty() && Out.back() == '\r')
			{
				Out.pop_back();
			}
			return true;
		}

		char Buffer[1024];
#if ZEN_PLATFORM_WINDOWS
		DWORD BytesRead = 0;
		if (!::ReadFile(m_StdoutPipe.ReadHandle, Buffer, sizeof(Buffer), &BytesRead, nullptr) || BytesRead == 0)
		{
			return false;
		}
		m_ReadBuffer.append(Buffer, BytesRead);
#else
		ssize_t BytesRead = ::read(m_StdoutPipe.ReadFd, Buffer, sizeof(Buffer));
		if (BytesRead <= 0)
		{
			if (BytesRead < 0 && errno == EINTR)
			{
				continue;
			}
			return false;
		}
		m_ReadBuffer.append(Buffer, static_cast<size_t>(BytesRead));
#endif
	}
}

std::string
LlvmSymbolizerResolver::DoQuery(const Module& M, uint64_t RelAddress) const
{
	if (!EnsureProcess())
	{
		return {};
	}

	// Write "<path> 0x<addr>\n". Paths with spaces must be quoted for llvm-symbolizer
	// interactive input; it accepts double quotes.
	std::string Line;
	if (M.FullPath.find(' ') != std::string::npos)
	{
		Line = fmt::format("\"{}\" 0x{:X}\n", M.FullPath, RelAddress);
	}
	else
	{
		Line = fmt::format("{} 0x{:X}\n", M.FullPath, RelAddress);
	}

#if ZEN_PLATFORM_WINDOWS
	DWORD BytesWritten = 0;
	if (!::WriteFile(m_StdinPipe.WriteHandle, Line.data(), static_cast<DWORD>(Line.size()), &BytesWritten, nullptr) ||
		BytesWritten != Line.size())
	{
		ZEN_WARN("llvm-symbolizer: write failed, disabling backend");
		m_Alive = false;
		return {};
	}
#else
	const char* Ptr		  = Line.data();
	size_t		Remaining = Line.size();
	while (Remaining > 0)
	{
		ssize_t N = ::write(m_StdinPipe.WriteFd, Ptr, Remaining);
		if (N <= 0)
		{
			if (N < 0 && errno == EINTR)
			{
				continue;
			}
			ZEN_WARN("llvm-symbolizer: write failed, disabling backend");
			m_Alive = false;
			return {};
		}
		Ptr += N;
		Remaining -= static_cast<size_t>(N);
	}
#endif

	// Read lines until a blank line terminates the record.
	std::string Function;
	std::string Location;
	std::string Buf;
	int			LineIdx = 0;
	while (ReadLine(Buf))
	{
		if (Buf.empty())
		{
			break;
		}
		if (LineIdx == 0)
		{
			Function = Buf;
		}
		else if (LineIdx == 1)
		{
			Location = Buf;
		}
		// Additional lines would be inline frames (--inlining=false suppresses them); ignore.
		++LineIdx;
	}

	if (Function.empty() || Function == "??")
	{
		return {};
	}

	std::string Result = std::move(Function);
	if (!Location.empty() && Location != "??:0:0")
	{
		// Location is "path:line:col" — trim to "basename:line" to match Windows output.
		std::string_view LocView(Location);
		size_t			 LastColon = LocView.find_last_of(':');
		if (LastColon != std::string_view::npos)
		{
			LocView = LocView.substr(0, LastColon);
		}
		size_t			 Slash	  = LocView.find_last_of("/\\");
		std::string_view FileLine = (Slash == std::string_view::npos) ? LocView : LocView.substr(Slash + 1);
		Result += fmt::format(" [{}]", FileLine);
	}
	return Result;
}

//////////////////////////////////////////////////////////////////////////////
// atos backend — macOS only. Apple's symbolizer; ships with Xcode + the CLT.
//
// Unlike llvm-symbolizer, atos accepts only one binary per process. We keep
// one subprocess per loaded module and demultiplex queries by module path.
//
// Protocol (one request / response):
//   We write:   "0x<absolute-address>\n"
//   It replies: "Function (in Binary) (file.cpp:NN)\n"
//               or "Function (in Binary) + 0x<disp>\n"  (no debug info)
//               or "0x<address>\n"                       (nothing known)
//
// Launched with: atos -o <binary> -l 0x<module-base>
// atos subtracts -l from each input address to get the file offset.

#if ZEN_PLATFORM_MAC

class AtosSymbolizerResolver final : public SymbolResolver
{
public:
	AtosSymbolizerResolver() = default;
	~AtosSymbolizerResolver() override;

	void		LoadModule(const ModuleInfo& Module) override;
	std::string Resolve(uint64_t Address) const override;

private:
	struct Module
	{
		std::string FullPath;
		uint64_t	Base = 0;
		uint64_t	End	 = 0;
	};

	// One atos subprocess per loaded module (atos is single-binary).
	struct AtosProcess
	{
		zen::ProcessHandle	   Process;
		zen::StdinPipeHandles  StdinPipe;
		zen::StdoutPipeHandles StdoutPipe;
		std::string			   ReadBuffer;
		bool				   Alive = false;
	};

	const Module* FindModule(uint64_t Address) const;
	AtosProcess*  EnsureProcessFor(const Module& M) const;
	bool		  ReadLine(AtosProcess& P, std::string& Out) const;
	std::string	  DoQuery(const Module& M, uint64_t Address) const;

	std::vector<Module> m_Modules;

	mutable std::mutex													  m_Mutex;
	mutable std::unordered_map<std::string, std::unique_ptr<AtosProcess>> m_Processes;
	mutable std::unordered_map<uint64_t, std::string>					  m_Cache;
};

AtosSymbolizerResolver::~AtosSymbolizerResolver()
{
	std::lock_guard Lock(m_Mutex);
	for (auto& [Path, P] : m_Processes)
	{
		if (P && P->Alive)
		{
			P->StdinPipe.CloseWriteEnd();
			P->Process.Wait(2000);
			if (P->Process.IsRunning())
			{
				P->Process.Terminate(0);
			}
		}
	}
}

void
AtosSymbolizerResolver::LoadModule(const ModuleInfo& Mod)
{
	if (Mod.FullPath.empty() || Mod.Base == 0)
	{
		return;
	}

	std::error_code Ec;
	if (!std::filesystem::exists(Mod.FullPath, Ec))
	{
		ZEN_DEBUG("atos: binary not found for {} at {}", Mod.Name, Mod.FullPath);
		return;
	}

	uint64_t End = Mod.Base + (Mod.Size > 0 ? Mod.Size : 0x1000000);

	std::lock_guard Lock(m_Mutex);
	m_Modules.push_back({Mod.FullPath, Mod.Base, End});
	ZEN_INFO("atos: registered {} [0x{:X}..0x{:X})", Mod.Name, Mod.Base, End);
}

std::string
AtosSymbolizerResolver::Resolve(uint64_t Address) const
{
	std::lock_guard Lock(m_Mutex);

	auto CacheIt = m_Cache.find(Address);
	if (CacheIt != m_Cache.end())
	{
		return CacheIt->second;
	}

	const Module* M = FindModule(Address);
	if (M == nullptr)
	{
		m_Cache.emplace(Address, std::string{});
		return {};
	}

	std::string Result = DoQuery(*M, Address);
	m_Cache.emplace(Address, Result);
	return Result;
}

const AtosSymbolizerResolver::Module*
AtosSymbolizerResolver::FindModule(uint64_t Address) const
{
	for (const Module& M : m_Modules)
	{
		if (Address >= M.Base && Address < M.End)
		{
			return &M;
		}
	}
	return nullptr;
}

AtosSymbolizerResolver::AtosProcess*
AtosSymbolizerResolver::EnsureProcessFor(const Module& M) const
{
	auto It = m_Processes.find(M.FullPath);
	if (It != m_Processes.end())
	{
		return It->second.get();
	}

	auto P = std::make_unique<AtosProcess>();

	if (!CreateStdinPipe(P->StdinPipe) || !CreateStdoutPipe(P->StdoutPipe))
	{
		ZEN_WARN("atos: failed to create pipes for {}", M.FullPath);
		auto [Ins, _] = m_Processes.emplace(M.FullPath, std::move(P));
		return Ins->second.get();  // Alive = false
	}

	std::filesystem::path Executable  = SearchPathForExecutable("atos");
	std::string			  CommandLine = fmt::format("{} -o \"{}\" -l 0x{:X}", QuoteIfNeeded(Executable.string()), M.FullPath, M.Base);

	CreateProcOptions Options;
	Options.StdinPipe  = &P->StdinPipe;
	Options.StdoutPipe = &P->StdoutPipe;

	CreateProcResult Handle = CreateProc(Executable, CommandLine, Options);
	if (Handle <= 0)
	{
		ZEN_WARN("atos: failed to launch for {} - `atos` should be on PATH on macOS", M.FullPath);
		P->StdinPipe.Close();
		P->StdoutPipe.Close();
		auto [Ins, _] = m_Processes.emplace(M.FullPath, std::move(P));
		return Ins->second.get();  // Alive = false
	}

	std::error_code Ec;
	P->Process.Initialize(int(Handle), Ec);
	if (Ec)
	{
		ZEN_WARN("atos: ProcessHandle init failed for {}: {}", M.FullPath, Ec.message());
		P->StdinPipe.Close();
		P->StdoutPipe.Close();
		auto [Ins, _] = m_Processes.emplace(M.FullPath, std::move(P));
		return Ins->second.get();  // Alive = false
	}

	P->StdinPipe.CloseReadEnd();
	P->StdoutPipe.CloseWriteEnd();
	P->Alive = true;

	auto [Ins, _] = m_Processes.emplace(M.FullPath, std::move(P));
	return Ins->second.get();
}

bool
AtosSymbolizerResolver::ReadLine(AtosProcess& P, std::string& Out) const
{
	for (;;)
	{
		size_t NewlinePos = P.ReadBuffer.find('\n');
		if (NewlinePos != std::string::npos)
		{
			Out.assign(P.ReadBuffer, 0, NewlinePos);
			P.ReadBuffer.erase(0, NewlinePos + 1);
			return true;
		}

		char	Buffer[1024];
		ssize_t BytesRead = ::read(P.StdoutPipe.ReadFd, Buffer, sizeof(Buffer));
		if (BytesRead <= 0)
		{
			if (BytesRead < 0 && errno == EINTR)
			{
				continue;
			}
			return false;
		}
		P.ReadBuffer.append(Buffer, static_cast<size_t>(BytesRead));
	}
}

std::string
AtosSymbolizerResolver::DoQuery(const Module& M, uint64_t Address) const
{
	AtosProcess* P = EnsureProcessFor(M);
	if (P == nullptr || !P->Alive)
	{
		return {};
	}

	std::string Line = fmt::format("0x{:X}\n", Address);

	const char* Ptr		  = Line.data();
	size_t		Remaining = Line.size();
	while (Remaining > 0)
	{
		ssize_t N = ::write(P->StdinPipe.WriteFd, Ptr, Remaining);
		if (N <= 0)
		{
			if (N < 0 && errno == EINTR)
			{
				continue;
			}
			ZEN_WARN("atos: write failed for {}, disabling", M.FullPath);
			P->Alive = false;
			return {};
		}
		Ptr += N;
		Remaining -= static_cast<size_t>(N);
	}

	std::string Reply;
	if (!ReadLine(*P, Reply) || Reply.empty())
	{
		return {};
	}

	// Parse "Function (in Binary) (file.cpp:NN)" or "... + 0xNN" or just "0xADDR".
	// Extract everything before " (in " as the function name.
	size_t InPos = Reply.find(" (in ");
	if (InPos == std::string::npos)
	{
		// No match — either raw "0xADDR" (no info) or an error message. Skip.
		return {};
	}

	std::string_view Function(Reply.data(), InPos);

	// Look for a trailing "(file:line)" after the "(in ...)" block.
	std::string_view LocationView;
	size_t			 AfterIn = Reply.find(')', InPos);
	if (AfterIn != std::string::npos)
	{
		size_t OpenParen = Reply.find('(', AfterIn);
		if (OpenParen != std::string::npos)
		{
			size_t CloseParen = Reply.find(')', OpenParen);
			if (CloseParen != std::string::npos && CloseParen > OpenParen + 1)
			{
				LocationView = std::string_view(Reply).substr(OpenParen + 1, CloseParen - OpenParen - 1);
			}
		}
	}

	std::string Result(Function);
	if (!LocationView.empty())
	{
		// atos gives us "file.cpp:NN" directly — no need to strip a directory.
		Result += fmt::format(" [{}]", LocationView);
	}
	return Result;
}

#endif	// ZEN_PLATFORM_MAC

//////////////////////////////////////////////////////////////////////////////
// Factory

namespace {

#if ZEN_PLATFORM_MAC
	// Probe PATH for a tool and return true if something usable was found.
	// SearchPathForExecutable returns the input unchanged if the tool can't be
	// found, so we compare against the filesystem to detect a hit.
	bool ToolIsOnPath(std::string_view Name)
	{
		std::filesystem::path Resolved = SearchPathForExecutable(Name);
		std::error_code		  Ec;
		return std::filesystem::exists(Resolved, Ec) && std::filesystem::is_regular_file(Resolved, Ec);
	}
#endif

	SymbolBackend ResolveAutoBackend()
	{
#if ZEN_PLATFORM_WINDOWS
		return SymbolBackend::Pdb;
#elif ZEN_PLATFORM_MAC
		if (ToolIsOnPath("llvm-symbolizer"))
		{
			return SymbolBackend::LlvmSymbolizer;
		}
		return SymbolBackend::Atos;
#else
		// Linux: llvm-symbolizer is the only backend we ship.
		return SymbolBackend::LlvmSymbolizer;
#endif
	}

}  // namespace

std::unique_ptr<SymbolResolver>
CreateSymbolResolver(SymbolBackend Backend)
{
	if (Backend == SymbolBackend::Auto)
	{
		Backend = ResolveAutoBackend();
	}

	if (Backend == SymbolBackend::Off)
	{
		return std::make_unique<NullSymbolResolver>();
	}

	if (Backend == SymbolBackend::LlvmSymbolizer)
	{
		return std::make_unique<LlvmSymbolizerResolver>();
	}

#if ZEN_PLATFORM_MAC
	if (Backend == SymbolBackend::Atos)
	{
		return std::make_unique<AtosSymbolizerResolver>();
	}
#else
	if (Backend == SymbolBackend::Atos)
	{
		ZEN_WARN("atos backend is macOS-only; falling back to llvm-symbolizer");
		return std::make_unique<LlvmSymbolizerResolver>();
	}
#endif

#if ZEN_PLATFORM_WINDOWS
	if (Backend == SymbolBackend::DbgHelp)
	{
		return std::make_unique<DbgHelpSymbolResolver>();
	}
	return std::make_unique<PdbSymbolResolver>();
#else
	// Pdb / DbgHelp aren't available on non-Windows; any other request falls back to llvm-symbolizer.
	return std::make_unique<LlvmSymbolizerResolver>();
#endif
}

SymbolBackend
ParseSymbolBackend(std::string_view Name)
{
	if (Name == "auto")
	{
		return SymbolBackend::Auto;
	}
	if (Name == "pdb")
	{
		return SymbolBackend::Pdb;
	}
	if (Name == "dbghelp")
	{
		return SymbolBackend::DbgHelp;
	}
	if (Name == "llvm" || Name == "llvm-symbolizer")
	{
		return SymbolBackend::LlvmSymbolizer;
	}
	if (Name == "atos")
	{
		return SymbolBackend::Atos;
	}
	if (Name == "off")
	{
		return SymbolBackend::Off;
	}
	return SymbolBackend::Off;
}

}  // namespace zen::trace_detail