stack_trace_unittest.cc 6.9 KB

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