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