blob: 6a50258570a8fcf5a7bed197a0391f2cd58ce45f [file] [log] [blame]
Aart Bik3e40f4a2015-07-07 17:09:41 -07001/*
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 Sehr5a1f6292018-01-19 11:08:51 -080026#include <inttypes.h>
Aart Bik3e40f4a2015-07-07 17:09:41 -070027#include <stdio.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070028#include <stdlib.h>
Aart Bik3e40f4a2015-07-07 17:09:41 -070029
David Sehr999646d2018-02-16 10:22:33 -080030#include <android-base/file.h>
Andreas Gampe221d9812018-01-22 17:48:56 -080031#include <android-base/logging.h>
32
David Sehr0225f8e2018-01-31 08:52:24 +000033#include "dex/code_item_accessors-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080034#include "dex/dex_file-inl.h"
35#include "dex/dex_file_loader.h"
Aart Bik3e40f4a2015-07-07 17:09:41 -070036
37namespace art {
38
39static const char* gProgName = "dexlist";
40
41/* Command-line options. */
42static 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 */
52static FILE* gOutFile = stdout;
53
54/*
55 * Data types that match the definitions in the VM specification.
56 */
57typedef uint8_t u1;
Aart Bik3e40f4a2015-07-07 17:09:41 -070058typedef uint32_t u4;
59typedef uint64_t u8;
Aart Bik3e40f4a2015-07-07 17:09:41 -070060
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 Bikc05e2f22016-07-12 15:53:13 -070067static std::unique_ptr<char[]> descriptorToDot(const char* str) {
68 size_t len = strlen(str);
Aart Bik3e40f4a2015-07-07 17:09:41 -070069 if (str[0] == 'L') {
Aart Bikc05e2f22016-07-12 15:53:13 -070070 len -= 2; // Two fewer chars to copy (trims L and ;).
71 str++; // Start past 'L'.
Aart Bik3e40f4a2015-07-07 17:09:41 -070072 }
Aart Bikc05e2f22016-07-12 15:53:13 -070073 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 Bik3e40f4a2015-07-07 17:09:41 -070076 }
Aart Bikc05e2f22016-07-12 15:53:13 -070077 newStr[len] = '\0';
Aart Bik3e40f4a2015-07-07 17:09:41 -070078 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 Srbeckyb06e28e2015-12-10 13:15:00 +000086static bool positionsCb(void* context, const DexFile::PositionInfo& entry) {
Aart Bik3e40f4a2015-07-07 17:09:41 -070087 int* pFirstLine = reinterpret_cast<int *>(context);
88 if (*pFirstLine == -1) {
David Srbeckyb06e28e2015-12-10 13:15:00 +000089 *pFirstLine = entry.line_;
Aart Bik3e40f4a2015-07-07 17:09:41 -070090 }
91 return 0;
92}
93
94/*
95 * Dumps a method.
96 */
97static void dumpMethod(const DexFile* pDexFile,
David Srbeckyb06e28e2015-12-10 13:15:00 +000098 const char* fileName, u4 idx, u4 flags ATTRIBUTE_UNUSED,
Aart Bik3e40f4a2015-07-07 17:09:41 -070099 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 Chartier8892c6b2018-01-09 15:10:17 -0800104 CodeItemDebugInfoAccessor accessor(*pDexFile, pCode, idx);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700105
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 Bikc05e2f22016-07-12 15:53:13 -0700110 std::unique_ptr<char[]> className(descriptorToDot(classDescriptor));
Aart Bik3e40f4a2015-07-07 17:09:41 -0700111 const u4 insnsOff = codeOffset + 0x10;
112
113 // Don't list methods that do not match a particular query.
114 if (gOptions.methodToFind != nullptr &&
Aart Bikc05e2f22016-07-12 15:53:13 -0700115 (strcmp(gOptions.classToFind, className.get()) != 0 ||
Aart Bik3e40f4a2015-07-07 17:09:41 -0700116 strcmp(gOptions.methodToFind, methodName) != 0)) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700117 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 Chartier641a3af2017-12-15 11:42:58 -0800127 pDexFile->DecodeDebugPositionInfo(accessor.DebugInfoOffset(), positionsCb, &firstLine);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700128
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 Chartier641a3af2017-12-15 11:42:58 -0800135 insnsOff, accessor.InsnsSizeInCodeUnits() * 2,
Aart Bikc05e2f22016-07-12 15:53:13 -0700136 className.get(), methodName, typeDesc, fileName, firstLine);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700137
138 free(typeDesc);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700139}
140
141/*
142 * Runs through all direct and virtual methods in the class.
143 */
144void dumpClass(const DexFile* pDexFile, u4 idx) {
145 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
146
147 const char* fileName;
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800148 if (!pClassDef.source_file_idx_.IsValid()) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700149 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 Chartiere17cf242017-06-19 11:05:51 -0700157 pClassData.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800158 // Direct and virtual methods.
159 for (; pClassData.HasNextMethod(); pClassData.Next()) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700160 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 */
172static 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 Bik37d6a3b2016-06-21 18:30:10 -0700175 static constexpr bool kVerifyChecksum = true;
David Sehr999646d2018-02-16 10:22:33 -0800176 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 Sehr5a1f6292018-01-19 11:08:51 -0800180 return -1;
181 }
Aart Bik3e40f4a2015-07-07 17:09:41 -0700182 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr999646d2018-02-16 10:22:33 -0800183 std::string error_msg;
David Sehr5a1f6292018-01-19 11:08:51 -0800184 const DexFileLoader dex_file_loader;
David Sehr999646d2018-02-16 10:22:33 -0800185 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 Gampe221d9812018-01-22 17:48:56 -0800192 LOG(ERROR) << error_msg;
Aart Bik3e40f4a2015-07-07 17:09:41 -0700193 return -1;
194 }
195
Aart Bik4e149602015-07-09 11:45:28 -0700196 // Success. Iterate over all dex files found in given file.
Aart Bik3e40f4a2015-07-07 17:09:41 -0700197 fprintf(gOutFile, "#%s\n", fileName);
Aart Bik4e149602015-07-09 11:45:28 -0700198 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 Bik3e40f4a2015-07-07 17:09:41 -0700205 }
206 return 0;
207}
208
209/*
210 * Shows usage.
211 */
212static void usage(void) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800213 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 Bik3e40f4a2015-07-07 17:09:41 -0700216}
217
218/*
219 * Main driver of the dexlist utility.
220 */
221int dexlistDriver(int argc, char** argv) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700222 // 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 Bikb1b45be2015-08-28 11:09:29 -0700237 // If -m p.c.m is given, then find all instances of the
Aart Bik3e40f4a2015-07-07 17:09:41 -0700238 // 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 Gampe221d9812018-01-22 17:48:56 -0800244 LOG(ERROR) << "Expected: package.Class.method";
Aart Bik3e40f4a2015-07-07 17:09:41 -0700245 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 Gampe221d9812018-01-22 17:48:56 -0800261 LOG(ERROR) << "No file specified";
Aart Bik3e40f4a2015-07-07 17:09:41 -0700262 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 Gampe221d9812018-01-22 17:48:56 -0800274 PLOG(ERROR) << "Can't open " << gOptions.outputFileName;
Aart Bik3e40f4a2015-07-07 17:09:41 -0700275 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
293int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800294 // Output all logging to stderr.
295 android::base::SetLogger(android::base::StderrLogger);
296
Aart Bik3e40f4a2015-07-07 17:09:41 -0700297 return art::dexlistDriver(argc, argv);
298}
299