33#include "llvm/ADT/SmallSet.h"
34#include "llvm/Support/FileSystem.h"
35#include "llvm/Support/ThreadPool.h"
42#include "mach/machine.h"
44#include <CoreFoundation/CoreFoundation.h>
56 CFUUIDRef uuid, CFURLRef exec_url) =
nullptr;
76 return "DebugSymbols symbol locator.";
87 LLDB_LOGF(log,
"Spotlight lookup for .dSYM bundles is disabled.");
91 return_module_spec = module_spec;
102 void *handle = dlopen(
103 "/System/Library/PrivateFrameworks/DebugSymbols.framework/DebugSymbols",
104 RTLD_LAZY | RTLD_LOCAL);
107 (CFURLRef(*)(CFUUIDRef, CFURLRef))dlsym(handle,
108 "DBGCopyFullDSYMURLForUUID");
110 handle,
"DBGCopyDSYMPropertyLists");
121 llvm::ArrayRef<uint8_t> module_uuid = uuid->
GetBytes();
122 if (module_uuid.size() == 16) {
124 NULL, module_uuid[0], module_uuid[1], module_uuid[2], module_uuid[3],
125 module_uuid[4], module_uuid[5], module_uuid[6], module_uuid[7],
126 module_uuid[8], module_uuid[9], module_uuid[10], module_uuid[11],
127 module_uuid[12], module_uuid[13], module_uuid[14], module_uuid[15]));
129 if (module_uuid_ref.
get()) {
134 if (exec_fspec->
GetPath(exec_cf_path,
sizeof(exec_cf_path)))
135 exec_url.
reset(::CFURLCreateFromFileSystemRepresentation(
136 NULL, (
const UInt8 *)exec_cf_path, strlen(exec_cf_path),
141 module_uuid_ref.
get(), exec_url.
get()));
144 if (dsym_url.
get()) {
145 if (::CFURLGetFileSystemRepresentation(
146 dsym_url.
get(),
true, (UInt8 *)path,
sizeof(path) - 1)) {
148 "DebugSymbols framework returned dSYM path of %s for "
149 "UUID %s -- looking for the dSYM",
157 dsym_filespec, uuid, arch);
165 bool success =
false;
167 if (::CFURLGetFileSystemRepresentation(
168 dsym_url.
get(),
true, (UInt8 *)path,
sizeof(path) - 1)) {
170 "DebugSymbols framework returned dSYM path of %s for "
171 "UUID %s -- looking for an exec file",
178 CFDictionaryRef uuid_dict = NULL;
181 uuid_dict =
static_cast<CFDictionaryRef
>(
182 ::CFDictionaryGetValue(dict.
get(), uuid_cfstr.
get()));
191 module_sp.reset(
new Module(exe_spec));
192 if (module_sp && module_sp->GetObjectFile() &&
193 module_sp->MatchesModuleSpec(exe_spec)) {
196 LLDB_LOGF(log,
"using original binary filepath %s for UUID %s",
215 "using binary from shared cache for filepath %s for "
224 if (!success && uuid_dict) {
227 uuid_dict, CFSTR(
"DBGSymbolRichExecutable")));
228 if (exec_cf_path && ::CFStringGetFileSystemRepresentation(
229 exec_cf_path, path,
sizeof(path))) {
230 LLDB_LOGF(log,
"plist bundle has exec path of %s for UUID %s",
245 if (::CFURLGetFileSystemRepresentation(
246 dsym_url.
get(),
true, (UInt8 *)path,
sizeof(path) - 1)) {
247 char *dsym_extension_pos = ::strstr(path,
".dSYM");
248 if (dsym_extension_pos) {
249 *dsym_extension_pos =
'\0';
251 "Looking for executable binary next to dSYM "
252 "bundle with name with name %s",
258 using namespace llvm::sys::fs;
259 switch (get_file_type(file_spec.
GetPath())) {
261 case file_type::directory_file:
266 if (bundle_exe_url.
get()) {
267 if (::CFURLGetFileSystemRepresentation(bundle_exe_url.
get(),
270 FileSpec bundle_exe_file_spec(path);
273 bundle_exe_file_spec, 0, 0, module_specs) &&
275 module_spec, matched_module_spec))
279 return_module_spec.
GetFileSpec() = bundle_exe_file_spec;
281 "Executable binary %s next to dSYM is "
289 case file_type::fifo_file:
290 case file_type::socket_file:
291 case file_type::file_not_found:
292 case file_type::status_error:
295 case file_type::type_unknown:
296 case file_type::regular_file:
297 case file_type::symlink_file:
298 case file_type::block_file:
299 case file_type::character_file:
303 matched_module_spec))
309 "Executable binary %s next to dSYM is "
324 return return_module_spec;
331 std::string dsym_bundle_path = dsym_bundle_fspec.
GetPath();
332 llvm::SmallString<128> buffer(dsym_bundle_path);
333 llvm::sys::path::append(buffer,
"Contents",
"Resources",
"DWARF");
336 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> vfs =
338 llvm::vfs::recursive_directory_iterator Iter(*vfs, buffer.str(), EC);
339 llvm::vfs::recursive_directory_iterator End;
340 for (; Iter != End && !EC; Iter.increment(EC)) {
341 llvm::ErrorOr<llvm::vfs::Status>
Status = vfs->status(Iter->path());
342 if (
Status->isDirectory())
349 for (
size_t i = 0; i < module_specs.
GetSize(); ++i) {
353 if ((uuid ==
nullptr ||
373 for (
size_t i = 0; i < module_specs.
GetSize(); ++i) {
398 FileSpec dsym_directory = exec_fspec;
401 std::string dsym_filename = filename.
AsCString();
402 dsym_filename +=
".dSYM";
412 dsym_fspec = dsym_directory;
424 std::string binary_name(filename.
AsCString());
425 auto last_dot = binary_name.find_last_of(
'.');
426 if (last_dot != std::string::npos) {
427 binary_name.erase(last_dot);
428 dsym_fspec = dsym_directory;
440 FileSpec dsym_yaa_fspec = exec_fspec;
442 std::string dsym_yaa_filename = filename.
AsCString();
443 dsym_yaa_filename +=
".dSYM.yaa";
479 LLDB_LOGF(log,
"dSYM with matching UUID & arch found at %s",
501 for (
int i = 0; i < 4; i++) {
507 if (::strchr(fn,
'.') !=
nullptr) {
511 LLDB_LOGF(log,
"dSYM with matching UUID & arch found at %s",
529 LLDB_LOGF(log,
"Spotlight lookup for .dSYM bundles is disabled.");
533 return_module_spec = module_spec;
544 void *handle = dlopen(
545 "/System/Library/PrivateFrameworks/DebugSymbols.framework/DebugSymbols",
546 RTLD_LAZY | RTLD_LOCAL);
549 (CFURLRef(*)(CFUUIDRef, CFURLRef))dlsym(handle,
550 "DBGCopyFullDSYMURLForUUID");
552 handle,
"DBGCopyDSYMPropertyLists");
563 llvm::ArrayRef<uint8_t> module_uuid = uuid->
GetBytes();
564 if (module_uuid.size() == 16) {
566 NULL, module_uuid[0], module_uuid[1], module_uuid[2], module_uuid[3],
567 module_uuid[4], module_uuid[5], module_uuid[6], module_uuid[7],
568 module_uuid[8], module_uuid[9], module_uuid[10], module_uuid[11],
569 module_uuid[12], module_uuid[13], module_uuid[14], module_uuid[15]));
571 if (module_uuid_ref.
get()) {
576 if (exec_fspec->
GetPath(exec_cf_path,
sizeof(exec_cf_path)))
577 exec_url.
reset(::CFURLCreateFromFileSystemRepresentation(
578 NULL, (
const UInt8 *)exec_cf_path, strlen(exec_cf_path),
583 module_uuid_ref.
get(), exec_url.
get()));
586 if (dsym_url.
get()) {
587 if (::CFURLGetFileSystemRepresentation(
588 dsym_url.
get(),
true, (UInt8 *)path,
sizeof(path) - 1)) {
590 "DebugSymbols framework returned dSYM path of %s for "
591 "UUID %s -- looking for the dSYM",
599 dsym_filespec, uuid, arch);
607 bool success =
false;
609 if (::CFURLGetFileSystemRepresentation(
610 dsym_url.
get(),
true, (UInt8 *)path,
sizeof(path) - 1)) {
612 "DebugSymbols framework returned dSYM path of %s for "
613 "UUID %s -- looking for an exec file",
620 CFDictionaryRef uuid_dict = NULL;
623 uuid_dict =
static_cast<CFDictionaryRef
>(
624 ::CFDictionaryGetValue(dict.
get(), uuid_cfstr.
get()));
633 module_sp.reset(
new Module(exe_spec));
634 if (module_sp && module_sp->GetObjectFile() &&
635 module_sp->MatchesModuleSpec(exe_spec)) {
638 LLDB_LOGF(log,
"using original binary filepath %s for UUID %s",
657 "using binary from shared cache for filepath %s for "
666 if (!success && uuid_dict) {
669 uuid_dict, CFSTR(
"DBGSymbolRichExecutable")));
670 if (exec_cf_path && ::CFStringGetFileSystemRepresentation(
671 exec_cf_path, path,
sizeof(path))) {
672 LLDB_LOGF(log,
"plist bundle has exec path of %s for UUID %s",
687 if (::CFURLGetFileSystemRepresentation(
688 dsym_url.
get(),
true, (UInt8 *)path,
sizeof(path) - 1)) {
689 char *dsym_extension_pos = ::strstr(path,
".dSYM");
690 if (dsym_extension_pos) {
691 *dsym_extension_pos =
'\0';
693 "Looking for executable binary next to dSYM "
694 "bundle with name with name %s",
700 using namespace llvm::sys::fs;
701 switch (get_file_type(file_spec.
GetPath())) {
703 case file_type::directory_file:
708 if (bundle_exe_url.
get()) {
709 if (::CFURLGetFileSystemRepresentation(bundle_exe_url.
get(),
712 FileSpec bundle_exe_file_spec(path);
715 bundle_exe_file_spec, 0, 0, module_specs) &&
717 module_spec, matched_module_spec))
721 return_module_spec.
GetFileSpec() = bundle_exe_file_spec;
723 "Executable binary %s next to dSYM is "
731 case file_type::fifo_file:
732 case file_type::socket_file:
733 case file_type::file_not_found:
734 case file_type::status_error:
737 case file_type::type_unknown:
738 case file_type::regular_file:
739 case file_type::symlink_file:
740 case file_type::block_file:
741 case file_type::character_file:
745 matched_module_spec))
751 "Executable binary %s next to dSYM is "
775 "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)",
780 "Locating external symbol file",
800 const std::string &command) {
802 bool success =
false;
803 if (uuid_dict != NULL && CFGetTypeID(uuid_dict) == CFDictionaryGetTypeID()) {
806 CFDictionaryRef cf_dict;
808 cf_str = (
CFStringRef)CFDictionaryGetValue((CFDictionaryRef)uuid_dict,
810 if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
812 std::string errorstr = command;
815 error.SetErrorString(errorstr);
820 (CFDictionaryRef)uuid_dict, CFSTR(
"DBGSymbolRichExecutable"));
821 if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
826 "From dsymForUUID plist: Symbol rich executable is at '%s'",
831 cf_str = (
CFStringRef)CFDictionaryGetValue((CFDictionaryRef)uuid_dict,
832 CFSTR(
"DBGDSYMPath"));
833 if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
836 FileSpec::Style::native);
839 LLDB_LOGF(log,
"From dsymForUUID plist: dSYM is at '%s'", str.c_str());
843 std::string DBGBuildSourcePath;
844 std::string DBGSourcePath;
853 cf_dict = (CFDictionaryRef)CFDictionaryGetValue(
854 (CFDictionaryRef)uuid_dict, CFSTR(
"DBGSourcePathRemapping"));
855 if (cf_dict && CFGetTypeID(cf_dict) == CFDictionaryGetTypeID()) {
858 bool new_style_source_remapping_dictionary =
false;
859 bool do_truncate_remapping_names =
false;
860 std::string original_DBGSourcePath_value = DBGSourcePath;
861 cf_str = (
CFStringRef)CFDictionaryGetValue((CFDictionaryRef)uuid_dict,
862 CFSTR(
"DBGVersion"));
863 if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
866 if (!version.empty() && isdigit(version[0])) {
867 int version_number = atoi(version.c_str());
868 if (version_number > 1) {
869 new_style_source_remapping_dictionary =
true;
871 if (version_number == 2) {
872 do_truncate_remapping_names =
true;
877 CFIndex kv_pair_count = CFDictionaryGetCount((CFDictionaryRef)uuid_dict);
878 if (kv_pair_count > 0) {
883 if (keys !=
nullptr && values !=
nullptr) {
884 CFDictionaryGetKeysAndValues((CFDictionaryRef)uuid_dict,
886 (
const void **)values);
888 for (CFIndex i = 0; i < kv_pair_count; i++) {
889 DBGBuildSourcePath.clear();
890 DBGSourcePath.clear();
891 if (keys[i] && CFGetTypeID(keys[i]) == CFStringGetTypeID()) {
894 if (values[i] && CFGetTypeID(values[i]) == CFStringGetTypeID()) {
897 if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty()) {
902 if (new_style_source_remapping_dictionary &&
903 !original_DBGSourcePath_value.empty()) {
904 DBGSourcePath = original_DBGSourcePath_value;
906 if (DBGSourcePath[0] ==
'~') {
907 FileSpec resolved_source_path(DBGSourcePath.c_str());
909 DBGSourcePath = resolved_source_path.
GetPath();
916 DBGSourcePath,
true);
917 if (do_truncate_remapping_names) {
918 FileSpec build_path(DBGBuildSourcePath.c_str());
919 FileSpec source_path(DBGSourcePath.c_str());
939 cf_str = (
CFStringRef)CFDictionaryGetValue((CFDictionaryRef)uuid_dict,
940 CFSTR(
"DBGBuildSourcePath"));
941 if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
945 cf_str = (
CFStringRef)CFDictionaryGetValue((CFDictionaryRef)uuid_dict,
946 CFSTR(
"DBGSourcePath"));
947 if (cf_str && CFGetTypeID(cf_str) == CFStringGetTypeID()) {
951 if (!DBGBuildSourcePath.empty() && !DBGSourcePath.empty()) {
952 if (DBGSourcePath[0] ==
'~') {
953 FileSpec resolved_source_path(DBGSourcePath.c_str());
955 DBGSourcePath = resolved_source_path.
GetPath();
958 DBGSourcePath,
true);
967 static std::once_flag g_once_flag;
968 static std::string g_dbgshell_command;
969 std::call_once(g_once_flag, [&]() {
970 CFTypeRef defaults_setting = CFPreferencesCopyAppValue(
971 CFSTR(
"DBGShellCommands"), CFSTR(
"com.apple.DebugSymbols"));
972 if (defaults_setting &&
973 CFGetTypeID(defaults_setting) == CFStringGetTypeID()) {
975 if (CFStringGetCString((
CFStringRef)defaults_setting, buffer,
976 sizeof(buffer), kCFStringEncodingUTF8)) {
977 g_dbgshell_command = buffer;
980 if (defaults_setting) {
981 CFRelease(defaults_setting);
984 return g_dbgshell_command;
993 if (
const char *dsymForUUID_env =
994 getenv(
"LLDB_APPLE_DSYMFORUUID_EXECUTABLE")) {
995 FileSpec dsymForUUID_executable(dsymForUUID_env);
998 return dsymForUUID_executable;
1001 static std::once_flag g_once_flag;
1002 static FileSpec g_dsymForUUID_executable;
1003 std::call_once(g_once_flag, [&]() {
1006 if (!dbgshell_command.empty()) {
1007 g_dsymForUUID_executable =
FileSpec(dbgshell_command);
1015 g_dsymForUUID_executable =
FileSpec(
"/usr/local/bin/dsymForUUID");
1021 g_dsymForUUID_executable = {};
1023 return g_dsymForUUID_executable;
1028 bool copy_executable) {
1039 if (!force_lookup && dbgshell_command.empty())
1049 if (!dsymForUUID_exe_spec)
1053 const std::string dsymForUUID_exe_path = dsymForUUID_exe_spec.
GetPath();
1054 const std::string uuid_str = uuid_ptr ? uuid_ptr->
GetAsString() :
"";
1056 std::string lookup_arg = uuid_str;
1057 if (lookup_arg.empty())
1058 lookup_arg = file_spec_ptr ? file_spec_ptr->
GetPath() :
"";
1059 if (lookup_arg.empty())
1063 command << dsymForUUID_exe_path <<
" --ignoreNegativeCache ";
1064 if (copy_executable)
1065 command <<
"--copyExecutable ";
1066 command << lookup_arg;
1069 std::string lookup_desc;
1070 if (uuid_ptr && file_spec_ptr)
1076 else if (file_spec_ptr)
1080 LLDB_LOG(log,
"Calling {0} for {1} to find dSYM: {2}", dsymForUUID_exe_path,
1083 Progress progress(
"Downloading symbol file for", lookup_desc);
1086 int exit_status = -1;
1088 std::string command_output;
1095 std::chrono::seconds(
1099 if (
error.Fail() || exit_status != 0 || command_output.empty()) {
1100 LLDB_LOGF(log,
"'%s' failed (exit status: %d, error: '%s', output: '%s')",
1102 command_output.c_str());
1107 CFDataCreateWithBytesNoCopy(NULL, (
const UInt8 *)command_output.data(),
1108 command_output.size(), kCFAllocatorNull));
1111 (CFDictionaryRef)::CFPropertyListCreateWithData(
1112 NULL, data.
get(), kCFPropertyListImmutable, NULL, NULL));
1115 LLDB_LOGF(log,
"'%s' failed: output is not a valid plist",
1120 if (CFGetTypeID(plist.
get()) != CFDictionaryGetTypeID()) {
1121 LLDB_LOGF(log,
"'%s' failed: output plist is not a valid CFDictionary",
1126 if (!uuid_str.empty()) {
1128 CFDictionaryRef uuid_dict =
1129 (CFDictionaryRef)CFDictionaryGetValue(plist.
get(), uuid_cfstr.
get());
1134 if (
const CFIndex num_values = ::CFDictionaryGetCount(plist.
get())) {
1135 std::vector<CFStringRef> keys(num_values, NULL);
1136 std::vector<CFDictionaryRef> values(num_values, NULL);
1137 ::CFDictionaryGetKeysAndValues(plist.
get(), NULL,
1138 (
const void **)&values[0]);
1139 if (num_values == 1) {
1144 for (CFIndex i = 0; i < num_values; ++i) {
1150 module_spec = curr_module_spec;
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
#define LLDB_LOGF(log,...)
#define LLDB_PLUGIN_DEFINE(PluginName)
static llvm::StringRef GetDbgShellCommand()
It's expensive to check for the DBGShellCommands defaults setting.
static CFDictionaryRef(* g_dlsym_DBGCopyDSYMPropertyLists)(CFURLRef dsym_url)
static bool LocateDSYMInVincinityOfExecutable(const ModuleSpec &module_spec, FileSpec &dsym_fspec)
static int LocateMacOSXFilesUsingDebugSymbols(const ModuleSpec &module_spec, ModuleSpec &return_module_spec)
static bool FileAtPathContainsArchAndUUID(const FileSpec &file_fspec, const ArchSpec *arch, const lldb_private::UUID *uuid)
static bool LookForDsymNextToExecutablePath(const ModuleSpec &mod_spec, const FileSpec &exec_fspec, FileSpec &dsym_fspec)
static CFURLRef(* g_dlsym_DBGCopyFullDSYMURLForUUID)(CFUUIDRef uuid, CFURLRef exec_url)
static FileSpec GetDsymForUUIDExecutable()
Get the dsymForUUID executable and cache the result so we don't end up stat'ing the binary over and o...
static bool GetModuleSpecInfoFromUUIDDictionary(CFDictionaryRef uuid_dict, ModuleSpec &module_spec, Status &error, const std::string &command)
#define LLDB_SCOPED_TIMERF(...)
CFURLRef CopyExecutableURL() const
static const char * FileSystemRepresentation(CFStringRef cf_str, std::string &str)
An architecture specification class.
bool IsCompatibleMatch(const ArchSpec &rhs) const
Shorthand for IsMatch(rhs, CompatibleMatch).
const char * GetArchitectureName() const
Returns a static string representing the current architecture.
A uniqued constant string class.
std::string GetString() const
Get the string value as a std::string.
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
void AppendPathComponent(llvm::StringRef component)
const ConstString & GetFilename() const
Filename string const get accessor.
bool RemoveLastPathComponent()
Removes the last path component by replacing the current path with its parent.
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
void Clear()
Clears the object state.
void Resolve(llvm::SmallVectorImpl< char > &path)
Resolve path to make it canonical.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
llvm::IntrusiveRefCntPtr< llvm::vfs::FileSystem > GetVirtualFileSystem()
static FileSystem & Instance()
static Status RunShellCommand(llvm::StringRef command, const FileSpec &working_dir, int *status_ptr, int *signo_ptr, std::string *command_output, const Timeout< std::micro > &timeout, bool run_in_shell=true, bool hide_stderr=false)
Run a shell command.
static ModuleListProperties & GetGlobalModuleListProperties()
bool GetModuleSpecAtIndex(size_t i, ModuleSpec &module_spec) const
bool FindMatchingModuleSpec(const ModuleSpec &module_spec, ModuleSpec &match_module_spec) const
PathMappingList & GetSourceMappingList() const
ArchSpec & GetArchitecture()
FileSpec * GetFileSpecPtr()
FileSpec & GetSymbolFileSpec()
ArchSpec * GetArchitecturePtr()
A class that describes an executable image and its associated object and symbol files.
static size_t GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, ModuleSpecList &specs, lldb::DataBufferSP data_sp=lldb::DataBufferSP())
void Append(llvm::StringRef path, llvm::StringRef replacement, bool notify)
static bool DownloadObjectAndSymbolFile(ModuleSpec &module_spec, Status &error, bool force_lookup=true, bool copy_executable=true)
static bool RegisterPlugin(llvm::StringRef name, llvm::StringRef description, ABICreateInstance create_callback)
static bool UnregisterPlugin(ABICreateInstance create_callback)
static FileSpec FindSymbolFileInBundle(const FileSpec &dsym_bundle_fspec, const UUID *uuid, const ArchSpec *arch)
A Progress indicator helper class.
const char * GetData() const
llvm::StringRef GetString() const
SymbolLocatorDebugSymbols()
static llvm::StringRef GetPluginNameStatic()
static std::optional< ModuleSpec > LocateExecutableObjectFile(const ModuleSpec &module_spec)
static lldb_private::SymbolLocator * CreateInstance()
static llvm::StringRef GetPluginDescriptionStatic()
static std::optional< FileSpec > LocateExecutableSymbolFile(const ModuleSpec &module_spec, const FileSpecList &default_search_paths)
static bool DownloadObjectAndSymbolFile(ModuleSpec &module_spec, Status &error, bool force_lookup, bool copy_executable)
static std::optional< FileSpec > FindSymbolFileInBundle(const FileSpec &dsym_bundle_fspec, const UUID *uuid, const ArchSpec *arch)
llvm::ArrayRef< uint8_t > GetBytes() const
std::string GetAsString(llvm::StringRef separator="-") const
#define UNUSED_IF_ASSERT_DISABLED(x)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
std::shared_ptr< lldb_private::Module > ModuleSP