| 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 | |
| 19 | #ifndef ART_RUNTIME_CODE_ITEM_ACCESSORS_H_ |
| 20 | #define ART_RUNTIME_CODE_ITEM_ACCESSORS_H_ |
| 21 | |
| 22 | #include "base/mutex.h" |
| 23 | #include "cdex/compact_dex_file.h" |
| 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: |
| 36 | ALWAYS_INLINE CodeItemInstructionAccessor(const DexFile* dex_file, |
| 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 | |
| 45 | uint32_t InsnsSizeInCodeUnits() const { |
| 46 | return insns_size_in_code_units_; |
| 47 | } |
| 48 | |
| 49 | const uint16_t* Insns() const { |
| 50 | return insns_; |
| 51 | } |
| 52 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 53 | // Return the instruction for a dex pc. |
| 54 | const Instruction& InstructionAt(uint32_t dex_pc) const { |
| 55 | return *Instruction::At(insns_ + dex_pc); |
| 56 | } |
| 57 | |
| Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 58 | // Return true if the accessor has a code item. |
| 59 | bool HasCodeItem() const { |
| 60 | return Insns() != nullptr; |
| 61 | } |
| 62 | |
| 63 | // CreateNullable allows ArtMethods that have a null code item. |
| 64 | ALWAYS_INLINE static CodeItemInstructionAccessor CreateNullable(ArtMethod* method) |
| 65 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 66 | |
| 67 | protected: |
| 68 | CodeItemInstructionAccessor() = default; |
| 69 | |
| 70 | ALWAYS_INLINE void Init(const CompactDexFile::CodeItem& code_item); |
| 71 | ALWAYS_INLINE void Init(const StandardDexFile::CodeItem& code_item); |
| 72 | ALWAYS_INLINE void Init(const DexFile* dex_file, const DexFile::CodeItem* code_item); |
| 73 | |
| 74 | private: |
| 75 | // size of the insns array, in 2 byte code units. 0 if there is no code item. |
| 76 | uint32_t insns_size_in_code_units_ = 0; |
| 77 | |
| 78 | // Pointer to the instructions, null if there is no code item. |
| 79 | const uint16_t* insns_ = 0; |
| 80 | }; |
| 81 | |
| 82 | // Abstracts accesses to code item fields other than debug info for CompactDexFile and |
| 83 | // StandardDexFile. |
| 84 | class CodeItemDataAccessor : public CodeItemInstructionAccessor { |
| 85 | public: |
| 86 | ALWAYS_INLINE CodeItemDataAccessor(const DexFile* dex_file, const DexFile::CodeItem* code_item); |
| 87 | |
| 88 | ALWAYS_INLINE explicit CodeItemDataAccessor(ArtMethod* method); |
| 89 | |
| 90 | uint16_t RegistersSize() const { |
| 91 | return registers_size_; |
| 92 | } |
| 93 | |
| 94 | uint16_t InsSize() const { |
| 95 | return ins_size_; |
| 96 | } |
| 97 | |
| 98 | uint16_t OutsSize() const { |
| 99 | return outs_size_; |
| 100 | } |
| 101 | |
| 102 | uint16_t TriesSize() const { |
| 103 | return tries_size_; |
| 104 | } |
| 105 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 106 | IterationRange<const DexFile::TryItem*> TryItems() const; |
| 107 | |
| 108 | const uint8_t* GetCatchHandlerData(size_t offset = 0) const; |
| 109 | |
| 110 | const DexFile::TryItem* FindTryItem(uint32_t try_dex_pc) const; |
| 111 | |
| Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 112 | // CreateNullable allows ArtMethods that have a null code item. |
| 113 | ALWAYS_INLINE static CodeItemDataAccessor CreateNullable(ArtMethod* method) |
| 114 | REQUIRES_SHARED(Locks::mutator_lock_); |
| 115 | |
| Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 116 | ALWAYS_INLINE static CodeItemDataAccessor CreateNullable( |
| 117 | const DexFile* dex_file, |
| 118 | const DexFile::CodeItem* code_item); |
| Mathieu Chartier | d5c0a0a | 2017-11-10 14:16:34 -0800 | [diff] [blame] | 119 | |
| Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 120 | protected: |
| 121 | CodeItemDataAccessor() = default; |
| 122 | |
| 123 | ALWAYS_INLINE void Init(const CompactDexFile::CodeItem& code_item); |
| 124 | ALWAYS_INLINE void Init(const StandardDexFile::CodeItem& code_item); |
| 125 | ALWAYS_INLINE void Init(const DexFile* dex_file, const DexFile::CodeItem* code_item); |
| 126 | |
| 127 | private: |
| 128 | // Fields mirrored from the dex/cdex code item. |
| 129 | uint16_t registers_size_; |
| 130 | uint16_t ins_size_; |
| 131 | uint16_t outs_size_; |
| 132 | uint16_t tries_size_; |
| 133 | }; |
| 134 | |
| 135 | } // namespace art |
| 136 | |
| 137 | #endif // ART_RUNTIME_CODE_ITEM_ACCESSORS_H_ |