David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 1 | /* |
| 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" |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 33 | #include "jit/offline_profiling_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 | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 47 | " [-s] [-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"); |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 60 | fprintf(stderr, " -w : output dex directory \n"); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /* |
| 64 | * Main driver of the dexlayout utility. |
| 65 | */ |
| 66 | int DexlayoutDriver(int argc, char** argv) { |
| 67 | // Art specific set up. |
David Sehr | f57589f | 2016-10-17 10:09:33 -0700 | [diff] [blame] | 68 | InitLogging(argv, Runtime::Aborter); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 69 | MemMap::Init(); |
| 70 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 71 | Options options; |
| 72 | options.dump_ = true; |
| 73 | options.verbose_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 74 | bool want_usage = false; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 75 | |
| 76 | // Parse all arguments. |
| 77 | while (1) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 78 | const int ic = getopt(argc, argv, "abcdefghil:mo:p:sw:"); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 79 | if (ic < 0) { |
| 80 | break; // done |
| 81 | } |
| 82 | switch (ic) { |
| 83 | case 'a': // display annotations |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 84 | options.show_annotations_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 85 | break; |
| 86 | case 'b': // build dex_ir |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 87 | options.build_dex_ir_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 88 | break; |
| 89 | case 'c': // verify the checksum then exit |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 90 | options.checksum_only_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 91 | break; |
| 92 | case 'd': // disassemble Dalvik instructions |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 93 | options.disassemble_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 94 | break; |
| 95 | case 'e': // exported items only |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 96 | options.exports_only_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 97 | break; |
| 98 | case 'f': // display outer file header |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 99 | options.show_file_headers_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 100 | break; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 101 | case 'h': // display section headers, i.e. all meta-data |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 102 | options.show_section_headers_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 103 | break; |
| 104 | case 'i': // continue even if checksum is bad |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 105 | options.ignore_bad_checksum_ = true; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 106 | break; |
| 107 | case 'l': // layout |
| 108 | if (strcmp(optarg, "plain") == 0) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 109 | options.output_format_ = kOutputPlain; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 110 | } else if (strcmp(optarg, "xml") == 0) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 111 | options.output_format_ = kOutputXml; |
| 112 | options.verbose_ = false; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 113 | } else { |
| 114 | want_usage = true; |
| 115 | } |
| 116 | break; |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 117 | case 'm': // output dex files to a memmap |
| 118 | options.output_to_memmap_ = true; |
| 119 | break; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 120 | case 'o': // output file |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 121 | options.output_file_name_ = optarg; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 122 | break; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 123 | case 'p': // profile file |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 124 | options.profile_file_name_ = optarg; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 125 | break; |
| 126 | case 's': // visualize access pattern |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 127 | options.visualize_pattern_ = true; |
| 128 | options.verbose_ = false; |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 129 | break; |
Jeff Hao | a862100 | 2016-10-04 18:13:44 +0000 | [diff] [blame] | 130 | case 'w': // output dex files directory |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 131 | options.output_dex_directory_ = optarg; |
Jeff Hao | 3ab96b4 | 2016-09-09 18:35:01 -0700 | [diff] [blame] | 132 | break; |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 133 | default: |
| 134 | want_usage = true; |
| 135 | break; |
| 136 | } // switch |
| 137 | } // while |
| 138 | |
| 139 | // Detect early problems. |
| 140 | if (optind == argc) { |
| 141 | fprintf(stderr, "%s: no file specified\n", kProgramName); |
| 142 | want_usage = true; |
| 143 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 144 | if (options.checksum_only_ && options.ignore_bad_checksum_) { |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 145 | fprintf(stderr, "Can't specify both -c and -i\n"); |
| 146 | want_usage = true; |
| 147 | } |
| 148 | if (want_usage) { |
| 149 | Usage(); |
| 150 | return 2; |
| 151 | } |
| 152 | |
| 153 | // Open alternative output file. |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 154 | FILE* out_file = stdout; |
| 155 | if (options.output_file_name_) { |
| 156 | out_file = fopen(options.output_file_name_, "w"); |
| 157 | if (!out_file) { |
| 158 | fprintf(stderr, "Can't open %s\n", options.output_file_name_); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 159 | return 1; |
| 160 | } |
| 161 | } |
| 162 | |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 163 | // Open profile file. |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 164 | ProfileCompilationInfo* profile_info = nullptr; |
| 165 | if (options.profile_file_name_) { |
| 166 | int profile_fd = open(options.profile_file_name_, O_RDONLY); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 167 | if (profile_fd < 0) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 168 | fprintf(stderr, "Can't open %s\n", options.profile_file_name_); |
David Sehr | cdcfde7 | 2016-09-26 07:44:04 -0700 | [diff] [blame] | 169 | return 1; |
| 170 | } |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 171 | profile_info = new ProfileCompilationInfo(); |
| 172 | if (!profile_info->Load(profile_fd)) { |
| 173 | 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] | 174 | return 1; |
| 175 | } |
| 176 | } |
| 177 | |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 178 | // Create DexLayout instance. |
| 179 | DexLayout dex_layout(options, profile_info, out_file); |
| 180 | |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 181 | // Process all files supplied on command line. |
| 182 | int result = 0; |
| 183 | while (optind < argc) { |
Jeff Hao | ea7c629 | 2016-11-14 18:10:16 -0800 | [diff] [blame] | 184 | result |= dex_layout.ProcessFile(argv[optind++]); |
David Sehr | 7629f60 | 2016-08-07 16:01:51 -0700 | [diff] [blame] | 185 | } // while |
| 186 | return result != 0; |
| 187 | } |
| 188 | |
| 189 | } // namespace art |
| 190 | |
| 191 | int main(int argc, char** argv) { |
| 192 | return art::DexlayoutDriver(argc, argv); |
| 193 | } |