1 //===-- ConnectionSharedMemory.cpp ----------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "lldb/Core/ConnectionSharedMemory.h"
11
12 // C Includes
13 #include <errno.h>
14 #include <stdlib.h>
15 #ifdef _WIN32
16 #include "lldb/Host/windows/windows.h"
17 #else
18 #include <sys/file.h>
19 #include <sys/mman.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #endif
23
24 // C++ Includes
25 // Other libraries and framework includes
26 // Project includes
27 #include "lldb/lldb-private-log.h"
28 #include "lldb/Core/Communication.h"
29 #include "lldb/Core/Log.h"
30
31 using namespace lldb;
32 using namespace lldb_private;
33
ConnectionSharedMemory()34 ConnectionSharedMemory::ConnectionSharedMemory () :
35 Connection(),
36 m_name(),
37 m_fd (-1),
38 m_mmap()
39 {
40 }
41
~ConnectionSharedMemory()42 ConnectionSharedMemory::~ConnectionSharedMemory ()
43 {
44 Disconnect (NULL);
45 }
46
47 bool
IsConnected() const48 ConnectionSharedMemory::IsConnected () const
49 {
50 return m_fd >= 0;
51 }
52
53 ConnectionStatus
Connect(const char * s,Error * error_ptr)54 ConnectionSharedMemory::Connect (const char *s, Error *error_ptr)
55 {
56 // if (s && s[0])
57 // {
58 // if (strstr(s, "shm-create://"))
59 // {
60 // }
61 // else if (strstr(s, "shm-connect://"))
62 // {
63 // }
64 // if (error_ptr)
65 // error_ptr->SetErrorStringWithFormat ("unsupported connection URL: '%s'", s);
66 // return eConnectionStatusError;
67 // }
68 if (error_ptr)
69 error_ptr->SetErrorString("invalid connect arguments");
70 return eConnectionStatusError;
71 }
72
73 ConnectionStatus
Disconnect(Error * error_ptr)74 ConnectionSharedMemory::Disconnect (Error *error_ptr)
75 {
76 m_mmap.Clear();
77 if (!m_name.empty())
78 {
79 #ifdef _WIN32
80 close(m_fd);
81 m_fd = -1;
82 #else
83 shm_unlink (m_name.c_str());
84 #endif
85 m_name.clear();
86 }
87 return eConnectionStatusSuccess;
88 }
89
90 size_t
Read(void * dst,size_t dst_len,uint32_t timeout_usec,ConnectionStatus & status,Error * error_ptr)91 ConnectionSharedMemory::Read (void *dst,
92 size_t dst_len,
93 uint32_t timeout_usec,
94 ConnectionStatus &status,
95 Error *error_ptr)
96 {
97 status = eConnectionStatusSuccess;
98 return 0;
99 }
100
101 size_t
Write(const void * src,size_t src_len,ConnectionStatus & status,Error * error_ptr)102 ConnectionSharedMemory::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
103 {
104 status = eConnectionStatusSuccess;
105 return 0;
106 }
107
108 ConnectionStatus
BytesAvailable(uint32_t timeout_usec,Error * error_ptr)109 ConnectionSharedMemory::BytesAvailable (uint32_t timeout_usec, Error *error_ptr)
110 {
111 return eConnectionStatusLostConnection;
112 }
113
114 ConnectionStatus
Open(bool create,const char * name,size_t size,Error * error_ptr)115 ConnectionSharedMemory::Open (bool create, const char *name, size_t size, Error *error_ptr)
116 {
117 if (m_fd != -1)
118 {
119 if (error_ptr)
120 error_ptr->SetErrorString("already open");
121 return eConnectionStatusError;
122 }
123
124 m_name.assign (name);
125
126 #ifdef _WIN32
127 HANDLE handle;
128 if (create)
129 handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, (DWORD)(size >> 32), (DWORD)(size), name);
130 else
131 handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name);
132
133 m_fd = _open_osfhandle((intptr_t)handle, 0);
134 #else
135 int oflag = O_RDWR;
136 if (create)
137 oflag |= O_CREAT;
138 m_fd = ::shm_open (m_name.c_str(), oflag, S_IRUSR|S_IWUSR);
139
140 if (create)
141 ::ftruncate (m_fd, size);
142 #endif
143
144 if (m_mmap.MemoryMapFromFileDescriptor(m_fd, 0, size, true, false) == size)
145 return eConnectionStatusSuccess;
146
147 Disconnect(NULL);
148 return eConnectionStatusError;
149 }
150
151