stack_tracer.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. *
  3. * Copyright 2020 the gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <grpc/support/port_platform.h>
  19. #include "test/core/util/stack_tracer.h"
  20. #include <cstdio>
  21. #include <string>
  22. #include "absl/debugging/stacktrace.h"
  23. #include "absl/debugging/symbolize.h"
  24. #include "src/core/lib/gprpp/examine_stack.h"
  25. namespace {
  26. constexpr int kPrintfPointerFieldWidth = 2 + 2 * sizeof(void*);
  27. void DumpPCAndFrameSizeAndSymbol(void (*writerfn)(const char*, void*),
  28. void* writerfn_arg, void* pc,
  29. void* symbolize_pc, int framesize,
  30. const char* const prefix) {
  31. char tmp[1024];
  32. const char* symbol = "(unknown)";
  33. if (absl::Symbolize(symbolize_pc, tmp, sizeof(tmp))) {
  34. symbol = tmp;
  35. }
  36. char buf[1024];
  37. if (framesize <= 0) {
  38. snprintf(buf, sizeof(buf), "%s@ %*p (unknown) %s\n", prefix,
  39. kPrintfPointerFieldWidth, pc, symbol);
  40. } else {
  41. snprintf(buf, sizeof(buf), "%s@ %*p %9d %s\n", prefix,
  42. kPrintfPointerFieldWidth, pc, framesize, symbol);
  43. }
  44. writerfn(buf, writerfn_arg);
  45. }
  46. void DumpPCAndFrameSize(void (*writerfn)(const char*, void*),
  47. void* writerfn_arg, void* pc, int framesize,
  48. const char* const prefix) {
  49. char buf[100];
  50. if (framesize <= 0) {
  51. snprintf(buf, sizeof(buf), "%s@ %*p (unknown)\n", prefix,
  52. kPrintfPointerFieldWidth, pc);
  53. } else {
  54. snprintf(buf, sizeof(buf), "%s@ %*p %9d\n", prefix,
  55. kPrintfPointerFieldWidth, pc, framesize);
  56. }
  57. writerfn(buf, writerfn_arg);
  58. }
  59. void DumpStackTrace(void* const stack[], int frame_sizes[], int depth,
  60. bool symbolize_stacktrace,
  61. void (*writerfn)(const char*, void*), void* writerfn_arg) {
  62. for (int i = 0; i < depth; i++) {
  63. if (symbolize_stacktrace) {
  64. DumpPCAndFrameSizeAndSymbol(writerfn, writerfn_arg, stack[i],
  65. reinterpret_cast<char*>(stack[i]) - 1,
  66. frame_sizes[i], " ");
  67. } else {
  68. DumpPCAndFrameSize(writerfn, writerfn_arg, stack[i], frame_sizes[i],
  69. " ");
  70. }
  71. }
  72. }
  73. void DebugWriteToString(const char* data, void* str) {
  74. reinterpret_cast<std::string*>(str)->append(data);
  75. }
  76. } // namespace
  77. namespace grpc_core {
  78. namespace testing {
  79. std::string GetCurrentStackTrace() {
  80. std::string result = "Stack trace:\n";
  81. constexpr int kNumStackFrames = 32;
  82. void* stack[kNumStackFrames];
  83. int frame_sizes[kNumStackFrames];
  84. int depth = absl::GetStackFrames(stack, frame_sizes, kNumStackFrames, 1);
  85. DumpStackTrace(stack, frame_sizes, depth, true, DebugWriteToString, &result);
  86. return result;
  87. }
  88. void InitializeStackTracer(const char* argv0) {
  89. absl::InitializeSymbolizer(argv0);
  90. SetCurrentStackTraceProvider(&GetCurrentStackTrace);
  91. }
  92. } // namespace testing
  93. } // namespace grpc_core