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