main.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "RemoteObject.h"
  7. #include "RemoteObjectGraphModel.h"
  8. #include "RemoteObjectPropertyModel.h"
  9. #include "RemoteProcess.h"
  10. #include <AK/URL.h>
  11. #include <LibCore/System.h>
  12. #include <LibDesktop/Launcher.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/BoxLayout.h>
  15. #include <LibGUI/Clipboard.h>
  16. #include <LibGUI/Menu.h>
  17. #include <LibGUI/Menubar.h>
  18. #include <LibGUI/MessageBox.h>
  19. #include <LibGUI/ModelEditingDelegate.h>
  20. #include <LibGUI/ProcessChooser.h>
  21. #include <LibGUI/Splitter.h>
  22. #include <LibGUI/TreeView.h>
  23. #include <LibGUI/Window.h>
  24. #include <LibMain/Main.h>
  25. #include <stdio.h>
  26. #include <unistd.h>
  27. using namespace Inspector;
  28. [[noreturn]] static void print_usage_and_exit()
  29. {
  30. outln("usage: Inspector <pid>");
  31. exit(0);
  32. }
  33. ErrorOr<int> serenity_main(Main::Arguments arguments)
  34. {
  35. TRY(Core::System::pledge("stdio recvfd sendfd rpath unix"));
  36. TRY(Core::System::unveil("/res", "r"));
  37. TRY(Core::System::unveil("/bin", "r"));
  38. TRY(Core::System::unveil("/tmp", "rwc"));
  39. TRY(Core::System::unveil("/proc/all", "r"));
  40. TRY(Core::System::unveil("/etc/passwd", "r"));
  41. TRY(Core::System::unveil(nullptr, nullptr));
  42. bool gui_mode = arguments.argc != 2;
  43. pid_t pid;
  44. auto app = TRY(GUI::Application::try_create(arguments));
  45. auto app_icon = TRY(GUI::Icon::try_create_default_icon("app-inspector"));
  46. if (gui_mode) {
  47. choose_pid:
  48. auto process_chooser = TRY(GUI::ProcessChooser::try_create("Inspector", "Inspect", app_icon.bitmap_for_size(16)));
  49. if (process_chooser->exec() == GUI::Dialog::ExecResult::Cancel)
  50. return 0;
  51. pid = process_chooser->pid();
  52. } else {
  53. auto pid_opt = String(arguments.strings[1]).to_int();
  54. if (!pid_opt.has_value())
  55. print_usage_and_exit();
  56. pid = pid_opt.value();
  57. }
  58. auto window = TRY(GUI::Window::try_create());
  59. if (pid == getpid()) {
  60. GUI::MessageBox::show(window, "Cannot inspect Inspector itself!", "Error", GUI::MessageBox::Type::Error);
  61. return 1;
  62. }
  63. RemoteProcess remote_process(pid);
  64. if (!remote_process.is_inspectable()) {
  65. GUI::MessageBox::show(window, String::formatted("Process pid={} is not inspectable", remote_process.pid()), "Error", GUI::MessageBox::Type::Error);
  66. if (gui_mode) {
  67. goto choose_pid;
  68. } else {
  69. return 1;
  70. }
  71. }
  72. TRY(Desktop::Launcher::add_allowed_handler_with_only_specific_urls("/bin/Help", { URL::create_with_file_protocol("/usr/share/man/man1/Inspector.md") }));
  73. TRY(Desktop::Launcher::seal_allowlist());
  74. window->set_title("Inspector");
  75. window->resize(685, 500);
  76. window->set_icon(app_icon.bitmap_for_size(16));
  77. auto& file_menu = window->add_menu("&File");
  78. file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
  79. auto& help_menu = window->add_menu("&Help");
  80. help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
  81. Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/Inspector.md"), "/bin/Help");
  82. }));
  83. help_menu.add_action(GUI::CommonActions::make_about_action("Inspector", app_icon, window));
  84. auto widget = TRY(window->try_set_main_widget<GUI::Widget>());
  85. widget->set_fill_with_background_color(true);
  86. widget->set_layout<GUI::VerticalBoxLayout>();
  87. auto& splitter = widget->add<GUI::HorizontalSplitter>();
  88. remote_process.on_update = [&] {
  89. if (!remote_process.process_name().is_null())
  90. window->set_title(String::formatted("{} ({}) - Inspector", remote_process.process_name(), remote_process.pid()));
  91. };
  92. auto& tree_view = splitter.add<GUI::TreeView>();
  93. tree_view.set_model(remote_process.object_graph_model());
  94. tree_view.set_activates_on_selection(true);
  95. tree_view.set_fixed_width(286);
  96. auto& properties_tree_view = splitter.add<GUI::TreeView>();
  97. properties_tree_view.set_should_fill_selected_rows(true);
  98. properties_tree_view.set_editable(true);
  99. properties_tree_view.aid_create_editing_delegate = [](auto&) {
  100. return make<GUI::StringModelEditingDelegate>();
  101. };
  102. tree_view.on_activation = [&](auto& index) {
  103. auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
  104. properties_tree_view.set_model(remote_object->property_model());
  105. remote_process.set_inspected_object(remote_object->address);
  106. };
  107. auto properties_tree_view_context_menu = TRY(GUI::Menu::try_create("Properties Tree View"));
  108. auto copy_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-copy.png").release_value_but_fixme_should_propagate_errors();
  109. auto copy_property_name_action = GUI::Action::create("Copy Property Name", copy_bitmap, [&](auto&) {
  110. GUI::Clipboard::the().set_plain_text(properties_tree_view.selection().first().data().to_string());
  111. });
  112. auto copy_property_value_action = GUI::Action::create("Copy Property Value", copy_bitmap, [&](auto&) {
  113. GUI::Clipboard::the().set_plain_text(properties_tree_view.selection().first().sibling_at_column(1).data().to_string());
  114. });
  115. properties_tree_view_context_menu->add_action(copy_property_name_action);
  116. properties_tree_view_context_menu->add_action(copy_property_value_action);
  117. properties_tree_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  118. if (index.is_valid()) {
  119. properties_tree_view_context_menu->popup(event.screen_position());
  120. }
  121. };
  122. window->show();
  123. remote_process.update();
  124. TRY(Core::System::pledge("stdio recvfd sendfd rpath"));
  125. return app->exec();
  126. }