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