Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 dexdump utility. |
| 17 | * |
| 18 | * This is a re-implementation of the original dexdump utility that was |
| 19 | * based on Dalvik functions in libdex into a new dexdump that is now |
Aart Bik | 37d6a3b | 2016-06-21 18:30:10 -0700 | [diff] [blame] | 20 | * based on Art functions in libart instead. The output is very similar to |
| 21 | * to the original for correct DEX files. Error messages may differ, however. |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 22 | * Also, ODEX files are no longer supported. |
| 23 | */ |
| 24 | |
| 25 | #include "dexdump.h" |
| 26 | |
| 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | #include <unistd.h> |
| 30 | |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 31 | #include <android-base/logging.h> |
| 32 | |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 33 | namespace art { |
| 34 | |
| 35 | static const char* gProgName = "dexdump"; |
| 36 | |
| 37 | /* |
| 38 | * Shows usage. |
| 39 | */ |
Andreas Gampe | 70dfb69 | 2018-09-18 16:50:18 -0700 | [diff] [blame] | 40 | static void usage() { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 41 | LOG(ERROR) << "Copyright (C) 2007 The Android Open Source Project\n"; |
Nicolas Geoffray | c1d8caa | 2018-02-27 10:15:14 +0000 | [diff] [blame] | 42 | LOG(ERROR) << gProgName << ": [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-j] [-l layout] [-o outfile]" |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 43 | " dexfile...\n"; |
| 44 | LOG(ERROR) << " -a : display annotations"; |
| 45 | LOG(ERROR) << " -c : verify checksum and exit"; |
| 46 | LOG(ERROR) << " -d : disassemble code sections"; |
| 47 | LOG(ERROR) << " -e : display exported items only"; |
| 48 | LOG(ERROR) << " -f : display summary information from file header"; |
| 49 | LOG(ERROR) << " -g : display CFG for dex"; |
| 50 | LOG(ERROR) << " -h : display file header details"; |
| 51 | LOG(ERROR) << " -i : ignore checksum failures"; |
Nicolas Geoffray | c1d8caa | 2018-02-27 10:15:14 +0000 | [diff] [blame] | 52 | LOG(ERROR) << " -j : disable dex file verification"; |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 53 | LOG(ERROR) << " -l : output layout, either 'plain' or 'xml'"; |
| 54 | LOG(ERROR) << " -o : output file name (defaults to stdout)"; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /* |
| 58 | * Main driver of the dexdump utility. |
| 59 | */ |
| 60 | int dexdumpDriver(int argc, char** argv) { |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 61 | // Reset options. |
| 62 | bool wantUsage = false; |
| 63 | memset(&gOptions, 0, sizeof(gOptions)); |
| 64 | gOptions.verbose = true; |
| 65 | |
| 66 | // Parse all arguments. |
Andreas Gampe | 70dfb69 | 2018-09-18 16:50:18 -0700 | [diff] [blame] | 67 | while (true) { |
Nicolas Geoffray | c1d8caa | 2018-02-27 10:15:14 +0000 | [diff] [blame] | 68 | const int ic = getopt(argc, argv, "acdefghijl:o:"); |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 69 | if (ic < 0) { |
| 70 | break; // done |
| 71 | } |
| 72 | switch (ic) { |
Aart Bik | dce5086 | 2016-06-10 16:04:03 -0700 | [diff] [blame] | 73 | case 'a': // display annotations |
| 74 | gOptions.showAnnotations = true; |
| 75 | break; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 76 | case 'c': // verify the checksum then exit |
| 77 | gOptions.checksumOnly = true; |
| 78 | break; |
| 79 | case 'd': // disassemble Dalvik instructions |
| 80 | gOptions.disassemble = true; |
| 81 | break; |
Aart Bik | 3f382ae | 2015-11-13 10:06:01 -0800 | [diff] [blame] | 82 | case 'e': // exported items only |
| 83 | gOptions.exportsOnly = true; |
| 84 | break; |
Aart Bik | dce5086 | 2016-06-10 16:04:03 -0700 | [diff] [blame] | 85 | case 'f': // display outer file header |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 86 | gOptions.showFileHeaders = true; |
| 87 | break; |
Aart Bik | dce5086 | 2016-06-10 16:04:03 -0700 | [diff] [blame] | 88 | case 'g': // display cfg |
| 89 | gOptions.showCfg = true; |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 90 | break; |
Aart Bik | dce5086 | 2016-06-10 16:04:03 -0700 | [diff] [blame] | 91 | case 'h': // display section headers, i.e. all meta-data |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 92 | gOptions.showSectionHeaders = true; |
| 93 | break; |
| 94 | case 'i': // continue even if checksum is bad |
| 95 | gOptions.ignoreBadChecksum = true; |
| 96 | break; |
Nicolas Geoffray | c1d8caa | 2018-02-27 10:15:14 +0000 | [diff] [blame] | 97 | case 'j': // disable dex file verification |
| 98 | gOptions.disableVerifier = true; |
| 99 | break; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 100 | case 'l': // layout |
| 101 | if (strcmp(optarg, "plain") == 0) { |
| 102 | gOptions.outputFormat = OUTPUT_PLAIN; |
| 103 | } else if (strcmp(optarg, "xml") == 0) { |
| 104 | gOptions.outputFormat = OUTPUT_XML; |
| 105 | gOptions.verbose = false; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 106 | } else { |
| 107 | wantUsage = true; |
| 108 | } |
| 109 | break; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 110 | case 'o': // output file |
| 111 | gOptions.outputFileName = optarg; |
| 112 | break; |
| 113 | default: |
| 114 | wantUsage = true; |
| 115 | break; |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 116 | } // switch |
| 117 | } // while |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 118 | |
| 119 | // Detect early problems. |
| 120 | if (optind == argc) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 121 | LOG(ERROR) << "No file specified"; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 122 | wantUsage = true; |
| 123 | } |
| 124 | if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 125 | LOG(ERROR) << "Can't specify both -c and -i"; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 126 | wantUsage = true; |
| 127 | } |
| 128 | if (wantUsage) { |
| 129 | usage(); |
| 130 | return 2; |
| 131 | } |
| 132 | |
| 133 | // Open alternative output file. |
| 134 | if (gOptions.outputFileName) { |
| 135 | gOutFile = fopen(gOptions.outputFileName, "w"); |
| 136 | if (!gOutFile) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 137 | PLOG(ERROR) << "Can't open " << gOptions.outputFileName; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 138 | return 1; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // Process all files supplied on command line. |
| 143 | int result = 0; |
| 144 | while (optind < argc) { |
| 145 | result |= processFile(argv[optind++]); |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 146 | } // while |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 147 | return result != 0; |
| 148 | } |
| 149 | |
| 150 | } // namespace art |
| 151 | |
| 152 | int main(int argc, char** argv) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 153 | // Output all logging to stderr. |
| 154 | android::base::SetLogger(android::base::StderrLogger); |
| 155 | |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 156 | return art::dexdumpDriver(argc, argv); |
| 157 | } |