Home | History | Annotate | Download | only in dexdump
      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 very similar to
     21  * to 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 "base/logging.h"
     32 #include "runtime.h"
     33 #include "mem_map.h"
     34 
     35 namespace art {
     36 
     37 static const char* gProgName = "dexdump";
     38 
     39 /*
     40  * Shows usage.
     41  */
     42 static void usage(void) {
     43   fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
     44   fprintf(stderr, "%s: [-a] [-c] [-d] [-e] [-f] [-h] [-i] [-l layout] [-o outfile]"
     45                   " dexfile...\n\n", gProgName);
     46   fprintf(stderr, " -a : display annotations\n");
     47   fprintf(stderr, " -c : verify checksum and exit\n");
     48   fprintf(stderr, " -d : disassemble code sections\n");
     49   fprintf(stderr, " -e : display exported items only\n");
     50   fprintf(stderr, " -f : display summary information from file header\n");
     51   fprintf(stderr, " -g : display CFG for dex\n");
     52   fprintf(stderr, " -h : display file header details\n");
     53   fprintf(stderr, " -i : ignore checksum failures\n");
     54   fprintf(stderr, " -l : output layout, either 'plain' or 'xml'\n");
     55   fprintf(stderr, " -o : output file name (defaults to stdout)\n");
     56 }
     57 
     58 /*
     59  * Main driver of the dexdump utility.
     60  */
     61 int dexdumpDriver(int argc, char** argv) {
     62   // Art specific set up.
     63   InitLogging(argv, Runtime::Aborter);
     64   MemMap::Init();
     65 
     66   // Reset options.
     67   bool wantUsage = false;
     68   memset(&gOptions, 0, sizeof(gOptions));
     69   gOptions.verbose = true;
     70 
     71   // Parse all arguments.
     72   while (1) {
     73     const int ic = getopt(argc, argv, "acdefghil:o:");
     74     if (ic < 0) {
     75       break;  // done
     76     }
     77     switch (ic) {
     78       case 'a':  // display annotations
     79         gOptions.showAnnotations = true;
     80         break;
     81       case 'c':  // verify the checksum then exit
     82         gOptions.checksumOnly = true;
     83         break;
     84       case 'd':  // disassemble Dalvik instructions
     85         gOptions.disassemble = true;
     86         break;
     87       case 'e':  // exported items only
     88         gOptions.exportsOnly = true;
     89         break;
     90       case 'f':  // display outer file header
     91         gOptions.showFileHeaders = true;
     92         break;
     93       case 'g':  // display cfg
     94         gOptions.showCfg = true;
     95         break;
     96       case 'h':  // display section headers, i.e. all meta-data
     97         gOptions.showSectionHeaders = true;
     98         break;
     99       case 'i':  // continue even if checksum is bad
    100         gOptions.ignoreBadChecksum = true;
    101         break;
    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;
    108         } else {
    109           wantUsage = true;
    110         }
    111         break;
    112       case 'o':  // output file
    113         gOptions.outputFileName = optarg;
    114         break;
    115       default:
    116         wantUsage = true;
    117         break;
    118     }  // switch
    119   }  // while
    120 
    121   // Detect early problems.
    122   if (optind == argc) {
    123     fprintf(stderr, "%s: no file specified\n", gProgName);
    124     wantUsage = true;
    125   }
    126   if (gOptions.checksumOnly && gOptions.ignoreBadChecksum) {
    127     fprintf(stderr, "Can't specify both -c and -i\n");
    128     wantUsage = true;
    129   }
    130   if (wantUsage) {
    131     usage();
    132     return 2;
    133   }
    134 
    135   // Open alternative output file.
    136   if (gOptions.outputFileName) {
    137     gOutFile = fopen(gOptions.outputFileName, "w");
    138     if (!gOutFile) {
    139       fprintf(stderr, "Can't open %s\n", gOptions.outputFileName);
    140       return 1;
    141     }
    142   }
    143 
    144   // Process all files supplied on command line.
    145   int result = 0;
    146   while (optind < argc) {
    147     result |= processFile(argv[optind++]);
    148   }  // while
    149   return result != 0;
    150 }
    151 
    152 }  // namespace art
    153 
    154 int main(int argc, char** argv) {
    155   return art::dexdumpDriver(argc, argv);
    156 }
    157