main.cpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2020, Itamar S. <itamar8910@gmail.com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/LexicalPath.h>
  27. #include <LibCore/DirectoryWatcher.h>
  28. #include <LibCoreDump/Backtrace.h>
  29. #include <LibCoreDump/Reader.h>
  30. #include <serenity.h>
  31. #include <spawn.h>
  32. #include <sys/stat.h>
  33. #include <time.h>
  34. #include <unistd.h>
  35. static void wait_until_coredump_is_ready(const String& coredump_path)
  36. {
  37. while (true) {
  38. struct stat statbuf;
  39. if (stat(coredump_path.characters(), &statbuf) < 0) {
  40. perror("stat");
  41. ASSERT_NOT_REACHED();
  42. }
  43. if (statbuf.st_mode & 0400) // Check if readable
  44. break;
  45. usleep(10000); // sleep for 10ms
  46. }
  47. }
  48. static void print_backtrace(const String& coredump_path)
  49. {
  50. auto coredump = CoreDump::Reader::create(coredump_path);
  51. if (!coredump) {
  52. dbgln("Could not open coredump '{}'", coredump_path);
  53. return;
  54. }
  55. for (auto& entry : coredump->backtrace().entries())
  56. dbgln("{}", entry.to_string(true));
  57. }
  58. static void launch_crash_reporter(const String& coredump_path)
  59. {
  60. pid_t child;
  61. const char* argv[] = { "CrashReporter", coredump_path.characters(), nullptr, nullptr };
  62. if ((errno = posix_spawn(&child, "/bin/CrashReporter", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  63. perror("posix_spawn");
  64. } else {
  65. if (disown(child) < 0)
  66. perror("disown");
  67. }
  68. }
  69. int main()
  70. {
  71. static constexpr const char* coredumps_dir = "/tmp/coredump";
  72. mkdir(coredumps_dir, 0777);
  73. Core::DirectoryWatcher watcher { coredumps_dir };
  74. while (true) {
  75. auto event = watcher.wait_for_event();
  76. ASSERT(event.has_value());
  77. if (event.value().type != Core::DirectoryWatcher::Event::Type::ChildAdded)
  78. continue;
  79. auto coredump_path = event.value().child_path;
  80. dbgln("New coredump file: {}", coredump_path);
  81. wait_until_coredump_is_ready(coredump_path);
  82. print_backtrace(coredump_path);
  83. launch_crash_reporter(coredump_path);
  84. }
  85. }