2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2019-08-19 15:29:52 -03:00
|
|
|
#include "RemoteObject.h"
|
2019-08-18 05:19:13 -03:00
|
|
|
#include "RemoteObjectGraphModel.h"
|
2019-08-19 15:29:52 -03:00
|
|
|
#include "RemoteObjectPropertyModel.h"
|
|
|
|
|
#include "RemoteProcess.h"
|
2021-02-23 09:44:41 -03:00
|
|
|
#include <AK/URL.h>
|
|
|
|
|
#include <LibDesktop/Launcher.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Application.h>
|
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
2021-04-21 09:34:08 -03:00
|
|
|
#include <LibGUI/Clipboard.h>
|
2020-07-28 07:11:21 -03:00
|
|
|
#include <LibGUI/Menu.h>
|
2021-04-13 11:18:20 -03:00
|
|
|
#include <LibGUI/Menubar.h>
|
2021-01-31 04:24:41 -03:00
|
|
|
#include <LibGUI/MessageBox.h>
|
2020-03-05 11:47:24 -03:00
|
|
|
#include <LibGUI/ModelEditingDelegate.h>
|
2020-07-28 07:11:21 -03:00
|
|
|
#include <LibGUI/ProcessChooser.h>
|
2020-02-06 16:33:02 -03:00
|
|
|
#include <LibGUI/Splitter.h>
|
|
|
|
|
#include <LibGUI/TreeView.h>
|
|
|
|
|
#include <LibGUI/Window.h>
|
2019-08-18 05:19:13 -03:00
|
|
|
#include <stdio.h>
|
2021-05-13 18:26:23 -03:00
|
|
|
#include <unistd.h>
|
2019-08-18 05:19:13 -03:00
|
|
|
|
2020-09-25 16:03:29 -03:00
|
|
|
using namespace Inspector;
|
|
|
|
|
|
2019-08-18 05:19:13 -03:00
|
|
|
[[noreturn]] static void print_usage_and_exit()
|
|
|
|
|
{
|
2020-10-13 13:34:27 -03:00
|
|
|
outln("usage: Inspector <pid>");
|
2019-08-18 05:19:13 -03:00
|
|
|
exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
|
{
|
2021-05-13 18:20:26 -03:00
|
|
|
if (pledge("stdio recvfd sendfd rpath unix", nullptr) < 0) {
|
2020-03-06 05:23:07 -03:00
|
|
|
perror("pledge");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (unveil("/res", "r") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-28 20:35:15 -03:00
|
|
|
if (unveil("/bin", "r") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-06 05:23:07 -03:00
|
|
|
if (unveil("/tmp", "rwc") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-28 07:11:21 -03:00
|
|
|
if (unveil("/proc/all", "r") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2020-03-06 05:23:07 -03:00
|
|
|
|
2020-07-28 07:11:21 -03:00
|
|
|
if (unveil("/etc/passwd", "r") < 0) {
|
|
|
|
|
perror("unveil");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
2019-08-18 05:19:13 -03:00
|
|
|
|
2020-07-28 07:11:21 -03:00
|
|
|
unveil(nullptr, nullptr);
|
2019-08-18 05:19:13 -03:00
|
|
|
|
2021-06-01 13:35:57 -03:00
|
|
|
bool gui_mode = argc != 2;
|
2020-07-28 07:11:21 -03:00
|
|
|
pid_t pid;
|
2020-06-12 16:07:52 -03:00
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
auto app = GUI::Application::construct(argc, argv);
|
2020-07-28 07:11:21 -03:00
|
|
|
auto app_icon = GUI::Icon::default_icon("app-inspector");
|
2021-06-01 13:35:57 -03:00
|
|
|
if (gui_mode) {
|
|
|
|
|
choose_pid:
|
2020-07-28 07:11:21 -03:00
|
|
|
auto process_chooser = GUI::ProcessChooser::construct("Inspector", "Inspect", app_icon.bitmap_for_size(16));
|
|
|
|
|
if (process_chooser->exec() == GUI::Dialog::ExecCancel)
|
|
|
|
|
return 0;
|
|
|
|
|
pid = process_chooser->pid();
|
|
|
|
|
} else {
|
|
|
|
|
auto pid_opt = String(argv[1]).to_int();
|
|
|
|
|
if (!pid_opt.has_value())
|
|
|
|
|
print_usage_and_exit();
|
|
|
|
|
pid = pid_opt.value();
|
|
|
|
|
}
|
2019-08-18 05:19:13 -03:00
|
|
|
|
2020-02-02 11:07:41 -03:00
|
|
|
auto window = GUI::Window::construct();
|
2021-01-31 04:24:41 -03:00
|
|
|
|
2021-04-20 09:40:37 -03:00
|
|
|
if (pid == getpid()) {
|
|
|
|
|
GUI::MessageBox::show(window, "Cannot inspect Inspector itself!", "Error", GUI::MessageBox::Type::Error);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-01 13:35:57 -03:00
|
|
|
RemoteProcess remote_process(pid);
|
|
|
|
|
if (!remote_process.is_inspectable()) {
|
|
|
|
|
GUI::MessageBox::show(window, String::formatted("Process pid={} is not inspectable", remote_process.pid()), "Error", GUI::MessageBox::Type::Error);
|
|
|
|
|
if (gui_mode) {
|
|
|
|
|
goto choose_pid;
|
|
|
|
|
} else {
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!Desktop::Launcher::add_allowed_handler_with_only_specific_urls(
|
|
|
|
|
"/bin/Help",
|
|
|
|
|
{ URL::create_with_file_protocol("/usr/share/man/man1/Inspector.md") })
|
|
|
|
|
|| !Desktop::Launcher::seal_allowlist()) {
|
|
|
|
|
warnln("Failed to set up allowed launch URLs");
|
2021-05-20 02:41:20 -03:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-18 05:19:13 -03:00
|
|
|
window->set_title("Inspector");
|
Misc: Use automatic window positioning in more applications
This is a follow up to #2936 / d3e3b4ae56aa79d9bde12ca1f143dcf116f89a4c.
Affected programs:
- Applications: Browser (Download, View source, Inspect DOM tree, JS
console), Terminal (Settings)
- Demos: Cube, Eyes, Fire, HelloWorld, LibGfxDemo, WebView,
WidgetGallery
- DevTools: HackStudio, Inspector, Profiler
- Games: 2048, Minesweeper, Snake, Solitaire
- Userland: test-web
A few have been left out where manual positioning is done on purpose,
e.g. ClipboardManager (to be close to the menu bar) or VisualBuilder (to
preserve alignment of the multiple application windows).
2020-08-15 12:24:05 -03:00
|
|
|
window->resize(685, 500);
|
2020-07-28 07:11:21 -03:00
|
|
|
window->set_icon(app_icon.bitmap_for_size(16));
|
|
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
auto& file_menu = window->add_menu("&File");
|
2021-05-01 05:45:39 -03:00
|
|
|
file_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
|
2020-07-28 07:11:21 -03:00
|
|
|
|
2021-07-21 16:21:03 -03:00
|
|
|
auto& help_menu = window->add_menu("&Help");
|
2021-02-23 09:44:41 -03:00
|
|
|
help_menu.add_action(GUI::CommonActions::make_help_action([](auto&) {
|
|
|
|
|
Desktop::Launcher::open(URL::create_with_file_protocol("/usr/share/man/man1/Inspector.md"), "/bin/Help");
|
|
|
|
|
}));
|
2021-01-04 19:51:49 -03:00
|
|
|
help_menu.add_action(GUI::CommonActions::make_about_action("Inspector", app_icon, window));
|
2019-08-18 05:19:13 -03:00
|
|
|
|
2020-03-04 05:46:23 -03:00
|
|
|
auto& widget = window->set_main_widget<GUI::Widget>();
|
|
|
|
|
widget.set_fill_with_background_color(true);
|
|
|
|
|
widget.set_layout<GUI::VerticalBoxLayout>();
|
2019-08-18 05:19:13 -03:00
|
|
|
|
2020-03-04 15:07:55 -03:00
|
|
|
auto& splitter = widget.add<GUI::HorizontalSplitter>();
|
2019-08-19 15:29:52 -03:00
|
|
|
|
2019-09-11 16:19:23 -03:00
|
|
|
remote_process.on_update = [&] {
|
|
|
|
|
if (!remote_process.process_name().is_null())
|
2020-10-13 13:34:27 -03:00
|
|
|
window->set_title(String::formatted("{} ({}) - Inspector", remote_process.process_name(), remote_process.pid()));
|
2019-09-11 16:19:23 -03:00
|
|
|
};
|
|
|
|
|
|
2020-03-04 15:07:55 -03:00
|
|
|
auto& tree_view = splitter.add<GUI::TreeView>();
|
|
|
|
|
tree_view.set_model(remote_process.object_graph_model());
|
|
|
|
|
tree_view.set_activates_on_selection(true);
|
2020-12-29 21:23:32 -03:00
|
|
|
tree_view.set_fixed_width(286);
|
2019-08-19 15:29:52 -03:00
|
|
|
|
2020-06-30 15:33:53 -03:00
|
|
|
auto& properties_tree_view = splitter.add<GUI::TreeView>();
|
2021-04-21 09:32:38 -03:00
|
|
|
properties_tree_view.set_should_fill_selected_rows(true);
|
2020-06-30 15:33:53 -03:00
|
|
|
properties_tree_view.set_editable(true);
|
|
|
|
|
properties_tree_view.aid_create_editing_delegate = [](auto&) {
|
2020-03-05 11:47:24 -03:00
|
|
|
return make<GUI::StringModelEditingDelegate>();
|
|
|
|
|
};
|
2019-08-19 15:29:52 -03:00
|
|
|
|
2020-03-04 15:07:55 -03:00
|
|
|
tree_view.on_activation = [&](auto& index) {
|
2019-08-19 15:29:52 -03:00
|
|
|
auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
|
2020-06-30 15:33:53 -03:00
|
|
|
properties_tree_view.set_model(remote_object->property_model());
|
2020-03-05 12:30:27 -03:00
|
|
|
remote_process.set_inspected_object(remote_object->address);
|
2019-08-19 15:29:52 -03:00
|
|
|
};
|
2019-08-18 05:19:13 -03:00
|
|
|
|
2021-04-21 09:34:08 -03:00
|
|
|
auto properties_tree_view_context_menu = GUI::Menu::construct("Properties Tree View");
|
|
|
|
|
|
2021-07-21 13:02:15 -03:00
|
|
|
auto copy_bitmap = Gfx::Bitmap::try_load_from_file("/res/icons/16x16/edit-copy.png");
|
2021-04-21 09:34:08 -03:00
|
|
|
auto copy_property_name_action = GUI::Action::create("Copy Property Name", copy_bitmap, [&](auto&) {
|
|
|
|
|
GUI::Clipboard::the().set_plain_text(properties_tree_view.selection().first().data().to_string());
|
|
|
|
|
});
|
|
|
|
|
auto copy_property_value_action = GUI::Action::create("Copy Property Value", copy_bitmap, [&](auto&) {
|
|
|
|
|
GUI::Clipboard::the().set_plain_text(properties_tree_view.selection().first().sibling_at_column(1).data().to_string());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
properties_tree_view_context_menu->add_action(copy_property_name_action);
|
|
|
|
|
properties_tree_view_context_menu->add_action(copy_property_value_action);
|
|
|
|
|
|
|
|
|
|
properties_tree_view.on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
|
|
|
|
|
if (index.is_valid()) {
|
|
|
|
|
properties_tree_view_context_menu->popup(event.screen_position());
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-08-18 05:19:13 -03:00
|
|
|
window->show();
|
2019-08-19 15:29:52 -03:00
|
|
|
remote_process.update();
|
2020-03-06 05:23:07 -03:00
|
|
|
|
2021-05-13 18:20:26 -03:00
|
|
|
if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
|
2020-03-06 05:23:07 -03:00
|
|
|
perror("pledge");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-04 09:05:19 -03:00
|
|
|
return app->exec();
|
2019-08-18 05:19:13 -03:00
|
|
|
}
|