blob: 27cec8d95133fffaa5c8b56427cb7650e28372e4 [file] [log] [blame]
David Sehrcdcfde72016-09-26 07:44:04 -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 * 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
Andreas Gampe221d9812018-01-22 17:48:56 -080032#include <android-base/logging.h>
33
David Sehrcdcfde72016-09-26 07:44:04 -070034#include "dex_ir.h"
35#include "dexlayout.h"
David Sehr82d046e2018-04-23 08:14:19 -070036#include "profile/profile_compilation_info.h"
David Sehrcdcfde72016-09-26 07:44:04 -070037
38namespace art {
39
David Sehr332b19e2017-03-15 10:42:46 -070040static std::string MultidexName(const std::string& prefix,
41 size_t dex_file_index,
42 const std::string& suffix) {
David Sehr93357492017-03-09 08:02:44 -080043 return prefix + ((dex_file_index > 0) ? std::to_string(dex_file_index + 1) : "") + suffix;
44}
45
David Sehrcdcfde72016-09-26 07:44:04 -070046class Dumper {
47 public:
48 // Colors are based on the type of the section in MapList.
David Sehr9037a3a2017-03-30 17:50:24 -070049 explicit Dumper(dex_ir::Header* header)
50 : out_file_(nullptr),
51 sorted_sections_(
52 dex_ir::GetSortedDexFileSections(header, dex_ir::SortDirection::kSortDescending)) { }
David Sehr93357492017-03-09 08:02:44 -080053
54 bool OpenAndPrintHeader(size_t dex_index) {
David Sehrcdcfde72016-09-26 07:44:04 -070055 // Open the file and emit the gnuplot prologue.
Andreas Gampedfcd82c2018-10-16 20:22:37 -070056 out_file_ = fopen(MultidexName("layout", dex_index, ".gnuplot").c_str(), "we");
David Sehr93357492017-03-09 08:02:44 -080057 if (out_file_ == nullptr) {
58 return false;
David Sehrcdcfde72016-09-26 07:44:04 -070059 }
David Sehrcdcfde72016-09-26 07:44:04 -070060 fprintf(out_file_, "set terminal png size 1920,1080\n");
David Sehr93357492017-03-09 08:02:44 -080061 fprintf(out_file_, "set output \"%s\"\n", MultidexName("layout", dex_index, ".png").c_str());
62 fprintf(out_file_, "set title \"%s\"\n", MultidexName("classes", dex_index, ".dex").c_str());
David Sehrcdcfde72016-09-26 07:44:04 -070063 fprintf(out_file_, "set xlabel \"Page offset into dex\"\n");
64 fprintf(out_file_, "set ylabel \"ClassDef index\"\n");
65 fprintf(out_file_, "set xtics rotate out (");
David Sehrcdcfde72016-09-26 07:44:04 -070066 bool printed_one = false;
David Sehr9037a3a2017-03-30 17:50:24 -070067
68 for (const dex_ir::DexFileSection& s : sorted_sections_) {
69 if (s.size > 0) {
David Sehrcdcfde72016-09-26 07:44:04 -070070 if (printed_one) {
71 fprintf(out_file_, ", ");
72 }
David Sehr9037a3a2017-03-30 17:50:24 -070073 fprintf(out_file_, "\"%s\" %d", s.name.c_str(), s.offset / kPageSize);
David Sehrcdcfde72016-09-26 07:44:04 -070074 printed_one = true;
75 }
76 }
77 fprintf(out_file_, ")\n");
78 fprintf(out_file_,
79 "plot \"-\" using 1:2:3:4:5 with vector nohead linewidth 1 lc variable notitle\n");
David Sehr93357492017-03-09 08:02:44 -080080 return true;
David Sehrcdcfde72016-09-26 07:44:04 -070081 }
82
83 int GetColor(uint32_t offset) const {
84 // The dread linear search to find the right section for the reference.
85 uint16_t section = 0;
David Sehr9037a3a2017-03-30 17:50:24 -070086 for (const dex_ir::DexFileSection& file_section : sorted_sections_) {
87 if (file_section.offset < offset) {
88 section = file_section.type;
David Sehrcdcfde72016-09-26 07:44:04 -070089 break;
90 }
91 }
92 // And a lookup table from type to color.
93 ColorMapType::const_iterator iter = kColorMap.find(section);
94 if (iter != kColorMap.end()) {
95 return iter->second;
96 }
97 return 0;
98 }
99
100 void DumpAddressRange(uint32_t from, uint32_t size, int class_index) {
101 const uint32_t low_page = from / kPageSize;
102 const uint32_t high_page = (size > 0) ? (from + size - 1) / kPageSize : low_page;
103 const uint32_t size_delta = high_page - low_page;
104 fprintf(out_file_, "%d %d %d 0 %d\n", low_page, class_index, size_delta, GetColor(from));
105 }
106
107 void DumpAddressRange(const dex_ir::Item* item, int class_index) {
108 if (item != nullptr) {
109 DumpAddressRange(item->GetOffset(), item->GetSize(), class_index);
110 }
111 }
112
113 void DumpStringData(const dex_ir::StringData* string_data, int class_index) {
114 DumpAddressRange(string_data, class_index);
115 }
116
117 void DumpStringId(const dex_ir::StringId* string_id, int class_index) {
118 DumpAddressRange(string_id, class_index);
119 if (string_id == nullptr) {
120 return;
121 }
122 DumpStringData(string_id->DataItem(), class_index);
123 }
124
125 void DumpTypeId(const dex_ir::TypeId* type_id, int class_index) {
126 DumpAddressRange(type_id, class_index);
127 DumpStringId(type_id->GetStringId(), class_index);
128 }
129
130 void DumpFieldId(const dex_ir::FieldId* field_id, int class_index) {
131 DumpAddressRange(field_id, class_index);
132 if (field_id == nullptr) {
133 return;
134 }
135 DumpTypeId(field_id->Class(), class_index);
136 DumpTypeId(field_id->Type(), class_index);
137 DumpStringId(field_id->Name(), class_index);
138 }
139
140 void DumpFieldItem(const dex_ir::FieldItem* field, int class_index) {
141 DumpAddressRange(field, class_index);
142 if (field == nullptr) {
143 return;
144 }
145 DumpFieldId(field->GetFieldId(), class_index);
146 }
147
148 void DumpProtoId(const dex_ir::ProtoId* proto_id, int class_index) {
149 DumpAddressRange(proto_id, class_index);
150 if (proto_id == nullptr) {
151 return;
152 }
153 DumpStringId(proto_id->Shorty(), class_index);
Jeff Haoa8621002016-10-04 18:13:44 +0000154 const dex_ir::TypeList* type_list = proto_id->Parameters();
155 if (type_list != nullptr) {
156 for (const dex_ir::TypeId* t : *type_list->GetTypeList()) {
157 DumpTypeId(t, class_index);
158 }
David Sehrcdcfde72016-09-26 07:44:04 -0700159 }
160 DumpTypeId(proto_id->ReturnType(), class_index);
161 }
162
163 void DumpMethodId(const dex_ir::MethodId* method_id, int class_index) {
164 DumpAddressRange(method_id, class_index);
165 if (method_id == nullptr) {
166 return;
167 }
168 DumpTypeId(method_id->Class(), class_index);
169 DumpProtoId(method_id->Proto(), class_index);
170 DumpStringId(method_id->Name(), class_index);
171 }
172
Jeff Haoea7c6292016-11-14 18:10:16 -0800173 void DumpMethodItem(dex_ir::MethodItem* method,
174 const DexFile* dex_file,
175 int class_index,
176 ProfileCompilationInfo* profile_info) {
177 if (profile_info != nullptr) {
David Sehrcdcfde72016-09-26 07:44:04 -0700178 uint32_t method_idx = method->GetMethodId()->GetIndex();
Mathieu Chartierbbe3a5e2017-06-13 16:36:17 -0700179 if (!profile_info->GetMethodHotness(MethodReference(dex_file, method_idx)).IsHot()) {
David Sehrcdcfde72016-09-26 07:44:04 -0700180 return;
181 }
182 }
183 DumpAddressRange(method, class_index);
184 if (method == nullptr) {
185 return;
186 }
187 DumpMethodId(method->GetMethodId(), class_index);
188 const dex_ir::CodeItem* code_item = method->GetCodeItem();
189 if (code_item != nullptr) {
190 DumpAddressRange(code_item, class_index);
David Sehrd1e44e22016-10-06 17:09:32 -0700191 const dex_ir::CodeFixups* fixups = code_item->GetCodeFixups();
192 if (fixups != nullptr) {
Vladimir Marko219cb902017-12-07 16:20:39 +0000193 for (dex_ir::TypeId* type_id : fixups->TypeIds()) {
David Sehrd1e44e22016-10-06 17:09:32 -0700194 DumpTypeId(type_id, class_index);
195 }
Vladimir Marko219cb902017-12-07 16:20:39 +0000196 for (dex_ir::StringId* string_id : fixups->StringIds()) {
David Sehrd1e44e22016-10-06 17:09:32 -0700197 DumpStringId(string_id, class_index);
198 }
Vladimir Marko219cb902017-12-07 16:20:39 +0000199 for (dex_ir::MethodId* method_id : fixups->MethodIds()) {
David Sehrd1e44e22016-10-06 17:09:32 -0700200 DumpMethodId(method_id, class_index);
201 }
Vladimir Marko219cb902017-12-07 16:20:39 +0000202 for (dex_ir::FieldId* field_id : fixups->FieldIds()) {
David Sehrd1e44e22016-10-06 17:09:32 -0700203 DumpFieldId(field_id, class_index);
204 }
205 }
David Sehrcdcfde72016-09-26 07:44:04 -0700206 }
207 }
208
209 ~Dumper() {
210 fclose(out_file_);
211 }
212
213 private:
David Sehrcdcfde72016-09-26 07:44:04 -0700214 using ColorMapType = std::map<uint16_t, int>;
215 const ColorMapType kColorMap = {
216 { DexFile::kDexTypeHeaderItem, 1 },
217 { DexFile::kDexTypeStringIdItem, 2 },
218 { DexFile::kDexTypeTypeIdItem, 3 },
219 { DexFile::kDexTypeProtoIdItem, 4 },
220 { DexFile::kDexTypeFieldIdItem, 5 },
221 { DexFile::kDexTypeMethodIdItem, 6 },
222 { DexFile::kDexTypeClassDefItem, 7 },
223 { DexFile::kDexTypeTypeList, 8 },
224 { DexFile::kDexTypeAnnotationSetRefList, 9 },
225 { DexFile::kDexTypeAnnotationSetItem, 10 },
226 { DexFile::kDexTypeClassDataItem, 11 },
227 { DexFile::kDexTypeCodeItem, 12 },
228 { DexFile::kDexTypeStringDataItem, 13 },
229 { DexFile::kDexTypeDebugInfoItem, 14 },
230 { DexFile::kDexTypeAnnotationItem, 15 },
231 { DexFile::kDexTypeEncodedArrayItem, 16 },
232 { DexFile::kDexTypeAnnotationsDirectoryItem, 16 }
233 };
234
David Sehrcdcfde72016-09-26 07:44:04 -0700235 FILE* out_file_;
David Sehr9037a3a2017-03-30 17:50:24 -0700236 std::vector<dex_ir::DexFileSection> sorted_sections_;
David Sehrcdcfde72016-09-26 07:44:04 -0700237
238 DISALLOW_COPY_AND_ASSIGN(Dumper);
239};
240
241/*
242 * Dumps a gnuplot data file showing the parts of the dex_file that belong to each class.
243 * If profiling information is present, it dumps only those classes that are marked as hot.
244 */
Jeff Haoea7c6292016-11-14 18:10:16 -0800245void VisualizeDexLayout(dex_ir::Header* header,
246 const DexFile* dex_file,
247 size_t dex_file_index,
248 ProfileCompilationInfo* profile_info) {
David Sehr9037a3a2017-03-30 17:50:24 -0700249 std::unique_ptr<Dumper> dumper(new Dumper(header));
David Sehr93357492017-03-09 08:02:44 -0800250 if (!dumper->OpenAndPrintHeader(dex_file_index)) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800251 LOG(ERROR) << "Could not open output file.";
David Sehr93357492017-03-09 08:02:44 -0800252 return;
253 }
David Sehrcdcfde72016-09-26 07:44:04 -0700254
David Sehr2b5a38f2018-06-14 15:13:04 -0700255 const uint32_t class_defs_size = header->ClassDefs().Size();
David Sehrcdcfde72016-09-26 07:44:04 -0700256 for (uint32_t class_index = 0; class_index < class_defs_size; class_index++) {
David Sehr2b5a38f2018-06-14 15:13:04 -0700257 dex_ir::ClassDef* class_def = header->ClassDefs()[class_index];
Andreas Gampea5b09a62016-11-17 15:21:22 -0800258 dex::TypeIndex type_idx(class_def->ClassType()->GetIndex());
Jeff Haoea7c6292016-11-14 18:10:16 -0800259 if (profile_info != nullptr && !profile_info->ContainsClass(*dex_file, type_idx)) {
David Sehrcdcfde72016-09-26 07:44:04 -0700260 continue;
261 }
262 dumper->DumpAddressRange(class_def, class_index);
263 // Type id.
264 dumper->DumpTypeId(class_def->ClassType(), class_index);
265 // Superclass type id.
266 dumper->DumpTypeId(class_def->Superclass(), class_index);
267 // Interfaces.
268 // TODO(jeffhao): get TypeList from class_def to use Item interface.
269 static constexpr uint32_t kInterfaceSizeKludge = 8;
270 dumper->DumpAddressRange(class_def->InterfacesOffset(), kInterfaceSizeKludge, class_index);
271 // Source file info.
272 dumper->DumpStringId(class_def->SourceFile(), class_index);
273 // Annotations.
274 dumper->DumpAddressRange(class_def->Annotations(), class_index);
275 // TODO(sehr): walk the annotations and dump them.
276 // Class data.
277 dex_ir::ClassData* class_data = class_def->GetClassData();
278 if (class_data != nullptr) {
279 dumper->DumpAddressRange(class_data, class_index);
280 if (class_data->StaticFields()) {
281 for (auto& field_item : *class_data->StaticFields()) {
David Sehrd83437c2018-06-11 14:06:23 -0700282 dumper->DumpFieldItem(&field_item, class_index);
David Sehrcdcfde72016-09-26 07:44:04 -0700283 }
284 }
285 if (class_data->InstanceFields()) {
286 for (auto& field_item : *class_data->InstanceFields()) {
David Sehrd83437c2018-06-11 14:06:23 -0700287 dumper->DumpFieldItem(&field_item, class_index);
David Sehrcdcfde72016-09-26 07:44:04 -0700288 }
289 }
290 if (class_data->DirectMethods()) {
291 for (auto& method_item : *class_data->DirectMethods()) {
David Sehrd83437c2018-06-11 14:06:23 -0700292 dumper->DumpMethodItem(&method_item, dex_file, class_index, profile_info);
David Sehrcdcfde72016-09-26 07:44:04 -0700293 }
294 }
295 if (class_data->VirtualMethods()) {
296 for (auto& method_item : *class_data->VirtualMethods()) {
David Sehrd83437c2018-06-11 14:06:23 -0700297 dumper->DumpMethodItem(&method_item, dex_file, class_index, profile_info);
David Sehrcdcfde72016-09-26 07:44:04 -0700298 }
299 }
300 }
301 } // for
302}
303
David Sehr332b19e2017-03-15 10:42:46 -0700304static uint32_t FindNextByteAfterSection(dex_ir::Header* header,
David Sehr9037a3a2017-03-30 17:50:24 -0700305 const std::vector<dex_ir::DexFileSection>& sorted_sections,
David Sehr332b19e2017-03-15 10:42:46 -0700306 size_t section_index) {
307 for (size_t i = section_index + 1; i < sorted_sections.size(); ++i) {
Vladimir Marko35d5b8a2018-07-03 09:18:32 +0100308 const dex_ir::DexFileSection& section = sorted_sections[i];
David Sehr9037a3a2017-03-30 17:50:24 -0700309 if (section.size != 0) {
310 return section.offset;
David Sehr332b19e2017-03-15 10:42:46 -0700311 }
312 }
313 return header->FileSize();
314}
315
David Sehr93357492017-03-09 08:02:44 -0800316/*
317 * Dumps the offset and size of sections within the file.
318 */
319void ShowDexSectionStatistics(dex_ir::Header* header, size_t dex_file_index) {
320 // Compute the (multidex) class file name).
David Sehr332b19e2017-03-15 10:42:46 -0700321 fprintf(stdout, "%s (%d bytes)\n",
322 MultidexName("classes", dex_file_index, ".dex").c_str(),
323 header->FileSize());
324 fprintf(stdout, "section offset items bytes pages pct\n");
David Sehr9037a3a2017-03-30 17:50:24 -0700325 std::vector<dex_ir::DexFileSection> sorted_sections =
326 GetSortedDexFileSections(header, dex_ir::SortDirection::kSortAscending);
David Sehr332b19e2017-03-15 10:42:46 -0700327 for (size_t i = 0; i < sorted_sections.size(); ++i) {
David Sehr9037a3a2017-03-30 17:50:24 -0700328 const dex_ir::DexFileSection& file_section = sorted_sections[i];
David Sehr332b19e2017-03-15 10:42:46 -0700329 uint32_t bytes = 0;
David Sehr9037a3a2017-03-30 17:50:24 -0700330 if (file_section.size > 0) {
331 bytes = FindNextByteAfterSection(header, sorted_sections, i) - file_section.offset;
David Sehr332b19e2017-03-15 10:42:46 -0700332 }
David Sehr9037a3a2017-03-30 17:50:24 -0700333 fprintf(stdout,
334 "%-10s %8d %8d %8d %8d %%%02d\n",
335 file_section.name.c_str(),
336 file_section.offset,
337 file_section.size,
338 bytes,
339 RoundUp(bytes, kPageSize) / kPageSize,
340 100 * bytes / header->FileSize());
David Sehr93357492017-03-09 08:02:44 -0800341 }
342 fprintf(stdout, "\n");
343}
344
David Sehrcdcfde72016-09-26 07:44:04 -0700345} // namespace art