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); |
| 58 | assert(got_spec); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 59 | 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 | |
| 69 | static 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 Flack | ab78164 | 2015-05-21 15:44:24 +0000 | [diff] [blame] | 127 | return true; |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 128 | } else { |
| 129 | *last_dot = '\0'; |
| 130 | char *prev_slash = strrchr(path, '/'); |
| 131 | if (prev_slash != NULL) |
| 132 | *prev_slash = '\0'; |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 133 | else |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | } else { |
| 137 | break; |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 138 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 139 | } |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 140 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 141 | } |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 142 | } |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 143 | } |
| 144 | dsym_fspec.Clear(); |
| 145 | return false; |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 148 | FileSpec 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 Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 152 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 153 | 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 Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 158 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 159 | 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 Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 170 | } |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 171 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 172 | ModuleSpec 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 Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 182 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 183 | 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 Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 193 | } |
| 194 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 195 | FileSpec 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 Berghammer | d00438e | 2015-07-30 12:38:18 +0000 | [diff] [blame] | 199 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 200 | 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 Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 204 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 205 | // 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 Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 209 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 210 | // Add current working directory. |
| 211 | debug_file_search_paths.AppendIfUnique(FileSpec(".", true)); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 212 | |
Vadim Macagon | 3014991 | 2015-10-13 16:30:28 +0000 | [diff] [blame] | 213 | #ifndef LLVM_ON_WIN32 |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 214 | // Add /usr/lib/debug directory. |
| 215 | debug_file_search_paths.AppendIfUnique(FileSpec("/usr/lib/debug", true)); |
Vadim Macagon | 3014991 | 2015-10-13 16:30:28 +0000 | [diff] [blame] | 216 | #endif // LLVM_ON_WIN32 |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 217 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 218 | 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 Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 228 | 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 Parsons | 771ef6d | 2016-11-02 20:34:10 +0000 | [diff] [blame] | 250 | FileSpec file_spec(filename, true); |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 251 | |
| 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 Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 277 | #if !defined(__APPLE__) |
Robert Flack | 31870e1 | 2015-04-24 18:09:54 +0000 | [diff] [blame] | 278 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 279 | FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle, |
| 280 | const lldb_private::UUID *uuid, |
| 281 | const ArchSpec *arch) { |
| 282 | // FIXME |
| 283 | return FileSpec(); |
Michael Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 +0000 | [diff] [blame] | 286 | bool 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 Sartain | a7499c9 | 2013-07-01 19:45:50 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Greg Clayton | 2bddd34 | 2010-09-07 20:11:56 +0000 | [diff] [blame] | 293 | #endif |