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