Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <sstream>
      6 #include <string>
      7 
      8 #include "base/debug_util.h"
      9 #include "base/logging.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 // Note: On Linux, this test currently only fully works on Debug builds.
     13 // See comments in the #ifdef soup if you intend to change this.
     14 // TODO(jar): BUG: 32070: Test is disabled... should be enabled.
     15 TEST(StackTrace, DISABLED_OutputToStream) {
     16   StackTrace trace;
     17 
     18   // Dump the trace into a string.
     19   std::ostringstream os;
     20   trace.OutputToStream(&os);
     21   std::string backtrace_message = os.str();
     22 
     23 #if defined(OS_POSIX) && !defined(OS_MACOSX) && NDEBUG
     24   // Stack traces require an extra data table that bloats our binaries,
     25   // so they're turned off for release builds.  We stop the test here,
     26   // at least letting us verify that the calls don't crash.
     27   return;
     28 #endif  // defined(OS_POSIX) && !defined(OS_MACOSX) && NDEBUG
     29 
     30   size_t frames_found = 0;
     31   trace.Addresses(&frames_found);
     32   ASSERT_GE(frames_found, 5u) <<
     33       "No stack frames found.  Skipping rest of test.";
     34 
     35   // Check if the output has symbol initialization warning.  If it does, fail.
     36   ASSERT_EQ(backtrace_message.find("Dumping unresolved backtrace"),
     37             std::string::npos) <<
     38       "Unable to resolve symbols.  Skipping rest of test.";
     39 
     40 #if defined(OS_MACOSX)
     41 #if 0
     42   // Disabled due to -fvisibility=hidden in build config.
     43 
     44   // Symbol resolution via the backtrace_symbol function does not work well
     45   // in OS X.
     46   // See this thread:
     47   //
     48   //    http://lists.apple.com/archives/darwin-dev/2009/Mar/msg00111.html
     49   //
     50   // Just check instead that we find our way back to the "start" symbol
     51   // which should be the first symbol in the trace.
     52   //
     53   // TODO(port): Find a more reliable way to resolve symbols.
     54 
     55   // Expect to at least find main.
     56   EXPECT_TRUE(backtrace_message.find("start") != std::string::npos)
     57       << "Expected to find start in backtrace:\n"
     58       << backtrace_message;
     59 
     60 #endif
     61 #elif defined(__GLIBCXX__)
     62   // This branch is for gcc-compiled code, but not Mac due to the
     63   // above #if.
     64   // Expect a demangled symbol.
     65   EXPECT_TRUE(backtrace_message.find("testing::Test::Run()") !=
     66               std::string::npos)
     67       << "Expected a demangled symbol in backtrace:\n"
     68       << backtrace_message;
     69 
     70 #elif 0
     71   // This is the fall-through case; it used to cover Windows.
     72   // But it's disabled because of varying buildbot configs;
     73   // some lack symbols.
     74 
     75   // Expect to at least find main.
     76   EXPECT_TRUE(backtrace_message.find("main") != std::string::npos)
     77       << "Expected to find main in backtrace:\n"
     78       << backtrace_message;
     79 
     80 #if defined(OS_WIN)
     81 // MSVC doesn't allow the use of C99's __func__ within C++, so we fake it with
     82 // MSVC's __FUNCTION__ macro.
     83 #define __func__ __FUNCTION__
     84 #endif
     85 
     86   // Expect to find this function as well.
     87   // Note: This will fail if not linked with -rdynamic (aka -export_dynamic)
     88   EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos)
     89       << "Expected to find " << __func__ << " in backtrace:\n"
     90       << backtrace_message;
     91 
     92 #endif  // define(OS_MACOSX)
     93 }
     94 
     95 // The test is used for manual testing (i.e. see the raw output).
     96 // To run the test use the flags:
     97 // --gtest_filter='*DebugOutputToStream' --gtest_also_run_disabled_tests
     98 TEST(StackTrace, DISABLED_DebugOutputToStream) {
     99   StackTrace trace;
    100   std::ostringstream os;
    101   trace.OutputToStream(&os);
    102   LOG(INFO) << os.str();
    103 }
    104 
    105 // The test is used for manual testing. See the comment above.
    106 TEST(StackTrace, DISABLED_DebugPrintBacktrace) {
    107   StackTrace().PrintBacktrace();
    108 }
    109