main.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. auto coredump = CoreDump::Reader::create(coredump_path);
  58. if (!coredump) {
  59. warnln("Could not open coredump '{}'", coredump_path);
  60. return 1;
  61. }
  62. auto app = GUI::Application::construct(argc, argv);
  63. if (pledge("stdio shared_buffer accept rpath unix", nullptr) < 0) {
  64. perror("pledge");
  65. return 1;
  66. }
  67. auto backtrace = coredump->backtrace();
  68. String executable_path;
  69. // FIXME: Maybe we should just embed the process's executable path
  70. // in the coredump by itself so we don't have to extract it from the backtrace.
  71. // Such a process section could also include the PID, which currently we'd have
  72. // to parse from the filename.
  73. if (!backtrace.entries().is_empty()) {
  74. executable_path = backtrace.entries().last().object_name;
  75. } else {
  76. warnln("Could not determine executable path from coredump");
  77. return 1;
  78. }
  79. if (unveil(executable_path.characters(), "r") < 0) {
  80. perror("unveil");
  81. return 1;
  82. }
  83. if (unveil("/res", "r") < 0) {
  84. perror("unveil");
  85. return 1;
  86. }
  87. if (unveil("/tmp/portal/launch", "rw") < 0) {
  88. perror("unveil");
  89. return 1;
  90. }
  91. if (unveil(nullptr, nullptr) < 0) {
  92. perror("unveil");
  93. return 1;
  94. }
  95. auto app_icon = GUI::Icon::default_icon("app-crash-reporter");
  96. auto window = GUI::Window::construct();
  97. window->set_title("Crash Reporter");
  98. window->set_icon(app_icon.bitmap_for_size(16));
  99. window->set_resizable(false);
  100. window->resize(460, 340);
  101. window->center_on_screen();
  102. auto& widget = window->set_main_widget<GUI::Widget>();
  103. widget.load_from_gml(crash_reporter_window_gml);
  104. auto& icon_image_widget = static_cast<GUI::ImageWidget&>(*widget.find_descendant_by_name("icon"));
  105. icon_image_widget.set_bitmap(GUI::FileIconProvider::icon_for_executable(executable_path).bitmap_for_size(32));
  106. auto app_name = LexicalPath(executable_path).basename();
  107. auto af = Desktop::AppFile::get_for_app(app_name);
  108. if (af->is_valid())
  109. app_name = af->name();
  110. auto& description_label = static_cast<GUI::Label&>(*widget.find_descendant_by_name("description"));
  111. description_label.set_text(String::formatted("\"{}\" has crashed!", app_name));
  112. auto& executable_link_label = static_cast<GUI::LinkLabel&>(*widget.find_descendant_by_name("executable_link"));
  113. executable_link_label.set_text(LexicalPath::canonicalized_path(executable_path));
  114. executable_link_label.on_click = [&] {
  115. Desktop::Launcher::open(URL::create_with_file_protocol(LexicalPath(executable_path).dirname()));
  116. };
  117. auto& coredump_link_label = static_cast<GUI::LinkLabel&>(*widget.find_descendant_by_name("coredump_link"));
  118. coredump_link_label.set_text(LexicalPath::canonicalized_path(coredump_path));
  119. coredump_link_label.on_click = [&] {
  120. Desktop::Launcher::open(URL::create_with_file_protocol(LexicalPath(coredump_path).dirname()));
  121. };
  122. StringBuilder backtrace_builder;
  123. auto first = true;
  124. for (auto& entry : backtrace.entries()) {
  125. if (first)
  126. first = false;
  127. else
  128. backtrace_builder.append('\n');
  129. backtrace_builder.append(entry.to_string());
  130. }
  131. auto& backtrace_text_editor = static_cast<GUI::TextEditor&>(*widget.find_descendant_by_name("backtrace_text_editor"));
  132. backtrace_text_editor.set_text(backtrace_builder.build());
  133. auto& close_button = static_cast<GUI::Button&>(*widget.find_descendant_by_name("close_button"));
  134. close_button.on_click = [&](auto) {
  135. app->quit();
  136. };
  137. window->show();
  138. return app->exec();
  139. }