1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // A utility class to manage the RPC session and the associated memory mappings.
16 : #include "syzygy/trace/client/rpc_session.h"
17 :
18 : #include "sawbuck/common/com_utils.h"
19 : #include "syzygy/trace/client/client_utils.h"
20 : #include "syzygy/trace/protocol/call_trace_defs.h"
21 : #include "syzygy/trace/rpc/call_trace_rpc.h"
22 : #include "syzygy/trace/rpc/rpc_helpers.h"
23 :
24 : namespace trace {
25 : namespace client {
26 :
27 : RpcSession::RpcSession()
28 : : rpc_binding_(NULL),
29 : session_handle_(NULL),
30 : flags_(0),
31 E : is_disabled_(false) {
32 E : }
33 :
34 E : RpcSession::~RpcSession() {
35 E : FreeSharedMemory();
36 E : }
37 :
38 E : bool RpcSession::MapSegmentBuffer(TraceFileSegment* segment) {
39 E : DCHECK(segment != NULL);
40 :
41 : HANDLE mem_handle =
42 E : reinterpret_cast<HANDLE>(segment->buffer_info.shared_memory_handle);
43 :
44 : // Get (or set) the mapping between the handle we've received and the
45 : // corresponding mapped base pointer. Note that the shared_memory_handles_
46 : // map is shared across threads, so we need to hold the shared_memory_lock_
47 : // when we access/update it. This should be the only synchronization point
48 : // in the call trace client library (other than the initial creation of the
49 : // RpcSession object, of course).
50 : {
51 E : base::AutoLock scoped_lock(shared_memory_lock_);
52 :
53 E : uint8*& base_ptr = shared_memory_handles_[mem_handle];
54 E : if (base_ptr == NULL) {
55 : base_ptr = reinterpret_cast<uint8*>(
56 : ::MapViewOfFile(mem_handle, FILE_MAP_WRITE, 0, 0,
57 E : segment->buffer_info.mapping_size));
58 E : if (base_ptr == NULL) {
59 i : DWORD error = ::GetLastError();
60 i : LOG(ERROR) << "Failed to map view of shared memory: "
61 : << com::LogWe(error) << ".";
62 i : ignore_result(::CloseHandle(mem_handle));
63 i : shared_memory_handles_.erase(mem_handle);
64 i : return false;
65 : }
66 : }
67 :
68 : segment->base_ptr =
69 E : base_ptr + segment->buffer_info.buffer_offset;
70 E : }
71 :
72 E : segment->header = NULL;
73 E : segment->write_ptr = segment->base_ptr;
74 : segment->end_ptr =
75 E : segment->base_ptr + segment->buffer_info.buffer_size;
76 E : segment->WriteSegmentHeader(session_handle_);
77 :
78 E : DCHECK(segment->header != NULL);
79 :
80 E : return true;
81 E : }
82 :
83 E : bool RpcSession::CreateSession(TraceFileSegment* segment) {
84 E : DCHECK(session_handle_ == NULL);
85 E : DCHECK(rpc_binding_ == NULL);
86 :
87 E : std::wstring protocol;
88 E : std::wstring endpoint;
89 E : ::GetSyzygyCallTraceRpcProtocol(&protocol);
90 E : ::GetSyzygyCallTraceRpcEndpoint(instance_id_, &endpoint);
91 :
92 E : if (!CreateRpcBinding(protocol, endpoint, &rpc_binding_)) {
93 i : is_disabled_ = true;
94 i : return false;
95 : }
96 :
97 E : DCHECK(rpc_binding_ != 0);
98 :
99 : bool succeeded = InvokeRpc(CallTraceClient_CreateSession,
100 : rpc_binding_,
101 : &session_handle_,
102 : &segment->buffer_info,
103 E : &flags_).succeeded();
104 :
105 E : if (!succeeded) {
106 E : LOG(ERROR) << "Failed to create call trace session!";
107 E : is_disabled_ = true;
108 E : return false;
109 : }
110 :
111 E : if ((flags_ & TRACE_FLAG_BATCH_ENTER) != 0) {
112 : // Batch mode is mutually exclusive of all other flags.
113 E : flags_ = TRACE_FLAG_BATCH_ENTER;
114 : }
115 :
116 E : if (!MapSegmentBuffer(segment)) {
117 i : is_disabled_ = true;
118 i : return false;
119 : }
120 :
121 E : return true;
122 E : }
123 :
124 E : bool RpcSession::AllocateBuffer(TraceFileSegment* segment) {
125 E : DCHECK(IsTracing());
126 E : DCHECK(segment != NULL);
127 :
128 : bool succeeded = InvokeRpc(CallTraceClient_AllocateBuffer,
129 : session_handle_,
130 E : &segment->buffer_info).succeeded();
131 :
132 E : return succeeded ? MapSegmentBuffer(segment) : false;
133 E : }
134 :
135 E : bool RpcSession::AllocateBuffer(size_t min_size, TraceFileSegment* segment) {
136 E : DCHECK(IsTracing());
137 E : DCHECK(segment != NULL);
138 :
139 : // We want the actual buffer to have the provided minimum size. The call is
140 : // going to prepend the buffer with a RecordPrefix and a
141 : // TraceFileSegmentHeader, so we make room for those.
142 : const size_t kHeaderSize = sizeof(RecordPrefix) +
143 E : sizeof(TraceFileSegmentHeader);
144 :
145 : bool succeeded = InvokeRpc(CallTraceClient_AllocateLargeBuffer,
146 : session_handle_,
147 : min_size + kHeaderSize,
148 E : &segment->buffer_info).succeeded();
149 E : if (!succeeded)
150 i : return false;
151 :
152 E : if (!MapSegmentBuffer(segment))
153 i : return false;
154 :
155 : // We want to make sure the mapped buffer has sufficient size.
156 E : DCHECK(segment->CanAllocateRaw(min_size));
157 :
158 E : return true;
159 E : }
160 :
161 E : bool RpcSession::ExchangeBuffer(TraceFileSegment* segment) {
162 E : DCHECK(IsTracing());
163 E : DCHECK(segment != NULL);
164 :
165 : bool succeeded = InvokeRpc(CallTraceClient_ExchangeBuffer,
166 : session_handle_,
167 E : &segment->buffer_info).succeeded();
168 :
169 E : return succeeded ? MapSegmentBuffer(segment) : false;
170 E : }
171 :
172 E : bool RpcSession::ReturnBuffer(TraceFileSegment* segment) {
173 E : DCHECK(IsTracing());
174 E : DCHECK(segment != NULL);
175 :
176 : return InvokeRpc(CallTraceClient_ReturnBuffer,
177 : session_handle_,
178 E : &segment->buffer_info).succeeded();
179 E : }
180 :
181 E : bool RpcSession::CloseSession() {
182 E : DCHECK(IsTracing());
183 :
184 : bool succeeded = InvokeRpc(CallTraceClient_CloseSession,
185 E : &session_handle_).succeeded();
186 :
187 E : ignore_result(::RpcBindingFree(&rpc_binding_));
188 E : rpc_binding_ = NULL;
189 :
190 E : return succeeded;
191 E : }
192 :
193 E : void RpcSession::FreeSharedMemory() {
194 E : base::AutoLock scoped_lock_(shared_memory_lock_);
195 :
196 E : if (shared_memory_handles_.empty())
197 E : return;
198 :
199 E : SharedMemoryHandleMap::iterator it = shared_memory_handles_.begin();
200 E : for (; it != shared_memory_handles_.end(); ++it) {
201 E : DCHECK(it->second != NULL);
202 E : if (::UnmapViewOfFile(it->second) == 0) {
203 i : DWORD error = ::GetLastError();
204 i : LOG(WARNING) << "Failed to unmap memory handle: " << com::LogWe(error);
205 : }
206 :
207 E : if (::CloseHandle(it->first) == 0) {
208 i : DWORD error = ::GetLastError();
209 i : LOG(WARNING) << "Failed to close memory handle: " << com::LogWe(error);
210 : }
211 E : }
212 :
213 E : shared_memory_handles_.clear();
214 E : }
215 :
216 : } // namespace client
217 : } // namespace trace
|