[Lldb-commits] [lldb] r297095 - Remove FileSpec::ReadFileContents.

Zachary Turner via lldb-commits lldb-commits at lists.llvm.org
Mon Mar 6 15:42:15 PST 2017


Author: zturner
Date: Mon Mar  6 17:42:14 2017
New Revision: 297095

URL: http://llvm.org/viewvc/llvm-project?rev=297095&view=rev
Log:
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

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

Modified: lldb/trunk/include/lldb/Host/FileSpec.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Host/FileSpec.h?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Host/FileSpec.h (original)
+++ lldb/trunk/include/lldb/Host/FileSpec.h Mon Mar  6 17:42:14 2017
@@ -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.
   //------------------------------------------------------------------

Modified: lldb/trunk/include/lldb/Utility/DataBufferLLVM.h
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/include/lldb/Utility/DataBufferLLVM.h?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/include/lldb/Utility/DataBufferLLVM.h (original)
+++ lldb/trunk/include/lldb/Utility/DataBufferLLVM.h Mon Mar  6 17:42:14 2017
@@ -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.

Modified: lldb/trunk/source/API/SBSection.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/API/SBSection.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/API/SBSection.cpp (original)
+++ lldb/trunk/source/API/SBSection.cpp Mon Mar  6 17:42:14 2017
@@ -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_
             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(),

Modified: lldb/trunk/source/Commands/CommandObjectMemory.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Commands/CommandObjectMemory.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Commands/CommandObjectMemory.cpp (original)
+++ lldb/trunk/source/Commands/CommandObjectMemory.cpp Mon Mar  6 17:42:14 2017
@@ -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) {

Modified: lldb/trunk/source/Core/SourceManager.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Core/SourceManager.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Core/SourceManager.cpp (original)
+++ lldb/trunk/source/Core/SourceManager.cpp Mon Mar  6 17:42:14 2017
@@ -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::CommonInitiali
   }
 
   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();
   }
 }

Modified: lldb/trunk/source/Host/common/FileSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/FileSpec.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/FileSpec.cpp (original)
+++ lldb/trunk/source/Host/common/FileSpec.cpp Mon Mar  6 17:42:14 2017
@@ -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) {

Modified: lldb/trunk/source/Host/common/Host.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Host/common/Host.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Host/common/Host.cpp (original)
+++ lldb/trunk/source/Host/common/Host.cpp Mon Mar  6 17:42:14 2017
@@ -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 &
             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());
           }
         }
       }

Modified: lldb/trunk/source/Interpreter/OptionValueFileSpec.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Interpreter/OptionValueFileSpec.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Interpreter/OptionValueFileSpec.cpp (original)
+++ lldb/trunk/source/Interpreter/OptionValueFileSpec.cpp Mon Mar  6 17:42:14 2017
@@ -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(boo
     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;

Modified: lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp (original)
+++ lldb/trunk/source/Plugins/LanguageRuntime/RenderScript/RenderScriptRuntime/RenderScriptRuntime.cpp Mon Mar  6 17:42:14 2017
@@ -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
   }
 
   // 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;
 

Modified: lldb/trunk/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp (original)
+++ lldb/trunk/source/Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.cpp Mon Mar  6 17:42:14 2017
@@ -314,7 +314,7 @@ ObjectContainer *ObjectContainerBSDArchi
       // 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::GetMod
   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(

Modified: lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp (original)
+++ lldb/trunk/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp Mon Mar  6 17:42:14 2017
@@ -388,7 +388,7 @@ ObjectFile *ObjectFileELF::CreateInstanc
                                           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::CreateInstanc
   // 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::GetModuleSpecifica
           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::GetModuleSpecifica
           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::GetModuleSpecifica
                 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::GetModuleSpecifica
                 }
 
                 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::GetModuleSpecifica
                     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);

Modified: lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp (original)
+++ lldb/trunk/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp Mon Mar  6 17:42:14 2017
@@ -860,7 +860,7 @@ ObjectFile *ObjectFileMachO::CreateInsta
                                             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::CreateInsta
   // 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::GetModuleSpecifi
       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
                                          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);
 

Modified: lldb/trunk/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp (original)
+++ lldb/trunk/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp Mon Mar  6 17:42:14 2017
@@ -68,7 +68,7 @@ ObjectFile *ObjectFilePECOFF::CreateInst
                                              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::CreateInst
   // 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::ParseCOFFOptional
 
 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());

Modified: lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp (original)
+++ lldb/trunk/source/Plugins/Platform/MacOSX/PlatformDarwin.cpp Mon Mar  6 17:42:14 2017
@@ -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::GetDeveloper
         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;
       }
     }

Modified: lldb/trunk/source/Plugins/Process/elf-core/ProcessElfCore.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/elf-core/ProcessElfCore.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/elf-core/ProcessElfCore.cpp (original)
+++ lldb/trunk/source/Plugins/Process/elf-core/ProcessElfCore.cpp Mon Mar  6 17:42:14 2017
@@ -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::CreateIn
     // 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;

Modified: lldb/trunk/source/Plugins/Process/mach-core/ProcessMachCore.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/mach-core/ProcessMachCore.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/mach-core/ProcessMachCore.cpp (original)
+++ lldb/trunk/source/Plugins/Process/mach-core/ProcessMachCore.cpp Mon Mar  6 17:42:14 2017
@@ -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::CreateI
   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);
 

Modified: lldb/trunk/source/Plugins/Process/minidump/ProcessMinidump.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/minidump/ProcessMinidump.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/minidump/ProcessMinidump.cpp (original)
+++ lldb/trunk/source/Plugins/Process/minidump/ProcessMinidump.cpp Mon Mar  6 17:42:14 2017
@@ -53,7 +53,7 @@ lldb::ProcessSP ProcessMinidump::CreateI
   // 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::CreateI
   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;
 

Modified: lldb/trunk/source/Symbol/ObjectFile.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Symbol/ObjectFile.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Symbol/ObjectFile.cpp (original)
+++ lldb/trunk/source/Symbol/ObjectFile.cpp Mon Mar  6 17:42:14 2017
@@ -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::Modul
         // 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::Modul
             }
             // 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::GetModuleSpecificatio
                                            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();

Modified: lldb/trunk/source/Utility/DataBufferLLVM.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Utility/DataBufferLLVM.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/source/Utility/DataBufferLLVM.cpp (original)
+++ lldb/trunk/source/Utility/DataBufferLLVM.cpp Mon Mar  6 17:42:14 2017
@@ -24,13 +24,28 @@ DataBufferLLVM::DataBufferLLVM(std::uniq
 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>(

Modified: lldb/trunk/unittests/Process/minidump/MinidumpParserTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/unittests/Process/minidump/MinidumpParserTest.cpp?rev=297095&r1=297094&r2=297095&view=diff
==============================================================================
--- lldb/trunk/unittests/Process/minidump/MinidumpParserTest.cpp (original)
+++ lldb/trunk/unittests/Process/minidump/MinidumpParserTest.cpp Mon Mar  6 17:42:14 2017
@@ -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);




More information about the lldb-commits mailing list