main.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/LexicalPath.h>
  7. #include <AK/StringBuilder.h>
  8. #include <AK/Types.h>
  9. #include <AK/URL.h>
  10. #include <Applications/CrashReporter/CrashReporterWindowGML.h>
  11. #include <LibCore/ArgsParser.h>
  12. #include <LibCore/File.h>
  13. #include <LibCoredump/Backtrace.h>
  14. #include <LibCoredump/Reader.h>
  15. #include <LibDesktop/AppFile.h>
  16. #include <LibDesktop/Launcher.h>
  17. #include <LibELF/Core.h>
  18. #include <LibGUI/Application.h>
  19. #include <LibGUI/BoxLayout.h>
  20. #include <LibGUI/Button.h>
  21. #include <LibGUI/FileIconProvider.h>
  22. #include <LibGUI/Icon.h>
  23. #include <LibGUI/ImageWidget.h>
  24. #include <LibGUI/Label.h>
  25. #include <LibGUI/LinkLabel.h>
  26. #include <LibGUI/TabWidget.h>
  27. #include <LibGUI/TextEditor.h>
  28. #include <LibGUI/Widget.h>
  29. #include <LibGUI/Window.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. struct TitleAndText {
  33. String title;
  34. String text;
  35. };
  36. static TitleAndText build_backtrace(Coredump::Reader const& coredump, ELF::Core::ThreadInfo const& thread_info, size_t thread_index)
  37. {
  38. Coredump::Backtrace backtrace(coredump, thread_info);
  39. auto metadata = coredump.metadata();
  40. StringBuilder builder;
  41. auto prepend_metadata = [&](auto& key, StringView fmt) {
  42. auto maybe_value = metadata.get(key);
  43. if (!maybe_value.has_value() || maybe_value.value().is_empty())
  44. return;
  45. builder.appendff(fmt, maybe_value.value());
  46. builder.append('\n');
  47. builder.append('\n');
  48. };
  49. if (metadata.contains("assertion"))
  50. prepend_metadata("assertion", "ASSERTION FAILED: {}");
  51. else if (metadata.contains("pledge_violation"))
  52. prepend_metadata("pledge_violation", "Has not pledged {}");
  53. auto fault_address = metadata.get("fault_address");
  54. auto fault_type = metadata.get("fault_type");
  55. auto fault_access = metadata.get("fault_access");
  56. if (fault_address.has_value() && fault_type.has_value() && fault_access.has_value()) {
  57. builder.appendff("{} fault on {} at address {}\n\n", fault_type.value(), fault_access.value(), fault_address.value());
  58. }
  59. auto first_entry = true;
  60. for (auto& entry : backtrace.entries()) {
  61. if (first_entry)
  62. first_entry = false;
  63. else
  64. builder.append('\n');
  65. builder.append(entry.to_string());
  66. }
  67. return {
  68. String::formatted("Thread #{} (TID {})", thread_index, thread_info.tid),
  69. builder.build()
  70. };
  71. }
  72. static TitleAndText build_cpu_registers(const ELF::Core::ThreadInfo& thread_info, size_t thread_index)
  73. {
  74. auto& regs = thread_info.regs;
  75. StringBuilder builder;
  76. #if ARCH(I386)
  77. builder.appendff("eax={:p} ebx={:p} ecx={:p} edx={:p}\n", regs.eax, regs.ebx, regs.ecx, regs.edx);
  78. builder.appendff("ebp={:p} esp={:p} esi={:p} edi={:p}\n", regs.ebp, regs.esp, regs.esi, regs.edi);
  79. builder.appendff("eip={:p} eflags={:p}", regs.eip, regs.eflags);
  80. #else
  81. builder.appendff("rax={:p} rbx={:p} rcx={:p} rdx={:p}\n", regs.rax, regs.rbx, regs.rcx, regs.rdx);
  82. builder.appendff("rbp={:p} rsp={:p} rsi={:p} rdi={:p}\n", regs.rbp, regs.rsp, regs.rsi, regs.rdi);
  83. builder.appendff(" r8={:p} r9={:p} r10={:p} r11={:p}\n", regs.r8, regs.r9, regs.r10, regs.r11);
  84. builder.appendff("r12={:p} r13={:p} r14={:p} r15={:p}\n", regs.r12, regs.r13, regs.r14, regs.r15);
  85. builder.appendff("rip={:p} rflags={:p}", regs.rip, regs.rflags);
  86. #endif
  87. return {
  88. String::formatted("Thread #{} (TID {})", thread_index, thread_info.tid),
  89. builder.build()
  90. };
  91. }
  92. int main(int argc, char** argv)
  93. {
  94. if (pledge("stdio recvfd sendfd cpath rpath unix", nullptr) < 0) {
  95. perror("pledge");
  96. return 1;
  97. }
  98. const char* coredump_path = nullptr;
  99. bool unlink_after_use = false;
  100. Core::ArgsParser args_parser;
  101. args_parser.set_general_help("Show information from an application crash coredump.");
  102. args_parser.add_positional_argument(coredump_path, "Coredump path", "coredump-path");
  103. args_parser.add_option(unlink_after_use, "Delete the coredump after its parsed", "unlink", 0);
  104. args_parser.parse(argc, argv);
  105. Vector<TitleAndText> thread_backtraces;
  106. Vector<TitleAndText> thread_cpu_registers;
  107. String executable_path;
  108. Vector<String> arguments;
  109. Vector<String> environment;
  110. int pid { 0 };
  111. u8 termination_signal { 0 };
  112. {
  113. auto coredump = Coredump::Reader::create(coredump_path);
  114. if (!coredump) {
  115. warnln("Could not open coredump '{}'", coredump_path);
  116. return 1;
  117. }
  118. size_t thread_index = 0;
  119. coredump->for_each_thread_info([&](auto& thread_info) {
  120. thread_backtraces.append(build_backtrace(*coredump, thread_info, thread_index));
  121. thread_cpu_registers.append(build_cpu_registers(thread_info, thread_index));
  122. ++thread_index;
  123. return IterationDecision::Continue;
  124. });
  125. executable_path = coredump->process_executable_path();
  126. arguments = coredump->process_arguments();
  127. environment = coredump->process_environment();
  128. pid = coredump->process_pid();
  129. termination_signal = coredump->process_termination_signal();
  130. }
  131. if (unlink_after_use) {
  132. if (Core::File::remove(coredump_path, Core::File::RecursionMode::Disallowed, false).is_error())
  133. dbgln("Failed deleting coredump file");
  134. }
  135. auto app = GUI::Application::construct(argc, argv);
  136. if (pledge("stdio recvfd sendfd rpath unix", nullptr) < 0) {
  137. perror("pledge");
  138. return 1;
  139. }
  140. if (unveil(executable_path.characters(), "r") < 0) {
  141. perror("unveil");
  142. return 1;
  143. }
  144. if (unveil("/res", "r") < 0) {
  145. perror("unveil");
  146. return 1;
  147. }
  148. if (unveil("/tmp/portal/launch", "rw") < 0) {
  149. perror("unveil");
  150. return 1;
  151. }
  152. if (unveil(nullptr, nullptr) < 0) {
  153. perror("unveil");
  154. return 1;
  155. }
  156. auto app_icon = GUI::Icon::default_icon("app-crash-reporter");
  157. auto window = GUI::Window::construct();
  158. window->set_title("Crash Reporter");
  159. window->set_icon(app_icon.bitmap_for_size(16));
  160. window->resize(460, 340);
  161. window->center_on_screen();
  162. auto& widget = window->set_main_widget<GUI::Widget>();
  163. widget.load_from_gml(crash_reporter_window_gml);
  164. auto& icon_image_widget = *widget.find_descendant_of_type_named<GUI::ImageWidget>("icon");
  165. icon_image_widget.set_bitmap(GUI::FileIconProvider::icon_for_executable(executable_path).bitmap_for_size(32));
  166. auto app_name = LexicalPath::basename(executable_path);
  167. auto af = Desktop::AppFile::get_for_app(app_name);
  168. if (af->is_valid())
  169. app_name = af->name();
  170. auto& description_label = *widget.find_descendant_of_type_named<GUI::Label>("description");
  171. description_label.set_text(String::formatted("\"{}\" (PID {}) has crashed - {} (signal {})", app_name, pid, strsignal(termination_signal), termination_signal));
  172. auto& executable_link_label = *widget.find_descendant_of_type_named<GUI::LinkLabel>("executable_link");
  173. executable_link_label.set_text(LexicalPath::canonicalized_path(executable_path));
  174. executable_link_label.on_click = [&] {
  175. LexicalPath path { executable_path };
  176. Desktop::Launcher::open(URL::create_with_file_protocol(path.dirname(), path.basename()));
  177. };
  178. auto& coredump_link_label = *widget.find_descendant_of_type_named<GUI::LinkLabel>("coredump_link");
  179. coredump_link_label.set_text(LexicalPath::canonicalized_path(coredump_path));
  180. coredump_link_label.on_click = [&] {
  181. LexicalPath path { coredump_path };
  182. Desktop::Launcher::open(URL::create_with_file_protocol(path.dirname(), path.basename()));
  183. };
  184. auto& arguments_label = *widget.find_descendant_of_type_named<GUI::Label>("arguments_label");
  185. arguments_label.set_text(String::join(" ", arguments));
  186. auto& tab_widget = *widget.find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  187. auto& backtrace_tab = tab_widget.add_tab<GUI::Widget>("Backtrace");
  188. backtrace_tab.set_layout<GUI::VerticalBoxLayout>();
  189. backtrace_tab.layout()->set_margins(4);
  190. auto& backtrace_label = backtrace_tab.add<GUI::Label>("A backtrace for each thread alive during the crash is listed below:");
  191. backtrace_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  192. backtrace_label.set_fixed_height(16);
  193. auto& backtrace_tab_widget = backtrace_tab.add<GUI::TabWidget>();
  194. backtrace_tab_widget.set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  195. for (auto& backtrace : thread_backtraces) {
  196. auto& backtrace_text_editor = backtrace_tab_widget.add_tab<GUI::TextEditor>(backtrace.title);
  197. backtrace_text_editor.set_layout<GUI::VerticalBoxLayout>();
  198. backtrace_text_editor.layout()->set_margins(4);
  199. backtrace_text_editor.set_text(backtrace.text);
  200. backtrace_text_editor.set_mode(GUI::TextEditor::Mode::ReadOnly);
  201. backtrace_text_editor.set_should_hide_unnecessary_scrollbars(true);
  202. }
  203. auto& cpu_registers_tab = tab_widget.add_tab<GUI::Widget>("CPU Registers");
  204. cpu_registers_tab.set_layout<GUI::VerticalBoxLayout>();
  205. cpu_registers_tab.layout()->set_margins(4);
  206. auto& cpu_registers_label = cpu_registers_tab.add<GUI::Label>("The CPU register state for each thread alive during the crash is listed below:");
  207. cpu_registers_label.set_text_alignment(Gfx::TextAlignment::CenterLeft);
  208. cpu_registers_label.set_fixed_height(16);
  209. auto& cpu_registers_tab_widget = cpu_registers_tab.add<GUI::TabWidget>();
  210. cpu_registers_tab_widget.set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  211. for (auto& cpu_registers : thread_cpu_registers) {
  212. auto& cpu_registers_text_editor = cpu_registers_tab_widget.add_tab<GUI::TextEditor>(cpu_registers.title);
  213. cpu_registers_text_editor.set_layout<GUI::VerticalBoxLayout>();
  214. cpu_registers_text_editor.layout()->set_margins(4);
  215. cpu_registers_text_editor.set_text(cpu_registers.text);
  216. cpu_registers_text_editor.set_mode(GUI::TextEditor::Mode::ReadOnly);
  217. cpu_registers_text_editor.set_should_hide_unnecessary_scrollbars(true);
  218. }
  219. auto& environment_tab = tab_widget.add_tab<GUI::Widget>("Environment");
  220. environment_tab.set_layout<GUI::VerticalBoxLayout>();
  221. environment_tab.layout()->set_margins(4);
  222. auto& environment_text_editor = environment_tab.add<GUI::TextEditor>();
  223. environment_text_editor.set_text(String::join("\n", environment));
  224. environment_text_editor.set_mode(GUI::TextEditor::Mode::ReadOnly);
  225. environment_text_editor.set_should_hide_unnecessary_scrollbars(true);
  226. auto& close_button = *widget.find_descendant_of_type_named<GUI::Button>("close_button");
  227. close_button.on_click = [&](auto) {
  228. app->quit();
  229. };
  230. window->show();
  231. return app->exec();
  232. }