SignalHandlerTest.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 <folly/experimental/symbolizer/test/SignalHandlerTest.h>
  17. #include <folly/experimental/symbolizer/SignalHandler.h>
  18. #include <folly/CPortability.h>
  19. #include <folly/FileUtil.h>
  20. #include <folly/Range.h>
  21. #include <folly/portability/GTest.h>
  22. namespace folly {
  23. namespace symbolizer {
  24. namespace test {
  25. namespace {
  26. void print(StringPiece sp) {
  27. writeFull(STDERR_FILENO, sp.data(), sp.size());
  28. }
  29. void callback1() {
  30. print("Callback1\n");
  31. }
  32. void callback2() {
  33. print("Callback2\n");
  34. }
  35. } // namespace
  36. TEST(SignalHandler, Simple) {
  37. addFatalSignalCallback(callback1);
  38. addFatalSignalCallback(callback2);
  39. installFatalSignalHandler();
  40. installFatalSignalCallbacks();
  41. EXPECT_DEATH(
  42. failHard(),
  43. "^\\*\\*\\* Aborted at [0-9]+ \\(Unix time, try 'date -d @[0-9]+'\\) "
  44. "\\*\\*\\*\n"
  45. "\\*\\*\\* Signal 11 \\(SIGSEGV\\) \\(0x2a\\) received by PID [0-9]+ "
  46. "\\(pthread TID 0x[0-9a-f]+\\) \\(linux TID [0-9]+\\) "
  47. "\\(maybe from PID [0-9]+, UID [0-9]+\\) "
  48. "\\(code: address not mapped to object\\), "
  49. "stack trace: \\*\\*\\*\n"
  50. ".*\n"
  51. ".* @ [0-9a-f]+.* folly::symbolizer::test::SignalHandler_Simple_Test"
  52. "::TestBody\\(\\).*\n"
  53. ".*\n"
  54. ".* @ [0-9a-f]+.* main.*\n"
  55. ".*\n"
  56. "Callback1\n"
  57. "Callback2\n"
  58. ".*");
  59. }
  60. } // namespace test
  61. } // namespace symbolizer
  62. } // namespace folly
  63. // Can't use initFacebookLight since that would install its own signal handlers
  64. // Can't use initFacebookNoSignals since we cannot depend on common
  65. int main(int argc, char** argv) {
  66. ::testing::InitGoogleTest(&argc, argv);
  67. return RUN_ALL_TESTS();
  68. }