JDevlieghere created this revision. JDevlieghere added reviewers: labath, zturner, davide. JDevlieghere added a project: LLDB.
This moves construction of data buffers into the FileSystem class. Like some of the previous refactorings we don't translate the path yet because the functionality hasn't been landed in LLVM yet. Repository: rLLDB LLDB https://reviews.llvm.org/D54272 Files: include/lldb/Host/FileSystem.h include/lldb/Utility/DataBufferLLVM.h source/API/SBSection.cpp source/Commands/CommandObjectMemory.cpp source/Core/SourceManager.cpp source/Host/common/FileSystem.cpp source/Host/common/Host.cpp source/Host/linux/Host.cpp source/Interpreter/OptionValueFileSpec.cpp source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp source/Plugins/Platform/MacOSX/PlatformDarwin.cpp source/Plugins/Process/elf-core/ProcessElfCore.cpp source/Plugins/Process/mach-core/ProcessMachCore.cpp source/Plugins/Process/minidump/ProcessMinidump.cpp source/Symbol/ObjectFile.cpp source/Utility/DataBufferLLVM.cpp unittests/Process/minidump/MinidumpParserTest.cpp
Index: unittests/Process/minidump/MinidumpParserTest.cpp =================================================================== --- unittests/Process/minidump/MinidumpParserTest.cpp +++ unittests/Process/minidump/MinidumpParserTest.cpp @@ -18,7 +18,6 @@ #include "TestingSupport/TestUtilities.h" #include "lldb/Target/MemoryRegionInfo.h" #include "lldb/Utility/ArchSpec.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/FileSpec.h" #include "llvm/ADT/ArrayRef.h" @@ -40,7 +39,8 @@ public: void SetUpData(const char *minidump_filename) { std::string filename = GetInputFilePath(minidump_filename); - auto BufferPtr = DataBufferLLVM::CreateSliceFromPath(filename, -1, 0); + auto BufferPtr = + FileSystem::Instance().CreateDataBufferFromPath(filename, -1, 0); ASSERT_NE(BufferPtr, nullptr); llvm::Optional<MinidumpParser> optional_parser = MinidumpParser::Create(BufferPtr); @@ -54,7 +54,7 @@ void InvalidMinidump(const char *minidump_filename, uint64_t load_size) { std::string filename = GetInputFilePath(minidump_filename); auto BufferPtr = - DataBufferLLVM::CreateSliceFromPath(filename, load_size, 0); + FileSystem::Instance().CreateDataBufferFromPath(filename, load_size, 0); ASSERT_NE(BufferPtr, nullptr); llvm::Optional<MinidumpParser> optional_parser = Index: source/Utility/DataBufferLLVM.cpp =================================================================== --- source/Utility/DataBufferLLVM.cpp +++ source/Utility/DataBufferLLVM.cpp @@ -27,34 +27,6 @@ DataBufferLLVM::~DataBufferLLVM() {} -std::shared_ptr<DataBufferLLVM> -DataBufferLLVM::CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size, - uint64_t Offset) { - // If the file resides non-locally, pass the volatile flag so that we don't - // mmap it. - bool IsVolatile = !llvm::sys::fs::is_local(Path); - - auto Buffer = - llvm::WritableMemoryBuffer::getFileSlice(Path, Size, Offset, IsVolatile); - if (!Buffer) - return nullptr; - return std::shared_ptr<DataBufferLLVM>( - new DataBufferLLVM(std::move(*Buffer))); -} - -std::shared_ptr<DataBufferLLVM> -DataBufferLLVM::CreateFromPath(const llvm::Twine &Path) { - // If the file resides non-locally, pass the volatile flag so that we don't - // mmap it. - bool IsVolatile = !llvm::sys::fs::is_local(Path); - - auto Buffer = llvm::WritableMemoryBuffer::getFile(Path, -1, IsVolatile); - if (!Buffer) - return nullptr; - return std::shared_ptr<DataBufferLLVM>( - new DataBufferLLVM(std::move(*Buffer))); -} - uint8_t *DataBufferLLVM::GetBytes() { return reinterpret_cast<uint8_t *>(Buffer->getBufferStart()); } Index: source/Symbol/ObjectFile.cpp =================================================================== --- source/Symbol/ObjectFile.cpp +++ source/Symbol/ObjectFile.cpp @@ -19,7 +19,6 @@ #include "lldb/Target/Target.h" #include "lldb/Utility/DataBuffer.h" #include "lldb/Utility/DataBufferHeap.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/RegularExpression.h" #include "lldb/Utility/Timer.h" @@ -75,8 +74,8 @@ // container plug-ins can use these bytes to see if they can parse this // file. if (file_size > 0) { - data_sp = - DataBufferLLVM::CreateSliceFromPath(file->GetPath(), 512, file_offset); + data_sp = FileSystem::Instance().CreateDataBufferFromPath( + file->GetPath(), 512, file_offset); data_offset = 0; } } @@ -120,8 +119,8 @@ } // We failed to find any cached object files in the container plug- // ins, so lets read the first 512 bytes and try again below... - data_sp = DataBufferLLVM::CreateSliceFromPath(archive_file.GetPath(), - 512, file_offset); + data_sp = FileSystem::Instance().CreateDataBufferFromPath( + archive_file.GetPath(), 512, file_offset); } } } @@ -209,7 +208,8 @@ lldb::offset_t file_offset, lldb::offset_t file_size, ModuleSpecList &specs) { - DataBufferSP data_sp = DataBufferLLVM::CreateSliceFromPath(file.GetPath(), 512, file_offset); + DataBufferSP data_sp = FileSystem::Instance().CreateDataBufferFromPath( + file.GetPath(), 512, file_offset); if (data_sp) { if (file_size == 0) { const lldb::offset_t actual_file_size = @@ -682,5 +682,6 @@ DataBufferSP ObjectFile::MapFileData(const FileSpec &file, uint64_t Size, uint64_t Offset) { - return DataBufferLLVM::CreateSliceFromPath(file.GetPath(), Size, Offset); + return FileSystem::Instance().CreateDataBufferFromPath(file.GetPath(), Size, + Offset); } Index: source/Plugins/Process/minidump/ProcessMinidump.cpp =================================================================== --- source/Plugins/Process/minidump/ProcessMinidump.cpp +++ source/Plugins/Process/minidump/ProcessMinidump.cpp @@ -21,7 +21,6 @@ #include "lldb/Target/SectionLoadList.h" #include "lldb/Target/Target.h" #include "lldb/Target/UnixSignals.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/LLDBAssert.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/State.h" @@ -101,8 +100,8 @@ lldb::ProcessSP process_sp; // Read enough data for the Minidump header constexpr size_t header_size = sizeof(MinidumpHeader); - auto DataPtr = - DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0); + auto DataPtr = FileSystem::Instance().CreateDataBufferFromPath( + crash_file->GetPath(), header_size, 0); if (!DataPtr) return nullptr; @@ -114,7 +113,8 @@ if (header == nullptr) return nullptr; - auto AllData = DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), -1, 0); + auto AllData = FileSystem::Instance().CreateDataBufferFromPath( + crash_file->GetPath(), -1, 0); if (!AllData) return nullptr; Index: source/Plugins/Process/mach-core/ProcessMachCore.cpp =================================================================== --- source/Plugins/Process/mach-core/ProcessMachCore.cpp +++ source/Plugins/Process/mach-core/ProcessMachCore.cpp @@ -29,7 +29,6 @@ #include "lldb/Target/Target.h" #include "lldb/Target/Thread.h" #include "lldb/Utility/DataBuffer.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/State.h" @@ -67,8 +66,8 @@ lldb::ProcessSP process_sp; if (crash_file) { const size_t header_size = sizeof(llvm::MachO::mach_header); - auto data_sp = - DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0); + auto data_sp = FileSystem::Instance().CreateDataBufferFromPath( + crash_file->GetPath(), header_size, 0); if (data_sp && data_sp->GetByteSize() == header_size) { DataExtractor data(data_sp, lldb::eByteOrderLittle, 4); Index: source/Plugins/Process/elf-core/ProcessElfCore.cpp =================================================================== --- source/Plugins/Process/elf-core/ProcessElfCore.cpp +++ source/Plugins/Process/elf-core/ProcessElfCore.cpp @@ -23,7 +23,6 @@ #include "lldb/Target/Target.h" #include "lldb/Target/UnixSignals.h" #include "lldb/Utility/DataBufferHeap.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/State.h" @@ -61,8 +60,8 @@ // the header extension. const size_t header_size = sizeof(llvm::ELF::Elf64_Ehdr); - auto data_sp = DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), - header_size, 0); + auto data_sp = FileSystem::Instance().CreateDataBufferFromPath( + crash_file->GetPath(), header_size, 0); if (data_sp && data_sp->GetByteSize() == header_size && elf::ELFHeader::MagicBytesMatch(data_sp->GetBytes())) { elf::ELFHeader elf_header; Index: source/Plugins/Platform/MacOSX/PlatformDarwin.cpp =================================================================== --- source/Plugins/Platform/MacOSX/PlatformDarwin.cpp +++ source/Plugins/Platform/MacOSX/PlatformDarwin.cpp @@ -33,7 +33,6 @@ #include "lldb/Target/Platform.h" #include "lldb/Target/Process.h" #include "lldb/Target/Target.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Status.h" #include "lldb/Utility/Timer.h" @@ -1169,8 +1168,8 @@ xcode_dir_path.append(xcode_select_prefix_dir); xcode_dir_path.append("/usr/share/xcode-select/xcode_dir_path"); temp_file_spec.SetFile(xcode_dir_path, FileSpec::Style::native); - auto dir_buffer = - DataBufferLLVM::CreateFromPath(temp_file_spec.GetPath()); + auto dir_buffer = FileSystem::Instance().CreateDataBufferFromPath( + temp_file_spec.GetPath()); if (dir_buffer && dir_buffer->GetByteSize() > 0) { llvm::StringRef path_ref(dir_buffer->GetChars()); // Trim tailing newlines and make sure there is enough room for a null Index: source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp =================================================================== --- source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp +++ source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp @@ -33,7 +33,6 @@ #include "lldb/Host/FileSystem.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Utility/ArchSpec.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/Timer.h" @@ -313,7 +312,8 @@ // file gets updated by a new build while this .a file is being used for // debugging DataBufferSP archive_data_sp = - DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset); + FileSystem::Instance().CreateDataBufferFromPath(*file, length, + file_offset); if (!archive_data_sp) return nullptr; @@ -467,8 +467,8 @@ bool set_archive_arch = false; if (!archive_sp) { set_archive_arch = true; - data_sp = - DataBufferLLVM::CreateSliceFromPath(file.GetPath(), file_size, file_offset); + data_sp = FileSystem::Instance().CreateDataBufferFromPath(file, file_size, + file_offset); if (data_sp) { data.SetData(data_sp, 0, data_sp->GetByteSize()); archive_sp = Archive::ParseAndCacheArchiveForFile( Index: source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp =================================================================== --- source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp +++ source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp @@ -40,7 +40,6 @@ #include "lldb/Target/Thread.h" #include "lldb/Utility/Args.h" #include "lldb/Utility/ConstString.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/RegisterValue.h" #include "lldb/Utility/RegularExpression.h" @@ -2544,7 +2543,8 @@ } // Read file into data buffer - auto data_sp = DataBufferLLVM::CreateFromPath(file.GetPath()); + auto data_sp = + FileSystem::Instance().CreateDataBufferFromPath(file.GetPath()); // Cast start of buffer to FileHeader and use pointer to read metadata void *file_buf = data_sp->GetBytes(); @@ -3085,7 +3085,8 @@ const addr_t size = info_sym->GetByteSize(); const FileSpec fs = m_module->GetFileSpec(); - auto buffer = DataBufferLLVM::CreateSliceFromPath(fs.GetPath(), size, addr); + auto buffer = + FileSystem::Instance().CreateDataBufferFromPath(fs.GetPath(), size, addr); if (!buffer) return false; Index: source/Interpreter/OptionValueFileSpec.cpp =================================================================== --- source/Interpreter/OptionValueFileSpec.cpp +++ source/Interpreter/OptionValueFileSpec.cpp @@ -14,7 +14,6 @@ #include "lldb/Interpreter/CommandCompletions.h" #include "lldb/Interpreter/CommandInterpreter.h" #include "lldb/Utility/Args.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/State.h" using namespace lldb; @@ -114,7 +113,8 @@ const auto file_mod_time = FileSystem::Instance().GetModificationTime(m_current_value); if (m_data_sp && m_data_mod_time == file_mod_time) return m_data_sp; - m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath()); + m_data_sp = FileSystem::Instance().CreateDataBufferFromPath( + m_current_value.GetPath()); m_data_mod_time = file_mod_time; } return m_data_sp; Index: source/Host/linux/Host.cpp =================================================================== --- source/Host/linux/Host.cpp +++ source/Host/linux/Host.cpp @@ -31,7 +31,6 @@ #include "lldb/Host/Host.h" #include "lldb/Host/HostInfo.h" #include "lldb/Host/linux/Support.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/DataExtractor.h" using namespace lldb; @@ -125,7 +124,8 @@ static ArchSpec GetELFProcessCPUType(llvm::StringRef exe_path) { Log *log = GetLogIfAllCategoriesSet(LIBLLDB_LOG_HOST); - auto buffer_sp = DataBufferLLVM::CreateSliceFromPath(exe_path, 0x20, 0); + auto buffer_sp = + FileSystem::Instance().CreateDataBufferFromPath(exe_path, 0x20, 0); if (!buffer_sp) return ArchSpec(); Index: source/Host/common/Host.cpp =================================================================== --- source/Host/common/Host.cpp +++ source/Host/common/Host.cpp @@ -564,8 +564,8 @@ error.SetErrorStringWithFormat( "shell command output is too large to fit into a std::string"); } else { - auto Buffer = - DataBufferLLVM::CreateFromPath(output_file_spec.GetPath()); + auto Buffer = FileSystem::Instance().CreateDataBufferFromPath( + output_file_spec); if (error.Success()) command_output_ptr->assign(Buffer->GetChars(), Buffer->GetByteSize()); Index: source/Host/common/FileSystem.cpp =================================================================== --- source/Host/common/FileSystem.cpp +++ source/Host/common/FileSystem.cpp @@ -135,6 +135,16 @@ return IsDirectory(file_spec.GetPath()); } +bool FileSystem::IsLocal(const Twine &path) const { + bool b; + m_fs->isLocal(path, b); + return b; +} + +bool FileSystem::IsLocal(const FileSpec &file_spec) const { + return IsLocal(file_spec.GetPath()); +} + void FileSystem::EnumerateDirectory(Twine path, bool find_directories, bool find_files, bool find_other, EnumerateDirectoryCallbackType callback, @@ -218,6 +228,34 @@ file_spec.SetIsResolved(true); } +std::shared_ptr<DataBufferLLVM> +FileSystem::CreateDataBufferFromPath(const llvm::Twine &path, uint64_t size, + uint64_t offset) { + const bool is_volatile = !IsLocal(path); + + std::unique_ptr<llvm::WritableMemoryBuffer> buffer; + if (size == 0) { + auto buffer_or_error = + llvm::WritableMemoryBuffer::getFile(path, -1, is_volatile); + if (!buffer_or_error) + return nullptr; + buffer = std::move(*buffer_or_error); + } else { + auto buffer_or_error = llvm::WritableMemoryBuffer::getFileSlice( + path, size, offset, is_volatile); + if (!buffer_or_error) + return nullptr; + buffer = std::move(*buffer_or_error); + } + return std::shared_ptr<DataBufferLLVM>(new DataBufferLLVM(std::move(buffer))); +} + +std::shared_ptr<DataBufferLLVM> +FileSystem::CreateDataBufferFromPath(const FileSpec &file_spec, uint64_t size, + uint64_t offset) { + return CreateDataBufferFromPath(file_spec.GetPath(), size, offset); +} + bool FileSystem::ResolveExecutableLocation(FileSpec &file_spec) { // If the directory is set there's nothing to do. const ConstString &directory = file_spec.GetDirectory(); Index: source/Core/SourceManager.cpp =================================================================== --- source/Core/SourceManager.cpp +++ source/Core/SourceManager.cpp @@ -442,7 +442,7 @@ } if (m_mod_time != llvm::sys::TimePoint<>()) - m_data_sp = DataBufferLLVM::CreateFromPath(m_file_spec.GetPath()); + m_data_sp = FileSystem::Instance().CreateDataBufferFromPath(m_file_spec); } uint32_t SourceManager::File::GetLineOffset(uint32_t line) { @@ -520,7 +520,7 @@ if (curr_mod_time != llvm::sys::TimePoint<>() && m_mod_time != curr_mod_time) { m_mod_time = curr_mod_time; - m_data_sp = DataBufferLLVM::CreateFromPath(m_file_spec.GetPath()); + m_data_sp = FileSystem::Instance().CreateDataBufferFromPath(m_file_spec); m_offsets.clear(); } } Index: source/Commands/CommandObjectMemory.cpp =================================================================== --- source/Commands/CommandObjectMemory.cpp +++ source/Commands/CommandObjectMemory.cpp @@ -1361,7 +1361,7 @@ size_t length = SIZE_MAX; if (item_byte_size > 1) length = item_byte_size; - auto data_sp = DataBufferLLVM::CreateSliceFromPath( + auto data_sp = FileSystem::Instance().CreateDataBufferFromPath( m_memory_options.m_infile.GetPath(), length, m_memory_options.m_infile_offset); if (data_sp) { Index: source/API/SBSection.cpp =================================================================== --- source/API/SBSection.cpp +++ source/API/SBSection.cpp @@ -14,7 +14,6 @@ #include "lldb/Core/Section.h" #include "lldb/Symbol/ObjectFile.h" #include "lldb/Utility/DataBuffer.h" -#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/DataExtractor.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/StreamString.h" @@ -166,7 +165,7 @@ else file_size = 0; } - auto data_buffer_sp = DataBufferLLVM::CreateSliceFromPath( + auto data_buffer_sp = FileSystem::Instance().CreateDataBufferFromPath( objfile->GetFileSpec().GetPath(), file_size, file_offset); if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) { DataExtractorSP data_extractor_sp( Index: include/lldb/Utility/DataBufferLLVM.h =================================================================== --- include/lldb/Utility/DataBufferLLVM.h +++ include/lldb/Utility/DataBufferLLVM.h @@ -23,23 +23,19 @@ namespace lldb_private { +class FileSystem; class DataBufferLLVM : public DataBuffer { public: ~DataBufferLLVM(); - static std::shared_ptr<DataBufferLLVM> - CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size, uint64_t Offset); - - static std::shared_ptr<DataBufferLLVM> - CreateFromPath(const llvm::Twine &Path); - uint8_t *GetBytes() override; const uint8_t *GetBytes() const override; lldb::offset_t GetByteSize() const override; char *GetChars() { return reinterpret_cast<char *>(GetBytes()); } private: + friend FileSystem; /// Construct a DataBufferLLVM from \p Buffer. \p Buffer must be a valid /// pointer. explicit DataBufferLLVM(std::unique_ptr<llvm::WritableMemoryBuffer> Buffer); Index: include/lldb/Host/FileSystem.h =================================================================== --- include/lldb/Host/FileSystem.h +++ include/lldb/Host/FileSystem.h @@ -11,6 +11,7 @@ #define liblldb_Host_FileSystem_h #include "lldb/Host/File.h" +#include "lldb/Utility/DataBufferLLVM.h" #include "lldb/Utility/FileSpec.h" #include "lldb/Utility/Status.h" @@ -94,6 +95,12 @@ bool IsDirectory(const llvm::Twine &path) const; /// @} + /// Returns whether the given path is local to the file system. + /// @{ + bool IsLocal(const FileSpec &file_spec) const; + bool IsLocal(const llvm::Twine &path) const; + /// @} + /// Make the given file path absolute. /// @{ std::error_code MakeAbsolute(llvm::SmallVectorImpl<char> &path) const; @@ -106,6 +113,16 @@ void Resolve(FileSpec &file_spec); /// @} + //// Create memory buffer from path. + /// @{ + std::shared_ptr<DataBufferLLVM> + CreateDataBufferFromPath(const llvm::Twine &path, uint64_t size = 0, + uint64_t offset = 0); + std::shared_ptr<DataBufferLLVM> + CreateDataBufferFromPath(const FileSpec &file_spec, uint64_t size = 0, + uint64_t offset = 0); + /// @} + /// Call into the Host to see if it can help find the file. bool ResolveExecutableLocation(FileSpec &file_spec);
_______________________________________________ lldb-commits mailing list lldb-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits