]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/lldb/source/Core/ConnectionSharedMemory.cpp
Update libucl to git version 8d3b186
[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
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 "llvm/Support/MathExtras.h"
28 #include "lldb/lldb-private-log.h"
29 #include "lldb/Core/Communication.h"
30 #include "lldb/Core/Log.h"
31
32 using namespace lldb;
33 using namespace lldb_private;
34
35 ConnectionSharedMemory::ConnectionSharedMemory () :
36     Connection(),
37     m_name(),
38     m_fd (-1),
39     m_mmap()
40 {
41 }
42
43 ConnectionSharedMemory::~ConnectionSharedMemory ()
44 {
45     Disconnect (NULL);
46 }
47
48 bool
49 ConnectionSharedMemory::IsConnected () const
50 {
51     return m_fd >= 0;
52 }
53
54 ConnectionStatus
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
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
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
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 ConnectionStatus
110 ConnectionSharedMemory::BytesAvailable (uint32_t timeout_usec, Error *error_ptr)
111 {
112     return eConnectionStatusLostConnection;
113 }
114
115 ConnectionStatus
116 ConnectionSharedMemory::Open (bool create, const char *name, size_t size, Error *error_ptr)
117 {
118     if (m_fd != -1)
119     {
120         if (error_ptr)
121             error_ptr->SetErrorString("already open");
122         return eConnectionStatusError;
123     }
124     
125     m_name.assign (name);
126
127 #ifdef _WIN32
128     HANDLE handle;
129     if (create) {
130         handle = CreateFileMapping(
131             INVALID_HANDLE_VALUE,
132             NULL,
133             PAGE_READWRITE,
134             llvm::Hi_32(size),
135             llvm::Lo_32(size),
136             name);
137     }
138     else
139         handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name);
140
141     m_fd = _open_osfhandle((intptr_t)handle, 0);
142 #else
143     int oflag = O_RDWR;
144     if (create)
145         oflag |= O_CREAT;
146     m_fd = ::shm_open (m_name.c_str(), oflag, S_IRUSR|S_IWUSR);
147
148     if (create)
149         ::ftruncate (m_fd, size);
150 #endif
151
152     if (m_mmap.MemoryMapFromFileDescriptor(m_fd, 0, size, true, false) == size)
153         return eConnectionStatusSuccess;
154
155     Disconnect(NULL);
156     return eConnectionStatusError;
157 }
158