blob: 0b3161426a4c0954ea797815420721eedbe8756b [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
33#include "dex_ir.h"
34#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);
504 fprintf(out_file_, "class_data_off : %d (0x%06x)\n",
505 class_def->GetClassData()->GetOffset(), class_def->GetClassData()->GetOffset());
506
507 // Fields and methods.
508 dex_ir::ClassData* class_data = class_def->GetClassData();
509 if (class_data != nullptr) {
510 fprintf(out_file_, "static_fields_size : %zu\n", class_data->StaticFields().size());
511 fprintf(out_file_, "instance_fields_size: %zu\n", class_data->InstanceFields().size());
512 fprintf(out_file_, "direct_methods_size : %zu\n", class_data->DirectMethods().size());
513 fprintf(out_file_, "virtual_methods_size: %zu\n", class_data->VirtualMethods().size());
514 } else {
515 fprintf(out_file_, "static_fields_size : 0\n");
516 fprintf(out_file_, "instance_fields_size: 0\n");
517 fprintf(out_file_, "direct_methods_size : 0\n");
518 fprintf(out_file_, "virtual_methods_size: 0\n");
519 }
520 fprintf(out_file_, "\n");
521}
522
523/**
524 * Dumps an annotation set item.
525 */
526static void DumpAnnotationSetItem(dex_ir::AnnotationSetItem* set_item) {
527 if (set_item == nullptr || set_item->GetItems().size() == 0) {
528 fputs(" empty-annotation-set\n", out_file_);
529 return;
530 }
531 for (std::unique_ptr<dex_ir::AnnotationSetItem::AnnotationItem>& annotation :
532 set_item->GetItems()) {
533 if (annotation == nullptr) {
534 continue;
535 }
536 fputs(" ", out_file_);
537 switch (annotation->GetVisibility()) {
538 case DexFile::kDexVisibilityBuild: fputs("VISIBILITY_BUILD ", out_file_); break;
539 case DexFile::kDexVisibilityRuntime: fputs("VISIBILITY_RUNTIME ", out_file_); break;
540 case DexFile::kDexVisibilitySystem: fputs("VISIBILITY_SYSTEM ", out_file_); break;
541 default: fputs("VISIBILITY_UNKNOWN ", out_file_); break;
542 } // switch
543 // Decode raw bytes in annotation.
544 // const uint8_t* rData = annotation->annotation_;
545 dex_ir::ArrayItem* data = annotation->GetItem();
546 DumpEncodedValue(data);
547 fputc('\n', out_file_);
548 }
549}
550
551/*
552 * Dumps class annotations.
553 */
554static void DumpClassAnnotations(dex_ir::Header* header, int idx) {
555 dex_ir::ClassDef* class_def = header->ClassDefs()[idx].get();
556 dex_ir::AnnotationsDirectoryItem* annotations_directory = class_def->Annotations();
557 if (annotations_directory == nullptr) {
558 return; // none
559 }
560
561 fprintf(out_file_, "Class #%d annotations:\n", idx);
562
563 dex_ir::AnnotationSetItem* class_set_item = annotations_directory->GetClassAnnotation();
564 std::vector<std::unique_ptr<dex_ir::AnnotationsDirectoryItem::FieldAnnotation>>& fields =
565 annotations_directory->GetFieldAnnotations();
566 std::vector<std::unique_ptr<dex_ir::AnnotationsDirectoryItem::MethodAnnotation>>& methods =
567 annotations_directory->GetMethodAnnotations();
568 std::vector<std::unique_ptr<dex_ir::AnnotationsDirectoryItem::ParameterAnnotation>>& parameters =
569 annotations_directory->GetParameterAnnotations();
570
571 // Annotations on the class itself.
572 if (class_set_item != nullptr) {
573 fprintf(out_file_, "Annotations on class\n");
574 DumpAnnotationSetItem(class_set_item);
575 }
576
577 // Annotations on fields.
578 for (auto& field : fields) {
579 const dex_ir::FieldId* field_id = field->GetFieldId();
580 const uint32_t field_idx = field_id->GetOffset();
581 const char* field_name = field_id->Name()->Data();
582 fprintf(out_file_, "Annotations on field #%u '%s'\n", field_idx, field_name);
583 DumpAnnotationSetItem(field->GetAnnotationSetItem());
584 }
585
586 // Annotations on methods.
587 for (auto& method : methods) {
588 const dex_ir::MethodId* method_id = method->GetMethodId();
589 const uint32_t method_idx = method_id->GetOffset();
590 const char* method_name = method_id->Name()->Data();
591 fprintf(out_file_, "Annotations on method #%u '%s'\n", method_idx, method_name);
592 DumpAnnotationSetItem(method->GetAnnotationSetItem());
593 }
594
595 // Annotations on method parameters.
596 for (auto& parameter : parameters) {
597 const dex_ir::MethodId* method_id = parameter->GetMethodId();
598 const uint32_t method_idx = method_id->GetOffset();
599 const char* method_name = method_id->Name()->Data();
600 fprintf(out_file_, "Annotations on method #%u '%s' parameters\n", method_idx, method_name);
601 uint32_t j = 0;
602 for (auto& annotation : parameter->GetAnnotations()) {
603 fprintf(out_file_, "#%u\n", j);
604 DumpAnnotationSetItem(annotation.get());
605 ++j;
606 }
607 }
608
609 fputc('\n', out_file_);
610}
611
612/*
613 * Dumps an interface that a class declares to implement.
614 */
615static void DumpInterface(dex_ir::TypeId* type_item, int i) {
616 const char* interface_name = type_item->GetStringId()->Data();
617 if (options_.output_format_ == kOutputPlain) {
618 fprintf(out_file_, " #%d : '%s'\n", i, interface_name);
619 } else {
Jeff Haoc3acfc52016-08-29 14:18:26 -0700620 std::string dot(DescriptorToDotWrapper(interface_name));
David Sehr7629f602016-08-07 16:01:51 -0700621 fprintf(out_file_, "<implements name=\"%s\">\n</implements>\n", dot.c_str());
622 }
623}
624
625/*
626 * Dumps the catches table associated with the code.
627 */
628static void DumpCatches(const dex_ir::CodeItem* code) {
629 const uint16_t tries_size = code->TriesSize();
630
631 // No catch table.
632 if (tries_size == 0) {
633 fprintf(out_file_, " catches : (none)\n");
634 return;
635 }
636
637 // Dump all table entries.
638 fprintf(out_file_, " catches : %d\n", tries_size);
639 std::vector<std::unique_ptr<const dex_ir::TryItem>>* tries = code->Tries();
640 for (uint32_t i = 0; i < tries_size; i++) {
641 const dex_ir::TryItem* try_item = (*tries)[i].get();
642 const uint32_t start = try_item->StartAddr();
643 const uint32_t end = start + try_item->InsnCount();
644 fprintf(out_file_, " 0x%04x - 0x%04x\n", start, end);
645 for (auto& handler : try_item->GetHandlers()) {
646 const dex_ir::TypeId* type_id = handler->GetTypeId();
647 const char* descriptor = (type_id == nullptr) ? "<any>" : type_id->GetStringId()->Data();
648 fprintf(out_file_, " %s -> 0x%04x\n", descriptor, handler->GetAddress());
649 } // for
650 } // for
651}
652
653/*
654 * Dumps all positions table entries associated with the code.
655 */
656static void DumpPositionInfo(const dex_ir::CodeItem* code) {
657 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
658 if (debug_info == nullptr) {
659 return;
660 }
661 std::vector<std::unique_ptr<dex_ir::PositionInfo>>& positions = debug_info->GetPositionInfo();
662 for (size_t i = 0; i < positions.size(); ++i) {
663 fprintf(out_file_, " 0x%04x line=%d\n", positions[i]->address_, positions[i]->line_);
664 }
665}
666
667/*
668 * Dumps all locals table entries associated with the code.
669 */
670static void DumpLocalInfo(const dex_ir::CodeItem* code) {
671 dex_ir::DebugInfoItem* debug_info = code->DebugInfo();
672 if (debug_info == nullptr) {
673 return;
674 }
675 std::vector<std::unique_ptr<dex_ir::LocalInfo>>& locals = debug_info->GetLocalInfo();
676 for (size_t i = 0; i < locals.size(); ++i) {
677 dex_ir::LocalInfo* entry = locals[i].get();
678 fprintf(out_file_, " 0x%04x - 0x%04x reg=%d %s %s %s\n",
679 entry->start_address_, entry->end_address_, entry->reg_,
680 entry->name_.c_str(), entry->descriptor_.c_str(), entry->signature_.c_str());
681 }
682}
683
684/*
685 * Helper for dumpInstruction(), which builds the string
686 * representation for the index in the given instruction.
687 * Returns a pointer to a buffer of sufficient size.
688 */
689static std::unique_ptr<char[]> IndexString(dex_ir::Header* header,
690 const Instruction* dec_insn,
691 size_t buf_size) {
692 std::unique_ptr<char[]> buf(new char[buf_size]);
693 // Determine index and width of the string.
694 uint32_t index = 0;
695 uint32_t width = 4;
696 switch (Instruction::FormatOf(dec_insn->Opcode())) {
697 // SOME NOT SUPPORTED:
698 // case Instruction::k20bc:
699 case Instruction::k21c:
700 case Instruction::k35c:
701 // case Instruction::k35ms:
702 case Instruction::k3rc:
703 // case Instruction::k3rms:
704 // case Instruction::k35mi:
705 // case Instruction::k3rmi:
706 index = dec_insn->VRegB();
707 width = 4;
708 break;
709 case Instruction::k31c:
710 index = dec_insn->VRegB();
711 width = 8;
712 break;
713 case Instruction::k22c:
714 // case Instruction::k22cs:
715 index = dec_insn->VRegC();
716 width = 4;
717 break;
718 default:
719 break;
720 } // switch
721
722 // Determine index type.
723 size_t outSize = 0;
724 switch (Instruction::IndexTypeOf(dec_insn->Opcode())) {
725 case Instruction::kIndexUnknown:
726 // This function should never get called for this type, but do
727 // something sensible here, just to help with debugging.
728 outSize = snprintf(buf.get(), buf_size, "<unknown-index>");
729 break;
730 case Instruction::kIndexNone:
731 // This function should never get called for this type, but do
732 // something sensible here, just to help with debugging.
733 outSize = snprintf(buf.get(), buf_size, "<no-index>");
734 break;
735 case Instruction::kIndexTypeRef:
736 if (index < header->TypeIdsSize()) {
737 const char* tp = header->TypeIds()[index]->GetStringId()->Data();
738 outSize = snprintf(buf.get(), buf_size, "%s // type@%0*x", tp, width, index);
739 } else {
740 outSize = snprintf(buf.get(), buf_size, "<type?> // type@%0*x", width, index);
741 }
742 break;
743 case Instruction::kIndexStringRef:
744 if (index < header->StringIdsSize()) {
745 const char* st = header->StringIds()[index]->Data();
746 outSize = snprintf(buf.get(), buf_size, "\"%s\" // string@%0*x", st, width, index);
747 } else {
748 outSize = snprintf(buf.get(), buf_size, "<string?> // string@%0*x", width, index);
749 }
750 break;
751 case Instruction::kIndexMethodRef:
752 if (index < header->MethodIdsSize()) {
753 dex_ir::MethodId* method_id = header->MethodIds()[index].get();
754 const char* name = method_id->Name()->Data();
755 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
756 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
757 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // method@%0*x",
758 back_descriptor, name, type_descriptor, width, index);
759 } else {
760 outSize = snprintf(buf.get(), buf_size, "<method?> // method@%0*x", width, index);
761 }
762 break;
763 case Instruction::kIndexFieldRef:
764 if (index < header->FieldIdsSize()) {
765 dex_ir::FieldId* field_id = header->FieldIds()[index].get();
766 const char* name = field_id->Name()->Data();
767 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
768 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
769 outSize = snprintf(buf.get(), buf_size, "%s.%s:%s // field@%0*x",
770 back_descriptor, name, type_descriptor, width, index);
771 } else {
772 outSize = snprintf(buf.get(), buf_size, "<field?> // field@%0*x", width, index);
773 }
774 break;
775 case Instruction::kIndexVtableOffset:
776 outSize = snprintf(buf.get(), buf_size, "[%0*x] // vtable #%0*x",
777 width, index, width, index);
778 break;
779 case Instruction::kIndexFieldOffset:
780 outSize = snprintf(buf.get(), buf_size, "[obj+%0*x]", width, index);
781 break;
782 // SOME NOT SUPPORTED:
783 // case Instruction::kIndexVaries:
784 // case Instruction::kIndexInlineMethod:
785 default:
786 outSize = snprintf(buf.get(), buf_size, "<?>");
787 break;
788 } // switch
789
790 // Determine success of string construction.
791 if (outSize >= buf_size) {
792 // The buffer wasn't big enough; retry with computed size. Note: snprintf()
793 // doesn't count/ the '\0' as part of its returned size, so we add explicit
794 // space for it here.
795 return IndexString(header, dec_insn, outSize + 1);
796 }
797 return buf;
798}
799
800/*
801 * Dumps a single instruction.
802 */
803static void DumpInstruction(dex_ir::Header* header, const dex_ir::CodeItem* code,
804 uint32_t code_offset, uint32_t insn_idx, uint32_t insn_width,
805 const Instruction* dec_insn) {
806 // Address of instruction (expressed as byte offset).
807 fprintf(out_file_, "%06x:", code_offset + 0x10 + insn_idx * 2);
808
809 // Dump (part of) raw bytes.
810 const uint16_t* insns = code->Insns();
811 for (uint32_t i = 0; i < 8; i++) {
812 if (i < insn_width) {
813 if (i == 7) {
814 fprintf(out_file_, " ... ");
815 } else {
816 // Print 16-bit value in little-endian order.
817 const uint8_t* bytePtr = (const uint8_t*) &insns[insn_idx + i];
818 fprintf(out_file_, " %02x%02x", bytePtr[0], bytePtr[1]);
819 }
820 } else {
821 fputs(" ", out_file_);
822 }
823 } // for
824
825 // Dump pseudo-instruction or opcode.
826 if (dec_insn->Opcode() == Instruction::NOP) {
827 const uint16_t instr = Get2LE((const uint8_t*) &insns[insn_idx]);
828 if (instr == Instruction::kPackedSwitchSignature) {
829 fprintf(out_file_, "|%04x: packed-switch-data (%d units)", insn_idx, insn_width);
830 } else if (instr == Instruction::kSparseSwitchSignature) {
831 fprintf(out_file_, "|%04x: sparse-switch-data (%d units)", insn_idx, insn_width);
832 } else if (instr == Instruction::kArrayDataSignature) {
833 fprintf(out_file_, "|%04x: array-data (%d units)", insn_idx, insn_width);
834 } else {
835 fprintf(out_file_, "|%04x: nop // spacer", insn_idx);
836 }
837 } else {
838 fprintf(out_file_, "|%04x: %s", insn_idx, dec_insn->Name());
839 }
840
841 // Set up additional argument.
842 std::unique_ptr<char[]> index_buf;
843 if (Instruction::IndexTypeOf(dec_insn->Opcode()) != Instruction::kIndexNone) {
844 index_buf = IndexString(header, dec_insn, 200);
845 }
846
847 // Dump the instruction.
848 //
849 // NOTE: pDecInsn->DumpString(pDexFile) differs too much from original.
850 //
851 switch (Instruction::FormatOf(dec_insn->Opcode())) {
852 case Instruction::k10x: // op
853 break;
854 case Instruction::k12x: // op vA, vB
855 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
856 break;
857 case Instruction::k11n: // op vA, #+B
858 fprintf(out_file_, " v%d, #int %d // #%x",
859 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint8_t)dec_insn->VRegB());
860 break;
861 case Instruction::k11x: // op vAA
862 fprintf(out_file_, " v%d", dec_insn->VRegA());
863 break;
864 case Instruction::k10t: // op +AA
865 case Instruction::k20t: { // op +AAAA
866 const int32_t targ = (int32_t) dec_insn->VRegA();
867 fprintf(out_file_, " %04x // %c%04x",
868 insn_idx + targ,
869 (targ < 0) ? '-' : '+',
870 (targ < 0) ? -targ : targ);
871 break;
872 }
873 case Instruction::k22x: // op vAA, vBBBB
874 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
875 break;
876 case Instruction::k21t: { // op vAA, +BBBB
877 const int32_t targ = (int32_t) dec_insn->VRegB();
878 fprintf(out_file_, " v%d, %04x // %c%04x", dec_insn->VRegA(),
879 insn_idx + targ,
880 (targ < 0) ? '-' : '+',
881 (targ < 0) ? -targ : targ);
882 break;
883 }
884 case Instruction::k21s: // op vAA, #+BBBB
885 fprintf(out_file_, " v%d, #int %d // #%x",
886 dec_insn->VRegA(), (int32_t) dec_insn->VRegB(), (uint16_t)dec_insn->VRegB());
887 break;
888 case Instruction::k21h: // op vAA, #+BBBB0000[00000000]
889 // The printed format varies a bit based on the actual opcode.
890 if (dec_insn->Opcode() == Instruction::CONST_HIGH16) {
891 const int32_t value = dec_insn->VRegB() << 16;
892 fprintf(out_file_, " v%d, #int %d // #%x",
893 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
894 } else {
895 const int64_t value = ((int64_t) dec_insn->VRegB()) << 48;
896 fprintf(out_file_, " v%d, #long %" PRId64 " // #%x",
897 dec_insn->VRegA(), value, (uint16_t) dec_insn->VRegB());
898 }
899 break;
900 case Instruction::k21c: // op vAA, thing@BBBB
901 case Instruction::k31c: // op vAA, thing@BBBBBBBB
902 fprintf(out_file_, " v%d, %s", dec_insn->VRegA(), index_buf.get());
903 break;
904 case Instruction::k23x: // op vAA, vBB, vCC
905 fprintf(out_file_, " v%d, v%d, v%d",
906 dec_insn->VRegA(), dec_insn->VRegB(), dec_insn->VRegC());
907 break;
908 case Instruction::k22b: // op vAA, vBB, #+CC
909 fprintf(out_file_, " v%d, v%d, #int %d // #%02x",
910 dec_insn->VRegA(), dec_insn->VRegB(),
911 (int32_t) dec_insn->VRegC(), (uint8_t) dec_insn->VRegC());
912 break;
913 case Instruction::k22t: { // op vA, vB, +CCCC
914 const int32_t targ = (int32_t) dec_insn->VRegC();
915 fprintf(out_file_, " v%d, v%d, %04x // %c%04x",
916 dec_insn->VRegA(), dec_insn->VRegB(),
917 insn_idx + targ,
918 (targ < 0) ? '-' : '+',
919 (targ < 0) ? -targ : targ);
920 break;
921 }
922 case Instruction::k22s: // op vA, vB, #+CCCC
923 fprintf(out_file_, " v%d, v%d, #int %d // #%04x",
924 dec_insn->VRegA(), dec_insn->VRegB(),
925 (int32_t) dec_insn->VRegC(), (uint16_t) dec_insn->VRegC());
926 break;
927 case Instruction::k22c: // op vA, vB, thing@CCCC
928 // NOT SUPPORTED:
929 // case Instruction::k22cs: // [opt] op vA, vB, field offset CCCC
930 fprintf(out_file_, " v%d, v%d, %s",
931 dec_insn->VRegA(), dec_insn->VRegB(), index_buf.get());
932 break;
933 case Instruction::k30t:
934 fprintf(out_file_, " #%08x", dec_insn->VRegA());
935 break;
936 case Instruction::k31i: { // op vAA, #+BBBBBBBB
937 // This is often, but not always, a float.
938 union {
939 float f;
940 uint32_t i;
941 } conv;
942 conv.i = dec_insn->VRegB();
943 fprintf(out_file_, " v%d, #float %g // #%08x",
944 dec_insn->VRegA(), conv.f, dec_insn->VRegB());
945 break;
946 }
947 case Instruction::k31t: // op vAA, offset +BBBBBBBB
948 fprintf(out_file_, " v%d, %08x // +%08x",
949 dec_insn->VRegA(), insn_idx + dec_insn->VRegB(), dec_insn->VRegB());
950 break;
951 case Instruction::k32x: // op vAAAA, vBBBB
952 fprintf(out_file_, " v%d, v%d", dec_insn->VRegA(), dec_insn->VRegB());
953 break;
954 case Instruction::k35c: { // op {vC, vD, vE, vF, vG}, thing@BBBB
955 // NOT SUPPORTED:
956 // case Instruction::k35ms: // [opt] invoke-virtual+super
957 // case Instruction::k35mi: // [opt] inline invoke
958 uint32_t arg[Instruction::kMaxVarArgRegs];
959 dec_insn->GetVarArgs(arg);
960 fputs(" {", out_file_);
961 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
962 if (i == 0) {
963 fprintf(out_file_, "v%d", arg[i]);
964 } else {
965 fprintf(out_file_, ", v%d", arg[i]);
966 }
967 } // for
968 fprintf(out_file_, "}, %s", index_buf.get());
969 break;
970 }
971 case Instruction::k3rc: // op {vCCCC .. v(CCCC+AA-1)}, thing@BBBB
972 // NOT SUPPORTED:
973 // case Instruction::k3rms: // [opt] invoke-virtual+super/range
974 // case Instruction::k3rmi: // [opt] execute-inline/range
975 {
976 // This doesn't match the "dx" output when some of the args are
977 // 64-bit values -- dx only shows the first register.
978 fputs(" {", out_file_);
979 for (int i = 0, n = dec_insn->VRegA(); i < n; i++) {
980 if (i == 0) {
981 fprintf(out_file_, "v%d", dec_insn->VRegC() + i);
982 } else {
983 fprintf(out_file_, ", v%d", dec_insn->VRegC() + i);
984 }
985 } // for
986 fprintf(out_file_, "}, %s", index_buf.get());
987 }
988 break;
989 case Instruction::k51l: { // op vAA, #+BBBBBBBBBBBBBBBB
990 // This is often, but not always, a double.
991 union {
992 double d;
993 uint64_t j;
994 } conv;
995 conv.j = dec_insn->WideVRegB();
996 fprintf(out_file_, " v%d, #double %g // #%016" PRIx64,
997 dec_insn->VRegA(), conv.d, dec_insn->WideVRegB());
998 break;
999 }
1000 // NOT SUPPORTED:
1001 // case Instruction::k00x: // unknown op or breakpoint
1002 // break;
1003 default:
1004 fprintf(out_file_, " ???");
1005 break;
1006 } // switch
1007
1008 fputc('\n', out_file_);
1009}
1010
1011/*
1012 * Dumps a bytecode disassembly.
1013 */
1014static void DumpBytecodes(dex_ir::Header* header, uint32_t idx,
1015 const dex_ir::CodeItem* code, uint32_t code_offset) {
1016 dex_ir::MethodId* method_id = header->MethodIds()[idx].get();
1017 const char* name = method_id->Name()->Data();
1018 const char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1019 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1020
1021 // Generate header.
Jeff Haoc3acfc52016-08-29 14:18:26 -07001022 std::string dot(DescriptorToDotWrapper(back_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001023 fprintf(out_file_, "%06x: |[%06x] %s.%s:%s\n",
1024 code_offset, code_offset, dot.c_str(), name, type_descriptor);
1025
1026 // Iterate over all instructions.
1027 const uint16_t* insns = code->Insns();
1028 for (uint32_t insn_idx = 0; insn_idx < code->InsnsSize();) {
1029 const Instruction* instruction = Instruction::At(&insns[insn_idx]);
1030 const uint32_t insn_width = instruction->SizeInCodeUnits();
1031 if (insn_width == 0) {
1032 fprintf(stderr, "GLITCH: zero-width instruction at idx=0x%04x\n", insn_idx);
1033 break;
1034 }
1035 DumpInstruction(header, code, code_offset, insn_idx, insn_width, instruction);
1036 insn_idx += insn_width;
1037 } // for
1038}
1039
1040/*
1041 * Dumps code of a method.
1042 */
1043static void DumpCode(dex_ir::Header* header, uint32_t idx, const dex_ir::CodeItem* code,
1044 uint32_t code_offset) {
1045 fprintf(out_file_, " registers : %d\n", code->RegistersSize());
1046 fprintf(out_file_, " ins : %d\n", code->InsSize());
1047 fprintf(out_file_, " outs : %d\n", code->OutsSize());
1048 fprintf(out_file_, " insns size : %d 16-bit code units\n",
1049 code->InsnsSize());
1050
1051 // Bytecode disassembly, if requested.
1052 if (options_.disassemble_) {
1053 DumpBytecodes(header, idx, code, code_offset);
1054 }
1055
1056 // Try-catch blocks.
1057 DumpCatches(code);
1058
1059 // Positions and locals table in the debug info.
1060 fprintf(out_file_, " positions : \n");
1061 DumpPositionInfo(code);
1062 fprintf(out_file_, " locals : \n");
1063 DumpLocalInfo(code);
1064}
1065
1066/*
1067 * Dumps a method.
1068 */
1069static void DumpMethod(dex_ir::Header* header, uint32_t idx, uint32_t flags,
1070 const dex_ir::CodeItem* code, int i) {
1071 // Bail for anything private if export only requested.
1072 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1073 return;
1074 }
1075
1076 dex_ir::MethodId* method_id = header->MethodIds()[idx].get();
1077 const char* name = method_id->Name()->Data();
1078 char* type_descriptor = strdup(GetSignatureForProtoId(method_id->Proto()).c_str());
1079 const char* back_descriptor = method_id->Class()->GetStringId()->Data();
1080 char* access_str = CreateAccessFlagStr(flags, kAccessForMethod);
1081
1082 if (options_.output_format_ == kOutputPlain) {
1083 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1084 fprintf(out_file_, " name : '%s'\n", name);
1085 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1086 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1087 if (code == nullptr) {
1088 fprintf(out_file_, " code : (none)\n");
1089 } else {
1090 fprintf(out_file_, " code -\n");
1091 DumpCode(header, idx, code, code->GetOffset());
1092 }
1093 if (options_.disassemble_) {
1094 fputc('\n', out_file_);
1095 }
1096 } else if (options_.output_format_ == kOutputXml) {
1097 const bool constructor = (name[0] == '<');
1098
1099 // Method name and prototype.
1100 if (constructor) {
1101 std::string dot(DescriptorClassToDot(back_descriptor));
1102 fprintf(out_file_, "<constructor name=\"%s\"\n", dot.c_str());
Jeff Haoc3acfc52016-08-29 14:18:26 -07001103 dot = DescriptorToDotWrapper(back_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001104 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1105 } else {
1106 fprintf(out_file_, "<method name=\"%s\"\n", name);
1107 const char* return_type = strrchr(type_descriptor, ')');
1108 if (return_type == nullptr) {
1109 fprintf(stderr, "bad method type descriptor '%s'\n", type_descriptor);
1110 goto bail;
1111 }
Jeff Haoc3acfc52016-08-29 14:18:26 -07001112 std::string dot(DescriptorToDotWrapper(return_type + 1));
David Sehr7629f602016-08-07 16:01:51 -07001113 fprintf(out_file_, " return=\"%s\"\n", dot.c_str());
1114 fprintf(out_file_, " abstract=%s\n", QuotedBool((flags & kAccAbstract) != 0));
1115 fprintf(out_file_, " native=%s\n", QuotedBool((flags & kAccNative) != 0));
1116 fprintf(out_file_, " synchronized=%s\n", QuotedBool(
1117 (flags & (kAccSynchronized | kAccDeclaredSynchronized)) != 0));
1118 }
1119
1120 // Additional method flags.
1121 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1122 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1123 // The "deprecated=" not knowable w/o parsing annotations.
1124 fprintf(out_file_, " visibility=%s\n>\n", QuotedVisibility(flags));
1125
1126 // Parameters.
1127 if (type_descriptor[0] != '(') {
1128 fprintf(stderr, "ERROR: bad descriptor '%s'\n", type_descriptor);
1129 goto bail;
1130 }
1131 char* tmp_buf = reinterpret_cast<char*>(malloc(strlen(type_descriptor) + 1));
1132 const char* base = type_descriptor + 1;
1133 int arg_num = 0;
1134 while (*base != ')') {
1135 char* cp = tmp_buf;
1136 while (*base == '[') {
1137 *cp++ = *base++;
1138 }
1139 if (*base == 'L') {
1140 // Copy through ';'.
1141 do {
1142 *cp = *base++;
1143 } while (*cp++ != ';');
1144 } else {
1145 // Primitive char, copy it.
1146 if (strchr("ZBCSIFJD", *base) == nullptr) {
1147 fprintf(stderr, "ERROR: bad method signature '%s'\n", base);
1148 break; // while
1149 }
1150 *cp++ = *base++;
1151 }
1152 // Null terminate and display.
1153 *cp++ = '\0';
Jeff Haoc3acfc52016-08-29 14:18:26 -07001154 std::string dot(DescriptorToDotWrapper(tmp_buf));
David Sehr7629f602016-08-07 16:01:51 -07001155 fprintf(out_file_, "<parameter name=\"arg%d\" type=\"%s\">\n"
1156 "</parameter>\n", arg_num++, dot.c_str());
1157 } // while
1158 free(tmp_buf);
1159 if (constructor) {
1160 fprintf(out_file_, "</constructor>\n");
1161 } else {
1162 fprintf(out_file_, "</method>\n");
1163 }
1164 }
1165
1166 bail:
1167 free(type_descriptor);
1168 free(access_str);
1169}
1170
1171/*
1172 * Dumps a static (class) field.
1173 */
1174static void DumpSField(dex_ir::Header* header, uint32_t idx, uint32_t flags,
1175 int i, dex_ir::ArrayItem* init) {
1176 // Bail for anything private if export only requested.
1177 if (options_.exports_only_ && (flags & (kAccPublic | kAccProtected)) == 0) {
1178 return;
1179 }
1180
1181 dex_ir::FieldId* field_id = header->FieldIds()[idx].get();
1182 const char* name = field_id->Name()->Data();
1183 const char* type_descriptor = field_id->Type()->GetStringId()->Data();
1184 const char* back_descriptor = field_id->Class()->GetStringId()->Data();
1185 char* access_str = CreateAccessFlagStr(flags, kAccessForField);
1186
1187 if (options_.output_format_ == kOutputPlain) {
1188 fprintf(out_file_, " #%d : (in %s)\n", i, back_descriptor);
1189 fprintf(out_file_, " name : '%s'\n", name);
1190 fprintf(out_file_, " type : '%s'\n", type_descriptor);
1191 fprintf(out_file_, " access : 0x%04x (%s)\n", flags, access_str);
1192 if (init != nullptr) {
1193 fputs(" value : ", out_file_);
1194 DumpEncodedValue(init);
1195 fputs("\n", out_file_);
1196 }
1197 } else if (options_.output_format_ == kOutputXml) {
1198 fprintf(out_file_, "<field name=\"%s\"\n", name);
Jeff Haoc3acfc52016-08-29 14:18:26 -07001199 std::string dot(DescriptorToDotWrapper(type_descriptor));
David Sehr7629f602016-08-07 16:01:51 -07001200 fprintf(out_file_, " type=\"%s\"\n", dot.c_str());
1201 fprintf(out_file_, " transient=%s\n", QuotedBool((flags & kAccTransient) != 0));
1202 fprintf(out_file_, " volatile=%s\n", QuotedBool((flags & kAccVolatile) != 0));
1203 // The "value=" is not knowable w/o parsing annotations.
1204 fprintf(out_file_, " static=%s\n", QuotedBool((flags & kAccStatic) != 0));
1205 fprintf(out_file_, " final=%s\n", QuotedBool((flags & kAccFinal) != 0));
1206 // The "deprecated=" is not knowable w/o parsing annotations.
1207 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(flags));
1208 if (init != nullptr) {
1209 fputs(" value=\"", out_file_);
1210 DumpEncodedValue(init);
1211 fputs("\"\n", out_file_);
1212 }
1213 fputs(">\n</field>\n", out_file_);
1214 }
1215
1216 free(access_str);
1217}
1218
1219/*
1220 * Dumps an instance field.
1221 */
1222static void DumpIField(dex_ir::Header* header, uint32_t idx, uint32_t flags, int i) {
1223 DumpSField(header, idx, flags, i, nullptr);
1224}
1225
1226/*
1227 * Dumping a CFG. Note that this will do duplicate work. utils.h doesn't expose the code-item
1228 * version, so the DumpMethodCFG code will have to iterate again to find it. But dexdump is a
1229 * tool, so this is not performance-critical.
1230 */
1231
1232static void DumpCFG(const DexFile* dex_file,
1233 uint32_t dex_method_idx,
1234 const DexFile::CodeItem* code) {
1235 if (code != nullptr) {
1236 std::ostringstream oss;
1237 DumpMethodCFG(dex_file, dex_method_idx, oss);
1238 fprintf(out_file_, "%s", oss.str().c_str());
1239 }
1240}
1241
1242static void DumpCFG(const DexFile* dex_file, int idx) {
1243 const DexFile::ClassDef& class_def = dex_file->GetClassDef(idx);
1244 const uint8_t* class_data = dex_file->GetClassData(class_def);
1245 if (class_data == nullptr) { // empty class such as a marker interface?
1246 return;
1247 }
1248 ClassDataItemIterator it(*dex_file, class_data);
1249 while (it.HasNextStaticField()) {
1250 it.Next();
1251 }
1252 while (it.HasNextInstanceField()) {
1253 it.Next();
1254 }
1255 while (it.HasNextDirectMethod()) {
1256 DumpCFG(dex_file,
1257 it.GetMemberIndex(),
1258 it.GetMethodCodeItem());
1259 it.Next();
1260 }
1261 while (it.HasNextVirtualMethod()) {
1262 DumpCFG(dex_file,
1263 it.GetMemberIndex(),
1264 it.GetMethodCodeItem());
1265 it.Next();
1266 }
1267}
1268
1269/*
1270 * Dumps the class.
1271 *
1272 * Note "idx" is a DexClassDef index, not a DexTypeId index.
1273 *
1274 * If "*last_package" is nullptr or does not match the current class' package,
1275 * the value will be replaced with a newly-allocated string.
1276 */
1277static void DumpClass(dex_ir::Header* header, int idx, char** last_package) {
1278 dex_ir::ClassDef* class_def = header->ClassDefs()[idx].get();
1279 // Omitting non-public class.
1280 if (options_.exports_only_ && (class_def->GetAccessFlags() & kAccPublic) == 0) {
1281 return;
1282 }
1283
1284 if (options_.show_section_headers_) {
1285 DumpClassDef(header, idx);
1286 }
1287
1288 if (options_.show_annotations_) {
1289 DumpClassAnnotations(header, idx);
1290 }
1291
1292 if (options_.show_cfg_) {
1293 DumpCFG(&header->GetDexFile(), idx);
1294 return;
1295 }
1296
1297 // For the XML output, show the package name. Ideally we'd gather
1298 // up the classes, sort them, and dump them alphabetically so the
1299 // package name wouldn't jump around, but that's not a great plan
1300 // for something that needs to run on the device.
1301 const char* class_descriptor = header->ClassDefs()[idx]->ClassType()->GetStringId()->Data();
1302 if (!(class_descriptor[0] == 'L' &&
1303 class_descriptor[strlen(class_descriptor)-1] == ';')) {
1304 // Arrays and primitives should not be defined explicitly. Keep going?
1305 fprintf(stderr, "Malformed class name '%s'\n", class_descriptor);
1306 } else if (options_.output_format_ == kOutputXml) {
1307 char* mangle = strdup(class_descriptor + 1);
1308 mangle[strlen(mangle)-1] = '\0';
1309
1310 // Reduce to just the package name.
1311 char* last_slash = strrchr(mangle, '/');
1312 if (last_slash != nullptr) {
1313 *last_slash = '\0';
1314 } else {
1315 *mangle = '\0';
1316 }
1317
1318 for (char* cp = mangle; *cp != '\0'; cp++) {
1319 if (*cp == '/') {
1320 *cp = '.';
1321 }
1322 } // for
1323
1324 if (*last_package == nullptr || strcmp(mangle, *last_package) != 0) {
1325 // Start of a new package.
1326 if (*last_package != nullptr) {
1327 fprintf(out_file_, "</package>\n");
1328 }
1329 fprintf(out_file_, "<package name=\"%s\"\n>\n", mangle);
1330 free(*last_package);
1331 *last_package = mangle;
1332 } else {
1333 free(mangle);
1334 }
1335 }
1336
1337 // General class information.
1338 char* access_str = CreateAccessFlagStr(class_def->GetAccessFlags(), kAccessForClass);
1339 const char* superclass_descriptor = nullptr;
1340 if (class_def->Superclass() != nullptr) {
1341 superclass_descriptor = class_def->Superclass()->GetStringId()->Data();
1342 }
1343 if (options_.output_format_ == kOutputPlain) {
1344 fprintf(out_file_, "Class #%d -\n", idx);
1345 fprintf(out_file_, " Class descriptor : '%s'\n", class_descriptor);
1346 fprintf(out_file_, " Access flags : 0x%04x (%s)\n",
1347 class_def->GetAccessFlags(), access_str);
1348 if (superclass_descriptor != nullptr) {
1349 fprintf(out_file_, " Superclass : '%s'\n", superclass_descriptor);
1350 }
1351 fprintf(out_file_, " Interfaces -\n");
1352 } else {
1353 std::string dot(DescriptorClassToDot(class_descriptor));
1354 fprintf(out_file_, "<class name=\"%s\"\n", dot.c_str());
1355 if (superclass_descriptor != nullptr) {
Jeff Haoc3acfc52016-08-29 14:18:26 -07001356 dot = DescriptorToDotWrapper(superclass_descriptor);
David Sehr7629f602016-08-07 16:01:51 -07001357 fprintf(out_file_, " extends=\"%s\"\n", dot.c_str());
1358 }
1359 fprintf(out_file_, " interface=%s\n",
1360 QuotedBool((class_def->GetAccessFlags() & kAccInterface) != 0));
1361 fprintf(out_file_, " abstract=%s\n",
1362 QuotedBool((class_def->GetAccessFlags() & kAccAbstract) != 0));
1363 fprintf(out_file_, " static=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccStatic) != 0));
1364 fprintf(out_file_, " final=%s\n", QuotedBool((class_def->GetAccessFlags() & kAccFinal) != 0));
1365 // The "deprecated=" not knowable w/o parsing annotations.
1366 fprintf(out_file_, " visibility=%s\n", QuotedVisibility(class_def->GetAccessFlags()));
1367 fprintf(out_file_, ">\n");
1368 }
1369
1370 // Interfaces.
1371 std::vector<dex_ir::TypeId*>* interfaces = class_def->Interfaces();
1372 for (uint32_t i = 0; i < interfaces->size(); i++) {
1373 DumpInterface((*interfaces)[i], i);
1374 } // for
1375
1376 // Fields and methods.
1377 dex_ir::ClassData* class_data = class_def->GetClassData();
1378 // Prepare data for static fields.
1379 std::vector<std::unique_ptr<dex_ir::ArrayItem>>* static_values = class_def->StaticValues();
1380 const uint32_t static_values_size = (static_values == nullptr) ? 0 : static_values->size();
1381
1382 // Static fields.
1383 if (options_.output_format_ == kOutputPlain) {
1384 fprintf(out_file_, " Static fields -\n");
1385 }
1386 std::vector<std::unique_ptr<dex_ir::FieldItem>>& static_fields = class_data->StaticFields();
1387 for (uint32_t i = 0; i < static_fields.size(); i++) {
1388 DumpSField(header,
1389 static_fields[i]->GetFieldId()->GetOffset(),
1390 static_fields[i]->GetAccessFlags(),
1391 i,
1392 i < static_values_size ? (*static_values)[i].get() : nullptr);
1393 } // for
1394
1395 // Instance fields.
1396 if (options_.output_format_ == kOutputPlain) {
1397 fprintf(out_file_, " Instance fields -\n");
1398 }
1399 std::vector<std::unique_ptr<dex_ir::FieldItem>>& instance_fields = class_data->InstanceFields();
1400 for (uint32_t i = 0; i < instance_fields.size(); i++) {
1401 DumpIField(header,
1402 instance_fields[i]->GetFieldId()->GetOffset(),
1403 instance_fields[i]->GetAccessFlags(),
1404 i);
1405 } // for
1406
1407 // Direct methods.
1408 if (options_.output_format_ == kOutputPlain) {
1409 fprintf(out_file_, " Direct methods -\n");
1410 }
1411 std::vector<std::unique_ptr<dex_ir::MethodItem>>& direct_methods = class_data->DirectMethods();
1412 for (uint32_t i = 0; i < direct_methods.size(); i++) {
1413 DumpMethod(header,
1414 direct_methods[i]->GetMethodId()->GetOffset(),
1415 direct_methods[i]->GetAccessFlags(),
1416 direct_methods[i]->GetCodeItem(),
1417 i);
1418 } // for
1419
1420 // Virtual methods.
1421 if (options_.output_format_ == kOutputPlain) {
1422 fprintf(out_file_, " Virtual methods -\n");
1423 }
1424 std::vector<std::unique_ptr<dex_ir::MethodItem>>& virtual_methods = class_data->VirtualMethods();
1425 for (uint32_t i = 0; i < virtual_methods.size(); i++) {
1426 DumpMethod(header,
1427 virtual_methods[i]->GetMethodId()->GetOffset(),
1428 virtual_methods[i]->GetAccessFlags(),
1429 virtual_methods[i]->GetCodeItem(),
1430 i);
1431 } // for
1432
1433 // End of class.
1434 if (options_.output_format_ == kOutputPlain) {
1435 const char* file_name = "unknown";
1436 if (class_def->SourceFile() != nullptr) {
1437 file_name = class_def->SourceFile()->Data();
1438 }
1439 const dex_ir::StringId* source_file = class_def->SourceFile();
1440 fprintf(out_file_, " source_file_idx : %d (%s)\n\n",
1441 source_file == nullptr ? 0xffffffffU : source_file->GetOffset(), file_name);
1442 } else if (options_.output_format_ == kOutputXml) {
1443 fprintf(out_file_, "</class>\n");
1444 }
1445
1446 free(access_str);
1447}
1448
1449/*
1450 * Dumps the requested sections of the file.
1451 */
1452static void ProcessDexFile(const char* file_name, const DexFile* dex_file) {
1453 if (options_.verbose_) {
1454 fprintf(out_file_, "Opened '%s', DEX version '%.3s'\n",
1455 file_name, dex_file->GetHeader().magic_ + 4);
1456 }
1457 dex_ir::Header header(*dex_file);
1458
1459 // Headers.
1460 if (options_.show_file_headers_) {
1461 DumpFileHeader(&header);
1462 }
1463
1464 // Open XML context.
1465 if (options_.output_format_ == kOutputXml) {
1466 fprintf(out_file_, "<api>\n");
1467 }
1468
1469 // Iterate over all classes.
1470 char* package = nullptr;
1471 const uint32_t class_defs_size = header.ClassDefsSize();
1472 for (uint32_t i = 0; i < class_defs_size; i++) {
1473 DumpClass(&header, i, &package);
1474 } // for
1475
1476 // Free the last package allocated.
1477 if (package != nullptr) {
1478 fprintf(out_file_, "</package>\n");
1479 free(package);
1480 }
1481
1482 // Close XML context.
1483 if (options_.output_format_ == kOutputXml) {
1484 fprintf(out_file_, "</api>\n");
1485 }
1486}
1487
1488/*
1489 * Processes a single file (either direct .dex or indirect .zip/.jar/.apk).
1490 */
1491int ProcessFile(const char* file_name) {
1492 if (options_.verbose_) {
1493 fprintf(out_file_, "Processing '%s'...\n", file_name);
1494 }
1495
1496 // If the file is not a .dex file, the function tries .zip/.jar/.apk files,
1497 // all of which are Zip archives with "classes.dex" inside.
1498 const bool verify_checksum = !options_.ignore_bad_checksum_;
1499 std::string error_msg;
1500 std::vector<std::unique_ptr<const DexFile>> dex_files;
1501 if (!DexFile::Open(file_name, file_name, verify_checksum, &error_msg, &dex_files)) {
1502 // Display returned error message to user. Note that this error behavior
1503 // differs from the error messages shown by the original Dalvik dexdump.
1504 fputs(error_msg.c_str(), stderr);
1505 fputc('\n', stderr);
1506 return -1;
1507 }
1508
1509 // Success. Either report checksum verification or process
1510 // all dex files found in given file.
1511 if (options_.checksum_only_) {
1512 fprintf(out_file_, "Checksum verified\n");
1513 } else {
1514 for (size_t i = 0; i < dex_files.size(); i++) {
1515 ProcessDexFile(file_name, dex_files[i].get());
1516 }
1517 }
1518 return 0;
1519}
1520
1521} // namespace art