examine_stack_test.cc
Go to the documentation of this file.
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 
20 
21 #include <stdio.h>
22 #include <string.h>
23 
24 #include <gtest/gtest.h>
25 
26 #include "absl/debugging/stacktrace.h"
27 #include "absl/debugging/symbolize.h"
28 
29 #include <grpc/support/log.h>
30 
31 namespace {
32 
33 std::string SimpleCurrentStackTraceProvider() { return "stacktrace"; }
34 
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++) {
42  char tmp[1024];
43  const char* symbol = "(unknown)";
44  if (absl::Symbolize(stack[i], tmp, sizeof(tmp))) {
45  symbol = tmp;
46  }
47  result += symbol;
48  result += +"\n";
49  }
50  return result;
51 }
52 
53 } // namespace
54 
55 TEST(ExamineStackTest, NullStackProvider) {
59 }
60 
61 TEST(ExamineStackTest, SimpleStackProvider) {
62  grpc_core::SetCurrentStackTraceProvider(&SimpleCurrentStackTraceProvider);
65 }
66 
67 TEST(ExamineStackTest, AbseilStackProvider) {
68  grpc_core::SetCurrentStackTraceProvider(&AbseilCurrentStackTraceProvider);
70  const absl::optional<std::string> stack_trace =
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);
76 #endif
77 }
78 
79 int main(int argc, char** argv) {
81  ::testing::InitGoogleTest(&argc, argv);
82  int ret = RUN_ALL_TESTS();
83  return ret;
84 }
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
log.h
grpc_core::GetCurrentStackTraceProvider
gpr_current_stack_trace_func GetCurrentStackTraceProvider()
Definition: src/core/lib/gprpp/examine_stack.cc:27
string.h
main
int main(int argc, char **argv)
Definition: examine_stack_test.cc:79
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::GetStackFrames
ABSL_ATTRIBUTE_NOINLINE ABSL_ATTRIBUTE_NO_TAIL_CALL int GetStackFrames(void **result, int *sizes, int max_depth, int skip_count)
Definition: abseil-cpp/absl/debugging/stacktrace.cc:87
grpc_core::GetCurrentStackTrace
absl::optional< std::string > GetCurrentStackTrace()
Definition: src/core/lib/gprpp/examine_stack.cc:36
grpc_core::SetCurrentStackTraceProvider
void SetCurrentStackTraceProvider(gpr_current_stack_trace_func current_stack_trace_provider)
Definition: src/core/lib/gprpp/examine_stack.cc:31
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
TEST
TEST(ExamineStackTest, NullStackProvider)
Definition: examine_stack_test.cc:55
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
absl::optional< std::string >
stack
NodeStack stack
Definition: cord_rep_btree.cc:356
examine_stack.h
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
absl::Symbolize
bool Symbolize(const void *pc, char *out, int out_size)
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
absl::InitializeSymbolizer
ABSL_NAMESPACE_BEGIN void InitializeSymbolizer(const char *argv0)
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
mkowners.depth
depth
Definition: mkowners.py:114
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:16