2010-06-09 00:52:24 +08:00
|
|
|
//===-- DataBufferMemoryMap.cpp ---------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
// C Includes
|
2010-06-09 00:52:24 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2013-08-23 20:44:05 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include "lldb/Host/windows/windows.h"
|
|
|
|
#else
|
2010-06-09 00:52:24 +08:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2016-05-21 03:18:20 +08:00
|
|
|
#define MAP_EXTRA_HOST_READ_FLAGS 0
|
|
|
|
|
|
|
|
#if defined (__APPLE__)
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Newer versions of MacOSX have a flag that will allow us to read from
|
|
|
|
// binaries whose code signature is invalid without crashing by using
|
|
|
|
// the MAP_RESILIENT_CODESIGN flag. Also if a file from removable media
|
|
|
|
// is mapped we can avoid crashing and return zeroes to any pages we try
|
|
|
|
// to read if the media becomes unavailable by using the
|
|
|
|
// MAP_RESILIENT_MEDIA flag.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
#if defined(MAP_RESILIENT_CODESIGN)
|
|
|
|
#undef MAP_EXTRA_HOST_READ_FLAGS
|
|
|
|
#if defined(MAP_RESILIENT_MEDIA)
|
|
|
|
#define MAP_EXTRA_HOST_READ_FLAGS MAP_RESILIENT_CODESIGN | MAP_RESILIENT_MEDIA
|
|
|
|
#else
|
|
|
|
#define MAP_EXTRA_HOST_READ_FLAGS MAP_RESILIENT_CODESIGN
|
|
|
|
#endif
|
|
|
|
#endif // #if defined(MAP_RESILIENT_CODESIGN)
|
|
|
|
#endif // #if defined (__APPLE__)
|
|
|
|
|
|
|
|
#endif // #else #ifdef _WIN32
|
2016-03-03 08:51:40 +08:00
|
|
|
// C++ Includes
|
|
|
|
#include <cerrno>
|
|
|
|
#include <climits>
|
|
|
|
|
|
|
|
// Other libraries and framework includes
|
2014-06-03 01:30:22 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
// Project includes
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Core/DataBufferMemoryMap.h"
|
|
|
|
#include "lldb/Core/Error.h"
|
2012-01-06 08:43:54 +08:00
|
|
|
#include "lldb/Host/File.h"
|
2011-02-08 13:05:52 +08:00
|
|
|
#include "lldb/Host/FileSpec.h"
|
2014-08-20 01:18:29 +08:00
|
|
|
#include "lldb/Host/HostInfo.h"
|
2013-02-07 01:22:03 +08:00
|
|
|
#include "lldb/Core/Log.h"
|
2010-06-09 00:52:24 +08:00
|
|
|
|
2013-02-07 01:22:03 +08:00
|
|
|
using namespace lldb;
|
2010-06-09 00:52:24 +08:00
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Default Constructor
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
DataBufferMemoryMap::DataBufferMemoryMap() :
|
2016-03-03 08:51:40 +08:00
|
|
|
m_mmap_addr(nullptr),
|
2010-06-09 00:52:24 +08:00
|
|
|
m_mmap_size(0),
|
2016-03-03 08:51:40 +08:00
|
|
|
m_data(nullptr),
|
2011-04-05 02:18:57 +08:00
|
|
|
m_size(0)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Virtual destructor since this class inherits from a pure virtual
|
|
|
|
// base class.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
DataBufferMemoryMap::~DataBufferMemoryMap()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-03-03 08:51:40 +08:00
|
|
|
// Return a pointer to the bytes owned by this object, or nullptr if
|
2010-06-09 00:52:24 +08:00
|
|
|
// the object contains no bytes.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
uint8_t *
|
|
|
|
DataBufferMemoryMap::GetBytes()
|
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2016-03-03 08:51:40 +08:00
|
|
|
// Return a const pointer to the bytes owned by this object, or nullptr
|
2010-06-09 00:52:24 +08:00
|
|
|
// if the object contains no bytes.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
const uint8_t *
|
|
|
|
DataBufferMemoryMap::GetBytes() const
|
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Return the number of bytes this object currently contains.
|
|
|
|
//----------------------------------------------------------------------
|
2013-03-15 02:31:44 +08:00
|
|
|
uint64_t
|
2010-06-09 00:52:24 +08:00
|
|
|
DataBufferMemoryMap::GetByteSize() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Reverts this object to an empty state by unmapping any memory
|
|
|
|
// that is currently owned.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
DataBufferMemoryMap::Clear()
|
|
|
|
{
|
2016-03-03 08:51:40 +08:00
|
|
|
if (m_mmap_addr != nullptr)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP));
|
2013-02-07 01:22:03 +08:00
|
|
|
if (log)
|
2015-04-09 04:23:52 +08:00
|
|
|
log->Printf("DataBufferMemoryMap::Clear() m_mmap_addr = %p, m_mmap_size = %" PRIu64 "", (void *)m_mmap_addr,
|
|
|
|
(uint64_t)m_mmap_size);
|
2013-08-23 20:44:05 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
UnmapViewOfFile(m_mmap_addr);
|
|
|
|
#else
|
2010-06-09 00:52:24 +08:00
|
|
|
::munmap((void *)m_mmap_addr, m_mmap_size);
|
2013-08-23 20:44:05 +08:00
|
|
|
#endif
|
2016-03-03 08:51:40 +08:00
|
|
|
m_mmap_addr = nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
m_mmap_size = 0;
|
2016-03-03 08:51:40 +08:00
|
|
|
m_data = nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Memory map "length" bytes from "file" starting "offset"
|
2010-06-09 17:53:11 +08:00
|
|
|
// bytes into the file. If "length" is set to SIZE_MAX, then
|
2010-06-09 00:52:24 +08:00
|
|
|
// map as many bytes as possible.
|
|
|
|
//
|
|
|
|
// Returns the number of bytes mapped starting from the requested
|
|
|
|
// offset.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
size_t
|
2012-10-05 06:47:07 +08:00
|
|
|
DataBufferMemoryMap::MemoryMapFromFileSpec (const FileSpec* filespec,
|
2013-03-15 02:31:44 +08:00
|
|
|
lldb::offset_t offset,
|
2014-06-03 01:30:22 +08:00
|
|
|
size_t length,
|
2011-04-05 02:18:57 +08:00
|
|
|
bool writeable)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2016-03-03 08:51:40 +08:00
|
|
|
if (filespec != nullptr)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP));
|
2013-02-07 01:22:03 +08:00
|
|
|
if (log)
|
|
|
|
{
|
2013-04-30 01:25:54 +08:00
|
|
|
log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec(file=\"%s\", offset=0x%" PRIx64 ", length=0x%" PRIx64 ", writeable=%i",
|
|
|
|
filespec->GetPath().c_str(),
|
2013-02-07 01:22:03 +08:00
|
|
|
offset,
|
2014-06-03 01:30:22 +08:00
|
|
|
(uint64_t)length,
|
2013-02-07 01:22:03 +08:00
|
|
|
writeable);
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
char path[PATH_MAX];
|
2012-10-05 06:47:07 +08:00
|
|
|
if (filespec->GetPath(path, sizeof(path)))
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2012-01-06 08:43:54 +08:00
|
|
|
uint32_t options = File::eOpenOptionRead;
|
2011-04-05 02:18:57 +08:00
|
|
|
if (writeable)
|
2012-01-06 08:43:54 +08:00
|
|
|
options |= File::eOpenOptionWrite;
|
2011-04-05 02:18:57 +08:00
|
|
|
|
2012-01-06 08:43:54 +08:00
|
|
|
File file;
|
|
|
|
Error error (file.Open(path, options));
|
|
|
|
if (error.Success())
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2011-04-05 02:18:57 +08:00
|
|
|
const bool fd_is_file = true;
|
2013-01-26 02:06:21 +08:00
|
|
|
return MemoryMapFromFileDescriptor (file.GetDescriptor(), offset, length, writeable, fd_is_file);
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// We should only get here if there was an error
|
|
|
|
Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
2014-06-03 01:30:22 +08:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
static size_t win32memmapalignment = 0;
|
|
|
|
void LoadWin32MemMapAlignment ()
|
|
|
|
{
|
|
|
|
SYSTEM_INFO data;
|
|
|
|
GetSystemInfo(&data);
|
|
|
|
win32memmapalignment = data.dwAllocationGranularity;
|
|
|
|
}
|
2013-09-27 02:52:08 +08:00
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// The file descriptor FD is assumed to already be opened as read only
|
|
|
|
// and the STAT structure is assumed to a valid pointer and already
|
|
|
|
// containing valid data from a call to stat().
|
|
|
|
//
|
|
|
|
// Memory map FILE_LENGTH bytes in FILE starting FILE_OFFSET bytes into
|
2010-06-09 17:53:11 +08:00
|
|
|
// the file. If FILE_LENGTH is set to SIZE_MAX, then map as many bytes
|
2010-06-09 00:52:24 +08:00
|
|
|
// as possible.
|
|
|
|
//
|
|
|
|
// RETURNS
|
|
|
|
// Number of bytes mapped starting from the requested offset.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
size_t
|
2011-04-05 02:18:57 +08:00
|
|
|
DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
|
2013-03-15 02:31:44 +08:00
|
|
|
lldb::offset_t offset,
|
2014-06-03 01:30:22 +08:00
|
|
|
size_t length,
|
2011-04-05 02:18:57 +08:00
|
|
|
bool writeable,
|
|
|
|
bool fd_is_file)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
2013-03-28 07:08:40 +08:00
|
|
|
Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP|LIBLLDB_LOG_VERBOSE));
|
2013-02-07 01:22:03 +08:00
|
|
|
if (log)
|
|
|
|
{
|
2014-06-03 01:30:22 +08:00
|
|
|
log->Printf("DataBufferMemoryMap::MemoryMapFromFileDescriptor(fd=%i, offset=0x%" PRIx64 ", length=0x%" PRIx64 ", writeable=%i, fd_is_file=%i)",
|
2013-02-07 01:22:03 +08:00
|
|
|
fd,
|
|
|
|
offset,
|
2014-06-03 01:30:22 +08:00
|
|
|
(uint64_t)length,
|
2013-02-07 01:22:03 +08:00
|
|
|
writeable,
|
|
|
|
fd_is_file);
|
|
|
|
}
|
2013-08-23 20:44:05 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
HANDLE handle = (HANDLE)_get_osfhandle(fd);
|
|
|
|
DWORD file_size_low, file_size_high;
|
|
|
|
file_size_low = GetFileSize(handle, &file_size_high);
|
2014-06-03 01:30:22 +08:00
|
|
|
const lldb::offset_t file_size = llvm::Make_64(file_size_high, file_size_low);
|
|
|
|
const lldb::offset_t max_bytes_available = file_size - offset;
|
|
|
|
const size_t max_bytes_mappable = (size_t)std::min<lldb::offset_t>(SIZE_MAX, max_bytes_available);
|
|
|
|
if (length == SIZE_MAX || length > max_bytes_mappable)
|
2013-08-23 20:44:05 +08:00
|
|
|
{
|
|
|
|
// Cap the length if too much data was requested
|
2014-06-03 01:30:22 +08:00
|
|
|
length = max_bytes_mappable;
|
2013-08-23 20:44:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
2016-03-03 08:51:40 +08:00
|
|
|
HANDLE fileMapping = CreateFileMapping(handle, nullptr, writeable ? PAGE_READWRITE : PAGE_READONLY, file_size_high, file_size_low, nullptr);
|
|
|
|
if (fileMapping != nullptr)
|
2013-08-23 20:44:05 +08:00
|
|
|
{
|
2014-06-03 01:30:22 +08:00
|
|
|
if (win32memmapalignment == 0) LoadWin32MemMapAlignment();
|
|
|
|
lldb::offset_t realoffset = offset;
|
|
|
|
lldb::offset_t delta = 0;
|
|
|
|
if (realoffset % win32memmapalignment != 0) {
|
|
|
|
realoffset = realoffset / win32memmapalignment * win32memmapalignment;
|
|
|
|
delta = offset - realoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPVOID data = MapViewOfFile(fileMapping, writeable ? FILE_MAP_WRITE : FILE_MAP_READ, 0, realoffset, length + delta);
|
|
|
|
m_mmap_addr = (uint8_t *)data;
|
|
|
|
if (!data) {
|
|
|
|
Error error;
|
|
|
|
error.SetErrorToErrno ();
|
2013-09-27 02:52:08 +08:00
|
|
|
} else {
|
2014-06-03 01:30:22 +08:00
|
|
|
m_data = m_mmap_addr + delta;
|
|
|
|
m_size = length;
|
|
|
|
}
|
2013-08-23 20:44:05 +08:00
|
|
|
CloseHandle(fileMapping);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
2010-06-09 00:52:24 +08:00
|
|
|
struct stat stat;
|
|
|
|
if (::fstat(fd, &stat) == 0)
|
|
|
|
{
|
2014-04-02 11:51:35 +08:00
|
|
|
if (S_ISREG(stat.st_mode) &&
|
|
|
|
(stat.st_size > static_cast<off_t>(offset)))
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2010-07-10 04:39:50 +08:00
|
|
|
const size_t max_bytes_available = stat.st_size - offset;
|
2010-06-09 17:53:11 +08:00
|
|
|
if (length == SIZE_MAX)
|
2010-07-10 04:39:50 +08:00
|
|
|
{
|
|
|
|
length = max_bytes_available;
|
|
|
|
}
|
|
|
|
else if (length > max_bytes_available)
|
|
|
|
{
|
|
|
|
// Cap the length if too much data was requested
|
|
|
|
length = max_bytes_available;
|
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
if (length > 0)
|
|
|
|
{
|
2011-04-05 02:18:57 +08:00
|
|
|
int prot = PROT_READ;
|
2016-05-21 03:18:20 +08:00
|
|
|
int flags = MAP_PRIVATE;
|
2011-04-05 02:18:57 +08:00
|
|
|
if (writeable)
|
|
|
|
prot |= PROT_WRITE;
|
2016-05-21 03:18:20 +08:00
|
|
|
else
|
|
|
|
flags |= MAP_EXTRA_HOST_READ_FLAGS;
|
2011-04-05 02:18:57 +08:00
|
|
|
|
|
|
|
if (fd_is_file)
|
|
|
|
flags |= MAP_FILE;
|
|
|
|
|
2016-03-03 08:51:40 +08:00
|
|
|
m_mmap_addr = (uint8_t *)::mmap(nullptr, length, prot, flags, fd, offset);
|
2013-02-07 01:22:03 +08:00
|
|
|
Error error;
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
if (m_mmap_addr == (void*)-1)
|
|
|
|
{
|
2011-04-05 02:18:57 +08:00
|
|
|
error.SetErrorToErrno ();
|
|
|
|
if (error.GetError() == EINVAL)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
// We may still have a shot at memory mapping if we align things correctly
|
2014-08-20 01:18:29 +08:00
|
|
|
size_t page_offset = offset % HostInfo::GetPageSize();
|
2010-06-09 00:52:24 +08:00
|
|
|
if (page_offset != 0)
|
|
|
|
{
|
2016-03-03 08:51:40 +08:00
|
|
|
m_mmap_addr = (uint8_t *)::mmap(nullptr, length + page_offset, prot, flags, fd, offset - page_offset);
|
2010-06-09 00:52:24 +08:00
|
|
|
if (m_mmap_addr == (void*)-1)
|
|
|
|
{
|
|
|
|
// Failed to map file
|
2016-03-03 08:51:40 +08:00
|
|
|
m_mmap_addr = nullptr;
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
2016-03-03 08:51:40 +08:00
|
|
|
else if (m_mmap_addr != nullptr)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
// We recovered and were able to memory map
|
|
|
|
// after we aligned things to page boundaries
|
|
|
|
|
|
|
|
// Save the actual mmap'ed size
|
|
|
|
m_mmap_size = length + page_offset;
|
2015-06-18 13:27:05 +08:00
|
|
|
// Our data is at an offset into the mapped data
|
2010-06-09 00:52:24 +08:00
|
|
|
m_data = m_mmap_addr + page_offset;
|
2015-06-18 13:27:05 +08:00
|
|
|
// Our pretend size is the size that was requested
|
2010-06-09 00:52:24 +08:00
|
|
|
m_size = length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-26 14:39:51 +08:00
|
|
|
if (error.GetError() == ENOMEM)
|
|
|
|
{
|
2012-11-30 05:49:15 +08:00
|
|
|
error.SetErrorStringWithFormat("could not allocate %" PRId64 " bytes of memory to mmap in file", (uint64_t) length);
|
2012-04-26 14:39:51 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// We were able to map the requested data in one chunk
|
|
|
|
// where our mmap and actual data are the same.
|
|
|
|
m_mmap_size = length;
|
|
|
|
m_data = m_mmap_addr;
|
|
|
|
m_size = length;
|
|
|
|
}
|
2013-02-07 01:22:03 +08:00
|
|
|
|
|
|
|
if (log)
|
|
|
|
{
|
2015-04-09 04:23:52 +08:00
|
|
|
log->Printf(
|
|
|
|
"DataBufferMemoryMap::MemoryMapFromFileSpec() m_mmap_addr = %p, m_mmap_size = %" PRIu64
|
|
|
|
", error = %s",
|
|
|
|
(void *)m_mmap_addr, (uint64_t)m_mmap_size, error.AsCString());
|
2013-02-07 01:22:03 +08:00
|
|
|
}
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-08-23 20:44:05 +08:00
|
|
|
#endif
|
2010-06-09 00:52:24 +08:00
|
|
|
}
|
|
|
|
return GetByteSize ();
|
|
|
|
}
|