1 //===-- DataBufferMemoryMap.cpp ---------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
16 #include "lldb/Host/windows/windows.h"
21 #include "lldb/Core/DataBufferMemoryMap.h"
22 #include "lldb/Core/Error.h"
23 #include "lldb/Host/File.h"
24 #include "lldb/Host/FileSpec.h"
25 #include "lldb/Host/Host.h"
26 #include "lldb/Core/Log.h"
27 #include "lldb/lldb-private-log.h"
30 using namespace lldb_private;
32 //----------------------------------------------------------------------
33 // Default Constructor
34 //----------------------------------------------------------------------
35 DataBufferMemoryMap::DataBufferMemoryMap() :
43 //----------------------------------------------------------------------
44 // Virtual destructor since this class inherits from a pure virtual
46 //----------------------------------------------------------------------
47 DataBufferMemoryMap::~DataBufferMemoryMap()
52 //----------------------------------------------------------------------
53 // Return a pointer to the bytes owned by this object, or NULL if
54 // the object contains no bytes.
55 //----------------------------------------------------------------------
57 DataBufferMemoryMap::GetBytes()
62 //----------------------------------------------------------------------
63 // Return a const pointer to the bytes owned by this object, or NULL
64 // if the object contains no bytes.
65 //----------------------------------------------------------------------
67 DataBufferMemoryMap::GetBytes() const
72 //----------------------------------------------------------------------
73 // Return the number of bytes this object currently contains.
74 //----------------------------------------------------------------------
76 DataBufferMemoryMap::GetByteSize() const
81 //----------------------------------------------------------------------
82 // Reverts this object to an empty state by unmapping any memory
83 // that is currently owned.
84 //----------------------------------------------------------------------
86 DataBufferMemoryMap::Clear()
88 if (m_mmap_addr != NULL)
90 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP));
92 log->Printf("DataBufferMemoryMap::Clear() m_mmap_addr = %p, m_mmap_size = %zu", m_mmap_addr, m_mmap_size);
94 UnmapViewOfFile(m_mmap_addr);
96 ::munmap((void *)m_mmap_addr, m_mmap_size);
105 //----------------------------------------------------------------------
106 // Memory map "length" bytes from "file" starting "offset"
107 // bytes into the file. If "length" is set to SIZE_MAX, then
108 // map as many bytes as possible.
110 // Returns the number of bytes mapped starting from the requested
112 //----------------------------------------------------------------------
114 DataBufferMemoryMap::MemoryMapFromFileSpec (const FileSpec* filespec,
115 lldb::offset_t offset,
116 lldb::offset_t length,
119 if (filespec != NULL)
121 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP));
124 log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec(file=\"%s\", offset=0x%" PRIx64 ", length=0x%" PRIx64 ", writeable=%i",
125 filespec->GetPath().c_str(),
131 if (filespec->GetPath(path, sizeof(path)))
133 uint32_t options = File::eOpenOptionRead;
135 options |= File::eOpenOptionWrite;
138 Error error (file.Open(path, options));
141 const bool fd_is_file = true;
142 return MemoryMapFromFileDescriptor (file.GetDescriptor(), offset, length, writeable, fd_is_file);
146 // We should only get here if there was an error
153 static size_t win32memmapalignment = 0;
\r
154 void LoadWin32MemMapAlignment ()
\r
157 GetSystemInfo(&data);
\r
158 win32memmapalignment = data.dwAllocationGranularity;
\r
162 //----------------------------------------------------------------------
163 // The file descriptor FD is assumed to already be opened as read only
164 // and the STAT structure is assumed to a valid pointer and already
165 // containing valid data from a call to stat().
167 // Memory map FILE_LENGTH bytes in FILE starting FILE_OFFSET bytes into
168 // the file. If FILE_LENGTH is set to SIZE_MAX, then map as many bytes
172 // Number of bytes mapped starting from the requested offset.
173 //----------------------------------------------------------------------
175 DataBufferMemoryMap::MemoryMapFromFileDescriptor (int fd,
176 lldb::offset_t offset,
177 lldb::offset_t length,
184 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_MMAP|LIBLLDB_LOG_VERBOSE));
188 log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec(fd=%p, offset=0x%" PRIx64 ", length=0x%" PRIx64 ", writeable=%i, fd_is_file=%i)",
190 log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec(fd=%i, offset=0x%" PRIx64 ", length=0x%" PRIx64 ", writeable=%i, fd_is_file=%i)",
199 HANDLE handle = (HANDLE)_get_osfhandle(fd);
200 DWORD file_size_low, file_size_high;
201 file_size_low = GetFileSize(handle, &file_size_high);
202 const size_t file_size = (file_size_high << 32) | file_size_low;
203 const size_t max_bytes_available = file_size - offset;
204 if (length == SIZE_MAX)
206 length = max_bytes_available;
208 else if (length > max_bytes_available)
210 // Cap the length if too much data was requested
211 length = max_bytes_available;
216 HANDLE fileMapping = CreateFileMapping(handle, NULL, writeable ? PAGE_READWRITE : PAGE_READONLY, file_size_high, file_size_low, NULL);
217 if (fileMapping != NULL)
219 if (win32memmapalignment == 0) LoadWin32MemMapAlignment();
\r
220 lldb::offset_t realoffset = offset;
\r
221 lldb::offset_t delta = 0;
\r
222 if (realoffset % win32memmapalignment != 0) {
\r
223 realoffset = realoffset / win32memmapalignment * win32memmapalignment;
\r
224 delta = offset - realoffset;
\r
227 LPVOID data = MapViewOfFile(fileMapping, writeable ? FILE_MAP_WRITE : FILE_MAP_READ, 0, realoffset, length + delta);
\r
228 m_mmap_addr = (uint8_t *)data;
\r
231 error.SetErrorToErrno ();
\r
233 m_data = m_mmap_addr + delta;
\r
236 CloseHandle(fileMapping);
241 if (::fstat(fd, &stat) == 0)
243 if (S_ISREG(stat.st_mode) && (stat.st_size > offset))
245 const size_t max_bytes_available = stat.st_size - offset;
246 if (length == SIZE_MAX)
248 length = max_bytes_available;
250 else if (length > max_bytes_available)
252 // Cap the length if too much data was requested
253 length = max_bytes_available;
258 int prot = PROT_READ;
262 int flags = MAP_PRIVATE;
266 m_mmap_addr = (uint8_t *)::mmap(NULL, length, prot, flags, fd, offset);
269 if (m_mmap_addr == (void*)-1)
271 error.SetErrorToErrno ();
272 if (error.GetError() == EINVAL)
274 // We may still have a shot at memory mapping if we align things correctly
275 size_t page_offset = offset % Host::GetPageSize();
276 if (page_offset != 0)
278 m_mmap_addr = (uint8_t *)::mmap(NULL, length + page_offset, prot, flags, fd, offset - page_offset);
279 if (m_mmap_addr == (void*)-1)
281 // Failed to map file
284 else if (m_mmap_addr != NULL)
286 // We recovered and were able to memory map
287 // after we aligned things to page boundaries
289 // Save the actual mmap'ed size
290 m_mmap_size = length + page_offset;
291 // Our data is at an offset into the the mapped data
292 m_data = m_mmap_addr + page_offset;
293 // Our pretend size is the size that was requestd
298 if (error.GetError() == ENOMEM)
300 error.SetErrorStringWithFormat("could not allocate %" PRId64 " bytes of memory to mmap in file", (uint64_t) length);
305 // We were able to map the requested data in one chunk
306 // where our mmap and actual data are the same.
307 m_mmap_size = length;
308 m_data = m_mmap_addr;
314 log->Printf("DataBufferMemoryMap::MemoryMapFromFileSpec() m_mmap_addr = %p, m_mmap_size = %zu, error = %s",
315 m_mmap_addr, m_mmap_size, error.AsCString());
322 return GetByteSize ();