David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 17 | #ifndef ART_COMPILER_DEBUG_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_ |
| 18 | #define ART_COMPILER_DEBUG_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_ |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 19 | |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 20 | #include "base/bit_utils.h" |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 21 | #include "debug/dwarf/dwarf_constants.h" |
| 22 | #include "debug/dwarf/register.h" |
| 23 | #include "debug/dwarf/writer.h" |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 24 | |
| 25 | namespace art { |
| 26 | namespace dwarf { |
| 27 | |
| 28 | // Writer for .debug_frame opcodes (DWARF-3). |
| 29 | // See the DWARF specification for the precise meaning of the opcodes. |
| 30 | // The writer is very light-weight, however it will do the following for you: |
| 31 | // * Choose the most compact encoding of a given opcode. |
| 32 | // * Keep track of current state and convert absolute values to deltas. |
| 33 | // * Divide by header-defined factors as appropriate. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 34 | template<typename Vector = std::vector<uint8_t> > |
| 35 | class DebugFrameOpCodeWriter : private Writer<Vector> { |
| 36 | static_assert(std::is_same<typename Vector::value_type, uint8_t>::value, "Invalid value type"); |
| 37 | |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 38 | public: |
| 39 | // To save space, DWARF divides most offsets by header-defined factors. |
| 40 | // They are used in integer divisions, so we make them constants. |
| 41 | // We usually subtract from stack base pointer, so making the factor |
| 42 | // negative makes the encoded values positive and thus easier to encode. |
| 43 | static constexpr int kDataAlignmentFactor = -4; |
| 44 | static constexpr int kCodeAlignmentFactor = 1; |
| 45 | |
| 46 | // Explicitely advance the program counter to given location. |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 47 | void ALWAYS_INLINE AdvancePC(int absolute_pc) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 48 | DCHECK_GE(absolute_pc, current_pc_); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 49 | if (UNLIKELY(enabled_)) { |
| 50 | int delta = FactorCodeOffset(absolute_pc - current_pc_); |
| 51 | if (delta != 0) { |
| 52 | if (delta <= 0x3F) { |
| 53 | this->PushUint8(DW_CFA_advance_loc | delta); |
| 54 | } else if (delta <= UINT8_MAX) { |
| 55 | this->PushUint8(DW_CFA_advance_loc1); |
| 56 | this->PushUint8(delta); |
| 57 | } else if (delta <= UINT16_MAX) { |
| 58 | this->PushUint8(DW_CFA_advance_loc2); |
| 59 | this->PushUint16(delta); |
| 60 | } else { |
| 61 | this->PushUint8(DW_CFA_advance_loc4); |
| 62 | this->PushUint32(delta); |
| 63 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 64 | } |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 65 | current_pc_ = absolute_pc; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 66 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | // Override this method to automatically advance the PC before each opcode. |
| 70 | virtual void ImplicitlyAdvancePC() { } |
| 71 | |
| 72 | // Common alias in assemblers - spill relative to current stack pointer. |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 73 | void ALWAYS_INLINE RelOffset(Reg reg, int offset) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 74 | Offset(reg, offset - current_cfa_offset_); |
| 75 | } |
| 76 | |
| 77 | // Common alias in assemblers - increase stack frame size. |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 78 | void ALWAYS_INLINE AdjustCFAOffset(int delta) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 79 | DefCFAOffset(current_cfa_offset_ + delta); |
| 80 | } |
| 81 | |
| 82 | // Custom alias - spill many registers based on bitmask. |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 83 | void ALWAYS_INLINE RelOffsetForMany(Reg reg_base, int offset, |
| 84 | uint32_t reg_mask, int reg_size) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 85 | DCHECK(reg_size == 4 || reg_size == 8); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 86 | if (UNLIKELY(enabled_)) { |
| 87 | for (int i = 0; reg_mask != 0u; reg_mask >>= 1, i++) { |
| 88 | // Skip zero bits and go to the set bit. |
| 89 | int num_zeros = CTZ(reg_mask); |
| 90 | i += num_zeros; |
| 91 | reg_mask >>= num_zeros; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 92 | RelOffset(Reg(reg_base.num() + i), offset); |
| 93 | offset += reg_size; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Custom alias - unspill many registers based on bitmask. |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 99 | void ALWAYS_INLINE RestoreMany(Reg reg_base, uint32_t reg_mask) { |
| 100 | if (UNLIKELY(enabled_)) { |
| 101 | for (int i = 0; reg_mask != 0u; reg_mask >>= 1, i++) { |
| 102 | // Skip zero bits and go to the set bit. |
| 103 | int num_zeros = CTZ(reg_mask); |
| 104 | i += num_zeros; |
| 105 | reg_mask >>= num_zeros; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 106 | Restore(Reg(reg_base.num() + i)); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 111 | void ALWAYS_INLINE Nop() { |
| 112 | if (UNLIKELY(enabled_)) { |
| 113 | this->PushUint8(DW_CFA_nop); |
| 114 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 115 | } |
| 116 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 117 | void ALWAYS_INLINE Offset(Reg reg, int offset) { |
| 118 | if (UNLIKELY(enabled_)) { |
| 119 | ImplicitlyAdvancePC(); |
| 120 | int factored_offset = FactorDataOffset(offset); // May change sign. |
| 121 | if (factored_offset >= 0) { |
| 122 | if (0 <= reg.num() && reg.num() <= 0x3F) { |
| 123 | this->PushUint8(DW_CFA_offset | reg.num()); |
| 124 | this->PushUleb128(factored_offset); |
| 125 | } else { |
| 126 | this->PushUint8(DW_CFA_offset_extended); |
| 127 | this->PushUleb128(reg.num()); |
| 128 | this->PushUleb128(factored_offset); |
| 129 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 130 | } else { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 131 | uses_dwarf3_features_ = true; |
| 132 | this->PushUint8(DW_CFA_offset_extended_sf); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 133 | this->PushUleb128(reg.num()); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 134 | this->PushSleb128(factored_offset); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 135 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 136 | } |
| 137 | } |
| 138 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 139 | void ALWAYS_INLINE Restore(Reg reg) { |
| 140 | if (UNLIKELY(enabled_)) { |
| 141 | ImplicitlyAdvancePC(); |
| 142 | if (0 <= reg.num() && reg.num() <= 0x3F) { |
| 143 | this->PushUint8(DW_CFA_restore | reg.num()); |
| 144 | } else { |
| 145 | this->PushUint8(DW_CFA_restore_extended); |
| 146 | this->PushUleb128(reg.num()); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void ALWAYS_INLINE Undefined(Reg reg) { |
| 152 | if (UNLIKELY(enabled_)) { |
| 153 | ImplicitlyAdvancePC(); |
| 154 | this->PushUint8(DW_CFA_undefined); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 155 | this->PushUleb128(reg.num()); |
| 156 | } |
| 157 | } |
| 158 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 159 | void ALWAYS_INLINE SameValue(Reg reg) { |
| 160 | if (UNLIKELY(enabled_)) { |
| 161 | ImplicitlyAdvancePC(); |
| 162 | this->PushUint8(DW_CFA_same_value); |
| 163 | this->PushUleb128(reg.num()); |
| 164 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // The previous value of "reg" is stored in register "new_reg". |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 168 | void ALWAYS_INLINE Register(Reg reg, Reg new_reg) { |
| 169 | if (UNLIKELY(enabled_)) { |
| 170 | ImplicitlyAdvancePC(); |
| 171 | this->PushUint8(DW_CFA_register); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 172 | this->PushUleb128(reg.num()); |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 173 | this->PushUleb128(new_reg.num()); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 174 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 175 | } |
| 176 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 177 | void ALWAYS_INLINE RememberState() { |
| 178 | if (UNLIKELY(enabled_)) { |
| 179 | ImplicitlyAdvancePC(); |
| 180 | this->PushUint8(DW_CFA_remember_state); |
| 181 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 182 | } |
| 183 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 184 | void ALWAYS_INLINE RestoreState() { |
| 185 | if (UNLIKELY(enabled_)) { |
| 186 | ImplicitlyAdvancePC(); |
| 187 | this->PushUint8(DW_CFA_restore_state); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | void ALWAYS_INLINE DefCFA(Reg reg, int offset) { |
| 192 | if (UNLIKELY(enabled_)) { |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 193 | ImplicitlyAdvancePC(); |
| 194 | if (offset >= 0) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 195 | this->PushUint8(DW_CFA_def_cfa); |
| 196 | this->PushUleb128(reg.num()); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 197 | this->PushUleb128(offset); // Non-factored. |
| 198 | } else { |
| 199 | uses_dwarf3_features_ = true; |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 200 | this->PushUint8(DW_CFA_def_cfa_sf); |
| 201 | this->PushUleb128(reg.num()); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 202 | this->PushSleb128(FactorDataOffset(offset)); |
| 203 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 204 | } |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 205 | current_cfa_offset_ = offset; |
| 206 | } |
| 207 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 208 | void ALWAYS_INLINE DefCFARegister(Reg reg) { |
| 209 | if (UNLIKELY(enabled_)) { |
| 210 | ImplicitlyAdvancePC(); |
| 211 | this->PushUint8(DW_CFA_def_cfa_register); |
| 212 | this->PushUleb128(reg.num()); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | void ALWAYS_INLINE DefCFAOffset(int offset) { |
| 217 | if (UNLIKELY(enabled_)) { |
| 218 | if (current_cfa_offset_ != offset) { |
| 219 | ImplicitlyAdvancePC(); |
| 220 | if (offset >= 0) { |
| 221 | this->PushUint8(DW_CFA_def_cfa_offset); |
| 222 | this->PushUleb128(offset); // Non-factored. |
| 223 | } else { |
| 224 | uses_dwarf3_features_ = true; |
| 225 | this->PushUint8(DW_CFA_def_cfa_offset_sf); |
| 226 | this->PushSleb128(FactorDataOffset(offset)); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | // Uncoditional so that the user can still get and check the value. |
| 231 | current_cfa_offset_ = offset; |
| 232 | } |
| 233 | |
| 234 | void ALWAYS_INLINE ValOffset(Reg reg, int offset) { |
| 235 | if (UNLIKELY(enabled_)) { |
| 236 | ImplicitlyAdvancePC(); |
| 237 | uses_dwarf3_features_ = true; |
| 238 | int factored_offset = FactorDataOffset(offset); // May change sign. |
| 239 | if (factored_offset >= 0) { |
| 240 | this->PushUint8(DW_CFA_val_offset); |
| 241 | this->PushUleb128(reg.num()); |
| 242 | this->PushUleb128(factored_offset); |
| 243 | } else { |
| 244 | this->PushUint8(DW_CFA_val_offset_sf); |
| 245 | this->PushUleb128(reg.num()); |
| 246 | this->PushSleb128(factored_offset); |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 251 | void ALWAYS_INLINE DefCFAExpression(uint8_t* expr, int expr_size) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 252 | if (UNLIKELY(enabled_)) { |
| 253 | ImplicitlyAdvancePC(); |
| 254 | uses_dwarf3_features_ = true; |
| 255 | this->PushUint8(DW_CFA_def_cfa_expression); |
| 256 | this->PushUleb128(expr_size); |
| 257 | this->PushData(expr, expr_size); |
| 258 | } |
| 259 | } |
| 260 | |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 261 | void ALWAYS_INLINE Expression(Reg reg, uint8_t* expr, int expr_size) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 262 | if (UNLIKELY(enabled_)) { |
| 263 | ImplicitlyAdvancePC(); |
| 264 | uses_dwarf3_features_ = true; |
| 265 | this->PushUint8(DW_CFA_expression); |
| 266 | this->PushUleb128(reg.num()); |
| 267 | this->PushUleb128(expr_size); |
| 268 | this->PushData(expr, expr_size); |
| 269 | } |
| 270 | } |
| 271 | |
David Srbecky | 91cb54e | 2016-01-15 13:47:59 +0000 | [diff] [blame] | 272 | void ALWAYS_INLINE ValExpression(Reg reg, uint8_t* expr, int expr_size) { |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 273 | if (UNLIKELY(enabled_)) { |
| 274 | ImplicitlyAdvancePC(); |
| 275 | uses_dwarf3_features_ = true; |
| 276 | this->PushUint8(DW_CFA_val_expression); |
| 277 | this->PushUleb128(reg.num()); |
| 278 | this->PushUleb128(expr_size); |
| 279 | this->PushData(expr, expr_size); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | bool IsEnabled() const { return enabled_; } |
| 284 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 285 | void SetEnabled(bool value) { |
| 286 | enabled_ = value; |
| 287 | if (enabled_ && opcodes_.capacity() == 0u) { |
| 288 | opcodes_.reserve(kDefaultCapacity); |
| 289 | } |
| 290 | } |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 291 | |
| 292 | int GetCurrentPC() const { return current_pc_; } |
| 293 | |
| 294 | int GetCurrentCFAOffset() const { return current_cfa_offset_; } |
| 295 | |
| 296 | void SetCurrentCFAOffset(int offset) { current_cfa_offset_ = offset; } |
| 297 | |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 298 | using Writer<Vector>::data; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 299 | |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 300 | explicit DebugFrameOpCodeWriter(bool enabled = true, |
| 301 | const typename Vector::allocator_type& alloc = |
| 302 | typename Vector::allocator_type()) |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 303 | : Writer<Vector>(&opcodes_), |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 304 | enabled_(false), |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 305 | opcodes_(alloc), |
| 306 | current_cfa_offset_(0), |
| 307 | current_pc_(0), |
| 308 | uses_dwarf3_features_(false) { |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 309 | SetEnabled(enabled); |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | virtual ~DebugFrameOpCodeWriter() { } |
| 313 | |
| 314 | protected: |
Vladimir Marko | 10ef694 | 2015-10-22 15:25:54 +0100 | [diff] [blame] | 315 | // Best guess based on couple of observed outputs. |
| 316 | static constexpr size_t kDefaultCapacity = 32u; |
| 317 | |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 318 | int FactorDataOffset(int offset) const { |
| 319 | DCHECK_EQ(offset % kDataAlignmentFactor, 0); |
| 320 | return offset / kDataAlignmentFactor; |
| 321 | } |
| 322 | |
| 323 | int FactorCodeOffset(int offset) const { |
| 324 | DCHECK_EQ(offset % kCodeAlignmentFactor, 0); |
| 325 | return offset / kCodeAlignmentFactor; |
| 326 | } |
| 327 | |
David Srbecky | c6b4dd8 | 2015-04-07 20:32:43 +0100 | [diff] [blame] | 328 | bool enabled_; // If disabled all writes are no-ops. |
Vladimir Marko | ec7802a | 2015-10-01 20:57:57 +0100 | [diff] [blame] | 329 | Vector opcodes_; |
David Srbecky | 15c1975 | 2015-03-31 14:53:55 +0000 | [diff] [blame] | 330 | int current_cfa_offset_; |
| 331 | int current_pc_; |
| 332 | bool uses_dwarf3_features_; |
| 333 | |
| 334 | private: |
| 335 | DISALLOW_COPY_AND_ASSIGN(DebugFrameOpCodeWriter); |
| 336 | }; |
| 337 | |
| 338 | } // namespace dwarf |
| 339 | } // namespace art |
| 340 | |
David Srbecky | 4fda4eb | 2016-02-05 13:34:46 +0000 | [diff] [blame] | 341 | #endif // ART_COMPILER_DEBUG_DWARF_DEBUG_FRAME_OPCODE_WRITER_H_ |