symbolize_emscripten.inc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #error #include <emscripten.h>
  16. #include <algorithm>
  17. #include <cstring>
  18. #include "y_absl/base/internal/raw_logging.h"
  19. #include "y_absl/debugging/internal/demangle.h"
  20. #include "y_absl/strings/numbers.h"
  21. #include "y_absl/strings/str_cat.h"
  22. #include "y_absl/strings/string_view.h"
  23. extern "C" {
  24. const char* emscripten_pc_get_function(const void* pc);
  25. }
  26. // clang-format off
  27. EM_JS(bool, HaveOffsetConverter, (),
  28. { return typeof wasmOffsetConverter !== 'undefined'; });
  29. // clang-format on
  30. namespace y_absl {
  31. Y_ABSL_NAMESPACE_BEGIN
  32. void InitializeSymbolizer(const char*) {
  33. if (!HaveOffsetConverter()) {
  34. Y_ABSL_RAW_LOG(INFO,
  35. "Symbolization unavailable. Rebuild with -sWASM=1 "
  36. "and -sUSE_OFFSET_CONVERTER=1.");
  37. }
  38. }
  39. bool Symbolize(const void* pc, char* out, int out_size) {
  40. // Check if we have the offset converter necessary for pc_get_function.
  41. // Without it, the program will abort().
  42. if (!HaveOffsetConverter()) {
  43. return false;
  44. }
  45. if (pc == nullptr || out_size <= 0) {
  46. return false;
  47. }
  48. const char* func_name = emscripten_pc_get_function(pc);
  49. if (func_name == nullptr) {
  50. return false;
  51. }
  52. strncpy(out, func_name, out_size);
  53. if (out[out_size - 1] != '\0') {
  54. // strncpy() does not '\0' terminate when it truncates.
  55. static constexpr char kEllipsis[] = "...";
  56. int ellipsis_size = std::min<int>(sizeof(kEllipsis) - 1, out_size - 1);
  57. memcpy(out + out_size - ellipsis_size - 1, kEllipsis, ellipsis_size);
  58. out[out_size - 1] = '\0';
  59. }
  60. return true;
  61. }
  62. Y_ABSL_NAMESPACE_END
  63. } // namespace y_absl