blob: d71a122aafd09a749d4e192f883c272e5f5903af [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"
14#include "lldb/Core/Module.h"
15#include "lldb/Core/ModuleSpec.h"
16#include "lldb/Core/StreamString.h"
17#include "lldb/Core/Timer.h"
18#include "lldb/Core/UUID.h"
19#include "lldb/Symbol/ObjectFile.h"
20#include "lldb/Target/Target.h"
Robert Flack31870e12015-04-24 18:09:54 +000021#include "lldb/Utility/SafeMachO.h"
Eli Friedman5423ebf2010-07-02 19:28:44 +000022
Zachary Turner88c6b622015-03-03 18:34:26 +000023#include "llvm/Support/FileSystem.h"
24
Robert Flack31870e12015-04-24 18:09:54 +000025// From MacOSX system header "mach/machine.h"
26typedef int cpu_type_t;
27typedef int cpu_subtype_t;
28
Eli Friedman5423ebf2010-07-02 19:28:44 +000029using namespace lldb;
30using namespace lldb_private;
Robert Flack31870e12015-04-24 18:09:54 +000031using namespace llvm::MachO;
Eli Friedman5423ebf2010-07-02 19:28:44 +000032
Robert Flack31870e12015-04-24 18:09:54 +000033#if defined(__APPLE__)
34
35// Forward declaration of method defined in source/Host/macosx/Symbols.cpp
36int
37LocateMacOSXFilesUsingDebugSymbols
38(
39 const ModuleSpec &module_spec,
40 FileSpec *out_exec_fspec, // If non-NULL, try and find the executable
41 FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file
42);
43
44#else
45
46int
47LocateMacOSXFilesUsingDebugSymbols
48(
49 const ModuleSpec &module_spec,
50 FileSpec *out_exec_fspec, // If non-NULL, try and find the executable
51 FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file
52) {
53 // Cannot find MacOSX files using debug symbols on non MacOSX.
54 return 0;
55}
56
57#endif
58
59static bool
60LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec)
61{
62 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
63 if (exec_fspec)
64 {
65 char path[PATH_MAX];
66 if (exec_fspec->GetPath(path, sizeof(path)))
67 {
68 // Make sure the module isn't already just a dSYM file...
69 if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL)
70 {
71 size_t obj_file_path_length = strlen(path);
72 ::strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path) - strlen(path) - 1);
73 ::strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path) - strlen(path) - 1);
74
75 dsym_fspec.SetFile(path, false);
76
77 ModuleSpecList module_specs;
78 ModuleSpec matched_module_spec;
79 if (dsym_fspec.Exists() &&
80 ObjectFile::GetModuleSpecifications(dsym_fspec, 0, 0, module_specs) &&
81 module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
82 {
83 return true;
84 }
85 else
86 {
87 path[obj_file_path_length] = '\0';
88
89 char *last_dot = strrchr(path, '.');
90 while (last_dot != NULL && last_dot[0])
91 {
92 char *next_slash = strchr(last_dot, '/');
93 if (next_slash != NULL)
94 {
95 *next_slash = '\0';
96 ::strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path) - strlen(path) - 1);
97 ::strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path) - strlen(path) - 1);
98 dsym_fspec.SetFile(path, false);
99 if (dsym_fspec.Exists() &&
100 ObjectFile::GetModuleSpecifications(dsym_fspec, 0, 0, module_specs) &&
101 module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
102 {
103 return true;
104 }
105 else
106 {
107 *last_dot = '\0';
108 char *prev_slash = strrchr(path, '/');
109 if (prev_slash != NULL)
110 *prev_slash = '\0';
111 else
112 break;
113 }
114 }
115 else
116 {
117 break;
118 }
119 }
120 }
121 }
122 }
123 }
124 dsym_fspec.Clear();
125 return false;
126}
127
128FileSpec
129LocateExecutableSymbolFileDsym (const ModuleSpec &module_spec)
130{
131 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
132 const ArchSpec *arch = module_spec.GetArchitecturePtr();
133 const UUID *uuid = module_spec.GetUUIDPtr();
134
135 Timer scoped_timer (__PRETTY_FUNCTION__,
136 "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)",
137 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
138 arch ? arch->GetArchitectureName() : "<NULL>",
139 uuid);
140
141 FileSpec symbol_fspec;
142 // First try and find the dSYM in the same directory as the executable or in
143 // an appropriate parent directory
144 if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false)
145 {
146 // We failed to easily find the dSYM above, so use DebugSymbols
147 LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec);
148 }
149 return symbol_fspec;
150}
Michael Sartaina7499c92013-07-01 19:45:50 +0000151
152FileSpec
153Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec)
154{
Robert Flack31870e12015-04-24 18:09:54 +0000155 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr();
156 const ArchSpec *arch = module_spec.GetArchitecturePtr();
157 const UUID *uuid = module_spec.GetUUIDPtr();
158 Timer scoped_timer (__PRETTY_FUNCTION__,
159 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)",
160 exec_fspec ? exec_fspec->GetFilename().AsCString ("<NULL>") : "<NULL>",
161 arch ? arch->GetArchitectureName() : "<NULL>",
162 uuid);
163
164 FileSpec objfile_fspec;
165 ModuleSpecList module_specs;
166 ModuleSpec matched_module_spec;
167 if (exec_fspec &&
168 ObjectFile::GetModuleSpecifications(*exec_fspec, 0, 0, module_specs) &&
169 module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec))
170 {
171 objfile_fspec = exec_fspec;
172 }
173 else
174 {
175 LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL);
176 }
177 return objfile_fspec;
Michael Sartaina7499c92013-07-01 19:45:50 +0000178}
179
180FileSpec
181Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec)
182{
183 const char *symbol_filename = module_spec.GetSymbolFileSpec().GetFilename().AsCString();
Robert Flack31870e12015-04-24 18:09:54 +0000184 if (symbol_filename && symbol_filename[0])
Michael Sartaina7499c92013-07-01 19:45:50 +0000185 {
Robert Flack31870e12015-04-24 18:09:54 +0000186 FileSpecList debug_file_search_paths (Target::GetDefaultDebugFileSearchPaths());
Michael Sartaina7499c92013-07-01 19:45:50 +0000187
Robert Flack31870e12015-04-24 18:09:54 +0000188 // Add module directory.
189 const ConstString &file_dir = module_spec.GetFileSpec().GetDirectory();
190 debug_file_search_paths.AppendIfUnique (FileSpec(file_dir.AsCString("."), true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000191
Robert Flack31870e12015-04-24 18:09:54 +0000192 // Add current working directory.
193 debug_file_search_paths.AppendIfUnique (FileSpec(".", true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000194
Robert Flack31870e12015-04-24 18:09:54 +0000195 // Add /usr/lib/debug directory.
196 debug_file_search_paths.AppendIfUnique (FileSpec("/usr/lib/debug", true));
Michael Sartaina7499c92013-07-01 19:45:50 +0000197
Robert Flack31870e12015-04-24 18:09:54 +0000198 std::string uuid_str;
199 const UUID &module_uuid = module_spec.GetUUID();
200 if (module_uuid.IsValid())
Michael Sartaina7499c92013-07-01 19:45:50 +0000201 {
Robert Flack31870e12015-04-24 18:09:54 +0000202 // Some debug files are stored in the .build-id directory like this:
203 // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug
204 uuid_str = module_uuid.GetAsString("");
205 uuid_str.insert (2, 1, '/');
206 uuid_str = uuid_str + ".debug";
207 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000208
Robert Flack31870e12015-04-24 18:09:54 +0000209 // Get directory of our module. Needed to check debug files like this:
210 // /usr/lib/debug/usr/lib/library.so.debug
211 std::string module_directory = module_spec.GetFileSpec().GetDirectory().AsCString();
212
213 size_t num_directories = debug_file_search_paths.GetSize();
214 for (size_t idx = 0; idx < num_directories; ++idx)
215 {
216 FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex (idx);
217 dirspec.ResolvePath();
218 if (!dirspec.Exists() || !dirspec.IsDirectory())
Michael Sartaina7499c92013-07-01 19:45:50 +0000219 continue;
220
Robert Flack31870e12015-04-24 18:09:54 +0000221 std::vector<std::string> files;
222 std::string dirname = dirspec.GetPath();
223
224 files.push_back (dirname + "/" + symbol_filename);
225 files.push_back (dirname + "/.debug/" + symbol_filename);
226 files.push_back (dirname + "/.build-id/" + uuid_str);
227 files.push_back (dirname + module_directory + "/" + symbol_filename);
228
229 const uint32_t num_files = files.size();
230 for (size_t idx_file = 0; idx_file < num_files; ++idx_file)
Michael Sartaina7499c92013-07-01 19:45:50 +0000231 {
Robert Flack31870e12015-04-24 18:09:54 +0000232 const std::string &filename = files[idx_file];
233 FileSpec file_spec (filename.c_str(), true);
234
235 if (llvm::sys::fs::equivalent (file_spec.GetPath(), module_spec.GetFileSpec().GetPath()))
236 continue;
237
238 if (file_spec.Exists())
Michael Sartaina7499c92013-07-01 19:45:50 +0000239 {
Robert Flack31870e12015-04-24 18:09:54 +0000240 lldb_private::ModuleSpecList specs;
241 const size_t num_specs = ObjectFile::GetModuleSpecifications (file_spec, 0, 0, specs);
242 assert (num_specs <= 1 && "Symbol Vendor supports only a single architecture");
243 if (num_specs == 1)
Michael Sartaina7499c92013-07-01 19:45:50 +0000244 {
Robert Flack31870e12015-04-24 18:09:54 +0000245 ModuleSpec mspec;
246 if (specs.GetModuleSpecAtIndex (0, mspec))
247 {
248 if (mspec.GetUUID() == module_uuid)
249 return file_spec;
250 }
Michael Sartaina7499c92013-07-01 19:45:50 +0000251 }
252 }
253 }
254 }
255 }
256
Robert Flack31870e12015-04-24 18:09:54 +0000257 return LocateExecutableSymbolFileDsym(module_spec);
Michael Sartaina7499c92013-07-01 19:45:50 +0000258}
259
Robert Flack31870e12015-04-24 18:09:54 +0000260#if !defined (__APPLE__)
261
Michael Sartaina7499c92013-07-01 19:45:50 +0000262FileSpec
263Symbols::FindSymbolFileInBundle (const FileSpec& symfile_bundle,
264 const lldb_private::UUID *uuid,
265 const ArchSpec *arch)
266{
267 // FIXME
268 return FileSpec();
269}
270
271bool
272Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup)
273{
274 // Fill in the module_spec.GetFileSpec() for the object file and/or the
275 // module_spec.GetSymbolFileSpec() for the debug symbols file.
276 return false;
277}
278
Greg Clayton2bddd342010-09-07 20:11:56 +0000279#endif