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
3 // found in the LICENSE file.
9 #include "base/debug/stack_trace.h"
10 #include "base/logging.h"
11 #include "base/process_util.h"
12 #include "base/test/test_timeouts.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "testing/multiprocess_func_list.h"
16 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_IOS)
17 #include "base/test/multiprocess_test.h"
23 #if defined(OS_POSIX) && !defined(OS_ANDROID) && !defined(OS_IOS)
24 typedef MultiProcessTest StackTraceTest
;
26 typedef testing::Test StackTraceTest
;
29 // Note: On Linux, this test currently only fully works on Debug builds.
30 // See comments in the #ifdef soup if you intend to change this.
32 // Always fails on Windows: crbug.com/32070
33 #define MAYBE_OutputToStream DISABLED_OutputToStream
35 #define MAYBE_OutputToStream OutputToStream
37 TEST_F(StackTraceTest
, MAYBE_OutputToStream
) {
40 // Dump the trace into a string.
41 std::ostringstream os
;
42 trace
.OutputToStream(&os
);
43 std::string backtrace_message
= os
.str();
45 // ToString() should produce the same output.
46 EXPECT_EQ(backtrace_message
, trace
.ToString());
48 #if defined(OS_POSIX) && !defined(OS_MACOSX) && NDEBUG
49 // Stack traces require an extra data table that bloats our binaries,
50 // so they're turned off for release builds. We stop the test here,
51 // at least letting us verify that the calls don't crash.
53 #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && NDEBUG
55 size_t frames_found
= 0;
56 trace
.Addresses(&frames_found
);
57 ASSERT_GE(frames_found
, 5u) <<
58 "No stack frames found. Skipping rest of test.";
60 // Check if the output has symbol initialization warning. If it does, fail.
61 ASSERT_EQ(backtrace_message
.find("Dumping unresolved backtrace"),
63 "Unable to resolve symbols. Skipping rest of test.";
65 #if defined(OS_MACOSX)
67 // Disabled due to -fvisibility=hidden in build config.
69 // Symbol resolution via the backtrace_symbol function does not work well
73 // http://lists.apple.com/archives/darwin-dev/2009/Mar/msg00111.html
75 // Just check instead that we find our way back to the "start" symbol
76 // which should be the first symbol in the trace.
78 // TODO(port): Find a more reliable way to resolve symbols.
80 // Expect to at least find main.
81 EXPECT_TRUE(backtrace_message
.find("start") != std::string::npos
)
82 << "Expected to find start in backtrace:\n"
86 #elif defined(USE_SYMBOLIZE)
87 // This branch is for gcc-compiled code, but not Mac due to the
89 // Expect a demangled symbol.
90 EXPECT_TRUE(backtrace_message
.find("testing::Test::Run()") !=
92 << "Expected a demangled symbol in backtrace:\n"
96 // This is the fall-through case; it used to cover Windows.
97 // But it's disabled because of varying buildbot configs;
100 // Expect to at least find main.
101 EXPECT_TRUE(backtrace_message
.find("main") != std::string::npos
)
102 << "Expected to find main in backtrace:\n"
103 << backtrace_message
;
106 // MSVC doesn't allow the use of C99's __func__ within C++, so we fake it with
107 // MSVC's __FUNCTION__ macro.
108 #define __func__ __FUNCTION__
111 // Expect to find this function as well.
112 // Note: This will fail if not linked with -rdynamic (aka -export_dynamic)
113 EXPECT_TRUE(backtrace_message
.find(__func__
) != std::string::npos
)
114 << "Expected to find " << __func__
<< " in backtrace:\n"
115 << backtrace_message
;
117 #endif // define(OS_MACOSX)
120 // The test is used for manual testing, e.g., to see the raw output.
121 TEST_F(StackTraceTest
, DebugOutputToStream
) {
123 std::ostringstream os
;
124 trace
.OutputToStream(&os
);
128 // The test is used for manual testing, e.g., to see the raw output.
129 TEST_F(StackTraceTest
, DebugPrintBacktrace
) {
130 StackTrace().PrintBacktrace();
133 #if defined(OS_POSIX) && !defined(OS_ANDROID)
135 MULTIPROCESS_TEST_MAIN(MismatchedMallocChildProcess
) {
136 char* pointer
= new char[10];
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()));
150 #endif // !defined(OS_IOS)
154 std::string
itoa_r_wrapper(intptr_t i
, size_t sz
, int base
, size_t padding
) {
156 CHECK_LE(sz
, sizeof(buffer
));
158 char* result
= internal::itoa_r(i
, buffer
, sz
, base
, padding
);
160 return std::string(buffer
);
165 TEST_F(StackTraceTest
, itoa_r
) {
166 EXPECT_EQ("0", itoa_r_wrapper(0, 128, 10, 0));
167 EXPECT_EQ("-1", itoa_r_wrapper(-1, 128, 10, 0));
170 if (sizeof(intptr_t) == 4) {
171 EXPECT_EQ("ffffffff", itoa_r_wrapper(-1, 128, 16, 0));
172 EXPECT_EQ("-2147483648",
173 itoa_r_wrapper(std::numeric_limits
<intptr_t>::min(), 128, 10, 0));
174 EXPECT_EQ("2147483647",
175 itoa_r_wrapper(std::numeric_limits
<intptr_t>::max(), 128, 10, 0));
177 EXPECT_EQ("80000000",
178 itoa_r_wrapper(std::numeric_limits
<intptr_t>::min(), 128, 16, 0));
179 EXPECT_EQ("7fffffff",
180 itoa_r_wrapper(std::numeric_limits
<intptr_t>::max(), 128, 16, 0));
181 } else if (sizeof(intptr_t) == 8) {
182 EXPECT_EQ("ffffffffffffffff", itoa_r_wrapper(-1, 128, 16, 0));
183 EXPECT_EQ("-9223372036854775808",
184 itoa_r_wrapper(std::numeric_limits
<intptr_t>::min(), 128, 10, 0));
185 EXPECT_EQ("9223372036854775807",
186 itoa_r_wrapper(std::numeric_limits
<intptr_t>::max(), 128, 10, 0));
188 EXPECT_EQ("8000000000000000",
189 itoa_r_wrapper(std::numeric_limits
<intptr_t>::min(), 128, 16, 0));
190 EXPECT_EQ("7fffffffffffffff",
191 itoa_r_wrapper(std::numeric_limits
<intptr_t>::max(), 128, 16, 0));
193 ADD_FAILURE() << "Missing test case for your size of intptr_t ("
194 << sizeof(intptr_t) << ")";
198 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 0));
199 EXPECT_EQ("deadbeef", itoa_r_wrapper(0xdeadbeef, 128, 16, 0));
201 // Check that itoa_r respects passed buffer size limit.
203 EXPECT_TRUE(internal::itoa_r(0xdeadbeef, buffer
, 10, 16, 0));
204 EXPECT_TRUE(internal::itoa_r(0xdeadbeef, buffer
, 9, 16, 0));
205 EXPECT_FALSE(internal::itoa_r(0xdeadbeef, buffer
, 8, 16, 0));
206 EXPECT_FALSE(internal::itoa_r(0xdeadbeef, buffer
, 7, 16, 0));
207 EXPECT_TRUE(internal::itoa_r(0xbeef, buffer
, 5, 16, 4));
208 EXPECT_FALSE(internal::itoa_r(0xbeef, buffer
, 5, 16, 5));
209 EXPECT_FALSE(internal::itoa_r(0xbeef, buffer
, 5, 16, 6));
212 EXPECT_EQ("1", itoa_r_wrapper(1, 128, 10, 0));
213 EXPECT_EQ("1", itoa_r_wrapper(1, 128, 10, 1));
214 EXPECT_EQ("01", itoa_r_wrapper(1, 128, 10, 2));
215 EXPECT_EQ("001", itoa_r_wrapper(1, 128, 10, 3));
216 EXPECT_EQ("0001", itoa_r_wrapper(1, 128, 10, 4));
217 EXPECT_EQ("00001", itoa_r_wrapper(1, 128, 10, 5));
218 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 0));
219 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 1));
220 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 2));
221 EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 3));
222 EXPECT_EQ("0688", itoa_r_wrapper(0x688, 128, 16, 4));
223 EXPECT_EQ("00688", itoa_r_wrapper(0x688, 128, 16, 5));
225 #endif // defined(OS_POSIX) && !defined(OS_ANDROID)