blob: 1ced8ca771387ab66b150a2caefdc3c4d742aa98 [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
Andreas Gampe57943812017-12-06 21:39:13 -080029#include "base/logging.h" // For InitLogging.
David Sehr013fd802018-01-11 22:55:24 -080030#include "dex/art_dex_file_loader.h"
David Sehr9e734c72018-01-04 17:56:19 -080031#include "dex/code_item_accessors-no_art-inl.h"
32#include "dex/dex_file-inl.h"
33#include "dex/dex_file_loader.h"
Aart Bik3e40f4a2015-07-07 17:09:41 -070034#include "mem_map.h"
35#include "runtime.h"
36
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;
Aart Bik3e40f4a2015-07-07 17:09:41 -0700176 std::string error_msg;
177 std::vector<std::unique_ptr<const DexFile>> dex_files;
David Sehr013fd802018-01-11 22:55:24 -0800178 const ArtDexFileLoader dex_file_loader;
179 if (!dex_file_loader.Open(
Nicolas Geoffray095c6c92017-10-19 13:59:55 +0100180 fileName, fileName, /* verify */ true, kVerifyChecksum, &error_msg, &dex_files)) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700181 fputs(error_msg.c_str(), stderr);
182 fputc('\n', stderr);
183 return -1;
184 }
185
Aart Bik4e149602015-07-09 11:45:28 -0700186 // Success. Iterate over all dex files found in given file.
Aart Bik3e40f4a2015-07-07 17:09:41 -0700187 fprintf(gOutFile, "#%s\n", fileName);
Aart Bik4e149602015-07-09 11:45:28 -0700188 for (size_t i = 0; i < dex_files.size(); i++) {
189 // Iterate over all classes in one dex file.
190 const DexFile* pDexFile = dex_files[i].get();
191 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
192 for (u4 idx = 0; idx < classDefsSize; idx++) {
193 dumpClass(pDexFile, idx);
194 }
Aart Bik3e40f4a2015-07-07 17:09:41 -0700195 }
196 return 0;
197}
198
199/*
200 * Shows usage.
201 */
202static void usage(void) {
203 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
204 fprintf(stderr, "%s: [-m p.c.m] [-o outfile] dexfile...\n", gProgName);
205 fprintf(stderr, "\n");
206}
207
208/*
209 * Main driver of the dexlist utility.
210 */
211int dexlistDriver(int argc, char** argv) {
212 // Art specific set up.
Andreas Gampe51d80cc2017-06-21 21:05:13 -0700213 InitLogging(argv, Runtime::Abort);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700214 MemMap::Init();
215
216 // Reset options.
217 bool wantUsage = false;
218 memset(&gOptions, 0, sizeof(gOptions));
219
220 // Parse all arguments.
221 while (1) {
222 const int ic = getopt(argc, argv, "o:m:");
223 if (ic < 0) {
224 break; // done
225 }
226 switch (ic) {
227 case 'o': // output file
228 gOptions.outputFileName = optarg;
229 break;
230 case 'm':
Aart Bikb1b45be2015-08-28 11:09:29 -0700231 // If -m p.c.m is given, then find all instances of the
Aart Bik3e40f4a2015-07-07 17:09:41 -0700232 // fully-qualified method name. This isn't really what
233 // dexlist is for, but it's easy to do it here.
234 {
235 gOptions.argCopy = strdup(optarg);
236 char* meth = strrchr(gOptions.argCopy, '.');
237 if (meth == nullptr) {
238 fprintf(stderr, "Expected: package.Class.method\n");
239 wantUsage = true;
240 } else {
241 *meth = '\0';
242 gOptions.classToFind = gOptions.argCopy;
243 gOptions.methodToFind = meth + 1;
244 }
245 }
246 break;
247 default:
248 wantUsage = true;
249 break;
250 } // switch
251 } // while
252
253 // Detect early problems.
254 if (optind == argc) {
255 fprintf(stderr, "%s: no file specified\n", gProgName);
256 wantUsage = true;
257 }
258 if (wantUsage) {
259 usage();
260 free(gOptions.argCopy);
261 return 2;
262 }
263
264 // Open alternative output file.
265 if (gOptions.outputFileName) {
266 gOutFile = fopen(gOptions.outputFileName, "w");
267 if (!gOutFile) {
268 fprintf(stderr, "Can't open %s\n", gOptions.outputFileName);
269 free(gOptions.argCopy);
270 return 1;
271 }
272 }
273
274 // Process all files supplied on command line. If one of them fails we
275 // continue on, only returning a failure at the end.
276 int result = 0;
277 while (optind < argc) {
278 result |= processFile(argv[optind++]);
279 } // while
280
281 free(gOptions.argCopy);
282 return result != 0;
283}
284
285} // namespace art
286
287int main(int argc, char** argv) {
288 return art::dexlistDriver(argc, argv);
289}
290