blob: 74b525372eaf777eed853408e642612114f438cc [file] [log] [blame]
David Sehr7629f602016-08-07 16:01:51 -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 * 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>
28
Jeff Haoea7c6292016-11-14 18:10:16 -080029#include "dex_ir.h"
30#include "mem_map.h"
31
David Sehr7629f602016-08-07 16:01:51 -070032namespace art {
33
Jeff Haoea7c6292016-11-14 18:10:16 -080034class DexFile;
35class Instruction;
David Sehrcdcfde72016-09-26 07:44:04 -070036class ProfileCompilationInfo;
37
David Sehr7629f602016-08-07 16:01:51 -070038/* Supported output formats. */
39enum OutputFormat {
40 kOutputPlain = 0, // default
41 kOutputXml, // XML-style
42};
43
44/* Command-line options. */
Jeff Haoea7c6292016-11-14 18:10:16 -080045class Options {
46 public:
47 Options() = default;
48
49 bool dump_ = false;
50 bool build_dex_ir_ = false;
51 bool checksum_only_ = false;
52 bool disassemble_ = false;
53 bool exports_only_ = false;
54 bool ignore_bad_checksum_ = false;
55 bool output_to_memmap_ = false;
56 bool show_annotations_ = false;
57 bool show_file_headers_ = false;
58 bool show_section_headers_ = false;
David Sehr93357492017-03-09 08:02:44 -080059 bool show_section_statistics_ = false;
Jeff Haoea7c6292016-11-14 18:10:16 -080060 bool verbose_ = false;
61 bool visualize_pattern_ = false;
62 OutputFormat output_format_ = kOutputPlain;
63 const char* output_dex_directory_ = nullptr;
64 const char* output_file_name_ = nullptr;
65 const char* profile_file_name_ = nullptr;
David Sehr7629f602016-08-07 16:01:51 -070066};
67
Jeff Haoea7c6292016-11-14 18:10:16 -080068class DexLayout {
69 public:
70 DexLayout(Options& options,
71 ProfileCompilationInfo* info,
72 FILE* out_file,
73 dex_ir::Header*
74 header = nullptr)
75 : options_(options), info_(info), out_file_(out_file), header_(header) { }
76
77 int ProcessFile(const char* file_name);
78 void ProcessDexFile(const char* file_name, const DexFile* dex_file, size_t dex_file_index);
79
80 dex_ir::Header* GetHeader() const { return header_; }
81 void SetHeader(dex_ir::Header* header) { header_ = header; }
82
83 MemMap* GetAndReleaseMemMap() { return mem_map_.release(); }
84
85 private:
86 void DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item);
87 void DumpBytecodes(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset);
88 void DumpCatches(const dex_ir::CodeItem* code);
89 void DumpClass(int idx, char** last_package);
90 void DumpClassAnnotations(int idx);
91 void DumpClassDef(int idx);
92 void DumpCode(uint32_t idx, const dex_ir::CodeItem* code, uint32_t code_offset);
93 void DumpEncodedAnnotation(dex_ir::EncodedAnnotation* annotation);
94 void DumpEncodedValue(const dex_ir::EncodedValue* data);
95 void DumpFileHeader();
96 void DumpIField(uint32_t idx, uint32_t flags, int i);
97 void DumpInstruction(const dex_ir::CodeItem* code,
98 uint32_t code_offset,
99 uint32_t insn_idx,
100 uint32_t insn_width,
101 const Instruction* dec_insn);
102 void DumpInterface(const dex_ir::TypeId* type_item, int i);
103 void DumpLocalInfo(const dex_ir::CodeItem* code);
104 void DumpMethod(uint32_t idx, uint32_t flags, const dex_ir::CodeItem* code, int i);
105 void DumpPositionInfo(const dex_ir::CodeItem* code);
106 void DumpSField(uint32_t idx, uint32_t flags, int i, dex_ir::EncodedValue* init);
Jeff Haoea7c6292016-11-14 18:10:16 -0800107 void DumpDexFile();
Jeff Hao042e8982016-10-19 11:17:11 -0700108
Jeff Haoe17f5892017-02-23 16:14:04 -0800109 std::vector<dex_ir::ClassData*> LayoutClassDefsAndClassData(const DexFile* dex_file);
110 int32_t LayoutCodeItems(std::vector<dex_ir::ClassData*> new_class_data_order);
111 bool IsNextSectionCodeItemAligned(uint32_t offset);
Jeff Haoea7c6292016-11-14 18:10:16 -0800112 template<class T> void FixupSection(std::map<uint32_t, std::unique_ptr<T>>& map, uint32_t diff);
113 void FixupSections(uint32_t offset, uint32_t diff);
Jeff Hao042e8982016-10-19 11:17:11 -0700114
115 // Creates a new layout for the dex file based on profile info.
116 // Currently reorders ClassDefs, ClassDataItems, and CodeItems.
Jeff Haoea7c6292016-11-14 18:10:16 -0800117 void LayoutOutputFile(const DexFile* dex_file);
118 void OutputDexFile(const std::string& dex_file_location);
119
120 void DumpCFG(const DexFile* dex_file, int idx);
121 void DumpCFG(const DexFile* dex_file, uint32_t dex_method_idx, const DexFile::CodeItem* code);
122
123 Options& options_;
124 ProfileCompilationInfo* info_;
125 FILE* out_file_;
126 dex_ir::Header* header_;
127 std::unique_ptr<MemMap> mem_map_;
128
129 DISALLOW_COPY_AND_ASSIGN(DexLayout);
130};
David Sehr7629f602016-08-07 16:01:51 -0700131
132} // namespace art
133
134#endif // ART_DEXLAYOUT_DEXLAYOUT_H_