|
| 1 | +//===-- ModuleTest.cpp ----------------------------------------------------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "lldb/Core/Module.h" |
| 10 | +#include "Plugins/Language/CPlusPlus/CPlusPlusLanguage.h" |
| 11 | +#include "Plugins/ObjectFile/ELF/ObjectFileELF.h" |
| 12 | +#include "Plugins/Platform/Linux/PlatformLinux.h" |
| 13 | +#include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h" |
| 14 | +#include "TestingSupport/SubsystemRAII.h" |
| 15 | +#include "TestingSupport/TestUtilities.h" |
| 16 | +#include "lldb/Core/Debugger.h" |
| 17 | +#include "lldb/Core/PluginManager.h" |
| 18 | +#include "lldb/Host/FileSystem.h" |
| 19 | +#include "lldb/Host/HostInfo.h" |
| 20 | +#include "lldb/Target/Language.h" |
| 21 | +#include "lldb/lldb-enumerations.h" |
| 22 | +#include "gtest/gtest.h" |
| 23 | +#include <optional> |
| 24 | + |
| 25 | +using namespace lldb; |
| 26 | +using namespace lldb_private; |
| 27 | + |
| 28 | +// Test that Module::FindFunctions correctly finds C++ mangled symbols |
| 29 | +// even when multiple language plugins are registered. |
| 30 | +TEST(ModuleTest, FindFunctionsCppMangledName) { |
| 31 | + // Create a mock language. The point of this language is to return something |
| 32 | + // in GetFunctionNameInfo that would interfere with the C++ language plugin, |
| 33 | + // were they sharing the same LookupInfo. |
| 34 | + class MockLanguageWithBogusLookupInfo : public Language { |
| 35 | + public: |
| 36 | + MockLanguageWithBogusLookupInfo() = default; |
| 37 | + ~MockLanguageWithBogusLookupInfo() override = default; |
| 38 | + |
| 39 | + lldb::LanguageType GetLanguageType() const override { |
| 40 | + // The language here doesn't really matter, it just has to be something |
| 41 | + // that is not C/C++/ObjC. |
| 42 | + return lldb::eLanguageTypeSwift; |
| 43 | + } |
| 44 | + |
| 45 | + llvm::StringRef GetPluginName() override { return "mock-bogus-language"; } |
| 46 | + |
| 47 | + bool IsSourceFile(llvm::StringRef file_path) const override { |
| 48 | + return file_path.ends_with(".swift"); |
| 49 | + } |
| 50 | + |
| 51 | + std::pair<lldb::FunctionNameType, std::optional<ConstString>> |
| 52 | + GetFunctionNameInfo(ConstString name) const override { |
| 53 | + // Say that every function is a selector. |
| 54 | + return {lldb::eFunctionNameTypeSelector, ConstString("BOGUS_BASENAME")}; |
| 55 | + } |
| 56 | + |
| 57 | + static void Initialize() { |
| 58 | + PluginManager::RegisterPlugin(GetPluginNameStatic(), "Mock Language", |
| 59 | + CreateInstance); |
| 60 | + } |
| 61 | + |
| 62 | + static void Terminate() { PluginManager::UnregisterPlugin(CreateInstance); } |
| 63 | + |
| 64 | + static lldb_private::Language *CreateInstance(lldb::LanguageType language) { |
| 65 | + if (language == lldb::eLanguageTypeSwift) |
| 66 | + return new MockLanguageWithBogusLookupInfo(); |
| 67 | + return nullptr; |
| 68 | + } |
| 69 | + |
| 70 | + static llvm::StringRef GetPluginNameStatic() { |
| 71 | + return "mock-bogus-language"; |
| 72 | + } |
| 73 | + }; |
| 74 | + SubsystemRAII<FileSystem, HostInfo, ObjectFileELF, SymbolFileSymtab, |
| 75 | + CPlusPlusLanguage, platform_linux::PlatformLinux, |
| 76 | + MockLanguageWithBogusLookupInfo> |
| 77 | + subsystems; |
| 78 | + |
| 79 | + ArchSpec arch("x86_64-pc-linux"); |
| 80 | + Platform::SetHostPlatform( |
| 81 | + platform_linux::PlatformLinux::CreateInstance(true, &arch)); |
| 82 | + |
| 83 | + std::call_once(TestUtilities::g_debugger_initialize_flag, |
| 84 | + []() { Debugger::Initialize(nullptr); }); |
| 85 | + |
| 86 | + // Create a simple ELF module with std::vector::size() as the only symbol. |
| 87 | + auto ExpectedFile = TestFile::fromYaml(R"( |
| 88 | +--- !ELF |
| 89 | +FileHeader: |
| 90 | + Class: ELFCLASS64 |
| 91 | + Data: ELFDATA2LSB |
| 92 | + Type: ET_DYN |
| 93 | + Machine: EM_X86_64 |
| 94 | +Sections: |
| 95 | + - Name: .text |
| 96 | + Type: SHT_PROGBITS |
| 97 | + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] |
| 98 | + Address: 0x1000 |
| 99 | + AddressAlign: 0x10 |
| 100 | + Size: 0x100 |
| 101 | +Symbols: |
| 102 | + - Name: _ZNSt6vectorIiE4sizeEv |
| 103 | + Type: STT_FUNC |
| 104 | + Section: .text |
| 105 | + Value: 0x1030 |
| 106 | + Size: 0x20 |
| 107 | +... |
| 108 | +)"); |
| 109 | + ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded()); |
| 110 | + |
| 111 | + DebuggerSP debugger_sp = Debugger::CreateInstance(); |
| 112 | + ASSERT_TRUE(debugger_sp); |
| 113 | + |
| 114 | + TargetSP target_sp; |
| 115 | + PlatformSP platform_sp; |
| 116 | + |
| 117 | + Status error = debugger_sp->GetTargetList().CreateTarget( |
| 118 | + *debugger_sp, "", arch, eLoadDependentsNo, platform_sp, target_sp); |
| 119 | + ASSERT_TRUE(error.Success()); |
| 120 | + ASSERT_TRUE(target_sp); |
| 121 | + |
| 122 | + auto module_sp = std::make_shared<Module>(ExpectedFile->moduleSpec()); |
| 123 | + target_sp->GetImages().Append(module_sp); |
| 124 | + |
| 125 | + // Verify both C++ and our mock language are registered. |
| 126 | + Language *cpp_lang = Language::FindPlugin(lldb::eLanguageTypeC_plus_plus); |
| 127 | + Language *mock_lang = Language::FindPlugin(lldb::eLanguageTypeSwift); |
| 128 | + ASSERT_NE(cpp_lang, nullptr) << "C++ language plugin should be registered"; |
| 129 | + ASSERT_NE(mock_lang, nullptr) |
| 130 | + << "Mock Swift language plugin should be registered"; |
| 131 | + |
| 132 | + ModuleFunctionSearchOptions function_options; |
| 133 | + function_options.include_symbols = true; |
| 134 | + |
| 135 | + ConstString symbol_name("_ZNSt6vectorIiE4sizeEv"); |
| 136 | + SymbolContextList results; |
| 137 | + module_sp->FindFunctions(symbol_name, CompilerDeclContext(), |
| 138 | + eFunctionNameTypeAuto, function_options, results); |
| 139 | + |
| 140 | + // Assert that we found one symbol. |
| 141 | + ASSERT_EQ(results.GetSize(), 1u); |
| 142 | + |
| 143 | + auto result = results[0]; |
| 144 | + auto name = result.GetFunctionName(); |
| 145 | + // Assert that the symbol we found is what we expected. |
| 146 | + ASSERT_EQ(name, "std::vector<int>::size()"); |
| 147 | + ASSERT_EQ(result.GetLanguage(), eLanguageTypeC_plus_plus); |
| 148 | +} |
0 commit comments