main.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
  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 <AK/StringBuilder.h>
  28. #include <AK/Types.h>
  29. #include <AK/URL.h>
  30. #include <Applications/CrashReporter/CrashReporterWindowGML.h>
  31. #include <LibCore/ArgsParser.h>
  32. #include <LibCoreDump/Backtrace.h>
  33. #include <LibCoreDump/Reader.h>
  34. #include <LibDesktop/AppFile.h>
  35. #include <LibDesktop/Launcher.h>
  36. #include <LibGUI/Application.h>
  37. #include <LibGUI/Button.h>
  38. #include <LibGUI/FileIconProvider.h>
  39. #include <LibGUI/Icon.h>
  40. #include <LibGUI/ImageWidget.h>
  41. #include <LibGUI/Label.h>
  42. #include <LibGUI/Layout.h>
  43. #include <LibGUI/LinkLabel.h>
  44. #include <LibGUI/TextEditor.h>
  45. #include <LibGUI/Window.h>
  46. int main(int argc, char** argv)
  47. {
  48. if (pledge("stdio shared_buffer accept cpath rpath unix fattr", nullptr) < 0) {
  49. perror("pledge");
  50. return 1;
  51. }
  52. const char* coredump_path = nullptr;
  53. Core::ArgsParser args_parser;
  54. args_parser.set_general_help("Show information from an application crash coredump.");
  55. args_parser.add_positional_argument(coredump_path, "Coredump path", "coredump-path");
  56. args_parser.parse(argc, argv);
  57. Optional<CoreDump::Backtrace> backtrace;
  58. {
  59. auto coredump = CoreDump::Reader::create(coredump_path);
  60. if (!coredump) {
  61. warnln("Could not open coredump '{}'", coredump_path);
  62. return 1;
  63. }
  64. backtrace = coredump->backtrace();
  65. }
  66. auto app = GUI::Application::construct(argc, argv);
  67. if (pledge("stdio shared_buffer accept rpath unix", nullptr) < 0) {
  68. perror("pledge");
  69. return 1;
  70. }
  71. String executable_path;
  72. // FIXME: Maybe we should just embed the process's executable path
  73. // in the coredump by itself so we don't have to extract it from the backtrace.
  74. // Such a process section could also include the PID, which currently we'd have
  75. // to parse from the filename.
  76. if (!backtrace.value().entries().is_empty()) {
  77. executable_path = backtrace.value().entries().last().object_name;
  78. } else {
  79. warnln("Could not determine executable path from coredump");
  80. return 1;
  81. }
  82. if (unveil(executable_path.characters(), "r") < 0) {
  83. perror("unveil");
  84. return 1;
  85. }
  86. if (unveil("/res", "r") < 0) {
  87. perror("unveil");
  88. return 1;
  89. }
  90. if (unveil("/tmp/portal/launch", "rw") < 0) {
  91. perror("unveil");
  92. return 1;
  93. }
  94. if (unveil(nullptr, nullptr) < 0) {
  95. perror("unveil");
  96. return 1;
  97. }
  98. auto app_icon = GUI::Icon::default_icon("app-crash-reporter");
  99. auto window = GUI::Window::construct();
  100. window->set_title("Crash Reporter");
  101. window->set_icon(app_icon.bitmap_for_size(16));
  102. window->set_resizable(false);
  103. window->resize(460, 340);
  104. window->center_on_screen();
  105. auto& widget = window->set_main_widget<GUI::Widget>();
  106. widget.load_from_gml(crash_reporter_window_gml);
  107. auto& icon_image_widget = static_cast<GUI::ImageWidget&>(*widget.find_descendant_by_name("icon"));
  108. icon_image_widget.set_bitmap(GUI::FileIconProvider::icon_for_executable(executable_path).bitmap_for_size(32));
  109. auto app_name = LexicalPath(executable_path).basename();
  110. auto af = Desktop::AppFile::get_for_app(app_name);
  111. if (af->is_valid())
  112. app_name = af->name();
  113. auto& description_label = static_cast<GUI::Label&>(*widget.find_descendant_by_name("description"));
  114. description_label.set_text(String::formatted("\"{}\" has crashed!", app_name));
  115. auto& executable_link_label = static_cast<GUI::LinkLabel&>(*widget.find_descendant_by_name("executable_link"));
  116. executable_link_label.set_text(LexicalPath::canonicalized_path(executable_path));
  117. executable_link_label.on_click = [&] {
  118. Desktop::Launcher::open(URL::create_with_file_protocol(LexicalPath(executable_path).dirname()));
  119. };
  120. auto& coredump_link_label = static_cast<GUI::LinkLabel&>(*widget.find_descendant_by_name("coredump_link"));
  121. coredump_link_label.set_text(LexicalPath::canonicalized_path(coredump_path));
  122. coredump_link_label.on_click = [&] {
  123. Desktop::Launcher::open(URL::create_with_file_protocol(LexicalPath(coredump_path).dirname()));
  124. };
  125. StringBuilder backtrace_builder;
  126. auto first = true;
  127. for (auto& entry : backtrace.value().entries()) {
  128. if (first)
  129. first = false;
  130. else
  131. backtrace_builder.append('\n');
  132. backtrace_builder.append(entry.to_string());
  133. }
  134. auto& backtrace_text_editor = static_cast<GUI::TextEditor&>(*widget.find_descendant_by_name("backtrace_text_editor"));
  135. backtrace_text_editor.set_text(backtrace_builder.build());
  136. auto& close_button = static_cast<GUI::Button&>(*widget.find_descendant_by_name("close_button"));
  137. close_button.on_click = [&](auto) {
  138. app->quit();
  139. };
  140. window->show();
  141. return app->exec();
  142. }