symbolize_darwin.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #include <cxxabi.h>
  15. #include <execinfo.h>
  16. #include <algorithm>
  17. #include <cstring>
  18. #include "absl/base/internal/raw_logging.h"
  19. #include "absl/debugging/internal/demangle.h"
  20. #include "absl/strings/numbers.h"
  21. #include "absl/strings/str_cat.h"
  22. #include "absl/strings/string_view.h"
  23. namespace absl {
  24. ABSL_NAMESPACE_BEGIN
  25. void InitializeSymbolizer(const char*) {}
  26. namespace debugging_internal {
  27. namespace {
  28. static std::string GetSymbolString(absl::string_view backtrace_line) {
  29. // Example Backtrace lines:
  30. // 0 libimaging_shared.dylib 0x018c152a
  31. // _ZNSt11_Deque_baseIN3nik7mediadb4PageESaIS2_EE17_M_initialize_mapEm + 3478
  32. //
  33. // or
  34. // 0 libimaging_shared.dylib 0x0000000001895c39
  35. // _ZN3nik4util19register_shared_ptrINS_3gpu7TextureEEEvPKvS5_ + 39
  36. //
  37. // or
  38. // 0 mysterious_app 0x0124000120120009 main + 17
  39. auto address_pos = backtrace_line.find(" 0x");
  40. if (address_pos == absl::string_view::npos) return std::string();
  41. absl::string_view symbol_view = backtrace_line.substr(address_pos + 1);
  42. auto space_pos = symbol_view.find(" ");
  43. if (space_pos == absl::string_view::npos) return std::string();
  44. symbol_view = symbol_view.substr(space_pos + 1); // to mangled symbol
  45. auto plus_pos = symbol_view.find(" + ");
  46. if (plus_pos == absl::string_view::npos) return std::string();
  47. symbol_view = symbol_view.substr(0, plus_pos); // strip remainng
  48. return std::string(symbol_view);
  49. }
  50. } // namespace
  51. } // namespace debugging_internal
  52. bool Symbolize(const void* pc, char* out, int out_size) {
  53. if (out_size <= 0 || pc == nullptr) {
  54. out = nullptr;
  55. return false;
  56. }
  57. // This allocates a char* array.
  58. char** frame_strings = backtrace_symbols(const_cast<void**>(&pc), 1);
  59. if (frame_strings == nullptr) return false;
  60. std::string symbol = debugging_internal::GetSymbolString(frame_strings[0]);
  61. free(frame_strings);
  62. char tmp_buf[1024];
  63. if (debugging_internal::Demangle(symbol.c_str(), tmp_buf, sizeof(tmp_buf))) {
  64. size_t len = strlen(tmp_buf);
  65. if (len + 1 <= static_cast<size_t>(out_size)) { // +1 for '\0'
  66. assert(len < sizeof(tmp_buf));
  67. memmove(out, tmp_buf, len + 1);
  68. }
  69. } else {
  70. strncpy(out, symbol.c_str(), static_cast<size_t>(out_size));
  71. }
  72. if (out[out_size - 1] != '\0') {
  73. // strncpy() does not '\0' terminate when it truncates.
  74. static constexpr char kEllipsis[] = "...";
  75. size_t ellipsis_size =
  76. std::min(sizeof(kEllipsis) - 1, static_cast<size_t>(out_size) - 1);
  77. memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size);
  78. out[out_size - 1] = '\0';
  79. }
  80. return true;
  81. }
  82. ABSL_NAMESPACE_END
  83. } // namespace absl