2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * List all methods in all concrete classes in one or more DEX files.
21 #include "libdex/DexFile.h"
23 #include "libdex/CmdUtils.h"
24 #include "libdex/DexClass.h"
25 #include "libdex/DexDebugInfo.h"
26 #include "libdex/DexProto.h"
27 #include "libdex/SysUtil.h"
39 static const char* gProgName = "dexlist";
41 /* command-line args */
44 const char* classToFind;
45 const char* methodToFind;
50 * Return a newly-allocated string for the "dot version" of the class
51 * name for the given type descriptor. That is, The initial "L" and
52 * final ";" (if any) have been removed and all occurrences of '/'
53 * have been changed to '.'.
55 static char* descriptorToDot(const char* str)
57 size_t at = strlen(str);
61 assert(str[at - 1] == ';');
62 at -= 2; /* Two fewer chars to copy. */
63 str++; /* Skip the 'L'. */
66 newStr = malloc(at + 1); /* Add one for the '\0'. */
71 newStr[at] = (str[at] == '/') ? '.' : str[at];
78 * Position 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.)
82 static int positionsCallback(void* cnxt, u4 address, u4 lineNum)
84 int* pFirstLine = (int*) cnxt;
85 if (*pFirstLine == -1)
86 *pFirstLine = lineNum;
94 void dumpMethod(DexFile* pDexFile, const char* fileName,
95 const DexMethod* pDexMethod, int i)
97 const DexMethodId* pMethodId;
99 const char* classDescriptor;
100 const char* methodName;
103 /* abstract and native methods don't get listed */
104 if (pDexMethod->codeOff == 0)
107 pMethodId = dexGetMethodId(pDexFile, pDexMethod->methodIdx);
108 methodName = dexStringById(pDexFile, pMethodId->nameIdx);
110 classDescriptor = dexStringByTypeIdx(pDexFile, pMethodId->classIdx);
112 pCode = dexGetCode(pDexFile, pDexMethod);
113 assert(pCode != NULL);
116 * If the filename is empty, then set it to something printable
117 * so that it is easier to parse.
119 * TODO: A method may override its class's default source file by
120 * specifying a different one in its debug info. This possibility
121 * should be handled here.
123 if (fileName == NULL || fileName[0] == 0) {
128 dexDecodeDebugInfo(pDexFile, pCode, classDescriptor, pMethodId->protoIdx,
129 pDexMethod->accessFlags, positionsCallback, NULL, &firstLine);
131 char* className = descriptorToDot(classDescriptor);
132 char* desc = dexCopyDescriptorFromMethodId(pDexFile, pMethodId);
133 u4 insnsOff = pDexMethod->codeOff + offsetof(DexCode, insns);
135 if (gParms.methodToFind != NULL &&
136 (strcmp(gParms.classToFind, className) != 0 ||
137 strcmp(gParms.methodToFind, methodName) != 0))
142 printf("0x%08x %d %s %s %s %s %d\n",
143 insnsOff, pCode->insnsSize * 2,
144 className, methodName, desc,
145 fileName, firstLine);
153 * Run through all direct and virtual methods in the class.
155 void dumpClass(DexFile* pDexFile, int idx)
157 const DexClassDef* pClassDef;
158 DexClassData* pClassData;
159 const u1* pEncodedData;
160 const char* fileName;
163 pClassDef = dexGetClassDef(pDexFile, idx);
164 pEncodedData = dexGetClassData(pDexFile, pClassDef);
165 pClassData = dexReadAndVerifyClassData(&pEncodedData, NULL);
167 if (pClassData == NULL) {
168 fprintf(stderr, "Trouble reading class data\n");
172 if (pClassDef->sourceFileIdx == 0xffffffff) {
175 fileName = dexStringById(pDexFile, pClassDef->sourceFileIdx);
179 * TODO: Each class def points at a sourceFile, so maybe that
180 * should be printed out. However, this needs to be coordinated
181 * with the tools that parse this output.
184 for (i = 0; i < (int) pClassData->header.directMethodsSize; i++) {
185 dumpMethod(pDexFile, fileName, &pClassData->directMethods[i], i);
188 for (i = 0; i < (int) pClassData->header.virtualMethodsSize; i++) {
189 dumpMethod(pDexFile, fileName, &pClassData->virtualMethods[i], i);
198 * Returns 0 on success.
200 int process(const char* fileName)
202 DexFile* pDexFile = NULL;
206 UnzipToFileResult utfr;
208 utfr = dexOpenAndMap(fileName, NULL, &map, true);
209 if (utfr != kUTFRSuccess) {
210 if (utfr == kUTFRNoClassesDex) {
211 /* no classes.dex in the APK; pretend we succeeded */
215 fprintf(stderr, "Unable to process '%s'\n", fileName);
220 pDexFile = dexFileParse(map.addr, map.length, kDexParseDefault);
221 if (pDexFile == NULL) {
222 fprintf(stderr, "Warning: DEX parse failed for '%s'\n", fileName);
226 printf("#%s\n", fileName);
229 for (i = 0; i < (int) pDexFile->pHeader->classDefsSize; i++) {
230 dumpClass(pDexFile, i);
237 sysReleaseShmem(&map);
238 if (pDexFile != NULL)
239 dexFileFree(pDexFile);
249 fprintf(stderr, "Copyright (C) 2007 The Android Open Source Project\n\n");
250 fprintf(stderr, "%s: dexfile [dexfile2 ...]\n", gProgName);
251 fprintf(stderr, "\n");
257 int main(int argc, char* const argv[])
263 * Find all instances of the fully-qualified method name. This isn't
264 * really what dexlist is for, but it's easy to do it here.
266 if (argc > 3 && strcmp(argv[1], "--method") == 0) {
267 gParms.argCopy = strdup(argv[2]);
268 char* meth = strrchr(gParms.argCopy, '.');
270 fprintf(stderr, "Expected package.Class.method\n");
271 free(gParms.argCopy);
275 gParms.classToFind = gParms.argCopy;
276 gParms.methodToFind = meth+1;
282 fprintf(stderr, "%s: no file specified\n", gProgName);
288 * Run through the list of files. If one of them fails we contine on,
289 * only returning a failure at the end.
291 for (i = 1; i < argc; i++)
292 result |= process(argv[i]);
294 free(gParms.argCopy);