examine_stack_test.cc 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. *
  3. * Copyright 2020 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 "src/core/lib/gprpp/examine_stack.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <gtest/gtest.h>
  22. #include "absl/debugging/stacktrace.h"
  23. #include "absl/debugging/symbolize.h"
  24. #include <grpc/support/log.h>
  25. namespace {
  26. std::string SimpleCurrentStackTraceProvider() { return "stacktrace"; }
  27. std::string AbseilCurrentStackTraceProvider() {
  28. std::string result = "Stack trace:\n";
  29. constexpr int kNumStackFrames = 10;
  30. void* stack[kNumStackFrames];
  31. int frame_sizes[kNumStackFrames];
  32. int depth = absl::GetStackFrames(stack, frame_sizes, kNumStackFrames, 1);
  33. for (int i = 0; i < depth; i++) {
  34. char tmp[1024];
  35. const char* symbol = "(unknown)";
  36. if (absl::Symbolize(stack[i], tmp, sizeof(tmp))) {
  37. symbol = tmp;
  38. }
  39. result += symbol;
  40. result += +"\n";
  41. }
  42. return result;
  43. }
  44. } // namespace
  45. TEST(ExamineStackTest, NullStackProvider) {
  46. grpc_core::SetCurrentStackTraceProvider(nullptr);
  47. EXPECT_EQ(grpc_core::GetCurrentStackTraceProvider(), nullptr);
  48. EXPECT_EQ(grpc_core::GetCurrentStackTrace(), absl::nullopt);
  49. }
  50. TEST(ExamineStackTest, SimpleStackProvider) {
  51. grpc_core::SetCurrentStackTraceProvider(&SimpleCurrentStackTraceProvider);
  52. EXPECT_NE(grpc_core::GetCurrentStackTraceProvider(), nullptr);
  53. EXPECT_EQ(grpc_core::GetCurrentStackTrace(), "stacktrace");
  54. }
  55. TEST(ExamineStackTest, AbseilStackProvider) {
  56. grpc_core::SetCurrentStackTraceProvider(&AbseilCurrentStackTraceProvider);
  57. EXPECT_NE(grpc_core::GetCurrentStackTraceProvider(), nullptr);
  58. const absl::optional<std::string> stack_trace =
  59. grpc_core::GetCurrentStackTrace();
  60. EXPECT_NE(stack_trace, absl::nullopt);
  61. gpr_log(GPR_INFO, "stack_trace=%s", stack_trace->c_str());
  62. #if !defined(NDEBUG) && !defined(GPR_MUSL_LIBC_COMPAT)
  63. EXPECT_TRUE(stack_trace->find("GetCurrentStackTrace") != std::string::npos);
  64. #endif
  65. }
  66. int main(int argc, char** argv) {
  67. absl::InitializeSymbolizer(argv[0]);
  68. ::testing::InitGoogleTest(&argc, argv);
  69. int ret = RUN_ALL_TESTS();
  70. return ret;
  71. }