blob: 9ef9ae9e4711698858d628ba3abb5d14a3a3dc90 [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
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -070033#include "dex/class_accessor-inl.h"
David Sehr0225f8e2018-01-31 08:52:24 +000034#include "dex/code_item_accessors-inl.h"
David Sehr9e734c72018-01-04 17:56:19 -080035#include "dex/dex_file-inl.h"
36#include "dex/dex_file_loader.h"
Aart Bik3e40f4a2015-07-07 17:09:41 -070037
38namespace art {
39
40static const char* gProgName = "dexlist";
41
42/* Command-line options. */
43static struct {
44 char* argCopy;
45 const char* classToFind;
46 const char* methodToFind;
47 const char* outputFileName;
48} gOptions;
49
50/*
51 * Output file. Defaults to stdout.
52 */
53static FILE* gOutFile = stdout;
54
55/*
56 * Data types that match the definitions in the VM specification.
57 */
Andreas Gampec55bb392018-09-21 00:02:02 +000058using u1 = uint8_t;
59using u4 = uint32_t;
60using u8 = uint64_t;
Aart Bik3e40f4a2015-07-07 17:09:41 -070061
62/*
63 * Returns a newly-allocated string for the "dot version" of the class
64 * name for the given type descriptor. That is, The initial "L" and
65 * final ";" (if any) have been removed and all occurrences of '/'
66 * have been changed to '.'.
67 */
Aart Bikc05e2f22016-07-12 15:53:13 -070068static std::unique_ptr<char[]> descriptorToDot(const char* str) {
69 size_t len = strlen(str);
Aart Bik3e40f4a2015-07-07 17:09:41 -070070 if (str[0] == 'L') {
Aart Bikc05e2f22016-07-12 15:53:13 -070071 len -= 2; // Two fewer chars to copy (trims L and ;).
72 str++; // Start past 'L'.
Aart Bik3e40f4a2015-07-07 17:09:41 -070073 }
Aart Bikc05e2f22016-07-12 15:53:13 -070074 std::unique_ptr<char[]> newStr(new char[len + 1]);
75 for (size_t i = 0; i < len; i++) {
76 newStr[i] = (str[i] == '/') ? '.' : str[i];
Aart Bik3e40f4a2015-07-07 17:09:41 -070077 }
Aart Bikc05e2f22016-07-12 15:53:13 -070078 newStr[len] = '\0';
Aart Bik3e40f4a2015-07-07 17:09:41 -070079 return newStr;
80}
81
82/*
Aart Bik3e40f4a2015-07-07 17:09:41 -070083 * Dumps a method.
84 */
85static void dumpMethod(const DexFile* pDexFile,
David Srbeckyb06e28e2015-12-10 13:15:00 +000086 const char* fileName, u4 idx, u4 flags ATTRIBUTE_UNUSED,
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080087 const dex::CodeItem* pCode, u4 codeOffset) {
Aart Bik3e40f4a2015-07-07 17:09:41 -070088 // Abstract and native methods don't get listed.
89 if (pCode == nullptr || codeOffset == 0) {
90 return;
91 }
Mathieu Chartier8892c6b2018-01-09 15:10:17 -080092 CodeItemDebugInfoAccessor accessor(*pDexFile, pCode, idx);
Aart Bik3e40f4a2015-07-07 17:09:41 -070093
94 // Method information.
Andreas Gampe3f1dcd32018-12-28 09:39:56 -080095 const dex::MethodId& pMethodId = pDexFile->GetMethodId(idx);
Aart Bik3e40f4a2015-07-07 17:09:41 -070096 const char* methodName = pDexFile->StringDataByIdx(pMethodId.name_idx_);
97 const char* classDescriptor = pDexFile->StringByTypeIdx(pMethodId.class_idx_);
Aart Bikc05e2f22016-07-12 15:53:13 -070098 std::unique_ptr<char[]> className(descriptorToDot(classDescriptor));
Aart Bik3e40f4a2015-07-07 17:09:41 -070099 const u4 insnsOff = codeOffset + 0x10;
100
101 // Don't list methods that do not match a particular query.
102 if (gOptions.methodToFind != nullptr &&
Aart Bikc05e2f22016-07-12 15:53:13 -0700103 (strcmp(gOptions.classToFind, className.get()) != 0 ||
Aart Bik3e40f4a2015-07-07 17:09:41 -0700104 strcmp(gOptions.methodToFind, methodName) != 0)) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700105 return;
106 }
107
108 // If the filename is empty, then set it to something printable.
109 if (fileName == nullptr || fileName[0] == 0) {
110 fileName = "(none)";
111 }
112
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700113 // We just want to catch the number of the first line in the method, which *should* correspond to
114 // the first entry from the table.
115 int first_line = -1;
116 accessor.DecodeDebugPositionInfo([&](const DexFile::PositionInfo& entry) {
117 first_line = entry.line_;
118 return true; // Early exit since we only want the first line.
119 });
Aart Bik3e40f4a2015-07-07 17:09:41 -0700120
121 // Method signature.
122 const Signature signature = pDexFile->GetMethodSignature(pMethodId);
123 char* typeDesc = strdup(signature.ToString().c_str());
124
125 // Dump actual method information.
126 fprintf(gOutFile, "0x%08x %d %s %s %s %s %d\n",
Mathieu Chartier641a3af2017-12-15 11:42:58 -0800127 insnsOff, accessor.InsnsSizeInCodeUnits() * 2,
Mathieu Chartier3e2e1232018-09-11 12:35:30 -0700128 className.get(), methodName, typeDesc, fileName, first_line);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700129
130 free(typeDesc);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700131}
132
133/*
134 * Runs through all direct and virtual methods in the class.
135 */
136void dumpClass(const DexFile* pDexFile, u4 idx) {
Andreas Gampe3f1dcd32018-12-28 09:39:56 -0800137 const dex::ClassDef& class_def = pDexFile->GetClassDef(idx);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700138
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -0700139 const char* fileName = nullptr;
140 if (class_def.source_file_idx_.IsValid()) {
141 fileName = pDexFile->StringDataByIdx(class_def.source_file_idx_);
Aart Bik3e40f4a2015-07-07 17:09:41 -0700142 }
143
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -0700144 ClassAccessor accessor(*pDexFile, class_def);
145 for (const ClassAccessor::Method& method : accessor.GetMethods()) {
146 dumpMethod(pDexFile,
147 fileName,
148 method.GetIndex(),
David Brazdil20c765f2018-10-27 21:45:15 +0000149 method.GetAccessFlags(),
Mathieu Chartier4ac9ade2018-07-24 10:27:21 -0700150 method.GetCodeItem(),
151 method.GetCodeItemOffset());
Aart Bik3e40f4a2015-07-07 17:09:41 -0700152 }
153}
154
155/*
156 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
157 */
158static int processFile(const char* fileName) {
159 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
160 // all of which are Zip archives with "classes.dex" inside.
Aart Bik37d6a3b2016-06-21 18:30:10 -0700161 static constexpr bool kVerifyChecksum = true;
David Sehr999646d2018-02-16 10:22:33 -0800162 std::string content;
163 // TODO: add an api to android::base to read a std::vector<uint8_t>.
164 if (!android::base::ReadFileToString(fileName, &content)) {
165 LOG(ERROR) << "ReadFileToString failed";
David Sehr5a1f6292018-01-19 11:08:51 -0800166 return -1;
167 }
Aart Bik3e40f4a2015-07-07 17:09:41 -0700168 std::vector<std::unique_ptr<const DexFile>> dex_files;
Dario Frenie166fac2018-07-16 11:08:03 +0100169 DexFileLoaderErrorCode error_code;
David Sehr999646d2018-02-16 10:22:33 -0800170 std::string error_msg;
David Sehr5a1f6292018-01-19 11:08:51 -0800171 const DexFileLoader dex_file_loader;
David Sehr999646d2018-02-16 10:22:33 -0800172 if (!dex_file_loader.OpenAll(reinterpret_cast<const uint8_t*>(content.data()),
173 content.size(),
174 fileName,
Andreas Gampe9b031f72018-10-04 11:03:34 -0700175 /*verify=*/ true,
David Sehr999646d2018-02-16 10:22:33 -0800176 kVerifyChecksum,
Dario Frenie166fac2018-07-16 11:08:03 +0100177 &error_code,
David Sehr999646d2018-02-16 10:22:33 -0800178 &error_msg,
179 &dex_files)) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800180 LOG(ERROR) << error_msg;
Aart Bik3e40f4a2015-07-07 17:09:41 -0700181 return -1;
182 }
183
Aart Bik4e149602015-07-09 11:45:28 -0700184 // Success. Iterate over all dex files found in given file.
Aart Bik3e40f4a2015-07-07 17:09:41 -0700185 fprintf(gOutFile, "#%s\n", fileName);
Aart Bik4e149602015-07-09 11:45:28 -0700186 for (size_t i = 0; i < dex_files.size(); i++) {
187 // Iterate over all classes in one dex file.
188 const DexFile* pDexFile = dex_files[i].get();
189 const u4 classDefsSize = pDexFile->GetHeader().class_defs_size_;
190 for (u4 idx = 0; idx < classDefsSize; idx++) {
191 dumpClass(pDexFile, idx);
192 }
Aart Bik3e40f4a2015-07-07 17:09:41 -0700193 }
194 return 0;
195}
196
197/*
198 * Shows usage.
199 */
Andreas Gampe70dfb692018-09-18 16:50:18 -0700200static void usage() {
Andreas Gampe221d9812018-01-22 17:48:56 -0800201 LOG(ERROR) << "Copyright (C) 2007 The Android Open Source Project\n";
202 LOG(ERROR) << gProgName << ": [-m p.c.m] [-o outfile] dexfile...";
203 LOG(ERROR) << "";
Aart Bik3e40f4a2015-07-07 17:09:41 -0700204}
205
206/*
207 * Main driver of the dexlist utility.
208 */
209int dexlistDriver(int argc, char** argv) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700210 // Reset options.
211 bool wantUsage = false;
212 memset(&gOptions, 0, sizeof(gOptions));
213
214 // Parse all arguments.
Andreas Gampe70dfb692018-09-18 16:50:18 -0700215 while (true) {
Aart Bik3e40f4a2015-07-07 17:09:41 -0700216 const int ic = getopt(argc, argv, "o:m:");
217 if (ic < 0) {
218 break; // done
219 }
220 switch (ic) {
221 case 'o': // output file
222 gOptions.outputFileName = optarg;
223 break;
224 case 'm':
Aart Bikb1b45be2015-08-28 11:09:29 -0700225 // If -m p.c.m is given, then find all instances of the
Aart Bik3e40f4a2015-07-07 17:09:41 -0700226 // fully-qualified method name. This isn't really what
227 // dexlist is for, but it's easy to do it here.
228 {
229 gOptions.argCopy = strdup(optarg);
230 char* meth = strrchr(gOptions.argCopy, '.');
231 if (meth == nullptr) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800232 LOG(ERROR) << "Expected: package.Class.method";
Aart Bik3e40f4a2015-07-07 17:09:41 -0700233 wantUsage = true;
234 } else {
235 *meth = '\0';
236 gOptions.classToFind = gOptions.argCopy;
237 gOptions.methodToFind = meth + 1;
238 }
239 }
240 break;
241 default:
242 wantUsage = true;
243 break;
244 } // switch
245 } // while
246
247 // Detect early problems.
248 if (optind == argc) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800249 LOG(ERROR) << "No file specified";
Aart Bik3e40f4a2015-07-07 17:09:41 -0700250 wantUsage = true;
251 }
252 if (wantUsage) {
253 usage();
254 free(gOptions.argCopy);
255 return 2;
256 }
257
258 // Open alternative output file.
259 if (gOptions.outputFileName) {
Andreas Gampedfcd82c2018-10-16 20:22:37 -0700260 gOutFile = fopen(gOptions.outputFileName, "we");
Aart Bik3e40f4a2015-07-07 17:09:41 -0700261 if (!gOutFile) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800262 PLOG(ERROR) << "Can't open " << gOptions.outputFileName;
Aart Bik3e40f4a2015-07-07 17:09:41 -0700263 free(gOptions.argCopy);
264 return 1;
265 }
266 }
267
268 // Process all files supplied on command line. If one of them fails we
269 // continue on, only returning a failure at the end.
270 int result = 0;
271 while (optind < argc) {
272 result |= processFile(argv[optind++]);
273 } // while
274
275 free(gOptions.argCopy);
Andreas Gampe7c5acbb2018-09-20 13:54:52 -0700276 return result != 0 ? 1 : 0;
Aart Bik3e40f4a2015-07-07 17:09:41 -0700277}
278
279} // namespace art
280
281int main(int argc, char** argv) {
Andreas Gampe221d9812018-01-22 17:48:56 -0800282 // Output all logging to stderr.
283 android::base::SetLogger(android::base::StderrLogger);
284
Aart Bik3e40f4a2015-07-07 17:09:41 -0700285 return art::dexlistDriver(argc, argv);
286}
287