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 : // Declares structure and functions useful to grinders that process basic-
16 : // block frequency data.
17 :
18 : #ifndef SYZYGY_GRINDER_BASIC_BLOCK_UTIL_H_
19 : #define SYZYGY_GRINDER_BASIC_BLOCK_UTIL_H_
20 :
21 : #include <map>
22 : #include <vector>
23 :
24 : #include "base/files/file_path.h"
25 : #include "sawbuck/sym_util/types.h"
26 : #include "syzygy/grinder/line_info.h"
27 : #include "syzygy/pe/pe_file.h"
28 : #include "syzygy/trace/protocol/call_trace_defs.h"
29 :
30 m : namespace grinder {
31 m : namespace basic_block_util {
32 :
33 : // Address related types.
34 m : typedef core::RelativeAddress RelativeAddress;
35 m : typedef core::AddressRange<RelativeAddress, size_t> RelativeAddressRange;
36 m : typedef std::vector<RelativeAddressRange> RelativeAddressRangeVector;
37 :
38 : // Module information.
39 m : typedef sym_util::ModuleInformation ModuleInformation;
40 :
41 : // Compares module information on identity properties alone.
42 m : struct ModuleIdentityComparator {
43 m : bool operator()(const ModuleInformation& lhs, const ModuleInformation& rhs);
44 m : };
45 :
46 : // Type definitions for the basic block entry count data.
47 m : typedef int32 EntryCountType;
48 m : typedef int32 BasicBlockOffset;
49 : // An entry count map maps from the relative virtual address of the first
50 : // instruction or data byte in the basic block, to its entry count.
51 m : typedef std::map<BasicBlockOffset, EntryCountType> EntryCountMap;
52 :
53 m : typedef std::map<ModuleInformation,
54 m : EntryCountMap,
55 m : ModuleIdentityComparator> ModuleEntryCountMap;
56 :
57 : // This structure holds the information extracted from a PDB file for a
58 : // given module.
59 m : struct PdbInfo {
60 : // The path to this PDB file.
61 m : base::FilePath pdb_path;
62 :
63 : // Line and coverage information for all the source files associated with
64 : // a particular PDB.
65 m : LineInfo line_info;
66 :
67 : // Basic-block addresses for the module associated with a particular PDB.
68 : // Used to transform basic-block frequency data to line visits via line_info.
69 m : RelativeAddressRangeVector bb_ranges;
70 m : };
71 :
72 m : typedef std::map<ModuleInformation,
73 m : PdbInfo,
74 m : ModuleIdentityComparator> PdbInfoMap;
75 :
76 : // A helper function to populate a ModuleInformation structure from a PE
77 : // signature.
78 m : void InitModuleInfo(const pe::PEFile::Signature& signature,
79 m : ModuleInformation* module_info);
80 :
81 : // Given a module @p signature, find the matching @p module_information
82 : // and @p entry_count_map in the given @p module_entry_count_map.
83 : // @returns true on success, false otherwise.
84 m : bool FindEntryCountMap(const pe::PEFile::Signature& signature,
85 m : const ModuleEntryCountMap& module_entry_count_map,
86 m : const EntryCountMap** entry_count_map);
87 :
88 : // A helper function to populate @p bb_ranges from the PDB file given by
89 : // @p pdb_path.
90 : // @returns true on success, false otherwise.
91 m : bool LoadBasicBlockRanges(const base::FilePath& pdb_path,
92 m : RelativeAddressRangeVector* bb_ranges);
93 :
94 : // Loads a new or retrieves the cached PDB info for the given module. This
95 : // also caches failures; it will not re-attempt to look up PDB information
96 : // if a previous attempt for the same module failed.
97 : // @param pdb_info_cache the cache of PDB info already seen.
98 : // @param module_info the info representing the module to find PDB info for.
99 : // @param pdb_info a pointer to the pdb info will be returned here.
100 : // @return true on success, false otherwise.
101 m : bool LoadPdbInfo(PdbInfoMap* pdb_info_cache,
102 m : const ModuleInformation& module_info,
103 m : PdbInfo** pdb_info);
104 :
105 : // @returns true if the given @p size is a valid frequency size.
106 m : bool IsValidFrequencySize(size_t size);
107 :
108 : // @returns the frequency value contained in @p data for the basic_block given
109 : // by @p bb_id.
110 m : uint32 GetFrequency(const TraceIndexedFrequencyData* data, size_t bb_id);
111 :
112 m : } // namespace basic_block_util
113 m : } // namespace grinder
114 :
115 : #endif // SYZYGY_GRINDER_BASIC_BLOCK_UTIL_H_
|