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 <limits> |
5 #include <sstream> | 6 #include <sstream> |
6 #include <string> | 7 #include <string> |
7 | 8 |
8 #include "base/debug/stack_trace.h" | 9 #include "base/debug/stack_trace.h" |
9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/process_util.h" |
| 12 #include "base/test/multiprocess_test.h" |
| 13 #include "base/test/test_timeouts.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 #include "testing/multiprocess_func_list.h" |
11 | 16 |
12 namespace base { | 17 namespace base { |
13 namespace debug { | 18 namespace debug { |
14 | 19 |
| 20 typedef MultiProcessTest StackTraceTest; |
| 21 |
15 // Note: On Linux, this test currently only fully works on Debug builds. | 22 // 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. | 23 // See comments in the #ifdef soup if you intend to change this. |
17 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
18 // Always fails on Windows: crbug.com/32070 | 25 // Always fails on Windows: crbug.com/32070 |
19 #define MAYBE_OutputToStream DISABLED_OutputToStream | 26 #define MAYBE_OutputToStream DISABLED_OutputToStream |
20 #else | 27 #else |
21 #define MAYBE_OutputToStream OutputToStream | 28 #define MAYBE_OutputToStream OutputToStream |
22 #endif | 29 #endif |
23 TEST(StackTrace, MAYBE_OutputToStream) { | 30 TEST_F(StackTraceTest, MAYBE_OutputToStream) { |
24 StackTrace trace; | 31 StackTrace trace; |
25 | 32 |
26 // Dump the trace into a string. | 33 // Dump the trace into a string. |
27 std::ostringstream os; | 34 std::ostringstream os; |
28 trace.OutputToStream(&os); | 35 trace.OutputToStream(&os); |
29 std::string backtrace_message = os.str(); | 36 std::string backtrace_message = os.str(); |
30 | 37 |
31 // ToString() should produce the same output. | 38 // ToString() should produce the same output. |
32 EXPECT_EQ(backtrace_message, trace.ToString()); | 39 EXPECT_EQ(backtrace_message, trace.ToString()); |
33 | 40 |
(...skipping 28 matching lines...) Expand all Loading... |
62 // which should be the first symbol in the trace. | 69 // which should be the first symbol in the trace. |
63 // | 70 // |
64 // TODO(port): Find a more reliable way to resolve symbols. | 71 // TODO(port): Find a more reliable way to resolve symbols. |
65 | 72 |
66 // Expect to at least find main. | 73 // Expect to at least find main. |
67 EXPECT_TRUE(backtrace_message.find("start") != std::string::npos) | 74 EXPECT_TRUE(backtrace_message.find("start") != std::string::npos) |
68 << "Expected to find start in backtrace:\n" | 75 << "Expected to find start in backtrace:\n" |
69 << backtrace_message; | 76 << backtrace_message; |
70 | 77 |
71 #endif | 78 #endif |
72 #elif defined(__GLIBCXX__) | 79 #elif defined(USE_SYMBOLIZE) |
73 // This branch is for gcc-compiled code, but not Mac due to the | 80 // This branch is for gcc-compiled code, but not Mac due to the |
74 // above #if. | 81 // above #if. |
75 // Expect a demangled symbol. | 82 // Expect a demangled symbol. |
76 EXPECT_TRUE(backtrace_message.find("testing::Test::Run()") != | 83 EXPECT_TRUE(backtrace_message.find("testing::Test::Run()") != |
77 std::string::npos) | 84 std::string::npos) |
78 << "Expected a demangled symbol in backtrace:\n" | 85 << "Expected a demangled symbol in backtrace:\n" |
79 << backtrace_message; | 86 << backtrace_message; |
80 | 87 |
81 #elif 0 | 88 #elif 0 |
82 // This is the fall-through case; it used to cover Windows. | 89 // This is the fall-through case; it used to cover Windows. |
(...skipping 13 matching lines...) Expand all Loading... |
96 | 103 |
97 // Expect to find this function as well. | 104 // Expect to find this function as well. |
98 // Note: This will fail if not linked with -rdynamic (aka -export_dynamic) | 105 // Note: This will fail if not linked with -rdynamic (aka -export_dynamic) |
99 EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos) | 106 EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos) |
100 << "Expected to find " << __func__ << " in backtrace:\n" | 107 << "Expected to find " << __func__ << " in backtrace:\n" |
101 << backtrace_message; | 108 << backtrace_message; |
102 | 109 |
103 #endif // define(OS_MACOSX) | 110 #endif // define(OS_MACOSX) |
104 } | 111 } |
105 | 112 |
| 113 #if defined(OS_POSIX) |
| 114 MULTIPROCESS_TEST_MAIN(MismatchedMallocChildProcess) { |
| 115 char* pointer = new char[10]; |
| 116 delete pointer; |
| 117 return 2; |
| 118 } |
| 119 |
| 120 // Regression test for StackDumpingSignalHandler async-signal unsafety. |
| 121 // Combined with tcmalloc's debugallocation, that signal handler |
| 122 // and e.g. mismatched new[]/delete would cause a hang because |
| 123 // of re-entering malloc. |
| 124 TEST_F(StackTraceTest, AsyncSignalUnsafeSignalHandlerHang) { |
| 125 ProcessHandle child = this->SpawnChild("MismatchedMallocChildProcess", false); |
| 126 ASSERT_NE(kNullProcessHandle, child); |
| 127 ASSERT_TRUE(WaitForSingleProcess(child, TestTimeouts::action_timeout())); |
| 128 } |
| 129 #endif // defined(OS_POSIX) |
| 130 |
106 // The test is used for manual testing, e.g., to see the raw output. | 131 // The test is used for manual testing, e.g., to see the raw output. |
107 TEST(StackTrace, DebugOutputToStream) { | 132 TEST_F(StackTraceTest, DebugOutputToStream) { |
108 StackTrace trace; | 133 StackTrace trace; |
109 std::ostringstream os; | 134 std::ostringstream os; |
110 trace.OutputToStream(&os); | 135 trace.OutputToStream(&os); |
111 VLOG(1) << os.str(); | 136 VLOG(1) << os.str(); |
112 } | 137 } |
113 | 138 |
114 // The test is used for manual testing, e.g., to see the raw output. | 139 // The test is used for manual testing, e.g., to see the raw output. |
115 TEST(StackTrace, DebugPrintBacktrace) { | 140 TEST_F(StackTraceTest, DebugPrintBacktrace) { |
116 StackTrace().PrintBacktrace(); | 141 StackTrace().PrintBacktrace(); |
117 } | 142 } |
118 | 143 |
| 144 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 145 namespace { |
| 146 |
| 147 std::string itoa_r_wrapper(intptr_t i, size_t sz, int base) { |
| 148 char buffer[1024]; |
| 149 CHECK_LE(sz, sizeof(buffer)); |
| 150 |
| 151 char* result = internal::itoa_r(i, buffer, sz, base); |
| 152 EXPECT_TRUE(result); |
| 153 return std::string(buffer); |
| 154 } |
| 155 |
| 156 } // namespace |
| 157 |
| 158 TEST_F(StackTraceTest, itoa_r) { |
| 159 EXPECT_EQ("0", itoa_r_wrapper(0, 128, 10)); |
| 160 EXPECT_EQ("-1", itoa_r_wrapper(-1, 128, 10)); |
| 161 |
| 162 // Test edge cases. |
| 163 if (sizeof(intptr_t) == 4) { |
| 164 EXPECT_EQ("ffffffff", itoa_r_wrapper(-1, 128, 16)); |
| 165 EXPECT_EQ("-2147483648", |
| 166 itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 10)); |
| 167 EXPECT_EQ("2147483647", |
| 168 itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 10)); |
| 169 |
| 170 EXPECT_EQ("80000000", |
| 171 itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 16)); |
| 172 EXPECT_EQ("7fffffff", |
| 173 itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 16)); |
| 174 } else if (sizeof(intptr_t) == 8) { |
| 175 EXPECT_EQ("ffffffffffffffff", itoa_r_wrapper(-1, 128, 16)); |
| 176 EXPECT_EQ("-9223372036854775808", |
| 177 itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 10)); |
| 178 EXPECT_EQ("9223372036854775807", |
| 179 itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 10)); |
| 180 |
| 181 EXPECT_EQ("8000000000000000", |
| 182 itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 16)); |
| 183 EXPECT_EQ("7fffffffffffffff", |
| 184 itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 16)); |
| 185 } else { |
| 186 ADD_FAILURE() << "Missing test case for your size of intptr_t (" |
| 187 << sizeof(intptr_t) << ")"; |
| 188 } |
| 189 |
| 190 // Test hex output. |
| 191 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16)); |
| 192 EXPECT_EQ("deadbeef", itoa_r_wrapper(0xdeadbeef, 128, 16)); |
| 193 |
| 194 // Check that itoa_r respects passed buffer size limit. |
| 195 char buffer[1024]; |
| 196 EXPECT_TRUE(internal::itoa_r(0xdeadbeef, buffer, 10, 16)); |
| 197 EXPECT_TRUE(internal::itoa_r(0xdeadbeef, buffer, 9, 16)); |
| 198 EXPECT_FALSE(internal::itoa_r(0xdeadbeef, buffer, 8, 16)); |
| 199 EXPECT_FALSE(internal::itoa_r(0xdeadbeef, buffer, 7, 16)); |
| 200 } |
| 201 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
| 202 |
119 } // namespace debug | 203 } // namespace debug |
120 } // namespace base | 204 } // namespace base |
OLD | NEW |