DexFile.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <android-base/unique_fd.h>
  23. #include <art_api/dex_file_support.h>
  24. #include <unwindstack/MapInfo.h>
  25. #include <unwindstack/Memory.h>
  26. #include "DexFile.h"
  27. namespace unwindstack {
  28. std::unique_ptr<DexFile> DexFile::Create(uint64_t dex_file_offset_in_memory, Memory* memory,
  29. MapInfo* info) {
  30. if (!info->name.empty()) {
  31. std::unique_ptr<DexFile> dex_file =
  32. DexFileFromFile::Create(dex_file_offset_in_memory - info->start + info->offset, info->name);
  33. if (dex_file) {
  34. return dex_file;
  35. }
  36. }
  37. return DexFileFromMemory::Create(dex_file_offset_in_memory, memory, info->name);
  38. }
  39. bool DexFile::GetMethodInformation(uint64_t dex_offset, std::string* method_name,
  40. uint64_t* method_offset) {
  41. art_api::dex::MethodInfo method_info = GetMethodInfoForOffset(dex_offset, false);
  42. if (method_info.offset == 0) {
  43. return false;
  44. }
  45. *method_name = method_info.name;
  46. *method_offset = dex_offset - method_info.offset;
  47. return true;
  48. }
  49. std::unique_ptr<DexFileFromFile> DexFileFromFile::Create(uint64_t dex_file_offset_in_file,
  50. const std::string& file) {
  51. android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(file.c_str(), O_RDONLY | O_CLOEXEC)));
  52. if (fd == -1) {
  53. return nullptr;
  54. }
  55. std::string error_msg;
  56. std::unique_ptr<art_api::dex::DexFile> art_dex_file =
  57. OpenFromFd(fd, dex_file_offset_in_file, file, &error_msg);
  58. if (art_dex_file == nullptr) {
  59. return nullptr;
  60. }
  61. return std::unique_ptr<DexFileFromFile>(new DexFileFromFile(std::move(*art_dex_file.release())));
  62. }
  63. std::unique_ptr<DexFileFromMemory> DexFileFromMemory::Create(uint64_t dex_file_offset_in_memory,
  64. Memory* memory,
  65. const std::string& name) {
  66. std::vector<uint8_t> backing_memory;
  67. for (size_t size = 0;;) {
  68. std::string error_msg;
  69. std::unique_ptr<art_api::dex::DexFile> art_dex_file =
  70. OpenFromMemory(backing_memory.data(), &size, name, &error_msg);
  71. if (art_dex_file != nullptr) {
  72. return std::unique_ptr<DexFileFromMemory>(
  73. new DexFileFromMemory(std::move(*art_dex_file.release()), std::move(backing_memory)));
  74. }
  75. if (!error_msg.empty()) {
  76. return nullptr;
  77. }
  78. backing_memory.resize(size);
  79. if (!memory->ReadFully(dex_file_offset_in_memory, backing_memory.data(),
  80. backing_memory.size())) {
  81. return nullptr;
  82. }
  83. }
  84. }
  85. } // namespace unwindstack