| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <sstream> | 5 #include <sstream> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/debug_util.h" | 8 #include "base/debug_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 11 |
| 12 // Note: this test currently only fully works on Linux Debug builds. |
| 13 // See comments in the #ifdef soup if you intend to change this. |
| 12 TEST(StackTrace, OutputToStream) { | 14 TEST(StackTrace, OutputToStream) { |
| 13 StackTrace trace; | 15 StackTrace trace; |
| 14 | 16 |
| 15 // Dump the trace into a string. | 17 // Dump the trace into a string. |
| 16 std::ostringstream os; | 18 std::ostringstream os; |
| 17 trace.OutputToStream(&os); | 19 trace.OutputToStream(&os); |
| 18 std::string backtrace_message = os.str(); | 20 std::string backtrace_message = os.str(); |
| 19 | 21 |
| 20 #if defined(OS_LINUX) && NDEBUG | 22 #if defined(OS_LINUX) && NDEBUG |
| 21 // Stack traces require an extra data table that bloats our binaries, | 23 // Stack traces require an extra data table that bloats our binaries, |
| 22 // so they're turned off for release builds. We stop the test here, | 24 // so they're turned off for release builds. We stop the test here, |
| 23 // at least letting us verify that the calls don't crash. | 25 // at least letting us verify that the calls don't crash. |
| 24 return; | 26 return; |
| 25 #endif // defined(OS_LINUX) && NDEBUG | 27 #endif // defined(OS_LINUX) && NDEBUG |
| 26 | 28 |
| 27 size_t frames_found = 0; | 29 size_t frames_found = 0; |
| 28 trace.Addresses(&frames_found); | 30 trace.Addresses(&frames_found); |
| 29 ASSERT_GE(frames_found, 5u) << | 31 ASSERT_GE(frames_found, 5u) << |
| 30 "No stack frames found. Skipping rest of test."; | 32 "No stack frames found. Skipping rest of test."; |
| 31 | 33 |
| 32 // Check if the output has symbol initialization warning. If it does, fail. | 34 // Check if the output has symbol initialization warning. If it does, fail. |
| 33 ASSERT_EQ(backtrace_message.find("Dumping unresolved backtrace"), | 35 ASSERT_EQ(backtrace_message.find("Dumping unresolved backtrace"), |
| 34 std::string::npos) << | 36 std::string::npos) << |
| 35 "Unable to resolve symbols. Skipping rest of test."; | 37 "Unable to resolve symbols. Skipping rest of test."; |
| 36 | 38 |
| 37 #if 0 | 39 #if defined(OS_MACOSX) && 0 |
| 38 // TODO(ajwong): Disabling checking of symbol resolution since it depends | 40 // Disabled due to -fvisibility=hidden in build config. |
| 39 // on whether or not symbols are present, and there are too many | |
| 40 // configurations to reliably ensure that symbols are findable. | |
| 41 #if defined(OS_MACOSX) | |
| 42 | 41 |
| 43 // Symbol resolution via the backtrace_symbol funciton does not work well | 42 // Symbol resolution via the backtrace_symbol function does not work well |
| 44 // in OsX. | 43 // in OS X. |
| 45 // See this thread: | 44 // See this thread: |
| 46 // | 45 // |
| 47 // http://lists.apple.com/archives/darwin-dev/2009/Mar/msg00111.html | 46 // http://lists.apple.com/archives/darwin-dev/2009/Mar/msg00111.html |
| 48 // | 47 // |
| 49 // Just check instead that we find our way back to the "start" symbol | 48 // Just check instead that we find our way back to the "start" symbol |
| 50 // which should be the first symbol in the trace. | 49 // which should be the first symbol in the trace. |
| 51 // | 50 // |
| 52 // TODO(port): Find a more reliable way to resolve symbols. | 51 // TODO(port): Find a more reliable way to resolve symbols. |
| 53 | 52 |
| 54 // Expect to at least find main. | 53 // Expect to at least find main. |
| 55 EXPECT_TRUE(backtrace_message.find("start") != std::string::npos) | 54 EXPECT_TRUE(backtrace_message.find("start") != std::string::npos) |
| 56 << "Expected to find start in backtrace:\n" | 55 << "Expected to find start in backtrace:\n" |
| 57 << backtrace_message; | 56 << backtrace_message; |
| 58 | 57 |
| 59 #elif defined(__GLIBCXX__) | 58 #elif defined(__GLIBCXX__) |
| 60 | 59 |
| 61 // Expect a demangled symbol. | 60 // Expect a demangled symbol. |
| 62 EXPECT_TRUE(backtrace_message.find("testing::Test::Run()") != | 61 EXPECT_TRUE(backtrace_message.find("testing::Test::Run()") != |
| 63 std::string::npos) | 62 std::string::npos) |
| 64 << "Expected a demangled symbol in backtrace:\n" | 63 << "Expected a demangled symbol in backtrace:\n" |
| 65 << backtrace_message; | 64 << backtrace_message; |
| 66 #else // defined(__GLIBCXX__) | 65 |
| 66 #elif 0 |
| 67 // Disabled on other platforms because of varying buildbot configs; |
| 68 // some lack symbols. |
| 67 | 69 |
| 68 // Expect to at least find main. | 70 // Expect to at least find main. |
| 69 EXPECT_TRUE(backtrace_message.find("main") != std::string::npos) | 71 EXPECT_TRUE(backtrace_message.find("main") != std::string::npos) |
| 70 << "Expected to find main in backtrace:\n" | 72 << "Expected to find main in backtrace:\n" |
| 71 << backtrace_message; | 73 << backtrace_message; |
| 72 | 74 |
| 73 #if defined(OS_WIN) | 75 #if defined(OS_WIN) |
| 74 // MSVC doesn't allow the use of C99's __func__ within C++, so we fake it with | 76 // MSVC doesn't allow the use of C99's __func__ within C++, so we fake it with |
| 75 // MSVC's __FUNCTION__ macro. | 77 // MSVC's __FUNCTION__ macro. |
| 76 #define __func__ __FUNCTION__ | 78 #define __func__ __FUNCTION__ |
| 77 #endif | 79 #endif |
| 78 | 80 |
| 79 // Expect to find this function as well. | 81 // Expect to find this function as well. |
| 80 // Note: This will fail if not linked with -rdynamic (aka -export_dynamic) | 82 // Note: This will fail if not linked with -rdynamic (aka -export_dynamic) |
| 81 EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos) | 83 EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos) |
| 82 << "Expected to find " << __func__ << " in backtrace:\n" | 84 << "Expected to find " << __func__ << " in backtrace:\n" |
| 83 << backtrace_message; | 85 << backtrace_message; |
| 84 | 86 |
| 85 #endif // define(OS_MACOSX) | 87 #endif // define(OS_MACOSX) |
| 86 #endif | |
| 87 } | 88 } |
| OLD | NEW |