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