Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
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
27#include "RemoteObject.h"
28#include "RemoteObjectGraphModel.h"
29#include "RemoteObjectPropertyModel.h"
30#include "RemoteProcess.h"
31#include <LibGUI/Application.h>
32#include <LibGUI/BoxLayout.h>
33#include <LibGUI/ModelEditingDelegate.h>
34#include <LibGUI/Splitter.h>
35#include <LibGUI/TableView.h>
36#include <LibGUI/TreeView.h>
37#include <LibGUI/Window.h>
38#include <stdio.h>
39
40[[noreturn]] static void print_usage_and_exit()
41{
42 printf("usage: Inspector <pid>\n");
43 exit(0);
44}
45
46int main(int argc, char** argv)
47{
48 if (pledge("stdio shared_buffer rpath accept unix cpath fattr", nullptr) < 0) {
49 perror("pledge");
50 return 1;
51 }
52
53 if (unveil("/res", "r") < 0) {
54 perror("unveil");
55 return 1;
56 }
57
58 if (unveil("/tmp", "rwc") < 0) {
59 perror("unveil");
60 return 1;
61 }
62
63 unveil(nullptr, nullptr);
64
65 if (argc != 2)
66 print_usage_and_exit();
67
68 bool ok;
69 pid_t pid = String(argv[1]).to_int(ok);
70 if (!ok)
71 print_usage_and_exit();
72
73 GUI::Application app(argc, argv);
74
75 auto window = GUI::Window::construct();
76 window->set_title("Inspector");
77 window->set_rect(150, 150, 300, 500);
78
79 auto& widget = window->set_main_widget<GUI::Widget>();
80 widget.set_fill_with_background_color(true);
81 widget.set_layout<GUI::VerticalBoxLayout>();
82
83 auto& splitter = widget.add<GUI::HorizontalSplitter>();
84
85 RemoteProcess remote_process(pid);
86
87 remote_process.on_update = [&] {
88 if (!remote_process.process_name().is_null())
89 window->set_title(String::format("%s (%d) - Inspector", remote_process.process_name().characters(), remote_process.pid()));
90 };
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
96 auto& properties_table_view = splitter.add<GUI::TableView>();
97 properties_table_view.set_size_columns_to_fit_content(true);
98 properties_table_view.set_editable(true);
99 properties_table_view.aid_create_editing_delegate = [](auto&) {
100 return make<GUI::StringModelEditingDelegate>();
101 };
102
103 tree_view.on_activation = [&](auto& index) {
104 auto* remote_object = static_cast<RemoteObject*>(index.internal_data());
105 properties_table_view.set_model(remote_object->property_model());
106 remote_process.set_inspected_object(remote_object->address);
107 };
108
109 window->show();
110 remote_process.update();
111
112 if (pledge("stdio shared_buffer rpath accept unix", nullptr) < 0) {
113 perror("pledge");
114 return 1;
115 }
116
117 return app.exec();
118}