zturner created this revision.

The idea is that `DataBufferLLVM` already covers this use case, so there is no 
point duplicating the code.  The only tricky thing is that I found one case 
where we attempt to modify the bytes we read in (in `ObjectFilePECOFF`).  It 
seems kind of hackish, but it worked because `ReadFileContents` would only ever 
load a copy of the data into memory.  But LLVM uses `mmap` whenever possible 
and maps as readonly, so this was causing failures when we tried to modify the 
bytes.

To address this I've added a flag called `Private` which forces us to not use 
`mmap`.  In an ideal world, we would still try to use `mmap`, but map the bytes 
as private / writable.  This is a patch for another day.

Also for another day is to rename `DataBufferLLVM` to `DataBufferFile`.


https://reviews.llvm.org/D30622

Files:
  lldb/include/lldb/Host/FileSpec.h
  lldb/include/lldb/Utility/DataBufferLLVM.h
  lldb/source/API/SBSection.cpp
  lldb/source/Commands/CommandObjectMemory.cpp
  lldb/source/Core/SourceManager.cpp
  lldb/source/Host/common/FileSpec.cpp
  lldb/source/Host/common/Host.cpp
  lldb/source/Interpreter/OptionValueFileSpec.cpp
  
lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
  lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
  lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
  lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
  lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
  lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
  lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
  lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
  lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
  lldb/source/Symbol/ObjectFile.cpp
  lldb/source/Utility/DataBufferLLVM.cpp
  lldb/unittests/Process/minidump/MinidumpParserTest.cpp

Index: lldb/unittests/Process/minidump/MinidumpParserTest.cpp
===================================================================
--- lldb/unittests/Process/minidump/MinidumpParserTest.cpp
+++ lldb/unittests/Process/minidump/MinidumpParserTest.cpp
@@ -53,7 +53,7 @@
     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);
Index: lldb/source/Utility/DataBufferLLVM.cpp
===================================================================
--- lldb/source/Utility/DataBufferLLVM.cpp
+++ lldb/source/Utility/DataBufferLLVM.cpp
@@ -24,13 +24,28 @@
 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>(
Index: lldb/source/Symbol/ObjectFile.cpp
===================================================================
--- lldb/source/Symbol/ObjectFile.cpp
+++ 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 @@
         // 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 @@
             }
             // 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 @@
                                            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();
Index: lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
===================================================================
--- lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
+++ lldb/source/Plugins/Process/minidump/ProcessMinidump.cpp
@@ -53,7 +53,7 @@
   // 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 @@
   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;
 
Index: lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
===================================================================
--- lldb/source/Plugins/Process/mach-core/ProcessMachCore.cpp
+++ 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 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);
 
Index: lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
===================================================================
--- lldb/source/Plugins/Process/elf-core/ProcessElfCore.cpp
+++ 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 @@
     // 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;
Index: lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
===================================================================
--- lldb/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
+++ 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 @@
         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;
       }
     }
Index: lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
===================================================================
--- lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
+++ lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
@@ -68,7 +68,7 @@
                                              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 @@
   // 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 @@
 
 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());
Index: lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
===================================================================
--- lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
+++ lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
@@ -860,7 +860,7 @@
                                             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 @@
   // 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 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 @@
                   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 @@
                                          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 @@
 
       // 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 @@
             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 @@
           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);
 
Index: lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
===================================================================
--- lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
+++ lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
@@ -388,7 +388,7 @@
                                           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 @@
   // 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 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 @@
           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 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 @@
                 }
 
                 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 @@
                     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);
Index: lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
===================================================================
--- lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
+++ lldb/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
@@ -314,7 +314,7 @@
       // 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 @@
   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(
Index: lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
===================================================================
--- lldb/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
+++ 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 @@
   }
 
   // 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 @@
   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;
 
Index: lldb/source/Interpreter/OptionValueFileSpec.cpp
===================================================================
--- lldb/source/Interpreter/OptionValueFileSpec.cpp
+++ 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 @@
     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;
Index: lldb/source/Host/common/Host.cpp
===================================================================
--- lldb/source/Host/common/Host.cpp
+++ 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.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());
           }
         }
       }
Index: lldb/source/Host/common/FileSpec.cpp
===================================================================
--- lldb/source/Host/common/FileSpec.cpp
+++ 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 @@
   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) {
Index: lldb/source/Core/SourceManager.cpp
===================================================================
--- lldb/source/Core/SourceManager.cpp
+++ 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 @@
   }
 
   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 @@
   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();
   }
 }
Index: lldb/source/Commands/CommandObjectMemory.cpp
===================================================================
--- lldb/source/Commands/CommandObjectMemory.cpp
+++ 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 @@
       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) {
Index: lldb/source/API/SBSection.cpp
===================================================================
--- lldb/source/API/SBSection.cpp
+++ 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 @@
             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(),
Index: lldb/include/lldb/Utility/DataBufferLLVM.h
===================================================================
--- lldb/include/lldb/Utility/DataBufferLLVM.h
+++ lldb/include/lldb/Utility/DataBufferLLVM.h
@@ -26,12 +26,17 @@
   ~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.
Index: lldb/include/lldb/Host/FileSpec.h
===================================================================
--- lldb/include/lldb/Host/FileSpec.h
+++ lldb/include/lldb/Host/FileSpec.h
@@ -498,55 +498,6 @@
   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.
   //------------------------------------------------------------------
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to