Skip to content

Commit ed7e468

Browse files
authored
[lldb] Improve error message for unrecognized executables (llvm#97490)
Currently, LLDB prints out a rather unhelpful error message when passed a file that it doesn't recognize as an executable. > error: '/path/to/file' doesn't contain any 'host' platform > architectures: arm64, armv7, armv7f, armv7k, armv7s, armv7m, armv7em, > armv6m, armv6, armv5, armv4, arm, thumbv7, thumbv7k, thumbv7s, > thumbv7f, thumbv7m, thumbv7em, thumbv6m, thumbv6, thumbv5, thumbv4t, > thumb, x86_64, x86_64, arm64, arm64e I did a quick search internally and found at least 24 instances of users being confused by this. This patch improves the error message when it doesn't recognize the file as an executable, but keeps the existing error message otherwise, i.e. when it's an object file we understand, but the current platform doesn't support.
1 parent 1952dba commit ed7e468

File tree

4 files changed

+55
-44
lines changed

4 files changed

+55
-44
lines changed

lldb/include/lldb/Symbol/ObjectFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
178178
lldb::offset_t file_offset,
179179
lldb::offset_t file_size,
180180
lldb_private::ModuleSpecList &specs);
181+
static bool IsObjectFile(lldb_private::FileSpec file_spec);
181182
/// Split a path into a file path with object name.
182183
///
183184
/// For paths like "/tmp/foo.a(bar.o)" we often need to split a path up into

lldb/source/Symbol/ObjectFile.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,15 @@ ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp,
184184
return object_file_sp;
185185
}
186186

187+
bool ObjectFile::IsObjectFile(lldb_private::FileSpec file_spec) {
188+
DataBufferSP data_sp;
189+
offset_t data_offset = 0;
190+
ModuleSP module_sp = std::make_shared<Module>(file_spec);
191+
return static_cast<bool>(ObjectFile::FindPlugin(
192+
module_sp, &file_spec, 0, FileSystem::Instance().GetByteSize(file_spec),
193+
data_sp, data_offset));
194+
}
195+
187196
size_t ObjectFile::GetModuleSpecifications(const FileSpec &file,
188197
lldb::offset_t file_offset,
189198
lldb::offset_t file_size,

lldb/source/Target/Platform.cpp

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,6 @@ Status
732732
Platform::ResolveExecutable(const ModuleSpec &module_spec,
733733
lldb::ModuleSP &exe_module_sp,
734734
const FileSpecList *module_search_paths_ptr) {
735-
Status error;
736735

737736
// We may connect to a process and use the provided executable (Don't use
738737
// local $PATH).
@@ -741,55 +740,57 @@ Platform::ResolveExecutable(const ModuleSpec &module_spec,
741740
// Resolve any executable within a bundle on MacOSX
742741
Host::ResolveExecutableInBundle(resolved_module_spec.GetFileSpec());
743742

744-
if (FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()) ||
745-
module_spec.GetUUID().IsValid()) {
746-
if (resolved_module_spec.GetArchitecture().IsValid() ||
747-
resolved_module_spec.GetUUID().IsValid()) {
748-
error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
749-
module_search_paths_ptr, nullptr,
750-
nullptr);
743+
if (!FileSystem::Instance().Exists(resolved_module_spec.GetFileSpec()) &&
744+
!module_spec.GetUUID().IsValid())
745+
return Status::createWithFormat("'{0}' does not exist",
746+
resolved_module_spec.GetFileSpec());
747+
748+
if (resolved_module_spec.GetArchitecture().IsValid() ||
749+
resolved_module_spec.GetUUID().IsValid()) {
750+
Status error =
751+
ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
752+
module_search_paths_ptr, nullptr, nullptr);
751753

754+
if (exe_module_sp && exe_module_sp->GetObjectFile())
755+
return error;
756+
exe_module_sp.reset();
757+
}
758+
// No valid architecture was specified or the exact arch wasn't found.
759+
// Ask the platform for the architectures that we should be using (in the
760+
// correct order) and see if we can find a match that way.
761+
StreamString arch_names;
762+
llvm::ListSeparator LS;
763+
ArchSpec process_host_arch;
764+
Status error;
765+
for (const ArchSpec &arch : GetSupportedArchitectures(process_host_arch)) {
766+
resolved_module_spec.GetArchitecture() = arch;
767+
error =
768+
ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
769+
module_search_paths_ptr, nullptr, nullptr);
770+
if (error.Success()) {
752771
if (exe_module_sp && exe_module_sp->GetObjectFile())
753-
return error;
754-
exe_module_sp.reset();
772+
break;
773+
error.SetErrorToGenericError();
755774
}
756-
// No valid architecture was specified or the exact arch wasn't found.
757-
// Ask the platform for the architectures that we should be using (in the
758-
// correct order) and see if we can find a match that way.
759-
StreamString arch_names;
760-
llvm::ListSeparator LS;
761-
ArchSpec process_host_arch;
762-
for (const ArchSpec &arch : GetSupportedArchitectures(process_host_arch)) {
763-
resolved_module_spec.GetArchitecture() = arch;
764-
error = ModuleList::GetSharedModule(resolved_module_spec, exe_module_sp,
765-
module_search_paths_ptr, nullptr,
766-
nullptr);
767-
if (error.Success()) {
768-
if (exe_module_sp && exe_module_sp->GetObjectFile())
769-
break;
770-
error.SetErrorToGenericError();
771-
}
772775

773-
arch_names << LS << arch.GetArchitectureName();
774-
}
776+
arch_names << LS << arch.GetArchitectureName();
777+
}
775778

776-
if (error.Fail() || !exe_module_sp) {
777-
if (FileSystem::Instance().Readable(resolved_module_spec.GetFileSpec())) {
778-
error.SetErrorStringWithFormatv(
779-
"'{0}' doesn't contain any '{1}' platform architectures: {2}",
780-
resolved_module_spec.GetFileSpec(), GetPluginName(),
781-
arch_names.GetData());
782-
} else {
783-
error.SetErrorStringWithFormatv("'{0}' is not readable",
784-
resolved_module_spec.GetFileSpec());
785-
}
786-
}
787-
} else {
788-
error.SetErrorStringWithFormatv("'{0}' does not exist",
779+
if (exe_module_sp && error.Success())
780+
return {};
781+
782+
if (!FileSystem::Instance().Readable(resolved_module_spec.GetFileSpec()))
783+
return Status::createWithFormat("'{0}' is not readable",
789784
resolved_module_spec.GetFileSpec());
790-
}
791785

792-
return error;
786+
if (!ObjectFile::IsObjectFile(resolved_module_spec.GetFileSpec()))
787+
return Status::createWithFormat("'{0}' is not a valid executable",
788+
resolved_module_spec.GetFileSpec());
789+
790+
return Status::createWithFormat(
791+
"'{0}' doesn't contain any '{1}' platform architectures: {2}",
792+
resolved_module_spec.GetFileSpec(), GetPluginName(),
793+
arch_names.GetData());
793794
}
794795

795796
Status Platform::ResolveSymbolFile(Target &target, const ModuleSpec &sym_spec,

lldb/test/Shell/ObjectFile/PECOFF/invalid-export-table.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# RUN: yaml2obj %s -o %t.exe
55
# RUN: %lldb %t.exe 2>&1 | FileCheck %s
66

7-
# CHECK: error: '{{.*}}' doesn't contain any {{.*}} platform architectures
7+
# CHECK: error: '{{.*}}' is not a valid executable
88
--- !COFF
99
OptionalHeader:
1010
AddressOfEntryPoint: 4096

0 commit comments

Comments
 (0)