Eli Friedman | 5423ebf | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 1 | //===-- 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 Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 11 | #include "lldb/Core/ArchSpec.h" |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Module.h" |
| 13 | #include "lldb/Core/ModuleSpec.h" |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Timer.h" |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 15 | #include "lldb/Symbol/ObjectFile.h" |
| 16 | #include "lldb/Target/Target.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 17 | #include "lldb/Utility/DataBuffer.h" |
| 18 | #include "lldb/Utility/DataExtractor.h" |
Zachary Turner | 6f9e690 | 2017-03-03 20:56:28 +0000 | [diff] [blame] | 19 | #include "lldb/Utility/Log.h" |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 20 | #include "lldb/Utility/SafeMachO.h" |
Zachary Turner | bf9a773 | 2017-02-02 21:39:50 +0000 | [diff] [blame] | 21 | #include "lldb/Utility/StreamString.h" |
Zachary Turner | 666cc0b | 2017-03-04 01:30:05 +0000 | [diff] [blame] | 22 | #include "lldb/Utility/UUID.h" |
Eli Friedman | 5423ebf | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 23 | |
Zachary Turner | 88c6b62 | 2015-03-03 18:34:26 +0000 | [diff] [blame] | 24 | #include "llvm/Support/FileSystem.h" |
| 25 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 26 | // From MacOSX system header "mach/machine.h" |
| 27 | typedef int cpu_type_t; |
| 28 | typedef int cpu_subtype_t; |
| 29 | |
Eli Friedman | 5423ebf | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 30 | using namespace lldb; |
| 31 | using namespace lldb_private; |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 32 | using namespace llvm::MachO; |
Eli Friedman | 5423ebf | 2010-07-02 19:28:44 +0000 | [diff] [blame] | 33 | |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 34 | #if defined(__APPLE__) |
| 35 | |
| 36 | // Forward declaration of method defined in source/Host/macosx/Symbols.cpp |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 37 | int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec, |
| 38 | ModuleSpec &return_module_spec); |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 39 | |
| 40 | #else |
| 41 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 42 | int 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 Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | #endif |
| 49 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 50 | static 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 Molenda | a1a8646 | 2017-02-16 02:08:33 +0000 | [diff] [blame] | 57 | bool got_spec = module_specs.GetModuleSpecAtIndex(i, spec); |
Bruce Mitchener | ef4536c | 2017-03-23 09:52:26 +0000 | [diff] [blame] | 58 | UNUSED_IF_ASSERT_DISABLED(got_spec); |
Jason Molenda | a1a8646 | 2017-02-16 02:08:33 +0000 | [diff] [blame] | 59 | assert(got_spec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 60 | 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 | |
| 70 | static 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 | } |
| 89 | size_t obj_file_path_length = strlen(path); |
| 90 | ::strncat(path, ".dSYM/Contents/Resources/DWARF/", |
| 91 | sizeof(path) - strlen(path) - 1); |
| 92 | ::strncat(path, exec_fspec->GetFilename().AsCString(), |
| 93 | sizeof(path) - strlen(path) - 1); |
| 94 | |
| 95 | dsym_fspec.SetFile(path, false); |
| 96 | |
| 97 | ModuleSpecList module_specs; |
| 98 | ModuleSpec matched_module_spec; |
| 99 | if (dsym_fspec.Exists() && |
| 100 | FileAtPathContainsArchAndUUID(dsym_fspec, |
| 101 | module_spec.GetArchitecturePtr(), |
| 102 | module_spec.GetUUIDPtr())) { |
| 103 | if (log) { |
| 104 | log->Printf("dSYM with matching UUID & arch found at %s", path); |
| 105 | } |
| 106 | return true; |
| 107 | } else { |
| 108 | path[obj_file_path_length] = '\0'; |
| 109 | |
| 110 | char *last_dot = strrchr(path, '.'); |
| 111 | while (last_dot != NULL && last_dot[0]) { |
| 112 | char *next_slash = strchr(last_dot, '/'); |
| 113 | if (next_slash != NULL) { |
| 114 | *next_slash = '\0'; |
| 115 | ::strncat(path, ".dSYM/Contents/Resources/DWARF/", |
| 116 | sizeof(path) - strlen(path) - 1); |
| 117 | ::strncat(path, exec_fspec->GetFilename().AsCString(), |
| 118 | sizeof(path) - strlen(path) - 1); |
| 119 | dsym_fspec.SetFile(path, false); |
| 120 | if (dsym_fspec.Exists() && |
| 121 | FileAtPathContainsArchAndUUID( |
| 122 | dsym_fspec, module_spec.GetArchitecturePtr(), |
| 123 | module_spec.GetUUIDPtr())) { |
| 124 | if (log) { |
| 125 | log->Printf("dSYM with matching UUID & arch found at %s", |
| 126 | path); |
| 127 | } |
Robert Flack | ab78164 | 2015-05-21 15:44:24 +0000 | [diff] [blame] | 128 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 129 | } else { |
| 130 | *last_dot = '\0'; |
| 131 | char *prev_slash = strrchr(path, '/'); |
| 132 | if (prev_slash != NULL) |
| 133 | *prev_slash = '\0'; |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 134 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 135 | break; |
| 136 | } |
| 137 | } else { |
| 138 | break; |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 139 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 140 | } |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 141 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 142 | } |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 143 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 144 | } |
| 145 | dsym_fspec.Clear(); |
| 146 | return false; |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 147 | } |
| 148 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 149 | FileSpec LocateExecutableSymbolFileDsym(const ModuleSpec &module_spec) { |
| 150 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
| 151 | const ArchSpec *arch = module_spec.GetArchitecturePtr(); |
| 152 | const UUID *uuid = module_spec.GetUUIDPtr(); |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 153 | |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame^] | 154 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 155 | Timer scoped_timer( |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame^] | 156 | func_cat, |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 157 | "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)", |
| 158 | exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>", |
| 159 | arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid); |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 160 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 161 | FileSpec symbol_fspec; |
| 162 | ModuleSpec dsym_module_spec; |
| 163 | // First try and find the dSYM in the same directory as the executable or in |
| 164 | // an appropriate parent directory |
| 165 | if (LocateDSYMInVincinityOfExecutable(module_spec, symbol_fspec) == false) { |
| 166 | // We failed to easily find the dSYM above, so use DebugSymbols |
| 167 | LocateMacOSXFilesUsingDebugSymbols(module_spec, dsym_module_spec); |
| 168 | } else { |
| 169 | dsym_module_spec.GetSymbolFileSpec() = symbol_fspec; |
| 170 | } |
| 171 | return dsym_module_spec.GetSymbolFileSpec(); |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 172 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 173 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 174 | ModuleSpec Symbols::LocateExecutableObjectFile(const ModuleSpec &module_spec) { |
| 175 | ModuleSpec result; |
| 176 | const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); |
| 177 | const ArchSpec *arch = module_spec.GetArchitecturePtr(); |
| 178 | const UUID *uuid = module_spec.GetUUIDPtr(); |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame^] | 179 | static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 180 | Timer scoped_timer( |
Pavel Labath | f9d1647 | 2017-05-15 13:02:37 +0000 | [diff] [blame^] | 181 | func_cat, "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)", |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 182 | exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>", |
| 183 | arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid); |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 184 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 185 | ModuleSpecList module_specs; |
| 186 | ModuleSpec matched_module_spec; |
| 187 | if (exec_fspec && |
| 188 | ObjectFile::GetModuleSpecifications(*exec_fspec, 0, 0, module_specs) && |
| 189 | module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) { |
| 190 | result.GetFileSpec() = exec_fspec; |
| 191 | } else { |
| 192 | LocateMacOSXFilesUsingDebugSymbols(module_spec, result); |
| 193 | } |
| 194 | return result; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 197 | FileSpec Symbols::LocateExecutableSymbolFile(const ModuleSpec &module_spec) { |
| 198 | FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec(); |
| 199 | if (symbol_file_spec.IsAbsolute() && symbol_file_spec.Exists()) |
| 200 | return symbol_file_spec; |
Tamas Berghammer | d00438e | 2015-07-30 12:38:18 +0000 | [diff] [blame] | 201 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 202 | const char *symbol_filename = symbol_file_spec.GetFilename().AsCString(); |
| 203 | if (symbol_filename && symbol_filename[0]) { |
| 204 | FileSpecList debug_file_search_paths( |
| 205 | Target::GetDefaultDebugFileSearchPaths()); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 206 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 207 | // Add module directory. |
| 208 | const ConstString &file_dir = module_spec.GetFileSpec().GetDirectory(); |
| 209 | debug_file_search_paths.AppendIfUnique( |
| 210 | FileSpec(file_dir.AsCString("."), true)); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 211 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 212 | // Add current working directory. |
| 213 | debug_file_search_paths.AppendIfUnique(FileSpec(".", true)); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 214 | |
Vadim Macagon | 3014991 | 2015-10-13 16:30:28 +0000 | [diff] [blame] | 215 | #ifndef LLVM_ON_WIN32 |
Kamil Rytarowski | 6420a2f | 2017-03-29 19:52:24 +0000 | [diff] [blame] | 216 | #if defined(__NetBSD__) |
| 217 | // Add /usr/libdata/debug directory. |
| 218 | debug_file_search_paths.AppendIfUnique(FileSpec("/usr/libdata/debug", true)); |
| 219 | #else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 220 | // Add /usr/lib/debug directory. |
| 221 | debug_file_search_paths.AppendIfUnique(FileSpec("/usr/lib/debug", true)); |
Kamil Rytarowski | 6420a2f | 2017-03-29 19:52:24 +0000 | [diff] [blame] | 222 | #endif |
Vadim Macagon | 3014991 | 2015-10-13 16:30:28 +0000 | [diff] [blame] | 223 | #endif // LLVM_ON_WIN32 |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 224 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 225 | std::string uuid_str; |
| 226 | const UUID &module_uuid = module_spec.GetUUID(); |
| 227 | if (module_uuid.IsValid()) { |
| 228 | // Some debug files are stored in the .build-id directory like this: |
| 229 | // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug |
| 230 | uuid_str = module_uuid.GetAsString(""); |
| 231 | uuid_str.insert(2, 1, '/'); |
| 232 | uuid_str = uuid_str + ".debug"; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 235 | size_t num_directories = debug_file_search_paths.GetSize(); |
| 236 | for (size_t idx = 0; idx < num_directories; ++idx) { |
| 237 | FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex(idx); |
| 238 | dirspec.ResolvePath(); |
Zachary Turner | 7d86ee5 | 2017-03-08 17:56:08 +0000 | [diff] [blame] | 239 | if (!llvm::sys::fs::is_directory(dirspec.GetPath())) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 240 | continue; |
| 241 | |
| 242 | std::vector<std::string> files; |
| 243 | std::string dirname = dirspec.GetPath(); |
| 244 | |
| 245 | files.push_back(dirname + "/" + symbol_filename); |
| 246 | files.push_back(dirname + "/.debug/" + symbol_filename); |
| 247 | files.push_back(dirname + "/.build-id/" + uuid_str); |
| 248 | |
| 249 | // Some debug files may stored in the module directory like this: |
| 250 | // /usr/lib/debug/usr/lib/library.so.debug |
| 251 | if (!file_dir.IsEmpty()) |
| 252 | files.push_back(dirname + file_dir.AsCString() + "/" + symbol_filename); |
| 253 | |
| 254 | const uint32_t num_files = files.size(); |
| 255 | for (size_t idx_file = 0; idx_file < num_files; ++idx_file) { |
| 256 | const std::string &filename = files[idx_file]; |
Malcolm Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 257 | FileSpec file_spec(filename, true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 258 | |
| 259 | if (llvm::sys::fs::equivalent(file_spec.GetPath(), |
| 260 | module_spec.GetFileSpec().GetPath())) |
| 261 | continue; |
| 262 | |
| 263 | if (file_spec.Exists()) { |
| 264 | lldb_private::ModuleSpecList specs; |
| 265 | const size_t num_specs = |
| 266 | ObjectFile::GetModuleSpecifications(file_spec, 0, 0, specs); |
| 267 | assert(num_specs <= 1 && |
| 268 | "Symbol Vendor supports only a single architecture"); |
| 269 | if (num_specs == 1) { |
| 270 | ModuleSpec mspec; |
| 271 | if (specs.GetModuleSpecAtIndex(0, mspec)) { |
| 272 | if (mspec.GetUUID() == module_uuid) |
| 273 | return file_spec; |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return LocateExecutableSymbolFileDsym(module_spec); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 282 | } |
| 283 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 284 | #if !defined(__APPLE__) |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 285 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 286 | FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle, |
| 287 | const lldb_private::UUID *uuid, |
| 288 | const ArchSpec *arch) { |
| 289 | // FIXME |
| 290 | return FileSpec(); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 293 | bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec, |
| 294 | bool force_lookup) { |
| 295 | // Fill in the module_spec.GetFileSpec() for the object file and/or the |
| 296 | // module_spec.GetSymbolFileSpec() for the debug symbols file. |
| 297 | return false; |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 300 | #endif |