blob: e8e1e19f2ac2864058fb73a6cbd11d467285a3e4 [file] [log] [blame]
[email protected]b026e35d2010-10-19 02:31:031// Copyright (c) 2010 The Chromium Authors. All rights reserved.
[email protected]96fd0032009-04-24 00:13:082// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <sstream>
6#include <string>
7
[email protected]58580352010-10-26 04:07:508#include "base/debug/stack_trace.h"
[email protected]96fd0032009-04-24 00:13:089#include "base/logging.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
[email protected]58580352010-10-26 04:07:5012namespace base {
13namespace debug {
14
[email protected]ff54b862010-01-12 18:06:4115// Note: On Linux, this test currently only fully works on Debug builds.
[email protected]889da7e42009-12-31 02:28:0916// See comments in the #ifdef soup if you intend to change this.
[email protected]a15115f02010-05-25 20:07:1217// Flaky, crbug.com/32070.
18TEST(StackTrace, FLAKY_OutputToStream) {
[email protected]96fd0032009-04-24 00:13:0819 StackTrace trace;
20
21 // Dump the trace into a string.
22 std::ostringstream os;
23 trace.OutputToStream(&os);
24 std::string backtrace_message = os.str();
25
[email protected]8d578822010-01-25 23:54:5426#if defined(OS_POSIX) && !defined(OS_MACOSX) && NDEBUG
[email protected]0d1c2262009-11-18 18:03:2327 // Stack traces require an extra data table that bloats our binaries,
28 // so they're turned off for release builds. We stop the test here,
29 // at least letting us verify that the calls don't crash.
30 return;
[email protected]8d578822010-01-25 23:54:5431#endif // defined(OS_POSIX) && !defined(OS_MACOSX) && NDEBUG
[email protected]0d1c2262009-11-18 18:03:2332
[email protected]96fd0032009-04-24 00:13:0833 size_t frames_found = 0;
34 trace.Addresses(&frames_found);
[email protected]de1b764f2009-08-24 15:36:4135 ASSERT_GE(frames_found, 5u) <<
36 "No stack frames found. Skipping rest of test.";
[email protected]96fd0032009-04-24 00:13:0837
38 // Check if the output has symbol initialization warning. If it does, fail.
[email protected]de1b764f2009-08-24 15:36:4139 ASSERT_EQ(backtrace_message.find("Dumping unresolved backtrace"),
40 std::string::npos) <<
41 "Unable to resolve symbols. Skipping rest of test.";
[email protected]96fd0032009-04-24 00:13:0842
43#if defined(OS_MACOSX)
[email protected]889da7e42009-12-31 02:28:0944#if 0
45 // Disabled due to -fvisibility=hidden in build config.
[email protected]96fd0032009-04-24 00:13:0846
[email protected]889da7e42009-12-31 02:28:0947 // Symbol resolution via the backtrace_symbol function does not work well
48 // in OS X.
[email protected]de1b764f2009-08-24 15:36:4149 // See this thread:
[email protected]96fd0032009-04-24 00:13:0850 //
51 // http://lists.apple.com/archives/darwin-dev/2009/Mar/msg00111.html
52 //
53 // Just check instead that we find our way back to the "start" symbol
54 // which should be the first symbol in the trace.
55 //
56 // TODO(port): Find a more reliable way to resolve symbols.
57
58 // Expect to at least find main.
59 EXPECT_TRUE(backtrace_message.find("start") != std::string::npos)
60 << "Expected to find start in backtrace:\n"
61 << backtrace_message;
62
[email protected]889da7e42009-12-31 02:28:0963#endif
[email protected]79b6fa62009-10-14 03:01:4464#elif defined(__GLIBCXX__)
[email protected]889da7e42009-12-31 02:28:0965 // This branch is for gcc-compiled code, but not Mac due to the
66 // above #if.
[email protected]79b6fa62009-10-14 03:01:4467 // Expect a demangled symbol.
68 EXPECT_TRUE(backtrace_message.find("testing::Test::Run()") !=
69 std::string::npos)
70 << "Expected a demangled symbol in backtrace:\n"
71 << backtrace_message;
[email protected]889da7e42009-12-31 02:28:0972
73#elif 0
74 // This is the fall-through case; it used to cover Windows.
75 // But it's disabled because of varying buildbot configs;
76 // some lack symbols.
[email protected]96fd0032009-04-24 00:13:0877
78 // Expect to at least find main.
79 EXPECT_TRUE(backtrace_message.find("main") != std::string::npos)
80 << "Expected to find main in backtrace:\n"
81 << backtrace_message;
82
83#if defined(OS_WIN)
84// MSVC doesn't allow the use of C99's __func__ within C++, so we fake it with
85// MSVC's __FUNCTION__ macro.
86#define __func__ __FUNCTION__
87#endif
88
89 // Expect to find this function as well.
90 // Note: This will fail if not linked with -rdynamic (aka -export_dynamic)
91 EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos)
92 << "Expected to find " << __func__ << " in backtrace:\n"
93 << backtrace_message;
94
95#endif // define(OS_MACOSX)
[email protected]96fd0032009-04-24 00:13:0896}
[email protected]48c27f72010-01-26 06:26:2697
[email protected]a15115f02010-05-25 20:07:1298// The test is used for manual testing, e.g., to see the raw output.
99TEST(StackTrace, DebugOutputToStream) {
[email protected]48c27f72010-01-26 06:26:26100 StackTrace trace;
101 std::ostringstream os;
102 trace.OutputToStream(&os);
[email protected]b026e35d2010-10-19 02:31:03103 VLOG(1) << os.str();
[email protected]48c27f72010-01-26 06:26:26104}
105
[email protected]a15115f02010-05-25 20:07:12106// The test is used for manual testing, e.g., to see the raw output.
107TEST(StackTrace, DebugPrintBacktrace) {
[email protected]48c27f72010-01-26 06:26:26108 StackTrace().PrintBacktrace();
109}
[email protected]58580352010-10-26 04:07:50110
111} // namespace debug
112} // namespace base