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 : #include "syzygy/instrument/instrumenters/branch_instrumenter.h"
16 :
17 : #include "base/logging.h"
18 : #include "base/files/file_util.h"
19 : #include "base/strings/string_number_conversions.h"
20 : #include "syzygy/application/application.h"
21 : #include "syzygy/pe/image_filter.h"
22 :
23 : namespace instrument {
24 : namespace instrumenters {
25 :
26 : const char BranchInstrumenter::kAgentDllBasicBlockEntry[] =
27 : "basic_block_entry_client.dll";
28 : const uint32 kNumSlots = 4U;
29 :
30 : BranchInstrumenter::BranchInstrumenter()
31 E : : buffering_(false), fs_slot_(0U) {
32 E : agent_dll_ = kAgentDllBasicBlockEntry;
33 E : }
34 :
35 E : bool BranchInstrumenter::InstrumentPrepare() {
36 E : return true;
37 E : }
38 :
39 E : bool BranchInstrumenter::InstrumentImpl() {
40 : branch_transform_.reset(
41 E : new instrument::transforms::BranchHookTransform());
42 E : branch_transform_->set_instrument_dll_name(agent_dll_);
43 E : branch_transform_->set_buffering(buffering_);
44 E : branch_transform_->set_fs_slot(fs_slot_);
45 E : if (!relinker_->AppendTransform(branch_transform_.get()))
46 i : return false;
47 :
48 : add_bb_addr_stream_mutator_.reset(new
49 : instrument::mutators::AddIndexedDataRangesStreamPdbMutator(
50 : branch_transform_->bb_ranges(),
51 E : common::kBasicBlockRangesStreamName));
52 E : if (!relinker_->AppendPdbMutator(add_bb_addr_stream_mutator_.get()))
53 i : return false;
54 :
55 E : return true;
56 E : }
57 :
58 : bool BranchInstrumenter::DoCommandLineParse(
59 E : const base::CommandLine* command_line) {
60 E : if (!Super::DoCommandLineParse(command_line))
61 i : return false;
62 :
63 : // Parse the additional command line arguments.
64 E : buffering_ = command_line->HasSwitch("buffering");
65 :
66 E : if (command_line->HasSwitch("fs-slot")) {
67 E : std::string fs_slot_str = command_line->GetSwitchValueASCII("fs-slot");
68 E : if (!base::StringToUint(fs_slot_str, &fs_slot_)) {
69 E : LOG(ERROR) << "Unrecognized FS-slot: not a valid number.";
70 E : return false;
71 : }
72 E : if (fs_slot_ == 0 || fs_slot_ > kNumSlots) {
73 E : LOG(ERROR) << "fs-slot must be from 1 to " << kNumSlots << ".";
74 E : return false;
75 : }
76 E : }
77 E : return true;
78 E : }
79 :
80 : } // namespace instrumenters
81 : } // namespace instrument
|