Austin Schuh | b4691e9 | 2020-12-31 12:37:18 -0800 | [diff] [blame^] | 1 | // Copyright 2020 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | #include <cxxabi.h> |
| 16 | #include <execinfo.h> |
| 17 | |
| 18 | #include <algorithm> |
| 19 | #include <cstring> |
| 20 | |
| 21 | #include "absl/base/internal/raw_logging.h" |
| 22 | #include "absl/debugging/internal/demangle.h" |
| 23 | #include "absl/strings/numbers.h" |
| 24 | #include "absl/strings/str_cat.h" |
| 25 | #include "absl/strings/string_view.h" |
| 26 | |
| 27 | namespace absl { |
| 28 | ABSL_NAMESPACE_BEGIN |
| 29 | |
| 30 | void InitializeSymbolizer(const char*) {} |
| 31 | |
| 32 | namespace debugging_internal { |
| 33 | namespace { |
| 34 | |
| 35 | static std::string GetSymbolString(absl::string_view backtrace_line) { |
| 36 | // Example Backtrace lines: |
| 37 | // 0 libimaging_shared.dylib 0x018c152a |
| 38 | // _ZNSt11_Deque_baseIN3nik7mediadb4PageESaIS2_EE17_M_initialize_mapEm + 3478 |
| 39 | // |
| 40 | // or |
| 41 | // 0 libimaging_shared.dylib 0x0000000001895c39 |
| 42 | // _ZN3nik4util19register_shared_ptrINS_3gpu7TextureEEEvPKvS5_ + 39 |
| 43 | // |
| 44 | // or |
| 45 | // 0 mysterious_app 0x0124000120120009 main + 17 |
| 46 | auto address_pos = backtrace_line.find(" 0x"); |
| 47 | if (address_pos == absl::string_view::npos) return std::string(); |
| 48 | absl::string_view symbol_view = backtrace_line.substr(address_pos + 1); |
| 49 | |
| 50 | auto space_pos = symbol_view.find(" "); |
| 51 | if (space_pos == absl::string_view::npos) return std::string(); |
| 52 | symbol_view = symbol_view.substr(space_pos + 1); // to mangled symbol |
| 53 | |
| 54 | auto plus_pos = symbol_view.find(" + "); |
| 55 | if (plus_pos == absl::string_view::npos) return std::string(); |
| 56 | symbol_view = symbol_view.substr(0, plus_pos); // strip remainng |
| 57 | |
| 58 | return std::string(symbol_view); |
| 59 | } |
| 60 | |
| 61 | } // namespace |
| 62 | } // namespace debugging_internal |
| 63 | |
| 64 | bool Symbolize(const void* pc, char* out, int out_size) { |
| 65 | if (out_size <= 0 || pc == nullptr) { |
| 66 | out = nullptr; |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | // This allocates a char* array. |
| 71 | char** frame_strings = backtrace_symbols(const_cast<void**>(&pc), 1); |
| 72 | |
| 73 | if (frame_strings == nullptr) return false; |
| 74 | |
| 75 | std::string symbol = debugging_internal::GetSymbolString(frame_strings[0]); |
| 76 | free(frame_strings); |
| 77 | |
| 78 | char tmp_buf[1024]; |
| 79 | if (debugging_internal::Demangle(symbol.c_str(), tmp_buf, sizeof(tmp_buf))) { |
| 80 | size_t len = strlen(tmp_buf); |
| 81 | if (len + 1 <= static_cast<size_t>(out_size)) { // +1 for '\0' |
| 82 | assert(len < sizeof(tmp_buf)); |
| 83 | memmove(out, tmp_buf, len + 1); |
| 84 | } |
| 85 | } else { |
| 86 | strncpy(out, symbol.c_str(), out_size); |
| 87 | } |
| 88 | |
| 89 | if (out[out_size - 1] != '\0') { |
| 90 | // strncpy() does not '\0' terminate when it truncates. |
| 91 | static constexpr char kEllipsis[] = "..."; |
| 92 | int ellipsis_size = std::min<int>(sizeof(kEllipsis) - 1, out_size - 1); |
| 93 | memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size); |
| 94 | out[out_size - 1] = '\0'; |
| 95 | } |
| 96 | |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | ABSL_NAMESPACE_END |
| 101 | } // namespace absl |