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/entry_thunk_instrumenter.h"
16 :
17 : #include "base/file_util.h"
18 : #include "base/logging.h"
19 : #include "syzygy/common/application.h"
20 : #include "syzygy/pe/image_filter.h"
21 :
22 : namespace instrument {
23 : namespace instrumenters {
24 :
25 : const char EntryThunkInstrumenter::kAgentDllProfile[] = "profile_client.dll";
26 : const char EntryThunkInstrumenter::kAgentDllRpc[] = "call_trace_client.dll";
27 :
28 : EntryThunkInstrumenter::EntryThunkInstrumenter(Mode instrumentation_mode)
29 : : instrumentation_mode_(instrumentation_mode),
30 E : thunk_imports_(false) {
31 E : DCHECK(instrumentation_mode != INVALID_MODE);
32 E : switch (instrumentation_mode) {
33 : case CALL_TRACE:
34 E : agent_dll_ = kAgentDllRpc;
35 E : instrument_unsafe_references_ = true;
36 E : module_entry_only_ = true;
37 E : break;
38 : case PROFILE:
39 E : agent_dll_ = kAgentDllProfile;
40 E : instrument_unsafe_references_ = false;
41 E : module_entry_only_ = false;
42 E : break;
43 : default:
44 i : NOTREACHED();
45 : break;
46 : }
47 E : }
48 :
49 E : bool EntryThunkInstrumenter::InstrumentImpl() {
50 : entry_thunk_transform_.reset(
51 E : new instrument::transforms::EntryThunkTransform());
52 E : entry_thunk_transform_->set_instrument_dll_name(agent_dll_);
53 : entry_thunk_transform_->set_instrument_unsafe_references(
54 E : instrument_unsafe_references_);
55 E : entry_thunk_transform_->set_src_ranges_for_thunks(debug_friendly_);
56 E : entry_thunk_transform_->set_only_instrument_module_entry(module_entry_only_);
57 E : relinker_->AppendTransform(entry_thunk_transform_.get());
58 :
59 : // If we are thunking imports then add the appropriate transform.
60 E : if (thunk_imports_) {
61 : import_thunk_tx_.reset(
62 i : new instrument::transforms::ThunkImportReferencesTransform);
63 : // Use the selected client DLL.
64 i : import_thunk_tx_->set_instrument_dll_name(agent_dll_);
65 i : relinker_->AppendTransform(import_thunk_tx_.get());
66 : }
67 :
68 E : return true;
69 E : }
70 :
71 : bool EntryThunkInstrumenter::ParseAdditionalCommandLineArguments(
72 E : const CommandLine* command_line) {
73 E : if (instrumentation_mode_ != PROFILE) {
74 E : module_entry_only_ = command_line->HasSwitch("module-entry-only");
75 E : instrument_unsafe_references_ = !command_line->HasSwitch("no-unsafe-refs");
76 : }
77 E : thunk_imports_ = command_line->HasSwitch("instrument-imports");
78 :
79 E : return true;
80 E : }
81 :
82 i : const char* EntryThunkInstrumenter::InstrumentationMode() {
83 i : switch (instrumentation_mode_) {
84 : case CALL_TRACE:
85 i : return "call trace";
86 : case PROFILE:
87 i : return "profile";
88 : default:
89 i : NOTREACHED();
90 i : return NULL;
91 : }
92 i : }
93 :
94 : } // namespace instrumenters
95 : } // namespace instrument
|