LLDB mainline
SymbolLocatorDefault.cpp
Go to the documentation of this file.
1//===-- SymbolLocatorDefault.cpp ------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10
11#include <cstring>
12#include <optional>
13
15#include "lldb/Core/Debugger.h"
16#include "lldb/Core/Module.h"
20#include "lldb/Core/Progress.h"
21#include "lldb/Core/Section.h"
23#include "lldb/Host/Host.h"
25#include "lldb/Target/Target.h"
30#include "lldb/Utility/Log.h"
32#include "lldb/Utility/Timer.h"
33#include "lldb/Utility/UUID.h"
34
35#include "llvm/ADT/SmallSet.h"
36#include "llvm/Support/FileSystem.h"
37#include "llvm/Support/ThreadPool.h"
38
39#if defined(__FreeBSD__)
40#include <sys/sysctl.h>
41#endif
42
43// From MacOSX system header "mach/machine.h"
44typedef int cpu_type_t;
45typedef int cpu_subtype_t;
46
47using namespace lldb;
48using namespace lldb_private;
49
51
53
60
64
66 return "Default symbol locator.";
67}
68
72
74 const ModuleSpec &module_spec) {
75 const FileSpec &exec_fspec = module_spec.GetFileSpec();
76 const ArchSpec *arch = module_spec.GetArchitecturePtr();
77 const UUID *uuid = module_spec.GetUUIDPtr();
79 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
80 exec_fspec ? exec_fspec.GetFilename().nonEmptyOr("<NULL>").str().c_str()
81 : "<NULL>",
82 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
83
84 ModuleSpec matched_module_spec;
85 if (!exec_fspec)
86 return {};
88 exec_fspec, module_spec.GetObjectOffset(), module_spec.GetObjectSize(),
89 module_spec.GetExtractor());
90 if (module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) {
91 ModuleSpec result;
92 result.GetFileSpec() = exec_fspec;
93 return result;
94 }
95
96 return {};
97}
98
99// Keep "symbols.enable-external-lookup" description in sync with this function.
101 const ModuleSpec &module_spec, const FileSpecList &default_search_paths) {
102
103 FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec();
104 if (symbol_file_spec.IsAbsolute() &&
105 FileSystem::Instance().Exists(symbol_file_spec))
106 return symbol_file_spec;
107
108 Progress progress(
109 "Locating external symbol file",
110 module_spec.GetFileSpec().GetFilename().nonEmptyOr("<Unknown>").str());
111
112 FileSpecList debug_file_search_paths = default_search_paths;
113
114 // Add module directory.
115 FileSpec module_file_spec = module_spec.GetFileSpec();
116 // We keep the unresolved pathname if it fails.
117 FileSystem::Instance().ResolveSymbolicLink(module_file_spec,
118 module_file_spec);
119
120 llvm::StringRef file_dir = module_file_spec.GetDirectory();
121 {
122 FileSpec file_spec(file_dir.nonEmptyOr("."));
123 FileSystem::Instance().Resolve(file_spec);
124 debug_file_search_paths.AppendIfUnique(file_spec);
125 }
126
127 if (ModuleList::GetGlobalModuleListProperties().GetEnableExternalLookup()) {
128
129 // Add current working directory.
130 {
131 FileSpec file_spec(".");
132 FileSystem::Instance().Resolve(file_spec);
133 debug_file_search_paths.AppendIfUnique(file_spec);
134 }
135
136#ifndef _WIN32
137#if defined(__NetBSD__)
138 // Add /usr/libdata/debug directory.
139 {
140 FileSpec file_spec("/usr/libdata/debug");
141 FileSystem::Instance().Resolve(file_spec);
142 debug_file_search_paths.AppendIfUnique(file_spec);
143 }
144#else
145 // Add /usr/lib/debug directory.
146 {
147 FileSpec file_spec("/usr/lib/debug");
148 FileSystem::Instance().Resolve(file_spec);
149 debug_file_search_paths.AppendIfUnique(file_spec);
150 }
151#if defined(__FreeBSD__)
152 // Add $LOCALBASE/lib/debug directory, where LOCALBASE is
153 // usually /usr/local, but may be adjusted by the end user.
154 {
155 int mib[2];
156 char buf[PATH_MAX];
157 size_t len = PATH_MAX;
158
159 mib[0] = CTL_USER;
160 mib[1] = USER_LOCALBASE;
161 if (::sysctl(mib, 2, buf, &len, NULL, 0) == 0) {
162 FileSpec file_spec("/lib/debug");
163 file_spec.PrependPathComponent(llvm::StringRef(buf));
164 FileSystem::Instance().Resolve(file_spec);
165 debug_file_search_paths.AppendIfUnique(file_spec);
166 }
167 }
168#endif // __FreeBSD__
169#endif
170#endif // _WIN32
171 }
172
173 std::string uuid_str;
174 const UUID &module_uuid = module_spec.GetUUID();
175 if (module_uuid.IsValid()) {
176 // Some debug files are stored in the .build-id directory like this:
177 // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
178 uuid_str = module_uuid.GetAsString("");
179 std::transform(uuid_str.begin(), uuid_str.end(), uuid_str.begin(),
180 ::tolower);
181 uuid_str.insert(2, 1, '/');
182 uuid_str = uuid_str + ".debug";
183 }
184
185 size_t num_directories = debug_file_search_paths.GetSize();
186 for (size_t idx = 0; idx < num_directories; ++idx) {
187 FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex(idx);
188 FileSystem::Instance().Resolve(dirspec);
189 if (!FileSystem::Instance().IsDirectory(dirspec))
190 continue;
191
192 std::vector<std::string> files;
193 std::string dirname = dirspec.GetPath();
194
195 if (!uuid_str.empty())
196 files.push_back(dirname + "/.build-id/" + uuid_str);
197 if (!symbol_file_spec.GetFilename().empty()) {
198 files.push_back(dirname + "/" + symbol_file_spec.GetFilename().str());
199 files.push_back(dirname + "/.debug/" +
200 symbol_file_spec.GetFilename().str());
201
202 // Some debug files may stored in the module directory like this:
203 // /usr/lib/debug/usr/lib/library.so.debug
204 if (!file_dir.empty())
205 files.push_back(dirname + file_dir.str() + "/" +
206 symbol_file_spec.GetFilename().str());
207 }
208
209 const uint32_t num_files = files.size();
210 for (size_t idx_file = 0; idx_file < num_files; ++idx_file) {
211 const std::string &filename = files[idx_file];
212 FileSpec file_spec(filename);
213 FileSystem::Instance().Resolve(file_spec);
214
215 if (llvm::sys::fs::equivalent(file_spec.GetPath(),
216 module_file_spec.GetPath()))
217 continue;
218
219 if (FileSystem::Instance().Exists(file_spec)) {
222 ModuleSpec mspec;
223 bool valid_mspec = false;
224 if (specs.GetSize() == 2) {
225 // Special case to handle both i386 and i686 from ObjectFilePECOFF
226 ModuleSpec mspec2;
227 if (specs.GetModuleSpecAtIndex(0, mspec) &&
228 specs.GetModuleSpecAtIndex(1, mspec2) &&
229 mspec.GetArchitecture().GetTriple().isCompatibleWith(
230 mspec2.GetArchitecture().GetTriple())) {
231 valid_mspec = true;
232 }
233 }
234 if (!valid_mspec) {
235 assert(specs.GetSize() <= 1 &&
236 "Symbol Vendor supports only a single architecture");
237 if (specs.GetSize() == 1) {
238 if (specs.GetModuleSpecAtIndex(0, mspec)) {
239 valid_mspec = true;
240 }
241 }
242 }
243 if (valid_mspec) {
244 // Skip the uuids check if module_uuid is invalid. For example,
245 // this happens for *.dwp files since at the moment llvm-dwp
246 // doesn't output build ids, nor does binutils dwp.
247 if (!module_uuid.IsValid() || module_uuid == mspec.GetUUID())
248 return file_spec;
249 }
250 }
251 }
252 }
253
254 return {};
255}
256
258 Status &error,
259 bool force_lookup,
260 bool copy_executable) {
261 return false;
262}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_PLUGIN_DEFINE(PluginName)
int cpu_subtype_t
int cpu_type_t
#define LLDB_SCOPED_TIMERF(...)
Definition Timer.h:91
An architecture specification class.
Definition ArchSpec.h:32
llvm::Triple & GetTriple()
Architecture triple accessor.
Definition ArchSpec.h:465
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
Definition ArchSpec.cpp:568
A file collection class.
const FileSpec & GetFileSpecAtIndex(size_t idx) const
Get file at index.
size_t GetSize() const
Get the number of files in the file list.
bool AppendIfUnique(const FileSpec &file)
Append a FileSpec object if unique.
A file utility class.
Definition FileSpec.h:56
llvm::StringRef GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:248
bool IsAbsolute() const
Returns true if the filespec represents an absolute path.
Definition FileSpec.cpp:518
void PrependPathComponent(llvm::StringRef component)
Definition FileSpec.cpp:440
llvm::StringRef GetDirectory() const
Directory string const get accessor.
Definition FileSpec.h:233
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:380
Status ResolveSymbolicLink(const FileSpec &src, FileSpec &dst)
static FileSystem & Instance()
void Resolve(llvm::SmallVectorImpl< char > &path, bool force_make_absolute=false)
Resolve path to make it canonical.
static ModuleListProperties & GetGlobalModuleListProperties()
bool GetModuleSpecAtIndex(size_t i, ModuleSpec &module_spec) const
Definition ModuleSpec.h:386
bool FindMatchingModuleSpec(const ModuleSpec &module_spec, ModuleSpec &match_module_spec) const
Definition ModuleSpec.h:396
uint64_t GetObjectOffset() const
Definition ModuleSpec.h:111
FileSpec & GetFileSpec()
Definition ModuleSpec.h:57
ArchSpec & GetArchitecture()
Definition ModuleSpec.h:93
FileSpec & GetSymbolFileSpec()
Definition ModuleSpec.h:81
lldb::DataExtractorSP GetExtractor() const
Definition ModuleSpec.h:140
ArchSpec * GetArchitecturePtr()
Definition ModuleSpec.h:85
uint64_t GetObjectSize() const
Definition ModuleSpec.h:117
static ModuleSpecList GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataExtractorSP=lldb::DataExtractorSP())
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
A Progress indicator helper class.
Definition Progress.h:60
An error handling class.
Definition Status.h:118
static llvm::StringRef GetPluginNameStatic()
static bool DownloadObjectAndSymbolFile(ModuleSpec &module_spec, Status &error, bool force_lookup, bool copy_executable)
static llvm::StringRef GetPluginDescriptionStatic()
static std::optional< FileSpec > LocateExecutableSymbolFile(const ModuleSpec &module_spec, const FileSpecList &default_search_paths)
static std::optional< ModuleSpec > LocateExecutableObjectFile(const ModuleSpec &module_spec)
static lldb_private::SymbolLocator * CreateInstance()
Represents UUID's of various sizes.
Definition UUID.h:27
std::string GetAsString(llvm::StringRef separator="-") const
Definition UUID.cpp:54
bool IsValid() const
Definition UUID.h:69
A class that represents a running process on the host machine.
#define PATH_MAX