Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 | |
| 17 | // TODO: Dex helpers have ART specific APIs, we may want to refactor these for use in dexdump. |
| 18 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 19 | #ifndef ART_RUNTIME_DEX_CODE_ITEM_ACCESSORS_H_ |
| 20 | #define ART_RUNTIME_DEX_CODE_ITEM_ACCESSORS_H_ |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 21 | |
| 22 | #include "base/mutex.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 23 | #include "compact_dex_file.h" |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 24 | #include "dex_file.h" |
| 25 | #include "dex_instruction_iterator.h" |
| 26 | #include "standard_dex_file.h" |
| 27 | |
| 28 | namespace art { |
| 29 | |
| 30 | class ArtMethod; |
| 31 | |
| 32 | // Abstracts accesses to the instruction fields of code items for CompactDexFile and |
| 33 | // StandardDexFile. |
| 34 | class CodeItemInstructionAccessor { |
| 35 | public: |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 36 | ALWAYS_INLINE CodeItemInstructionAccessor(const DexFile& dex_file, |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 37 | const DexFile::CodeItem* code_item); |
| 38 | |
| 39 | ALWAYS_INLINE explicit CodeItemInstructionAccessor(ArtMethod* method); |
| 40 | |
| 41 | ALWAYS_INLINE DexInstructionIterator begin() const; |
| 42 | |
| 43 | ALWAYS_INLINE DexInstructionIterator end() const; |
| 44 | |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 45 | IterationRange<DexInstructionIterator> InstructionsFrom(uint32_t start_dex_pc) const; |
| 46 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 47 | uint32_t InsnsSizeInCodeUnits() const { |
| 48 | return insns_size_in_code_units_; |
| 49 | } |
| 50 | |
| 51 | const uint16_t* Insns() const { |
| 52 | return insns_; |
| 53 | } |
| 54 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 55 | // Return the instruction for a dex pc. |
| 56 | const Instruction& InstructionAt(uint32_t dex_pc) const { |
Mathieu Chartier | 808c7a5 | 2017-12-15 11:19:33 -0800 | [diff] [blame] | 57 | DCHECK_LT(dex_pc, InsnsSizeInCodeUnits()); |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 58 | return *Instruction::At(insns_ + dex_pc); |
| 59 | } |
| 60 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 61 | // Return true if the accessor has a code item. |
| 62 | bool HasCodeItem() const { |
| 63 | return Insns() != nullptr; |
| 64 | } |
| 65 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 66 | protected: |
| 67 | CodeItemInstructionAccessor() = default; |
| 68 | |
| 69 | ALWAYS_INLINE void Init(const CompactDexFile::CodeItem& code_item); |
| 70 | ALWAYS_INLINE void Init(const StandardDexFile::CodeItem& code_item); |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 71 | ALWAYS_INLINE void Init(const DexFile& dex_file, const DexFile::CodeItem* code_item); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | // size of the insns array, in 2 byte code units. 0 if there is no code item. |
| 75 | uint32_t insns_size_in_code_units_ = 0; |
| 76 | |
| 77 | // Pointer to the instructions, null if there is no code item. |
| 78 | const uint16_t* insns_ = 0; |
| 79 | }; |
| 80 | |
| 81 | // Abstracts accesses to code item fields other than debug info for CompactDexFile and |
| 82 | // StandardDexFile. |
| 83 | class CodeItemDataAccessor : public CodeItemInstructionAccessor { |
| 84 | public: |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 85 | ALWAYS_INLINE CodeItemDataAccessor(const DexFile& dex_file, const DexFile::CodeItem* code_item); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 86 | |
| 87 | ALWAYS_INLINE explicit CodeItemDataAccessor(ArtMethod* method); |
| 88 | |
| 89 | uint16_t RegistersSize() const { |
| 90 | return registers_size_; |
| 91 | } |
| 92 | |
| 93 | uint16_t InsSize() const { |
| 94 | return ins_size_; |
| 95 | } |
| 96 | |
| 97 | uint16_t OutsSize() const { |
| 98 | return outs_size_; |
| 99 | } |
| 100 | |
| 101 | uint16_t TriesSize() const { |
| 102 | return tries_size_; |
| 103 | } |
| 104 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 105 | IterationRange<const DexFile::TryItem*> TryItems() const; |
| 106 | |
| 107 | const uint8_t* GetCatchHandlerData(size_t offset = 0) const; |
| 108 | |
| 109 | const DexFile::TryItem* FindTryItem(uint32_t try_dex_pc) const; |
| 110 | |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 111 | inline const void* CodeItemDataEnd() const; |
| 112 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 113 | protected: |
| 114 | CodeItemDataAccessor() = default; |
| 115 | |
| 116 | ALWAYS_INLINE void Init(const CompactDexFile::CodeItem& code_item); |
| 117 | ALWAYS_INLINE void Init(const StandardDexFile::CodeItem& code_item); |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 118 | ALWAYS_INLINE void Init(const DexFile& dex_file, const DexFile::CodeItem* code_item); |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 119 | |
| 120 | private: |
| 121 | // Fields mirrored from the dex/cdex code item. |
| 122 | uint16_t registers_size_; |
| 123 | uint16_t ins_size_; |
| 124 | uint16_t outs_size_; |
| 125 | uint16_t tries_size_; |
| 126 | }; |
| 127 | |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 128 | // Abstract accesses to code item data including debug info offset. More heavy weight than the other |
| 129 | // helpers. |
| 130 | class CodeItemDebugInfoAccessor : public CodeItemDataAccessor { |
| 131 | public: |
| 132 | CodeItemDebugInfoAccessor() = default; |
| 133 | |
| 134 | // Handles null code items, but not null dex files. |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 135 | ALWAYS_INLINE CodeItemDebugInfoAccessor(const DexFile& dex_file, |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 136 | const DexFile::CodeItem* code_item); |
| 137 | |
Mathieu Chartier | 641a3af | 2017-12-15 11:42:58 -0800 | [diff] [blame] | 138 | // Initialize with an existing offset. |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 139 | ALWAYS_INLINE CodeItemDebugInfoAccessor(const DexFile& dex_file, |
Mathieu Chartier | 641a3af | 2017-12-15 11:42:58 -0800 | [diff] [blame] | 140 | const DexFile::CodeItem* code_item, |
| 141 | uint32_t debug_info_offset) { |
| 142 | Init(dex_file, code_item, debug_info_offset); |
| 143 | } |
| 144 | |
Mathieu Chartier | 698ebbc | 2018-01-05 11:00:42 -0800 | [diff] [blame] | 145 | ALWAYS_INLINE void Init(const DexFile& dex_file, |
Mathieu Chartier | 641a3af | 2017-12-15 11:42:58 -0800 | [diff] [blame] | 146 | const DexFile::CodeItem* code_item, |
| 147 | uint32_t debug_info_offset); |
| 148 | |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 149 | ALWAYS_INLINE explicit CodeItemDebugInfoAccessor(ArtMethod* method); |
| 150 | |
| 151 | uint32_t DebugInfoOffset() const { |
| 152 | return debug_info_offset_; |
| 153 | } |
| 154 | |
Mathieu Chartier | 641a3af | 2017-12-15 11:42:58 -0800 | [diff] [blame] | 155 | template<typename NewLocalCallback> |
| 156 | bool DecodeDebugLocalInfo(bool is_static, |
| 157 | uint32_t method_idx, |
| 158 | NewLocalCallback new_local, |
| 159 | void* context) const; |
| 160 | |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 161 | protected: |
| 162 | ALWAYS_INLINE void Init(const CompactDexFile::CodeItem& code_item); |
| 163 | ALWAYS_INLINE void Init(const StandardDexFile::CodeItem& code_item); |
| 164 | |
| 165 | private: |
Mathieu Chartier | 641a3af | 2017-12-15 11:42:58 -0800 | [diff] [blame] | 166 | const DexFile* dex_file_ = nullptr; |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 167 | uint32_t debug_info_offset_ = 0u; |
| 168 | }; |
| 169 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 170 | } // namespace art |
| 171 | |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 172 | #endif // ART_RUNTIME_DEX_CODE_ITEM_ACCESSORS_H_ |