| David Sehr | 7629f60 | 2016-08-07 16:01:51 -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 | * Header file of the dexlayout utility. |
| 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 | #ifndef ART_DEXLAYOUT_DEXLAYOUT_H_ |
| 24 | #define ART_DEXLAYOUT_DEXLAYOUT_H_ |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <stdio.h> |
| Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 28 | #include <unordered_map> |
| David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 29 | |
| David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 30 | #include "dex/compact_dex_level.h" |
| 31 | #include "dex/dex_file_layout.h" |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 32 | #include "dex_ir.h" |
| 33 | #include "mem_map.h" |
| 34 | |
| David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 35 | namespace art { |
| 36 | |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 37 | class DexFile; |
| 38 | class Instruction; |
| David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 39 | class ProfileCompilationInfo; |
| 40 | |
| David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 41 | /* Supported output formats. */ |
| 42 | enum OutputFormat { |
| 43 | kOutputPlain = 0, // default |
| 44 | kOutputXml, // XML-style |
| 45 | }; |
| 46 | |
| 47 | /* Command-line options. */ |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 48 | class Options { |
| 49 | public: |
| 50 | Options() = default; |
| 51 | |
| 52 | bool dump_ = false; |
| 53 | bool build_dex_ir_ = false; |
| 54 | bool checksum_only_ = false; |
| 55 | bool disassemble_ = false; |
| 56 | bool exports_only_ = false; |
| 57 | bool ignore_bad_checksum_ = false; |
| 58 | bool output_to_memmap_ = false; |
| 59 | bool show_annotations_ = false; |
| 60 | bool show_file_headers_ = false; |
| 61 | bool show_section_headers_ = false; |
| David Sehr | 9335749 | 2017-03-09 08:02:44 -0800 | [diff] [blame] | 62 | bool show_section_statistics_ = false; |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 63 | bool verbose_ = false; |
| Mathieu Chartier | 2ef3b88 | 2017-10-20 19:50:39 -0700 | [diff] [blame] | 64 | bool verify_output_ = kIsDebugBuild; |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 65 | bool visualize_pattern_ = false; |
| Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 66 | bool update_checksum_ = false; |
| Mathieu Chartier | 603ccab | 2017-10-20 14:34:28 -0700 | [diff] [blame] | 67 | CompactDexLevel compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone; |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 68 | OutputFormat output_format_ = kOutputPlain; |
| 69 | const char* output_dex_directory_ = nullptr; |
| 70 | const char* output_file_name_ = nullptr; |
| 71 | const char* profile_file_name_ = nullptr; |
| David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 72 | }; |
| 73 | |
| Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 74 | // Hotness info |
| 75 | class DexLayoutHotnessInfo { |
| 76 | public: |
| 77 | // Store layout information so that the offset calculation can specify the section sizes. |
| 78 | std::unordered_map<dex_ir::CodeItem*, LayoutType> code_item_layout_; |
| 79 | }; |
| 80 | |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 81 | class DexLayout { |
| 82 | public: |
| Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 83 | // Setting this to false disables class def layout entirely, which is stronger than strictly |
| 84 | // necessary to ensure the partial order w.r.t. class derivation. TODO: Re-enable (b/68317550). |
| 85 | static constexpr bool kChangeClassDefOrder = false; |
| 86 | |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 87 | DexLayout(Options& options, |
| 88 | ProfileCompilationInfo* info, |
| 89 | FILE* out_file, |
| 90 | dex_ir::Header* |
| 91 | header = nullptr) |
| 92 | : options_(options), info_(info), out_file_(out_file), header_(header) { } |
| 93 | |
| 94 | int ProcessFile(const char* file_name); |
| 95 | void ProcessDexFile(const char* file_name, const DexFile* dex_file, size_t dex_file_index); |
| 96 | |
| 97 | dex_ir::Header* GetHeader() const { return header_; } |
| 98 | void SetHeader(dex_ir::Header* header) { header_ = header; } |
| 99 | |
| 100 | MemMap* GetAndReleaseMemMap() { return mem_map_.release(); } |
| 101 | |
| Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 102 | DexLayoutSections& GetSections() { |
| Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 103 | return dex_sections_; |
| 104 | } |
| 105 | |
| Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 106 | const DexLayoutHotnessInfo& LayoutHotnessInfo() const { |
| 107 | return layout_hotness_info_; |
| 108 | } |
| 109 | |
| Mathieu Chartier | 2c4b084 | 2017-12-13 11:49:51 -0800 | [diff] [blame] | 110 | const Options& GetOptions() const { |
| 111 | return options_; |
| 112 | } |
| 113 | |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 114 | private: |
| 115 | void DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item); |
| 116 | void DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset); |
| 117 | void DumpCatches(const dex_ir::CodeItem* code); |
| 118 | void DumpClass(int idx, char** last_package); |
| 119 | void DumpClassAnnotations(int idx); |
| 120 | void DumpClassDef(int idx); |
| David Sehr | aa6abb0 | 2017-10-12 08:25:11 -0700 | [diff] [blame] | 121 | void DumpCode(uint32_t idx, |
| 122 | const dex_ir::CodeItem* code, |
| 123 | uint32_t code_offset, |
| 124 | const char* declaring_class_descriptor, |
| 125 | const char* method_name, |
| 126 | bool is_static, |
| 127 | const dex_ir::ProtoId* proto); |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 128 | void DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation); |
| 129 | void DumpEncodedValue(const dex_ir::EncodedValue* data); |
| 130 | void DumpFileHeader(); |
| 131 | void DumpIField(uint32_t idx, uint32_t flags, int i); |
| 132 | void DumpInstruction(const dex_ir::CodeItem* code, |
| 133 | uint32_t code_offset, |
| 134 | uint32_t insn_idx, |
| 135 | uint32_t insn_width, |
| 136 | const Instruction* dec_insn); |
| 137 | void DumpInterface(const dex_ir::TypeId* type_item, int i); |
| 138 | void DumpLocalInfo(const dex_ir::CodeItem* code); |
| 139 | void DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i); |
| 140 | void DumpPositionInfo(const dex_ir::CodeItem* code); |
| 141 | void DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init); |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 142 | void DumpDexFile(); |
| Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 143 | |
| Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 144 | void LayoutClassDefsAndClassData(const DexFile* dex_file); |
| 145 | void LayoutCodeItems(const DexFile* dex_file); |
| 146 | void LayoutStringData(const DexFile* dex_file); |
| Jeff Hao | 042e898 | 2016-10-19 11:17:11 -0700 | [diff] [blame] | 147 | |
| 148 | // Creates a new layout for the dex file based on profile info. |
| 149 | // Currently reorders ClassDefs, ClassDataItems, and CodeItems. |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 150 | void LayoutOutputFile(const DexFile* dex_file); |
| Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 151 | void OutputDexFile(const DexFile* dex_file, bool compute_offsets); |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 152 | |
| 153 | void DumpCFG(const DexFile* dex_file, int idx); |
| 154 | void DumpCFG(const DexFile* dex_file, uint32_t dex_method_idx, const DexFile::CodeItem* code); |
| 155 | |
| 156 | Options& options_; |
| 157 | ProfileCompilationInfo* info_; |
| 158 | FILE* out_file_; |
| 159 | dex_ir::Header* header_; |
| 160 | std::unique_ptr<MemMap> mem_map_; |
| Mathieu Chartier | 120aa28 | 2017-08-05 16:03:03 -0700 | [diff] [blame] | 161 | DexLayoutSections dex_sections_; |
| Mathieu Chartier | 3e0c517 | 2017-11-12 12:58:40 -0800 | [diff] [blame] | 162 | // Layout hotness information is only calculated when dexlayout is enabled. |
| 163 | DexLayoutHotnessInfo layout_hotness_info_; |
| Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 164 | |
| 165 | DISALLOW_COPY_AND_ASSIGN(DexLayout); |
| 166 | }; |
| David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 167 | |
| 168 | } // namespace art |
| 169 | |
| 170 | #endif // ART_DEXLAYOUT_DEXLAYOUT_H_ |