3 * Copyright 2020 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "src/core/lib/gprpp/examine_stack.h"
24 #include <gtest/gtest.h>
26 #include "absl/debugging/stacktrace.h"
27 #include "absl/debugging/symbolize.h"
29 #include <grpc/support/log.h>
33 std::string SimpleCurrentStackTraceProvider() { return "stacktrace"; }
35 std::string AbseilCurrentStackTraceProvider() {
36 std::string result = "Stack trace:\n";
37 constexpr int kNumStackFrames = 10;
38 void* stack[kNumStackFrames];
39 int frame_sizes[kNumStackFrames];
40 int depth = absl::GetStackFrames(stack, frame_sizes, kNumStackFrames, 1);
41 for (int i = 0; i < depth; i++) {
43 const char* symbol = "(unknown)";
44 if (absl::Symbolize(stack[i], tmp, sizeof(tmp))) {
55 TEST(ExamineStackTest, NullStackProvider) {
56 grpc_core::SetCurrentStackTraceProvider(nullptr);
57 EXPECT_EQ(grpc_core::GetCurrentStackTraceProvider(), nullptr);
58 EXPECT_EQ(grpc_core::GetCurrentStackTrace(), absl::nullopt);
61 TEST(ExamineStackTest, SimpleStackProvider) {
62 grpc_core::SetCurrentStackTraceProvider(&SimpleCurrentStackTraceProvider);
63 EXPECT_NE(grpc_core::GetCurrentStackTraceProvider(), nullptr);
64 EXPECT_EQ(grpc_core::GetCurrentStackTrace(), "stacktrace");
67 TEST(ExamineStackTest, AbseilStackProvider) {
68 grpc_core::SetCurrentStackTraceProvider(&AbseilCurrentStackTraceProvider);
69 EXPECT_NE(grpc_core::GetCurrentStackTraceProvider(), nullptr);
70 const absl::optional<std::string> stack_trace =
71 grpc_core::GetCurrentStackTrace();
72 EXPECT_NE(stack_trace, absl::nullopt);
73 gpr_log(GPR_INFO, "stack_trace=%s", stack_trace->c_str());
74 #if !defined(NDEBUG) && !defined(GPR_MUSL_LIBC_COMPAT)
75 EXPECT_TRUE(stack_trace->find("GetCurrentStackTrace") != std::string::npos);
79 int main(int argc, char** argv) {
80 absl::InitializeSymbolizer(argv[0]);
81 ::testing::InitGoogleTest(&argc, argv);
82 int ret = RUN_ALL_TESTS();