Index: source/Host/common/Symbols.cpp =================================================================== --- source/Host/common/Symbols.cpp +++ source/Host/common/Symbols.cpp @@ -18,19 +18,331 @@ #include "lldb/Core/UUID.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Target/Target.h" +#include "lldb/Utility/SafeMachO.h" #include "llvm/Support/FileSystem.h" +// From MacOSX system header "mach/machine.h" +typedef int cpu_type_t; +typedef int cpu_subtype_t; + using namespace lldb; using namespace lldb_private; +using namespace llvm::MachO; + +bool +SkinnyMachOFileContainsArchAndUUID +( + const FileSpec &file_spec, + const ArchSpec *arch, + const lldb_private::UUID *uuid, // the UUID we are looking for + off_t file_offset, + DataExtractor& data, + lldb::offset_t data_offset, + const uint32_t magic +) +{ + assert(magic == MH_MAGIC || magic == MH_CIGAM || magic == MH_MAGIC_64 || magic == MH_CIGAM_64); + if (magic == MH_MAGIC || magic == MH_MAGIC_64) + data.SetByteOrder (lldb::endian::InlHostByteOrder()); + else if (lldb::endian::InlHostByteOrder() == eByteOrderBig) + data.SetByteOrder (eByteOrderLittle); + else + data.SetByteOrder (eByteOrderBig); + + uint32_t i; + const uint32_t cputype = data.GetU32(&data_offset); // cpu specifier + const uint32_t cpusubtype = data.GetU32(&data_offset); // machine specifier + data_offset+=4; // Skip mach file type + const uint32_t ncmds = data.GetU32(&data_offset); // number of load commands + const uint32_t sizeofcmds = data.GetU32(&data_offset); // the size of all the load commands + data_offset+=4; // Skip flags + + // Check the architecture if we have a valid arch pointer + if (arch) + { + ArchSpec file_arch(eArchTypeMachO, cputype, cpusubtype); + + if (!file_arch.IsCompatibleMatch(*arch)) + return false; + } -#if defined (__linux__) || defined (__FreeBSD__) + // The file exists, and if a valid arch pointer was passed in we know + // if already matches, so we can return if we aren't looking for a specific + // UUID + if (uuid == NULL) + return true; + + if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) + data_offset += 4; // Skip reserved field for in mach_header_64 + + // Make sure we have enough data for all the load commands + if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) + { + if (data.GetByteSize() < sizeof(struct mach_header_64) + sizeofcmds) + { + DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header_64) + sizeofcmds)); + data.SetData (data_buffer_sp); + } + } + else + { + if (data.GetByteSize() < sizeof(struct mach_header) + sizeofcmds) + { + DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header) + sizeofcmds)); + data.SetData (data_buffer_sp); + } + } + + for (i=0; iGetByteSize() > 0) + { + data.SetData(data_buffer_sp); + + lldb::offset_t data_offset = 0; + uint32_t magic = data.GetU32(&data_offset); + + switch (magic) + { + // 32 bit mach-o file + case MH_MAGIC: + case MH_CIGAM: + case MH_MAGIC_64: + case MH_CIGAM_64: + return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); + + // fat mach-o file + case FAT_MAGIC: + case FAT_CIGAM: + return UniversalMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); + + default: + break; + } + } + return false; +} + +#if defined(__APPLE__) + +// Forward declaration of method defined in source/Host/macosx/Symbols.cpp +int +LocateMacOSXFilesUsingDebugSymbols +( + const ModuleSpec &module_spec, + FileSpec *out_exec_fspec, // If non-NULL, try and find the executable + FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file +); + +#else + +int +LocateMacOSXFilesUsingDebugSymbols +( + const ModuleSpec &module_spec, + FileSpec *out_exec_fspec, // If non-NULL, try and find the executable + FileSpec *out_dsym_fspec // If non-NULL try and find the debug symbol file +) { + // Cannot find MacOSX files using debug symbols on non MacOSX. + return 0; +} + +#endif + +static bool +LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec) +{ + const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); + if (exec_fspec) + { + char path[PATH_MAX]; + if (exec_fspec->GetPath(path, sizeof(path))) + { + // Make sure the module isn't already just a dSYM file... + if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL) + { + size_t obj_file_path_length = strlen(path); + ::strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path) - strlen(path) - 1); + ::strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path) - strlen(path) - 1); + + dsym_fspec.SetFile(path, false); + + if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr())) + { + return true; + } + else + { + path[obj_file_path_length] = '\0'; + + char *last_dot = strrchr(path, '.'); + while (last_dot != NULL && last_dot[0]) + { + char *next_slash = strchr(last_dot, '/'); + if (next_slash != NULL) + { + *next_slash = '\0'; + ::strncat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path) - strlen(path) - 1); + ::strncat(path, exec_fspec->GetFilename().AsCString(), sizeof(path) - strlen(path) - 1); + dsym_fspec.SetFile(path, false); + if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr())) + return true; + else + { + *last_dot = '\0'; + char *prev_slash = strrchr(path, '/'); + if (prev_slash != NULL) + *prev_slash = '\0'; + else + break; + } + } + else + { + break; + } + } + } + } + } + } + dsym_fspec.Clear(); + return false; +} + +FileSpec +LocateExecutableSymbolFileDsym (const ModuleSpec &module_spec) +{ + const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); + const ArchSpec *arch = module_spec.GetArchitecturePtr(); + const UUID *uuid = module_spec.GetUUIDPtr(); + + Timer scoped_timer (__PRETTY_FUNCTION__, + "LocateExecutableSymbolFileDsym (file = %s, arch = %s, uuid = %p)", + exec_fspec ? exec_fspec->GetFilename().AsCString ("") : "", + arch ? arch->GetArchitectureName() : "", + uuid); + + FileSpec symbol_fspec; + // First try and find the dSYM in the same directory as the executable or in + // an appropriate parent directory + if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false) + { + // We failed to easily find the dSYM above, so use DebugSymbols + LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec); + } + return symbol_fspec; +} FileSpec Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec) { - // FIXME - return FileSpec(); + const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); + const ArchSpec *arch = module_spec.GetArchitecturePtr(); + const UUID *uuid = module_spec.GetUUIDPtr(); + Timer scoped_timer (__PRETTY_FUNCTION__, + "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)", + exec_fspec ? exec_fspec->GetFilename().AsCString ("") : "", + arch ? arch->GetArchitectureName() : "", + uuid); + + FileSpec objfile_fspec; + if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid)) + objfile_fspec = exec_fspec; + else + LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL); + return objfile_fspec; } FileSpec @@ -110,47 +422,17 @@ } } - return FileSpec(); -} - -FileSpec -Symbols::FindSymbolFileInBundle (const FileSpec& symfile_bundle, - const lldb_private::UUID *uuid, - const ArchSpec *arch) -{ - // FIXME - return FileSpec(); -} - -bool -Symbols::DownloadObjectAndSymbolFile (ModuleSpec &module_spec, bool force_lookup) -{ - // Fill in the module_spec.GetFileSpec() for the object file and/or the - // module_spec.GetSymbolFileSpec() for the debug symbols file. - return false; -} - -#elif !defined (__APPLE__) - -FileSpec -Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec) -{ - // FIXME - return FileSpec(); + return LocateExecutableSymbolFileDsym(module_spec); } -FileSpec -Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec) -{ - // FIXME - return FileSpec(); -} +#if !defined (__APPLE__) FileSpec Symbols::FindSymbolFileInBundle (const FileSpec& symfile_bundle, const lldb_private::UUID *uuid, const ArchSpec *arch) { + // FIXME return FileSpec(); } Index: source/Host/macosx/Symbols.cpp =================================================================== --- source/Host/macosx/Symbols.cpp +++ source/Host/macosx/Symbols.cpp @@ -36,7 +36,6 @@ #include "Host/macosx/cfcpp/CFCString.h" #include "mach/machine.h" - using namespace lldb; using namespace lldb_private; using namespace llvm::MachO; @@ -50,7 +49,8 @@ } #endif -static bool +// Forward declarations of functions declared in source/Host/common/Symbols.cpp +bool SkinnyMachOFileContainsArchAndUUID ( const FileSpec &file_spec, @@ -60,77 +60,7 @@ DataExtractor& data, lldb::offset_t data_offset, const uint32_t magic -) -{ - assert(magic == MH_MAGIC || magic == MH_CIGAM || magic == MH_MAGIC_64 || magic == MH_CIGAM_64); - if (magic == MH_MAGIC || magic == MH_MAGIC_64) - data.SetByteOrder (lldb::endian::InlHostByteOrder()); - else if (lldb::endian::InlHostByteOrder() == eByteOrderBig) - data.SetByteOrder (eByteOrderLittle); - else - data.SetByteOrder (eByteOrderBig); - - uint32_t i; - const uint32_t cputype = data.GetU32(&data_offset); // cpu specifier - const uint32_t cpusubtype = data.GetU32(&data_offset); // machine specifier - data_offset+=4; // Skip mach file type - const uint32_t ncmds = data.GetU32(&data_offset); // number of load commands - const uint32_t sizeofcmds = data.GetU32(&data_offset); // the size of all the load commands - data_offset+=4; // Skip flags - - // Check the architecture if we have a valid arch pointer - if (arch) - { - ArchSpec file_arch(eArchTypeMachO, cputype, cpusubtype); - - if (!file_arch.IsCompatibleMatch(*arch)) - return false; - } - - // The file exists, and if a valid arch pointer was passed in we know - // if already matches, so we can return if we aren't looking for a specific - // UUID - if (uuid == NULL) - return true; - - if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) - data_offset += 4; // Skip reserved field for in mach_header_64 - - // Make sure we have enough data for all the load commands - if (magic == MH_CIGAM_64 || magic == MH_MAGIC_64) - { - if (data.GetByteSize() < sizeof(struct mach_header_64) + sizeofcmds) - { - DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header_64) + sizeofcmds)); - data.SetData (data_buffer_sp); - } - } - else - { - if (data.GetByteSize() < sizeof(struct mach_header) + sizeofcmds) - { - DataBufferSP data_buffer_sp (file_spec.ReadFileContents (file_offset, sizeof(struct mach_header) + sizeofcmds)); - data.SetData (data_buffer_sp); - } - } - - for (i=0; iGetByteSize() > 0) - { - data.SetData(data_buffer_sp); - - lldb::offset_t data_offset = 0; - uint32_t magic = data.GetU32(&data_offset); - - switch (magic) - { - // 32 bit mach-o file - case MH_MAGIC: - case MH_CIGAM: - case MH_MAGIC_64: - case MH_CIGAM_64: - return SkinnyMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); - - // fat mach-o file - case FAT_MAGIC: - case FAT_CIGAM: - return UniversalMachOFileContainsArchAndUUID (file_spec, arch, uuid, file_offset, data, data_offset, magic); - - default: - break; - } - } - return false; -} - -FileSpec -Symbols::FindSymbolFileInBundle (const FileSpec& dsym_bundle_fspec, - const lldb_private::UUID *uuid, - const ArchSpec *arch) -{ - char path[PATH_MAX]; - - FileSpec dsym_fspec; - - if (dsym_bundle_fspec.GetPath(path, sizeof(path))) - { - ::strncat (path, "/Contents/Resources/DWARF", sizeof(path) - strlen(path) - 1); - - lldb_utility::CleanUp dirp (opendir(path), NULL, closedir); - if (dirp.is_valid()) - { - dsym_fspec.GetDirectory().SetCString(path); - struct dirent* dp; - while ((dp = readdir(dirp.get())) != NULL) - { - // Only search directories - if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) - { - if (dp->d_namlen == 1 && dp->d_name[0] == '.') - continue; +); - if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.') - continue; - } - - if (dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN) - { - dsym_fspec.GetFilename().SetCString(dp->d_name); - if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid)) - return dsym_fspec; - } - } - } - } - dsym_fspec.Clear(); - return dsym_fspec; -} - -static int +int LocateMacOSXFilesUsingDebugSymbols ( const ModuleSpec &module_spec, @@ -426,7 +229,7 @@ if (::CFURLGetFileSystemRepresentation (bundle_exe_url.get(), true, (UInt8*)path, sizeof(path)-1)) { FileSpec bundle_exe_file_spec (path, true); - + if (FileAtPathContainsArchAndUUID (bundle_exe_file_spec, arch, uuid)) { ++items_found; @@ -436,7 +239,7 @@ } } break; - + case FileSpec::eFileTypePipe: // Forget pipes case FileSpec::eFileTypeSocket: // We can't process socket files case FileSpec::eFileTypeInvalid: // File doesn't exist... @@ -466,112 +269,49 @@ return items_found; } -static bool -LocateDSYMInVincinityOfExecutable (const ModuleSpec &module_spec, FileSpec &dsym_fspec) +FileSpec +Symbols::FindSymbolFileInBundle (const FileSpec& dsym_bundle_fspec, + const lldb_private::UUID *uuid, + const ArchSpec *arch) { - const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); - if (exec_fspec) + char path[PATH_MAX]; + + FileSpec dsym_fspec; + + if (dsym_bundle_fspec.GetPath(path, sizeof(path))) { - char path[PATH_MAX]; - if (exec_fspec->GetPath(path, sizeof(path))) + ::strncat (path, "/Contents/Resources/DWARF", sizeof(path) - strlen(path) - 1); + + lldb_utility::CleanUp dirp (opendir(path), NULL, closedir); + if (dirp.is_valid()) { - // Make sure the module isn't already just a dSYM file... - if (strcasestr(path, ".dSYM/Contents/Resources/DWARF") == NULL) + dsym_fspec.GetDirectory().SetCString(path); + struct dirent* dp; + while ((dp = readdir(dirp.get())) != NULL) { - size_t obj_file_path_length = strlen(path); - strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); - strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); - - dsym_fspec.SetFile(path, false); - - if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr())) + // Only search directories + if (dp->d_type == DT_DIR || dp->d_type == DT_UNKNOWN) { - return true; + if (dp->d_namlen == 1 && dp->d_name[0] == '.') + continue; + + if (dp->d_namlen == 2 && dp->d_name[0] == '.' && dp->d_name[1] == '.') + continue; } - else - { - path[obj_file_path_length] = '\0'; - char *last_dot = strrchr(path, '.'); - while (last_dot != NULL && last_dot[0]) - { - char *next_slash = strchr(last_dot, '/'); - if (next_slash != NULL) - { - *next_slash = '\0'; - strlcat(path, ".dSYM/Contents/Resources/DWARF/", sizeof(path)); - strlcat(path, exec_fspec->GetFilename().AsCString(), sizeof(path)); - dsym_fspec.SetFile(path, false); - if (dsym_fspec.Exists() && FileAtPathContainsArchAndUUID (dsym_fspec, module_spec.GetArchitecturePtr(), module_spec.GetUUIDPtr())) - return true; - else - { - *last_dot = '\0'; - char *prev_slash = strrchr(path, '/'); - if (prev_slash != NULL) - *prev_slash = '\0'; - else - break; - } - } - else - { - break; - } - } + if (dp->d_type == DT_REG || dp->d_type == DT_UNKNOWN) + { + dsym_fspec.GetFilename().SetCString(dp->d_name); + if (FileAtPathContainsArchAndUUID (dsym_fspec, arch, uuid)) + return dsym_fspec; } } } } dsym_fspec.Clear(); - return false; -} - -FileSpec -Symbols::LocateExecutableObjectFile (const ModuleSpec &module_spec) -{ - const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); - const ArchSpec *arch = module_spec.GetArchitecturePtr(); - const UUID *uuid = module_spec.GetUUIDPtr(); - Timer scoped_timer (__PRETTY_FUNCTION__, - "LocateExecutableObjectFile (file = %s, arch = %s, uuid = %p)", - exec_fspec ? exec_fspec->GetFilename().AsCString ("") : "", - arch ? arch->GetArchitectureName() : "", - uuid); - - FileSpec objfile_fspec; - if (exec_fspec && FileAtPathContainsArchAndUUID (exec_fspec, arch, uuid)) - objfile_fspec = exec_fspec; - else - LocateMacOSXFilesUsingDebugSymbols (module_spec, &objfile_fspec, NULL); - return objfile_fspec; -} - -FileSpec -Symbols::LocateExecutableSymbolFile (const ModuleSpec &module_spec) -{ - const FileSpec *exec_fspec = module_spec.GetFileSpecPtr(); - const ArchSpec *arch = module_spec.GetArchitecturePtr(); - const UUID *uuid = module_spec.GetUUIDPtr(); - - Timer scoped_timer (__PRETTY_FUNCTION__, - "LocateExecutableSymbolFile (file = %s, arch = %s, uuid = %p)", - exec_fspec ? exec_fspec->GetFilename().AsCString ("") : "", - arch ? arch->GetArchitectureName() : "", - uuid); - - FileSpec symbol_fspec; - // First try and find the dSYM in the same directory as the executable or in - // an appropriate parent directory - if (LocateDSYMInVincinityOfExecutable (module_spec, symbol_fspec) == false) - { - // We failed to easily find the dSYM above, so use DebugSymbols - LocateMacOSXFilesUsingDebugSymbols (module_spec, NULL, &symbol_fspec); - } - return symbol_fspec; + return dsym_fspec; } - static bool GetModuleSpecInfoFromUUIDDictionary (CFDictionaryRef uuid_dict, ModuleSpec &module_spec) {