Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 1 | //===-- SymbolFilePDB.cpp ---------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "SymbolFilePDB.h" |
| 11 | |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 12 | #include "clang/Lex/Lexer.h" |
| 13 | |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Module.h" |
| 15 | #include "lldb/Core/PluginManager.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 16 | #include "lldb/Symbol/ClangASTContext.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 17 | #include "lldb/Symbol/CompileUnit.h" |
| 18 | #include "lldb/Symbol/LineTable.h" |
| 19 | #include "lldb/Symbol/ObjectFile.h" |
| 20 | #include "lldb/Symbol/SymbolContext.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 21 | #include "lldb/Symbol/TypeMap.h" |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 22 | |
| 23 | #include "llvm/DebugInfo/PDB/IPDBEnumChildren.h" |
| 24 | #include "llvm/DebugInfo/PDB/IPDBLineNumber.h" |
| 25 | #include "llvm/DebugInfo/PDB/IPDBSourceFile.h" |
| 26 | #include "llvm/DebugInfo/PDB/PDBSymbol.h" |
| 27 | #include "llvm/DebugInfo/PDB/PDBSymbolCompiland.h" |
| 28 | #include "llvm/DebugInfo/PDB/PDBSymbolCompilandDetails.h" |
| 29 | #include "llvm/DebugInfo/PDB/PDBSymbolExe.h" |
| 30 | #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h" |
| 31 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugEnd.h" |
| 32 | #include "llvm/DebugInfo/PDB/PDBSymbolFuncDebugStart.h" |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 33 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeEnum.h" |
| 34 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeTypedef.h" |
| 35 | #include "llvm/DebugInfo/PDB/PDBSymbolTypeUDT.h" |
| 36 | |
| 37 | #include "Plugins/SymbolFile/PDB/PDBASTParser.h" |
| 38 | |
| 39 | #include <regex> |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace lldb_private; |
| 42 | |
| 43 | namespace |
| 44 | { |
| 45 | lldb::LanguageType TranslateLanguage(llvm::PDB_Lang lang) |
| 46 | { |
| 47 | switch (lang) |
| 48 | { |
| 49 | case llvm::PDB_Lang::Cpp: |
| 50 | return lldb::LanguageType::eLanguageTypeC_plus_plus; |
| 51 | case llvm::PDB_Lang::C: |
| 52 | return lldb::LanguageType::eLanguageTypeC; |
| 53 | default: |
| 54 | return lldb::LanguageType::eLanguageTypeUnknown; |
| 55 | } |
| 56 | } |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 57 | |
| 58 | bool |
| 59 | ShouldAddLine(uint32_t requested_line, uint32_t actual_line, uint32_t addr_length) |
| 60 | { |
| 61 | return ((requested_line == 0 || actual_line == requested_line) && addr_length > 0); |
| 62 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void |
| 66 | SymbolFilePDB::Initialize() |
| 67 | { |
| 68 | PluginManager::RegisterPlugin(GetPluginNameStatic(), GetPluginDescriptionStatic(), CreateInstance, |
| 69 | DebuggerInitialize); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | SymbolFilePDB::Terminate() |
| 74 | { |
| 75 | PluginManager::UnregisterPlugin(CreateInstance); |
| 76 | } |
| 77 | |
| 78 | void |
| 79 | SymbolFilePDB::DebuggerInitialize(lldb_private::Debugger &debugger) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | lldb_private::ConstString |
| 84 | SymbolFilePDB::GetPluginNameStatic() |
| 85 | { |
| 86 | static ConstString g_name("pdb"); |
| 87 | return g_name; |
| 88 | } |
| 89 | |
| 90 | const char * |
| 91 | SymbolFilePDB::GetPluginDescriptionStatic() |
| 92 | { |
| 93 | return "Microsoft PDB debug symbol file reader."; |
| 94 | } |
| 95 | |
| 96 | lldb_private::SymbolFile * |
| 97 | SymbolFilePDB::CreateInstance(lldb_private::ObjectFile *obj_file) |
| 98 | { |
| 99 | return new SymbolFilePDB(obj_file); |
| 100 | } |
| 101 | |
| 102 | SymbolFilePDB::SymbolFilePDB(lldb_private::ObjectFile *object_file) |
| 103 | : SymbolFile(object_file), m_cached_compile_unit_count(0) |
| 104 | { |
| 105 | } |
| 106 | |
| 107 | SymbolFilePDB::~SymbolFilePDB() |
| 108 | { |
| 109 | } |
| 110 | |
| 111 | uint32_t |
| 112 | SymbolFilePDB::CalculateAbilities() |
| 113 | { |
| 114 | if (!m_session_up) |
| 115 | { |
| 116 | // Lazily load and match the PDB file, but only do this once. |
| 117 | std::string exePath = m_obj_file->GetFileSpec().GetPath(); |
| 118 | auto error = llvm::loadDataForEXE(llvm::PDB_ReaderType::DIA, llvm::StringRef(exePath), m_session_up); |
| 119 | if (error != llvm::PDB_ErrorCode::Success) |
| 120 | return 0; |
| 121 | } |
| 122 | return CompileUnits | LineTables; |
| 123 | } |
| 124 | |
| 125 | void |
| 126 | SymbolFilePDB::InitializeObject() |
| 127 | { |
| 128 | lldb::addr_t obj_load_address = m_obj_file->GetFileOffset(); |
| 129 | m_session_up->setLoadAddress(obj_load_address); |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 130 | |
| 131 | TypeSystem *type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 132 | ClangASTContext *clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
| 133 | m_tu_decl_ctx_up = llvm::make_unique<CompilerDeclContext>(type_system, clang_type_system->GetTranslationUnitDecl()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | uint32_t |
| 137 | SymbolFilePDB::GetNumCompileUnits() |
| 138 | { |
| 139 | if (m_cached_compile_unit_count == 0) |
| 140 | { |
| 141 | auto global = m_session_up->getGlobalScope(); |
| 142 | auto compilands = global->findAllChildren<llvm::PDBSymbolCompiland>(); |
| 143 | m_cached_compile_unit_count = compilands->getChildCount(); |
| 144 | |
| 145 | // The linker can inject an additional "dummy" compilation unit into the PDB. |
| 146 | // Ignore this special compile unit for our purposes, if it is there. It is |
| 147 | // always the last one. |
| 148 | auto last_cu = compilands->getChildAtIndex(m_cached_compile_unit_count - 1); |
| 149 | std::string name = last_cu->getName(); |
| 150 | if (name == "* Linker *") |
| 151 | --m_cached_compile_unit_count; |
| 152 | } |
| 153 | return m_cached_compile_unit_count; |
| 154 | } |
| 155 | |
| 156 | lldb::CompUnitSP |
| 157 | SymbolFilePDB::ParseCompileUnitAtIndex(uint32_t index) |
| 158 | { |
| 159 | auto global = m_session_up->getGlobalScope(); |
| 160 | auto compilands = global->findAllChildren<llvm::PDBSymbolCompiland>(); |
| 161 | auto cu = compilands->getChildAtIndex(index); |
| 162 | |
| 163 | uint32_t id = cu->getSymIndexId(); |
| 164 | |
| 165 | return ParseCompileUnitForSymIndex(id); |
| 166 | } |
| 167 | |
| 168 | lldb::LanguageType |
| 169 | SymbolFilePDB::ParseCompileUnitLanguage(const lldb_private::SymbolContext &sc) |
| 170 | { |
| 171 | // What fields should I expect to be filled out on the SymbolContext? Is it |
| 172 | // safe to assume that `sc.comp_unit` is valid? |
| 173 | if (!sc.comp_unit) |
| 174 | return lldb::eLanguageTypeUnknown; |
| 175 | |
| 176 | auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(sc.comp_unit->GetID()); |
| 177 | if (!cu) |
| 178 | return lldb::eLanguageTypeUnknown; |
| 179 | auto details = cu->findOneChild<llvm::PDBSymbolCompilandDetails>(); |
| 180 | if (!details) |
| 181 | return lldb::eLanguageTypeUnknown; |
| 182 | return TranslateLanguage(details->getLanguage()); |
| 183 | } |
| 184 | |
| 185 | size_t |
| 186 | SymbolFilePDB::ParseCompileUnitFunctions(const lldb_private::SymbolContext &sc) |
| 187 | { |
| 188 | // TODO: Implement this |
| 189 | return size_t(); |
| 190 | } |
| 191 | |
| 192 | bool |
| 193 | SymbolFilePDB::ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc) |
| 194 | { |
| 195 | return ParseCompileUnitLineTable(sc, 0); |
| 196 | } |
| 197 | |
| 198 | bool |
| 199 | SymbolFilePDB::ParseCompileUnitDebugMacros(const lldb_private::SymbolContext &sc) |
| 200 | { |
| 201 | // PDB doesn't contain information about macros |
| 202 | return false; |
| 203 | } |
| 204 | |
| 205 | bool |
| 206 | SymbolFilePDB::ParseCompileUnitSupportFiles(const lldb_private::SymbolContext &sc, |
| 207 | lldb_private::FileSpecList &support_files) |
| 208 | { |
| 209 | if (!sc.comp_unit) |
| 210 | return false; |
| 211 | |
| 212 | // In theory this is unnecessary work for us, because all of this information is easily |
| 213 | // (and quickly) accessible from DebugInfoPDB, so caching it a second time seems like a waste. |
| 214 | // Unfortunately, there's no good way around this short of a moderate refactor, since SymbolVendor |
| 215 | // depends on being able to cache this list. |
| 216 | auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(sc.comp_unit->GetID()); |
| 217 | if (!cu) |
| 218 | return false; |
| 219 | auto files = m_session_up->getSourceFilesForCompiland(*cu); |
| 220 | if (!files || files->getChildCount() == 0) |
| 221 | return false; |
| 222 | |
| 223 | while (auto file = files->getNext()) |
| 224 | { |
| 225 | FileSpec spec(file->getFileName(), false); |
| 226 | support_files.Append(spec); |
| 227 | } |
| 228 | return true; |
| 229 | } |
| 230 | |
| 231 | bool |
| 232 | SymbolFilePDB::ParseImportedModules(const lldb_private::SymbolContext &sc, |
| 233 | std::vector<lldb_private::ConstString> &imported_modules) |
| 234 | { |
| 235 | // PDB does not yet support module debug info |
| 236 | return false; |
| 237 | } |
| 238 | |
| 239 | size_t |
| 240 | SymbolFilePDB::ParseFunctionBlocks(const lldb_private::SymbolContext &sc) |
| 241 | { |
| 242 | // TODO: Implement this |
| 243 | return size_t(); |
| 244 | } |
| 245 | |
| 246 | size_t |
| 247 | SymbolFilePDB::ParseTypes(const lldb_private::SymbolContext &sc) |
| 248 | { |
| 249 | // TODO: Implement this |
| 250 | return size_t(); |
| 251 | } |
| 252 | |
| 253 | size_t |
| 254 | SymbolFilePDB::ParseVariablesForContext(const lldb_private::SymbolContext &sc) |
| 255 | { |
| 256 | // TODO: Implement this |
| 257 | return size_t(); |
| 258 | } |
| 259 | |
| 260 | lldb_private::Type * |
| 261 | SymbolFilePDB::ResolveTypeUID(lldb::user_id_t type_uid) |
| 262 | { |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 263 | auto find_result = m_types.find(type_uid); |
| 264 | if (find_result != m_types.end()) |
| 265 | return find_result->second.get(); |
| 266 | |
| 267 | TypeSystem *type_system = GetTypeSystemForLanguage(lldb::eLanguageTypeC_plus_plus); |
| 268 | ClangASTContext *clang_type_system = llvm::dyn_cast_or_null<ClangASTContext>(type_system); |
| 269 | if (!clang_type_system) |
| 270 | return nullptr; |
| 271 | PDBASTParser *pdb = llvm::dyn_cast<PDBASTParser>(clang_type_system->GetPDBParser()); |
| 272 | if (!pdb) |
| 273 | return nullptr; |
| 274 | |
| 275 | auto pdb_type = m_session_up->getSymbolById(type_uid); |
| 276 | if (pdb_type == nullptr) |
| 277 | return nullptr; |
| 278 | |
| 279 | lldb::TypeSP result = pdb->CreateLLDBTypeFromPDBType(*pdb_type); |
| 280 | m_types.insert(std::make_pair(type_uid, result)); |
| 281 | return result.get(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | bool |
| 285 | SymbolFilePDB::CompleteType(lldb_private::CompilerType &compiler_type) |
| 286 | { |
| 287 | // TODO: Implement this |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | lldb_private::CompilerDecl |
| 292 | SymbolFilePDB::GetDeclForUID(lldb::user_id_t uid) |
| 293 | { |
| 294 | return lldb_private::CompilerDecl(); |
| 295 | } |
| 296 | |
| 297 | lldb_private::CompilerDeclContext |
| 298 | SymbolFilePDB::GetDeclContextForUID(lldb::user_id_t uid) |
| 299 | { |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 300 | // PDB always uses the translation unit decl context for everything. We can improve this later |
| 301 | // but it's not easy because PDB doesn't provide a high enough level of type fidelity in this area. |
| 302 | return *m_tu_decl_ctx_up; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | lldb_private::CompilerDeclContext |
| 306 | SymbolFilePDB::GetDeclContextContainingUID(lldb::user_id_t uid) |
| 307 | { |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 308 | return *m_tu_decl_ctx_up; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | void |
| 312 | SymbolFilePDB::ParseDeclsForContext(lldb_private::CompilerDeclContext decl_ctx) |
| 313 | { |
| 314 | } |
| 315 | |
| 316 | uint32_t |
| 317 | SymbolFilePDB::ResolveSymbolContext(const lldb_private::Address &so_addr, uint32_t resolve_scope, |
| 318 | lldb_private::SymbolContext &sc) |
| 319 | { |
| 320 | return uint32_t(); |
| 321 | } |
| 322 | |
| 323 | uint32_t |
| 324 | SymbolFilePDB::ResolveSymbolContext(const lldb_private::FileSpec &file_spec, uint32_t line, bool check_inlines, |
| 325 | uint32_t resolve_scope, lldb_private::SymbolContextList &sc_list) |
| 326 | { |
| 327 | if (resolve_scope & lldb::eSymbolContextCompUnit) |
| 328 | { |
| 329 | // Locate all compilation units with line numbers referencing the specified file. For example, if |
| 330 | // `file_spec` is <vector>, then this should return all source files and header files that reference |
| 331 | // <vector>, either directly or indirectly. |
| 332 | auto compilands = |
| 333 | m_session_up->findCompilandsForSourceFile(file_spec.GetPath(), llvm::PDB_NameSearchFlags::NS_CaseInsensitive); |
| 334 | |
| 335 | // For each one, either find get its previously parsed data, or parse it afresh and add it to |
| 336 | // the symbol context list. |
| 337 | while (auto compiland = compilands->getNext()) |
| 338 | { |
| 339 | // If we're not checking inlines, then don't add line information for this file unless the FileSpec |
| 340 | // matches. |
| 341 | if (!check_inlines) |
| 342 | { |
| 343 | // `getSourceFileName` returns the basename of the original source file used to generate this compiland. |
| 344 | // It does not return the full path. Currently the only way to get that is to do a basename lookup to |
| 345 | // get the IPDBSourceFile, but this is ambiguous in the case of two source files with the same name |
| 346 | // contributing to the same compiland. This is a moderately extreme edge case, so we consider this ok |
| 347 | // for now, although we need to find a long term solution. |
| 348 | std::string source_file = compiland->getSourceFileName(); |
| 349 | auto pdb_file = m_session_up->findOneSourceFile(compiland.get(), source_file, |
| 350 | llvm::PDB_NameSearchFlags::NS_CaseInsensitive); |
| 351 | source_file = pdb_file->getFileName(); |
| 352 | FileSpec this_spec(source_file, false, FileSpec::ePathSyntaxWindows); |
| 353 | if (!file_spec.FileEquals(this_spec)) |
| 354 | continue; |
| 355 | } |
| 356 | |
| 357 | SymbolContext sc; |
| 358 | auto cu = ParseCompileUnitForSymIndex(compiland->getSymIndexId()); |
| 359 | sc.comp_unit = cu.get(); |
| 360 | sc.module_sp = cu->GetModule(); |
| 361 | sc_list.Append(sc); |
| 362 | |
| 363 | // If we were asked to resolve line entries, add all entries to the line table that match the requested |
| 364 | // line (or all lines if `line` == 0) |
| 365 | if (resolve_scope & lldb::eSymbolContextLineEntry) |
| 366 | ParseCompileUnitLineTable(sc, line); |
| 367 | } |
| 368 | } |
| 369 | return sc_list.GetSize(); |
| 370 | } |
| 371 | |
| 372 | uint32_t |
| 373 | SymbolFilePDB::FindGlobalVariables(const lldb_private::ConstString &name, |
| 374 | const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, |
| 375 | uint32_t max_matches, lldb_private::VariableList &variables) |
| 376 | { |
| 377 | return uint32_t(); |
| 378 | } |
| 379 | |
| 380 | uint32_t |
| 381 | SymbolFilePDB::FindGlobalVariables(const lldb_private::RegularExpression ®ex, bool append, uint32_t max_matches, |
| 382 | lldb_private::VariableList &variables) |
| 383 | { |
| 384 | return uint32_t(); |
| 385 | } |
| 386 | |
| 387 | uint32_t |
| 388 | SymbolFilePDB::FindFunctions(const lldb_private::ConstString &name, |
| 389 | const lldb_private::CompilerDeclContext *parent_decl_ctx, uint32_t name_type_mask, |
| 390 | bool include_inlines, bool append, lldb_private::SymbolContextList &sc_list) |
| 391 | { |
| 392 | return uint32_t(); |
| 393 | } |
| 394 | |
| 395 | uint32_t |
| 396 | SymbolFilePDB::FindFunctions(const lldb_private::RegularExpression ®ex, bool include_inlines, bool append, |
| 397 | lldb_private::SymbolContextList &sc_list) |
| 398 | { |
| 399 | return uint32_t(); |
| 400 | } |
| 401 | |
| 402 | void |
| 403 | SymbolFilePDB::GetMangledNamesForFunction(const std::string &scope_qualified_name, |
| 404 | std::vector<lldb_private::ConstString> &mangled_names) |
| 405 | { |
| 406 | } |
| 407 | |
| 408 | uint32_t |
| 409 | SymbolFilePDB::FindTypes(const lldb_private::SymbolContext &sc, const lldb_private::ConstString &name, |
| 410 | const lldb_private::CompilerDeclContext *parent_decl_ctx, bool append, uint32_t max_matches, |
| 411 | llvm::DenseSet<lldb_private::SymbolFile *> &searched_symbol_files, |
| 412 | lldb_private::TypeMap &types) |
| 413 | { |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 414 | if (!append) |
| 415 | types.Clear(); |
| 416 | if (!name) |
| 417 | return 0; |
| 418 | |
| 419 | searched_symbol_files.clear(); |
| 420 | searched_symbol_files.insert(this); |
| 421 | |
| 422 | std::string name_str = name.AsCString(); |
| 423 | |
| 424 | // If this might be a regex, we have to return EVERY symbol and process them one by one, which is going |
| 425 | // to destroy performance on large PDB files. So try really hard not to use a regex match. |
| 426 | if (name_str.find_first_of("[]?*.-+\\") != std::string::npos) |
| 427 | FindTypesByRegex(name_str, max_matches, types); |
| 428 | else |
| 429 | FindTypesByName(name_str, max_matches, types); |
| 430 | return types.GetSize(); |
| 431 | } |
| 432 | |
| 433 | void |
| 434 | SymbolFilePDB::FindTypesByRegex(const std::string ®ex, uint32_t max_matches, lldb_private::TypeMap &types) |
| 435 | { |
| 436 | // When searching by regex, we need to go out of our way to limit the search space as much as possible, since |
| 437 | // the way this is implemented is by searching EVERYTHING in the PDB and manually doing a regex compare. PDB |
| 438 | // library isn't optimized for regex searches or searches across multiple symbol types at the same time, so the |
| 439 | // best we can do is to search enums, then typedefs, then classes one by one, and do a regex compare against all |
| 440 | // of them. |
| 441 | llvm::PDB_SymType tags_to_search[] = {llvm::PDB_SymType::Enum, llvm::PDB_SymType::Typedef, llvm::PDB_SymType::UDT}; |
| 442 | auto global = m_session_up->getGlobalScope(); |
| 443 | std::unique_ptr<llvm::IPDBEnumSymbols> results; |
| 444 | |
| 445 | std::regex re(regex); |
| 446 | |
| 447 | uint32_t matches = 0; |
| 448 | |
| 449 | for (auto tag : tags_to_search) |
| 450 | { |
| 451 | results = global->findAllChildren(tag); |
| 452 | while (auto result = results->getNext()) |
| 453 | { |
| 454 | if (max_matches > 0 && matches >= max_matches) |
| 455 | break; |
| 456 | |
| 457 | std::string type_name; |
| 458 | if (auto enum_type = llvm::dyn_cast<llvm::PDBSymbolTypeEnum>(result.get())) |
| 459 | type_name = enum_type->getName(); |
| 460 | else if (auto typedef_type = llvm::dyn_cast<llvm::PDBSymbolTypeTypedef>(result.get())) |
| 461 | type_name = typedef_type->getName(); |
| 462 | else if (auto class_type = llvm::dyn_cast<llvm::PDBSymbolTypeUDT>(result.get())) |
| 463 | type_name = class_type->getName(); |
| 464 | else |
| 465 | { |
| 466 | // We're only looking for types that have names. Skip symbols, as well as |
| 467 | // unnamed types such as arrays, pointers, etc. |
| 468 | continue; |
| 469 | } |
| 470 | |
| 471 | if (!std::regex_match(type_name, re)) |
| 472 | continue; |
| 473 | |
| 474 | // This should cause the type to get cached and stored in the `m_types` lookup. |
| 475 | if (!ResolveTypeUID(result->getSymIndexId())) |
| 476 | continue; |
| 477 | |
| 478 | auto iter = m_types.find(result->getSymIndexId()); |
| 479 | if (iter == m_types.end()) |
| 480 | continue; |
| 481 | types.Insert(iter->second); |
| 482 | ++matches; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void |
| 488 | SymbolFilePDB::FindTypesByName(const std::string &name, uint32_t max_matches, lldb_private::TypeMap &types) |
| 489 | { |
| 490 | auto global = m_session_up->getGlobalScope(); |
| 491 | std::unique_ptr<llvm::IPDBEnumSymbols> results; |
| 492 | results = global->findChildren(llvm::PDB_SymType::None, name.c_str(), llvm::PDB_NameSearchFlags::NS_Default); |
| 493 | |
| 494 | uint32_t matches = 0; |
| 495 | |
| 496 | while (auto result = results->getNext()) |
| 497 | { |
| 498 | if (max_matches > 0 && matches >= max_matches) |
| 499 | break; |
| 500 | switch (result->getSymTag()) |
| 501 | { |
| 502 | case llvm::PDB_SymType::Enum: |
| 503 | case llvm::PDB_SymType::UDT: |
| 504 | case llvm::PDB_SymType::Typedef: |
| 505 | break; |
| 506 | default: |
| 507 | // We're only looking for types that have names. Skip symbols, as well as |
| 508 | // unnamed types such as arrays, pointers, etc. |
| 509 | continue; |
| 510 | } |
| 511 | |
| 512 | // This should cause the type to get cached and stored in the `m_types` lookup. |
| 513 | if (!ResolveTypeUID(result->getSymIndexId())) |
| 514 | continue; |
| 515 | |
| 516 | auto iter = m_types.find(result->getSymIndexId()); |
| 517 | if (iter == m_types.end()) |
| 518 | continue; |
| 519 | types.Insert(iter->second); |
| 520 | ++matches; |
| 521 | } |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| 524 | size_t |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 525 | SymbolFilePDB::FindTypes(const std::vector<lldb_private::CompilerContext> &contexts, bool append, |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 526 | lldb_private::TypeMap &types) |
| 527 | { |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 528 | return 0; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | lldb_private::TypeList * |
| 532 | SymbolFilePDB::GetTypeList() |
| 533 | { |
| 534 | return nullptr; |
| 535 | } |
| 536 | |
| 537 | size_t |
| 538 | SymbolFilePDB::GetTypes(lldb_private::SymbolContextScope *sc_scope, uint32_t type_mask, |
| 539 | lldb_private::TypeList &type_list) |
| 540 | { |
| 541 | return size_t(); |
| 542 | } |
| 543 | |
| 544 | lldb_private::TypeSystem * |
| 545 | SymbolFilePDB::GetTypeSystemForLanguage(lldb::LanguageType language) |
| 546 | { |
| 547 | auto type_system = m_obj_file->GetModule()->GetTypeSystemForLanguage(language); |
| 548 | if (type_system) |
| 549 | type_system->SetSymbolFile(this); |
| 550 | return type_system; |
| 551 | } |
| 552 | |
| 553 | lldb_private::CompilerDeclContext |
| 554 | SymbolFilePDB::FindNamespace(const lldb_private::SymbolContext &sc, const lldb_private::ConstString &name, |
| 555 | const lldb_private::CompilerDeclContext *parent_decl_ctx) |
| 556 | { |
| 557 | return lldb_private::CompilerDeclContext(); |
| 558 | } |
| 559 | |
| 560 | lldb_private::ConstString |
| 561 | SymbolFilePDB::GetPluginName() |
| 562 | { |
| 563 | static ConstString g_name("pdb"); |
| 564 | return g_name; |
| 565 | } |
| 566 | |
| 567 | uint32_t |
| 568 | SymbolFilePDB::GetPluginVersion() |
| 569 | { |
| 570 | return 1; |
| 571 | } |
| 572 | |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 573 | llvm::IPDBSession & |
| 574 | SymbolFilePDB::GetPDBSession() |
| 575 | { |
| 576 | return *m_session_up; |
| 577 | } |
| 578 | |
| 579 | const llvm::IPDBSession & |
| 580 | SymbolFilePDB::GetPDBSession() const |
| 581 | { |
| 582 | return *m_session_up; |
| 583 | } |
| 584 | |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 585 | lldb::CompUnitSP |
| 586 | SymbolFilePDB::ParseCompileUnitForSymIndex(uint32_t id) |
| 587 | { |
| 588 | auto found_cu = m_comp_units.find(id); |
| 589 | if (found_cu != m_comp_units.end()) |
| 590 | return found_cu->second; |
| 591 | |
| 592 | auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(id); |
| 593 | |
| 594 | // `getSourceFileName` returns the basename of the original source file used to generate this compiland. It does |
| 595 | // not return the full path. Currently the only way to get that is to do a basename lookup to get the |
| 596 | // IPDBSourceFile, but this is ambiguous in the case of two source files with the same name contributing to the |
| 597 | // same compiland. This is a moderately extreme edge case, so we consider this ok for now, although we need to find |
| 598 | // a long term solution. |
| 599 | auto file = m_session_up->findOneSourceFile(cu.get(), cu->getSourceFileName(), |
| 600 | llvm::PDB_NameSearchFlags::NS_CaseInsensitive); |
| 601 | std::string path = file->getFileName(); |
| 602 | |
| 603 | lldb::LanguageType lang; |
| 604 | auto details = cu->findOneChild<llvm::PDBSymbolCompilandDetails>(); |
| 605 | if (!details) |
| 606 | lang = lldb::eLanguageTypeC_plus_plus; |
| 607 | else |
| 608 | lang = TranslateLanguage(details->getLanguage()); |
| 609 | |
| 610 | // Don't support optimized code for now, DebugInfoPDB does not return this information. |
| 611 | bool optimized = false; |
| 612 | auto result = std::make_shared<CompileUnit>(m_obj_file->GetModule(), nullptr, path.c_str(), id, lang, optimized); |
| 613 | m_comp_units.insert(std::make_pair(id, result)); |
| 614 | return result; |
| 615 | } |
| 616 | |
| 617 | bool |
| 618 | SymbolFilePDB::ParseCompileUnitLineTable(const lldb_private::SymbolContext &sc, uint32_t match_line) |
| 619 | { |
| 620 | auto global = m_session_up->getGlobalScope(); |
| 621 | auto cu = m_session_up->getConcreteSymbolById<llvm::PDBSymbolCompiland>(sc.comp_unit->GetID()); |
| 622 | |
| 623 | // LineEntry needs the *index* of the file into the list of support files returned by |
| 624 | // ParseCompileUnitSupportFiles. But the underlying SDK gives us a globally unique |
| 625 | // idenfitifier in the namespace of the PDB. So, we have to do a mapping so that we |
| 626 | // can hand out indices. |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 627 | llvm::DenseMap<uint32_t, uint32_t> index_map; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 628 | BuildSupportFileIdToSupportFileIndexMap(*cu, index_map); |
| 629 | auto line_table = llvm::make_unique<LineTable>(sc.comp_unit); |
| 630 | |
| 631 | // Find contributions to `cu` from all source and header files. |
| 632 | std::string path = sc.comp_unit->GetPath(); |
| 633 | auto files = m_session_up->getSourceFilesForCompiland(*cu); |
| 634 | |
| 635 | // For each source and header file, create a LineSequence for contributions to the cu |
| 636 | // from that file, and add the sequence. |
| 637 | while (auto file = files->getNext()) |
| 638 | { |
| 639 | std::unique_ptr<LineSequence> sequence(line_table->CreateLineSequenceContainer()); |
| 640 | auto lines = m_session_up->findLineNumbers(*cu, *file); |
| 641 | int entry_count = lines->getChildCount(); |
| 642 | |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 643 | uint64_t prev_addr; |
| 644 | uint32_t prev_length; |
| 645 | uint32_t prev_line; |
| 646 | uint32_t prev_source_idx; |
| 647 | |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 648 | for (int i = 0; i < entry_count; ++i) |
| 649 | { |
| 650 | auto line = lines->getChildAtIndex(i); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 651 | |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 652 | uint64_t lno = line->getLineNumber(); |
| 653 | uint64_t addr = line->getVirtualAddress(); |
| 654 | uint32_t length = line->getLength(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 655 | uint32_t source_id = line->getSourceFileId(); |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 656 | uint32_t col = line->getColumnNumber(); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 657 | uint32_t source_idx = index_map[source_id]; |
| 658 | |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 659 | // There was a gap between the current entry and the previous entry if the addresses don't perfectly line |
| 660 | // up. |
| 661 | bool is_gap = (i > 0) && (prev_addr + prev_length < addr); |
| 662 | |
| 663 | // Before inserting the current entry, insert a terminal entry at the end of the previous entry's address |
| 664 | // range if the current entry resulted in a gap from the previous entry. |
| 665 | if (is_gap && ShouldAddLine(match_line, prev_line, prev_length)) |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 666 | { |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 667 | line_table->AppendLineEntryToSequence(sequence.get(), prev_addr + prev_length, prev_line, 0, |
| 668 | prev_source_idx, false, false, false, false, true); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 671 | if (ShouldAddLine(match_line, lno, length)) |
| 672 | { |
| 673 | bool is_statement = line->isStatement(); |
| 674 | bool is_prologue = false; |
| 675 | bool is_epilogue = false; |
| 676 | auto func = m_session_up->findSymbolByAddress(addr, llvm::PDB_SymType::Function); |
| 677 | if (func) |
| 678 | { |
| 679 | auto prologue = func->findOneChild<llvm::PDBSymbolFuncDebugStart>(); |
| 680 | is_prologue = (addr == prologue->getVirtualAddress()); |
| 681 | |
| 682 | auto epilogue = func->findOneChild<llvm::PDBSymbolFuncDebugEnd>(); |
| 683 | is_epilogue = (addr == epilogue->getVirtualAddress()); |
| 684 | } |
| 685 | |
| 686 | line_table->AppendLineEntryToSequence(sequence.get(), addr, lno, col, source_idx, is_statement, false, |
| 687 | is_prologue, is_epilogue, false); |
| 688 | } |
| 689 | |
| 690 | prev_addr = addr; |
| 691 | prev_length = length; |
| 692 | prev_line = lno; |
| 693 | prev_source_idx = source_idx; |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 694 | } |
Zachary Turner | 7e8c7be | 2016-03-10 00:06:26 +0000 | [diff] [blame] | 695 | |
| 696 | if (entry_count > 0 && ShouldAddLine(match_line, prev_line, prev_length)) |
| 697 | { |
| 698 | // The end is always a terminal entry, so insert it regardless. |
| 699 | line_table->AppendLineEntryToSequence(sequence.get(), prev_addr + prev_length, prev_line, 0, |
| 700 | prev_source_idx, false, false, false, false, true); |
| 701 | } |
| 702 | |
| 703 | line_table->InsertSequence(sequence.release()); |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | sc.comp_unit->SetLineTable(line_table.release()); |
| 707 | return true; |
| 708 | } |
| 709 | |
| 710 | void |
| 711 | SymbolFilePDB::BuildSupportFileIdToSupportFileIndexMap(const llvm::PDBSymbolCompiland &cu, |
Zachary Turner | 42dff79 | 2016-04-15 00:21:26 +0000 | [diff] [blame] | 712 | llvm::DenseMap<uint32_t, uint32_t> &index_map) const |
Zachary Turner | 74e08ca | 2016-03-02 22:05:52 +0000 | [diff] [blame] | 713 | { |
| 714 | // This is a hack, but we need to convert the source id into an index into the support |
| 715 | // files array. We don't want to do path comparisons to avoid basename / full path |
| 716 | // issues that may or may not even be a problem, so we use the globally unique source |
| 717 | // file identifiers. Ideally we could use the global identifiers everywhere, but LineEntry |
| 718 | // currently assumes indices. |
| 719 | auto source_files = m_session_up->getSourceFilesForCompiland(cu); |
| 720 | int index = 0; |
| 721 | |
| 722 | while (auto file = source_files->getNext()) |
| 723 | { |
| 724 | uint32_t source_id = file->getUniqueId(); |
| 725 | index_map[source_id] = index++; |
| 726 | } |
| 727 | } |