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