| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | /* |
| 17 | * List all methods in all concrete classes in a DEX file. |
| 18 | */ |
| 19 | #include "libdex/DexFile.h" |
| 20 | #include "libdex/DexClass.h" |
| 21 | #include "libdex/DexProto.h" |
| 22 | #include "libdex/SysUtil.h" |
| 23 | #include "libdex/CmdUtils.h" |
| 24 | |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <fcntl.h> |
| 28 | #include <stddef.h> |
| 29 | #include <string.h> |
| 30 | #include <unistd.h> |
| 31 | #include <getopt.h> |
| 32 | #include <errno.h> |
| 33 | #include <assert.h> |
| 34 | |
| 35 | static const char* gProgName = "dexlist"; |
| 36 | |
| 37 | /* |
| 38 | * Return a newly-allocated string for the "dot version" of the class |
| 39 | * name for the given type descriptor. That is, The initial "L" and |
| 40 | * final ";" (if any) have been removed and all occurrences of '/' |
| 41 | * have been changed to '.'. |
| 42 | */ |
| 43 | static char* descriptorToDot(const char* str) |
| 44 | { |
| 45 | size_t at = strlen(str); |
| 46 | char* newStr; |
| 47 | |
| 48 | if (str[0] == 'L') { |
| 49 | assert(str[at - 1] == ';'); |
| 50 | at -= 2; /* Two fewer chars to copy. */ |
| 51 | str++; /* Skip the 'L'. */ |
| 52 | } |
| 53 | |
| 54 | newStr = malloc(at + 1); /* Add one for the '\0'. */ |
| 55 | newStr[at] = '\0'; |
| 56 | |
| 57 | while (at > 0) { |
| 58 | at--; |
| 59 | newStr[at] = (str[at] == '/') ? '.' : str[at]; |
| 60 | } |
| 61 | |
| 62 | return newStr; |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * Position table callback; we just want to catch the number of the |
| 67 | * first line in the method, which *should* correspond to the first |
| 68 | * entry from the table. (Could also use "min" here.) |
| 69 | */ |
| 70 | static int positionsCallback(void* cnxt, u4 address, u4 lineNum) |
| 71 | { |
| 72 | int* pFirstLine = (int*) cnxt; |
| 73 | if (*pFirstLine == -1) |
| 74 | *pFirstLine = lineNum; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | * Dump a method. |
| 81 | */ |
| 82 | void dumpMethod(DexFile* pDexFile, const char* fileName, |
| 83 | const DexMethod* pDexMethod, int i) |
| 84 | { |
| 85 | const DexMethodId* pMethodId; |
| 86 | const DexCode* pCode; |
| 87 | const char* classDescriptor; |
| 88 | const char* methodName; |
| 89 | int firstLine; |
| 90 | |
| 91 | /* abstract and native methods don't get listed */ |
| 92 | if (pDexMethod->codeOff == 0) |
| 93 | return; |
| 94 | |
| 95 | pMethodId = dexGetMethodId(pDexFile, pDexMethod->methodIdx); |
| 96 | methodName = dexStringById(pDexFile, pMethodId->nameIdx); |
| 97 | |
| 98 | classDescriptor = dexStringByTypeIdx(pDexFile, pMethodId->classIdx); |
| 99 | |
| 100 | pCode = dexGetCode(pDexFile, pDexMethod); |
| 101 | assert(pCode != NULL); |
| 102 | |
| 103 | /* |
| 104 | * If the filename is empty, then set it to something printable |
| 105 | * so that it is easier to parse. |
| 106 | * |
| 107 | * TODO: A method may override its class's default source file by |
| 108 | * specifying a different one in its debug info. This possibility |
| 109 | * should be handled here. |
| 110 | */ |
| 111 | if (fileName == NULL || fileName[0] == 0) { |
| 112 | fileName = "(none)"; |
| 113 | } |
| 114 | |
| 115 | firstLine = -1; |
| 116 | dexDecodeDebugInfo(pDexFile, pCode, classDescriptor, pMethodId->protoIdx, |
| 117 | pDexMethod->accessFlags, positionsCallback, NULL, &firstLine); |
| 118 | |
| 119 | char* className = descriptorToDot(classDescriptor); |
| 120 | char* desc = dexCopyDescriptorFromMethodId(pDexFile, pMethodId); |
| 121 | u4 insnsOff = pDexMethod->codeOff + offsetof(DexCode, insns); |
| 122 | |
| 123 | printf("0x%08x %d %s %s %s %s %d\n", |
| 124 | insnsOff, pCode->insnsSize * 2, |
| 125 | className, methodName, desc, |
| 126 | fileName, firstLine); |
| 127 | |
| 128 | free(desc); |
| 129 | free(className); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Run through all direct and virtual methods in the class. |
| 134 | */ |
| 135 | void dumpClass(DexFile* pDexFile, int idx) |
| 136 | { |
| 137 | const DexClassDef* pClassDef; |
| 138 | DexClassData* pClassData; |
| 139 | const u1* pEncodedData; |
| 140 | const char* fileName; |
| 141 | int i; |
| 142 | |
| 143 | pClassDef = dexGetClassDef(pDexFile, idx); |
| 144 | pEncodedData = dexGetClassData(pDexFile, pClassDef); |
| 145 | pClassData = dexReadAndVerifyClassData(&pEncodedData, NULL); |
| 146 | |
| 147 | if (pClassData == NULL) { |
| 148 | fprintf(stderr, "Trouble reading class data\n"); |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | if (pClassDef->sourceFileIdx == 0xffffffff) { |
| 153 | fileName = NULL; |
| 154 | } else { |
| 155 | fileName = dexStringById(pDexFile, pClassDef->sourceFileIdx); |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * TODO: Each class def points at a sourceFile, so maybe that |
| 160 | * should be printed out. However, this needs to be coordinated |
| 161 | * with the tools that parse this output. |
| 162 | */ |
| 163 | |
| 164 | for (i = 0; i < (int) pClassData->header.directMethodsSize; i++) { |
| 165 | dumpMethod(pDexFile, fileName, &pClassData->directMethods[i], i); |
| 166 | } |
| 167 | |
| 168 | for (i = 0; i < (int) pClassData->header.virtualMethodsSize; i++) { |
| 169 | dumpMethod(pDexFile, fileName, &pClassData->virtualMethods[i], i); |
| 170 | } |
| 171 | |
| 172 | free(pClassData); |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * Process a file. |
| 177 | * |
| 178 | * Returns 0 on success. |
| 179 | */ |
| 180 | int process(const char* fileName) |
| 181 | { |
| 182 | DexFile* pDexFile = NULL; |
| 183 | MemMapping map; |
| 184 | bool mapped = false; |
| 185 | int result = -1; |
| 186 | UnzipToFileResult utfr; |
| 187 | |
| 188 | utfr = dexOpenAndMap(fileName, NULL, &map, true); |
| 189 | if (utfr != kUTFRSuccess) { |
| 190 | if (utfr == kUTFRNoClassesDex) { |
| 191 | /* no classes.dex in the APK; pretend we succeeded */ |
| 192 | result = 0; |
| 193 | goto bail; |
| 194 | } |
| 195 | fprintf(stderr, "Unable to process '%s'\n", fileName); |
| 196 | goto bail; |
| 197 | } |
| 198 | mapped = true; |
| 199 | |
| The Android Open Source Project | 89c1feb | 2008-12-17 18:03:55 -0800 | [diff] [blame] | 200 | pDexFile = dexFileParse(map.addr, map.length, kDexParseDefault); |
| The Android Open Source Project | 2ad60cf | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 201 | if (pDexFile == NULL) { |
| 202 | fprintf(stderr, "Warning: DEX parse failed for '%s'\n", fileName); |
| 203 | goto bail; |
| 204 | } |
| 205 | |
| 206 | printf("#%s\n", fileName); |
| 207 | |
| 208 | int i; |
| 209 | for (i = 0; i < (int) pDexFile->pHeader->classDefsSize; i++) { |
| 210 | dumpClass(pDexFile, i); |
| 211 | } |
| 212 | |
| 213 | result = 0; |
| 214 | |
| 215 | bail: |
| 216 | if (mapped) |
| 217 | sysReleaseShmem(&map); |
| 218 | if (pDexFile != NULL) |
| 219 | dexFileFree(pDexFile); |
| 220 | return result; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* |
| 225 | * Show usage. |
| 226 | */ |
| 227 | void usage(void) |
| 228 | { |
| 229 | fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n"); |
| 230 | fprintf(stderr, "%s: dexfile [dexfile2 ...]\n", gProgName); |
| 231 | fprintf(stderr, "\n"); |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Parse args. |
| 236 | */ |
| 237 | int main(int argc, char* const argv[]) |
| 238 | { |
| 239 | int result = 0; |
| 240 | int i; |
| 241 | |
| 242 | if (argc < 2) { |
| 243 | fprintf(stderr, "%s: no file specified\n", gProgName); |
| 244 | usage(); |
| 245 | return 2; |
| 246 | } |
| 247 | |
| 248 | /* |
| 249 | * Run through the list of files. If one of them fails we contine on, |
| 250 | * only returning a failure at the end. |
| 251 | */ |
| 252 | for (i = 1; i < argc; i++) |
| 253 | result |= process(argv[i]); |
| 254 | |
| 255 | return result; |
| 256 | } |
| 257 | |