blob: 0d0b37a9c084a4b36636001495238d55b362bdb3 [file] [log] [blame]
David Sehr7629f602016-08-07 16:01:51 -07001/*
2 * Copyright (C) 2016 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 dexlayout utility.
17 *
18 * This is a tool to read dex files into an internal representation,
19 * reorganize the representation, and emit dex files with a better
20 * file layout.
21 */
22
23#include "dexlayout.h"
24
25#include <inttypes.h>
26#include <stdio.h>
27
28#include <iostream>
29#include <memory>
30#include <sstream>
31#include <vector>
32
David Sehr853a8e12016-09-01 13:03:50 -070033#include "dex_ir_builder.h"
David Sehr7629f602016-08-07 16:01:51 -070034#include "dex_file-inl.h"
35#include "dex_instruction-inl.h"
36#include "utils.h"
37
38namespace art {
39
40/*
41 * Options parsed in main driver.
42 */
43struct Options options_;
44
45/*
46 * Output file. Defaults to stdout.
47 */
48FILE* out_file_ = stdout;
49
50/*
51 * Flags for use with createAccessFlagStr().
52 */
53enum AccessFor {
54 kAccessForClass = 0, kAccessForMethod = 1, kAccessForField = 2, kAccessForMAX
55};
56const int kNumFlags = 18;
57
58/*
59 * Gets 2 little-endian bytes.
60 */
61static inline uint16_t Get2LE(unsigned char const* src) {
62 return src[0] | (src[1] << 8);
63}
64
65/*
Jeff Haoc3acfc52016-08-29 14:18:26 -070066 * Converts a type descriptor to human-readable "dotted" form. For
67 * example, "Ljava/lang/String;" becomes "java.lang.String", and
68 * "[I" becomes "int[]". Also converts '$' to '.', which means this
69 * form can't be converted back to a descriptor.
70 */
71static std::string DescriptorToDotWrapper(const char* descriptor) {
72 std::string result = DescriptorToDot(descriptor);
73 size_t found = result.find('$');
74 while (found != std::string::npos) {
75 result[found] = '.';
76 found = result.find('$', found);
77 }
78 return result;
79}
80
81/*
David Sehr7629f602016-08-07 16:01:51 -070082 * Converts the class name portion of a type descriptor to human-readable
83 * "dotted" form. For example, "Ljava/lang/String;" becomes "String".
84 */
85static std::string DescriptorClassToDot(const char* str) {
86 std::string descriptor(str);
87 // Reduce to just the class name prefix.
88 size_t last_slash = descriptor.rfind('/');
89 if (last_slash == std::string::npos) {
90 last_slash = 0;
91 }
92 // Start past the '/' or 'L'.
93 last_slash++;
94
95 // Copy class name over, trimming trailing ';'.
96 size_t size = descriptor.size() - 1 - last_slash;
97 std::string result(descriptor.substr(last_slash, size));
98
99 // Replace '$' with '.'.
100 size_t dollar_sign = result.find('$');
101 while (dollar_sign != std::string::npos) {
102 result[dollar_sign] = '.';
103 dollar_sign = result.find('$', dollar_sign);
104 }
105
106 return result;
107}
108
109/*
110 * Returns string representing the boolean value.
111 */
112static const char* StrBool(bool val) {
113 return val ? "true" : "false";
114}
115
116/*
117 * Returns a quoted string representing the boolean value.
118 */
119static const char* QuotedBool(bool val) {
120 return val ? "\"true\"" : "\"false\"";
121}
122
123/*
124 * Returns a quoted string representing the access flags.
125 */
126static const char* QuotedVisibility(uint32_t access_flags) {
127 if (access_flags & kAccPublic) {
128 return "\"public\"";
129 } else if (access_flags & kAccProtected) {
130 return "\"protected\"";
131 } else if (access_flags & kAccPrivate) {
132 return "\"private\"";
133 } else {
134 return "\"package\"";
135 }
136}
137
138/*
139 * Counts the number of '1' bits in a word.
140 */
141static int CountOnes(uint32_t val) {
142 val = val - ((val >> 1) & 0x55555555);
143 val = (val & 0x33333333) + ((val >> 2) & 0x33333333);
144 return (((val + (val >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
145}
146
147/*
148 * Creates a new string with human-readable access flags.
149 *
150 * In the base language the access_flags fields are type uint16_t; in Dalvik they're uint32_t.
151 */
152static char* CreateAccessFlagStr(uint32_t flags, AccessFor for_what) {
153 static const char* kAccessStrings[kAccessForMAX][kNumFlags] = {
154 {
155 "PUBLIC", /* 0x00001 */
156 "PRIVATE", /* 0x00002 */
157 "PROTECTED", /* 0x00004 */
158 "STATIC", /* 0x00008 */
159 "FINAL", /* 0x00010 */
160 "?", /* 0x00020 */
161 "?", /* 0x00040 */
162 "?", /* 0x00080 */
163 "?", /* 0x00100 */
164 "INTERFACE", /* 0x00200 */
165 "ABSTRACT", /* 0x00400 */
166 "?", /* 0x00800 */
167 "SYNTHETIC", /* 0x01000 */
168 "ANNOTATION", /* 0x02000 */
169 "ENUM", /* 0x04000 */
170 "?", /* 0x08000 */
171 "VERIFIED", /* 0x10000 */
172 "OPTIMIZED", /* 0x20000 */
173 }, {
174 "PUBLIC", /* 0x00001 */
175 "PRIVATE", /* 0x00002 */
176 "PROTECTED", /* 0x00004 */
177 "STATIC", /* 0x00008 */
178 "FINAL", /* 0x00010 */
179 "SYNCHRONIZED", /* 0x00020 */
180 "BRIDGE", /* 0x00040 */
181 "VARARGS", /* 0x00080 */
182 "NATIVE", /* 0x00100 */
183 "?", /* 0x00200 */
184 "ABSTRACT", /* 0x00400 */
185 "STRICT", /* 0x00800 */
186 "SYNTHETIC", /* 0x01000 */
187 "?", /* 0x02000 */
188 "?", /* 0x04000 */
189 "MIRANDA", /* 0x08000 */
190 "CONSTRUCTOR", /* 0x10000 */
191 "DECLARED_SYNCHRONIZED", /* 0x20000 */
192 }, {
193 "PUBLIC", /* 0x00001 */
194 "PRIVATE", /* 0x00002 */
195 "PROTECTED", /* 0x00004 */
196 "STATIC", /* 0x00008 */
197 "FINAL", /* 0x00010 */
198 "?", /* 0x00020 */
199 "VOLATILE", /* 0x00040 */
200 "TRANSIENT", /* 0x00080 */
201 "?", /* 0x00100 */
202 "?", /* 0x00200 */
203 "?", /* 0x00400 */
204 "?", /* 0x00800 */
205 "SYNTHETIC", /* 0x01000 */
206 "?", /* 0x02000 */
207 "ENUM", /* 0x04000 */
208 "?", /* 0x08000 */
209 "?", /* 0x10000 */
210 "?", /* 0x20000 */
211 },
212 };
213
214 // Allocate enough storage to hold the expected number of strings,
215 // plus a space between each. We over-allocate, using the longest
216 // string above as the base metric.
217 const int kLongest = 21; // The strlen of longest string above.
218 const int count = CountOnes(flags);
219 char* str;
220 char* cp;
221 cp = str = reinterpret_cast<char*>(malloc(count * (kLongest + 1) + 1));
222
223 for (int i = 0; i < kNumFlags; i++) {
224 if (flags & 0x01) {
225 const char* accessStr = kAccessStrings[for_what][i];
226 const int len = strlen(accessStr);
227 if (cp != str) {
228 *cp++ = ' ';
229 }
230 memcpy(cp, accessStr, len);
231 cp += len;
232 }
233 flags >>= 1;
234 } // for
235
236 *cp = '\0';
237 return str;
238}
239
240static std::string GetSignatureForProtoId(const dex_ir::ProtoId* proto) {
241 if (proto == nullptr) {
242 return "<no signature>";
243 }
244
245 const std::vector<const dex_ir::TypeId*>& params = proto->Parameters();
246 std::string result("(");
247 for (uint32_t i = 0; i < params.size(); ++i) {
248 result += params[i]->GetStringId()->Data();
249 }
250 result += ")";
251 result += proto->ReturnType()->GetStringId()->Data();
252 return result;
253}
254
255/*
256 * Copies character data from "data" to "out", converting non-ASCII values
257 * to fprintf format chars or an ASCII filler ('.' or '?').
258 *
259 * The output buffer must be able to hold (2*len)+1 bytes. The result is
260 * NULL-terminated.
261 */
262static void Asciify(char* out, const unsigned char* data, size_t len) {
263 while (len--) {
264 if (*data < 0x20) {
265 // Could do more here, but we don't need them yet.
266 switch (*data) {
267 case '\0':
268 *out++ = '\\';
269 *out++ = '0';
270 break;
271 case '\n':
272 *out++ = '\\';
273 *out++ = 'n';
274 break;
275 default:
276 *out++ = '.';
277 break;
278 } // switch
279 } else if (*data >= 0x80) {
280 *out++ = '?';
281 } else {
282 *out++ = *data;
283 }
284 data++;
285 } // while
286 *out = '\0';
287}
288
289/*
290 * Dumps a string value with some escape characters.
291 */
292static void DumpEscapedString(const char* p) {
293 fputs("\"", out_file_);
294 for (; *p; p++) {
295 switch (*p) {
296 case '\\':
297 fputs("\\\\", out_file_);
298 break;
299 case '\"':
300 fputs("\\\"", out_file_);
301 break;
302 case '\t':
303 fputs("\\t", out_file_);
304 break;
305 case '\n':
306 fputs("\\n", out_file_);
307 break;
308 case '\r':
309 fputs("\\r", out_file_);
310 break;
311 default:
312 putc(*p, out_file_);
313 } // switch
314 } // for
315 fputs("\"", out_file_);
316}
317
318/*
319 * Dumps a string as an XML attribute value.
320 */
321static void DumpXmlAttribute(const char* p) {
322 for (; *p; p++) {
323 switch (*p) {
324 case '&':
325 fputs("&amp;", out_file_);
326 break;
327 case '<':
328 fputs("&lt;", out_file_);
329 break;
330 case '>':
331 fputs("&gt;", out_file_);
332 break;
333 case '"':
334 fputs("&quot;", out_file_);
335 break;
336 case '\t':
337 fputs("&#x9;", out_file_);
338 break;
339 case '\n':
340 fputs("&#xA;", out_file_);
341 break;
342 case '\r':
343 fputs("&#xD;", out_file_);
344 break;
345 default:
346 putc(*p, out_file_);
347 } // switch
348 } // for
349}
350
351/*
352 * Dumps encoded value.
353 */
354static void DumpEncodedValue(const dex_ir::ArrayItem* data) {
355 switch (data->Type()) {
356 case DexFile::kDexAnnotationByte:
357 fprintf(out_file_, "%" PRId8, data->GetByte());
358 break;
359 case DexFile::kDexAnnotationShort:
360 fprintf(out_file_, "%" PRId16, data->GetShort());
361 break;
362 case DexFile::kDexAnnotationChar:
363 fprintf(out_file_, "%" PRIu16, data->GetChar());
364 break;
365 case DexFile::kDexAnnotationInt:
366 fprintf(out_file_, "%" PRId32, data->GetInt());
367 break;
368 case DexFile::kDexAnnotationLong:
369 fprintf(out_file_, "%" PRId64, data->GetLong());
370 break;
371 case DexFile::kDexAnnotationFloat: {
372 fprintf(out_file_, "%g", data->GetFloat());
373 break;
374 }
375 case DexFile::kDexAnnotationDouble: {
376 fprintf(out_file_, "%g", data->GetDouble());
377 break;
378 }
379 case DexFile::kDexAnnotationString: {
380 dex_ir::StringId* string_id = data->GetStringId();
381 if (options_.output_format_ == kOutputPlain) {
382 DumpEscapedString(string_id->Data());
383 } else {
384 DumpXmlAttribute(string_id->Data());
385 }
386 break;
387 }
388 case DexFile::kDexAnnotationType: {
389 dex_ir::StringId* string_id = data->GetStringId();
390 fputs(string_id->Data(), out_file_);
391 break;
392 }
393 case DexFile::kDexAnnotationField:
394 case DexFile::kDexAnnotationEnum: {
395 dex_ir::FieldId* field_id = data->GetFieldId();
396 fputs(field_id->Name()->Data(), out_file_);
397 break;
398 }
399 case DexFile::kDexAnnotationMethod: {
400 dex_ir::MethodId* method_id = data->GetMethodId();
401 fputs(method_id->Name()->Data(), out_file_);
402 break;
403 }
404 case DexFile::kDexAnnotationArray: {
405 fputc('{', out_file_);
406 // Display all elements.
407 for (auto& array : *data->GetAnnotationArray()) {
408 fputc(' ', out_file_);
409 DumpEncodedValue(array.get());
410 }
411 fputs(" }", out_file_);
412 break;
413 }
414 case DexFile::kDexAnnotationAnnotation: {
415 fputs(data->GetAnnotationAnnotationString()->Data(), out_file_);
416 // Display all name=value pairs.
417 for (auto& subannotation : *data->GetAnnotationAnnotationNameValuePairArray()) {
418 fputc(' ', out_file_);
419 fputs(subannotation->Name()->Data(), out_file_);
420 fputc('=', out_file_);
421 DumpEncodedValue(subannotation->Value());
422 }
423 break;
424 }
425 case DexFile::kDexAnnotationNull:
426 fputs("null", out_file_);
427 break;
428 case DexFile::kDexAnnotationBoolean:
429 fputs(StrBool(data->GetBoolean()), out_file_);
430 break;
431 default:
432 fputs("????", out_file_);
433 break;
434 } // switch
435}
436
437/*
438 * Dumps the file header.
439 */
440static void DumpFileHeader(const dex_ir::Header* header) {
441 char sanitized[8 * 2 + 1];
442 fprintf(out_file_, "DEX file header:\n");
443 Asciify(sanitized, header->Magic(), 8);
444 fprintf(out_file_, "magic : '%s'\n", sanitized);
445 fprintf(out_file_, "checksum : %08x\n", header->Checksum());
446 fprintf(out_file_, "signature : %02x%02x...%02x%02x\n",
447 header->Signature()[0], header->Signature()[1],
448 header->Signature()[DexFile::kSha1DigestSize - 2],
449 header->Signature()[DexFile::kSha1DigestSize - 1]);
450 fprintf(out_file_, "file_size : %d\n", header->FileSize());
451 fprintf(out_file_, "header_size : %d\n", header->HeaderSize());
452 fprintf(out_file_, "link_size : %d\n", header->LinkSize());
453 fprintf(out_file_, "link_off : %d (0x%06x)\n",
454 header->LinkOffset(), header->LinkOffset());
455 fprintf(out_file_, "string_ids_size : %d\n", header->StringIdsSize());
456 fprintf(out_file_, "string_ids_off : %d (0x%06x)\n",
457 header->StringIdsOffset(), header->StringIdsOffset());
458 fprintf(out_file_, "type_ids_size : %d\n", header->TypeIdsSize());
459 fprintf(out_file_, "type_ids_off : %d (0x%06x)\n",
460 header->TypeIdsOffset(), header->TypeIdsOffset());
461 fprintf(out_file_, "proto_ids_size : %d\n", header->ProtoIdsSize());
462 fprintf(out_file_, "proto_ids_off : %d (0x%06x)\n",
463 header->ProtoIdsOffset(), header->ProtoIdsOffset());
464 fprintf(out_file_, "field_ids_size : %d\n", header->FieldIdsSize());
465 fprintf(out_file_, "field_ids_off : %d (0x%06x)\n",
466 header->FieldIdsOffset(), header->FieldIdsOffset());
467 fprintf(out_file_, "method_ids_size : %d\n", header->MethodIdsSize());
468 fprintf(out_file_, "method_ids_off : %d (0x%06x)\n",
469 header->MethodIdsOffset(), header->MethodIdsOffset());
470 fprintf(out_file_, "class_defs_size : %d\n", header->ClassDefsSize());
471 fprintf(out_file_, "class_defs_off : %d (0x%06x)\n",
472 header->ClassDefsOffset(), header->ClassDefsOffset());
473 fprintf(out_file_, "data_size : %d\n", header->DataSize());
474 fprintf(out_file_, "data_off : %d (0x%06x)\n\n",
475 header->DataOffset(), header->DataOffset());
476}
477
478/*
479 * Dumps a class_def_item.
480 */
481static void DumpClassDef(dex_ir::Header* header, int idx) {
482 // General class information.
483 dex_ir::ClassDef* class_def = header->ClassDefs()[idx].get();
484 fprintf(out_file_, "Class #%d header:\n", idx);
485 fprintf(out_file_, "class_idx : %d\n", class_def->ClassType()->GetOffset());
486 fprintf(out_file_, "access_flags : %d (0x%04x)\n",
487 class_def->GetAccessFlags(), class_def->GetAccessFlags());
Jeff Haoc3acfc52016-08-29 14:18:26 -0700488 uint32_t superclass_idx = class_def->Superclass() == nullptr ?
489 DexFile::kDexNoIndex16 : class_def->Superclass()->GetOffset();
490 fprintf(out_file_, "superclass_idx : %d\n", superclass_idx);
David Sehr7629f602016-08-07 16:01:51 -0700491 fprintf(out_file_, "interfaces_off : %d (0x%06x)\n",
492 class_def->InterfacesOffset(), class_def->InterfacesOffset());
493 uint32_t source_file_offset = 0xffffffffU;
494 if (class_def->SourceFile() != nullptr) {
495 source_file_offset = class_def->SourceFile()->GetOffset();
496 }
497 fprintf(out_file_, "source_file_idx : %d\n", source_file_offset);
498 uint32_t annotations_offset = 0;
499 if (class_def->Annotations() != nullptr) {
500 annotations_offset = class_def->Annotations()->GetOffset();
501 }
502 fprintf(out_file_, "annotations_off : %d (0x%06x)\n",
503 annotations_offset, annotations_offset);
David Sehr853a8e12016-09-01 13:03:50 -0700504 if (class_def->GetClassData() == nullptr) {
505 fprintf(out_file_, "class_data_off : %d (0x%06x)\n", 0, 0);
506 } else {
507 fprintf(out_file_, "class_data_off : %d (0x%06x)\n",
508 class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
509 }
David Sehr7629f602016-08-07 16:01:51 -0700510
511 // Fields and methods.
512 dex_ir::ClassData* class_data = class_def->GetClassData();
David Sehr853a8e12016-09-01 13:03:50 -0700513 if (class_data != nullptr && class_data->StaticFields() != nullptr) {
514 fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields()->size());
David Sehr7629f602016-08-07 16:01:51 -0700515 } else {
516 fprintf(out_file_, "static_fields_size : 0\n");
David Sehr853a8e12016-09-01 13:03:50 -0700517 }
518 if (class_data != nullptr && class_data->InstanceFields() != nullptr) {
519 fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields()->size());
520 } else {
David Sehr7629f602016-08-07 16:01:51 -0700521 fprintf(out_file_, "instance_fields_size: 0\n");
David Sehr853a8e12016-09-01 13:03:50 -0700522 }
523 if (class_data != nullptr && class_data->DirectMethods() != nullptr) {
524 fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods()->size());
525 } else {
David Sehr7629f602016-08-07 16:01:51 -0700526 fprintf(out_file_, "direct_methods_size : 0\n");
David Sehr853a8e12016-09-01 13:03:50 -0700527 }
528 if (class_data != nullptr && class_data->VirtualMethods() != nullptr) {
529 fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods()->size());
530 } else {
David Sehr7629f602016-08-07 16:01:51 -0700531 fprintf(out_file_, "virtual_methods_size: 0\n");
532 }
533 fprintf(out_file_, "\n");
534}
535
536/**
537 * Dumps an annotation set item.
538 */
539static void DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
David Sehr853a8e12016-09-01 13:03:50 -0700540 if (set_item == nullptr || set_item->GetItems()->size() == 0) {
David Sehr7629f602016-08-07 16:01:51 -0700541 fputs(" empty-annotation-set\n", out_file_);
542 return;
543 }
David Sehr853a8e12016-09-01 13:03:50 -0700544 for (std::unique_ptr<dex_ir::AnnotationItem>& annotation : *set_item->GetItems()) {
David Sehr7629f602016-08-07 16:01:51 -0700545 if (annotation == nullptr) {
546 continue;
547 }
548 fputs(" ", out_file_);
549 switch (annotation->GetVisibility()) {
550 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break;
551 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
552 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break;
553 default: fputs("VISIBILITY_UNKNOWN ", out_file_); break;
554 } // switch
555 // Decode raw bytes in annotation.
556 // const uint8_t* rData = annotation->annotation_;
557 dex_ir::ArrayItem* data = annotation->GetItem();
558 DumpEncodedValue(data);
559 fputc('\n', out_file_);
560 }
561}
562
563/*
564 * Dumps class annotations.
565 */
566static void DumpClassAnnotations(dex_ir::Header* header, int idx) {
567 dex_ir::ClassDef* class_def = header->ClassDefs()[idx].get();
568 dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
569 if (annotations_directory == nullptr) {
570 return; // none
571 }
572
573 fprintf(out_file_, "Class #%d annotations:\n", idx);
574
575 dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
David Sehr853a8e12016-09-01 13:03:50 -0700576 dex_ir::FieldAnnotationVector* fields = annotations_directory->GetFieldAnnotations();
577 dex_ir::MethodAnnotationVector* methods = annotations_directory->GetMethodAnnotations();
578 dex_ir::ParameterAnnotationVector* parameters = annotations_directory->GetParameterAnnotations();
David Sehr7629f602016-08-07 16:01:51 -0700579
580 // Annotations on the class itself.
581 if (class_set_item != nullptr) {
582 fprintf(out_file_, "Annotations on class\n");
583 DumpAnnotationSetItem(class_set_item);
584 }
585
586 // Annotations on fields.
David Sehr853a8e12016-09-01 13:03:50 -0700587 if (fields != nullptr) {
588 for (auto& field : *fields) {
589 const dex_ir::FieldId* field_id = field->GetFieldId();
590 const uint32_t field_idx = field_id->GetOffset();
591 const char* field_name = field_id->Name()->Data();
592 fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
593 DumpAnnotationSetItem(field->GetAnnotationSetItem());
594 }
David Sehr7629f602016-08-07 16:01:51 -0700595 }
596
597 // Annotations on methods.
David Sehr853a8e12016-09-01 13:03:50 -0700598 if (methods != nullptr) {
599 for (auto& method : *methods) {
600 const dex_ir::MethodId* method_id = method->GetMethodId();
601 const uint32_t method_idx = method_id->GetOffset();
602 const char* method_name = method_id->Name()->Data();
603 fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
604 DumpAnnotationSetItem(method->GetAnnotationSetItem());
605 }
David Sehr7629f602016-08-07 16:01:51 -0700606 }
607
608 // Annotations on method parameters.
David Sehr853a8e12016-09-01 13:03:50 -0700609 if (parameters != nullptr) {
610 for (auto& parameter : *parameters) {
611 const dex_ir::MethodId* method_id = parameter->GetMethodId();
612 const uint32_t method_idx = method_id->GetOffset();
613 const char* method_name = method_id->Name()->Data();
614 fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
615 uint32_t j = 0;
616 for (auto& annotation : *parameter->GetAnnotations()) {
617 fprintf(out_file_, "#%u\n", j);
618 DumpAnnotationSetItem(annotation.get());
619 ++j;
620 }
David Sehr7629f602016-08-07 16:01:51 -0700621 }
622 }
623
624 fputc('\n', out_file_);
625}
626
627/*
628 * Dumps an interface that a class declares to implement.
629 */
David Sehr853a8e12016-09-01 13:03:50 -0700630static void DumpInterface(const dex_ir::TypeId* type_item, int i) {
David Sehr7629f602016-08-07 16:01:51 -0700631 const char* interface_name = type_item->GetStringId()->Data();
632 if (options_.output_format_ == kOutputPlain) {
633 fprintf(out_file_, " #%d : '%s'\n", i, interface_name);
634 } else {
Jeff Haoc3acfc52016-08-29 14:18:26 -0700635 std::string dot(DescriptorToDotWrapper(interface_name));
David Sehr7629f602016-08-07 16:01:51 -0700636 fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
637 }
638}
639
640/*
641 * Dumps the catches table associated with the code.
642 */
643static void DumpCatches(const dex_ir::CodeItem* code) {
644 const uint16_t tries_size = code->TriesSize();
645
646 // No catch table.
647 if (tries_size == 0) {
648 fprintf(out_file_, " catches : (none)\n");
649 return;
650 }
651
652 // Dump all table entries.
653 fprintf(out_file_, " catches : %d\n", tries_size);
654 std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
655 for (uint32_t i = 0; i < tries_size; i++) {
656 const dex_ir::TryItem* try_item = (*tries)[i].get();
657 const uint32_t start = try_item->StartAddr();
658 const uint32_t end = start + try_item->InsnCount();
659 fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end);
660 for (auto& handler : try_item->GetHandlers()) {
661 const dex_ir::TypeId* type_id = handler->GetTypeId();
662 const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
663 fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress());
664 } // for
665 } // for
666}
667
668/*
669 * Dumps all positions table entries associated with the code.
670 */
671static void DumpPositionInfo(const dex_ir::CodeItem* code) {
672 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
673 if (debug_info == nullptr) {
674 return;
675 }
676 std::vector<std::unique_ptr<dex_ir::PositionInfo>>& positions = debug_info->GetPositionInfo();
677 for (size_t i = 0; i < positions.size(); ++i) {
678 fprintf(out_file_, " 0x%04x line=%d\n", positions[i]->address_, positions[i]->line_);
679 }
680}
681
682/*
683 * Dumps all locals table entries associated with the code.
684 */
685static void DumpLocalInfo(const dex_ir::CodeItem* code) {
686 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
687 if (debug_info == nullptr) {
688 return;
689 }
690 std::vector<std::unique_ptr<dex_ir::LocalInfo>>& locals = debug_info->GetLocalInfo();
691 for (size_t i = 0; i < locals.size(); ++i) {
692 dex_ir::LocalInfo* entry = locals[i].get();
693 fprintf(out_file_, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
694 entry->start_address_, entry->end_address_, entry->reg_,
695 entry->name_.c_str(), entry->descriptor_.c_str(), entry->signature_.c_str());
696 }
697}
698
699/*
700 * Helper for dumpInstruction(), which builds the string
701 * representation for the index in the given instruction.
702 * Returns a pointer to a buffer of sufficient size.
703 */
704static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
705 const Instruction* dec_insn,
706 size_t buf_size) {
707 std::unique_ptr<char[]> buf(new char[buf_size]);
708 // Determine index and width of the string.
709 uint32_t index = 0;
710 uint32_t width = 4;
711 switch (Instruction::FormatOf(dec_insn->Opcode())) {
712 // SOME NOT SUPPORTED:
713 // case Instruction::k20bc:
714 case Instruction::k21c:
715 case Instruction::k35c:
716 // case Instruction::k35ms:
717 case Instruction::k3rc:
718 // case Instruction::k3rms:
719 // case Instruction::k35mi:
720 // case Instruction::k3rmi:
721 index = dec_insn->VRegB();
722 width = 4;
723 break;
724 case Instruction::k31c:
725 index = dec_insn->VRegB();
726 width = 8;
727 break;
728 case Instruction::k22c:
729 // case Instruction::k22cs:
730 index = dec_insn->VRegC();
731 width = 4;
732 break;
733 default:
734 break;
735 } // switch
736
737 // Determine index type.
738 size_t outSize = 0;
739 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
740 case Instruction::kIndexUnknown:
741 // This function should never get called for this type, but do
742 // something sensible here, just to help with debugging.
743 outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
744 break;
745 case Instruction::kIndexNone:
746 // This function should never get called for this type, but do
747 // something sensible here, just to help with debugging.
748 outSize = snprintf(buf.get(), buf_size, "<no-index>");
749 break;
750 case Instruction::kIndexTypeRef:
751 if (index < header->TypeIdsSize()) {
752 const char* tp = header->TypeIds()[index]->GetStringId()->Data();
753 outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
754 } else {
755 outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
756 }
757 break;
758 case Instruction::kIndexStringRef:
759 if (index < header->StringIdsSize()) {
760 const char* st = header->StringIds()[index]->Data();
761 outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
762 } else {
763 outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
764 }
765 break;
766 case Instruction::kIndexMethodRef:
767 if (index < header->MethodIdsSize()) {
768 dex_ir::MethodId* method_id = header->MethodIds()[index].get();
769 const char* name = method_id->Name()->Data();
770 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
771 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
772 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
773 back_descriptor, name, type_descriptor, width, index);
774 } else {
775 outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
776 }
777 break;
778 case Instruction::kIndexFieldRef:
779 if (index < header->FieldIdsSize()) {
780 dex_ir::FieldId* field_id = header->FieldIds()[index].get();
781 const char* name = field_id->Name()->Data();
782 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
783 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
784 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
785 back_descriptor, name, type_descriptor, width, index);
786 } else {
787 outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
788 }
789 break;
790 case Instruction::kIndexVtableOffset:
791 outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
792 width, index, width, index);
793 break;
794 case Instruction::kIndexFieldOffset:
795 outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
796 break;
797 // SOME NOT SUPPORTED:
798 // case Instruction::kIndexVaries:
799 // case Instruction::kIndexInlineMethod:
800 default:
801 outSize = snprintf(buf.get(), buf_size, "<?>");
802 break;
803 } // switch
804
805 // Determine success of string construction.
806 if (outSize >= buf_size) {
807 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
808 // doesn't count/ the '\0' as part of its returned size, so we add explicit
809 // space for it here.
810 return IndexString(header, dec_insn, outSize + 1);
811 }
812 return buf;
813}
814
815/*
816 * Dumps a single instruction.
817 */
818static void DumpInstruction(dex_ir::Header* header, const dex_ir::CodeItem* code,
819 uint32_t code_offset, uint32_t insn_idx, uint32_t insn_width,
820 const Instruction* dec_insn) {
821 // Address of instruction (expressed as byte offset).
822 fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
823
824 // Dump (part of) raw bytes.
825 const uint16_t* insns = code->Insns();
826 for (uint32_t i = 0; i < 8; i++) {
827 if (i < insn_width) {
828 if (i == 7) {
829 fprintf(out_file_, " ... ");
830 } else {
831 // Print 16-bit value in little-endian order.
832 const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
833 fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
834 }
835 } else {
836 fputs(" ", out_file_);
837 }
838 } // for
839
840 // Dump pseudo-instruction or opcode.
841 if (dec_insn->Opcode() == Instruction::NOP) {
842 const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
843 if (instr == Instruction::kPackedSwitchSignature) {
844 fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
845 } else if (instr == Instruction::kSparseSwitchSignature) {
846 fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
847 } else if (instr == Instruction::kArrayDataSignature) {
848 fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
849 } else {
850 fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
851 }
852 } else {
853 fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
854 }
855
856 // Set up additional argument.
857 std::unique_ptr<char[]> index_buf;
858 if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
859 index_buf = IndexString(header, dec_insn, 200);
860 }
861
862 // Dump the instruction.
863 //
864 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
865 //
866 switch (Instruction::FormatOf(dec_insn->Opcode())) {
867 case Instruction::k10x: // op
868 break;
869 case Instruction::k12x: // op vA, vB
870 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
871 break;
872 case Instruction::k11n: // op vA, #+B
873 fprintf(out_file_, " v%d, #int %d // #%x",
874 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
875 break;
876 case Instruction::k11x: // op vAA
877 fprintf(out_file_, " v%d", dec_insn->VRegA());
878 break;
879 case Instruction::k10t: // op +AA
880 case Instruction::k20t: { // op +AAAA
881 const int32_t targ = (int32_t) dec_insn->VRegA();
882 fprintf(out_file_, " %04x // %c%04x",
883 insn_idx + targ,
884 (targ < 0) ? '-' : '+',
885 (targ < 0) ? -targ : targ);
886 break;
887 }
888 case Instruction::k22x: // op vAA, vBBBB
889 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
890 break;
891 case Instruction::k21t: { // op vAA, +BBBB
892 const int32_t targ = (int32_t) dec_insn->VRegB();
893 fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
894 insn_idx + targ,
895 (targ < 0) ? '-' : '+',
896 (targ < 0) ? -targ : targ);
897 break;
898 }
899 case Instruction::k21s: // op vAA, #+BBBB
900 fprintf(out_file_, " v%d, #int %d // #%x",
901 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
902 break;
903 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
904 // The printed format varies a bit based on the actual opcode.
905 if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
906 const int32_t value = dec_insn->VRegB() << 16;
907 fprintf(out_file_, " v%d, #int %d // #%x",
908 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
909 } else {
910 const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
911 fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
912 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
913 }
914 break;
915 case Instruction::k21c: // op vAA, thing@BBBB
916 case Instruction::k31c: // op vAA, thing@BBBBBBBB
917 fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
918 break;
919 case Instruction::k23x: // op vAA, vBB, vCC
920 fprintf(out_file_, " v%d, v%d, v%d",
921 dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
922 break;
923 case Instruction::k22b: // op vAA, vBB, #+CC
924 fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
925 dec_insn->VRegA(), dec_insn->VRegB(),
926 (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
927 break;
928 case Instruction::k22t: { // op vA, vB, +CCCC
929 const int32_t targ = (int32_t) dec_insn->VRegC();
930 fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
931 dec_insn->VRegA(), dec_insn->VRegB(),
932 insn_idx + targ,
933 (targ < 0) ? '-' : '+',
934 (targ < 0) ? -targ : targ);
935 break;
936 }
937 case Instruction::k22s: // op vA, vB, #+CCCC
938 fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
939 dec_insn->VRegA(), dec_insn->VRegB(),
940 (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
941 break;
942 case Instruction::k22c: // op vA, vB, thing@CCCC
943 // NOT SUPPORTED:
944 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
945 fprintf(out_file_, " v%d, v%d, %s",
946 dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
947 break;
948 case Instruction::k30t:
949 fprintf(out_file_, " #%08x", dec_insn->VRegA());
950 break;
951 case Instruction::k31i: { // op vAA, #+BBBBBBBB
952 // This is often, but not always, a float.
953 union {
954 float f;
955 uint32_t i;
956 } conv;
957 conv.i = dec_insn->VRegB();
958 fprintf(out_file_, " v%d, #float %g // #%08x",
959 dec_insn->VRegA(), conv.f, dec_insn->VRegB());
960 break;
961 }
962 case Instruction::k31t: // op vAA, offset +BBBBBBBB
963 fprintf(out_file_, " v%d, %08x // +%08x",
964 dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
965 break;
966 case Instruction::k32x: // op vAAAA, vBBBB
967 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
968 break;
969 case Instruction::k35c: { // op {vC, vD, vE, vF, vG}, thing@BBBB
970 // NOT SUPPORTED:
971 // case Instruction::k35ms: // [opt] invoke-virtual+super
972 // case Instruction::k35mi: // [opt] inline invoke
973 uint32_t arg[Instruction::kMaxVarArgRegs];
974 dec_insn->GetVarArgs(arg);
975 fputs(" {", out_file_);
976 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
977 if (i == 0) {
978 fprintf(out_file_, "v%d", arg[i]);
979 } else {
980 fprintf(out_file_, ", v%d", arg[i]);
981 }
982 } // for
983 fprintf(out_file_, "}, %s", index_buf.get());
984 break;
985 }
986 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
987 // NOT SUPPORTED:
988 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
989 // case Instruction::k3rmi: // [opt] execute-inline/range
990 {
991 // This doesn't match the "dx" output when some of the args are
992 // 64-bit values -- dx only shows the first register.
993 fputs(" {", out_file_);
994 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
995 if (i == 0) {
996 fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
997 } else {
998 fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
999 }
1000 } // for
1001 fprintf(out_file_, "}, %s", index_buf.get());
1002 }
1003 break;
1004 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
1005 // This is often, but not always, a double.
1006 union {
1007 double d;
1008 uint64_t j;
1009 } conv;
1010 conv.j = dec_insn->WideVRegB();
1011 fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
1012 dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
1013 break;
1014 }
1015 // NOT SUPPORTED:
1016 // case Instruction::k00x: // unknown op or breakpoint
1017 // break;
1018 default:
1019 fprintf(out_file_, " ???");
1020 break;
1021 } // switch
1022
1023 fputc('\n', out_file_);
1024}
1025
1026/*
1027 * Dumps a bytecode disassembly.
1028 */
1029static void DumpBytecodes(dex_ir::Header* header, uint32_t idx,
1030 const dex_ir::CodeItem* code, uint32_t code_offset) {
1031 dex_ir::MethodId* method_id = header->MethodIds()[idx].get();
1032 const char* name = method_id->Name()->Data();
1033 const char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1034 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1035
1036 // Generate header.
Jeff Haoc3acfc52016-08-29 14:18:26 -07001037 std::string dot(DescriptorToDotWrapper(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001038 fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n",
1039 code_offset, code_offset, dot.c_str(), name, type_descriptor);
1040
1041 // Iterate over all instructions.
1042 const uint16_t* insns = code->Insns();
1043 for (uint32_t insn_idx = 0; insn_idx < code->InsnsSize();) {
1044 const Instruction* instruction = Instruction::At(&insns[insn_idx]);
1045 const uint32_t insn_width = instruction->SizeInCodeUnits();
1046 if (insn_width == 0) {
1047 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insn_idx);
1048 break;
1049 }
1050 DumpInstruction(header, code, code_offset, insn_idx, insn_width, instruction);
1051 insn_idx += insn_width;
1052 } // for
1053}
1054
1055/*
1056 * Dumps code of a method.
1057 */
1058static void DumpCode(dex_ir::Header* header, uint32_t idx, const dex_ir::CodeItem* code,
1059 uint32_t code_offset) {
1060 fprintf(out_file_, " registers : %d\n", code->RegistersSize());
1061 fprintf(out_file_, " ins : %d\n", code->InsSize());
1062 fprintf(out_file_, " outs : %d\n", code->OutsSize());
1063 fprintf(out_file_, " insns size : %d 16-bit code units\n",
1064 code->InsnsSize());
1065
1066 // Bytecode disassembly, if requested.
1067 if (options_.disassemble_) {
1068 DumpBytecodes(header, idx, code, code_offset);
1069 }
1070
1071 // Try-catch blocks.
1072 DumpCatches(code);
1073
1074 // Positions and locals table in the debug info.
1075 fprintf(out_file_, " positions : \n");
1076 DumpPositionInfo(code);
1077 fprintf(out_file_, " locals : \n");
1078 DumpLocalInfo(code);
1079}
1080
1081/*
1082 * Dumps a method.
1083 */
1084static void DumpMethod(dex_ir::Header* header, uint32_t idx, uint32_t flags,
1085 const dex_ir::CodeItem* code, int i) {
1086 // Bail for anything private if export only requested.
1087 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1088 return;
1089 }
1090
1091 dex_ir::MethodId* method_id = header->MethodIds()[idx].get();
1092 const char* name = method_id->Name()->Data();
1093 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1094 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1095 char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
1096
1097 if (options_.output_format_ == kOutputPlain) {
1098 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1099 fprintf(out_file_, " name : '%s'\n", name);
1100 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1101 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1102 if (code == nullptr) {
1103 fprintf(out_file_, " code : (none)\n");
1104 } else {
1105 fprintf(out_file_, " code -\n");
1106 DumpCode(header, idx, code, code->GetOffset());
1107 }
1108 if (options_.disassemble_) {
1109 fputc('\n', out_file_);
1110 }
1111 } else if (options_.output_format_ == kOutputXml) {
1112 const bool constructor = (name[0] == '<');
1113
1114 // Method name and prototype.
1115 if (constructor) {
1116 std::string dot(DescriptorClassToDot(back_descriptor));
1117 fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
Jeff Haoc3acfc52016-08-29 14:18:26 -07001118 dot = DescriptorToDotWrapper(back_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001119 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1120 } else {
1121 fprintf(out_file_, "<method name=\"%s\"\n", name);
1122 const char* return_type = strrchr(type_descriptor, ')');
1123 if (return_type == nullptr) {
1124 fprintf(stderr, "bad method type descriptor '%s'\n", type_descriptor);
1125 goto bail;
1126 }
Jeff Haoc3acfc52016-08-29 14:18:26 -07001127 std::string dot(DescriptorToDotWrapper(return_type + 1));
David Sehr7629f602016-08-07 16:01:51 -07001128 fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
1129 fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
1130 fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
1131 fprintf(out_file_, " synchronized=%s\n", QuotedBool(
1132 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1133 }
1134
1135 // Additional method flags.
1136 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1137 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1138 // The "deprecated=" not knowable w/o parsing annotations.
1139 fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
1140
1141 // Parameters.
1142 if (type_descriptor[0] != '(') {
1143 fprintf(stderr, "ERROR: bad descriptor '%s'\n", type_descriptor);
1144 goto bail;
1145 }
1146 char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
1147 const char* base = type_descriptor + 1;
1148 int arg_num = 0;
1149 while (*base != ')') {
1150 char* cp = tmp_buf;
1151 while (*base == '[') {
1152 *cp++ = *base++;
1153 }
1154 if (*base == 'L') {
1155 // Copy through ';'.
1156 do {
1157 *cp = *base++;
1158 } while (*cp++ != ';');
1159 } else {
1160 // Primitive char, copy it.
1161 if (strchr("ZBCSIFJD", *base) == nullptr) {
1162 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
1163 break; // while
1164 }
1165 *cp++ = *base++;
1166 }
1167 // Null terminate and display.
1168 *cp++ = '\0';
Jeff Haoc3acfc52016-08-29 14:18:26 -07001169 std::string dot(DescriptorToDotWrapper(tmp_buf));
David Sehr7629f602016-08-07 16:01:51 -07001170 fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
1171 "</parameter>\n", arg_num++, dot.c_str());
1172 } // while
1173 free(tmp_buf);
1174 if (constructor) {
1175 fprintf(out_file_, "</constructor>\n");
1176 } else {
1177 fprintf(out_file_, "</method>\n");
1178 }
1179 }
1180
1181 bail:
1182 free(type_descriptor);
1183 free(access_str);
1184}
1185
1186/*
1187 * Dumps a static (class) field.
1188 */
1189static void DumpSField(dex_ir::Header* header, uint32_t idx, uint32_t flags,
1190 int i, dex_ir::ArrayItem* init) {
1191 // Bail for anything private if export only requested.
1192 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1193 return;
1194 }
1195
1196 dex_ir::FieldId* field_id = header->FieldIds()[idx].get();
1197 const char* name = field_id->Name()->Data();
1198 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
1199 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
1200 char* access_str = CreateAccessFlagStr(flags, kAccessForField);
1201
1202 if (options_.output_format_ == kOutputPlain) {
1203 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1204 fprintf(out_file_, " name : '%s'\n", name);
1205 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1206 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1207 if (init != nullptr) {
1208 fputs(" value : ", out_file_);
1209 DumpEncodedValue(init);
1210 fputs("\n", out_file_);
1211 }
1212 } else if (options_.output_format_ == kOutputXml) {
1213 fprintf(out_file_, "<field name=\"%s\"\n", name);
Jeff Haoc3acfc52016-08-29 14:18:26 -07001214 std::string dot(DescriptorToDotWrapper(type_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001215 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1216 fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0));
1217 fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0));
1218 // The "value=" is not knowable w/o parsing annotations.
1219 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1220 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1221 // The "deprecated=" is not knowable w/o parsing annotations.
1222 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags));
1223 if (init != nullptr) {
1224 fputs(" value=\"", out_file_);
1225 DumpEncodedValue(init);
1226 fputs("\"\n", out_file_);
1227 }
1228 fputs(">\n</field>\n", out_file_);
1229 }
1230
1231 free(access_str);
1232}
1233
1234/*
1235 * Dumps an instance field.
1236 */
1237static void DumpIField(dex_ir::Header* header, uint32_t idx, uint32_t flags, int i) {
1238 DumpSField(header, idx, flags, i, nullptr);
1239}
1240
1241/*
1242 * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
1243 * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
1244 * tool, so this is not performance-critical.
1245 */
1246
1247static void DumpCFG(const DexFile* dex_file,
1248 uint32_t dex_method_idx,
1249 const DexFile::CodeItem* code) {
1250 if (code != nullptr) {
1251 std::ostringstream oss;
1252 DumpMethodCFG(dex_file, dex_method_idx, oss);
1253 fprintf(out_file_, "%s", oss.str().c_str());
1254 }
1255}
1256
1257static void DumpCFG(const DexFile* dex_file, int idx) {
1258 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
1259 const uint8_t* class_data = dex_file->GetClassData(class_def);
1260 if (class_data == nullptr) { // empty class such as a marker interface?
1261 return;
1262 }
1263 ClassDataItemIterator it(*dex_file, class_data);
1264 while (it.HasNextStaticField()) {
1265 it.Next();
1266 }
1267 while (it.HasNextInstanceField()) {
1268 it.Next();
1269 }
1270 while (it.HasNextDirectMethod()) {
1271 DumpCFG(dex_file,
1272 it.GetMemberIndex(),
1273 it.GetMethodCodeItem());
1274 it.Next();
1275 }
1276 while (it.HasNextVirtualMethod()) {
1277 DumpCFG(dex_file,
David Sehr853a8e12016-09-01 13:03:50 -07001278 it.GetMemberIndex(),
1279 it.GetMethodCodeItem());
David Sehr7629f602016-08-07 16:01:51 -07001280 it.Next();
1281 }
1282}
1283
1284/*
1285 * Dumps the class.
1286 *
1287 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1288 *
1289 * If "*last_package" is nullptr or does not match the current class' package,
1290 * the value will be replaced with a newly-allocated string.
1291 */
David Sehr853a8e12016-09-01 13:03:50 -07001292static void DumpClass(const DexFile* dex_file,
1293 dex_ir::Header* header,
1294 int idx,
1295 char** last_package) {
David Sehr7629f602016-08-07 16:01:51 -07001296 dex_ir::ClassDef* class_def = header->ClassDefs()[idx].get();
1297 // Omitting non-public class.
1298 if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) {
1299 return;
1300 }
1301
1302 if (options_.show_section_headers_) {
1303 DumpClassDef(header, idx);
1304 }
1305
1306 if (options_.show_annotations_) {
1307 DumpClassAnnotations(header, idx);
1308 }
1309
1310 if (options_.show_cfg_) {
David Sehr853a8e12016-09-01 13:03:50 -07001311 DumpCFG(dex_file, idx);
David Sehr7629f602016-08-07 16:01:51 -07001312 return;
1313 }
1314
1315 // For the XML output, show the package name. Ideally we'd gather
1316 // up the classes, sort them, and dump them alphabetically so the
1317 // package name wouldn't jump around, but that's not a great plan
1318 // for something that needs to run on the device.
1319 const char* class_descriptor = header->ClassDefs()[idx]->ClassType()->GetStringId()->Data();
1320 if (!(class_descriptor[0] == 'L' &&
1321 class_descriptor[strlen(class_descriptor)-1] == ';')) {
1322 // Arrays and primitives should not be defined explicitly. Keep going?
1323 fprintf(stderr, "Malformed class name '%s'\n", class_descriptor);
1324 } else if (options_.output_format_ == kOutputXml) {
1325 char* mangle = strdup(class_descriptor + 1);
1326 mangle[strlen(mangle)-1] = '\0';
1327
1328 // Reduce to just the package name.
1329 char* last_slash = strrchr(mangle, '/');
1330 if (last_slash != nullptr) {
1331 *last_slash = '\0';
1332 } else {
1333 *mangle = '\0';
1334 }
1335
1336 for (char* cp = mangle; *cp != '\0'; cp++) {
1337 if (*cp == '/') {
1338 *cp = '.';
1339 }
1340 } // for
1341
1342 if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) {
1343 // Start of a new package.
1344 if (*last_package != nullptr) {
1345 fprintf(out_file_, "</package>\n");
1346 }
1347 fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle);
1348 free(*last_package);
1349 *last_package = mangle;
1350 } else {
1351 free(mangle);
1352 }
1353 }
1354
1355 // General class information.
1356 char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass);
1357 const char* superclass_descriptor = nullptr;
1358 if (class_def->Superclass() != nullptr) {
1359 superclass_descriptor = class_def->Superclass()->GetStringId()->Data();
1360 }
1361 if (options_.output_format_ == kOutputPlain) {
1362 fprintf(out_file_, "Class #%d -\n", idx);
1363 fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor);
1364 fprintf(out_file_, " Access flags : 0x%04x (%s)\n",
1365 class_def->GetAccessFlags(), access_str);
1366 if (superclass_descriptor != nullptr) {
1367 fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor);
1368 }
1369 fprintf(out_file_, " Interfaces -\n");
1370 } else {
1371 std::string dot(DescriptorClassToDot(class_descriptor));
1372 fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str());
1373 if (superclass_descriptor != nullptr) {
Jeff Haoc3acfc52016-08-29 14:18:26 -07001374 dot = DescriptorToDotWrapper(superclass_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001375 fprintf(out_file_, " extends=\"%s\"\n", dot.c_str());
1376 }
1377 fprintf(out_file_, " interface=%s\n",
1378 QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0));
1379 fprintf(out_file_, " abstract=%s\n",
1380 QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0));
1381 fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0));
1382 fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0));
1383 // The "deprecated=" not knowable w/o parsing annotations.
1384 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags()));
1385 fprintf(out_file_, ">\n");
1386 }
1387
1388 // Interfaces.
David Sehr853a8e12016-09-01 13:03:50 -07001389 dex_ir::TypeIdVector* interfaces = class_def->Interfaces();
1390 if (interfaces != nullptr) {
1391 for (uint32_t i = 0; i < interfaces->size(); i++) {
1392 DumpInterface((*interfaces)[i], i);
1393 } // for
1394 }
David Sehr7629f602016-08-07 16:01:51 -07001395
1396 // Fields and methods.
1397 dex_ir::ClassData* class_data = class_def->GetClassData();
1398 // Prepare data for static fields.
1399 std::vector<std::unique_ptr<dex_ir::ArrayItem>>* static_values = class_def->StaticValues();
1400 const uint32_t static_values_size = (static_values == nullptr) ? 0 : static_values->size();
1401
1402 // Static fields.
1403 if (options_.output_format_ == kOutputPlain) {
1404 fprintf(out_file_, " Static fields -\n");
1405 }
David Sehr853a8e12016-09-01 13:03:50 -07001406 if (class_data != nullptr) {
1407 dex_ir::FieldItemVector* static_fields = class_data->StaticFields();
1408 if (static_fields != nullptr) {
1409 for (uint32_t i = 0; i < static_fields->size(); i++) {
1410 DumpSField(header,
1411 (*static_fields)[i]->GetFieldId()->GetOffset(),
1412 (*static_fields)[i]->GetAccessFlags(),
1413 i,
1414 i < static_values_size ? (*static_values)[i].get() : nullptr);
1415 } // for
1416 }
1417 }
David Sehr7629f602016-08-07 16:01:51 -07001418
1419 // Instance fields.
1420 if (options_.output_format_ == kOutputPlain) {
1421 fprintf(out_file_, " Instance fields -\n");
1422 }
David Sehr853a8e12016-09-01 13:03:50 -07001423 if (class_data != nullptr) {
1424 dex_ir::FieldItemVector* instance_fields = class_data->InstanceFields();
1425 if (instance_fields != nullptr) {
1426 for (uint32_t i = 0; i < instance_fields->size(); i++) {
1427 DumpIField(header,
1428 (*instance_fields)[i]->GetFieldId()->GetOffset(),
1429 (*instance_fields)[i]->GetAccessFlags(),
1430 i);
1431 } // for
1432 }
1433 }
David Sehr7629f602016-08-07 16:01:51 -07001434
1435 // Direct methods.
1436 if (options_.output_format_ == kOutputPlain) {
1437 fprintf(out_file_, " Direct methods -\n");
1438 }
David Sehr853a8e12016-09-01 13:03:50 -07001439 if (class_data != nullptr) {
1440 dex_ir::MethodItemVector* direct_methods = class_data->DirectMethods();
1441 if (direct_methods != nullptr) {
1442 for (uint32_t i = 0; i < direct_methods->size(); i++) {
1443 DumpMethod(header,
1444 (*direct_methods)[i]->GetMethodId()->GetOffset(),
1445 (*direct_methods)[i]->GetAccessFlags(),
1446 (*direct_methods)[i]->GetCodeItem(),
1447 i);
1448 } // for
1449 }
1450 }
David Sehr7629f602016-08-07 16:01:51 -07001451
1452 // Virtual methods.
1453 if (options_.output_format_ == kOutputPlain) {
1454 fprintf(out_file_, " Virtual methods -\n");
1455 }
David Sehr853a8e12016-09-01 13:03:50 -07001456 if (class_data != nullptr) {
1457 dex_ir::MethodItemVector* virtual_methods = class_data->VirtualMethods();
1458 if (virtual_methods != nullptr) {
1459 for (uint32_t i = 0; i < virtual_methods->size(); i++) {
1460 DumpMethod(header,
1461 (*virtual_methods)[i]->GetMethodId()->GetOffset(),
1462 (*virtual_methods)[i]->GetAccessFlags(),
1463 (*virtual_methods)[i]->GetCodeItem(),
1464 i);
1465 } // for
1466 }
1467 }
David Sehr7629f602016-08-07 16:01:51 -07001468
1469 // End of class.
1470 if (options_.output_format_ == kOutputPlain) {
1471 const char* file_name = "unknown";
1472 if (class_def->SourceFile() != nullptr) {
1473 file_name = class_def->SourceFile()->Data();
1474 }
1475 const dex_ir::StringId* source_file = class_def->SourceFile();
1476 fprintf(out_file_, " source_file_idx : %d (%s)\n\n",
1477 source_file == nullptr ? 0xffffffffU : source_file->GetOffset(), file_name);
1478 } else if (options_.output_format_ == kOutputXml) {
1479 fprintf(out_file_, "</class>\n");
1480 }
1481
1482 free(access_str);
1483}
1484
1485/*
1486 * Dumps the requested sections of the file.
1487 */
1488static void ProcessDexFile(const char* file_name, const DexFile* dex_file) {
1489 if (options_.verbose_) {
1490 fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n",
1491 file_name, dex_file->GetHeader().magic_ + 4);
1492 }
David Sehr853a8e12016-09-01 13:03:50 -07001493 dex_ir::Header* header = dex_ir::DexIrBuilder(*dex_file);
David Sehr7629f602016-08-07 16:01:51 -07001494
1495 // Headers.
1496 if (options_.show_file_headers_) {
David Sehr853a8e12016-09-01 13:03:50 -07001497 DumpFileHeader(header);
David Sehr7629f602016-08-07 16:01:51 -07001498 }
1499
1500 // Open XML context.
1501 if (options_.output_format_ == kOutputXml) {
1502 fprintf(out_file_, "<api>\n");
1503 }
1504
1505 // Iterate over all classes.
1506 char* package = nullptr;
David Sehr853a8e12016-09-01 13:03:50 -07001507 const uint32_t class_defs_size = header->ClassDefsSize();
David Sehr7629f602016-08-07 16:01:51 -07001508 for (uint32_t i = 0; i < class_defs_size; i++) {
David Sehr853a8e12016-09-01 13:03:50 -07001509 DumpClass(dex_file, header, i, &package);
David Sehr7629f602016-08-07 16:01:51 -07001510 } // for
1511
1512 // Free the last package allocated.
1513 if (package != nullptr) {
1514 fprintf(out_file_, "</package>\n");
1515 free(package);
1516 }
1517
1518 // Close XML context.
1519 if (options_.output_format_ == kOutputXml) {
1520 fprintf(out_file_, "</api>\n");
1521 }
1522}
1523
1524/*
1525 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1526 */
1527int ProcessFile(const char* file_name) {
1528 if (options_.verbose_) {
1529 fprintf(out_file_, "Processing '%s'...\n", file_name);
1530 }
1531
1532 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1533 // all of which are Zip archives with "classes.dex" inside.
1534 const bool verify_checksum = !options_.ignore_bad_checksum_;
1535 std::string error_msg;
1536 std::vector<std::unique_ptr<const DexFile>> dex_files;
1537 if (!DexFile::Open(file_name, file_name, verify_checksum, &error_msg, &dex_files)) {
1538 // Display returned error message to user. Note that this error behavior
1539 // differs from the error messages shown by the original Dalvik dexdump.
1540 fputs(error_msg.c_str(), stderr);
1541 fputc('\n', stderr);
1542 return -1;
1543 }
1544
1545 // Success. Either report checksum verification or process
1546 // all dex files found in given file.
1547 if (options_.checksum_only_) {
1548 fprintf(out_file_, "Checksum verified\n");
1549 } else {
1550 for (size_t i = 0; i < dex_files.size(); i++) {
1551 ProcessDexFile(file_name, dex_files[i].get());
1552 }
1553 }
1554 return 0;
1555}
1556
1557} // namespace art