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 : // The runtime portion of a basic-block counting agent. This is responsible for
16 : // initializing the RPC connection and per-thread counter buffering on
17 : // demand as necessary as well as saturation incrementing the appropriate
18 : // counter when requested.
19 : //
20 : // The instrumenter can be used to inject a run-time dependency on this
21 : // library as well as to add the appropriate entry-hook code.
22 : // For details on the implementation, see basic_block_entry.cc.
23 :
24 : #ifndef SYZYGY_AGENT_BASIC_BLOCK_ENTRY_BASIC_BLOCK_ENTRY_H_
25 : #define SYZYGY_AGENT_BASIC_BLOCK_ENTRY_BASIC_BLOCK_ENTRY_H_
26 :
27 : #include <windows.h>
28 : #include <winnt.h>
29 : #include <vector>
30 :
31 : #include "base/lazy_instance.h"
32 : #include "base/win/pe_image.h"
33 : #include "syzygy/agent/common/thread_state.h"
34 : #include "syzygy/common/indexed_frequency_data.h"
35 : #include "syzygy/trace/client/rpc_session.h"
36 :
37 m : namespace agent {
38 m : namespace basic_block_entry {
39 :
40 : // The basic-block counting agent.
41 : // @note: There's a single instance of this class.
42 : // TODO(sebmarchand): Rename this class to BasicBlockAgent (or something
43 : // similar) as this is used by various modes of instrumentation (basic block
44 : // entry counting, basic block arc counts, jump table entry counts, etc).
45 m : class BasicBlockEntry {
46 m : public:
47 m : using IndexedFrequencyData = ::common::IndexedFrequencyData;
48 m : using ThreadLocalIndexedFrequencyData =
49 m : ::common::ThreadLocalIndexedFrequencyData;
50 m : using ThreadStateManager = ::agent::common::ThreadStateManager;
51 :
52 : // The size in DWORD of the buffer. We choose a multiple of memory page size.
53 m : static const size_t kBufferSize = 4096;
54 : // The number of entries in the simulated branch predictor cache.
55 m : static const size_t kPredictorCacheSize = 4096;
56 :
57 : // This structure describes the contents of the stack above a call to
58 : // BasicBlockEntry::IncrementIndexedFreqDataHook. A pointer to this structure
59 : // will be given to the IncrementIndexedFreqDataHook by
60 : // _increment_indexed_freq_data.
61 m : struct IncrementIndexedFreqDataFrame;
62 :
63 : // This structure describes the contents of the stack above a call to
64 : // BasicBlockEntry::DllMainEntryHook(). A pointer to this structure will
65 : // be given to the DllMainEntryHook by _indirect_penter_dllmain.
66 m : struct DllMainEntryFrame;
67 :
68 : // This structure describes the contents of the stack above a call to
69 : // BasicBlockEntry::ExeMainEntryHook(). A pointer to this structure will
70 : // be given to the ExeMainEntryHook by _indirect_penter_exemain.
71 m : struct ExeMainEntryFrame;
72 :
73 : // Retrieves the basic block entry singleton instance.
74 m : static BasicBlockEntry* Instance();
75 :
76 : // Called from _increment_indexed_freq_data().
77 m : static void WINAPI IncrementIndexedFreqDataHook(
78 m : IncrementIndexedFreqDataFrame* entry_frame);
79 :
80 : // Called from _branch_enter.
81 m : static void WINAPI BranchEnterHook(
82 m : IncrementIndexedFreqDataFrame* entry_frame);
83 :
84 : // Called from _branch_enter_buffered.
85 m : static void WINAPI BranchEnterBufferedHook(
86 m : IncrementIndexedFreqDataFrame* entry_frame);
87 :
88 : // Called from _branch_exit.
89 m : static void WINAPI BranchExitHook(
90 m : IncrementIndexedFreqDataFrame* entry_frame);
91 :
92 : // Called from _function_enter_slotX.
93 m : template<int S>
94 m : static inline void __fastcall FunctionEnterHookSlot(
95 m : IndexedFrequencyData* module_data);
96 :
97 : // Called from _branch_enter_slotX.
98 m : template <int S>
99 m : static inline void __fastcall BranchEnterHookSlot(uint32_t index);
100 :
101 : // Called from _branch_enter_buffered_slotX.
102 m : template <int S>
103 m : static inline void __fastcall BranchEnterBufferedHookSlot(uint32_t index);
104 :
105 : // Called from _branch_exit_slotX.
106 m : template <int S>
107 m : static inline void __fastcall BranchExitHookSlot(uint32_t index);
108 :
109 : // Called from _indirect_penter_dllmain.
110 m : static void WINAPI DllMainEntryHook(DllMainEntryFrame* entry_frame);
111 :
112 : // Called from _indirect_penter_exemain.
113 m : static void WINAPI ExeMainEntryHook(ExeMainEntryFrame* entry_frame);
114 :
115 m : protected:
116 : // This class defines the per-thread-per-instrumented-module state managed
117 : // by this agent.
118 m : class ThreadState;
119 m : friend class ThreadState;
120 :
121 : // Make sure the LazyInstance can be created.
122 m : friend struct base::DefaultLazyInstanceTraits<BasicBlockEntry>;
123 :
124 m : BasicBlockEntry();
125 m : ~BasicBlockEntry();
126 :
127 : // Initializes the given frequency data element.
128 m : bool InitializeFrequencyData(IndexedFrequencyData* data);
129 :
130 : // Handles EXE startup on ExeMainEntryHook and DLL_PROCESS_ATTACH messages
131 : // received by DllMainEntryHook().
132 m : void OnProcessAttach(IndexedFrequencyData* module_data);
133 :
134 : // Handles DLL_THREAD_DETACH and DLL_PROCESS_DETACH messages received by
135 : // DllMainEntryHook().
136 m : void OnThreadDetach(IndexedFrequencyData* module_data);
137 :
138 : // Registers the module containing @p addr with the call_trace_service.
139 m : void RegisterModule(const void* addr);
140 :
141 : // Register a TLS slot for this module.
142 m : void RegisterFastPathSlot(IndexedFrequencyData* module_data,
143 m : unsigned int slot);
144 :
145 : // Unregister a TLS slot for this module.
146 m : void UnregisterFastPathSlot(IndexedFrequencyData* module_data,
147 m : unsigned int slot);
148 :
149 : // Create the local thread state for the current thread. This should only
150 : // be called if the local thread state has not already been created.
151 m : ThreadState* CreateThreadState(IndexedFrequencyData* module_data);
152 :
153 : // Returns the local thread state for the current thread. If the thread state
154 : // is unavailable, this function returns NULL.
155 m : static ThreadState* GetThreadState(IndexedFrequencyData* module_data);
156 :
157 : // Returns the local thread state for the current thread (when instrumented
158 : // with fast-path).
159 m : template<int S>
160 m : static ThreadState* GetThreadStateSlot();
161 :
162 : // Registered thread local specific slot.
163 m : uint32_t registered_slots_;
164 :
165 : // The RPC session we're logging to/through.
166 m : trace::client::RpcSession session_;
167 :
168 : // A helper to manage the life-cycle of the ThreadState instances allocated
169 : // by this agent.
170 m : ThreadStateManager thread_state_manager_;
171 :
172 : // The trace file segment we're writing module events to. The frequency data
173 : // goes to specially allocated segments that we don't explicitly keep track
174 : // of, but rather that we let live until the client gets torn down.
175 m : trace::client::TraceFileSegment segment_; // Under lock_.
176 :
177 : // Global lock to avoid concurrent segment_ update.
178 m : base::Lock lock_;
179 m : };
180 :
181 m : } // namespace basic_block_entry
182 m : } // namespace agent
183 :
184 : #endif // SYZYGY_AGENT_BASIC_BLOCK_ENTRY_BASIC_BLOCK_ENTRY_H_
|