BacktraceMap.cpp 4.1 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. #define LOG_TAG "backtrace-map"
  17. #include <ctype.h>
  18. #include <inttypes.h>
  19. #include <stdint.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <log/log.h>
  23. #include <android-base/stringprintf.h>
  24. #include <backtrace/Backtrace.h>
  25. #include <backtrace/BacktraceMap.h>
  26. #include <backtrace/backtrace_constants.h>
  27. #if defined(__linux__)
  28. #include <procinfo/process_map.h>
  29. #endif
  30. using android::base::StringPrintf;
  31. std::string backtrace_map_t::Name() const {
  32. if (!name.empty()) return name;
  33. if (start == 0 && end == 0) return "";
  34. return StringPrintf("<anonymous:%" PRIPTR ">", start);
  35. }
  36. BacktraceMap::BacktraceMap(pid_t pid) : pid_(pid) {
  37. if (pid_ < 0) {
  38. pid_ = getpid();
  39. }
  40. }
  41. BacktraceMap::~BacktraceMap() {}
  42. void BacktraceMap::FillIn(uint64_t addr, backtrace_map_t* map) {
  43. ScopedBacktraceMapIteratorLock lock(this);
  44. for (auto it = begin(); it != end(); ++it) {
  45. const backtrace_map_t* entry = *it;
  46. if (addr >= entry->start && addr < entry->end) {
  47. *map = *entry;
  48. return;
  49. }
  50. }
  51. *map = {};
  52. }
  53. #if defined(__APPLE__)
  54. static bool ParseLine(const char* line, backtrace_map_t* map) {
  55. uint64_t start;
  56. uint64_t end;
  57. char permissions[5];
  58. int name_pos;
  59. // Mac OS vmmap(1) output:
  60. // __TEXT 0009f000-000a1000 [ 8K 8K] r-x/rwx SM=COW
  61. // /Volumes/android/dalvik-dev/out/host/darwin-x86/bin/libcorkscrew_test\n
  62. // 012345678901234567890123456789012345678901234567890123456789
  63. // 0 1 2 3 4 5
  64. if (sscanf(line, "%*21c %" SCNx64 "-%" SCNx64 " [%*13c] %3c/%*3c SM=%*3c %n", &start, &end,
  65. permissions, &name_pos) != 3) {
  66. return false;
  67. }
  68. map->start = start;
  69. map->end = end;
  70. map->flags = PROT_NONE;
  71. if (permissions[0] == 'r') {
  72. map->flags |= PROT_READ;
  73. }
  74. if (permissions[1] == 'w') {
  75. map->flags |= PROT_WRITE;
  76. }
  77. if (permissions[2] == 'x') {
  78. map->flags |= PROT_EXEC;
  79. }
  80. map->name = line + name_pos;
  81. if (!map->name.empty() && map->name[map->name.length() - 1] == '\n') {
  82. map->name.erase(map->name.length() - 1);
  83. }
  84. ALOGV("Parsed map: start=%p, end=%p, flags=%x, name=%s", reinterpret_cast<void*>(map->start),
  85. reinterpret_cast<void*>(map->end), map->flags, map->name.c_str());
  86. return true;
  87. }
  88. #endif // defined(__APPLE__)
  89. bool BacktraceMap::Build() {
  90. #if defined(__APPLE__)
  91. char
  92. cmd[sizeof(pid_t) * 3 + sizeof("vmmap -w -resident -submap -allSplitLibs -interleaved ") + 1];
  93. char line[1024];
  94. // cmd is guaranteed to always be big enough to hold this string.
  95. snprintf(cmd, sizeof(cmd), "vmmap -w -resident -submap -allSplitLibs -interleaved %d", pid_);
  96. FILE* fp = popen(cmd, "r");
  97. if (fp == nullptr) {
  98. return false;
  99. }
  100. while (fgets(line, sizeof(line), fp)) {
  101. backtrace_map_t map;
  102. if (ParseLine(line, &map)) {
  103. maps_.push_back(map);
  104. }
  105. }
  106. pclose(fp);
  107. return true;
  108. #else
  109. return android::procinfo::ReadProcessMaps(
  110. pid_, [&](uint64_t start, uint64_t end, uint16_t flags, uint64_t, ino_t, const char* name) {
  111. maps_.resize(maps_.size() + 1);
  112. backtrace_map_t& map = maps_.back();
  113. map.start = start;
  114. map.end = end;
  115. map.flags = flags;
  116. map.name = name;
  117. });
  118. #endif
  119. }
  120. #if defined(__APPLE__)
  121. // Corkscrew and libunwind don't compile on the mac, so create a generic
  122. // map object.
  123. BacktraceMap* BacktraceMap::Create(pid_t pid, bool /*uncached*/) {
  124. BacktraceMap* map = new BacktraceMap(pid);
  125. if (!map->Build()) {
  126. delete map;
  127. return nullptr;
  128. }
  129. return map;
  130. }
  131. #endif