| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2016 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 | #ifndef _LIBUNWINDSTACK_DWARF_OP_H | 
 | 18 | #define _LIBUNWINDSTACK_DWARF_OP_H | 
 | 19 |  | 
 | 20 | #include <stdint.h> | 
 | 21 |  | 
 | 22 | #include <deque> | 
 | 23 | #include <string> | 
 | 24 | #include <type_traits> | 
 | 25 | #include <vector> | 
 | 26 |  | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 27 | #include <unwindstack/DwarfError.h> | 
 | 28 |  | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 29 | #include "DwarfEncoding.h" | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 30 | #include "RegsInfo.h" | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 31 |  | 
 | 32 | namespace unwindstack { | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 33 |  | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 34 | // Forward declarations. | 
 | 35 | class DwarfMemory; | 
 | 36 | class Memory; | 
 | 37 | template <typename AddressType> | 
| Christopher Ferris | 7b8e467 | 2017-06-01 17:55:25 -0700 | [diff] [blame] | 38 | class RegsImpl; | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 39 |  | 
 | 40 | template <typename AddressType> | 
 | 41 | class DwarfOp { | 
 | 42 |   // Signed version of AddressType | 
 | 43 |   typedef typename std::make_signed<AddressType>::type SignedType; | 
 | 44 |  | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 45 |  public: | 
 | 46 |   DwarfOp(DwarfMemory* memory, Memory* regular_memory) | 
 | 47 |       : memory_(memory), regular_memory_(regular_memory) {} | 
 | 48 |   virtual ~DwarfOp() = default; | 
 | 49 |  | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 50 |   bool Decode(); | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 51 |  | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 52 |   bool Eval(uint64_t start, uint64_t end); | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 53 |  | 
 | 54 |   void GetLogInfo(uint64_t start, uint64_t end, std::vector<std::string>* lines); | 
 | 55 |  | 
 | 56 |   AddressType StackAt(size_t index) { return stack_[index]; } | 
 | 57 |   size_t StackSize() { return stack_.size(); } | 
 | 58 |  | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 59 |   void set_regs_info(RegsInfo<AddressType>* regs_info) { regs_info_ = regs_info; } | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 60 |  | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 61 |   const DwarfErrorData& last_error() { return last_error_; } | 
 | 62 |   DwarfErrorCode LastErrorCode() { return last_error_.code; } | 
 | 63 |   uint64_t LastErrorAddress() { return last_error_.address; } | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 64 |  | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 65 |   bool dex_pc_set() { return dex_pc_set_; } | 
 | 66 |  | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 67 |   bool is_register() { return is_register_; } | 
 | 68 |  | 
 | 69 |   uint8_t cur_op() { return cur_op_; } | 
 | 70 |  | 
 | 71 |   Memory* regular_memory() { return regular_memory_; } | 
 | 72 |  | 
 | 73 |  protected: | 
 | 74 |   AddressType OperandAt(size_t index) { return operands_[index]; } | 
 | 75 |   size_t OperandsSize() { return operands_.size(); } | 
 | 76 |  | 
 | 77 |   AddressType StackPop() { | 
 | 78 |     AddressType value = stack_.front(); | 
 | 79 |     stack_.pop_front(); | 
 | 80 |     return value; | 
 | 81 |   } | 
 | 82 |  | 
 | 83 |  private: | 
 | 84 |   DwarfMemory* memory_; | 
 | 85 |   Memory* regular_memory_; | 
 | 86 |  | 
| Christopher Ferris | 559c7f2 | 2018-02-12 20:18:03 -0800 | [diff] [blame] | 87 |   RegsInfo<AddressType>* regs_info_; | 
 | 88 |   bool dex_pc_set_ = false; | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 89 |   bool is_register_ = false; | 
| Christopher Ferris | 2fcf4cf | 2018-01-23 17:52:23 -0800 | [diff] [blame] | 90 |   DwarfErrorData last_error_{DWARF_ERROR_NONE, 0}; | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 91 |   uint8_t cur_op_; | 
 | 92 |   std::vector<AddressType> operands_; | 
 | 93 |   std::deque<AddressType> stack_; | 
 | 94 |  | 
 | 95 |   inline AddressType bool_to_dwarf_bool(bool value) { return value ? 1 : 0; } | 
 | 96 |  | 
 | 97 |   // Op processing functions. | 
 | 98 |   bool op_deref(); | 
 | 99 |   bool op_deref_size(); | 
 | 100 |   bool op_push(); | 
 | 101 |   bool op_dup(); | 
 | 102 |   bool op_drop(); | 
 | 103 |   bool op_over(); | 
 | 104 |   bool op_pick(); | 
 | 105 |   bool op_swap(); | 
 | 106 |   bool op_rot(); | 
 | 107 |   bool op_abs(); | 
 | 108 |   bool op_and(); | 
 | 109 |   bool op_div(); | 
 | 110 |   bool op_minus(); | 
 | 111 |   bool op_mod(); | 
 | 112 |   bool op_mul(); | 
 | 113 |   bool op_neg(); | 
 | 114 |   bool op_not(); | 
 | 115 |   bool op_or(); | 
 | 116 |   bool op_plus(); | 
 | 117 |   bool op_plus_uconst(); | 
 | 118 |   bool op_shl(); | 
 | 119 |   bool op_shr(); | 
 | 120 |   bool op_shra(); | 
 | 121 |   bool op_xor(); | 
 | 122 |   bool op_bra(); | 
 | 123 |   bool op_eq(); | 
 | 124 |   bool op_ge(); | 
 | 125 |   bool op_gt(); | 
 | 126 |   bool op_le(); | 
 | 127 |   bool op_lt(); | 
 | 128 |   bool op_ne(); | 
 | 129 |   bool op_skip(); | 
 | 130 |   bool op_lit(); | 
 | 131 |   bool op_reg(); | 
 | 132 |   bool op_regx(); | 
 | 133 |   bool op_breg(); | 
 | 134 |   bool op_bregx(); | 
 | 135 |   bool op_nop(); | 
 | 136 |   bool op_not_implemented(); | 
 | 137 |  | 
| Vic Yang | cc8009f | 2019-01-24 11:00:26 -0800 | [diff] [blame] | 138 |   using OpHandleFuncPtr = bool (DwarfOp::*)(); | 
 | 139 |   static const OpHandleFuncPtr kOpHandleFuncList[]; | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 140 | }; | 
 | 141 |  | 
| Christopher Ferris | d226a51 | 2017-07-14 10:37:19 -0700 | [diff] [blame] | 142 | }  // namespace unwindstack | 
 | 143 |  | 
| Christopher Ferris | 55d22ef | 2017-04-04 10:41:31 -0700 | [diff] [blame] | 144 | #endif  // _LIBUNWINDSTACK_DWARF_OP_H |