1 : // Copyright 2013 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 : // Declares the data structure that will be used by various pieces of the
16 : // instrumentation and trace agent's to collect data at runtime.
17 :
18 : #ifndef SYZYGY_COMMON_INDEXED_FREQUENCY_DATA_H_
19 : #define SYZYGY_COMMON_INDEXED_FREQUENCY_DATA_H_
20 :
21 : #include <windows.h>
22 :
23 : #include "base/strings/string_piece.h"
24 : #include "syzygy/common/assertions.h"
25 :
26 m : namespace common {
27 :
28 : #pragma pack(push, 1)
29 :
30 : // This data structure is injected into an instrumented image in a read-write
31 : // section of its own. It will be initialized by the runtime client library
32 : // and is referred to by all of the instrumentation code.
33 m : struct IndexedFrequencyData {
34 :
35 : // Describe the kind of data contained in frequency_data;
36 : // IndexedFrequencyDataTypeName must be maintained if this is changed.
37 m : enum DataType {
38 m : INVALID_DATA_TYPE = 0,
39 m : BASIC_BLOCK_ENTRY = 1,
40 m : BRANCH = 2,
41 m : COVERAGE = 3,
42 m : JUMP_TABLE = 4,
43 m : MAX_DATA_TYPE = 5,
44 m : };
45 :
46 : // An identifier denoting the agent with which this frequency data
47 : // instrumentation is intended to work.
48 m : uint32_t agent_id;
49 :
50 : // The version of the data structure and agent of the toolchain that
51 : // instrumented the binary. If this doesn't match the running client
52 : // library then the whole process should be aborted. This just a simple
53 : // counter which should be updated whenever a non-backwards compatible
54 : // change is made to the data structure or its usage.
55 m : uint32_t version;
56 :
57 : // This points to an array of length 'num_entries' counter elements. At
58 : // link time it is initialized to point to statically allocated array that is
59 : // in the .data section of the image (this is done so that if capture is not
60 : // enabled the binary can still run without crashing). If a single process-
61 : // wide frequency table is needed, the agent may allocate a call-trace buffer
62 : // and redirect this pointer to point into it. Alternatively, it may allocate
63 : // any thread-specific context it requires and refer to this pointer as a
64 : // fall-back measure if tracing is disabled.
65 : //
66 : // The total size (in bytes) of the buffer pointed to is
67 : // num_entries * num_columns * frequency_size.
68 m : void* frequency_data;
69 :
70 : // The number of entries in the frequency table. This is required by the
71 : // runtime client library so it knows how big an array to allocate.
72 m : uint32_t num_entries;
73 :
74 : // The number of columns for each entry.
75 m : uint32_t num_columns;
76 :
77 : // The number of bytes used for each element of frequency_data: 1, 4, or 8.
78 m : uint8_t frequency_size;
79 :
80 : // Each module only needs to be registered once with the call-trace service.
81 : // Our hooks grab various entry points (e.g. TLS initializers and the image
82 : // entry points), so the initialization routine may be called repeatedly. We
83 : // use this to determine whether or not we should try initializing things.
84 : // Upon first entry this is protected by the loader lock and afterwards it
85 : // is only read, so synchronization is not an issue.
86 m : uint8_t initialization_attempted;
87 :
88 : // The type of data associated with this module.
89 m : uint8_t data_type;
90 m : };
91 m : COMPILE_ASSERT_IS_POD(IndexedFrequencyData);
92 :
93 : // Indexed frequency data that is managed as a thread local. Used by the basic
94 : // block entry instrumentation and agent, and variants.
95 m : struct ThreadLocalIndexedFrequencyData {
96 : // The indexed frequency data information common for all agents.
97 m : ::common::IndexedFrequencyData module_data;
98 :
99 : // The TLS slot associated with this module (if any). This allows for the
100 : // frequency trace data to be managed on a per-thread basis, if desired by the
101 : // agent. The TLS index is initialized to TLS_OUT_OF_INDEXES by the
102 : // instrumenter.
103 m : DWORD tls_index;
104 :
105 : // The FS slot associated with this module (if any). This allows for the
106 : // frequency trace data to be managed on a per-thread basis, if desired by the
107 : // agent. An unused slot is initialized to zero by the instrumenter, otherwise
108 : // it is initialized to a slot index between 1 and 4.
109 m : DWORD fs_slot;
110 m : };
111 :
112 : #pragma pack(pop)
113 :
114 : // The basic-block coverage agent ID.
115 m : extern const uint32_t kBasicBlockCoverageAgentId;
116 :
117 : // The basic-block entry counting agent ID.
118 m : extern const uint32_t kBasicBlockEntryAgentId;
119 :
120 : // The jump table counting agent ID.
121 m : extern const uint32_t kJumpTableCountAgentId;
122 :
123 : // The basic-block trace agent version.
124 m : extern const uint32_t kBasicBlockFrequencyDataVersion;
125 :
126 : // The branch trace agent version.
127 m : extern const uint32_t kBranchFrequencyDataVersion;
128 :
129 : // The jump table trace agent version.
130 m : extern const uint32_t kJumpTableFrequencyDataVersion;
131 :
132 : // The name of the basic-block ranges stream added to the PDB by
133 : // any instrumentation employing basic-block trace data.
134 m : extern const char kBasicBlockRangesStreamName[];
135 :
136 : // A string table mapping from DataType to text representation.
137 : // This array must be maintained if enum DataType is changed.
138 m : extern const char* IndexedFrequencyDataTypeName[];
139 :
140 : // Produce a human readable name for the IndexedFrequencyData::DataType.
141 : // @param type type to convert in string.
142 : // @param result on success, receives the textual representation of @p type.
143 m : bool IndexedFrequencyDataTypeToString(IndexedFrequencyData::DataType type,
144 m : std::string* result);
145 :
146 : // Parse a string to get the IndexedFrequencyData::DataType.
147 : // @param str the string to convert.
148 : // @param result on success, receives the DataType value of @p str.
149 m : bool ParseFrequencyDataType(const base::StringPiece& str,
150 m : IndexedFrequencyData::DataType* type);
151 :
152 m : } // namespace common
153 :
154 : #endif // SYZYGY_COMMON_INDEXED_FREQUENCY_DATA_H_
|