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 | |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 24 | #include "android-base/macros.h" |
| 25 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 26 | #include "arch/instruction_set.h" |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 27 | |
| 28 | namespace art { |
| 29 | |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 30 | class DisassemblerOptions { |
| 31 | public: |
Andreas Gampe | 372f3a3 | 2016-08-19 10:49:06 -0700 | [diff] [blame] | 32 | using ThreadOffsetNameFunction = void (*)(std::ostream& os, uint32_t offset); |
| 33 | |
| 34 | ThreadOffsetNameFunction thread_offset_name_function_; |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 35 | |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 36 | // Base address for calculating relative code offsets when absolute_addresses_ is false. |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 37 | const uint8_t* const base_address_; |
| 38 | |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 39 | // End address (exclusive); |
| 40 | const uint8_t* const end_address_; |
| 41 | |
Andreas Gampe | 372f3a3 | 2016-08-19 10:49:06 -0700 | [diff] [blame] | 42 | // Should the disassembler print absolute or relative addresses. |
| 43 | const bool absolute_addresses_; |
| 44 | |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 45 | // If set, the disassembler is allowed to look at load targets in literal |
| 46 | // pools. |
| 47 | const bool can_read_literals_; |
| 48 | |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 49 | DisassemblerOptions(bool absolute_addresses, |
| 50 | const uint8_t* base_address, |
| 51 | const uint8_t* end_address, |
Andreas Gampe | 372f3a3 | 2016-08-19 10:49:06 -0700 | [diff] [blame] | 52 | bool can_read_literals, |
| 53 | ThreadOffsetNameFunction fn) |
| 54 | : thread_offset_name_function_(fn), |
Aart Bik | d3059e7 | 2016-05-11 10:30:47 -0700 | [diff] [blame] | 55 | base_address_(base_address), |
| 56 | end_address_(end_address), |
Andreas Gampe | 372f3a3 | 2016-08-19 10:49:06 -0700 | [diff] [blame] | 57 | absolute_addresses_(absolute_addresses), |
Alexandre Rames | a37d925 | 2014-10-27 11:28:14 +0000 | [diff] [blame] | 58 | can_read_literals_(can_read_literals) {} |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 59 | |
| 60 | private: |
| 61 | DISALLOW_COPY_AND_ASSIGN(DisassemblerOptions); |
| 62 | }; |
| 63 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 64 | class Disassembler { |
| 65 | public: |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 66 | // Creates a Disassembler for the given InstructionSet with the |
| 67 | // non-null DisassemblerOptions which become owned by the |
| 68 | // Disassembler. |
| 69 | static Disassembler* Create(InstructionSet instruction_set, DisassemblerOptions* options); |
| 70 | |
| 71 | virtual ~Disassembler() { |
| 72 | delete disassembler_options_; |
| 73 | } |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 74 | |
Ian Rogers | b23a772 | 2012-10-09 16:54:26 -0700 | [diff] [blame] | 75 | // Dump a single instruction returning the length of that instruction. |
| 76 | virtual size_t Dump(std::ostream& os, const uint8_t* begin) = 0; |
| 77 | // Dump instructions within a range. |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 78 | 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] | 79 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 80 | const DisassemblerOptions* GetDisassemblerOptions() const { |
| 81 | return disassembler_options_; |
| 82 | } |
| 83 | |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 84 | protected: |
Andreas Gampe | bda1d60 | 2016-08-29 17:43:45 -0700 | [diff] [blame] | 85 | explicit Disassembler(DisassemblerOptions* disassembler_options); |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 86 | |
| 87 | std::string FormatInstructionPointer(const uint8_t* begin); |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 88 | |
| 89 | private: |
Brian Carlstrom | 2cbaccb | 2014-09-14 20:34:17 -0700 | [diff] [blame] | 90 | DisassemblerOptions* disassembler_options_; |
Elliott Hughes | 105afd2 | 2012-04-10 15:04:25 -0700 | [diff] [blame] | 91 | DISALLOW_COPY_AND_ASSIGN(Disassembler); |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 92 | }; |
| 93 | |
Nicolas Geoffray | 3c7bb98 | 2014-07-23 16:04:16 +0100 | [diff] [blame] | 94 | static inline bool HasBitSet(uint32_t value, uint32_t bit) { |
| 95 | return (value & (1 << bit)) != 0; |
| 96 | } |
| 97 | |
Alexandre Rames | eb7b739 | 2015-06-19 14:47:01 +0100 | [diff] [blame] | 98 | extern "C" |
| 99 | Disassembler* create_disassembler(InstructionSet instruction_set, DisassemblerOptions* options); |
| 100 | |
Ian Rogers | 3a5c1ce | 2012-02-29 10:06:46 -0800 | [diff] [blame] | 101 | } // namespace art |
| 102 | |
Ian Rogers | 02ed4c0 | 2013-09-06 13:10:04 -0700 | [diff] [blame] | 103 | #endif // ART_DISASSEMBLER_DISASSEMBLER_H_ |