blob: de5863bd2f6d70c23503f1d37d1101f9d1e0c824 [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 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000089 ::strncat(path, ".dSYM/Contents/Resources/DWARF/",
90 sizeof(path) - strlen(path) - 1);
91 ::strncat(path, exec_fspec->GetFilename().AsCString(),
92 sizeof(path) - strlen(path) - 1);
93
94 dsym_fspec.SetFile(path, false);
95
96 ModuleSpecList module_specs;
97 ModuleSpec matched_module_spec;
98 if (dsym_fspec.Exists() &&
99 FileAtPathContainsArchAndUUID(dsym_fspec,
100 module_spec.GetArchitecturePtr(),
101 module_spec.GetUUIDPtr())) {
102 if (log) {
103 log->Printf("dSYM with matching UUID & arch found at %s", path);
104 }
105 return true;
106 } else {
Jason Molenda244b6bb2017-06-15 01:42:48 +0000107 // Get a clean copy of the executable path, without the final filename
108 FileSpec path_dir_fspec (exec_fspec->GetDirectory().AsCString(), true);
109 std::string path_dir_str = path_dir_fspec.GetPath();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000110
Jason Molenda244b6bb2017-06-15 01:42:48 +0000111 // Add a ".dSYM" name to each directory component of the path, stripping
112 // off components. e.g. we may have a binary like
113 // /S/L/F/Foundation.framework/Versions/A/Foundation
114 // and
115 // /S/L/F/Foundation.framework.dSYM
116 //
117 // so we'll need to start with /S/L/F/Foundation.framework/Versions/A,
118 // add the .dSYM part to the "A", and if that doesn't exist, strip off
119 // the "A" and try it again with "Versions", etc., until we find a dSYM
120 // bundle or we've stripped off enough path components that there's no
121 // need to continue.
122
123 for (int i = 0; i < 4; i++) {
124 std::string path_dir_plus_dsym (path_dir_str);
125 path_dir_plus_dsym += ".dSYM/Contents/Resources/DWARF/";
126 path_dir_plus_dsym += exec_fspec->GetFilename().AsCString();
127 dsym_fspec.SetFile (path_dir_plus_dsym, true);
128 if (dsym_fspec.Exists() &&
129 FileAtPathContainsArchAndUUID(
130 dsym_fspec, module_spec.GetArchitecturePtr(),
131 module_spec.GetUUIDPtr())) {
132 if (log) {
133 log->Printf("dSYM with matching UUID & arch found at %s",
134 path_dir_plus_dsym.c_str());
135 }
136 return true;
Robert Flack31870e12015-04-24 18:09:54 +0000137 }
Jason Molenda244b6bb2017-06-15 01:42:48 +0000138 auto const last_slash = path_dir_str.rfind('/');
139 if (last_slash == std::string::npos)
140 break;
141 path_dir_str.resize(last_slash);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000142 }
Robert Flack31870e12015-04-24 18:09:54 +0000143 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000144 }
Robert Flack31870e12015-04-24 18:09:54 +0000145 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000146 }
147 dsym_fspec.Clear();
148 return false;
Robert Flack31870e12015-04-24 18:09:54 +0000149}
150
Kate Stoneb9c1b512016-09-06 20:57:50 +0000151FileSpec LocateExecutableSymbolFileDsym(const ModuleSpec &module_spec) {
152 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
153 const ArchSpec *arch = module_spec.GetArchitecturePtr();
154 const UUID *uuid = module_spec.GetUUIDPtr();
Robert Flack31870e12015-04-24 18:09:54 +0000155
Pavel Labathf9d16472017-05-15 13:02:37 +0000156 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000157 Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +0000158 func_cat,
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)",
160 exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>",
161 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
Robert Flack31870e12015-04-24 18:09:54 +0000162
Kate Stoneb9c1b512016-09-06 20:57:50 +0000163 FileSpec symbol_fspec;
164 ModuleSpec dsym_module_spec;
165 // First try and find the dSYM in the same directory as the executable or in
166 // an appropriate parent directory
167 if (LocateDSYMInVincinityOfExecutable(module_spec, symbol_fspec) == false) {
168 // We failed to easily find the dSYM above, so use DebugSymbols
169 LocateMacOSXFilesUsingDebugSymbols(module_spec, dsym_module_spec);
170 } else {
171 dsym_module_spec.GetSymbolFileSpec() = symbol_fspec;
172 }
173 return dsym_module_spec.GetSymbolFileSpec();
Robert Flack31870e12015-04-24 18:09:54 +0000174}
Michael Sartaina7499c92013-07-01 19:45:50 +0000175
Kate Stoneb9c1b512016-09-06 20:57:50 +0000176ModuleSpec Symbols::LocateExecutableObjectFile(const ModuleSpec &module_spec) {
177 ModuleSpec result;
178 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
179 const ArchSpec *arch = module_spec.GetArchitecturePtr();
180 const UUID *uuid = module_spec.GetUUIDPtr();
Pavel Labathf9d16472017-05-15 13:02:37 +0000181 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000182 Timer scoped_timer(
Pavel Labathf9d16472017-05-15 13:02:37 +0000183 func_cat, "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184 exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>",
185 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
Robert Flack31870e12015-04-24 18:09:54 +0000186
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187 ModuleSpecList module_specs;
188 ModuleSpec matched_module_spec;
189 if (exec_fspec &&
190 ObjectFile::GetModuleSpecifications(*exec_fspec, 0, 0, module_specs) &&
191 module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) {
192 result.GetFileSpec() = exec_fspec;
193 } else {
194 LocateMacOSXFilesUsingDebugSymbols(module_spec, result);
195 }
196 return result;
Michael Sartaina7499c92013-07-01 19:45:50 +0000197}
198
Kate Stoneb9c1b512016-09-06 20:57:50 +0000199FileSpec Symbols::LocateExecutableSymbolFile(const ModuleSpec &module_spec) {
200 FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec();
201 if (symbol_file_spec.IsAbsolute() && symbol_file_spec.Exists())
202 return symbol_file_spec;
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000203
Kate Stoneb9c1b512016-09-06 20:57:50 +0000204 const char *symbol_filename = symbol_file_spec.GetFilename().AsCString();
205 if (symbol_filename && symbol_filename[0]) {
206 FileSpecList debug_file_search_paths(
207 Target::GetDefaultDebugFileSearchPaths());
Michael Sartaina7499c92013-07-01 19:45:50 +0000208
Kate Stoneb9c1b512016-09-06 20:57:50 +0000209 // Add module directory.
210 const ConstString &file_dir = module_spec.GetFileSpec().GetDirectory();
211 debug_file_search_paths.AppendIfUnique(
212 FileSpec(file_dir.AsCString("."), true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000213
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 // Add current working directory.
215 debug_file_search_paths.AppendIfUnique(FileSpec(".", true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000216
Vadim Macagon30149912015-10-13 16:30:28 +0000217#ifndef LLVM_ON_WIN32
Kamil Rytarowski6420a2f2017-03-29 19:52:24 +0000218#if defined(__NetBSD__)
219 // Add /usr/libdata/debug directory.
220 debug_file_search_paths.AppendIfUnique(FileSpec("/usr/libdata/debug", true));
221#else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000222 // Add /usr/lib/debug directory.
223 debug_file_search_paths.AppendIfUnique(FileSpec("/usr/lib/debug", true));
Kamil Rytarowski6420a2f2017-03-29 19:52:24 +0000224#endif
Vadim Macagon30149912015-10-13 16:30:28 +0000225#endif // LLVM_ON_WIN32
Michael Sartaina7499c92013-07-01 19:45:50 +0000226
Kate Stoneb9c1b512016-09-06 20:57:50 +0000227 std::string uuid_str;
228 const UUID &module_uuid = module_spec.GetUUID();
229 if (module_uuid.IsValid()) {
230 // Some debug files are stored in the .build-id directory like this:
231 // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
232 uuid_str = module_uuid.GetAsString("");
233 uuid_str.insert(2, 1, '/');
234 uuid_str = uuid_str + ".debug";
Michael Sartaina7499c92013-07-01 19:45:50 +0000235 }
236
Kate Stoneb9c1b512016-09-06 20:57:50 +0000237 size_t num_directories = debug_file_search_paths.GetSize();
238 for (size_t idx = 0; idx < num_directories; ++idx) {
239 FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex(idx);
240 dirspec.ResolvePath();
Zachary Turner7d86ee52017-03-08 17:56:08 +0000241 if (!llvm::sys::fs::is_directory(dirspec.GetPath()))
Kate Stoneb9c1b512016-09-06 20:57:50 +0000242 continue;
243
244 std::vector<std::string> files;
245 std::string dirname = dirspec.GetPath();
246
247 files.push_back(dirname + "/" + symbol_filename);
248 files.push_back(dirname + "/.debug/" + symbol_filename);
249 files.push_back(dirname + "/.build-id/" + uuid_str);
250
251 // Some debug files may stored in the module directory like this:
252 // /usr/lib/debug/usr/lib/library.so.debug
253 if (!file_dir.IsEmpty())
254 files.push_back(dirname + file_dir.AsCString() + "/" + symbol_filename);
255
256 const uint32_t num_files = files.size();
257 for (size_t idx_file = 0; idx_file < num_files; ++idx_file) {
258 const std::string &filename = files[idx_file];
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000259 FileSpec file_spec(filename, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000260
261 if (llvm::sys::fs::equivalent(file_spec.GetPath(),
262 module_spec.GetFileSpec().GetPath()))
263 continue;
264
265 if (file_spec.Exists()) {
266 lldb_private::ModuleSpecList specs;
267 const size_t num_specs =
268 ObjectFile::GetModuleSpecifications(file_spec, 0, 0, specs);
269 assert(num_specs <= 1 &&
270 "Symbol Vendor supports only a single architecture");
271 if (num_specs == 1) {
272 ModuleSpec mspec;
273 if (specs.GetModuleSpecAtIndex(0, mspec)) {
274 if (mspec.GetUUID() == module_uuid)
275 return file_spec;
276 }
277 }
278 }
279 }
280 }
281 }
282
283 return LocateExecutableSymbolFileDsym(module_spec);
Michael Sartaina7499c92013-07-01 19:45:50 +0000284}
285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286#if !defined(__APPLE__)
Robert Flack31870e12015-04-24 18:09:54 +0000287
Kate Stoneb9c1b512016-09-06 20:57:50 +0000288FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle,
289 const lldb_private::UUID *uuid,
290 const ArchSpec *arch) {
291 // FIXME
292 return FileSpec();
Michael Sartaina7499c92013-07-01 19:45:50 +0000293}
294
Kate Stoneb9c1b512016-09-06 20:57:50 +0000295bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
296 bool force_lookup) {
297 // Fill in the module_spec.GetFileSpec() for the object file and/or the
298 // module_spec.GetSymbolFileSpec() for the debug symbols file.
299 return false;
Michael Sartaina7499c92013-07-01 19:45:50 +0000300}
301
Greg Clayton2bddd342010-09-07 20:11:56 +0000302#endif