UnwindMap.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) 2014 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 <pthread.h>
  17. #include <stdint.h>
  18. #include <stdlib.h>
  19. #include <sys/types.h>
  20. #include <unistd.h>
  21. #include <backtrace/BacktraceMap.h>
  22. #include <libunwind.h>
  23. #include "BacktraceLog.h"
  24. #include "UnwindMap.h"
  25. //-------------------------------------------------------------------------
  26. // libunwind has a single shared address space for the current process
  27. // aka local. If multiple maps are created for the current pid, then
  28. // only update the local address space once, and keep a reference count
  29. // of maps using the same map cursor.
  30. //-------------------------------------------------------------------------
  31. UnwindMap::UnwindMap(pid_t pid) : BacktraceMap(pid) {
  32. unw_map_cursor_clear(&map_cursor_);
  33. }
  34. UnwindMapRemote::UnwindMapRemote(pid_t pid) : UnwindMap(pid) {
  35. }
  36. UnwindMapRemote::~UnwindMapRemote() {
  37. unw_map_cursor_destroy(&map_cursor_);
  38. unw_map_cursor_clear(&map_cursor_);
  39. }
  40. bool UnwindMapRemote::GenerateMap() {
  41. // Use the map_cursor information to construct the BacktraceMap data
  42. // rather than reparsing /proc/self/maps.
  43. unw_map_cursor_reset(&map_cursor_);
  44. unw_map_t unw_map;
  45. while (unw_map_cursor_get_next(&map_cursor_, &unw_map)) {
  46. backtrace_map_t map;
  47. map.start = unw_map.start;
  48. map.end = unw_map.end;
  49. map.offset = unw_map.offset;
  50. map.load_bias = unw_map.load_base;
  51. map.flags = unw_map.flags;
  52. map.name = unw_map.path;
  53. // The maps are in descending order, but we want them in ascending order.
  54. maps_.push_front(map);
  55. }
  56. return true;
  57. }
  58. bool UnwindMapRemote::Build() {
  59. return (unw_map_cursor_create(&map_cursor_, pid_) == 0) && GenerateMap();
  60. }
  61. UnwindMapLocal::UnwindMapLocal() : UnwindMap(getpid()), map_created_(false) {
  62. pthread_rwlock_init(&map_lock_, nullptr);
  63. }
  64. UnwindMapLocal::~UnwindMapLocal() {
  65. if (map_created_) {
  66. unw_map_local_destroy();
  67. unw_map_cursor_clear(&map_cursor_);
  68. }
  69. }
  70. bool UnwindMapLocal::GenerateMap() {
  71. // Lock so that multiple threads cannot modify the maps data at the
  72. // same time.
  73. pthread_rwlock_wrlock(&map_lock_);
  74. // It's possible for the map to be regenerated while this loop is occurring.
  75. // If that happens, get the map again, but only try at most three times
  76. // before giving up.
  77. bool generated = false;
  78. for (int i = 0; i < 3; i++) {
  79. maps_.clear();
  80. // Save the map data retrieved so we can tell if it changes.
  81. unw_map_local_cursor_get(&map_cursor_);
  82. unw_map_t unw_map;
  83. int ret;
  84. while ((ret = unw_map_local_cursor_get_next(&map_cursor_, &unw_map)) > 0) {
  85. backtrace_map_t map;
  86. map.start = unw_map.start;
  87. map.end = unw_map.end;
  88. map.offset = unw_map.offset;
  89. map.load_bias = unw_map.load_base;
  90. map.flags = unw_map.flags;
  91. map.name = unw_map.path;
  92. free(unw_map.path);
  93. // The maps are in descending order, but we want them in ascending order.
  94. maps_.push_front(map);
  95. }
  96. // Check to see if the map changed while getting the data.
  97. if (ret != -UNW_EINVAL) {
  98. generated = true;
  99. break;
  100. }
  101. }
  102. pthread_rwlock_unlock(&map_lock_);
  103. if (!generated) {
  104. BACK_LOGW("Unable to generate the map.");
  105. }
  106. return generated;
  107. }
  108. bool UnwindMapLocal::Build() {
  109. return (map_created_ = (unw_map_local_create() == 0)) && GenerateMap();;
  110. }
  111. void UnwindMapLocal::FillIn(uint64_t addr, backtrace_map_t* map) {
  112. BacktraceMap::FillIn(addr, map);
  113. if (!IsValid(*map)) {
  114. // Check to see if the underlying map changed and regenerate the map
  115. // if it did.
  116. if (unw_map_local_cursor_valid(&map_cursor_) < 0) {
  117. if (GenerateMap()) {
  118. BacktraceMap::FillIn(addr, map);
  119. }
  120. }
  121. }
  122. }