blob: 9850625cbc43fb7e63ac7b09a12b82181f81c656 [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);
58 assert(got_spec);
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 if ((uuid == NULL || (spec.GetUUIDPtr() && spec.GetUUID() == *uuid)) &&
60 (arch == NULL || (spec.GetArchitecturePtr() &&
61 spec.GetArchitecture().IsCompatibleMatch(*arch)))) {
62 return true;
63 }
64 }
65 }
66 return false;
67}
68
69static bool LocateDSYMInVincinityOfExecutable(const ModuleSpec &module_spec,
70 FileSpec &dsym_fspec) {
71 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
72 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
73 if (exec_fspec) {
74 char path[PATH_MAX];
75 if (exec_fspec->GetPath(path, sizeof(path))) {
76 // Make sure the module isn't already just a dSYM file...
77 if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL) {
78 if (log) {
79 if (module_spec.GetUUIDPtr() && module_spec.GetUUIDPtr()->IsValid()) {
80 log->Printf(
81 "Searching for dSYM bundle next to executable %s, UUID %s",
82 path, module_spec.GetUUIDPtr()->GetAsString().c_str());
83 } else {
84 log->Printf("Searching for dSYM bundle next to executable %s",
85 path);
86 }
87 }
88 size_t obj_file_path_length = strlen(path);
89 ::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 {
107 path[obj_file_path_length] = '\0';
108
109 char *last_dot = strrchr(path, '.');
110 while (last_dot != NULL && last_dot[0]) {
111 char *next_slash = strchr(last_dot, '/');
112 if (next_slash != NULL) {
113 *next_slash = '\0';
114 ::strncat(path, ".dSYM/Contents/Resources/DWARF/",
115 sizeof(path) - strlen(path) - 1);
116 ::strncat(path, exec_fspec->GetFilename().AsCString(),
117 sizeof(path) - strlen(path) - 1);
118 dsym_fspec.SetFile(path, false);
119 if (dsym_fspec.Exists() &&
120 FileAtPathContainsArchAndUUID(
121 dsym_fspec, module_spec.GetArchitecturePtr(),
122 module_spec.GetUUIDPtr())) {
123 if (log) {
124 log->Printf("dSYM with matching UUID & arch found at %s",
125 path);
126 }
Robert Flackab781642015-05-21 15:44:24 +0000127 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000128 } else {
129 *last_dot = '\0';
130 char *prev_slash = strrchr(path, '/');
131 if (prev_slash != NULL)
132 *prev_slash = '\0';
Robert Flack31870e12015-04-24 18:09:54 +0000133 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000134 break;
135 }
136 } else {
137 break;
Robert Flack31870e12015-04-24 18:09:54 +0000138 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 }
Robert Flack31870e12015-04-24 18:09:54 +0000140 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000141 }
Robert Flack31870e12015-04-24 18:09:54 +0000142 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143 }
144 dsym_fspec.Clear();
145 return false;
Robert Flack31870e12015-04-24 18:09:54 +0000146}
147
Kate Stoneb9c1b512016-09-06 20:57:50 +0000148FileSpec LocateExecutableSymbolFileDsym(const ModuleSpec &module_spec) {
149 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
150 const ArchSpec *arch = module_spec.GetArchitecturePtr();
151 const UUID *uuid = module_spec.GetUUIDPtr();
Robert Flack31870e12015-04-24 18:09:54 +0000152
Kate Stoneb9c1b512016-09-06 20:57:50 +0000153 Timer scoped_timer(
154 LLVM_PRETTY_FUNCTION,
155 "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)",
156 exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>",
157 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
Robert Flack31870e12015-04-24 18:09:54 +0000158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 FileSpec symbol_fspec;
160 ModuleSpec dsym_module_spec;
161 // First try and find the dSYM in the same directory as the executable or in
162 // an appropriate parent directory
163 if (LocateDSYMInVincinityOfExecutable(module_spec, symbol_fspec) == false) {
164 // We failed to easily find the dSYM above, so use DebugSymbols
165 LocateMacOSXFilesUsingDebugSymbols(module_spec, dsym_module_spec);
166 } else {
167 dsym_module_spec.GetSymbolFileSpec() = symbol_fspec;
168 }
169 return dsym_module_spec.GetSymbolFileSpec();
Robert Flack31870e12015-04-24 18:09:54 +0000170}
Michael Sartaina7499c92013-07-01 19:45:50 +0000171
Kate Stoneb9c1b512016-09-06 20:57:50 +0000172ModuleSpec Symbols::LocateExecutableObjectFile(const ModuleSpec &module_spec) {
173 ModuleSpec result;
174 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
175 const ArchSpec *arch = module_spec.GetArchitecturePtr();
176 const UUID *uuid = module_spec.GetUUIDPtr();
177 Timer scoped_timer(
178 LLVM_PRETTY_FUNCTION,
179 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
180 exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>",
181 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid);
Robert Flack31870e12015-04-24 18:09:54 +0000182
Kate Stoneb9c1b512016-09-06 20:57:50 +0000183 ModuleSpecList module_specs;
184 ModuleSpec matched_module_spec;
185 if (exec_fspec &&
186 ObjectFile::GetModuleSpecifications(*exec_fspec, 0, 0, module_specs) &&
187 module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) {
188 result.GetFileSpec() = exec_fspec;
189 } else {
190 LocateMacOSXFilesUsingDebugSymbols(module_spec, result);
191 }
192 return result;
Michael Sartaina7499c92013-07-01 19:45:50 +0000193}
194
Kate Stoneb9c1b512016-09-06 20:57:50 +0000195FileSpec Symbols::LocateExecutableSymbolFile(const ModuleSpec &module_spec) {
196 FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec();
197 if (symbol_file_spec.IsAbsolute() && symbol_file_spec.Exists())
198 return symbol_file_spec;
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000199
Kate Stoneb9c1b512016-09-06 20:57:50 +0000200 const char *symbol_filename = symbol_file_spec.GetFilename().AsCString();
201 if (symbol_filename && symbol_filename[0]) {
202 FileSpecList debug_file_search_paths(
203 Target::GetDefaultDebugFileSearchPaths());
Michael Sartaina7499c92013-07-01 19:45:50 +0000204
Kate Stoneb9c1b512016-09-06 20:57:50 +0000205 // Add module directory.
206 const ConstString &file_dir = module_spec.GetFileSpec().GetDirectory();
207 debug_file_search_paths.AppendIfUnique(
208 FileSpec(file_dir.AsCString("."), true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000209
Kate Stoneb9c1b512016-09-06 20:57:50 +0000210 // Add current working directory.
211 debug_file_search_paths.AppendIfUnique(FileSpec(".", true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000212
Vadim Macagon30149912015-10-13 16:30:28 +0000213#ifndef LLVM_ON_WIN32
Kate Stoneb9c1b512016-09-06 20:57:50 +0000214 // Add /usr/lib/debug directory.
215 debug_file_search_paths.AppendIfUnique(FileSpec("/usr/lib/debug", true));
Vadim Macagon30149912015-10-13 16:30:28 +0000216#endif // LLVM_ON_WIN32
Michael Sartaina7499c92013-07-01 19:45:50 +0000217
Kate Stoneb9c1b512016-09-06 20:57:50 +0000218 std::string uuid_str;
219 const UUID &module_uuid = module_spec.GetUUID();
220 if (module_uuid.IsValid()) {
221 // Some debug files are stored in the .build-id directory like this:
222 // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
223 uuid_str = module_uuid.GetAsString("");
224 uuid_str.insert(2, 1, '/');
225 uuid_str = uuid_str + ".debug";
Michael Sartaina7499c92013-07-01 19:45:50 +0000226 }
227
Kate Stoneb9c1b512016-09-06 20:57:50 +0000228 size_t num_directories = debug_file_search_paths.GetSize();
229 for (size_t idx = 0; idx < num_directories; ++idx) {
230 FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex(idx);
231 dirspec.ResolvePath();
232 if (!dirspec.Exists() || !dirspec.IsDirectory())
233 continue;
234
235 std::vector<std::string> files;
236 std::string dirname = dirspec.GetPath();
237
238 files.push_back(dirname + "/" + symbol_filename);
239 files.push_back(dirname + "/.debug/" + symbol_filename);
240 files.push_back(dirname + "/.build-id/" + uuid_str);
241
242 // Some debug files may stored in the module directory like this:
243 // /usr/lib/debug/usr/lib/library.so.debug
244 if (!file_dir.IsEmpty())
245 files.push_back(dirname + file_dir.AsCString() + "/" + symbol_filename);
246
247 const uint32_t num_files = files.size();
248 for (size_t idx_file = 0; idx_file < num_files; ++idx_file) {
249 const std::string &filename = files[idx_file];
Malcolm Parsons771ef6d2016-11-02 20:34:10 +0000250 FileSpec file_spec(filename, true);
Kate Stoneb9c1b512016-09-06 20:57:50 +0000251
252 if (llvm::sys::fs::equivalent(file_spec.GetPath(),
253 module_spec.GetFileSpec().GetPath()))
254 continue;
255
256 if (file_spec.Exists()) {
257 lldb_private::ModuleSpecList specs;
258 const size_t num_specs =
259 ObjectFile::GetModuleSpecifications(file_spec, 0, 0, specs);
260 assert(num_specs <= 1 &&
261 "Symbol Vendor supports only a single architecture");
262 if (num_specs == 1) {
263 ModuleSpec mspec;
264 if (specs.GetModuleSpecAtIndex(0, mspec)) {
265 if (mspec.GetUUID() == module_uuid)
266 return file_spec;
267 }
268 }
269 }
270 }
271 }
272 }
273
274 return LocateExecutableSymbolFileDsym(module_spec);
Michael Sartaina7499c92013-07-01 19:45:50 +0000275}
276
Kate Stoneb9c1b512016-09-06 20:57:50 +0000277#if !defined(__APPLE__)
Robert Flack31870e12015-04-24 18:09:54 +0000278
Kate Stoneb9c1b512016-09-06 20:57:50 +0000279FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle,
280 const lldb_private::UUID *uuid,
281 const ArchSpec *arch) {
282 // FIXME
283 return FileSpec();
Michael Sartaina7499c92013-07-01 19:45:50 +0000284}
285
Kate Stoneb9c1b512016-09-06 20:57:50 +0000286bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec,
287 bool force_lookup) {
288 // Fill in the module_spec.GetFileSpec() for the object file and/or the
289 // module_spec.GetSymbolFileSpec() for the debug symbols file.
290 return false;
Michael Sartaina7499c92013-07-01 19:45:50 +0000291}
292
Greg Clayton2bddd342010-09-07 20:11:56 +0000293#endif