main.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2020-2021, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/LexicalPath.h>
  8. #include <AK/StringBuilder.h>
  9. #include <AK/Types.h>
  10. #include <AK/URL.h>
  11. #include <Applications/CrashReporter/CrashReporterWindowGML.h>
  12. #include <LibC/serenity.h>
  13. #include <LibC/spawn.h>
  14. #include <LibCore/ArgsParser.h>
  15. #include <LibCore/File.h>
  16. #include <LibCore/System.h>
  17. #include <LibCoredump/Backtrace.h>
  18. #include <LibCoredump/Reader.h>
  19. #include <LibDesktop/AppFile.h>
  20. #include <LibDesktop/Launcher.h>
  21. #include <LibELF/Core.h>
  22. #include <LibGUI/Application.h>
  23. #include <LibGUI/BoxLayout.h>
  24. #include <LibGUI/Button.h>
  25. #include <LibGUI/FileIconProvider.h>
  26. #include <LibGUI/Icon.h>
  27. #include <LibGUI/ImageWidget.h>
  28. #include <LibGUI/Label.h>
  29. #include <LibGUI/LinkLabel.h>
  30. #include <LibGUI/Progressbar.h>
  31. #include <LibGUI/TabWidget.h>
  32. #include <LibGUI/TextEditor.h>
  33. #include <LibGUI/Widget.h>
  34. #include <LibGUI/Window.h>
  35. #include <LibMain/Main.h>
  36. #include <string.h>
  37. #include <unistd.h>
  38. struct TitleAndText {
  39. String title;
  40. String text;
  41. };
  42. static NonnullRefPtr<GUI::Window> create_progress_window()
  43. {
  44. auto window = GUI::Window::construct();
  45. window->set_title("CrashReporter");
  46. window->set_resizable(false);
  47. window->resize(240, 64);
  48. window->center_on_screen();
  49. auto& main_widget = window->set_main_widget<GUI::Widget>();
  50. main_widget.set_fill_with_background_color(true);
  51. main_widget.set_layout<GUI::VerticalBoxLayout>();
  52. auto& label = main_widget.add<GUI::Label>("Generating crash report...");
  53. label.set_fixed_height(30);
  54. auto& progressbar = main_widget.add<GUI::Progressbar>();
  55. progressbar.set_name("progressbar");
  56. progressbar.set_fixed_width(150);
  57. progressbar.set_fixed_height(22);
  58. window->on_close = [&]() {
  59. if (progressbar.value() != progressbar.max())
  60. exit(0);
  61. };
  62. return window;
  63. }
  64. static TitleAndText build_backtrace(Coredump::Reader const& coredump, ELF::Core::ThreadInfo const& thread_info, size_t thread_index)
  65. {
  66. // Show a very simple progress window ASAP to make crashing feel more responsive.
  67. // FIXME: This is not the most beautifully factored thing.
  68. auto progress_window = create_progress_window();
  69. progress_window->show();
  70. auto& progressbar = *progress_window->main_widget()->find_descendant_of_type_named<GUI::Progressbar>("progressbar");
  71. auto timer = Core::ElapsedTimer::start_new();
  72. Coredump::Backtrace backtrace(coredump, thread_info, [&](size_t frame_index, size_t frame_count) {
  73. progress_window->set_progress(100.0f * (float)(frame_index + 1) / (float)frame_count);
  74. progressbar.set_value(frame_index + 1);
  75. progressbar.set_max(frame_count);
  76. Core::EventLoop::current().pump(Core::EventLoop::WaitMode::PollForEvents);
  77. });
  78. progress_window->close();
  79. auto metadata = coredump.metadata();
  80. dbgln("Generating backtrace took {} ms", timer.elapsed());
  81. StringBuilder builder;
  82. auto prepend_metadata = [&](auto& key, StringView fmt) {
  83. auto maybe_value = metadata.get(key);
  84. if (!maybe_value.has_value() || maybe_value.value().is_empty())
  85. return;
  86. builder.appendff(fmt, maybe_value.value());
  87. builder.append('\n');
  88. builder.append('\n');
  89. };
  90. if (metadata.contains("assertion"))
  91. prepend_metadata("assertion", "ASSERTION FAILED: {}");
  92. else if (metadata.contains("pledge_violation"))
  93. prepend_metadata("pledge_violation", "Has not pledged {}");
  94. auto fault_address = metadata.get("fault_address");
  95. auto fault_type = metadata.get("fault_type");
  96. auto fault_access = metadata.get("fault_access");
  97. if (fault_address.has_value() && fault_type.has_value() && fault_access.has_value()) {
  98. builder.appendff("{} fault on {} at address {}\n\n", fault_type.value(), fault_access.value(), fault_address.value());
  99. }
  100. auto first_entry = true;
  101. for (auto& entry : backtrace.entries()) {
  102. if (first_entry)
  103. first_entry = false;
  104. else
  105. builder.append('\n');
  106. builder.append(entry.to_string());
  107. }
  108. dbgln("--- Backtrace for thread #{} (TID {}) ---", thread_index, thread_info.tid);
  109. for (auto& entry : backtrace.entries()) {
  110. dbgln("{}", entry.to_string(true));
  111. }
  112. return {
  113. String::formatted("Thread #{} (TID {})", thread_index, thread_info.tid),
  114. builder.build()
  115. };
  116. }
  117. static TitleAndText build_cpu_registers(const ELF::Core::ThreadInfo& thread_info, size_t thread_index)
  118. {
  119. auto& regs = thread_info.regs;
  120. StringBuilder builder;
  121. #if ARCH(I386)
  122. builder.appendff("eax={:p} ebx={:p} ecx={:p} edx={:p}\n", regs.eax, regs.ebx, regs.ecx, regs.edx);
  123. builder.appendff("ebp={:p} esp={:p} esi={:p} edi={:p}\n", regs.ebp, regs.esp, regs.esi, regs.edi);
  124. builder.appendff("eip={:p} eflags={:p}", regs.eip, regs.eflags);
  125. #else
  126. builder.appendff("rax={:p} rbx={:p} rcx={:p} rdx={:p}\n", regs.rax, regs.rbx, regs.rcx, regs.rdx);
  127. builder.appendff("rbp={:p} rsp={:p} rsi={:p} rdi={:p}\n", regs.rbp, regs.rsp, regs.rsi, regs.rdi);
  128. builder.appendff(" r8={:p} r9={:p} r10={:p} r11={:p}\n", regs.r8, regs.r9, regs.r10, regs.r11);
  129. builder.appendff("r12={:p} r13={:p} r14={:p} r15={:p}\n", regs.r12, regs.r13, regs.r14, regs.r15);
  130. builder.appendff("rip={:p} rflags={:p}", regs.rip, regs.rflags);
  131. #endif
  132. return {
  133. String::formatted("Thread #{} (TID {})", thread_index, thread_info.tid),
  134. builder.build()
  135. };
  136. }
  137. static void unlink_coredump(StringView const& coredump_path)
  138. {
  139. if (Core::File::remove(coredump_path, Core::File::RecursionMode::Disallowed, false).is_error())
  140. dbgln("Failed deleting coredump file");
  141. }
  142. ErrorOr<int> serenity_main(Main::Arguments arguments)
  143. {
  144. TRY(Core::System::pledge("stdio recvfd sendfd cpath rpath unix proc exec"));
  145. auto app = TRY(GUI::Application::try_create(arguments));
  146. const char* coredump_path = nullptr;
  147. bool unlink_on_exit = false;
  148. Core::ArgsParser args_parser;
  149. args_parser.set_general_help("Show information from an application crash coredump.");
  150. args_parser.add_positional_argument(coredump_path, "Coredump path", "coredump-path");
  151. args_parser.add_option(unlink_on_exit, "Delete the coredump after its parsed", "unlink", 0);
  152. args_parser.parse(arguments);
  153. Vector<TitleAndText> thread_backtraces;
  154. Vector<TitleAndText> thread_cpu_registers;
  155. String executable_path;
  156. Vector<String> crashed_process_arguments;
  157. Vector<String> environment;
  158. Vector<String> memory_regions;
  159. int pid { 0 };
  160. u8 termination_signal { 0 };
  161. {
  162. auto coredump = Coredump::Reader::create(coredump_path);
  163. if (!coredump) {
  164. warnln("Could not open coredump '{}'", coredump_path);
  165. return 1;
  166. }
  167. size_t thread_index = 0;
  168. coredump->for_each_thread_info([&](auto& thread_info) {
  169. thread_backtraces.append(build_backtrace(*coredump, thread_info, thread_index));
  170. thread_cpu_registers.append(build_cpu_registers(thread_info, thread_index));
  171. ++thread_index;
  172. return IterationDecision::Continue;
  173. });
  174. coredump->for_each_memory_region_info([&](auto& memory_region_info) {
  175. memory_regions.append(String::formatted("{:p} - {:p}: {}", memory_region_info.region_start, memory_region_info.region_end, (const char*)memory_region_info.region_name));
  176. return IterationDecision::Continue;
  177. });
  178. executable_path = coredump->process_executable_path();
  179. crashed_process_arguments = coredump->process_arguments();
  180. environment = coredump->process_environment();
  181. pid = coredump->process_pid();
  182. termination_signal = coredump->process_termination_signal();
  183. }
  184. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix cpath proc exec"));
  185. TRY(Core::System::unveil(executable_path.characters(), "r"));
  186. TRY(Core::System::unveil("/res", "r"));
  187. TRY(Core::System::unveil("/tmp/portal/launch", "rw"));
  188. if (unlink_on_exit)
  189. TRY(Core::System::unveil(coredump_path, "c"));
  190. TRY(Core::System::unveil("/bin/HackStudio", "rx"));
  191. TRY(Core::System::unveil(nullptr, nullptr));
  192. auto app_icon = GUI::Icon::default_icon("app-crash-reporter");
  193. auto window = TRY(GUI::Window::try_create());
  194. window->set_title("Crash Reporter");
  195. window->set_icon(app_icon.bitmap_for_size(16));
  196. window->resize(460, 340);
  197. window->center_on_screen();
  198. window->on_close = [unlink_on_exit, &coredump_path]() {
  199. if (unlink_on_exit)
  200. unlink_coredump(coredump_path);
  201. };
  202. auto widget = TRY(window->try_set_main_widget<GUI::Widget>());
  203. widget->load_from_gml(crash_reporter_window_gml);
  204. auto& icon_image_widget = *widget->find_descendant_of_type_named<GUI::ImageWidget>("icon");
  205. icon_image_widget.set_bitmap(GUI::FileIconProvider::icon_for_executable(executable_path).bitmap_for_size(32));
  206. auto app_name = LexicalPath::basename(executable_path);
  207. auto af = Desktop::AppFile::get_for_app(app_name);
  208. if (af->is_valid())
  209. app_name = af->name();
  210. auto& description_label = *widget->find_descendant_of_type_named<GUI::Label>("description");
  211. description_label.set_text(String::formatted("\"{}\" (PID {}) has crashed - {} (signal {})", app_name, pid, strsignal(termination_signal), termination_signal));
  212. auto& executable_link_label = *widget->find_descendant_of_type_named<GUI::LinkLabel>("executable_link");
  213. executable_link_label.set_text(LexicalPath::canonicalized_path(executable_path));
  214. executable_link_label.on_click = [&] {
  215. LexicalPath path { executable_path };
  216. Desktop::Launcher::open(URL::create_with_file_protocol(path.dirname(), path.basename()));
  217. };
  218. auto& coredump_link_label = *widget->find_descendant_of_type_named<GUI::LinkLabel>("coredump_link");
  219. coredump_link_label.set_text(LexicalPath::canonicalized_path(coredump_path));
  220. coredump_link_label.on_click = [&] {
  221. LexicalPath path { coredump_path };
  222. Desktop::Launcher::open(URL::create_with_file_protocol(path.dirname(), path.basename()));
  223. };
  224. auto& arguments_label = *widget->find_descendant_of_type_named<GUI::Label>("arguments_label");
  225. arguments_label.set_text(String::join(" ", crashed_process_arguments));
  226. auto& tab_widget = *widget->find_descendant_of_type_named<GUI::TabWidget>("tab_widget");
  227. auto backtrace_tab = TRY(tab_widget.try_add_tab<GUI::Widget>("Backtrace"));
  228. TRY(backtrace_tab->try_set_layout<GUI::VerticalBoxLayout>());
  229. backtrace_tab->layout()->set_margins(4);
  230. auto backtrace_label = TRY(backtrace_tab->try_add<GUI::Label>("A backtrace for each thread alive during the crash is listed below:"));
  231. backtrace_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  232. backtrace_label->set_fixed_height(16);
  233. auto backtrace_tab_widget = TRY(backtrace_tab->try_add<GUI::TabWidget>());
  234. backtrace_tab_widget->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  235. for (auto& backtrace : thread_backtraces) {
  236. auto container = TRY(backtrace_tab_widget->try_add_tab<GUI::Widget>(backtrace.title));
  237. TRY(container->try_set_layout<GUI::VerticalBoxLayout>());
  238. container->layout()->set_margins(4);
  239. auto backtrace_text_editor = TRY(container->try_add<GUI::TextEditor>());
  240. backtrace_text_editor->set_text(backtrace.text);
  241. backtrace_text_editor->set_mode(GUI::TextEditor::Mode::ReadOnly);
  242. backtrace_text_editor->set_should_hide_unnecessary_scrollbars(true);
  243. }
  244. auto cpu_registers_tab = TRY(tab_widget.try_add_tab<GUI::Widget>("CPU Registers"));
  245. cpu_registers_tab->set_layout<GUI::VerticalBoxLayout>();
  246. cpu_registers_tab->layout()->set_margins(4);
  247. auto cpu_registers_label = TRY(cpu_registers_tab->try_add<GUI::Label>("The CPU register state for each thread alive during the crash is listed below:"));
  248. cpu_registers_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  249. cpu_registers_label->set_fixed_height(16);
  250. auto cpu_registers_tab_widget = TRY(cpu_registers_tab->try_add<GUI::TabWidget>());
  251. cpu_registers_tab_widget->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  252. for (auto& cpu_registers : thread_cpu_registers) {
  253. auto container = TRY(cpu_registers_tab_widget->try_add_tab<GUI::Widget>(cpu_registers.title));
  254. TRY(container->try_set_layout<GUI::VerticalBoxLayout>());
  255. container->layout()->set_margins(4);
  256. auto cpu_registers_text_editor = TRY(container->try_add<GUI::TextEditor>());
  257. cpu_registers_text_editor->set_text(cpu_registers.text);
  258. cpu_registers_text_editor->set_mode(GUI::TextEditor::Mode::ReadOnly);
  259. cpu_registers_text_editor->set_should_hide_unnecessary_scrollbars(true);
  260. }
  261. auto environment_tab = TRY(tab_widget.try_add_tab<GUI::Widget>("Environment"));
  262. TRY(environment_tab->try_set_layout<GUI::VerticalBoxLayout>());
  263. environment_tab->layout()->set_margins(4);
  264. auto environment_text_editor = TRY(environment_tab->try_add<GUI::TextEditor>());
  265. environment_text_editor->set_text(String::join("\n", environment));
  266. environment_text_editor->set_mode(GUI::TextEditor::Mode::ReadOnly);
  267. environment_text_editor->set_should_hide_unnecessary_scrollbars(true);
  268. auto memory_regions_tab = TRY(tab_widget.try_add_tab<GUI::Widget>("Memory Regions"));
  269. TRY(memory_regions_tab->try_set_layout<GUI::VerticalBoxLayout>());
  270. memory_regions_tab->layout()->set_margins(4);
  271. auto memory_regions_text_editor = TRY(memory_regions_tab->try_add<GUI::TextEditor>());
  272. memory_regions_text_editor->set_text(String::join("\n", memory_regions));
  273. memory_regions_text_editor->set_mode(GUI::TextEditor::Mode::ReadOnly);
  274. memory_regions_text_editor->set_should_hide_unnecessary_scrollbars(true);
  275. memory_regions_text_editor->set_visualize_trailing_whitespace(false);
  276. auto& close_button = *widget->find_descendant_of_type_named<GUI::Button>("close_button");
  277. close_button.on_click = [&](auto) {
  278. if (unlink_on_exit)
  279. unlink_coredump(coredump_path);
  280. app->quit();
  281. };
  282. auto& debug_button = *widget->find_descendant_of_type_named<GUI::Button>("debug_button");
  283. debug_button.set_icon(TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-hack-studio.png")));
  284. debug_button.on_click = [&](int) {
  285. pid_t child;
  286. const char* argv[4] = { "HackStudio", "-c", coredump_path, nullptr };
  287. if ((errno = posix_spawn(&child, "/bin/HackStudio", nullptr, nullptr, const_cast<char**>(argv), environ))) {
  288. perror("posix_spawn");
  289. } else {
  290. if (disown(child) < 0)
  291. perror("disown");
  292. }
  293. };
  294. window->show();
  295. return app->exec();
  296. }