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 <emscripten.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 27extern "C" { 28const char* emscripten_pc_get_function(const void* pc); 29} 30 31// clang-format off 32EM_JS(bool, HaveOffsetConverter, (), 33 { return typeof wasmOffsetConverter !== 'undefined'; }); 34// clang-format on 35 36namespace absl { 37ABSL_NAMESPACE_BEGIN 38 39void InitializeSymbolizer(const char*) { 40 if (!HaveOffsetConverter()) { 41 ABSL_RAW_LOG(INFO, 42 "Symbolization unavailable. Rebuild with -sWASM=1 " 43 "and -sUSE_OFFSET_CONVERTER=1."); 44 } 45} 46 47bool Symbolize(const void* pc, char* out, int out_size) { 48 // Check if we have the offset converter necessary for pc_get_function. 49 // Without it, the program will abort(). 50 if (!HaveOffsetConverter()) { 51 return false; 52 } 53 if (pc == nullptr || out_size <= 0) { 54 return false; 55 } 56 const char* func_name = emscripten_pc_get_function(pc); 57 if (func_name == nullptr) { 58 return false; 59 } 60 61 strncpy(out, func_name, out_size); 62 63 if (out[out_size - 1] != '\0') { 64 // strncpy() does not '\0' terminate when it truncates. 65 static constexpr char kEllipsis[] = "..."; 66 int ellipsis_size = std::min<int>(sizeof(kEllipsis) - 1, out_size - 1); 67 memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size); 68 out[out_size - 1] = '\0'; 69 } 70 71 return true; 72} 73 74ABSL_NAMESPACE_END 75} // namespace absl 76