blob: be7bade8efb5c6adbcf261b69e4ea9d650ee07ee [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
32#include "dex_ir.h"
33#include "dexlayout.h"
34#include "jit/offline_profiling_info.h"
35
36namespace art {
37
38struct 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
46static 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,
90 &dex_ir::Collections::EncodedArraySize,
91 &dex_ir::Collections::EncodedArrayOffset
92 }, {
93 "Annotation",
94 DexFile::kDexTypeAnnotationItem,
95 &dex_ir::Collections::AnnotationSize,
96 &dex_ir::Collections::AnnotationOffset
97 }, {
98 "AnnoSet",
99 DexFile::kDexTypeAnnotationSetItem,
100 &dex_ir::Collections::AnnotationSetSize,
101 &dex_ir::Collections::AnnotationSetOffset
102 }, {
103 "AnnoSetRL",
104 DexFile::kDexTypeAnnotationSetRefList,
105 &dex_ir::Collections::AnnotationSetRefListsSize,
106 &dex_ir::Collections::AnnotationSetRefListsOffset
107 }, {
108 "AnnoDir",
109 DexFile::kDexTypeAnnotationsDirectoryItem,
110 &dex_ir::Collections::AnnotationsDirectorySize,
111 &dex_ir::Collections::AnnotationsDirectoryOffset
112 }, {
113 "DebugInfo",
114 DexFile::kDexTypeDebugInfoItem,
115 &dex_ir::Collections::DebugInfoSize,
116 &dex_ir::Collections::DebugInfoOffset
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
130class 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);
247 const dex_ir::TypeIdVector& parameters = proto_id->Parameters();
248 for (const dex_ir::TypeId* t : parameters) {
249 DumpTypeId(t, class_index);
250 }
251 DumpTypeId(proto_id->ReturnType(), class_index);
252 }
253
254 void DumpMethodId(const dex_ir::MethodId* method_id, int class_index) {
255 DumpAddressRange(method_id, class_index);
256 if (method_id == nullptr) {
257 return;
258 }
259 DumpTypeId(method_id->Class(), class_index);
260 DumpProtoId(method_id->Proto(), class_index);
261 DumpStringId(method_id->Name(), class_index);
262 }
263
264 void DumpMethodItem(const dex_ir::MethodItem* method, const DexFile* dex_file, int class_index) {
265 if (profile_info_ != nullptr) {
266 uint32_t method_idx = method->GetMethodId()->GetIndex();
267 MethodReference mr(dex_file, method_idx);
268 if (!profile_info_->ContainsMethod(mr)) {
269 return;
270 }
271 }
272 DumpAddressRange(method, class_index);
273 if (method == nullptr) {
274 return;
275 }
276 DumpMethodId(method->GetMethodId(), class_index);
277 const dex_ir::CodeItem* code_item = method->GetCodeItem();
278 if (code_item != nullptr) {
279 DumpAddressRange(code_item, class_index);
280 }
281 }
282
283 ~Dumper() {
284 fclose(out_file_);
285 }
286
287 private:
288 struct SectionColor {
289 public:
290 SectionColor(uint16_t type, uint32_t offset) : type_(type), offset_(offset) { }
291 uint16_t type_;
292 uint32_t offset_;
293 };
294
295 using ColorMapType = std::map<uint16_t, int>;
296 const ColorMapType kColorMap = {
297 { DexFile::kDexTypeHeaderItem, 1 },
298 { DexFile::kDexTypeStringIdItem, 2 },
299 { DexFile::kDexTypeTypeIdItem, 3 },
300 { DexFile::kDexTypeProtoIdItem, 4 },
301 { DexFile::kDexTypeFieldIdItem, 5 },
302 { DexFile::kDexTypeMethodIdItem, 6 },
303 { DexFile::kDexTypeClassDefItem, 7 },
304 { DexFile::kDexTypeTypeList, 8 },
305 { DexFile::kDexTypeAnnotationSetRefList, 9 },
306 { DexFile::kDexTypeAnnotationSetItem, 10 },
307 { DexFile::kDexTypeClassDataItem, 11 },
308 { DexFile::kDexTypeCodeItem, 12 },
309 { DexFile::kDexTypeStringDataItem, 13 },
310 { DexFile::kDexTypeDebugInfoItem, 14 },
311 { DexFile::kDexTypeAnnotationItem, 15 },
312 { DexFile::kDexTypeEncodedArrayItem, 16 },
313 { DexFile::kDexTypeAnnotationsDirectoryItem, 16 }
314 };
315
316 std::vector<SectionColor> table_;
317 FILE* out_file_;
318
319 DISALLOW_COPY_AND_ASSIGN(Dumper);
320};
321
322/*
323 * Dumps a gnuplot data file showing the parts of the dex_file that belong to each class.
324 * If profiling information is present, it dumps only those classes that are marked as hot.
325 */
326void VisualizeDexLayout(dex_ir::Header* header, const DexFile* dex_file, size_t dex_file_index) {
327 std::unique_ptr<Dumper> dumper(new Dumper(header->GetCollections(), dex_file_index));
328
329 const uint32_t class_defs_size = header->GetCollections().ClassDefsSize();
330 for (uint32_t class_index = 0; class_index < class_defs_size; class_index++) {
331 dex_ir::ClassDef* class_def = header->GetCollections().GetClassDef(class_index);
332 if (profile_info_ != nullptr && !profile_info_->ContainsClass(*dex_file, class_index)) {
333 continue;
334 }
335 dumper->DumpAddressRange(class_def, class_index);
336 // Type id.
337 dumper->DumpTypeId(class_def->ClassType(), class_index);
338 // Superclass type id.
339 dumper->DumpTypeId(class_def->Superclass(), class_index);
340 // Interfaces.
341 // TODO(jeffhao): get TypeList from class_def to use Item interface.
342 static constexpr uint32_t kInterfaceSizeKludge = 8;
343 dumper->DumpAddressRange(class_def->InterfacesOffset(), kInterfaceSizeKludge, class_index);
344 // Source file info.
345 dumper->DumpStringId(class_def->SourceFile(), class_index);
346 // Annotations.
347 dumper->DumpAddressRange(class_def->Annotations(), class_index);
348 // TODO(sehr): walk the annotations and dump them.
349 // Class data.
350 dex_ir::ClassData* class_data = class_def->GetClassData();
351 if (class_data != nullptr) {
352 dumper->DumpAddressRange(class_data, class_index);
353 if (class_data->StaticFields()) {
354 for (auto& field_item : *class_data->StaticFields()) {
355 dumper->DumpFieldItem(field_item.get(), class_index);
356 }
357 }
358 if (class_data->InstanceFields()) {
359 for (auto& field_item : *class_data->InstanceFields()) {
360 dumper->DumpFieldItem(field_item.get(), class_index);
361 }
362 }
363 if (class_data->DirectMethods()) {
364 for (auto& method_item : *class_data->DirectMethods()) {
365 dumper->DumpMethodItem(method_item.get(), dex_file, class_index);
366 }
367 }
368 if (class_data->VirtualMethods()) {
369 for (auto& method_item : *class_data->VirtualMethods()) {
370 dumper->DumpMethodItem(method_item.get(), dex_file, class_index);
371 }
372 }
373 }
374 } // for
375}
376
377} // namespace art