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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2011-04-08 21:36:44 +08:00
|
|
|
#include <limits.h>
|
2010-06-09 00:52:24 +08:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
|
|
|
|
#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"
|
2010-06-09 00:52:24 +08:00
|
|
|
#include "lldb/Host/Host.h"
|
|
|
|
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Default Constructor
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
DataBufferMemoryMap::DataBufferMemoryMap() :
|
|
|
|
m_mmap_addr(NULL),
|
|
|
|
m_mmap_size(0),
|
|
|
|
m_data(NULL),
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Return a pointer to the bytes owned by this object, or NULL if
|
|
|
|
// the object contains no bytes.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
uint8_t *
|
|
|
|
DataBufferMemoryMap::GetBytes()
|
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Return a const pointer to the bytes owned by this object, or NULL
|
|
|
|
// if the object contains no bytes.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
const uint8_t *
|
|
|
|
DataBufferMemoryMap::GetBytes() const
|
|
|
|
{
|
|
|
|
return m_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Return the number of bytes this object currently contains.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
size_t
|
|
|
|
DataBufferMemoryMap::GetByteSize() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Reverts this object to an empty state by unmapping any memory
|
|
|
|
// that is currently owned.
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void
|
|
|
|
DataBufferMemoryMap::Clear()
|
|
|
|
{
|
|
|
|
if (m_mmap_addr != NULL)
|
|
|
|
{
|
|
|
|
::munmap((void *)m_mmap_addr, m_mmap_size);
|
|
|
|
m_mmap_addr = NULL;
|
|
|
|
m_mmap_size = 0;
|
|
|
|
m_data = NULL;
|
|
|
|
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,
|
2011-04-05 02:18:57 +08:00
|
|
|
off_t offset,
|
|
|
|
size_t length,
|
|
|
|
bool writeable)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
2012-10-05 06:47:07 +08:00
|
|
|
if (filespec != NULL)
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// 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,
|
|
|
|
off_t offset,
|
|
|
|
size_t length,
|
|
|
|
bool writeable,
|
|
|
|
bool fd_is_file)
|
2010-06-09 00:52:24 +08:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
struct stat stat;
|
|
|
|
if (::fstat(fd, &stat) == 0)
|
|
|
|
{
|
2011-07-08 08:38:03 +08:00
|
|
|
if (S_ISREG(stat.st_mode) && (stat.st_size > 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;
|
|
|
|
if (writeable)
|
|
|
|
prot |= PROT_WRITE;
|
|
|
|
|
2012-01-10 10:53:13 +08:00
|
|
|
int flags = MAP_PRIVATE;
|
2011-04-05 02:18:57 +08:00
|
|
|
if (fd_is_file)
|
|
|
|
flags |= MAP_FILE;
|
|
|
|
|
|
|
|
m_mmap_addr = (uint8_t *)::mmap(NULL, length, prot, flags, fd, offset);
|
2010-06-09 00:52:24 +08:00
|
|
|
|
|
|
|
if (m_mmap_addr == (void*)-1)
|
|
|
|
{
|
2011-04-05 02:18:57 +08:00
|
|
|
Error error;
|
|
|
|
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
|
|
|
|
size_t page_offset = offset % Host::GetPageSize();
|
|
|
|
if (page_offset != 0)
|
|
|
|
{
|
2011-04-05 02:18:57 +08:00
|
|
|
m_mmap_addr = (uint8_t *)::mmap(NULL, 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
|
|
|
|
m_mmap_addr = NULL;
|
|
|
|
}
|
|
|
|
else if (m_mmap_addr != NULL)
|
|
|
|
{
|
|
|
|
// 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;
|
|
|
|
// Our data is at an offset into the the mapped data
|
|
|
|
m_data = m_mmap_addr + page_offset;
|
|
|
|
// Our pretend size is the size that was requestd
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return GetByteSize ();
|
|
|
|
}
|