From 96fd00320f4eef1cc52f68939f86b319f6d2095f Mon Sep 17 00:00:00 2001 From: "ajwong@chromium.org" Date: Fri, 24 Apr 2009 00:13:08 +0000 Subject: Print backtraces on FATAL log messages in debug mode. This provides basic support for looking up backtrace information on GNU libc systems and in Windows. The code is only enabled for FATAL log messages in debug mode. In a release build, it is unlikely that symbols will be available making the backtrace less useful. Review URL: http://codereview.chromium.org/62140 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@14391 0039d316-1c4b-4281-b951-d872f2087c98 --- base/debug_util_unittest.cc | 72 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 base/debug_util_unittest.cc (limited to 'base/debug_util_unittest.cc') diff --git a/base/debug_util_unittest.cc b/base/debug_util_unittest.cc new file mode 100644 index 0000000..2636e45 --- /dev/null +++ b/base/debug_util_unittest.cc @@ -0,0 +1,72 @@ +// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include +#include + +#include "base/debug_util.h" +#include "base/logging.h" +#include "testing/gtest/include/gtest/gtest.h" + +TEST(StackTrace, OutputToStream) { + StackTrace trace; + + // Dump the trace into a string. + std::ostringstream os; + trace.OutputToStream(&os); + std::string backtrace_message = os.str(); + + size_t frames_found = 0; + trace.Addresses(&frames_found); + if (frames_found == 0) { + LOG(ERROR) << "No stack frames found. Skipping rest of test."; + return; + } + + // Check if the output has symbol initialization warning. If it does, fail. + if (backtrace_message.find("Dumping unresolved backtrace") != + std::string::npos) { + LOG(ERROR) << "Unable to resolve symbols. Skipping rest of test."; + return; + } + +#if defined(OS_MACOSX) + + // Symbol resolution via the backtrace_symbol funciton does not work well + // in OsX. + // See this thread: + // + // http://lists.apple.com/archives/darwin-dev/2009/Mar/msg00111.html + // + // Just check instead that we find our way back to the "start" symbol + // which should be the first symbol in the trace. + // + // TODO(port): Find a more reliable way to resolve symbols. + + // Expect to at least find main. + EXPECT_TRUE(backtrace_message.find("start") != std::string::npos) + << "Expected to find start in backtrace:\n" + << backtrace_message; + +#else // defined(OS_MACOSX) + + // Expect to at least find main. + EXPECT_TRUE(backtrace_message.find("main") != std::string::npos) + << "Expected to find main in backtrace:\n" + << backtrace_message; + +#if defined(OS_WIN) +// MSVC doesn't allow the use of C99's __func__ within C++, so we fake it with +// MSVC's __FUNCTION__ macro. +#define __func__ __FUNCTION__ +#endif + + // Expect to find this function as well. + // Note: This will fail if not linked with -rdynamic (aka -export_dynamic) + EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos) + << "Expected to find " << __func__ << " in backtrace:\n" + << backtrace_message; + +#endif // define(OS_MACOSX) +} -- cgit v1.1