blob: 6f19df55fd6c2044e318de648f7eda6cc98c4e0b [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
26#include <stdlib.h>
27#include <stdio.h>
28
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 */
63static char* descriptorToDot(const char* str) {
64 size_t at = strlen(str);
65 if (str[0] == 'L') {
66 at -= 2; // Two fewer chars to copy.
67 str++;
68 }
69 char* newStr = reinterpret_cast<char*>(malloc(at + 1));
70 newStr[at] = '\0';
71 while (at > 0) {
72 at--;
73 newStr[at] = (str[at] == '/') ? '.' : str[at];
74 }
75 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_);
106 char* className = descriptorToDot(classDescriptor);
107 const u4 insnsOff = codeOffset + 0x10;
108
109 // Don't list methods that do not match a particular query.
110 if (gOptions.methodToFind != nullptr &&
111 (strcmp(gOptions.classToFind, className) != 0 ||
112 strcmp(gOptions.methodToFind, methodName) != 0)) {
113 free(className);
114 return;
115 }
116
117 // If the filename is empty, then set it to something printable.
118 if (fileName == nullptr || fileName[0] == 0) {
119 fileName = "(none)";
120 }
121
122 // Find the first line.
123 int firstLine = -1;
David Srbeckyb06e28e2015-12-10 13:15:00 +0000124 pDexFile->DecodeDebugPositionInfo(pCode, positionsCb, &firstLine);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700125
126 // Method signature.
127 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
128 char* typeDesc = strdup(signature.ToString().c_str());
129
130 // Dump actual method information.
131 fprintf(gOutFile, "0x%08x %d %s %s %s %s %d\n",
132 insnsOff, pCode->insns_size_in_code_units_ * 2,
133 className, methodName, typeDesc, fileName, firstLine);
134
135 free(typeDesc);
136 free(className);
137}
138
139/*
140 * Runs through all direct and virtual methods in the class.
141 */
142void dumpClass(const DexFile* pDexFile, u4 idx) {
143 const DexFile::ClassDef& pClassDef = pDexFile->GetClassDef(idx);
144
145 const char* fileName;
146 if (pClassDef.source_file_idx_ == DexFile::kDexNoIndex) {
147 fileName = nullptr;
148 } else {
149 fileName = pDexFile->StringDataByIdx(pClassDef.source_file_idx_);
150 }
151
152 const u1* pEncodedData = pDexFile->GetClassData(pClassDef);
153 if (pEncodedData != nullptr) {
154 ClassDataItemIterator pClassData(*pDexFile, pEncodedData);
155 // Skip the fields.
156 for (; pClassData.HasNextStaticField(); pClassData.Next()) {}
157 for (; pClassData.HasNextInstanceField(); pClassData.Next()) {}
158 // Direct methods.
159 for (; pClassData.HasNextDirectMethod(); pClassData.Next()) {
160 dumpMethod(pDexFile, fileName,
161 pClassData.GetMemberIndex(),
162 pClassData.GetRawMemberAccessFlags(),
163 pClassData.GetMethodCodeItem(),
164 pClassData.GetMethodCodeItemOffset());
165 }
166 // Virtual methods.
167 for (; pClassData.HasNextVirtualMethod(); pClassData.Next()) {
168 dumpMethod(pDexFile, fileName,
169 pClassData.GetMemberIndex(),
170 pClassData.GetRawMemberAccessFlags(),
171 pClassData.GetMethodCodeItem(),
172 pClassData.GetMethodCodeItemOffset());
173 }
174 }
175}
176
177/*
178 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
179 */
180static int processFile(const char* fileName) {
181 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
182 // all of which are Zip archives with "classes.dex" inside.
Aart Bik37d6a3b2016-06-21 18:30:10 -0700183 static constexpr bool kVerifyChecksum = true;
Aart Bik3e40f4a2015-07-07 17:09:41 -0700184 std::string error_msg;
185 std::vector<std::unique_ptr<const DexFile>> dex_files;
Aart Bik37d6a3b2016-06-21 18:30:10 -0700186 if (!DexFile::Open(fileName, fileName, kVerifyChecksum, &error_msg, &dex_files)) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700187 fputs(error_msg.c_str(), stderr);
188 fputc('\n', stderr);
189 return -1;
190 }
191
Aart Bik4e149602015-07-09 11:45:28 -0700192 // Success. Iterate over all dex files found in given file.
Aart Bik3e40f4a2015-07-07 17:09:41 -0700193 fprintf(gOutFile, "#%s\n", fileName);
Aart Bik4e149602015-07-09 11:45:28 -0700194 for (size_t i = 0; i < dex_files.size(); i++) {
195 // Iterate over all classes in one dex file.
196 const DexFile* pDexFile = dex_files[i].get();
197 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
198 for (u4 idx = 0; idx < classDefsSize; idx++) {
199 dumpClass(pDexFile, idx);
200 }
Aart Bik3e40f4a2015-07-07 17:09:41 -0700201 }
202 return 0;
203}
204
205/*
206 * Shows usage.
207 */
208static void usage(void) {
209 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
210 fprintf(stderr, "%s: [-m p.c.m] [-o outfile] dexfile...\n", gProgName);
211 fprintf(stderr, "\n");
212}
213
214/*
215 * Main driver of the dexlist utility.
216 */
217int dexlistDriver(int argc, char** argv) {
218 // Art specific set up.
219 InitLogging(argv);
220 MemMap::Init();
221
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 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) {
244 fprintf(stderr, "Expected: package.Class.method\n");
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) {
261 fprintf(stderr, "%s: no file specified\n", gProgName);
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) {
274 fprintf(stderr, "Can't open %s\n", gOptions.outputFileName);
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
293int main(int argc, char** argv) {
294 return art::dexlistDriver(argc, argv);
295}
296