David Sehr | cdcfde7 | 2016-09-26 07:44:04 -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 | * Implementation file of the dex layout visualization. |
| 17 | * |
| 18 | * This is a tool to read dex files into an internal representation, |
| 19 | * reorganize the representation, and emit dex files with a better |
| 20 | * file layout. |
| 21 | */ |
| 22 | |
| 23 | #include "dex_visualize.h" |
| 24 | |
| 25 | #include <inttypes.h> |
| 26 | #include <stdio.h> |
| 27 | |
| 28 | #include <functional> |
| 29 | #include <memory> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include "dex_ir.h" |
| 33 | #include "dexlayout.h" |
| 34 | #include "jit/offline_profiling_info.h" |
| 35 | |
| 36 | namespace art { |
| 37 | |
| 38 | struct FileSection { |
| 39 | public: |
| 40 | std::string name_; |
| 41 | uint16_t type_; |
| 42 | std::function<uint32_t(const dex_ir::Collections&)> size_fn_; |
| 43 | std::function<uint32_t(const dex_ir::Collections&)> offset_fn_; |
| 44 | }; |
| 45 | |
| 46 | static const std::vector<FileSection> kFileSections = { |
| 47 | { |
| 48 | "StringId", |
| 49 | DexFile::kDexTypeStringIdItem, |
| 50 | &dex_ir::Collections::StringIdsSize, |
| 51 | &dex_ir::Collections::StringIdsOffset |
| 52 | }, { |
| 53 | "TypeId", |
| 54 | DexFile::kDexTypeTypeIdItem, |
| 55 | &dex_ir::Collections::TypeIdsSize, |
| 56 | &dex_ir::Collections::TypeIdsOffset |
| 57 | }, { |
| 58 | "ProtoId", |
| 59 | DexFile::kDexTypeProtoIdItem, |
| 60 | &dex_ir::Collections::ProtoIdsSize, |
| 61 | &dex_ir::Collections::ProtoIdsOffset |
| 62 | }, { |
| 63 | "FieldId", |
| 64 | DexFile::kDexTypeFieldIdItem, |
| 65 | &dex_ir::Collections::FieldIdsSize, |
| 66 | &dex_ir::Collections::FieldIdsOffset |
| 67 | }, { |
| 68 | "MethodId", |
| 69 | DexFile::kDexTypeMethodIdItem, |
| 70 | &dex_ir::Collections::MethodIdsSize, |
| 71 | &dex_ir::Collections::MethodIdsOffset |
| 72 | }, { |
| 73 | "ClassDef", |
| 74 | DexFile::kDexTypeClassDefItem, |
| 75 | &dex_ir::Collections::ClassDefsSize, |
| 76 | &dex_ir::Collections::ClassDefsOffset |
| 77 | }, { |
| 78 | "StringData", |
| 79 | DexFile::kDexTypeStringDataItem, |
| 80 | &dex_ir::Collections::StringDatasSize, |
| 81 | &dex_ir::Collections::StringDatasOffset |
| 82 | }, { |
| 83 | "TypeList", |
| 84 | DexFile::kDexTypeTypeList, |
| 85 | &dex_ir::Collections::TypeListsSize, |
| 86 | &dex_ir::Collections::TypeListsOffset |
| 87 | }, { |
| 88 | "EncArr", |
| 89 | DexFile::kDexTypeEncodedArrayItem, |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 90 | &dex_ir::Collections::EncodedArrayItemsSize, |
| 91 | &dex_ir::Collections::EncodedArrayItemsOffset |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 92 | }, { |
| 93 | "Annotation", |
| 94 | DexFile::kDexTypeAnnotationItem, |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 95 | &dex_ir::Collections::AnnotationItemsSize, |
| 96 | &dex_ir::Collections::AnnotationItemsOffset |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 97 | }, { |
| 98 | "AnnoSet", |
| 99 | DexFile::kDexTypeAnnotationSetItem, |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 100 | &dex_ir::Collections::AnnotationSetItemsSize, |
| 101 | &dex_ir::Collections::AnnotationSetItemsOffset |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 102 | }, { |
| 103 | "AnnoSetRL", |
| 104 | DexFile::kDexTypeAnnotationSetRefList, |
| 105 | &dex_ir::Collections::AnnotationSetRefListsSize, |
| 106 | &dex_ir::Collections::AnnotationSetRefListsOffset |
| 107 | }, { |
| 108 | "AnnoDir", |
| 109 | DexFile::kDexTypeAnnotationsDirectoryItem, |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 110 | &dex_ir::Collections::AnnotationsDirectoryItemsSize, |
| 111 | &dex_ir::Collections::AnnotationsDirectoryItemsOffset |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 112 | }, { |
| 113 | "DebugInfo", |
| 114 | DexFile::kDexTypeDebugInfoItem, |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 115 | &dex_ir::Collections::DebugInfoItemsSize, |
| 116 | &dex_ir::Collections::DebugInfoItemsOffset |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 117 | }, { |
| 118 | "CodeItem", |
| 119 | DexFile::kDexTypeCodeItem, |
| 120 | &dex_ir::Collections::CodeItemsSize, |
| 121 | &dex_ir::Collections::CodeItemsOffset |
| 122 | }, { |
| 123 | "ClassData", |
| 124 | DexFile::kDexTypeClassDataItem, |
| 125 | &dex_ir::Collections::ClassDatasSize, |
| 126 | &dex_ir::Collections::ClassDatasOffset |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | class Dumper { |
| 131 | public: |
| 132 | // Colors are based on the type of the section in MapList. |
| 133 | Dumper(const dex_ir::Collections& collections, size_t dex_file_index) { |
| 134 | // Build the table that will map from offset to color |
| 135 | table_.emplace_back(DexFile::kDexTypeHeaderItem, 0u); |
| 136 | for (const FileSection& s : kFileSections) { |
| 137 | table_.emplace_back(s.type_, s.offset_fn_(collections)); |
| 138 | } |
| 139 | // Sort into descending order by offset. |
| 140 | std::sort(table_.begin(), |
| 141 | table_.end(), |
| 142 | [](const SectionColor& a, const SectionColor& b) { return a.offset_ > b.offset_; }); |
| 143 | // Open the file and emit the gnuplot prologue. |
| 144 | std::string dex_file_name("classes"); |
| 145 | std::string out_file_base_name("layout"); |
| 146 | if (dex_file_index > 0) { |
| 147 | out_file_base_name += std::to_string(dex_file_index + 1); |
| 148 | dex_file_name += std::to_string(dex_file_index + 1); |
| 149 | } |
| 150 | dex_file_name += ".dex"; |
| 151 | std::string out_file_name(out_file_base_name + ".gnuplot"); |
| 152 | std::string png_file_name(out_file_base_name + ".png"); |
| 153 | out_file_ = fopen(out_file_name.c_str(), "w"); |
| 154 | fprintf(out_file_, "set terminal png size 1920,1080\n"); |
| 155 | fprintf(out_file_, "set output \"%s\"\n", png_file_name.c_str()); |
| 156 | fprintf(out_file_, "set title \"%s\"\n", dex_file_name.c_str()); |
| 157 | fprintf(out_file_, "set xlabel \"Page offset into dex\"\n"); |
| 158 | fprintf(out_file_, "set ylabel \"ClassDef index\"\n"); |
| 159 | fprintf(out_file_, "set xtics rotate out ("); |
| 160 | fprintf(out_file_, "\"Header\" %d, ", 0); |
| 161 | bool printed_one = false; |
| 162 | for (const FileSection& s : kFileSections) { |
| 163 | if (s.size_fn_(collections) > 0) { |
| 164 | if (printed_one) { |
| 165 | fprintf(out_file_, ", "); |
| 166 | } |
| 167 | fprintf(out_file_, "\"%s\" %d", s.name_.c_str(), s.offset_fn_(collections) / kPageSize); |
| 168 | printed_one = true; |
| 169 | } |
| 170 | } |
| 171 | fprintf(out_file_, ")\n"); |
| 172 | fprintf(out_file_, |
| 173 | "plot \"-\" using 1:2:3:4:5 with vector nohead linewidth 1 lc variable notitle\n"); |
| 174 | } |
| 175 | |
| 176 | int GetColor(uint32_t offset) const { |
| 177 | // The dread linear search to find the right section for the reference. |
| 178 | uint16_t section = 0; |
| 179 | for (uint16_t i = 0; i < table_.size(); ++i) { |
| 180 | if (table_[i].offset_ < offset) { |
| 181 | section = table_[i].type_; |
| 182 | break; |
| 183 | } |
| 184 | } |
| 185 | // And a lookup table from type to color. |
| 186 | ColorMapType::const_iterator iter = kColorMap.find(section); |
| 187 | if (iter != kColorMap.end()) { |
| 188 | return iter->second; |
| 189 | } |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | void DumpAddressRange(uint32_t from, uint32_t size, int class_index) { |
| 194 | const uint32_t low_page = from / kPageSize; |
| 195 | const uint32_t high_page = (size > 0) ? (from + size - 1) / kPageSize : low_page; |
| 196 | const uint32_t size_delta = high_page - low_page; |
| 197 | fprintf(out_file_, "%d %d %d 0 %d\n", low_page, class_index, size_delta, GetColor(from)); |
| 198 | } |
| 199 | |
| 200 | void DumpAddressRange(const dex_ir::Item* item, int class_index) { |
| 201 | if (item != nullptr) { |
| 202 | DumpAddressRange(item->GetOffset(), item->GetSize(), class_index); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | void DumpStringData(const dex_ir::StringData* string_data, int class_index) { |
| 207 | DumpAddressRange(string_data, class_index); |
| 208 | } |
| 209 | |
| 210 | void DumpStringId(const dex_ir::StringId* string_id, int class_index) { |
| 211 | DumpAddressRange(string_id, class_index); |
| 212 | if (string_id == nullptr) { |
| 213 | return; |
| 214 | } |
| 215 | DumpStringData(string_id->DataItem(), class_index); |
| 216 | } |
| 217 | |
| 218 | void DumpTypeId(const dex_ir::TypeId* type_id, int class_index) { |
| 219 | DumpAddressRange(type_id, class_index); |
| 220 | DumpStringId(type_id->GetStringId(), class_index); |
| 221 | } |
| 222 | |
| 223 | void DumpFieldId(const dex_ir::FieldId* field_id, int class_index) { |
| 224 | DumpAddressRange(field_id, class_index); |
| 225 | if (field_id == nullptr) { |
| 226 | return; |
| 227 | } |
| 228 | DumpTypeId(field_id->Class(), class_index); |
| 229 | DumpTypeId(field_id->Type(), class_index); |
| 230 | DumpStringId(field_id->Name(), class_index); |
| 231 | } |
| 232 | |
| 233 | void DumpFieldItem(const dex_ir::FieldItem* field, int class_index) { |
| 234 | DumpAddressRange(field, class_index); |
| 235 | if (field == nullptr) { |
| 236 | return; |
| 237 | } |
| 238 | DumpFieldId(field->GetFieldId(), class_index); |
| 239 | } |
| 240 | |
| 241 | void DumpProtoId(const dex_ir::ProtoId* proto_id, int class_index) { |
| 242 | DumpAddressRange(proto_id, class_index); |
| 243 | if (proto_id == nullptr) { |
| 244 | return; |
| 245 | } |
| 246 | DumpStringId(proto_id->Shorty(), class_index); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 247 | const dex_ir::TypeList* type_list = proto_id->Parameters(); |
| 248 | if (type_list != nullptr) { |
| 249 | for (const dex_ir::TypeId* t : *type_list->GetTypeList()) { |
| 250 | DumpTypeId(t, class_index); |
| 251 | } |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 252 | } |
| 253 | DumpTypeId(proto_id->ReturnType(), class_index); |
| 254 | } |
| 255 | |
| 256 | void DumpMethodId(const dex_ir::MethodId* method_id, int class_index) { |
| 257 | DumpAddressRange(method_id, class_index); |
| 258 | if (method_id == nullptr) { |
| 259 | return; |
| 260 | } |
| 261 | DumpTypeId(method_id->Class(), class_index); |
| 262 | DumpProtoId(method_id->Proto(), class_index); |
| 263 | DumpStringId(method_id->Name(), class_index); |
| 264 | } |
| 265 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 266 | void DumpMethodItem(dex_ir::MethodItem* method, |
| 267 | const DexFile* dex_file, |
| 268 | int class_index, |
| 269 | ProfileCompilationInfo* profile_info) { |
| 270 | if (profile_info != nullptr) { |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 271 | uint32_t method_idx = method->GetMethodId()->GetIndex(); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 272 | if (!profile_info->ContainsMethod(MethodReference(dex_file, method_idx))) { |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 273 | return; |
| 274 | } |
| 275 | } |
| 276 | DumpAddressRange(method, class_index); |
| 277 | if (method == nullptr) { |
| 278 | return; |
| 279 | } |
| 280 | DumpMethodId(method->GetMethodId(), class_index); |
| 281 | const dex_ir::CodeItem* code_item = method->GetCodeItem(); |
| 282 | if (code_item != nullptr) { |
| 283 | DumpAddressRange(code_item, class_index); |
David Sehr | d1e44e2 | 2016-10-06 17:09:32 -0700 | [diff] [blame] | 284 | const dex_ir::CodeFixups* fixups = code_item->GetCodeFixups(); |
| 285 | if (fixups != nullptr) { |
| 286 | std::vector<dex_ir::TypeId*>* type_ids = fixups->TypeIds(); |
| 287 | for (dex_ir::TypeId* type_id : *type_ids) { |
| 288 | DumpTypeId(type_id, class_index); |
| 289 | } |
| 290 | std::vector<dex_ir::StringId*>* string_ids = fixups->StringIds(); |
| 291 | for (dex_ir::StringId* string_id : *string_ids) { |
| 292 | DumpStringId(string_id, class_index); |
| 293 | } |
| 294 | std::vector<dex_ir::MethodId*>* method_ids = fixups->MethodIds(); |
| 295 | for (dex_ir::MethodId* method_id : *method_ids) { |
| 296 | DumpMethodId(method_id, class_index); |
| 297 | } |
| 298 | std::vector<dex_ir::FieldId*>* field_ids = fixups->FieldIds(); |
| 299 | for (dex_ir::FieldId* field_id : *field_ids) { |
| 300 | DumpFieldId(field_id, class_index); |
| 301 | } |
| 302 | } |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | ~Dumper() { |
| 307 | fclose(out_file_); |
| 308 | } |
| 309 | |
| 310 | private: |
| 311 | struct SectionColor { |
| 312 | public: |
| 313 | SectionColor(uint16_t type, uint32_t offset) : type_(type), offset_(offset) { } |
| 314 | uint16_t type_; |
| 315 | uint32_t offset_; |
| 316 | }; |
| 317 | |
| 318 | using ColorMapType = std::map<uint16_t, int>; |
| 319 | const ColorMapType kColorMap = { |
| 320 | { DexFile::kDexTypeHeaderItem, 1 }, |
| 321 | { DexFile::kDexTypeStringIdItem, 2 }, |
| 322 | { DexFile::kDexTypeTypeIdItem, 3 }, |
| 323 | { DexFile::kDexTypeProtoIdItem, 4 }, |
| 324 | { DexFile::kDexTypeFieldIdItem, 5 }, |
| 325 | { DexFile::kDexTypeMethodIdItem, 6 }, |
| 326 | { DexFile::kDexTypeClassDefItem, 7 }, |
| 327 | { DexFile::kDexTypeTypeList, 8 }, |
| 328 | { DexFile::kDexTypeAnnotationSetRefList, 9 }, |
| 329 | { DexFile::kDexTypeAnnotationSetItem, 10 }, |
| 330 | { DexFile::kDexTypeClassDataItem, 11 }, |
| 331 | { DexFile::kDexTypeCodeItem, 12 }, |
| 332 | { DexFile::kDexTypeStringDataItem, 13 }, |
| 333 | { DexFile::kDexTypeDebugInfoItem, 14 }, |
| 334 | { DexFile::kDexTypeAnnotationItem, 15 }, |
| 335 | { DexFile::kDexTypeEncodedArrayItem, 16 }, |
| 336 | { DexFile::kDexTypeAnnotationsDirectoryItem, 16 } |
| 337 | }; |
| 338 | |
| 339 | std::vector<SectionColor> table_; |
| 340 | FILE* out_file_; |
| 341 | |
| 342 | DISALLOW_COPY_AND_ASSIGN(Dumper); |
| 343 | }; |
| 344 | |
| 345 | /* |
| 346 | * Dumps a gnuplot data file showing the parts of the dex_file that belong to each class. |
| 347 | * If profiling information is present, it dumps only those classes that are marked as hot. |
| 348 | */ |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 349 | void VisualizeDexLayout(dex_ir::Header* header, |
| 350 | const DexFile* dex_file, |
| 351 | size_t dex_file_index, |
| 352 | ProfileCompilationInfo* profile_info) { |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 353 | std::unique_ptr<Dumper> dumper(new Dumper(header->GetCollections(), dex_file_index)); |
| 354 | |
| 355 | const uint32_t class_defs_size = header->GetCollections().ClassDefsSize(); |
| 356 | for (uint32_t class_index = 0; class_index < class_defs_size; class_index++) { |
| 357 | dex_ir::ClassDef* class_def = header->GetCollections().GetClassDef(class_index); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 358 | dex::TypeIndex type_idx(class_def->ClassType()->GetIndex()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 359 | if (profile_info != nullptr && !profile_info->ContainsClass(*dex_file, type_idx)) { |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 360 | continue; |
| 361 | } |
| 362 | dumper->DumpAddressRange(class_def, class_index); |
| 363 | // Type id. |
| 364 | dumper->DumpTypeId(class_def->ClassType(), class_index); |
| 365 | // Superclass type id. |
| 366 | dumper->DumpTypeId(class_def->Superclass(), class_index); |
| 367 | // Interfaces. |
| 368 | // TODO(jeffhao): get TypeList from class_def to use Item interface. |
| 369 | static constexpr uint32_t kInterfaceSizeKludge = 8; |
| 370 | dumper->DumpAddressRange(class_def->InterfacesOffset(), kInterfaceSizeKludge, class_index); |
| 371 | // Source file info. |
| 372 | dumper->DumpStringId(class_def->SourceFile(), class_index); |
| 373 | // Annotations. |
| 374 | dumper->DumpAddressRange(class_def->Annotations(), class_index); |
| 375 | // TODO(sehr): walk the annotations and dump them. |
| 376 | // Class data. |
| 377 | dex_ir::ClassData* class_data = class_def->GetClassData(); |
| 378 | if (class_data != nullptr) { |
| 379 | dumper->DumpAddressRange(class_data, class_index); |
| 380 | if (class_data->StaticFields()) { |
| 381 | for (auto& field_item : *class_data->StaticFields()) { |
| 382 | dumper->DumpFieldItem(field_item.get(), class_index); |
| 383 | } |
| 384 | } |
| 385 | if (class_data->InstanceFields()) { |
| 386 | for (auto& field_item : *class_data->InstanceFields()) { |
| 387 | dumper->DumpFieldItem(field_item.get(), class_index); |
| 388 | } |
| 389 | } |
| 390 | if (class_data->DirectMethods()) { |
| 391 | for (auto& method_item : *class_data->DirectMethods()) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 392 | dumper->DumpMethodItem(method_item.get(), dex_file, class_index, profile_info); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | if (class_data->VirtualMethods()) { |
| 396 | for (auto& method_item : *class_data->VirtualMethods()) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 397 | dumper->DumpMethodItem(method_item.get(), dex_file, class_index, profile_info); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | } |
| 401 | } // for |
| 402 | } |
| 403 | |
| 404 | } // namespace art |