blob: 6a1e22a525386384a963be9e6a9f2620b0dc273e [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"
30#include "mem_map.h"
31#include "runtime.h"
32
33namespace art {
34
35static const char* gProgName = "dexlist";
36
37/* Command-line options. */
38static 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 */
48static FILE* gOutFile = stdout;
49
50/*
51 * Data types that match the definitions in the VM specification.
52 */
53typedef uint8_t u1;
Aart Bik3e40f4a2015-07-07 17:09:41 -070054typedef uint32_t u4;
55typedef uint64_t u8;
Aart Bik3e40f4a2015-07-07 17:09:41 -070056
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 */
Aart Bikc05e2f22016-07-12 15:53:13 -070063static std::unique_ptr<char[]> descriptorToDot(const char* str) {
64 size_t len = strlen(str);
Aart Bik3e40f4a2015-07-07 17:09:41 -070065 if (str[0] == 'L') {
Aart Bikc05e2f22016-07-12 15:53:13 -070066 len -= 2; // Two fewer chars to copy (trims L and ;).
67 str++; // Start past 'L'.
Aart Bik3e40f4a2015-07-07 17:09:41 -070068 }
Aart Bikc05e2f22016-07-12 15:53:13 -070069 std::unique_ptr<char[]> newStr(new char[len + 1]);
70 for (size_t i = 0; i < len; i++) {
71 newStr[i] = (str[i] == '/') ? '.' : str[i];
Aart Bik3e40f4a2015-07-07 17:09:41 -070072 }
Aart Bikc05e2f22016-07-12 15:53:13 -070073 newStr[len] = '\0';
Aart Bik3e40f4a2015-07-07 17:09:41 -070074 return newStr;
75}
76
77/*
78 * Positions table callback; we just want to catch the number of the
79 * first line in the method, which *should* correspond to the first
80 * entry from the table. (Could also use "min" here.)
81 */
David Srbeckyb06e28e2015-12-10 13:15:00 +000082static bool positionsCb(void* context, const DexFile::PositionInfo& entry) {
Aart Bik3e40f4a2015-07-07 17:09:41 -070083 int* pFirstLine = reinterpret_cast<int *>(context);
84 if (*pFirstLine == -1) {
David Srbeckyb06e28e2015-12-10 13:15:00 +000085 *pFirstLine = entry.line_;
Aart Bik3e40f4a2015-07-07 17:09:41 -070086 }
87 return 0;
88}
89
90/*
91 * Dumps a method.
92 */
93static void dumpMethod(const DexFile* pDexFile,
David Srbeckyb06e28e2015-12-10 13:15:00 +000094 const char* fileName, u4 idx, u4 flags ATTRIBUTE_UNUSED,
Aart Bik3e40f4a2015-07-07 17:09:41 -070095 const DexFile::CodeItem* pCode, u4 codeOffset) {
96 // Abstract and native methods don't get listed.
97 if (pCode == nullptr || codeOffset == 0) {
98 return;
99 }
100
101 // Method information.
102 const DexFile::MethodId& pMethodId = pDexFile->GetMethodId(idx);
103 const char* methodName = pDexFile->StringDataByIdx(pMethodId.name_idx_);
104 const char* classDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
Aart Bikc05e2f22016-07-12 15:53:13 -0700105 std::unique_ptr<char[]> className(descriptorToDot(classDescriptor));
Aart Bik3e40f4a2015-07-07 17:09:41 -0700106 const u4 insnsOff = codeOffset + 0x10;
107
108 // Don't list methods that do not match a particular query.
109 if (gOptions.methodToFind != nullptr &&
Aart Bikc05e2f22016-07-12 15:53:13 -0700110 (strcmp(gOptions.classToFind, className.get()) != 0 ||
Aart Bik3e40f4a2015-07-07 17:09:41 -0700111 strcmp(gOptions.methodToFind, methodName) != 0)) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700112 return;
113 }
114
115 // If the filename is empty, then set it to something printable.
116 if (fileName == nullptr || fileName[0] == 0) {
117 fileName = "(none)";
118 }
119
120 // Find the first line.
121 int firstLine = -1;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000122 pDexFile->DecodeDebugPositionInfo(pCode, positionsCb, &firstLine);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700123
124 // Method signature.
125 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
126 char* typeDesc = strdup(signature.ToString().c_str());
127
128 // Dump actual method information.
129 fprintf(gOutFile, "0x%08x %d %s %s %s %s %d\n",
130 insnsOff, pCode->insns_size_in_code_units_ * 2,
Aart Bikc05e2f22016-07-12 15:53:13 -0700131 className.get(), methodName, typeDesc, fileName, firstLine);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700132
133 free(typeDesc);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700134}
135
136/*
137 * Runs through all direct and virtual methods in the class.
138 */
139void dumpClass(const DexFile* pDexFile, u4 idx) {
140 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
141
142 const char* fileName;
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800143 if (!pClassDef.source_file_idx_.IsValid()) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700144 fileName = nullptr;
145 } else {
146 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
147 }
148
149 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
150 if (pEncodedData != nullptr) {
151 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
Mathieu Chartiere17cf242017-06-19 11:05:51 -0700152 pClassData.SkipAllFields();
Aart Bik3e40f4a2015-07-07 17:09:41 -0700153 // Direct methods.
154 for (; pClassData.HasNextDirectMethod(); pClassData.Next()) {
155 dumpMethod(pDexFile, fileName,
156 pClassData.GetMemberIndex(),
157 pClassData.GetRawMemberAccessFlags(),
158 pClassData.GetMethodCodeItem(),
159 pClassData.GetMethodCodeItemOffset());
160 }
161 // Virtual methods.
162 for (; pClassData.HasNextVirtualMethod(); pClassData.Next()) {
163 dumpMethod(pDexFile, fileName,
164 pClassData.GetMemberIndex(),
165 pClassData.GetRawMemberAccessFlags(),
166 pClassData.GetMethodCodeItem(),
167 pClassData.GetMethodCodeItemOffset());
168 }
169 }
170}
171
172/*
173 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
174 */
175static int processFile(const char* fileName) {
176 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
177 // all of which are Zip archives with "classes.dex" inside.
Aart Bik37d6a3b2016-06-21 18:30:10 -0700178 static constexpr bool kVerifyChecksum = true;
Aart Bik3e40f4a2015-07-07 17:09:41 -0700179 std::string error_msg;
180 std::vector<std::unique_ptr<const DexFile>> dex_files;
Aart Bik37d6a3b2016-06-21 18:30:10 -0700181 if (!DexFile::Open(fileName, fileName, kVerifyChecksum, &error_msg, &dex_files)) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700182 fputs(error_msg.c_str(), stderr);
183 fputc('\n', stderr);
184 return -1;
185 }
186
Aart Bik4e149602015-07-09 11:45:28 -0700187 // Success. Iterate over all dex files found in given file.
Aart Bik3e40f4a2015-07-07 17:09:41 -0700188 fprintf(gOutFile, "#%s\n", fileName);
Aart Bik4e149602015-07-09 11:45:28 -0700189 for (size_t i = 0; i < dex_files.size(); i++) {
190 // Iterate over all classes in one dex file.
191 const DexFile* pDexFile = dex_files[i].get();
192 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
193 for (u4 idx = 0; idx < classDefsSize; idx++) {
194 dumpClass(pDexFile, idx);
195 }
Aart Bik3e40f4a2015-07-07 17:09:41 -0700196 }
197 return 0;
198}
199
200/*
201 * Shows usage.
202 */
203static void usage(void) {
204 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
205 fprintf(stderr, "%s: [-m p.c.m] [-o outfile] dexfile...\n", gProgName);
206 fprintf(stderr, "\n");
207}
208
209/*
210 * Main driver of the dexlist utility.
211 */
212int dexlistDriver(int argc, char** argv) {
213 // Art specific set up.
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700214 InitLogging(argv, Runtime::Abort);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700215 MemMap::Init();
216
217 // Reset options.
218 bool wantUsage = false;
219 memset(&gOptions, 0, sizeof(gOptions));
220
221 // Parse all arguments.
222 while (1) {
223 const int ic = getopt(argc, argv, "o:m:");
224 if (ic < 0) {
225 break; // done
226 }
227 switch (ic) {
228 case 'o': // output file
229 gOptions.outputFileName = optarg;
230 break;
231 case 'm':
Aart Bikb1b45be2015-08-28 11:09:29 -0700232 // If -m p.c.m is given, then find all instances of the
Aart Bik3e40f4a2015-07-07 17:09:41 -0700233 // fully-qualified method name. This isn't really what
234 // dexlist is for, but it's easy to do it here.
235 {
236 gOptions.argCopy = strdup(optarg);
237 char* meth = strrchr(gOptions.argCopy, '.');
238 if (meth == nullptr) {
239 fprintf(stderr, "Expected: package.Class.method\n");
240 wantUsage = true;
241 } else {
242 *meth = '\0';
243 gOptions.classToFind = gOptions.argCopy;
244 gOptions.methodToFind = meth + 1;
245 }
246 }
247 break;
248 default:
249 wantUsage = true;
250 break;
251 } // switch
252 } // while
253
254 // Detect early problems.
255 if (optind == argc) {
256 fprintf(stderr, "%s: no file specified\n", gProgName);
257 wantUsage = true;
258 }
259 if (wantUsage) {
260 usage();
261 free(gOptions.argCopy);
262 return 2;
263 }
264
265 // Open alternative output file.
266 if (gOptions.outputFileName) {
267 gOutFile = fopen(gOptions.outputFileName, "w");
268 if (!gOutFile) {
269 fprintf(stderr, "Can't open %s\n", gOptions.outputFileName);
270 free(gOptions.argCopy);
271 return 1;
272 }
273 }
274
275 // Process all files supplied on command line. If one of them fails we
276 // continue on, only returning a failure at the end.
277 int result = 0;
278 while (optind < argc) {
279 result |= processFile(argv[optind++]);
280 } // while
281
282 free(gOptions.argCopy);
283 return result != 0;
284}
285
286} // namespace art
287
288int main(int argc, char** argv) {
289 return art::dexlistDriver(argc, argv);
290}
291