]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/ConnectionSharedMemory.cpp
Update llvm/clang to r240225.
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / lldb / source / Core / ConnectionSharedMemory.cpp
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/lldb-private-log.h"
30 #include "lldb/Core/Communication.h"
31 #include "lldb/Core/Log.h"
32
33 using namespace lldb;
34 using namespace lldb_private;
35
36 ConnectionSharedMemory::ConnectionSharedMemory () :
37     Connection(),
38     m_name(),
39     m_fd (-1),
40     m_mmap()
41 {
42 }
43
44 ConnectionSharedMemory::~ConnectionSharedMemory ()
45 {
46     Disconnect (NULL);
47 }
48
49 bool
50 ConnectionSharedMemory::IsConnected () const
51 {
52     return m_fd >= 0;
53 }
54
55 ConnectionStatus
56 ConnectionSharedMemory::Connect (const char *s, Error *error_ptr)
57 {
58 //    if (s && s[0])
59 //    {
60 //        if (strstr(s, "shm-create://"))
61 //        {
62 //        }
63 //        else if (strstr(s, "shm-connect://"))
64 //        {
65 //        }
66 //        if (error_ptr)
67 //            error_ptr->SetErrorStringWithFormat ("unsupported connection URL: '%s'", s);
68 //        return eConnectionStatusError;
69 //    }
70     if (error_ptr)
71         error_ptr->SetErrorString("invalid connect arguments");
72     return eConnectionStatusError;
73 }
74
75 ConnectionStatus
76 ConnectionSharedMemory::Disconnect (Error *error_ptr)
77 {
78     m_mmap.Clear();
79     if (!m_name.empty())
80     {
81 #ifdef _WIN32
82         close(m_fd);
83         m_fd = -1;
84 #else
85         shm_unlink (m_name.c_str());
86 #endif
87         m_name.clear();
88     }
89     return eConnectionStatusSuccess;
90 }
91
92 size_t
93 ConnectionSharedMemory::Read (void *dst, 
94                               size_t dst_len, 
95                               uint32_t timeout_usec,
96                               ConnectionStatus &status, 
97                               Error *error_ptr)
98 {
99     status = eConnectionStatusSuccess;
100     return 0;
101 }
102
103 size_t
104 ConnectionSharedMemory::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
105 {
106     status = eConnectionStatusSuccess;
107     return 0;
108 }
109
110 ConnectionStatus
111 ConnectionSharedMemory::BytesAvailable (uint32_t timeout_usec, Error *error_ptr)
112 {
113     return eConnectionStatusLostConnection;
114 }
115
116 ConnectionStatus
117 ConnectionSharedMemory::Open (bool create, const char *name, size_t size, Error *error_ptr)
118 {
119     if (m_fd != -1)
120     {
121         if (error_ptr)
122             error_ptr->SetErrorString("already open");
123         return eConnectionStatusError;
124     }
125     
126     m_name.assign (name);
127
128 #ifdef _WIN32
129     HANDLE handle;
130     if (create) {
131         handle = CreateFileMapping(
132             INVALID_HANDLE_VALUE,
133             NULL,
134             PAGE_READWRITE,
135             llvm::Hi_32(size),
136             llvm::Lo_32(size),
137             name);
138     }
139     else
140         handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name);
141
142     m_fd = _open_osfhandle((intptr_t)handle, 0);
143 #else
144     int oflag = O_RDWR;
145     if (create)
146         oflag |= O_CREAT;
147     m_fd = ::shm_open (m_name.c_str(), oflag, S_IRUSR|S_IWUSR);
148
149     if (create)
150         ::ftruncate (m_fd, size);
151 #endif
152
153     if (m_mmap.MemoryMapFromFileDescriptor(m_fd, 0, size, true, false) == size)
154         return eConnectionStatusSuccess;
155
156     Disconnect(NULL);
157     return eConnectionStatusError;
158 }
159
160 #endif // __ANDROID_NDK__