LocalUnwinder.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright (C) 2018 The Android Open Source Project
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * * Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in
  12. * the documentation and/or other materials provided with the
  13. * distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  18. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  19. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  21. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  22. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  23. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  24. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  25. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  26. * SUCH DAMAGE.
  27. */
  28. #include <pthread.h>
  29. #include <stdint.h>
  30. #include <memory>
  31. #include <string>
  32. #include <vector>
  33. #include <unwindstack/Elf.h>
  34. #include <unwindstack/LocalUnwinder.h>
  35. #include <unwindstack/MapInfo.h>
  36. #include <unwindstack/Maps.h>
  37. #include <unwindstack/Memory.h>
  38. #include <unwindstack/Regs.h>
  39. #include <unwindstack/RegsGetLocal.h>
  40. namespace unwindstack {
  41. bool LocalUnwinder::Init() {
  42. pthread_rwlock_init(&maps_rwlock_, nullptr);
  43. // Create the maps.
  44. maps_.reset(new unwindstack::LocalUpdatableMaps());
  45. if (!maps_->Parse()) {
  46. maps_.reset();
  47. return false;
  48. }
  49. process_memory_ = unwindstack::Memory::CreateProcessMemory(getpid());
  50. return true;
  51. }
  52. bool LocalUnwinder::ShouldSkipLibrary(const std::string& map_name) {
  53. for (const std::string& skip_library : skip_libraries_) {
  54. if (skip_library == map_name) {
  55. return true;
  56. }
  57. }
  58. return false;
  59. }
  60. MapInfo* LocalUnwinder::GetMapInfo(uint64_t pc) {
  61. pthread_rwlock_rdlock(&maps_rwlock_);
  62. MapInfo* map_info = maps_->Find(pc);
  63. pthread_rwlock_unlock(&maps_rwlock_);
  64. if (map_info == nullptr) {
  65. pthread_rwlock_wrlock(&maps_rwlock_);
  66. // This is guaranteed not to invalidate any previous MapInfo objects so
  67. // we don't need to worry about any MapInfo* values already in use.
  68. if (maps_->Reparse()) {
  69. map_info = maps_->Find(pc);
  70. }
  71. pthread_rwlock_unlock(&maps_rwlock_);
  72. }
  73. return map_info;
  74. }
  75. bool LocalUnwinder::Unwind(std::vector<LocalFrameData>* frame_info, size_t max_frames) {
  76. std::unique_ptr<unwindstack::Regs> regs(unwindstack::Regs::CreateFromLocal());
  77. unwindstack::RegsGetLocal(regs.get());
  78. ArchEnum arch = regs->Arch();
  79. size_t num_frames = 0;
  80. bool adjust_pc = false;
  81. while (true) {
  82. uint64_t cur_pc = regs->pc();
  83. uint64_t cur_sp = regs->sp();
  84. MapInfo* map_info = GetMapInfo(cur_pc);
  85. if (map_info == nullptr) {
  86. break;
  87. }
  88. Elf* elf = map_info->GetElf(process_memory_, arch);
  89. uint64_t rel_pc = elf->GetRelPc(cur_pc, map_info);
  90. uint64_t step_pc = rel_pc;
  91. uint64_t pc_adjustment;
  92. if (adjust_pc) {
  93. pc_adjustment = regs->GetPcAdjustment(rel_pc, elf);
  94. } else {
  95. pc_adjustment = 0;
  96. }
  97. step_pc -= pc_adjustment;
  98. bool finished = false;
  99. if (elf->StepIfSignalHandler(rel_pc, regs.get(), process_memory_.get())) {
  100. step_pc = rel_pc;
  101. } else if (!elf->Step(step_pc, regs.get(), process_memory_.get(), &finished)) {
  102. finished = true;
  103. }
  104. // Skip any locations that are within this library.
  105. if (num_frames != 0 || !ShouldSkipLibrary(map_info->name)) {
  106. // Add frame information.
  107. std::string func_name;
  108. uint64_t func_offset;
  109. if (elf->GetFunctionName(rel_pc, &func_name, &func_offset)) {
  110. frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment,
  111. func_name, func_offset);
  112. } else {
  113. frame_info->emplace_back(map_info, cur_pc - pc_adjustment, rel_pc - pc_adjustment, "", 0);
  114. }
  115. num_frames++;
  116. }
  117. if (finished || frame_info->size() == max_frames ||
  118. (cur_pc == regs->pc() && cur_sp == regs->sp())) {
  119. break;
  120. }
  121. adjust_pc = true;
  122. }
  123. return num_frames != 0;
  124. }
  125. } // namespace unwindstack