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
19 #include "src/core/lib/gprpp/examine_stack.h"
20
21 #include "absl/debugging/stacktrace.h"
22 #include "absl/debugging/symbolize.h"
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25
26 #include <grpc/support/log.h>
27
28 namespace {
29
SimpleCurrentStackTraceProvider()30 std::string SimpleCurrentStackTraceProvider() { return "stacktrace"; }
31
AbseilCurrentStackTraceProvider()32 std::string AbseilCurrentStackTraceProvider() {
33 std::string result = "Stack trace:\n";
34 constexpr int kNumStackFrames = 10;
35 void* stack[kNumStackFrames];
36 int frame_sizes[kNumStackFrames];
37 int depth = absl::GetStackFrames(stack, frame_sizes, kNumStackFrames, 1);
38 for (int i = 0; i < depth; i++) {
39 char tmp[1024];
40 const char* symbol = "(unknown)";
41 if (absl::Symbolize(stack[i], tmp, sizeof(tmp))) {
42 symbol = tmp;
43 }
44 result += symbol;
45 result += +"\n";
46 }
47 return result;
48 }
49
50 } // namespace
51
TEST(ExamineStackTest,NullStackProvider)52 TEST(ExamineStackTest, NullStackProvider) {
53 grpc_core::SetCurrentStackTraceProvider(nullptr);
54 EXPECT_EQ(grpc_core::GetCurrentStackTraceProvider(), nullptr);
55 EXPECT_EQ(grpc_core::GetCurrentStackTrace(), absl::nullopt);
56 }
57
TEST(ExamineStackTest,SimpleStackProvider)58 TEST(ExamineStackTest, SimpleStackProvider) {
59 grpc_core::SetCurrentStackTraceProvider(&SimpleCurrentStackTraceProvider);
60 EXPECT_NE(grpc_core::GetCurrentStackTraceProvider(), nullptr);
61 EXPECT_EQ(grpc_core::GetCurrentStackTrace(), "stacktrace");
62 }
63
TEST(ExamineStackTest,AbseilStackProvider)64 TEST(ExamineStackTest, AbseilStackProvider) {
65 grpc_core::SetCurrentStackTraceProvider(&AbseilCurrentStackTraceProvider);
66 EXPECT_NE(grpc_core::GetCurrentStackTraceProvider(), nullptr);
67 const absl::optional<std::string> stack_trace =
68 grpc_core::GetCurrentStackTrace();
69 EXPECT_NE(stack_trace, absl::nullopt);
70 gpr_log(GPR_INFO, "stack_trace=%s", stack_trace->c_str());
71 #if !defined(NDEBUG) && !defined(GPR_MUSL_LIBC_COMPAT)
72 // Expect to see some gtest signature on the stack (this used to be
73 // GetCurrentStackTrace, but some operating systems have trouble with the leaf
74 // function).
75 EXPECT_THAT(*stack_trace, ::testing::HasSubstr("testing::"));
76 #endif
77 }
78
main(int argc,char ** argv)79 int main(int argc, char** argv) {
80 absl::InitializeSymbolizer(argv[0]);
81 ::testing::InitGoogleTest(&argc, argv);
82 int ret = RUN_ALL_TESTS();
83 return ret;
84 }
85