main.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (c) 2020-2021, 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 <LibELF/CoreDump.h>
  37. #include <LibGUI/Application.h>
  38. #include <LibGUI/BoxLayout.h>
  39. #include <LibGUI/Button.h>
  40. #include <LibGUI/FileIconProvider.h>
  41. #include <LibGUI/Icon.h>
  42. #include <LibGUI/ImageWidget.h>
  43. #include <LibGUI/Label.h>
  44. #include <LibGUI/LinkLabel.h>
  45. #include <LibGUI/TabWidget.h>
  46. #include <LibGUI/TextEditor.h>
  47. #include <LibGUI/Widget.h>
  48. #include <LibGUI/Window.h>
  49. #include <string.h>
  50. struct TitleAndText {
  51. String title;
  52. String text;
  53. };
  54. static TitleAndText build_backtrace(const CoreDump::Reader& coredump, const ELF::Core::ThreadInfo& thread_info, size_t thread_index)
  55. {
  56. CoreDump::Backtrace backtrace(coredump, thread_info);
  57. StringBuilder builder;
  58. auto prepend_assertion = [&] {
  59. auto assertion = coredump.metadata().get("assertion");
  60. if (!assertion.has_value() || assertion.value().is_empty())
  61. return;
  62. builder.append("ASSERTION FAILED: ");
  63. builder.append(assertion.value().characters());
  64. builder.append('\n');
  65. builder.append('\n');
  66. };
  67. auto first_entry = true;
  68. for (auto& entry : backtrace.entries()) {
  69. if (first_entry) {
  70. if (entry.function_name.starts_with("__assertion_failed"))
  71. prepend_assertion();
  72. first_entry = false;
  73. } else {
  74. builder.append('\n');
  75. }
  76. builder.append(entry.to_string());
  77. }
  78. return {
  79. String::formatted("Thread #{} (TID {})", thread_index, thread_info.tid),
  80. builder.build()
  81. };
  82. }
  83. int main(int argc, char** argv)
  84. {
  85. if (pledge("stdio sendfd shared_buffer accept cpath rpath unix fattr", nullptr) < 0) {
  86. perror("pledge");
  87. return 1;
  88. }
  89. const char* coredump_path = nullptr;
  90. Core::ArgsParser args_parser;
  91. args_parser.set_general_help("Show information from an application crash coredump.");
  92. args_parser.add_positional_argument(coredump_path, "Coredump path", "coredump-path");
  93. args_parser.parse(argc, argv);
  94. Vector<TitleAndText> thread_backtraces;
  95. String executable_path;
  96. int pid { 0 };
  97. u8 termination_signal { 0 };
  98. {
  99. auto coredump = CoreDump::Reader::create(coredump_path);
  100. if (!coredump) {
  101. warnln("Could not open coredump '{}'", coredump_path);
  102. return 1;
  103. }
  104. size_t thread_index = 0;
  105. coredump->for_each_thread_info([&](auto& thread_info) {
  106. thread_backtraces.append(build_backtrace(*coredump, thread_info, thread_index));
  107. ++thread_index;
  108. return IterationDecision::Continue;
  109. });
  110. executable_path = coredump->process_executable_path();
  111. pid = coredump->process_pid();
  112. termination_signal = coredump->process_termination_signal();
  113. }
  114. auto app = GUI::Application::construct(argc, argv);
  115. if (pledge("stdio sendfd shared_buffer accept rpath unix", nullptr) < 0) {
  116. perror("pledge");
  117. return 1;
  118. }
  119. if (unveil(executable_path.characters(), "r") < 0) {
  120. perror("unveil");
  121. return 1;
  122. }
  123. if (unveil("/res", "r") < 0) {
  124. perror("unveil");
  125. return 1;
  126. }
  127. if (unveil("/tmp/portal/launch", "rw") < 0) {
  128. perror("unveil");
  129. return 1;
  130. }
  131. if (unveil(nullptr, nullptr) < 0) {
  132. perror("unveil");
  133. return 1;
  134. }
  135. auto app_icon = GUI::Icon::default_icon("app-crash-reporter");
  136. auto window = GUI::Window::construct();
  137. window->set_title("Crash Reporter");
  138. window->set_icon(app_icon.bitmap_for_size(16));
  139. window->resize(460, 340);
  140. window->center_on_screen();
  141. auto& widget = window->set_main_widget<GUI::Widget>();
  142. widget.load_from_gml(crash_reporter_window_gml);
  143. auto& icon_image_widget = *widget.find_descendant_of_type_named<GUI::ImageWidget>("icon");
  144. icon_image_widget.set_bitmap(GUI::FileIconProvider::icon_for_executable(executable_path).bitmap_for_size(32));
  145. auto app_name = LexicalPath(executable_path).basename();
  146. auto af = Desktop::AppFile::get_for_app(app_name);
  147. if (af->is_valid())
  148. app_name = af->name();
  149. auto& description_label = *widget.find_descendant_of_type_named<GUI::Label>("description");
  150. description_label.set_text(String::formatted("\"{}\" (PID {}) has crashed - {} (signal {})", app_name, pid, strsignal(termination_signal), termination_signal));
  151. auto& executable_link_label = *widget.find_descendant_of_type_named<GUI::LinkLabel>("executable_link");
  152. executable_link_label.set_text(LexicalPath::canonicalized_path(executable_path));
  153. executable_link_label.on_click = [&] {
  154. Desktop::Launcher::open(URL::create_with_file_protocol(LexicalPath(executable_path).dirname()));
  155. };
  156. auto& coredump_link_label = *widget.find_descendant_of_type_named<GUI::LinkLabel>("coredump_link");
  157. coredump_link_label.set_text(LexicalPath::canonicalized_path(coredump_path));
  158. coredump_link_label.on_click = [&] {
  159. Desktop::Launcher::open(URL::create_with_file_protocol(LexicalPath(coredump_path).dirname()));
  160. };
  161. auto& tab_widget = *widget.find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  162. auto& backtrace_tab = tab_widget.add_tab<GUI::Widget>("Backtrace");
  163. backtrace_tab.set_layout<GUI::VerticalBoxLayout>();
  164. backtrace_tab.layout()->set_margins({ 4, 4, 4, 4 });
  165. auto& backtrace_label = backtrace_tab.add<GUI::Label>("A backtrace for each thread alive during the crash is listed below:");
  166. backtrace_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  167. backtrace_label.set_fixed_height(16);
  168. auto& backtrace_tab_widget = backtrace_tab.add<GUI::TabWidget>();
  169. backtrace_tab_widget.set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  170. for (auto& backtrace : thread_backtraces) {
  171. auto& backtrace_text_editor = backtrace_tab_widget.add_tab<GUI::TextEditor>(backtrace.title);
  172. backtrace_text_editor.set_layout<GUI::VerticalBoxLayout>();
  173. backtrace_text_editor.layout()->set_margins({ 4, 4, 4, 4 });
  174. backtrace_text_editor.set_text(backtrace.text);
  175. backtrace_text_editor.set_mode(GUI::TextEditor::Mode::ReadOnly);
  176. backtrace_text_editor.set_should_hide_unnecessary_scrollbars(true);
  177. }
  178. auto& close_button = *widget.find_descendant_of_type_named<GUI::Button>("close_button");
  179. close_button.on_click = [&](auto) {
  180. app->quit();
  181. };
  182. window->show();
  183. return app->exec();
  184. }