blob: e3ca59c8bfe1c5b524a66bf5c1aad49737981646 [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
Aart Bik3e40f4a2015-07-07 17:09:41 -070026#include <stdio.h>
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070027#include <stdlib.h>
Aart Bik3e40f4a2015-07-07 17:09:41 -070028
29#include "dex_file-inl.h"
Mathieu Chartier79c87da2017-10-10 11:54:29 -070030#include "dex_file_loader.h"
Aart Bik3e40f4a2015-07-07 17:09:41 -070031#include "mem_map.h"
32#include "runtime.h"
33
34namespace art {
35
36static const char* gProgName = "dexlist";
37
38/* Command-line options. */
39static struct {
40 char* argCopy;
41 const char* classToFind;
42 const char* methodToFind;
43 const char* outputFileName;
44} gOptions;
45
46/*
47 * Output file. Defaults to stdout.
48 */
49static FILE* gOutFile = stdout;
50
51/*
52 * Data types that match the definitions in the VM specification.
53 */
54typedef uint8_t u1;
Aart Bik3e40f4a2015-07-07 17:09:41 -070055typedef uint32_t u4;
56typedef uint64_t u8;
Aart Bik3e40f4a2015-07-07 17:09:41 -070057
58/*
59 * Returns a newly-allocated string for the "dot version" of the class
60 * name for the given type descriptor. That is, The initial "L" and
61 * final ";" (if any) have been removed and all occurrences of '/'
62 * have been changed to '.'.
63 */
Aart Bikc05e2f22016-07-12 15:53:13 -070064static std::unique_ptr<char[]> descriptorToDot(const char* str) {
65 size_t len = strlen(str);
Aart Bik3e40f4a2015-07-07 17:09:41 -070066 if (str[0] == 'L') {
Aart Bikc05e2f22016-07-12 15:53:13 -070067 len -= 2; // Two fewer chars to copy (trims L and ;).
68 str++; // Start past 'L'.
Aart Bik3e40f4a2015-07-07 17:09:41 -070069 }
Aart Bikc05e2f22016-07-12 15:53:13 -070070 std::unique_ptr<char[]> newStr(new char[len + 1]);
71 for (size_t i = 0; i < len; i++) {
72 newStr[i] = (str[i] == '/') ? '.' : str[i];
Aart Bik3e40f4a2015-07-07 17:09:41 -070073 }
Aart Bikc05e2f22016-07-12 15:53:13 -070074 newStr[len] = '\0';
Aart Bik3e40f4a2015-07-07 17:09:41 -070075 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 */
David Srbeckyb06e28e2015-12-10 13:15:00 +000083static bool positionsCb(void* context, const DexFile::PositionInfo& entry) {
Aart Bik3e40f4a2015-07-07 17:09:41 -070084 int* pFirstLine = reinterpret_cast<int *>(context);
85 if (*pFirstLine == -1) {
David Srbeckyb06e28e2015-12-10 13:15:00 +000086 *pFirstLine = entry.line_;
Aart Bik3e40f4a2015-07-07 17:09:41 -070087 }
88 return 0;
89}
90
91/*
92 * Dumps a method.
93 */
94static void dumpMethod(const DexFile* pDexFile,
David Srbeckyb06e28e2015-12-10 13:15:00 +000095 const char* fileName, u4 idx, u4 flags ATTRIBUTE_UNUSED,
Aart Bik3e40f4a2015-07-07 17:09:41 -070096 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_);
Aart Bikc05e2f22016-07-12 15:53:13 -0700106 std::unique_ptr<char[]> className(descriptorToDot(classDescriptor));
Aart Bik3e40f4a2015-07-07 17:09:41 -0700107 const u4 insnsOff = codeOffset + 0x10;
108
109 // Don't list methods that do not match a particular query.
110 if (gOptions.methodToFind != nullptr &&
Aart Bikc05e2f22016-07-12 15:53:13 -0700111 (strcmp(gOptions.classToFind, className.get()) != 0 ||
Aart Bik3e40f4a2015-07-07 17:09:41 -0700112 strcmp(gOptions.methodToFind, methodName) != 0)) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700113 return;
114 }
115
116 // If the filename is empty, then set it to something printable.
117 if (fileName == nullptr || fileName[0] == 0) {
118 fileName = "(none)";
119 }
120
121 // Find the first line.
122 int firstLine = -1;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000123 pDexFile->DecodeDebugPositionInfo(pCode, positionsCb, &firstLine);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700124
125 // Method signature.
126 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
127 char* typeDesc = strdup(signature.ToString().c_str());
128
129 // Dump actual method information.
130 fprintf(gOutFile, "0x%08x %d %s %s %s %s %d\n",
131 insnsOff, pCode->insns_size_in_code_units_ * 2,
Aart Bikc05e2f22016-07-12 15:53:13 -0700132 className.get(), methodName, typeDesc, fileName, firstLine);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700133
134 free(typeDesc);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700135}
136
137/*
138 * Runs through all direct and virtual methods in the class.
139 */
140void dumpClass(const DexFile* pDexFile, u4 idx) {
141 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
142
143 const char* fileName;
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800144 if (!pClassDef.source_file_idx_.IsValid()) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700145 fileName = nullptr;
146 } else {
147 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
148 }
149
150 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
151 if (pEncodedData != nullptr) {
152 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700153 pClassData.SkipAllFields();
Mathieu Chartierb7c273c2017-11-10 18:07:56 -0800154 // Direct and virtual methods.
155 for (; pClassData.HasNextMethod(); pClassData.Next()) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700156 dumpMethod(pDexFile, fileName,
157 pClassData.GetMemberIndex(),
158 pClassData.GetRawMemberAccessFlags(),
159 pClassData.GetMethodCodeItem(),
160 pClassData.GetMethodCodeItemOffset());
161 }
162 }
163}
164
165/*
166 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
167 */
168static int processFile(const char* fileName) {
169 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
170 // all of which are Zip archives with "classes.dex" inside.
Aart Bik37d6a3b2016-06-21 18:30:10 -0700171 static constexpr bool kVerifyChecksum = true;
Aart Bik3e40f4a2015-07-07 17:09:41 -0700172 std::string error_msg;
173 std::vector<std::unique_ptr<const DexFile>> dex_files;
Nicolas Geoffray095c6c92017-10-19 13:59:55 +0100174 if (!DexFileLoader::Open(
175 fileName, fileName, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700176 fputs(error_msg.c_str(), stderr);
177 fputc('\n', stderr);
178 return -1;
179 }
180
Aart Bik4e149602015-07-09 11:45:28 -0700181 // Success. Iterate over all dex files found in given file.
Aart Bik3e40f4a2015-07-07 17:09:41 -0700182 fprintf(gOutFile, "#%s\n", fileName);
Aart Bik4e149602015-07-09 11:45:28 -0700183 for (size_t i = 0; i < dex_files.size(); i++) {
184 // Iterate over all classes in one dex file.
185 const DexFile* pDexFile = dex_files[i].get();
186 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
187 for (u4 idx = 0; idx < classDefsSize; idx++) {
188 dumpClass(pDexFile, idx);
189 }
Aart Bik3e40f4a2015-07-07 17:09:41 -0700190 }
191 return 0;
192}
193
194/*
195 * Shows usage.
196 */
197static void usage(void) {
198 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
199 fprintf(stderr, "%s: [-m p.c.m] [-o outfile] dexfile...\n", gProgName);
200 fprintf(stderr, "\n");
201}
202
203/*
204 * Main driver of the dexlist utility.
205 */
206int dexlistDriver(int argc, char** argv) {
207 // Art specific set up.
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700208 InitLogging(argv, Runtime::Abort);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700209 MemMap::Init();
210
211 // Reset options.
212 bool wantUsage = false;
213 memset(&gOptions, 0, sizeof(gOptions));
214
215 // Parse all arguments.
216 while (1) {
217 const int ic = getopt(argc, argv, "o:m:");
218 if (ic < 0) {
219 break; // done
220 }
221 switch (ic) {
222 case 'o': // output file
223 gOptions.outputFileName = optarg;
224 break;
225 case 'm':
Aart Bikb1b45be2015-08-28 11:09:29 -0700226 // If -m p.c.m is given, then find all instances of the
Aart Bik3e40f4a2015-07-07 17:09:41 -0700227 // fully-qualified method name. This isn't really what
228 // dexlist is for, but it's easy to do it here.
229 {
230 gOptions.argCopy = strdup(optarg);
231 char* meth = strrchr(gOptions.argCopy, '.');
232 if (meth == nullptr) {
233 fprintf(stderr, "Expected: package.Class.method\n");
234 wantUsage = true;
235 } else {
236 *meth = '\0';
237 gOptions.classToFind = gOptions.argCopy;
238 gOptions.methodToFind = meth + 1;
239 }
240 }
241 break;
242 default:
243 wantUsage = true;
244 break;
245 } // switch
246 } // while
247
248 // Detect early problems.
249 if (optind == argc) {
250 fprintf(stderr, "%s: no file specified\n", gProgName);
251 wantUsage = true;
252 }
253 if (wantUsage) {
254 usage();
255 free(gOptions.argCopy);
256 return 2;
257 }
258
259 // Open alternative output file.
260 if (gOptions.outputFileName) {
261 gOutFile = fopen(gOptions.outputFileName, "w");
262 if (!gOutFile) {
263 fprintf(stderr, "Can't open %s\n", gOptions.outputFileName);
264 free(gOptions.argCopy);
265 return 1;
266 }
267 }
268
269 // Process all files supplied on command line. If one of them fails we
270 // continue on, only returning a failure at the end.
271 int result = 0;
272 while (optind < argc) {
273 result |= processFile(argv[optind++]);
274 } // while
275
276 free(gOptions.argCopy);
277 return result != 0;
278}
279
280} // namespace art
281
282int main(int argc, char** argv) {
283 return art::dexlistDriver(argc, argv);
284}
285