Make SBDebugger::CreateTargetWithFileAndArch work with lldb::LLDB_DEFAULT_ARCH
authorJim Ingham <jingham@apple.com>
Tue, 26 Jan 2021 20:15:09 +0000 (12:15 -0800)
committerJim Ingham <jingham@apple.com>
Tue, 26 Jan 2021 20:17:39 +0000 (12:17 -0800)
Second try, handling both a bogus arch string and the "null file & arch" used
to create an empty but valid target.
Also check in that case before logging (previously the logging would have
crashed.)

lldb/source/API/SBDebugger.cpp
lldb/test/API/python_api/target/TestTargetAPI.py

index dc1cc91..6245b3a 100644 (file)
@@ -804,21 +804,33 @@ SBTarget SBDebugger::CreateTargetWithFileAndArch(const char *filename,
   TargetSP target_sp;
   if (m_opaque_sp) {
     Status error;
-    const bool add_dependent_modules = true;
-
-    error = m_opaque_sp->GetTargetList().CreateTarget(
-        *m_opaque_sp, filename, arch_cstr,
-        add_dependent_modules ? eLoadDependentsYes : eLoadDependentsNo, nullptr,
-        target_sp);
-
+    if (arch_cstr == nullptr) {
+      // The version of CreateTarget that takes an ArchSpec won't accept an
+      // empty ArchSpec, so when the arch hasn't been specified, we need to
+      // call the target triple version.
+      error = m_opaque_sp->GetTargetList().CreateTarget(*m_opaque_sp, filename, 
+          arch_cstr, eLoadDependentsYes, nullptr, target_sp);
+    } else {
+      PlatformSP platform_sp = m_opaque_sp->GetPlatformList()
+          .GetSelectedPlatform();
+      ArchSpec arch = Platform::GetAugmentedArchSpec(platform_sp.get(), 
+          arch_cstr);
+      if (arch.IsValid())
+        error = m_opaque_sp->GetTargetList().CreateTarget(*m_opaque_sp, filename, 
+            arch, eLoadDependentsYes, platform_sp, target_sp);
+      else
+        error.SetErrorStringWithFormat("invalid arch_cstr: %s", arch_cstr);
+    }
     if (error.Success())
       sb_target.SetSP(target_sp);
   }
-
+  
   LLDB_LOGF(log,
             "SBDebugger(%p)::CreateTargetWithFileAndArch (filename=\"%s\", "
             "arch=%s) => SBTarget(%p)",
-            static_cast<void *>(m_opaque_sp.get()), filename, arch_cstr,
+            static_cast<void *>(m_opaque_sp.get()),
+            filename ? filename : "<unspecified>",
+            arch_cstr ? arch_cstr : "<unspecified>",
             static_cast<void *>(target_sp.get()));
 
   return LLDB_RECORD_RESULT(sb_target);
index f058a0a..12c9d9d 100644 (file)
@@ -476,3 +476,16 @@ class TargetAPITestCase(TestBase):
         desc2 = get_description(symbol2)
         self.assertTrue(desc1 and desc2 and desc1 == desc2,
                         "The two addresses should resolve to the same symbol")
+    def test_default_arch(self):
+        """ Test the other two target create methods using LLDB_ARCH_DEFAULT. """
+        self.build()
+        exe = self.getBuildArtifact("a.out")
+        target = self.dbg.CreateTargetWithFileAndArch(exe, lldb.LLDB_ARCH_DEFAULT)
+        self.assertTrue(target.IsValid(), "Default arch made a valid target.")
+        # This should also work with the target's triple:
+        target2 = self.dbg.CreateTargetWithFileAndArch(exe, target.GetTriple())
+        self.assertTrue(target2.IsValid(), "Round trip with triple works")
+        # And this triple should work for the FileAndTriple API:
+        target3 = self.dbg.CreateTargetWithFileAndTargetTriple(exe, target.GetTriple())
+        self.assertTrue(target3.IsValid())
+