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