StackTraceTest.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*
  2. * Copyright 2013-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <cstring>
  17. #include <folly/experimental/TestUtil.h>
  18. #include <folly/experimental/symbolizer/StackTrace.h>
  19. #include <folly/experimental/symbolizer/Symbolizer.h>
  20. #include <glog/logging.h>
  21. #include <folly/portability/GTest.h>
  22. using namespace folly;
  23. using namespace folly::symbolizer;
  24. FOLLY_NOINLINE void foo1();
  25. FOLLY_NOINLINE void foo2();
  26. void verifyStackTraces() {
  27. constexpr size_t kMaxAddresses = 100;
  28. FrameArray<kMaxAddresses> fa;
  29. CHECK(getStackTrace(fa));
  30. FrameArray<kMaxAddresses> faSafe;
  31. CHECK(getStackTraceSafe(faSafe));
  32. CHECK_EQ(fa.frameCount, faSafe.frameCount);
  33. if (VLOG_IS_ON(1)) {
  34. Symbolizer symbolizer;
  35. OStreamSymbolizePrinter printer(std::cerr, SymbolizePrinter::COLOR_IF_TTY);
  36. symbolizer.symbolize(fa);
  37. VLOG(1) << "getStackTrace\n";
  38. printer.println(fa);
  39. symbolizer.symbolize(faSafe);
  40. VLOG(1) << "getStackTraceSafe\n";
  41. printer.println(faSafe);
  42. }
  43. // Other than the top 2 frames (this one and getStackTrace /
  44. // getStackTraceSafe), the stack traces should be identical
  45. for (size_t i = 2; i < fa.frameCount; ++i) {
  46. LOG(INFO) << "i=" << i << " " << std::hex << "0x" << fa.addresses[i]
  47. << " 0x" << faSafe.addresses[i];
  48. EXPECT_EQ(fa.addresses[i], faSafe.addresses[i]);
  49. }
  50. }
  51. void foo1() {
  52. foo2();
  53. }
  54. void foo2() {
  55. verifyStackTraces();
  56. }
  57. volatile bool handled = false;
  58. void handler(int /* num */, siginfo_t* /* info */, void* /* ctx */) {
  59. // Yes, getStackTrace and VLOG aren't async-signal-safe, but signals
  60. // raised with raise() aren't "async" signals.
  61. foo1();
  62. handled = true;
  63. }
  64. TEST(StackTraceTest, Simple) {
  65. foo1();
  66. }
  67. TEST(StackTraceTest, Signal) {
  68. struct sigaction sa;
  69. memset(&sa, 0, sizeof(sa));
  70. sa.sa_sigaction = handler;
  71. sa.sa_flags = SA_RESETHAND | SA_SIGINFO;
  72. CHECK_ERR(sigaction(SIGUSR1, &sa, nullptr));
  73. raise(SIGUSR1);
  74. EXPECT_TRUE(handled);
  75. }
  76. ssize_t read_all(int fd, uint8_t* buffer, size_t size) {
  77. uint8_t* pos = buffer;
  78. ssize_t bytes_read;
  79. do {
  80. bytes_read = read(fd, pos, size);
  81. if (bytes_read < 0) {
  82. if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) {
  83. continue;
  84. }
  85. return bytes_read;
  86. }
  87. pos += bytes_read;
  88. size -= bytes_read;
  89. } while (bytes_read > 0 && size > 0);
  90. return pos - buffer;
  91. }
  92. // Returns the position in the file after done reading.
  93. off_t get_stack_trace(int fd, size_t file_pos, uint8_t* buffer, size_t count) {
  94. off_t rv = lseek(fd, file_pos, SEEK_SET);
  95. CHECK_EQ(rv, (off_t)file_pos);
  96. // Subtract 1 from size of buffer to hold nullptr.
  97. ssize_t bytes_read = read_all(fd, buffer, count - 1);
  98. CHECK_GT(bytes_read, 0);
  99. buffer[bytes_read] = '\0';
  100. return lseek(fd, 0, SEEK_CUR);
  101. }
  102. template <class StackTracePrinter>
  103. void testStackTracePrinter(StackTracePrinter& printer, int fd) {
  104. ASSERT_GT(fd, 0);
  105. printer.printStackTrace(true);
  106. printer.flush();
  107. std::array<uint8_t, 4000> first;
  108. off_t pos = get_stack_trace(fd, 0, first.data(), first.size());
  109. ASSERT_GT(pos, 0);
  110. printer.printStackTrace(true);
  111. printer.flush();
  112. std::array<uint8_t, 4000> second;
  113. get_stack_trace(fd, pos, second.data(), second.size());
  114. // The first two lines refer to this stack frame, which is different in the
  115. // two cases, so strip those off. The rest should be equal.
  116. ASSERT_STREQ(
  117. strchr(strchr((const char*)first.data(), '\n') + 1, '\n') + 1,
  118. strchr(strchr((const char*)second.data(), '\n') + 1, '\n') + 1);
  119. }
  120. TEST(StackTraceTest, SafeStackTracePrinter) {
  121. test::TemporaryFile file;
  122. SafeStackTracePrinter printer{10, file.fd()};
  123. testStackTracePrinter<SafeStackTracePrinter>(printer, file.fd());
  124. }
  125. TEST(StackTraceTest, FastStackTracePrinter) {
  126. test::TemporaryFile file;
  127. FastStackTracePrinter printer{
  128. std::make_unique<FDSymbolizePrinter>(file.fd())};
  129. testStackTracePrinter<FastStackTracePrinter>(printer, file.fd());
  130. }