[Target] Return an llvm::Expected from GetEntryPointAddress (NFC)
authorJonas Devlieghere <jonas@devlieghere.com>
Fri, 19 Jul 2019 00:52:08 +0000 (00:52 +0000)
committerJonas Devlieghere <jonas@devlieghere.com>
Fri, 19 Jul 2019 00:52:08 +0000 (00:52 +0000)
Instead of taking a status and potentially returning an invalid address,
return an expected which is guaranteed to contain a valid address.

llvm-svn: 366521

lldb/include/lldb/Target/Target.h
lldb/source/Target/Target.cpp
lldb/source/Target/ThreadPlanCallFunction.cpp

index b1de193..af8090c 100644 (file)
@@ -1118,7 +1118,7 @@ public:
 
   /// This method will return the address of the starting function for
   /// this binary, e.g. main() or its equivalent.  This can be used as
-  /// an address of a function that is not called once a binary has 
+  /// an address of a function that is not called once a binary has
   /// started running - e.g. as a return address for inferior function
   /// calls that are unambiguous completion of the function call, not
   /// called during the course of the inferior function code running.
@@ -1130,9 +1130,9 @@ public:
   ///     be found, and may contain a helpful error message.
   //
   /// \return
-  ///     Returns the entry address for this program, LLDB_INVALID_ADDRESS
+  ///     Returns the entry address for this program, or an error
   ///     if none can be found.
-  lldb_private::Address GetEntryPointAddress(Status &err);
+  llvm::Expected<lldb_private::Address> GetEntryPointAddress();
 
   // Target Stop Hooks
   class StopHook : public UserID {
index 290094e..7c12b75 100644 (file)
@@ -2448,44 +2448,43 @@ lldb::addr_t Target::GetPersistentSymbol(ConstString name) {
   return address;
 }
 
-lldb_private::Address Target::GetEntryPointAddress(Status &err) {
-  err.Clear();
-  Address entry_addr;
+llvm::Expected<lldb_private::Address> Target::GetEntryPointAddress() {
   Module *exe_module = GetExecutableModulePointer();
+  llvm::Error error = llvm::Error::success();
+  assert(!error); // Check the success value when assertions are enabled.
 
   if (!exe_module || !exe_module->GetObjectFile()) {
-    err.SetErrorStringWithFormat("No primary executable found");
+    error = llvm::make_error<llvm::StringError>("No primary executable found",
+                                                llvm::inconvertibleErrorCode());
   } else {
-    entry_addr = exe_module->GetObjectFile()->GetEntryPointAddress();
-    if (!entry_addr.IsValid()) {
-      err.SetErrorStringWithFormat(
-         "Could not find entry point address for executable module \"%s\".",
-         exe_module->GetFileSpec().GetFilename().AsCString());
-    }
+    Address entry_addr = exe_module->GetObjectFile()->GetEntryPointAddress();
+    if (entry_addr.IsValid())
+      return entry_addr;
+
+    error = llvm::make_error<llvm::StringError>(
+        "Could not find entry point address for executable module \"" +
+            exe_module->GetFileSpec().GetFilename().GetStringRef() + "\"",
+        llvm::inconvertibleErrorCode());
   }
 
-  if (!entry_addr.IsValid()) {
     const ModuleList &modules = GetImages();
     const size_t num_images = modules.GetSize();
     for (size_t idx = 0; idx < num_images; ++idx) {
       ModuleSP module_sp(modules.GetModuleAtIndex(idx));
-      if (module_sp && module_sp->GetObjectFile()) {
-        entry_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
-        if (entry_addr.IsValid()) {
-          // Clear out any old error messages from the original
-          // main-executable-binary search; one of the other modules
-          // was able to provide an address.
-          err.Clear();
-          break;
-        }
-      }
+      if (!module_sp || !module_sp->GetObjectFile())
+        continue;
+
+      Address entry_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
+      if (entry_addr.IsValid()) {
+        // Discard the error.
+        llvm::consumeError(std::move(error));
+        return entry_addr;
     }
   }
 
-  return entry_addr;
+  return std::move(error);
 }
 
-
 lldb::addr_t Target::GetCallableLoadAddress(lldb::addr_t load_addr,
                                             AddressClass addr_class) const {
   auto arch_plugin = GetArchitecturePlugin();
index 57f4ef5..c4cf133 100644 (file)
@@ -65,19 +65,17 @@ bool ThreadPlanCallFunction::ConstructorSetup(
     return false;
   }
 
-  m_start_addr = GetTarget().GetEntryPointAddress(error);
-
-  if (log && error.Fail()) {
-    m_constructor_errors.Printf("%s", error.AsCString());
-    log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
-                 m_constructor_errors.GetData());
-    return false;
-  }
-
-  if (!m_start_addr.IsValid()) {
+  llvm::Expected<Address> start_address = GetTarget().GetEntryPointAddress();
+  if (!start_address) {
+    m_constructor_errors.Printf(
+        "%s", llvm::toString(start_address.takeError()).c_str());
+    if (log)
+      log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
+                  m_constructor_errors.GetData());
     return false;
   }
 
+  m_start_addr = *start_address;
   start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
 
   // Checkpoint the thread state so we can restore it later.