Warning, /include/absl/debugging/symbolize_emscripten.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 <emscripten.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 extern "C" {
0028 const char* emscripten_pc_get_function(const void* pc);
0029 }
0030
0031 // clang-format off
0032 EM_JS(bool, HaveOffsetConverter, (),
0033 { return typeof wasmOffsetConverter !== 'undefined'; });
0034 // clang-format on
0035
0036 namespace absl {
0037 ABSL_NAMESPACE_BEGIN
0038
0039 void InitializeSymbolizer(const char*) {
0040 if (!HaveOffsetConverter()) {
0041 ABSL_RAW_LOG(INFO,
0042 "Symbolization unavailable. Rebuild with -sWASM=1 "
0043 "and -sUSE_OFFSET_CONVERTER=1.");
0044 }
0045 }
0046
0047 bool Symbolize(const void* pc, char* out, int out_size) {
0048 // Check if we have the offset converter necessary for pc_get_function.
0049 // Without it, the program will abort().
0050 if (!HaveOffsetConverter()) {
0051 return false;
0052 }
0053 if (pc == nullptr || out_size <= 0) {
0054 return false;
0055 }
0056 const char* func_name = emscripten_pc_get_function(pc);
0057 if (func_name == nullptr) {
0058 return false;
0059 }
0060
0061 strncpy(out, func_name, out_size);
0062
0063 if (out[out_size - 1] != '\0') {
0064 // strncpy() does not '\0' terminate when it truncates.
0065 static constexpr char kEllipsis[] = "...";
0066 int ellipsis_size = std::min<int>(sizeof(kEllipsis) - 1, out_size - 1);
0067 memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size);
0068 out[out_size - 1] = '\0';
0069 }
0070
0071 return true;
0072 }
0073
0074 ABSL_NAMESPACE_END
0075 } // namespace absl