1 //===-- LocateSymbolFile.cpp ----------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "lldb/Symbol/LocateSymbolFile.h" 10 11 #include "lldb/Core/ModuleList.h" 12 #include "lldb/Core/ModuleSpec.h" 13 #include "lldb/Core/Progress.h" 14 #include "lldb/Host/FileSystem.h" 15 #include "lldb/Symbol/ObjectFile.h" 16 #include "lldb/Utility/ArchSpec.h" 17 #include "lldb/Utility/DataBuffer.h" 18 #include "lldb/Utility/DataExtractor.h" 19 #include "lldb/Utility/LLDBLog.h" 20 #include "lldb/Utility/Log.h" 21 #include "lldb/Utility/Reproducer.h" 22 #include "lldb/Utility/StreamString.h" 23 #include "lldb/Utility/Timer.h" 24 #include "lldb/Utility/UUID.h" 25 26 #include "llvm/Support/FileSystem.h" 27 28 // From MacOSX system header "mach/machine.h" 29 typedef int cpu_type_t; 30 typedef int cpu_subtype_t; 31 32 using namespace lldb; 33 using namespace lldb_private; 34 35 #if defined(__APPLE__) 36 37 // Forward declaration of method defined in source/Host/macosx/Symbols.cpp 38 int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec, 39 ModuleSpec &return_module_spec); 40 41 #else 42 43 int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec, 44 ModuleSpec &return_module_spec) { 45 // Cannot find MacOSX files using debug symbols on non MacOSX. 46 return 0; 47 } 48 49 #endif 50 51 static bool FileAtPathContainsArchAndUUID(const FileSpec &file_fspec, 52 const ArchSpec *arch, 53 const lldb_private::UUID *uuid) { 54 ModuleSpecList module_specs; 55 if (ObjectFile::GetModuleSpecifications(file_fspec, 0, 0, module_specs)) { 56 ModuleSpec spec; 57 for (size_t i = 0; i < module_specs.GetSize(); ++i) { 58 bool got_spec = module_specs.GetModuleSpecAtIndex(i, spec); 59 UNUSED_IF_ASSERT_DISABLED(got_spec); 60 assert(got_spec); 61 if ((uuid == nullptr || (spec.GetUUIDPtr() && spec.GetUUID() == *uuid)) && 62 (arch == nullptr || 63 (spec.GetArchitecturePtr() && 64 spec.GetArchitecture().IsCompatibleMatch(*arch)))) { 65 return true; 66 } 67 } 68 } 69 return false; 70 } 71 72 // Given a binary exec_fspec, and a ModuleSpec with an architecture/uuid, 73 // return true if there is a matching dSYM bundle next to the exec_fspec, 74 // and return that value in dsym_fspec. 75 // If there is a .dSYM.yaa compressed archive next to the exec_fspec, 76 // call through Symbols::DownloadObjectAndSymbolFile to download the 77 // expanded/uncompressed dSYM and return that filepath in dsym_fspec. 78 79 static bool LookForDsymNextToExecutablePath(const ModuleSpec &mod_spec, 80 const FileSpec &exec_fspec, 81 FileSpec &dsym_fspec) { 82 ConstString filename = exec_fspec.GetFilename(); 83 FileSpec dsym_directory = exec_fspec; 84 dsym_directory.RemoveLastPathComponent(); 85 86 std::string dsym_filename = filename.AsCString(); 87 dsym_filename += ".dSYM"; 88 dsym_directory.AppendPathComponent(dsym_filename); 89 dsym_directory.AppendPathComponent("Contents"); 90 dsym_directory.AppendPathComponent("Resources"); 91 dsym_directory.AppendPathComponent("DWARF"); 92 93 if (FileSystem::Instance().Exists(dsym_directory)) { 94 95 // See if the binary name exists in the dSYM DWARF 96 // subdir. 97 dsym_fspec = dsym_directory; 98 dsym_fspec.AppendPathComponent(filename.AsCString()); 99 if (FileSystem::Instance().Exists(dsym_fspec) && 100 FileAtPathContainsArchAndUUID(dsym_fspec, mod_spec.GetArchitecturePtr(), 101 mod_spec.GetUUIDPtr())) { 102 return true; 103 } 104 105 // See if we have "../CF.framework" - so we'll look for 106 // CF.framework.dSYM/Contents/Resources/DWARF/CF 107 // We need to drop the last suffix after '.' to match 108 // 'CF' in the DWARF subdir. 109 std::string binary_name(filename.AsCString()); 110 auto last_dot = binary_name.find_last_of('.'); 111 if (last_dot != std::string::npos) { 112 binary_name.erase(last_dot); 113 dsym_fspec = dsym_directory; 114 dsym_fspec.AppendPathComponent(binary_name); 115 if (FileSystem::Instance().Exists(dsym_fspec) && 116 FileAtPathContainsArchAndUUID(dsym_fspec, 117 mod_spec.GetArchitecturePtr(), 118 mod_spec.GetUUIDPtr())) { 119 return true; 120 } 121 } 122 } 123 124 // See if we have a .dSYM.yaa next to this executable path. 125 FileSpec dsym_yaa_fspec = exec_fspec; 126 dsym_yaa_fspec.RemoveLastPathComponent(); 127 std::string dsym_yaa_filename = filename.AsCString(); 128 dsym_yaa_filename += ".dSYM.yaa"; 129 dsym_yaa_fspec.AppendPathComponent(dsym_yaa_filename); 130 131 if (FileSystem::Instance().Exists(dsym_yaa_fspec)) { 132 ModuleSpec mutable_mod_spec = mod_spec; 133 if (Symbols::DownloadObjectAndSymbolFile(mutable_mod_spec, true) && 134 FileSystem::Instance().Exists(mutable_mod_spec.GetSymbolFileSpec())) { 135 dsym_fspec = mutable_mod_spec.GetSymbolFileSpec(); 136 return true; 137 } 138 } 139 140 return false; 141 } 142 143 // Given a ModuleSpec with a FileSpec and optionally uuid/architecture 144 // filled in, look for a .dSYM bundle next to that binary. Returns true 145 // if a .dSYM bundle is found, and that path is returned in the dsym_fspec 146 // FileSpec. 147 // 148 // This routine looks a few directory layers above the given exec_path - 149 // exec_path might be /System/Library/Frameworks/CF.framework/CF and the 150 // dSYM might be /System/Library/Frameworks/CF.framework.dSYM. 151 // 152 // If there is a .dSYM.yaa compressed archive found next to the binary, 153 // we'll call DownloadObjectAndSymbolFile to expand it into a plain .dSYM 154 155 static bool LocateDSYMInVincinityOfExecutable(const ModuleSpec &module_spec, 156 FileSpec &dsym_fspec) { 157 Log *log = GetLog(LLDBLog::Host); 158 const FileSpec &exec_fspec = module_spec.GetFileSpec(); 159 if (exec_fspec) { 160 if (::LookForDsymNextToExecutablePath(module_spec, exec_fspec, 161 dsym_fspec)) { 162 if (log) { 163 LLDB_LOGF(log, "dSYM with matching UUID & arch found at %s", 164 dsym_fspec.GetPath().c_str()); 165 } 166 return true; 167 } else { 168 FileSpec parent_dirs = exec_fspec; 169 170 // Remove the binary name from the FileSpec 171 parent_dirs.RemoveLastPathComponent(); 172 173 // Add a ".dSYM" name to each directory component of the path, 174 // stripping off components. e.g. we may have a binary like 175 // /S/L/F/Foundation.framework/Versions/A/Foundation and 176 // /S/L/F/Foundation.framework.dSYM 177 // 178 // so we'll need to start with 179 // /S/L/F/Foundation.framework/Versions/A, add the .dSYM part to the 180 // "A", and if that doesn't exist, strip off the "A" and try it again 181 // with "Versions", etc., until we find a dSYM bundle or we've 182 // stripped off enough path components that there's no need to 183 // continue. 184 185 for (int i = 0; i < 4; i++) { 186 // Does this part of the path have a "." character - could it be a 187 // bundle's top level directory? 188 const char *fn = parent_dirs.GetFilename().AsCString(); 189 if (fn == nullptr) 190 break; 191 if (::strchr(fn, '.') != nullptr) { 192 if (::LookForDsymNextToExecutablePath(module_spec, parent_dirs, 193 dsym_fspec)) { 194 if (log) { 195 LLDB_LOGF(log, "dSYM with matching UUID & arch found at %s", 196 dsym_fspec.GetPath().c_str()); 197 } 198 return true; 199 } 200 } 201 parent_dirs.RemoveLastPathComponent(); 202 } 203 } 204 } 205 dsym_fspec.Clear(); 206 return false; 207 } 208 209 static FileSpec LocateExecutableSymbolFileDsym(const ModuleSpec &module_spec) { 210 const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); 211 const ArchSpec *arch = module_spec.GetArchitecturePtr(); 212 const UUID *uuid = module_spec.GetUUIDPtr(); 213 214 LLDB_SCOPED_TIMERF( 215 "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)", 216 exec_fspec ? exec_fspec->GetFilename().AsCString("<NULL>") : "<NULL>", 217 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid); 218 219 FileSpec symbol_fspec; 220 ModuleSpec dsym_module_spec; 221 // First try and find the dSYM in the same directory as the executable or in 222 // an appropriate parent directory 223 if (!LocateDSYMInVincinityOfExecutable(module_spec, symbol_fspec)) { 224 // We failed to easily find the dSYM above, so use DebugSymbols 225 LocateMacOSXFilesUsingDebugSymbols(module_spec, dsym_module_spec); 226 } else { 227 dsym_module_spec.GetSymbolFileSpec() = symbol_fspec; 228 } 229 230 return dsym_module_spec.GetSymbolFileSpec(); 231 } 232 233 ModuleSpec Symbols::LocateExecutableObjectFile(const ModuleSpec &module_spec) { 234 ModuleSpec result; 235 const FileSpec &exec_fspec = module_spec.GetFileSpec(); 236 const ArchSpec *arch = module_spec.GetArchitecturePtr(); 237 const UUID *uuid = module_spec.GetUUIDPtr(); 238 LLDB_SCOPED_TIMERF( 239 "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)", 240 exec_fspec ? exec_fspec.GetFilename().AsCString("<NULL>") : "<NULL>", 241 arch ? arch->GetArchitectureName() : "<NULL>", (const void *)uuid); 242 243 ModuleSpecList module_specs; 244 ModuleSpec matched_module_spec; 245 if (exec_fspec && 246 ObjectFile::GetModuleSpecifications(exec_fspec, 0, 0, module_specs) && 247 module_specs.FindMatchingModuleSpec(module_spec, matched_module_spec)) { 248 result.GetFileSpec() = exec_fspec; 249 } else { 250 LocateMacOSXFilesUsingDebugSymbols(module_spec, result); 251 } 252 253 return result; 254 } 255 256 // Keep "symbols.enable-external-lookup" description in sync with this function. 257 258 FileSpec 259 Symbols::LocateExecutableSymbolFile(const ModuleSpec &module_spec, 260 const FileSpecList &default_search_paths) { 261 FileSpec symbol_file_spec = module_spec.GetSymbolFileSpec(); 262 if (symbol_file_spec.IsAbsolute() && 263 FileSystem::Instance().Exists(symbol_file_spec)) 264 return symbol_file_spec; 265 266 Progress progress(llvm::formatv( 267 "Locating external symbol file for {0}", 268 module_spec.GetFileSpec().GetFilename().AsCString("<Unknown>"))); 269 270 FileSpecList debug_file_search_paths = default_search_paths; 271 272 // Add module directory. 273 FileSpec module_file_spec = module_spec.GetFileSpec(); 274 // We keep the unresolved pathname if it fails. 275 FileSystem::Instance().ResolveSymbolicLink(module_file_spec, 276 module_file_spec); 277 278 ConstString file_dir = module_file_spec.GetDirectory(); 279 { 280 FileSpec file_spec(file_dir.AsCString(".")); 281 FileSystem::Instance().Resolve(file_spec); 282 debug_file_search_paths.AppendIfUnique(file_spec); 283 } 284 285 if (ModuleList::GetGlobalModuleListProperties().GetEnableExternalLookup()) { 286 287 // Add current working directory. 288 { 289 FileSpec file_spec("."); 290 FileSystem::Instance().Resolve(file_spec); 291 debug_file_search_paths.AppendIfUnique(file_spec); 292 } 293 294 #ifndef _WIN32 295 #if defined(__NetBSD__) 296 // Add /usr/libdata/debug directory. 297 { 298 FileSpec file_spec("/usr/libdata/debug"); 299 FileSystem::Instance().Resolve(file_spec); 300 debug_file_search_paths.AppendIfUnique(file_spec); 301 } 302 #else 303 // Add /usr/lib/debug directory. 304 { 305 FileSpec file_spec("/usr/lib/debug"); 306 FileSystem::Instance().Resolve(file_spec); 307 debug_file_search_paths.AppendIfUnique(file_spec); 308 } 309 #endif 310 #endif // _WIN32 311 } 312 313 std::string uuid_str; 314 const UUID &module_uuid = module_spec.GetUUID(); 315 if (module_uuid.IsValid()) { 316 // Some debug files are stored in the .build-id directory like this: 317 // /usr/lib/debug/.build-id/ff/e7fe727889ad82bb153de2ad065b2189693315.debug 318 uuid_str = module_uuid.GetAsString(""); 319 std::transform(uuid_str.begin(), uuid_str.end(), uuid_str.begin(), 320 ::tolower); 321 uuid_str.insert(2, 1, '/'); 322 uuid_str = uuid_str + ".debug"; 323 } 324 325 size_t num_directories = debug_file_search_paths.GetSize(); 326 for (size_t idx = 0; idx < num_directories; ++idx) { 327 FileSpec dirspec = debug_file_search_paths.GetFileSpecAtIndex(idx); 328 FileSystem::Instance().Resolve(dirspec); 329 if (!FileSystem::Instance().IsDirectory(dirspec)) 330 continue; 331 332 std::vector<std::string> files; 333 std::string dirname = dirspec.GetPath(); 334 335 if (!uuid_str.empty()) 336 files.push_back(dirname + "/.build-id/" + uuid_str); 337 if (symbol_file_spec.GetFilename()) { 338 files.push_back(dirname + "/" + 339 symbol_file_spec.GetFilename().GetCString()); 340 files.push_back(dirname + "/.debug/" + 341 symbol_file_spec.GetFilename().GetCString()); 342 343 // Some debug files may stored in the module directory like this: 344 // /usr/lib/debug/usr/lib/library.so.debug 345 if (!file_dir.IsEmpty()) 346 files.push_back(dirname + file_dir.AsCString() + "/" + 347 symbol_file_spec.GetFilename().GetCString()); 348 } 349 350 const uint32_t num_files = files.size(); 351 for (size_t idx_file = 0; idx_file < num_files; ++idx_file) { 352 const std::string &filename = files[idx_file]; 353 FileSpec file_spec(filename); 354 FileSystem::Instance().Resolve(file_spec); 355 356 if (llvm::sys::fs::equivalent(file_spec.GetPath(), 357 module_file_spec.GetPath())) 358 continue; 359 360 if (FileSystem::Instance().Exists(file_spec)) { 361 lldb_private::ModuleSpecList specs; 362 const size_t num_specs = 363 ObjectFile::GetModuleSpecifications(file_spec, 0, 0, specs); 364 assert(num_specs <= 1 && 365 "Symbol Vendor supports only a single architecture"); 366 if (num_specs == 1) { 367 ModuleSpec mspec; 368 if (specs.GetModuleSpecAtIndex(0, mspec)) { 369 // Skip the uuids check if module_uuid is invalid. For example, 370 // this happens for *.dwp files since at the moment llvm-dwp 371 // doesn't output build ids, nor does binutils dwp. 372 if (!module_uuid.IsValid() || module_uuid == mspec.GetUUID()) 373 return file_spec; 374 } 375 } 376 } 377 } 378 } 379 380 return LocateExecutableSymbolFileDsym(module_spec); 381 } 382 383 #if !defined(__APPLE__) 384 385 FileSpec Symbols::FindSymbolFileInBundle(const FileSpec &symfile_bundle, 386 const lldb_private::UUID *uuid, 387 const ArchSpec *arch) { 388 // FIXME 389 return FileSpec(); 390 } 391 392 bool Symbols::DownloadObjectAndSymbolFile(ModuleSpec &module_spec, 393 bool force_lookup) { 394 // Fill in the module_spec.GetFileSpec() for the object file and/or the 395 // module_spec.GetSymbolFileSpec() for the debug symbols file. 396 return false; 397 } 398 399 #endif 400