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 : // Specialization of the instrumenter interface for the instrumenters who use an
16 : // agent. This performs all the common bits of this kind of instrumenters:
17 : // - Parse the shared command-line parameters.
18 : // - Initialization the relinker.
19 : // - Default implementation of Instrument.
20 : #ifndef SYZYGY_INSTRUMENT_INSTRUMENTERS_INSTRUMENTER_WITH_AGENT_H_
21 : #define SYZYGY_INSTRUMENT_INSTRUMENTERS_INSTRUMENTER_WITH_AGENT_H_
22 :
23 : #include <string>
24 :
25 : #include "base/command_line.h"
26 : #include "syzygy/instrument/instrumenter.h"
27 : #include "syzygy/pe/coff_relinker.h"
28 : #include "syzygy/pe/pe_relinker.h"
29 :
30 : namespace instrument {
31 : namespace instrumenters {
32 :
33 : class InstrumenterWithAgent : public InstrumenterInterface {
34 : public:
35 : InstrumenterWithAgent()
36 : : image_format_(pe::PE_IMAGE),
37 : allow_overwrite_(false),
38 : debug_friendly_(false),
39 : no_augment_pdb_(false),
40 : no_strip_strings_(false),
41 E : old_decomposer_(false) {
42 E : }
43 :
44 E : ~InstrumenterWithAgent() { }
45 :
46 : // @name InstrumenterInterface implementation.
47 : // @{
48 : virtual bool ParseCommandLine(const CommandLine* command_line) OVERRIDE;
49 : virtual bool Instrument() OVERRIDE;
50 : // @}
51 :
52 : // @name Accessors.
53 : // @
54 E : const std::string& agent_dll() {
55 E : return agent_dll_;
56 E : }
57 : // @}
58 :
59 : protected:
60 : // Virtual method that determines whether or not the input object file
61 : // format is supported by the instrumenter. The default implementation
62 : // supports PE files, and does not support COFF files.
63 : virtual bool ImageFormatIsSupported(pe::ImageFormat image_format);
64 :
65 : // Virtual method that does the actual instrumentation for a given agent.
66 : // This function is meant to be called by the Instrument function.
67 : // @note The implementation should log on failure.
68 : virtual bool InstrumentImpl() = 0;
69 :
70 : // Pure virtual method that should return the name of the instrumentation
71 : // mode.
72 : virtual const char* InstrumentationMode() = 0;
73 :
74 : // Virtual method called by ParseCommandLine to parse the additional
75 : // command-line arguments specific to an instrumenter.
76 : // @param command_line the command-line to be parsed.
77 : // @returns true by default. Should return false on error.
78 : virtual bool ParseAdditionalCommandLineArguments(
79 E : const CommandLine* command_line) {
80 E : return true;
81 E : }
82 :
83 : // @name Internal machinery, replaceable for testing purposes. These will
84 : // only ever be called once per object lifetime.
85 : // @{
86 : virtual pe::PETransformPolicy* GetPETransformPolicy();
87 : virtual pe::CoffTransformPolicy* GetCoffTransformPolicy();
88 : virtual pe::PERelinker* GetPERelinker();
89 : virtual pe::CoffRelinker* GetCoffRelinker();
90 : // @}
91 :
92 : // Creates and configures a relinker. This is split out for unittesting
93 : // purposes, allowing child classes to test their InstrumentImpl functions
94 : // in isolation.
95 : bool CreateRelinker();
96 :
97 : // The agent DLL used by this instrumentation.
98 : std::string agent_dll_;
99 :
100 : // The type of image file we are transforming.
101 : pe::ImageFormat image_format_;
102 :
103 : // @name Command-line parameters.
104 : // @{
105 : base::FilePath input_image_path_;
106 : base::FilePath input_pdb_path_;
107 : base::FilePath output_image_path_;
108 : base::FilePath output_pdb_path_;
109 : bool allow_overwrite_;
110 : bool debug_friendly_;
111 : bool no_augment_pdb_;
112 : bool no_strip_strings_;
113 : bool old_decomposer_;
114 : // @}
115 :
116 : // This is used to save a pointer to the object returned by the call to
117 : // Get(PE|Coff)Relinker. Ownership of the object is internal in the default
118 : // case, but may be external during tests.
119 : pe::RelinkerInterface* relinker_;
120 :
121 : private:
122 : // They are used as containers for holding policy and relinker objects that
123 : // are allocated by our default Get* implementations above.
124 : scoped_ptr<block_graph::TransformPolicyInterface> policy_object_;
125 : scoped_ptr<pe::RelinkerInterface> relinker_object_;
126 :
127 : DISALLOW_COPY_AND_ASSIGN(InstrumenterWithAgent);
128 : };
129 :
130 : } // namespace instrumenters
131 : } // namespace instrument
132 :
133 : #endif // SYZYGY_INSTRUMENT_INSTRUMENTERS_INSTRUMENTER_WITH_AGENT_H_
|