1 : // Copyright 2012 Google Inc.
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 : #include "syzygy/instrument/mutators/add_bb_ranges_stream.h"
16 :
17 : #include "syzygy/common/basic_block_frequency_data.h"
18 : #include "syzygy/pdb/pdb_byte_stream.h"
19 :
20 : namespace instrument {
21 : namespace mutators {
22 :
23 : const char AddBasicBlockRangesStreamPdbMutator::kMutatorName[] =
24 : "AddBasicBlockRangesStreamPdbMutator";
25 :
26 : bool AddBasicBlockRangesStreamPdbMutator::AddNamedStreams(
27 E : const pdb::PdbFile& pdb_file) {
28 E : if (bb_ranges_.size() == 0) {
29 E : LOG(INFO) << "Basic-block addresses vector is empty. Not adding stream.";
30 E : return true;
31 : }
32 :
33 : // Create the stream.
34 E : scoped_refptr<pdb::PdbByteStream> stream(new pdb::PdbByteStream);
35 : CHECK(stream->Init(reinterpret_cast<const uint8*>(&bb_ranges_.at(0)),
36 E : bb_ranges_.size() * sizeof(bb_ranges_.at(0))));
37 :
38 : // Add the stream to the PDB.
39 E : if (!SetNamedStream(common::kBasicBlockRangesStreamName, stream.get())) {
40 : // This should not happen, as it indicates we are trying to doubly
41 : // instrument a given binary.
42 E : LOG(ERROR) << "Basic-block ranges stream already exists.";
43 E : return false;
44 : }
45 :
46 E : return true;
47 E : }
48 :
49 : } // namespace mutators
50 : } // namespace instrument
|