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