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 :
17 : #ifndef SYZYGY_TRACE_CLIENT_RPC_SESSION_H_
18 : #define SYZYGY_TRACE_CLIENT_RPC_SESSION_H_
19 :
20 : #include <map>
21 :
22 : #include "base/logging.h"
23 : #include "base/synchronization/lock.h"
24 : #include "syzygy/trace/client/client_utils.h"
25 : #include "syzygy/trace/protocol/call_trace_defs.h"
26 :
27 : namespace trace {
28 : namespace client {
29 :
30 : class RpcSession {
31 : public:
32 : RpcSession();
33 : ~RpcSession();
34 :
35 : // Set the instance identifier for this session.
36 E : void set_instance_id(const base::StringPiece16& instance_id) {
37 E : DCHECK(!IsTracing());
38 E : instance_id_.assign(instance_id.begin(), instance_id.end());
39 E : }
40 :
41 : // @returns the instance ID for this session.
42 E : const std::wstring instance_id() const { return instance_id_; }
43 :
44 : // @name Wrapper and helper functions for the RPC and shared memory calls made
45 : // by the call-trace client.
46 : // @{
47 :
48 : // @note Do not call this function directly unless you know exactly what
49 : // you're doing. For consistent semantics across agents please use
50 : // trace::client::InitializeRpcSession.
51 : bool CreateSession(TraceFileSegment* segment);
52 : bool AllocateBuffer(TraceFileSegment* segment);
53 : bool AllocateBuffer(size_t min_size, TraceFileSegment* segment);
54 : bool ExchangeBuffer(TraceFileSegment* segment);
55 : bool ReturnBuffer(TraceFileSegment* segment);
56 : bool CloseSession();
57 : void FreeSharedMemory();
58 : // @}
59 :
60 : inline bool IsEnabled(unsigned long bit_mask) const {
61 : return (flags_ & bit_mask) != 0;
62 : }
63 :
64 E : bool IsTracing() const {
65 E : return session_handle_ != NULL;
66 E : }
67 :
68 E : bool IsDisabled() const {
69 E : return is_disabled_;
70 E : }
71 :
72 : unsigned long flags() const { return flags_; }
73 :
74 : private:
75 : // Map a tracefile segment buffer into local memory.
76 : bool MapSegmentBuffer(TraceFileSegment* segment);
77 :
78 : // The call trace RPC binding.
79 : handle_t rpc_binding_;
80 :
81 : // The handle to the call trace session. Initialization of the session
82 : // is protected by a lock. This is a separate lock since we don't seem
83 : // to have recursive locks in base.
84 : SessionHandle session_handle_;
85 :
86 : // The set of trace flags returned by the call trace server. These instruct
87 : // the client as to which types of events to capture.
88 : unsigned long flags_;
89 :
90 : // We track the set of shared memory handles we've mapped into the
91 : // process. This allows us to avoid mapping a handle twice, as well
92 : // as letting us know what to clean up on exit. Access to the set
93 : // of handles must be serialized with a lock.
94 : typedef std::map<HANDLE, uint8*> SharedMemoryHandleMap;
95 : base::Lock shared_memory_lock_;
96 : SharedMemoryHandleMap shared_memory_handles_;
97 :
98 : // This becomes true if the client fails to attach to a call trace service.
99 : // This is used to allow the application to run even if no call trace
100 : // service is available.
101 : bool is_disabled_;
102 :
103 : // The (optional) unique id used to differentiate concurrent instances of the
104 : // RPC call-trace logging service.
105 : std::wstring instance_id_;
106 : };
107 :
108 : } // namespace trace::client
109 : } // namespace trace
110 :
111 : #endif // SYZYGY_TRACE_CLIENT_RPC_SESSION_H_
|