Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -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 | * Implementation file of the dexlist utility. |
| 17 | * |
| 18 | * This is a re-implementation of the original dexlist utility that was |
| 19 | * based on Dalvik functions in libdex into a new dexlist 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 | * |
| 23 | * List all methods in all concrete classes in one or more DEX files. |
| 24 | */ |
| 25 | |
| 26 | #include <stdlib.h> |
| 27 | #include <stdio.h> |
| 28 | |
| 29 | #include "dex_file-inl.h" |
| 30 | #include "mem_map.h" |
| 31 | #include "runtime.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
| 35 | static const char* gProgName = "dexlist"; |
| 36 | |
| 37 | /* Command-line options. */ |
| 38 | static struct { |
| 39 | char* argCopy; |
| 40 | const char* classToFind; |
| 41 | const char* methodToFind; |
| 42 | const char* outputFileName; |
| 43 | } gOptions; |
| 44 | |
| 45 | /* |
| 46 | * Output file. Defaults to stdout. |
| 47 | */ |
| 48 | static FILE* gOutFile = stdout; |
| 49 | |
| 50 | /* |
| 51 | * Data types that match the definitions in the VM specification. |
| 52 | */ |
| 53 | typedef uint8_t u1; |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 54 | typedef uint32_t u4; |
| 55 | typedef uint64_t u8; |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * Returns a newly-allocated string for the "dot version" of the class |
| 59 | * name for the given type descriptor. That is, The initial "L" and |
| 60 | * final ";" (if any) have been removed and all occurrences of '/' |
| 61 | * have been changed to '.'. |
| 62 | */ |
| 63 | static char* descriptorToDot(const char* str) { |
| 64 | size_t at = strlen(str); |
| 65 | if (str[0] == 'L') { |
| 66 | at -= 2; // Two fewer chars to copy. |
| 67 | str++; |
| 68 | } |
| 69 | char* newStr = reinterpret_cast<char*>(malloc(at + 1)); |
| 70 | newStr[at] = '\0'; |
| 71 | while (at > 0) { |
| 72 | at--; |
| 73 | newStr[at] = (str[at] == '/') ? '.' : str[at]; |
| 74 | } |
| 75 | return newStr; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Positions table callback; we just want to catch the number of the |
| 80 | * first line in the method, which *should* correspond to the first |
| 81 | * entry from the table. (Could also use "min" here.) |
| 82 | */ |
| 83 | static bool positionsCb(void* context, u4 /*address*/, u4 lineNum) { |
| 84 | int* pFirstLine = reinterpret_cast<int *>(context); |
| 85 | if (*pFirstLine == -1) { |
| 86 | *pFirstLine = lineNum; |
| 87 | } |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /* |
| 92 | * Dumps a method. |
| 93 | */ |
| 94 | static void dumpMethod(const DexFile* pDexFile, |
| 95 | const char* fileName, u4 idx, u4 flags, |
| 96 | const DexFile::CodeItem* pCode, u4 codeOffset) { |
| 97 | // Abstract and native methods don't get listed. |
| 98 | if (pCode == nullptr || codeOffset == 0) { |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | // Method information. |
| 103 | const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx); |
| 104 | const char* methodName = pDexFile->StringDataByIdx(pMethodId.name_idx_); |
| 105 | const char* classDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_); |
| 106 | char* className = descriptorToDot(classDescriptor); |
| 107 | const u4 insnsOff = codeOffset + 0x10; |
| 108 | |
| 109 | // Don't list methods that do not match a particular query. |
| 110 | if (gOptions.methodToFind != nullptr && |
| 111 | (strcmp(gOptions.classToFind, className) != 0 || |
| 112 | strcmp(gOptions.methodToFind, methodName) != 0)) { |
| 113 | free(className); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | // If the filename is empty, then set it to something printable. |
| 118 | if (fileName == nullptr || fileName[0] == 0) { |
| 119 | fileName = "(none)"; |
| 120 | } |
| 121 | |
| 122 | // Find the first line. |
| 123 | int firstLine = -1; |
| 124 | bool is_static = (flags & kAccStatic) != 0; |
| 125 | pDexFile->DecodeDebugInfo( |
| 126 | pCode, is_static, idx, positionsCb, nullptr, &firstLine); |
| 127 | |
| 128 | // Method signature. |
| 129 | const Signature signature = pDexFile->GetMethodSignature(pMethodId); |
| 130 | char* typeDesc = strdup(signature.ToString().c_str()); |
| 131 | |
| 132 | // Dump actual method information. |
| 133 | fprintf(gOutFile, "0x%08x %d %s %s %s %s %d\n", |
| 134 | insnsOff, pCode->insns_size_in_code_units_ * 2, |
| 135 | className, methodName, typeDesc, fileName, firstLine); |
| 136 | |
| 137 | free(typeDesc); |
| 138 | free(className); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Runs through all direct and virtual methods in the class. |
| 143 | */ |
| 144 | void dumpClass(const DexFile* pDexFile, u4 idx) { |
| 145 | const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx); |
| 146 | |
| 147 | const char* fileName; |
| 148 | if (pClassDef.source_file_idx_ == DexFile::kDexNoIndex) { |
| 149 | fileName = nullptr; |
| 150 | } else { |
| 151 | fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_); |
| 152 | } |
| 153 | |
| 154 | const u1* pEncodedData = pDexFile->GetClassData(pClassDef); |
| 155 | if (pEncodedData != nullptr) { |
| 156 | ClassDataItemIterator pClassData(*pDexFile, pEncodedData); |
| 157 | // Skip the fields. |
| 158 | for (; pClassData.HasNextStaticField(); pClassData.Next()) {} |
| 159 | for (; pClassData.HasNextInstanceField(); pClassData.Next()) {} |
| 160 | // Direct methods. |
| 161 | for (; pClassData.HasNextDirectMethod(); pClassData.Next()) { |
| 162 | dumpMethod(pDexFile, fileName, |
| 163 | pClassData.GetMemberIndex(), |
| 164 | pClassData.GetRawMemberAccessFlags(), |
| 165 | pClassData.GetMethodCodeItem(), |
| 166 | pClassData.GetMethodCodeItemOffset()); |
| 167 | } |
| 168 | // Virtual methods. |
| 169 | for (; pClassData.HasNextVirtualMethod(); pClassData.Next()) { |
| 170 | dumpMethod(pDexFile, fileName, |
| 171 | pClassData.GetMemberIndex(), |
| 172 | pClassData.GetRawMemberAccessFlags(), |
| 173 | pClassData.GetMethodCodeItem(), |
| 174 | pClassData.GetMethodCodeItemOffset()); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /* |
| 180 | * Processes a single file (either direct .dex or indirect .zip/.jar/.apk). |
| 181 | */ |
| 182 | static int processFile(const char* fileName) { |
| 183 | // If the file is not a .dex file, the function tries .zip/.jar/.apk files, |
| 184 | // all of which are Zip archives with "classes.dex" inside. |
| 185 | std::string error_msg; |
| 186 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
| 187 | if (!DexFile::Open(fileName, fileName, &error_msg, &dex_files)) { |
| 188 | fputs(error_msg.c_str(), stderr); |
| 189 | fputc('\n', stderr); |
| 190 | return -1; |
| 191 | } |
| 192 | |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 193 | // Success. Iterate over all dex files found in given file. |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 194 | fprintf(gOutFile, "#%s\n", fileName); |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 195 | for (size_t i = 0; i < dex_files.size(); i++) { |
| 196 | // Iterate over all classes in one dex file. |
| 197 | const DexFile* pDexFile = dex_files[i].get(); |
| 198 | const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_; |
| 199 | for (u4 idx = 0; idx < classDefsSize; idx++) { |
| 200 | dumpClass(pDexFile, idx); |
| 201 | } |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 202 | } |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * Shows usage. |
| 208 | */ |
| 209 | static void usage(void) { |
| 210 | fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n"); |
| 211 | fprintf(stderr, "%s: [-m p.c.m] [-o outfile] dexfile...\n", gProgName); |
| 212 | fprintf(stderr, "\n"); |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Main driver of the dexlist utility. |
| 217 | */ |
| 218 | int dexlistDriver(int argc, char** argv) { |
| 219 | // Art specific set up. |
| 220 | InitLogging(argv); |
| 221 | MemMap::Init(); |
| 222 | |
| 223 | // Reset options. |
| 224 | bool wantUsage = false; |
| 225 | memset(&gOptions, 0, sizeof(gOptions)); |
| 226 | |
| 227 | // Parse all arguments. |
| 228 | while (1) { |
| 229 | const int ic = getopt(argc, argv, "o:m:"); |
| 230 | if (ic < 0) { |
| 231 | break; // done |
| 232 | } |
| 233 | switch (ic) { |
| 234 | case 'o': // output file |
| 235 | gOptions.outputFileName = optarg; |
| 236 | break; |
| 237 | case 'm': |
Aart Bik | b1b45be | 2015-08-28 11:09:29 -0700 | [diff] [blame] | 238 | // If -m p.c.m is given, then find all instances of the |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 239 | // fully-qualified method name. This isn't really what |
| 240 | // dexlist is for, but it's easy to do it here. |
| 241 | { |
| 242 | gOptions.argCopy = strdup(optarg); |
| 243 | char* meth = strrchr(gOptions.argCopy, '.'); |
| 244 | if (meth == nullptr) { |
| 245 | fprintf(stderr, "Expected: package.Class.method\n"); |
| 246 | wantUsage = true; |
| 247 | } else { |
| 248 | *meth = '\0'; |
| 249 | gOptions.classToFind = gOptions.argCopy; |
| 250 | gOptions.methodToFind = meth + 1; |
| 251 | } |
| 252 | } |
| 253 | break; |
| 254 | default: |
| 255 | wantUsage = true; |
| 256 | break; |
| 257 | } // switch |
| 258 | } // while |
| 259 | |
| 260 | // Detect early problems. |
| 261 | if (optind == argc) { |
| 262 | fprintf(stderr, "%s: no file specified\n", gProgName); |
| 263 | wantUsage = true; |
| 264 | } |
| 265 | if (wantUsage) { |
| 266 | usage(); |
| 267 | free(gOptions.argCopy); |
| 268 | return 2; |
| 269 | } |
| 270 | |
| 271 | // Open alternative output file. |
| 272 | if (gOptions.outputFileName) { |
| 273 | gOutFile = fopen(gOptions.outputFileName, "w"); |
| 274 | if (!gOutFile) { |
| 275 | fprintf(stderr, "Can't open %s\n", gOptions.outputFileName); |
| 276 | free(gOptions.argCopy); |
| 277 | return 1; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // Process all files supplied on command line. If one of them fails we |
| 282 | // continue on, only returning a failure at the end. |
| 283 | int result = 0; |
| 284 | while (optind < argc) { |
| 285 | result |= processFile(argv[optind++]); |
| 286 | } // while |
| 287 | |
| 288 | free(gOptions.argCopy); |
| 289 | return result != 0; |
| 290 | } |
| 291 | |
| 292 | } // namespace art |
| 293 | |
| 294 | int main(int argc, char** argv) { |
| 295 | return art::dexlistDriver(argc, argv); |
| 296 | } |
| 297 | |