From 0288c269689bfa7b6b90da1a1e058c9ebb6fa278 Mon Sep 17 00:00:00 2001 From: Jonas Devlieghere Date: Fri, 19 Jul 2019 00:52:08 +0000 Subject: [PATCH] [Target] Return an llvm::Expected from GetEntryPointAddress (NFC) 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 | 6 ++-- lldb/source/Target/Target.cpp | 45 +++++++++++++-------------- lldb/source/Target/ThreadPlanCallFunction.cpp | 18 +++++------ 3 files changed, 33 insertions(+), 36 deletions(-) diff --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h index b1de193..af8090c 100644 --- a/lldb/include/lldb/Target/Target.h +++ b/lldb/include/lldb/Target/Target.h @@ -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 GetEntryPointAddress(); // Target Stop Hooks class StopHook : public UserID { diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp index 290094e..7c12b75 100644 --- a/lldb/source/Target/Target.cpp +++ b/lldb/source/Target/Target.cpp @@ -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 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("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( + "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(); diff --git a/lldb/source/Target/ThreadPlanCallFunction.cpp b/lldb/source/Target/ThreadPlanCallFunction.cpp index 57f4ef5..c4cf133 100644 --- a/lldb/source/Target/ThreadPlanCallFunction.cpp +++ b/lldb/source/Target/ThreadPlanCallFunction.cpp @@ -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(this), - m_constructor_errors.GetData()); - return false; - } - - if (!m_start_addr.IsValid()) { + llvm::Expected
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(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. -- 2.7.4