Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame] | 1 | /* |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 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 | * Main driver 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 | #include "dexlayout.h" |
| 24 | |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 25 | #include <fcntl.h> |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 26 | #include <stdio.h> |
| 27 | #include <string.h> |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 28 | #include <sys/stat.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 29 | #include <sys/types.h> |
| 30 | #include <unistd.h> |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 31 | |
Andreas Gampe | 5794381 | 2017-12-06 21:39:13 -0800 | [diff] [blame] | 32 | #include <android-base/logging.h> |
| 33 | |
| 34 | #include "base/logging.h" // For InitLogging. |
Calin Juravle | 33083d6 | 2017-01-18 15:29:12 -0800 | [diff] [blame] | 35 | #include "jit/profile_compilation_info.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 36 | #include "mem_map.h" |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 37 | #include "runtime.h" |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
| 41 | static const char* kProgramName = "dexlayout"; |
| 42 | |
| 43 | /* |
| 44 | * Shows usage. |
| 45 | */ |
| 46 | static void Usage(void) { |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 47 | fprintf(stderr, "Copyright (C) 2016 The Android Open Source Project\n\n"); |
| 48 | fprintf(stderr, "%s: [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile] [-p profile]" |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame] | 49 | " [-s] [-t] [-v] [-w directory] dexfile...\n\n", kProgramName); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 50 | fprintf(stderr, " -a : display annotations\n"); |
| 51 | fprintf(stderr, " -b : build dex_ir\n"); |
| 52 | fprintf(stderr, " -c : verify checksum and exit\n"); |
| 53 | fprintf(stderr, " -d : disassemble code sections\n"); |
| 54 | fprintf(stderr, " -e : display exported items only\n"); |
| 55 | fprintf(stderr, " -f : display summary information from file header\n"); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 56 | fprintf(stderr, " -h : display file header details\n"); |
| 57 | fprintf(stderr, " -i : ignore checksum failures\n"); |
| 58 | fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n"); |
| 59 | fprintf(stderr, " -o : output file name (defaults to stdout)\n"); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 60 | fprintf(stderr, " -p : profile file name (defaults to no profile)\n"); |
| 61 | fprintf(stderr, " -s : visualize reference pattern\n"); |
David Sehr | 9335749 | 2017-03-09 08:02:44 -0800 | [diff] [blame] | 62 | fprintf(stderr, " -t : display file section sizes\n"); |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame] | 63 | fprintf(stderr, " -v : verify output file is canonical to input (IR level comparison)\n"); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 64 | fprintf(stderr, " -w : output dex directory \n"); |
Mathieu Chartier | 5c36220 | 2018-01-17 14:52:55 -0800 | [diff] [blame^] | 65 | fprintf(stderr, " -x : compact dex generation level, either 'none' or 'fast'\n"); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Main driver of the dexlayout utility. |
| 70 | */ |
| 71 | int DexlayoutDriver(int argc, char** argv) { |
| 72 | // Art specific set up. |
Andreas Gampe | 51d80cc | 2017-06-21 21:05:13 -0700 | [diff] [blame] | 73 | InitLogging(argv, Runtime::Abort); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 74 | MemMap::Init(); |
| 75 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 76 | Options options; |
| 77 | options.dump_ = true; |
| 78 | options.verbose_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 79 | bool want_usage = false; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 80 | |
| 81 | // Parse all arguments. |
| 82 | while (1) { |
Mathieu Chartier | 5c36220 | 2018-01-17 14:52:55 -0800 | [diff] [blame^] | 83 | const int ic = getopt(argc, argv, "abcdefghil:mo:p:stvw:x:"); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 84 | if (ic < 0) { |
| 85 | break; // done |
| 86 | } |
| 87 | switch (ic) { |
| 88 | case 'a': // display annotations |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 89 | options.show_annotations_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 90 | break; |
| 91 | case 'b': // build dex_ir |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 92 | options.build_dex_ir_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 93 | break; |
| 94 | case 'c': // verify the checksum then exit |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 95 | options.checksum_only_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 96 | break; |
| 97 | case 'd': // disassemble Dalvik instructions |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 98 | options.disassemble_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 99 | break; |
| 100 | case 'e': // exported items only |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 101 | options.exports_only_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 102 | break; |
| 103 | case 'f': // display outer file header |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 104 | options.show_file_headers_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 105 | break; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 106 | case 'h': // display section headers, i.e. all meta-data |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 107 | options.show_section_headers_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 108 | break; |
| 109 | case 'i': // continue even if checksum is bad |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 110 | options.ignore_bad_checksum_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 111 | break; |
| 112 | case 'l': // layout |
| 113 | if (strcmp(optarg, "plain") == 0) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 114 | options.output_format_ = kOutputPlain; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 115 | } else if (strcmp(optarg, "xml") == 0) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 116 | options.output_format_ = kOutputXml; |
| 117 | options.verbose_ = false; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 118 | } else { |
| 119 | want_usage = true; |
| 120 | } |
| 121 | break; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 122 | case 'm': // output dex files to a memmap |
| 123 | options.output_to_memmap_ = true; |
| 124 | break; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 125 | case 'o': // output file |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 126 | options.output_file_name_ = optarg; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 127 | break; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 128 | case 'p': // profile file |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 129 | options.profile_file_name_ = optarg; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 130 | break; |
| 131 | case 's': // visualize access pattern |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 132 | options.visualize_pattern_ = true; |
| 133 | options.verbose_ = false; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 134 | break; |
David Sehr | 9335749 | 2017-03-09 08:02:44 -0800 | [diff] [blame] | 135 | case 't': // display section statistics |
| 136 | options.show_section_statistics_ = true; |
| 137 | options.verbose_ = false; |
| 138 | break; |
Jeff Hao | ec7f1a9 | 2017-03-13 16:24:24 -0700 | [diff] [blame] | 139 | case 'v': // verify output |
| 140 | options.verify_output_ = true; |
| 141 | break; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 142 | case 'w': // output dex files directory |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 143 | options.output_dex_directory_ = optarg; |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 144 | break; |
Mathieu Chartier | 5c36220 | 2018-01-17 14:52:55 -0800 | [diff] [blame^] | 145 | case 'x': // compact dex level |
| 146 | if (strcmp(optarg, "none") == 0) { |
| 147 | options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelNone; |
| 148 | } else if (strcmp(optarg, "fast") == 0) { |
| 149 | options.compact_dex_level_ = CompactDexLevel::kCompactDexLevelFast; |
| 150 | } else { |
| 151 | want_usage = true; |
| 152 | } |
| 153 | break; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 154 | default: |
| 155 | want_usage = true; |
| 156 | break; |
| 157 | } // switch |
| 158 | } // while |
| 159 | |
| 160 | // Detect early problems. |
| 161 | if (optind == argc) { |
| 162 | fprintf(stderr, "%s: no file specified\n", kProgramName); |
| 163 | want_usage = true; |
| 164 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 165 | if (options.checksum_only_ && options.ignore_bad_checksum_) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 166 | fprintf(stderr, "Can't specify both -c and -i\n"); |
| 167 | want_usage = true; |
| 168 | } |
| 169 | if (want_usage) { |
| 170 | Usage(); |
| 171 | return 2; |
| 172 | } |
| 173 | |
| 174 | // Open alternative output file. |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 175 | FILE* out_file = stdout; |
| 176 | if (options.output_file_name_) { |
| 177 | out_file = fopen(options.output_file_name_, "w"); |
| 178 | if (!out_file) { |
| 179 | fprintf(stderr, "Can't open %s\n", options.output_file_name_); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 180 | return 1; |
| 181 | } |
| 182 | } |
| 183 | |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 184 | // Open profile file. |
Andreas Gampe | 08ae77f | 2017-04-26 22:02:33 -0700 | [diff] [blame] | 185 | std::unique_ptr<ProfileCompilationInfo> profile_info; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 186 | if (options.profile_file_name_) { |
| 187 | int profile_fd = open(options.profile_file_name_, O_RDONLY); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 188 | if (profile_fd < 0) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 189 | fprintf(stderr, "Can't open %s\n", options.profile_file_name_); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 190 | return 1; |
| 191 | } |
Andreas Gampe | 08ae77f | 2017-04-26 22:02:33 -0700 | [diff] [blame] | 192 | profile_info.reset(new ProfileCompilationInfo()); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 193 | if (!profile_info->Load(profile_fd)) { |
| 194 | fprintf(stderr, "Can't read profile info from %s\n", options.profile_file_name_); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 195 | return 1; |
| 196 | } |
| 197 | } |
| 198 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 199 | // Create DexLayout instance. |
Andreas Gampe | 08ae77f | 2017-04-26 22:02:33 -0700 | [diff] [blame] | 200 | DexLayout dex_layout(options, profile_info.get(), out_file); |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 201 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 202 | // Process all files supplied on command line. |
| 203 | int result = 0; |
| 204 | while (optind < argc) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 205 | result |= dex_layout.ProcessFile(argv[optind++]); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 206 | } // while |
Andreas Gampe | 08ae77f | 2017-04-26 22:02:33 -0700 | [diff] [blame] | 207 | |
| 208 | if (options.output_file_name_) { |
| 209 | CHECK(out_file != nullptr && out_file != stdout); |
| 210 | fclose(out_file); |
| 211 | } |
| 212 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 213 | return result != 0; |
| 214 | } |
| 215 | |
| 216 | } // namespace art |
| 217 | |
| 218 | int main(int argc, char** argv) { |
| 219 | return art::DexlayoutDriver(argc, argv); |
| 220 | } |