blob: ac9fd2d16d0834006d3ff22e6ef0e8135d9e8041 [file] [log] [blame]
Christopher Ferris55d22ef2017-04-04 10:41:31 -07001/*
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 Ferris2fcf4cf2018-01-23 17:52:23 -080027#include <unwindstack/DwarfError.h>
28
Christopher Ferris55d22ef2017-04-04 10:41:31 -070029#include "DwarfEncoding.h"
Christopher Ferris559c7f22018-02-12 20:18:03 -080030#include "RegsInfo.h"
Christopher Ferrisd226a512017-07-14 10:37:19 -070031
32namespace unwindstack {
Christopher Ferris55d22ef2017-04-04 10:41:31 -070033
Christopher Ferris55d22ef2017-04-04 10:41:31 -070034// Forward declarations.
35class DwarfMemory;
36class Memory;
37template <typename AddressType>
Christopher Ferris7b8e4672017-06-01 17:55:25 -070038class RegsImpl;
Christopher Ferris55d22ef2017-04-04 10:41:31 -070039
40template <typename AddressType>
41class DwarfOp {
42 // Signed version of AddressType
43 typedef typename std::make_signed<AddressType>::type SignedType;
44
Christopher Ferris55d22ef2017-04-04 10:41:31 -070045 public:
46 DwarfOp(DwarfMemory* memory, Memory* regular_memory)
47 : memory_(memory), regular_memory_(regular_memory) {}
48 virtual ~DwarfOp() = default;
49
Christopher Ferris559c7f22018-02-12 20:18:03 -080050 bool Decode();
Christopher Ferris55d22ef2017-04-04 10:41:31 -070051
Christopher Ferris559c7f22018-02-12 20:18:03 -080052 bool Eval(uint64_t start, uint64_t end);
Christopher Ferris55d22ef2017-04-04 10:41:31 -070053
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 Ferris559c7f22018-02-12 20:18:03 -080059 void set_regs_info(RegsInfo<AddressType>* regs_info) { regs_info_ = regs_info; }
Christopher Ferris55d22ef2017-04-04 10:41:31 -070060
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080061 const DwarfErrorData& last_error() { return last_error_; }
62 DwarfErrorCode LastErrorCode() { return last_error_.code; }
63 uint64_t LastErrorAddress() { return last_error_.address; }
Christopher Ferris55d22ef2017-04-04 10:41:31 -070064
Christopher Ferris559c7f22018-02-12 20:18:03 -080065 bool dex_pc_set() { return dex_pc_set_; }
66
Christopher Ferris55d22ef2017-04-04 10:41:31 -070067 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 Ferris559c7f22018-02-12 20:18:03 -080087 RegsInfo<AddressType>* regs_info_;
88 bool dex_pc_set_ = false;
Christopher Ferris55d22ef2017-04-04 10:41:31 -070089 bool is_register_ = false;
Christopher Ferris2fcf4cf2018-01-23 17:52:23 -080090 DwarfErrorData last_error_{DWARF_ERROR_NONE, 0};
Christopher Ferris55d22ef2017-04-04 10:41:31 -070091 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 Yangcc8009f2019-01-24 11:00:26 -0800138 using OpHandleFuncPtr = bool (DwarfOp::*)();
139 static const OpHandleFuncPtr kOpHandleFuncList[];
Christopher Ferris55d22ef2017-04-04 10:41:31 -0700140};
141
Christopher Ferrisd226a512017-07-14 10:37:19 -0700142} // namespace unwindstack
143
Christopher Ferris55d22ef2017-04-04 10:41:31 -0700144#endif // _LIBUNWINDSTACK_DWARF_OP_H