blob: f24423da6f9b4b38501f716bc7dc23a271b9ba22 [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"
12#include "lldb/Core/DataBuffer.h"
13#include "lldb/Core/DataExtractor.h"
Jason Molenda08a32582015-07-25 02:39:42 +000014#include "lldb/Core/Log.h"
Michael Sartaina7499c92013-07-01 19:45:50 +000015#include "lldb/Core/Module.h"
16#include "lldb/Core/ModuleSpec.h"
17#include "lldb/Core/StreamString.h"
18#include "lldb/Core/Timer.h"
19#include "lldb/Core/UUID.h"
20#include "lldb/Symbol/ObjectFile.h"
21#include "lldb/Target/Target.h"
Robert Flack31870e12015-04-24 18:09:54 +000022#include "lldb/Utility/SafeMachO.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
37int
38LocateMacOSXFilesUsingDebugSymbols
39(
40 const ModuleSpec &module_spec,
41 FileSpec *out_exec_fspec, // If non-NULL, try and find the executable
42 FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file
43);
44
45#else
46
47int
48LocateMacOSXFilesUsingDebugSymbols
49(
50 const ModuleSpec &module_spec,
51 FileSpec *out_exec_fspec, // If non-NULL, try and find the executable
52 FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file
53) {
54 // Cannot find MacOSX files using debug symbols on non MacOSX.
55 return 0;
56}
57
58#endif
59
60static bool
Robert Flackab781642015-05-21 15:44:24 +000061FileAtPathContainsArchAndUUID (const FileSpec &file_fspec, const ArchSpec *arch, const lldb_private::UUID *uuid)
62{
63 ModuleSpecList module_specs;
64 if (ObjectFile::GetModuleSpecifications(file_fspec, 0, 0, module_specs))
65 {
66 ModuleSpec spec;
67 for (size_t i = 0; i < module_specs.GetSize(); ++i)
68 {
69 assert(module_specs.GetModuleSpecAtIndex(i, spec));
70 if ((uuid == NULL || (spec.GetUUIDPtr() && spec.GetUUID() == *uuid)) &&
71 (arch == NULL || (spec.GetArchitecturePtr() && spec.GetArchitecture().IsCompatibleMatch(*arch))))
72 {
73 return true;
74 }
75 }
76 }
77 return false;
78}
79
80static bool
Robert Flack31870e12015-04-24 18:09:54 +000081LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec)
82{
Jason Molenda08a32582015-07-25 02:39:42 +000083 Log *log = lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST);
Robert Flack31870e12015-04-24 18:09:54 +000084 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
85 if (exec_fspec)
86 {
87 char path[PATH_MAX];
88 if (exec_fspec->GetPath(path, sizeof(path)))
89 {
90 // Make sure the module isn't already just a dSYM file...
91 if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL)
92 {
Jason Molenda08a32582015-07-25 02:39:42 +000093 if (log)
94 {
95 if (module_spec.GetUUIDPtr() && module_spec.GetUUIDPtr()->IsValid())
96 {
97 log->Printf ("Searching for dSYM bundle next to executable %s, UUID %s", path, module_spec.GetUUIDPtr()->GetAsString().c_str());
98 }
99 else
100 {
101 log->Printf ("Searching for dSYM bundle next to executable %s", path);
102 }
103 }
Robert Flack31870e12015-04-24 18:09:54 +0000104 size_t obj_file_path_length = strlen(path);
105 ::strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path) - strlen(path) - 1);
106 ::strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path) - strlen(path) - 1);
107
108 dsym_fspec.SetFile(path, false);
109
110 ModuleSpecList module_specs;
111 ModuleSpec matched_module_spec;
112 if (dsym_fspec.Exists() &&
Robert Flackab781642015-05-21 15:44:24 +0000113 FileAtPathContainsArchAndUUID(dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Robert Flack31870e12015-04-24 18:09:54 +0000114 {
Jason Molenda08a32582015-07-25 02:39:42 +0000115 if (log)
116 {
117 log->Printf ("dSYM with matching UUID & arch found at %s", path);
118 }
Robert Flack31870e12015-04-24 18:09:54 +0000119 return true;
120 }
121 else
122 {
123 path[obj_file_path_length] = '\0';
124
125 char *last_dot = strrchr(path, '.');
126 while (last_dot != NULL && last_dot[0])
127 {
128 char *next_slash = strchr(last_dot, '/');
129 if (next_slash != NULL)
130 {
131 *next_slash = '\0';
132 ::strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path) - strlen(path) - 1);
133 ::strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path) - strlen(path) - 1);
134 dsym_fspec.SetFile(path, false);
135 if (dsym_fspec.Exists() &&
Robert Flackab781642015-05-21 15:44:24 +0000136 FileAtPathContainsArchAndUUID(dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr()))
Robert Flack31870e12015-04-24 18:09:54 +0000137 {
Jason Molenda08a32582015-07-25 02:39:42 +0000138 if (log)
139 {
140 log->Printf ("dSYM with matching UUID & arch found at %s", path);
141 }
Robert Flack31870e12015-04-24 18:09:54 +0000142 return true;
143 }
144 else
145 {
146 *last_dot = '\0';
147 char *prev_slash = strrchr(path, '/');
148 if (prev_slash != NULL)
149 *prev_slash = '\0';
150 else
151 break;
152 }
153 }
154 else
155 {
156 break;
157 }
158 }
159 }
160 }
161 }
162 }
163 dsym_fspec.Clear();
164 return false;
165}
166
167FileSpec
168LocateExecutableSymbolFileDsym (const ModuleSpec &module_spec)
169{
170 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
171 const ArchSpec *arch = module_spec.GetArchitecturePtr();
172 const UUID *uuid = module_spec.GetUUIDPtr();
173
174 Timer scoped_timer (__PRETTY_FUNCTION__,
175 "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)",
176 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
177 arch ? arch->GetArchitectureName() : "<NULL>",
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000178 (void*)uuid);
Robert Flack31870e12015-04-24 18:09:54 +0000179
180 FileSpec symbol_fspec;
181 // First try and find the dSYM in the same directory as the executable or in
182 // an appropriate parent directory
183 if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false)
184 {
185 // We failed to easily find the dSYM above, so use DebugSymbols
186 LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec);
187 }
188 return symbol_fspec;
189}
Michael Sartaina7499c92013-07-01 19:45:50 +0000190
191FileSpec
192Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
193{
Robert Flack31870e12015-04-24 18:09:54 +0000194 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
195 const ArchSpec *arch = module_spec.GetArchitecturePtr();
196 const UUID *uuid = module_spec.GetUUIDPtr();
197 Timer scoped_timer (__PRETTY_FUNCTION__,
198 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
199 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
200 arch ? arch->GetArchitectureName() : "<NULL>",
Vince Harrond7e6a4f2015-05-13 00:25:54 +0000201 (void*)uuid);
Robert Flack31870e12015-04-24 18:09:54 +0000202
203 FileSpec objfile_fspec;
204 ModuleSpecList module_specs;
205 ModuleSpec matched_module_spec;
206 if (exec_fspec &&
207 ObjectFile::GetModuleSpecifications(*exec_fspec, 0, 0, module_specs) &&
208 module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
209 {
210 objfile_fspec = exec_fspec;
211 }
212 else
213 {
214 LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL);
215 }
216 return objfile_fspec;
Michael Sartaina7499c92013-07-01 19:45:50 +0000217}
218
219FileSpec
220Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
221{
222 const char *symbol_filename = module_spec.GetSymbolFileSpec().GetFilename().AsCString();
Robert Flack31870e12015-04-24 18:09:54 +0000223 if (symbol_filename && symbol_filename[0])
Michael Sartaina7499c92013-07-01 19:45:50 +0000224 {
Robert Flack31870e12015-04-24 18:09:54 +0000225 FileSpecList debug_file_search_paths (Target::GetDefaultDebugFileSearchPaths());
Michael Sartaina7499c92013-07-01 19:45:50 +0000226
Robert Flack31870e12015-04-24 18:09:54 +0000227 // Add module directory.
228 const ConstString &file_dir = module_spec.GetFileSpec().GetDirectory();
229 debug_file_search_paths.AppendIfUnique (FileSpec(file_dir.AsCString("."), true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000230
Robert Flack31870e12015-04-24 18:09:54 +0000231 // Add current working directory.
232 debug_file_search_paths.AppendIfUnique (FileSpec(".", true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000233
Robert Flack31870e12015-04-24 18:09:54 +0000234 // Add /usr/lib/debug directory.
235 debug_file_search_paths.AppendIfUnique (FileSpec("/usr/lib/debug", true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000236
Robert Flack31870e12015-04-24 18:09:54 +0000237 std::string uuid_str;
238 const UUID &module_uuid = module_spec.GetUUID();
239 if (module_uuid.IsValid())
Michael Sartaina7499c92013-07-01 19:45:50 +0000240 {
Robert Flack31870e12015-04-24 18:09:54 +0000241 // Some debug files are stored in the .build-id directory like this:
242 // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
243 uuid_str = module_uuid.GetAsString("");
244 uuid_str.insert (2, 1, '/');
245 uuid_str = uuid_str + ".debug";
246 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000247
Robert Flack31870e12015-04-24 18:09:54 +0000248 // Get directory of our module. Needed to check debug files like this:
249 // /usr/lib/debug/usr/lib/library.so.debug
250 std::string module_directory = module_spec.GetFileSpec().GetDirectory().AsCString();
251
252 size_t num_directories = debug_file_search_paths.GetSize();
253 for (size_t idx = 0; idx < num_directories; ++idx)
254 {
255 FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex (idx);
256 dirspec.ResolvePath();
257 if (!dirspec.Exists() || !dirspec.IsDirectory())
Michael Sartaina7499c92013-07-01 19:45:50 +0000258 continue;
259
Robert Flack31870e12015-04-24 18:09:54 +0000260 std::vector<std::string> files;
261 std::string dirname = dirspec.GetPath();
262
263 files.push_back (dirname + "/" + symbol_filename);
264 files.push_back (dirname + "/.debug/" + symbol_filename);
265 files.push_back (dirname + "/.build-id/" + uuid_str);
266 files.push_back (dirname + module_directory + "/" + symbol_filename);
267
268 const uint32_t num_files = files.size();
269 for (size_t idx_file = 0; idx_file < num_files; ++idx_file)
Michael Sartaina7499c92013-07-01 19:45:50 +0000270 {
Robert Flack31870e12015-04-24 18:09:54 +0000271 const std::string &filename = files[idx_file];
272 FileSpec file_spec (filename.c_str(), true);
273
274 if (llvm::sys::fs::equivalent (file_spec.GetPath(), module_spec.GetFileSpec().GetPath()))
275 continue;
276
277 if (file_spec.Exists())
Michael Sartaina7499c92013-07-01 19:45:50 +0000278 {
Robert Flack31870e12015-04-24 18:09:54 +0000279 lldb_private::ModuleSpecList specs;
280 const size_t num_specs = ObjectFile::GetModuleSpecifications (file_spec, 0, 0, specs);
281 assert (num_specs <= 1 && "Symbol Vendor supports only a single architecture");
282 if (num_specs == 1)
Michael Sartaina7499c92013-07-01 19:45:50 +0000283 {
Robert Flack31870e12015-04-24 18:09:54 +0000284 ModuleSpec mspec;
285 if (specs.GetModuleSpecAtIndex (0, mspec))
286 {
287 if (mspec.GetUUID() == module_uuid)
288 return file_spec;
289 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000290 }
291 }
292 }
293 }
294 }
295
Robert Flack31870e12015-04-24 18:09:54 +0000296 return LocateExecutableSymbolFileDsym(module_spec);
Michael Sartaina7499c92013-07-01 19:45:50 +0000297}
298
Robert Flack31870e12015-04-24 18:09:54 +0000299#if !defined (__APPLE__)
300
Michael Sartaina7499c92013-07-01 19:45:50 +0000301FileSpec
302Symbols::FindSymbolFileInBundle (const FileSpec& symfile_bundle,
303 const lldb_private::UUID *uuid,
304 const ArchSpec *arch)
305{
306 // FIXME
307 return FileSpec();
308}
309
310bool
311Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup)
312{
313 // Fill in the module_spec.GetFileSpec() for the object file and/or the
314 // module_spec.GetSymbolFileSpec() for the debug symbols file.
315 return false;
316}
317
Greg Clayton2bddd342010-09-07 20:11:56 +0000318#endif