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/logging.h"
18 : #include "base/files/file_util.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::InstrumentPrepare() {
52 E : return true;
53 E : }
54 :
55 E : bool EntryThunkInstrumenter::InstrumentImpl() {
56 : entry_thunk_transform_.reset(
57 E : new instrument::transforms::EntryThunkTransform());
58 E : entry_thunk_transform_->set_instrument_dll_name(agent_dll_);
59 : entry_thunk_transform_->set_instrument_unsafe_references(
60 E : instrument_unsafe_references_);
61 E : entry_thunk_transform_->set_src_ranges_for_thunks(debug_friendly_);
62 E : entry_thunk_transform_->set_only_instrument_module_entry(module_entry_only_);
63 E : if (!relinker_->AppendTransform(entry_thunk_transform_.get()))
64 i : return false;
65 :
66 : // If we are thunking imports then add the appropriate transform.
67 E : if (thunk_imports_) {
68 : import_thunk_tx_.reset(
69 i : new instrument::transforms::ThunkImportReferencesTransform);
70 : // Use the selected client DLL.
71 i : import_thunk_tx_->set_instrument_dll_name(agent_dll_);
72 i : if (!relinker_->AppendTransform(import_thunk_tx_.get()))
73 i : return false;
74 : }
75 :
76 E : return true;
77 E : }
78 :
79 : bool EntryThunkInstrumenter::DoCommandLineParse(
80 E : const base::CommandLine* command_line) {
81 E : if (!Super::DoCommandLineParse(command_line))
82 E : return false;
83 :
84 E : if (instrumentation_mode_ == CALL_TRACE) {
85 E : module_entry_only_ = command_line->HasSwitch("module-entry-only");
86 E : instrument_unsafe_references_ = !command_line->HasSwitch("no-unsafe-refs");
87 : }
88 E : thunk_imports_ = command_line->HasSwitch("instrument-imports");
89 :
90 E : return true;
91 E : }
92 :
93 i : const char* EntryThunkInstrumenter::InstrumentationMode() {
94 i : switch (instrumentation_mode_) {
95 : case CALL_TRACE:
96 i : return "call trace";
97 : case PROFILE:
98 i : return "profile";
99 : default:
100 i : NOTREACHED();
101 i : return NULL;
102 : }
103 i : }
104 :
105 : } // namespace instrumenters
106 : } // namespace instrument
|