diff --git a/lldb/source/API/SBTarget.cpp b/lldb/source/API/SBTarget.cpp --- a/lldb/source/API/SBTarget.cpp +++ b/lldb/source/API/SBTarget.cpp @@ -1586,8 +1586,16 @@ if (path) module_spec.GetFileSpec().SetFile(path, FileSpec::Style::native); - if (uuid_cstr) - module_spec.GetUUID().SetFromStringRef(uuid_cstr); + if (uuid_cstr) { + llvm::StringRef uuid_str = llvm::StringRef(uuid_cstr).trim(); + llvm::SmallVector bytes; + llvm::StringRef rest = + UUID::DecodeUUIDBytesFromString(uuid_str, bytes, 20); + // If the UUID string was consumed and it is not empty, let us use it for + // the lookup. + if (rest.empty() && bytes.size() > 0) + module_spec.GetUUID() = UUID::fromData(bytes); + } if (triple) module_spec.GetArchitecture() = Platform::GetAugmentedArchSpec( diff --git a/lldb/test/API/python_api/addmodule-uuid/Makefile b/lldb/test/API/python_api/addmodule-uuid/Makefile new file mode 100644 --- /dev/null +++ b/lldb/test/API/python_api/addmodule-uuid/Makefile @@ -0,0 +1,3 @@ +CXX_SOURCES := main.cpp + +include Makefile.rules diff --git a/lldb/test/API/python_api/addmodule-uuid/TestAddModuleUUID.py b/lldb/test/API/python_api/addmodule-uuid/TestAddModuleUUID.py new file mode 100644 --- /dev/null +++ b/lldb/test/API/python_api/addmodule-uuid/TestAddModuleUUID.py @@ -0,0 +1,45 @@ +"""Test Python APIs for adding modules""" + +import lldb +from lldbsuite.test.decorators import * +from lldbsuite.test.lldbtest import * +from lldbsuite.test import lldbutil + +class TestAddModuleUUID(TestBase): + mydir = TestBase.compute_mydir(__file__) + NO_DEBUG_INFO_TESTCASE = True + + @skipIf(oslist=no_match(['linux'])) + def test_add_existing_module_with_buildid_fast(self): + self.run_test_add_existing_module_with_uuid('-Wl,--build-id=fast') + + @skipIf(oslist=no_match(['linux'])) + def test_add_existing_module_with_buildid_sha1(self): + self.run_test_add_existing_module_with_uuid('-Wl,--build-id=sha1') + + @skipIf(oslist=no_match(['linux'])) + def test_add_existing_module_with_buildid_uuid(self): + self.run_test_add_existing_module_with_uuid('-Wl,--build-id=uuid') + + def run_test_add_existing_module_with_uuid(self, ldflags): + d = self.getBuildFlags() + d['LD_EXTRAS'] = ldflags + self.build(dictionary=d) + (target, _, _, _) = lldbutil.run_to_source_breakpoint( + self, "// break here", lldb.SBFileSpec("main.cpp")) + module = target.FindModule(target.GetExecutable()) + + # Check that adding the existing module gives that module back. + path = str(module.GetFileSpec()) + uuid = module.GetUUIDString(); + add_result = target.AddModule(path, "", uuid) + self.assertEqual(module, add_result) + + # Change the UUID and check that adding with wrong UUID fails. + if len(uuid) != 0: + if uuid[0] == "0": + different_uuid = "1" + uuid[1:] + else: + different_uuid = "0" + uuid[1:] + add_result = target.AddModule(path, "", different_uuid) + self.assertTrue(not add_result.IsValid()) diff --git a/lldb/test/API/python_api/addmodule-uuid/main.cpp b/lldb/test/API/python_api/addmodule-uuid/main.cpp new file mode 100644 --- /dev/null +++ b/lldb/test/API/python_api/addmodule-uuid/main.cpp @@ -0,0 +1,3 @@ +int main() { + return 0; // break here +}