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 | |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 26 | #include <inttypes.h> |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 27 | #include <stdio.h> |
Andreas Gampe | 8cf9cb3 | 2017-07-19 09:28:38 -0700 | [diff] [blame] | 28 | #include <stdlib.h> |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 29 | |
David Sehr | 999646d | 2018-02-16 10:22:33 -0800 | [diff] [blame^] | 30 | #include <android-base/file.h> |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 31 | #include <android-base/logging.h> |
| 32 | |
David Sehr | 0225f8e | 2018-01-31 08:52:24 +0000 | [diff] [blame] | 33 | #include "dex/code_item_accessors-inl.h" |
David Sehr | 9e734c7 | 2018-01-04 17:56:19 -0800 | [diff] [blame] | 34 | #include "dex/dex_file-inl.h" |
| 35 | #include "dex/dex_file_loader.h" |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 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; |
David Sehr | 999646d | 2018-02-16 10:22:33 -0800 | [diff] [blame^] | 176 | std::string content; |
| 177 | // TODO: add an api to android::base to read a std::vector<uint8_t>. |
| 178 | if (!android::base::ReadFileToString(fileName, &content)) { |
| 179 | LOG(ERROR) << "ReadFileToString failed"; |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 180 | return -1; |
| 181 | } |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 182 | std::vector<std::unique_ptr<const DexFile>> dex_files; |
David Sehr | 999646d | 2018-02-16 10:22:33 -0800 | [diff] [blame^] | 183 | std::string error_msg; |
David Sehr | 5a1f629 | 2018-01-19 11:08:51 -0800 | [diff] [blame] | 184 | const DexFileLoader dex_file_loader; |
David Sehr | 999646d | 2018-02-16 10:22:33 -0800 | [diff] [blame^] | 185 | if (!dex_file_loader.OpenAll(reinterpret_cast<const uint8_t*>(content.data()), |
| 186 | content.size(), |
| 187 | fileName, |
| 188 | /*verify*/ true, |
| 189 | kVerifyChecksum, |
| 190 | &error_msg, |
| 191 | &dex_files)) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 192 | LOG(ERROR) << error_msg; |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 193 | return -1; |
| 194 | } |
| 195 | |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 196 | // Success. Iterate over all dex files found in given file. |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 197 | fprintf(gOutFile, "#%s\n", fileName); |
Aart Bik | 4e14960 | 2015-07-09 11:45:28 -0700 | [diff] [blame] | 198 | for (size_t i = 0; i < dex_files.size(); i++) { |
| 199 | // Iterate over all classes in one dex file. |
| 200 | const DexFile* pDexFile = dex_files[i].get(); |
| 201 | const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_; |
| 202 | for (u4 idx = 0; idx < classDefsSize; idx++) { |
| 203 | dumpClass(pDexFile, idx); |
| 204 | } |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Shows usage. |
| 211 | */ |
| 212 | static void usage(void) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 213 | LOG(ERROR) << "Copyright (C) 2007 The Android Open Source Project\n"; |
| 214 | LOG(ERROR) << gProgName << ": [-m p.c.m] [-o outfile] dexfile..."; |
| 215 | LOG(ERROR) << ""; |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | /* |
| 219 | * Main driver of the dexlist utility. |
| 220 | */ |
| 221 | int dexlistDriver(int argc, char** argv) { |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 222 | // Reset options. |
| 223 | bool wantUsage = false; |
| 224 | memset(&gOptions, 0, sizeof(gOptions)); |
| 225 | |
| 226 | // Parse all arguments. |
| 227 | while (1) { |
| 228 | const int ic = getopt(argc, argv, "o:m:"); |
| 229 | if (ic < 0) { |
| 230 | break; // done |
| 231 | } |
| 232 | switch (ic) { |
| 233 | case 'o': // output file |
| 234 | gOptions.outputFileName = optarg; |
| 235 | break; |
| 236 | case 'm': |
Aart Bik | b1b45be | 2015-08-28 11:09:29 -0700 | [diff] [blame] | 237 | // 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] | 238 | // fully-qualified method name. This isn't really what |
| 239 | // dexlist is for, but it's easy to do it here. |
| 240 | { |
| 241 | gOptions.argCopy = strdup(optarg); |
| 242 | char* meth = strrchr(gOptions.argCopy, '.'); |
| 243 | if (meth == nullptr) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 244 | LOG(ERROR) << "Expected: package.Class.method"; |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 245 | wantUsage = true; |
| 246 | } else { |
| 247 | *meth = '\0'; |
| 248 | gOptions.classToFind = gOptions.argCopy; |
| 249 | gOptions.methodToFind = meth + 1; |
| 250 | } |
| 251 | } |
| 252 | break; |
| 253 | default: |
| 254 | wantUsage = true; |
| 255 | break; |
| 256 | } // switch |
| 257 | } // while |
| 258 | |
| 259 | // Detect early problems. |
| 260 | if (optind == argc) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 261 | LOG(ERROR) << "No file specified"; |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 262 | wantUsage = true; |
| 263 | } |
| 264 | if (wantUsage) { |
| 265 | usage(); |
| 266 | free(gOptions.argCopy); |
| 267 | return 2; |
| 268 | } |
| 269 | |
| 270 | // Open alternative output file. |
| 271 | if (gOptions.outputFileName) { |
| 272 | gOutFile = fopen(gOptions.outputFileName, "w"); |
| 273 | if (!gOutFile) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 274 | PLOG(ERROR) << "Can't open " << gOptions.outputFileName; |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 275 | free(gOptions.argCopy); |
| 276 | return 1; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Process all files supplied on command line. If one of them fails we |
| 281 | // continue on, only returning a failure at the end. |
| 282 | int result = 0; |
| 283 | while (optind < argc) { |
| 284 | result |= processFile(argv[optind++]); |
| 285 | } // while |
| 286 | |
| 287 | free(gOptions.argCopy); |
| 288 | return result != 0; |
| 289 | } |
| 290 | |
| 291 | } // namespace art |
| 292 | |
| 293 | int main(int argc, char** argv) { |
Andreas Gampe | 221d981 | 2018-01-22 17:48:56 -0800 | [diff] [blame] | 294 | // Output all logging to stderr. |
| 295 | android::base::SetLogger(android::base::StderrLogger); |
| 296 | |
Aart Bik | 3e40f4a | 2015-07-07 17:09:41 -0700 | [diff] [blame] | 297 | return art::dexlistDriver(argc, argv); |
| 298 | } |
| 299 | |