summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZachary Turner <zturner@google.com>2017-03-06 23:42:14 +0000
committerZachary Turner <zturner@google.com>2017-03-06 23:42:14 +0000
commit7f6a7a37521e3610be7242ccec7ca9a15c1c307a (patch)
treee2b7f59e0254269500347dfbd067108d86ca70a0
parent15492af547112d272572994bfef2d560ce651d7b (diff)
downloadbcm5719-llvm-7f6a7a37521e3610be7242ccec7ca9a15c1c307a.tar.gz
bcm5719-llvm-7f6a7a37521e3610be7242ccec7ca9a15c1c307a.zip
Remove FileSpec::ReadFileContents.
This functionality is subsumed by DataBufferLLVM, which is also more efficient since it will try to mmap. However, we don't yet support mmaping writable private sections, and in some cases we were using ReadFileContents and then modifying the buffer. To address that I've added a flag to the DataBufferLLVM methods that allow you to map privately, which disables the mmaping path entirely. Eventually we should teach DataBufferLLVM to use mmap with writable private, but that is orthogonal to this effort. Differential Revision: https://reviews.llvm.org/D30622 llvm-svn: 297095
-rw-r--r--lldb/include/lldb/Host/FileSpec.h49
-rw-r--r--lldb/include/lldb/Utility/DataBufferLLVM.h7
-rw-r--r--lldb/source/API/SBSection.cpp5
-rw-r--r--lldb/source/Commands/CommandObjectMemory.cpp6
-rw-r--r--lldb/source/Core/SourceManager.cpp5
-rw-r--r--lldb/source/Host/common/FileSpec.cpp76
-rw-r--r--lldb/source/Host/common/Host.cpp9
-rw-r--r--lldb/source/Interpreter/OptionValueFileSpec.cpp7
-rw-r--r--lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp5
-rw-r--r--lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp4
-rw-r--r--lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp14
-rw-r--r--lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp19
-rw-r--r--lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp9
-rw-r--r--lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp18
-rw-r--r--lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp4
-rw-r--r--lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp4
-rw-r--r--lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp4
-rw-r--r--lldb/source/Symbol/ObjectFile.cpp10
-rw-r--r--lldb/source/Utility/DataBufferLLVM.cpp23
-rw-r--r--lldb/unittests/Process/minidump/MinidumpParserTest.cpp2
20 files changed, 97 insertions, 183 deletions
diff --git a/lldb/include/lldb/Host/FileSpec.h b/lldb/include/lldb/Host/FileSpec.h
index 4eaa22b8532..f7123a016cf 100644
--- a/lldb/include/lldb/Host/FileSpec.h
+++ b/lldb/include/lldb/Host/FileSpec.h
@@ -498,55 +498,6 @@ public:
size_t MemorySize() const;
//------------------------------------------------------------------
- /// Read part of, or the entire contents of, a file into a heap based data
- /// buffer.
- ///
- /// Returns a shared pointer to a data buffer that contains all or
- /// part of the contents of a file. The data copies into a heap based
- /// buffer that lives in the DataBuffer shared pointer object returned.
- /// The data that is cached will start \a offset bytes into the
- /// file, and \a length bytes will be mapped. If \a length is
- /// greater than the number of bytes available in the file starting
- /// at \a offset, the number of bytes will be appropriately
- /// truncated. The final number of bytes that get mapped can be
- /// verified using the DataBuffer::GetByteSize() function.
- ///
- /// @param[in] offset
- /// The offset in bytes from the beginning of the file where
- /// memory mapping should begin.
- ///
- /// @param[in] length
- /// The size in bytes that should be mapped starting \a offset
- /// bytes into the file. If \a length is \c SIZE_MAX, map
- /// as many bytes as possible.
- ///
- /// @return
- /// A shared pointer to the memory mapped data. This shared
- /// pointer can contain a nullptr DataBuffer pointer, so the contained
- /// pointer must be checked prior to using it.
- //------------------------------------------------------------------
- lldb::DataBufferSP ReadFileContents(off_t offset = 0,
- size_t length = SIZE_MAX,
- Error *error_ptr = nullptr) const;
-
- size_t ReadFileContents(off_t file_offset, void *dst, size_t dst_len,
- Error *error_ptr) const;
-
- //------------------------------------------------------------------
- /// Read the entire contents of a file as data that can be used
- /// as a C string.
- ///
- /// Read the entire contents of a file and ensure that the data
- /// is NULL terminated so it can be used as a C string.
- ///
- /// @return
- /// A shared pointer to the data. This shared pointer can
- /// contain a nullptr DataBuffer pointer, so the contained pointer
- /// must be checked prior to using it.
- //------------------------------------------------------------------
- lldb::DataBufferSP ReadFileContentsAsCString(Error *error_ptr = nullptr);
-
- //------------------------------------------------------------------
/// Normalize a pathname by collapsing redundant separators and
/// up-level references.
//------------------------------------------------------------------
diff --git a/lldb/include/lldb/Utility/DataBufferLLVM.h b/lldb/include/lldb/Utility/DataBufferLLVM.h
index 928c49220a3..fb6cd26f6ad 100644
--- a/lldb/include/lldb/Utility/DataBufferLLVM.h
+++ b/lldb/include/lldb/Utility/DataBufferLLVM.h
@@ -26,12 +26,17 @@ public:
~DataBufferLLVM();
static std::shared_ptr<DataBufferLLVM>
- CreateFromPath(const llvm::Twine &Path, uint64_t Size, uint64_t Offset);
+ CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size, uint64_t Offset, bool Private = false);
+
+ static std::shared_ptr<DataBufferLLVM>
+ CreateFromPath(const llvm::Twine &Path, bool NullTerminate = false, bool Private = false);
uint8_t *GetBytes() override;
const uint8_t *GetBytes() const override;
lldb::offset_t GetByteSize() const override;
+ char *GetChars() { return reinterpret_cast<char *>(GetBytes()); }
+
private:
/// \brief Construct a DataBufferLLVM from \p Buffer. \p Buffer must be a
/// valid pointer.
diff --git a/lldb/source/API/SBSection.cpp b/lldb/source/API/SBSection.cpp
index 2850ce1172b..9da5d170da9 100644
--- a/lldb/source/API/SBSection.cpp
+++ b/lldb/source/API/SBSection.cpp
@@ -14,6 +14,7 @@
#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"
@@ -165,8 +166,8 @@ SBData SBSection::GetSectionData(uint64_t offset, uint64_t size) {
else
file_size = 0;
}
- DataBufferSP data_buffer_sp(
- objfile->GetFileSpec().ReadFileContents(file_offset, file_size));
+ auto data_buffer_sp = DataBufferLLVM::CreateSliceFromPath(
+ objfile->GetFileSpec().GetPath(), file_size, file_offset);
if (data_buffer_sp && data_buffer_sp->GetByteSize() > 0) {
DataExtractorSP data_extractor_sp(
new DataExtractor(data_buffer_sp, objfile->GetByteOrder(),
diff --git a/lldb/source/Commands/CommandObjectMemory.cpp b/lldb/source/Commands/CommandObjectMemory.cpp
index 2f3e15a95ee..8b30189559b 100644
--- a/lldb/source/Commands/CommandObjectMemory.cpp
+++ b/lldb/source/Commands/CommandObjectMemory.cpp
@@ -41,6 +41,7 @@
#include "lldb/Target/StackFrame.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/DataBufferHeap.h"
+#include "lldb/Utility/DataBufferLLVM.h"
#include "lldb/Utility/StreamString.h"
#include "lldb/lldb-private.h"
@@ -1358,8 +1359,9 @@ protected:
size_t length = SIZE_MAX;
if (item_byte_size > 1)
length = item_byte_size;
- lldb::DataBufferSP data_sp(m_memory_options.m_infile.ReadFileContents(
- m_memory_options.m_infile_offset, length));
+ auto data_sp = DataBufferLLVM::CreateSliceFromPath(
+ m_memory_options.m_infile.GetPath(), length,
+ m_memory_options.m_infile_offset);
if (data_sp) {
length = data_sp->GetByteSize();
if (length > 0) {
diff --git a/lldb/source/Core/SourceManager.cpp b/lldb/source/Core/SourceManager.cpp
index d4101ab7f43..3a7e4e969ba 100644
--- a/lldb/source/Core/SourceManager.cpp
+++ b/lldb/source/Core/SourceManager.cpp
@@ -22,6 +22,7 @@
#include "lldb/Target/Target.h"
#include "lldb/Utility/AnsiTerminal.h"
#include "lldb/Utility/DataBuffer.h"
+#include "lldb/Utility/DataBufferLLVM.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Stream.h"
@@ -404,7 +405,7 @@ void SourceManager::File::CommonInitializer(const FileSpec &file_spec,
}
if (m_mod_time != llvm::sys::TimePoint<>())
- m_data_sp = m_file_spec.ReadFileContents();
+ m_data_sp = DataBufferLLVM::CreateFromPath(m_file_spec.GetPath());
}
uint32_t SourceManager::File::GetLineOffset(uint32_t line) {
@@ -482,7 +483,7 @@ void SourceManager::File::UpdateIfNeeded() {
if (curr_mod_time != llvm::sys::TimePoint<>() &&
m_mod_time != curr_mod_time) {
m_mod_time = curr_mod_time;
- m_data_sp = m_file_spec.ReadFileContents();
+ m_data_sp = DataBufferLLVM::CreateFromPath(m_file_spec.GetPath());
m_offsets.clear();
}
}
diff --git a/lldb/source/Host/common/FileSpec.cpp b/lldb/source/Host/common/FileSpec.cpp
index 67fbb0b9bbd..4867592db58 100644
--- a/lldb/source/Host/common/FileSpec.cpp
+++ b/lldb/source/Host/common/FileSpec.cpp
@@ -26,12 +26,9 @@
#endif
#include "lldb/Core/ArchSpec.h"
-#include "lldb/Host/File.h"
#include "lldb/Host/FileSpec.h"
#include "lldb/Host/FileSystem.h"
-#include "lldb/Host/Host.h"
#include "lldb/Utility/CleanUp.h"
-#include "lldb/Utility/DataBufferHeap.h"
#include "lldb/Utility/RegularExpression.h"
#include "lldb/Utility/Stream.h"
#include "lldb/Utility/StreamString.h"
@@ -833,79 +830,6 @@ size_t FileSpec::MemorySize() const {
return m_filename.MemorySize() + m_directory.MemorySize();
}
-size_t FileSpec::ReadFileContents(off_t file_offset, void *dst, size_t dst_len,
- Error *error_ptr) const {
- Error error;
- size_t bytes_read = 0;
- char resolved_path[PATH_MAX];
- if (GetPath(resolved_path, sizeof(resolved_path))) {
- File file;
- error = file.Open(resolved_path, File::eOpenOptionRead);
- if (error.Success()) {
- off_t file_offset_after_seek = file_offset;
- bytes_read = dst_len;
- error = file.Read(dst, bytes_read, file_offset_after_seek);
- }
- } else {
- error.SetErrorString("invalid file specification");
- }
- if (error_ptr)
- *error_ptr = error;
- return bytes_read;
-}
-
-//------------------------------------------------------------------
-// Returns a shared pointer to a data buffer that contains all or
-// part of the contents of a file. The data copies into a heap based
-// buffer that lives in the DataBuffer shared pointer object returned.
-// The data that is cached will start "file_offset" bytes into the
-// file, and "file_size" bytes will be mapped. If "file_size" is
-// greater than the number of bytes available in the file starting
-// at "file_offset", the number of bytes will be appropriately
-// truncated. The final number of bytes that get mapped can be
-// verified using the DataBuffer::GetByteSize() function.
-//------------------------------------------------------------------
-DataBufferSP FileSpec::ReadFileContents(off_t file_offset, size_t file_size,
- Error *error_ptr) const {
- Error error;
- DataBufferSP data_sp;
- char resolved_path[PATH_MAX];
- if (GetPath(resolved_path, sizeof(resolved_path))) {
- File file;
- error = file.Open(resolved_path, File::eOpenOptionRead);
- if (error.Success()) {
- const bool null_terminate = false;
- error = file.Read(file_size, file_offset, null_terminate, data_sp);
- }
- } else {
- error.SetErrorString("invalid file specification");
- }
- if (error_ptr)
- *error_ptr = error;
- return data_sp;
-}
-
-DataBufferSP FileSpec::ReadFileContentsAsCString(Error *error_ptr) {
- Error error;
- DataBufferSP data_sp;
- char resolved_path[PATH_MAX];
- if (GetPath(resolved_path, sizeof(resolved_path))) {
- File file;
- error = file.Open(resolved_path, File::eOpenOptionRead);
- if (error.Success()) {
- off_t offset = 0;
- size_t length = SIZE_MAX;
- const bool null_terminate = true;
- error = file.Read(length, offset, null_terminate, data_sp);
- }
- } else {
- error.SetErrorString("invalid file specification");
- }
- if (error_ptr)
- *error_ptr = error;
- return data_sp;
-}
-
FileSpec::EnumerateDirectoryResult
FileSpec::ForEachItemInDirectory(llvm::StringRef dir_path,
DirectoryCallback const &callback) {
diff --git a/lldb/source/Host/common/Host.cpp b/lldb/source/Host/common/Host.cpp
index 6b958201c20..5d254ec0561 100644
--- a/lldb/source/Host/common/Host.cpp
+++ b/lldb/source/Host/common/Host.cpp
@@ -63,6 +63,7 @@
#include "lldb/Target/ProcessLaunchInfo.h"
#include "lldb/Target/UnixSignals.h"
#include "lldb/Utility/CleanUp.h"
+#include "lldb/Utility/DataBufferLLVM.h"
#include "lldb/Utility/Error.h"
#include "lldb/Utility/Log.h"
#include "lldb/lldb-private-forward.h"
@@ -586,11 +587,11 @@ Error Host::RunShellCommand(const Args &args, const FileSpec &working_dir,
error.SetErrorStringWithFormat(
"shell command output is too large to fit into a std::string");
} else {
- std::vector<char> command_output(file_size);
- output_file_spec.ReadFileContents(0, command_output.data(),
- file_size, &error);
+ auto Buffer =
+ DataBufferLLVM::CreateFromPath(output_file_spec.GetPath());
if (error.Success())
- command_output_ptr->assign(command_output.data(), file_size);
+ command_output_ptr->assign(Buffer->GetChars(),
+ Buffer->GetByteSize());
}
}
}
diff --git a/lldb/source/Interpreter/OptionValueFileSpec.cpp b/lldb/source/Interpreter/OptionValueFileSpec.cpp
index a6eb5375851..0df581af5d6 100644
--- a/lldb/source/Interpreter/OptionValueFileSpec.cpp
+++ b/lldb/source/Interpreter/OptionValueFileSpec.cpp
@@ -15,6 +15,7 @@
#include "lldb/Interpreter/Args.h"
#include "lldb/Interpreter/CommandCompletions.h"
#include "lldb/Interpreter/CommandInterpreter.h"
+#include "lldb/Utility/DataBufferLLVM.h"
using namespace lldb;
using namespace lldb_private;
@@ -118,10 +119,8 @@ OptionValueFileSpec::GetFileContents(bool null_terminate) {
const auto file_mod_time = FileSystem::GetModificationTime(m_current_value);
if (m_data_sp && m_data_mod_time == file_mod_time)
return m_data_sp;
- if (null_terminate)
- m_data_sp = m_current_value.ReadFileContentsAsCString();
- else
- m_data_sp = m_current_value.ReadFileContents();
+ m_data_sp = DataBufferLLVM::CreateFromPath(m_current_value.GetPath(),
+ null_terminate);
m_data_mod_time = file_mod_time;
}
return m_data_sp;
diff --git a/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp b/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
index d1f1a3bb12b..a4bd1c99855 100644
--- a/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
+++ b/lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
@@ -39,6 +39,7 @@
#include "lldb/Target/Target.h"
#include "lldb/Target/Thread.h"
#include "lldb/Utility/ConstString.h"
+#include "lldb/Utility/DataBufferLLVM.h"
#include "lldb/Utility/Error.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/RegularExpression.h"
@@ -2536,7 +2537,7 @@ bool RenderScriptRuntime::LoadAllocation(Stream &strm, const uint32_t alloc_id,
}
// Read file into data buffer
- DataBufferSP data_sp(file.ReadFileContents());
+ auto data_sp = DataBufferLLVM::CreateFromPath(file.GetPath());
// Cast start of buffer to FileHeader and use pointer to read metadata
void *file_buf = data_sp->GetBytes();
@@ -3074,7 +3075,7 @@ bool RSModuleDescriptor::ParseRSInfo() {
const addr_t size = info_sym->GetByteSize();
const FileSpec fs = m_module->GetFileSpec();
- const DataBufferSP buffer = fs.ReadFileContents(addr, size);
+ auto buffer = DataBufferLLVM::CreateSliceFromPath(fs.GetPath(), size, addr);
if (!buffer)
return false;
diff --git a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
index 2113876e6c1..b74da330017 100644
--- a/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
+++ b/lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
@@ -314,7 +314,7 @@ ObjectContainer *ObjectContainerBSDArchive::CreateInstance(
// file gets updated by a new build while this .a file is being used for
// debugging
DataBufferSP archive_data_sp =
- DataBufferLLVM::CreateFromPath(file->GetPath(), length, file_offset);
+ DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
if (!archive_data_sp)
return nullptr;
@@ -469,7 +469,7 @@ size_t ObjectContainerBSDArchive::GetModuleSpecifications(
if (!archive_sp) {
set_archive_arch = true;
data_sp =
- DataBufferLLVM::CreateFromPath(file.GetPath(), file_size, file_offset);
+ DataBufferLLVM::CreateSliceFromPath(file.GetPath(), file_size, file_offset);
if (data_sp) {
data.SetData(data_sp, 0, data_sp->GetByteSize());
archive_sp = Archive::ParseAndCacheArchiveForFile(
diff --git a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
index 8a4734bd337..10213f572b8 100644
--- a/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ b/lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -388,7 +388,7 @@ ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp,
lldb::offset_t length) {
if (!data_sp) {
data_sp =
- DataBufferLLVM::CreateFromPath(file->GetPath(), length, file_offset);
+ DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
if (!data_sp)
return nullptr;
data_offset = 0;
@@ -406,7 +406,7 @@ ObjectFile *ObjectFileELF::CreateInstance(const lldb::ModuleSP &module_sp,
// Update the data to contain the entire file if it doesn't already
if (data_sp->GetByteSize() < length) {
data_sp =
- DataBufferLLVM::CreateFromPath(file->GetPath(), length, file_offset);
+ DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
if (!data_sp)
return nullptr;
data_offset = 0;
@@ -665,7 +665,7 @@ size_t ObjectFileELF::GetModuleSpecifications(
size_t section_header_end = header.e_shoff + header.e_shentsize;
if (header.HasHeaderExtension() &&
section_header_end > data_sp->GetByteSize()) {
- data_sp = DataBufferLLVM::CreateFromPath(
+ data_sp = DataBufferLLVM::CreateSliceFromPath(
file.GetPath(), section_header_end, file_offset);
if (data_sp) {
data.SetData(data_sp);
@@ -679,7 +679,7 @@ size_t ObjectFileELF::GetModuleSpecifications(
section_header_end =
header.e_shoff + header.e_shnum * header.e_shentsize;
if (section_header_end > data_sp->GetByteSize()) {
- data_sp = DataBufferLLVM::CreateFromPath(
+ data_sp = DataBufferLLVM::CreateSliceFromPath(
file.GetPath(), section_header_end, file_offset);
if (data_sp)
data.SetData(data_sp);
@@ -724,7 +724,7 @@ size_t ObjectFileELF::GetModuleSpecifications(
size_t program_headers_end =
header.e_phoff + header.e_phnum * header.e_phentsize;
if (program_headers_end > data_sp->GetByteSize()) {
- data_sp = DataBufferLLVM::CreateFromPath(
+ data_sp = DataBufferLLVM::CreateSliceFromPath(
file.GetPath(), program_headers_end, file_offset);
if (data_sp)
data.SetData(data_sp);
@@ -740,7 +740,7 @@ size_t ObjectFileELF::GetModuleSpecifications(
}
if (segment_data_end > data_sp->GetByteSize()) {
- data_sp = DataBufferLLVM::CreateFromPath(
+ data_sp = DataBufferLLVM::CreateSliceFromPath(
file.GetPath(), segment_data_end, file_offset);
if (data_sp)
data.SetData(data_sp);
@@ -750,7 +750,7 @@ size_t ObjectFileELF::GetModuleSpecifications(
CalculateELFNotesSegmentsCRC32(program_headers, data);
} else {
// Need to map entire file into memory to calculate the crc.
- data_sp = DataBufferLLVM::CreateFromPath(file.GetPath(), -1,
+ data_sp = DataBufferLLVM::CreateSliceFromPath(file.GetPath(), -1,
file_offset);
if (data_sp) {
data.SetData(data_sp);
diff --git a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
index 5901a1b4935..a6d5a85e2d4 100644
--- a/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
+++ b/lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
@@ -860,7 +860,7 @@ ObjectFile *ObjectFileMachO::CreateInstance(const lldb::ModuleSP &module_sp,
lldb::offset_t length) {
if (!data_sp) {
data_sp =
- DataBufferLLVM::CreateFromPath(file->GetPath(), length, file_offset);
+ DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
if (!data_sp)
return nullptr;
data_offset = 0;
@@ -872,7 +872,7 @@ ObjectFile *ObjectFileMachO::CreateInstance(const lldb::ModuleSP &module_sp,
// Update the data to contain the entire file if it doesn't already
if (data_sp->GetByteSize() < length) {
data_sp =
- DataBufferLLVM::CreateFromPath(file->GetPath(), length, file_offset);
+ DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
if (!data_sp)
return nullptr;
data_offset = 0;
@@ -911,7 +911,8 @@ size_t ObjectFileMachO::GetModuleSpecifications(
size_t header_and_load_cmds =
header.sizeofcmds + MachHeaderSizeFromMagic(header.magic);
if (header_and_load_cmds >= data_sp->GetByteSize()) {
- data_sp = file.ReadFileContents(file_offset, header_and_load_cmds);
+ data_sp = DataBufferLLVM::CreateSliceFromPath(
+ file.GetPath(), header_and_load_cmds, file_offset);
data.SetData(data_sp);
data_offset = MachHeaderSizeFromMagic(header.magic);
}
@@ -1123,8 +1124,8 @@ bool ObjectFileMachO::ParseHeader() {
ReadMemory(process_sp, m_memory_addr, header_and_lc_size);
} else {
// Read in all only the load command data from the file on disk
- data_sp =
- m_file.ReadFileContents(m_file_offset, header_and_lc_size);
+ data_sp = DataBufferLLVM::CreateSliceFromPath(
+ m_file.GetPath(), header_and_lc_size, m_file_offset);
if (data_sp->GetByteSize() != header_and_lc_size)
return false;
}
@@ -2095,7 +2096,7 @@ UUID ObjectFileMachO::GetSharedCacheUUID(FileSpec dyld_shared_cache,
const ByteOrder byte_order,
const uint32_t addr_byte_size) {
UUID dsc_uuid;
- DataBufferSP DscData = DataBufferLLVM::CreateFromPath(
+ DataBufferSP DscData = DataBufferLLVM::CreateSliceFromPath(
dyld_shared_cache.GetPath(),
sizeof(struct lldb_copy_dyld_cache_header_v1), 0);
if (!DscData)
@@ -2703,7 +2704,7 @@ size_t ObjectFileMachO::ParseSymtab() {
// Process the dyld shared cache header to find the unmapped symbols
- DataBufferSP dsc_data_sp = DataBufferLLVM::CreateFromPath(
+ DataBufferSP dsc_data_sp = DataBufferLLVM::CreateSliceFromPath(
dsc_filespec.GetPath(), sizeof(struct lldb_copy_dyld_cache_header_v1),
0);
if (!dsc_uuid.IsValid()) {
@@ -2738,7 +2739,7 @@ size_t ObjectFileMachO::ParseSymtab() {
mappingOffset >= sizeof(struct lldb_copy_dyld_cache_header_v1)) {
DataBufferSP dsc_mapping_info_data_sp =
- DataBufferLLVM::CreateFromPath(
+ DataBufferLLVM::CreateSliceFromPath(
dsc_filespec.GetPath(),
sizeof(struct lldb_copy_dyld_cache_mapping_info),
mappingOffset);
@@ -2765,7 +2766,7 @@ size_t ObjectFileMachO::ParseSymtab() {
if (localSymbolsOffset && localSymbolsSize) {
// Map the local symbols
DataBufferSP dsc_local_symbols_data_sp =
- DataBufferLLVM::CreateFromPath(dsc_filespec.GetPath(),
+ DataBufferLLVM::CreateSliceFromPath(dsc_filespec.GetPath(),
localSymbolsSize,
localSymbolsOffset);
diff --git a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
index b9577b5d1f2..df665129ae2 100644
--- a/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
+++ b/lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
@@ -68,7 +68,7 @@ ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
lldb::offset_t length) {
if (!data_sp) {
data_sp =
- DataBufferLLVM::CreateFromPath(file->GetPath(), length, file_offset);
+ DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
if (!data_sp)
return nullptr;
data_offset = 0;
@@ -80,7 +80,7 @@ ObjectFile *ObjectFilePECOFF::CreateInstance(const lldb::ModuleSP &module_sp,
// Update the data to contain the entire file if it doesn't already
if (data_sp->GetByteSize() < length) {
data_sp =
- DataBufferLLVM::CreateFromPath(file->GetPath(), length, file_offset);
+ DataBufferLLVM::CreateSliceFromPath(file->GetPath(), length, file_offset);
if (!data_sp)
return nullptr;
}
@@ -430,7 +430,10 @@ bool ObjectFilePECOFF::ParseCOFFOptionalHeader(lldb::offset_t *offset_ptr) {
DataExtractor ObjectFilePECOFF::ReadImageData(uint32_t offset, size_t size) {
if (m_file) {
- DataBufferSP buffer_sp(m_file.ReadFileContents(offset, size));
+ // A bit of a hack, but we intend to write to this buffer, so we can't
+ // mmap it.
+ auto buffer_sp =
+ DataBufferLLVM::CreateSliceFromPath(m_file.GetPath(), size, offset, true);
return DataExtractor(buffer_sp, GetByteOrder(), GetAddressByteSize());
}
ProcessSP process_sp(m_process_wp.lock());
diff --git a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
index b1937efe553..75311078f74 100644
--- a/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ b/lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
@@ -37,6 +37,7 @@
#include "lldb/Symbol/SymbolVendor.h"
#include "lldb/Target/Process.h"
#include "lldb/Target/Target.h"
+#include "lldb/Utility/DataBufferLLVM.h"
#include "lldb/Utility/Error.h"
#include "lldb/Utility/Log.h"
#include "llvm/ADT/STLExtras.h"
@@ -1154,13 +1155,16 @@ const char *PlatformDarwin::GetDeveloperDirectory() {
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, false);
- size_t bytes_read = temp_file_spec.ReadFileContents(
- 0, developer_dir_path, sizeof(developer_dir_path), NULL);
- if (bytes_read > 0) {
- developer_dir_path[bytes_read] = '\0';
- while (developer_dir_path[bytes_read - 1] == '\r' ||
- developer_dir_path[bytes_read - 1] == '\n')
- developer_dir_path[--bytes_read] = '\0';
+ auto dir_buffer =
+ DataBufferLLVM::CreateFromPath(temp_file_spec.GetPath(), true);
+ 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
+ // terminator.
+ path_ref =
+ path_ref.rtrim("\r\n").take_front(sizeof(developer_dir_path) - 1);
+ ::memcpy(developer_dir_path, path_ref.data(), path_ref.size());
+ developer_dir_path[path_ref.size()] = '\0';
developer_dir_path_valid = true;
}
}
diff --git a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
index adb199ef166..99c1b7d3733 100644
--- a/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
+++ b/lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
@@ -24,6 +24,7 @@
#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 "llvm/Support/ELF.h"
@@ -61,7 +62,8 @@ lldb::ProcessSP ProcessElfCore::CreateInstance(lldb::TargetSP target_sp,
// to ignore possible presence of the header extension.
const size_t header_size = sizeof(llvm::ELF::Elf64_Ehdr);
- lldb::DataBufferSP data_sp(crash_file->ReadFileContents(0, header_size));
+ auto data_sp =
+ DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0);
if (data_sp && data_sp->GetByteSize() == header_size &&
elf::ELFHeader::MagicBytesMatch(data_sp->GetBytes())) {
elf::ELFHeader elf_header;
diff --git a/lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp b/lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
index 9941c632f3a..665fcd169b0 100644
--- a/lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
+++ b/lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
@@ -30,6 +30,7 @@
#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"
// Project includes
@@ -66,7 +67,8 @@ lldb::ProcessSP ProcessMachCore::CreateInstance(lldb::TargetSP target_sp,
lldb::ProcessSP process_sp;
if (crash_file) {
const size_t header_size = sizeof(llvm::MachO::mach_header);
- lldb::DataBufferSP data_sp(crash_file->ReadFileContents(0, header_size));
+ auto data_sp =
+ DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0);
if (data_sp && data_sp->GetByteSize() == header_size) {
DataExtractor data(data_sp, lldb::eByteOrderLittle, 4);
diff --git a/lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp b/lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
index 352e33b1f36..f3f4664ad6e 100644
--- a/lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
+++ b/lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
@@ -53,7 +53,7 @@ lldb::ProcessSP ProcessMinidump::CreateInstance(lldb::TargetSP target_sp,
// Read enough data for the Minidump header
constexpr size_t header_size = sizeof(MinidumpHeader);
auto DataPtr =
- DataBufferLLVM::CreateFromPath(crash_file->GetPath(), header_size, 0);
+ DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), header_size, 0);
if (!DataPtr)
return nullptr;
@@ -65,7 +65,7 @@ lldb::ProcessSP ProcessMinidump::CreateInstance(lldb::TargetSP target_sp,
if (header == nullptr)
return nullptr;
- auto AllData = DataBufferLLVM::CreateFromPath(crash_file->GetPath(), -1, 0);
+ auto AllData = DataBufferLLVM::CreateSliceFromPath(crash_file->GetPath(), -1, 0);
if (!AllData)
return nullptr;
diff --git a/lldb/source/Symbol/ObjectFile.cpp b/lldb/source/Symbol/ObjectFile.cpp
index 0394b9b60fe..483a315defb 100644
--- a/lldb/source/Symbol/ObjectFile.cpp
+++ b/lldb/source/Symbol/ObjectFile.cpp
@@ -22,6 +22,7 @@
#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/lldb-private.h"
@@ -76,8 +77,8 @@ ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file,
// and object container plug-ins can use these bytes to see if they
// can parse this file.
if (file_size > 0) {
- data_sp = file->ReadFileContents(file_offset,
- std::min<size_t>(512, file_size));
+ data_sp =
+ DataBufferLLVM::CreateSliceFromPath(file->GetPath(), 512, file_offset);
data_offset = 0;
}
}
@@ -120,7 +121,8 @@ ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file,
}
// 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 = archive_file.ReadFileContents(file_offset, 512);
+ data_sp = DataBufferLLVM::CreateSliceFromPath(archive_file.GetPath(),
+ 512, file_offset);
}
}
}
@@ -206,7 +208,7 @@ size_t ObjectFile::GetModuleSpecifications(const FileSpec &file,
lldb::offset_t file_offset,
lldb::offset_t file_size,
ModuleSpecList &specs) {
- DataBufferSP data_sp(file.ReadFileContents(file_offset, 512));
+ DataBufferSP data_sp = DataBufferLLVM::CreateSliceFromPath(file.GetPath(), 512, file_offset);
if (data_sp) {
if (file_size == 0) {
const lldb::offset_t actual_file_size = file.GetByteSize();
diff --git a/lldb/source/Utility/DataBufferLLVM.cpp b/lldb/source/Utility/DataBufferLLVM.cpp
index 2f6654d4c9a..cd1bb3a3328 100644
--- a/lldb/source/Utility/DataBufferLLVM.cpp
+++ b/lldb/source/Utility/DataBufferLLVM.cpp
@@ -24,13 +24,28 @@ DataBufferLLVM::DataBufferLLVM(std::unique_ptr<llvm::MemoryBuffer> MemBuffer)
DataBufferLLVM::~DataBufferLLVM() {}
std::shared_ptr<DataBufferLLVM>
-DataBufferLLVM::CreateFromPath(const llvm::Twine &Path, uint64_t Size,
- uint64_t Offset) {
+DataBufferLLVM::CreateSliceFromPath(const llvm::Twine &Path, uint64_t Size,
+ uint64_t Offset, bool Private) {
// If the file resides non-locally, pass the volatile flag so that we don't
// mmap it.
- bool Volatile = !llvm::sys::fs::is_local(Path);
+ if (!Private)
+ Private = !llvm::sys::fs::is_local(Path);
- auto Buffer = llvm::MemoryBuffer::getFileSlice(Path, Size, Offset, Volatile);
+ auto Buffer = llvm::MemoryBuffer::getFileSlice(Path, Size, Offset, Private);
+ if (!Buffer)
+ return nullptr;
+ return std::shared_ptr<DataBufferLLVM>(
+ new DataBufferLLVM(std::move(*Buffer)));
+}
+
+std::shared_ptr<DataBufferLLVM>
+DataBufferLLVM::CreateFromPath(const llvm::Twine &Path, bool NullTerminate, bool Private) {
+ // If the file resides non-locally, pass the volatile flag so that we don't
+ // mmap it.
+ if (!Private)
+ Private = !llvm::sys::fs::is_local(Path);
+
+ auto Buffer = llvm::MemoryBuffer::getFile(Path, -1, NullTerminate, Private);
if (!Buffer)
return nullptr;
return std::shared_ptr<DataBufferLLVM>(
diff --git a/lldb/unittests/Process/minidump/MinidumpParserTest.cpp b/lldb/unittests/Process/minidump/MinidumpParserTest.cpp
index f2f5af6d1c0..f5a60a64e86 100644
--- a/lldb/unittests/Process/minidump/MinidumpParserTest.cpp
+++ b/lldb/unittests/Process/minidump/MinidumpParserTest.cpp
@@ -53,7 +53,7 @@ public:
llvm::SmallString<128> filename = inputs_folder;
llvm::sys::path::append(filename, minidump_filename);
- auto BufferPtr = DataBufferLLVM::CreateFromPath(filename, load_size, 0);
+ auto BufferPtr = DataBufferLLVM::CreateSliceFromPath(filename, load_size, 0);
llvm::Optional<MinidumpParser> optional_parser =
MinidumpParser::Create(BufferPtr);
OpenPOWER on IntegriCloud