stack_trace_unittest.cc 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.
  4. #include <limits>
  5. #include <sstream>
  6. #include <string>
  7. #include "butil/debug/stack_trace.h"
  8. #include "butil/logging.h"
  9. #include "test/test_timeouts.h"
  10. #include <gtest/gtest.h>
  11. namespace butil {
  12. namespace debug {
  13. typedef testing::Test StackTraceTest;
  14. // Note: On Linux, this test currently only fully works on Debug builds.
  15. // See comments in the #ifdef soup if you intend to change this.
  16. #if defined(OS_WIN)
  17. // Always fails on Windows: crbug.com/32070
  18. #define MAYBE_OutputToStream DISABLED_OutputToStream
  19. #else
  20. #define MAYBE_OutputToStream OutputToStream
  21. #endif
  22. #if !defined(__UCLIBC__)
  23. TEST_F(StackTraceTest, MAYBE_OutputToStream) {
  24. StackTrace trace;
  25. // Dump the trace into a string.
  26. std::ostringstream os;
  27. trace.OutputToStream(&os);
  28. std::string backtrace_message = os.str();
  29. // ToString() should produce the same output.
  30. EXPECT_EQ(backtrace_message, trace.ToString());
  31. #if defined(OS_POSIX) && !defined(OS_MACOSX) && NDEBUG
  32. // Stack traces require an extra data table that bloats our binaries,
  33. // so they're turned off for release builds. We stop the test here,
  34. // at least letting us verify that the calls don't crash.
  35. return;
  36. #endif // defined(OS_POSIX) && !defined(OS_MACOSX) && NDEBUG
  37. size_t frames_found = 0;
  38. trace.Addresses(&frames_found);
  39. ASSERT_GE(frames_found, 5u) <<
  40. "No stack frames found. Skipping rest of test.";
  41. // Check if the output has symbol initialization warning. If it does, fail.
  42. ASSERT_EQ(backtrace_message.find("Dumping unresolved backtrace"),
  43. std::string::npos) <<
  44. "Unable to resolve symbols. Skipping rest of test.";
  45. #if defined(OS_MACOSX)
  46. #if 0
  47. // Disabled due to -fvisibility=hidden in build config.
  48. // Symbol resolution via the backtrace_symbol function does not work well
  49. // in OS X.
  50. // See this thread:
  51. //
  52. // http://lists.apple.com/archives/darwin-dev/2009/Mar/msg00111.html
  53. //
  54. // Just check instead that we find our way back to the "start" symbol
  55. // which should be the first symbol in the trace.
  56. //
  57. // TODO(port): Find a more reliable way to resolve symbols.
  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. #endif
  63. #elif defined(USE_SYMBOLIZE)
  64. // This branch is for gcc-compiled code, but not Mac due to the
  65. // above #if.
  66. // Expect a demangled symbol.
  67. EXPECT_TRUE(backtrace_message.find("testing::Test::Run()") !=
  68. std::string::npos)
  69. << "Expected a demangled symbol in backtrace:\n"
  70. << backtrace_message;
  71. #elif 0
  72. // This is the fall-through case; it used to cover Windows.
  73. // But it's disabled because of varying buildbot configs;
  74. // some lack symbols.
  75. // Expect to at least find main.
  76. EXPECT_TRUE(backtrace_message.find("main") != std::string::npos)
  77. << "Expected to find main in backtrace:\n"
  78. << backtrace_message;
  79. #if defined(OS_WIN)
  80. // MSVC doesn't allow the use of C99's __func__ within C++, so we fake it with
  81. // MSVC's __FUNCTION__ macro.
  82. #define __func__ __FUNCTION__
  83. #endif
  84. // Expect to find this function as well.
  85. // Note: This will fail if not linked with -rdynamic (aka -export_dynamic)
  86. EXPECT_TRUE(backtrace_message.find(__func__) != std::string::npos)
  87. << "Expected to find " << __func__ << " in backtrace:\n"
  88. << backtrace_message;
  89. #endif // define(OS_MACOSX)
  90. }
  91. // The test is used for manual testing, e.g., to see the raw output.
  92. TEST_F(StackTraceTest, DebugOutputToStream) {
  93. StackTrace trace;
  94. std::ostringstream os;
  95. trace.OutputToStream(&os);
  96. VLOG(1) << os.str();
  97. }
  98. // The test is used for manual testing, e.g., to see the raw output.
  99. TEST_F(StackTraceTest, DebugPrintBacktrace) {
  100. StackTrace().Print();
  101. }
  102. #endif // !defined(__UCLIBC__)
  103. #if defined(OS_POSIX) && !defined(OS_ANDROID)
  104. namespace {
  105. std::string itoa_r_wrapper(intptr_t i, size_t sz, int base, size_t padding) {
  106. char buffer[1024];
  107. CHECK_LE(sz, sizeof(buffer));
  108. char* result = internal::itoa_r(i, buffer, sz, base, padding);
  109. EXPECT_TRUE(result);
  110. return std::string(buffer);
  111. }
  112. } // namespace
  113. TEST_F(StackTraceTest, itoa_r) {
  114. EXPECT_EQ("0", itoa_r_wrapper(0, 128, 10, 0));
  115. EXPECT_EQ("-1", itoa_r_wrapper(-1, 128, 10, 0));
  116. // Test edge cases.
  117. if (sizeof(intptr_t) == 4) {
  118. EXPECT_EQ("ffffffff", itoa_r_wrapper(-1, 128, 16, 0));
  119. EXPECT_EQ("-2147483648",
  120. itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 10, 0));
  121. EXPECT_EQ("2147483647",
  122. itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 10, 0));
  123. EXPECT_EQ("80000000",
  124. itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 16, 0));
  125. EXPECT_EQ("7fffffff",
  126. itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 16, 0));
  127. } else if (sizeof(intptr_t) == 8) {
  128. EXPECT_EQ("ffffffffffffffff", itoa_r_wrapper(-1, 128, 16, 0));
  129. EXPECT_EQ("-9223372036854775808",
  130. itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 10, 0));
  131. EXPECT_EQ("9223372036854775807",
  132. itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 10, 0));
  133. EXPECT_EQ("8000000000000000",
  134. itoa_r_wrapper(std::numeric_limits<intptr_t>::min(), 128, 16, 0));
  135. EXPECT_EQ("7fffffffffffffff",
  136. itoa_r_wrapper(std::numeric_limits<intptr_t>::max(), 128, 16, 0));
  137. } else {
  138. ADD_FAILURE() << "Missing test case for your size of intptr_t ("
  139. << sizeof(intptr_t) << ")";
  140. }
  141. // Test hex output.
  142. EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 0));
  143. EXPECT_EQ("deadbeef", itoa_r_wrapper(0xdeadbeef, 128, 16, 0));
  144. // Check that itoa_r respects passed buffer size limit.
  145. char buffer[1024];
  146. EXPECT_TRUE(internal::itoa_r(0xdeadbeef, buffer, 10, 16, 0));
  147. EXPECT_TRUE(internal::itoa_r(0xdeadbeef, buffer, 9, 16, 0));
  148. EXPECT_FALSE(internal::itoa_r(0xdeadbeef, buffer, 8, 16, 0));
  149. EXPECT_FALSE(internal::itoa_r(0xdeadbeef, buffer, 7, 16, 0));
  150. EXPECT_TRUE(internal::itoa_r(0xbeef, buffer, 5, 16, 4));
  151. EXPECT_FALSE(internal::itoa_r(0xbeef, buffer, 5, 16, 5));
  152. EXPECT_FALSE(internal::itoa_r(0xbeef, buffer, 5, 16, 6));
  153. // Test padding.
  154. EXPECT_EQ("1", itoa_r_wrapper(1, 128, 10, 0));
  155. EXPECT_EQ("1", itoa_r_wrapper(1, 128, 10, 1));
  156. EXPECT_EQ("01", itoa_r_wrapper(1, 128, 10, 2));
  157. EXPECT_EQ("001", itoa_r_wrapper(1, 128, 10, 3));
  158. EXPECT_EQ("0001", itoa_r_wrapper(1, 128, 10, 4));
  159. EXPECT_EQ("00001", itoa_r_wrapper(1, 128, 10, 5));
  160. EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 0));
  161. EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 1));
  162. EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 2));
  163. EXPECT_EQ("688", itoa_r_wrapper(0x688, 128, 16, 3));
  164. EXPECT_EQ("0688", itoa_r_wrapper(0x688, 128, 16, 4));
  165. EXPECT_EQ("00688", itoa_r_wrapper(0x688, 128, 16, 5));
  166. }
  167. #endif // defined(OS_POSIX) && !defined(OS_ANDROID)
  168. } // namespace debug
  169. } // namespace butil