Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/absl/debugging/symbolize_darwin.inc is written in an unsupported language. File is not indexed.

0001 // Copyright 2020 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 #include <cxxabi.h>
0016 #include <execinfo.h>
0017 
0018 #include <algorithm>
0019 #include <cstring>
0020 
0021 #include "absl/base/internal/raw_logging.h"
0022 #include "absl/debugging/internal/demangle.h"
0023 #include "absl/strings/numbers.h"
0024 #include "absl/strings/str_cat.h"
0025 #include "absl/strings/string_view.h"
0026 
0027 namespace absl {
0028 ABSL_NAMESPACE_BEGIN
0029 
0030 void InitializeSymbolizer(const char*) {}
0031 
0032 namespace debugging_internal {
0033 namespace {
0034 
0035 static std::string GetSymbolString(absl::string_view backtrace_line) {
0036   // Example Backtrace lines:
0037   // 0   libimaging_shared.dylib             0x018c152a
0038   // _ZNSt11_Deque_baseIN3nik7mediadb4PageESaIS2_EE17_M_initialize_mapEm + 3478
0039   //
0040   // or
0041   // 0   libimaging_shared.dylib             0x0000000001895c39
0042   // _ZN3nik4util19register_shared_ptrINS_3gpu7TextureEEEvPKvS5_ + 39
0043   //
0044   // or
0045   // 0   mysterious_app                      0x0124000120120009 main + 17
0046   auto address_pos = backtrace_line.find(" 0x");
0047   if (address_pos == absl::string_view::npos) return std::string();
0048   absl::string_view symbol_view = backtrace_line.substr(address_pos + 1);
0049 
0050   auto space_pos = symbol_view.find(" ");
0051   if (space_pos == absl::string_view::npos) return std::string();
0052   symbol_view = symbol_view.substr(space_pos + 1);  // to mangled symbol
0053 
0054   auto plus_pos = symbol_view.find(" + ");
0055   if (plus_pos == absl::string_view::npos) return std::string();
0056   symbol_view = symbol_view.substr(0, plus_pos);  // strip remainng
0057 
0058   return std::string(symbol_view);
0059 }
0060 
0061 }  // namespace
0062 }  // namespace debugging_internal
0063 
0064 bool Symbolize(const void* pc, char* out, int out_size) {
0065   if (out_size <= 0 || pc == nullptr) {
0066     out = nullptr;
0067     return false;
0068   }
0069 
0070   // This allocates a char* array.
0071   char** frame_strings = backtrace_symbols(const_cast<void**>(&pc), 1);
0072 
0073   if (frame_strings == nullptr) return false;
0074 
0075   std::string symbol = debugging_internal::GetSymbolString(frame_strings[0]);
0076   free(frame_strings);
0077 
0078   char tmp_buf[1024];
0079   if (debugging_internal::Demangle(symbol.c_str(), tmp_buf, sizeof(tmp_buf))) {
0080     size_t len = strlen(tmp_buf);
0081     if (len + 1 <= static_cast<size_t>(out_size)) {  // +1 for '\0'
0082       assert(len < sizeof(tmp_buf));
0083       memmove(out, tmp_buf, len + 1);
0084     }
0085   } else {
0086     strncpy(out, symbol.c_str(), static_cast<size_t>(out_size));
0087   }
0088 
0089   if (out[out_size - 1] != '\0') {
0090     // strncpy() does not '\0' terminate when it truncates.
0091     static constexpr char kEllipsis[] = "...";
0092     size_t ellipsis_size =
0093         std::min(sizeof(kEllipsis) - 1, static_cast<size_t>(out_size) - 1);
0094     memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size);
0095     out[out_size - 1] = '\0';
0096   }
0097 
0098   return true;
0099 }
0100 
0101 ABSL_NAMESPACE_END
0102 }  // namespace absl