| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/stack_trace.h" | 8 #include "base/debug/stack_trace.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/process_util.h" |
| 11 #include "base/test/multiprocess_test.h" |
| 12 #include "base/test/test_timeouts.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
| 14 #include "testing/multiprocess_func_list.h" |
| 11 | 15 |
| 12 namespace base { | 16 namespace base { |
| 13 namespace debug { | 17 namespace debug { |
| 14 | 18 |
| 19 typedef MultiProcessTest StackTraceTest; |
| 20 |
| 15 // Note: On Linux, this test currently only fully works on Debug builds. | 21 // Note: On Linux, this test currently only fully works on Debug builds. |
| 16 // See comments in the #ifdef soup if you intend to change this. | 22 // See comments in the #ifdef soup if you intend to change this. |
| 17 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
| 18 // Always fails on Windows: crbug.com/32070 | 24 // Always fails on Windows: crbug.com/32070 |
| 19 #define MAYBE_OutputToStream DISABLED_OutputToStream | 25 #define MAYBE_OutputToStream DISABLED_OutputToStream |
| 20 #else | 26 #else |
| 21 #define MAYBE_OutputToStream OutputToStream | 27 #define MAYBE_OutputToStream OutputToStream |
| 22 #endif | 28 #endif |
| 23 TEST(StackTrace, MAYBE_OutputToStream) { | 29 TEST_F(StackTraceTest, MAYBE_OutputToStream) { |
| 24 StackTrace trace; | 30 StackTrace trace; |
| 25 | 31 |
| 26 // Dump the trace into a string. | 32 // Dump the trace into a string. |
| 27 std::ostringstream os; | 33 std::ostringstream os; |
| 28 trace.OutputToStream(&os); | 34 trace.OutputToStream(&os); |
| 29 std::string backtrace_message = os.str(); | 35 std::string backtrace_message = os.str(); |
| 30 | 36 |
| 31 // ToString() should produce the same output. | 37 // ToString() should produce the same output. |
| 32 EXPECT_EQ(backtrace_message, trace.ToString()); | 38 EXPECT_EQ(backtrace_message, trace.ToString()); |
| 33 | 39 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 62 // which should be the first symbol in the trace. | 68 // which should be the first symbol in the trace. |
| 63 // | 69 // |
| 64 // TODO(port): Find a more reliable way to resolve symbols. | 70 // TODO(port): Find a more reliable way to resolve symbols. |
| 65 | 71 |
| 66 // Expect to at least find main. | 72 // Expect to at least find main. |
| 67 EXPECT_TRUE(backtrace_message.find("start") != std::string::npos) | 73 EXPECT_TRUE(backtrace_message.find("start") != std::string::npos) |
| 68 << "Expected to find start in backtrace:\n" | 74 << "Expected to find start in backtrace:\n" |
| 69 << backtrace_message; | 75 << backtrace_message; |
| 70 | 76 |
| 71 #endif | 77 #endif |
| 72 #elif defined(__GLIBCXX__) | 78 #elif defined(USE_SYMBOLIZE) |
| 73 // This branch is for gcc-compiled code, but not Mac due to the | 79 // This branch is for gcc-compiled code, but not Mac due to the |
| 74 // above #if. | 80 // above #if. |
| 75 // Expect a demangled symbol. | 81 // Expect a demangled symbol. |
| 76 EXPECT_TRUE(backtrace_message.find("testing::Test::Run()") != | 82 EXPECT_TRUE(backtrace_message.find("testing::Test::Run()") != |
| 77 std::string::npos) | 83 std::string::npos) |
| 78 << "Expected a demangled symbol in backtrace:\n" | 84 << "Expected a demangled symbol in backtrace:\n" |
| 79 << backtrace_message; | 85 << backtrace_message; |
| 80 | 86 |
| 81 #elif 0 | 87 #elif 0 |
| 82 // This is the fall-through case; it used to cover Windows. | 88 // This is the fall-through case; it used to cover Windows. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 96 | 102 |
| 97 // Expect to find this function as well. | 103 // Expect to find this function as well. |
| 98 // Note: This will fail if not linked with -rdynamic (aka -export_dynamic) | 104 // Note: This will fail if not linked with -rdynamic (aka -export_dynamic) |
| 99 EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos) | 105 EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos) |
| 100 << "Expected to find " << __func__ << " in backtrace:\n" | 106 << "Expected to find " << __func__ << " in backtrace:\n" |
| 101 << backtrace_message; | 107 << backtrace_message; |
| 102 | 108 |
| 103 #endif // define(OS_MACOSX) | 109 #endif // define(OS_MACOSX) |
| 104 } | 110 } |
| 105 | 111 |
| 112 MULTIPROCESS_TEST_MAIN(MismatchedMallocChildProcess) { |
| 113 char* pointer = new char[10]; |
| 114 delete pointer; |
| 115 return 2; |
| 116 } |
| 117 |
| 118 // Regression test for StackDumpingSignalHandler async-signal unsafety. |
| 119 // Combined with tcmalloc's debugallocation, that signal handler |
| 120 // and e.g. mismatched new[]/delete would cause a hang because |
| 121 // of re-entering malloc. |
| 122 TEST_F(StackTraceTest, AsyncSignalUnsafeSignalHandlerHang) { |
| 123 ProcessHandle child = this->SpawnChild("MismatchedMallocChildProcess", false); |
| 124 ASSERT_NE(kNullProcessHandle, child); |
| 125 ASSERT_TRUE(WaitForSingleProcess(child, TestTimeouts::action_timeout())); |
| 126 } |
| 127 |
| 106 // The test is used for manual testing, e.g., to see the raw output. | 128 // The test is used for manual testing, e.g., to see the raw output. |
| 107 TEST(StackTrace, DebugOutputToStream) { | 129 TEST_F(StackTraceTest, DebugOutputToStream) { |
| 108 StackTrace trace; | 130 StackTrace trace; |
| 109 std::ostringstream os; | 131 std::ostringstream os; |
| 110 trace.OutputToStream(&os); | 132 trace.OutputToStream(&os); |
| 111 VLOG(1) << os.str(); | 133 VLOG(1) << os.str(); |
| 112 } | 134 } |
| 113 | 135 |
| 114 // The test is used for manual testing, e.g., to see the raw output. | 136 // The test is used for manual testing, e.g., to see the raw output. |
| 115 TEST(StackTrace, DebugPrintBacktrace) { | 137 TEST_F(StackTraceTest, DebugPrintBacktrace) { |
| 116 StackTrace().PrintBacktrace(); | 138 StackTrace().PrintBacktrace(); |
| 117 } | 139 } |
| 118 | 140 |
| 119 } // namespace debug | 141 } // namespace debug |
| 120 } // namespace base | 142 } // namespace base |
| OLD | NEW |