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"; |
Ian Zerny | 19df0e9 | 2022-02-02 16:51:02 +0100 | [diff] [blame] | 42 | LOG(ERROR) << gProgName << ": [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-j] [-l layout] [-n]" |
| 43 | " [-o outfile] dexfile...\n"; |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 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'"; |
Ian Zerny | 19df0e9 | 2022-02-02 16:51:02 +0100 | [diff] [blame] | 54 | LOG(ERROR) << " -n : don't display debug information"; |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 55 | LOG(ERROR) << " -o : output file name (defaults to stdout)"; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Main driver of the dexdump utility. |
| 60 | */ |
| 61 | int dexdumpDriver(int argc, char** argv) { |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 62 | // Reset options. |
| 63 | bool wantUsage = false; |
| 64 | memset(&gOptions, 0, sizeof(gOptions)); |
| 65 | gOptions.verbose = true; |
Ian Zerny | 19df0e9 | 2022-02-02 16:51:02 +0100 | [diff] [blame] | 66 | gOptions.showDebugInfo = true; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 67 | |
| 68 | // Parse all arguments. |
Andreas Gampe | 70dfb69 | 2018-09-18 16:50:18 -0700 | [diff] [blame] | 69 | while (true) { |
Ian Zerny | 19df0e9 | 2022-02-02 16:51:02 +0100 | [diff] [blame] | 70 | const int ic = getopt(argc, argv, "acdefghijl:no:"); |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 71 | if (ic < 0) { |
| 72 | break; // done |
| 73 | } |
| 74 | switch (ic) { |
Aart Bik | dce5086 | 2016-06-10 16:04:03 -0700 | [diff] [blame] | 75 | case 'a': // display annotations |
| 76 | gOptions.showAnnotations = true; |
| 77 | break; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 78 | case 'c': // verify the checksum then exit |
| 79 | gOptions.checksumOnly = true; |
| 80 | break; |
| 81 | case 'd': // disassemble Dalvik instructions |
| 82 | gOptions.disassemble = true; |
| 83 | break; |
Aart Bik | 3f382ae | 2015-11-13 10:06:01 -0800 | [diff] [blame] | 84 | case 'e': // exported items only |
| 85 | gOptions.exportsOnly = true; |
| 86 | break; |
Aart Bik | dce5086 | 2016-06-10 16:04:03 -0700 | [diff] [blame] | 87 | case 'f': // display outer file header |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 88 | gOptions.showFileHeaders = true; |
| 89 | break; |
Aart Bik | dce5086 | 2016-06-10 16:04:03 -0700 | [diff] [blame] | 90 | case 'g': // display cfg |
| 91 | gOptions.showCfg = true; |
Andreas Gampe | 5073fed | 2015-08-10 11:40:25 -0700 | [diff] [blame] | 92 | break; |
Aart Bik | dce5086 | 2016-06-10 16:04:03 -0700 | [diff] [blame] | 93 | case 'h': // display section headers, i.e. all meta-data |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 94 | gOptions.showSectionHeaders = true; |
| 95 | break; |
| 96 | case 'i': // continue even if checksum is bad |
| 97 | gOptions.ignoreBadChecksum = true; |
| 98 | break; |
Nicolas Geoffray | c1d8caa | 2018-02-27 10:15:14 +0000 | [diff] [blame] | 99 | case 'j': // disable dex file verification |
| 100 | gOptions.disableVerifier = true; |
| 101 | break; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 102 | case 'l': // layout |
| 103 | if (strcmp(optarg, "plain") == 0) { |
| 104 | gOptions.outputFormat = OUTPUT_PLAIN; |
| 105 | } else if (strcmp(optarg, "xml") == 0) { |
| 106 | gOptions.outputFormat = OUTPUT_XML; |
| 107 | gOptions.verbose = false; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 108 | } else { |
| 109 | wantUsage = true; |
| 110 | } |
| 111 | break; |
Ian Zerny | 19df0e9 | 2022-02-02 16:51:02 +0100 | [diff] [blame] | 112 | case 'n': // don't display debug information |
| 113 | gOptions.showDebugInfo = false; |
| 114 | break; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 115 | case 'o': // output file |
| 116 | gOptions.outputFileName = optarg; |
| 117 | break; |
| 118 | default: |
| 119 | wantUsage = true; |
| 120 | break; |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 121 | } // switch |
| 122 | } // while |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 123 | |
| 124 | // Detect early problems. |
| 125 | if (optind == argc) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 126 | LOG(ERROR) << "No file specified"; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 127 | wantUsage = true; |
| 128 | } |
| 129 | if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 130 | LOG(ERROR) << "Can't specify both -c and -i"; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 131 | wantUsage = true; |
| 132 | } |
| 133 | if (wantUsage) { |
| 134 | usage(); |
| 135 | return 2; |
| 136 | } |
| 137 | |
| 138 | // Open alternative output file. |
| 139 | if (gOptions.outputFileName) { |
| 140 | gOutFile = fopen(gOptions.outputFileName, "w"); |
| 141 | if (!gOutFile) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 142 | PLOG(ERROR) << "Can't open " << gOptions.outputFileName; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 143 | return 1; |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | // Process all files supplied on command line. |
| 148 | int result = 0; |
| 149 | while (optind < argc) { |
| 150 | result |= processFile(argv[optind++]); |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 151 | } // while |
Andreas Gampe | 7c5acbb | 2018-09-20 13:54:52 -0700 | [diff] [blame] | 152 | return result != 0 ? 1 : 0; |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | } // namespace art |
| 156 | |
| 157 | int main(int argc, char** argv) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 158 | // Output all logging to stderr. |
| 159 | android::base::SetLogger(android::base::StderrLogger); |
| 160 | |
Aart Bik | 69ae54a | 2015-07-01 14:52:26 -0700 | [diff] [blame] | 161 | return art::dexdumpDriver(argc, argv); |
| 162 | } |