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
|
//===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "SymbolFilePDB.h"
#include "clang/Lex/Lexer.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/PluginManager.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/LineTable.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolContext.h"
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Symbol/TypeList.h"
#include "lldb/Symbol/TypeMap.h"
#include "lldb/Symbol/Variable.h"
#include "lldb/Utility/RegularExpression.h"
#include "llvm/DebugInfo/PDB/GenericError.h"
#include "llvm/DebugInfo/PDB/IPDBDataStream.h"
#include "llvm/DebugInfo/PDB/IPDBEnumChildren.h"
#include "llvm/DebugInfo/PDB/IPDBLineNumber.h"
#include "llvm/DebugInfo/PDB/IPDBSectionContrib.h"
#include "llvm/DebugInfo/PDB/IPDBSourceFile.h"
#include "llvm/DebugInfo/PDB/IPDBTable.h"
#include "llvm/DebugInfo/PDB/PDBSymbol.h"
#include "llvm/DebugInfo/PDB/PDBSymbolBlock.h"
#include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h"
#include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h"
#include "llvm/DebugInfo/PDB/PDBSymbolData.h"
#include "llvm/DebugInfo/PDB/PDBSymbolExe.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h"
#include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h"
#include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h"
#include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h"
#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h"
#include "Plugins/SymbolFile/PDB/PDBASTParser.h"
#include "Plugins/SymbolFile/PDB/PDBLocationToDWARFExpression.h"
#include <regex>
using namespace lldb;
using namespace lldb_private;
using namespace llvm::pdb;
namespace {
lldb::LanguageType TranslateLanguage(PDB_Lang lang) {
switch (lang) {
case PDB_Lang::Cpp:
return lldb::LanguageType::eLanguageTypeC_plus_plus;
case PDB_Lang::C:
return lldb::LanguageType::eLanguageTypeC;
default:
return lldb::LanguageType::eLanguageTypeUnknown;
}
}
bool ShouldAddLine(uint32_t requested_line, uint32_t actual_line,
uint32_t addr_length) {
return ((requested_line == 0 || actual_line == requested_line) &&
addr_length > 0);
}
} // namespace
void SymbolFilePDB::Initialize() {
PluginManager::RegisterPlugin(GetPluginNameStatic(),
GetPluginDescriptionStatic(), CreateInstance,
DebuggerInitialize);
}
void SymbolFilePDB::Terminate() {
PluginManager::UnregisterPlugin(CreateInstance);
}
void SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) {}
lldb_private::ConstString SymbolFilePDB::GetPluginNameStatic() {
static ConstString g_name("pdb");
return g_name;
}
const char *SymbolFilePDB::GetPluginDescriptionStatic() {
return "Microsoft PDB debug symbol file reader.";
}
lldb_private::SymbolFile *
SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) {
return new SymbolFilePDB(obj_file);
}
SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file)
: SymbolFile(object_file), m_session_up(), m_global_scope_up(),
m_cached_compile_unit_count(0), m_tu_decl_ctx_up() {}
SymbolFilePDB::~SymbolFilePDB() {}
uint32_t SymbolFilePDB::CalculateAbilities() {
uint32_t abilities = 0;
if (!m_obj_file)
return 0;
if (!m_session_up) {
// Lazily load and match the PDB file, but only do this once.
std::string exePath = m_obj_file->GetFileSpec().GetPath();
auto error = loadDataForEXE(PDB_ReaderType::DIA, llvm::StringRef(exePath),
m_session_up);
if (error) {
llvm::consumeError(std::move(error));
auto module_sp = m_obj_file->GetModule();
if (!module_sp)
return 0;
// See if any symbol file is specified through `--symfile` option.
FileSpec symfile = module_sp->GetSymbolFileFileSpec();
if (!symfile)
return 0;
error = loadDataForPDB(PDB_ReaderType::DIA,
llvm::StringRef(symfile.GetPath()), m_session_up);
if (error) {
llvm::consumeError(std::move(error));
return 0;
}
}
}
if (!m_session_up)
return 0;
auto enum_tables_up = m_session_up->getEnumTables();
if (!enum_tables_up)
return 0;
while (auto table_up = enum_tables_up->getNext()) {
if (table_up->getItemCount() == 0)
continue;
auto type = table_up->getTableType();
switch (type) {
case PDB_TableType::Symbols:
// This table represents a store of symbols with types listed in
// PDBSym_Type
abilities |= (CompileUnits | Functions | Blocks | GlobalVariables |
LocalVariables | VariableTypes);
break;
case PDB_TableType::LineNumbers:
abilities |= LineTables;
break;
default:
break;
}
}
return abilities;
}
void SymbolFilePDB::InitializeObject() {
lldb::addr_t obj_load_address = m_obj_file->GetFileOffset();
lldbassert(obj_load_address && obj_load_address != LLDB_INVALID_ADDRESS);
m_session_up->setLoadAddress(obj_load_address);
if (!m_global_scope_up)
m_global_scope_up = m_session_up->getGlobalScope();
lldbassert(m_global_scope_up.get());
TypeSystem *type_system =
GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
ClangASTContext *clang_type_system =
llvm::dyn_cast_or_null<ClangASTContext>(type_system);
lldbassert(clang_type_system);
m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(
type_system, clang_type_system->GetTranslationUnitDecl());
}
uint32_t SymbolFilePDB::GetNumCompileUnits() {
if (m_cached_compile_unit_count == 0) {
auto compilands = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
if (!compilands)
return 0;
// The linker could link *.dll (compiland language = LINK), or import
// *.dll. For example, a compiland with name `Import:KERNEL32.dll` could be
// found as a child of the global scope (PDB executable). Usually, such
// compilands contain `thunk` symbols in which we are not interested for
// now. However we still count them in the compiland list. If we perform
// any compiland related activity, like finding symbols through
// llvm::pdb::IPDBSession methods, such compilands will all be searched
// automatically no matter whether we include them or not.
m_cached_compile_unit_count = compilands->getChildCount();
// The linker can inject an additional "dummy" compilation unit into the
// PDB. Ignore this special compile unit for our purposes, if it is there.
// It is always the last one.
auto last_compiland_up =
compilands->getChildAtIndex(m_cached_compile_unit_count - 1);
lldbassert(last_compiland_up.get());
std::string name = last_compiland_up->getName();
if (name == "* Linker *")
--m_cached_compile_unit_count;
}
return m_cached_compile_unit_count;
}
void SymbolFilePDB::GetCompileUnitIndex(
const llvm::pdb::PDBSymbolCompiland &pdb_compiland, uint32_t &index) {
auto results_up = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
if (!results_up)
return;
auto uid = pdb_compiland.getSymIndexId();
for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
auto compiland_up = results_up->getChildAtIndex(cu_idx);
if (!compiland_up)
continue;
if (compiland_up->getSymIndexId() == uid) {
index = cu_idx;
return;
}
}
index = UINT32_MAX;
return;
}
std::unique_ptr<llvm::pdb::PDBSymbolCompiland>
SymbolFilePDB::GetPDBCompilandByUID(uint32_t uid) {
return m_session_up->getConcreteSymbolById<PDBSymbolCompiland>(uid);
}
lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) {
if (index >= GetNumCompileUnits())
return CompUnitSP();
// Assuming we always retrieve same compilands listed in same order through
// `PDBSymbolExe::findAllChildren` method, otherwise using `index` to get a
// compile unit makes no sense.
auto results = m_global_scope_up->findAllChildren<PDBSymbolCompiland>();
if (!results)
return CompUnitSP();
auto compiland_up = results->getChildAtIndex(index);
if (!compiland_up)
return CompUnitSP();
return ParseCompileUnitForUID(compiland_up->getSymIndexId(), index);
}
lldb::LanguageType
SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) {
// What fields should I expect to be filled out on the SymbolContext? Is it
// safe to assume that `sc.comp_unit` is valid?
if (!sc.comp_unit)
return lldb::eLanguageTypeUnknown;
auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
if (!compiland_up)
return lldb::eLanguageTypeUnknown;
auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
if (!details)
return lldb::eLanguageTypeUnknown;
return TranslateLanguage(details->getLanguage());
}
lldb_private::Function *SymbolFilePDB::ParseCompileUnitFunctionForPDBFunc(
const PDBSymbolFunc &pdb_func, const lldb_private::SymbolContext &sc) {
lldbassert(sc.comp_unit && sc.module_sp.get());
auto file_vm_addr = pdb_func.getVirtualAddress();
if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
return nullptr;
auto func_length = pdb_func.getLength();
AddressRange func_range =
AddressRange(file_vm_addr, func_length, sc.module_sp->GetSectionList());
if (!func_range.GetBaseAddress().IsValid())
return nullptr;
lldb_private::Type *func_type = ResolveTypeUID(pdb_func.getSymIndexId());
if (!func_type)
return nullptr;
user_id_t func_type_uid = pdb_func.getSignatureId();
Mangled mangled = GetMangledForPDBFunc(pdb_func);
FunctionSP func_sp =
std::make_shared<Function>(sc.comp_unit, pdb_func.getSymIndexId(),
func_type_uid, mangled, func_type, func_range);
sc.comp_unit->AddFunction(func_sp);
return func_sp.get();
}
size_t SymbolFilePDB::ParseCompileUnitFunctions(
const lldb_private::SymbolContext &sc) {
lldbassert(sc.comp_unit);
size_t func_added = 0;
auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
if (!compiland_up)
return 0;
auto results_up = compiland_up->findAllChildren<PDBSymbolFunc>();
if (!results_up)
return 0;
while (auto pdb_func_up = results_up->getNext()) {
auto func_sp =
sc.comp_unit->FindFunctionByUID(pdb_func_up->getSymIndexId());
if (!func_sp) {
if (ParseCompileUnitFunctionForPDBFunc(*pdb_func_up, sc))
++func_added;
}
}
return func_added;
}
bool SymbolFilePDB::ParseCompileUnitLineTable(
const lldb_private::SymbolContext &sc) {
lldbassert(sc.comp_unit);
if (sc.comp_unit->GetLineTable())
return true;
return ParseCompileUnitLineTable(sc, 0);
}
bool SymbolFilePDB::ParseCompileUnitDebugMacros(
const lldb_private::SymbolContext &sc) {
// PDB doesn't contain information about macros
return false;
}
bool SymbolFilePDB::ParseCompileUnitSupportFiles(
const lldb_private::SymbolContext &sc,
lldb_private::FileSpecList &support_files) {
lldbassert(sc.comp_unit);
// In theory this is unnecessary work for us, because all of this information
// is easily (and quickly) accessible from DebugInfoPDB, so caching it a
// second time seems like a waste. Unfortunately, there's no good way around
// this short of a moderate refactor since SymbolVendor depends on being able
// to cache this list.
auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
if (!compiland_up)
return false;
auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
if (!files || files->getChildCount() == 0)
return false;
while (auto file = files->getNext()) {
FileSpec spec(file->getFileName(), false, FileSpec::Style::windows);
support_files.AppendIfUnique(spec);
}
// LLDB uses the DWARF-like file numeration (one based),
// the zeroth file is the compile unit itself
support_files.Insert(0, *sc.comp_unit);
return true;
}
bool SymbolFilePDB::ParseImportedModules(
const lldb_private::SymbolContext &sc,
std::vector<lldb_private::ConstString> &imported_modules) {
// PDB does not yet support module debug info
return false;
}
static size_t ParseFunctionBlocksForPDBSymbol(
const lldb_private::SymbolContext &sc, uint64_t func_file_vm_addr,
const llvm::pdb::PDBSymbol *pdb_symbol, lldb_private::Block *parent_block,
bool is_top_parent) {
assert(pdb_symbol && parent_block);
size_t num_added = 0;
switch (pdb_symbol->getSymTag()) {
case PDB_SymType::Block:
case PDB_SymType::Function: {
Block *block = nullptr;
auto &raw_sym = pdb_symbol->getRawSymbol();
if (auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(pdb_symbol)) {
if (pdb_func->hasNoInlineAttribute())
break;
if (is_top_parent)
block = parent_block;
else
break;
} else if (llvm::dyn_cast<PDBSymbolBlock>(pdb_symbol)) {
auto uid = pdb_symbol->getSymIndexId();
if (parent_block->FindBlockByID(uid))
break;
if (raw_sym.getVirtualAddress() < func_file_vm_addr)
break;
auto block_sp = std::make_shared<Block>(pdb_symbol->getSymIndexId());
parent_block->AddChild(block_sp);
block = block_sp.get();
} else
llvm_unreachable("Unexpected PDB symbol!");
block->AddRange(Block::Range(
raw_sym.getVirtualAddress() - func_file_vm_addr, raw_sym.getLength()));
block->FinalizeRanges();
++num_added;
auto results_up = pdb_symbol->findAllChildren();
if (!results_up)
break;
while (auto symbol_up = results_up->getNext()) {
num_added += ParseFunctionBlocksForPDBSymbol(
sc, func_file_vm_addr, symbol_up.get(), block, false);
}
} break;
default:
break;
}
return num_added;
}
size_t
SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) {
lldbassert(sc.comp_unit && sc.function);
size_t num_added = 0;
auto uid = sc.function->GetID();
auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
if (!pdb_func_up)
return 0;
Block &parent_block = sc.function->GetBlock(false);
num_added =
ParseFunctionBlocksForPDBSymbol(sc, pdb_func_up->getVirtualAddress(),
pdb_func_up.get(), &parent_block, true);
return num_added;
}
size_t SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) {
lldbassert(sc.module_sp.get());
if (!sc.comp_unit)
return 0;
size_t num_added = 0;
auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
if (!compiland)
return 0;
auto ParseTypesByTagFn = [&num_added, this](const PDBSymbol &raw_sym) {
std::unique_ptr<IPDBEnumSymbols> results;
PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
PDB_SymType::UDT};
for (auto tag : tags_to_search) {
results = raw_sym.findAllChildren(tag);
if (!results || results->getChildCount() == 0)
continue;
while (auto symbol = results->getNext()) {
switch (symbol->getSymTag()) {
case PDB_SymType::Enum:
case PDB_SymType::UDT:
case PDB_SymType::Typedef:
break;
default:
continue;
}
// This should cause the type to get cached and stored in the `m_types`
// lookup.
if (!ResolveTypeUID(symbol->getSymIndexId()))
continue;
++num_added;
}
}
};
if (sc.function) {
auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
sc.function->GetID());
if (!pdb_func)
return 0;
ParseTypesByTagFn(*pdb_func);
} else {
ParseTypesByTagFn(*compiland);
// Also parse global types particularly coming from this compiland.
// Unfortunately, PDB has no compiland information for each global type. We
// have to parse them all. But ensure we only do this once.
static bool parse_all_global_types = false;
if (!parse_all_global_types) {
ParseTypesByTagFn(*m_global_scope_up);
parse_all_global_types = true;
}
}
return num_added;
}
size_t
SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) {
if (!sc.comp_unit)
return 0;
size_t num_added = 0;
if (sc.function) {
auto pdb_func = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(
sc.function->GetID());
if (!pdb_func)
return 0;
num_added += ParseVariables(sc, *pdb_func);
sc.function->GetBlock(false).SetDidParseVariables(true, true);
} else if (sc.comp_unit) {
auto compiland = GetPDBCompilandByUID(sc.comp_unit->GetID());
if (!compiland)
return 0;
if (sc.comp_unit->GetVariableList(false))
return 0;
auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
if (results && results->getChildCount()) {
while (auto result = results->getNext()) {
auto cu_id = result->getCompilandId();
// FIXME: We are not able to determine variable's compile unit.
if (cu_id == 0)
continue;
if (cu_id == sc.comp_unit->GetID())
num_added += ParseVariables(sc, *result);
}
}
// FIXME: A `file static` or `global constant` variable appears both in
// compiland's children and global scope's children with unexpectedly
// different symbol's Id making it ambiguous.
// FIXME: 'local constant', for example, const char var[] = "abc", declared
// in a function scope, can't be found in PDB.
// Parse variables in this compiland.
num_added += ParseVariables(sc, *compiland);
}
return num_added;
}
lldb_private::Type *SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) {
auto find_result = m_types.find(type_uid);
if (find_result != m_types.end())
return find_result->second.get();
TypeSystem *type_system =
GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus);
ClangASTContext *clang_type_system =
llvm::dyn_cast_or_null<ClangASTContext>(type_system);
if (!clang_type_system)
return nullptr;
PDBASTParser *pdb =
llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser());
if (!pdb)
return nullptr;
auto pdb_type = m_session_up->getSymbolById(type_uid);
if (pdb_type == nullptr)
return nullptr;
lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type);
if (result) {
m_types.insert(std::make_pair(type_uid, result));
auto type_list = GetTypeList();
if (type_list)
type_list->Insert(result);
}
return result.get();
}
bool SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) {
// TODO: Implement this
return false;
}
lldb_private::CompilerDecl SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) {
return lldb_private::CompilerDecl();
}
lldb_private::CompilerDeclContext
SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) {
// PDB always uses the translation unit decl context for everything. We can
// improve this later but it's not easy because PDB doesn't provide a high
// enough level of type fidelity in this area.
return *m_tu_decl_ctx_up;
}
lldb_private::CompilerDeclContext
SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) {
return *m_tu_decl_ctx_up;
}
void SymbolFilePDB::ParseDeclsForContext(
lldb_private::CompilerDeclContext decl_ctx) {}
uint32_t
SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr,
uint32_t resolve_scope,
lldb_private::SymbolContext &sc) {
uint32_t resolved_flags = 0;
if (resolve_scope & eSymbolContextCompUnit ||
resolve_scope & eSymbolContextVariable ||
resolve_scope & eSymbolContextFunction ||
resolve_scope & eSymbolContextBlock ||
resolve_scope & eSymbolContextLineEntry) {
auto cu_sp = GetCompileUnitContainsAddress(so_addr);
if (!cu_sp) {
if (resolved_flags | eSymbolContextVariable) {
// TODO: Resolve variables
}
return 0;
}
sc.comp_unit = cu_sp.get();
resolved_flags |= eSymbolContextCompUnit;
lldbassert(sc.module_sp == cu_sp->GetModule());
}
if (resolve_scope & eSymbolContextFunction) {
addr_t file_vm_addr = so_addr.GetFileAddress();
auto symbol_up =
m_session_up->findSymbolByAddress(file_vm_addr, PDB_SymType::Function);
if (symbol_up) {
auto *pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
assert(pdb_func);
auto func_uid = pdb_func->getSymIndexId();
sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
if (sc.function == nullptr)
sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
if (sc.function) {
resolved_flags |= eSymbolContextFunction;
if (resolve_scope & eSymbolContextBlock) {
Block &block = sc.function->GetBlock(true);
sc.block = block.FindBlockByID(sc.function->GetID());
if (sc.block)
resolved_flags |= eSymbolContextBlock;
}
}
}
}
if (resolve_scope & eSymbolContextLineEntry) {
if (auto *line_table = sc.comp_unit->GetLineTable()) {
Address addr(so_addr);
if (line_table->FindLineEntryByAddress(addr, sc.line_entry))
resolved_flags |= eSymbolContextLineEntry;
}
}
return resolved_flags;
}
uint32_t SymbolFilePDB::ResolveSymbolContext(
const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines,
uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) {
const size_t old_size = sc_list.GetSize();
if (resolve_scope & lldb::eSymbolContextCompUnit) {
// Locate all compilation units with line numbers referencing the specified
// file. For example, if `file_spec` is <vector>, then this should return
// all source files and header files that reference <vector>, either
// directly or indirectly.
auto compilands = m_session_up->findCompilandsForSourceFile(
file_spec.GetPath(), PDB_NameSearchFlags::NS_CaseInsensitive);
if (!compilands)
return 0;
// For each one, either find its previously parsed data or parse it afresh
// and add it to the symbol context list.
while (auto compiland = compilands->getNext()) {
// If we're not checking inlines, then don't add line information for
// this file unless the FileSpec matches. For inline functions, we don't
// have to match the FileSpec since they could be defined in headers
// other than file specified in FileSpec.
if (!check_inlines) {
std::string source_file = compiland->getSourceFileFullPath();
if (source_file.empty())
continue;
FileSpec this_spec(source_file, false, FileSpec::Style::windows);
bool need_full_match = !file_spec.GetDirectory().IsEmpty();
if (FileSpec::Compare(file_spec, this_spec, need_full_match) != 0)
continue;
}
SymbolContext sc;
auto cu = ParseCompileUnitForUID(compiland->getSymIndexId());
if (!cu)
continue;
sc.comp_unit = cu.get();
sc.module_sp = cu->GetModule();
// If we were asked to resolve line entries, add all entries to the line
// table that match the requested line (or all lines if `line` == 0).
if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock |
eSymbolContextLineEntry)) {
bool has_line_table = ParseCompileUnitLineTable(sc, line);
if ((resolve_scope & eSymbolContextLineEntry) && !has_line_table) {
// The query asks for line entries, but we can't get them for the
// compile unit. This is not normal for `line` = 0. So just assert
// it.
assert(line && "Couldn't get all line entries!\n");
// Current compiland does not have the requested line. Search next.
continue;
}
if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock)) {
if (!has_line_table)
continue;
auto *line_table = sc.comp_unit->GetLineTable();
lldbassert(line_table);
uint32_t num_line_entries = line_table->GetSize();
// Skip the terminal line entry.
--num_line_entries;
// If `line `!= 0, see if we can resolve function for each line entry
// in the line table.
for (uint32_t line_idx = 0; line && line_idx < num_line_entries;
++line_idx) {
if (!line_table->GetLineEntryAtIndex(line_idx, sc.line_entry))
continue;
auto file_vm_addr =
sc.line_entry.range.GetBaseAddress().GetFileAddress();
if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
continue;
auto symbol_up = m_session_up->findSymbolByAddress(
file_vm_addr, PDB_SymType::Function);
if (symbol_up) {
auto func_uid = symbol_up->getSymIndexId();
sc.function = sc.comp_unit->FindFunctionByUID(func_uid).get();
if (sc.function == nullptr) {
auto pdb_func = llvm::dyn_cast<PDBSymbolFunc>(symbol_up.get());
assert(pdb_func);
sc.function = ParseCompileUnitFunctionForPDBFunc(*pdb_func, sc);
}
if (sc.function && (resolve_scope & eSymbolContextBlock)) {
Block &block = sc.function->GetBlock(true);
sc.block = block.FindBlockByID(sc.function->GetID());
}
}
sc_list.Append(sc);
}
} else if (has_line_table) {
// We can parse line table for the compile unit. But no query to
// resolve function or block. We append `sc` to the list anyway.
sc_list.Append(sc);
}
} else {
// No query for line entry, function or block. But we have a valid
// compile unit, append `sc` to the list.
sc_list.Append(sc);
}
}
}
return sc_list.GetSize() - old_size;
}
std::string SymbolFilePDB::GetMangledForPDBData(const PDBSymbolData &pdb_data) {
std::string decorated_name;
auto vm_addr = pdb_data.getVirtualAddress();
if (vm_addr != LLDB_INVALID_ADDRESS && vm_addr) {
auto result_up =
m_global_scope_up->findAllChildren(PDB_SymType::PublicSymbol);
if (result_up) {
while (auto symbol_up = result_up->getNext()) {
if (symbol_up->getRawSymbol().getVirtualAddress() == vm_addr) {
decorated_name = symbol_up->getRawSymbol().getName();
break;
}
}
}
}
if (!decorated_name.empty())
return decorated_name;
return std::string();
}
VariableSP SymbolFilePDB::ParseVariableForPDBData(
const lldb_private::SymbolContext &sc,
const llvm::pdb::PDBSymbolData &pdb_data) {
VariableSP var_sp;
uint32_t var_uid = pdb_data.getSymIndexId();
auto result = m_variables.find(var_uid);
if (result != m_variables.end())
return result->second;
ValueType scope = eValueTypeInvalid;
bool is_static_member = false;
bool is_external = false;
bool is_artificial = false;
switch (pdb_data.getDataKind()) {
case PDB_DataKind::Global:
scope = eValueTypeVariableGlobal;
is_external = true;
break;
case PDB_DataKind::Local:
scope = eValueTypeVariableLocal;
break;
case PDB_DataKind::FileStatic:
scope = eValueTypeVariableStatic;
break;
case PDB_DataKind::StaticMember:
is_static_member = true;
scope = eValueTypeVariableStatic;
break;
case PDB_DataKind::Member:
scope = eValueTypeVariableStatic;
break;
case PDB_DataKind::Param:
scope = eValueTypeVariableArgument;
break;
case PDB_DataKind::Constant:
scope = eValueTypeConstResult;
break;
default:
break;
}
switch (pdb_data.getLocationType()) {
case PDB_LocType::TLS:
scope = eValueTypeVariableThreadLocal;
break;
case PDB_LocType::RegRel: {
// It is a `this` pointer.
if (pdb_data.getDataKind() == PDB_DataKind::ObjectPtr) {
scope = eValueTypeVariableArgument;
is_artificial = true;
}
} break;
default:
break;
}
Declaration decl;
if (!is_artificial && !pdb_data.isCompilerGenerated()) {
if (auto lines = pdb_data.getLineNumbers()) {
if (auto first_line = lines->getNext()) {
uint32_t src_file_id = first_line->getSourceFileId();
auto src_file = m_session_up->getSourceFileById(src_file_id);
if (src_file) {
FileSpec spec(src_file->getFileName(), /*resolve_path*/ false);
decl.SetFile(spec);
decl.SetColumn(first_line->getColumnNumber());
decl.SetLine(first_line->getLineNumber());
}
}
}
}
Variable::RangeList ranges;
SymbolContextScope *context_scope = sc.comp_unit;
if (scope == eValueTypeVariableLocal) {
if (sc.function) {
context_scope = sc.function->GetBlock(true).FindBlockByID(
pdb_data.getClassParentId());
if (context_scope == nullptr)
context_scope = sc.function;
}
}
SymbolFileTypeSP type_sp =
std::make_shared<SymbolFileType>(*this, pdb_data.getTypeId());
auto var_name = pdb_data.getName();
auto mangled = GetMangledForPDBData(pdb_data);
auto mangled_cstr = mangled.empty() ? nullptr : mangled.c_str();
bool is_constant;
DWARFExpression location = ConvertPDBLocationToDWARFExpression(
GetObjectFile()->GetModule(), pdb_data, is_constant);
var_sp = std::make_shared<Variable>(
var_uid, var_name.c_str(), mangled_cstr, type_sp, scope, context_scope,
ranges, &decl, location, is_external, is_artificial, is_static_member);
var_sp->SetLocationIsConstantValueData(is_constant);
m_variables.insert(std::make_pair(var_uid, var_sp));
return var_sp;
}
size_t
SymbolFilePDB::ParseVariables(const lldb_private::SymbolContext &sc,
const llvm::pdb::PDBSymbol &pdb_symbol,
lldb_private::VariableList *variable_list) {
size_t num_added = 0;
if (auto pdb_data = llvm::dyn_cast<PDBSymbolData>(&pdb_symbol)) {
VariableListSP local_variable_list_sp;
auto result = m_variables.find(pdb_data->getSymIndexId());
if (result != m_variables.end()) {
if (variable_list)
variable_list->AddVariableIfUnique(result->second);
} else {
// Prepare right VariableList for this variable.
if (auto lexical_parent = pdb_data->getLexicalParent()) {
switch (lexical_parent->getSymTag()) {
case PDB_SymType::Exe:
assert(sc.comp_unit);
LLVM_FALLTHROUGH;
case PDB_SymType::Compiland: {
if (sc.comp_unit) {
local_variable_list_sp = sc.comp_unit->GetVariableList(false);
if (!local_variable_list_sp) {
local_variable_list_sp = std::make_shared<VariableList>();
sc.comp_unit->SetVariableList(local_variable_list_sp);
}
}
} break;
case PDB_SymType::Block:
case PDB_SymType::Function: {
if (sc.function) {
Block *block = sc.function->GetBlock(true).FindBlockByID(
lexical_parent->getSymIndexId());
if (block) {
local_variable_list_sp = block->GetBlockVariableList(false);
if (!local_variable_list_sp) {
local_variable_list_sp = std::make_shared<VariableList>();
block->SetVariableList(local_variable_list_sp);
}
}
}
} break;
default:
break;
}
}
if (local_variable_list_sp) {
if (auto var_sp = ParseVariableForPDBData(sc, *pdb_data)) {
local_variable_list_sp->AddVariableIfUnique(var_sp);
if (variable_list)
variable_list->AddVariableIfUnique(var_sp);
++num_added;
}
}
}
}
if (auto results = pdb_symbol.findAllChildren()) {
while (auto result = results->getNext())
num_added += ParseVariables(sc, *result, variable_list);
}
return num_added;
}
uint32_t SymbolFilePDB::FindGlobalVariables(
const lldb_private::ConstString &name,
const lldb_private::CompilerDeclContext *parent_decl_ctx,
uint32_t max_matches, lldb_private::VariableList &variables) {
if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
return 0;
if (name.IsEmpty())
return 0;
auto results =
m_global_scope_up->findChildren(PDB_SymType::Data, name.GetStringRef(),
PDB_NameSearchFlags::NS_CaseSensitive);
if (!results)
return 0;
uint32_t matches = 0;
size_t old_size = variables.GetSize();
while (auto result = results->getNext()) {
auto pdb_data = llvm::dyn_cast<PDBSymbolData>(result.get());
if (max_matches > 0 && matches >= max_matches)
break;
SymbolContext sc;
sc.module_sp = m_obj_file->GetModule();
lldbassert(sc.module_sp.get());
sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
// FIXME: We are not able to determine the compile unit.
if (sc.comp_unit == nullptr)
continue;
ParseVariables(sc, *pdb_data, &variables);
matches = variables.GetSize() - old_size;
}
return matches;
}
uint32_t
SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex,
uint32_t max_matches,
lldb_private::VariableList &variables) {
if (!regex.IsValid())
return 0;
auto results = m_global_scope_up->findAllChildren<PDBSymbolData>();
if (!results)
return 0;
uint32_t matches = 0;
size_t old_size = variables.GetSize();
while (auto pdb_data = results->getNext()) {
if (max_matches > 0 && matches >= max_matches)
break;
auto var_name = pdb_data->getName();
if (var_name.empty())
continue;
if (!regex.Execute(var_name))
continue;
SymbolContext sc;
sc.module_sp = m_obj_file->GetModule();
lldbassert(sc.module_sp.get());
sc.comp_unit = ParseCompileUnitForUID(pdb_data->getCompilandId()).get();
// FIXME: We are not able to determine the compile unit.
if (sc.comp_unit == nullptr)
continue;
ParseVariables(sc, *pdb_data, &variables);
matches = variables.GetSize() - old_size;
}
return matches;
}
bool SymbolFilePDB::ResolveFunction(const llvm::pdb::PDBSymbolFunc &pdb_func,
bool include_inlines,
lldb_private::SymbolContextList &sc_list) {
lldb_private::SymbolContext sc;
sc.comp_unit = ParseCompileUnitForUID(pdb_func.getCompilandId()).get();
if (!sc.comp_unit)
return false;
sc.module_sp = sc.comp_unit->GetModule();
sc.function = ParseCompileUnitFunctionForPDBFunc(pdb_func, sc);
if (!sc.function)
return false;
sc_list.Append(sc);
return true;
}
bool SymbolFilePDB::ResolveFunction(uint32_t uid, bool include_inlines,
lldb_private::SymbolContextList &sc_list) {
auto pdb_func_up = m_session_up->getConcreteSymbolById<PDBSymbolFunc>(uid);
if (!pdb_func_up && !(include_inlines && pdb_func_up->hasInlineAttribute()))
return false;
return ResolveFunction(*pdb_func_up, include_inlines, sc_list);
}
void SymbolFilePDB::CacheFunctionNames() {
if (!m_func_full_names.IsEmpty())
return;
std::map<uint64_t, uint32_t> addr_ids;
if (auto results_up = m_global_scope_up->findAllChildren<PDBSymbolFunc>()) {
while (auto pdb_func_up = results_up->getNext()) {
if (pdb_func_up->isCompilerGenerated())
continue;
auto name = pdb_func_up->getName();
auto demangled_name = pdb_func_up->getUndecoratedName();
if (name.empty() && demangled_name.empty())
continue;
auto uid = pdb_func_up->getSymIndexId();
if (!demangled_name.empty() && pdb_func_up->getVirtualAddress())
addr_ids.insert(std::make_pair(pdb_func_up->getVirtualAddress(), uid));
if (auto parent = pdb_func_up->getClassParent()) {
// PDB have symbols for class/struct methods or static methods in Enum
// Class. We won't bother to check if the parent is UDT or Enum here.
m_func_method_names.Append(ConstString(name), uid);
ConstString cstr_name(name);
// To search a method name, like NS::Class:MemberFunc, LLDB searches
// its base name, i.e. MemberFunc by default. Since PDBSymbolFunc does
// not have inforamtion of this, we extract base names and cache them
// by our own effort.
llvm::StringRef basename;
CPlusPlusLanguage::MethodName cpp_method(cstr_name);
if (cpp_method.IsValid()) {
llvm::StringRef context;
basename = cpp_method.GetBasename();
if (basename.empty())
CPlusPlusLanguage::ExtractContextAndIdentifier(name.c_str(),
context, basename);
}
if (!basename.empty())
m_func_base_names.Append(ConstString(basename), uid);
else {
m_func_base_names.Append(ConstString(name), uid);
}
if (!demangled_name.empty())
m_func_full_names.Append(ConstString(demangled_name), uid);
} else {
// Handle not-method symbols.
// The function name might contain namespace, or its lexical scope. It
// is not safe to get its base name by applying same scheme as we deal
// with the method names.
// FIXME: Remove namespace if function is static in a scope.
m_func_base_names.Append(ConstString(name), uid);
if (name == "main") {
m_func_full_names.Append(ConstString(name), uid);
if (!demangled_name.empty() && name != demangled_name) {
m_func_full_names.Append(ConstString(demangled_name), uid);
m_func_base_names.Append(ConstString(demangled_name), uid);
}
} else if (!demangled_name.empty()) {
m_func_full_names.Append(ConstString(demangled_name), uid);
} else {
m_func_full_names.Append(ConstString(name), uid);
}
}
}
}
if (auto results_up =
m_global_scope_up->findAllChildren<PDBSymbolPublicSymbol>()) {
while (auto pub_sym_up = results_up->getNext()) {
if (!pub_sym_up->isFunction())
continue;
auto name = pub_sym_up->getName();
if (name.empty())
continue;
if (CPlusPlusLanguage::IsCPPMangledName(name.c_str())) {
auto vm_addr = pub_sym_up->getVirtualAddress();
// PDB public symbol has mangled name for its associated function.
if (vm_addr && addr_ids.find(vm_addr) != addr_ids.end()) {
// Cache mangled name.
m_func_full_names.Append(ConstString(name), addr_ids[vm_addr]);
}
}
}
}
// Sort them before value searching is working properly
m_func_full_names.Sort();
m_func_full_names.SizeToFit();
m_func_method_names.Sort();
m_func_method_names.SizeToFit();
m_func_base_names.Sort();
m_func_base_names.SizeToFit();
}
uint32_t SymbolFilePDB::FindFunctions(
const lldb_private::ConstString &name,
const lldb_private::CompilerDeclContext *parent_decl_ctx,
uint32_t name_type_mask, bool include_inlines, bool append,
lldb_private::SymbolContextList &sc_list) {
if (!append)
sc_list.Clear();
lldbassert((name_type_mask & eFunctionNameTypeAuto) == 0);
if (name_type_mask == eFunctionNameTypeNone)
return 0;
if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
return 0;
if (name.IsEmpty())
return 0;
auto old_size = sc_list.GetSize();
if (name_type_mask & eFunctionNameTypeFull ||
name_type_mask & eFunctionNameTypeBase ||
name_type_mask & eFunctionNameTypeMethod) {
CacheFunctionNames();
std::set<uint32_t> resolved_ids;
auto ResolveFn = [include_inlines, &name, &sc_list, &resolved_ids,
this](UniqueCStringMap<uint32_t> &Names) {
std::vector<uint32_t> ids;
if (Names.GetValues(name, ids)) {
for (auto id : ids) {
if (resolved_ids.find(id) == resolved_ids.end()) {
if (ResolveFunction(id, include_inlines, sc_list))
resolved_ids.insert(id);
}
}
}
};
if (name_type_mask & eFunctionNameTypeFull) {
ResolveFn(m_func_full_names);
}
if (name_type_mask & eFunctionNameTypeBase) {
ResolveFn(m_func_base_names);
}
if (name_type_mask & eFunctionNameTypeMethod) {
ResolveFn(m_func_method_names);
}
}
return sc_list.GetSize() - old_size;
}
uint32_t
SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex,
bool include_inlines, bool append,
lldb_private::SymbolContextList &sc_list) {
if (!append)
sc_list.Clear();
if (!regex.IsValid())
return 0;
auto old_size = sc_list.GetSize();
CacheFunctionNames();
std::set<uint32_t> resolved_ids;
auto ResolveFn = [®ex, include_inlines, &sc_list, &resolved_ids,
this](UniqueCStringMap<uint32_t> &Names) {
std::vector<uint32_t> ids;
if (Names.GetValues(regex, ids)) {
for (auto id : ids) {
if (resolved_ids.find(id) == resolved_ids.end())
if (ResolveFunction(id, include_inlines, sc_list))
resolved_ids.insert(id);
}
}
};
ResolveFn(m_func_full_names);
ResolveFn(m_func_base_names);
return sc_list.GetSize() - old_size;
}
void SymbolFilePDB::GetMangledNamesForFunction(
const std::string &scope_qualified_name,
std::vector<lldb_private::ConstString> &mangled_names) {}
uint32_t SymbolFilePDB::FindTypes(
const lldb_private::SymbolContext &sc,
const lldb_private::ConstString &name,
const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append,
uint32_t max_matches,
llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files,
lldb_private::TypeMap &types) {
if (!append)
types.Clear();
if (!name)
return 0;
if (!DeclContextMatchesThisSymbolFile(parent_decl_ctx))
return 0;
searched_symbol_files.clear();
searched_symbol_files.insert(this);
std::string name_str = name.AsCString();
// There is an assumption 'name' is not a regex
FindTypesByName(name_str, max_matches, types);
return types.GetSize();
}
void SymbolFilePDB::FindTypesByRegex(
const lldb_private::RegularExpression ®ex, uint32_t max_matches,
lldb_private::TypeMap &types) {
// When searching by regex, we need to go out of our way to limit the search
// space as much as possible since this searches EVERYTHING in the PDB,
// manually doing regex comparisons. PDB library isn't optimized for regex
// searches or searches across multiple symbol types at the same time, so the
// best we can do is to search enums, then typedefs, then classes one by one,
// and do a regex comparison against each of them.
PDB_SymType tags_to_search[] = {PDB_SymType::Enum, PDB_SymType::Typedef,
PDB_SymType::UDT};
std::unique_ptr<IPDBEnumSymbols> results;
uint32_t matches = 0;
for (auto tag : tags_to_search) {
results = m_global_scope_up->findAllChildren(tag);
if (!results)
continue;
while (auto result = results->getNext()) {
if (max_matches > 0 && matches >= max_matches)
break;
std::string type_name;
if (auto enum_type = llvm::dyn_cast<PDBSymbolTypeEnum>(result.get()))
type_name = enum_type->getName();
else if (auto typedef_type =
llvm::dyn_cast<PDBSymbolTypeTypedef>(result.get()))
type_name = typedef_type->getName();
else if (auto class_type = llvm::dyn_cast<PDBSymbolTypeUDT>(result.get()))
type_name = class_type->getName();
else {
// We're looking only for types that have names. Skip symbols, as well
// as unnamed types such as arrays, pointers, etc.
continue;
}
if (!regex.Execute(type_name))
continue;
// This should cause the type to get cached and stored in the `m_types`
// lookup.
if (!ResolveTypeUID(result->getSymIndexId()))
continue;
auto iter = m_types.find(result->getSymIndexId());
if (iter == m_types.end())
continue;
types.Insert(iter->second);
++matches;
}
}
}
void SymbolFilePDB::FindTypesByName(const std::string &name,
uint32_t max_matches,
lldb_private::TypeMap &types) {
std::unique_ptr<IPDBEnumSymbols> results;
if (name.empty())
return;
results = m_global_scope_up->findChildren(PDB_SymType::None, name,
PDB_NameSearchFlags::NS_Default);
if (!results)
return;
uint32_t matches = 0;
while (auto result = results->getNext()) {
if (max_matches > 0 && matches >= max_matches)
break;
switch (result->getSymTag()) {
case PDB_SymType::Enum:
case PDB_SymType::UDT:
case PDB_SymType::Typedef:
break;
default:
// We're looking only for types that have names. Skip symbols, as well
// as unnamed types such as arrays, pointers, etc.
continue;
}
// This should cause the type to get cached and stored in the `m_types`
// lookup.
if (!ResolveTypeUID(result->getSymIndexId()))
continue;
auto iter = m_types.find(result->getSymIndexId());
if (iter == m_types.end())
continue;
types.Insert(iter->second);
++matches;
}
}
size_t SymbolFilePDB::FindTypes(
const std::vector<lldb_private::CompilerContext> &contexts, bool append,
lldb_private::TypeMap &types) {
return 0;
}
lldb_private::TypeList *SymbolFilePDB::GetTypeList() {
return m_obj_file->GetModule()->GetTypeList();
}
void SymbolFilePDB::GetTypesForPDBSymbol(const llvm::pdb::PDBSymbol &pdb_symbol,
uint32_t type_mask,
TypeCollection &type_collection) {
bool can_parse = false;
switch (pdb_symbol.getSymTag()) {
case PDB_SymType::ArrayType:
can_parse = ((type_mask & eTypeClassArray) != 0);
break;
case PDB_SymType::BuiltinType:
can_parse = ((type_mask & eTypeClassBuiltin) != 0);
break;
case PDB_SymType::Enum:
can_parse = ((type_mask & eTypeClassEnumeration) != 0);
break;
case PDB_SymType::Function:
case PDB_SymType::FunctionSig:
can_parse = ((type_mask & eTypeClassFunction) != 0);
break;
case PDB_SymType::PointerType:
can_parse = ((type_mask & (eTypeClassPointer | eTypeClassBlockPointer |
eTypeClassMemberPointer)) != 0);
break;
case PDB_SymType::Typedef:
can_parse = ((type_mask & eTypeClassTypedef) != 0);
break;
case PDB_SymType::UDT: {
auto *udt = llvm::dyn_cast<PDBSymbolTypeUDT>(&pdb_symbol);
assert(udt);
can_parse = (udt->getUdtKind() != PDB_UdtType::Interface &&
((type_mask & (eTypeClassClass | eTypeClassStruct |
eTypeClassUnion)) != 0));
} break;
default:
break;
}
if (can_parse) {
if (auto *type = ResolveTypeUID(pdb_symbol.getSymIndexId())) {
auto result =
std::find(type_collection.begin(), type_collection.end(), type);
if (result == type_collection.end())
type_collection.push_back(type);
}
}
auto results_up = pdb_symbol.findAllChildren();
while (auto symbol_up = results_up->getNext())
GetTypesForPDBSymbol(*symbol_up, type_mask, type_collection);
}
size_t SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope,
uint32_t type_mask,
lldb_private::TypeList &type_list) {
TypeCollection type_collection;
uint32_t old_size = type_list.GetSize();
CompileUnit *cu =
sc_scope ? sc_scope->CalculateSymbolContextCompileUnit() : nullptr;
if (cu) {
auto compiland_up = GetPDBCompilandByUID(cu->GetID());
if (!compiland_up)
return 0;
GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
} else {
for (uint32_t cu_idx = 0; cu_idx < GetNumCompileUnits(); ++cu_idx) {
auto cu_sp = ParseCompileUnitAtIndex(cu_idx);
if (cu_sp) {
if (auto compiland_up = GetPDBCompilandByUID(cu_sp->GetID()))
GetTypesForPDBSymbol(*compiland_up, type_mask, type_collection);
}
}
}
for (auto type : type_collection) {
type->GetForwardCompilerType();
type_list.Insert(type->shared_from_this());
}
return type_list.GetSize() - old_size;
}
lldb_private::TypeSystem *
SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) {
auto type_system =
m_obj_file->GetModule()->GetTypeSystemForLanguage(language);
if (type_system)
type_system->SetSymbolFile(this);
return type_system;
}
lldb_private::CompilerDeclContext SymbolFilePDB::FindNamespace(
const lldb_private::SymbolContext &sc,
const lldb_private::ConstString &name,
const lldb_private::CompilerDeclContext *parent_decl_ctx) {
return lldb_private::CompilerDeclContext();
}
lldb_private::ConstString SymbolFilePDB::GetPluginName() {
static ConstString g_name("pdb");
return g_name;
}
uint32_t SymbolFilePDB::GetPluginVersion() { return 1; }
IPDBSession &SymbolFilePDB::GetPDBSession() { return *m_session_up; }
const IPDBSession &SymbolFilePDB::GetPDBSession() const {
return *m_session_up;
}
lldb::CompUnitSP SymbolFilePDB::ParseCompileUnitForUID(uint32_t id,
uint32_t index) {
auto found_cu = m_comp_units.find(id);
if (found_cu != m_comp_units.end())
return found_cu->second;
auto compiland_up = GetPDBCompilandByUID(id);
if (!compiland_up)
return CompUnitSP();
lldb::LanguageType lang;
auto details = compiland_up->findOneChild<PDBSymbolCompilandDetails>();
if (!details)
lang = lldb::eLanguageTypeC_plus_plus;
else
lang = TranslateLanguage(details->getLanguage());
if (lang == lldb::LanguageType::eLanguageTypeUnknown)
return CompUnitSP();
std::string path = compiland_up->getSourceFileFullPath();
if (path.empty())
return CompUnitSP();
// Don't support optimized code for now, DebugInfoPDB does not return this
// information.
LazyBool optimized = eLazyBoolNo;
auto cu_sp = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr,
path.c_str(), id, lang, optimized);
if (!cu_sp)
return CompUnitSP();
m_comp_units.insert(std::make_pair(id, cu_sp));
if (index == UINT32_MAX)
GetCompileUnitIndex(*compiland_up, index);
lldbassert(index != UINT32_MAX);
m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(index,
cu_sp);
return cu_sp;
}
bool SymbolFilePDB::ParseCompileUnitLineTable(
const lldb_private::SymbolContext &sc, uint32_t match_line) {
lldbassert(sc.comp_unit);
auto compiland_up = GetPDBCompilandByUID(sc.comp_unit->GetID());
if (!compiland_up)
return false;
// LineEntry needs the *index* of the file into the list of support files
// returned by ParseCompileUnitSupportFiles. But the underlying SDK gives us
// a globally unique idenfitifier in the namespace of the PDB. So, we have
// to do a mapping so that we can hand out indices.
llvm::DenseMap<uint32_t, uint32_t> index_map;
BuildSupportFileIdToSupportFileIndexMap(*compiland_up, index_map);
auto line_table = llvm::make_unique<LineTable>(sc.comp_unit);
// Find contributions to `compiland` from all source and header files.
std::string path = sc.comp_unit->GetPath();
auto files = m_session_up->getSourceFilesForCompiland(*compiland_up);
if (!files)
return false;
// For each source and header file, create a LineSequence for contributions
// to the compiland from that file, and add the sequence.
while (auto file = files->getNext()) {
std::unique_ptr<LineSequence> sequence(
line_table->CreateLineSequenceContainer());
auto lines = m_session_up->findLineNumbers(*compiland_up, *file);
if (!lines)
continue;
int entry_count = lines->getChildCount();
uint64_t prev_addr;
uint32_t prev_length;
uint32_t prev_line;
uint32_t prev_source_idx;
for (int i = 0; i < entry_count; ++i) {
auto line = lines->getChildAtIndex(i);
uint64_t lno = line->getLineNumber();
uint64_t addr = line->getVirtualAddress();
uint32_t length = line->getLength();
uint32_t source_id = line->getSourceFileId();
uint32_t col = line->getColumnNumber();
uint32_t source_idx = index_map[source_id];
// There was a gap between the current entry and the previous entry if
// the addresses don't perfectly line up.
bool is_gap = (i > 0) && (prev_addr + prev_length < addr);
// Before inserting the current entry, insert a terminal entry at the end
// of the previous entry's address range if the current entry resulted in
// a gap from the previous entry.
if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) {
line_table->AppendLineEntryToSequence(
sequence.get(), prev_addr + prev_length, prev_line, 0,
prev_source_idx, false, false, false, false, true);
line_table->InsertSequence(sequence.release());
sequence.reset(line_table->CreateLineSequenceContainer());
}
if (ShouldAddLine(match_line, lno, length)) {
bool is_statement = line->isStatement();
bool is_prologue = false;
bool is_epilogue = false;
auto func =
m_session_up->findSymbolByAddress(addr, PDB_SymType::Function);
if (func) {
auto prologue = func->findOneChild<PDBSymbolFuncDebugStart>();
if (prologue)
is_prologue = (addr == prologue->getVirtualAddress());
auto epilogue = func->findOneChild<PDBSymbolFuncDebugEnd>();
if (epilogue)
is_epilogue = (addr == epilogue->getVirtualAddress());
}
line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col,
source_idx, is_statement, false,
is_prologue, is_epilogue, false);
}
prev_addr = addr;
prev_length = length;
prev_line = lno;
prev_source_idx = source_idx;
}
if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) {
// The end is always a terminal entry, so insert it regardless.
line_table->AppendLineEntryToSequence(
sequence.get(), prev_addr + prev_length, prev_line, 0,
prev_source_idx, false, false, false, false, true);
}
line_table->InsertSequence(sequence.release());
}
if (line_table->GetSize()) {
sc.comp_unit->SetLineTable(line_table.release());
return true;
}
return false;
}
void SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(
const PDBSymbolCompiland &compiland,
llvm::DenseMap<uint32_t, uint32_t> &index_map) const {
// This is a hack, but we need to convert the source id into an index into
// the support files array. We don't want to do path comparisons to avoid
// basename / full path issues that may or may not even be a problem, so we
// use the globally unique source file identifiers. Ideally we could use the
// global identifiers everywhere, but LineEntry currently assumes indices.
auto source_files = m_session_up->getSourceFilesForCompiland(compiland);
if (!source_files)
return;
// LLDB uses the DWARF-like file numeration (one based)
int index = 1;
while (auto file = source_files->getNext()) {
uint32_t source_id = file->getUniqueId();
index_map[source_id] = index++;
}
}
lldb::CompUnitSP SymbolFilePDB::GetCompileUnitContainsAddress(
const lldb_private::Address &so_addr) {
lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
if (file_vm_addr == LLDB_INVALID_ADDRESS || file_vm_addr == 0)
return nullptr;
// If it is a PDB function's vm addr, this is the first sure bet.
if (auto lines =
m_session_up->findLineNumbersByAddress(file_vm_addr, /*Length=*/1)) {
if (auto first_line = lines->getNext())
return ParseCompileUnitForUID(first_line->getCompilandId());
}
// Otherwise we resort to section contributions.
if (auto sec_contribs = m_session_up->getSectionContribs()) {
while (auto section = sec_contribs->getNext()) {
auto va = section->getVirtualAddress();
if (file_vm_addr >= va && file_vm_addr < va + section->getLength())
return ParseCompileUnitForUID(section->getCompilandId());
}
}
return nullptr;
}
Mangled
SymbolFilePDB::GetMangledForPDBFunc(const llvm::pdb::PDBSymbolFunc &pdb_func) {
Mangled mangled;
auto func_name = pdb_func.getName();
auto func_undecorated_name = pdb_func.getUndecoratedName();
std::string func_decorated_name;
// Seek from public symbols for non-static function's decorated name if any.
// For static functions, they don't have undecorated names and aren't exposed
// in Public Symbols either.
if (!func_undecorated_name.empty()) {
auto result_up = m_global_scope_up->findChildren(
PDB_SymType::PublicSymbol, func_undecorated_name,
PDB_NameSearchFlags::NS_UndecoratedName);
if (result_up) {
while (auto symbol_up = result_up->getNext()) {
// For a public symbol, it is unique.
lldbassert(result_up->getChildCount() == 1);
if (auto *pdb_public_sym =
llvm::dyn_cast_or_null<PDBSymbolPublicSymbol>(
symbol_up.get())) {
if (pdb_public_sym->isFunction()) {
func_decorated_name = pdb_public_sym->getName();
break;
}
}
}
}
}
if (!func_decorated_name.empty()) {
mangled.SetMangledName(ConstString(func_decorated_name));
// For MSVC, format of C funciton's decorated name depends on calling
// conventon. Unfortunately none of the format is recognized by current
// LLDB. For example, `_purecall` is a __cdecl C function. From PDB,
// `__purecall` is retrieved as both its decorated and undecorated name
// (using PDBSymbolFunc::getUndecoratedName method). However `__purecall`
// string is not treated as mangled in LLDB (neither `?` nor `_Z` prefix).
// Mangled::GetDemangledName method will fail internally and caches an
// empty string as its undecorated name. So we will face a contradition
// here for the same symbol:
// non-empty undecorated name from PDB
// empty undecorated name from LLDB
if (!func_undecorated_name.empty() &&
mangled.GetDemangledName(mangled.GuessLanguage()).IsEmpty())
mangled.SetDemangledName(ConstString(func_undecorated_name));
// LLDB uses several flags to control how a C++ decorated name is
// undecorated for MSVC. See `safeUndecorateName` in Class Mangled. So the
// yielded name could be different from what we retrieve from
// PDB source unless we also apply same flags in getting undecorated
// name through PDBSymbolFunc::getUndecoratedNameEx method.
if (!func_undecorated_name.empty() &&
mangled.GetDemangledName(mangled.GuessLanguage()) !=
ConstString(func_undecorated_name))
mangled.SetDemangledName(ConstString(func_undecorated_name));
} else if (!func_undecorated_name.empty()) {
mangled.SetDemangledName(ConstString(func_undecorated_name));
} else if (!func_name.empty())
mangled.SetValue(ConstString(func_name), false);
return mangled;
}
bool SymbolFilePDB::DeclContextMatchesThisSymbolFile(
const lldb_private::CompilerDeclContext *decl_ctx) {
if (decl_ctx == nullptr || !decl_ctx->IsValid())
return true;
TypeSystem *decl_ctx_type_system = decl_ctx->GetTypeSystem();
if (!decl_ctx_type_system)
return false;
TypeSystem *type_system = GetTypeSystemForLanguage(
decl_ctx_type_system->GetMinimumLanguage(nullptr));
if (decl_ctx_type_system == type_system)
return true; // The type systems match, return true
return false;
}
|