blob: 9e0a3b5bf4dfafe9200768bef3b08a8254ca29ce [file] [log] [blame]
Eli Friedman5423ebf2010-07-02 19:28:44 +00001//===-- Symbols.cpp ---------------------------------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Host/Symbols.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000011#include "lldb/Core/ArchSpec.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000012#include "lldb/Core/Module.h"
13#include "lldb/Core/ModuleSpec.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000014#include "lldb/Core/Timer.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000015#include "lldb/Symbol/ObjectFile.h"
16#include "lldb/Target/Target.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000017#include "lldb/Utility/DataBuffer.h"
18#include "lldb/Utility/DataExtractor.h"
Zachary Turner6f9e6902017-03-03 20:56:28 +000019#include "lldb/Utility/Log.h"
Robert Flack31870e12015-04-24 18:09:54 +000020#include "lldb/Utility/SafeMachO.h"
Zachary Turnerbf9a7732017-02-02 21:39:50 +000021#include "lldb/Utility/StreamString.h"
Zachary Turner666cc0b2017-03-04 01:30:05 +000022#include "lldb/Utility/UUID.h"
Eli Friedman5423ebf2010-07-02 19:28:44 +000023
Zachary Turner88c6b622015-03-03 18:34:26 +000024#include "llvm/Support/FileSystem.h"
25
Robert Flack31870e12015-04-24 18:09:54 +000026// From MacOSX system header "mach/machine.h"
27typedef int cpu_type_t;
28typedef int cpu_subtype_t;
29
Eli Friedman5423ebf2010-07-02 19:28:44 +000030using namespace lldb;
31using namespace lldb_private;
Robert Flack31870e12015-04-24 18:09:54 +000032using namespace llvm::MachO;
Eli Friedman5423ebf2010-07-02 19:28:44 +000033
Robert Flack31870e12015-04-24 18:09:54 +000034#if defined(__APPLE__)
35
36// Forward declaration of method defined in source/Host/macosx/Symbols.cpp
Kate Stoneb9c1b512016-09-06 20:57:50 +000037int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec,
38 ModuleSpec &return_module_spec);
Robert Flack31870e12015-04-24 18:09:54 +000039
40#else
41
Kate Stoneb9c1b512016-09-06 20:57:50 +000042int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec,
43 ModuleSpec &return_module_spec) {
44 // Cannot find MacOSX files using debug symbols on non MacOSX.
45 return 0;
Robert Flack31870e12015-04-24 18:09:54 +000046}
47
48#endif
49
Kate Stoneb9c1b512016-09-06 20:57:50 +000050static bool FileAtPathContainsArchAndUUID(const FileSpec &file_fspec,
51 const ArchSpec *arch,
52 const lldb_private::UUID *uuid) {
53 ModuleSpecList module_specs;
54 if (ObjectFile::GetModuleSpecifications(file_fspec, 0, 0, module_specs)) {
55 ModuleSpec spec;
56 for (size_t i = 0; i < module_specs.GetSize(); ++i) {
Jason Molendaa1a86462017-02-16 02:08:33 +000057 bool got_spec = module_specs.GetModuleSpecAtIndex(i, spec);
Bruce Mitcheneref4536c2017-03-23 09:52:26 +000058 UNUSED_IF_ASSERT_DISABLED(got_spec);
Jason Molendaa1a86462017-02-16 02:08:33 +000059 assert(got_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +000060 if ((uuid == NULL || (spec.GetUUIDPtr() && spec.GetUUID() == *uuid)) &&
61 (arch == NULL || (spec.GetArchitecturePtr() &&
62 spec.GetArchitecture().IsCompatibleMatch(*arch)))) {
63 return true;
64 }
65 }
66 }
67 return false;
68}
69
70static bool LocateDSYMInVincinityOfExecutable(const ModuleSpec &module_spec,
71 FileSpec &dsym_fspec) {
72 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
73 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
74 if (exec_fspec) {
75 char path[PATH_MAX];
76 if (exec_fspec->GetPath(path, sizeof(path))) {
77 // Make sure the module isn't already just a dSYM file...
78 if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL) {
79 if (log) {
80 if (module_spec.GetUUIDPtr() && module_spec.GetUUIDPtr()->IsValid()) {
81 log->Printf(
82 "Searching for dSYM bundle next to executable %s, UUID %s",
83 path, module_spec.GetUUIDPtr()->GetAsString().c_str());
84 } else {
85 log->Printf("Searching for dSYM bundle next to executable %s",
86 path);
87 }
88 }
89 size_t obj_file_path_length = strlen(path);
90 ::strncat(path, ".dSYM/Contents/Resources/DWARF/",
91 sizeof(path) - strlen(path) - 1);
92 ::strncat(path, exec_fspec->GetFilename().AsCString(),
93 sizeof(path) - strlen(path) - 1);
94
95 dsym_fspec.SetFile(path, false);
96
97 ModuleSpecList module_specs;
98 ModuleSpec matched_module_spec;
99 if (dsym_fspec.Exists() &&
100 FileAtPathContainsArchAndUUID(dsym_fspec,
101 module_spec.GetArchitecturePtr(),
102 module_spec.GetUUIDPtr())) {
103 if (log) {
104 log->Printf("dSYM with matching UUID & arch found at %s", path);
105 }
106 return true;
107 } else {
108 path[obj_file_path_length] = '\0';
109
110 char *last_dot = strrchr(path, '.');
111 while (last_dot != NULL && last_dot[0]) {
112 char *next_slash = strchr(last_dot, '/');
113 if (next_slash != NULL) {
114 *next_slash = '\0';
115 ::strncat(path, ".dSYM/Contents/Resources/DWARF/",
116 sizeof(path) - strlen(path) - 1);
117 ::strncat(path, exec_fspec->GetFilename().AsCString(),
118 sizeof(path) - strlen(path) - 1);
119 dsym_fspec.SetFile(path, false);
120 if (dsym_fspec.Exists() &&
121 FileAtPathContainsArchAndUUID(
122 dsym_fspec, module_spec.GetArchitecturePtr(),
123 module_spec.GetUUIDPtr())) {
124 if (log) {
125 log->Printf("dSYM with matching UUID & arch found at %s",
126 path);
127 }
Robert Flackab781642015-05-21 15:44:24 +0000128 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000129 } else {
130 *last_dot = '\0';
131 char *prev_slash = strrchr(path, '/');
132 if (prev_slash != NULL)
133 *prev_slash = '\0';
Robert Flack31870e12015-04-24 18:09:54 +0000134 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000135 break;
136 }
137 } else {
138 break;
Robert Flack31870e12015-04-24 18:09:54 +0000139 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000140 }
Robert Flack31870e12015-04-24 18:09:54 +0000141 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 }
Robert Flack31870e12015-04-24 18:09:54 +0000143 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 }
145 dsym_fspec.Clear();
146 return false;
Robert Flack31870e12015-04-24 18:09:54 +0000147}
148
Kate Stoneb9c1b512016-09-06 20:57:50 +0000149FileSpec LocateExecutableSymbolFileDsym(const ModuleSpec &module_spec) {
150 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
151 const ArchSpec *arch = module_spec.GetArchitecturePtr();
152 const UUID *uuid = module_spec.GetUUIDPtr();
Robert Flack31870e12015-04-24 18:09:54 +0000153
Kate Stoneb9c1b512016-09-06 20:57:50 +0000154 Timer scoped_timer(
155 LLVM_PRETTY_FUNCTION,
156 "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)",
157 exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>",
158 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
Robert Flack31870e12015-04-24 18:09:54 +0000159
Kate Stoneb9c1b512016-09-06 20:57:50 +0000160 FileSpec symbol_fspec;
161 ModuleSpec dsym_module_spec;
162 // First try and find the dSYM in the same directory as the executable or in
163 // an appropriate parent directory
164 if (LocateDSYMInVincinityOfExecutable(module_spec, symbol_fspec) == false) {
165 // We failed to easily find the dSYM above, so use DebugSymbols
166 LocateMacOSXFilesUsingDebugSymbols(module_spec, dsym_module_spec);
167 } else {
168 dsym_module_spec.GetSymbolFileSpec() = symbol_fspec;
169 }
170 return dsym_module_spec.GetSymbolFileSpec();
Robert Flack31870e12015-04-24 18:09:54 +0000171}
Michael Sartaina7499c92013-07-01 19:45:50 +0000172
Kate Stoneb9c1b512016-09-06 20:57:50 +0000173ModuleSpec Symbols::LocateExecutableObjectFile(const ModuleSpec &module_spec) {
174 ModuleSpec result;
175 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
176 const ArchSpec *arch = module_spec.GetArchitecturePtr();
177 const UUID *uuid = module_spec.GetUUIDPtr();
178 Timer scoped_timer(
179 LLVM_PRETTY_FUNCTION,
180 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
181 exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>",
182 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
Robert Flack31870e12015-04-24 18:09:54 +0000183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 ModuleSpecList module_specs;
185 ModuleSpec matched_module_spec;
186 if (exec_fspec &&
187 ObjectFile::GetModuleSpecifications(*exec_fspec, 0, 0, module_specs) &&
188 module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) {
189 result.GetFileSpec() = exec_fspec;
190 } else {
191 LocateMacOSXFilesUsingDebugSymbols(module_spec, result);
192 }
193 return result;
Michael Sartaina7499c92013-07-01 19:45:50 +0000194}
195
Kate Stoneb9c1b512016-09-06 20:57:50 +0000196FileSpec Symbols::LocateExecutableSymbolFile(const ModuleSpec &module_spec) {
197 FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec();
198 if (symbol_file_spec.IsAbsolute() && symbol_file_spec.Exists())
199 return symbol_file_spec;
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000200
Kate Stoneb9c1b512016-09-06 20:57:50 +0000201 const char *symbol_filename = symbol_file_spec.GetFilename().AsCString();
202 if (symbol_filename && symbol_filename[0]) {
203 FileSpecList debug_file_search_paths(
204 Target::GetDefaultDebugFileSearchPaths());
Michael Sartaina7499c92013-07-01 19:45:50 +0000205
Kate Stoneb9c1b512016-09-06 20:57:50 +0000206 // Add module directory.
207 const ConstString &file_dir = module_spec.GetFileSpec().GetDirectory();
208 debug_file_search_paths.AppendIfUnique(
209 FileSpec(file_dir.AsCString("."), true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000210
Kate Stoneb9c1b512016-09-06 20:57:50 +0000211 // Add current working directory.
212 debug_file_search_paths.AppendIfUnique(FileSpec(".", true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000213
Vadim Macagon30149912015-10-13 16:30:28 +0000214#ifndef LLVM_ON_WIN32
Kamil Rytarowski6420a2f2017-03-29 19:52:24 +0000215#if defined(__NetBSD__)
216 // Add /usr/libdata/debug directory.
217 debug_file_search_paths.AppendIfUnique(FileSpec("/usr/libdata/debug", true));
218#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000219 // Add /usr/lib/debug directory.
220 debug_file_search_paths.AppendIfUnique(FileSpec("/usr/lib/debug", true));
Kamil Rytarowski6420a2f2017-03-29 19:52:24 +0000221#endif
Vadim Macagon30149912015-10-13 16:30:28 +0000222#endif // LLVM_ON_WIN32
Michael Sartaina7499c92013-07-01 19:45:50 +0000223
Kate Stoneb9c1b512016-09-06 20:57:50 +0000224 std::string uuid_str;
225 const UUID &module_uuid = module_spec.GetUUID();
226 if (module_uuid.IsValid()) {
227 // Some debug files are stored in the .build-id directory like this:
228 // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
229 uuid_str = module_uuid.GetAsString("");
230 uuid_str.insert(2, 1, '/');
231 uuid_str = uuid_str + ".debug";
Michael Sartaina7499c92013-07-01 19:45:50 +0000232 }
233
Kate Stoneb9c1b512016-09-06 20:57:50 +0000234 size_t num_directories = debug_file_search_paths.GetSize();
235 for (size_t idx = 0; idx < num_directories; ++idx) {
236 FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex(idx);
237 dirspec.ResolvePath();
Zachary Turner7d86ee52017-03-08 17:56:08 +0000238 if (!llvm::sys::fs::is_directory(dirspec.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 continue;
240
241 std::vector<std::string> files;
242 std::string dirname = dirspec.GetPath();
243
244 files.push_back(dirname + "/" + symbol_filename);
245 files.push_back(dirname + "/.debug/" + symbol_filename);
246 files.push_back(dirname + "/.build-id/" + uuid_str);
247
248 // Some debug files may stored in the module directory like this:
249 // /usr/lib/debug/usr/lib/library.so.debug
250 if (!file_dir.IsEmpty())
251 files.push_back(dirname + file_dir.AsCString() + "/" + symbol_filename);
252
253 const uint32_t num_files = files.size();
254 for (size_t idx_file = 0; idx_file < num_files; ++idx_file) {
255 const std::string &filename = files[idx_file];
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000256 FileSpec file_spec(filename, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000257
258 if (llvm::sys::fs::equivalent(file_spec.GetPath(),
259 module_spec.GetFileSpec().GetPath()))
260 continue;
261
262 if (file_spec.Exists()) {
263 lldb_private::ModuleSpecList specs;
264 const size_t num_specs =
265 ObjectFile::GetModuleSpecifications(file_spec, 0, 0, specs);
266 assert(num_specs <= 1 &&
267 "Symbol Vendor supports only a single architecture");
268 if (num_specs == 1) {
269 ModuleSpec mspec;
270 if (specs.GetModuleSpecAtIndex(0, mspec)) {
271 if (mspec.GetUUID() == module_uuid)
272 return file_spec;
273 }
274 }
275 }
276 }
277 }
278 }
279
280 return LocateExecutableSymbolFileDsym(module_spec);
Michael Sartaina7499c92013-07-01 19:45:50 +0000281}
282
Kate Stoneb9c1b512016-09-06 20:57:50 +0000283#if !defined(__APPLE__)
Robert Flack31870e12015-04-24 18:09:54 +0000284
Kate Stoneb9c1b512016-09-06 20:57:50 +0000285FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle,
286 const lldb_private::UUID *uuid,
287 const ArchSpec *arch) {
288 // FIXME
289 return FileSpec();
Michael Sartaina7499c92013-07-01 19:45:50 +0000290}
291
Kate Stoneb9c1b512016-09-06 20:57:50 +0000292bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
293 bool force_lookup) {
294 // Fill in the module_spec.GetFileSpec() for the object file and/or the
295 // module_spec.GetSymbolFileSpec() for the debug symbols file.
296 return false;
Michael Sartaina7499c92013-07-01 19:45:50 +0000297}
298
Greg Clayton2bddd342010-09-07 20:11:56 +0000299#endif