From 9029fc24f60b3c98c06a61d3b5c94cb1840b9a37 Mon Sep 17 00:00:00 2001 From: Brian Zhao <bmzhao@google.com> Date: Tue, 3 Sep 2019 10:51:31 -0700 Subject: [PATCH] Adding a stacktrace test. PiperOrigin-RevId: 266966740 --- tensorflow/core/BUILD | 1 + tensorflow/core/platform/default/stacktrace.h | 10 ++--- tensorflow/core/platform/stacktrace_test.cc | 39 +++++++++++++++++++ 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 tensorflow/core/platform/stacktrace_test.cc diff --git a/tensorflow/core/BUILD b/tensorflow/core/BUILD index 68e338b21c4..a7e65884923 100644 --- a/tensorflow/core/BUILD +++ b/tensorflow/core/BUILD @@ -3735,6 +3735,7 @@ tf_cc_tests( "//tensorflow/core/platform:profile_utils/cpu_utils_test.cc", "//tensorflow/core/platform:scanner_test.cc", "//tensorflow/core/platform:stacktrace_handler_test.cc", + "//tensorflow/core/platform:stacktrace_test.cc", "//tensorflow/core/platform:str_util_test.cc", "//tensorflow/core/platform:strcat_test.cc", "//tensorflow/core/platform:stringpiece_test.cc", diff --git a/tensorflow/core/platform/default/stacktrace.h b/tensorflow/core/platform/default/stacktrace.h index 808ef25c430..859faecdddb 100644 --- a/tensorflow/core/platform/default/stacktrace.h +++ b/tensorflow/core/platform/default/stacktrace.h @@ -21,11 +21,11 @@ limitations under the License. // clang-format on #if !defined(IS_MOBILE_PLATFORM) && !defined(PLATFORM_WINDOWS) && \ - defined(PLATFORM_POSIX) && (defined(__clang__) || defined(__GNUC__)) -#define TF_GENERATE_BACKTRACE + (defined(__clang__) || defined(__GNUC__)) +#define TF_HAS_STACKTRACE #endif -#if defined(TF_GENERATE_BACKTRACE) +#if defined(TF_HAS_STACKTRACE) #include <dlfcn.h> #include <execinfo.h> #include <stdio.h> @@ -41,7 +41,7 @@ namespace tensorflow { // Function to create a pretty stacktrace. inline std::string CurrentStackTrace() { -#if defined(TF_GENERATE_BACKTRACE) +#if defined(TF_HAS_STACKTRACE) std::stringstream ss(""); ss << "*** Begin stack trace ***" << std::endl; @@ -71,7 +71,7 @@ inline std::string CurrentStackTrace() { return ss.str(); #else return std::string(); -#endif // defined(TF_GENERATE_BACKTRACE) +#endif // defined(TF_HAS_STACKTRACE) } inline void DebugWriteToString(const char* data, void* arg) { diff --git a/tensorflow/core/platform/stacktrace_test.cc b/tensorflow/core/platform/stacktrace_test.cc new file mode 100644 index 00000000000..6e9c15276ee --- /dev/null +++ b/tensorflow/core/platform/stacktrace_test.cc @@ -0,0 +1,39 @@ +/* Copyright 2019 The TensorFlow Authors. All Rights Reserved. + +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. +==============================================================================*/ +// Testing proper operation of the stacktrace handler. + +#include "tensorflow/core/platform/stacktrace.h" + +#include <string> + +#include "tensorflow/core/platform/logging.h" +#include "tensorflow/core/platform/test.h" + +namespace tensorflow { +namespace { + +#if defined(TF_HAS_STACKTRACE) + +TEST(StacktraceTest, StacktraceWorks) { + std::string stacktrace = CurrentStackTrace(); + LOG(INFO) << "CurrentStackTrace():\n" << stacktrace; + std::string expected_frame = "testing::internal::UnitTestImpl::RunAllTests"; + EXPECT_NE(stacktrace.find(expected_frame), std::string::npos); +} + +#endif // defined(TF_HAS_STACKTRACE) + +} // namespace +} // namespace tensorflow