DexFiles.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2018 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. #include <stdint.h>
  17. #include <sys/mman.h>
  18. #include <sys/stat.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <memory>
  22. #include <unwindstack/DexFiles.h>
  23. #include <unwindstack/MapInfo.h>
  24. #include <unwindstack/Maps.h>
  25. #include <unwindstack/Memory.h>
  26. #include "DexFile.h"
  27. namespace unwindstack {
  28. struct DEXFileEntry32 {
  29. uint32_t next;
  30. uint32_t prev;
  31. uint32_t dex_file;
  32. };
  33. struct DEXFileEntry64 {
  34. uint64_t next;
  35. uint64_t prev;
  36. uint64_t dex_file;
  37. };
  38. DexFiles::DexFiles(std::shared_ptr<Memory>& memory) : Global(memory) {}
  39. DexFiles::DexFiles(std::shared_ptr<Memory>& memory, std::vector<std::string>& search_libs)
  40. : Global(memory, search_libs) {}
  41. DexFiles::~DexFiles() {}
  42. void DexFiles::ProcessArch() {
  43. switch (arch()) {
  44. case ARCH_ARM:
  45. case ARCH_MIPS:
  46. case ARCH_X86:
  47. read_entry_ptr_func_ = &DexFiles::ReadEntryPtr32;
  48. read_entry_func_ = &DexFiles::ReadEntry32;
  49. break;
  50. case ARCH_ARM64:
  51. case ARCH_MIPS64:
  52. case ARCH_X86_64:
  53. read_entry_ptr_func_ = &DexFiles::ReadEntryPtr64;
  54. read_entry_func_ = &DexFiles::ReadEntry64;
  55. break;
  56. case ARCH_UNKNOWN:
  57. abort();
  58. }
  59. }
  60. uint64_t DexFiles::ReadEntryPtr32(uint64_t addr) {
  61. uint32_t entry;
  62. const uint32_t field_offset = 12; // offset of first_entry_ in the descriptor struct.
  63. if (!memory_->ReadFully(addr + field_offset, &entry, sizeof(entry))) {
  64. return 0;
  65. }
  66. return entry;
  67. }
  68. uint64_t DexFiles::ReadEntryPtr64(uint64_t addr) {
  69. uint64_t entry;
  70. const uint32_t field_offset = 16; // offset of first_entry_ in the descriptor struct.
  71. if (!memory_->ReadFully(addr + field_offset, &entry, sizeof(entry))) {
  72. return 0;
  73. }
  74. return entry;
  75. }
  76. bool DexFiles::ReadEntry32() {
  77. DEXFileEntry32 entry;
  78. if (!memory_->ReadFully(entry_addr_, &entry, sizeof(entry)) || entry.dex_file == 0) {
  79. entry_addr_ = 0;
  80. return false;
  81. }
  82. addrs_.push_back(entry.dex_file);
  83. entry_addr_ = entry.next;
  84. return true;
  85. }
  86. bool DexFiles::ReadEntry64() {
  87. DEXFileEntry64 entry;
  88. if (!memory_->ReadFully(entry_addr_, &entry, sizeof(entry)) || entry.dex_file == 0) {
  89. entry_addr_ = 0;
  90. return false;
  91. }
  92. addrs_.push_back(entry.dex_file);
  93. entry_addr_ = entry.next;
  94. return true;
  95. }
  96. bool DexFiles::ReadVariableData(uint64_t ptr_offset) {
  97. entry_addr_ = (this->*read_entry_ptr_func_)(ptr_offset);
  98. return entry_addr_ != 0;
  99. }
  100. void DexFiles::Init(Maps* maps) {
  101. if (initialized_) {
  102. return;
  103. }
  104. initialized_ = true;
  105. entry_addr_ = 0;
  106. FindAndReadVariable(maps, "__dex_debug_descriptor");
  107. }
  108. DexFile* DexFiles::GetDexFile(uint64_t dex_file_offset, MapInfo* info) {
  109. // Lock while processing the data.
  110. DexFile* dex_file;
  111. auto entry = files_.find(dex_file_offset);
  112. if (entry == files_.end()) {
  113. std::unique_ptr<DexFile> new_dex_file = DexFile::Create(dex_file_offset, memory_.get(), info);
  114. dex_file = new_dex_file.get();
  115. files_[dex_file_offset] = std::move(new_dex_file);
  116. } else {
  117. dex_file = entry->second.get();
  118. }
  119. return dex_file;
  120. }
  121. bool DexFiles::GetAddr(size_t index, uint64_t* addr) {
  122. if (index < addrs_.size()) {
  123. *addr = addrs_[index];
  124. return true;
  125. }
  126. if (entry_addr_ != 0 && (this->*read_entry_func_)()) {
  127. *addr = addrs_.back();
  128. return true;
  129. }
  130. return false;
  131. }
  132. void DexFiles::GetMethodInformation(Maps* maps, MapInfo* info, uint64_t dex_pc,
  133. std::string* method_name, uint64_t* method_offset) {
  134. std::lock_guard<std::mutex> guard(lock_);
  135. if (!initialized_) {
  136. Init(maps);
  137. }
  138. size_t index = 0;
  139. uint64_t addr;
  140. while (GetAddr(index++, &addr)) {
  141. if (addr < info->start || addr >= info->end) {
  142. continue;
  143. }
  144. DexFile* dex_file = GetDexFile(addr, info);
  145. if (dex_file != nullptr &&
  146. dex_file->GetMethodInformation(dex_pc - addr, method_name, method_offset)) {
  147. break;
  148. }
  149. }
  150. }
  151. } // namespace unwindstack