Added histograms for default search provider protection. Removed protection
[chromium-blink-merge.git] / tools / memory_watcher / mini_disassembler.h
blob1a75c54c5e99efa39aa145f1b6a8c60bcf95df94
1 /* Copyright (c) 2007, Google Inc.
2 * All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * ---
32 * Definition of MiniDisassembler.
35 #ifndef GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H__
36 #define GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H__
38 #include <windows.h>
39 #include "mini_disassembler_types.h"
41 // compatibility shim
42 #include "base/logging.h"
43 #define ASSERT(cond, msg) DCHECK(cond)
44 #define ASSERT1(cond) DCHECK(cond)
46 namespace sidestep {
48 // This small disassembler is very limited
49 // in its functionality, and in fact does only the bare minimum required by the
50 // preamble patching utility. It may be useful for other purposes, however.
52 // The limitations include at least the following:
53 // -# No support for coprocessor opcodes, MMX, etc.
54 // -# No machine-readable identification of opcodes or decoding of
55 // assembly parameters. The name of the opcode (as a string) is given,
56 // however, to aid debugging.
58 // You may ask what this little disassembler actually does, then? The answer is
59 // that it does the following, which is exactly what the patching utility needs:
60 // -# Indicates if opcode is a jump (any kind) or a return (any kind)
61 // because this is important for the patching utility to determine if
62 // a function is too short or there are jumps too early in it for it
63 // to be preamble patched.
64 // -# The opcode length is always calculated, so that the patching utility
65 // can figure out where the next instruction starts, and whether it
66 // already has enough instructions to replace with the absolute jump
67 // to the patching code.
69 // The usage is quite simple; just create a MiniDisassembler and use its
70 // Disassemble() method.
72 // If you would like to extend this disassembler, please refer to the
73 // IA-32 Intel Architecture Software Developer’s Manual Volume 2:
74 // Instruction Set Reference for information about operand decoding
75 // etc.
76 class MiniDisassembler {
77 public:
79 // Creates a new instance and sets defaults.
81 // @param operand_default_32_bits If true, the default operand size is
82 // set to 32 bits, which is the default under Win32. Otherwise it is 16 bits.
83 // @param address_default_32_bits If true, the default address size is
84 // set to 32 bits, which is the default under Win32. Otherwise it is 16 bits.
85 MiniDisassembler(bool operand_default_32_bits,
86 bool address_default_32_bits);
88 // Equivalent to MiniDisassembler(true, true);
89 MiniDisassembler();
91 // Attempts to disassemble a single instruction starting from the
92 // address in memory it is pointed to.
94 // @param start Address where disassembly should start.
95 // @param instruction_bytes Variable that will be <b>incremented</b> by
96 // the length in bytes of the instruction.
97 // @return enItJump, enItReturn or enItGeneric on success. enItUnknown
98 // if unable to disassemble, enItUnused if this seems to be an unused
99 // opcode. In the last two (error) cases, cbInstruction will be set
100 // to 0xffffffff.
102 // @post This instance of the disassembler is ready to be used again,
103 // with unchanged defaults from creation time.
104 InstructionType Disassemble(unsigned char* start, unsigned int& instruction_bytes);
106 private:
108 // Makes the disassembler ready for reuse.
109 void Initialize();
111 // Sets the flags for address and operand sizes.
112 // @return Number of prefix bytes.
113 InstructionType ProcessPrefixes(unsigned char* start, unsigned int& size);
115 // Sets the flag for whether we have ModR/M, and increments
116 // operand_bytes_ if any are specifies by the opcode directly.
117 // @return Number of opcode bytes.
118 InstructionType ProcessOpcode(unsigned char * start,
119 unsigned int table,
120 unsigned int& size);
122 // Checks the type of the supplied operand. Increments
123 // operand_bytes_ if it directly indicates an immediate etc.
124 // operand. Asserts have_modrm_ if the operand specifies
125 // a ModR/M byte.
126 bool ProcessOperand(int flag_operand);
128 // Increments operand_bytes_ by size specified by ModR/M and
129 // by SIB if present.
130 // @return 0 in case of error, 1 if there is just a ModR/M byte,
131 // 2 if there is a ModR/M byte and a SIB byte.
132 bool ProcessModrm(unsigned char* start, unsigned int& size);
134 // Processes the SIB byte that it is pointed to.
135 // @param start Pointer to the SIB byte.
136 // @param mod The mod field from the ModR/M byte.
137 // @return 1 to indicate success (indicates 1 SIB byte)
138 bool ProcessSib(unsigned char* start, unsigned char mod, unsigned int& size);
140 // The instruction type we have decoded from the opcode.
141 InstructionType instruction_type_;
143 // Counts the number of bytes that is occupied by operands in
144 // the current instruction (note: we don't care about how large
145 // operands stored in registers etc. are).
146 unsigned int operand_bytes_;
148 // True iff there is a ModR/M byte in this instruction.
149 bool have_modrm_;
151 // True iff we need to decode the ModR/M byte (sometimes it just
152 // points to a register, we can tell by the addressing mode).
153 bool should_decode_modrm_;
155 // Current operand size is 32 bits if true, 16 bits if false.
156 bool operand_is_32_bits_;
158 // Default operand size is 32 bits if true, 16 bits if false.
159 bool operand_default_is_32_bits_;
161 // Current address size is 32 bits if true, 16 bits if false.
162 bool address_is_32_bits_;
164 // Default address size is 32 bits if true, 16 bits if false.
165 bool address_default_is_32_bits_;
167 // Huge big opcode table based on the IA-32 manual, defined
168 // in Ia32OpcodeMap.cc
169 static const OpcodeTable s_ia32_opcode_map_[];
171 // Somewhat smaller table to help with decoding ModR/M bytes
172 // when 16-bit addressing mode is being used. Defined in
173 // Ia32ModrmMap.cc
174 static const ModrmEntry s_ia16_modrm_map_[];
176 // Somewhat smaller table to help with decoding ModR/M bytes
177 // when 32-bit addressing mode is being used. Defined in
178 // Ia32ModrmMap.cc
179 static const ModrmEntry s_ia32_modrm_map_[];
181 // Indicators of whether we got certain prefixes that certain
182 // silly Intel instructions depend on in nonstandard ways for
183 // their behaviors.
184 bool got_f2_prefix_, got_f3_prefix_, got_66_prefix_;
187 }; // namespace sidestep
189 #endif // GOOGLE_PERFTOOLS_MINI_DISASSEMBLER_H__