Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 17 | #ifndef ART_DISASSEMBLER_DISASSEMBLER_H_ |
| 18 | #define ART_DISASSEMBLER_DISASSEMBLER_H_ |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 19 | |
Elliott Hughes | 0f3c553 | 2012-03-30 14:51:51 -0700 | [diff] [blame] | 20 | #include <stdint.h> |
| 21 | |
| 22 | #include <iosfwd> |
| 23 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 24 | #include "arch/instruction_set.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 25 | #include "base/macros.h" |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 26 | |
| 27 | namespace art { |
| 28 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 29 | class DisassemblerOptions { |
| 30 | public: |
| 31 | // Should the disassembler print absolute or relative addresses. |
| 32 | const bool absolute_addresses_; |
| 33 | |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 34 | // Base address for calculating relative code offsets when absolute_addresses_ is false. |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 35 | const uint8_t* const base_address_; |
| 36 | |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 37 | // End address (exclusive); |
| 38 | const uint8_t* const end_address_; |
| 39 | |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 40 | // If set, the disassembler is allowed to look at load targets in literal |
| 41 | // pools. |
| 42 | const bool can_read_literals_; |
| 43 | |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 44 | DisassemblerOptions(bool absolute_addresses, |
| 45 | const uint8_t* base_address, |
| 46 | const uint8_t* end_address, |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 47 | bool can_read_literals) |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 48 | : absolute_addresses_(absolute_addresses), |
| 49 | base_address_(base_address), |
| 50 | end_address_(end_address), |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 51 | can_read_literals_(can_read_literals) {} |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
| 54 | DISALLOW_COPY_AND_ASSIGN(DisassemblerOptions); |
| 55 | }; |
| 56 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 57 | class Disassembler { |
| 58 | public: |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 59 | // Creates a Disassembler for the given InstructionSet with the |
| 60 | // non-null DisassemblerOptions which become owned by the |
| 61 | // Disassembler. |
| 62 | static Disassembler* Create(InstructionSet instruction_set, DisassemblerOptions* options); |
| 63 | |
| 64 | virtual ~Disassembler() { |
| 65 | delete disassembler_options_; |
| 66 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 67 | |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 68 | // Dump a single instruction returning the length of that instruction. |
| 69 | virtual size_t Dump(std::ostream& os, const uint8_t* begin) = 0; |
| 70 | // Dump instructions within a range. |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 71 | virtual void Dump(std::ostream& os, const uint8_t* begin, const uint8_t* end) = 0; |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 72 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 73 | const DisassemblerOptions* GetDisassemblerOptions() const { |
| 74 | return disassembler_options_; |
| 75 | } |
| 76 | |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 77 | protected: |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 78 | explicit Disassembler(DisassemblerOptions* disassembler_options) |
| 79 | : disassembler_options_(disassembler_options) { |
| 80 | CHECK(disassembler_options_ != nullptr); |
| 81 | } |
| 82 | |
| 83 | std::string FormatInstructionPointer(const uint8_t* begin); |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 86 | DisassemblerOptions* disassembler_options_; |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 87 | DISALLOW_COPY_AND_ASSIGN(Disassembler); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 90 | static inline bool HasBitSet(uint32_t value, uint32_t bit) { |
| 91 | return (value & (1 << bit)) != 0; |
| 92 | } |
| 93 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 94 | extern "C" |
| 95 | Disassembler* create_disassembler(InstructionSet instruction_set, DisassemblerOptions* options); |
| 96 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 97 | } // namespace art |
| 98 | |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 99 | #endif // ART_DISASSEMBLER_DISASSEMBLER_H_ |