blob: 76e28449f96107107a192294e10751563ed4a8c2 [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>",
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000178 (const 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>",
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000201 (const 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{
Tamas Berghammerd00438e2015-07-30 12:38:18 +0000222 FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec();
223 if (symbol_file_spec.IsAbsolute() && symbol_file_spec.Exists())
224 return symbol_file_spec;
225
226 const char *symbol_filename = symbol_file_spec.GetFilename().AsCString();
Robert Flack31870e12015-04-24 18:09:54 +0000227 if (symbol_filename && symbol_filename[0])
Michael Sartaina7499c92013-07-01 19:45:50 +0000228 {
Robert Flack31870e12015-04-24 18:09:54 +0000229 FileSpecList debug_file_search_paths (Target::GetDefaultDebugFileSearchPaths());
Michael Sartaina7499c92013-07-01 19:45:50 +0000230
Robert Flack31870e12015-04-24 18:09:54 +0000231 // Add module directory.
232 const ConstString &file_dir = module_spec.GetFileSpec().GetDirectory();
233 debug_file_search_paths.AppendIfUnique (FileSpec(file_dir.AsCString("."), true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000234
Robert Flack31870e12015-04-24 18:09:54 +0000235 // Add current working directory.
236 debug_file_search_paths.AppendIfUnique (FileSpec(".", true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000237
Robert Flack31870e12015-04-24 18:09:54 +0000238 // Add /usr/lib/debug directory.
239 debug_file_search_paths.AppendIfUnique (FileSpec("/usr/lib/debug", true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000240
Robert Flack31870e12015-04-24 18:09:54 +0000241 std::string uuid_str;
242 const UUID &module_uuid = module_spec.GetUUID();
243 if (module_uuid.IsValid())
Michael Sartaina7499c92013-07-01 19:45:50 +0000244 {
Robert Flack31870e12015-04-24 18:09:54 +0000245 // Some debug files are stored in the .build-id directory like this:
246 // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
247 uuid_str = module_uuid.GetAsString("");
248 uuid_str.insert (2, 1, '/');
249 uuid_str = uuid_str + ".debug";
250 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000251
Robert Flack31870e12015-04-24 18:09:54 +0000252 // Get directory of our module. Needed to check debug files like this:
253 // /usr/lib/debug/usr/lib/library.so.debug
254 std::string module_directory = module_spec.GetFileSpec().GetDirectory().AsCString();
255
256 size_t num_directories = debug_file_search_paths.GetSize();
257 for (size_t idx = 0; idx < num_directories; ++idx)
258 {
259 FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex (idx);
260 dirspec.ResolvePath();
261 if (!dirspec.Exists() || !dirspec.IsDirectory())
Michael Sartaina7499c92013-07-01 19:45:50 +0000262 continue;
263
Robert Flack31870e12015-04-24 18:09:54 +0000264 std::vector<std::string> files;
265 std::string dirname = dirspec.GetPath();
266
267 files.push_back (dirname + "/" + symbol_filename);
268 files.push_back (dirname + "/.debug/" + symbol_filename);
269 files.push_back (dirname + "/.build-id/" + uuid_str);
270 files.push_back (dirname + module_directory + "/" + symbol_filename);
271
272 const uint32_t num_files = files.size();
273 for (size_t idx_file = 0; idx_file < num_files; ++idx_file)
Michael Sartaina7499c92013-07-01 19:45:50 +0000274 {
Robert Flack31870e12015-04-24 18:09:54 +0000275 const std::string &filename = files[idx_file];
276 FileSpec file_spec (filename.c_str(), true);
277
278 if (llvm::sys::fs::equivalent (file_spec.GetPath(), module_spec.GetFileSpec().GetPath()))
279 continue;
280
281 if (file_spec.Exists())
Michael Sartaina7499c92013-07-01 19:45:50 +0000282 {
Robert Flack31870e12015-04-24 18:09:54 +0000283 lldb_private::ModuleSpecList specs;
284 const size_t num_specs = ObjectFile::GetModuleSpecifications (file_spec, 0, 0, specs);
285 assert (num_specs <= 1 && "Symbol Vendor supports only a single architecture");
286 if (num_specs == 1)
Michael Sartaina7499c92013-07-01 19:45:50 +0000287 {
Robert Flack31870e12015-04-24 18:09:54 +0000288 ModuleSpec mspec;
289 if (specs.GetModuleSpecAtIndex (0, mspec))
290 {
291 if (mspec.GetUUID() == module_uuid)
292 return file_spec;
293 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000294 }
295 }
296 }
297 }
298 }
299
Robert Flack31870e12015-04-24 18:09:54 +0000300 return LocateExecutableSymbolFileDsym(module_spec);
Michael Sartaina7499c92013-07-01 19:45:50 +0000301}
302
Robert Flack31870e12015-04-24 18:09:54 +0000303#if !defined (__APPLE__)
304
Michael Sartaina7499c92013-07-01 19:45:50 +0000305FileSpec
306Symbols::FindSymbolFileInBundle (const FileSpec& symfile_bundle,
307 const lldb_private::UUID *uuid,
308 const ArchSpec *arch)
309{
310 // FIXME
311 return FileSpec();
312}
313
314bool
315Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup)
316{
317 // Fill in the module_spec.GetFileSpec() for the object file and/or the
318 // module_spec.GetSymbolFileSpec() for the debug symbols file.
319 return false;
320}
321
Greg Clayton2bddd342010-09-07 20:11:56 +0000322#endif