mirror of https://github.com/grpc/grpc.git
commit
e0cbc7e5dd
28 changed files with 564 additions and 33 deletions
@ -0,0 +1,43 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2020 gRPC authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include "src/core/lib/gprpp/examine_stack.h" |
||||||
|
|
||||||
|
namespace grpc_core { |
||||||
|
|
||||||
|
gpr_current_stack_trace_func g_current_stack_trace_provider = nullptr; |
||||||
|
|
||||||
|
gpr_current_stack_trace_func GetCurrentStackTraceProvider() { |
||||||
|
return g_current_stack_trace_provider; |
||||||
|
} |
||||||
|
|
||||||
|
void SetCurrentStackTraceProvider( |
||||||
|
gpr_current_stack_trace_func current_stack_trace_provider) { |
||||||
|
g_current_stack_trace_provider = current_stack_trace_provider; |
||||||
|
} |
||||||
|
|
||||||
|
absl::optional<std::string> GetCurrentStackTrace() { |
||||||
|
if (g_current_stack_trace_provider != nullptr) { |
||||||
|
return g_current_stack_trace_provider(); |
||||||
|
} |
||||||
|
return absl::nullopt; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace grpc_core
|
@ -0,0 +1,46 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2020 gRPC authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef GRPC_CORE_LIB_GPRPP_EXAMINE_STACK_H |
||||||
|
#define GRPC_CORE_LIB_GPRPP_EXAMINE_STACK_H |
||||||
|
|
||||||
|
#include <grpc/support/port_platform.h> |
||||||
|
|
||||||
|
#include <functional> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "absl/types/optional.h" |
||||||
|
|
||||||
|
namespace grpc_core { |
||||||
|
|
||||||
|
typedef std::string (*gpr_current_stack_trace_func)(); |
||||||
|
|
||||||
|
// Returns a current_stack_trace_provider.
|
||||||
|
gpr_current_stack_trace_func GetCurrentStackTraceProvider(); |
||||||
|
|
||||||
|
// Sets current_stack_trace_provider which provides a current-stack trace.
|
||||||
|
void SetCurrentStackTraceProvider( |
||||||
|
gpr_current_stack_trace_func current_stack_trace_provider); |
||||||
|
|
||||||
|
// Returns the current stack trace as a string via current_stack_trace_provider
|
||||||
|
// If current_stack_trace_provider is not set, it returns absl::nullopt.
|
||||||
|
absl::optional<std::string> GetCurrentStackTrace(); |
||||||
|
|
||||||
|
} // namespace grpc_core
|
||||||
|
|
||||||
|
#endif /* GRPC_CORE_LIB_GPRPP_EXAMINE_STACK_H */ |
@ -0,0 +1,82 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2020 gRPC authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "src/core/lib/gprpp/examine_stack.h" |
||||||
|
|
||||||
|
#include <stdio.h> |
||||||
|
#include <string.h> |
||||||
|
|
||||||
|
#include <gtest/gtest.h> |
||||||
|
|
||||||
|
#include "absl/debugging/stacktrace.h" |
||||||
|
#include "absl/debugging/symbolize.h" |
||||||
|
|
||||||
|
#include <grpc/support/log.h> |
||||||
|
|
||||||
|
namespace { |
||||||
|
|
||||||
|
std::string SimpleCurrentStackTraceProvider() { return "stacktrace"; } |
||||||
|
|
||||||
|
std::string AbseilCurrentStackTraceProvider() { |
||||||
|
std::string result = "Stack trace:\n"; |
||||||
|
constexpr int kNumStackFrames = 10; |
||||||
|
void* stack[kNumStackFrames]; |
||||||
|
int frame_sizes[kNumStackFrames]; |
||||||
|
int depth = absl::GetStackFrames(stack, frame_sizes, kNumStackFrames, 1); |
||||||
|
for (int i = 0; i < depth; i++) { |
||||||
|
char tmp[1024]; |
||||||
|
const char* symbol = "(unknown)"; |
||||||
|
if (absl::Symbolize(stack[i], tmp, sizeof(tmp))) { |
||||||
|
symbol = tmp; |
||||||
|
} |
||||||
|
result += symbol; |
||||||
|
result += +"\n"; |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(ExamineStackTest, NullStackProvider) { |
||||||
|
grpc_core::SetCurrentStackTraceProvider(nullptr); |
||||||
|
EXPECT_EQ(grpc_core::GetCurrentStackTraceProvider(), nullptr); |
||||||
|
EXPECT_EQ(grpc_core::GetCurrentStackTrace(), absl::nullopt); |
||||||
|
} |
||||||
|
|
||||||
|
TEST(ExamineStackTest, SimpleStackProvider) { |
||||||
|
grpc_core::SetCurrentStackTraceProvider(&SimpleCurrentStackTraceProvider); |
||||||
|
EXPECT_NE(grpc_core::GetCurrentStackTraceProvider(), nullptr); |
||||||
|
EXPECT_EQ(grpc_core::GetCurrentStackTrace(), "stacktrace"); |
||||||
|
} |
||||||
|
|
||||||
|
TEST(ExamineStackTest, AbseilStackProvider) { |
||||||
|
grpc_core::SetCurrentStackTraceProvider(&AbseilCurrentStackTraceProvider); |
||||||
|
EXPECT_NE(grpc_core::GetCurrentStackTraceProvider(), nullptr); |
||||||
|
const absl::optional<std::string> stack_trace = |
||||||
|
grpc_core::GetCurrentStackTrace(); |
||||||
|
EXPECT_NE(stack_trace, absl::nullopt); |
||||||
|
gpr_log(GPR_INFO, "stack_trace=%s", stack_trace->c_str()); |
||||||
|
EXPECT_TRUE(stack_trace->find("GetCurrentStackTrace") != -1); |
||||||
|
} |
||||||
|
|
||||||
|
int main(int argc, char** argv) { |
||||||
|
absl::InitializeSymbolizer(argv[0]); |
||||||
|
::testing::InitGoogleTest(&argc, argv); |
||||||
|
int ret = RUN_ALL_TESTS(); |
||||||
|
return ret; |
||||||
|
} |
@ -0,0 +1,41 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2020 the gRPC authors. |
||||||
|
* |
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
||||||
|
* you may not use this file except in compliance with the License. |
||||||
|
* You may obtain a copy of the License at |
||||||
|
* |
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* |
||||||
|
* Unless required by applicable law or agreed to in writing, software |
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS, |
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
||||||
|
* See the License for the specific language governing permissions and |
||||||
|
* limitations under the License. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "test/core/util/stack_tracer.h" |
||||||
|
|
||||||
|
#include <gtest/gtest.h> |
||||||
|
#include <string> |
||||||
|
|
||||||
|
#include "absl/debugging/symbolize.h" |
||||||
|
|
||||||
|
#include <grpc/support/log.h> |
||||||
|
|
||||||
|
#include "test/core/util/test_config.h" |
||||||
|
|
||||||
|
TEST(StackTracerTest, Basic) { |
||||||
|
std::string stack_trace = grpc_core::testing::GetCurrentStackTrace(); |
||||||
|
gpr_log(GPR_INFO, "stack_trace=%s", stack_trace.c_str()); |
||||||
|
EXPECT_TRUE(stack_trace.find("Basic") != -1); |
||||||
|
} |
||||||
|
|
||||||
|
int main(int argc, char** argv) { |
||||||
|
grpc::testing::TestEnvironment env(argc, argv); |
||||||
|
testing::InitGoogleTest(&argc, argv); |
||||||
|
int ret = RUN_ALL_TESTS(); |
||||||
|
return ret; |
||||||
|
} |
Loading…
Reference in new issue