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