Skip to content

Commit 0288c26

Browse files
committed
[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
1 parent cb30520 commit 0288c26

File tree

3 files changed

+33
-36
lines changed

3 files changed

+33
-36
lines changed

lldb/include/lldb/Target/Target.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ class Target : public std::enable_shared_from_this<Target>,
11181118

11191119
/// This method will return the address of the starting function for
11201120
/// this binary, e.g. main() or its equivalent. This can be used as
1121-
/// an address of a function that is not called once a binary has
1121+
/// an address of a function that is not called once a binary has
11221122
/// started running - e.g. as a return address for inferior function
11231123
/// calls that are unambiguous completion of the function call, not
11241124
/// called during the course of the inferior function code running.
@@ -1130,9 +1130,9 @@ class Target : public std::enable_shared_from_this<Target>,
11301130
/// be found, and may contain a helpful error message.
11311131
//
11321132
/// \return
1133-
/// Returns the entry address for this program, LLDB_INVALID_ADDRESS
1133+
/// Returns the entry address for this program, or an error
11341134
/// if none can be found.
1135-
lldb_private::Address GetEntryPointAddress(Status &err);
1135+
llvm::Expected<lldb_private::Address> GetEntryPointAddress();
11361136

11371137
// Target Stop Hooks
11381138
class StopHook : public UserID {

lldb/source/Target/Target.cpp

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,44 +2448,43 @@ lldb::addr_t Target::GetPersistentSymbol(ConstString name) {
24482448
return address;
24492449
}
24502450

2451-
lldb_private::Address Target::GetEntryPointAddress(Status &err) {
2452-
err.Clear();
2453-
Address entry_addr;
2451+
llvm::Expected<lldb_private::Address> Target::GetEntryPointAddress() {
24542452
Module *exe_module = GetExecutableModulePointer();
2453+
llvm::Error error = llvm::Error::success();
2454+
assert(!error); // Check the success value when assertions are enabled.
24552455

24562456
if (!exe_module || !exe_module->GetObjectFile()) {
2457-
err.SetErrorStringWithFormat("No primary executable found");
2457+
error = llvm::make_error<llvm::StringError>("No primary executable found",
2458+
llvm::inconvertibleErrorCode());
24582459
} else {
2459-
entry_addr = exe_module->GetObjectFile()->GetEntryPointAddress();
2460-
if (!entry_addr.IsValid()) {
2461-
err.SetErrorStringWithFormat(
2462-
"Could not find entry point address for executable module \"%s\".",
2463-
exe_module->GetFileSpec().GetFilename().AsCString());
2464-
}
2460+
Address entry_addr = exe_module->GetObjectFile()->GetEntryPointAddress();
2461+
if (entry_addr.IsValid())
2462+
return entry_addr;
2463+
2464+
error = llvm::make_error<llvm::StringError>(
2465+
"Could not find entry point address for executable module \"" +
2466+
exe_module->GetFileSpec().GetFilename().GetStringRef() + "\"",
2467+
llvm::inconvertibleErrorCode());
24652468
}
24662469

2467-
if (!entry_addr.IsValid()) {
24682470
const ModuleList &modules = GetImages();
24692471
const size_t num_images = modules.GetSize();
24702472
for (size_t idx = 0; idx < num_images; ++idx) {
24712473
ModuleSP module_sp(modules.GetModuleAtIndex(idx));
2472-
if (module_sp && module_sp->GetObjectFile()) {
2473-
entry_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
2474-
if (entry_addr.IsValid()) {
2475-
// Clear out any old error messages from the original
2476-
// main-executable-binary search; one of the other modules
2477-
// was able to provide an address.
2478-
err.Clear();
2479-
break;
2480-
}
2481-
}
2474+
if (!module_sp || !module_sp->GetObjectFile())
2475+
continue;
2476+
2477+
Address entry_addr = module_sp->GetObjectFile()->GetEntryPointAddress();
2478+
if (entry_addr.IsValid()) {
2479+
// Discard the error.
2480+
llvm::consumeError(std::move(error));
2481+
return entry_addr;
24822482
}
24832483
}
24842484

2485-
return entry_addr;
2485+
return std::move(error);
24862486
}
24872487

2488-
24892488
lldb::addr_t Target::GetCallableLoadAddress(lldb::addr_t load_addr,
24902489
AddressClass addr_class) const {
24912490
auto arch_plugin = GetArchitecturePlugin();

lldb/source/Target/ThreadPlanCallFunction.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,17 @@ bool ThreadPlanCallFunction::ConstructorSetup(
6565
return false;
6666
}
6767

68-
m_start_addr = GetTarget().GetEntryPointAddress(error);
69-
70-
if (log && error.Fail()) {
71-
m_constructor_errors.Printf("%s", error.AsCString());
72-
log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
73-
m_constructor_errors.GetData());
74-
return false;
75-
}
76-
77-
if (!m_start_addr.IsValid()) {
68+
llvm::Expected<Address> start_address = GetTarget().GetEntryPointAddress();
69+
if (!start_address) {
70+
m_constructor_errors.Printf(
71+
"%s", llvm::toString(start_address.takeError()).c_str());
72+
if (log)
73+
log->Printf("ThreadPlanCallFunction(%p): %s.", static_cast<void *>(this),
74+
m_constructor_errors.GetData());
7875
return false;
7976
}
8077

78+
m_start_addr = *start_address;
8179
start_load_addr = m_start_addr.GetLoadAddress(&GetTarget());
8280

8381
// Checkpoint the thread state so we can restore it later.

0 commit comments

Comments
 (0)