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 const char* func_name = emscripten_pc_get_function(pc); 54 if (func_name == nullptr) { 55 return false; 56 } 57 58 strncpy(out, func_name, out_size); 59 60 if (out[out_size - 1] != '\0') { 61 // strncpy() does not '\0' terminate when it truncates. 62 static constexpr char kEllipsis[] = "..."; 63 int ellipsis_size = std::min<int>(sizeof(kEllipsis) - 1, out_size - 1); 64 memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size); 65 out[out_size - 1] = '\0'; 66 } 67 68 return true; 69} 70 71ABSL_NAMESPACE_END 72} // namespace absl 73