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/file_util.h"
18 : #include "base/logging.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::InstrumentImpl() {
36 : branch_transform_.reset(
37 E : new instrument::transforms::BranchHookTransform());
38 E : branch_transform_->set_instrument_dll_name(agent_dll_);
39 E : branch_transform_->set_buffering(buffering_);
40 E : branch_transform_->set_fs_slot(fs_slot_);
41 E : if (!relinker_->AppendTransform(branch_transform_.get()))
42 i : return false;
43 :
44 : add_bb_addr_stream_mutator_.reset(new
45 : instrument::mutators::AddIndexedDataRangesStreamPdbMutator(
46 : branch_transform_->bb_ranges(),
47 E : common::kBasicBlockRangesStreamName));
48 E : if (!relinker_->AppendPdbMutator(add_bb_addr_stream_mutator_.get()))
49 i : return false;
50 :
51 E : return true;
52 E : }
53 :
54 : bool BranchInstrumenter::ParseAdditionalCommandLineArguments(
55 E : const CommandLine* command_line) {
56 : // Parse the additional command line arguments.
57 E : buffering_ = command_line->HasSwitch("buffering");
58 :
59 E : if (command_line->HasSwitch("fs-slot")) {
60 E : std::string fs_slot_str = command_line->GetSwitchValueASCII("fs-slot");
61 E : if (!base::StringToUint(fs_slot_str, &fs_slot_)) {
62 E : LOG(ERROR) << "Unrecognized FS-slot: not a valid number.";
63 E : return false;
64 : }
65 E : if (fs_slot_ == 0 || fs_slot_ > kNumSlots) {
66 E : LOG(ERROR) << "fs-slot must be from 1 to " << kNumSlots << ".";
67 E : return false;
68 : }
69 E : }
70 E : return true;
71 E : }
72 :
73 : } // namespace instrumenters
74 : } // namespace instrument
|