Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 Sehr | 334b9d7 | 2018-02-12 18:27:56 -0800 | [diff] [blame] | 17 | #ifndef ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_ |
| 18 | #define ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_ |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 19 | |
Andreas Gampe | 1e96e13 | 2018-06-25 19:36:55 -0700 | [diff] [blame] | 20 | #include "dex_file.h" |
| 21 | |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 22 | #include "base/casts.h" |
David Sehr | 67bf42e | 2018-02-26 16:43:04 -0800 | [diff] [blame] | 23 | #include "base/leb128.h" |
Brian Carlstrom | a280655 | 2014-02-27 12:29:32 -0800 | [diff] [blame] | 24 | #include "base/stringpiece.h" |
Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 25 | #include "base/utils.h" |
Mathieu Chartier | 05dc23e | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 26 | #include "class_iterator.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 27 | #include "compact_dex_file.h" |
Andreas Gampe | 1e96e13 | 2018-06-25 19:36:55 -0700 | [diff] [blame] | 28 | #include "dex_instruction_iterator.h" |
Andreas Gampe | 04c6ab9 | 2017-06-08 21:49:14 -0700 | [diff] [blame] | 29 | #include "invoke_type.h" |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 30 | #include "standard_dex_file.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
| 34 | inline int32_t DexFile::GetStringLength(const StringId& string_id) const { |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 35 | const uint8_t* ptr = DataBegin() + string_id.string_data_off_; |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 36 | return DecodeUnsignedLeb128(&ptr); |
| 37 | } |
| 38 | |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 39 | inline const char* DexFile::GetStringDataAndUtf16Length(const StringId& string_id, |
| 40 | uint32_t* utf16_length) const { |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 41 | DCHECK(utf16_length != nullptr) << GetLocation(); |
Mathieu Chartier | c3a22aa | 2018-01-19 18:58:34 -0800 | [diff] [blame] | 42 | const uint8_t* ptr = DataBegin() + string_id.string_data_off_; |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 43 | *utf16_length = DecodeUnsignedLeb128(&ptr); |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 44 | return reinterpret_cast<const char*>(ptr); |
| 45 | } |
| 46 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 47 | inline const char* DexFile::GetStringData(const StringId& string_id) const { |
| 48 | uint32_t ignored; |
| 49 | return GetStringDataAndUtf16Length(string_id, &ignored); |
| 50 | } |
| 51 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 52 | inline const char* DexFile::StringDataAndUtf16LengthByIdx(dex::StringIndex idx, |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 53 | uint32_t* utf16_length) const { |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 54 | if (!idx.IsValid()) { |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 55 | *utf16_length = 0; |
| 56 | return nullptr; |
| 57 | } |
| 58 | const StringId& string_id = GetStringId(idx); |
| 59 | return GetStringDataAndUtf16Length(string_id, utf16_length); |
| 60 | } |
| 61 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 62 | inline const char* DexFile::StringDataByIdx(dex::StringIndex idx) const { |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 63 | uint32_t unicode_length; |
| 64 | return StringDataAndUtf16LengthByIdx(idx, &unicode_length); |
| 65 | } |
| 66 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 67 | inline const char* DexFile::StringByTypeIdx(dex::TypeIndex idx, uint32_t* unicode_length) const { |
Jeff Hao | 120504f | 2017-04-14 14:33:52 -0700 | [diff] [blame] | 68 | if (!idx.IsValid()) { |
| 69 | return nullptr; |
| 70 | } |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 71 | const TypeId& type_id = GetTypeId(idx); |
| 72 | return StringDataAndUtf16LengthByIdx(type_id.descriptor_idx_, unicode_length); |
| 73 | } |
| 74 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 75 | inline const char* DexFile::StringByTypeIdx(dex::TypeIndex idx) const { |
Jeff Hao | 120504f | 2017-04-14 14:33:52 -0700 | [diff] [blame] | 76 | if (!idx.IsValid()) { |
| 77 | return nullptr; |
| 78 | } |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 79 | const TypeId& type_id = GetTypeId(idx); |
| 80 | return StringDataByIdx(type_id.descriptor_idx_); |
| 81 | } |
| 82 | |
| 83 | inline const char* DexFile::GetTypeDescriptor(const TypeId& type_id) const { |
| 84 | return StringDataByIdx(type_id.descriptor_idx_); |
| 85 | } |
| 86 | |
| 87 | inline const char* DexFile::GetFieldTypeDescriptor(const FieldId& field_id) const { |
| 88 | const DexFile::TypeId& type_id = GetTypeId(field_id.type_idx_); |
| 89 | return GetTypeDescriptor(type_id); |
| 90 | } |
| 91 | |
| 92 | inline const char* DexFile::GetFieldName(const FieldId& field_id) const { |
| 93 | return StringDataByIdx(field_id.name_idx_); |
| 94 | } |
| 95 | |
| 96 | inline const char* DexFile::GetMethodDeclaringClassDescriptor(const MethodId& method_id) const { |
| 97 | const DexFile::TypeId& type_id = GetTypeId(method_id.class_idx_); |
| 98 | return GetTypeDescriptor(type_id); |
| 99 | } |
| 100 | |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 101 | inline const Signature DexFile::GetMethodSignature(const MethodId& method_id) const { |
| 102 | return Signature(this, GetProtoId(method_id.proto_idx_)); |
| 103 | } |
| 104 | |
Orion Hodson | b34bb19 | 2016-10-18 17:02:58 +0100 | [diff] [blame] | 105 | inline const Signature DexFile::GetProtoSignature(const ProtoId& proto_id) const { |
| 106 | return Signature(this, proto_id); |
| 107 | } |
| 108 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 109 | inline const char* DexFile::GetMethodName(const MethodId& method_id) const { |
| 110 | return StringDataByIdx(method_id.name_idx_); |
| 111 | } |
| 112 | |
David Srbecky | 39d8c87 | 2018-11-01 16:44:20 +0000 | [diff] [blame] | 113 | inline const char* DexFile::GetMethodName(const MethodId& method_id, uint32_t* utf_length) const { |
| 114 | return StringDataAndUtf16LengthByIdx(method_id.name_idx_, utf_length); |
| 115 | } |
| 116 | |
| 117 | inline const char* DexFile::GetMethodName(uint32_t idx, uint32_t* utf_length) const { |
| 118 | return StringDataAndUtf16LengthByIdx(GetMethodId(idx).name_idx_, utf_length); |
| 119 | } |
| 120 | |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 121 | inline const char* DexFile::GetMethodShorty(uint32_t idx) const { |
| 122 | return StringDataByIdx(GetProtoId(GetMethodId(idx).proto_idx_).shorty_idx_); |
| 123 | } |
| 124 | |
| 125 | inline const char* DexFile::GetMethodShorty(const MethodId& method_id) const { |
| 126 | return StringDataByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_); |
| 127 | } |
| 128 | |
| 129 | inline const char* DexFile::GetMethodShorty(const MethodId& method_id, uint32_t* length) const { |
| 130 | // Using the UTF16 length is safe here as shorties are guaranteed to be ASCII characters. |
| 131 | return StringDataAndUtf16LengthByIdx(GetProtoId(method_id.proto_idx_).shorty_idx_, length); |
| 132 | } |
| 133 | |
| 134 | inline const char* DexFile::GetClassDescriptor(const ClassDef& class_def) const { |
| 135 | return StringByTypeIdx(class_def.class_idx_); |
| 136 | } |
| 137 | |
| 138 | inline const char* DexFile::GetReturnTypeDescriptor(const ProtoId& proto_id) const { |
| 139 | return StringByTypeIdx(proto_id.return_type_idx_); |
| 140 | } |
| 141 | |
Orion Hodson | 06d10a7 | 2018-05-14 08:53:38 +0100 | [diff] [blame] | 142 | inline const char* DexFile::GetShorty(dex::ProtoIndex proto_idx) const { |
Vladimir Marko | 5c6a587 | 2016-06-27 13:50:16 +0100 | [diff] [blame] | 143 | const ProtoId& proto_id = GetProtoId(proto_idx); |
| 144 | return StringDataByIdx(proto_id.shorty_idx_); |
| 145 | } |
| 146 | |
Mathieu Chartier | 3da1d0f | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 147 | inline const DexFile::TryItem* DexFile::GetTryItems(const DexInstructionIterator& code_item_end, |
| 148 | uint32_t offset) { |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 149 | return reinterpret_cast<const TryItem*> |
Mathieu Chartier | 8740c66 | 2018-01-11 14:50:02 -0800 | [diff] [blame] | 150 | (RoundUp(reinterpret_cast<uintptr_t>(&code_item_end.Inst()), TryItem::kAlignment)) + offset; |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 151 | } |
| 152 | |
Andreas Gampe | 8a0128a | 2016-11-28 07:38:35 -0800 | [diff] [blame] | 153 | static inline bool DexFileStringEquals(const DexFile* df1, dex::StringIndex sidx1, |
| 154 | const DexFile* df2, dex::StringIndex sidx2) { |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 155 | uint32_t s1_len; // Note: utf16 length != mutf8 length. |
| 156 | const char* s1_data = df1->StringDataAndUtf16LengthByIdx(sidx1, &s1_len); |
| 157 | uint32_t s2_len; |
| 158 | const char* s2_data = df2->StringDataAndUtf16LengthByIdx(sidx2, &s2_len); |
| 159 | return (s1_len == s2_len) && (strcmp(s1_data, s2_data) == 0); |
| 160 | } |
| 161 | |
| 162 | inline bool Signature::operator==(const Signature& rhs) const { |
| 163 | if (dex_file_ == nullptr) { |
| 164 | return rhs.dex_file_ == nullptr; |
| 165 | } |
| 166 | if (rhs.dex_file_ == nullptr) { |
| 167 | return false; |
| 168 | } |
| 169 | if (dex_file_ == rhs.dex_file_) { |
| 170 | return proto_id_ == rhs.proto_id_; |
| 171 | } |
| 172 | uint32_t lhs_shorty_len; // For a shorty utf16 length == mutf8 length. |
| 173 | const char* lhs_shorty_data = dex_file_->StringDataAndUtf16LengthByIdx(proto_id_->shorty_idx_, |
| 174 | &lhs_shorty_len); |
| 175 | StringPiece lhs_shorty(lhs_shorty_data, lhs_shorty_len); |
| 176 | { |
| 177 | uint32_t rhs_shorty_len; |
| 178 | const char* rhs_shorty_data = |
| 179 | rhs.dex_file_->StringDataAndUtf16LengthByIdx(rhs.proto_id_->shorty_idx_, |
| 180 | &rhs_shorty_len); |
| 181 | StringPiece rhs_shorty(rhs_shorty_data, rhs_shorty_len); |
| 182 | if (lhs_shorty != rhs_shorty) { |
| 183 | return false; // Shorty mismatch. |
| 184 | } |
| 185 | } |
| 186 | if (lhs_shorty[0] == 'L') { |
| 187 | const DexFile::TypeId& return_type_id = dex_file_->GetTypeId(proto_id_->return_type_idx_); |
| 188 | const DexFile::TypeId& rhs_return_type_id = |
| 189 | rhs.dex_file_->GetTypeId(rhs.proto_id_->return_type_idx_); |
| 190 | if (!DexFileStringEquals(dex_file_, return_type_id.descriptor_idx_, |
| 191 | rhs.dex_file_, rhs_return_type_id.descriptor_idx_)) { |
| 192 | return false; // Return type mismatch. |
| 193 | } |
| 194 | } |
| 195 | if (lhs_shorty.find('L', 1) != StringPiece::npos) { |
| 196 | const DexFile::TypeList* params = dex_file_->GetProtoParameters(*proto_id_); |
| 197 | const DexFile::TypeList* rhs_params = rhs.dex_file_->GetProtoParameters(*rhs.proto_id_); |
Vladimir Marko | ba11882 | 2017-06-12 15:41:56 +0100 | [diff] [blame] | 198 | // We found a reference parameter in the matching shorty, so both lists must be non-empty. |
| 199 | DCHECK(params != nullptr); |
| 200 | DCHECK(rhs_params != nullptr); |
| 201 | uint32_t params_size = params->Size(); |
| 202 | DCHECK_EQ(params_size, rhs_params->Size()); // Parameter list size must match. |
| 203 | for (uint32_t i = 0; i < params_size; ++i) { |
| 204 | const DexFile::TypeId& param_id = dex_file_->GetTypeId(params->GetTypeItem(i).type_idx_); |
| 205 | const DexFile::TypeId& rhs_param_id = |
| 206 | rhs.dex_file_->GetTypeId(rhs_params->GetTypeItem(i).type_idx_); |
| 207 | if (!DexFileStringEquals(dex_file_, param_id.descriptor_idx_, |
| 208 | rhs.dex_file_, rhs_param_id.descriptor_idx_)) { |
| 209 | return false; // Parameter type mismatch. |
Ian Rogers | dfb325e | 2013-10-30 01:00:44 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | } |
| 213 | return true; |
| 214 | } |
| 215 | |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 216 | template<typename NewLocalCallback, typename IndexToStringData, typename TypeIndexToStringData> |
| 217 | bool DexFile::DecodeDebugLocalInfo(const uint8_t* stream, |
| 218 | const std::string& location, |
| 219 | const char* declaring_class_descriptor, |
| 220 | const std::vector<const char*>& arg_descriptors, |
| 221 | const std::string& method_name, |
| 222 | bool is_static, |
| 223 | uint16_t registers_size, |
| 224 | uint16_t ins_size, |
| 225 | uint16_t insns_size_in_code_units, |
Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 226 | const IndexToStringData& index_to_string_data, |
| 227 | const TypeIndexToStringData& type_index_to_string_data, |
| 228 | const NewLocalCallback& new_local_callback) { |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 229 | if (stream == nullptr) { |
| 230 | return false; |
| 231 | } |
| 232 | std::vector<LocalInfo> local_in_reg(registers_size); |
| 233 | |
| 234 | uint16_t arg_reg = registers_size - ins_size; |
| 235 | if (!is_static) { |
| 236 | const char* descriptor = declaring_class_descriptor; |
| 237 | local_in_reg[arg_reg].name_ = "this"; |
| 238 | local_in_reg[arg_reg].descriptor_ = descriptor; |
| 239 | local_in_reg[arg_reg].signature_ = nullptr; |
| 240 | local_in_reg[arg_reg].start_address_ = 0; |
| 241 | local_in_reg[arg_reg].reg_ = arg_reg; |
| 242 | local_in_reg[arg_reg].is_live_ = true; |
| 243 | arg_reg++; |
| 244 | } |
| 245 | |
| 246 | DecodeUnsignedLeb128(&stream); // Line. |
| 247 | uint32_t parameters_size = DecodeUnsignedLeb128(&stream); |
| 248 | uint32_t i; |
| 249 | if (parameters_size != arg_descriptors.size()) { |
| 250 | LOG(ERROR) << "invalid stream - problem with parameter iterator in " << location |
| 251 | << " for method " << method_name; |
| 252 | return false; |
| 253 | } |
| 254 | for (i = 0; i < parameters_size && i < arg_descriptors.size(); ++i) { |
| 255 | if (arg_reg >= registers_size) { |
| 256 | LOG(ERROR) << "invalid stream - arg reg >= reg size (" << arg_reg |
| 257 | << " >= " << registers_size << ") in " << location; |
| 258 | return false; |
| 259 | } |
| 260 | uint32_t name_idx = DecodeUnsignedLeb128P1(&stream); |
| 261 | const char* descriptor = arg_descriptors[i]; |
| 262 | local_in_reg[arg_reg].name_ = index_to_string_data(name_idx); |
| 263 | local_in_reg[arg_reg].descriptor_ = descriptor; |
| 264 | local_in_reg[arg_reg].signature_ = nullptr; |
| 265 | local_in_reg[arg_reg].start_address_ = 0; |
| 266 | local_in_reg[arg_reg].reg_ = arg_reg; |
| 267 | local_in_reg[arg_reg].is_live_ = true; |
| 268 | switch (*descriptor) { |
| 269 | case 'D': |
| 270 | case 'J': |
| 271 | arg_reg += 2; |
| 272 | break; |
| 273 | default: |
| 274 | arg_reg += 1; |
| 275 | break; |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | uint32_t address = 0; |
| 280 | for (;;) { |
| 281 | uint8_t opcode = *stream++; |
| 282 | switch (opcode) { |
| 283 | case DBG_END_SEQUENCE: |
| 284 | // Emit all variables which are still alive at the end of the method. |
| 285 | for (uint16_t reg = 0; reg < registers_size; reg++) { |
| 286 | if (local_in_reg[reg].is_live_) { |
| 287 | local_in_reg[reg].end_address_ = insns_size_in_code_units; |
Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 288 | new_local_callback(local_in_reg[reg]); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | return true; |
| 292 | case DBG_ADVANCE_PC: |
| 293 | address += DecodeUnsignedLeb128(&stream); |
| 294 | break; |
| 295 | case DBG_ADVANCE_LINE: |
| 296 | DecodeSignedLeb128(&stream); // Line. |
| 297 | break; |
| 298 | case DBG_START_LOCAL: |
| 299 | case DBG_START_LOCAL_EXTENDED: { |
| 300 | uint16_t reg = DecodeUnsignedLeb128(&stream); |
| 301 | if (reg >= registers_size) { |
| 302 | LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= " |
| 303 | << registers_size << ") in " << location; |
| 304 | return false; |
| 305 | } |
| 306 | |
| 307 | uint32_t name_idx = DecodeUnsignedLeb128P1(&stream); |
| 308 | uint16_t descriptor_idx = DecodeUnsignedLeb128P1(&stream); |
| 309 | uint32_t signature_idx = dex::kDexNoIndex; |
| 310 | if (opcode == DBG_START_LOCAL_EXTENDED) { |
| 311 | signature_idx = DecodeUnsignedLeb128P1(&stream); |
| 312 | } |
| 313 | |
| 314 | // Emit what was previously there, if anything |
| 315 | if (local_in_reg[reg].is_live_) { |
| 316 | local_in_reg[reg].end_address_ = address; |
Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 317 | new_local_callback(local_in_reg[reg]); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | local_in_reg[reg].name_ = index_to_string_data(name_idx); |
| 321 | local_in_reg[reg].descriptor_ = type_index_to_string_data(descriptor_idx);; |
| 322 | local_in_reg[reg].signature_ = index_to_string_data(signature_idx); |
| 323 | local_in_reg[reg].start_address_ = address; |
| 324 | local_in_reg[reg].reg_ = reg; |
| 325 | local_in_reg[reg].is_live_ = true; |
| 326 | break; |
| 327 | } |
| 328 | case DBG_END_LOCAL: { |
| 329 | uint16_t reg = DecodeUnsignedLeb128(&stream); |
| 330 | if (reg >= registers_size) { |
| 331 | LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= " |
| 332 | << registers_size << ") in " << location; |
| 333 | return false; |
| 334 | } |
| 335 | // If the register is live, close it properly. Otherwise, closing an already |
| 336 | // closed register is sloppy, but harmless if no further action is taken. |
| 337 | if (local_in_reg[reg].is_live_) { |
| 338 | local_in_reg[reg].end_address_ = address; |
Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 339 | new_local_callback(local_in_reg[reg]); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 340 | local_in_reg[reg].is_live_ = false; |
| 341 | } |
| 342 | break; |
| 343 | } |
| 344 | case DBG_RESTART_LOCAL: { |
| 345 | uint16_t reg = DecodeUnsignedLeb128(&stream); |
| 346 | if (reg >= registers_size) { |
| 347 | LOG(ERROR) << "invalid stream - reg >= reg size (" << reg << " >= " |
| 348 | << registers_size << ") in " << location; |
| 349 | return false; |
| 350 | } |
| 351 | // If the register is live, the "restart" is superfluous, |
| 352 | // and we don't want to mess with the existing start address. |
| 353 | if (!local_in_reg[reg].is_live_) { |
| 354 | local_in_reg[reg].start_address_ = address; |
| 355 | local_in_reg[reg].is_live_ = true; |
| 356 | } |
| 357 | break; |
| 358 | } |
| 359 | case DBG_SET_PROLOGUE_END: |
| 360 | case DBG_SET_EPILOGUE_BEGIN: |
| 361 | break; |
| 362 | case DBG_SET_FILE: |
| 363 | DecodeUnsignedLeb128P1(&stream); // name. |
| 364 | break; |
| 365 | default: |
| 366 | address += (opcode - DBG_FIRST_SPECIAL) / DBG_LINE_RANGE; |
| 367 | break; |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | template<typename NewLocalCallback> |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 373 | bool DexFile::DecodeDebugLocalInfo(uint32_t registers_size, |
| 374 | uint32_t ins_size, |
| 375 | uint32_t insns_size_in_code_units, |
Nicolas Geoffray | 58cc1cb | 2017-11-20 13:27:29 +0000 | [diff] [blame] | 376 | uint32_t debug_info_offset, |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 377 | bool is_static, |
| 378 | uint32_t method_idx, |
Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 379 | const NewLocalCallback& new_local_callback) const { |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 380 | const uint8_t* const stream = GetDebugInfoStream(debug_info_offset); |
| 381 | if (stream == nullptr) { |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 382 | return false; |
| 383 | } |
| 384 | std::vector<const char*> arg_descriptors; |
| 385 | DexFileParameterIterator it(*this, GetMethodPrototype(GetMethodId(method_idx))); |
| 386 | for (; it.HasNext(); it.Next()) { |
| 387 | arg_descriptors.push_back(it.GetDescriptor()); |
| 388 | } |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 389 | return DecodeDebugLocalInfo(stream, |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 390 | GetLocation(), |
| 391 | GetMethodDeclaringClassDescriptor(GetMethodId(method_idx)), |
| 392 | arg_descriptors, |
| 393 | this->PrettyMethod(method_idx), |
| 394 | is_static, |
Mathieu Chartier | 31f4c9f | 2017-12-08 15:46:11 -0800 | [diff] [blame] | 395 | registers_size, |
| 396 | ins_size, |
| 397 | insns_size_in_code_units, |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 398 | [this](uint32_t idx) { |
| 399 | return StringDataByIdx(dex::StringIndex(idx)); |
| 400 | }, |
| 401 | [this](uint32_t idx) { |
| 402 | return StringByTypeIdx(dex::TypeIndex( |
| 403 | dchecked_integral_cast<uint16_t>(idx))); |
| 404 | }, |
Mathieu Chartier | e5afbf3 | 2018-09-12 17:51:54 -0700 | [diff] [blame] | 405 | new_local_callback); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | template<typename DexDebugNewPosition, typename IndexToStringData> |
| 409 | bool DexFile::DecodeDebugPositionInfo(const uint8_t* stream, |
Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 410 | const IndexToStringData& index_to_string_data, |
| 411 | const DexDebugNewPosition& position_functor) { |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 412 | if (stream == nullptr) { |
| 413 | return false; |
| 414 | } |
| 415 | |
Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 416 | PositionInfo entry; |
| 417 | entry.line_ = DecodeDebugInfoParameterNames(&stream, VoidFunctor()); |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 418 | |
| 419 | for (;;) { |
| 420 | uint8_t opcode = *stream++; |
| 421 | switch (opcode) { |
| 422 | case DBG_END_SEQUENCE: |
| 423 | return true; // end of stream. |
| 424 | case DBG_ADVANCE_PC: |
| 425 | entry.address_ += DecodeUnsignedLeb128(&stream); |
| 426 | break; |
| 427 | case DBG_ADVANCE_LINE: |
| 428 | entry.line_ += DecodeSignedLeb128(&stream); |
| 429 | break; |
| 430 | case DBG_START_LOCAL: |
| 431 | DecodeUnsignedLeb128(&stream); // reg. |
| 432 | DecodeUnsignedLeb128P1(&stream); // name. |
| 433 | DecodeUnsignedLeb128P1(&stream); // descriptor. |
| 434 | break; |
| 435 | case DBG_START_LOCAL_EXTENDED: |
| 436 | DecodeUnsignedLeb128(&stream); // reg. |
| 437 | DecodeUnsignedLeb128P1(&stream); // name. |
| 438 | DecodeUnsignedLeb128P1(&stream); // descriptor. |
| 439 | DecodeUnsignedLeb128P1(&stream); // signature. |
| 440 | break; |
| 441 | case DBG_END_LOCAL: |
| 442 | case DBG_RESTART_LOCAL: |
| 443 | DecodeUnsignedLeb128(&stream); // reg. |
| 444 | break; |
| 445 | case DBG_SET_PROLOGUE_END: |
| 446 | entry.prologue_end_ = true; |
| 447 | break; |
| 448 | case DBG_SET_EPILOGUE_BEGIN: |
| 449 | entry.epilogue_begin_ = true; |
| 450 | break; |
| 451 | case DBG_SET_FILE: { |
| 452 | uint32_t name_idx = DecodeUnsignedLeb128P1(&stream); |
| 453 | entry.source_file_ = index_to_string_data(name_idx); |
| 454 | break; |
| 455 | } |
| 456 | default: { |
| 457 | int adjopcode = opcode - DBG_FIRST_SPECIAL; |
| 458 | entry.address_ += adjopcode / DBG_LINE_RANGE; |
| 459 | entry.line_ += DBG_LINE_BASE + (adjopcode % DBG_LINE_RANGE); |
Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 460 | if (position_functor(entry)) { |
David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 461 | return true; // early exit. |
| 462 | } |
| 463 | entry.prologue_end_ = false; |
| 464 | entry.epilogue_begin_ = false; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | |
Mathieu Chartier | 69147f1 | 2017-11-06 20:02:24 -0800 | [diff] [blame] | 471 | inline const CompactDexFile* DexFile::AsCompactDexFile() const { |
| 472 | DCHECK(IsCompactDexFile()); |
| 473 | return down_cast<const CompactDexFile*>(this); |
| 474 | } |
| 475 | |
| 476 | inline const StandardDexFile* DexFile::AsStandardDexFile() const { |
| 477 | DCHECK(IsStandardDexFile()); |
| 478 | return down_cast<const StandardDexFile*>(this); |
| 479 | } |
| 480 | |
Mathieu Chartier | 6238c83 | 2018-01-04 09:55:13 -0800 | [diff] [blame] | 481 | // Get the base of the encoded data for the given DexCode. |
| 482 | inline const uint8_t* DexFile::GetCatchHandlerData(const DexInstructionIterator& code_item_end, |
| 483 | uint32_t tries_size, |
| 484 | uint32_t offset) { |
| 485 | const uint8_t* handler_data = |
| 486 | reinterpret_cast<const uint8_t*>(GetTryItems(code_item_end, tries_size)); |
| 487 | return handler_data + offset; |
| 488 | } |
| 489 | |
Mathieu Chartier | 05dc23e | 2018-05-22 11:56:14 -0700 | [diff] [blame] | 490 | inline IterationRange<ClassIterator> DexFile::GetClasses() const { |
| 491 | return { ClassIterator(*this, 0u), ClassIterator(*this, NumClassDefs()) }; |
| 492 | } |
| 493 | |
Mathieu Chartier | 3e2e123 | 2018-09-11 12:35:30 -0700 | [diff] [blame] | 494 | // Returns the line number |
| 495 | template <typename Visitor> |
| 496 | inline uint32_t DexFile::DecodeDebugInfoParameterNames(const uint8_t** debug_info, |
| 497 | const Visitor& visitor) { |
| 498 | uint32_t line = DecodeUnsignedLeb128(debug_info); |
| 499 | const uint32_t parameters_size = DecodeUnsignedLeb128(debug_info); |
| 500 | for (uint32_t i = 0; i < parameters_size; ++i) { |
| 501 | visitor(dex::StringIndex(DecodeUnsignedLeb128P1(debug_info))); |
| 502 | } |
| 503 | return line; |
| 504 | } |
| 505 | |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 506 | } // namespace art |
| 507 | |
David Sehr | 334b9d7 | 2018-02-12 18:27:56 -0800 | [diff] [blame] | 508 | #endif // ART_LIBDEXFILE_DEX_DEX_FILE_INL_H_ |